home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / DevCon / Atlanta_1990 / Atlanta-Devcon.2 / Libraries / IFFParse / Examples / tiff.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-26  |  4.2 KB  |  216 lines

  1. /*  :ts=8 bk=0
  2.  *
  3.  * tiff.c:    ILBM scanner utilizing builtin chunk handlers (PropChunk(),
  4.  *        StopChunk(), CollectionChunk(), StopOnExit()).
  5.  *
  6.  * All the code needed for an ILBM reader, except the stuff to process the
  7.  * ILBM itself.
  8.  *
  9.  * Stuart Ferguson (Created)                8807.??
  10.  * ewhac: Adjusted for new semantics            8808.16
  11.  * ewhac: Adjustments; added "Not an ILBM" feature.    8811.02
  12.  * shf:   Updated to Dec 88 design revision        8812.15
  13.  * ewhac: Updated to 1.4 Beta                8912.06
  14.  * ewhac: Latticeification                9005.30
  15.  */
  16. #include <exec/types.h>
  17. #include <libraries/dos.h>
  18. #include <libraries/iffparse.h>
  19. #include <clib/exec_protos.h>
  20. #include <clib/dos_protos.h>
  21. #include "iffparse_protos.h"
  22. #include "iffparse.p"
  23.  
  24. #define    ID_ILBM    MAKE_ID('I','L','B','M')
  25. #define    ID_BODY    MAKE_ID('B','O','D','Y')
  26. #define    ID_BMHD    MAKE_ID('B','M','H','D')
  27. #define    ID_CMAP    MAKE_ID('C','M','A','P')
  28. #define    ID_CAMG    MAKE_ID('C','A','M','G')
  29. #define    ID_CRNG    MAKE_ID('C','R','N','G')
  30.  
  31.  
  32. /*
  33.  * Forward function declarations.  (I hate ANSI.)
  34.  */
  35. void ProcessCrng (struct IFFHandle *);
  36. void ProcessBody (struct IFFHandle *);
  37.  
  38.  
  39. static long    ilbmprops[] = {
  40.     ID_ILBM, ID_BMHD,
  41.     ID_ILBM, ID_CMAP,
  42.     ID_ILBM, ID_CAMG
  43. };
  44.  
  45. struct Library    *IFFParseBase;
  46.  
  47.  
  48. main (argc, argv)
  49. int    argc;
  50. char    **argv;
  51. {
  52.     struct IFFHandle    *iff;
  53.     struct ContextNode    *cn;
  54.     long            error;
  55.     char            foundbody = 0;
  56.  
  57.     if (!(IFFParseBase = OpenLibrary ("iffparse.library", 0L))) {
  58.         puts ("Cannot open library.");
  59.         goto die;
  60.     }
  61.  
  62.     /*
  63.      * Create IFF file and open a DOS stream on it.
  64.      */
  65.     if (!(iff = AllocIFF())) {
  66.         puts ("AllocIFF() failed.");
  67.         goto die;
  68.     }
  69.  
  70.     if (!(iff -> iff_Stream = Open (argv[1], MODE_OLDFILE))) {
  71.         puts ("File open failed.");
  72.         goto die;
  73.     }
  74.     InitIFFasDOS (iff);
  75.  
  76.     /*
  77.      * Declare property, collection and stop chunks.
  78.      */
  79.     if (error = PropChunks (iff, ilbmprops, 3L))
  80.         goto err;
  81.     if (error = StopChunk (iff, ID_ILBM, ID_BODY))
  82.         goto err;
  83.     if (error = CollectionChunk (iff, ID_ILBM, ID_CRNG))
  84.         goto err;
  85.  
  86.     /*
  87.      * We want to stop at the end of an ILBM context.
  88.      */
  89.     if (error = StopOnExit (iff, ID_ILBM, ID_FORM))
  90.         goto err;
  91.  
  92.     if (error = OpenIFF (iff, IFFF_READ))
  93.         goto err;
  94.  
  95.     /*
  96.      * Take first parse step to enter main chunk.
  97.      */
  98.     if (error = ParseIFF (iff, IFFPARSE_STEP))
  99.         goto err;
  100.  
  101.     /*
  102.      * Test the chunk info to see if this is a simple ILBM.
  103.      */
  104.     if (!(cn = CurrentChunk (iff))) {
  105.         puts ("No top chunk!");
  106.         goto die;
  107.     }
  108.     if (cn -> cn_ID != ID_FORM  ||  cn -> cn_Type != ID_ILBM)
  109.         puts ("Not a FORM ILBM.  Blundering on anyway...");
  110.  
  111.     while (!error) {
  112.         /*
  113.          * ParseIFF() will return on BODY chunk or end of
  114.          * context for an ILBM FORM.
  115.          */
  116.         error = ParseIFF (iff, IFFPARSE_SCAN);
  117.  
  118.         /*
  119.          * Test for end of context condition and process
  120.          * collected CRNG chunks here.
  121.          */
  122.         if (error == IFFERR_EOC) {
  123.             ProcessCrng (iff);
  124.             /* error = 0;
  125.                continue;
  126.              */
  127.             break;
  128.         }
  129.  
  130.         /*
  131.          * Other values for error are real errors.
  132.          */
  133.         if (error)
  134.             break;
  135.  
  136.         ProcessBody (iff);
  137.         foundbody = 1;
  138.     }
  139. err:
  140.     if (error == IFFERR_EOC) {
  141.         if (foundbody)
  142.             puts ("File scan complete.");
  143.         else
  144.             puts ("Failed to find an ILBM BODY chunk.");
  145.     } else if (error == IFFERR_EOF)
  146.         puts ("Failed to find a FORM ILBM.");
  147.     else
  148.         printf ("File scan aborted (%ld)\n", error);
  149.  
  150. die:
  151.     if (iff) {
  152.         CloseIFF (iff);
  153.         if (iff -> iff_Stream)
  154.             Close (iff -> iff_Stream);
  155.         FreeIFF (iff);
  156.     }
  157.  
  158.     if (IFFParseBase)    CloseLibrary (IFFParseBase);
  159. }
  160.  
  161.  
  162. void
  163. ProcessBody (iff)
  164. struct IFFHandle *iff;
  165. {
  166.     register struct StoredProperty    *sp;
  167.  
  168.     if (sp = FindProp (iff, ID_ILBM, ID_BMHD))
  169.         printf ("BMHD chunk, size: %ld\n", sp -> sp_Size);
  170.     else
  171.         puts ("No BMHD chunk.");
  172.  
  173.     if (sp = FindProp (iff, ID_ILBM, ID_CMAP))
  174.         printf ("CMAP chunk, size: %ld\n", sp -> sp_Size);
  175.     else
  176.         puts ("No CMAP chunk.");
  177.  
  178.     if (sp = FindProp (iff, ID_ILBM, ID_CAMG))
  179.         printf ("CAMG chunk, size: %ld\n", sp -> sp_Size);
  180.     else
  181.         puts ("No CAMG chunk.");
  182.  
  183.     printf ("BODY chunk, size: %ld\n", CurrentChunk (iff) -> cn_Size);
  184. }
  185.  
  186.  
  187. void
  188. ProcessCrng (iff)
  189. struct IFFHandle *iff;
  190. {
  191.     register struct CollectionItem    *ci;
  192.  
  193.     ci = FindCollection (iff, ID_ILBM, ID_CRNG);
  194.     if (!ci) {
  195.         puts ("No CRNG chunks.");
  196.         return;
  197.     }
  198.  
  199.     for ( ;  ci;  ci = ci->ci_Next) {
  200.         printf ("CRNG chunk, size: %ld\n", ci -> ci_Size);
  201.     }
  202. }
  203.  
  204. /*
  205.  * Disable Lattice's default ^C trap.
  206.  */
  207. chkabort ()
  208. {
  209.     return (0);
  210. }
  211.  
  212. CXBRK ()
  213. {
  214.     return (0);
  215. }
  216.