home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 4 / DATAFILE_PDCD4.iso / utilities / utilsm / netpbmsca / pbm / c / gemtopbm < prev    next >
Encoding:
Text File  |  1993-10-04  |  5.8 KB  |  233 lines

  1. /*
  2.  * Convert a GEM .img file to a portable bitmap file.
  3.  *
  4.  * Author: Diomidis D. Spinellis
  5.  * (C) Copyright 1988 Diomidis D. Spinellis.
  6.  *
  7.  * Permission to use, copy, modify, and distribute this software and its
  8.  * documentation for any purpose and without fee is hereby granted,
  9.  * provided that the above copyright notice appear in all copies and that
  10.  * both that copyright notice and this permission notice appear in
  11.  * supporting documentation.
  12.  *
  13.  * This file is provided AS IS with no warranties of any kind.  The author
  14.  * shall have no liability with respect to the infringement of copyrights,
  15.  * trade secrets or any patents by this file or any part thereof.  In no
  16.  * event will the author be liable for any lost revenue or profits or
  17.  * other special, indirect and consequential damages.
  18.  *
  19.  * Comments and additions should be sent to the author:
  20.  *
  21.  *                     Diomidis D. Spinellis
  22.  *                     1 Myrsinis Str.
  23.  *                     GR-145 62 Kifissia
  24.  *                     GREECE
  25.  *
  26.  */
  27.  
  28. #include <assert.h>
  29. #include "pbm.h"
  30.  
  31. /*
  32.  * Handle systems that do CR-LF translation on reading / writing and
  33.  * little endians (some guesswork is involved).
  34.  */
  35. #ifdef MSDOS
  36. #define LITTLE_ENDIAN
  37. #endif /*MSDOS*/
  38. #ifdef xenix
  39. #define LITTLE_ENDIAN
  40. #endif /*xenix*/
  41. #ifdef sun386
  42. #define LITTLE_ENDIAN
  43. #endif /*sun386*/
  44. #ifdef vax
  45. #define LITTLE_ENDIAN
  46. #endif /*vax*/
  47.  
  48.  
  49. /* Seek from current position */
  50. #ifndef SEEK_CUR
  51. #define SEEK_CUR 1
  52. #endif /*SEEK_CUR*/
  53.  
  54. /*
  55.  * File header structure
  56.  */
  57. struct header {
  58.     short           version;/* Image file version */
  59.     unsigned short  hlen;    /* Header length in bytes */
  60.     unsigned short  planes;    /* Number of planes */
  61.     unsigned short  patlen;    /* Pattern definition length (bytes) */
  62.     unsigned short  pxlen;    /* Pixel height (microns) */
  63.     unsigned short  pxht;    /* Pixel height (microns) */
  64.     unsigned short  linewid;/* Scan line width (bytes) */
  65.     unsigned short  nitems;    /* Number of scan line items */
  66. };
  67.  
  68. /*
  69.  * Scan item header
  70.  */
  71. struct line {
  72.     char            d1, d2;    /* Should be 0 */
  73.     char            d3;    /* Should be 0xff */
  74.     char            count;    /* Repetition count */
  75. };
  76.  
  77. char            pattern[256];
  78.  
  79.  
  80. #ifdef LITTLE_ENDIAN
  81. static void byteswap ARGS(( unsigned short* data, int n ));
  82. #endif /*LITTLE_ENDIAN*/
  83.  
  84. int
  85. main(argc, argv)
  86.     int             argc;
  87.     char           *argv[];
  88. {
  89.     int             debug = 0;
  90.     FILE           *f;
  91.     struct header   hd;
  92.     int             x;
  93.     int             i, j, k, l;
  94.     int             c, cc, linerep;
  95.     int        rows, cols;
  96.     bit        *bitrow;
  97.  
  98.  
  99.     pbm_init( &argc, argv );
  100.  
  101.     /* Check if the compiler alligns structures the way we want */
  102.     if ( sizeof( struct header ) != 16 )
  103.         pm_error( "sizeof( struct header ) != 16 ???" );
  104.  
  105.     if (argc > 1 && !strcmp(argv[1], "-d")) {
  106.         argc--;
  107.         argv[1] = argv[0];
  108.         ++argv;
  109.         ++debug;
  110.     }
  111.  
  112.     if (argc != 2)
  113.         pm_usage("[-d] <gemfile>");
  114.  
  115.     f = pm_openr( argv[1] );
  116.  
  117.     if (fread(&hd, sizeof hd, 1, f) != 1)
  118.         pm_perror( "read" );
  119.  
  120. #ifdef LITTLE_ENDIAN
  121.     byteswap((unsigned short *) &hd, sizeof hd / 2);
  122. #endif /*LITTLE_ENDIAN*/
  123.  
  124.     for ( i = sizeof(struct header); i < hd.hlen; ++i )
  125.         (void) getc(f);
  126.  
  127.     if (debug) {
  128.         pm_message(
  129.             "version %d, hlen %d, planes %d, patlen %d",
  130.                 hd.version, hd.hlen, hd.planes, hd.patlen, 0);
  131.         pm_message(
  132.             "pxlen %d, pxht %d, linewid %d, nitems %d",
  133.             hd.pxlen, hd.pxht, hd.linewid, hd.nitems, 0);
  134.     }
  135.  
  136.     cols = hd.linewid % 8 == 0 ? hd.linewid : hd.linewid + 8 - hd.linewid % 8;
  137.     rows = hd.nitems;
  138.     pbm_writepbminit( stdout, cols, rows, 0 );
  139.     bitrow = pbm_allocrow( cols );
  140.  
  141.     for (i = 0; i < hd.nitems; ) {
  142.         x = 0;
  143.         linerep = 0;
  144.         while (x < hd.linewid) {
  145.             switch (c = getc(f)) {
  146.             case 0x80:    /* Bit String */
  147.                 c = getc(f);    /* Byte count */
  148.                 if (debug)
  149.                     pm_message(
  150.                         "bit string of %d bytes",
  151.                         c );
  152.                 for (j = 0; j < c; ++j) {
  153.                     cc = getc(f);
  154.                     for (k = 0x80; k; k >>= 1) {
  155.                         bitrow[x] = (k & cc) ? PBM_BLACK : PBM_WHITE;
  156.                         ++x;
  157.                     }
  158.                 }
  159.                 break;
  160.             case 0:        /* Pattern run */
  161.                 c = getc(f);    /* Repeat count */
  162.                 if (debug)
  163.                     pm_message(
  164.                         "pattern run of %d repetitions",
  165.                         c );
  166.                                 /* line repeat */
  167.                                 if (c == 0) {
  168.                                         c = getc(f);
  169.                                         if (c != 0x00ff)
  170.                                                 pm_error( "badly formed line repeat" );
  171.                                         linerep = getc(f);
  172.                                         break;
  173.                                 }
  174.                 fread(pattern, 1, hd.patlen, f);
  175.                 for (j = 0; j < c; ++j)
  176.                     for (l = 0; l < hd.patlen; ++l)
  177.                         for (k = 0x80; k; k >>= 1) {
  178.                             bitrow[x] = (k & pattern[l]) ? PBM_BLACK : PBM_WHITE;
  179.                             ++x;
  180.                         }
  181.                 break;
  182.  
  183.             default:    /* Solid run */
  184.                 if (debug)
  185.                     pm_message(
  186.                         "solid run of %d bytes %s",
  187.                         c & 0x7f,
  188.                         c & 0x80 ? "on" : "off" );
  189.                                 /* each byte had eight bits DSB */
  190.                                 l = (c & 0x80) ? PBM_BLACK : PBM_WHITE;
  191.                                 c = (c & 0x7f) * 8;
  192.                                 for (j = 0; j < c; ++j) {
  193.                                         bitrow[x] = l;
  194.                                         ++x;
  195.                                 }
  196.                 break;
  197.  
  198.             case EOF:    /* End of file */
  199.                 pm_error( "end of file reached" );
  200.  
  201.             }
  202.         }
  203.                 if ( debug )
  204.                         pm_message( "EOL" );
  205.                 if ( x != hd.linewid )
  206.                         pm_error( "EOL beyond edge" );
  207.         do {
  208.             pbm_writepbmrow( stdout, bitrow, cols, 0 );
  209.             ++i;
  210.         } while (linerep--);
  211.     }
  212.     pm_close( f );
  213.     pm_close( stdout );
  214.     exit(0);
  215. }
  216.  
  217. #ifdef LITTLE_ENDIAN
  218. static void
  219. byteswap(data, n)
  220.     unsigned short* data;
  221.     int             n;
  222. {
  223.     register        i;
  224.     char           *p, c;
  225.  
  226.     for (i = 0, p = (char *) data; i < n; ++i, ++p) {
  227.         c = *p;
  228.         *p = *(p + 1);
  229.         *++p = c;
  230.     }
  231. }
  232. #endif /*LITTLE_ENDIAN*/
  233.