home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 3 / goldfish_volume_3.bin / files / text / tex / pastex / source / driver / util / iff / saveilbm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-06  |  6.0 KB  |  223 lines

  1. /* saveilbm.c 04/92  C. Scheppner CBM
  2.  *
  3.  * High-level ILBM save routines
  4.  *
  5.  * 37.10 07/92 - use scr->RastPort.BitMap instead of &scr->BitMap
  6.  *            for future compatibility
  7.  * 39.5 11/92 - raised BODYBUF size from 4096 to 5004 (should allow saving
  8.  *              of bitmaps up to pixel width of 16384)
  9.  */
  10. #define INTUI_V36_NAMES_ONLY
  11.  
  12. #include <graphics/gfxbase.h>
  13.  
  14. #include "//defines.h"
  15. #include "/globals.h"
  16. #include "/globals.i"
  17.  
  18. #include "ilbm.h"
  19. #include "ilbmapp.h"
  20.  
  21.  
  22.  
  23. /*
  24.  * Fuer die locale-Library:
  25.  *
  26.  * Hier duerfen *nur* die MSG_#? Nummern eingebunden werden!
  27.  * Achtung:
  28.  * Es muss/sollte 'multiple-include' erlaubt sein!
  29.  */
  30. #include "local.i"
  31.  
  32. #undef  CATCOMP_ARRAY
  33. #undef  CATCOMP_BLOCK
  34. #undef  CATCOMP_STRINGS
  35. #define CATCOMP_NUMBERS
  36. #include "localstr.h"
  37.  
  38.  
  39.  
  40. extern struct GfxBase *GfxBase;
  41.  
  42. /* screensave.c
  43.  *
  44.  * Given an ILBMInfo with a  currently available (not in use)
  45.  *   ParseInfo->iff IFFHandle, a screen pointer, filename, and
  46.  *   optional chunklist, will save screen as an ILBM
  47.  * The struct Chunk *chunklist1 and 2 are for chunks you wish written
  48.  * out other than BMHD, CMAP, and CAMG (they will be screened out
  49.  * because they are computed and written separately).
  50.  *
  51.  * Note -  screensave passes NULL for transparent color and mask
  52.  *
  53.  * Returns 0 for success or an IFFERR (libraries/iffparse.h)
  54.  */
  55. LONG screensave(struct ILBMInfo *ilbm,
  56.             struct Screen *scr,
  57.             struct Chunk *chunklist1, struct Chunk *chunklist2,
  58.             UBYTE *filename)
  59.     {
  60.     Color32 *colortable32;
  61.     UWORD *colortable, count;
  62.     ULONG modeid;
  63.     LONG error;
  64.     int k;
  65.     
  66.     D(bug("In screensave()!\n"));
  67.  
  68.     if(GfxBase->LibNode.lib_Version >= 36)
  69.     modeid=GetVPModeID(&scr->ViewPort);
  70.     else
  71.     modeid = scr->ViewPort.Modes & OLDCAMGMASK;
  72.  
  73.     count = scr->ViewPort.ColorMap->Count;
  74.  
  75.     error = IFFERR_NOMEM;
  76.  
  77.     if(GfxBase->LibNode.lib_Version >= 39)
  78.     {
  79.         if(colortable32 = (Color32 *)AllocMem(sizeof(Color32) * count, MEMF_CLEAR))
  80.         {
  81.         GetRGB32(scr->ViewPort.ColorMap,0L,count,(ULONG *)colortable32);
  82.             error = saveilbm(ilbm, scr->RastPort.BitMap, modeid,
  83.         scr->Width, scr->Height, scr->Width, scr->Height,
  84.         colortable32, count, 32,
  85.         mskNone, 0,
  86.         chunklist1, chunklist2, filename);
  87.         FreeMem(colortable32,sizeof(Color32) * count);
  88.         }
  89.     }
  90.     else
  91.     {
  92.         if(colortable = (UWORD *)AllocMem(count << 1, MEMF_CLEAR))
  93.         {
  94.         for(k=0; k<count; k++)
  95.         colortable[k]=GetRGB4(scr->ViewPort.ColorMap,k);
  96.  
  97.             error = saveilbm(ilbm, scr->RastPort.BitMap, modeid,
  98.         scr->Width, scr->Height, scr->Width, scr->Height,
  99.         colortable, count, 4,
  100.         mskNone, 0,
  101.         chunklist1, chunklist2, filename);
  102.         FreeMem(colortable,count << 1);
  103.         }
  104.     }
  105.     return(error);
  106.     }
  107.  
  108.  
  109. /* saveilbm
  110.  *
  111.  * Given an ILBMInfo with a currently available (not-in-use)
  112.  *   ParseInfo->iff IFFHandle, a BitMap ptr,
  113.  *   modeid, widths/heights, colortable, ncolors, bitspergun,
  114.  *   masking, transparent color, optional chunklists, and filename,
  115.  *   will save the bitmap as an ILBM.
  116.  *
  117.  *  if bitspergun=4,  colortable is words, each with nibbles 0RGB
  118.  *  if bitspergun=8,  colortable is byte guns of RGBRGB etc. (like a CMAP)
  119.  *  if bitspergun=32, colortable is ULONG guns of RGBRGB etc.
  120.  *     Only the high eight bits of each gun will be written to CMAP.
  121.  *     Four bit guns n will be saved as nn
  122.  *
  123.  * The struct Chunk *chunklist is for chunks you wish written
  124.  * other than BMHD, CMAP, and CAMG (they will be screened out)
  125.  * because they are calculated and written separately
  126.  *
  127.  * Returns 0 for success, or an IFFERR
  128.  */
  129. LONG saveilbm(struct ILBMInfo *ilbm,
  130.         struct BitMap *bitmap, ULONG modeid,
  131.         WORD width, WORD height, WORD pagewidth, WORD pageheight,
  132.         APTR colortable, UWORD ncolors, UWORD bitspergun,
  133.         WORD masking, WORD transparentColor,
  134.         struct Chunk *chunklist1, struct Chunk *chunklist2,
  135.         UBYTE *filename)
  136. {
  137. struct IFFHandle *iff;
  138. struct Chunk *chunk;
  139. ULONG chunkID;
  140. UBYTE *bodybuf;
  141. LONG size, error = 0L;
  142. #define BODYBUFSZ    5004
  143.  
  144.     iff = ilbm->ParseInfo.iff;
  145.  
  146.     if(!(modeid & 0xFFFF0000))    modeid &= OLDCAMGMASK;
  147.  
  148.     if(!(bodybuf = AllocMem(BODYBUFSZ,MEMF_PUBLIC)))
  149.     {
  150.     //message(SI(MSG_IFFP_NOMEM));
  151.     Warning(MSG_NO_MEM);
  152.     return(IFFERR_NOMEM);
  153.     }
  154.  
  155.     if(!(error = openifile(&(ilbm->ParseInfo), filename, IFFF_WRITE)))
  156.     {
  157.     D(bug("Opened %s for write\n",filename));
  158.  
  159.     error = PushChunk(iff, ID_ILBM, ID_FORM, IFFSIZE_UNKNOWN);
  160.  
  161.     D(bug("After PushChunk FORM ILBM - error = %ld\n", error));
  162.  
  163.         initbmhd(&ilbm->Bmhd, bitmap, masking, cmpByteRun1, transparentColor,
  164.                     width, height, pagewidth, pageheight, modeid);
  165.  
  166.     D(bug("Error before putbmhd = %ld\n",error));
  167.  
  168.     CkErr(putbmhd(iff,&ilbm->Bmhd));    
  169.  
  170.     if(colortable)    CkErr(putcmap(iff,colortable,ncolors,bitspergun));
  171.  
  172.     ilbm->camg = modeid;
  173.     D(bug("before putcamg - error = %ld\n",error));
  174.     CkErr(putcamg(iff,&modeid));
  175.  
  176.     D(bug("Past putBMHD, CMAP, CAMG - error = %ld\n",error));
  177.  
  178.     /* Write out chunklists 1 & 2 (if any), except for
  179.      * any BMHD, CMAP, or CAMG (computed/written separately)
  180.      */
  181.     for(chunk = chunklist1; chunk; chunk = chunk->ch_Next)
  182.         {
  183.         D(bug("chunklist1 - have a %.4s\n",&chunk->ch_ID));
  184.         chunkID = chunk->ch_ID;
  185.         if((chunkID != ID_BMHD)&&(chunkID != ID_CMAP)&&(chunkID != ID_CAMG))
  186.         {
  187.         size = chunk->ch_Size==IFFSIZE_UNKNOWN ?
  188.             strlen(chunk->ch_Data) : chunk->ch_Size;
  189.         D(bug("Putting %.4s\n",&chunk->ch_ID));
  190.         CkErr(PutCk(iff, chunkID, size, chunk->ch_Data));
  191.         }
  192.         }
  193.  
  194.     for(chunk = chunklist2; chunk; chunk = chunk->ch_Next)
  195.         {
  196.         chunkID = chunk->ch_ID;
  197.         D(bug("chunklist2 - have a %.4s\n",&chunk->ch_ID));
  198.         if((chunkID != ID_BMHD)&&(chunkID != ID_CMAP)&&(chunkID != ID_CAMG))
  199.         {
  200.         size = chunk->ch_Size==IFFSIZE_UNKNOWN ?
  201.             strlen(chunk->ch_Data) : chunk->ch_Size;
  202.         D(bug("Putting %.4s\n",&chunk->ch_ID));
  203.         CkErr(PutCk(iff, chunkID, size, chunk->ch_Data));
  204.         }
  205.         }
  206.  
  207.     /* Write out the BODY
  208.      */
  209.     CkErr(putbody(iff, bitmap, NULL, &ilbm->Bmhd, bodybuf, BODYBUFSZ));
  210.  
  211.     D(bug("Past putbody - error = %ld\n",error));
  212.  
  213.  
  214.     CkErr(PopChunk(iff));    /* close out the FORM */
  215.     closeifile(&(ilbm->ParseInfo));    /* and the file */
  216.     }
  217.  
  218.     FreeMem(bodybuf,BODYBUFSZ);
  219.  
  220.     return(error);
  221. }
  222.  
  223.