home *** CD-ROM | disk | FTP | other *** search
/ Megahits 4 / MegaHits_Vol.4.iso / mui / dev / gui / myimagegclass / source / ilbm_read.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-24  |  5.1 KB  |  192 lines

  1. //-----------------------------------------------------------------------------
  2. // ILBM_read.c - makes a color font hack 8 out of an iff picture
  3. // 08 Jan 1994
  4. // R. Reed
  5. // $Id: ILBM_read.c,v 1.1 94/01/11 03:02:19 rick Exp Locker: rick $
  6. //-----------------------------------------------------------------------------
  7.  
  8. #include <exec/memory.h>
  9. #include <libraries/iffparse.h>
  10. #include <proto/exec.h>
  11. #include <proto/dos.h>
  12. #include <proto/iffparse.h>
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include <string.h>
  16.  
  17. #define MAX_PLANES   8
  18.  
  19. struct BitMapHeader
  20. {
  21.     UWORD w,h;
  22.     WORD  x,y;
  23.     UBYTE nPlanes;
  24.     UBYTE masking;
  25.     UBYTE compression;
  26.     UBYTE pad1;
  27.     UWORD transparentColor;
  28.     UBYTE xAspect,yAspect;
  29.     WORD  pageWidth,pageHeight;
  30. };
  31.  
  32. // STATIC GLOBALS --------------------------------------------------------------
  33. static WORD width, height; // these pass info from bitmap loader to image loader
  34. static UBYTE depth;
  35.  
  36. // PROGRAM ---------------------------------------------------------------------
  37.  
  38. static BOOL DecompressLine(struct IFFHandle *iff, UBYTE *buffer, UWORD byteWidth)
  39. {
  40.    BYTE data, *endPos;
  41.  
  42.    endPos = buffer + byteWidth;
  43.  
  44.    do
  45.    {
  46.       if (ReadChunkBytes(iff, &data, 1) < 0)  break;
  47.       if ((data >= 0) && (data <= 127))
  48.       {
  49.          if (ReadChunkBytes(iff, buffer, data+1) < 0)  break;
  50.          buffer += data+1;
  51.       }
  52.       else if ((data >= -127) && (data <= -1))
  53.       {
  54.          register short i;
  55.          BYTE repeatByte;
  56.  
  57.          if (ReadChunkBytes(iff, &repeatByte, 1) < 0)  break;
  58.          for (i = 0; i < -data+1; i++, buffer++)
  59.             *buffer = repeatByte;
  60.       }
  61.    }
  62.    while (buffer < endPos);
  63.    return((BOOL)((buffer < endPos)?FALSE:TRUE));
  64. }
  65.  
  66. static BOOL DecodeBody(struct IFFHandle *iff, struct BitMap *bm, UBYTE compression)
  67. {
  68.    PLANEPTR rowPtr[MAX_PLANES];
  69.    BOOL ok;
  70.    register short row, p;
  71.  
  72.    memcpy(rowPtr, bm->Planes, sizeof(PLANEPTR)*bm->Depth);
  73.  
  74.    if (compression)
  75.       for (row = 0; row < bm->Rows; row++)
  76.       {
  77.          for (p = 0; p < bm->Depth; rowPtr[p++]+=bm->BytesPerRow)
  78.             if (!(ok = DecompressLine(iff, rowPtr[p], bm->BytesPerRow)))
  79.                break;
  80.          if (!ok)  break;
  81.       }
  82.    else
  83.       for (row = 0; row < bm->Rows; row++)
  84.       {
  85.          for (p = 0; p < bm->Depth; rowPtr[p++]+=bm->BytesPerRow)
  86.             if (!(ok = ReadChunkBytes(iff, rowPtr[p], bm->BytesPerRow)))
  87.                break;
  88.          if (!ok)  break;
  89.       }
  90.    return(ok);
  91. }
  92.  
  93. static BOOL ProcessCMAP(struct IFFHandle *iff, UWORD *colorTable)
  94. {
  95.    struct StoredProperty *sp;
  96.  
  97.    if (sp = FindProp(iff, 'ILBM', 'CMAP'))
  98.    {
  99.       struct RGB {UBYTE r,g,b;} *color;
  100.       register short i, maxColors;
  101.  
  102.       color = (struct RGB *)sp->sp_Data;
  103.       maxColors = sp->sp_Size/3;
  104.  
  105.       for (i = 0; i < maxColors; i++, color++,colorTable++)
  106.       {
  107.          color->r >>= 4;  color->g >>= 4;  color->b >>= 4;
  108.          *colorTable = (color->r<<8)|(color->g<<4)|color->b;
  109.       }
  110.       return(TRUE);
  111.    }
  112.    else
  113.       return(FALSE);
  114. }
  115.  
  116. WORD ReadILBM2BitMap(char *name, struct BitMap *bm, UWORD *colorTable)
  117. {
  118.    struct IFFHandle *iff;
  119.    struct BitMapHeader *bmhd;
  120.    WORD error = 0;
  121.  
  122.    if ((iff = AllocIFF()) && (iff->iff_Stream = Open(name, MODE_OLDFILE)))
  123.    {
  124.       InitIFFasDOS(iff);
  125.       if (!OpenIFF(iff, IFFF_READ))
  126.       {
  127.          PropChunk(iff, 'ILBM', 'BMHD');
  128.          PropChunk(iff, 'ILBM', 'CMAP');
  129.          StopChunk(iff, 'ILBM', 'BODY');
  130.          if (!ParseIFF(iff, IFFPARSE_SCAN))
  131.          {
  132.             struct StoredProperty *sp;
  133.  
  134.             if (sp = FindProp(iff, 'ILBM', 'BMHD'))
  135.             {
  136.                PLANEPTR planeStart;
  137.                ULONG planeSize;
  138.  
  139.                bmhd = (struct BitMapHeader *)sp->sp_Data;
  140.                width = bmhd->w;  height = bmhd->h;  depth = bmhd->nPlanes;
  141.                InitBitMap(bm, bmhd->nPlanes,bmhd->w,bmhd->h);
  142.                planeSize = RASSIZE(bmhd->w,bmhd->h);
  143.                if (planeStart = (PLANEPTR)AllocVec(planeSize*bmhd->nPlanes,MEMF_CHIP))
  144.                {
  145.                   register short i;
  146.  
  147.                   for (i = 0; i < bmhd->nPlanes; i++, planeStart += planeSize)
  148.                      bm->Planes[i] = planeStart;
  149.  
  150.                   if (colorTable)
  151.                      ProcessCMAP(iff, colorTable);
  152.  
  153.                   error = DecodeBody(iff, bm, bmhd->compression)?0:IFFERR_MANGLED;
  154.                }
  155.                else
  156.                   error = IFFERR_NOMEM;
  157.             }
  158.             else
  159.                error = IFFERR_SYNTAX;
  160.          }
  161.          else
  162.             error = IFFERR_READ;
  163.  
  164.          CloseIFF(iff);
  165.       }
  166.       else
  167.          error = IFFERR_READ;
  168.  
  169.       Close(iff->iff_Stream);
  170.    }
  171.    return(error);
  172. }
  173.  
  174. WORD ReadILBM2Image(char *name, struct Image *image, UWORD *colorTable)
  175. {
  176.    struct BitMap bitMap;
  177.    WORD error;
  178.  
  179.    if (!(error = ReadILBM2BitMap(name, &bitMap, colorTable)))
  180.    {
  181.       image->LeftEdge = image->TopEdge = 0;
  182.       image->Width = width;
  183.       image->Height = height;
  184.       image->Depth = depth;
  185.       image->ImageData = (UWORD *)bitMap.Planes[0];
  186.       image->PlanePick = (0xff>>(MAX_PLANES-depth));
  187.       image->PlaneOnOff = 0;
  188.       image->NextImage = NULL;
  189.    }
  190.    return(error);
  191. }
  192.