home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / Misc / TRSICAT.LZX / CATS_CD2_TRSI / Reference_Library / Devices / modules / loadilbm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-21  |  5.0 KB  |  209 lines

  1. /* loadilbm.c 05/91  C. Scheppner CBM
  2.  *
  3.  * High-level ILBM load routines
  4.  */
  5.  
  6. #include "iffp/ilbm.h"
  7. #include "iffp/ilbmapp.h"
  8.  
  9. extern struct Library *GfxBase;
  10.  
  11. /* loadbrush
  12.  *
  13.  * Passed an initialized ILBMInfo with a not-in-use ParseInfo.iff
  14.  *   IFFHandle and desired propchks, collectchks, and stopchks, and filename,
  15.  *   will load an ILBM as a brush, setting up ilbm->Bmhd, ilbm->camg,
  16.  *   ilbm->brbitmap, ilbm->colortable, and ilbm->ncolors
  17.  *
  18.  *   Note that ncolors may be more colors than you can LoadRGB4.
  19.  *   Use MIN(ilbm->ncolors,MAXAMCOLORREG) for color count if you change
  20.  *   the colors yourself using 1.3/2.0 functions.
  21.  *
  22.  * Returns 0 for success or an IFFERR (libraries/iffparse.h)
  23.  */
  24.  
  25. LONG loadbrush(struct ILBMInfo *ilbm, UBYTE *filename)
  26. {
  27. LONG error = 0L;
  28.  
  29.     if(!(ilbm->ParseInfo.iff))    return(CLIENT_ERROR);
  30.  
  31.     if(!(error = openifile((struct ParseInfo *)ilbm, filename, IFFF_READ)))
  32.     {
  33.     error = parseifile((struct ParseInfo *)ilbm,
  34.                 ID_FORM, ID_ILBM,
  35.                 ilbm->ParseInfo.propchks,
  36.                 ilbm->ParseInfo.collectchks,
  37.                 ilbm->ParseInfo.stopchks);
  38.     if((!error)||(error == IFFERR_EOC)||(error == IFFERR_EOF))
  39.         {
  40.         if(contextis(ilbm->ParseInfo.iff,ID_ILBM,ID_FORM))
  41.         {
  42.             if(error = createbrush(ilbm))   deletebrush(ilbm);
  43.         }
  44.         else
  45.         {
  46.         closeifile((struct ParseInfo *)ilbm);
  47.         message("Not an ILBM\n");
  48.         error = NOFILE;
  49.         }
  50.         }
  51.     }
  52.     return(error);
  53. }
  54.  
  55.  
  56. /* unloadbrush
  57.  *
  58.  * frees and close everything alloc'd/opened by loadbrush
  59.  */
  60. void unloadbrush(struct ILBMInfo *ilbm)
  61. {
  62.     closeifile((struct ParseInfo *)ilbm);
  63.     deletebrush(ilbm);
  64. }
  65.  
  66.  
  67. /* queryilbm
  68.  *
  69.  * Passed an initilized ILBMInfo with a not-in-use IFFHandle,
  70.  *   and a filename,
  71.  *   will open an ILBM, fill in ilbm->camg and ilbm->bmhd,
  72.  *   and close the ILBM.
  73.  *
  74.  * This allows you to determine if the ILBM is a size and
  75.  *   type you want to deal with.
  76.  *
  77.  * Returns 0 for success or an IFFERR (libraries/iffparse.h)
  78.  */
  79.  
  80. /* query just wants these chunks */
  81. LONG queryprops[] = { ID_ILBM, ID_BMHD,
  82.               ID_ILBM, ID_CAMG,
  83.                       TAG_DONE };
  84.  
  85. /* scan can stop when a CMAP or BODY is reached */
  86. LONG querystops[] = { ID_ILBM, ID_CMAP,
  87.               ID_ILBM, ID_BODY,
  88.               TAG_DONE };
  89.  
  90. LONG queryilbm(struct ILBMInfo *ilbm, UBYTE *filename)
  91. {
  92. LONG error = 0L;
  93. BitMapHeader *bmhd;
  94.  
  95.     if(!(ilbm->ParseInfo.iff))    return(CLIENT_ERROR);
  96.  
  97.     if(!(error = openifile((struct ParseInfo *)ilbm, filename, IFFF_READ)))
  98.     {
  99.     D(bug("queryilbm: openifile successful\n"));
  100.  
  101.     error = parseifile((struct ParseInfo *)ilbm,
  102.             ID_FORM, ID_ILBM,
  103.             queryprops, NULL, querystops);
  104.  
  105.     D(bug("queryilbm: after parseifile, error = %ld\n",error));
  106.  
  107.     if((!error)||(error == IFFERR_EOC)||(error == IFFERR_EOF))
  108.         {
  109.         if(contextis(ilbm->ParseInfo.iff,ID_ILBM,ID_FORM))
  110.         {
  111.         if(bmhd = (BitMapHeader*)
  112.             findpropdata(ilbm->ParseInfo.iff,ID_ILBM,ID_BMHD))
  113.             {
  114.             *(&ilbm->Bmhd) = *bmhd;
  115.             ilbm->camg = getcamg(ilbm);
  116.             }
  117.         else error = NOFILE;
  118.         }
  119.         else
  120.         {
  121.         message("Not an ILBM\n");
  122.         error = NOFILE;
  123.         }
  124.         }
  125.     closeifile(ilbm);
  126.     }
  127.     return(error);
  128. }
  129.  
  130.  
  131. /* loadilbm
  132.  *
  133.  * Passed a not-in-use IFFHandle, an initialized ILBMInfo, and filename,
  134.  *   will load an ILBM into your already opened ilbm->scr, setting up
  135.  *   ilbm->Bmhd, ilbm->camg, ilbm->colortable, and ilbm->ncolors
  136.  *   and loading the colors into the screen's viewport
  137.  *
  138.  *   Note that ncolors may be more colors than you can LoadRGB4.
  139.  *   Use MIN(ilbm->ncolors,MAXAMCOLORREG) for color count if you change
  140.  *   the colors yourself using 1.3/2.0 functions.
  141.  *
  142.  * Returns 0 for success or an IFFERR (libraries/iffparse.h)
  143.  *
  144.  * NOTE - loadilbm() keeps the IFFHandle open so you can copy
  145.  *   or examine other chunks.  You must call closeifile(iff,ilbm)
  146.  *   to close the file and deallocate the parsed context
  147.  *
  148.  */
  149.  
  150. LONG loadilbm(struct ILBMInfo *ilbm, UBYTE *filename)
  151. {
  152. LONG error = 0L;
  153.  
  154.  
  155.     D(bug("loadilbm:\n"));
  156.  
  157.     if(!(ilbm->ParseInfo.iff))    return(CLIENT_ERROR);
  158.     if(!ilbm->scr)        return(CLIENT_ERROR);
  159.  
  160.     if(!(error = openifile((struct ParseInfo *)ilbm, filename, IFFF_READ)))
  161.     {
  162.     D(bug("loadilbm: openifile successful\n"));
  163.  
  164.     error = parseifile((struct ParseInfo *)ilbm,
  165.             ID_FORM, ID_ILBM,
  166.             ilbm->ParseInfo.propchks,
  167.             ilbm->ParseInfo.collectchks,
  168.             ilbm->ParseInfo.stopchks);
  169.  
  170.     D(bug("loadilbm: after parseifile, error = %ld\n",error));
  171.  
  172.     if((!error)||(error == IFFERR_EOC)||(error == IFFERR_EOF))
  173.         {
  174.         if(contextis(ilbm->ParseInfo.iff,ID_ILBM,ID_FORM))
  175.         {
  176.             error = loadbody(ilbm->ParseInfo.iff,
  177.                     &ilbm->scr->BitMap, &ilbm->Bmhd);
  178.  
  179.         D(bug("loadilbm: after loadbody, error = %ld\n",error));
  180.  
  181.         if(!error)
  182.             {
  183.             if(!(getcolors(ilbm)))
  184.                 LoadRGB4(&ilbm->scr->ViewPort,ilbm->colortable,
  185.                     MIN(ilbm->ncolors,MAXAMCOLORREG));
  186.             } 
  187.         }
  188.         else
  189.         {
  190.         closeifile((struct ParseInfo *)ilbm);
  191.         message("Not an ILBM\n");
  192.         error = NOFILE;
  193.         }
  194.         }
  195.     }
  196.     return(error);
  197. }
  198.  
  199.  
  200. /* unloadilbm
  201.  *
  202.  * frees and closes everything allocated by loadilbm
  203.  */
  204. void unloadilbm(struct ILBMInfo *ilbm)
  205. {
  206.     closeifile((struct ParseInfo *)ilbm);
  207.     freecolors(ilbm);
  208. }
  209.