home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / Misc / TRSICAT.LZX / CATS_CD2_TRSI / Reference_Library / Devices / modules / getbitmap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-21  |  3.2 KB  |  137 lines

  1.  
  2. /*----------------------------------------------------------------------*
  3.  * GETBITMAP.C  Support routines for reading ILBM files.
  4.  * (IFF is Interchange Format File.)
  5.  *
  6.  * Based on code by Jerry Morrison and Steve Shaw, Electronic Arts.
  7.  * This software is in the public domain.
  8.  * Modified for iffparse.library by CBM 04/90
  9.  * This version for the Commodore-Amiga computer.
  10.  *----------------------------------------------------------------------*/
  11.  
  12. #include "iffp/ilbm.h"
  13. #include "iffp/packer.h"
  14. #include "iffp/ilbmapp.h"
  15.  
  16. /* createbrush
  17.  *
  18.  * Passed an initialized ILBMInfo with a parsed IFFHandle (chunks parsed,
  19.  * stopped at BODY),
  20.  * gets the bitmap and colors
  21.  * Sets up ilbm->brbitmap, ilbm->colortable, ilbm->ncolors
  22.  * Returns 0 for success
  23.  */
  24. LONG createbrush(struct ILBMInfo *ilbm)
  25.     {
  26.     int error;
  27.  
  28.     error             = getbitmap(ilbm);
  29.     if(!error) error     = loadbody(ilbm->ParseInfo.iff,
  30.                         ilbm->brbitmap,&ilbm->Bmhd);
  31.     if(!error)         getcolors(ilbm);
  32.     if(error)          deletebrush(ilbm);
  33.     return(error);
  34.     }
  35.  
  36. /* deletebrush
  37.  *
  38.  * closes and deallocates created brush bitmap and colors
  39.  */
  40. void deletebrush(ilbm)
  41. struct ILBMInfo        *ilbm;
  42.     {
  43.     freebitmap(ilbm);
  44.     freecolors(ilbm);
  45.     }
  46.  
  47.  
  48. /* getbitmap
  49.  *
  50.  * Passed an initialized ILBMInfo with parsed IFFHandle (chunks parsed,
  51.  * stopped at BODY), allocates a BitMap structure and planes just large
  52.  * enough for the BODY.  Generally used for brushes but may be used
  53.  * to load backgrounds without displaying, or to load deep ILBM's.
  54.  * Sets ilbm->brbitmap.  Returns 0 for success.
  55.  */
  56. LONG getbitmap(struct ILBMInfo *ilbm)
  57.     {
  58.     struct IFFHandle    *iff;
  59.     BitMapHeader    *bmhd;
  60.     USHORT            wide, high;
  61.     LONG  error = NULL;
  62.     int k, extra=0;
  63.     BYTE deep;
  64.  
  65.     if(!(iff=ilbm->ParseInfo.iff))    return(CLIENT_ERROR);
  66.  
  67.     ilbm->brbitmap = NULL;
  68.  
  69.     if (!( bmhd = (BitMapHeader *)
  70.             findpropdata(iff, ID_ILBM, ID_BMHD)))
  71.         {
  72.         message("No ILBM.BMHD chunk!\n");
  73.         return(IFFERR_SYNTAX);
  74.         }
  75.  
  76.     *(&ilbm->Bmhd) = *bmhd;    /* copy contents of BMHD */
  77.  
  78.     wide = BitsPerRow(bmhd->w);
  79.     high = bmhd->h;
  80.     deep = bmhd->nPlanes;
  81.  
  82.     ilbm->camg = getcamg(ilbm);
  83.  
  84.     D(bug("allocbitmap: bmhd=$%lx wide=%ld high=%ld deep=%ld\n",
  85.             bmhd,wide,high,deep));
  86.     /*
  87.      * Allocate Bitmap and planes
  88.      */
  89.         extra = deep > 8 ? deep - 8 : 0;
  90.     if(ilbm->brbitmap = AllocMem(sizeof(struct BitMap)+(extra<<2),MEMF_CLEAR))
  91.         {
  92.         InitBitMap(ilbm->brbitmap,deep,wide,high);
  93.         for(k=0; k<deep && (!error); k++) 
  94.             {
  95.             if(!(ilbm->brbitmap->Planes[k] = AllocRaster(wide,high))) 
  96.                         error = 1;
  97.             if(! error)
  98.             BltClear(ilbm->brbitmap->Planes[k],RASSIZE(wide,high),0);
  99.             }
  100.  
  101.         if(error)
  102.             {
  103.             message("Failed to allocate raster\n");
  104.             freebitmap(ilbm);
  105.             }
  106.         }
  107.     else error = 1;
  108.     return(error);
  109.     }
  110.  
  111.     
  112. /* freebitmap
  113.  *
  114.  * deallocates ilbm->brbitmap BitMap structure and planes 
  115.  */
  116. void freebitmap(struct ILBMInfo * ilbm)
  117.     {
  118.     int k, extra=0;
  119.  
  120.     if(ilbm->brbitmap)
  121.         {    
  122.         for(k=0; k< ilbm->brbitmap->Depth; k++) 
  123.             {
  124.             if(ilbm->brbitmap->Planes[k]) 
  125.                 FreeRaster(ilbm->brbitmap->Planes[k],
  126.                     (USHORT)(ilbm->brbitmap->BytesPerRow << 3),
  127.                     ilbm->brbitmap->Rows);
  128.             }
  129.  
  130.             extra = ilbm->brbitmap->Depth > 8 ? ilbm->brbitmap->Depth - 8 : 0;
  131.         FreeMem(ilbm->brbitmap,sizeof(struct BitMap) + (extra << 2));
  132.         ilbm->brbitmap = NULL;
  133.         }
  134.     }
  135.  
  136. /* end */
  137.