home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / gbmsrc.zip / gbmgem.c < prev    next >
C/C++ Source or Header  |  1998-06-21  |  27KB  |  1,095 lines

  1. /*
  2.  
  3. gbmgem.c - GEM Raster support
  4.  
  5. Reads 1 bpp files and returns 1 bpp data.
  6. Reads 2,3 and 4 bpp files and returns 4 bpp data.
  7. Reads 5,6,7 and 8 bpp files and returns 8 bpp data.
  8. Reads 24 bpp XIMG files and returns 24 bpp data.
  9. Writes 1 bpp data as B/W 1 bpp file.
  10. Writes 4 bpp data mapped to 'standard' 16 colour palette.
  11. Or writes 4 bpp data greyscale 4 bpp file, if output option "grey" given.
  12. Or writes 4 bpp data to XIMG file with palette if "pal" option given.
  13. Writes 8 bpp data as greyscale 8 bpp file.
  14. Or writes 8 bpp to XIMG file with palette if "pal" option given.
  15. Writes 24 bpp data as an XIMG file.
  16. The pixel dimensions in microns can be specified via "pixw=#,pixh=#".
  17.  
  18. Reference material used :-
  19.     p300 of EoGFF book.
  20.         Documents 1 bpp, 4 bpp and 8 bpp formats.
  21.         No mention of XIMG extension.
  22.         Suspected inaccurate - 8 word 4 bpp files are *colour*.
  23.         Its supplied sample FLAG_B24.IMG on CD-ROM proves the point.
  24.     Revision 17 of Image Alchemy.
  25.         Suspected wrong also - reorders palette writing 4 bpp IMG.
  26.         EoGFF sample and other samples all agree, Alchemy odd-one-out.
  27.     Public Domain source and other sample files.
  28.         Introduces notion of XIMG header extension block.
  29.         XIMGs can include palette entries (exact layout guessed).
  30.         XIMGs introduces notion of 24 bpp files.
  31.  
  32. */
  33.  
  34. /*...sincludes:0:*/
  35. #include <stdio.h>
  36. #include <ctype.h>
  37. #include <stddef.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <memory.h>
  41. #include <malloc.h>
  42. #include "gbm.h"
  43. #include "gbmhelp.h"
  44.  
  45. /*...vgbm\46\h:0:*/
  46. /*...vgbmhelp\46\h:0:*/
  47.  
  48. #ifndef min
  49. #define    min(a,b)    (((a)<(b))?(a):(b))
  50. #endif
  51. /*...e*/
  52.  
  53. /*...suseful:0:*/
  54. static word getword(byte *p) { return (p[0]<<8)|p[1]; }
  55. static void putword(byte *p, word w) { p[0] = (byte) (w>>8); p[1] = (byte) w; }
  56. /*...e*/
  57.  
  58. static GBMFT gem_gbmft =
  59.     {
  60.     "GemRas",
  61.     "GEM Raster",
  62.     "IMG",
  63.     GBM_FT_R1|GBM_FT_R4|GBM_FT_R8|GBM_FT_R24|
  64.     GBM_FT_W1|GBM_FT_W4|GBM_FT_W8|GBM_FT_W24,
  65.     };
  66.  
  67. /*...serror codes:0:*/
  68. #define    GBM_ERR_GEM_IS_DVI    ((GBM_ERR) 1700)
  69. #define    GBM_ERR_GEM_IS_IMDS    ((GBM_ERR) 1701)
  70. #define    GBM_ERR_GEM_BAD_VERSION    ((GBM_ERR) 1702)
  71. #define    GBM_ERR_GEM_BAD_HDRLEN    ((GBM_ERR) 1703)
  72. #define    GBM_ERR_GEM_BAD_PLANES    ((GBM_ERR) 1704)
  73. #define    GBM_ERR_GEM_BAD_FLAG    ((GBM_ERR) 1705)
  74. #define    GBM_ERR_GEM_FIXEDCPAL    ((GBM_ERR) 1706)
  75. #define    GBM_ERR_GEM_XIMG_TYPE    ((GBM_ERR) 1707)
  76. /*...e*/
  77.  
  78. typedef struct
  79.     {
  80.     word lenhdr, planes, patlen, flag;
  81.     BOOLEAN ximg;
  82.     } GEM_PRIV;
  83.  
  84. /*...srgb_bgr:0:*/
  85. static void rgb_bgr(const byte *p, byte *q, int n)
  86.     {
  87.     while ( n-- )
  88.         {
  89.         byte r = *p++;
  90.         byte g = *p++;
  91.         byte b = *p++;
  92.  
  93.         *q++ = b;
  94.         *q++ = g;
  95.         *q++ = r;
  96.         }
  97.     }
  98. /*...e*/
  99.  
  100. /*...sgem_qft:0:*/
  101. GBM_ERR gem_qft(GBMFT *gbmft)
  102.     {
  103.     *gbmft = gem_gbmft;
  104.     return GBM_ERR_OK;
  105.     }
  106. /*...e*/
  107. /*...sgem_rhdr:0:*/
  108. /* GEM-Raster files, The green plane of an ActionMedia DVI still, and IMDS
  109.    image files all share the common .IMG file extension. GBM only supports
  110.    GEM-Raster files, but to be nice, I'll give a nice diagnostic if I detect
  111.    one of the others.
  112.  
  113.    GEM-Rasters can be :-
  114.       8 word header, data
  115.          planes==1 => b/w image
  116.          planes==3 => fixed 8 colour palette (guesswork)
  117.          planes==4 => fixed 16 colour palette
  118.       9 word header including flag, data
  119.          flag==0 => treat as 8 word header
  120.          flag==1 => treat as greyscale
  121.       other size header, with XIMG signiture and 3<<planes words palette, data
  122.       other size header, with XIMG signiture and planes==24, data
  123. */
  124.  
  125. static byte imds_begin_image_segment[] = { 0x70, 0x04, 0x00, 0x00, 0x00, 0x00 };
  126. static byte imds_modca_wrapping     [] = { 0x08, 0x00, 0x05, 0x00, 0x01, 0x00, 0x00, 0x00 };
  127.  
  128. GBM_ERR gem_rhdr(const char *fn, int fd, GBM *gbm, const char *opt)
  129.     {
  130.     GEM_PRIV *priv = (GEM_PRIV *) gbm->priv;
  131.     byte hdr[22];
  132.  
  133.     fn=fn; opt=opt; /* Suppress 'unref arg' compiler warnings */
  134.  
  135.     if ( gbm_file_read(fd, hdr, 22) != 22 )
  136.         return GBM_ERR_READ;
  137.  
  138.     if ( !memcmp(hdr, "VDVI", 4) )
  139.         return GBM_ERR_GEM_IS_DVI;
  140.  
  141.     if ( !memcmp(hdr, imds_begin_image_segment, sizeof(imds_begin_image_segment)) ||
  142.          !memcmp(hdr, imds_modca_wrapping, sizeof(imds_modca_wrapping)) )
  143.         return GBM_ERR_GEM_IS_IMDS;
  144.  
  145.     if ( getword(hdr) != 1 )
  146.         return GBM_ERR_GEM_BAD_VERSION;
  147.  
  148.     priv->lenhdr = getword(hdr+1*2);
  149.     if ( priv->lenhdr < 8 )
  150.         return GBM_ERR_GEM_BAD_HDRLEN;
  151.  
  152.     gbm_file_lseek(fd, priv->lenhdr*2, SEEK_SET);
  153.  
  154.     if ( !memcmp(hdr+8*2, "XIMG", 4) )
  155.         /* XIMG signiture has been found */
  156.         {
  157.         priv->ximg = TRUE;
  158.         if ( getword(hdr+10*2) != 0 )
  159.             return GBM_ERR_GEM_XIMG_TYPE;
  160.         }
  161.     else
  162.         priv->ximg = FALSE;
  163.  
  164.     priv->planes = getword(hdr+2*2);
  165.     if ( priv->planes >= 1 && priv->planes <= 8 )
  166.         ; /* All ok */
  167.     else if ( priv->ximg && priv->planes == 24 )
  168.         ; /* Still all ok */
  169.     else
  170.         /* Don't know how to handle this */
  171.         return GBM_ERR_GEM_BAD_PLANES;
  172.  
  173.     priv->patlen = getword(hdr+3*2);
  174.  
  175.     gbm->w = getword(hdr+6*2);
  176.     gbm->h = getword(hdr+7*2);
  177.  
  178.     if ( priv->lenhdr == 9 )
  179.         {
  180.         priv->flag = getword(hdr+8*2);
  181.         if ( priv->flag != 0 && priv->flag != 1 )
  182.             return GBM_ERR_GEM_BAD_FLAG;
  183.         }
  184.  
  185.     switch ( priv->planes )
  186.         {
  187.         case 1:
  188.             gbm->bpp = 1; break;
  189.         case 2: case 3: case 4:
  190.             gbm->bpp = 4; break;
  191.         case 5: case 6: case 7: case 8:
  192.             gbm->bpp = 8; break;
  193.         case 24:
  194.             gbm->bpp = 24; break;
  195.         }
  196.  
  197.     /* Enforce assumed rules */
  198.     if ( priv->lenhdr == 8 ||
  199.          (priv->lenhdr == 9 && priv->flag == 0) )
  200.         /* Is a fixed colour palette */
  201.         if ( priv->planes != 1 &&
  202.              priv->planes != 3 &&
  203.              priv->planes != 4 )
  204.             return GBM_ERR_GEM_FIXEDCPAL;
  205.  
  206.     return GBM_ERR_OK;
  207.     }
  208. /*...e*/
  209. /*...sgem_rpal:0:*/
  210. /* This palettes were determined by looking at .IMG files and their equivelent
  211.    .BMP files (that had been converted elsewhere). */
  212.  
  213. /*...sgbmrgb_3 \45\ 3 plane palette:0:*/
  214. static GBMRGB gbmrgb_3[] =
  215.     {
  216.     /* r, g, b */
  217.     0xff,0xff,0xff,        /* White */
  218.     0xff,0   ,0   ,        /* Red */
  219.     0   ,0xff,0   ,        /* Green */
  220.     0xff,0xff,0   ,        /* (Yellow) Orange */
  221.     0   ,0   ,0xff,        /* Blue */
  222.     0xff,0   ,0xff,        /* Magenta */
  223.     0   ,0xff,0xff,        /* Cyan */
  224.     0   ,0   ,0   ,        /* Black */
  225.     };
  226. /*...e*/
  227. /*...sgbmrgb_4 \45\ 4 plane palette:0:*/
  228. static GBMRGB gbmrgb_4[] =
  229.     {
  230.     /* r, g, b */
  231.     0xff,0xff,0xff,        /* White */
  232.     0xff,0   ,0   ,        /* Red */
  233.     0   ,0xff,0   ,        /* Green */
  234.     0xff,0xff,0   ,        /* (Yellow) Orange */
  235.     0   ,0   ,0xff,        /* Blue */
  236.     0xff,0   ,0xff,        /* Magenta */
  237.     0   ,0xff,0xff,        /* Cyan */
  238.     0xcc,0xcc,0xcc,        /* Grey */
  239.     0x80,0x80,0x80,        /* Dark grey */
  240.     0x80,0   ,0   ,        /* Dark red */
  241.     0   ,0x80,0   ,        /* Dark green */
  242.     0x80,0x80,0   ,        /* Dark yellow */
  243.     0   ,0   ,0x80,        /* Dark blue */
  244.     0x80,0   ,0x80,        /* Dark magenta */
  245.     0   ,0x80,0x80,        /* Dark cyan */
  246.     0   ,0   ,0   ,        /* Black */
  247.     };
  248. /*...e*/
  249.  
  250. GBM_ERR gem_rpal(int fd, GBM *gbm, GBMRGB *gbmrgb)
  251.     {
  252.     GEM_PRIV *priv = (GEM_PRIV *) gbm->priv;
  253.     if ( priv->planes == 24 )
  254.         return GBM_ERR_OK;
  255.     if ( priv->ximg )
  256. /*...shandle the palette in the file:16:*/
  257. {
  258. int i, ncols = (1 << priv->planes);
  259. gbm_file_lseek(fd, (8+3)*2, SEEK_SET);
  260. for ( i = 0; i < ncols; i++ )
  261.     {
  262.     byte palentry[3*2];
  263.     if ( gbm_file_read(fd, palentry, 3*2) != 3*2 )
  264.         return GBM_ERR_READ;
  265.     /* Guesswork, based on sample file intel.img */
  266.     gbmrgb[i].r = (byte) ((getword(palentry  )*255UL)/1000UL);
  267.     gbmrgb[i].g = (byte) ((getword(palentry+2)*255UL)/1000UL);
  268.     gbmrgb[i].b = (byte) ((getword(palentry+4)*255UL)/1000UL);
  269.     }
  270. }
  271. /*...e*/
  272.     else
  273.         switch ( gbm->bpp )
  274.             {
  275. /*...s1 \45\ fixed b\47\w palette:24:*/
  276. case 1:
  277.     gbmrgb[0].r = gbmrgb[0].g = gbmrgb[0].b = 0xff;
  278.     gbmrgb[1].r = gbmrgb[1].g = gbmrgb[1].b = 0x00;
  279.     break;
  280. /*...e*/
  281. /*...s4 \45\ either greyscale or a fixed palette:24:*/
  282. case 4:
  283.     if ( priv->lenhdr == 9 && priv->flag == 1 )
  284.         /* Is greyscale data */
  285.         /* Its guesswork that it goes dark to light */
  286.         {
  287.         int i, cols = (1<<priv->planes);
  288.         for ( i = 0; i < cols; i++ )
  289.             gbmrgb[i].r =
  290.             gbmrgb[i].g =
  291.             gbmrgb[i].b = (byte) ((i*0xff)/(cols-1));
  292.         }
  293.     else
  294.         switch ( priv->planes )
  295.             {
  296.             case 3:
  297.                 memcpy(gbmrgb, gbmrgb_3, sizeof(gbmrgb_3));
  298.                 break;
  299.             case 4:
  300.                 memcpy(gbmrgb, gbmrgb_4, sizeof(gbmrgb_4));
  301.                 break;
  302.             }
  303.     break;
  304. /*...e*/
  305. /*...s8 \45\ greyscale palette:24:*/
  306. case 8:
  307.     /* Again, its guesswork that it goes from dark to light */
  308.     {
  309.     int i;
  310.     for ( i = 0; i < 0x100; i++ )
  311.         gbmrgb[i].r =
  312.         gbmrgb[i].g =
  313.         gbmrgb[i].b = (byte) i;
  314.     }
  315.     break;
  316. /*...e*/
  317.             }
  318.     return GBM_ERR_OK;
  319.     }
  320. /*...e*/
  321. /*...sgem_rdata:0:*/
  322. /*...sread_gem_line:0:*/
  323. /* Vertical replication codes are only allowed in certain places */
  324.  
  325. static void read_gem_line(
  326.     AHEAD *ahead,
  327.     byte *line, int scan,
  328.     int patlen,
  329.     int *vrep, BOOLEAN allow_vrep
  330.     )
  331.     {
  332.     while ( scan )
  333.         {
  334.         byte b1 = (byte) gbm_read_ahead(ahead);
  335.         if ( b1 == 0x80 )
  336.             /* Literal run */
  337.             {
  338.             byte len = (byte) gbm_read_ahead(ahead);
  339.             scan -= len;
  340.             while ( len-- > 0 )
  341.                 *line++ = (byte) gbm_read_ahead(ahead);
  342.             }
  343.         else if ( b1 == 0x00 )
  344.             /* Pattern code */
  345.             {
  346.             byte rep = (byte) gbm_read_ahead(ahead);
  347.             if ( rep == 0 && allow_vrep )
  348.                 /* Is actually a vertical replication */
  349.                 {
  350.                 gbm_read_ahead(ahead); /* Swallow 0xff */
  351.                 *vrep = (int) gbm_read_ahead(ahead) - 1;
  352.                 }
  353.             else
  354.                 {
  355.                 int i;
  356.                 scan -= patlen*rep;
  357.                 for ( i = 0; i < patlen; i++ )
  358.                     *line++ = (byte) gbm_read_ahead(ahead);
  359.                 while ( rep-- > 1 )
  360.                     for ( i = 0; i < patlen; i++, line++ )
  361.                         *line = line[-patlen];
  362.                 }
  363.             }
  364.         else
  365.             /* Is a black/white (=0xff's/0x00's) run code */
  366.             {
  367.             byte store = (byte) ( (signed char) b1 >> 7 );
  368.             b1 &= 0x7f;
  369.             memset(line, store, b1);
  370.             line += b1;
  371.             scan -= b1;
  372.             }
  373.         allow_vrep = FALSE;
  374.         }
  375.     }
  376. /*...e*/
  377. /*...sspread:0:*/
  378. static void spread(byte b, byte bit_to_set, byte *dest)
  379.     {
  380.     if ( b & 0x80 ) dest[0] |= (bit_to_set & 0xf0);
  381.     if ( b & 0x40 ) dest[0] |= (bit_to_set & 0x0f);
  382.     if ( b & 0x20 ) dest[1] |= (bit_to_set & 0xf0);
  383.     if ( b & 0x10 ) dest[1] |= (bit_to_set & 0x0f);
  384.     if ( b & 0x08 ) dest[2] |= (bit_to_set & 0xf0);
  385.     if ( b & 0x04 ) dest[2] |= (bit_to_set & 0x0f);
  386.     if ( b & 0x02 ) dest[3] |= (bit_to_set & 0xf0);
  387.     if ( b & 0x01 ) dest[3] |= (bit_to_set & 0x0f);
  388.     }
  389. /*...e*/
  390.  
  391. GBM_ERR gem_rdata(int fd, GBM *gbm, byte *data)
  392.     {
  393.     GEM_PRIV *priv = (GEM_PRIV *) gbm->priv;
  394.     int scan = (gbm->w+7)/8;
  395.     int stride = ((gbm->w*gbm->bpp + 31) / 32) * 4;
  396.     byte *line;
  397.     AHEAD *ahead;
  398.  
  399.     if ( (ahead = gbm_create_ahead(fd)) == NULL )
  400.         return GBM_ERR_MEM;
  401.  
  402.     if ( (line = malloc((size_t) scan)) == NULL )
  403.         {
  404.         gbm_destroy_ahead(ahead);
  405.         return GBM_ERR_MEM;
  406.         }
  407.  
  408.     switch ( gbm->bpp )
  409.         {
  410. /*...s1:16:*/
  411. case 1:
  412.     {
  413.     int y, vrep = 0;
  414.     data += (gbm->h-1) * stride;
  415.     for ( y = gbm->h - 1; y >= 0; y--, data -= stride )
  416.         if ( vrep )
  417.             { memcpy(data, data+stride, stride); vrep--; }
  418.         else
  419.             read_gem_line(ahead, data, scan, priv->patlen, &vrep, TRUE);
  420.     }
  421.     break;
  422. /*...e*/
  423. /*...s4:16:*/
  424. case 4:
  425.     {
  426.     int y, vrep = 0;
  427.     int bytes = (gbm->w / 8);
  428.     int bits  = (gbm->w & 7);
  429.  
  430.     memset(data, 0, gbm->h * stride);
  431.     data += (gbm->h-1) * stride;
  432.     for ( y = gbm->h - 1; y >= 0; y--, data -= stride )
  433.         if ( vrep )
  434.             { memcpy(data, data+stride, stride); vrep--; }
  435.         else
  436.             {
  437.             int p;
  438.             byte bit;
  439.  
  440.             for ( p = 0, bit = 0x11; p < priv->planes; p++, bit <<= 1 )
  441.                 {
  442.                 int x;
  443.                 byte *dest = data;
  444.                 read_gem_line(ahead, line, scan, priv->patlen, &vrep, p==0);
  445.                 for ( x = 0; x < bytes; x++, dest += 4 )
  446.                     spread(line[x], bit, dest);
  447.                 if ( bits )
  448.                     spread((byte) (line[x] & (0xff00U >> bits)), bit, dest);
  449.                 }
  450.             }
  451.     }
  452.     break;
  453. /*...e*/
  454. /*...s8:16:*/
  455. case 8:
  456.     {
  457.     int y, vrep = 0;
  458.     memset(data, 0, gbm->h*stride);
  459.     data += (gbm->h-1) * stride;
  460.     for ( y = gbm->h - 1; y >= 0; y--, data -= stride )
  461.         if ( vrep )
  462.             { memcpy(data, data+stride, stride); vrep--; }
  463.         else
  464.             {
  465.             int p;
  466.             byte bit;
  467.             for ( p = 0, bit = 0x01; p < priv->planes; p++, bit <<= 1 )
  468.                 {
  469.                 int x;
  470.                 read_gem_line(ahead, line, scan, priv->patlen, &vrep, p==0);
  471.                 for ( x = 0; x < gbm->w; x++ )
  472.                     if ( line[x>>3]&(0x80U>>(x&7)) )
  473.                         data[x] |= bit;
  474.                 }
  475.             }
  476.     }
  477.     break;
  478. /*...e*/
  479. /*...s24:16:*/
  480. case 24:
  481.     /* 24bpp data is strange in that it isn't bit planed at all like
  482.        the others are. This makes decoding quicker, and most 24bpp
  483.        hardware is not bit planar, and this may explain why.
  484.        Of course, my guesswork says that the order is R,G,B and as
  485.        GBM used B,G,R, i'll have to reverse the order. */
  486.     {
  487.     int y, vrep = 0;
  488.     memset(data, 0, gbm->h*stride);
  489.     data += (gbm->h-1) * stride;
  490.     for ( y = gbm->h - 1; y >= 0; y--, data -= stride )
  491.         if ( vrep )
  492.             { memcpy(data, data+stride, stride); vrep--; }
  493.         else
  494.             {
  495.             read_gem_line(ahead, data, scan*24, priv->patlen, &vrep, TRUE);
  496.             rgb_bgr(data, data, gbm->w);
  497.             }
  498.     }
  499.     break;
  500. /*...e*/
  501.         }
  502.  
  503.     free(line);
  504.  
  505.     gbm_destroy_ahead(ahead);
  506.  
  507.     return GBM_ERR_OK;
  508.     }
  509. /*...e*/
  510. /*...sgem_w:0:*/
  511. /*...swrite_pal:0:*/
  512. static BOOLEAN write_pal(int fd, const GBMRGB *gbmrgb, int cnt)
  513.     {
  514.     int i;
  515.     for ( i = 0; i < cnt; i++ )
  516.         {
  517.         byte buf[3*2];
  518.         putword(buf  , (word) ((gbmrgb[i].r*1000UL)/255UL));
  519.         putword(buf+2, (word) ((gbmrgb[i].g*1000UL)/255UL));
  520.         putword(buf+4, (word) ((gbmrgb[i].b*1000UL)/255UL));
  521.         if ( gbm_file_write(fd, buf, 3*2) != 3*2 )
  522.             return FALSE;
  523.         }
  524.     return TRUE;
  525.     }
  526. /*...e*/
  527. /*...smap_to_grey4:0:*/
  528. /* Sum of R,G and B will be from 0 to 0x2fd. Sum/3 is from 0 to 0xff.
  529.    Sum/0x30 is from 0 to 0x0f, which is what we want. */
  530.  
  531. static byte map_to_grey4(const GBMRGB *gbmrgb)
  532.     {
  533.     return (byte) ( ( (word) gbmrgb->r +
  534.               (word) gbmrgb->g +
  535.               (word) gbmrgb->b ) / 0x30 );
  536.     }
  537. /*...e*/
  538. /*...smap_to_col4:0:*/
  539. /* The default 16 colour palette is disgusting to map to quickly.
  540.    Still, never mind, we only expect to be called 512 times. */
  541.  
  542. /*...sdist_between:0:*/
  543. static int dist_between(const GBMRGB *p1, const GBMRGB *p2)
  544.     {
  545.     int dr = (int) ( (unsigned int) p1->r - (unsigned int) p2->r );
  546.     int dg = (int) ( (unsigned int) p1->g - (unsigned int) p2->g );
  547.     int db = (int) ( (unsigned int) p1->b - (unsigned int) p2->b );
  548.     return dr*dr + dg*dg + db*db;
  549.     }
  550. /*...e*/
  551.  
  552. static byte map_to_col4(const GBMRGB *gbmrgb)
  553.     {
  554.     int i, i_min = 0;
  555.     int dist_min = dist_between(gbmrgb, &gbmrgb_4[0]);
  556.     for ( i = 1; i < 0x10; i++ )
  557.         {
  558.         int dist = dist_between(gbmrgb, &gbmrgb_4[i]);
  559.         if ( dist < dist_min )
  560.             {
  561.             dist_min = dist;
  562.             i_min = i;
  563.             }
  564.         }
  565.     return (byte) i_min;
  566.     }
  567. /*...e*/
  568. /*...shandle_vrep:0:*/
  569. static BOOLEAN handle_vrep(int fd, const byte *data, int step, int most, int *dy, int len)
  570.     {
  571.     byte buf[4];
  572.     if ( most > 0xff )
  573.         most = 0xff;
  574.     for ( *dy = 1; *dy < most; (*dy)++, data += step )
  575.         if ( memcmp(data, data+step, len) )
  576.             break;
  577.     if ( (*dy) == 1 )
  578.         return TRUE;
  579.     buf[0] = 0x00;
  580.     buf[1] = 0x00;
  581.     buf[2] = 0xff;
  582.     buf[3] = (byte) (*dy);
  583.     return gbm_file_write(fd, buf, 4) == 4;
  584.     }
  585. /*...e*/
  586. /*...sencode1:0:*/
  587. /* Encode scan bytes of data into temporary buffer enc.
  588.    0x00 repeated N times codes to N       does not expand
  589.    0xff repeated N times codes to 0x80|N  does not expand
  590.    X repeated N times codes to 0x00 N X   expands if N<3
  591.    other N bytes codes to 0x80 N bytes    N=1 expands to 3  ie: 3:1
  592.                                           N=2 expands to 4  ie: 2:1
  593.  
  594.    len(enc) = len(data)*4/2 + 2 should do it. Worst case typically like :-
  595.    
  596.    A B            0xff  C D            0xff  F
  597.    0x80 0x02 A B  0x81  0x80 0x02 C D  0x81  0x80 0x01 F    */
  598.  
  599. /*...sfind_run1:0:*/
  600. /* Look for a run of the same byte */
  601.  
  602. static byte find_run1(const byte *data, int len)
  603.     {
  604.     const byte *p = data;
  605.     byte b = *p++;
  606.     if ( len > 0x7f )
  607.         len = 0x7f;
  608.     while ( --len > 0 && *p == b )
  609.         p++;
  610.     return (byte) ( p-data );
  611.     }
  612. /*...e*/
  613. /*...sfind_lit1:0:*/
  614. /* Handle literal runs.
  615.    We know the first 3 bytes (if there are as many as 3) differ.
  616.    We can only return a number as high as 0x7f from this function. */
  617.  
  618. static byte find_lit1(const byte *data, int len)
  619.     {
  620.     const byte *p;
  621.  
  622.     if ( len <= 3 )
  623.         return len;
  624.     if ( len > 0x7f )
  625.         len = 0x7f;
  626.  
  627.     p = data + 2;
  628.     while ( len-- > 3 && p[0] != p[1] && p[1] != 0x00 && p[1] != 0xff )
  629.         p++;
  630.  
  631.     return p-data;    
  632.     }
  633. /*...e*/
  634.  
  635. static int encode1(const byte *data, int scan, byte *enc)
  636.     {
  637.     byte *enc_start = enc;
  638.     while ( scan )
  639.         {
  640.         byte len = find_run1(data, scan);
  641.         if ( *data == 0x00 || *data == 0xff )
  642.             *enc++ = ((*data&0x80)|len);
  643.         else if ( len >= 3 )
  644.             {
  645.             *enc++ = 0x00;    /* Pattern */
  646.             *enc++ = len;    /* Repeated len times */
  647.             *enc++ = *data;    /* 1-byte pattern (patlen=1) */
  648.             }
  649.         else
  650.             {
  651.             len = find_lit1(data, scan);
  652.             *enc++ = 0x80;    /* Literal run */
  653.             *enc++ = len;
  654.             memcpy(enc, data, len);
  655.             enc += len;
  656.             }
  657.         data += len;
  658.         scan -= len;
  659.         }        
  660.     return enc-enc_start;
  661.     }
  662. /*...e*/
  663. /*...sencode3:0:*/
  664. /* Like encode1, except that patlen=3, so we try to find 3-byte-patterns.
  665.    In this code we always work in 3 byte aligned chunks each step.
  666.    This is not quite optimal, but nothing worth worrying too much about.
  667.    len(enc) = len(data)*5/3 + 2 should do it. Worst case is like :-
  668.  
  669.    A B C            0xff 0xff 0xff  D E F
  670.    0x80 0x03 A B C  0x83            0x80 0x03 D E F     */
  671.  
  672. /*...sfind_run3:0:*/
  673. /* Look for a run of the same 3-byte-pattern.
  674.    We know that data[0..2] are not all 0x00's or 0xff's on entry.
  675.    Takes length in 3-byte-units and returns length in 3-byte-units */
  676.  
  677. static byte find_run3(const byte *data, int len)
  678.     {
  679.     const byte *p = data;
  680.     byte b0, b1, b2;
  681.     if ( len <= 1 )
  682.         return len;
  683.     if ( len > 0x7f )
  684.         len = 0x7f;
  685.     b0 = *p++; b1 = *p++; b2 = *p++;
  686.     while ( --len > 0 && p[0] == b0 && p[1] == b1 && p[2] == b2 )
  687.         p += 3;
  688.     return (byte) ( (p-data)/3 );
  689.     }
  690. /*...e*/
  691. /*...sfind_lit3:0:*/
  692. /* On entry data[0..2] differs from data[3..5], assuming len>=2.
  693.    Takes length in 3 byte units, returns length in bytes. */
  694.  
  695. static byte find_lit3(const byte *data, int len)
  696.     {
  697.     const byte *p;
  698.  
  699.     if ( len == 1 )
  700.         return 3;
  701.     if ( len > 0x7f/3 )
  702.         len = 0x7f/3;
  703.  
  704.     p = data + 6;
  705.     while ( --len >= 2               &&        /* Another 3 */
  706.         (p[0]!=p[-3]||
  707.          p[1]!=p[-2]||
  708.          p[2]!=p[-1])            &&        /* They differ */
  709.         (p[0]|p[1]|p[2]) != 0x00 &&        /* Next not all 0x00's */
  710.         (p[0]&p[1]&p[2]) != 0xff )        /* And not all 0xff's */
  711.         p += 3;
  712.     return (byte) ( p-data );
  713.     }
  714. /*...e*/
  715.  
  716. static int encode3(const byte *data, int scan, byte *enc)
  717.     {
  718.     byte *enc_start = enc;
  719.     while ( scan )
  720.         {
  721.         byte len;
  722.         if ( (*data == 0x00 || *data == 0xff) &&
  723.              (len = find_run1(data, scan)) >= 3 )
  724.             {
  725.             len = ((len/3)*3);
  726.             *enc++ = ((*data&0x80)|len);
  727.             data += len;
  728.             scan -= len;
  729.             }
  730.         else if ( (len = find_run3(data, scan/3)) >= 2 )
  731.             {
  732.             *enc++ = 0x00;        /* Pattern */
  733.             *enc++ = len;        /* Repeated len times */
  734.             *enc++ = data[0];    /* 3-byte pattern */
  735.             *enc++ = data[1];
  736.             *enc++ = data[2];
  737.             data += 3*len;
  738.             scan -= 3*len;
  739.             }
  740.         else
  741.             {
  742.             len = find_lit3(data, scan/3);
  743.             *enc++ = 0x80;        /* Literal run */
  744.             *enc++ = len;        /* # bytes */
  745.             memcpy(enc, data, len);
  746.             enc  += len;
  747.             data += len;
  748.             scan -= len;
  749.             }
  750.         }
  751.     return enc-enc_start;
  752.     }
  753. /*...e*/
  754. /*...sliterally:0:*/
  755. /* The normal encode routines may result in expanding the data.
  756.    This routine checks that, and if so, re-encodes it literally.
  757.    A literal encoding only adds 2 bytes in 127. */
  758.  
  759. static int literally(const byte *data, int scan, byte *enc, int len_enc)
  760.     {
  761.     byte *enc_start = enc;
  762.     if ( len_enc <= (scan*(0x7f+2))/0x7f )
  763.         return len_enc;
  764.     while ( scan )
  765.         {
  766.         int len = min(scan, 0x7f);
  767.         *enc++ = 0x80;
  768.         *enc++ = (byte) len;
  769.         memcpy(enc, data, len);
  770.         enc  += len;
  771.         data += len;
  772.         scan -= len;
  773.         }
  774.     return enc-enc_start;
  775.     }
  776. /*...e*/
  777.  
  778. GBM_ERR gem_w(const char *fn, int fd, const GBM *gbm, const GBMRGB *gbmrgb, const byte *data, const char *opt)
  779.     {
  780.     byte hdr[22];
  781.     int pixw = 85, pixh = 85;
  782.     BOOLEAN pal, grey;
  783.     int stride = ((gbm->w*gbm->bpp + 31) / 32) * 4;
  784.     int scan = (gbm->w+7)/8;
  785.  
  786. /*...shandle options:8:*/
  787. {
  788. const char *s;
  789.  
  790. fn=fn; /* Suppress 'unref arg' compiler warnings */
  791.  
  792. pal  = ( gbm_find_word(opt, "pal") != NULL );
  793. grey = ( gbm_find_word(opt, "grey") != NULL );
  794.  
  795. if ( (s = gbm_find_word_prefix(opt, "pixw=")) != NULL )
  796.     sscanf(s + 5, "%d", &pixw);
  797.  
  798. if ( (s = gbm_find_word_prefix(opt, "pixh=")) != NULL )
  799.     sscanf(s + 5, "%d", &pixh);
  800. }
  801. /*...e*/
  802.  
  803.     /* Initial stab at a header */
  804.     putword(hdr    , (word) 1);        /* Version */
  805.     putword(hdr+1*2, (word) 8);        /* Header length */
  806.     putword(hdr+2*2, (word) gbm->bpp);    /* Number of planes */
  807.     putword(hdr+3*2, (word) 1);        /* Pattern length */
  808.     putword(hdr+4*2, (word) pixw);        /* Pixel width in microns */
  809.     putword(hdr+5*2, (word) pixh);        /* Pixel height in microns */
  810.     putword(hdr+6*2, (word) gbm->w);    /* Width in pixels */
  811.     putword(hdr+7*2, (word) gbm->h);    /* Height in pixels */
  812.  
  813.     data += (gbm->h-1) * stride;
  814.  
  815.     switch ( gbm->bpp )
  816.         {
  817. /*...s1  \45\ simple case\44\ just 8 word header then data:16:*/
  818. case 1:
  819.     {
  820.     int y, dy;
  821.     byte *line, *enc;
  822.     BOOLEAN invert;
  823.  
  824.     if ( pal )
  825.         {
  826.         putword(hdr+1*2, (word) (11+6));    /* Header length */
  827.         memcpy(hdr+8*2, "XIMG", 4);        /* Signiture */
  828.         putword(hdr+10*2, 0);            /* RGB */
  829.         if ( gbm_file_write(fd, hdr, 11*2) != 11*2 )
  830.             return GBM_ERR_WRITE;
  831.         if ( !write_pal(fd, gbmrgb, 2) )
  832.             return GBM_ERR_WRITE;
  833.         invert = FALSE;
  834.         }
  835.     else
  836.         {
  837.         if ( gbm_file_write(fd, hdr, 8*2) != 8*2 )
  838.             return GBM_ERR_WRITE;
  839.          invert = ( gbmrgb[0].r+gbmrgb[0].g+gbmrgb[0].b <
  840.                gbmrgb[1].r+gbmrgb[1].g+gbmrgb[1].b );
  841.         }
  842.  
  843.     if ( (line = malloc((size_t) scan)) == NULL )
  844.         return GBM_ERR_MEM;
  845.  
  846.     if ( (enc = malloc((size_t) (scan*2+2))) == NULL )
  847.         { free(line); return GBM_ERR_MEM; }
  848.  
  849.     for ( y = gbm->h; y > 0; y -= dy, data -= dy*stride )
  850.         {
  851.         int len_enc;
  852.         const byte *p;
  853.         if ( !handle_vrep(fd, data, -stride, y, &dy, scan) )
  854.             { free(enc); free(line); return GBM_ERR_WRITE; }
  855.         if ( invert )
  856.             {
  857.             int x;
  858.             for ( x = 0; x < scan; x++ )
  859.                 line[x] = ~data[x];
  860.             p = line;
  861.             }
  862.         else
  863.             p = data;
  864.         len_enc = literally(p, scan, enc, encode1(p, scan, enc));
  865.         if ( gbm_file_write(fd, enc, len_enc) != len_enc )
  866.             { free(enc); free(line); return GBM_ERR_WRITE; }
  867.         }
  868.  
  869.     free(enc);
  870.     free(line);
  871.  
  872.     }
  873.     break;
  874. /*...e*/
  875. /*...s4  \45\ map to colour palette\44\ or to greyscale\44\ or XIMG it:16:*/
  876. case 4:
  877.     {
  878.     int y, dy;
  879.     byte *line, *mapped, *enc;
  880.     byte map[0x100];
  881.  
  882.     if ( pal )
  883.         {
  884.         putword(hdr+1*2, (word) (11+0x30));    /* Header length */
  885.         memcpy(hdr+8*2, "XIMG", 4);        /* Signiture */
  886.         putword(hdr+10*2, 0);            /* RGB */
  887.         if ( gbm_file_write(fd, hdr, 11*2) != 11*2 )
  888.             return GBM_ERR_WRITE;
  889.         if ( !write_pal(fd, gbmrgb, 0x10) )
  890.             return GBM_ERR_WRITE;
  891.         }
  892.     else if ( grey )
  893.         {
  894.         unsigned int i;
  895.         putword(hdr+1*2, (word) 9);        /* Header length */
  896.         putword(hdr+8*2, (word) 1);        /* Greyscale data */
  897.         if ( gbm_file_write(fd, hdr, 9*2) != 9*2 )
  898.             return GBM_ERR_WRITE;
  899.         for ( i = 0; i < 0x100; i++ )
  900.             map[i] = (map_to_grey4(gbmrgb+((i>>4)&15U))<<4) |
  901.                   map_to_grey4(gbmrgb+( i    &15U))     ;
  902.         }
  903.     else
  904.         {
  905.         unsigned int i;
  906.         if ( gbm_file_write(fd, hdr, 8*2) != 8*2 )
  907.             return GBM_ERR_WRITE;
  908.         for ( i = 0; i < 0x100; i++ )
  909.             map[i] = (map_to_col4(gbmrgb+((i>>4)&15U))<<4) |
  910.                   map_to_col4(gbmrgb+( i    &15U))     ;
  911.         }
  912.  
  913.     if ( (mapped = malloc((size_t) ((gbm->w+1)/2))) == NULL )
  914.         return GBM_ERR_MEM;
  915.  
  916.     if ( (line = malloc((size_t) scan)) == NULL )
  917.         { free(mapped); return GBM_ERR_MEM; }
  918.  
  919.     if ( (enc = malloc((size_t) (scan*2+2))) == NULL )
  920.         { free(line); free(mapped); return GBM_ERR_MEM; }
  921.  
  922.     for ( y = gbm->h; y > 0; y -= dy, data -= dy*stride )
  923.         {
  924.         int x, len_enc, plane;
  925.         const byte *p;
  926.         byte bit, mask;
  927.         if ( !handle_vrep(fd, data, -stride, y, &dy, gbm->w) )
  928.             { free(enc); free(line); free(mapped); return GBM_ERR_WRITE; }
  929.         if ( pal )
  930.             p = data;
  931.         else
  932.             {
  933.             for ( x = 0; x < (gbm->w+1)/2; x++ )
  934.                 mapped[x] = map[data[x]];
  935.             p = mapped;
  936.             }
  937.         for ( plane = 0, bit = 0x11; plane < 4; plane++, bit <<= 1 )
  938.             {
  939.             memset(line, 0, scan);
  940.             for ( x = 0, mask = 0xf0; x < gbm->w; x++, mask ^= 0xff )
  941.                 if ( p[(unsigned)x>>1]&(bit&mask) )
  942.                     line[(unsigned)x>>3] |= (0x80>>((unsigned)x&7));
  943.             len_enc = literally(line, scan, enc, encode1(line, scan, enc));
  944.             if ( gbm_file_write(fd, enc, len_enc) != len_enc )
  945.                 { free(enc); free(line); free(mapped); return GBM_ERR_MEM; }
  946.             }
  947.         }
  948.  
  949.     free(enc);
  950.     free(line);
  951.     free(mapped);
  952.  
  953.     }
  954.     break;
  955. /*...e*/
  956. /*...s8  \45\ map to greyscale\44\ or XIMG it:16:*/
  957. case 8:
  958.     {
  959.     int y, dy;
  960.     byte *line, *mapped, *enc;
  961.     byte map[0x100];
  962.  
  963.     if ( pal )
  964.         {
  965.         putword(hdr+1*2, (word) (11+0x300));    /* Header length */
  966.         memcpy(hdr+8*2, "XIMG", 4);        /* Signiture */
  967.         putword(hdr+10*2, 0);            /* RGB */
  968.         if ( gbm_file_write(fd, hdr, 11*2) != 11*2 )
  969.             return GBM_ERR_WRITE;
  970.         if ( !write_pal(fd, gbmrgb, 0x100) )
  971.             return GBM_ERR_WRITE;
  972.         }
  973.     else
  974.         {
  975.         int i;
  976.         putword(hdr+1*2, (word) 9);        /* Header length */
  977.         putword(hdr+8*2, (word) 1);        /* Greyscale data */
  978.         if ( gbm_file_write(fd, hdr, 9*2) != 9*2 )
  979.             return GBM_ERR_WRITE;
  980.         for ( i = 0; i < 0x100; i++ )
  981.             map[i] = (byte) ( ( (word) gbmrgb[i].r+
  982.                         (word) gbmrgb[i].g+
  983.                         (word) gbmrgb[i].b) / 3 );
  984.         }
  985.  
  986.     if ( (mapped = malloc((size_t) gbm->w)) == NULL )
  987.         return GBM_ERR_MEM;
  988.  
  989.     if ( (line = malloc((size_t) scan)) == NULL )
  990.         { free(mapped); return GBM_ERR_MEM; }
  991.  
  992.     if ( (enc = malloc((size_t) (scan*2+2))) == NULL )
  993.         { free(line); free(mapped); return GBM_ERR_MEM; }
  994.  
  995.     for ( y = gbm->h; y > 0; y -= dy, data -= dy*stride )
  996.         {
  997.         int x, len_enc, plane;
  998.         const byte *p;
  999.         byte bit;
  1000.         if ( !handle_vrep(fd, data, -stride, y, &dy, gbm->w) )
  1001.             { free(enc); free(line); free(mapped); return GBM_ERR_WRITE; }
  1002.         if ( pal )
  1003.             p = data;
  1004.         else
  1005.             {
  1006.             for ( x = 0; x < gbm->w; x++ )
  1007.                 mapped[x] = map[data[x]];
  1008.             p = mapped;
  1009.             }
  1010.         for ( plane = 0, bit = 0x01; plane < 8; plane++, bit <<= 1 )
  1011.             {
  1012.             memset(line, 0, scan);
  1013.             for ( x = 0; x < gbm->w; x++ )
  1014.                 if ( p[x]&bit )
  1015.                     line[(unsigned)x>>3] |= (0x80U>>((unsigned)x&7U));
  1016.             len_enc = literally(line, scan, enc, encode1(line, scan, enc));
  1017.             if ( gbm_file_write(fd, enc, len_enc) != len_enc )
  1018.                 { free(enc); free(line); free(mapped); return GBM_ERR_MEM; }
  1019.             }
  1020.         }
  1021.  
  1022.     free(enc);
  1023.     free(line);
  1024.     free(mapped);
  1025.  
  1026.     }
  1027.     break;
  1028. /*...e*/
  1029. /*...s24 \45\ write raw 24 bpp data in XIMG file:16:*/
  1030. case 24:
  1031.     {
  1032.     int y, dy;
  1033.     byte *line, *enc;
  1034.  
  1035.     putword(hdr+1*2, (word) 11);    /* Header length */
  1036.     putword(hdr+3*2, (word) 3);    /* Pattern length */
  1037.     memcpy(hdr+8*2, "XIMG", 4);    /* Signiture */
  1038.     putword(hdr+10*2, (word) 0);    /* RGB */
  1039.     if ( gbm_file_write(fd, hdr, 11*2) != 11*2 )
  1040.         return GBM_ERR_WRITE;
  1041.  
  1042.     if ( (line = malloc((size_t) (gbm->w*3))) == NULL )
  1043.         return GBM_ERR_MEM;
  1044.  
  1045.     if ( (enc = malloc((size_t) (gbm->w*3*2+2))) == NULL )
  1046.         { free(line); return GBM_ERR_MEM; }
  1047.  
  1048.     for ( y = gbm->h; y > 0; y -= dy, data -= dy*stride )
  1049.         {
  1050.         int len_enc;
  1051.         if ( !handle_vrep(fd, data, -stride, y, &dy, gbm->w*3) )
  1052.             { free(enc); free(line); return GBM_ERR_WRITE; }
  1053.         rgb_bgr(data, line, gbm->w);
  1054.         len_enc = literally(line, gbm->w*3, enc, encode3(line, gbm->w*3, enc));
  1055.         if ( gbm_file_write(fd, enc, len_enc) != len_enc )
  1056.             { free(enc); free(line); return GBM_ERR_WRITE; }
  1057.         }
  1058.  
  1059.     free(enc);
  1060.     free(line);
  1061.  
  1062.     }
  1063.     break;
  1064. /*...e*/
  1065.         }
  1066.  
  1067.     return GBM_ERR_OK;
  1068.     }
  1069. /*...e*/
  1070. /*...sgem_err:0:*/
  1071. const char *gem_err(GBM_ERR rc)
  1072.     {
  1073.     switch ( (int) rc )
  1074.         {
  1075.         case GBM_ERR_GEM_IS_DVI:
  1076.             return "not GEM Raster, probably green channel of an Intel DVI image";
  1077.         case GBM_ERR_GEM_IS_IMDS:
  1078.             return "not GEM Raster, probably IMDS image";
  1079.         case GBM_ERR_GEM_BAD_VERSION:
  1080.             return "version number not 1";
  1081.         case GBM_ERR_GEM_BAD_HDRLEN:
  1082.             return "header length must be 8 words or more";
  1083.         case GBM_ERR_GEM_BAD_PLANES:
  1084.             return "number of planes not in range 1 to 8, or 24";
  1085.         case GBM_ERR_GEM_BAD_FLAG:
  1086.             return "flag not 0 or 1";
  1087.         case GBM_ERR_GEM_FIXEDCPAL:
  1088.             return "number of planes in colour image must be 1,3 or 4";
  1089.         case GBM_ERR_GEM_XIMG_TYPE:
  1090.             return "palette type not 0 in XIMG header";
  1091.         }
  1092.     return NULL;
  1093.     }
  1094. /*...e*/
  1095.