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

  1. /* saveilbm.c 05/91  C. Scheppner CBM
  2.  *
  3.  * High-level ILBM save routines
  4.  */
  5.  
  6. #include "iffp/ilbm.h"
  7. #include "iffp/ilbmapp.h"
  8.  
  9. extern struct Library *GfxBase;
  10.  
  11. /* screensave.c
  12.  *
  13.  * Given an ILBMInfo with a  currently available (not in use)
  14.  *   ParseInfo->iff IFFHandle, a screen pointer, filename, and
  15.  *   optional chunklist, will save screen as an ILBM
  16.  * The struct Chunk *chunklist1 and 2 are for chunks you wish written
  17.  * out other than BMHD, CMAP, and CAMG (they will be screened out
  18.  * because they are computed and written separately).
  19.  *
  20.  * Note -  screensave passes NULL for transparent color and mask
  21.  *
  22.  * Returns 0 for success or an IFFERR (libraries/iffparse.h)
  23.  */
  24. LONG screensave(struct ILBMInfo *ilbm,
  25.             struct Screen *scr,
  26.             struct Chunk *chunklist1, struct Chunk *chunklist2,
  27.             UBYTE *filename)
  28. {
  29. extern struct Library *GfxBase;
  30. UWORD *colortable, count;
  31. ULONG modeid;
  32. LONG error;
  33. int k;
  34.  
  35.     if(GfxBase->lib_Version >= 36)
  36.     modeid=GetVPModeID(&scr->ViewPort);
  37.     else
  38.     modeid = scr->ViewPort.Modes & OLDCAMGMASK;
  39.  
  40.     count = scr->ViewPort.ColorMap->Count;
  41.     if(colortable = (UWORD *)AllocMem(count << 1, MEMF_CLEAR))
  42.     {
  43.     for(k=0; k<count; k++)    colortable[k]=GetRGB4(scr->ViewPort.ColorMap,k);
  44.  
  45.         error = saveilbm(ilbm, &scr->BitMap, modeid,
  46.         scr->Width, scr->Height, scr->Width, scr->Height,
  47.         colortable, count, 4,
  48.         mskNone, 0,
  49.         chunklist1, chunklist2, filename);
  50.     FreeMem(colortable,count << 1);
  51.     }
  52.     else error = IFFERR_NOMEM;
  53.     return(error);
  54. }
  55.  
  56.  
  57. /* saveilbm
  58.  *
  59.  * Given an ILBMInfo with a currently available (not-in-use)
  60.  *   ParseInfo->iff IFFHandle, a BitMap ptr,
  61.  *   modeid, widths/heights, colortable, ncolors, bitspergun,
  62.  *   masking, transparent color, optional chunklists, and filename,
  63.  *   will save the bitmap as an ILBM.
  64.  *
  65.  *  if bitspergun=4,  colortable is words, each with nibbles 0RGB
  66.  *  if bitspergun=8,  colortable is byte guns of RGBRGB etc. (like a CMAP)
  67.  *  if bitspergun=32, colortable is ULONG guns of RGBRGB etc.
  68.  *     Only the high eight bits of each gun will be written to CMAP.
  69.  *     Four bit guns n will be saved as nn
  70.  *
  71.  * The struct Chunk *chunklist is for chunks you wish written
  72.  * other than BMHD, CMAP, and CAMG (they will be screened out)
  73.  * because they are calculated and written separately
  74.  *
  75.  * Returns 0 for success, or an IFFERR
  76.  */
  77. LONG saveilbm(struct ILBMInfo *ilbm,
  78.         struct BitMap *bitmap, ULONG modeid,
  79.         WORD width, WORD height, WORD pagewidth, WORD pageheight,
  80.         APTR colortable, UWORD ncolors, UWORD bitspergun,
  81.         WORD masking, WORD transparentColor,
  82.         struct Chunk *chunklist1, struct Chunk *chunklist2,
  83.         UBYTE *filename)
  84. {
  85. struct IFFHandle *iff;
  86. struct Chunk *chunk;
  87. ULONG chunkID;
  88. UBYTE *bodybuf;
  89. LONG size, error = 0L;
  90. #define BODYBUFSZ    4096
  91.  
  92.     iff = ilbm->ParseInfo.iff;
  93.  
  94.     if(!(modeid & 0xFFFF0000))    modeid &= OLDCAMGMASK;
  95.  
  96.     if(!(bodybuf = AllocMem(BODYBUFSZ,MEMF_PUBLIC)))
  97.     {
  98.     message("Not enough memory\n");
  99.     return(IFFERR_NOMEM);
  100.     }
  101.  
  102.     if(!(error = openifile(ilbm, filename, IFFF_WRITE)))
  103.     {
  104.     D(bug("Opened %s for write\n",filename));
  105.  
  106.     error = PushChunk(iff, ID_ILBM, ID_FORM, IFFSIZE_UNKNOWN);
  107.  
  108.     D(bug("After PushChunk FORM ILBM - error = %ld\n", error));
  109.  
  110.         initbmhd(&ilbm->Bmhd, bitmap, masking, cmpByteRun1, transparentColor,
  111.                     width, height, pagewidth, pageheight, modeid);
  112.  
  113.     D(bug("Error before putbmhd = %ld\n",error));
  114.  
  115.     CkErr(putbmhd(iff,&ilbm->Bmhd));    
  116.  
  117.     if(colortable)    CkErr(putcmap(iff,colortable,ncolors,bitspergun));
  118.  
  119.     ilbm->camg = modeid;
  120.     D(bug("before putcamg - error = %ld\n",error));
  121.     CkErr(putcamg(iff,&modeid));
  122.  
  123.     D(bug("Past putBMHD, CMAP, CAMG - error = %ld\n",error));
  124.  
  125.     /* Write out chunklists 1 & 2 (if any), except for
  126.      * any BMHD, CMAP, or CAMG (computed/written separately)
  127.      */
  128.     for(chunk = chunklist1; chunk; chunk = chunk->ch_Next)
  129.         {
  130.         D(bug("chunklist1 - have a %.4s\n",&chunk->ch_ID));
  131.         chunkID = chunk->ch_ID;
  132.         if((chunkID != ID_BMHD)&&(chunkID != ID_CMAP)&&(chunkID != ID_CAMG))
  133.         {
  134.         size = chunk->ch_Size==IFFSIZE_UNKNOWN ?
  135.             strlen(chunk->ch_Data) : chunk->ch_Size;
  136.         D(bug("Putting %.4s\n",&chunk->ch_ID));
  137.         CkErr(PutCk(iff, chunkID, size, chunk->ch_Data));
  138.         }
  139.         }
  140.  
  141.     for(chunk = chunklist2; chunk; chunk = chunk->ch_Next)
  142.         {
  143.         chunkID = chunk->ch_ID;
  144.         D(bug("chunklist2 - have a %.4s\n",&chunk->ch_ID));
  145.         if((chunkID != ID_BMHD)&&(chunkID != ID_CMAP)&&(chunkID != ID_CAMG))
  146.         {
  147.         size = chunk->ch_Size==IFFSIZE_UNKNOWN ?
  148.             strlen(chunk->ch_Data) : chunk->ch_Size;
  149.         D(bug("Putting %.4s\n",&chunk->ch_ID));
  150.         CkErr(PutCk(iff, chunkID, size, chunk->ch_Data));
  151.         }
  152.         }
  153.  
  154.     /* Write out the BODY
  155.      */
  156.     CkErr(putbody(iff, bitmap, NULL, &ilbm->Bmhd, bodybuf, BODYBUFSZ));
  157.  
  158.     D(bug("Past putbody - error = %ld\n",error));
  159.  
  160.  
  161.     CkErr(PopChunk(iff));    /* close out the FORM */
  162.     closeifile(ilbm);    /* and the file */
  163.     }
  164.  
  165.     FreeMem(bodybuf,BODYBUFSZ);
  166.  
  167.     return(error);
  168. }
  169.  
  170.