home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Misc / CBMDevKit4.dms / CBMDevKit4.adf / IFF / newiff39.lha / newiff39 / apps / ILBMLoad / ILBMLoad.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-28  |  6.2 KB  |  246 lines

  1. /* ILBMLoad.c 04/92  C. Scheppner CBM
  2.  *
  3.  * Example which
  4.  *  - first queries an ILBM to determine size and mode
  5.  *  - then opens an appropriate screen and window
  6.  *  - then loads the ILBM into the already opened screen
  7.  *
  8.  * For clipboard, use filename -c[unit] (like -c, -c1, -c2, etc.)
  9.  *
  10.  * requires linkage with several IFF modules
  11.  * see Makefile
  12.  *
  13.  * 39.10 - use screen.c maxdisplaydepth() for V37 and higher, not MAXAMDEPTH
  14.  */
  15. #define INTUI_V36_NAMES_ONLY
  16.  
  17. #include "iffp/ilbmapp.h"
  18.  
  19.  
  20. #ifdef __SASC
  21. void __chkabort(void) {}          /* Disable SAS CTRL-C checking. */
  22. #else
  23. #ifdef LATTICE
  24. void chkabort(void) {}            /* Disable LATTICE CTRL-C checking */
  25. #endif
  26. #endif
  27.  
  28.  
  29. void cleanup(void);
  30. void bye(UBYTE *s,int error);
  31.  
  32. #define MINARGS 2
  33.  
  34. #include "ilbmload_rev.h"
  35. UBYTE vers[] = VERSTAG;
  36. UBYTE Copyright[] = VERS " Load ILBM into screen - Freely Redistributable";
  37.  
  38. char *usage = "Usage: ILBMLoad ilbmname (-c[unit] for clipboard";
  39.  
  40.  
  41. struct Library *IntuitionBase  = NULL;
  42. struct Library *GfxBase        = NULL;
  43. struct Library *IFFParseBase   = NULL;
  44.  
  45. /* Note - these fields are also available in the ILBMInfo structure */
  46. struct   Screen         *scr;         /* for ptr to screen structure */
  47. struct   Window         *win;         /* for ptr to window structure */
  48. struct   RastPort       *wrp;         /* for ptr to RastPort  */
  49. struct   ViewPort       *vp;          /* for ptr to Viewport  */
  50.  
  51. struct   IntuiMessage   *msg;
  52.  
  53. struct   NewWindow      mynw = {
  54.    0, 0,                                  /* LeftEdge and TopEdge */
  55.    0, 0,                                /* Width and Height */
  56.    (UBYTE)-1, (UBYTE)-1,                  /* DetailPen and BlockPen */
  57.    IDCMP_VANILLAKEY | IDCMP_MOUSEBUTTONS, /* IDCMP Flags with Flags below */
  58.    WFLG_BACKDROP | WFLG_BORDERLESS |
  59.    WFLG_SMART_REFRESH | WFLG_NOCAREREFRESH |
  60.    WFLG_ACTIVATE | WFLG_RMBTRAP,
  61.    NULL, NULL,                            /* Gadget and Image pointers */
  62.    NULL,                                  /* Title string */
  63.    NULL,                                  /* Screen ptr null till opened */
  64.    NULL,                                  /* BitMap pointer */
  65.    50, 20,                                /* MinWidth and MinHeight */
  66.    0 , 0,                                 /* MaxWidth and MaxHeight */
  67.    CUSTOMSCREEN                           /* Type of window */
  68.    };
  69.  
  70.  
  71. BOOL   FromWb;
  72.  
  73.  
  74. /* ILBM Property chunks to be grabbed
  75.  * List BMHD, CMAP and CAMG first so we can skip them when we write
  76.  * the file back out (they will be written out with separate code)
  77.  */
  78. LONG    ilbmprops[] = {
  79.         ID_ILBM, ID_BMHD,
  80.         ID_ILBM, ID_CMAP,
  81.         ID_ILBM, ID_CAMG,
  82.         ID_ILBM, ID_CCRT,
  83.         ID_ILBM, ID_AUTH,
  84.         ID_ILBM, ID_Copyright,
  85.         TAG_DONE
  86.         };
  87.  
  88. /* ILBM Collection chunks (more than one in file) to be gathered */
  89. LONG    ilbmcollects[] = {
  90.         ID_ILBM, ID_CRNG,
  91.         TAG_DONE
  92.         };
  93.  
  94. /* ILBM Chunk to stop on */
  95. LONG    ilbmstops[] = {
  96.         ID_ILBM, ID_BODY,
  97.         TAG_DONE
  98.         };
  99.  
  100.  
  101. UBYTE nomem[]  = "Not enough memory\n";
  102. UBYTE noiffh[] = "Can't alloc iff\n";
  103.  
  104.  
  105.  
  106. /* For our allocated ILBM frame */
  107. struct ILBMInfo  *ilbm;
  108.  
  109.  
  110. /* 
  111.  * MAIN 
  112.  */
  113. void main(int argc, char **argv)
  114.    {
  115.    UBYTE *ilbmname=NULL;
  116.    UWORD maxdepth;
  117.    LONG error = 0L;
  118.  
  119.    FromWb = argc ? FALSE : TRUE;
  120.  
  121.    if((argc<MINARGS)||(argv[argc-1][0]=='?'))
  122.     {
  123.     printf("%s\n%s\n",Copyright,usage);
  124.         bye("",RETURN_OK);
  125.     }
  126.  
  127.    ilbmname = argv[1];
  128.  
  129.    /* Open Libraries */
  130.  
  131.    if(!(IntuitionBase = OpenLibrary("intuition.library", 0)))
  132.       bye("Can't open intuition library.\n",RETURN_WARN);
  133.       
  134.    if(!(GfxBase = OpenLibrary("graphics.library",0)))
  135.       bye("Can't open graphics library.\n",RETURN_WARN);
  136.  
  137.    if(!(IFFParseBase = OpenLibrary("iffparse.library",0)))
  138.       bye("Can't open iffparse library.\n",RETURN_WARN);
  139.  
  140.  
  141.  
  142. /* 
  143.  * Alloc one ILBMInfo struct
  144.  */
  145.     if(!(ilbm = (struct ILBMInfo *)
  146.     AllocMem(sizeof(struct ILBMInfo),MEMF_PUBLIC|MEMF_CLEAR))) 
  147.         bye(nomem,RETURN_FAIL);
  148.  
  149. /*
  150.  * Here we set up our ILBMInfo fields for our
  151.  * application.
  152.  * Above we have defined the propery and collection chunks
  153.  * we are interested in (some required like BMHD)
  154.  */
  155.  
  156.     ilbm->ParseInfo.propchks    = ilbmprops;
  157.     ilbm->ParseInfo.collectchks    = ilbmcollects;
  158.     ilbm->ParseInfo.stopchks    = ilbmstops;
  159.  
  160.     ilbm->windef    = &mynw;
  161.  
  162. /* 
  163.  * Alloc IFF handle for frame
  164.  */
  165.     if(!(ilbm->ParseInfo.iff = AllocIFF())) bye(noiffh,RETURN_FAIL);
  166.  
  167. /* Normally you would use showilbm() to open an appropriate acreen
  168.  * and display an ILBM in it.
  169.  *
  170.  * However, here we are demonstrating
  171.  *  - first querying an ILBM to get its BMHD and CAMG (real or computed)
  172.  *  - then opening our own display
  173.  *  - then loading the ILBM into it
  174.  */
  175.  
  176.     if(!(error = queryilbm(ilbm,ilbmname)))
  177.     {
  178.     D(bug("ilbmload: after query, this ILBM is %ld x %ld x %ld, modeid=$%lx\n",
  179.         ilbm->Bmhd.w, ilbm->Bmhd.h, ilbm->Bmhd.nPlanes, ilbm->camg));
  180.  
  181.     /* Note - you could use your own routines to open your
  182.      * display, but if so, you must initialize ilbm->scr,
  183.      * ilbm->win, ilbm->wrp, ilbm->srp, and ilbm->vp for your display.
  184.      * Here we will use opendisplay() which will initialize
  185.      * those fields.
  186.      */
  187.     maxdepth = maxdisplaydepth(ilbm->camg);
  188.     if(!(opendisplay(ilbm,
  189.             MAX(ilbm->Bmhd.pageWidth, ilbm->Bmhd.w),
  190.             MAX(ilbm->Bmhd.pageHeight,ilbm->Bmhd.h),
  191.             MIN(ilbm->Bmhd.nPlanes, maxdepth),
  192.             ilbm->camg)))
  193.         {
  194.         printf("Failed to open display\n");
  195.         }
  196.     else
  197.         {
  198.         D(bug("ilbmload: opendisplay successful\n"));
  199.  
  200.         scr = ilbm->scr;
  201.         win = ilbm->win;
  202.  
  203.         if(!(error = loadilbm(ilbm, ilbmname)))
  204.         {
  205.             D(bug("ilbmload: loadilbm successful\n"));
  206.  
  207.         /* Note - we don't need to examine or copy any
  208.          * chunks from the file, so we will close file now
  209.          */
  210.         closeifile(ilbm);
  211.         ScreenToFront(ilbm->scr);
  212.         Wait(1<<win->UserPort->mp_SigBit);
  213.         unloadilbm(ilbm);    /* deallocs colors, closeifile if needed */
  214.         }
  215.         closedisplay(ilbm);
  216.          }
  217.           }
  218.  
  219.     if(error)    printf("%s\n",IFFerr(error));
  220.  
  221.     cleanup();
  222.     exit(RETURN_OK);
  223.     }
  224.  
  225.  
  226. void bye(UBYTE *s,int error)
  227.    {
  228.    if((*s)&&(!FromWb)) printf("%s\n",s);
  229.    cleanup();
  230.    exit(error);
  231.    }
  232.  
  233.  
  234. void cleanup()
  235.    {
  236.    if(ilbm)
  237.     {
  238.     if(ilbm->ParseInfo.iff)     FreeIFF(ilbm->ParseInfo.iff);
  239.     FreeMem(ilbm,sizeof(struct ILBMInfo));
  240.     }
  241.  
  242.    if(GfxBase)              CloseLibrary(GfxBase);
  243.    if(IntuitionBase)     CloseLibrary(IntuitionBase);
  244.    if(IFFParseBase)      CloseLibrary(IFFParseBase);
  245.    }
  246.