| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
#ifdef __WIN32__ |
| 28 |
#define WIN32_LEAN_AND_MEAN |
| 29 |
#include <windows.h> |
| 30 |
#define WIN32_LEAN_AND_MEAN |
| 31 |
#endif |
| 32 |
|
| 33 |
#include <assert.h> |
| 34 |
#include <setjmp.h> |
| 35 |
#include <stdio.h> |
| 36 |
#include "jpeglib.h" |
| 37 |
#include "jerror.h" |
| 38 |
|
| 39 |
#include "tiffiop.h" |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
#undef EXTERN |
| 55 |
#include "imgInt.h" |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
#define SETJMP(jbuf) setjmp(jbuf) |
| 62 |
#define LONGJMP(jbuf,code) longjmp(jbuf,code) |
| 63 |
#define JMP_BUF jmp_buf |
| 64 |
|
| 65 |
typedef struct jpeg_destination_mgr jpeg_destination_mgr; |
| 66 |
typedef struct jpeg_source_mgr jpeg_source_mgr; |
| 67 |
typedef struct jpeg_error_mgr jpeg_error_mgr; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
typedef struct { |
| 83 |
union { |
| 84 |
struct jpeg_compress_struct c; |
| 85 |
struct jpeg_decompress_struct d; |
| 86 |
struct jpeg_common_struct comm; |
| 87 |
} cinfo; |
| 88 |
jpeg_error_mgr err; |
| 89 |
JMP_BUF exit_jmpbuf; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
jpeg_destination_mgr dest; |
| 95 |
jpeg_source_mgr src; |
| 96 |
|
| 97 |
TIFF* tif; |
| 98 |
uint16 photometric; |
| 99 |
uint16 h_sampling; |
| 100 |
uint16 v_sampling; |
| 101 |
tsize_t bytesperline; |
| 102 |
|
| 103 |
JSAMPARRAY ds_buffer[MAX_COMPONENTS]; |
| 104 |
int scancount; |
| 105 |
int samplesperclump; |
| 106 |
|
| 107 |
TIFFVGetMethod vgetparent; |
| 108 |
TIFFVSetMethod vsetparent; |
| 109 |
TIFFStripMethod defsparent; |
| 110 |
TIFFTileMethod deftparent; |
| 111 |
|
| 112 |
void* jpegtables; |
| 113 |
uint32 jpegtables_length; |
| 114 |
int jpegquality; |
| 115 |
int jpegcolormode; |
| 116 |
int jpegtablesmode; |
| 117 |
} JPEGState; |
| 118 |
|
| 119 |
#define JState(tif) ((JPEGState*)(tif)->tif_data) |
| 120 |
|
| 121 |
static int JPEGDecode _ANSI_ARGS_((TIFF*, tidata_t, tsize_t, tsample_t)); |
| 122 |
static int JPEGDecodeRaw _ANSI_ARGS_((TIFF*, tidata_t, tsize_t, tsample_t)); |
| 123 |
static int JPEGEncode _ANSI_ARGS_((TIFF*, tidata_t, tsize_t, tsample_t)); |
| 124 |
static int JPEGEncodeRaw _ANSI_ARGS_((TIFF*, tidata_t, tsize_t, tsample_t)); |
| 125 |
|
| 126 |
void ImgTIFFfree _ANSI_ARGS_((tdata_t data)); |
| 127 |
tdata_t ImgTIFFmalloc _ANSI_ARGS_((tsize_t size)); |
| 128 |
tdata_t ImgTIFFrealloc _ANSI_ARGS_((tdata_t data, tsize_t size)); |
| 129 |
tdata_t ImgTIFFmemcpy _ANSI_ARGS_((tdata_t, tdata_t, tsize_t)); |
| 130 |
void ImgTIFFError _ANSI_ARGS_(TCL_VARARGS(CONST char *, module)); |
| 131 |
int ImgTIFFPredictorInit _ANSI_ARGS_((TIFF *tif)); |
| 132 |
void ImgTIFFMergeFieldInfo _ANSI_ARGS_((TIFF* tif, CONST VOID *voidp, int i)); |
| 133 |
int ImgTIFFFlushData1 _ANSI_ARGS_((TIFF *tif)); |
| 134 |
void ImgTIFFNoPostDecode _ANSI_ARGS_((TIFF *, tidata_t, tsize_t)); |
| 135 |
tsize_t ImgTIFFTileRowSize _ANSI_ARGS_((TIFF *)); |
| 136 |
int ImgTIFFScanlineSize _ANSI_ARGS_((TIFF *)); |
| 137 |
void ImgTIFFsetByteArray _ANSI_ARGS_((VOID **, VOID*, long)); |
| 138 |
int ImgTIFFSetField _ANSI_ARGS_(TCL_VARARGS(TIFF *, tif)); |
| 139 |
tsize_t ImgTIFFTileSize _ANSI_ARGS_((TIFF*)); |
| 140 |
|
| 141 |
boolean Imgjpeg_resync_to_restart _ANSI_ARGS_((j_decompress_ptr, int)); |
| 142 |
JDIMENSION Imgjpeg_read_scanlines _ANSI_ARGS_((j_decompress_ptr, |
| 143 |
JSAMPARRAY, JDIMENSION)); |
| 144 |
int Imgjpeg_set_colorspace _ANSI_ARGS_((j_compress_ptr, J_COLOR_SPACE)); |
| 145 |
int Imgjpeg_set_defaults _ANSI_ARGS_((j_compress_ptr)); |
| 146 |
int Imgjpeg_start_decompress _ANSI_ARGS_((j_decompress_ptr)); |
| 147 |
void Imgjpeg_destroy _ANSI_ARGS_((j_common_ptr)); |
| 148 |
struct jpeg_error_mgr * Imgjpeg_std_error _ANSI_ARGS_((struct jpeg_error_mgr *)); |
| 149 |
int Imgjpeg_CreateDecompress _ANSI_ARGS_((j_decompress_ptr, int, size_t)); |
| 150 |
JDIMENSION Imgjpeg_write_raw_data _ANSI_ARGS_((j_compress_ptr, |
| 151 |
JSAMPIMAGE, JDIMENSION)); |
| 152 |
void Imgjpeg_suppress_tables _ANSI_ARGS_((j_compress_ptr, boolean)); |
| 153 |
void Imgjpeg_abort _ANSI_ARGS_((j_common_ptr)); |
| 154 |
int Imgjpeg_read_header _ANSI_ARGS_((j_decompress_ptr, int)); |
| 155 |
int Imgjpeg_start_compress _ANSI_ARGS_((j_compress_ptr, int)); |
| 156 |
void Imgjpeg_write_tables _ANSI_ARGS_((j_compress_ptr)); |
| 157 |
int Imgjpeg_finish_decompress _ANSI_ARGS_((j_decompress_ptr)); |
| 158 |
int Imgjpeg_CreateCompress _ANSI_ARGS_((j_compress_ptr, int, size_t)); |
| 159 |
int Imgjpeg_finish_compress _ANSI_ARGS_((j_compress_ptr)); |
| 160 |
JDIMENSION Imgjpeg_read_raw_data _ANSI_ARGS_((j_decompress_ptr, |
| 161 |
JSAMPIMAGE, JDIMENSION)); |
| 162 |
int Imgjpeg_set_quality _ANSI_ARGS_((j_compress_ptr, int, int)); |
| 163 |
JDIMENSION Imgjpeg_write_scanlines _ANSI_ARGS_((j_compress_ptr, |
| 164 |
JSAMPARRAY, JDIMENSION)); |
| 165 |
|
| 166 |
#define FIELD_JPEGTABLES (FIELD_CODEC+0) |
| 167 |
|
| 168 |
static const TIFFFieldInfo jpegFieldInfo[] = { |
| 169 |
{ TIFFTAG_JPEGTABLES, -1,-1, TIFF_UNDEFINED, FIELD_JPEGTABLES, |
| 170 |
FALSE, TRUE, "JPEGTables" }, |
| 171 |
{ TIFFTAG_JPEGQUALITY, 0, 0, TIFF_ANY, FIELD_PSEUDO, |
| 172 |
TRUE, FALSE, "" }, |
| 173 |
{ TIFFTAG_JPEGCOLORMODE, 0, 0, TIFF_ANY, FIELD_PSEUDO, |
| 174 |
FALSE, FALSE, "" }, |
| 175 |
{ TIFFTAG_JPEGTABLESMODE, 0, 0, TIFF_ANY, FIELD_PSEUDO, |
| 176 |
FALSE, FALSE, "" }, |
| 177 |
}; |
| 178 |
#define N(a) (sizeof (a) / sizeof (a[0])) |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
static void TIFFjpeg_error_exit _ANSI_ARGS_((j_common_ptr)); |
| 196 |
|
| 197 |
static void |
| 198 |
TIFFjpeg_error_exit(cinfo) |
| 199 |
j_common_ptr cinfo; |
| 200 |
{ |
| 201 |
JPEGState *sp = (JPEGState *) cinfo; |
| 202 |
char buffer[JMSG_LENGTH_MAX]; |
| 203 |
|
| 204 |
(*cinfo->err->format_message) (cinfo, buffer); |
| 205 |
ImgTIFFError("JPEGLib", buffer); |
| 206 |
Imgjpeg_abort(cinfo); |
| 207 |
LONGJMP(sp->exit_jmpbuf, 1); |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
static void TIFFjpeg_output_message _ANSI_ARGS_((j_common_ptr)); |
| 217 |
static void |
| 218 |
TIFFjpeg_output_message(cinfo) |
| 219 |
j_common_ptr cinfo; |
| 220 |
{ |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
#define CALLJPEG(sp, fail, op) (SETJMP((sp)->exit_jmpbuf) ? (fail) : (op)) |
| 230 |
#define CALLVJPEG(sp, op) CALLJPEG(sp, 0, ((op),1)) |
| 231 |
|
| 232 |
|
| 233 |
static int TIFFjpeg_create_compress _ANSI_ARGS_((JPEGState*)); |
| 234 |
|
| 235 |
static int |
| 236 |
TIFFjpeg_create_compress(sp) |
| 237 |
JPEGState* sp; |
| 238 |
{ |
| 239 |
|
| 240 |
sp->cinfo.c.err = Imgjpeg_std_error(&sp->err); |
| 241 |
sp->err.error_exit = TIFFjpeg_error_exit; |
| 242 |
sp->err.output_message = TIFFjpeg_output_message; |
| 243 |
|
| 244 |
return CALLVJPEG(sp, Imgjpeg_CreateCompress(&sp->cinfo.c, JPEG_LIB_VERSION, |
| 245 |
(size_t) sizeof(struct jpeg_compress_struct))); |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
static int TIFFjpeg_create_decompress _ANSI_ARGS_((JPEGState*)); |
| 250 |
|
| 251 |
static int |
| 252 |
TIFFjpeg_create_decompress(sp) |
| 253 |
JPEGState* sp; |
| 254 |
{ |
| 255 |
|
| 256 |
sp->cinfo.d.err = Imgjpeg_std_error(&sp->err); |
| 257 |
sp->err.error_exit = TIFFjpeg_error_exit; |
| 258 |
sp->err.output_message = TIFFjpeg_output_message; |
| 259 |
|
| 260 |
return CALLVJPEG(sp, Imgjpeg_CreateDecompress(&sp->cinfo.d, JPEG_LIB_VERSION, |
| 261 |
(size_t) sizeof(struct jpeg_decompress_struct))); |
| 262 |
} |
| 263 |
|
| 264 |
static int TIFFjpeg_set_defaults _ANSI_ARGS_((JPEGState*)); |
| 265 |
|
| 266 |
static int |
| 267 |
TIFFjpeg_set_defaults(sp) |
| 268 |
JPEGState* sp; |
| 269 |
{ |
| 270 |
return CALLVJPEG(sp, Imgjpeg_set_defaults(&sp->cinfo.c)); |
| 271 |
} |
| 272 |
|
| 273 |
static int TIFFjpeg_set_colorspace _ANSI_ARGS_((JPEGState*, J_COLOR_SPACE)); |
| 274 |
|
| 275 |
static int |
| 276 |
TIFFjpeg_set_colorspace(sp, colorspace) |
| 277 |
JPEGState* sp; |
| 278 |
J_COLOR_SPACE colorspace; |
| 279 |
{ |
| 280 |
return CALLVJPEG(sp, Imgjpeg_set_colorspace(&sp->cinfo.c, colorspace)); |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
static int TIFFjpeg_set_quality _ANSI_ARGS_((JPEGState*, int, boolean)); |
| 285 |
static int |
| 286 |
TIFFjpeg_set_quality(sp, quality, force_baseline) |
| 287 |
JPEGState* sp; |
| 288 |
int quality; |
| 289 |
boolean force_baseline; |
| 290 |
{ |
| 291 |
return CALLVJPEG(sp, |
| 292 |
Imgjpeg_set_quality(&sp->cinfo.c, quality, force_baseline)); |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
static int TIFFjpeg_suppress_tables _ANSI_ARGS_((JPEGState*, boolean)); |
| 297 |
static int |
| 298 |
TIFFjpeg_suppress_tables(sp, suppress) |
| 299 |
JPEGState* sp; |
| 300 |
boolean suppress; |
| 301 |
{ |
| 302 |
return CALLVJPEG(sp, Imgjpeg_suppress_tables(&sp->cinfo.c, suppress)); |
| 303 |
} |
| 304 |
|
| 305 |
static int TIFFjpeg_start_compress _ANSI_ARGS_((JPEGState*, boolean)); |
| 306 |
static int |
| 307 |
TIFFjpeg_start_compress(sp, write_all_tables) |
| 308 |
JPEGState* sp; |
| 309 |
boolean write_all_tables; |
| 310 |
{ |
| 311 |
return CALLVJPEG(sp, |
| 312 |
Imgjpeg_start_compress(&sp->cinfo.c, write_all_tables)); |
| 313 |
} |
| 314 |
|
| 315 |
static int TIFFjpeg_write_scanlines _ANSI_ARGS_((JPEGState*, JSAMPARRAY, int)); |
| 316 |
static int |
| 317 |
TIFFjpeg_write_scanlines(sp, scanlines, num_lines) |
| 318 |
JPEGState* sp; |
| 319 |
JSAMPARRAY scanlines; |
| 320 |
int num_lines; |
| 321 |
{ |
| 322 |
return CALLJPEG(sp, -1, (int) Imgjpeg_write_scanlines(&sp->cinfo.c, |
| 323 |
scanlines, (JDIMENSION) num_lines)); |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
static int TIFFjpeg_write_raw_data _ANSI_ARGS_((JPEGState*, JSAMPIMAGE, int)); |
| 328 |
static int |
| 329 |
TIFFjpeg_write_raw_data(sp, data, num_lines) |
| 330 |
JPEGState* sp; |
| 331 |
JSAMPIMAGE data; |
| 332 |
int num_lines; |
| 333 |
{ |
| 334 |
return CALLJPEG(sp, -1, (int) Imgjpeg_write_raw_data(&sp->cinfo.c, |
| 335 |
data, (JDIMENSION) num_lines)); |
| 336 |
} |
| 337 |
|
| 338 |
static int TIFFjpeg_finish_compress _ANSI_ARGS_((JPEGState* sp)); |
| 339 |
static int |
| 340 |
TIFFjpeg_finish_compress(sp) |
| 341 |
JPEGState* sp; |
| 342 |
{ |
| 343 |
return CALLVJPEG(sp, Imgjpeg_finish_compress(&sp->cinfo.c)); |
| 344 |
} |
| 345 |
|
| 346 |
static int TIFFjpeg_write_tables _ANSI_ARGS_((JPEGState*)); |
| 347 |
static int |
| 348 |
TIFFjpeg_write_tables(sp) |
| 349 |
JPEGState* sp; |
| 350 |
{ |
| 351 |
return CALLVJPEG(sp, Imgjpeg_write_tables(&sp->cinfo.c)); |
| 352 |
} |
| 353 |
|
| 354 |
static int TIFFjpeg_read_header _ANSI_ARGS_((JPEGState*, boolean)); |
| 355 |
|
| 356 |
static int |
| 357 |
TIFFjpeg_read_header(sp, require_image) |
| 358 |
JPEGState* sp; |
| 359 |
boolean require_image; |
| 360 |
{ |
| 361 |
return CALLJPEG(sp, -1, Imgjpeg_read_header(&sp->cinfo.d, require_image)); |
| 362 |
} |
| 363 |
|
| 364 |
static int TIFFjpeg_start_decompress _ANSI_ARGS_((JPEGState*)); |
| 365 |
static int |
| 366 |
TIFFjpeg_start_decompress(sp) |
| 367 |
JPEGState* sp; |
| 368 |
{ |
| 369 |
return CALLVJPEG(sp, Imgjpeg_start_decompress(&sp->cinfo.d)); |
| 370 |
} |
| 371 |
|
| 372 |
static int TIFFjpeg_read_scanlines _ANSI_ARGS_((JPEGState*, JSAMPARRAY, int)); |
| 373 |
static int |
| 374 |
TIFFjpeg_read_scanlines(sp, scanlines, max_lines) |
| 375 |
JPEGState* sp; |
| 376 |
JSAMPARRAY scanlines; |
| 377 |
int max_lines; |
| 378 |
{ |
| 379 |
return CALLJPEG(sp, -1, (int) Imgjpeg_read_scanlines(&sp->cinfo.d, |
| 380 |
scanlines, (JDIMENSION) max_lines)); |
| 381 |
} |
| 382 |
|
| 383 |
static int TIFFjpeg_read_raw_data _ANSI_ARGS_((JPEGState*, JSAMPIMAGE, int)); |
| 384 |
static int |
| 385 |
TIFFjpeg_read_raw_data(sp, data, max_lines) |
| 386 |
JPEGState* sp; |
| 387 |
JSAMPIMAGE data; |
| 388 |
int max_lines; |
| 389 |
{ |
| 390 |
return CALLJPEG(sp, -1, (int) Imgjpeg_read_raw_data(&sp->cinfo.d, |
| 391 |
data, (JDIMENSION) max_lines)); |
| 392 |
} |
| 393 |
|
| 394 |
static int TIFFjpeg_finish_decompress _ANSI_ARGS_((JPEGState*)); |
| 395 |
static int |
| 396 |
TIFFjpeg_finish_decompress(sp) |
| 397 |
JPEGState* sp; |
| 398 |
{ |
| 399 |
return CALLJPEG(sp, -1, (int) Imgjpeg_finish_decompress(&sp->cinfo.d)); |
| 400 |
} |
| 401 |
|
| 402 |
static int TIFFjpeg_abort _ANSI_ARGS_((JPEGState*)); |
| 403 |
static int |
| 404 |
TIFFjpeg_abort(sp) |
| 405 |
JPEGState* sp; |
| 406 |
{ |
| 407 |
return CALLVJPEG(sp, Imgjpeg_abort(&sp->cinfo.comm)); |
| 408 |
} |
| 409 |
|
| 410 |
static int TIFFjpeg_destroy _ANSI_ARGS_((JPEGState*)); |
| 411 |
static int |
| 412 |
TIFFjpeg_destroy(sp) |
| 413 |
JPEGState* sp; |
| 414 |
{ |
| 415 |
return CALLVJPEG(sp, Imgjpeg_destroy(&sp->cinfo.comm)); |
| 416 |
} |
| 417 |
|
| 418 |
static JSAMPARRAY TIFFjpeg_alloc_sarray _ANSI_ARGS_((JPEGState*, |
| 419 |
int, JDIMENSION, JDIMENSION)); |
| 420 |
static JSAMPARRAY |
| 421 |
TIFFjpeg_alloc_sarray(sp, pool_id, samplesperrow, numrows) |
| 422 |
JPEGState* sp; |
| 423 |
int pool_id; |
| 424 |
JDIMENSION samplesperrow; |
| 425 |
JDIMENSION numrows; |
| 426 |
{ |
| 427 |
return CALLJPEG(sp, (JSAMPARRAY) NULL, |
| 428 |
(*sp->cinfo.comm.mem->alloc_sarray) |
| 429 |
(&sp->cinfo.comm, pool_id, samplesperrow, numrows)); |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
static void std_init_destination _ANSI_ARGS_((j_compress_ptr)); |
| 439 |
static void |
| 440 |
std_init_destination(cinfo) |
| 441 |
j_compress_ptr cinfo; |
| 442 |
{ |
| 443 |
JPEGState* sp = (JPEGState*) cinfo; |
| 444 |
TIFF* tif = sp->tif; |
| 445 |
|
| 446 |
sp->dest.next_output_byte = (JOCTET*) tif->tif_rawdata; |
| 447 |
sp->dest.free_in_buffer = (size_t) tif->tif_rawdatasize; |
| 448 |
} |
| 449 |
|
| 450 |
static boolean std_empty_output_buffer _ANSI_ARGS_((j_compress_ptr)); |
| 451 |
static boolean |
| 452 |
std_empty_output_buffer(cinfo) |
| 453 |
j_compress_ptr cinfo; |
| 454 |
{ |
| 455 |
JPEGState* sp = (JPEGState*) cinfo; |
| 456 |
TIFF* tif = sp->tif; |
| 457 |
|
| 458 |
|
| 459 |
tif->tif_rawcc = tif->tif_rawdatasize; |
| 460 |
ImgTIFFFlushData1(tif); |
| 461 |
sp->dest.next_output_byte = (JOCTET*) tif->tif_rawdata; |
| 462 |
sp->dest.free_in_buffer = (size_t) tif->tif_rawdatasize; |
| 463 |
|
| 464 |
return (TRUE); |
| 465 |
} |
| 466 |
|
| 467 |
static void std_term_destination _ANSI_ARGS_((j_compress_ptr)); |
| 468 |
static void |
| 469 |
std_term_destination(cinfo) |
| 470 |
j_compress_ptr cinfo; |
| 471 |
{ |
| 472 |
JPEGState* sp = (JPEGState*) cinfo; |
| 473 |
TIFF* tif = sp->tif; |
| 474 |
|
| 475 |
tif->tif_rawcp = (tidata_t) sp->dest.next_output_byte; |
| 476 |
tif->tif_rawcc = |
| 477 |
tif->tif_rawdatasize - (tsize_t) sp->dest.free_in_buffer; |
| 478 |
|
| 479 |
} |
| 480 |
|
| 481 |
static void TIFFjpeg_data_dest _ANSI_ARGS_((JPEGState*, TIFF*)); |
| 482 |
static void |
| 483 |
TIFFjpeg_data_dest(sp, tif) |
| 484 |
JPEGState* sp; |
| 485 |
TIFF* tif; |
| 486 |
{ |
| 487 |
(void) tif; |
| 488 |
sp->cinfo.c.dest = &sp->dest; |
| 489 |
sp->dest.init_destination = std_init_destination; |
| 490 |
sp->dest.empty_output_buffer = std_empty_output_buffer; |
| 491 |
sp->dest.term_destination = std_term_destination; |
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
static void tables_init_destination _ANSI_ARGS_((j_compress_ptr)); |
| 499 |
static void |
| 500 |
tables_init_destination(cinfo) |
| 501 |
j_compress_ptr cinfo; |
| 502 |
{ |
| 503 |
JPEGState* sp = (JPEGState*) cinfo; |
| 504 |
|
| 505 |
|
| 506 |
sp->dest.next_output_byte = (JOCTET*) sp->jpegtables; |
| 507 |
sp->dest.free_in_buffer = (size_t) sp->jpegtables_length; |
| 508 |
} |
| 509 |
|
| 510 |
static boolean tables_empty_output_buffer _ANSI_ARGS_((j_compress_ptr)); |
| 511 |
static boolean |
| 512 |
tables_empty_output_buffer(cinfo) |
| 513 |
j_compress_ptr cinfo; |
| 514 |
{ |
| 515 |
JPEGState* sp = (JPEGState*) cinfo; |
| 516 |
void* newbuf; |
| 517 |
|
| 518 |
|
| 519 |
newbuf = ImgTIFFrealloc((tdata_t) sp->jpegtables, |
| 520 |
(tsize_t) (sp->jpegtables_length + 1000)); |
| 521 |
if (newbuf == NULL) |
| 522 |
ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 100); |
| 523 |
sp->dest.next_output_byte = (JOCTET*) newbuf + sp->jpegtables_length; |
| 524 |
sp->dest.free_in_buffer = (size_t) 1000; |
| 525 |
sp->jpegtables = newbuf; |
| 526 |
sp->jpegtables_length += 1000; |
| 527 |
return (TRUE); |
| 528 |
} |
| 529 |
|
| 530 |
static void tables_term_destination _ANSI_ARGS_((j_compress_ptr)); |
| 531 |
static void |
| 532 |
tables_term_destination(cinfo) |
| 533 |
j_compress_ptr cinfo; |
| 534 |
{ |
| 535 |
JPEGState* sp = (JPEGState*) cinfo; |
| 536 |
|
| 537 |
|
| 538 |
sp->jpegtables_length -= sp->dest.free_in_buffer; |
| 539 |
} |
| 540 |
|
| 541 |
static int TIFFjpeg_tables_dest _ANSI_ARGS_((JPEGState*, TIFF*)); |
| 542 |
static int |
| 543 |
TIFFjpeg_tables_dest(sp, tif) |
| 544 |
JPEGState* sp; |
| 545 |
TIFF* tif; |
| 546 |
{ |
| 547 |
(void) tif; |
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
if (sp->jpegtables) |
| 553 |
ImgTIFFfree(sp->jpegtables); |
| 554 |
sp->jpegtables_length = 1000; |
| 555 |
sp->jpegtables = (void*) ImgTIFFmalloc((tsize_t) sp->jpegtables_length); |
| 556 |
if (sp->jpegtables == NULL) { |
| 557 |
sp->jpegtables_length = 0; |
| 558 |
ImgTIFFError("TIFFjpeg_tables_dest", "No space for JPEGTables"); |
| 559 |
return (0); |
| 560 |
} |
| 561 |
sp->cinfo.c.dest = &sp->dest; |
| 562 |
sp->dest.init_destination = tables_init_destination; |
| 563 |
sp->dest.empty_output_buffer = tables_empty_output_buffer; |
| 564 |
sp->dest.term_destination = tables_term_destination; |
| 565 |
return (1); |
| 566 |
} |
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
static void std_init_source _ANSI_ARGS_((j_decompress_ptr)); |
| 574 |
static void |
| 575 |
std_init_source(cinfo) |
| 576 |
j_decompress_ptr cinfo; |
| 577 |
{ |
| 578 |
JPEGState* sp = (JPEGState*) cinfo; |
| 579 |
TIFF* tif = sp->tif; |
| 580 |
|
| 581 |
sp->src.next_input_byte = (const JOCTET*) tif->tif_rawdata; |
| 582 |
sp->src.bytes_in_buffer = (size_t) tif->tif_rawcc; |
| 583 |
} |
| 584 |
|
| 585 |
static boolean std_fill_input_buffer _ANSI_ARGS_((j_decompress_ptr)); |
| 586 |
static boolean |
| 587 |
std_fill_input_buffer(cinfo) |
| 588 |
j_decompress_ptr cinfo; |
| 589 |
{ |
| 590 |
JPEGState* sp = (JPEGState* ) cinfo; |
| 591 |
static const JOCTET dummy_EOI[2] = { 0xFF, JPEG_EOI }; |
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
WARNMS(cinfo, JWRN_JPEG_EOF); |
| 599 |
|
| 600 |
sp->src.next_input_byte = dummy_EOI; |
| 601 |
sp->src.bytes_in_buffer = 2; |
| 602 |
return (TRUE); |
| 603 |
} |
| 604 |
|
| 605 |
static void std_skip_input_data _ANSI_ARGS_((j_decompress_ptr, long)); |
| 606 |
static void |
| 607 |
std_skip_input_data(cinfo, num_bytes) |
| 608 |
j_decompress_ptr cinfo; |
| 609 |
long num_bytes; |
| 610 |
{ |
| 611 |
JPEGState* sp = (JPEGState*) cinfo; |
| 612 |
|
| 613 |
if (num_bytes > 0) { |
| 614 |
if (num_bytes > (long) sp->src.bytes_in_buffer) { |
| 615 |
|
| 616 |
(void) std_fill_input_buffer(cinfo); |
| 617 |
} else { |
| 618 |
sp->src.next_input_byte += (size_t) num_bytes; |
| 619 |
sp->src.bytes_in_buffer -= (size_t) num_bytes; |
| 620 |
} |
| 621 |
} |
| 622 |
} |
| 623 |
|
| 624 |
static void std_term_source _ANSI_ARGS_((j_decompress_ptr)); |
| 625 |
static void |
| 626 |
std_term_source(cinfo) |
| 627 |
j_decompress_ptr cinfo; |
| 628 |
{ |
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
(void) cinfo; |
| 633 |
} |
| 634 |
|
| 635 |
static void TIFFjpeg_data_src _ANSI_ARGS_((JPEGState*, TIFF*)); |
| 636 |
static void |
| 637 |
TIFFjpeg_data_src(sp, tif) |
| 638 |
JPEGState* sp; |
| 639 |
TIFF* tif; |
| 640 |
{ |
| 641 |
(void) tif; |
| 642 |
sp->cinfo.d.src = &sp->src; |
| 643 |
sp->src.init_source = std_init_source; |
| 644 |
sp->src.fill_input_buffer = std_fill_input_buffer; |
| 645 |
sp->src.skip_input_data = std_skip_input_data; |
| 646 |
sp->src.resync_to_restart = Imgjpeg_resync_to_restart; |
| 647 |
sp->src.term_source = std_term_source; |
| 648 |
sp->src.bytes_in_buffer = 0; |
| 649 |
sp->src.next_input_byte = NULL; |
| 650 |
} |
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
static void tables_init_source _ANSI_ARGS_((j_decompress_ptr)); |
| 658 |
static void |
| 659 |
tables_init_source(cinfo) |
| 660 |
j_decompress_ptr cinfo; |
| 661 |
{ |
| 662 |
JPEGState* sp = (JPEGState*) cinfo; |
| 663 |
|
| 664 |
sp->src.next_input_byte = (const JOCTET*) sp->jpegtables; |
| 665 |
sp->src.bytes_in_buffer = (size_t) sp->jpegtables_length; |
| 666 |
} |
| 667 |
|
| 668 |
static void TIFFjpeg_tables_src _ANSI_ARGS_((JPEGState*, TIFF*)); |
| 669 |
static void |
| 670 |
TIFFjpeg_tables_src(sp, tif) |
| 671 |
JPEGState* sp; |
| 672 |
TIFF* tif; |
| 673 |
{ |
| 674 |
TIFFjpeg_data_src(sp, tif); |
| 675 |
sp->src.init_source = tables_init_source; |
| 676 |
} |
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
static int alloc_downsampled_buffers _ANSI_ARGS_((TIFF*, |
| 687 |
jpeg_component_info*, int)); |
| 688 |
static int |
| 689 |
alloc_downsampled_buffers(tif, comp_info, num_components) |
| 690 |
TIFF* tif; |
| 691 |
jpeg_component_info* comp_info; |
| 692 |
int num_components; |
| 693 |
{ |
| 694 |
JPEGState* sp = JState(tif); |
| 695 |
int ci; |
| 696 |
jpeg_component_info* compptr; |
| 697 |
JSAMPARRAY buf; |
| 698 |
int samples_per_clump = 0; |
| 699 |
|
| 700 |
for (ci = 0, compptr = comp_info; ci < num_components; |
| 701 |
ci++, compptr++) { |
| 702 |
samples_per_clump += compptr->h_samp_factor * |
| 703 |
compptr->v_samp_factor; |
| 704 |
buf = TIFFjpeg_alloc_sarray(sp, JPOOL_IMAGE, |
| 705 |
compptr->width_in_blocks * DCTSIZE, |
| 706 |
(JDIMENSION) (compptr->v_samp_factor*DCTSIZE)); |
| 707 |
if (buf == NULL) |
| 708 |
return (0); |
| 709 |
sp->ds_buffer[ci] = buf; |
| 710 |
} |
| 711 |
sp->samplesperclump = samples_per_clump; |
| 712 |
|
| 713 |
|
| 714 |
sp->bytesperline = sizeof(JSAMPLE) * samples_per_clump * |
| 715 |
comp_info[1].downsampled_width; |
| 716 |
return (1); |
| 717 |
} |
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
static int JPEGSetupDecode _ANSI_ARGS_((TIFF* tif)); |
| 725 |
|
| 726 |
static int |
| 727 |
JPEGSetupDecode(tif) |
| 728 |
TIFF* tif; |
| 729 |
{ |
| 730 |
JPEGState* sp = JState(tif); |
| 731 |
TIFFDirectory *td = &tif->tif_dir; |
| 732 |
|
| 733 |
assert(sp != NULL); |
| 734 |
assert(sp->cinfo.comm.is_decompressor); |
| 735 |
|
| 736 |
|
| 737 |
if (TIFFFieldSet(tif,FIELD_JPEGTABLES)) { |
| 738 |
TIFFjpeg_tables_src(sp, tif); |
| 739 |
if(TIFFjpeg_read_header(sp,FALSE) != JPEG_HEADER_TABLES_ONLY) { |
| 740 |
ImgTIFFError("JPEGSetupDecode", "Bogus JPEGTables field"); |
| 741 |
return (0); |
| 742 |
} |
| 743 |
} |
| 744 |
|
| 745 |
|
| 746 |
sp->photometric = td->td_photometric; |
| 747 |
switch (sp->photometric) { |
| 748 |
case PHOTOMETRIC_YCBCR: |
| 749 |
sp->h_sampling = td->td_ycbcrsubsampling[0]; |
| 750 |
sp->v_sampling = td->td_ycbcrsubsampling[1]; |
| 751 |
break; |
| 752 |
default: |
| 753 |
|
| 754 |
sp->h_sampling = 1; |
| 755 |
sp->v_sampling = 1; |
| 756 |
break; |
| 757 |
} |
| 758 |
|
| 759 |
|
| 760 |
TIFFjpeg_data_src(sp, tif); |
| 761 |
tif->tif_postdecode = ImgTIFFNoPostDecode; |
| 762 |
return (1); |
| 763 |
} |
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
static int |
| 770 |
#ifdef _USING_PROTOTYPES_ |
| 771 |
JPEGPreDecode ( |
| 772 |
TIFF* tif, |
| 773 |
tsample_t s) |
| 774 |
#else |
| 775 |
JPEGPreDecode(tif, s) |
| 776 |
TIFF* tif; |
| 777 |
tsample_t s; |
| 778 |
#endif |
| 779 |
{ |
| 780 |
JPEGState *sp = JState(tif); |
| 781 |
TIFFDirectory *td = &tif->tif_dir; |
| 782 |
static char module[] = "JPEGPreDecode"; |
| 783 |
uint32 segment_width, segment_height; |
| 784 |
int downsampled_output; |
| 785 |
int ci; |
| 786 |
|
| 787 |
assert(sp != NULL); |
| 788 |
assert(sp->cinfo.comm.is_decompressor); |
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
if (!TIFFjpeg_abort(sp)) |
| 794 |
return (0); |
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
if (TIFFjpeg_read_header(sp, TRUE) != JPEG_HEADER_OK) |
| 799 |
return (0); |
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
if (isTiled(tif)) { |
| 804 |
segment_width = td->td_tilewidth; |
| 805 |
segment_height = td->td_tilelength; |
| 806 |
sp->bytesperline = ImgTIFFTileRowSize(tif); |
| 807 |
} else { |
| 808 |
segment_width = td->td_imagewidth; |
| 809 |
segment_height = td->td_imagelength - tif->tif_row; |
| 810 |
if (segment_height > td->td_rowsperstrip) |
| 811 |
segment_height = td->td_rowsperstrip; |
| 812 |
sp->bytesperline = ImgTIFFScanlineSize(tif); |
| 813 |
} |
| 814 |
if (td->td_planarconfig == PLANARCONFIG_SEPARATE && s > 0) { |
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
segment_width = TIFFhowmany(segment_width, sp->h_sampling); |
| 820 |
segment_height = TIFFhowmany(segment_height, sp->v_sampling); |
| 821 |
} |
| 822 |
if (sp->cinfo.d.image_width != segment_width || |
| 823 |
sp->cinfo.d.image_height != segment_height) { |
| 824 |
ImgTIFFError(module, "Improper JPEG strip/tile size"); |
| 825 |
return (0); |
| 826 |
} |
| 827 |
if (sp->cinfo.d.num_components != |
| 828 |
(td->td_planarconfig == PLANARCONFIG_CONTIG ? |
| 829 |
td->td_samplesperpixel : 1)) { |
| 830 |
ImgTIFFError(module, "Improper JPEG component count"); |
| 831 |
return (0); |
| 832 |
} |
| 833 |
if (sp->cinfo.d.data_precision != td->td_bitspersample) { |
| 834 |
ImgTIFFError(module, "Improper JPEG data precision"); |
| 835 |
return (0); |
| 836 |
} |
| 837 |
if (td->td_planarconfig == PLANARCONFIG_CONTIG) { |
| 838 |
|
| 839 |
if (sp->cinfo.d.comp_info[0].h_samp_factor != sp->h_sampling || |
| 840 |
sp->cinfo.d.comp_info[0].v_samp_factor != sp->v_sampling) { |
| 841 |
ImgTIFFError(module, "Improper JPEG sampling factors"); |
| 842 |
return (0); |
| 843 |
} |
| 844 |
|
| 845 |
for (ci = 1; ci < sp->cinfo.d.num_components; ci++) { |
| 846 |
if (sp->cinfo.d.comp_info[ci].h_samp_factor != 1 || |
| 847 |
sp->cinfo.d.comp_info[ci].v_samp_factor != 1) { |
| 848 |
ImgTIFFError(module, "Improper JPEG sampling factors"); |
| 849 |
return (0); |
| 850 |
} |
| 851 |
} |
| 852 |
} else { |
| 853 |
|
| 854 |
if (sp->cinfo.d.comp_info[0].h_samp_factor != 1 || |
| 855 |
sp->cinfo.d.comp_info[0].v_samp_factor != 1) { |
| 856 |
ImgTIFFError(module, "Improper JPEG sampling factors"); |
| 857 |
return (0); |
| 858 |
} |
| 859 |
} |
| 860 |
downsampled_output = FALSE; |
| 861 |
if (td->td_planarconfig == PLANARCONFIG_CONTIG && |
| 862 |
sp->photometric == PHOTOMETRIC_YCBCR && |
| 863 |
sp->jpegcolormode == JPEGCOLORMODE_RGB) { |
| 864 |
|
| 865 |
sp->cinfo.d.jpeg_color_space = JCS_YCbCr; |
| 866 |
sp->cinfo.d.out_color_space = JCS_RGB; |
| 867 |
} else { |
| 868 |
|
| 869 |
sp->cinfo.d.jpeg_color_space = JCS_UNKNOWN; |
| 870 |
sp->cinfo.d.out_color_space = JCS_UNKNOWN; |
| 871 |
if (td->td_planarconfig == PLANARCONFIG_CONTIG && |
| 872 |
(sp->h_sampling != 1 || sp->v_sampling != 1)) |
| 873 |
downsampled_output = TRUE; |
| 874 |
|
| 875 |
} |
| 876 |
if (downsampled_output) { |
| 877 |
|
| 878 |
sp->cinfo.d.raw_data_out = TRUE; |
| 879 |
tif->tif_decoderow = JPEGDecodeRaw; |
| 880 |
tif->tif_decodestrip = JPEGDecodeRaw; |
| 881 |
tif->tif_decodetile = JPEGDecodeRaw; |
| 882 |
} else { |
| 883 |
|
| 884 |
sp->cinfo.d.raw_data_out = FALSE; |
| 885 |
tif->tif_decoderow = JPEGDecode; |
| 886 |
tif->tif_decodestrip = JPEGDecode; |
| 887 |
tif->tif_decodetile = JPEGDecode; |
| 888 |
} |
| 889 |
|
| 890 |
if (!TIFFjpeg_start_decompress(sp)) |
| 891 |
return (0); |
| 892 |
|
| 893 |
if (downsampled_output) { |
| 894 |
if (!alloc_downsampled_buffers(tif, sp->cinfo.d.comp_info, |
| 895 |
sp->cinfo.d.num_components)) |
| 896 |
return (0); |
| 897 |
sp->scancount = DCTSIZE; |
| 898 |
} |
| 899 |
return (1); |
| 900 |
} |
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
static int |
| 908 |
#ifdef _USING_PROTOTYPES_ |
| 909 |
JPEGDecode ( |
| 910 |
TIFF* tif, |
| 911 |
tidata_t buf, |
| 912 |
tsize_t cc, |
| 913 |
tsample_t s) |
| 914 |
#else |
| 915 |
JPEGDecode(tif, buf, cc, s) |
| 916 |
TIFF* tif; |
| 917 |
tidata_t buf; |
| 918 |
tsize_t cc; |
| 919 |
tsample_t s; |
| 920 |
#endif |
| 921 |
{ |
| 922 |
JPEGState *sp = JState(tif); |
| 923 |
tsize_t nrows; |
| 924 |
JSAMPROW bufptr[1]; |
| 925 |
|
| 926 |
(void) s; |
| 927 |
assert(sp != NULL); |
| 928 |
|
| 929 |
nrows = cc / sp->bytesperline; |
| 930 |
|
| 931 |
while (nrows-- > 0) { |
| 932 |
bufptr[0] = (JSAMPROW) buf; |
| 933 |
if (TIFFjpeg_read_scanlines(sp, bufptr, 1) != 1) |
| 934 |
return (0); |
| 935 |
if (nrows > 0) |
| 936 |
tif->tif_row++; |
| 937 |
buf += sp->bytesperline; |
| 938 |
} |
| 939 |
|
| 940 |
if (sp->cinfo.d.output_scanline == sp->cinfo.d.output_height) { |
| 941 |
if (TIFFjpeg_finish_decompress(sp) != TRUE) |
| 942 |
return (0); |
| 943 |
} |
| 944 |
return (1); |
| 945 |
} |
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
static int |
| 953 |
#ifdef _USING_PROTOTYPES_ |
| 954 |
JPEGDecodeRaw ( |
| 955 |
TIFF* tif, |
| 956 |
tidata_t buf, |
| 957 |
tsize_t cc, |
| 958 |
tsample_t s) |
| 959 |
#else |
| 960 |
JPEGDecodeRaw(tif, buf, cc, s) |
| 961 |
TIFF* tif; |
| 962 |
tidata_t buf; |
| 963 |
tsize_t cc; |
| 964 |
tsample_t s; |
| 965 |
#endif |
| 966 |
{ |
| 967 |
JPEGState *sp = JState(tif); |
| 968 |
JSAMPLE* inptr; |
| 969 |
JSAMPLE* outptr; |
| 970 |
tsize_t nrows; |
| 971 |
JDIMENSION clumps_per_line, nclump; |
| 972 |
int clumpoffset, ci, xpos, ypos; |
| 973 |
jpeg_component_info* compptr; |
| 974 |
int samples_per_clump = sp->samplesperclump; |
| 975 |
|
| 976 |
(void) s; |
| 977 |
assert(sp != NULL); |
| 978 |
|
| 979 |
nrows = cc / sp->bytesperline; |
| 980 |
|
| 981 |
|
| 982 |
clumps_per_line = sp->cinfo.d.comp_info[1].downsampled_width; |
| 983 |
|
| 984 |
while (nrows-- > 0) { |
| 985 |
|
| 986 |
if (sp->scancount >= DCTSIZE) { |
| 987 |
int n = sp->cinfo.d.max_v_samp_factor * DCTSIZE; |
| 988 |
if (TIFFjpeg_read_raw_data(sp, sp->ds_buffer, n) != n) |
| 989 |
return (0); |
| 990 |
sp->scancount = 0; |
| 991 |
|
| 992 |
if (sp->cinfo.d.output_scanline >= |
| 993 |
sp->cinfo.d.output_height) { |
| 994 |
if (TIFFjpeg_finish_decompress(sp) != TRUE) |
| 995 |
return (0); |
| 996 |
} |
| 997 |
} |
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
clumpoffset = 0; |
| 1003 |
for (ci = 0, compptr = sp->cinfo.d.comp_info; |
| 1004 |
ci < sp->cinfo.d.num_components; |
| 1005 |
ci++, compptr++) { |
| 1006 |
int hsamp = compptr->h_samp_factor; |
| 1007 |
int vsamp = compptr->v_samp_factor; |
| 1008 |
for (ypos = 0; ypos < vsamp; ypos++) { |
| 1009 |
inptr = sp->ds_buffer[ci][sp->scancount*vsamp + ypos]; |
| 1010 |
outptr = ((JSAMPLE*) buf) + clumpoffset; |
| 1011 |
if (hsamp == 1) { |
| 1012 |
|
| 1013 |
for (nclump = clumps_per_line; nclump-- > 0; ) { |
| 1014 |
outptr[0] = *inptr++; |
| 1015 |
outptr += samples_per_clump; |
| 1016 |
} |
| 1017 |
} else { |
| 1018 |
|
| 1019 |
for (nclump = clumps_per_line; nclump-- > 0; ) { |
| 1020 |
for (xpos = 0; xpos < hsamp; xpos++) |
| 1021 |
outptr[xpos] = *inptr++; |
| 1022 |
outptr += samples_per_clump; |
| 1023 |
} |
| 1024 |
} |
| 1025 |
clumpoffset += hsamp; |
| 1026 |
} |
| 1027 |
} |
| 1028 |
sp->scancount++; |
| 1029 |
if (nrows > 0) |
| 1030 |
tif->tif_row++; |
| 1031 |
buf += sp->bytesperline; |
| 1032 |
} |
| 1033 |
return (1); |
| 1034 |
} |
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
static void unsuppress_quant_table _ANSI_ARGS_((JPEGState*, int)); |
| 1042 |
static void |
| 1043 |
unsuppress_quant_table(sp, tblno) |
| 1044 |
JPEGState* sp; |
| 1045 |
int tblno; |
| 1046 |
{ |
| 1047 |
JQUANT_TBL* qtbl; |
| 1048 |
|
| 1049 |
if ((qtbl = sp->cinfo.c.quant_tbl_ptrs[tblno]) != NULL) |
| 1050 |
qtbl->sent_table = FALSE; |
| 1051 |
} |
| 1052 |
|
| 1053 |
static void unsuppress_huff_table _ANSI_ARGS_((JPEGState*, int)); |
| 1054 |
static void |
| 1055 |
unsuppress_huff_table(sp, tblno) |
| 1056 |
JPEGState* sp; |
| 1057 |
int tblno; |
| 1058 |
{ |
| 1059 |
JHUFF_TBL* htbl; |
| 1060 |
|
| 1061 |
if ((htbl = sp->cinfo.c.dc_huff_tbl_ptrs[tblno]) != NULL) |
| 1062 |
htbl->sent_table = FALSE; |
| 1063 |
if ((htbl = sp->cinfo.c.ac_huff_tbl_ptrs[tblno]) != NULL) |
| 1064 |
htbl->sent_table = FALSE; |
| 1065 |
} |
| 1066 |
|
| 1067 |
static int prepare_JPEGTables _ANSI_ARGS_((TIFF*)); |
| 1068 |
static int |
| 1069 |
prepare_JPEGTables(tif) |
| 1070 |
TIFF* tif; |
| 1071 |
{ |
| 1072 |
JPEGState* sp = JState(tif); |
| 1073 |
|
| 1074 |
|
| 1075 |
if (!TIFFjpeg_set_quality(sp, sp->jpegquality, FALSE)) |
| 1076 |
return (0); |
| 1077 |
|
| 1078 |
|
| 1079 |
if (!TIFFjpeg_suppress_tables(sp, TRUE)) |
| 1080 |
return (0); |
| 1081 |
if (sp->jpegtablesmode & JPEGTABLESMODE_QUANT) { |
| 1082 |
unsuppress_quant_table(sp, 0); |
| 1083 |
if (sp->photometric == PHOTOMETRIC_YCBCR) |
| 1084 |
unsuppress_quant_table(sp, 1); |
| 1085 |
} |
| 1086 |
if (sp->jpegtablesmode & JPEGTABLESMODE_HUFF) { |
| 1087 |
unsuppress_huff_table(sp, 0); |
| 1088 |
if (sp->photometric == PHOTOMETRIC_YCBCR) |
| 1089 |
unsuppress_huff_table(sp, 1); |
| 1090 |
} |
| 1091 |
|
| 1092 |
if (!TIFFjpeg_tables_dest(sp, tif)) |
| 1093 |
return (0); |
| 1094 |
|
| 1095 |
if (!TIFFjpeg_write_tables(sp)) |
| 1096 |
return (0); |
| 1097 |
|
| 1098 |
return (1); |
| 1099 |
} |
| 1100 |
|
| 1101 |
static int JPEGSetupEncode _ANSI_ARGS_((TIFF* tif)); |
| 1102 |
static int |
| 1103 |
JPEGSetupEncode(tif) |
| 1104 |
TIFF* tif; |
| 1105 |
{ |
| 1106 |
JPEGState* sp = JState(tif); |
| 1107 |
TIFFDirectory *td = &tif->tif_dir; |
| 1108 |
static char module[] = "JPEGSetupEncode"; |
| 1109 |
|
| 1110 |
assert(sp != NULL); |
| 1111 |
assert(!sp->cinfo.comm.is_decompressor); |
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
sp->cinfo.c.in_color_space = JCS_UNKNOWN; |
| 1119 |
sp->cinfo.c.input_components = 1; |
| 1120 |
if (!TIFFjpeg_set_defaults(sp)) |
| 1121 |
return (0); |
| 1122 |
|
| 1123 |
sp->photometric = td->td_photometric; |
| 1124 |
switch (sp->photometric) { |
| 1125 |
case PHOTOMETRIC_YCBCR: |
| 1126 |
sp->h_sampling = td->td_ycbcrsubsampling[0]; |
| 1127 |
sp->v_sampling = td->td_ycbcrsubsampling[1]; |
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
#ifdef COLORIMETRY_SUPPORT |
| 1134 |
if (!TIFFFieldSet(tif, FIELD_REFBLACKWHITE)) { |
| 1135 |
float refbw[6]; |
| 1136 |
long top = 1L << td->td_bitspersample; |
| 1137 |
refbw[0] = 0; |
| 1138 |
refbw[1] = (float)(top-1L); |
| 1139 |
refbw[2] = (float)(top>>1); |
| 1140 |
refbw[3] = refbw[1]; |
| 1141 |
refbw[4] = refbw[2]; |
| 1142 |
refbw[5] = refbw[1]; |
| 1143 |
ImgTIFFSetField(tif, TIFFTAG_REFERENCEBLACKWHITE, refbw); |
| 1144 |
} |
| 1145 |
#endif |
| 1146 |
break; |
| 1147 |
case PHOTOMETRIC_PALETTE: |
| 1148 |
case PHOTOMETRIC_MASK: |
| 1149 |
ImgTIFFError(module, |
| 1150 |
"PhotometricInterpretation %d not allowed for JPEG", |
| 1151 |
(int) sp->photometric); |
| 1152 |
return (0); |
| 1153 |
default: |
| 1154 |
|
| 1155 |
sp->h_sampling = 1; |
| 1156 |
sp->v_sampling = 1; |
| 1157 |
break; |
| 1158 |
} |
| 1159 |
|
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
|
| 1164 |
|
| 1165 |
|
| 1166 |
|
| 1167 |
if (td->td_bitspersample != BITS_IN_JSAMPLE) { |
| 1168 |
ImgTIFFError(module, "BitsPerSample %d not allowed for JPEG", |
| 1169 |
(int) td->td_bitspersample); |
| 1170 |
return (0); |
| 1171 |
} |
| 1172 |
sp->cinfo.c.data_precision = td->td_bitspersample; |
| 1173 |
if (isTiled(tif)) { |
| 1174 |
if ((td->td_tilelength % (sp->v_sampling * DCTSIZE)) != 0) { |
| 1175 |
ImgTIFFError(module, |
| 1176 |
"JPEG tile height must be multiple of %d", |
| 1177 |
sp->v_sampling * DCTSIZE); |
| 1178 |
return (0); |
| 1179 |
} |
| 1180 |
if ((td->td_tilewidth % (sp->h_sampling * DCTSIZE)) != 0) { |
| 1181 |
ImgTIFFError(module, |
| 1182 |
"JPEG tile width must be multiple of %d", |
| 1183 |
sp->h_sampling * DCTSIZE); |
| 1184 |
return (0); |
| 1185 |
} |
| 1186 |
} else { |
| 1187 |
if (td->td_rowsperstrip < td->td_imagelength && |
| 1188 |
(td->td_rowsperstrip % (sp->v_sampling * DCTSIZE)) != 0) { |
| 1189 |
ImgTIFFError(module, |
| 1190 |
"RowsPerStrip must be multiple of %d for JPEG", |
| 1191 |
sp->v_sampling * DCTSIZE); |
| 1192 |
return (0); |
| 1193 |
} |
| 1194 |
} |
| 1195 |
|
| 1196 |
|
| 1197 |
if (sp->jpegtablesmode & (JPEGTABLESMODE_QUANT|JPEGTABLESMODE_HUFF)) { |
| 1198 |
if (!prepare_JPEGTables(tif)) |
| 1199 |
return (0); |
| 1200 |
|
| 1201 |
|
| 1202 |
TIFFSetFieldBit(tif, FIELD_JPEGTABLES); |
| 1203 |
tif->tif_flags |= TIFF_DIRTYDIRECT; |
| 1204 |
} else { |
| 1205 |
|
| 1206 |
|
| 1207 |
TIFFClrFieldBit(tif, FIELD_JPEGTABLES); |
| 1208 |
} |
| 1209 |
|
| 1210 |
|
| 1211 |
TIFFjpeg_data_dest(sp, tif); |
| 1212 |
|
| 1213 |
return (1); |
| 1214 |
} |
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
|
| 1220 |
static int |
| 1221 |
#ifdef _USING_PROTOTYPES_ |
| 1222 |
JPEGPreEncode ( |
| 1223 |
TIFF* tif, |
| 1224 |
tsample_t s) |
| 1225 |
#else |
| 1226 |
JPEGPreEncode(tif, s) |
| 1227 |
TIFF* tif; |
| 1228 |
tsample_t s; |
| 1229 |
#endif |
| 1230 |
{ |
| 1231 |
JPEGState *sp = JState(tif); |
| 1232 |
TIFFDirectory *td = &tif->tif_dir; |
| 1233 |
static char module[] = "JPEGPreEncode"; |
| 1234 |
uint32 segment_width, segment_height; |
| 1235 |
int downsampled_input; |
| 1236 |
|
| 1237 |
assert(sp != NULL); |
| 1238 |
assert(!sp->cinfo.comm.is_decompressor); |
| 1239 |
|
| 1240 |
|
| 1241 |
|
| 1242 |
if (isTiled(tif)) { |
| 1243 |
segment_width = td->td_tilewidth; |
| 1244 |
segment_height = td->td_tilelength; |
| 1245 |
sp->bytesperline = ImgTIFFTileRowSize(tif); |
| 1246 |
} else { |
| 1247 |
segment_width = td->td_imagewidth; |
| 1248 |
segment_height = td->td_imagelength - tif->tif_row; |
| 1249 |
if (segment_height > td->td_rowsperstrip) |
| 1250 |
segment_height = td->td_rowsperstrip; |
| 1251 |
sp->bytesperline = ImgTIFFScanlineSize(tif); |
| 1252 |
} |
| 1253 |
if (td->td_planarconfig == PLANARCONFIG_SEPARATE && s > 0) { |
| 1254 |
|
| 1255 |
|
| 1256 |
|
| 1257 |
segment_width = TIFFhowmany(segment_width, sp->h_sampling); |
| 1258 |
segment_height = TIFFhowmany(segment_height, sp->v_sampling); |
| 1259 |
} |
| 1260 |
if (segment_width > 65535 || segment_height > 65535) { |
| 1261 |
ImgTIFFError(module, "Strip/tile too large for JPEG"); |
| 1262 |
return (0); |
| 1263 |
} |
| 1264 |
sp->cinfo.c.image_width = segment_width; |
| 1265 |
sp->cinfo.c.image_height = segment_height; |
| 1266 |
downsampled_input = FALSE; |
| 1267 |
if (td->td_planarconfig == PLANARCONFIG_CONTIG) { |
| 1268 |
sp->cinfo.c.input_components = td->td_samplesperpixel; |
| 1269 |
if (sp->photometric == PHOTOMETRIC_YCBCR) { |
| 1270 |
if (sp->jpegcolormode == JPEGCOLORMODE_RGB) { |
| 1271 |
sp->cinfo.c.in_color_space = JCS_RGB; |
| 1272 |
} else { |
| 1273 |
sp->cinfo.c.in_color_space = JCS_YCbCr; |
| 1274 |
if (sp->h_sampling != 1 || sp->v_sampling != 1) |
| 1275 |
downsampled_input = TRUE; |
| 1276 |
} |
| 1277 |
if (!TIFFjpeg_set_colorspace(sp, JCS_YCbCr)) |
| 1278 |
return (0); |
| 1279 |
|
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
sp->cinfo.c.comp_info[0].h_samp_factor = sp->h_sampling; |
| 1284 |
sp->cinfo.c.comp_info[0].v_samp_factor = sp->v_sampling; |
| 1285 |
} else { |
| 1286 |
sp->cinfo.c.in_color_space = JCS_UNKNOWN; |
| 1287 |
if (!TIFFjpeg_set_colorspace(sp, JCS_UNKNOWN)) |
| 1288 |
return (0); |
| 1289 |
|
| 1290 |
} |
| 1291 |
} else { |
| 1292 |
sp->cinfo.c.input_components = 1; |
| 1293 |
sp->cinfo.c.in_color_space = JCS_UNKNOWN; |
| 1294 |
if (!TIFFjpeg_set_colorspace(sp, JCS_UNKNOWN)) |
| 1295 |
return (0); |
| 1296 |
sp->cinfo.c.comp_info[0].component_id = s; |
| 1297 |
|
| 1298 |
if (sp->photometric == PHOTOMETRIC_YCBCR && s > 0) { |
| 1299 |
sp->cinfo.c.comp_info[0].quant_tbl_no = 1; |
| 1300 |
sp->cinfo.c.comp_info[0].dc_tbl_no = 1; |
| 1301 |
sp->cinfo.c.comp_info[0].ac_tbl_no = 1; |
| 1302 |
} |
| 1303 |
} |
| 1304 |
|
| 1305 |
sp->cinfo.c.write_JFIF_header = FALSE; |
| 1306 |
sp->cinfo.c.write_Adobe_marker = FALSE; |
| 1307 |
|
| 1308 |
if (! (sp->jpegtablesmode & JPEGTABLESMODE_QUANT)) { |
| 1309 |
if (!TIFFjpeg_set_quality(sp, sp->jpegquality, FALSE)) |
| 1310 |
return (0); |
| 1311 |
unsuppress_quant_table(sp, 0); |
| 1312 |
unsuppress_quant_table(sp, 1); |
| 1313 |
} |
| 1314 |
if (sp->jpegtablesmode & JPEGTABLESMODE_HUFF) |
| 1315 |
sp->cinfo.c.optimize_coding = FALSE; |
| 1316 |
else |
| 1317 |
sp->cinfo.c.optimize_coding = TRUE; |
| 1318 |
if (downsampled_input) { |
| 1319 |
|
| 1320 |
sp->cinfo.c.raw_data_in = TRUE; |
| 1321 |
tif->tif_encoderow = JPEGEncodeRaw; |
| 1322 |
tif->tif_encodestrip = JPEGEncodeRaw; |
| 1323 |
tif->tif_encodetile = JPEGEncodeRaw; |
| 1324 |
} else { |
| 1325 |
|
| 1326 |
sp->cinfo.c.raw_data_in = FALSE; |
| 1327 |
tif->tif_encoderow = JPEGEncode; |
| 1328 |
tif->tif_encodestrip = JPEGEncode; |
| 1329 |
tif->tif_encodetile = JPEGEncode; |
| 1330 |
} |
| 1331 |
|
| 1332 |
if (!TIFFjpeg_start_compress(sp, FALSE)) |
| 1333 |
return (0); |
| 1334 |
|
| 1335 |
if (downsampled_input) { |
| 1336 |
if (!alloc_downsampled_buffers(tif, sp->cinfo.c.comp_info, |
| 1337 |
sp->cinfo.c.num_components)) |
| 1338 |
return (0); |
| 1339 |
} |
| 1340 |
sp->scancount = 0; |
| 1341 |
|
| 1342 |
return (1); |
| 1343 |
} |
| 1344 |
|
| 1345 |
|
| 1346 |
|
| 1347 |
|
| 1348 |
|
| 1349 |
|
| 1350 |
static int JPEGEncode _ANSI_ARGS_((TIFF*, tidata_t, tsize_t, tsample_t)); |
| 1351 |
static int |
| 1352 |
JPEGEncode(tif, buf, cc, s) |
| 1353 |
TIFF* tif; |
| 1354 |
tidata_t buf; |
| 1355 |
tsize_t cc; |
| 1356 |
tsample_t s; |
| 1357 |
{ |
| 1358 |
JPEGState *sp = JState(tif); |
| 1359 |
tsize_t nrows; |
| 1360 |
JSAMPROW bufptr[1]; |
| 1361 |
|
| 1362 |
(void) s; |
| 1363 |
assert(sp != NULL); |
| 1364 |
|
| 1365 |
nrows = cc / sp->bytesperline; |
| 1366 |
|
| 1367 |
while (nrows-- > 0) { |
| 1368 |
bufptr[0] = (JSAMPROW) buf; |
| 1369 |
if (TIFFjpeg_write_scanlines(sp, bufptr, 1) != 1) |
| 1370 |
return (0); |
| 1371 |
if (nrows > 0) |
| 1372 |
tif->tif_row++; |
| 1373 |
buf += sp->bytesperline; |
| 1374 |
} |
| 1375 |
return (1); |
| 1376 |
} |
| 1377 |
|
| 1378 |
|
| 1379 |
|
| 1380 |
|
| 1381 |
|
| 1382 |
|
| 1383 |
static int JPEGEncodeRaw _ANSI_ARGS_((TIFF*, tidata_t, tsize_t, tsample_t)); |
| 1384 |
static int |
| 1385 |
JPEGEncodeRaw(tif, buf, cc, s) |
| 1386 |
TIFF* tif; |
| 1387 |
tidata_t buf; |
| 1388 |
tsize_t cc; |
| 1389 |
tsample_t s; |
| 1390 |
{ |
| 1391 |
JPEGState *sp = JState(tif); |
| 1392 |
JSAMPLE* inptr; |
| 1393 |
JSAMPLE* outptr; |
| 1394 |
tsize_t nrows; |
| 1395 |
JDIMENSION clumps_per_line, nclump; |
| 1396 |
int clumpoffset, ci, xpos, ypos; |
| 1397 |
jpeg_component_info* compptr; |
| 1398 |
int samples_per_clump = sp->samplesperclump; |
| 1399 |
|
| 1400 |
(void) s; |
| 1401 |
assert(sp != NULL); |
| 1402 |
|
| 1403 |
nrows = cc / sp->bytesperline; |
| 1404 |
|
| 1405 |
|
| 1406 |
clumps_per_line = sp->cinfo.c.comp_info[1].downsampled_width; |
| 1407 |
|
| 1408 |
while (nrows-- > 0) { |
| 1409 |
|
| 1410 |
|
| 1411 |
|
| 1412 |
|
| 1413 |
clumpoffset = 0; |
| 1414 |
for (ci = 0, compptr = sp->cinfo.c.comp_info; |
| 1415 |
ci < sp->cinfo.c.num_components; |
| 1416 |
ci++, compptr++) { |
| 1417 |
int hsamp = compptr->h_samp_factor; |
| 1418 |
int vsamp = compptr->v_samp_factor; |
| 1419 |
int padding = (int) (compptr->width_in_blocks * DCTSIZE - |
| 1420 |
clumps_per_line * hsamp); |
| 1421 |
for (ypos = 0; ypos < vsamp; ypos++) { |
| 1422 |
inptr = ((JSAMPLE*) buf) + clumpoffset; |
| 1423 |
outptr = sp->ds_buffer[ci][sp->scancount*vsamp + ypos]; |
| 1424 |
if (hsamp == 1) { |
| 1425 |
|
| 1426 |
for (nclump = clumps_per_line; nclump-- > 0; ) { |
| 1427 |
*outptr++ = inptr[0]; |
| 1428 |
inptr += samples_per_clump; |
| 1429 |
} |
| 1430 |
} else { |
| 1431 |
|
| 1432 |
for (nclump = clumps_per_line; nclump-- > 0; ) { |
| 1433 |
for (xpos = 0; xpos < hsamp; xpos++) |
| 1434 |
*outptr++ = inptr[xpos]; |
| 1435 |
inptr += samples_per_clump; |
| 1436 |
} |
| 1437 |
} |
| 1438 |
|
| 1439 |
for (xpos = 0; xpos < padding; xpos++) { |
| 1440 |
*outptr = outptr[-1]; |
| 1441 |
outptr++; |
| 1442 |
} |
| 1443 |
clumpoffset += hsamp; |
| 1444 |
} |
| 1445 |
} |
| 1446 |
sp->scancount++; |
| 1447 |
if (sp->scancount >= DCTSIZE) { |
| 1448 |
int n = sp->cinfo.c.max_v_samp_factor * DCTSIZE; |
| 1449 |
if (TIFFjpeg_write_raw_data(sp, sp->ds_buffer, n) != n) |
| 1450 |
return (0); |
| 1451 |
sp->scancount = 0; |
| 1452 |
} |
| 1453 |
if (nrows > 0) |
| 1454 |
tif->tif_row++; |
| 1455 |
buf += sp->bytesperline; |
| 1456 |
} |
| 1457 |
return (1); |
| 1458 |
} |
| 1459 |
|
| 1460 |
|
| 1461 |
|
| 1462 |
|
| 1463 |
|
| 1464 |
static int JPEGPostEncode _ANSI_ARGS_((TIFF* tif)); |
| 1465 |
static int |
| 1466 |
JPEGPostEncode(tif) |
| 1467 |
TIFF* tif; |
| 1468 |
{ |
| 1469 |
JPEGState *sp = JState(tif); |
| 1470 |
|
| 1471 |
if (sp->scancount > 0) { |
| 1472 |
|
| 1473 |
|
| 1474 |
|
| 1475 |
|
| 1476 |
int ci, ypos, n; |
| 1477 |
jpeg_component_info* compptr; |
| 1478 |
|
| 1479 |
for (ci = 0, compptr = sp->cinfo.c.comp_info; |
| 1480 |
ci < sp->cinfo.c.num_components; |
| 1481 |
ci++, compptr++) { |
| 1482 |
int vsamp = compptr->v_samp_factor; |
| 1483 |
tsize_t row_width = compptr->width_in_blocks * DCTSIZE |
| 1484 |
* sizeof(JSAMPLE); |
| 1485 |
for (ypos = sp->scancount * vsamp; |
| 1486 |
ypos < DCTSIZE * vsamp; ypos++) { |
| 1487 |
ImgTIFFmemcpy((tdata_t)sp->ds_buffer[ci][ypos], |
| 1488 |
(tdata_t)sp->ds_buffer[ci][ypos-1], |
| 1489 |
row_width); |
| 1490 |
|
| 1491 |
} |
| 1492 |
} |
| 1493 |
n = sp->cinfo.c.max_v_samp_factor * DCTSIZE; |
| 1494 |
if (TIFFjpeg_write_raw_data(sp, sp->ds_buffer, n) != n) |
| 1495 |
return (0); |
| 1496 |
} |
| 1497 |
|
| 1498 |
return (TIFFjpeg_finish_compress(JState(tif))); |
| 1499 |
} |
| 1500 |
|
| 1501 |
static void JPEGCleanup _ANSI_ARGS_((TIFF*)); |
| 1502 |
static void |
| 1503 |
JPEGCleanup(tif) |
| 1504 |
TIFF* tif; |
| 1505 |
{ |
| 1506 |
if (tif->tif_data) { |
| 1507 |
JPEGState *sp = JState(tif); |
| 1508 |
TIFFjpeg_destroy(sp); |
| 1509 |
if (sp->jpegtables) |
| 1510 |
ImgTIFFfree(sp->jpegtables); |
| 1511 |
ImgTIFFfree(tif->tif_data); |
| 1512 |
tif->tif_data = NULL; |
| 1513 |
} |
| 1514 |
} |
| 1515 |
|
| 1516 |
static int JPEGVSetField _ANSI_ARGS_((TIFF* tif, ttag_t tag, va_list ap)); |
| 1517 |
|
| 1518 |
static int |
| 1519 |
JPEGVSetField(tif, tag, ap) |
| 1520 |
TIFF* tif; |
| 1521 |
ttag_t tag; |
| 1522 |
va_list ap; |
| 1523 |
{ |
| 1524 |
JPEGState* sp = JState(tif); |
| 1525 |
TIFFDirectory* td = &tif->tif_dir; |
| 1526 |
uint32 v32; |
| 1527 |
|
| 1528 |
switch (tag) { |
| 1529 |
case TIFFTAG_JPEGTABLES: |
| 1530 |
v32 = va_arg(ap, uint32); |
| 1531 |
if (v32 == 0) { |
| 1532 |
|
| 1533 |
return (0); |
| 1534 |
} |
| 1535 |
ImgTIFFsetByteArray(&sp->jpegtables, va_arg(ap, void*), |
| 1536 |
(long) v32); |
| 1537 |
sp->jpegtables_length = v32; |
| 1538 |
TIFFSetFieldBit(tif, FIELD_JPEGTABLES); |
| 1539 |
break; |
| 1540 |
case TIFFTAG_JPEGQUALITY: |
| 1541 |
sp->jpegquality = va_arg(ap, int); |
| 1542 |
return (1); |
| 1543 |
case TIFFTAG_JPEGCOLORMODE: |
| 1544 |
sp->jpegcolormode = va_arg(ap, int); |
| 1545 |
|
| 1546 |
|
| 1547 |
|
| 1548 |
|
| 1549 |
|
| 1550 |
tif->tif_flags &= ~TIFF_UPSAMPLED; |
| 1551 |
if (td->td_planarconfig == PLANARCONFIG_CONTIG) { |
| 1552 |
if (td->td_photometric == PHOTOMETRIC_YCBCR && |
| 1553 |
sp->jpegcolormode == JPEGCOLORMODE_RGB) { |
| 1554 |
tif->tif_flags |= TIFF_UPSAMPLED; |
| 1555 |
} else { |
| 1556 |
if (td->td_ycbcrsubsampling[0] != 1 || |
| 1557 |
td->td_ycbcrsubsampling[1] != 1) |
| 1558 |
; |
| 1559 |
} |
| 1560 |
} |
| 1561 |
|
| 1562 |
|
| 1563 |
|
| 1564 |
|
| 1565 |
tif->tif_tilesize = ImgTIFFTileSize(tif); |
| 1566 |
return (1); |
| 1567 |
case TIFFTAG_JPEGTABLESMODE: |
| 1568 |
sp->jpegtablesmode = va_arg(ap, int); |
| 1569 |
return (1); |
| 1570 |
default: |
| 1571 |
return (*sp->vsetparent)(tif, tag, ap); |
| 1572 |
} |
| 1573 |
tif->tif_flags |= TIFF_DIRTYDIRECT; |
| 1574 |
return (1); |
| 1575 |
} |
| 1576 |
|
| 1577 |
static int JPEGVGetField _ANSI_ARGS_((TIFF* tif, ttag_t tag, va_list ap)); |
| 1578 |
|
| 1579 |
static int |
| 1580 |
JPEGVGetField(tif, tag, ap) |
| 1581 |
TIFF* tif; |
| 1582 |
ttag_t tag; |
| 1583 |
va_list ap; |
| 1584 |
{ |
| 1585 |
JPEGState* sp = JState(tif); |
| 1586 |
|
| 1587 |
switch (tag) { |
| 1588 |
case TIFFTAG_JPEGTABLES: |
| 1589 |
|
| 1590 |
|
| 1591 |
*va_arg(ap, u_short*) = (u_short) sp->jpegtables_length; |
| 1592 |
*va_arg(ap, void**) = sp->jpegtables; |
| 1593 |
break; |
| 1594 |
case TIFFTAG_JPEGQUALITY: |
| 1595 |
*va_arg(ap, int*) = sp->jpegquality; |
| 1596 |
break; |
| 1597 |
case TIFFTAG_JPEGCOLORMODE: |
| 1598 |
*va_arg(ap, int*) = sp->jpegcolormode; |
| 1599 |
break; |
| 1600 |
case TIFFTAG_JPEGTABLESMODE: |
| 1601 |
*va_arg(ap, int*) = sp->jpegtablesmode; |
| 1602 |
break; |
| 1603 |
default: |
| 1604 |
return (*sp->vgetparent)(tif, tag, ap); |
| 1605 |
} |
| 1606 |
return (1); |
| 1607 |
} |
| 1608 |
|
| 1609 |
static uint32 JPEGDefaultStripSize _ANSI_ARGS_((TIFF*, uint32)); |
| 1610 |
static uint32 |
| 1611 |
JPEGDefaultStripSize(tif, s) |
| 1612 |
TIFF* tif; |
| 1613 |
uint32 s; |
| 1614 |
{ |
| 1615 |
JPEGState* sp = JState(tif); |
| 1616 |
TIFFDirectory *td = &tif->tif_dir; |
| 1617 |
|
| 1618 |
s = (*sp->defsparent)(tif, s); |
| 1619 |
if (s < td->td_imagelength) |
| 1620 |
s = TIFFroundup(s, td->td_ycbcrsubsampling[1] * DCTSIZE); |
| 1621 |
return (s); |
| 1622 |
} |
| 1623 |
|
| 1624 |
static void JPEGDefaultTileSize _ANSI_ARGS_((TIFF*, uint32*, uint32*)); |
| 1625 |
static void |
| 1626 |
JPEGDefaultTileSize(tif, tw, th) |
| 1627 |
TIFF* tif; |
| 1628 |
uint32* tw; |
| 1629 |
uint32* th; |
| 1630 |
{ |
| 1631 |
JPEGState* sp = JState(tif); |
| 1632 |
TIFFDirectory *td = &tif->tif_dir; |
| 1633 |
|
| 1634 |
(*sp->deftparent)(tif, tw, th); |
| 1635 |
*tw = TIFFroundup(*tw, td->td_ycbcrsubsampling[0] * DCTSIZE); |
| 1636 |
*th = TIFFroundup(*th, td->td_ycbcrsubsampling[1] * DCTSIZE); |
| 1637 |
} |
| 1638 |
|
| 1639 |
int |
| 1640 |
ImgInitTIFFjpeg(handle, scheme) |
| 1641 |
VOID* handle; |
| 1642 |
int scheme; |
| 1643 |
{ |
| 1644 |
TIFF *tif = (TIFF *) handle; |
| 1645 |
JPEGState* sp; |
| 1646 |
|
| 1647 |
assert(scheme == COMPRESSION_JPEG); |
| 1648 |
|
| 1649 |
if (ImgLoadJpegLibrary() != TCL_OK) { |
| 1650 |
ImgTIFFError("TIFFInitJPEG", "cannot use JPEG compression for TIFF"); |
| 1651 |
return 0; |
| 1652 |
} |
| 1653 |
|
| 1654 |
|
| 1655 |
|
| 1656 |
tif->tif_data = (tidata_t) ImgTIFFmalloc(sizeof (JPEGState)); |
| 1657 |
if (tif->tif_data == NULL) { |
| 1658 |
ImgTIFFError("TIFFInitJPEG", "No space for JPEG state block"); |
| 1659 |
return (0); |
| 1660 |
} |
| 1661 |
sp = JState(tif); |
| 1662 |
sp->tif = tif; |
| 1663 |
|
| 1664 |
|
| 1665 |
|
| 1666 |
|
| 1667 |
|
| 1668 |
ImgTIFFMergeFieldInfo(tif, jpegFieldInfo, N(jpegFieldInfo)); |
| 1669 |
sp->vgetparent = tif->tif_vgetfield; |
| 1670 |
tif->tif_vgetfield = JPEGVGetField; |
| 1671 |
sp->vsetparent = tif->tif_vsetfield; |
| 1672 |
tif->tif_vsetfield = JPEGVSetField; |
| 1673 |
|
| 1674 |
|
| 1675 |
sp->jpegtables = NULL; |
| 1676 |
sp->jpegtables_length = 0; |
| 1677 |
sp->jpegquality = 75; |
| 1678 |
sp->jpegcolormode = JPEGCOLORMODE_RAW; |
| 1679 |
sp->jpegtablesmode = JPEGTABLESMODE_QUANT | JPEGTABLESMODE_HUFF; |
| 1680 |
|
| 1681 |
|
| 1682 |
|
| 1683 |
|
| 1684 |
tif->tif_setupdecode = JPEGSetupDecode; |
| 1685 |
tif->tif_predecode = JPEGPreDecode; |
| 1686 |
tif->tif_decoderow = JPEGDecode; |
| 1687 |
tif->tif_decodestrip = JPEGDecode; |
| 1688 |
tif->tif_decodetile = JPEGDecode; |
| 1689 |
tif->tif_setupencode = JPEGSetupEncode; |
| 1690 |
tif->tif_preencode = JPEGPreEncode; |
| 1691 |
tif->tif_postencode = JPEGPostEncode; |
| 1692 |
tif->tif_encoderow = JPEGEncode; |
| 1693 |
tif->tif_encodestrip = JPEGEncode; |
| 1694 |
tif->tif_encodetile = JPEGEncode; |
| 1695 |
tif->tif_cleanup = JPEGCleanup; |
| 1696 |
sp->defsparent = tif->tif_defstripsize; |
| 1697 |
tif->tif_defstripsize = JPEGDefaultStripSize; |
| 1698 |
sp->deftparent = tif->tif_deftilesize; |
| 1699 |
tif->tif_deftilesize = JPEGDefaultTileSize; |
| 1700 |
tif->tif_flags |= TIFF_NOBITREV; |
| 1701 |
|
| 1702 |
|
| 1703 |
|
| 1704 |
|
| 1705 |
if (tif->tif_mode == O_RDONLY) { |
| 1706 |
if (!TIFFjpeg_create_decompress(sp)) |
| 1707 |
return (0); |
| 1708 |
} else { |
| 1709 |
if (!TIFFjpeg_create_compress(sp)) |
| 1710 |
return (0); |
| 1711 |
} |
| 1712 |
|
| 1713 |
return (1); |
| 1714 |
} |