home *** CD-ROM | disk | FTP | other *** search
/ Micro R&D 1 / MicroRD-CD-ROM-Vol1-1994.iso / os30 / gfx / animdemo.lha / makeraw / makeraw.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-23  |  13.0 KB  |  562 lines

  1. /*******************************************************************************
  2.  *
  3.  * (c) Copyright 1993 Commodore-Amiga, Inc.  All rights reserved.
  4.  *
  5.  * This software is provided as-is and is subject to change; no warranties
  6.  * are made.  All use is at your own risk.  No liability or responsibility
  7.  * is assumed.
  8.  *
  9.  * MakeRaw - the main code to convert the files needed for the animation to
  10.  * the file format used by playraw.
  11.  *
  12.  ******************************************************************************/
  13.  
  14. #define LIBVERSION 39
  15.  
  16. #define MAX(a,b) ((a) > (b) ? (a) : (b))
  17. #define NO_CTRL_C 1
  18. #if NO_CTRL_C == 1
  19. int CXBRK (void) { return (0) ; }    /* disable ctrl-c */
  20. #endif
  21.  
  22. #include <exec/types.h>
  23. #include <exec/exec.h>
  24. #include <proto/exec.h>
  25. #include <dos/dos.h>
  26. #include <dos/rdargs.h>
  27. #include <proto/dos.h>
  28. #include <intuition/screens.h>
  29. #include <intuition/intuition.h>
  30. #include <intuition/intuitionbase.h>
  31. #include <proto/intuition.h>
  32. #include <graphics/gfx.h>
  33. #include <graphics/gfxbase.h>
  34. #include <graphics/displayinfo.h>
  35. #include <proto/graphics.h>
  36. #include <libraries/iffparse.h>
  37. #include <proto/iffparse.h>
  38.  
  39. #include "/playanim.h"
  40.  
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <ctype.h>
  44.  
  45. /*********************************************************************/
  46. /*                            GLOBAL VARIABLES                       */
  47. /*********************************************************************/
  48.  
  49. struct IntuitionBase *IntuitionBase = NULL ;
  50. struct GfxBase *GfxBase = NULL ;
  51. struct Library *IFFParseBase = NULL;
  52.  
  53. struct RDArgs *rdargs = NULL;
  54. BPTR OutFile = NULL;
  55. struct IFFHandle *iff = NULL;
  56. UBYTE *inbuff = NULL;
  57. struct ColorMap *cm = NULL;
  58. BPTR ScriptFile = NULL;
  59.  
  60. /**********************************************************************/
  61. /*                                                                    */
  62. /* void Error (char *String)                                          */
  63. /* Print string and exit                                              */
  64. /*                                                                    */
  65. /**********************************************************************/
  66.  
  67. void Error (char *String)
  68. {
  69.     void CloseAll(void);
  70.     
  71.     printf(String);
  72.  
  73.     CloseAll();
  74.     exit(0);
  75. }
  76.  
  77.  
  78. /**********************************************************************/
  79. /*                                                                    */
  80. /* void Init ()                                                       */
  81. /*                                                                    */
  82. /* Opens all the required libraries                                   */
  83. /* allocates all memory, etc.                                         */
  84. /*                                                                    */
  85. /**********************************************************************/
  86.  
  87. void Init ()
  88. {
  89.     if ((IntuitionBase = (struct IntuitionBase *)OpenLibrary ("intuition.library", LIBVERSION)) == NULL)
  90.     {
  91.     Error ("Could not open the Intuition.library") ;
  92.     }
  93.  
  94.     if ((GfxBase = (struct GfxBase *)OpenLibrary ("graphics.library", LIBVERSION)) == NULL)
  95.     {
  96.     Error ("Could not open the Graphics.library") ;
  97.     }
  98.  
  99.     if ((IFFParseBase = OpenLibrary("iffparse.library", 37L)) == NULL)
  100.     {
  101.     Error("Could not open the iffparse.library");
  102.     }
  103.  
  104.     return ;
  105. }
  106.  
  107. /**********************************************************************/
  108. /*                                                                    */
  109. /* void CloseAll ()                                                   */
  110. /*                                                                    */
  111. /* Closes and tidies up everything that was used.                     */
  112. /*                                                                    */
  113. /**********************************************************************/
  114.  
  115. void CloseAll ()
  116. {
  117.     /* Close everything in the reverse order in which they were opened */
  118.  
  119.     if (cm)
  120.     {
  121.         FreeColorMap(cm);
  122.     }
  123.  
  124.     if (iff)
  125.     {
  126.     CloseIFF(iff);
  127.     if (iff->iff_Stream)
  128.     {
  129.         Close(iff->iff_Stream);
  130.     }
  131.     FreeIFF(iff);
  132.     }
  133.  
  134.     if (inbuff)
  135.     {
  136.     FreeVec(inbuff);
  137.     }
  138.  
  139.     if (rdargs)
  140.     {
  141.     FreeArgs(rdargs);
  142.     }
  143.  
  144.     if (ScriptFile)
  145.     {
  146.     Close(ScriptFile);
  147.     }
  148.  
  149.     if (OutFile)
  150.     {
  151.         Close(OutFile);
  152.     }
  153.  
  154.     if (IFFParseBase)
  155.     {
  156.     CloseLibrary(IFFParseBase);
  157.     }
  158.  
  159.     if (GfxBase)
  160.     {
  161.     CloseLibrary((struct Library *)GfxBase);
  162.     }
  163.  
  164.     if (IntuitionBase)
  165.     {
  166.     CloseLibrary((struct Library *)IntuitionBase);
  167.     }
  168.  
  169.     return;
  170. }
  171.  
  172. /***************************************************************************/
  173.  
  174. #define VISTALINE 80
  175. UWORD *ReadVista(BPTR ScriptFile, STRPTR Vista, UWORD *outbuffer)
  176. {
  177.     /* This code will read a line of a VistaPro script. */
  178.     WORD v = 0;
  179.     WORD sign = 1;
  180.     UBYTE *line = (UBYTE *)Vista;
  181.  
  182.     FGets(ScriptFile, Vista, VISTALINE);
  183.     while (*line)
  184.     {
  185.     if (*line == '.')
  186.     {
  187.         line += 3;
  188.     }
  189.     if (*line == ',')
  190.     {
  191.         *outbuffer++ = (v * sign);
  192.         sign = 1;
  193.     }
  194.     if (*line == '-')
  195.     {
  196.         sign = -1;
  197.     }
  198.     else
  199.     {
  200.         if (isalnum(*line))
  201.         {
  202.             UBYTE *nline;
  203.             v = atoi(line);
  204.             if (nline = (UBYTE *)strchr(line, ','))
  205.             {
  206.                 line = (nline - 1);
  207.             }
  208.             else
  209.             {
  210.                 return(outbuffer);
  211.             }
  212.         }
  213.     }
  214.     line++;
  215.     }
  216.     return(outbuffer);
  217. }
  218.  
  219.  
  220. #define ID_ILBM MAKE_ID('I','L','B','M')
  221. #define ID_BODY MAKE_ID('B','O','D','Y')
  222. #define ID_DLTA MAKE_ID('D','L','T','A')
  223. #define ID_BMHD MAKE_ID('B','M','H','D')
  224. #define ID_CMAP MAKE_ID('C','M','A','P')
  225.  
  226. void GetAudio(STRPTR FileName, BPTR OutFile);
  227.  
  228. void main (int argc, char *argv[])
  229. {
  230.     #define TEMPLATE "FROM/A,TO/A,M=MODEID/N,FIT/S,SC=SCRIPT,F=AUDIOFIRE/K,T=AUDIOTHRUST/K,X=REPEAT/N"
  231.     #define OPT_FROM 0
  232.     #define OPT_TO 1
  233.     #define OPT_MODE 2
  234.     #define OPT_FIT 3
  235.     #define OPT_SCRIPT 4
  236.     #define OPT_FIRE 5
  237.     #define OPT_THRUST 6
  238.     #define OPT_REPEAT 7
  239.     #define OPT_COUNT 8
  240.  
  241.     #define SCREEN_W (fit ? f1.f1_ImageWidth : 320)
  242.     #define SCREEN_H (fit ? f1.f1_ImageHeight : 200)
  243.     #define SCREEN_D 8
  244.     #define SCREEN_MODEID ModeID
  245.  
  246.     LONG result[OPT_COUNT];
  247.     LONG props[] =
  248.     {
  249.     ID_ILBM, ID_CMAP,
  250.     ID_ILBM, ID_BMHD,
  251.     };
  252.     STRPTR from = NULL, to = NULL, script = NULL, fire = NULL, thrust = NULL;
  253.     ULONG secsize;
  254.     ULONG maxsec = 0;
  255.     ULONG ModeID = 0;
  256.     ULONG Repeat = 0;
  257.     LONG *val;
  258.     LONG read;
  259.     struct ContextNode *cn;
  260.     struct StoredProperty *sp;
  261.     UBYTE *cmap;
  262.     BOOL fit = FALSE;
  263.     struct Frame1Head f1;
  264.     struct BitMapHeader bmhd;
  265.     UWORD ncolours;
  266.     UBYTE Vista[VISTALINE];
  267.  
  268.     Init () ;
  269.  
  270.     /* init the result[] */
  271.     result[OPT_FROM] = (LONG)&from;
  272.     result[OPT_TO] = (LONG)&to;
  273.     result[OPT_MODE] = (LONG)&ModeID;
  274.     result[OPT_FIT] = fit;
  275.     result[OPT_SCRIPT] = NULL;
  276.     result[OPT_FIRE] = NULL;
  277.     result[OPT_THRUST] = NULL;
  278.     result[OPT_REPEAT] = (LONG)&Repeat;
  279.  
  280.     if (rdargs = ReadArgs(TEMPLATE, result, NULL))
  281.     {
  282.     from = (STRPTR)result[OPT_FROM];
  283.     to = (STRPTR)result[OPT_TO];
  284.     script = (STRPTR)result[OPT_SCRIPT];
  285.     fire = (STRPTR)result[OPT_FIRE];
  286.     thrust = (STRPTR)result[OPT_THRUST];
  287.     if (val = (LONG *)result[OPT_MODE])
  288.     {
  289.         ModeID = *val;
  290.     }
  291.     if (val = (LONG *)result[OPT_REPEAT])
  292.     {
  293.         Repeat = *val;
  294.     }
  295.     fit = result[OPT_FIT];
  296.     }
  297.     else
  298.     {
  299.         Error("Command line error\n");
  300.     }
  301.  
  302.     if ((OutFile = Open(to, MODE_NEWFILE)) == NULL)
  303.     {
  304.     Error("OutFile did not open\n");
  305.     }
  306.  
  307.     if (script && ((ScriptFile = Open(script, MODE_OLDFILE)) == NULL))
  308.     {
  309.     Error("Script did not open\n");
  310.     }
  311.     /* Skip the first two lines of the buffer */
  312.     FGets(ScriptFile, (STRPTR)&Vista, VISTALINE);
  313.     FGets(ScriptFile, (STRPTR)&Vista, VISTALINE);
  314.  
  315.     /* Read the first ILBM chunk */
  316.  
  317.     if (iff = AllocIFF())
  318.     {
  319.     if ((iff->iff_Stream = Open(from, MODE_OLDFILE)) == NULL)
  320.     {
  321.         Error("InFile did not open\n");
  322.     }
  323.     InitIFFasDOS(iff);
  324.     if (OpenIFF(iff, IFFF_READ) == NULL)
  325.     {
  326.         if ((PropChunks(iff, props, 2) == 0) &&
  327.             (StopChunk(iff, ID_ILBM, ID_BODY) == 0) &&
  328.             (StopChunk(iff, ID_ILBM, ID_DLTA) == 0) &&
  329.             (ParseIFF(iff, IFFPARSE_SCAN) == 0))
  330.         {
  331.             if (sp = FindProp(iff, ID_ILBM, ID_BMHD))
  332.             {
  333.                 bmhd = (*(struct BitMapHeader *)sp->sp_Data);
  334.                 printf("width %ld, height %ld, depth %ld\n", bmhd.w, bmhd.h, bmhd.nplanes);
  335.                 f1.f1_ImageWidth = bmhd.w;
  336.                 f1.f1_ImageHeight = bmhd.h;
  337.                 f1.f1_ImageDepth = bmhd.nplanes;
  338.             }
  339.             else
  340.             {
  341.                 Error("No BMHD\n");
  342.             }
  343.             if (sp = FindProp(iff, ID_ILBM, ID_CMAP))
  344.             {
  345.                 int i;
  346.  
  347.                 cmap = (UBYTE *)sp->sp_Data;
  348.                 printf("CMAP found\n");
  349.                 /* We need to store these before losing the context */
  350.                 if (cm = GetColorMap(ncolours = (sp->sp_Size / 3)))
  351.                 {
  352.                     for (i = 0; i < ncolours; i++)
  353.                     {
  354.                         SetRGB32CM(cm, i, ((*cmap++) << 24), ((*cmap++) << 24), ((*cmap++) << 24));
  355.                     }
  356.                 }
  357.                 else
  358.                 {
  359.                     Error("Could not get CMap\n");
  360.                 }
  361.             }
  362.             else
  363.             {
  364.                 Error("no CMAP\n");
  365.             }
  366.  
  367.             f1.f1_ScreenWidth = SCREEN_W;
  368.             f1.f1_ScreenHeight = SCREEN_H;
  369.             f1.f1_ScreenDepth = SCREEN_D;
  370.             f1.f1_ScreenModeID = SCREEN_MODEID;
  371.             
  372.             /* we are at the BODY chunk */
  373.             if ((cn = CurrentChunk(iff)) &&
  374.                 (secsize = BYTES_TO_WHOLE_SECTORS(cn->cn_Size)) && 
  375.                 (inbuff = AllocVec(secsize, MEMF_CLEAR)))
  376.             {
  377.                 struct Frame1Head *f1t = (struct Frame1Head *)inbuff;
  378.                 UBYTE *tocm = (inbuff + sizeof(f1));
  379.                 ULONG rgb[3];
  380.                 int i;
  381.  
  382.                 f1.f1_MaxSectorSize = 0;
  383.                 f1.f1_ByteSize = cn->cn_Size;
  384.                 f1.f1_Sectors = BYTES_TO_SECTORS(cn->cn_Size);
  385.                 f1.f1_Colours = ncolours;
  386.                 *f1t = f1;
  387.  
  388.                 /* write out the colours */
  389.                 for (i = 0; i < ncolours; i++)
  390.                 {
  391.                     GetRGB32(cm, i, 1, rgb);
  392.                     *tocm++ = (rgb[0] >> 24);
  393.                     *tocm++ = (rgb[1] >> 24);
  394.                     *tocm++ = (rgb[2] >> 24);
  395.                 }
  396.  
  397.                 printf("BODY size = %lx\n", cn->cn_Size);
  398.                 read = ReadChunkBytes(iff, (inbuff + sizeof(f1) + (ncolours * 3)), cn->cn_Size);
  399.                 printf("read %lx\n", read);
  400.                 if (read != cn->cn_Size)
  401.                 {
  402.                     Error("ILBM read error\n");
  403.                 }
  404.                 maxsec = MAX(secsize, maxsec);
  405.             }
  406.         }
  407.     }
  408.     }
  409.         
  410.     if ((ParseIFF(iff, IFFPARSE_SCAN) == 0) &&
  411.     (cn = CurrentChunk(iff)) &&
  412.     (cn->cn_ID == ID_DLTA))
  413.     {
  414.     ULONG *end = (ULONG *)(inbuff + sizeof(f1) + (ncolours * 3) + read);
  415.  
  416.     /* End of the ILBM contains the data for the next frame */
  417.     *(end++) = cn->cn_Size;    /* next size */
  418.     *(end++) = BYTES_TO_SECTORS(cn->cn_Size);
  419.  
  420.     /* Put other data here */
  421.  
  422.     /* First, the VistaScript stuff for this frame (not including the Focal) */
  423.     if (script)
  424.     {
  425.         ReadVista(ScriptFile, (STRPTR)Vista, (UWORD *)end);
  426.     }
  427.  
  428.     /* Write out the whole thing */
  429.     if (Write(OutFile, inbuff, secsize) != secsize)
  430.     {
  431.         Error("Error writing frame 0\n");
  432.     }
  433.     FreeVec(inbuff);
  434.     inbuff = NULL;
  435.     read = cn->cn_Size;
  436.  
  437.     }
  438.     else
  439.     {
  440.         Error("End of Anim\n");
  441.     }
  442.  
  443.     /* Write out the Audio chunks */
  444.     if (fire)
  445.     {
  446.     GetAudio(fire, OutFile);
  447.     }
  448.     if (thrust)
  449.     {
  450.     GetAudio(thrust, OutFile);
  451.     }
  452.  
  453.     /* Now read the DLTA chunks */
  454.     while ((secsize = BYTES_TO_WHOLE_SECTORS(read)) &&
  455.            (inbuff = AllocVec(secsize, MEMF_CLEAR)) &&
  456.            (ReadChunkBytes(iff, inbuff, read) == read))
  457.  
  458.     {
  459.     ULONG *end = (ULONG *)(inbuff + read);
  460.  
  461.     if ((ParseIFF(iff, IFFPARSE_SCAN) == 0) &&
  462.             (cn = CurrentChunk(iff)) &&
  463.             (cn->cn_ID == ID_DLTA))
  464.     {
  465.         /* End of the DLTA contains the data for the next frame */
  466.         *(end++) = cn->cn_Size;    /* next size */
  467.         *(end++) = BYTES_TO_SECTORS(cn->cn_Size);
  468.         read = cn->cn_Size;
  469.     }
  470.     else
  471.     {
  472.         /* at the end of the anim. If we need to repeat,
  473.          * shut everythink down and start again.
  474.          *
  475.          * The animation was WRAPPED at the end, so we can skip the
  476.          * first DLTA.
  477.          */
  478.         if (Repeat--)
  479.         {
  480.             printf("repeating, %ld left to go\n", Repeat);
  481.              CloseIFF(iff);
  482.              Close(iff->iff_Stream);
  483.              FreeIFF(iff);
  484.              iff = NULL;
  485.              if ((iff = AllocIFF()) == NULL)
  486.              {
  487.                  Error("Phooey!\n");
  488.              }
  489.              if ((iff->iff_Stream = Open(from, MODE_OLDFILE)) == NULL)
  490.              {
  491.                  Error("Could not reopen the anim file\n");
  492.              }
  493.              InitIFFasDOS(iff);
  494.              if (OpenIFF(iff, IFFF_READ))
  495.              {
  496.                  Error("repeat anim Open error\n");
  497.              }
  498.              if ((StopChunk(iff, ID_ILBM, ID_DLTA) == NULL) &&
  499.                  (ParseIFF(iff, IFFPARSE_SCAN) == 0) &&
  500.                  (ParseIFF(iff, IFFPARSE_SCAN) == 0) &&
  501.                  (cn = CurrentChunk(iff)) &&
  502.                  (cn->cn_ID == ID_DLTA))
  503.              {
  504.                  *(end++) = cn->cn_Size;
  505.                  *(end++) = BYTES_TO_SECTORS(cn->cn_Size);
  506.                  read = cn->cn_Size;
  507.              }
  508.         }
  509.     }
  510.  
  511.     /* Put other data here */
  512.     if (script)
  513.     {
  514.         ReadVista(ScriptFile, (STRPTR)Vista, (UWORD *)end);
  515.     }
  516.  
  517.     /* Write it out */
  518.     if (Write(OutFile, inbuff, secsize) != secsize)
  519.     {
  520.         Error("File write error\n");
  521.     }
  522.     FreeVec(inbuff);
  523.     inbuff = NULL;
  524.     maxsec = MAX(secsize, maxsec);
  525.     }
  526.  
  527.     /* write out the last frame */
  528.     if (inbuff)
  529.     {
  530.     ULONG *end = (ULONG *)(inbuff + read);
  531.  
  532.     /* End of the DLTA contains the data for the next frame */
  533.     *(end++) = 0;
  534.     *(end++) = 0;
  535.  
  536.     Write(OutFile, inbuff, secsize);
  537.     FreeVec(inbuff);
  538.     inbuff = NULL;
  539.     }
  540.  
  541.     /* write out the max buffer size */
  542.     Close(OutFile);
  543.     OutFile = NULL;
  544.     if (OutFile = Open(to, MODE_READWRITE))
  545.     {
  546.     maxsec = BYTES_TO_SECTORS(maxsec);
  547.     printf("Max sector size is %ld\n", maxsec);
  548.     Write(OutFile, &maxsec, sizeof(maxsec));
  549.     }
  550.     else
  551.     {
  552.     Error("Seek error\n");
  553.     }
  554.  
  555.     CloseIFF(iff);
  556.     Close(iff->iff_Stream);
  557.     FreeIFF(iff);
  558.     iff = NULL;
  559.  
  560.     CloseAll () ;
  561. }
  562.