home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 609b.lha / MandelSquare_v1.3 / Source.LZH / Source / ReadILBM.c < prev    next >
C/C++ Source or Header  |  1992-01-05  |  6KB  |  326 lines

  1. /** Revision Header * Header built automatically - do not edit! *************
  2.  *
  3.  *    (C) Copyright 1991 by Olaf `Olsen' Barthel, all rights reserved
  4.  *
  5.  *    Name .....: ReadILBM.c
  6.  *    Created ..: Monday 26-Aug-91 11:20
  7.  *    Revision .: 1
  8.  *
  9.  *    Date            Author          Comment
  10.  *    =========       ========        ====================
  11.  *    26-Aug-91    Olsen        Created this file!
  12.  *
  13.  ***************************************************************************/
  14.  
  15.     /* OpenImageFile(UBYTE *Name):
  16.      *
  17.      *    Open an IFF-ILBM file with given name.
  18.      */
  19.  
  20. struct IFFHandle * __regargs
  21. OpenImageFile(UBYTE *Name)
  22. {
  23.     struct IFFHandle *Handle;
  24.  
  25.     if(Handle = AllocIFF())
  26.     {
  27.         if(Handle -> iff_Stream = Open(Name,MODE_OLDFILE))
  28.         {
  29.             InitIFFasDOS(Handle);
  30.  
  31.             if(!OpenIFF(Handle,IFFF_READ))
  32.                 return(Handle);
  33.  
  34.             Close(Handle -> iff_Stream);
  35.         }
  36.  
  37.         FreeIFF(Handle);
  38.     }
  39.  
  40.     return(NULL);
  41. }
  42.  
  43.     /* CloseImageFile(struct IFFHandle *Handle):
  44.      *
  45.      *    Close an IFF-ILBM file opened by OpenImageFile().
  46.      */
  47.  
  48. VOID __regargs
  49. CloseImageFile(struct IFFHandle *Handle)
  50. {
  51.     CloseIFF(Handle);
  52.  
  53.     Close(Handle -> iff_Stream);
  54.  
  55.     FreeIFF(Handle);
  56. }
  57.  
  58.     /* ReadImageBody():
  59.      *
  60.      *    Reads the image data contained in a standard 4 bit IFF-ILBM
  61.      *    image file.
  62.      */
  63.  
  64. BYTE __regargs
  65. ReadImageBody(struct IFFHandle *Handle,struct BitMap *BitMap,struct BitMapHeader *BitMapHeader)
  66. {
  67.     USHORT             i,j,k;
  68.     UBYTE             Value,SoFar,Compr,Depth;
  69.     BYTE             ChkVal;
  70.     LONG             Height,Width;
  71.     PLANEPTR         Planes[9];
  72.  
  73.     BYTE            *AuxBuff,*AuxBuff2;
  74.  
  75.     struct ContextNode    *ContextNode;
  76.  
  77.     if(ContextNode = CurrentChunk(Handle))
  78.     {
  79.         Width    = BitMap -> BytesPerRow;
  80.         Height    = BitMap -> Rows;
  81.         Depth    = BitMapHeader -> nPlanes;
  82.         Compr    = BitMapHeader -> compression;
  83.  
  84.         if(Compr > 1 || Depth < 1)
  85.             return(FALSE);
  86.  
  87.         for(i = 0 ; i < Depth ; i++)
  88.             Planes[i] = BitMap -> Planes[i];
  89.  
  90.         for(i = Depth ; i < 9 ; i++)
  91.             Planes[i] = NULL;
  92.  
  93.         if(BitMapHeader -> masking == 1)
  94.             Depth++;
  95.  
  96.         if(AuxBuff = (BYTE *)AllocVec(ContextNode -> cn_Size,MEMF_PUBLIC))
  97.         {
  98.             if(ReadChunkBytes(Handle,AuxBuff,ContextNode -> cn_Size) == ContextNode -> cn_Size)
  99.             {
  100.                 AuxBuff2 = AuxBuff;
  101.  
  102.                 if(Compr == 0)
  103.                 {
  104.                     for(k = 0 ; k < Height ; k++)
  105.                     {
  106.                         for(j = 0 ; j < Depth ; j++)
  107.                         {
  108.                             if(Planes[j])
  109.                             {
  110.                                 CopyMem(AuxBuff,Planes[j],Width);
  111.                                 Planes[j] += Width;
  112.                             }
  113.  
  114.                             AuxBuff += Width;
  115.                         }
  116.                     }
  117.                 }
  118.  
  119.                 if(Compr == 1)
  120.                 {
  121.                     for(k = 0 ; k < Height ; k++)
  122.                     {
  123.                         for(j = 0 ; j < Depth ; j++)
  124.                         {
  125.                             for(SoFar = 0 ; SoFar < Width ; )
  126.                             {
  127.                                 ChkVal = *AuxBuff;
  128.                                 AuxBuff++;
  129.  
  130.                                 if(ChkVal > 0)
  131.                                 {
  132.                                     if(Planes[j])
  133.                                     {
  134.                                         CopyMem(AuxBuff,Planes[j],ChkVal + 1);
  135.  
  136.                                         Planes[j] += ChkVal + 1;
  137.                                     }
  138.  
  139.                                     AuxBuff += ChkVal + 1;
  140.  
  141.                                     SoFar += ChkVal + 1;
  142.                                 }
  143.                                 else
  144.                                 {
  145.                                     if(ChkVal != (WORD)-128)
  146.                                     {
  147.                                         Value = *AuxBuff;
  148.                                         AuxBuff++;
  149.  
  150.                                         for(i = 0 ; i <= -ChkVal ; i++)
  151.                                         {
  152.                                             if(Planes[j])
  153.                                                 *Planes[j]++ = Value;
  154.  
  155.                                             SoFar++;
  156.                                         }
  157.                                     }
  158.                                 }
  159.                             }
  160.                         }
  161.                     }
  162.                 }
  163.  
  164.                 FreeVec(AuxBuff2);
  165.  
  166.                 return(TRUE);
  167.             }
  168.  
  169.             FreeVec(AuxBuff);
  170.         }
  171.     }
  172.  
  173.     return(FALSE);
  174. }
  175.  
  176.     /* ReadImageHeader():
  177.      *
  178.      *    Reads the header information, viewmodes and colours contained
  179.      *    in an IFF-ILBM file.
  180.      */
  181.  
  182. BYTE __regargs
  183. ReadImageHeader(struct IFFHandle *Handle,ULONG *ViewModes,BYTE *NumCols,UWORD *Colours,struct BitMapHeader *BitMapHeader,struct MandelInfo *MandelInfo)
  184. {
  185.     STATIC ULONG ImageProps[] =
  186.     {
  187.         'ILBM','BMHD',
  188.         'ILBM','CMAP',
  189.         'ILBM','CAMG',
  190.         'ILBM','MAND'
  191.     };
  192.  
  193.     struct StoredProperty *Prop;
  194.  
  195.     if(!PropChunks(Handle,&ImageProps[0],4))
  196.     {
  197.         if(!StopChunk(Handle,'ILBM','BODY'))
  198.         {
  199.             if(!ParseIFF(Handle,IFFPARSE_SCAN))
  200.             {
  201.                 if(Prop = FindProp(Handle,'ILBM','BMHD'))
  202.                 {
  203.                     CopyMem(Prop -> sp_Data,BitMapHeader,sizeof(struct BitMapHeader));
  204.  
  205.                     if(BitMapHeader -> nPlanes > 0 && BitMapHeader -> nPlanes <= 8)
  206.                     {
  207.                         if(MandelInfo)
  208.                         {
  209.                             if(Prop = FindProp(Handle,'ILBM','MAND'))
  210.                             {
  211.                                 MandelInfo -> Iterations = 32;
  212.  
  213.                                 CopyMem(Prop -> sp_Data,MandelInfo,Prop -> sp_Size);
  214.                             }
  215.                             else
  216.                                 MandelInfo -> Iterations = 0;
  217.                         }
  218.  
  219.                         if(ViewModes)
  220.                         {
  221.                             extern struct GfxBase *GfxBase;
  222.  
  223.                             *ViewModes = NULL;
  224.  
  225.                             if(BitMapHeader -> pageHeight > ((GfxBase -> DisplayFlags & PAL) ? 256 : 200))
  226.                                 *ViewModes |= LACE;
  227.  
  228.                             if(BitMapHeader -> pageWidth >= 640)
  229.                                 *ViewModes |= HIRES;
  230.  
  231.                             if(BitMapHeader -> nPlanes == 6)
  232.                                 *ViewModes |= HAM;
  233.  
  234.                             if(Prop = FindProp(Handle,'ILBM','CAMG'))
  235.                                 *ViewModes = *(ULONG *)Prop -> sp_Data;
  236.  
  237.                             *ViewModes &= ~(SPRITES | VP_HIDE | GENLOCK_AUDIO | GENLOCK_VIDEO);
  238.  
  239.                             if(ModeNotAvailable(*ViewModes))
  240.                                 *ViewModes &= 0xFFFF;
  241.  
  242.                             if(ModeNotAvailable(*ViewModes))
  243.                                 return(FALSE);
  244.                         }
  245.  
  246.                         if(NumCols && Colours)
  247.                         {
  248.                             UBYTE    *ColourArray;
  249.                             WORD     i,R,G,B;
  250.  
  251.                             if(!(Prop = FindProp(Handle,'ILBM','CMAP')))
  252.                                 return(FALSE);
  253.  
  254.                             ColourArray = (UBYTE *)Prop -> sp_Data;
  255.  
  256.                             *NumCols = Prop -> sp_Size / 3;
  257.  
  258.                             for(i = 0 ; i < *NumCols ; i++)
  259.                             {
  260.                                 R = ColourArray[i * 3 + 0] >> 4;
  261.                                 G = ColourArray[i * 3 + 1] >> 4;
  262.                                 B = ColourArray[i * 3 + 2] >> 4;
  263.  
  264.                                 Colours[i] = (R << 8) | (G << 4) | (B);
  265.                             }
  266.                         }
  267.  
  268.                         return(TRUE);
  269.                     }
  270.                 }
  271.             }
  272.         }
  273.     }
  274.  
  275.     return(FALSE);
  276. }
  277.  
  278.     /* CreateBitMap(BYTE Depth,UWORD Width,UWORD Height):
  279.      *
  280.      *    Creates and initializes a BitMap structure with
  281.      *    given dimensions.
  282.      */
  283.  
  284. struct BitMap * __regargs
  285. CreateBitMap(BYTE Depth,UWORD Width,UWORD Height)
  286. {
  287.     struct BitMap    *BitMap;
  288.     BYTE         i;
  289.  
  290.     if(BitMap = (struct BitMap *)AllocVec(sizeof(struct BitMap),MEMF_PUBLIC|MEMF_CLEAR))
  291.     {
  292.         LONG PlaneSize;
  293.  
  294.         InitBitMap(BitMap,Depth,Width,Height);
  295.  
  296.         PlaneSize = BitMap -> BytesPerRow * BitMap -> Rows;
  297.  
  298.         if(!(BitMap -> Planes[0] = AllocVec(PlaneSize * BitMap -> Depth,MEMF_CHIP|MEMF_PUBLIC|MEMF_CLEAR)))
  299.         {
  300.             FreeVec(BitMap);
  301.  
  302.             return(NULL);
  303.         }
  304.         else
  305.         {
  306.             for(i = 1 ; i < Depth ; i++)
  307.                 BitMap -> Planes[i] = &BitMap -> Planes[i - 1][PlaneSize];
  308.         }
  309.     }
  310.  
  311.     return(BitMap);
  312. }
  313.  
  314.     /* DeleteBitMap(struct BitMap *BitMap):
  315.      *
  316.      *    Deallocates any BitMap allocated by CreateBitMap().
  317.      */
  318.  
  319. VOID __regargs
  320. DeleteBitMap(struct BitMap *BitMap)
  321. {
  322.     FreeVec(BitMap -> Planes[0]);
  323.  
  324.     FreeVec(BitMap);
  325. }
  326.