home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d5xx / d553 / 24bittools.lha / 24BitTools / Pro2IFF / readilbm.c < prev    next >
C/C++ Source or Header  |  1991-10-28  |  7KB  |  234 lines

  1. #include <functions.h>
  2. #include "iff.h"
  3.  
  4. extern void *IntuitionBase,*GfxBase,*DiskfontBase;
  5.  
  6. /* READILBM.C : based on NewZAP's READILBM.C
  7.                 (c) 1986,1990,1991 Dallas J. Hodgson
  8.  
  9.                 Loads bitmaps into MEMF_PUBLIC
  10.  */
  11.  
  12. LoadMode Mode;
  13. unsigned char *pdiskptr;
  14.  
  15. /************************************************************************
  16. *                                                                       *
  17. *    Routine name(s) : ReadILBM()                                       *
  18. *    Author          : D. John Hodgson                                  *
  19. *    Environment     : Aztec "C", default                               *
  20. *                                                                       *
  21. *    ReadILBM attempts to read an IFF file into an user-supplied        *
  22. *    bitmap. Returns FALSE if successful. Brushes supported.            *
  23. *                                                                       *
  24. *    OPTIONS : Filespec reflects user-supplied pointer to IFF file in   *
  25. *              RAM if mode argument is non-zero. Otherwise, disk.       *
  26. *                                                                       *
  27. *    LIMITATIONS : Bare-bones ILBM; no CATS/LISTS/PROPS.                *
  28. *                  All other graphics (compressed or not) supported.    *
  29. ************************************************************************/
  30.  
  31. BPTR OpenNEW(unsigned char *name,long accessMode)
  32. {
  33.   if (Mode==DISK_MODE) return(Open((char *)name,accessMode));
  34.  
  35.   pdiskptr=name; /* initialize pseudo-disk RAM ptr */ 
  36.   return((BPTR)pdiskptr);
  37. }
  38.  
  39. long ReadNEW(BPTR file,char *buffer,long length)
  40. {
  41.   if (Mode==DISK_MODE) return(Read(file,buffer,length));
  42.  
  43.   movmem(pdiskptr,buffer,(unsigned int)length);
  44.   pdiskptr+=length;
  45.  
  46.   return(length);
  47. }
  48.  
  49. long SeekNEW(BPTR file,long position,long tmode)
  50. {
  51.   if (Mode==DISK_MODE) return(Seek(file,position,tmode));
  52.  
  53.   /* Note : RAM seek only supports one mode : OFFSET_CURRENT! */
  54.   if (tmode!=OFFSET_CURRENT) return(BAD_MODE);
  55.     
  56.   pdiskptr+=position;
  57.  
  58.   return((long)pdiskptr-position); /* Seek() returns previous position! */
  59.  
  60. void CloseNEW(BPTR file)
  61. {
  62.   if (Mode==DISK_MODE) Close(file);
  63. }
  64.  
  65. ReadILBM(char *fspec,LoadMode mode,struct PicMap *picmap)
  66. {
  67.   BPTR fp;
  68.   Chunk header;
  69.   BitMapHeader bmhd;
  70.   unsigned char *tmp,*bufstart,*sourcebuf;
  71.   UWORD i,color;
  72.   long id;
  73.  
  74.   Mode=mode; 
  75.   memset(picmap,0,sizeof(*picmap));
  76.  
  77.   if (!(fp=OpenNEW((unsigned char *)fspec,MODE_OLDFILE))) return(DISK_ERR);
  78.  
  79.   SafeRead(fp,&header,(long)sizeof(header));
  80.   if (header.ckID!=ID_FORM) { CloseNEW(fp); return(IFF_ERR); }
  81.  
  82.   SafeRead(fp,&id,(long)sizeof(id));
  83.   if (id!=ID_ILBM) { CloseNEW(fp); return(IFF_ERR); }
  84.  
  85.   for (;;) {
  86.     SafeRead(fp,&header,(long)sizeof(header));
  87.  
  88.     if (header.ckID==ID_BODY) break;
  89.  
  90.     switch(header.ckID) {
  91.       case ID_BMHD: SafeRead(fp,&bmhd,(long)sizeof(bmhd));
  92.  
  93.                     /* force WORD boundary for all pics */
  94.                     bmhd.w=(bmhd.w + 0xf) & 0xfff0;
  95.  
  96.                     break;
  97.  
  98.       case ID_CMAP: tmp=picmap->palette;
  99.                     SafeRead(fp,tmp,(long)header.ckSize);
  100.  
  101.                     for (i=0;i<header.ckSize/3;i++) {
  102.  
  103.                       color = (*tmp++ & 0xf0) << 4;
  104.                       color |= (*tmp++ & 0xf0);
  105.                       color |= *tmp++ >> 4;
  106.  
  107.                       picmap->colormap[i]=color;
  108.                     }
  109.  
  110.                     break;    
  111.  
  112.       case ID_CAMG: SafeRead(fp,&picmap->ViewModes,(long)header.ckSize);
  113.                     break;
  114.  
  115.       default:      SeekNEW(fp,ROUNDODDUP(header.ckSize),OFFSET_CURRENT);
  116.     }
  117.   }
  118.  
  119.   /* make some forced assumptions if CAMG chunk unavailable */
  120.  
  121.   if (!picmap->ViewModes) {
  122.     if (bmhd.w>LOWIDTH) picmap->ViewModes=HIRES;
  123.     if (bmhd.h>LOHEIGHT) picmap->ViewModes|=LACE;
  124.   }
  125.  
  126.   /* Read planes into RAM for ease of decompression */
  127.  
  128.   if (Mode==RAM_MODE) sourcebuf=bufstart=pdiskptr;  /* memory-resident IFF? */
  129.   else { /* disk-resident IFF? */
  130.     if (!(sourcebuf=bufstart=AllocMem((long)header.ckSize,MEMF_PUBLIC)))
  131.        return(OUT_OF_MEM);
  132.     SafeRead(fp,sourcebuf,(long)header.ckSize); CloseNEW(fp);  
  133.   }
  134.  
  135.   InitBitMap(&picmap->BitMap,(long)bmhd.nPlanes,(long)bmhd.w,(long)bmhd.h);
  136.  
  137.   /* note that we're not necessarily allocating CHIP_MEM here! */
  138.  
  139.   for (i=0;i<bmhd.nPlanes;i++)
  140.     if (!(picmap->BitMap.Planes[i]=AllocMem(RASSIZE(bmhd.w,bmhd.h),MEMF_PUBLIC))) {
  141.       FreeBitMap(&picmap->BitMap);
  142.       FreeMem(bufstart,(long)header.ckSize);
  143.       return(OUT_OF_MEM);
  144.     }
  145.  
  146.   i=Expand(&bmhd,bufstart,&picmap->BitMap);
  147.  
  148.   if (Mode==DISK_MODE) FreeMem(bufstart,(long)header.ckSize);
  149.  
  150.   return(i); /* error if unpacking prob */
  151. }      
  152.  
  153. void FreeBitMap(struct BitMap *bitmap)
  154. {
  155.   short i;
  156.  
  157.   /* take into acct partially allocated maps (due to error) */
  158.  
  159.   for (i=0;i<bitmap->Depth;i++) {
  160.     if (bitmap->Planes[i]) FreeMem(
  161.        bitmap->Planes[i],bitmap->BytesPerRow*bitmap->Rows);
  162.   }
  163.  
  164.   memset(bitmap,0,sizeof(bitmap)); /* prevent dbl-dealloc errs */
  165. }
  166.  
  167. /* Fast line decompress/deinterleave : NEW VERSION */
  168.  
  169. Expand(BitMapHeader *bmhd,register unsigned char *sourcebuf,struct BitMap *bitmap)
  170. {
  171.   register char n,*destbuf; /* in order of preferred allocation */
  172.   register short plane,rowlen,rowbytes,i;
  173.   short planes=bmhd->nPlanes,maskplane = -1;
  174.  
  175.   /* This routine now supports mask (stencil) planes and brushes. The only
  176.      thing it doesn't support is "trimming" the right edge of brushes to
  177.      the background color if they're not exactly modulo 16 wide. DPaint III
  178.      doesn't bother either, if you try to load a brush as a screen. */
  179.  
  180.   rowlen=((bmhd->w +15) & 0xFFF0) >> 3; /* round widths up to word boundary */
  181.  
  182.   /* For deinterleaving purposes, decompress (but don't store)
  183.      any mask planes (if present). */
  184.  
  185.   if (bmhd->masking==mskHasMask) {
  186.     maskplane=planes;
  187.     planes++;
  188.   }
  189.  
  190.   for (i=0;i<bmhd->h;i++) /* process n lines/screen */
  191.     for (plane=0;plane<planes;plane++) { /* process n planes/line */
  192.  
  193.       if (plane!=maskplane) destbuf=(char *)(bitmap->Planes[plane])+rowlen*i;
  194.  
  195.       if (bmhd->compression==cmpByteRun1) { /* compressed screen? */
  196.         rowbytes=rowlen;
  197.  
  198.         while (rowbytes>0) { /* unpack until 1 scan-line complete */
  199.           n=*sourcebuf++; /* fetch block run marker */
  200.  
  201.           /* uncompressed block? copy n bytes verbatim */
  202.           if (n>=0) {
  203.             ++n;
  204.             if (plane!=maskplane) {
  205.               movmem(sourcebuf,destbuf,(unsigned int)n);
  206.               destbuf+=n;
  207.             }
  208.             rowbytes-=n; sourcebuf+=n;
  209.           }
  210.           else { /* compressed block? expand n duplicate bytes */
  211.             n=-n+1;
  212.             if (plane!=maskplane) {
  213.               memset(destbuf,(int)(*sourcebuf),(int)n);
  214.               destbuf+=n;
  215.             }
  216.             rowbytes -=n; sourcebuf++;
  217.           }
  218.  
  219.         } /* finish unpacking line */
  220.         if (rowbytes) return(EXPAND_ERR); /* shouldn't be any left over! */
  221.       }
  222.       else { /* uncompressed? just copy */
  223.         if (plane!=maskplane) {
  224.           movmem(sourcebuf,destbuf,(unsigned int)rowlen);
  225.           destbuf+=rowlen;
  226.         }
  227.         sourcebuf+=rowlen;
  228.       }
  229.     } /* finish interleaved planes, lines */
  230.  
  231.   return(0); /* good result! */
  232. }
  233.