| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#include "cdjpeg.h" |
| 22 |
|
| 23 |
#ifdef PPM_SUPPORTED |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
#ifdef HAVE_UNSIGNED_CHAR |
| 42 |
typedef unsigned char U_CHAR; |
| 43 |
#define UCH(x) ((int) (x)) |
| 44 |
#else |
| 45 |
#ifdef CHAR_IS_UNSIGNED |
| 46 |
typedef char U_CHAR; |
| 47 |
#define UCH(x) ((int) (x)) |
| 48 |
#else |
| 49 |
typedef char U_CHAR; |
| 50 |
#define UCH(x) ((int) (x) & 0xFF) |
| 51 |
#endif |
| 52 |
#endif |
| 53 |
|
| 54 |
|
| 55 |
#define ReadOK(file,buffer,len) (JFREAD(file,buffer,len) == ((size_t) (len))) |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
typedef struct { |
| 72 |
struct cjpeg_source_struct pub; |
| 73 |
|
| 74 |
U_CHAR *iobuffer; |
| 75 |
JSAMPROW pixrow; |
| 76 |
size_t buffer_width; |
| 77 |
JSAMPLE *rescale; |
| 78 |
} ppm_source_struct; |
| 79 |
|
| 80 |
typedef ppm_source_struct * ppm_source_ptr; |
| 81 |
|
| 82 |
|
| 83 |
LOCAL(int) |
| 84 |
pbm_getc (FILE * infile) |
| 85 |
|
| 86 |
|
| 87 |
{ |
| 88 |
register int ch; |
| 89 |
|
| 90 |
ch = getc(infile); |
| 91 |
if (ch == '#') { |
| 92 |
do { |
| 93 |
ch = getc(infile); |
| 94 |
} while (ch != '\n' && ch != EOF); |
| 95 |
} |
| 96 |
return ch; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
LOCAL(unsigned int) |
| 101 |
read_pbm_integer (j_compress_ptr cinfo, FILE * infile) |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
{ |
| 107 |
register int ch; |
| 108 |
register unsigned int val; |
| 109 |
|
| 110 |
|
| 111 |
do { |
| 112 |
ch = pbm_getc(infile); |
| 113 |
if (ch == EOF) |
| 114 |
ERREXIT(cinfo, JERR_INPUT_EOF); |
| 115 |
} while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r'); |
| 116 |
|
| 117 |
if (ch < '0' || ch > '9') |
| 118 |
ERREXIT(cinfo, JERR_PPM_NONNUMERIC); |
| 119 |
|
| 120 |
val = ch - '0'; |
| 121 |
while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') { |
| 122 |
val *= 10; |
| 123 |
val += ch - '0'; |
| 124 |
} |
| 125 |
return val; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
METHODDEF(JDIMENSION) |
| 141 |
get_text_gray_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
| 142 |
|
| 143 |
{ |
| 144 |
ppm_source_ptr source = (ppm_source_ptr) sinfo; |
| 145 |
FILE * infile = source->pub.input_file; |
| 146 |
register JSAMPROW ptr; |
| 147 |
register JSAMPLE *rescale = source->rescale; |
| 148 |
JDIMENSION col; |
| 149 |
|
| 150 |
ptr = source->pub.buffer[0]; |
| 151 |
for (col = cinfo->image_width; col > 0; col--) { |
| 152 |
*ptr++ = rescale[read_pbm_integer(cinfo, infile)]; |
| 153 |
} |
| 154 |
return 1; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
METHODDEF(JDIMENSION) |
| 159 |
get_text_rgb_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
| 160 |
|
| 161 |
{ |
| 162 |
ppm_source_ptr source = (ppm_source_ptr) sinfo; |
| 163 |
FILE * infile = source->pub.input_file; |
| 164 |
register JSAMPROW ptr; |
| 165 |
register JSAMPLE *rescale = source->rescale; |
| 166 |
JDIMENSION col; |
| 167 |
|
| 168 |
ptr = source->pub.buffer[0]; |
| 169 |
for (col = cinfo->image_width; col > 0; col--) { |
| 170 |
*ptr++ = rescale[read_pbm_integer(cinfo, infile)]; |
| 171 |
*ptr++ = rescale[read_pbm_integer(cinfo, infile)]; |
| 172 |
*ptr++ = rescale[read_pbm_integer(cinfo, infile)]; |
| 173 |
} |
| 174 |
return 1; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
METHODDEF(JDIMENSION) |
| 179 |
get_scaled_gray_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
| 180 |
|
| 181 |
{ |
| 182 |
ppm_source_ptr source = (ppm_source_ptr) sinfo; |
| 183 |
register JSAMPROW ptr; |
| 184 |
register U_CHAR * bufferptr; |
| 185 |
register JSAMPLE *rescale = source->rescale; |
| 186 |
JDIMENSION col; |
| 187 |
|
| 188 |
if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
| 189 |
ERREXIT(cinfo, JERR_INPUT_EOF); |
| 190 |
ptr = source->pub.buffer[0]; |
| 191 |
bufferptr = source->iobuffer; |
| 192 |
for (col = cinfo->image_width; col > 0; col--) { |
| 193 |
*ptr++ = rescale[UCH(*bufferptr++)]; |
| 194 |
} |
| 195 |
return 1; |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
METHODDEF(JDIMENSION) |
| 200 |
get_scaled_rgb_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
| 201 |
|
| 202 |
{ |
| 203 |
ppm_source_ptr source = (ppm_source_ptr) sinfo; |
| 204 |
register JSAMPROW ptr; |
| 205 |
register U_CHAR * bufferptr; |
| 206 |
register JSAMPLE *rescale = source->rescale; |
| 207 |
JDIMENSION col; |
| 208 |
|
| 209 |
if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
| 210 |
ERREXIT(cinfo, JERR_INPUT_EOF); |
| 211 |
ptr = source->pub.buffer[0]; |
| 212 |
bufferptr = source->iobuffer; |
| 213 |
for (col = cinfo->image_width; col > 0; col--) { |
| 214 |
*ptr++ = rescale[UCH(*bufferptr++)]; |
| 215 |
*ptr++ = rescale[UCH(*bufferptr++)]; |
| 216 |
*ptr++ = rescale[UCH(*bufferptr++)]; |
| 217 |
} |
| 218 |
return 1; |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
METHODDEF(JDIMENSION) |
| 223 |
get_raw_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
{ |
| 229 |
ppm_source_ptr source = (ppm_source_ptr) sinfo; |
| 230 |
|
| 231 |
if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
| 232 |
ERREXIT(cinfo, JERR_INPUT_EOF); |
| 233 |
return 1; |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
METHODDEF(JDIMENSION) |
| 238 |
get_word_gray_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
| 239 |
|
| 240 |
{ |
| 241 |
ppm_source_ptr source = (ppm_source_ptr) sinfo; |
| 242 |
register JSAMPROW ptr; |
| 243 |
register U_CHAR * bufferptr; |
| 244 |
register JSAMPLE *rescale = source->rescale; |
| 245 |
JDIMENSION col; |
| 246 |
|
| 247 |
if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
| 248 |
ERREXIT(cinfo, JERR_INPUT_EOF); |
| 249 |
ptr = source->pub.buffer[0]; |
| 250 |
bufferptr = source->iobuffer; |
| 251 |
for (col = cinfo->image_width; col > 0; col--) { |
| 252 |
register int temp; |
| 253 |
temp = UCH(*bufferptr++); |
| 254 |
temp |= UCH(*bufferptr++) << 8; |
| 255 |
*ptr++ = rescale[temp]; |
| 256 |
} |
| 257 |
return 1; |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
METHODDEF(JDIMENSION) |
| 262 |
get_word_rgb_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
| 263 |
|
| 264 |
{ |
| 265 |
ppm_source_ptr source = (ppm_source_ptr) sinfo; |
| 266 |
register JSAMPROW ptr; |
| 267 |
register U_CHAR * bufferptr; |
| 268 |
register JSAMPLE *rescale = source->rescale; |
| 269 |
JDIMENSION col; |
| 270 |
|
| 271 |
if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
| 272 |
ERREXIT(cinfo, JERR_INPUT_EOF); |
| 273 |
ptr = source->pub.buffer[0]; |
| 274 |
bufferptr = source->iobuffer; |
| 275 |
for (col = cinfo->image_width; col > 0; col--) { |
| 276 |
register int temp; |
| 277 |
temp = UCH(*bufferptr++); |
| 278 |
temp |= UCH(*bufferptr++) << 8; |
| 279 |
*ptr++ = rescale[temp]; |
| 280 |
temp = UCH(*bufferptr++); |
| 281 |
temp |= UCH(*bufferptr++) << 8; |
| 282 |
*ptr++ = rescale[temp]; |
| 283 |
temp = UCH(*bufferptr++); |
| 284 |
temp |= UCH(*bufferptr++) << 8; |
| 285 |
*ptr++ = rescale[temp]; |
| 286 |
} |
| 287 |
return 1; |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
METHODDEF(void) |
| 296 |
start_input_ppm (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
| 297 |
{ |
| 298 |
ppm_source_ptr source = (ppm_source_ptr) sinfo; |
| 299 |
int c; |
| 300 |
unsigned int w, h, maxval; |
| 301 |
boolean need_iobuffer, use_raw_buffer, need_rescale; |
| 302 |
|
| 303 |
if (getc(source->pub.input_file) != 'P') |
| 304 |
ERREXIT(cinfo, JERR_PPM_NOT); |
| 305 |
|
| 306 |
c = getc(source->pub.input_file); |
| 307 |
|
| 308 |
|
| 309 |
switch (c) { |
| 310 |
case '2': |
| 311 |
case '3': |
| 312 |
case '5': |
| 313 |
case '6': |
| 314 |
break; |
| 315 |
default: |
| 316 |
ERREXIT(cinfo, JERR_PPM_NOT); |
| 317 |
break; |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
w = read_pbm_integer(cinfo, source->pub.input_file); |
| 322 |
h = read_pbm_integer(cinfo, source->pub.input_file); |
| 323 |
maxval = read_pbm_integer(cinfo, source->pub.input_file); |
| 324 |
|
| 325 |
if (w <= 0 || h <= 0 || maxval <= 0) |
| 326 |
ERREXIT(cinfo, JERR_PPM_NOT); |
| 327 |
|
| 328 |
cinfo->data_precision = BITS_IN_JSAMPLE; |
| 329 |
cinfo->image_width = (JDIMENSION) w; |
| 330 |
cinfo->image_height = (JDIMENSION) h; |
| 331 |
|
| 332 |
|
| 333 |
need_iobuffer = TRUE; |
| 334 |
use_raw_buffer = FALSE; |
| 335 |
need_rescale = TRUE; |
| 336 |
|
| 337 |
switch (c) { |
| 338 |
case '2': |
| 339 |
cinfo->input_components = 1; |
| 340 |
cinfo->in_color_space = JCS_GRAYSCALE; |
| 341 |
TRACEMS2(cinfo, 1, JTRC_PGM_TEXT, w, h); |
| 342 |
source->pub.get_pixel_rows = get_text_gray_row; |
| 343 |
need_iobuffer = FALSE; |
| 344 |
break; |
| 345 |
|
| 346 |
case '3': |
| 347 |
cinfo->input_components = 3; |
| 348 |
cinfo->in_color_space = JCS_RGB; |
| 349 |
TRACEMS2(cinfo, 1, JTRC_PPM_TEXT, w, h); |
| 350 |
source->pub.get_pixel_rows = get_text_rgb_row; |
| 351 |
need_iobuffer = FALSE; |
| 352 |
break; |
| 353 |
|
| 354 |
case '5': |
| 355 |
cinfo->input_components = 1; |
| 356 |
cinfo->in_color_space = JCS_GRAYSCALE; |
| 357 |
TRACEMS2(cinfo, 1, JTRC_PGM, w, h); |
| 358 |
if (maxval > 255) { |
| 359 |
source->pub.get_pixel_rows = get_word_gray_row; |
| 360 |
} else if (maxval == MAXJSAMPLE && SIZEOF(JSAMPLE) == SIZEOF(U_CHAR)) { |
| 361 |
source->pub.get_pixel_rows = get_raw_row; |
| 362 |
use_raw_buffer = TRUE; |
| 363 |
need_rescale = FALSE; |
| 364 |
} else { |
| 365 |
source->pub.get_pixel_rows = get_scaled_gray_row; |
| 366 |
} |
| 367 |
break; |
| 368 |
|
| 369 |
case '6': |
| 370 |
cinfo->input_components = 3; |
| 371 |
cinfo->in_color_space = JCS_RGB; |
| 372 |
TRACEMS2(cinfo, 1, JTRC_PPM, w, h); |
| 373 |
if (maxval > 255) { |
| 374 |
source->pub.get_pixel_rows = get_word_rgb_row; |
| 375 |
} else if (maxval == MAXJSAMPLE && SIZEOF(JSAMPLE) == SIZEOF(U_CHAR)) { |
| 376 |
source->pub.get_pixel_rows = get_raw_row; |
| 377 |
use_raw_buffer = TRUE; |
| 378 |
need_rescale = FALSE; |
| 379 |
} else { |
| 380 |
source->pub.get_pixel_rows = get_scaled_rgb_row; |
| 381 |
} |
| 382 |
break; |
| 383 |
} |
| 384 |
|
| 385 |
|
| 386 |
if (need_iobuffer) { |
| 387 |
source->buffer_width = (size_t) w * cinfo->input_components * |
| 388 |
((maxval<=255) ? SIZEOF(U_CHAR) : (2*SIZEOF(U_CHAR))); |
| 389 |
source->iobuffer = (U_CHAR *) |
| 390 |
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
| 391 |
source->buffer_width); |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
if (use_raw_buffer) { |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
source->pixrow = (JSAMPROW) source->iobuffer; |
| 400 |
source->pub.buffer = & source->pixrow; |
| 401 |
source->pub.buffer_height = 1; |
| 402 |
} else { |
| 403 |
|
| 404 |
source->pub.buffer = (*cinfo->mem->alloc_sarray) |
| 405 |
((j_common_ptr) cinfo, JPOOL_IMAGE, |
| 406 |
(JDIMENSION) w * cinfo->input_components, (JDIMENSION) 1); |
| 407 |
source->pub.buffer_height = 1; |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
if (need_rescale) { |
| 412 |
INT32 val, half_maxval; |
| 413 |
|
| 414 |
|
| 415 |
source->rescale = (JSAMPLE *) |
| 416 |
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
| 417 |
(size_t) (((long) maxval + 1L) * SIZEOF(JSAMPLE))); |
| 418 |
half_maxval = maxval / 2; |
| 419 |
for (val = 0; val <= (INT32) maxval; val++) { |
| 420 |
|
| 421 |
source->rescale[val] = (JSAMPLE) ((val*MAXJSAMPLE + half_maxval)/maxval); |
| 422 |
} |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
METHODDEF(void) |
| 432 |
finish_input_ppm (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
| 433 |
{ |
| 434 |
|
| 435 |
} |
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
GLOBAL(cjpeg_source_ptr) |
| 443 |
jinit_read_ppm (j_compress_ptr cinfo) |
| 444 |
{ |
| 445 |
ppm_source_ptr source; |
| 446 |
|
| 447 |
|
| 448 |
source = (ppm_source_ptr) |
| 449 |
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
| 450 |
SIZEOF(ppm_source_struct)); |
| 451 |
|
| 452 |
source->pub.start_input = start_input_ppm; |
| 453 |
source->pub.finish_input = finish_input_ppm; |
| 454 |
|
| 455 |
return (cjpeg_source_ptr) source; |
| 456 |
} |
| 457 |
|
| 458 |
#endif |