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