Back to index

plt-scheme  4.2.1
wrppm.c
Go to the documentation of this file.
00001 /*
00002  * wrppm.c
00003  *
00004  * Copyright (C) 1991-1996, Thomas G. Lane.
00005  * This file is part of the Independent JPEG Group's software.
00006  * For conditions of distribution and use, see the accompanying README file.
00007  *
00008  * This file contains routines to write output images in PPM/PGM format.
00009  * The extended 2-byte-per-sample raw PPM/PGM formats are supported.
00010  * The PBMPLUS library is NOT required to compile this software
00011  * (but it is highly useful as a set of PPM image manipulation programs).
00012  *
00013  * These routines may need modification for non-Unix environments or
00014  * specialized applications.  As they stand, they assume output to
00015  * an ordinary stdio stream.
00016  */
00017 
00018 #include "cdjpeg.h"         /* Common decls for cjpeg/djpeg applications */
00019 
00020 #ifdef PPM_SUPPORTED
00021 
00022 
00023 /*
00024  * For 12-bit JPEG data, we either downscale the values to 8 bits
00025  * (to write standard byte-per-sample PPM/PGM files), or output
00026  * nonstandard word-per-sample PPM/PGM files.  Downscaling is done
00027  * if PPM_NORAWWORD is defined (this can be done in the Makefile
00028  * or in jconfig.h).
00029  * (When the core library supports data precision reduction, a cleaner
00030  * implementation will be to ask for that instead.)
00031  */
00032 
00033 #if BITS_IN_JSAMPLE == 8
00034 #define PUTPPMSAMPLE(ptr,v)  *ptr++ = (char) (v)
00035 #define BYTESPERSAMPLE 1
00036 #define PPM_MAXVAL 255
00037 #else
00038 #ifdef PPM_NORAWWORD
00039 #define PUTPPMSAMPLE(ptr,v)  *ptr++ = (char) ((v) >> (BITS_IN_JSAMPLE-8))
00040 #define BYTESPERSAMPLE 1
00041 #define PPM_MAXVAL 255
00042 #else
00043 /* The word-per-sample format always puts the LSB first. */
00044 #define PUTPPMSAMPLE(ptr,v)               \
00045        { register int val_ = v;           \
00046          *ptr++ = (char) (val_ & 0xFF);   \
00047          *ptr++ = (char) ((val_ >> 8) & 0xFF);   \
00048        }
00049 #define BYTESPERSAMPLE 2
00050 #define PPM_MAXVAL ((1<<BITS_IN_JSAMPLE)-1)
00051 #endif
00052 #endif
00053 
00054 
00055 /*
00056  * When JSAMPLE is the same size as char, we can just fwrite() the
00057  * decompressed data to the PPM or PGM file.  On PCs, in order to make this
00058  * work the output buffer must be allocated in near data space, because we are
00059  * assuming small-data memory model wherein fwrite() can't reach far memory.
00060  * If you need to process very wide images on a PC, you might have to compile
00061  * in large-memory model, or else replace fwrite() with a putc() loop ---
00062  * which will be much slower.
00063  */
00064 
00065 
00066 /* Private version of data destination object */
00067 
00068 typedef struct {
00069   struct djpeg_dest_struct pub;    /* public fields */
00070 
00071   /* Usually these two pointers point to the same place: */
00072   char *iobuffer;           /* fwrite's I/O buffer */
00073   JSAMPROW pixrow;          /* decompressor output buffer */
00074   size_t buffer_width;             /* width of I/O buffer */
00075   JDIMENSION samples_per_row;      /* JSAMPLEs per output row */
00076 } ppm_dest_struct;
00077 
00078 typedef ppm_dest_struct * ppm_dest_ptr;
00079 
00080 
00081 /*
00082  * Write some pixel data.
00083  * In this module rows_supplied will always be 1.
00084  *
00085  * put_pixel_rows handles the "normal" 8-bit case where the decompressor
00086  * output buffer is physically the same as the fwrite buffer.
00087  */
00088 
00089 METHODDEF(void)
00090 put_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
00091               JDIMENSION rows_supplied)
00092 {
00093   ppm_dest_ptr dest = (ppm_dest_ptr) dinfo;
00094 
00095   (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
00096 }
00097 
00098 
00099 /*
00100  * This code is used when we have to copy the data and apply a pixel
00101  * format translation.  Typically this only happens in 12-bit mode.
00102  */
00103 
00104 METHODDEF(void)
00105 copy_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
00106                JDIMENSION rows_supplied)
00107 {
00108   ppm_dest_ptr dest = (ppm_dest_ptr) dinfo;
00109   register char * bufferptr;
00110   register JSAMPROW ptr;
00111   register JDIMENSION col;
00112 
00113   ptr = dest->pub.buffer[0];
00114   bufferptr = dest->iobuffer;
00115   for (col = dest->samples_per_row; col > 0; col--) {
00116     PUTPPMSAMPLE(bufferptr, GETJSAMPLE(*ptr++));
00117   }
00118   (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
00119 }
00120 
00121 
00122 /*
00123  * Write some pixel data when color quantization is in effect.
00124  * We have to demap the color index values to straight data.
00125  */
00126 
00127 METHODDEF(void)
00128 put_demapped_rgb (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
00129                 JDIMENSION rows_supplied)
00130 {
00131   ppm_dest_ptr dest = (ppm_dest_ptr) dinfo;
00132   register char * bufferptr;
00133   register int pixval;
00134   register JSAMPROW ptr;
00135   register JSAMPROW color_map0 = cinfo->colormap[0];
00136   register JSAMPROW color_map1 = cinfo->colormap[1];
00137   register JSAMPROW color_map2 = cinfo->colormap[2];
00138   register JDIMENSION col;
00139 
00140   ptr = dest->pub.buffer[0];
00141   bufferptr = dest->iobuffer;
00142   for (col = cinfo->output_width; col > 0; col--) {
00143     pixval = GETJSAMPLE(*ptr++);
00144     PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map0[pixval]));
00145     PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map1[pixval]));
00146     PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map2[pixval]));
00147   }
00148   (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
00149 }
00150 
00151 
00152 METHODDEF(void)
00153 put_demapped_gray (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
00154                  JDIMENSION rows_supplied)
00155 {
00156   ppm_dest_ptr dest = (ppm_dest_ptr) dinfo;
00157   register char * bufferptr;
00158   register JSAMPROW ptr;
00159   register JSAMPROW color_map = cinfo->colormap[0];
00160   register JDIMENSION col;
00161 
00162   ptr = dest->pub.buffer[0];
00163   bufferptr = dest->iobuffer;
00164   for (col = cinfo->output_width; col > 0; col--) {
00165     PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map[GETJSAMPLE(*ptr++)]));
00166   }
00167   (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
00168 }
00169 
00170 
00171 /*
00172  * Startup: write the file header.
00173  */
00174 
00175 METHODDEF(void)
00176 start_output_ppm (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
00177 {
00178   ppm_dest_ptr dest = (ppm_dest_ptr) dinfo;
00179 
00180   /* Emit file header */
00181   switch (cinfo->out_color_space) {
00182   case JCS_GRAYSCALE:
00183     /* emit header for raw PGM format */
00184     fprintf(dest->pub.output_file, "P5\n%ld %ld\n%d\n",
00185            (long) cinfo->output_width, (long) cinfo->output_height,
00186            PPM_MAXVAL);
00187     break;
00188   case JCS_RGB:
00189     /* emit header for raw PPM format */
00190     fprintf(dest->pub.output_file, "P6\n%ld %ld\n%d\n",
00191            (long) cinfo->output_width, (long) cinfo->output_height,
00192            PPM_MAXVAL);
00193     break;
00194   default:
00195     ERREXIT(cinfo, JERR_PPM_COLORSPACE);
00196   }
00197 }
00198 
00199 
00200 /*
00201  * Finish up at the end of the file.
00202  */
00203 
00204 METHODDEF(void)
00205 finish_output_ppm (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
00206 {
00207   /* Make sure we wrote the output file OK */
00208   fflush(dinfo->output_file);
00209   if (ferror(dinfo->output_file))
00210     ERREXIT(cinfo, JERR_FILE_WRITE);
00211 }
00212 
00213 
00214 /*
00215  * The module selection routine for PPM format output.
00216  */
00217 
00218 GLOBAL(djpeg_dest_ptr)
00219 jinit_write_ppm (j_decompress_ptr cinfo)
00220 {
00221   ppm_dest_ptr dest;
00222 
00223   /* Create module interface object, fill in method pointers */
00224   dest = (ppm_dest_ptr)
00225       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
00226                               SIZEOF(ppm_dest_struct));
00227   dest->pub.start_output = start_output_ppm;
00228   dest->pub.finish_output = finish_output_ppm;
00229 
00230   /* Calculate output image dimensions so we can allocate space */
00231   jpeg_calc_output_dimensions(cinfo);
00232 
00233   /* Create physical I/O buffer.  Note we make this near on a PC. */
00234   dest->samples_per_row = cinfo->output_width * cinfo->out_color_components;
00235   dest->buffer_width = dest->samples_per_row * (BYTESPERSAMPLE * SIZEOF(char));
00236   dest->iobuffer = (char *) (*cinfo->mem->alloc_small)
00237     ((j_common_ptr) cinfo, JPOOL_IMAGE, dest->buffer_width);
00238 
00239   if (cinfo->quantize_colors || BITS_IN_JSAMPLE != 8 ||
00240       SIZEOF(JSAMPLE) != SIZEOF(char)) {
00241     /* When quantizing, we need an output buffer for colormap indexes
00242      * that's separate from the physical I/O buffer.  We also need a
00243      * separate buffer if pixel format translation must take place.
00244      */
00245     dest->pub.buffer = (*cinfo->mem->alloc_sarray)
00246       ((j_common_ptr) cinfo, JPOOL_IMAGE,
00247        cinfo->output_width * cinfo->output_components, (JDIMENSION) 1);
00248     dest->pub.buffer_height = 1;
00249     if (! cinfo->quantize_colors)
00250       dest->pub.put_pixel_rows = copy_pixel_rows;
00251     else if (cinfo->out_color_space == JCS_GRAYSCALE)
00252       dest->pub.put_pixel_rows = put_demapped_gray;
00253     else
00254       dest->pub.put_pixel_rows = put_demapped_rgb;
00255   } else {
00256     /* We will fwrite() directly from decompressor output buffer. */
00257     /* Synthesize a JSAMPARRAY pointer structure */
00258     /* Cast here implies near->far pointer conversion on PCs */
00259     dest->pixrow = (JSAMPROW) dest->iobuffer;
00260     dest->pub.buffer = & dest->pixrow;
00261     dest->pub.buffer_height = 1;
00262     dest->pub.put_pixel_rows = put_pixel_rows;
00263   }
00264 
00265   return (djpeg_dest_ptr) dest;
00266 }
00267 
00268 #endif /* PPM_SUPPORTED */