home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / netpbma.zip / pnm / pnmtoddif.c < prev    next >
C/C++ Source or Header  |  1993-10-15  |  18KB  |  608 lines

  1. #ifndef lint
  2. static char *RCSid = "$Id: pnmtoddif.c,v 1.6 1993/01/25 08:14:06 neideck Exp neideck $";
  3. #endif
  4.  
  5. /*
  6.  * $Log: pnmtoddif.c,v $
  7.  * Revision 1.6  1993/01/25  08:14:06  neideck
  8.  * Placed into public use form.
  9.  *
  10.  * Revision 1.5  1992/12/02  08:15:18  neideck
  11.  *  Added RCS id.
  12.  *
  13.  */
  14.  
  15. /*
  16.  * Author:      Burkhard Neidecker-Lutz
  17.  *              Digital CEC Karlsruhe
  18.  *         neideck@nestvx.enet.dec.com 
  19.  
  20.  Copyright (c) Digital Equipment Corporation, 1992
  21.  
  22.  Permission to use, copy, modify, and distribute this software and its
  23.  documentation for any purpose and without fee is hereby granted,
  24.  provided that the above copyright notice appear in all copies and that
  25.  both that copyright notice and this permission notice appear in
  26.  supporting documentation, and that the name of Digital Equipment
  27.  Corporation not be used in advertising or publicity pertaining to
  28.  distribution of the software without specific, written prior
  29.  permission. Digital Equipment Corporation makes no representations
  30.  about the suitability of this software for any purpose. It is provided
  31.  "as is" without express or implied warranty.
  32.  
  33.  Version: 1.0            30.07.92
  34.  
  35. */
  36.  
  37. #include "pnm.h"
  38.  
  39. /* The structure we use to convey all sorts of "magic" data to the DDIF */
  40. /* header write procedure.                        */
  41.  
  42. typedef struct {
  43.   int width;
  44.   int height;
  45.   int h_res;            /* Resolutions in dpi for bounding box */
  46.   int v_res;
  47.   int bits_per_pixel;
  48.   int bytes_per_line;
  49.   int spectral;            /* 2 == monochrome, 5 == rgb        */
  50.   int components;
  51.   int bits_per_component;
  52.   int polarity;            /* zeromin == 2 , zeromax == 1        */
  53. } imageparams;
  54.  
  55.  
  56. static void tag ARGS((unsigned char **, int, int, unsigned int));
  57. static void ind ARGS((unsigned char **));
  58. static void wr_null ARGS((unsigned char **));
  59. static void wr_length ARGS((unsigned char **, int));
  60. static void wr_int ARGS((unsigned char **, int));
  61. static void eoc ARGS((unsigned char **));
  62. static void wr_string ARGS((unsigned char **, char *));
  63. static void emit_isolatin1 ARGS((unsigned char **, char *));
  64. int write_header ARGS((FILE *, imageparams *));
  65. int write_trailer ARGS((FILE *));
  66. int main ARGS((int, char *[]));
  67.  
  68.  
  69. /* ASN.1 basic encoding rules magic number */
  70. #define UNIVERSAL 0
  71. #define APPLICATION 1
  72. #define CONTEXT 2
  73. #define PRIVATE 3
  74.  
  75. #define PRIM 0
  76. #define CONS 1
  77.  
  78. /* "tag": Emit an ASN tag of the specified class and tag number.    */
  79. /* This is used in conjuntion with the                    */
  80. /* wr_xxx routines that follow to construct the various ASN.1 entities. */
  81. /* Writing each entity is a two-step process, where first the tag is    */
  82. /* written and then the length and value.                */
  83. /* All of these routines take a pointer to a pointer into an output    */
  84. /* buffer in the first argument and update it accordingly.        */
  85.  
  86. static void tag(buffer,cl,constructed,t)
  87. unsigned char **buffer;
  88. int cl;
  89. int constructed;
  90. unsigned int t;
  91. {
  92.   int tag_first;
  93.   unsigned int stack[10];
  94.   int sp;
  95.   unsigned char *p = *buffer;
  96.  
  97.   tag_first = (cl << 6) | constructed << 5;
  98.   if (t < 31) {         /* Short tag form    */
  99.     *p++ = tag_first | t;
  100.   } else {            /* Long tag form */
  101.     *p++ = tag_first | 31;
  102.     sp = 0;
  103.     while (t > 0) {
  104.       stack[sp++] = t & 0x7f; 
  105.       t >>= 7;
  106.     }
  107.     while (--sp > 0) {    /* Tag values with continuation bits */
  108.       *p++ = stack[sp] | 0x80;
  109.     }
  110.     *p++ = stack[0];    /* Last tag portion without continuation bit */
  111.   }
  112.   *buffer = p;        /* Update buffer pointer */
  113. }
  114.  
  115. /* Emit indefinite length encoding */
  116. static void ind(buffer)
  117. unsigned char **buffer;
  118. {
  119.   unsigned char *p = *buffer;
  120.  
  121.   *p++ = 0x80;
  122.   *buffer = p;
  123. }
  124.  
  125. /* Emit ASN.1 NULL */
  126. static void wr_null(buffer)
  127. unsigned char **buffer;
  128. {
  129.   unsigned char *p = *buffer;
  130.  
  131.   *p++ = 0;
  132.   *buffer = p;
  133. }
  134.  
  135. /* Emit ASN.1 length only into buffer, no data */
  136. static void wr_length(buffer,amount)
  137. unsigned char **buffer;
  138. int amount;
  139. {
  140.   int length;
  141.   unsigned int mask;
  142.   unsigned char *p = *buffer;
  143.  
  144.   length = 4;
  145.   mask = 0xff000000;
  146.  
  147.   if (amount < 128) {
  148.     *p++ = amount;
  149.   } else {            /* > 127 */
  150.     while (!(amount & mask)) {    /* Search for first non-zero byte */
  151.       mask >>= 8;
  152.       --length;
  153.     }
  154.  
  155.     *p++ = length | 0x80;        /* Number of length bytes */
  156.  
  157.     while (--length >= 0) {        /* Put length bytes      */
  158.       *p++ =(amount >> (8*length)) & 0xff;
  159.     }
  160.  
  161.   }
  162.   *buffer = p;
  163. }
  164.  
  165. /* BER encode an integer and write it's length and value */
  166. static void wr_int(buffer,val)
  167. unsigned char **buffer;
  168. int val;
  169. {
  170.   int length;
  171.   int sign;
  172.   unsigned int mask;
  173.   unsigned char *p = *buffer;
  174.  
  175.   if (val == 0) {
  176.     *p++ = 1;                /* length */
  177.     *p++ = 0;                /* value  */
  178.   } else {
  179.     sign = val < 0 ? 0xff : 0x00;    /* Sign bits */
  180.     length = 4;
  181. #ifdef __STDC__
  182.     mask  = 0xffu << 24;
  183. #else
  184.     mask  = 0xff << 24;
  185. #endif
  186.     while ((val & mask) == sign) {    /* Find the smallest representation */
  187.       length--;
  188.       mask >>= 8;
  189.     }
  190.     sign = (0x80 << ((length-1)*8)) & val; /* New sign bit */
  191.     if (((val < 0) && !sign) || ((val > 0) && sign)) { /* Sign error */
  192.       length++;
  193.     }
  194.     *p++ = length;            /* length */
  195.     while (--length >= 0) {
  196.       *p++ = (val >> (8*length)) & 0xff;
  197.     }
  198.   }
  199.   *buffer = p;
  200. }
  201.  
  202. /* Emit and End Of Coding sequence    */
  203. static void eoc(buffer)
  204. unsigned char **buffer;
  205. {
  206.   unsigned char *p = *buffer;
  207.  
  208.   *p++ = 0;
  209.   *p++ = 0;
  210.   *buffer = p;
  211. }
  212.  
  213. /* Emit a simple string    */
  214. static void wr_string(buffer,val)
  215. unsigned char **buffer;
  216. char* val;
  217. {
  218.   int length;
  219.   unsigned char *p = *buffer;
  220.  
  221.   length  = strlen(val);        
  222.   if (length > 127) {
  223.    fprintf(stderr,"Can't encode length > 127 yet (%d)\n",length);
  224.    exit(1);
  225.   }
  226.   *p++ = length;
  227.   while (*val) {            /* string */
  228.     *p++ = *val++;
  229.   }
  230.   *buffer = p;
  231. }
  232.  
  233. /* Emit a ISOLATIN-1 string */
  234. static void emit_isolatin1(buffer,val)
  235. unsigned char **buffer;
  236. char* val;
  237. {
  238.   int length;
  239.   unsigned char *p = *buffer;
  240.  
  241.   length  = strlen(val) + 1;        /* One NULL byte and charset leader */
  242.   if (length > 127) {
  243.    fprintf(stderr,"Can't encode length > 127 yet (%d)\n",length);
  244.    exit(1);
  245.   }
  246.   *p++ = length;
  247.   *p++ = 1;                /* ISO LATIN-1 */
  248.   while (*val) {            /* string */
  249.     *p++ = *val++;
  250.   }
  251.   *buffer = p;
  252. }
  253.  
  254. /* Write the DDIF grammar onto "file" up to the actual starting location */
  255. /* of the image data. The "ip" structure needs to be set to the right     */
  256. /* values. A lot of the values here are hardcoded to be just right for     */
  257. /* the bit grammars that the PBMPLUS formats want.             */
  258.  
  259. int write_header(file,ip)
  260. FILE *file;
  261. imageparams *ip;
  262. {
  263.   unsigned char buffer[300];            /* Be careful with the size ! */
  264.   unsigned char *p = buffer;
  265.   int headersize;
  266.   int bounding_x;
  267.   int bounding_y;
  268.   int i;
  269.  
  270.   /* Calculate the bounding box from the resolutions    */
  271.   bounding_x = ((int) (1200 * ((double) (ip->width) / ip->h_res)));
  272.   bounding_y = ((int) (1200 * ((double) (ip->height) / ip->v_res)));
  273.  
  274.   /* This is gross. The entire DDIF grammar is constructed by    */
  275.   /* hand. The indentation is meant to indicate DDIF document structure */
  276.  
  277.   tag(&p,PRIVATE,CONS,16383); ind(&p);         /* DDIF Document */
  278.    tag(&p,CONTEXT,CONS, 0); ind(&p);         /* Document Descriptor */
  279.     tag(&p,CONTEXT,PRIM, 0); wr_int(&p,1);    /* Major Version */
  280.     tag(&p,CONTEXT,PRIM, 1); wr_int(&p,3);    /* Minor Version */
  281.     tag(&p,CONTEXT,PRIM, 2); wr_string(&p,"PBM+"); /* Product Indentifier */
  282.     tag(&p,CONTEXT,CONS, 3); ind(&p);         /* Product Name */
  283.      tag(&p,PRIVATE,PRIM, 9); emit_isolatin1(&p,"PBMPLUS Writer V1.0");
  284.     eoc(&p);
  285.    eoc(&p);                    /* Document Descriptor */
  286.    tag(&p,CONTEXT,CONS, 1); ind(&p);         /* Document Header     */
  287.     tag(&p,CONTEXT,CONS, 3); ind(&p);         /* Version */
  288.      tag(&p,PRIVATE,PRIM, 9); emit_isolatin1(&p,"1.0");
  289.     eoc(&p);
  290.    eoc(&p);                    /* Document Header */
  291.    tag(&p,CONTEXT,CONS, 2); ind(&p);         /* Document Content    */
  292.     tag(&p,APPLICATION,CONS,2); ind(&p);     /* Segment Primitive    */
  293.     eoc(&p);
  294.     tag(&p,APPLICATION,CONS,2); ind(&p);     /* Segment    */
  295.      tag(&p,CONTEXT,CONS, 3); ind(&p);         /* Segment Specific Attributes */
  296.       tag(&p,CONTEXT,PRIM, 2); wr_string(&p,"$I");  /* Category    */
  297.       tag(&p,CONTEXT,CONS,22); ind(&p);        /* Image Attributes    */
  298.        tag(&p,CONTEXT,CONS, 0); ind(&p);     /* Image Presentation Attributes */
  299.         tag(&p,CONTEXT,PRIM, 1); wr_int(&p,0);  /* Pixel Path */
  300.         tag(&p,CONTEXT,PRIM, 2); wr_int(&p,270); /* Line Progression */
  301.         tag(&p,CONTEXT,CONS, 3); ind(&p);      /* Pixel Aspect Ratio */
  302.          tag(&p,CONTEXT,PRIM, 0); wr_int(&p,1); /* PP Pixel Dist */
  303.          tag(&p,CONTEXT,PRIM, 1); wr_int(&p,1); /* LP Pixel Dist */
  304.         eoc(&p);                /* Pixel Aspect Ratio */
  305.         tag(&p,CONTEXT,PRIM, 4); wr_int(&p,ip->polarity);  /* Brightness Polarity */
  306.         tag(&p,CONTEXT,PRIM, 5); wr_int(&p,1);  /* Grid Type    */
  307.         tag(&p,CONTEXT,PRIM, 7); wr_int(&p,ip->spectral);  /* Spectral Mapping */
  308.         tag(&p,CONTEXT,CONS,10); ind(&p);     /* Pixel Group Info */
  309.          tag(&p,CONTEXT,PRIM, 0); wr_int(&p,1); /* Pixel Group Size */
  310.          tag(&p,CONTEXT,PRIM, 1); wr_int(&p,1); /* Pixel Group Order */
  311.         eoc(&p);                /* Pixel Group Info */
  312.        eoc(&p);                           /* Image Presentation Attributes */
  313.        tag(&p,CONTEXT,CONS, 1); ind(&p);       /* Component Space Attributes */
  314.         tag(&p,CONTEXT,PRIM, 0); wr_int(&p,1);  /* Component Space Organization */
  315.         tag(&p,CONTEXT,PRIM, 1); wr_int(&p,1);  /* Planes per Pixel    */
  316.         tag(&p,CONTEXT,PRIM, 2); wr_int(&p,1);  /* Plane Significance     */
  317.         tag(&p,CONTEXT,PRIM, 3); wr_int(&p,ip->components);  /* Number of Components    */
  318.         tag(&p,CONTEXT,CONS, 4); ind(&p);    /* Bits per Component    */
  319.      for (i = 0; i < ip->components; i++) {
  320.            tag(&p,UNIVERSAL,PRIM,2); wr_int(&p,ip->bits_per_component);
  321.      }
  322.         eoc(&p);                /* Bits per Component    */
  323.         tag(&p,CONTEXT,CONS, 5); ind(&p);    /* Component Quantization Levels */
  324.      for (i = 0; i < ip->components; i++) {
  325.            tag(&p,UNIVERSAL,PRIM,2); wr_int(&p,1 << ip->bits_per_component);
  326.      }
  327.         eoc(&p);                /* Component Quantization Levels */
  328.        eoc(&p);                       /* Component Space Attributes */
  329.       eoc(&p);                       /* Image Attributes    */
  330.       tag(&p,CONTEXT,CONS,23); ind(&p);     /* Frame Parameters    */
  331.        tag(&p,CONTEXT,CONS, 1); ind(&p);     /* Bounding Box    */
  332.         tag(&p,CONTEXT,CONS, 0); ind(&p);     /* lower-left    */
  333.          tag(&p,CONTEXT,CONS, 0); ind(&p);     /* XCoordinate    */
  334.           tag(&p,CONTEXT,PRIM, 0); wr_int(&p,0);
  335.          eoc(&p);                             /* XCoordinate    */
  336.          tag(&p,CONTEXT,CONS, 1); ind(&p);     /* YCoordinate    */
  337.           tag(&p,CONTEXT,PRIM, 0); wr_int(&p,0);
  338.          eoc(&p);                               /* YCoordinate    */
  339.         eoc(&p);                /* lower left */
  340.         tag(&p,CONTEXT,CONS, 1); ind(&p);       /* upper right */
  341.          tag(&p,CONTEXT,CONS, 0); ind(&p);      /* XCoordinate    */
  342.           tag(&p,CONTEXT,PRIM, 0); wr_int(&p,bounding_x);
  343.          eoc(&p);                 /* XCoordinate    */
  344.          tag(&p,CONTEXT,CONS, 1); ind(&p);      /* YCoordinate    */
  345.           tag(&p,CONTEXT,PRIM, 0); wr_int(&p,bounding_y);
  346.          eoc(&p);                     /* YCoordinate    */
  347.         eoc(&p);                            /* upper right */
  348.        eoc(&p);                       /* Bounding Box */
  349.        tag(&p,CONTEXT,CONS, 4); ind(&p);     /* Frame Position */
  350.         tag(&p,CONTEXT,CONS, 0); ind(&p);    /* XCoordinate    */
  351.          tag(&p,CONTEXT,PRIM, 0); wr_int(&p,0);
  352.         eoc(&p);                /* XCoordinate    */
  353.         tag(&p,CONTEXT,CONS, 1); ind(&p);     /* YCoordinate    */
  354.          tag(&p,CONTEXT,PRIM, 0); wr_int(&p,0);
  355.         eoc(&p);                    /* YCoordinate    */
  356.        eoc(&p);                       /* Frame Position */
  357.       eoc(&p);                       /* Frame Parameters */
  358.     eoc(&p);                        /* Segment Specific Attributes */
  359.     eoc(&p);                    /* Segment */
  360.     tag(&p,APPLICATION,CONS,17); ind(&p);     /* Image Data Descriptor */
  361.      tag(&p,UNIVERSAL,CONS,16); ind(&p);    /* Sequence */
  362.       tag(&p,CONTEXT,CONS, 0); ind(&p);     /* Image Coding Attributes */
  363.        tag(&p,CONTEXT,PRIM, 1); wr_int(&p,ip->width); /* Pixels per Line    */
  364.        tag(&p,CONTEXT,PRIM, 2); wr_int(&p,ip->height);  /* Number of Lines    */
  365.        tag(&p,CONTEXT,PRIM, 3); wr_int(&p,2);   /* Compression Type    */
  366.        tag(&p,CONTEXT,PRIM, 5); wr_int(&p,0);   /* Data Offset    */
  367.        tag(&p,CONTEXT,PRIM, 6); wr_int(&p,ip->bits_per_pixel);  /* Pixel Stride    */
  368.        tag(&p,CONTEXT,PRIM, 7); wr_int(&p,ip->bytes_per_line * 8);/* Scanline Stride    */
  369.        tag(&p,CONTEXT,PRIM, 8); wr_int(&p,1);    /* Bit Order        */
  370.        tag(&p,CONTEXT,PRIM, 9); wr_int(&p,ip->bits_per_pixel);    /* Planebits per Pixel */
  371.        tag(&p,CONTEXT,CONS,10); ind(&p);     /* Byteorder Info    */
  372.         tag(&p,CONTEXT,PRIM, 0); wr_int(&p,1);    /* Byte Unit        */
  373.         tag(&p,CONTEXT,PRIM, 1); wr_int(&p,1);    /* Byte Order    */
  374.        eoc(&p);                       /* Byteorder Info    */
  375.        tag(&p,CONTEXT,PRIM,11); wr_int(&p,3);    /* Data Type        */
  376.       eoc(&p);                                /* Image Coding Attributes */
  377.       tag(&p,CONTEXT,PRIM, 1); wr_length(&p,ip->bytes_per_line*ip->height);  /* Component Plane Data */
  378.   /* End of DDIF document Indentation */
  379.   headersize = p - buffer;
  380.   if (headersize >= 300)  {
  381.     fprintf(stderr,"Overran buffer area %d >= 300\n",headersize);
  382.     exit(1);
  383.   }
  384.  
  385.   return (fwrite(buffer, 1, headersize, file) == headersize);
  386. }
  387.  
  388. /* Write all the closing brackets of the DDIF grammar that are missing */
  389. /* The strange indentation reflects exactly the same indentation that  */
  390. /* we left off in the write_header procedure.                   */
  391. int write_trailer(file)
  392. FILE *file;
  393. {
  394.   unsigned char buffer[30];
  395.   unsigned char *p = buffer;
  396.   int trailersize;
  397.  
  398.   /* Indentation below gives DDIF document structure */
  399.      eoc(&p);                         /* Sequence */
  400.     eoc(&p);                     /* Image Data Descriptor */
  401.     tag(&p,APPLICATION,PRIM,1); wr_null(&p);     /* End Segment */
  402.     tag(&p,APPLICATION,PRIM,1); wr_null(&p);     /* End Segment */
  403.    eoc(&p);                     /* Document Content */
  404.   eoc(&p);                     /* DDIF Document */
  405.   /* End of DDIF document Indentation */
  406.   trailersize = p - buffer;
  407.   if (trailersize >= 30)  {
  408.     fprintf(stderr,"Overran buffer area %d >= 30\n",trailersize);
  409.     exit(1);
  410.   }
  411.  
  412.   return(fwrite(buffer, 1, trailersize, file) == trailersize);
  413. }
  414.  
  415. int main(argc,argv)
  416. int argc;
  417. char *argv[];
  418. {
  419.   FILE           *ifd;
  420.   FILE         *ofd;
  421.   int             rows, cols;
  422.   xelval          maxval;
  423.   int             format;
  424.   char           *usage = "[-resolution x y] [pnmfile [ddiffile]]";
  425.   int             i, j;
  426.   char           *outfile;
  427.   int        argn;
  428.   int hor_resolution = 75;
  429.   int ver_resolution = 75;
  430.   imageparams ip;
  431.   unsigned char  *data, *p;
  432.  
  433.   pnm_init(&argc, argv);
  434.  
  435.   for (argn = 1;argn < argc && argv[argn][0] == '-';argn++) {
  436.     int arglen = strlen(argv[argn]);
  437.  
  438.     if (!strncmp (argv[argn],"-resolution", arglen)) {
  439.       if (argn + 2 < argc) {
  440.         hor_resolution = atoi(argv[argn+1]);
  441.         ver_resolution = atoi(argv[argn+2]);
  442.         argn += 2;
  443.         continue;
  444.       } else {
  445.         pm_usage(usage);
  446.       }
  447.     } else {
  448.       pm_usage(usage);
  449.     }
  450.   }
  451.  
  452.   if (hor_resolution <= 0 || ver_resolution <= 0) {
  453.     fprintf(stderr,"Unreasonable resolution values: %d x %d\n",
  454.     hor_resolution,ver_resolution);
  455.     exit(1);
  456.   }
  457.  
  458.   if (argn == argc - 2) {
  459.     ifd = pm_openr(argv[argn]);
  460.     outfile = argv[argn+1];
  461.     if (!(ofd = fopen(outfile,"w"))) {
  462.       perror(outfile);
  463.       exit(1);
  464.     }
  465.   } else if (argn == argc - 1) {
  466.     ifd = pm_openr(argv[argn]);
  467.     ofd = stdout;
  468.   } else {
  469.     ifd = stdin;
  470.     ofd = stdout;
  471.   }
  472.  
  473.   pnm_readpnminit(ifd, &cols, &rows, &maxval, &format);
  474.  
  475.   ip.width = cols;
  476.   ip.height = rows;
  477.   ip.h_res = hor_resolution;
  478.   ip.v_res = ver_resolution;
  479.  
  480.   switch (PNM_FORMAT_TYPE(format)) {
  481.   case PBM_TYPE:
  482.      ip.bits_per_pixel = 1;
  483.      ip.bytes_per_line = (cols + 7) / 8;
  484.      ip.spectral = 2;
  485.      ip.components = 1;
  486.      ip.bits_per_component = 1;
  487.      ip.polarity = 1;
  488.     break;
  489.   case PGM_TYPE:
  490.      ip.bytes_per_line = cols;
  491.      ip.bits_per_pixel = 8;
  492.      ip.spectral = 2;
  493.      ip.components = 1;
  494.      ip.bits_per_component = 8;
  495.      ip.polarity = 2;
  496.     break;
  497.   case PPM_TYPE:
  498.      ip.bytes_per_line = 3 * cols;
  499.      ip.bits_per_pixel = 24;
  500.      ip.spectral = 5;
  501.      ip.components = 3;
  502.      ip.bits_per_component = 8;
  503.      ip.polarity = 2;
  504.     break;
  505.   default:
  506.     fprintf(stderr, "Unrecognized PBMPLUS format %d\n", format);
  507.     exit(1);
  508.   }
  509.  
  510.   if (!write_header(ofd,&ip)) {
  511.     perror("Writing header");
  512.     exit(1);
  513.   }
  514.  
  515.   if (!(p = data = (unsigned char*)  malloc(ip.bytes_per_line))) {
  516.     perror("allocating line buffer");
  517.     exit(1);
  518.   }
  519.  
  520.   switch (PNM_FORMAT_TYPE(format)) {
  521.   case PBM_TYPE:
  522.     {
  523.       bit            *pixels;
  524.       int             mask;
  525.       int             k;
  526.  
  527.       pixels = pbm_allocrow(cols);
  528.  
  529.       for (i = 0; i < rows; i++) {
  530.     pbm_readpbmrow(ifd, pixels, cols, format);
  531.     mask = 0;
  532.     p = data;
  533.     for (j = 0, k = 0; j < cols; j++) {
  534.       if (pixels[j] == PBM_BLACK) {
  535.         mask |= 1 << k;
  536.       }
  537.       if (k == 7) {
  538.         *p++ = mask;
  539.         mask = 0;
  540.         k = 0;
  541.       } else {
  542.         k++;
  543.       }
  544.     }
  545.     if (k != 7) {        /* Flush the rest of the column */
  546.       *p = mask;
  547.     }
  548.     if (fwrite(data,1,ip.bytes_per_line,ofd) != ip.bytes_per_line) {
  549.       perror("Writing image data\n");
  550.       exit(1);
  551.     }
  552.       }
  553.     }
  554.     break;
  555.   case PGM_TYPE:
  556.     {
  557.       gray           *pixels;
  558.  
  559.       pixels = (gray *) data;
  560.  
  561.       for (i = 0; i < rows; i++) {
  562.     pgm_readpgmrow(ifd, pixels, cols, maxval, format);
  563.     if (fwrite(data,1,ip.bytes_per_line,ofd) != ip.bytes_per_line) {
  564.       perror("Writing image data\n");
  565.       exit(1);
  566.     }
  567.       }
  568.     }
  569.     break;
  570.   case PPM_TYPE:
  571.     {
  572.       pixel          *pixels = ppm_allocrow(cols);
  573.  
  574.       for (i = 0; i < rows; i++) {
  575.         p = data;
  576.     ppm_readppmrow(ifd, pixels, cols, maxval, format);
  577.     for (j = 0; j < cols; j++) {
  578.       *p++ = PPM_GETR(pixels[j]);
  579.       *p++ = PPM_GETG(pixels[j]);
  580.       *p++ = PPM_GETB(pixels[j]);
  581.     }
  582.     if (fwrite(data,1,ip.bytes_per_line,ofd) != ip.bytes_per_line) {
  583.       perror("Writing image data\n");
  584.       exit(1);
  585.     }
  586.       }
  587.       ppm_freerow(pixels);
  588.     }
  589.     break;
  590.   }
  591.  
  592.   pm_close(ifd);
  593.  
  594.   free(data);
  595.  
  596.   if (!write_trailer(ofd)) {
  597.     perror("Writing trailer");
  598.     exit(1);
  599.   }
  600.  
  601.   if (fclose(ofd) == EOF) {
  602.     perror("Closing output file");
  603.     exit(1);
  604.   };
  605.  
  606.   return(0);
  607. }
  608.