home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 2 / FFMCD02.bin / new / gfx / edit / tsmorph / getbitmap.c < prev    next >
C/C++ Source or Header  |  1993-12-21  |  4KB  |  143 lines

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