home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 543a.lha / Nebula / source.LZH / source / loadpic.c < prev    next >
C/C++ Source or Header  |  1991-06-10  |  5KB  |  239 lines

  1. /*
  2.  
  3. ------------------------------------------------------------------
  4.  
  5. Black Nebula
  6.  
  7. File :                loadpic.c
  8. Programmer:        Colin Adams/Someone else
  9. Date:                28/4/91
  10. Last Modified :    10/6/91
  11.  
  12. Description:
  13.  
  14. Loads an IFF picture file into both bitmaps of the buffered
  15. display.  Based on something so old I can't remember where I got
  16. this code.  I've modified it a few times though...
  17.  
  18. ------------------------------------------------------------------
  19.  
  20. */
  21.  
  22. /*    ------------------------------------------------------------------
  23.         Defines and Includes
  24.         ------------------------------------------------------------------
  25. */
  26.  
  27. #define AMIGA_INCLUDES
  28. #include "3d.h"
  29.  
  30. #define ID_FORM MakeID('F','O','R','M')
  31. #define ID_ILBM MakeID('I','L','B','M')
  32. #define ID_BMHD MakeID('B','M','H','D')
  33. #define ID_CAMG MakeID('C','A','M','G')
  34. #define ID_CMAP MakeID('C','M','A','P')
  35. #define ID_BODY MakeID('B','O','D','Y')
  36.  
  37. #define cmpByteRun1 1
  38.  
  39. /*    ------------------------------------------------------------------
  40.         Macros
  41.         ------------------------------------------------------------------
  42. */
  43.  
  44. #define ROUNDODDUP(a) (((a)+1)&(~1L))
  45. #define MakeID(a,b,c,d) ((a)<<24L | (b)<<16L | (c)<<8 | (d))
  46. #define SafeRead(a,b,c) if (Read((BPTR)a,(APTR)b,c)==-1L) { Close((BPTR)a); return(NULL); }
  47.  
  48. /*    ------------------------------------------------------------------
  49.         Typedefs
  50.         ------------------------------------------------------------------
  51. */
  52.  
  53. typedef struct
  54. {
  55.   long ckID, ckSize;
  56. } Chunk;
  57.  
  58. typedef struct
  59. {
  60.   short w, h, x, y;
  61.   char  nPlanes, masking, compression, pad1;
  62.   short transparentColor;
  63.   char  xAspect, yAspect;
  64.   short pageWidth, pageHeight;
  65. } BitMapHeader;
  66.  
  67.  
  68. /*    ------------------------------------------------------------------
  69.         Data Structures
  70.         ------------------------------------------------------------------
  71. */
  72.  
  73. struct FileHandle *f79_outfp;
  74. BitMapHeader f79_bmhd;
  75. char *f79_bufstart;
  76. Chunk f79_header;
  77.  
  78. /*    ------------------------------------------------------------------
  79.         External Structures
  80.         ------------------------------------------------------------------
  81. */
  82.  
  83. extern struct BitMap my_bit_map, back_bit_map;
  84. extern struct IntuitionBase *IntuitionBase;
  85. extern struct GfxBase *GfxBase;
  86.  
  87. /*    ------------------------------------------------------------------
  88.         Code
  89.         ------------------------------------------------------------------
  90. */
  91.  
  92.  
  93. void F79_CleanUpandExit(void)
  94. {
  95.    if(f79_bufstart)
  96.       FreeMem(f79_bufstart,(long)f79_header.ckSize); 
  97.    if(f79_outfp)
  98.       Close((BPTR)f79_outfp); 
  99. }
  100.  
  101. char F79_ReadILBM(char *fspec)
  102. {
  103.   struct FileHandle *fp;
  104.   char colormap[32][3], *sourcebuf;
  105.   short colorcount;
  106.   long id, ViewModes = 0;
  107.  
  108.   fp=(struct FileHandle *)Open(fspec,MODE_OLDFILE);    
  109.   if (fp==0)
  110.   {
  111.         F79_CleanUpandExit();
  112.         return 1;
  113.   }
  114.   
  115.     SafeRead(fp,&f79_header,(long)sizeof(f79_header));
  116.   
  117.     if (f79_header.ckID!=ID_FORM) 
  118.   { 
  119.         Close((BPTR)fp); 
  120.         F79_CleanUpandExit(); 
  121.         return 1;
  122.   }
  123.  
  124.   SafeRead(fp,&id,(long)sizeof(id));
  125.   if (id!=ID_ILBM) 
  126.   { 
  127.         Close((BPTR)fp); 
  128.         F79_CleanUpandExit(); 
  129.         return 1;
  130.   }
  131.  
  132.   for (;;)
  133.     {
  134.     SafeRead(fp,&f79_header,(long)sizeof(f79_header));
  135.  
  136.     if (f79_header.ckID==ID_BODY) break;
  137.  
  138.     switch(f79_header.ckID)
  139.       {
  140.       case ID_BMHD: SafeRead(fp,&f79_bmhd,(long)sizeof(f79_bmhd));
  141.                     break;
  142.  
  143.       case ID_CMAP: SafeRead(fp,&colormap[0][0],(long)f79_header.ckSize);
  144.                     colorcount=f79_header.ckSize/3;
  145.                     break;
  146.  
  147.       case ID_CAMG: SafeRead(fp,&ViewModes,(long)f79_header.ckSize);
  148.                     break;
  149.  
  150.       default:      Seek((BPTR)fp,ROUNDODDUP(f79_header.ckSize),OFFSET_CURRENT);
  151.     }
  152.   }
  153.  
  154.   /* Read planes into RAM for ease if decompression */
  155.      
  156.   sourcebuf=f79_bufstart=(char *)AllocMem((long)f79_header.ckSize,MEMF_PUBLIC|MEMF_FAST);
  157.   if (sourcebuf==0L)
  158.   {
  159.      sourcebuf=f79_bufstart=(char *)AllocMem((long)f79_header.ckSize,MEMF_PUBLIC|MEMF_CHIP);
  160.      if (!sourcebuf)
  161.      {
  162.         F79_CleanUpandExit();
  163.          return 1;
  164.      }
  165.   }
  166.  
  167.   SafeRead(fp,sourcebuf,(long)f79_header.ckSize);
  168.   Close((BPTR)fp);
  169.     return 0;
  170. }      
  171.  
  172. void F79_Expand(BitMapHeader *bmhd, char *sourcebuf)
  173. {
  174.   char n,*destbuf, *tempbuf;
  175.   short plane,linelen, rowbytes, i;
  176.  
  177.   linelen = bmhd->w/8;
  178.  
  179.   for (i=0;i<bmhd->h;i++)
  180.     for (plane=0;plane<bmhd->nPlanes;plane++)
  181.       {
  182.          int j;
  183.          
  184.          for(j=0; j<2; j++)
  185.          {
  186.             if(!j)
  187.             {
  188.               destbuf = (char *)(my_bit_map.Planes[plane])+linelen*i;
  189.                 tempbuf = sourcebuf;
  190.             }
  191.             else
  192.             {
  193.                 destbuf = (char *)(back_bit_map.Planes[plane])+linelen*i;
  194.                 sourcebuf = tempbuf;
  195.             }
  196.  
  197.           if (bmhd->compression==cmpByteRun1)
  198.             {
  199.             rowbytes = linelen;
  200.  
  201.             while (rowbytes)
  202.                 {
  203.                 n = *sourcebuf++; /* fetch block run marker */
  204.  
  205.                   if (n>=0)
  206.                     {
  207.                    movmem(sourcebuf,destbuf,(unsigned int)++n);
  208.                         rowbytes -= n;
  209.                    destbuf += n; sourcebuf += n;
  210.                 }
  211.                 else
  212.                     {
  213.                         n = -n+1;
  214.                         rowbytes -= n;
  215.                         setmem(destbuf,(unsigned int)n,(unsigned int)*sourcebuf++);
  216.                         destbuf+=n;
  217.                   }
  218.                 }
  219.           }
  220.           else
  221.              {
  222.                 movmem(sourcebuf,destbuf,(unsigned int)linelen);
  223.                 sourcebuf += linelen;
  224.           }
  225.         }
  226.     }
  227. }
  228.  
  229. void loadpic(char *argv)
  230. {
  231.     char error;
  232.  
  233.     if(error = F79_ReadILBM(argv))
  234.         return;
  235.  
  236.     F79_Expand(&f79_bmhd,f79_bufstart);
  237. }
  238.  
  239. /* end of module loadpic.c */