| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
#include "cdjpeg.h" |
| 19 |
|
| 20 |
#ifdef PPM_SUPPORTED |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
#if BITS_IN_JSAMPLE == 8 |
| 34 |
#define PUTPPMSAMPLE(ptr,v) *ptr++ = (char) (v) |
| 35 |
#define BYTESPERSAMPLE 1 |
| 36 |
#define PPM_MAXVAL 255 |
| 37 |
#else |
| 38 |
#ifdef PPM_NORAWWORD |
| 39 |
#define PUTPPMSAMPLE(ptr,v) *ptr++ = (char) ((v) >> (BITS_IN_JSAMPLE-8)) |
| 40 |
#define BYTESPERSAMPLE 1 |
| 41 |
#define PPM_MAXVAL 255 |
| 42 |
#else |
| 43 |
|
| 44 |
#define PUTPPMSAMPLE(ptr,v) \ |
| 45 |
{ register int val_ = v; \ |
| 46 |
*ptr++ = (char) (val_ & 0xFF); \ |
| 47 |
*ptr++ = (char) ((val_ >> 8) & 0xFF); \ |
| 48 |
} |
| 49 |
#define BYTESPERSAMPLE 2 |
| 50 |
#define PPM_MAXVAL ((1<<BITS_IN_JSAMPLE)-1) |
| 51 |
#endif |
| 52 |
#endif |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
typedef struct { |
| 69 |
struct djpeg_dest_struct pub; |
| 70 |
|
| 71 |
|
| 72 |
char *iobuffer; |
| 73 |
JSAMPROW pixrow; |
| 74 |
size_t buffer_width; |
| 75 |
JDIMENSION samples_per_row; |
| 76 |
} ppm_dest_struct; |
| 77 |
|
| 78 |
typedef ppm_dest_struct * ppm_dest_ptr; |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
METHODDEF(void) |
| 90 |
put_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, |
| 91 |
JDIMENSION rows_supplied) |
| 92 |
{ |
| 93 |
ppm_dest_ptr dest = (ppm_dest_ptr) dinfo; |
| 94 |
|
| 95 |
(void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
METHODDEF(void) |
| 105 |
copy_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, |
| 106 |
JDIMENSION rows_supplied) |
| 107 |
{ |
| 108 |
ppm_dest_ptr dest = (ppm_dest_ptr) dinfo; |
| 109 |
register char * bufferptr; |
| 110 |
register JSAMPROW ptr; |
| 111 |
register JDIMENSION col; |
| 112 |
|
| 113 |
ptr = dest->pub.buffer[0]; |
| 114 |
bufferptr = dest->iobuffer; |
| 115 |
for (col = dest->samples_per_row; col > 0; col--) { |
| 116 |
PUTPPMSAMPLE(bufferptr, GETJSAMPLE(*ptr++)); |
| 117 |
} |
| 118 |
(void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
METHODDEF(void) |
| 128 |
put_demapped_rgb (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, |
| 129 |
JDIMENSION rows_supplied) |
| 130 |
{ |
| 131 |
ppm_dest_ptr dest = (ppm_dest_ptr) dinfo; |
| 132 |
register char * bufferptr; |
| 133 |
register int pixval; |
| 134 |
register JSAMPROW ptr; |
| 135 |
register JSAMPROW color_map0 = cinfo->colormap[0]; |
| 136 |
register JSAMPROW color_map1 = cinfo->colormap[1]; |
| 137 |
register JSAMPROW color_map2 = cinfo->colormap[2]; |
| 138 |
register JDIMENSION col; |
| 139 |
|
| 140 |
ptr = dest->pub.buffer[0]; |
| 141 |
bufferptr = dest->iobuffer; |
| 142 |
for (col = cinfo->output_width; col > 0; col--) { |
| 143 |
pixval = GETJSAMPLE(*ptr++); |
| 144 |
PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map0[pixval])); |
| 145 |
PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map1[pixval])); |
| 146 |
PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map2[pixval])); |
| 147 |
} |
| 148 |
(void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width); |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
METHODDEF(void) |
| 153 |
put_demapped_gray (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, |
| 154 |
JDIMENSION rows_supplied) |
| 155 |
{ |
| 156 |
ppm_dest_ptr dest = (ppm_dest_ptr) dinfo; |
| 157 |
register char * bufferptr; |
| 158 |
register JSAMPROW ptr; |
| 159 |
register JSAMPROW color_map = cinfo->colormap[0]; |
| 160 |
register JDIMENSION col; |
| 161 |
|
| 162 |
ptr = dest->pub.buffer[0]; |
| 163 |
bufferptr = dest->iobuffer; |
| 164 |
for (col = cinfo->output_width; col > 0; col--) { |
| 165 |
PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map[GETJSAMPLE(*ptr++)])); |
| 166 |
} |
| 167 |
(void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
METHODDEF(void) |
| 176 |
start_output_ppm (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo) |
| 177 |
{ |
| 178 |
ppm_dest_ptr dest = (ppm_dest_ptr) dinfo; |
| 179 |
|
| 180 |
|
| 181 |
switch (cinfo->out_color_space) { |
| 182 |
case JCS_GRAYSCALE: |
| 183 |
|
| 184 |
fprintf(dest->pub.output_file, "P5\n%ld %ld\n%d\n", |
| 185 |
(long) cinfo->output_width, (long) cinfo->output_height, |
| 186 |
PPM_MAXVAL); |
| 187 |
break; |
| 188 |
case JCS_RGB: |
| 189 |
|
| 190 |
fprintf(dest->pub.output_file, "P6\n%ld %ld\n%d\n", |
| 191 |
(long) cinfo->output_width, (long) cinfo->output_height, |
| 192 |
PPM_MAXVAL); |
| 193 |
break; |
| 194 |
default: |
| 195 |
ERREXIT(cinfo, JERR_PPM_COLORSPACE); |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
METHODDEF(void) |
| 205 |
finish_output_ppm (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo) |
| 206 |
{ |
| 207 |
|
| 208 |
fflush(dinfo->output_file); |
| 209 |
if (ferror(dinfo->output_file)) |
| 210 |
ERREXIT(cinfo, JERR_FILE_WRITE); |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
GLOBAL(djpeg_dest_ptr) |
| 219 |
jinit_write_ppm (j_decompress_ptr cinfo) |
| 220 |
{ |
| 221 |
ppm_dest_ptr dest; |
| 222 |
|
| 223 |
|
| 224 |
dest = (ppm_dest_ptr) |
| 225 |
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
| 226 |
SIZEOF(ppm_dest_struct)); |
| 227 |
dest->pub.start_output = start_output_ppm; |
| 228 |
dest->pub.finish_output = finish_output_ppm; |
| 229 |
|
| 230 |
|
| 231 |
jpeg_calc_output_dimensions(cinfo); |
| 232 |
|
| 233 |
|
| 234 |
dest->samples_per_row = cinfo->output_width * cinfo->out_color_components; |
| 235 |
dest->buffer_width = dest->samples_per_row * (BYTESPERSAMPLE * SIZEOF(char)); |
| 236 |
dest->iobuffer = (char *) (*cinfo->mem->alloc_small) |
| 237 |
((j_common_ptr) cinfo, JPOOL_IMAGE, dest->buffer_width); |
| 238 |
|
| 239 |
if (cinfo->quantize_colors || BITS_IN_JSAMPLE != 8 || |
| 240 |
SIZEOF(JSAMPLE) != SIZEOF(char)) { |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
dest->pub.buffer = (*cinfo->mem->alloc_sarray) |
| 246 |
((j_common_ptr) cinfo, JPOOL_IMAGE, |
| 247 |
cinfo->output_width * cinfo->output_components, (JDIMENSION) 1); |
| 248 |
dest->pub.buffer_height = 1; |
| 249 |
if (! cinfo->quantize_colors) |
| 250 |
dest->pub.put_pixel_rows = copy_pixel_rows; |
| 251 |
else if (cinfo->out_color_space == JCS_GRAYSCALE) |
| 252 |
dest->pub.put_pixel_rows = put_demapped_gray; |
| 253 |
else |
| 254 |
dest->pub.put_pixel_rows = put_demapped_rgb; |
| 255 |
} else { |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
dest->pixrow = (JSAMPROW) dest->iobuffer; |
| 260 |
dest->pub.buffer = & dest->pixrow; |
| 261 |
dest->pub.buffer_height = 1; |
| 262 |
dest->pub.put_pixel_rows = put_pixel_rows; |
| 263 |
} |
| 264 |
|
| 265 |
return (djpeg_dest_ptr) dest; |
| 266 |
} |
| 267 |
|
| 268 |
#endif |