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