Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "cdjpeg.h"
00019
00020 #ifdef PPM_SUPPORTED
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
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
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
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068 typedef struct {
00069 struct djpeg_dest_struct pub;
00070
00071
00072 char *iobuffer;
00073 JSAMPROW pixrow;
00074 size_t buffer_width;
00075 JDIMENSION samples_per_row;
00076 } ppm_dest_struct;
00077
00078 typedef ppm_dest_struct * ppm_dest_ptr;
00079
00080
00081
00082
00083
00084
00085
00086
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
00101
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
00124
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
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
00181 switch (cinfo->out_color_space) {
00182 case JCS_GRAYSCALE:
00183
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
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
00202
00203
00204 METHODDEF(void)
00205 finish_output_ppm (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
00206 {
00207
00208 fflush(dinfo->output_file);
00209 if (ferror(dinfo->output_file))
00210 ERREXIT(cinfo, JERR_FILE_WRITE);
00211 }
00212
00213
00214
00215
00216
00217
00218 GLOBAL(djpeg_dest_ptr)
00219 jinit_write_ppm (j_decompress_ptr cinfo)
00220 {
00221 ppm_dest_ptr dest;
00222
00223
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
00231 jpeg_calc_output_dimensions(cinfo);
00232
00233
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
00242
00243
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
00257
00258
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