home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 015.lha / tracer_source / saveilbm.c < prev    next >
C/C++ Source or Header  |  1986-11-10  |  4KB  |  145 lines

  1. /***************************************************************************
  2. *  SaveILBM.c --  Save raw raster image as ILBM file
  3. *                 by Carolyn Scheppner  CBM  01/86
  4. *     Using IFF rtns by J.Morrison and S.Shaw of Electronic Arts
  5. *
  6. ***************************************************************************/
  7.  
  8. /*
  9. #include <exec/types.h>
  10. #include <exec/memory.h>
  11. #include <libraries/dos.h>
  12. #include <graphics/gfxbase.h>
  13. #include <graphics/rastport.h>
  14. #include <graphics/gfx.h>
  15. #include <graphics/view.h>
  16. */
  17.  
  18. #include <intuition/intuition.h>
  19. #include <intuition/intuitionbase.h>
  20.  
  21. #include <libraries/dos.h>
  22.  
  23. #include "ilbm.h"
  24.  
  25. #define    bufSize    512
  26.  
  27.  
  28. /*
  29. struct    Window    *activeWindow;
  30. struct    Screen    *activeScreen;
  31. struct    Screen    *firstScreen;
  32.  
  33. struct    Screen        *nextScreen, *highScreen;
  34. struct    View        *thisView;
  35. struct    View        *picView = 0;
  36. */
  37. struct    ViewPort    *picViewPort;
  38. struct    BitMap        *picBitMap;
  39. WORD            *picColorTable;
  40.  
  41.  
  42. /* main() */
  43. void    save_screen( s, name )
  44. struct    Screen    *s;
  45. char    name[];
  46. {
  47.     LONG    file;
  48.  
  49.     if( file = Open(name, MODE_NEWFILE) ) {
  50.  
  51.         Write(file,"x",1);  /* so Seek to beginning works ????? */
  52.  
  53.         picViewPort = &( s -> ViewPort );
  54.  
  55.         picBitMap = (struct BitMap *)
  56.                 picViewPort -> RasInfo -> BitMap;
  57.  
  58.         picColorTable = (WORD *)
  59.                 picViewPort -> ColorMap -> ColorTable;
  60.  
  61.         /* iffp = */
  62.         PutPicture(file, picBitMap, picColorTable);
  63.  
  64.         Close(file);
  65.     } else
  66.         printf("Error opening file [%s].\n", name );
  67. }
  68.  
  69. /** PutPicture() ***********************************************************
  70.  *
  71.  * Put a picture into an IFF file.
  72.  * This procedure calls PutAnILBM, passing in an <x, y> location of <0, 0>,
  73.  * a NULL mask, and a locally-allocated buffer. It also assumes you want to
  74.  * write out all the bitplanes in the BitMap.
  75.  *
  76.  ***************************************************************************/
  77. Point2D nullPoint = {0, 0};
  78.  
  79. IFFP    PutPicture(file, bitmap, colorMap)
  80. LONG    file;
  81. struct    BitMap    *bitmap;
  82. WORD    *colorMap;
  83. {
  84.     BYTE    buffer[bufSize];
  85.  
  86.     return(
  87.         PutAnILBM(file, bitmap, NULL,
  88.             colorMap, bitmap->Depth, &nullPoint,
  89.             buffer, bufSize
  90.         )
  91.     );
  92. }
  93.  
  94.    
  95. /** PutAnILBM() ************************************************************
  96.  *
  97.  * Write an entire BitMap as a FORM ILBM in an IFF file.
  98.  * This version works for any display mode (C. Scheppner).
  99.  *
  100.  * Normal return result is IFF_OKAY.
  101.  *
  102.  * The utility program IFFCheck would print the following outline of the
  103.  * resulting file:
  104.  *
  105.  *   FORM ILBM
  106.  *     BMHD
  107.  *     CMAP
  108.  *     BODY       (compressed)
  109.  *
  110.  ***************************************************************************/
  111. #define CkErr(expression)  {if (ifferr == IFF_OKAY) ifferr = (expression);}
  112.  
  113. IFFP PutAnILBM(file, bitmap, mask, colorMap, depth, xy, buffer, bufsize)
  114.       LONG file;  struct BitMap *bitmap;  BYTE *mask;  WORD *colorMap;
  115.       UBYTE depth;  Point2D *xy;
  116.       BYTE *buffer;  LONG bufsize;
  117. {
  118.    BitMapHeader bmHdr;
  119.    GroupContext fileContext, formContext;
  120.    IFFP ifferr;
  121.    WORD pageWidth, pageHeight;
  122.  
  123.    pageWidth  = (bitmap->BytesPerRow) << 3;
  124.    pageHeight = bitmap->Rows;
  125.  
  126.    ifferr = InitBMHdr(&bmHdr, bitmap, mskNone,
  127.                       cmpByteRun1, 0, pageWidth, pageHeight);
  128.    /* You could write an uncompressed image by passing cmpNone instead
  129.     * of cmpByteRun1 to InitBMHdr. */
  130.    bmHdr.nPlanes = depth;   /* This must be  <= bitmap->Depth */
  131.    if (mask != NULL) bmHdr.masking = mskHasMask;
  132.    bmHdr.x = xy->x;   bmHdr.y = xy->y;
  133.  
  134.    CkErr( OpenWIFF(file, &fileContext, szNotYetKnown) );
  135.    CkErr(StartWGroup(&fileContext, FORM, szNotYetKnown, ID_ILBM, &formContext));
  136.  
  137.    CkErr( PutBMHD(&formContext, &bmHdr) );
  138.    CkErr( PutCMAP(&formContext, colorMap, depth) );
  139.    CkErr( PutBODY(&formContext, bitmap, mask, &bmHdr, buffer, bufsize) );
  140.  
  141.    CkErr( EndWGroup(&formContext) );
  142.    CkErr( CloseWGroup(&fileContext) );
  143.    return( ifferr );
  144. }    
  145.