home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / new / gfx / edit / tsmorph / src / ilbmw.c < prev    next >
C/C++ Source or Header  |  1994-01-29  |  7KB  |  215 lines

  1. /*----------------------------------------------------------------------*
  2.  * ILBMW.C  Support routines for writing ILBM files using IFFParse.
  3.  * (IFF is Interchange Format File.)
  4.  *
  5.  * Based on code by Jerry Morrison and Steve Shaw, Electronic Arts.
  6.  * This software is in the public domain.
  7.  *
  8.  * This version for the Commodore-Amiga computer.
  9.  *
  10.  * mod 06-Apr-92 - Add JM change for separate source and dest modulo
  11.  *----------------------------------------------------------------------*/
  12. #define INTUI_V36_NAMES_ONLY
  13.  
  14. #include "iffp/ilbm.h"
  15. #include "iffp/packer.h"
  16.  
  17. #include <graphics/gfxbase.h>
  18.  
  19. extern struct Library *GfxBase;
  20.  
  21. /*---------- initbmhd -------------------------------------------------*/
  22. long initbmhd(BitMapHeader *bmhd, struct BitMap *bitmap,
  23.           WORD masking, WORD compression, WORD transparentColor,
  24.           WORD width, WORD height, WORD pageWidth, WORD pageHeight,
  25.           ULONG modeid)
  26.     {
  27.     extern struct Library *GfxBase;
  28.     struct DisplayInfo DI;
  29.  
  30.     WORD rowBytes = bitmap->BytesPerRow;
  31.  
  32.     D(bug("In InitBMHD\n"));
  33.  
  34.     bmhd->w = width;
  35.     bmhd->h = height;
  36.     bmhd->x = bmhd->y = 0;    /* Default position is (0,0).*/
  37.     bmhd->nPlanes = bitmap->Depth;
  38.     bmhd->masking = masking;
  39.     bmhd->compression = compression;
  40.     bmhd->reserved1 = 0;
  41.     bmhd->transparentColor = transparentColor;
  42.     bmhd->pageWidth = pageWidth;
  43.     bmhd->pageHeight = pageHeight;
  44.  
  45.     bmhd->xAspect = 0;    /* So we can tell when we've got it */
  46.     if(GfxBase->lib_Version >=36)
  47.     {
  48.        if(GetDisplayInfoData(NULL, (UBYTE *)&DI,
  49.         sizeof(struct DisplayInfo), DTAG_DISP, modeid))
  50.         {
  51.             bmhd->xAspect =  DI.Resolution.x;
  52.             bmhd->yAspect =  DI.Resolution.y;
  53.         }
  54.     }
  55.  
  56.     /* If running under 1.3 or GetDisplayInfoData failed, use old method
  57.      * of guessing aspect ratio
  58.      */
  59.     if(! bmhd->xAspect)
  60.     {
  61.         bmhd->xAspect =  44;
  62.         bmhd->yAspect =
  63.         ((struct GfxBase *)GfxBase)->DisplayFlags & PAL ? 44 : 52;
  64.         if(modeid & HIRES)    bmhd->xAspect = bmhd->xAspect >> 1;
  65.         if(modeid & LACE)    bmhd->yAspect = bmhd->yAspect >> 1;
  66.     }
  67.  
  68.     return( IS_ODD(rowBytes) ? CLIENT_ERROR : IFF_OKAY );
  69.     }
  70.  
  71. /*---------- putcmap ---------------------------------------------------*/
  72. /* This function will accept a table of color values in one of the
  73.  * following forms:
  74.  *  if bitspergun=4,  colortable is words, each with nibbles 0RGB
  75.  *  if bitspergun=8,  colortable is bytes of RGBRGB etc. (like a CMAP)
  76.  *  if bitspergun=32, colortable is ULONGS of RGBRGB etc.
  77.  *     (only the high eight bits of each gun will be written to CMAP)
  78.  */
  79. long putcmap(struct IFFHandle *iff, APTR colortable,
  80.          UWORD ncolors, UWORD bitspergun)
  81.    {
  82.    long error, offs;
  83.    WORD  *tabw;
  84.    UBYTE *tab8;
  85.    ColorRegister cmapReg;
  86.  
  87.    D(bug("In PutCMAP\n"));
  88.  
  89.    if((!iff)||(!colortable))    return(CLIENT_ERROR);
  90.  
  91.    /* size of CMAP is 3 bytes * ncolors */
  92.    if(error = PushChunk(iff, NULL, ID_CMAP, ncolors * sizeofColorRegister))
  93.     return(error);
  94.  
  95.    D(bug("Pushed ID_CMAP, error = %ld\n",error));
  96.  
  97.    if(bitspergun == 4)
  98.     {
  99.        /* Store each 4-bit value n as nn */
  100.     tabw = (UWORD *)colortable;
  101.        for( ;  ncolors;  --ncolors )
  102.         {
  103.               cmapReg.red    = ( *tabw >> 4 ) & 0xf0;
  104.               cmapReg.red   |= (cmapReg.red >> 4);
  105.  
  106.               cmapReg.green  = ( *tabw      ) & 0xf0;
  107.               cmapReg.green |= (cmapReg.green >> 4);
  108.  
  109.               cmapReg.blue   = ( *tabw << 4 ) & 0xf0;
  110.               cmapReg.blue  |= (cmapReg.blue >> 4);
  111.  
  112.               if((WriteChunkBytes(iff, (BYTE *)&cmapReg, sizeofColorRegister))
  113.                 != sizeofColorRegister)
  114.                         return(IFFERR_WRITE);
  115.               ++tabw;
  116.               }
  117.     }
  118.    else if((bitspergun == 8)||(bitspergun == 32))
  119.     {
  120.     tab8 = (UBYTE *)colortable;
  121.     offs = (bitspergun == 8) ? 1 : 4;
  122.        for( ;  ncolors;  --ncolors )
  123.         {
  124.         cmapReg.red   = *tab8;
  125.         tab8 += offs;
  126.         cmapReg.green = *tab8;
  127.         tab8 += offs;
  128.         cmapReg.blue  = *tab8;
  129.         tab8 += offs;
  130.               if((WriteChunkBytes(iff, (BYTE *)&cmapReg, sizeofColorRegister))
  131.                 != sizeofColorRegister)
  132.                         return(IFFERR_WRITE);
  133.         }
  134.     }
  135.    else (error = CLIENT_ERROR);
  136.  
  137.    D(bug("Wrote registers, error = %ld\n",error));
  138.  
  139.    error = PopChunk(iff);
  140.    return(error);
  141.    }
  142.  
  143. /*---------- putbody ---------------------------------------------------*/
  144. /* NOTE: This implementation could be a LOT faster if it used more of the
  145.  * supplied buffer. It would make far fewer calls to IFFWriteBytes (and
  146.  * therefore to DOS Write).
  147.  *
  148.  * Incorporates modification by Jesper Steen Moller to accept source
  149.  * rows are wider than dest rows, one for the source bitplanes
  150.  * and one for the ILBM bitmap.
  151.  */
  152. long putbody(struct IFFHandle *iff, struct BitMap *bitmap, BYTE *mask,
  153.          BitMapHeader *bmhd, BYTE *buffer, LONG bufsize)
  154.    {
  155.    long error;
  156.    LONG rowBytes = bitmap->BytesPerRow;
  157.    LONG FileRowBytes = RowBytes(bmhd->w);
  158.    int dstDepth = bmhd->nPlanes;
  159.    UBYTE compression = bmhd->compression;
  160.    int planeCnt;        /* number of bit planes including mask */
  161.    register int iPlane, iRow;
  162.    register LONG packedRowBytes;
  163.    BYTE *buf;
  164.    BYTE *planes[MAXSAVEDEPTH + 1]; /* array of ptrs to planes & mask */
  165.  
  166.    D(bug("In PutBODY, rows = %ld, rowBytes = %ld, expected %ld\n", bitmap->Rows, rowBytes, RowBytes(bmhd->w)));
  167.  
  168.    if ( bufsize < MaxPackedSize(rowBytes)  ||    /* Must buffer a comprsd row*/
  169.         compression > cmpByteRun1  ||        /* bad arg */
  170.     bitmap->Rows != bmhd->h   ||        /* inconsistent */
  171.     rowBytes < FileRowBytes  ||    /* inconsistent*/
  172.     bitmap->Depth < dstDepth   ||        /* inconsistent */
  173.     dstDepth > MAXSAVEDEPTH )        /* too many for this routine*/
  174.       return(CLIENT_ERROR);
  175.  
  176.    planeCnt = dstDepth + (mask == NULL ? 0 : 1);
  177.  
  178.    /* Copy the ptrs to bit & mask planes into local array "planes" */
  179.    for (iPlane = 0; iPlane < dstDepth; iPlane++)
  180.       planes[iPlane] = (BYTE *)bitmap->Planes[iPlane];
  181.    if (mask != NULL)
  182.       planes[dstDepth] = mask;
  183.  
  184.    /* Write out a BODY chunk header */
  185.    if(error = PushChunk(iff, NULL, ID_BODY, IFFSIZE_UNKNOWN)) return(error);
  186.  
  187.    /* Write out the BODY contents */
  188.    for (iRow = bmhd->h; iRow > 0; iRow--)  {
  189.       for (iPlane = 0; iPlane < planeCnt; iPlane++)  {
  190.  
  191.          /* Write next row.*/
  192.          if (compression == cmpNone) {
  193.         if(WriteChunkBytes(iff,planes[iPlane],FileRowBytes) != FileRowBytes)
  194.         error = IFFERR_WRITE;
  195.             planes[iPlane] += rowBytes; /* Possibly skipping unused bytes */
  196.             }
  197.  
  198.          /* Compress and write next row.*/
  199.          else {
  200.             buf = buffer;
  201.             packedRowBytes = packrow(&planes[iPlane], &buf, FileRowBytes);
  202.             planes[iPlane] += rowBytes-FileRowBytes; /* Possibly skipping unused bytes */
  203.         if(WriteChunkBytes(iff,buffer,packedRowBytes) != packedRowBytes)
  204.         error = IFFERR_WRITE;
  205.             }
  206.  
  207.          if(error)    return(error);
  208.          }
  209.       }
  210.  
  211.    /* Finish the chunk */
  212.    error = PopChunk(iff);
  213.    return(error);
  214.    }
  215.