home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff214.lzh / MandelVroom / src / SaveILBM.c < prev    next >
C/C++ Source or Header  |  1989-05-30  |  7KB  |  269 lines

  1. /***************************************************************************
  2. *  SaveILBM.c --  Save front screen as ILBM file
  3. *                 Saves a CAMG chunk for Amiga ViewModes
  4. *                   by Carolyn Scheppner  CBM  10/86
  5. *
  6. *                   modified by Kevin L. Clague for use in Mandelbrot
  7. *
  8. *     Using IFF rtns by J.Morrison and S.Shaw of Electronic Arts
  9. *
  10. ***************************************************************************/
  11.  
  12. #include "SaveILBM.h"
  13.  
  14. /* CAMG Stuff */
  15. typedef struct {
  16.    ULONG ViewModes;
  17.    } CamgChunk;
  18.  
  19. #define PutCAMG(context, camg)  \
  20.     PutCk(context, ID_CAMG, sizeof(CamgChunk), (BYTE *)camg)
  21.  
  22. #define ID_CRNG  MakeID('C','R','N','G')
  23. #define PutCRNG(context, crng)  \
  24.     PutCk(context, ID_CRNG, sizeof(CrngChunk), (BYTE *)crng)
  25.  
  26. #define bufSize 512
  27.  
  28. extern struct IntuitionBase *IntuitionBase;
  29. extern struct Screen *screen;
  30.  
  31. struct Screen   *frontScreen;
  32.  
  33. struct ViewPort *picViewPort;
  34. struct BitMap   *picBitMap;
  35. WORD            *picColorTable;
  36. ULONG            picViewModes;
  37.  
  38. /* ILBM Icon Image */
  39.  
  40. USHORT  ILBMimagedata[] = {
  41.  0x0000, 0x0000, 0x0000, 0x0000,
  42.  0x7FFF, 0xFFFF, 0xFFFF, 0xFF80,
  43.  0x6000, 0x0000, 0x0000, 0x0180,
  44.  0x6000, 0x07FF, 0xFFFF, 0x8180,
  45.  0x6000, 0x401F, 0xFFC0, 0x0180,
  46.  0x6002, 0xE000, 0x0000, 0x0180,
  47.  0x6003, 0x6002, 0x0008, 0x0180,
  48.  0x600B, 0xF807, 0x005E, 0x0180,
  49.  0x6001, 0xFC03, 0x809F, 0x8180,
  50.  0x6000, 0x0E07, 0xC2FF, 0xE180,
  51.  0x600D, 0xAF05, 0xE017, 0xF980,
  52.  0x6021, 0x2F83, 0xC057, 0xF980,
  53.  0x6025, 0xFF1F, 0x24C7, 0xF980,
  54.  0x6097, 0xFA6E, 0x881F, 0xF980,
  55.  0x601E, 0xFCEC, 0x3B5B, 0xF980,
  56.  0x6055, 0xFFBA, 0x559F, 0xF980,
  57.  0x60EF, 0xFFFF, 0xFFFF, 0xF980,
  58.  0x6000, 0x0000, 0x0000, 0x0180,
  59.  0x7FFF, 0xFFFF, 0xFFFF, 0xFF80,
  60.  0x0000, 0x0000, 0x0000, 0x0000,
  61. /**/
  62.  0xFFFF, 0xFFFF, 0xFFFF, 0xFFC0,
  63.  0x8000, 0x0000, 0x0000, 0x0040,
  64.  0x9FFF, 0xFFFF, 0xFFFF, 0xFE40,
  65.  0x9800, 0x0000, 0x0000, 0x0640,
  66.  0x9800, 0x0000, 0x0000, 0x0640,
  67.  0x9801, 0x0000, 0x0000, 0x0640,
  68.  0x9804, 0x8004, 0x0000, 0x0640,
  69.  0x9804, 0x8008, 0x0020, 0x0640,
  70.  0x981F, 0x0C0C, 0x0160, 0x0640,
  71.  0x983F, 0xFE1F, 0x8D00, 0x0640,
  72.  0x98FF, 0xFF7F, 0xDFE8, 0x0640,
  73.  0x9BFF, 0xFFFF, 0xFFFF, 0x0640,
  74.  0x9FFF, 0xFFFF, 0xFFFF, 0x8640,
  75.  0x9FFF, 0xFFFF, 0xFFFF, 0xFE40,
  76.  0x9FFF, 0xFFFF, 0xFFFF, 0xFE40,
  77.  0x9FFF, 0xFFFF, 0xFFFF, 0xFE40,
  78.  0x9FFF, 0xFFFF, 0xFFFF, 0xFE40,
  79.  0x9FFF, 0xFFFF, 0xFFFF, 0xFE40,
  80.  0x8000, 0x0000, 0x0000, 0x0040,
  81.  0xFFFF, 0xFFFF, 0xFFFF, 0xFFC0
  82. /**/
  83.  };
  84.  
  85. struct Image  ILBMimage[] = {
  86.  0,  /* LeftEdge */
  87.  0,  /* TopEdge */
  88.  58,  /* Width */
  89.  20,  /* Height */
  90.  2,  /* Depth */
  91.  (USHORT *)&ILBMimagedata,  /* ImageData */
  92.  0x3,  /* PlanePick */
  93.  0x0,  /* PlaneOnOff */
  94.  0  /* [NextImage] */
  95.  };
  96.  
  97. struct DiskObject ILBMIcon = {
  98.  WB_DISKMAGIC,   /* do_Magic */
  99.  WB_DISKVERSION, /* do_Version */
  100.  
  101. /* Embedded Gadget Structure */
  102.  0,  /* [NextGadget] */
  103.  147,  /* LeftEdge */
  104.  23,  /* TopEdge */
  105.  58,  /* Width */
  106.  20,  /* Height */
  107.  0x4,  /* Flags */
  108.  0x3,  /* Activation */
  109.  0x1,  /* GadgetType */
  110.  (APTR)&ILBMimage, /* GadgetRender */
  111.  NULL,     /* SelectRender */
  112.  NULL,     /* GadgetText */
  113.  0x0,  /* MutualExclude */
  114.  0x0,  /* [SpecialInfo] */
  115.  0,  /* GadgetID */
  116.  0x0,  /* [UserData] */
  117.  
  118. /* Rest of DiskObject structure */
  119.  0x4,  /* do_Type */
  120.  ":ViewILBM",   /* do_Default Tool */
  121.  0xC06508,  /* [do_ToolTypes] */
  122.  NO_ICON_POSITION,  /* do_CurrentX */
  123.  NO_ICON_POSITION,  /* do_CurrentY */
  124.  0x0,  /* [do_DrawerData] */
  125.  0x0,  /* [do_ToolWindow] */
  126.  0x0  /* do_StackSize */
  127.  };
  128.  
  129. /**************************************************************************
  130.  *
  131.  *  Save the current screen as an ILBM file
  132.  *
  133.  **************************************************************************/
  134.  
  135. SaveILBM(FileName, Crng, numCrng)
  136.   char *FileName;
  137.   CrngChunk *Crng;
  138.   int numCrng;
  139. {
  140.   LONG            file;
  141.   IFFP            iffp = NO_FILE;
  142.  
  143.   int l;
  144.  
  145.   if (!(file = Open(FileName, MODE_NEWFILE))) {
  146.  
  147.     char Msg[80];
  148.     sprintf(Msg,"Can't open file %s\n",FileName);
  149.     DispErrMsg(Msg,0);
  150.     return(0);
  151.   }
  152.  
  153.   Write(file,"x",1);  /* 1.1 so Seek to beginning works ? */
  154.  
  155.   frontScreen  = screen;
  156.  
  157.   picViewPort =  &( frontScreen->ViewPort );
  158.   picBitMap =     (struct BitMap*)picViewPort->RasInfo->BitMap;
  159.   picColorTable = (WORD *)picViewPort->ColorMap->ColorTable;
  160.   picViewModes =  (ULONG)picViewPort->Modes;
  161.  
  162.   iffp = PutPicture(file, picBitMap, picColorTable, picViewModes,
  163.     Crng, numCrng);
  164.   Close(file);
  165.  
  166.   if (iffp == IFF_OKAY && FromWB ) {
  167.  
  168.     /* This line outputs the icon */
  169.     PutDiskObject(FileName,&ILBMIcon);
  170.   }
  171. } /* SaveILBM */
  172.  
  173.  
  174. /** PutPicture() ***********************************************************
  175.  *
  176.  * Put a picture into an IFF file.
  177.  * This procedure calls PutAnILBM, passing in an <x, y> location of <0, 0>,
  178.  * a NULL mask, and a locally-allocated buffer. It also assumes you want to
  179.  * write out all the bitplanes in the BitMap.
  180.  *
  181.  ***************************************************************************/
  182. Point2D nullPoint = {0, 0};
  183.  
  184. IFFP PutPicture(file, bitmap, colorMap, viewmodes, crng, numcrng)
  185.       LONG file;  struct BitMap *bitmap;
  186.       WORD *colorMap;  ULONG viewmodes;
  187.       CrngChunk *crng;
  188.       int numcrng;
  189.    {
  190.    BYTE buffer[bufSize];
  191.    return( PutAnILBM(file, bitmap, NULL,
  192.            colorMap, bitmap->Depth, viewmodes,
  193.            &nullPoint, crng, numcrng, buffer, bufSize) );
  194.    }
  195.  
  196.  
  197. /** PutAnILBM() ************************************************************
  198.  *
  199.  * Write an entire BitMap as a FORM ILBM in an IFF file.
  200.  * This version works for any display mode (C. Scheppner).
  201.  *
  202.  * Normal return result is IFF_OKAY.
  203.  *
  204.  * The utility program IFFCheck would print the following outline of the
  205.  * resulting file:
  206.  *
  207.  *   FORM ILBM
  208.  *     BMHD
  209.  *     CAMG
  210.  *     CMAP
  211.  *     CRNG
  212.  *     CRNG
  213.  *     CRNG
  214.  *     CRNG
  215.  *     BODY       (compressed)
  216.  *
  217.  ***************************************************************************/
  218. #define CkErr(expression)  {if (ifferr == IFF_OKAY) ifferr = (expression);}
  219.  
  220. IFFP PutAnILBM(file, bitmap, mask, colorMap, depth,
  221.                                 viewmodes, xy, crng, numcrng,
  222.                                 buffer, bufsize)
  223.       LONG file;
  224.       struct BitMap *bitmap;
  225.       BYTE *mask;  WORD *colorMap; UBYTE depth;
  226.       ULONG viewmodes;
  227.       Point2D *xy; BYTE *buffer;  LONG bufsize;
  228.       CrngChunk *crng;
  229.       int numcrng;
  230.    {
  231.    BitMapHeader bmHdr;
  232.    CamgChunk    camgChunk;
  233.    GroupContext fileContext, formContext;
  234.    IFFP ifferr;
  235.    WORD pageWidth, pageHeight;
  236.    int i;
  237.  
  238.    pageWidth  = (bitmap->BytesPerRow) << 3;
  239.    pageHeight = bitmap->Rows;
  240.  
  241.    ifferr = InitBMHdr(&bmHdr, bitmap, mskNone,
  242.                       cmpByteRun1, 0, pageWidth, pageHeight);
  243.    /* You could write an uncompressed image by passing cmpNone instead
  244.     * of cmpByteRun1 to InitBMHdr. */
  245.    bmHdr.nPlanes = depth;   /* This must be  <= bitmap->Depth */
  246.    if (mask != NULL) bmHdr.masking = mskHasMask;
  247.    bmHdr.x = xy->x;   bmHdr.y = xy->y;
  248.  
  249.    camgChunk.ViewModes = viewmodes;
  250.  
  251.    CkErr( OpenWIFF(file, &fileContext, szNotYetKnown) );
  252.    CkErr(StartWGroup(&fileContext, FORM, szNotYetKnown, ID_ILBM, &formContext));
  253.  
  254.    CkErr( PutBMHD(&formContext, &bmHdr) );
  255.    CkErr( PutCAMG(&formContext, &camgChunk) );
  256.    CkErr( PutCMAP(&formContext, colorMap, depth) );
  257.  
  258.    for (i = 0; i < numcrng; i++) {
  259.      CkErr( PutCRNG(&formContext, &crng[i]));
  260.    }
  261.    CkErr( PutBODY(&formContext, bitmap, mask, &bmHdr, buffer, bufsize) );
  262.  
  263.    CkErr( EndWGroup(&formContext) );
  264.    CkErr( CloseWGroup(&fileContext) );
  265.    return( ifferr );
  266.    }
  267.  
  268.  
  269.