Alien-FreeImage
view release on metacpan or search on metacpan
src/Source/LibTIFF4/tif_jpeg.c view on Meta::CPAN
}
/*
* Alternate source manager for reading from JPEGTables.
* We can share all the code except for the init routine.
*/
static void
tables_init_source(j_decompress_ptr cinfo)
{
JPEGState* sp = (JPEGState*) cinfo;
sp->src.next_input_byte = (const JOCTET*) sp->jpegtables;
sp->src.bytes_in_buffer = (size_t) sp->jpegtables_length;
}
static void
TIFFjpeg_tables_src(JPEGState* sp, TIFF* tif)
{
TIFFjpeg_data_src(sp, tif);
sp->src.init_source = tables_init_source;
}
/*
* Allocate downsampled-data buffers needed for downsampled I/O.
* We use values computed in jpeg_start_compress or jpeg_start_decompress.
* We use libjpeg's allocator so that buffers will be released automatically
* when done with strip/tile.
* This is also a handy place to compute samplesperclump, bytesperline.
*/
static int
alloc_downsampled_buffers(TIFF* tif, jpeg_component_info* comp_info,
int num_components)
{
JPEGState* sp = JState(tif);
int ci;
jpeg_component_info* compptr;
JSAMPARRAY buf;
int samples_per_clump = 0;
for (ci = 0, compptr = comp_info; ci < num_components;
ci++, compptr++) {
samples_per_clump += compptr->h_samp_factor *
compptr->v_samp_factor;
buf = TIFFjpeg_alloc_sarray(sp, JPOOL_IMAGE,
compptr->width_in_blocks * DCTSIZE,
(JDIMENSION) (compptr->v_samp_factor*DCTSIZE));
if (buf == NULL)
return (0);
sp->ds_buffer[ci] = buf;
}
sp->samplesperclump = samples_per_clump;
return (1);
}
/*
* JPEG Decoding.
*/
#ifdef CHECK_JPEG_YCBCR_SUBSAMPLING
#define JPEG_MARKER_SOF0 0xC0
#define JPEG_MARKER_SOF1 0xC1
#define JPEG_MARKER_SOF2 0xC2
#define JPEG_MARKER_SOF9 0xC9
#define JPEG_MARKER_SOF10 0xCA
#define JPEG_MARKER_DHT 0xC4
#define JPEG_MARKER_SOI 0xD8
#define JPEG_MARKER_SOS 0xDA
#define JPEG_MARKER_DQT 0xDB
#define JPEG_MARKER_DRI 0xDD
#define JPEG_MARKER_APP0 0xE0
#define JPEG_MARKER_COM 0xFE
struct JPEGFixupTagsSubsamplingData
{
TIFF* tif;
void* buffer;
uint32 buffersize;
uint8* buffercurrentbyte;
uint32 bufferbytesleft;
uint64 fileoffset;
uint64 filebytesleft;
uint8 filepositioned;
};
static void JPEGFixupTagsSubsampling(TIFF* tif);
static int JPEGFixupTagsSubsamplingSec(struct JPEGFixupTagsSubsamplingData* data);
static int JPEGFixupTagsSubsamplingReadByte(struct JPEGFixupTagsSubsamplingData* data, uint8* result);
static int JPEGFixupTagsSubsamplingReadWord(struct JPEGFixupTagsSubsamplingData* data, uint16* result);
static void JPEGFixupTagsSubsamplingSkip(struct JPEGFixupTagsSubsamplingData* data, uint16 skiplength);
#endif
static int
JPEGFixupTags(TIFF* tif)
{
#ifdef CHECK_JPEG_YCBCR_SUBSAMPLING
if ((tif->tif_dir.td_photometric==PHOTOMETRIC_YCBCR)&&
(tif->tif_dir.td_planarconfig==PLANARCONFIG_CONTIG)&&
(tif->tif_dir.td_samplesperpixel==3))
JPEGFixupTagsSubsampling(tif);
#endif
return(1);
}
#ifdef CHECK_JPEG_YCBCR_SUBSAMPLING
static void
JPEGFixupTagsSubsampling(TIFF* tif)
{
/*
* Some JPEG-in-TIFF produces do not emit the YCBCRSUBSAMPLING values in
* the TIFF tags, but still use non-default (2,2) values within the jpeg
* data stream itself. In order for TIFF applications to work properly
* - for instance to get the strip buffer size right - it is imperative
* that the subsampling be available before we start reading the image
* data normally. This function will attempt to analyze the first strip in
* order to get the sampling values from the jpeg data stream.
*
* Note that JPEGPreDeocode() will produce a fairly loud warning when the
* discovered sampling does not match the default sampling (2,2) or whatever
* was actually in the tiff tags.
*
* See the bug in bugzilla for details:
*
* http://bugzilla.remotesensing.org/show_bug.cgi?id=168
*
* Frank Warmerdam, July 2002
* Joris Van Damme, May 2007
*/
static const char module[] = "JPEGFixupTagsSubsampling";
struct JPEGFixupTagsSubsamplingData m;
_TIFFFillStriles( tif );
if( tif->tif_dir.td_stripbytecount == NULL
|| tif->tif_dir.td_stripoffset == NULL
|| tif->tif_dir.td_stripbytecount[0] == 0 )
{
/* Do not even try to check if the first strip/tile does not
yet exist, as occurs when GDAL has created a new NULL file
for instance. */
return;
}
m.tif=tif;
m.buffersize=2048;
m.buffer=_TIFFmalloc(m.buffersize);
if (m.buffer==NULL)
{
TIFFWarningExt(tif->tif_clientdata,module,
"Unable to allocate memory for auto-correcting of subsampling values; auto-correcting skipped");
return;
}
m.buffercurrentbyte=NULL;
m.bufferbytesleft=0;
m.fileoffset=tif->tif_dir.td_stripoffset[0];
m.filepositioned=0;
m.filebytesleft=tif->tif_dir.td_stripbytecount[0];
if (!JPEGFixupTagsSubsamplingSec(&m))
TIFFWarningExt(tif->tif_clientdata,module,
"Unable to auto-correct subsampling values, likely corrupt JPEG compressed data in first strip/tile; auto-correcting skipped");
_TIFFfree(m.buffer);
}
static int
JPEGFixupTagsSubsamplingSec(struct JPEGFixupTagsSubsamplingData* data)
{
static const char module[] = "JPEGFixupTagsSubsamplingSec";
uint8 m;
while (1)
{
src/Source/LibTIFF4/tif_jpeg.c view on Meta::CPAN
}
static void
JPEGFixupTagsSubsamplingSkip(struct JPEGFixupTagsSubsamplingData* data, uint16 skiplength)
{
if ((uint32)skiplength<=data->bufferbytesleft)
{
data->buffercurrentbyte+=skiplength;
data->bufferbytesleft-=skiplength;
}
else
{
uint16 m;
m=skiplength-data->bufferbytesleft;
if (m<=data->filebytesleft)
{
data->bufferbytesleft=0;
data->fileoffset+=m;
data->filebytesleft-=m;
data->filepositioned=0;
}
else
{
data->bufferbytesleft=0;
data->filebytesleft=0;
}
}
}
#endif
static int
JPEGSetupDecode(TIFF* tif)
{
JPEGState* sp = JState(tif);
TIFFDirectory *td = &tif->tif_dir;
#if defined(JPEG_DUAL_MODE_8_12) && !defined(TIFFInitJPEG)
if( tif->tif_dir.td_bitspersample == 12 )
return TIFFReInitJPEG_12( tif, COMPRESSION_JPEG, 0 );
#endif
JPEGInitializeLibJPEG( tif, TRUE );
assert(sp != NULL);
assert(sp->cinfo.comm.is_decompressor);
/* Read JPEGTables if it is present */
if (TIFFFieldSet(tif,FIELD_JPEGTABLES)) {
TIFFjpeg_tables_src(sp, tif);
if(TIFFjpeg_read_header(sp,FALSE) != JPEG_HEADER_TABLES_ONLY) {
TIFFErrorExt(tif->tif_clientdata, "JPEGSetupDecode", "Bogus JPEGTables field");
return (0);
}
}
/* Grab parameters that are same for all strips/tiles */
sp->photometric = td->td_photometric;
switch (sp->photometric) {
case PHOTOMETRIC_YCBCR:
sp->h_sampling = td->td_ycbcrsubsampling[0];
sp->v_sampling = td->td_ycbcrsubsampling[1];
break;
default:
/* TIFF 6.0 forbids subsampling of all other color spaces */
sp->h_sampling = 1;
sp->v_sampling = 1;
break;
}
/* Set up for reading normal data */
TIFFjpeg_data_src(sp, tif);
tif->tif_postdecode = _TIFFNoPostDecode; /* override byte swapping */
return (1);
}
/*
* Set up for decoding a strip or tile.
*/
static int
JPEGPreDecode(TIFF* tif, uint16 s)
{
JPEGState *sp = JState(tif);
TIFFDirectory *td = &tif->tif_dir;
static const char module[] = "JPEGPreDecode";
uint32 segment_width, segment_height;
int downsampled_output;
int ci;
assert(sp != NULL);
if (sp->cinfo.comm.is_decompressor == 0)
{
tif->tif_setupdecode( tif );
}
assert(sp->cinfo.comm.is_decompressor);
/*
* Reset decoder state from any previous strip/tile,
* in case application didn't read the whole strip.
*/
if (!TIFFjpeg_abort(sp))
return (0);
/*
* Read the header for this strip/tile.
*/
if (TIFFjpeg_read_header(sp, TRUE) != JPEG_HEADER_OK)
return (0);
tif->tif_rawcp = (uint8*) sp->src.next_input_byte;
tif->tif_rawcc = sp->src.bytes_in_buffer;
/*
* Check image parameters and set decompression parameters.
*/
segment_width = td->td_imagewidth;
segment_height = td->td_imagelength - tif->tif_row;
if (isTiled(tif)) {
segment_width = td->td_tilewidth;
src/Source/LibTIFF4/tif_jpeg.c view on Meta::CPAN
* been reported as less than the amount of data jpeg will
* return, some potential security issues arise. Catch this
* case and error out.
*/
TIFFErrorExt(tif->tif_clientdata, module,
"JPEG strip/tile size exceeds expected dimensions,"
" expected %dx%d, got %dx%d",
segment_width, segment_height,
sp->cinfo.d.image_width, sp->cinfo.d.image_height);
return (0);
}
if (sp->cinfo.d.num_components !=
(td->td_planarconfig == PLANARCONFIG_CONTIG ?
td->td_samplesperpixel : 1)) {
TIFFErrorExt(tif->tif_clientdata, module, "Improper JPEG component count");
return (0);
}
#ifdef JPEG_LIB_MK1
if (12 != td->td_bitspersample && 8 != td->td_bitspersample) {
TIFFErrorExt(tif->tif_clientdata, module, "Improper JPEG data precision");
return (0);
}
sp->cinfo.d.data_precision = td->td_bitspersample;
sp->cinfo.d.bits_in_jsample = td->td_bitspersample;
#else
if (sp->cinfo.d.data_precision != td->td_bitspersample) {
TIFFErrorExt(tif->tif_clientdata, module, "Improper JPEG data precision");
return (0);
}
#endif
if (td->td_planarconfig == PLANARCONFIG_CONTIG) {
/* Component 0 should have expected sampling factors */
if (sp->cinfo.d.comp_info[0].h_samp_factor != sp->h_sampling ||
sp->cinfo.d.comp_info[0].v_samp_factor != sp->v_sampling) {
TIFFErrorExt(tif->tif_clientdata, module,
"Improper JPEG sampling factors %d,%d\n"
"Apparently should be %d,%d.",
sp->cinfo.d.comp_info[0].h_samp_factor,
sp->cinfo.d.comp_info[0].v_samp_factor,
sp->h_sampling, sp->v_sampling);
return (0);
}
/* Rest should have sampling factors 1,1 */
for (ci = 1; ci < sp->cinfo.d.num_components; ci++) {
if (sp->cinfo.d.comp_info[ci].h_samp_factor != 1 ||
sp->cinfo.d.comp_info[ci].v_samp_factor != 1) {
TIFFErrorExt(tif->tif_clientdata, module, "Improper JPEG sampling factors");
return (0);
}
}
} else {
/* PC 2's single component should have sampling factors 1,1 */
if (sp->cinfo.d.comp_info[0].h_samp_factor != 1 ||
sp->cinfo.d.comp_info[0].v_samp_factor != 1) {
TIFFErrorExt(tif->tif_clientdata, module, "Improper JPEG sampling factors");
return (0);
}
}
downsampled_output = FALSE;
if (td->td_planarconfig == PLANARCONFIG_CONTIG &&
sp->photometric == PHOTOMETRIC_YCBCR &&
sp->jpegcolormode == JPEGCOLORMODE_RGB) {
/* Convert YCbCr to RGB */
sp->cinfo.d.jpeg_color_space = JCS_YCbCr;
sp->cinfo.d.out_color_space = JCS_RGB;
} else {
/* Suppress colorspace handling */
sp->cinfo.d.jpeg_color_space = JCS_UNKNOWN;
sp->cinfo.d.out_color_space = JCS_UNKNOWN;
if (td->td_planarconfig == PLANARCONFIG_CONTIG &&
(sp->h_sampling != 1 || sp->v_sampling != 1))
downsampled_output = TRUE;
/* XXX what about up-sampling? */
}
if (downsampled_output) {
/* Need to use raw-data interface to libjpeg */
sp->cinfo.d.raw_data_out = TRUE;
#if JPEG_LIB_VERSION >= 70
sp->cinfo.d.do_fancy_upsampling = FALSE;
#endif /* JPEG_LIB_VERSION >= 70 */
tif->tif_decoderow = DecodeRowError;
tif->tif_decodestrip = JPEGDecodeRaw;
tif->tif_decodetile = JPEGDecodeRaw;
} else {
/* Use normal interface to libjpeg */
sp->cinfo.d.raw_data_out = FALSE;
tif->tif_decoderow = JPEGDecode;
tif->tif_decodestrip = JPEGDecode;
tif->tif_decodetile = JPEGDecode;
}
/* Start JPEG decompressor */
if (!TIFFjpeg_start_decompress(sp))
return (0);
/* Allocate downsampled-data buffers if needed */
if (downsampled_output) {
if (!alloc_downsampled_buffers(tif, sp->cinfo.d.comp_info,
sp->cinfo.d.num_components))
return (0);
sp->scancount = DCTSIZE; /* mark buffer empty */
}
return (1);
}
/*
* Decode a chunk of pixels.
* "Standard" case: returned data is not downsampled.
*/
/*ARGSUSED*/ static int
JPEGDecode(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s)
{
JPEGState *sp = JState(tif);
tmsize_t nrows;
(void) s;
/*
** Update available information, buffer may have been refilled
** between decode requests
*/
sp->src.next_input_byte = (const JOCTET*) tif->tif_rawcp;
sp->src.bytes_in_buffer = (size_t) tif->tif_rawcc;
src/Source/LibTIFF4/tif_jpeg.c view on Meta::CPAN
/*
* JPEG Encoding.
*/
static void
unsuppress_quant_table (JPEGState* sp, int tblno)
{
JQUANT_TBL* qtbl;
if ((qtbl = sp->cinfo.c.quant_tbl_ptrs[tblno]) != NULL)
qtbl->sent_table = FALSE;
}
static void
suppress_quant_table (JPEGState* sp, int tblno)
{
JQUANT_TBL* qtbl;
if ((qtbl = sp->cinfo.c.quant_tbl_ptrs[tblno]) != NULL)
qtbl->sent_table = TRUE;
}
static void
unsuppress_huff_table (JPEGState* sp, int tblno)
{
JHUFF_TBL* htbl;
if ((htbl = sp->cinfo.c.dc_huff_tbl_ptrs[tblno]) != NULL)
htbl->sent_table = FALSE;
if ((htbl = sp->cinfo.c.ac_huff_tbl_ptrs[tblno]) != NULL)
htbl->sent_table = FALSE;
}
static void
suppress_huff_table (JPEGState* sp, int tblno)
{
JHUFF_TBL* htbl;
if ((htbl = sp->cinfo.c.dc_huff_tbl_ptrs[tblno]) != NULL)
htbl->sent_table = TRUE;
if ((htbl = sp->cinfo.c.ac_huff_tbl_ptrs[tblno]) != NULL)
htbl->sent_table = TRUE;
}
static int
prepare_JPEGTables(TIFF* tif)
{
JPEGState* sp = JState(tif);
/* Initialize quant tables for current quality setting */
if (!TIFFjpeg_set_quality(sp, sp->jpegquality, FALSE))
return (0);
/* Mark only the tables we want for output */
/* NB: chrominance tables are currently used only with YCbCr */
if (!TIFFjpeg_suppress_tables(sp, TRUE))
return (0);
if (sp->jpegtablesmode & JPEGTABLESMODE_QUANT) {
unsuppress_quant_table(sp, 0);
if (sp->photometric == PHOTOMETRIC_YCBCR)
unsuppress_quant_table(sp, 1);
}
if (sp->jpegtablesmode & JPEGTABLESMODE_HUFF) {
unsuppress_huff_table(sp, 0);
if (sp->photometric == PHOTOMETRIC_YCBCR)
unsuppress_huff_table(sp, 1);
}
/* Direct libjpeg output into jpegtables */
if (!TIFFjpeg_tables_dest(sp, tif))
return (0);
/* Emit tables-only datastream */
if (!TIFFjpeg_write_tables(sp))
return (0);
return (1);
}
static int
JPEGSetupEncode(TIFF* tif)
{
JPEGState* sp = JState(tif);
TIFFDirectory *td = &tif->tif_dir;
static const char module[] = "JPEGSetupEncode";
#if defined(JPEG_DUAL_MODE_8_12) && !defined(TIFFInitJPEG)
if( tif->tif_dir.td_bitspersample == 12 )
return TIFFReInitJPEG_12( tif, COMPRESSION_JPEG, 1 );
#endif
JPEGInitializeLibJPEG( tif, FALSE );
assert(sp != NULL);
assert(!sp->cinfo.comm.is_decompressor);
sp->photometric = td->td_photometric;
/*
* Initialize all JPEG parameters to default values.
* Note that jpeg_set_defaults needs legal values for
* in_color_space and input_components.
*/
if (td->td_planarconfig == PLANARCONFIG_CONTIG) {
sp->cinfo.c.input_components = td->td_samplesperpixel;
if (sp->photometric == PHOTOMETRIC_YCBCR) {
if (sp->jpegcolormode == JPEGCOLORMODE_RGB) {
sp->cinfo.c.in_color_space = JCS_RGB;
} else {
sp->cinfo.c.in_color_space = JCS_YCbCr;
}
} else {
if ((td->td_photometric == PHOTOMETRIC_MINISWHITE || td->td_photometric == PHOTOMETRIC_MINISBLACK) && td->td_samplesperpixel == 1)
sp->cinfo.c.in_color_space = JCS_GRAYSCALE;
else if (td->td_photometric == PHOTOMETRIC_RGB && td->td_samplesperpixel == 3)
sp->cinfo.c.in_color_space = JCS_RGB;
else if (td->td_photometric == PHOTOMETRIC_SEPARATED && td->td_samplesperpixel == 4)
sp->cinfo.c.in_color_space = JCS_CMYK;
else
sp->cinfo.c.in_color_space = JCS_UNKNOWN;
}
} else {
sp->cinfo.c.input_components = 1;
sp->cinfo.c.in_color_space = JCS_UNKNOWN;
}
if (!TIFFjpeg_set_defaults(sp))
return (0);
/* Set per-file parameters */
switch (sp->photometric) {
case PHOTOMETRIC_YCBCR:
sp->h_sampling = td->td_ycbcrsubsampling[0];
sp->v_sampling = td->td_ycbcrsubsampling[1];
/*
* A ReferenceBlackWhite field *must* be present since the
* default value is inappropriate for YCbCr. Fill in the
* proper value if application didn't set it.
*/
{
float *ref;
if (!TIFFGetField(tif, TIFFTAG_REFERENCEBLACKWHITE,
&ref)) {
float refbw[6];
long top = 1L << td->td_bitspersample;
refbw[0] = 0;
refbw[1] = (float)(top-1L);
refbw[2] = (float)(top>>1);
refbw[3] = refbw[1];
refbw[4] = refbw[2];
refbw[5] = refbw[1];
TIFFSetField(tif, TIFFTAG_REFERENCEBLACKWHITE,
refbw);
}
}
break;
case PHOTOMETRIC_PALETTE: /* disallowed by Tech Note */
case PHOTOMETRIC_MASK:
TIFFErrorExt(tif->tif_clientdata, module,
"PhotometricInterpretation %d not allowed for JPEG",
(int) sp->photometric);
return (0);
default:
/* TIFF 6.0 forbids subsampling of all other color spaces */
sp->h_sampling = 1;
sp->v_sampling = 1;
break;
}
/* Verify miscellaneous parameters */
/*
* This would need work if libtiff ever supports different
* depths for different components, or if libjpeg ever supports
* run-time selection of depth. Neither is imminent.
*/
#ifdef JPEG_LIB_MK1
/* BITS_IN_JSAMPLE now permits 8 and 12 --- dgilbert */
if (td->td_bitspersample != 8 && td->td_bitspersample != 12)
#else
if (td->td_bitspersample != BITS_IN_JSAMPLE )
#endif
{
TIFFErrorExt(tif->tif_clientdata, module, "BitsPerSample %d not allowed for JPEG",
(int) td->td_bitspersample);
return (0);
}
sp->cinfo.c.data_precision = td->td_bitspersample;
#ifdef JPEG_LIB_MK1
sp->cinfo.c.bits_in_jsample = td->td_bitspersample;
#endif
if (isTiled(tif)) {
src/Source/LibTIFF4/tif_jpeg.c view on Meta::CPAN
/* so mark the field not present */
TIFFClrFieldBit(tif, FIELD_JPEGTABLES);
}
/* Direct libjpeg output to libtiff's output buffer */
TIFFjpeg_data_dest(sp, tif);
return (1);
}
/*
* Set encoding state at the start of a strip or tile.
*/
static int
JPEGPreEncode(TIFF* tif, uint16 s)
{
JPEGState *sp = JState(tif);
TIFFDirectory *td = &tif->tif_dir;
static const char module[] = "JPEGPreEncode";
uint32 segment_width, segment_height;
int downsampled_input;
assert(sp != NULL);
if (sp->cinfo.comm.is_decompressor == 1)
{
tif->tif_setupencode( tif );
}
assert(!sp->cinfo.comm.is_decompressor);
/*
* Set encoding parameters for this strip/tile.
*/
if (isTiled(tif)) {
segment_width = td->td_tilewidth;
segment_height = td->td_tilelength;
sp->bytesperline = TIFFTileRowSize(tif);
} else {
segment_width = td->td_imagewidth;
segment_height = td->td_imagelength - tif->tif_row;
if (segment_height > td->td_rowsperstrip)
segment_height = td->td_rowsperstrip;
sp->bytesperline = TIFFScanlineSize(tif);
}
if (td->td_planarconfig == PLANARCONFIG_SEPARATE && s > 0) {
/* for PC 2, scale down the strip/tile size
* to match a downsampled component
*/
segment_width = TIFFhowmany_32(segment_width, sp->h_sampling);
segment_height = TIFFhowmany_32(segment_height, sp->v_sampling);
}
if (segment_width > 65535 || segment_height > 65535) {
TIFFErrorExt(tif->tif_clientdata, module, "Strip/tile too large for JPEG");
return (0);
}
sp->cinfo.c.image_width = segment_width;
sp->cinfo.c.image_height = segment_height;
downsampled_input = FALSE;
if (td->td_planarconfig == PLANARCONFIG_CONTIG) {
sp->cinfo.c.input_components = td->td_samplesperpixel;
if (sp->photometric == PHOTOMETRIC_YCBCR) {
if (sp->jpegcolormode != JPEGCOLORMODE_RGB) {
if (sp->h_sampling != 1 || sp->v_sampling != 1)
downsampled_input = TRUE;
}
if (!TIFFjpeg_set_colorspace(sp, JCS_YCbCr))
return (0);
/*
* Set Y sampling factors;
* we assume jpeg_set_colorspace() set the rest to 1
*/
sp->cinfo.c.comp_info[0].h_samp_factor = sp->h_sampling;
sp->cinfo.c.comp_info[0].v_samp_factor = sp->v_sampling;
} else {
if (!TIFFjpeg_set_colorspace(sp, sp->cinfo.c.in_color_space))
return (0);
/* jpeg_set_colorspace set all sampling factors to 1 */
}
} else {
if (!TIFFjpeg_set_colorspace(sp, JCS_UNKNOWN))
return (0);
sp->cinfo.c.comp_info[0].component_id = s;
/* jpeg_set_colorspace() set sampling factors to 1 */
if (sp->photometric == PHOTOMETRIC_YCBCR && s > 0) {
sp->cinfo.c.comp_info[0].quant_tbl_no = 1;
sp->cinfo.c.comp_info[0].dc_tbl_no = 1;
sp->cinfo.c.comp_info[0].ac_tbl_no = 1;
}
}
/* ensure libjpeg won't write any extraneous markers */
sp->cinfo.c.write_JFIF_header = FALSE;
sp->cinfo.c.write_Adobe_marker = FALSE;
/* set up table handling correctly */
/* calling TIFFjpeg_set_quality() causes quantization tables to be flagged */
/* as being to be emitted, which we don't want in the JPEGTABLESMODE_QUANT */
/* mode, so we must manually suppress them. However TIFFjpeg_set_quality() */
/* should really be called when dealing with files with directories with */
/* mixed qualities. see http://trac.osgeo.org/gdal/ticket/3539 */
if (!TIFFjpeg_set_quality(sp, sp->jpegquality, FALSE))
return (0);
if (sp->jpegtablesmode & JPEGTABLESMODE_QUANT) {
suppress_quant_table(sp, 0);
suppress_quant_table(sp, 1);
}
else {
unsuppress_quant_table(sp, 0);
unsuppress_quant_table(sp, 1);
}
if (sp->jpegtablesmode & JPEGTABLESMODE_HUFF)
{
/* Explicit suppression is only needed if we did not go through the */
/* prepare_JPEGTables() code path, which may be the case if updating */
/* an existing file */
suppress_huff_table(sp, 0);
suppress_huff_table(sp, 1);
sp->cinfo.c.optimize_coding = FALSE;
}
else
sp->cinfo.c.optimize_coding = TRUE;
if (downsampled_input) {
/* Need to use raw-data interface to libjpeg */
sp->cinfo.c.raw_data_in = TRUE;
tif->tif_encoderow = JPEGEncodeRaw;
tif->tif_encodestrip = JPEGEncodeRaw;
tif->tif_encodetile = JPEGEncodeRaw;
} else {
/* Use normal interface to libjpeg */
sp->cinfo.c.raw_data_in = FALSE;
tif->tif_encoderow = JPEGEncode;
tif->tif_encodestrip = JPEGEncode;
tif->tif_encodetile = JPEGEncode;
}
/* Start JPEG compressor */
if (!TIFFjpeg_start_compress(sp, FALSE))
return (0);
/* Allocate downsampled-data buffers if needed */
if (downsampled_input) {
if (!alloc_downsampled_buffers(tif, sp->cinfo.c.comp_info,
sp->cinfo.c.num_components))
return (0);
}
sp->scancount = 0;
return (1);
src/Source/LibTIFF4/tif_jpeg.c view on Meta::CPAN
jpeg_component_info* compptr;
for (ci = 0, compptr = sp->cinfo.c.comp_info;
ci < sp->cinfo.c.num_components;
ci++, compptr++) {
int vsamp = compptr->v_samp_factor;
tmsize_t row_width = compptr->width_in_blocks * DCTSIZE
* sizeof(JSAMPLE);
for (ypos = sp->scancount * vsamp;
ypos < DCTSIZE * vsamp; ypos++) {
_TIFFmemcpy((void*)sp->ds_buffer[ci][ypos],
(void*)sp->ds_buffer[ci][ypos-1],
row_width);
}
}
n = sp->cinfo.c.max_v_samp_factor * DCTSIZE;
if (TIFFjpeg_write_raw_data(sp, sp->ds_buffer, n) != n)
return (0);
}
return (TIFFjpeg_finish_compress(JState(tif)));
}
static void
JPEGCleanup(TIFF* tif)
{
JPEGState *sp = JState(tif);
assert(sp != 0);
tif->tif_tagmethods.vgetfield = sp->vgetparent;
tif->tif_tagmethods.vsetfield = sp->vsetparent;
tif->tif_tagmethods.printdir = sp->printdir;
if( sp != NULL ) {
if( sp->cinfo_initialized )
TIFFjpeg_destroy(sp); /* release libjpeg resources */
if (sp->jpegtables) /* tag value */
_TIFFfree(sp->jpegtables);
}
_TIFFfree(tif->tif_data); /* release local state */
tif->tif_data = NULL;
_TIFFSetDefaultCompressionState(tif);
}
static void
JPEGResetUpsampled( TIFF* tif )
{
JPEGState* sp = JState(tif);
TIFFDirectory* td = &tif->tif_dir;
/*
* Mark whether returned data is up-sampled or not so TIFFStripSize
* and TIFFTileSize return values that reflect the true amount of
* data.
*/
tif->tif_flags &= ~TIFF_UPSAMPLED;
if (td->td_planarconfig == PLANARCONFIG_CONTIG) {
if (td->td_photometric == PHOTOMETRIC_YCBCR &&
sp->jpegcolormode == JPEGCOLORMODE_RGB) {
tif->tif_flags |= TIFF_UPSAMPLED;
} else {
#ifdef notdef
if (td->td_ycbcrsubsampling[0] != 1 ||
td->td_ycbcrsubsampling[1] != 1)
; /* XXX what about up-sampling? */
#endif
}
}
/*
* Must recalculate cached tile size in case sampling state changed.
* Should we really be doing this now if image size isn't set?
*/
if( tif->tif_tilesize > 0 )
tif->tif_tilesize = isTiled(tif) ? TIFFTileSize(tif) : (tmsize_t)(-1);
if( tif->tif_scanlinesize > 0 )
tif->tif_scanlinesize = TIFFScanlineSize(tif);
}
static int
JPEGVSetField(TIFF* tif, uint32 tag, va_list ap)
{
JPEGState* sp = JState(tif);
const TIFFField* fip;
uint32 v32;
assert(sp != NULL);
switch (tag) {
case TIFFTAG_JPEGTABLES:
v32 = (uint32) va_arg(ap, uint32);
if (v32 == 0) {
/* XXX */
return (0);
}
_TIFFsetByteArray(&sp->jpegtables, va_arg(ap, void*),
(long) v32);
sp->jpegtables_length = v32;
TIFFSetFieldBit(tif, FIELD_JPEGTABLES);
break;
case TIFFTAG_JPEGQUALITY:
sp->jpegquality = (int) va_arg(ap, int);
return (1); /* pseudo tag */
case TIFFTAG_JPEGCOLORMODE:
sp->jpegcolormode = (int) va_arg(ap, int);
JPEGResetUpsampled( tif );
return (1); /* pseudo tag */
case TIFFTAG_PHOTOMETRIC:
{
int ret_value = (*sp->vsetparent)(tif, tag, ap);
JPEGResetUpsampled( tif );
return ret_value;
}
case TIFFTAG_JPEGTABLESMODE:
sp->jpegtablesmode = (int) va_arg(ap, int);
return (1); /* pseudo tag */
case TIFFTAG_YCBCRSUBSAMPLING:
/* mark the fact that we have a real ycbcrsubsampling! */
sp->ycbcrsampling_fetched = 1;
/* should we be recomputing upsampling info here? */
return (*sp->vsetparent)(tif, tag, ap);
default:
return (*sp->vsetparent)(tif, tag, ap);
}
if ((fip = TIFFFieldWithTag(tif, tag))) {
TIFFSetFieldBit(tif, fip->field_bit);
} else {
return (0);
}
tif->tif_flags |= TIFF_DIRTYDIRECT;
return (1);
}
static int
JPEGVGetField(TIFF* tif, uint32 tag, va_list ap)
{
JPEGState* sp = JState(tif);
assert(sp != NULL);
switch (tag) {
case TIFFTAG_JPEGTABLES:
*va_arg(ap, uint32*) = sp->jpegtables_length;
*va_arg(ap, void**) = sp->jpegtables;
break;
case TIFFTAG_JPEGQUALITY:
*va_arg(ap, int*) = sp->jpegquality;
break;
case TIFFTAG_JPEGCOLORMODE:
*va_arg(ap, int*) = sp->jpegcolormode;
break;
case TIFFTAG_JPEGTABLESMODE:
*va_arg(ap, int*) = sp->jpegtablesmode;
break;
default:
return (*sp->vgetparent)(tif, tag, ap);
}
return (1);
}
static void
JPEGPrintDir(TIFF* tif, FILE* fd, long flags)
{
JPEGState* sp = JState(tif);
assert(sp != NULL);
(void) flags;
if( sp != NULL ) {
if (TIFFFieldSet(tif,FIELD_JPEGTABLES))
fprintf(fd, " JPEG Tables: (%lu bytes)\n",
(unsigned long) sp->jpegtables_length);
if (sp->printdir)
(*sp->printdir)(tif, fd, flags);
}
( run in 0.347 second using v1.01-cache-2.11-cpan-3782747c604 )