home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / graphics / utility / mgif35s / readpi.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-13  |  11.3 KB  |  530 lines

  1. /*
  2.  *    read a degas pi1 or pi2 files. decode and stuff into raster array
  3.  *    and color map. both ReadPI1 and ReadPI2 are here...
  4.  */
  5.  
  6. static char *sccsid = "@(#) readpi.c 1.0 91/6/9 rosenkra\0\0                 ";
  7.  
  8. #include <stdio.h>
  9. #include <osbind.h>
  10. #include "mgif.h"
  11.  
  12.  
  13. #ifdef LOCAL
  14. #undef LOCAL
  15. #endif
  16. #ifdef GLOBAL
  17. #undef GLOBAL
  18. #endif
  19. #define LOCAL        /*static*/
  20. #define GLOBAL
  21. #define reg_t        register
  22.  
  23. #define PI1        0        /* type of file (.pi1, etc) */
  24. #define PI2        1        /* also res in file */
  25. #define PI3        2
  26.  
  27.  
  28. /*
  29.  *    globals for GIF decode procedure (only needed in this file,
  30.  *    should actually be static)
  31.  */
  32. LOCAL int    _Palette[16];    /* pi1/pi2 color palette */
  33. LOCAL uchar_t    _Quant[8] = {0,36,72,108,144,180,216,255};
  34.                 /* for quantizing 8 levels (3 bits) to 256 */
  35.  
  36.  
  37. /*
  38.  *    local functions
  39.  */
  40. LOCAL void    _PI1toIndx ();
  41. LOCAL void    _PI2toIndx ();
  42.  
  43.  
  44. /*------------------------------*/
  45. /*    ReadPI1            */
  46. /*------------------------------*/
  47. GLOBAL int ReadPI1 (fname, raster, colormap, opt)
  48. char           *fname;        /* file name with .GIF */
  49. uchar_t           *raster;        /* place to put raster */
  50. uchar_t        colormap[][3];    /* color map */
  51. int        opt;        /* SILENT, INQUIRE, VERBOSE, NORMAL */
  52. {
  53.  
  54. /*
  55.  *    read a pi1 image from open file infile into raster array.
  56.  *    the array will contain index into color table. caller supplies
  57.  *    all needed space (raster array, color map, etc). colormap
  58.  *    will contain either global or local color map, depending on image.
  59.  *
  60.  *    The following illustrates the general file layout of .PI1 files:
  61.  *
  62.  *        1    word    resolution
  63.  *        16    word    palette
  64.  *        16000    word    screen memory
  65.  */
  66.  
  67.     static int    firsttime = 1;
  68.  
  69.     FILE           *in;
  70.     int        infile;
  71.     reg_t long    ii;
  72.     int        i;
  73.     int        flag;
  74.     int        nread;
  75.     int        res;
  76.     int        line[80];
  77.     int        c;
  78.     int        q;
  79.  
  80.  
  81.  
  82.  
  83.     /*
  84.      *   open file...
  85.      */
  86.     if (opt != SILENT)
  87.     {
  88.         printf ("   \n");
  89.         printf ("\nEnter ReadPI1\n\n");
  90.         printf ("Open file:                           %s\n", fname);
  91.     }
  92.     if ((in = fopenb (fname, "r")) == (FILE *) 0)
  93.     {
  94.         return (EGIFFILE);
  95.     }
  96.  
  97.  
  98.  
  99.     /*
  100.      *   get ready for next image by clearing variables, tables
  101.      */
  102.     if (firsttime)
  103.         firsttime = 0;
  104.     else
  105.     {
  106.         /* these take a while to do */
  107.         if (opt != SILENT)
  108.             printf ("\nResetting tables\n");
  109.  
  110.         for (ii = 0; ii < MAXIMG; ii++)
  111.             raster[ii] = 0;
  112.     }
  113.  
  114.  
  115.  
  116.     /*
  117.      *   make sure it is a pi1 file
  118.      */
  119.     if (opt != SILENT)
  120.         printf ("\nRead resolution\n");
  121.     nread = fread (&res, 2, 1, in);
  122.     if (nread != 1)
  123.     {
  124.         fclose (in);
  125.         return (EGIFEOF);
  126.     }
  127.     if (res != PI1)
  128.     {
  129.         fclose (in);
  130.         return (EGIFMAGIC);
  131.     }
  132.     if (opt != SILENT)
  133.         printf ("     res = %d\n", res);
  134.  
  135.  
  136.  
  137.     /*
  138.      *   read palette...
  139.      */
  140.     if (opt != SILENT)
  141.         printf ("\nRead palette\n");
  142.     nread = fread (_Palette, 2, 16, in);
  143.     if (nread != 16)
  144.     {
  145.         fclose (in);
  146.         return (EGIFEOF);
  147.     }
  148.     if (opt != SILENT)
  149.     {
  150.         printf ("     %04x %04x %04x %04x %04x %04x %04x %04x\n",
  151.             _Palette[0], _Palette[1], _Palette[2], _Palette[3],
  152.             _Palette[4], _Palette[5], _Palette[6], _Palette[7]);
  153.         printf ("     %04x %04x %04x %04x %04x %04x %04x %04x\n",
  154.             _Palette[8], _Palette[9], _Palette[10], _Palette[11],
  155.             _Palette[12], _Palette[13], _Palette[14], _Palette[15]);
  156.     }
  157.  
  158.  
  159.  
  160.     /*
  161.      *   set up colormap from palette...
  162.      */
  163.     for (i = 0; i < 16; i++)
  164.     {
  165.         /*
  166.          *   the mask for the palette is 0x0777. we quantize based
  167.          *   on color table with 256 possible values.
  168.          */
  169.         c              = _Palette[i];
  170.         q              = (int) ((c & 0x0700) >> 8);
  171.         colormap[i][0] = (uchar_t) _Quant[q];
  172.         q              = (int) ((c & 0x0070) >> 4);
  173.         colormap[i][1] = (uchar_t) _Quant[q];
  174.         q              = (int)  (c & 0x0007);
  175.         colormap[i][2] = (uchar_t) _Quant[q];
  176.     }
  177.  
  178.  
  179.  
  180.     /*
  181.      *   read screen...
  182.      */
  183.     if (opt != SILENT)
  184.         printf ("\nRead screen\n");
  185.     for (i = 0; i < 200; i++)
  186.     {
  187.         nread = fread (line, 2, 80, in);
  188.         if (nread != 80)
  189.         {
  190.             fclose (in);
  191.             return (EGIFEOF);
  192.         }
  193.         _PI1toIndx (line, raster);
  194.  
  195.         raster = (uchar_t *) ((long) raster + 320L);
  196.     }
  197.     fclose (in);
  198.  
  199.     /* at this point, Raster[] contains colormap indices at each pixel
  200.        and ColMap contains RGB colormap requantized to 256 levels. only
  201.        the first 16 elements of ColMap are used */
  202.  
  203.     return (EGIFOK);
  204. }
  205.  
  206.  
  207.  
  208.  
  209. /*------------------------------*/
  210. /*    _PI1toIndx        */
  211. /*------------------------------*/
  212. LOCAL void _PI1toIndx (pscrn, pras)
  213. int           *pscrn;
  214. uchar_t           *pras;
  215. {
  216.  
  217. /*
  218.  *    decompose gem bitmap row into colormap index. here we just get
  219.  *    each pixel's color index and stuff it into the raster. the color
  220.  *    map is done elsewhere.
  221.  *
  222.  *    it works like this:
  223.  *
  224.  *               word, 16 bits
  225.  *            |    .     |
  226.  *            |    .     |
  227.  *    plane 0        |0110010110001110|
  228.  *    plane 1        |0100110111110000|
  229.  *    plane 2        |0111110100000001|
  230.  *    plane 3        |0000000110111000|
  231.  *    plane 0        |     ^    .     |
  232.  *            |     |    .     |    color index:
  233.  *                  \-------------- 0 1 1 1 = 7
  234.  *                            |
  235.  *                            v  111111
  236.  *                    palette: 0123456789012345
  237.  *                            |
  238.  *                            v
  239.  *                            0 7 3 1
  240.  *                              ^ ^ ^
  241.  *                              | | |
  242.  *                            red---/ | \---blue
  243.  *                                |
  244.  *                              green
  245.  *
  246.  *    the mask for the palette is 0x0777. low rez can use all 16 palette
  247.  *    entries. we only need the color index. the example above shows
  248.  *    further how to decompose that to rgb intensities.
  249.  */
  250.  
  251.     register int    shift;
  252.     register int    indx;
  253.     register int    colindx;
  254.     register int    ix;
  255.  
  256.     int        pl_0,        /* 4 planes in low rez... */
  257.             pl_1,
  258.             pl_2,
  259.             pl_3;
  260.  
  261.  
  262.     for (ix = 0; ix < 320; ix++)
  263.     {
  264.         /*
  265.          *   indx is byte, shift gets proper bit. pl_x are
  266.          *   each of the four planes of a gem image, and are
  267.          *   interleaved. given the pixel value for each plane,
  268.          *   the result maps to a color index which in turn
  269.          *   indexes into the palette for the color. the color
  270.          *   in the palette gives RGB values for the pixel.
  271.          */
  272. /*        indx      = (ix / 16) * 4;*/
  273.         indx      = (ix >> 4) << 2;
  274.         shift     = 15 - (ix % 16);
  275.  
  276.         pl_0      = ((pscrn[indx  ] >> shift) & 1);
  277.         pl_1      = ((pscrn[indx+1] >> shift) & 1);
  278.         pl_2      = ((pscrn[indx+2] >> shift) & 1);
  279.         pl_3      = ((pscrn[indx+3] >> shift) & 1);
  280.  
  281.         colindx   = (pl_3 << 3) | (pl_2 << 2) | (pl_1 << 1) | (pl_0);
  282.  
  283.         pras[ix] = (uchar_t) colindx;
  284.     }
  285. }
  286.  
  287.  
  288.  
  289.  
  290. /*------------------------------*/
  291. /*    ReadPI2            */
  292. /*------------------------------*/
  293. GLOBAL int ReadPI2 (fname, raster, colormap, opt)
  294. char           *fname;        /* file name with .GIF */
  295. uchar_t           *raster;        /* place to put raster */
  296. uchar_t        colormap[][3];    /* color map */
  297. int        opt;        /* SILENT, INQUIRE, VERBOSE, NORMAL */
  298. {
  299.  
  300. /*
  301.  *    read a pi2 image from open file infile into raster array.
  302.  *    the array will contain index into color table. caller supplies
  303.  *    all needed space (raster array, color map, etc). colormap
  304.  *    will contain either global or local color map, depending on image.
  305.  *
  306.  *    The following illustrates the general file layout of .PI2 files:
  307.  *
  308.  *        1    word    resolution
  309.  *        16    word    palette
  310.  *        16000    word    screen memory
  311.  */
  312.  
  313.     static int    firsttime = 1;
  314.  
  315.     FILE           *in;
  316.     int        infile;
  317.     reg_t long    ii;
  318.     int        i;
  319.     int        flag;
  320.     int        nread;
  321.     int        res;
  322.     int        line[80];
  323.     int        c;
  324.     int        q;
  325.  
  326.  
  327.  
  328.  
  329.     /*
  330.      *   open file...
  331.      */
  332.     if (opt != SILENT)
  333.     {
  334.         printf ("   \n");
  335.         printf ("\nEnter ReadPI2\n\n");
  336.         printf ("Open file:                           %s\n", fname);
  337.     }
  338.     if ((in = fopenb (fname, "r")) == (FILE *) 0)
  339.     {
  340.         return (EGIFFILE);
  341.     }
  342.  
  343.  
  344.  
  345.     /*
  346.      *   get ready for next image by clearing variables, tables
  347.      */
  348.     if (firsttime)
  349.         firsttime = 0;
  350.     else
  351.     {
  352.         /* these take a while to do */
  353.         if (opt != SILENT)
  354.             printf ("\nResetting tables\n");
  355.  
  356.         for (ii = 0; ii < MAXIMG; ii++)
  357.             raster[ii] = 0;
  358.     }
  359.  
  360.  
  361.  
  362.     /*
  363.      *   make sure it is a pi1 file
  364.      */
  365.     if (opt != SILENT)
  366.         printf ("\nRead resolution\n");
  367.     nread = fread (&res, 2, 1, in);
  368.     if (nread != 1)
  369.     {
  370.         fclose (in);
  371.         return (EGIFEOF);
  372.     }
  373.     if (res != PI2)
  374.     {
  375.         fclose (in);
  376.         return (EGIFMAGIC);
  377.     }
  378.     if (opt != SILENT)
  379.         printf ("     res = %d\n", res);
  380.  
  381.  
  382.  
  383.     /*
  384.      *   read palette...
  385.      */
  386.     if (opt != SILENT)
  387.         printf ("\nRead palette\n");
  388.     nread = fread (_Palette, 2, 16, in);
  389.     if (nread != 16)
  390.     {
  391.         fclose (in);
  392.         return (EGIFEOF);
  393.     }
  394.     if (opt != SILENT)
  395.     {
  396.         printf ("     %04x %04x %04x %04x %04x %04x %04x %04x\n",
  397.             _Palette[0], _Palette[1], _Palette[2], _Palette[3],
  398.             _Palette[4], _Palette[5], _Palette[6], _Palette[7]);
  399.         printf ("     %04x %04x %04x %04x %04x %04x %04x %04x\n",
  400.             _Palette[8], _Palette[9], _Palette[10], _Palette[11],
  401.             _Palette[12], _Palette[13], _Palette[14], _Palette[15]);
  402.     }
  403.  
  404.  
  405.  
  406.     /*
  407.      *   set up colormap from palette...
  408.      */
  409.     for (i = 0; i < 4; i++)
  410.     {
  411.         /*
  412.          *   the mask for the palette is 0x0777. we quantize based
  413.          *   on color table with 256 possible values.
  414.          */
  415.         c              = _Palette[i];
  416.         q              = (int) ((c & 0x0700) >> 8);
  417.         colormap[i][0] = (uchar_t) _Quant[q];
  418.         q              = (int) ((c & 0x0070) >> 4);
  419.         colormap[i][1] = (uchar_t) _Quant[q];
  420.         q              = (int)  (c & 0x0007);
  421.         colormap[i][2] = (uchar_t) _Quant[q];
  422.     }
  423.  
  424.  
  425.  
  426.     /*
  427.      *   read screen...
  428.      */
  429.     if (opt != SILENT)
  430.         printf ("\nRead screen\n");
  431.     for (i = 0; i < 200; i++)
  432.     {
  433.         nread = fread (line, 2, 80, in);
  434.         if (nread != 80)
  435.         {
  436.             fclose (in);
  437.             return (EGIFEOF);
  438.         }
  439.         _PI2toIndx (line, raster);
  440.  
  441.         raster = (uchar_t *) ((long) raster + 640L);
  442.     }
  443.     fclose (in);
  444.  
  445.     /* at this point, Raster[] contains colormap indices at each pixel
  446.        and ColMap contains RGB colormap requantized to 256 levels. only
  447.        the first 16 elements of ColMap are used */
  448.  
  449.     return (EGIFOK);
  450. }
  451.  
  452.  
  453.  
  454.  
  455. /*------------------------------*/
  456. /*    _PI2toIndx        */
  457. /*------------------------------*/
  458. LOCAL void _PI2toIndx (pscrn, pras)
  459. int           *pscrn;
  460. uchar_t           *pras;
  461. {
  462.  
  463. /*
  464.  *    decompose gem bitmap row into colormap index. here we just get
  465.  *    each pixel's color index and stuff it into the raster. the color
  466.  *    map is done elsewhere.
  467.  *
  468.  *    it works like this:
  469.  *
  470.  *               word, 16 bits
  471.  *            |    .     |
  472.  *            |    .     |
  473.  *    plane 0        |0110000110001110|
  474.  *    plane 1        |0100110111110000|
  475.  *    plane 0        |     ^    .     |
  476.  *            |     |    .     |    color index:
  477.  *                  \-------------- 1 0 = 2
  478.  *                            |
  479.  *                            v       111111
  480.  *                    palette:  0123456789012345
  481.  *                            |
  482.  *                            v
  483.  *                            0 7 3 1
  484.  *                              ^ ^ ^
  485.  *                              | | |
  486.  *                        red---/ | \---blue
  487.  *                                |
  488.  *
  489.  *    the mask for the palette is 0x0777. med rez can use 4 palette
  490.  *    entries. we only need the color index. the example above shows
  491.  *    further how to decompose that to rgb intensities.
  492.  */
  493.  
  494.     register int    shift;
  495.     register int    indx;
  496.     register int    colindx;
  497.     register int    ix;
  498.  
  499.     int        pl_0,        /* 2 planes in med rez... */
  500.             pl_1;
  501.  
  502.  
  503.     for (ix = 0; ix < 640; ix++)
  504.     {
  505.         /*
  506.          *   indx is byte, shift gets proper bit. pl_x are
  507.          *   each of the four planes of a gem image, and are
  508.          *   interleaved. given the pixel value for each plane,
  509.          *   the result maps to a color index which in turn
  510.          *   indexes into the palette for the color. the color
  511.          *   in the palette gives RGB values for the pixel.
  512.          */
  513. /*        indx      = (ix / 16) * 2;*/
  514.         indx      = (ix >> 4) << 1;
  515.         shift     = 15 - (ix % 16);
  516.  
  517.         pl_0      = ((pscrn[indx  ] >> shift) & 1);
  518.         pl_1      = ((pscrn[indx+1] >> shift) & 1);
  519.  
  520.         colindx   = (pl_1 << 1) | (pl_0);
  521.  
  522.         pras[ix] = (uchar_t) colindx;
  523.     }
  524. }
  525.  
  526.  
  527.  
  528.  
  529.  
  530.