home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 038.lha / MeltILBM / meltilbm.c < prev    next >
C/C++ Source or Header  |  1987-05-16  |  9KB  |  275 lines

  1. /************************************************************************
  2.  * MeltILBM
  3.  *    by Mark Nuiver 05/27/87
  4.  *    99.44% of this program is taken directly from the public domain
  5.  * program "ShowILBM" by Jerry Morrison, Steve Shaw, and Steve Hayes of
  6.  * Electronic Arts.  My changes are: call my function "meltviewport()"
  7.  * for 15 seconds in the DisplayPic function instead of simply displaying
  8.  * the picture for 5 seconds, changing the program's print statements
  9.  * to correspond to the change in the program's name and purpose,
  10.  * and fixing a bug that caused the program to crash the system (!)
  11.  * if a bad filename was entered as the second or subsequent filename on
  12.  * the command line.
  13.  *    Program displays a sequence of one or more IFF "FORM ILBM" pictures
  14.  * and "melts" each picture for 15 seconds before moving to the
  15.  * next picture.  (cf. Stephen Coy's "Melt")
  16.  *
  17.  * Usage from CLI:
  18.  *   meltilbm picture1 [picture2] ...
  19.  *
  20.  * Usage from WorkBench:
  21.  * Click on meltILBM, hold down shift key, click on each picture to show,
  22.  * Double-click on final picture to complete the selection, release the
  23.  * shift key.
  24.  *
  25.  * COMPILER INFORMATION:
  26.  *    Lattice C version 3.10
  27.  *    AmigaDOS 1.2
  28.  *    no options
  29.  *
  30.  * LINKING INFORMATION:
  31.  *    meltilbm.o
  32.  *    meltviewport.o requires LIB:lcm.lib
  33.  *    readpict.o       <---
  34.  *    remalloc.o           \
  35.  *    ilbmr.o               ---> recompile these with 3.10
  36.  *    iffr.o               /
  37.  *    unpacker.o       <---
  38.  *************************************************************************/
  39.  
  40. #include "iff/intuall.h"
  41. #include "libraries/dos.h"
  42. #include "libraries/dosextens.h"
  43. #include "iff/ilbm.h"
  44. #include "workbench/workbench.h"
  45. #include "workbench/startup.h"
  46. #include "iff/readpict.h"
  47. #include "iff/remalloc.h"
  48.  
  49. #define LOCAL static
  50.  
  51. #define MIN(a,b) ((a)<(b)?(a):(b))
  52. #define MAX(a,b) ((a)>(b)?(a):(b))
  53.  
  54. extern void meltviewport(struct ViewPort *,LONG);
  55.  
  56. /* general usage pointers */
  57. struct GfxBase *GfxBase;
  58. LONG IconBase;   /* Actually, "struct IconBase *" if you've got
  59.                     some ".h" file */
  60.  
  61. /* For displaying an image */
  62. LOCAL struct RastPort rP;
  63. LOCAL struct BitMap bitmap0;
  64. LOCAL struct RasInfo rasinfo;
  65. LOCAL struct View v = {0};
  66. LOCAL struct ViewPort vp = {0};
  67.  
  68. LOCAL ILBMFrame iFrame;
  69.     
  70. /* Define the size of a temporary buffer used in unscrambling the ILBM rows.*/
  71. #define bufSz 512
  72.  
  73. /* Message strings for IFFP codes. */
  74. LOCAL char MsgOkay[]        = {
  75.    "(IFF_OKAY) Didn't find a FORM ILBM in the file." };
  76. LOCAL char MsgEndMark[]     = { "(END_MARK) How did you get this message?" };
  77. LOCAL char MsgDone[]        = { "(IFF_DONE) All done."};
  78. LOCAL char MsgDos[]         = { "(DOS_ERROR) The DOS returned an error." };
  79. LOCAL char MsgNot[]         = { "(NOT_IFF) Not an IFF file." };
  80. LOCAL char MsgNoFile[]      = { "(NO_FILE) No such file found." };
  81. LOCAL char MsgClientError[] = {
  82.    "(CLIENT_ERROR) ShowILBM bug or insufficient RAM."};
  83. LOCAL char MsgForm[]        = { "(BAD_FORM) A malformed FORM ILBM." };
  84. LOCAL char MsgShort[]       = { "(SHORT_CHUNK) A malformed FORM ILBM." };
  85. LOCAL char MsgBad[]         = { "(BAD_IFF) A mangled IFF file." };
  86.  
  87. /* THESE MUST APPEAR IN RIGHT ORDER!! */
  88. LOCAL char *IFFPMessages[-(int)LAST_ERROR+1] = {
  89.     /*IFF_OKAY*/  MsgOkay,
  90.     /*END_MARK*/  MsgEndMark,
  91.     /*IFF_DONE*/  MsgDone,
  92.     /*DOS_ERROR*/ MsgDos,
  93.     /*NOT_IFF*/   MsgNot,
  94.     /*NO_FILE*/   MsgNoFile,
  95.     /*CLIENT_ERROR*/ MsgClientError,
  96.     /*BAD_FORM*/  MsgForm,
  97.     /*SHORT_CHUNK*/  MsgShort,
  98.     /*BAD_IFF*/   MsgBad
  99.     };
  100.  
  101. /** DisplayPic() ************************************************************
  102.  *
  103.  * Interface to Amiga graphics ROM routines.
  104.  *
  105.  ****************************************************************************/
  106. void DisplayPic(bm, ptilbmFrame)
  107.     struct BitMap *bm;  ILBMFrame *ptilbmFrame;  {
  108.     struct View *oldView = GfxBase->ActiView;   /* so we can restore it */
  109.  
  110.     InitView(&v);
  111.     InitVPort(&vp);
  112.     v.ViewPort = &vp;
  113.     InitRastPort(&rP);
  114.     rP.BitMap = bm;
  115.     rasinfo.BitMap = bm;
  116.  
  117.     /* Always show the upper left-hand corner of this picture. */
  118.     rasinfo.RxOffset = 0;
  119.     rasinfo.RyOffset = 0;
  120.  
  121.     vp.DWidth = MAX(ptilbmFrame->bmHdr.w, 4*8);
  122.     vp.DHeight = ptilbmFrame->bmHdr.h;
  123.  
  124. #if 0
  125.     /* Specify where on screen to put the ViewPort. */
  126.     vp.DxOffset = ptilbmFrame->bmHdr.x;
  127.     vp.DyOffset = ptilbmFrame->bmHdr.y;
  128. #else
  129.     /* Always display it in upper left corner of screen.*/
  130. #endif
  131.  
  132.     if (ptilbmFrame->bmHdr.pageWidth <= 320) 
  133.    vp.Modes = 0;
  134.     else vp.Modes = HIRES;
  135.     if (ptilbmFrame->bmHdr.pageHeight > 200) {
  136.    v.Modes |= LACE;
  137.    vp.Modes |= LACE;
  138.    }
  139.     vp.RasInfo = &rasinfo;
  140.     MakeVPort(&v,&vp);
  141.     MrgCop(&v);
  142.     LoadView(&v);   /* show the picture */
  143.     WaitBlit();
  144.     WaitTOF();
  145.     LoadRGB4(&vp, ptilbmFrame->colorMap, ptilbmFrame->nColorRegs);
  146.  
  147. /* [m?n] Here's my major contribution... */
  148.     meltviewport(&vp,15);
  149.  
  150.     LoadView(oldView);   /* switch back to old view */
  151.     }
  152.  
  153. /** stuff for main0() *******************************************************/
  154. LOCAL struct WBStartup *wbStartup = 0;   /* 0 unless started from WorkBench.*/
  155.  
  156. void PrintS(msg)  char *msg; {
  157.     if (!wbStartup)  printf(msg);
  158.     }
  159.  
  160. void GoodBye(msg)  char *msg; {
  161. /*     PrintS(msg);   PrintS("\n");  */
  162.     printf(msg);   printf("\n");    /* If linked with Lstartup.obj and
  163.                  * NOT compiled with -dTINY, this 
  164.                  * outputs the message to the window
  165.                  * that Lattice opens.
  166.                  * ... carolyn.
  167.                  */
  168.     exit(0);
  169.     }
  170.  
  171. /** OpenArg() ***************************************************************
  172.  *  Given a "workbench argument" (a file reference) and an I/O mode.
  173.  *  It opens the file.
  174.  ****************************************************************************/
  175. LONG OpenArg(wa, openmode)  struct WBArg *wa;   int openmode; {
  176.     LONG olddir;
  177.     LONG file;
  178.     if (wa->wa_Lock)   olddir = CurrentDir(wa->wa_Lock);
  179.     file = Open(wa->wa_Name, openmode);
  180.     if (wa->wa_Lock)   CurrentDir(olddir);
  181.     return(file);
  182.     }
  183.  
  184. /** main0() *****************************************************************/
  185. void main0(wa)  struct WBArg *wa;  {
  186.     LONG file;
  187.     IFFP iffp = NO_FILE;
  188.  
  189.     /* load and display the picture */
  190.     file = OpenArg(wa, MODE_OLDFILE);
  191.     if (file)
  192.    iffp = ReadPicture(file, &bitmap0, &iFrame, ChipAlloc);
  193.    /* Allocates BitMap using ChipAlloc().*/
  194.    Close(file);
  195.     if (iffp == IFF_DONE)
  196.    DisplayPic(&bitmap0, &iFrame);
  197.  
  198. /*     PrintS(" ");   PrintS(IFFPMessages[-iffp]);   PrintS("\n");   */
  199.      printf(" ");   printf(IFFPMessages[-iffp]);   printf("\n");  
  200.    /* see note near definition of PrintS */
  201.  
  202.     /* cleanup */
  203.     if (bitmap0.Planes[0])  {
  204.    RemFree(bitmap0.Planes[0]);
  205.       /* ASSUMES allocated all planes via a single ChipAlloc call.*/
  206. /* [m?n] This fixes the bug in the original ShowILBM program that
  207.    would cause it to crash when invoked from CLI with multiple files
  208.    when the second or subsequent file didn't exist.  RemFree doesn't set
  209.    bitmap0.Planes[0] to 0, so the program would attempt here to deallocate
  210.    memory that hadn't been allocated, crashing the machine... */
  211.    bitmap0.Planes[0] = 0;
  212.    FreeVPortCopLists(&vp);
  213.    FreeCprList(v.LOFCprList);
  214.    }
  215.     }
  216.  
  217. extern struct WBStartup *WBenchMsg;   /* added: Carolyn Scheppner */
  218.  
  219. /** main() ******************************************************************/
  220.  
  221. void main(argc, argv)  int argc;  char **argv;  {
  222.     struct WBArg wbArg, *wbArgs;
  223.     LONG olddir;
  224. /*sss    struct Process *myProcess; */
  225.  
  226.     if( !(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0)) )
  227.    GoodBye("No graphics.library");
  228.     if( !(IconBase = OpenLibrary("icon.library",0)) )
  229.    GoodBye("No icon.library");
  230.     if (!argc) {
  231.    /* Invoked via workbench */
  232. /*    wbStartup = (struct WBStartup *)argv;   */
  233.    wbStartup = WBenchMsg;   /* modified by Carolyn Scheppner */
  234.    wbArgs = wbStartup->sm_ArgList;
  235.    argc = wbStartup->sm_NumArgs;
  236.    while (argc >= 2) {
  237.        olddir = CurrentDir(wbArgs[1].wa_Lock);
  238.        main0(&wbArgs[1]);
  239.        argc--;   wbArgs = &wbArgs[1];
  240.        }
  241. #if 0
  242.    /* [TBD] We want to get an error msg to the Workbench user... */
  243.    if (argc < 2) {
  244.        printf ("Usage from workbench:\n");
  245.        printf (" Click mouse on MeltILBM, Then hold 'SHIFT' key\n");
  246.  
  247.    /* BOTH OF THESE WERE "PrintS", see note near PrintS definition */
  248.  
  249.        GoodBye(" while double-click on file to display.");
  250.        }
  251. #endif
  252.    }
  253.     else {
  254.    /* Invoked via CLI.  Make a lock for current directory.
  255.     * Eventually, scan name, separate out directory reference?*/
  256.    if (argc < 2)
  257.        GoodBye("Usage from CLI: 'MeltILBM filename'");
  258. /*sss   myProcess = (struct Process *)FindTask(0); */
  259.    wbArg.wa_Lock = 0; /*sss myProcess->pr_CurrentDir; */
  260.    while (argc >= 2) {
  261.        wbArg.wa_Name = argv[1];
  262.        printf("Melting file ");   printf(wbArg.wa_Name);   printf(" ...");
  263.    /* THESE WERE "PrintS", see note near PrintS definition */
  264.        main0(&wbArg);
  265.        printf("\n");
  266.    /* THIS WAS "PrintS", see note near PrintS definition */
  267.        argc--;   argv = &argv[1];
  268.        }
  269.    }
  270.     CloseLibrary(GfxBase);
  271.     CloseLibrary(IconBase);
  272.     exit(0);
  273.     }
  274.  
  275.