home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume38 / tovcr / part01 / tovcr.c.A < prev   
Text File  |  1993-07-14  |  46KB  |  1,984 lines

  1. /*
  2.  * This software is copyrighted as noted below.  It may be freely copied,
  3.  * modified, and redistributed, provided that the copyright notice is
  4.  * preserved on all copies.
  5.  *
  6.  * There is no warranty or other guarantee of fitness for this software,
  7.  * it is provided solely "as is".  Bug reports or fixes may be sent
  8.  * to the author, who may or may not act on them as he desires.
  9.  *
  10.  * You may not include this software in a program or other software product
  11.  * without supplying the source, or without informing the end-user that the
  12.  * source is available for no extra charge.
  13.  *
  14.  * If you modify this software, you should include a notice giving the
  15.  * name of the person performing the modification, the date of modification,
  16.  * and the reason for such modification.
  17.  */
  18. /*
  19.  * tovcr.c - display/record images on a Video Recorder using the SGI VFR.
  20.  *
  21.  * Author:      Raul Rivero
  22.  *              Mathematics Dept.
  23.  *              University of Oviedo
  24.  * Date:        Mon Aug 17 1992
  25.  * Copyright (c) 1992, Raul Rivero
  26.  *
  27.  */
  28. /*
  29.  * Revisions:
  30.  *
  31.  * 1.1
  32.  *      Raul Rivero
  33.  *      Mathematics Dept.
  34.  *      University of Oviedo
  35.  *      Mon May 17 1993
  36.  *      ( nuevos@carreras.ccu.uniovi.es )
  37.  *
  38.  *      i.  New file formats supported ( JPEG, PBM/PGM/PPM, 24bits-PCX ).
  39.  *      ii. Some little fixes.
  40.  *
  41.  * -----
  42.  */
  43.  
  44. #include <stdio.h>
  45. #include <math.h>
  46. #include <signal.h>
  47. #include <string.h>
  48. #include <sys/types.h>
  49. #include <sys/wait.h>
  50. #include <sys/stat.h>
  51. #include "image.h"
  52. #include "vframer.h"
  53.  
  54. #ifdef iTIFF
  55. #   include "tiffio.h"
  56. #   define GetField(h, t, v)       if ( !TIFFGetField(h, t, v ) ) error(18);
  57. #endif  /* iTIFF */
  58.  
  59. #ifdef iRLE
  60. #   include "rle.h"
  61. #   define PSEUDOCOLOR             1
  62. #   define DIRECTCOLOR             2
  63. #   define TRUECOLOR               3
  64. #   define GRAYSCALE               4
  65. #endif  /* iRLE */
  66.  
  67. #ifdef iJPEG
  68. #   include <jinclude.h>
  69. #endif  /* iJPEG */
  70.  
  71. /*
  72.  * The magic flag.
  73.  */
  74. #define LUGUSED                     12345   /* why not ? */
  75.  
  76. #define byte                        unsigned char
  77. #define ushort                      unsigned short
  78. #define Fread(p, s, n, f)           if ( fread(p, s, n, f) != n ) error(3)
  79. #define Fwrite(p, s, n, f)          if ( fwrite(p, s, n, f) != n ) error(4)
  80. #define VPRINTF                     if (verbose) fprintf
  81. #define VFLUSH                      if (verbose) fflush
  82. #define NOBLUR                      0
  83. #define BLUR                        1
  84. #define CHROMABORDER                20
  85. #define PREROLLTIME                 "3:00"
  86. #define POSTROLLTIME                "1:00"
  87. #define AUTOINCREMENT               1
  88.  
  89. /*
  90.  * This is the name of the 'super' image. Be careful with
  91.  * the file format.
  92.  */
  93. #define SUPERNAME                   "/usr/a3demo/bin/fuentes/super"
  94.  
  95. /* More esasy */
  96. #define equal(s, t)                 ( strstr(s,t) != NULL ? 1 : 0 )
  97.  
  98. /*
  99.  * ==========================================
  100.  *  REMEMBER!!!
  101.  *      I'm working with R-Y PAL, but you ?.
  102.  * ==========================================
  103.  *
  104.  * Be careful with the next 3 blocks of defines !!!.
  105.  */
  106.  
  107. /*
  108.  * The image will be adjusted to ...
  109.  *
  110.  */
  111. #define XFBUFFER                    780
  112. #define YFBUFFER                    576
  113. #define XSIZE                       710
  114. #define YSIZE                       576
  115. #define VFR_LINESIZE                1024
  116.  
  117. /*
  118.  * Video FRamer default buffer names.
  119.  */
  120. #define DEFAULT_VFR_BUFFER          "r_y_625"
  121. #define OPTIONAL_VFR_BUFFER         "rgb_625"
  122.  
  123. /*
  124.  * Last frame into SMPTE ( PAL has 25 frames per
  125.  * second ==> 24 ).
  126.  */
  127. #define FPS                         24
  128.  
  129. /*
  130.  * TGA's defines.
  131.  */
  132. #define TGAINTERLACED(a)            ( (a) & 0xc0 )
  133. #define TGAFLIP(a)                  ( ((a) & 0x20) ? 0 : 1)
  134. #define TGA_MAPPED                  1
  135. #define TGA_RGB                     2
  136. #define TGA_RLE_MAPPED              9
  137. #define TGA_RLE_RGB                 10
  138.  
  139. /*
  140.  * GIF's defines.
  141.  */
  142. #define GIFHEADER                   "GIF87a"
  143. #define GIFIMGSEPAR                 ','
  144. #define ENDGIF                      ';'
  145. #define GIFEXISTCOLOR               (1 << 7)
  146. #define GIFINTERLAZE                (1 << 6)
  147.  
  148. /*
  149.  * PCX's defines
  150.  */
  151. #define PCX_HEADERSIZE              128
  152. #define PCX_MAGICNUMBER             0x0a
  153. #define PCX_256COLORS               0x0c
  154. #define PCX_COMPRESS                0xc0
  155.  
  156. #ifdef USE_STDLIB_H
  157. #   include <stdlib.h>
  158. #else
  159. #   ifdef VOID_STAR
  160.         extern void *malloc();
  161. #   else
  162.         extern char *malloc();
  163. #   endif /* VOID_STAR */
  164.     extern void free();
  165. #endif /* USE_STDLIB_H */
  166.  
  167. #ifndef BSD
  168. #   define bzero(s, n)             memset((s), 0, (n))
  169. #   define bcopy(f, t, c)          memcpy((t), (f), (c))
  170. #endif /* BSD */
  171.  
  172. /*
  173.  * Pointer to funtions.
  174.  */
  175. typedef int (* ifunptr)();
  176.  
  177. /*
  178.  * My bitmap header.
  179.  */
  180. typedef struct {
  181.         int xsize, ysize;
  182.         int depth;
  183.         int colors;
  184.         byte *r, *g, *b;
  185.         byte *cmap;
  186.         int magic;
  187. } bitmap_hdr;
  188.  
  189. /*
  190.  * Wavefront's RLA format headers.
  191.  */
  192. typedef struct {
  193.    short left, right, bottom, top;
  194. } WINDOW_S;
  195.  
  196. typedef struct {
  197.    WINDOW_S     window;
  198.    WINDOW_S     act_window;
  199.    short        frame;
  200.    short        storage_type;
  201.    short        num_chan;
  202.    short        num_matte;
  203.    short        num_aux;
  204.    short        aux_mask;
  205.    char         gamma[16];
  206.    char         red_pri[24];
  207.    char         green_pri[24];
  208.    char         blue_pri[24];
  209.    char         white_pt[24];
  210.    long         job_num;
  211.    char         name[128];
  212.    char         desc[128];
  213.    char         program[64];
  214.    char         machine[32];
  215.    char         user[32];
  216.    char         date[20];
  217.    char         aspect[32];
  218.    char         chan[32];
  219.    char         space[128];
  220. } RLA_HEADER;
  221.  
  222. /*
  223.  * Targa header.
  224.  */
  225. typedef struct {
  226.         byte   num_id;
  227.         byte   cmap_type;
  228.         byte   image_type;
  229.         ushort cmap_orign;
  230.         ushort cmap_length;
  231.         byte   cmap_entry_size;
  232.         ushort xorig, yorig;
  233.         ushort xsize, ysize;
  234.         byte   pixel_size;
  235.         byte   image_descriptor;
  236. } tga_hdr;
  237.  
  238. /*
  239.  * Alias header.
  240.  */
  241. typedef struct {
  242.         short xsize, ysize;
  243.         short xinit, yinit;
  244.         short depth;
  245. } alias_hdr;
  246.  
  247. typedef byte color_map[3];
  248.  
  249. /*
  250.  * Prototypes ( only what is necesary ).
  251.  */
  252. FILE *Fopen();
  253. char *Malloc();
  254. long filelen();
  255. char *read_file();
  256. byte *create_bw_pallete();
  257. byte *zoom();
  258. byte *blur();
  259. int fparent_ready();
  260. int fchild_ready();
  261. VFR_DEV *open_video();
  262. int vfr_not_ready();
  263. int error_from_other();
  264. int press_break();
  265. char *Vfr_Vlan_Cmd();
  266. byte *shorttobyte();
  267. byte *read_gif_screen_hdr();
  268. byte *read_gif_image_hdr();
  269. byte *unblockgif();
  270. byte *flip();
  271. #ifdef iJPEG
  272.     void get_jpeg_header();
  273.     void get_jpeg_cmap();
  274.     void get_jpeg_row();
  275.     void end_put_jpeg();
  276.     void d_ui_method_selection();
  277. #endif  /* iJPEG */
  278.  
  279. /*
  280.  * This pointer contents the funtion to read the
  281.  * input image file format.
  282.  */
  283. ifunptr read_image;
  284.  
  285. /*
  286.  * Current formats supported.
  287.  */
  288. int read_alias_file();
  289. int read_rla_file();
  290. int read_sgi_file();
  291. int read_tga_file();
  292. int read_gif_file();
  293. int read_pcx_file();
  294. int read_pbm_file();
  295. #ifdef iTIFF
  296.     int read_tiff_file();
  297. #endif
  298. #ifdef iRLE
  299.     int read_rle_file();
  300. #endif
  301. #ifdef iJPEG
  302.     int read_jpeg_file();
  303. #endif
  304.  
  305. /*
  306.  * The name of the program and an auxiliar file name
  307.  * to uncompress input file ( if compressed ).
  308.  */
  309. char *MY_NAME;
  310. char aux_file[40];
  311.  
  312. /* Used by the GIF reader */
  313. int lug_read_code_position;
  314.  
  315. /* Used by the JPEG reader */
  316. #ifdef iJPEG
  317.     static bitmap_hdr *jpeg_image;
  318.     byte *jpeg_r, *jpeg_g, *jpeg_b;
  319. #endif  /* iJPEG */
  320.  
  321. /* IDs for child and parent process */
  322. int cpid, parent_pid;
  323.  
  324. /* We'll simulate semaphores with ... */
  325. int child_ready, parent_ready;
  326.  
  327. /*
  328.  * Flags ...
  329.  */
  330. int verbose = 0;
  331. int super = 0;
  332. int center = 0;
  333. int resetvfr = 0;
  334. int rgbvfr = 0;
  335. int blurflag = 0;
  336. int record_sequence = 0;
  337. int forceblur = 0;
  338.  
  339. main(argc, argv)
  340. int argc;
  341. char **argv;
  342. {
  343.   register int i;
  344.   char *inpoint;
  345.   int start, end;
  346.  
  347.   MY_NAME = argv[0];
  348.   parent_pid = getpid();
  349.  
  350.   /* Default is Alias "pix" format */
  351.   read_image = read_alias_file;
  352.  
  353.   /*
  354.    * Get options ( some day I'll build a procedure ).
  355.    */
  356.   if ( argc > 1 ) {             /* else core on SGI */
  357.    while ( argv[1][0] == '-' ) {
  358.       for ( i = 1; argv[1][i]; i++ ) {
  359.         switch ( argv[1][i] ) {
  360.           case 'v':
  361.                 verbose++;
  362.                 break;
  363.           case 'b':
  364.                 forceblur++;
  365.                 break;
  366.           case 'c':
  367.                 center++;
  368.                 break;
  369.           case 's':
  370.                 super++;
  371.                 break;
  372.           case 'i':
  373.                 resetvfr++;
  374.                 break;
  375.           case 'r':
  376.                 rgbvfr++;
  377.                 resetvfr++;
  378.                 break;
  379.           case 'n':
  380.                 blurflag++;;
  381.                 break;
  382. #ifdef iTIFF
  383.           case 't':
  384.                 read_image = read_tiff_file;
  385.                 break;
  386. #endif
  387. #ifdef iRLE
  388.           case 'u':
  389.                 read_image = read_rle_file;
  390.                 break;
  391. #endif
  392. #ifdef iJPEG
  393.           case 'j':
  394.                 read_image = read_jpeg_file;
  395.                 break;
  396. #endif
  397.           case 'w':
  398.                 read_image = read_rla_file;
  399.                 break;
  400.           case 'g':
  401.                 read_image = read_sgi_file;
  402.                 break;
  403.           case 'a':
  404.                 read_image = read_tga_file;
  405.                 break;
  406.           case 'f':
  407.                 read_image = read_gif_file;
  408.                 break;
  409.           case 'x':
  410.                 read_image = read_pcx_file;
  411.                 break;
  412.           case 'p':
  413.                 read_image = read_pbm_file;
  414.                 break;
  415.           case '!':
  416.                 print_copyright();
  417.                 break;
  418.           default :
  419.                 error( 0 );
  420.                 break;
  421.         }
  422.       }
  423.       argv++;
  424.       argc--;
  425.     }
  426.   }
  427.  
  428.   if ( argc < 2 )
  429.     error( 0 );
  430.  
  431.   /* Skip the command name */
  432.   argv++;
  433.  
  434.   /*
  435.    * Auxiliar file name where uncompress images if
  436.    * it's necesary.
  437.    */
  438.   sprintf(aux_file, "/usr/tmp/spix%d", parent_pid);
  439.  
  440.   if ( is_a_incode( *argv ) ) {
  441.     /*
  442.      * We wanna record frames, so we read the incode.
  443.      */
  444.     inpoint = *argv++;
  445.     start = Atoi( *argv++ );
  446.     if ( isnumber( *argv ) ) {
  447.       /*
  448.        * Hey!, it's a number. Ok, we wanna record a
  449.        * sequence of animated frames.
  450.        */
  451.       record_sequence++;
  452.       end = Atoi( *argv++ );
  453.       /* Number of images to fade */
  454.       i = argc - 4;
  455.     }else {
  456.       /*
  457.        * We wanna record only a frame.
  458.        */
  459.       end = start;
  460.       i = argc - 3;
  461.     }
  462.  
  463.     /*
  464.      * Ok, all ready ...
  465.      */
  466.     to_video( inpoint, start, end, i, argv );
  467.   }else {
  468.     /*
  469.      * What we wanna is display a image into the VFR, don't
  470.      * record it.
  471.      */
  472.     show_vfr_image( argv, argc - 1 );
  473.   }
  474.  
  475.   rm_compress();
  476. }
  477.  
  478. show_vfr_image( original_names, no_frames )
  479. char **original_names;
  480. int no_frames;
  481. {
  482.   register int i;
  483.   VFR_DEV *vfr;
  484.   bitmap_hdr outbitmap;
  485.   ifunptr save_read_interface;
  486.  
  487.   /*
  488.    * Connect with VFR interface.
  489.    */
  490.   vfr = ( VFR_DEV *) open_video();
  491.  
  492.   /*
  493.    * Create the background image.
  494.    */
  495.    create_solid_image( &outbitmap, XFBUFFER, YFBUFFER, 0, 0, 0 );
  496.  
  497.    /*
  498.     * Load all the imames.
  499.     */
  500.    for ( i = 0; i < no_frames; i++ ) {
  501.      add_image( &outbitmap, original_names[i], center );
  502.    }
  503.  
  504.    if ( super ) {
  505.      /*
  506.       * Glue the super to end image ( but we need use
  507.       * Alias interface ).
  508.       */
  509.      save_read_interface = read_image;
  510.      read_image = read_alias_file;
  511.      add_image( &outbitmap, SUPERNAME, 0 );
  512.      /* Restores old interface */
  513.      read_image = save_read_interface;
  514.    }
  515.  
  516.    /*
  517.     * Outbitmap is the new image.
  518.     */
  519.    write_vfr( vfr, &outbitmap );
  520.    freebitmap( &outbitmap );
  521. }
  522.  
  523. to_video( inpoint, start, end, no_frames, original_names )
  524. char *inpoint, **original_names;
  525. int start, end, no_frames;
  526. {
  527.   register int i;
  528.   VFR_DEV *vfr;
  529.   int point[4];
  530.   char *buffer_names[20];
  531.   char **frames_names;
  532.   int autoincrement;
  533.  
  534.   /*
  535.    * Connect with VFR interface.
  536.    */
  537.   vfr = ( VFR_DEV *) open_video();
  538.  
  539.   /*
  540.    * Caugth errors from child and interrupt key.
  541.    */
  542.   signal( SIGINT, press_break );
  543.  
  544.   /* Convert inpoint to numbers */
  545.   convert_inpoint( inpoint, point );
  546.  
  547.   if ( record_sequence ) {
  548.     /*
  549.      * We have a sequence of frames, so we'll build each
  550.      * name ( and need memory ) ...
  551.      */
  552.     frames_names = buffer_names;
  553.     malloc_names( no_frames, original_names, frames_names);
  554.     creat_names( no_frames, original_names, frames_names, start );
  555.     /* Time for each frame */
  556.     autoincrement = AUTOINCREMENT;
  557.   }else {
  558.     /* Use original names */
  559.     frames_names = original_names;
  560.     /* Time for the frame */
  561.     autoincrement = start;
  562.   }
  563.  
  564.   /*
  565.    * Child can build the image.
  566.    */
  567.   cpid = release_child( vfr, no_frames, frames_names );
  568.  
  569.   /*
  570.    * Initialize the VLAN.
  571.    */
  572.   init_vlan( vfr, inpoint, autoincrement );
  573.   /*
  574.    * Now the child can put the image into VFR.
  575.    */
  576.   unlock_vfr( cpid );
  577.  
  578.   for ( i = start; i <= end; i++ ) {
  579.     /* Check if the video are ready, PAUSE */
  580.     check_point( vfr );
  581.     /* Wait for child ( the child put the image into the VFR ) */
  582.     wait_for_children( cpid );
  583.     /* PerForm */
  584.     (void)Vfr_Vlan_Cmd( vfr, "PF" );
  585.     write_current_frame( point, i );
  586.     if ( i != end ) {
  587.       /* Not the end, so creat next names */
  588.       creat_names( no_frames, original_names, frames_names, i+1 );
  589.       cpid = release_child( vfr, no_frames, frames_names );
  590.     }
  591.     /* Check if the video perform the record */
  592.     check_status( vfr );
  593.     /*
  594.      * If the video record the last frame then child
  595.      * can put current image into VFR.
  596.      */
  597.     unlock_vfr( cpid );
  598.   }
  599.  
  600.   if ( record_sequence ) {
  601.     free_names( no_frames, frames_names );
  602.   }
  603. }
  604.  
  605. release_child ( vfr, no_frames, frames_names )
  606. VFR_DEV *vfr;
  607. int no_frames;
  608. char **frames_names;
  609. {
  610.   bitmap_hdr outbitmap;
  611.   ifunptr save_read_interface;
  612.   int pid;
  613.   int i;
  614.  
  615.   child_ready = parent_ready = 0;
  616.  
  617.   switch( pid = fork() ) {
  618.     case -1 :
  619.               /*
  620.                * fork() cannot respawn.
  621.                */
  622.               error( 13 );
  623.               break;
  624.     case  0 :
  625.               signal( SIGUSR1, fparent_ready );
  626.               signal( SIGUSR2, error_from_other );
  627.               /*
  628.                * Create the background image.
  629.                */
  630.               create_solid_image( &outbitmap, XFBUFFER, YFBUFFER, 0, 0, 0 );
  631.  
  632.               /*
  633.                * Load all the images.
  634.                */
  635.               for ( i = 0; i < no_frames; i++ ) {
  636.                 add_image( &outbitmap, frames_names[i], center );
  637.               }
  638.  
  639.               if ( super ) {
  640.                 /*
  641.                  * Glue the super to end image ( but we need use
  642.                  * Alias interface ).
  643.                  */
  644.                 save_read_interface = read_image;
  645.                 read_image = read_alias_file;
  646.                 add_image( &outbitmap, SUPERNAME, 0 );
  647.                 /* Restores old interface */
  648.                 read_image = save_read_interface;
  649.               }
  650.  
  651.               /*
  652.                * Wait for parent.
  653.                */
  654.               /*if ( !parent_ready )
  655.                 pause();*/
  656.               while ( !parent_ready );
  657.  
  658.               /*
  659.                * Outbitmap is the new image. We'll use last argument
  660.                * as file name.
  661.                */
  662.               write_vfr( vfr, &outbitmap );
  663.               freebitmap( &outbitmap );
  664.  
  665.               /*
  666.                * This is the end of the child so report it to
  667.                * the parent.
  668.                */
  669.               exit( 0 );
  670.               break;
  671.     default:
  672.               signal( SIGUSR2, error_from_other );
  673.               return pid;
  674.               break;
  675.   }
  676. }
  677.  
  678. add_image( base, filename, flag )
  679. bitmap_hdr *base;
  680. char *filename;
  681. int flag;
  682. {
  683.   bitmap_hdr inbitmap;
  684.   bitmap_hdr palbitmap;
  685.  
  686.   /*
  687.    * Read the image.
  688.    */
  689.   read_image( filename, &inbitmap );
  690.  
  691.   /*
  692.    * Adjust file to PAL and glue it to end image.
  693.    */
  694.   if ( !flag ) {
  695.     adjust( &inbitmap, &palbitmap, XSIZE, YSIZE, blurflag );
  696.     chroma( base, &palbitmap );
  697.   }else center_image( base, &inbitmap );
  698.  
  699.   /*
  700.    * Free buffers.
  701.    */
  702.   freebitmap( &inbitmap );
  703.   if( flag )
  704.     freebitmap( &palbitmap );
  705. }
  706.  
  707.  
  708. /**************************************
  709.  *
  710.  * SMPTE code handlers.
  711.  *
  712.  */
  713. calculate_outpoint( inpoint, outpoint, totalframes )
  714. char *inpoint;
  715. char *outpoint, totalframes;
  716. {
  717.   register int i;
  718.   int point[4];
  719.  
  720.   /* Convert inpoint to numbers */
  721.   convert_inpoint( inpoint, point );
  722.  
  723.   /* Calculate the outpoint */
  724.   for ( i = 0; i < totalframes; i++ )
  725.     add_incode( point );
  726.  
  727.   /* Convert outpoint numbers to a SMTPE format code */
  728.   sprintf ( outpoint, "%.2d:%.2d:%.2d:%.2d\n",
  729.             point[0], point[1], point[2], point[3] );
  730. }
  731.  
  732. is_a_incode( inpoint )
  733. char *inpoint;
  734. {
  735.   int marks = 0;
  736.  
  737.   while ( *inpoint )
  738.     if ( *inpoint++ == ':' )
  739.       marks++;
  740.  
  741.   return( marks == 3 );
  742. }
  743.  
  744. convert_inpoint( inpoint, point )
  745. char *inpoint;
  746. int *point;
  747. {
  748.   sscanf( inpoint, "%d:%d:%d:%d", &point[0], &point[1],
  749.                                   &point[2], &point[3] );
  750.  
  751.   if (  point[0] <  0 || point[1] <  0 || point[2] <  0 || point[3] < 0 ||
  752.         point[0] > 23 || point[1] > 59 || point[2] > 59 || point[3] > FPS )
  753.     error( 14 );
  754. }
  755.  
  756. write_current_frame( point, frame )
  757. int *point;
  758. int frame;
  759. {
  760. #if AUTOINCREMENT > 1
  761.   register int i;
  762. #endif
  763.  
  764.   if ( record_sequence ) {
  765.     fprintf ( stdout, "Recording frame %d on %.2d:%.2d:%.2d:%.2d\n", frame,
  766.               point[0], point[1], point[2], point[3] );
  767.   }else {
  768.     fprintf ( stdout, "Recording frame on %.2d:%.2d:%.2d:%.2d\n",
  769.               point[0], point[1], point[2], point[3] );
  770.   }
  771.  
  772.   /*
  773.    * If AUTOINCREMENT is greater than one then we need add more
  774.    * frames to the current, else we can skip this loop.
  775.    */
  776. #if AUTOINCREMENT > 1
  777.   for ( i = 0; i < AUTOINCREMENT; i++ )
  778. #endif
  779.     add_incode( point );
  780. }
  781.  
  782. add_incode( point )
  783. int *point;
  784. {
  785.   if ( point[3] == FPS ) {
  786.     point[3] = 0;
  787.     if ( point[2] == 59 ) {
  788.       point[2] = 0;
  789.       if ( point[1] == 59 ) {
  790.         point[1] = 0;
  791.         point[0]++;
  792.       }else point[1]++;
  793.     }else point[2]++;
  794.   }else point[3]++;
  795. }
  796.  
  797.  
  798. /**************************************
  799.  *
  800.  * File names builders.
  801.  *
  802.  */
  803. malloc_names( no_frames, original_names, frames_names )
  804. int no_frames;
  805. char **original_names, **frames_names;
  806. {
  807.   register int i;
  808.  
  809.   /* Allocate memory to sequence names */
  810.   for ( i = 0; i < no_frames; i++ ) {
  811.     frames_names[i] = (char *) Malloc( strlen(original_names[i] ) + 10 );
  812.   }
  813. }
  814.  
  815. creat_names( no_frames, original_names, frames_names, current_frame )
  816. int no_frames, current_frame;
  817. char **original_names, **frames_names;
  818. {
  819.   register int i;
  820.  
  821.   /* Creat the names of frames */
  822.   for ( i = 0; i < no_frames; i++ ) {
  823.     sprintf( frames_names[i], "%s.%d", original_names[i], current_frame );
  824.   }
  825. }
  826.  
  827. free_names( no_frames, frames_names )
  828. int no_frames;
  829. char **frames_names;
  830. {
  831.   register int i;
  832.  
  833.   /* Free sequence names */
  834.   for ( i = 0; i < no_frames; i++ ) {
  835.     Free( frames_names[i] );
  836.   }
  837. }
  838.  
  839.  
  840. /**************************************
  841.  *
  842.  * Signal handlers.
  843.  *
  844.  */
  845. vfr_not_ready()
  846. {
  847.   /*
  848.    * The alarm has been actived, the VFR doesn't respond.
  849.    */
  850.   error( 15 );
  851. }
  852.  
  853. error_from_other()
  854. {
  855.   /*
  856.    * The other process had an error.
  857.    */
  858.   exit( 1 );
  859. }
  860.  
  861. unlock_vfr( cpid )
  862. int cpid;
  863. {
  864.   /*
  865.    * Child can put image into VFR.
  866.    */
  867.   kill( cpid, SIGUSR1 );
  868. }
  869.  
  870. wait_for_children( cpid )
  871. int cpid;
  872. {
  873.   int rpid;
  874.   int status;
  875.  
  876.   /*
  877.    * The parent are ready, but the child ?.
  878.    */
  879.  
  880.   rpid = wait( &status );
  881.   if ( ! WIFEXITED( status ) )
  882.     error( 16 );
  883.   signal( SIGUSR2, SIG_DFL );
  884. }
  885.  
  886. press_break()
  887. {
  888.   /*
  889.    * User press break, so we need kill
  890.    * child ( if exist ).
  891.    */
  892.   if ( getpid() == parent_pid ) {
  893.     kill( cpid, SIGKILL );
  894.   }
  895.  
  896.   rm_compress();
  897.   exit( 0 );
  898. }
  899.  
  900. fparent_ready()
  901. {
  902.   parent_ready = 1;
  903.   signal( SIGUSR1, fparent_ready );
  904. }
  905.  
  906. fchild_ready()
  907. {
  908.   child_ready = 1;
  909.   signal( SIGUSR1, fchild_ready );
  910. }
  911.  
  912.  
  913. /**************************************
  914.  *
  915.  * Digital Image Editing procedures.
  916.  *
  917.  */
  918. adjust(inbitmap, outbitmap, newx, newy, noblur)
  919. bitmap_hdr *inbitmap;
  920. bitmap_hdr *outbitmap;
  921. int newx, newy;
  922. int noblur;
  923. {
  924.  
  925.   /* Really, is an image ? */
  926.   if ( inbitmap->magic != LUGUSED )
  927.     error( 21 );
  928.  
  929.   /* An 8 bits image ? */
  930.   if ( inbitmap->depth <= 8)
  931.     error(7);
  932.  
  933.   /* If PAL then return the same image */
  934.   if ( inbitmap->xsize == newx && inbitmap->ysize == newy ) {
  935.     copy_bitmap( inbitmap, outbitmap );
  936.   }else {
  937.     /*
  938.      * Fill new header ...
  939.      */
  940.     outbitmap->magic = LUGUSED;
  941.     outbitmap->xsize = newx;
  942.     outbitmap->ysize = newy;
  943.     outbitmap->depth = inbitmap->depth;         /* 24 planes */
  944.     outbitmap->colors = inbitmap->colors;
  945.  
  946.     /*
  947.      * ... and zoom each component.
  948.      */
  949.     VPRINTF(stderr, "Zooming R\n");
  950.     outbitmap->r= zoom( inbitmap->xsize, inbitmap->ysize,
  951.                         newx, newy, inbitmap->r, noblur );
  952.     VPRINTF(stderr, "Zooming G\n");
  953.     outbitmap->g= zoom( inbitmap->xsize, inbitmap->ysize,
  954.                         newx, newy, inbitmap->g, noblur );
  955.     VPRINTF(stderr, "Zooming B\n");
  956.     outbitmap->b= zoom( inbitmap->xsize, inbitmap->ysize,
  957.                         newx, newy, inbitmap->b, noblur );
  958.   }
  959. }
  960.  
  961. byte *zoom(oldx, oldy, newx, newy, buffer, noblur)
  962. int oldx, oldy;
  963. int newx, newy;
  964. register byte *buffer;
  965. int noblur;
  966. {
  967.   register int i, j;
  968.   register byte *base, *i_base;
  969.   double relx, rely;
  970.   double nrelx, nrely;
  971.   byte *ptr;
  972.   byte *nbuffer;
  973.   double foldx, foldy;
  974.   double fnewx, fnewy;
  975.  
  976.   /* Allocate memory for new zoomed image */
  977.   nbuffer = (byte *) Malloc( newx * newy );
  978.  
  979.   /*
  980.    * We use double variables ( only once transformation ).
  981.    */
  982.   fnewx = (double) newx, fnewy= (double) newy;
  983.   foldx = (double) oldx, foldy= (double) oldy;
  984.  
  985.   for ( i = 0; i< newy; i++ ) {
  986.     /* pointer to first pixel in current row */
  987.     base = nbuffer + (int) (newx*i);
  988.     /* scale row from 0 to 1 with the new image */
  989.     nrely = ((double) i) / fnewy;
  990.     /* get equal value in old image */
  991.     rely = nrely * foldy;
  992.     /* pointer to first pixel in that row */
  993.     i_base = buffer + ((int) rely) * oldx;
  994.     for ( j = 0; j < newx; j++ ) {
  995.       /* scale column from 0 to 1 ... */
  996.       nrelx = ((double) j) / fnewx;
  997.       /* then we get position of old image */
  998.       relx = nrelx * foldx;
  999.       ptr = i_base + (int) relx;
  1000.       *base++ = *ptr;
  1001.     }
  1002.   }
  1003.  
  1004.   /*
  1005.    * If image is greater then blur ( if no_blur switch
  1006.    * is actived ).
  1007.    */
  1008.   if ( (newx > oldx || newy > oldy) && !noblur ) {
  1009.     /* Blur zoomed image */
  1010.     ptr = blur( nbuffer, newx, newy );
  1011.     /* We have a new [blur] image, free first */
  1012.     Free( nbuffer );
  1013.     return ptr;
  1014.   }else {
  1015.     return nbuffer;
  1016.   }
  1017. }
  1018.  
  1019. byte *blur(buffer, xsize, ysize)
  1020. byte *buffer;
  1021. int xsize, ysize;
  1022. {
  1023.   register int i;
  1024.   int totalsize = xsize * ysize;
  1025.   byte *nbuffer;
  1026.   register byte *fin;
  1027.   register byte *base, *line_before;    /* pointers to new [blured] image */
  1028.   register byte *last, *next_line;      /* pointers to old [zoomed] image */
  1029.  
  1030.   /* Allocate memory for new blured image */
  1031.   nbuffer= (byte *) Malloc( totalsize );
  1032.  
  1033.   /*
  1034.    * First and last rows, and first and last pixels in
  1035.    * each row are not modified, so we copy image into new
  1036.    * buffer and keep all information.
  1037.    */
  1038.   bcopy( buffer, nbuffer, totalsize );
  1039.  
  1040.   /* Skip first and last row */
  1041.   ysize--;
  1042.   for ( i = 1; i < ysize; i++ ) {
  1043.     /* Skip first pixel of current line */
  1044.     base = nbuffer + i*xsize + 1;
  1045.     /* Point to current line */
  1046.     last = buffer + i*xsize + 1;
  1047.     /* Point to line before */
  1048.     line_before = last - xsize;
  1049.     /* Point to next line */
  1050.     next_line = last + xsize;
  1051.     /*
  1052.      * Pointer to last pixel for being modified of the current
  1053.      * line ( skip last pixel ).
  1054.      */
  1055.     fin = base + xsize - 1;
  1056.     while ( base < fin ) {
  1057.       /* Blur the current pixel */
  1058.       *base++ = .4 * *last +
  1059.                 .1 * *(last-1) + .1 * *(last+1) +
  1060.                 .1 * *line_before + .1 * *next_line +
  1061.                 .05 * *(line_before-1) + .05 * *(line_before+1) +
  1062.                 .05 * *(next_line-1) + .05 * *(next_line+1);
  1063.       /* Update pointers */
  1064.       next_line++;
  1065.       line_before++;
  1066.       last++;
  1067.     }
  1068.   }
  1069.  
  1070.   return nbuffer;
  1071. }
  1072.  
  1073. flip_image(inbitmap, outbitmap)
  1074. bitmap_hdr *inbitmap;
  1075. bitmap_hdr *outbitmap;
  1076. {
  1077.  
  1078.   /* Really, is an image ? */
  1079.   if ( inbitmap->magic != LUGUSED )
  1080.     error( 21 );
  1081.  
  1082.   VPRINTF(stderr, "Flipping image\n");
  1083.  
  1084.   /* Fill our header */
  1085.   outbitmap->magic = LUGUSED;
  1086.   outbitmap->xsize = inbitmap->xsize;
  1087.   outbitmap->ysize = inbitmap->ysize;
  1088.   outbitmap->depth = inbitmap->depth;
  1089.   outbitmap->colors = inbitmap->colors;
  1090.  
  1091.   outbitmap->r = flip( inbitmap->r, outbitmap->xsize, outbitmap->ysize );
  1092.   if ( outbitmap->depth > 8 ) {
  1093.     /* 24 planes */
  1094.     outbitmap->g = flip( inbitmap->g, outbitmap->xsize, outbitmap->ysize );
  1095.     outbitmap->b = flip( inbitmap->b, outbitmap->xsize, outbitmap->ysize );
  1096.   }else outbitmap->cmap = inbitmap->cmap;
  1097. }
  1098.  
  1099. byte *flip(buffer, xsize, ysize)
  1100. byte *buffer;
  1101. int xsize, ysize;
  1102. {
  1103.   register int i, j;
  1104.   int twolines;
  1105.   byte *image, *ptr;
  1106.  
  1107.   ptr = image = (byte *) Malloc( xsize * ysize );
  1108.   twolines = 2 * xsize;
  1109.   buffer += xsize * ysize - xsize;
  1110.  
  1111.   for ( i = 0; i < ysize; i++) {
  1112.     for ( j = 0; j < xsize; j++ ) {
  1113.       *ptr++ = *buffer++;
  1114.     }
  1115.     buffer -= twolines;
  1116.   }
  1117.  
  1118.   return image;
  1119. }
  1120.  
  1121. to24( inbitmap, outbitmap )
  1122. bitmap_hdr *inbitmap;
  1123. bitmap_hdr *outbitmap;
  1124. {
  1125.   int total_size;
  1126.   color_map *map;
  1127.   byte *r, *g, *b;
  1128.   byte *base;
  1129.   byte *end;
  1130.  
  1131.   /* Really, is an image ? */
  1132.   if ( inbitmap->magic != LUGUSED )
  1133.     error( 21 );
  1134.  
  1135.   /* Fill new header */
  1136.   outbitmap->magic = LUGUSED;
  1137.   outbitmap->xsize = inbitmap->xsize;
  1138.   outbitmap->ysize = inbitmap->ysize;
  1139.   outbitmap->depth = 24;
  1140.   outbitmap->colors = ( 1 << outbitmap->depth );
  1141.   total_size  = outbitmap->xsize * outbitmap->ysize;
  1142.   r = outbitmap->r = (byte *) Malloc(total_size);
  1143.   g = outbitmap->g = (byte *) Malloc(total_size);
  1144.   b = outbitmap->b = (byte *) Malloc(total_size);
  1145.  
  1146.   /* A pointer to cmap */
  1147.   map  = (color_map *) inbitmap->cmap;
  1148.   /* A pointer to original 8 bits buffer */
  1149.   base = inbitmap->r;
  1150.   /* A pointer to the end */
  1151.   end = base + total_size;
  1152.  
  1153.   VPRINTF(stderr, "Reconverting to 24 planes\n");
  1154.   while ( base < end ) {
  1155.     *r++ = map[*base][0];
  1156.     *g++ = map[*base][1];
  1157.     *b++ = map[*base][2];
  1158.     base++;
  1159.   }
  1160. }
  1161.  
  1162. create_solid_image(image, xsize, ysize, r, g, b)
  1163. bitmap_hdr *image;
  1164. int xsize, ysize;
  1165. byte r, g, b;
  1166. {
  1167.   int totalsize = xsize * ysize;
  1168.  
  1169.   image->magic = LUGUSED;
  1170.   image->xsize = xsize;
  1171.   image->ysize = ysize;
  1172.   image->depth = 24;
  1173.   image->colors = ( 1 << image->depth );
  1174.   image->r= (byte *) Malloc( totalsize );
  1175.   image->g= (byte *) Malloc( totalsize );
  1176.   image->b= (byte *) Malloc( totalsize );
  1177.   if ( r )
  1178.     memset( image->r, r, totalsize );
  1179.   if ( g )
  1180.     memset( image->g, g, totalsize );
  1181.   if ( b )
  1182.     memset( image->b, b, totalsize );
  1183. }
  1184.  
  1185. center_image( base, super )
  1186. bitmap_hdr *base, *super;
  1187. {
  1188.   int xmid, ymid;
  1189.  
  1190.   xmid = ( XSIZE / 2 ) - ( super->xsize / 2 );
  1191.   ymid = ( YSIZE / 2 ) - ( super->ysize / 2 );
  1192.  
  1193.   paste( base, super, xmid, ymid );
  1194. }
  1195.  
  1196. paste(base, super, xpos, ypos)
  1197. bitmap_hdr *base, *super;
  1198. int xpos, ypos;
  1199. {
  1200.   register int i, j;
  1201.   byte *r1, *g1, *b1;
  1202.   byte *r2, *g2, *b2;
  1203.  
  1204.    /* Really, are images ? */
  1205.   if ( base->magic != LUGUSED || super->magic != LUGUSED )
  1206.     error( 21 );
  1207.  
  1208.   r1 = base->r,  g1 = base->g,  b1 = base->b;
  1209.   r2 = super->r, g2 = super->g, b2 = super->b;
  1210.  
  1211.   /*
  1212.    * Skip pre-paste rows.
  1213.    */
  1214.   for (i= 0; i< ypos; i++) {
  1215.     r1 += base->xsize;
  1216.     g1 += base->xsize;
  1217.     b1 += base->xsize;
  1218.   }
  1219.  
  1220.   /*
  1221.    * While we are inside surface to paste ...
  1222.    */
  1223.   for ( i = 0; i < super->ysize; i++ ) {
  1224.     for ( j = 0; j< base->xsize; j++ )
  1225.       if ( j < xpos  || j >= super->xsize+xpos ) {
  1226.         r1++, g1++, b1++;
  1227.       }else {
  1228.         if ( (*r2 + *g2 + *b2) < CHROMABORDER ) {
  1229.           r1++, g1++, b1++;
  1230.           r2++, g2++, b2++;
  1231.         }else {
  1232.           *r1++ = *r2++;
  1233.           *g1++ = *g2++;
  1234.           *b1++ = *b2++;
  1235.         }
  1236.       }
  1237.   }
  1238. }
  1239.  
  1240. chroma( base, super )
  1241. bitmap_hdr *base;
  1242. bitmap_hdr *super;
  1243. {
  1244.   register int i, j;
  1245.   byte *r1, *g1, *b1;
  1246.   byte *r2, *g2, *b2;
  1247.  
  1248.   /* Really, are images ? */
  1249.   if ( base->magic != LUGUSED || super->magic != LUGUSED )
  1250.     error( 21 );
  1251.  
  1252.   /* Set pointers */
  1253.   r1= base->r , g1= base->g , b1= base->b;
  1254.   r2= super->r, g2= super->g, b2= super->b;
  1255.  
  1256.   for ( i = 0; i < super->ysize; i++ ) {
  1257.     for ( j = 0; j < base->xsize; j++ ) {
  1258.       if ( j < super->xsize ) {
  1259.         /* We are into super image. */
  1260.         if ( (*r2 + *g2 + *b2) < CHROMABORDER ) {
  1261.           r1++, g1++, b1++;
  1262.           r2++, g2++, b2++;
  1263.         }else {
  1264.           *r1++ = *r2++;
  1265.           *g1++ = *g2++;
  1266.           *b1++ = *b2++;
  1267.         }
  1268.       }else {
  1269.         /* Out of super borders */
  1270.         r1++, g1++, b1++;
  1271.       }
  1272.     }
  1273.   }
  1274. }
  1275.  
  1276. byte *create_bw_pallete()
  1277. {
  1278.   register int i;
  1279.   byte *buffer = (byte *) Malloc( 3 * 256 );
  1280.   byte *ptr;
  1281.  
  1282.   /* I'll use a pointer */
  1283.   ptr = buffer;
  1284.   for (i = 0; i < 256; i++) {
  1285.     *ptr++= (byte) i;   /* R */
  1286.     *ptr++= (byte) i;   /* G */
  1287.     *ptr++= (byte) i;   /* B */
  1288.   }
  1289.  
  1290.   return buffer;
  1291. }
  1292.  
  1293.  
  1294. /**************************************
  1295.  *
  1296.  * ALIAS interface
  1297.  *
  1298.  */
  1299. read_alias_file( name, bitmap )
  1300. char *name;
  1301. bitmap_hdr *bitmap;
  1302. {
  1303.   FILE *handle;
  1304.  
  1305.   /* Open the file descriptor */
  1306.   if ( name != NULL )
  1307.     handle = Fopen( name, "r" );
  1308.   else handle = stdin;
  1309.  
  1310.   /* Read the bitmap */
  1311.   read_alias( handle, bitmap );
  1312.  
  1313.   /* Close the file */
  1314.   Fclose( handle );
  1315. }
  1316.  
  1317. read_alias(handle, image)
  1318. FILE *handle;
  1319. bitmap_hdr *image;
  1320. {
  1321.   register int i;
  1322.   alias_hdr header;
  1323.   int total_size;
  1324.   register int xsize;
  1325.   byte *end;
  1326.   byte *r, *g, *b;
  1327.   int can_read_all;
  1328.   byte *buffer, *ptr;
  1329.   int filesize;
  1330.   int allplanes = 0;
  1331.   int internalerror;
  1332.   int cmapsize;
  1333.  
  1334.   /*
  1335.    * Alias "pix" is a bad format to read line per line. If every
  1336.    * triplet is different to its next, then we read 4 bytes, 4 bytes ...
  1337.    * ... and read a file of 1 Mb. with this form ... hmmmmmm !!!
  1338.    * This is necesary if we are reading from stdin, but if we have
  1339.    * a file, we'll read the file into memory ( one access to disk ).
  1340.    */
  1341. #ifndef DEC
  1342.   can_read_all = (handle != stdin);     /* a regular file ? */
  1343.   VPRINTF(stderr, "Reading Alias from %s\n",
  1344.                   (can_read_all ? "file" : "stdin"));
  1345. #else
  1346.   can_read_all = 0;
  1347. #endif
  1348.  
  1349.   if (can_read_all) {
  1350.     /*
  1351.      * We are using a regular file ( not a pipe ), so we can
  1352.      * read it into memory.
  1353.      */
  1354.     ptr= buffer= (byte *) read_file(handle, &filesize);
  1355.     VPRINTF(stderr, "File size: %d\n", filesize);
  1356.     /* Copy the header */
  1357.     bcopy(ptr, &header, sizeof(alias_hdr));
  1358.     ptr += sizeof(alias_hdr);           /* skip the header */
  1359.   }else {
  1360.     /*
  1361.      * We are using stdin so ...
  1362.      */
  1363.     /* We need the header of Alias pix */
  1364.     VPRINTF(stderr, "Reading Alias header\n");
  1365.     read_alias_header(handle, &header);
  1366.   }
  1367.  
  1368.   /* Size in pixels */
  1369.   total_size = header.xsize * header.ysize;
  1370.  
  1371.   /* Fill our header */
  1372.   image->magic  = LUGUSED;
  1373.   image->xsize  = header.xsize;
  1374.   image->ysize  = header.ysize;
  1375.   image->depth  = header.depth;
  1376.   image->colors = 1 << image->depth;
  1377.   allplanes = ( image->depth > 8 );     /* an image with 24 planes ? */
  1378.   VPRINTF(stderr, "Image size: %dx%d\n", image->xsize, image->ysize);
  1379.   VPRINTF(stderr, "Depth: %d\n", image->depth);
  1380.  
  1381.   /* Get some memory */
  1382.   if ( allplanes ) {
  1383.     /*
  1384.      * We have a image with 24 planes, so we need three buffers to
  1385.      * store it.
  1386.      */
  1387.     r= image->r = (byte *) Malloc(total_size);
  1388.     g= image->g = (byte *) Malloc(total_size);
  1389.     b= image->b = (byte *) Malloc(total_size);
  1390.   }else {
  1391.     /*
  1392.      * The image has less than 256 colors, so we use one plane,
  1393.      * for the bitmap, and the cmap.
  1394.      */
  1395.     r= image->r = (byte *) Malloc(total_size);
  1396.     cmapsize= image->colors * 3;        /* size in bytes */
  1397.     image->cmap = (byte *) Malloc(cmapsize);
  1398.     /* Creating the cmap from file */
  1399.     VPRINTF(stderr, "Creating cmap\n");
  1400.     create_alias_cmap(image);
  1401.   }
  1402.  
  1403.   /*
  1404.    * We are ready to uncompress Alias file.
  1405.    */
  1406.   VPRINTF(stderr, "Uncompressing Alias file\n");
  1407.   if (can_read_all) {
  1408.     /*
  1409.      * We have the file into memory so we uncode it directly.
  1410.      */
  1411.     end = r + total_size;       /* the end of main buffer */
  1412.     if ( allplanes )
  1413.       uncode_alias24(ptr, r, g, b, end);
  1414.     else uncode_alias(ptr, r, end);
  1415.     /*
  1416.      * We have read the file and uncompressed, so we can
  1417.      * free the memory ( and exit ).
  1418.      */
  1419.     Free(buffer);
  1420.   }else {
  1421.     /*
  1422.      * We'll read each line from Alias file ( stdin ) until we
  1423.      * fill our buffers or we get an error.
  1424.      */
  1425.     xsize = image->xsize;
  1426.     for (i= 0; i< image->ysize; i++) {
  1427.       /*
  1428.        * Read and decode the Alias raster information, two cases:
  1429.        * with planes > 8 => a triplet (RGB), else a index.
  1430.        */
  1431.       if ( allplanes ) {
  1432.         /* An image with 24 planes */
  1433.         internalerror= read_line_alias24(handle, r, b, g, xsize);
  1434.       }else {
  1435.         /* An image with indexes to cmap */
  1436.         internalerror= read_line_alias(handle, r, xsize);
  1437.       }
  1438.       if (internalerror) {
  1439.         /* An error, so we exit */
  1440.         fprintf(stderr, "Error while reading line %d\n", i);
  1441.         return 1;
  1442.       }else {
  1443.         /* A line has been read, so we increment theirs pointers */
  1444.         r += xsize;
  1445.         if ( allplanes ) {
  1446.           g += xsize;
  1447.           b += xsize;
  1448.         }
  1449.       }
  1450.     }
  1451.   } /* end of reading from stdin */
  1452.  
  1453.   /* Finish with no problems */
  1454.   return 0;
  1455. }
  1456.  
  1457. read_alias_header(handle, header)
  1458. FILE *handle;
  1459. alias_hdr *header;
  1460. {
  1461.   byte buffer[10];
  1462.  
  1463.   /* Read the header */
  1464.   Fread(buffer, 10, 1, handle);
  1465.  
  1466.   /* Fill the header structure */
  1467.   header->xsize = ( buffer[0] << 8 ) | buffer[1];
  1468.   header->ysize = ( buffer[2] << 8 ) | buffer[3];
  1469.   header->xinit = ( buffer[4] << 8 ) | buffer[5];
  1470.   header->yinit = ( buffer[6] << 8 ) | buffer[7];
  1471.   header->depth = ( buffer[8] << 8 ) | buffer[9];
  1472.  
  1473.   /* Check some things */
  1474.   if ( header->xsize < 1 || header->xsize > 2560 ||
  1475.        header->ysize < 1 || header->ysize > 2560 )
  1476.     error(5);
  1477.   if ( header->depth > 24 || header->depth < 1 )
  1478.     error(5);
  1479. }
  1480.  
  1481. read_line_alias24(handle, r, g, b, size)
  1482. FILE *handle;
  1483. byte *r, *g, *b;
  1484. register int size;
  1485. {
  1486.   register int i;
  1487.   register int count = 0;
  1488.   byte *end;
  1489.   byte buffer[4];
  1490.  
  1491.   end = r + size;
  1492.   while (count < size) {
  1493.     /*
  1494.      * Alias code format:  <repeat>BGR => 4 bytes.
  1495.      */
  1496.     if ( !fread(buffer, 4, 1, handle) )         /* read next buffer */
  1497.       return 1;   /* hey !, an EOF here ?. Hmmmm, this is an error ! */
  1498.  
  1499.     count += buffer[0];                 /* # of repetitions */
  1500.     /* repeat 'buffer[0]' times these triplet */
  1501.     for (i= 0; i< buffer[0]; i++) {
  1502.       *r++ = buffer[3];
  1503.       *g++ = buffer[1];
  1504.       *b++ = buffer[2];
  1505.     }
  1506.     if ( r > end )
  1507.       error(6);         /* uncompress more bytes than size */
  1508.   }
  1509.  
  1510.   /* No problems */
  1511.   return 0;
  1512. }
  1513.  
  1514. read_line_alias(handle, r, size)
  1515. FILE *handle;
  1516. byte *r;
  1517. register int size;
  1518. {
  1519.   register int i;
  1520.   register int count = 0;
  1521.   byte *end;
  1522.   byte buffer[2];
  1523.  
  1524.   end = r + size;
  1525.   while (count < size) {
  1526.     /*
  1527.      * Alias code format:  <repeat><index> => 2 bytes.
  1528.      */
  1529.     if ( !fread(buffer, 2, 1, handle) )         /* read next buffer */
  1530.       return 1;   /* hey !, an EOF here ?. Hmmmm, this is an error ! */
  1531.  
  1532.     count += buffer[0];                 /* # of repetitions */
  1533.     /* repeat 'buffer[0]' times these index */
  1534.     for (i= 0; i< buffer[0]; i++) {
  1535.       *r++ = buffer[1];
  1536.     }
  1537.     if ( r > end )
  1538.       error(6);         /* uncompress more bytes than size */
  1539.   }
  1540.  
  1541.   /* No problems */
  1542.   return 0;
  1543. }
  1544.  
  1545. create_alias_cmap(image)
  1546. bitmap_hdr *image;
  1547. {
  1548.   register int i;
  1549.   byte *ptr;
  1550.  
  1551.   /*
  1552.    * Alias 8 bits files are b&w, so ...
  1553.    */
  1554.   ptr = (byte *) image->cmap;
  1555.   for (i= 0; i< image->colors; i++) {
  1556.     *ptr++ = i;
  1557.     *ptr++ = i;
  1558.     *ptr++ = i;
  1559.   }
  1560. }
  1561.  
  1562. uncode_alias24(ptr, rbuf, gbuf, bbuf, end)
  1563. byte *ptr;
  1564. byte *rbuf, *gbuf, *bbuf;
  1565. byte *end;
  1566. {
  1567.   register int i;
  1568.   byte r, g, b;
  1569.  
  1570.   while ( rbuf < end ) {        /* while not end of buffer */
  1571.     if ( (i= *ptr++) > 1 ) {            /* how mary repetitions ? */
  1572.       b= *ptr++;
  1573.       g= *ptr++;
  1574.       r= *ptr++;
  1575.       while( i-- ) {    /* copy these triplet 'i' times */
  1576.         *rbuf++ = r;
  1577.         *gbuf++ = g;
  1578.         *bbuf++ = b;
  1579.       }
  1580.     }else {                     /* else, copy these triplet */
  1581.       *bbuf++ = *ptr++;
  1582.       *gbuf++ = *ptr++;
  1583.       *rbuf++ = *ptr++;
  1584.     }
  1585.   }
  1586. }
  1587.  
  1588. uncode_alias(ptr, rbuf, end)
  1589. byte *ptr;
  1590. byte *rbuf;
  1591. byte *end;
  1592. {
  1593.   register int i;
  1594.   byte r;
  1595.  
  1596.   while ( rbuf < end ) {        /* while not end of buffer */
  1597.     if ( (i= *ptr++) > 1 ) {            /* how mary repetitions ? */
  1598.       r= *ptr++;
  1599.       while( i-- ) {    /* copy these index 'i' times */
  1600.         *rbuf++ = r;
  1601.       }
  1602.     }else {                     /* else, copy these index */
  1603.       *rbuf++ = *ptr++;
  1604.     }
  1605.   }
  1606. }
  1607.  
  1608.  
  1609. /**************************************
  1610.  *
  1611.  * Wavefront's RLA interface.
  1612.  *
  1613.  */
  1614. read_rla_file( name, bitmap )
  1615. char *name;
  1616. bitmap_hdr *bitmap;
  1617. {
  1618.   FILE *handle;
  1619.  
  1620.   /* Open the file descriptor */
  1621.   if ( name != NULL )
  1622.     handle = Fopen( name, "r" );
  1623.   else error( 20 );
  1624.  
  1625.   /* Read the bitmap */
  1626.   read_rla( handle, bitmap );
  1627.  
  1628.   /* Close the file */
  1629.   Fclose( handle );
  1630. }
  1631.  
  1632. read_rla(handle, image)
  1633. FILE *handle;
  1634. bitmap_hdr *image;
  1635. {
  1636.   register int i, j;
  1637.   RLA_HEADER rla;
  1638.   int totalsize;
  1639.   byte *r, *g, *b;
  1640.   int *offsets;
  1641.   short no_bytes;
  1642.   byte *ptr;
  1643.   byte *raux, *gaux, *baux;
  1644.   byte *rptr, *gptr, *bptr;
  1645.  
  1646.   /*
  1647.    * Read the RLA's header.
  1648.    */
  1649.   Fread(&rla, 740, 1, handle);
  1650.   image->magic = LUGUSED;
  1651.   image->xsize = rla.window.right - rla.window.left + 1;
  1652.   image->ysize = rla.window.top - rla.window.bottom + 1;
  1653.   image->depth = 24;
  1654.   image->colors = ( 1 << image->depth );
  1655.   totalsize= image->xsize * image->ysize;
  1656.  
  1657.   if ( image->xsize < 1 || image->xsize > 2559 ||
  1658.        image->ysize < 1 || image->ysize > 2559 )
  1659.     error(5);
  1660.  
  1661.   /*
  1662.    * Allocate for bitmap components.
  1663.    */
  1664.   r = image->r = (byte *) Malloc(totalsize);
  1665.   g = image->g = (byte *) Malloc(totalsize);
  1666.   b = image->b = (byte *) Malloc(totalsize);
  1667.  
  1668.   /* Some space to unpack scanlines */
  1669.   raux = (byte *) Malloc(image->xsize);
  1670.   gaux = (byte *) Malloc(image->xsize);
  1671.   baux = (byte *) Malloc(image->xsize);
  1672.   ptr  = (byte *) Malloc(2560);
  1673.  
  1674.   /* Allocate space for offsets */
  1675.   offsets = (int *) Malloc(image->ysize*sizeof(int));
  1676.   Fread(offsets, sizeof(int), image->ysize, handle);
  1677.  
  1678.   for (i = rla.window.top; i >= rla.window.bottom; i--) {
  1679.     if ( i > rla.act_window.top || i < rla.act_window.bottom ) {
  1680.       /* Outlines => zeros ( done with Malloc ) */
  1681.       r += image->xsize;
  1682.       g += image->xsize;
  1683.       b += image->xsize;
  1684.     }else {
  1685.       /*
  1686.        * Set pointer where offsets table says.
  1687.        */
  1688.       if (fseek(handle, (long) offsets[i - rla.act_window.bottom], 0))
  1689.         error( 20 );
  1690.       /*
  1691.        * Uncode R channel.
  1692.        */
  1693.       Fread(&no_bytes, sizeof(short), 1, handle);
  1694.       Fread(ptr, (int) no_bytes, 1, handle);
  1695.       decodeRLA(ptr, raux, no_bytes);
  1696.       /*
  1697.        * Uncode G channel.
  1698.        */
  1699.       Fread(&no_bytes, sizeof(short), 1, handle);
  1700.       Fread(ptr, (int) no_bytes, 1, handle);
  1701.       decodeRLA(ptr, gaux, no_bytes);
  1702.       /*
  1703.        * Uncode B channel.
  1704.        */
  1705.       Fread(&no_bytes, sizeof(short), 1, handle);
  1706.       Fread(ptr, (int) no_bytes, 1, handle);
  1707.       decodeRLA(ptr, baux, no_bytes);
  1708.  
  1709.       /*
  1710.        * If you wanna you do the same task with matte ( but I
  1711.        * don't mind that channel ).
  1712.        */
  1713.  
  1714.       /*
  1715.        * We uncode the active window, now we fill the real window
  1716.        * with these datas and aditional zeros.
  1717.        */
  1718.       rptr= raux;
  1719.       gptr= gaux;
  1720.       bptr= baux;
  1721.       for (j = rla.window.left; j <= rla.window.right; j++) {
  1722.         if ( j < rla.act_window.left || j > rla.act_window.right ) {
  1723.           /* Outlines => zeros ( done with Malloc ) */
  1724.           *r++, *g++, *b++;
  1725.          }else {
  1726.            *r++ = *rptr++;
  1727.            *g++ = *gptr++;
  1728.            *b++ = *bptr++;
  1729.          }
  1730.       }
  1731.     }
  1732.   }
  1733.  
  1734.   /*
  1735.    * Free memory.
  1736.    */
  1737.   Free(raux);
  1738.   Free(gaux);
  1739.   Free(baux);
  1740.   Free(offsets);
  1741.   Free(ptr);
  1742.  
  1743.   /* no errors */
  1744.   return 0;
  1745. }
  1746.  
  1747. decodeRLA(buf_in, buf_out, len)
  1748. byte    *buf_in;
  1749. byte    *buf_out;
  1750. int     len;
  1751. {
  1752.    int  ct;
  1753.    byte *end;
  1754.  
  1755.    end = buf_out + len;
  1756.    while(len > 0) {
  1757.       ct = *buf_in++;
  1758.       len--;
  1759.       if (ct < 128) {
  1760.         /* repeat pixel value ct+1 times */
  1761.          while (ct-- >= 0)
  1762.             *buf_out++ = *buf_in;
  1763.          buf_in++;
  1764.          len--;
  1765.       }
  1766.       else {
  1767.         /* copy ct unencoded values */
  1768.          for (ct = 256-ct; ct-- > 0; len--)
  1769.             *buf_out++ = *buf_in++;
  1770.       }
  1771.       /* if ( buf_out > end )
  1772.         error(6);*/
  1773.    }
  1774. }
  1775.  
  1776.  
  1777. /**************************************
  1778.  *
  1779.  * SGI interface
  1780.  *
  1781.  */
  1782. read_sgi_file( name, bitmap )
  1783. char *name;
  1784. bitmap_hdr *bitmap;
  1785. {
  1786.   register int i, j;
  1787.   IMAGE *sgi_image;
  1788.   byte *r, *g, *b;
  1789.   short *sgibuf;
  1790.   int totalsize;
  1791.   int flag24;
  1792.  
  1793.   VPRINTF(stderr, "Reading SGI image\n");
  1794.  
  1795.   if ( (sgi_image = iopen( name, "r" )) == NULL )
  1796.     error( 1 );
  1797.   /* if ( sgi_image->zsize < 3 )
  1798.       error( 7 ); */
  1799.  
  1800.   /*
  1801.    * Fill our header.
  1802.    */
  1803.   bitmap->magic = LUGUSED;
  1804.   bitmap->xsize = sgi_image->xsize;
  1805.   bitmap->ysize = sgi_image->ysize;
  1806.   bitmap->depth = 8 * sgi_image->zsize;
  1807.   bitmap->colors = ( 1 << bitmap->depth );
  1808.   flag24 = (bitmap->depth > 8);
  1809.  
  1810.   totalsize = bitmap->xsize * bitmap->ysize;
  1811.   r = bitmap->r = (byte *) Malloc( totalsize );
  1812.   if ( flag24 ) {
  1813.     g = bitmap->g = (byte *) Malloc( totalsize );
  1814.     b = bitmap->b = (byte *) Malloc( totalsize );
  1815.   }else bitmap->cmap = (byte *) create_bw_pallete();
  1816.  
  1817.   /*
  1818.    * Allocate memory for sgi raster line.
  1819.    */
  1820.   sgibuf = (short *) Malloc( sizeof(short) * bitmap->xsize );
  1821.  
  1822.   for ( i = 0; i < bitmap->ysize; i++ ) {
  1823.     j = bitmap->ysize - 1 - i;
  1824.     getrow( sgi_image, sgibuf, j, 0 );
  1825.     (void) shorttobyte( sgibuf, r, bitmap->xsize );
  1826.     r += bitmap->xsize;
  1827.     if ( flag24 ) {
  1828.       getrow( sgi_image, sgibuf, j, 1 );
  1829.       (void) shorttobyte( sgibuf, g, bitmap->xsize );
  1830.       g += bitmap->xsize;
  1831.       getrow( sgi_image, sgibuf, j, 2 );
  1832.       (void) shorttobyte( sgibuf, b, bitmap->xsize );
  1833.       b += bitmap->xsize;
  1834.     }
  1835.   }
  1836.  
  1837.   iclose( sgi_image );
  1838.   Free( sgibuf );
  1839. }
  1840.  
  1841.  
  1842. /**************************************
  1843.  *
  1844.  * TARGA interface
  1845.  *
  1846.  */
  1847. read_tga_file( name, bitmap )
  1848. char *name;
  1849. bitmap_hdr *bitmap;
  1850. {
  1851.   FILE *handle;
  1852.   bitmap_hdr image;
  1853.  
  1854.   /* Open the file descriptor */
  1855.   if ( name != NULL )
  1856.     handle = Fopen( name, "r" );
  1857.   else handle = stdin;
  1858.  
  1859.   /* Read the bitmap */
  1860.   read_tga( handle, &image );
  1861.  
  1862.   /* Close the file */
  1863.   Fclose( handle );
  1864.  
  1865.   /*
  1866.    * Now we could have a 8 or 24 bits image, but 'tovcr'
  1867.    * needs true color images, so check it...
  1868.    */
  1869.   if ( image.depth > 8 ){
  1870.     copy_bitmap( &image, bitmap );
  1871.   }else {
  1872.     to24( &image, bitmap );
  1873.   }
  1874.  
  1875.   /* Free the tmp bitmap */
  1876.   freebitmap( &image );
  1877. }
  1878.  
  1879. read_tga(handle, image)
  1880. FILE *handle;
  1881. bitmap_hdr *image;
  1882. {
  1883.   register int i;
  1884.   tga_hdr tga;
  1885.   int allplanes = 0;
  1886.   int lessplanes = 0;
  1887.   int forceallplanes = 0;
  1888.   byte aux4[4];
  1889.   int totalsize;
  1890.   byte *ptr;
  1891.   byte *r, *g, *b;
  1892.   byte *aux;
  1893.  
  1894.   /* Read the Tagar header */
  1895.   read_tga_header( handle, &tga );
  1896.  
  1897.   /* Fill our header */
  1898.   image->magic = LUGUSED;
  1899.   image->xsize = tga.xsize;
  1900.   image->ysize = tga.ysize;
  1901.   totalsize = image->xsize * image->ysize;
  1902.   /*
  1903.    * Targa format can be mapped but with moer than 256 colors,
  1904.    * our mapped images only support 8 bits, so we need convert
  1905.    * these images to 24 bits.
  1906.    */
  1907.   switch ( tga.image_type ) {
  1908.     case TGA_RGB:
  1909.     case TGA_RLE_RGB:
  1910.                 allplanes = 1;
  1911.                 break;
  1912.     case TGA_MAPPED:
  1913.     case TGA_RLE_MAPPED:
  1914.                 lessplanes = 1;
  1915.                 if ( tga.cmap_type != 1 )
  1916.                   error( 10 );
  1917.                 if ( tga.cmap_length > 256 )
  1918.                   forceallplanes = 1;
  1919.                 break;
  1920.   }
  1921.  
  1922.   /*
  1923.    * Read the cmap info ( if exists ).
  1924.    */
  1925.   if ( tga.cmap_type ) {
  1926.     VPRINTF(stderr, "Reading cmap info\n");
  1927.     if ( allplanes ) {
  1928.       /* We only need skip the cmap block */
  1929.       for ( i = 0; i < tga.cmap_length; i++ )
  1930.         read_tga_data( aux4, tga.cmap_entry_size, handle );
  1931.     }else {
  1932.       ptr = image->cmap = (byte *) Malloc( 3 * tga.cmap_length );
  1933.       for ( i = 0; i < tga.cmap_length; i++) {
  1934.         read_tga_data( ptr, tga.cmap_entry_size, handle );
  1935.         ptr += 3;
  1936.       }
  1937.     }
  1938.   }
  1939.  
  1940.   /*
  1941.    * Allocate memory for the bitmap and fill our header.
  1942.    */
  1943.   image->xsize = tga.xsize;
  1944.   image->ysize = tga.ysize;
  1945.   totalsize = image->xsize * image->ysize;
  1946.   if ( allplanes || forceallplanes ) {
  1947.     r = image->r = (byte *) Malloc( totalsize );
  1948.     g = image->g = (byte *) Malloc( totalsize );
  1949.     b = image->b = (byte *) Malloc( totalsize );
  1950.     image->depth = 24;
  1951.   }else {
  1952.     r = image->r = (byte *) Malloc( totalsize );
  1953.     image->depth = no_bits( tga.cmap_length ) + 1;
  1954.   }
  1955.   image->colors = ( 1 << image->depth );
  1956.  
  1957.   /*
  1958.    * Read the raster information.
  1959.    */
  1960.   if ( allplanes ) {
  1961.     read_tga24( handle, r, g, b, &tga );
  1962.  
  1963.   }else if ( forceallplanes )
  1964.           read_tga_to24( handle, r, g, b, &tga, image->cmap );
  1965.         else read_tga8( handle, r, &tga );
  1966.  
  1967.   if ( TGAFLIP( tga.image_descriptor ) ) {
  1968.     VPRINTF(stderr, "Flipping the image\n");
  1969.     aux = (byte *) flip( r, image->xsize, image->ysize );
  1970.     Free( r );
  1971.     image->r = aux;
  1972.     if ( allplanes ) {
  1973.       /* A true color image, so we have G and B buffers */
  1974.       aux = (byte *) flip( g, image->xsize, image->ysize );
  1975.       Free( g );
  1976.       image->g = aux;
  1977.       aux = (byte *) flip( b, image->xsize, image->ysize );
  1978.       Free( b );
  1979.       image->b = aux;
  1980.     }
  1981.   }
  1982. }
  1983.  
  1984.