home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1993 #2 / Image.iso / graphics / acksrc.zip / ACKIFF.C < prev    next >
Text File  |  1993-06-15  |  5KB  |  185 lines

  1. // Sample IFF File Reader
  2. //
  3. // This function will return a pointer to a buffer that holds the raw image.
  4. // just free the pointer to delete this buffer. After returning, the array
  5. // colordat will hold the adjusted palette of this pic.
  6. //
  7. // Also, this has been modified to only read in form PBM brushes. form ILBM's
  8. // (the "old" type) are not supported. use the "new" deluxe paint .lbm type
  9. // and do not choose "old".
  10.  
  11. #include <stdio.h>
  12. #include <conio.h>
  13. #include <process.h>
  14. #include <bios.h>
  15. #include <fcntl.h>
  16. #include <malloc.h>
  17. #include <mem.h>
  18. #include "ack3d.h"
  19. #include "ackext.h"
  20. #include "iff.h"
  21.  
  22.  
  23. unsigned char far  colordat[768];  /* maximum it can be...256 colors    */
  24.  
  25. unsigned char far  cplanes[8][80]; /* setting max at 640 pixels width    */
  26.                    /* thats 8 pixels per byte per plane */
  27. unsigned char far  *pplanes= (char far *) &cplanes[0][0];  /* for a form pbm        */
  28.  
  29. unsigned char far * Readiff(char *picname)
  30.    {
  31.    FILE *pic;
  32.    form_chunk    fchunk;
  33.    ChunkHeader    chunk;
  34.    BitMapHeader bmhd;
  35.    long length;
  36.    char value;       // must remain signed, no matter what. ignore any warnings.
  37.    int sofar;
  38.    int width,height,planes;
  39.    int pixw;
  40.    unsigned char far *destx, *savedestx;
  41.  
  42.    if ((pic = fopen(picname,"r+b"))== NULL)
  43.     {
  44.     ErrorCode = ERR_BADPICNAME;
  45.     return(0L);
  46.     }
  47.  
  48.    fread(&fchunk,1,sizeof(form_chunk),pic); /* read in the first 12 bytes*/
  49.  
  50.    if (fchunk.type != FORM)
  51.       {
  52.       fclose(pic);
  53.       ErrorCode = ERR_INVALIDFORM;
  54.       return(0L);
  55.       }
  56.  
  57.    if (fchunk.subtype != ID_PBM)
  58.       {
  59.       printf("Error: Not form PBM!");
  60.       fclose(pic);
  61.       ErrorCode = ERR_NOPBM;
  62.       return(0L);
  63.       }
  64.    // now lets loop...Because the Chunks can be in any order!
  65.    while(1)
  66.       {
  67.       fread(&chunk,1,sizeof(ChunkHeader),pic);
  68.       ByteFlipLong(&(long)(chunk.ckSize));
  69.       if (chunk.ckSize & 1) chunk.ckSize ++;    // must be word aligned
  70.       if(chunk.ckID == ID_BMHD)
  71.       {
  72.       fread(&bmhd,1,sizeof(BitMapHeader),pic);
  73.       bmhd.w=swab(bmhd.w);            // the only things we need.
  74.       bmhd.h=swab(bmhd.h);
  75.       destx = (unsigned char far *)malloc((bmhd.w * bmhd.h)+4);
  76.       if ( !destx )
  77.         {
  78.         fclose(pic);
  79.         ErrorCode = ERR_NOMEMORY;
  80.         return(0L);
  81.         }
  82.  
  83.       savedestx = destx;
  84.  
  85.       destx[0] = bmhd.w%256;
  86.       destx[1] = bmhd.w/256;
  87.       destx[2] = bmhd.h%256;
  88.       destx[3] = bmhd.h/256;
  89.       destx += 4;
  90.       continue;
  91.       }
  92.       if(chunk.ckID == ID_CMAP)
  93.       {
  94.       int i;
  95.       unsigned char r,g;
  96.  
  97.       fread(colordat,1,chunk.ckSize,pic);
  98.       for (i=0;i<768;i++)
  99.           {
  100.            r = colordat[i];      // r,g do not stand for red and green
  101.           g = r >> 2;
  102.           colordat[i] = g;
  103.           }
  104.       continue;
  105.       }
  106.       if(chunk.ckID == ID_BODY)
  107.       {
  108.       for(height = 0; height<bmhd.h; height ++)
  109.          {
  110.          unsigned char *dest;
  111.          dest = (unsigned char *)&(pplanes[0]); /* point at first char  */
  112.          sofar = bmhd.w;                /* number of bytes = 8  */
  113.          if (sofar&1) sofar++;
  114.          while (sofar)
  115.         {
  116.         if (bmhd.compression)
  117.            {
  118.            value=fgetc(pic);      /* get the next byte      */
  119.            // if (value == 128) continue; /* NOP..just get another*/
  120.            if (value > 0)
  121.               {
  122.               int len;
  123.               len = value +1;
  124.               sofar -= len;
  125.               if(!(fread(dest,len,1,pic)))
  126.               {
  127.               fclose(pic);
  128.               ErrorCode = ERR_BADPICFILE;
  129.               free(savedestx);
  130.               return(0L);
  131.               }
  132.               dest +=len;
  133.               }
  134.            else
  135.               {
  136.               int count;
  137.               count = -value; /* get amount to dup */
  138.               count ++;
  139.               sofar -= count;
  140.               value=fgetc(pic);
  141.               while (--count >= 0) *dest++ = value;
  142.               }
  143.            }
  144.         else
  145.            {
  146.            fread(dest,sofar,1,pic); /* just throw on plane */
  147.            sofar = 0;
  148.            }
  149.         }
  150.          if (sofar < 0)
  151.         {
  152.         fclose(pic);
  153.         }
  154.          _fmemcpy(destx,pplanes,bmhd.w);
  155.          destx += bmhd.w;
  156.          }
  157.       break; /* leave if we've unpacked the BODY*/
  158.       }
  159.       fseek(pic,chunk.ckSize,SEEK_CUR);
  160.       }
  161.  
  162.    fclose(pic);
  163.    return((char far *)savedestx);
  164.    }
  165.  
  166. void ByteFlipLong(long *NUMBER)
  167.    {
  168.    // Hey, I didn;t write this function!!!
  169.    long int Y, T;
  170.    int I;
  171.  
  172.    T = *NUMBER;
  173.    Y=0;for (I=0;I<4;I++){Y = Y | (T & 0xFF);if (I<3) {Y = Y << 8;T = T >> 8;}}
  174.    *NUMBER = Y;
  175.    }
  176.  
  177. int swab(unsigned short number)
  178.    {
  179.    unsigned int xx1,xx2;
  180.    unsigned int result;
  181.  
  182.    xx1 = number <<8; xx2 = number >>8; result = xx1|xx2;
  183.    return(result);
  184.    }
  185.