📄 tif_ojpeg.c
字号:
range_limit[ *Y++ + RIGHT_SHIFT(table0[*Cb++]+table1[*Cr++],16) ]; }; return; case RGB_BLUE : irow1p = in[1] + row; table0 = (INT32 *)cconvert->Cb_b_tab; while (--nrows >= 0) { register JSAMPROW Cb = *irow1p++; register int i = cinfo->output_width; Y = *irow0p++; outp = *out++; while (--i >= 0) *outp++ = range_limit[*Y++ + table0[*Cb++]]; } } }METHODDEF(void)null_convert(register j_decompress_ptr cinfo,JSAMPIMAGE in,JDIMENSION row, register JSAMPARRAY out,register int nrows) { register JSAMPARRAY irowp = in[cinfo->output_scan_number - 1] + row; while (--nrows >= 0) _TIFFmemcpy(*out++,*irowp++,cinfo->output_width); }static intOJPEGSetupDecode(register TIFF *tif) { static char module[]={"OJPEGSetupDecode"}; J_COLOR_SPACE jpeg_color_space, /* Color space of JPEG-compressed image */ out_color_space; /* Color space of decompressed image */ uint32 segment_width; int status = 1; /* Assume success by default */ boolean downsampled_output=FALSE, /* <=> Want JPEG Library's "raw" image? */ is_JFIF; /* <=> JFIF image? */ register OJPEGState *sp = OJState(tif);# define td (&tif->tif_dir) /* Verify miscellaneous parameters. This will need work if the TIFF Library ever supports different depths for different components, or if the JPEG Library ever supports run-time depth selection. Neither seems imminent. */ if (td->td_bitspersample != sp->cinfo.d.data_precision) { TIFFError(module,bad_bps,td->td_bitspersample); status = 0; }; /* The TIFF Version 6.0 specification and IJG JPEG Library accept different sets of color spaces, so verify that our image belongs to the common subset and map its photometry code, then initialize to handle subsampling and optional JPEG Library YCbCr <-> RGB color-space conversion. */ switch (td->td_photometric) { case PHOTOMETRIC_YCBCR : /* ISO IS 10918-1 requires that JPEG subsampling factors be 1-4, but TIFF Version 6.0 is more restrictive: only 1, 2, and 4 are allowed. */ if ( ( td->td_ycbcrsubsampling[0] == 1 || td->td_ycbcrsubsampling[0] == 2 || td->td_ycbcrsubsampling[0] == 4 ) && ( td->td_ycbcrsubsampling[1] == 1 || td->td_ycbcrsubsampling[1] == 2 || td->td_ycbcrsubsampling[1] == 4 ) ) downsampled_output = ( (sp->h_sampling = td->td_ycbcrsubsampling[0]) << 3 | (sp->v_sampling = td->td_ycbcrsubsampling[1]) ) != 011; else { TIFFError(module,bad_subsampling); status = 0; }; jpeg_color_space = JCS_YCbCr; if (sp->jpegcolormode == JPEGCOLORMODE_RGB) { downsampled_output = FALSE; out_color_space = JCS_RGB; break; }; goto L2; case PHOTOMETRIC_MINISBLACK: jpeg_color_space = JCS_GRAYSCALE; goto L1; case PHOTOMETRIC_RGB : jpeg_color_space = JCS_RGB; goto L1; case PHOTOMETRIC_SEPARATED : jpeg_color_space = JCS_CMYK; L1: sp->jpegcolormode = JPEGCOLORMODE_RAW; /* No JPEG Lib. conversion */ L2: out_color_space = jpeg_color_space; break; default : TIFFError(module,bad_photometry,td->td_photometric); status = 0; }; if (status == 0) return 0; /* If TIFF errors, don't bother to continue */ /* Set parameters that are same for all strips/tiles. */ sp->cinfo.d.src = &sp->src; sp->src.init_source = std_init_source; sp->src.fill_input_buffer = std_fill_input_buffer; sp->src.skip_input_data = std_skip_input_data; sp->src.resync_to_restart = jpeg_resync_to_restart; sp->src.term_source = std_term_source; /* BOGOSITY ALERT! The Wang Imaging application for Microsoft Windows produces images containing "JPEGInterchangeFormat[Length]" TIFF records that resemble JFIF-in-TIFF encapsulations but, in fact, violate the TIFF Version 6 specification in several ways; nevertheless, we try to handle them gracefully because there are apparently a lot of them around. The purported "JFIF" data stream in one of these files vaguely resembles a JPEG "tables only" data stream, except that there's no trailing EOI marker. The rest of the JPEG data stream lies in a discontiguous file region, identified by the 0th Strip offset (which is *also* illegal!), where it begins with an SOS marker and apparently continues to the end of the file. There is no trailing EOI marker here, either. */ is_JFIF = !sp->is_WANG && TIFFFieldSet(tif,FIELD_JPEGIFOFFSET); /* Initialize decompression parameters that won't be overridden by JPEG Library defaults set during the "jpeg_read_header()" call, below. */ segment_width = td->td_imagewidth; if (isTiled(tif)) { if (sp->is_WANG) /* we don't know how to handle it */ { TIFFError(module,"Tiled Wang image not supported"); return 0; }; /* BOGOSITY ALERT! "TIFFTileRowSize()" seems to work fine for modern JPEG- in-TIFF encapsulations where the image width--like the tile width--is a multiple of 8 or 16 pixels. But image widths and heights are aren't restricted to 8- or 16-bit multiples, and we need the exact Byte count of decompressed scan lines when we call the JPEG Library. At least one old file ("zackthecat.tif") in the TIFF Library test suite has widths and heights slightly less than the tile sizes, and it apparently used the bogus computation below to determine the number of Bytes per scan line (was this due to an old, broken version of "TIFFhowmany()"?). Before we get here, "OJPEGSetupDecode()" verified that our image uses 8-bit samples, so the following check appears to return the correct answer in all known cases tested to date. */ if (is_JFIF || (segment_width & 7) == 0) sp->bytesperline = TIFFTileRowSize(tif); /* Normal case */ else { /* Was the file-encoder's segment-width calculation bogus? */ segment_width = (segment_width/sp->h_sampling + 1) * sp->h_sampling; sp->bytesperline = segment_width * td->td_samplesperpixel; } } else sp->bytesperline = TIFFVStripSize(tif,1); /* BEWARE OF KLUDGE: If we have JPEG Interchange File Format (JFIF) image, then we want to read "metadata" in the bit-stream's header and validate it against corresponding information in TIFF records. But if we have a *really old* JPEG file that's not JFIF, then we simply assign TIFF-record values to JPEG Library variables without checking. */ if (is_JFIF) /* JFIF image */ { unsigned char *end_of_data; int subsampling_factors; register unsigned char *p; register int i; /* WARNING: Although the image file contains a JFIF bit stream, it might also contain some old TIFF records causing "OJPEGVSetField()" to have allocated quantization or Huffman decoding tables. But when the JPEG Library reads and parses the JFIF header below, it reallocate these tables anew without checking for "dangling" pointers, thereby causing a memory "leak". We have enough information to potentially deallocate the old tables here, but unfortunately JPEG Library Version 6B uses a "pool" allocator for small objects, with no deallocation procedure; instead, it reclaims a whole pool when an image is closed/destroyed, so well-behaved TIFF client applications (i.e., those which close their JPEG images as soon as they're no longer needed) will waste memory for a short time but recover it eventually. But ill-behaved TIFF clients (i.e., those which keep many JPEG images open gratuitously) can exhaust memory prematurely. If the JPEG Library ever implements a deallocation procedure, insert this clean-up code: */# ifdef someday if (sp->jpegtablesmode & JPEGTABLESMODE_QUANT) /* free quant. tables */ { register int i = 0; do { register JQUANT_TBL *q; if (q = sp->cinfo.d.quant_tbl_ptrs[i]) { jpeg_free_small(&sp->cinfo.comm,q,sizeof *q); sp->cinfo.d.quant_tbl_ptrs[i] = 0; } } while (++i < NUM_QUANT_TBLS); }; if (sp->jpegtablesmode & JPEGTABLESMODE_HUFF) /* free Huffman tables */ { register int i = 0; do { register JHUFF_TBL *h; if (h = sp->cinfo.d.dc_huff_tbl_ptrs[i]) { jpeg_free_small(&sp->cinfo.comm,h,sizeof *h); sp->cinfo.d.dc_huff_tbl_ptrs[i] = 0; }; if (h = sp->cinfo.d.ac_huff_tbl_ptrs[i]) { jpeg_free_small(&sp->cinfo.comm,h,sizeof *h); sp->cinfo.d.ac_huff_tbl_ptrs[i] = 0; } } while (++i < NUM_HUFF_TBLS); };# endif /* someday */ /* Since we might someday wish to try rewriting "old format" JPEG-in-TIFF encapsulations in "new format" files, try to synthesize the value of a modern "JPEGTables" TIFF record by scanning the JPEG data from just past the "Start of Information" (SOI) marker until something other than a legitimate "table" marker is found, as defined in ISO IS 10918-1 Appending B.2.4; namely: -- Define Quantization Table (DQT) -- Define Huffman Table (DHT) -- Define Arithmetic Coding table (DAC) -- Define Restart Interval (DRI) -- Comment (COM) -- Application data (APPn) For convenience, we also accept "Expansion" (EXP) markers, although they are apparently not a part of normal "table" data. */ sp->jpegtables = p = (unsigned char *)sp->src.next_input_byte; end_of_data = p + sp->src.bytes_in_buffer; p += 2; while (p < end_of_data && p[0] == 0xFF) switch (p[1]) { default : goto L; case 0xC0: /* SOF0 */ case 0xC1: /* SOF1 */ case 0xC2: /* SOF2 */ case 0xC3: /* SOF3 */ case 0xC4: /* DHT */ case 0xC5: /* SOF5 */ case 0xC6: /* SOF6 */ case 0xC7: /* SOF7 */ case 0xC9: /* SOF9 */ case 0xCA: /* SOF10 */ case 0xCB: /* SOF11 */ case 0xCC: /* DAC */ case 0xCD: /* SOF13 */ case 0xCE: /* SOF14 */ case 0xCF: /* SOF15 */ case 0xDB: /* DQT */ case 0xDD: /* DRI */ case 0xDF: /* EXP */ case 0xE0: /* APP0 */ case 0xE1: /* APP1 */ case 0xE2: /* APP2 */ case 0xE3: /* APP3 */ case 0xE4: /* APP4 */ case 0xE5: /* APP5 */ case 0xE6: /* APP6 */ case 0xE7: /* APP7 */ case 0xE8: /* APP8 */ case 0xE9: /* APP9 */ case 0xEA: /* APP10 */ case 0xEB: /* APP11 */ case 0xEC: /* APP12 */ case 0xED: /* APP13 */ case 0xEE: /* APP14 */ case 0xEF: /* APP15 */ case 0xFE: /* COM */ p += (p[2] << 8 | p[3]) + 2; }; L: if (p - (unsigned char *)sp->jpegtables > 2) /* fake "JPEGTables" */ { /* In case our client application asks, pretend that this image file contains a modern "JPEGTables" TIFF record by copying to a buffer the initial part of the JFIF bit-stream that we just scanned, from the SOI marker through the "metadata" tables, then append an EOI marker and flag the "JPEGTables" TIFF record as "present". */ sp->jpegtables_length = p - (unsigned char*)sp->jpegtables + 2; p = sp->jpegtables; if (!(sp->jpegtables = _TIFFmalloc(sp->jpegtables_length))) { TIFFError(module,no_jtable_space); return 0; }; _TIFFmemcpy(sp->jpegtables,p,sp->jpegtables_length-2); p = (unsigned char *)sp->jpegtables + sp->jpegtables_length; p[-2] = 0xFF; p[-1] = JPEG_EOI; /* Append EOI marker */ TIFFSetFieldBit(tif,FIELD_JPEGTABLES); tif->tif_flags |= TIFF_DIRTYDIRECT; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -