163
175
int jpegtablesmode; /* What to put in JPEGTables */
165
177
int ycbcrsampling_fetched;
166
uint32 recvparams; /* encoded Class 2 session params */
167
char* subaddress; /* subaddress string */
168
uint32 recvtime; /* time spent receiving (secs) */
169
char* faxdcs; /* encoded fax parameters (DCS, Table 2/T.30) */
172
180
#define JState(tif) ((JPEGState*)(tif)->tif_data)
174
static int JPEGDecode(TIFF*, tidata_t, tsize_t, tsample_t);
175
static int JPEGDecodeRaw(TIFF*, tidata_t, tsize_t, tsample_t);
176
static int JPEGEncode(TIFF*, tidata_t, tsize_t, tsample_t);
177
static int JPEGEncodeRaw(TIFF*, tidata_t, tsize_t, tsample_t);
178
static int JPEGInitializeLibJPEG( TIFF * tif,
179
int force_encode, int force_decode );
182
static int JPEGDecode(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s);
183
static int JPEGDecodeRaw(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s);
184
static int JPEGEncode(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s);
185
static int JPEGEncodeRaw(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s);
186
static int JPEGInitializeLibJPEG(TIFF * tif, int decode );
187
static int DecodeRowError(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s);
181
189
#define FIELD_JPEGTABLES (FIELD_CODEC+0)
182
#define FIELD_RECVPARAMS (FIELD_CODEC+1)
183
#define FIELD_SUBADDRESS (FIELD_CODEC+2)
184
#define FIELD_RECVTIME (FIELD_CODEC+3)
185
#define FIELD_FAXDCS (FIELD_CODEC+4)
187
static const TIFFFieldInfo jpegFieldInfo[] = {
188
{ TIFFTAG_JPEGTABLES, -3,-3, TIFF_UNDEFINED, FIELD_JPEGTABLES,
189
FALSE, TRUE, "JPEGTables" },
190
{ TIFFTAG_JPEGQUALITY, 0, 0, TIFF_ANY, FIELD_PSEUDO,
192
{ TIFFTAG_JPEGCOLORMODE, 0, 0, TIFF_ANY, FIELD_PSEUDO,
194
{ TIFFTAG_JPEGTABLESMODE, 0, 0, TIFF_ANY, FIELD_PSEUDO,
196
/* Specific for JPEG in faxes */
197
{ TIFFTAG_FAXRECVPARAMS, 1, 1, TIFF_LONG, FIELD_RECVPARAMS,
198
TRUE, FALSE, "FaxRecvParams" },
199
{ TIFFTAG_FAXSUBADDRESS, -1,-1, TIFF_ASCII, FIELD_SUBADDRESS,
200
TRUE, FALSE, "FaxSubAddress" },
201
{ TIFFTAG_FAXRECVTIME, 1, 1, TIFF_LONG, FIELD_RECVTIME,
202
TRUE, FALSE, "FaxRecvTime" },
203
{ TIFFTAG_FAXDCS, -1, -1, TIFF_ASCII, FIELD_FAXDCS,
204
TRUE, FALSE, "FaxDcs" },
191
static const TIFFField jpegFields[] = {
192
{ TIFFTAG_JPEGTABLES, -3, -3, TIFF_UNDEFINED, 0, TIFF_SETGET_C32_UINT8, TIFF_SETGET_C32_UINT8, FIELD_JPEGTABLES, FALSE, TRUE, "JPEGTables", NULL },
193
{ TIFFTAG_JPEGQUALITY, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, TRUE, FALSE, "", NULL },
194
{ TIFFTAG_JPEGCOLORMODE, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, FALSE, FALSE, "", NULL },
195
{ TIFFTAG_JPEGTABLESMODE, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, FALSE, FALSE, "", NULL }
206
#define N(a) (sizeof (a) / sizeof (a[0]))
209
199
* libjpeg interface layer.
657
#ifdef CHECK_JPEG_YCBCR_SUBSAMPLING
659
#define JPEG_MARKER_SOF0 0xC0
660
#define JPEG_MARKER_SOF1 0xC1
661
#define JPEG_MARKER_SOF3 0xC3
662
#define JPEG_MARKER_DHT 0xC4
663
#define JPEG_MARKER_SOI 0xD8
664
#define JPEG_MARKER_SOS 0xDA
665
#define JPEG_MARKER_DQT 0xDB
666
#define JPEG_MARKER_DRI 0xDD
667
#define JPEG_MARKER_APP0 0xE0
668
#define JPEG_MARKER_COM 0xFE
669
struct JPEGFixupTagsSubsamplingData
674
uint8* buffercurrentbyte;
675
uint32 bufferbytesleft;
677
uint64 filebytesleft;
678
uint8 filepositioned;
680
static void JPEGFixupTagsSubsampling(TIFF* tif);
681
static int JPEGFixupTagsSubsamplingSec(struct JPEGFixupTagsSubsamplingData* data);
682
static int JPEGFixupTagsSubsamplingReadByte(struct JPEGFixupTagsSubsamplingData* data, uint8* result);
683
static int JPEGFixupTagsSubsamplingReadWord(struct JPEGFixupTagsSubsamplingData* data, uint16* result);
684
static void JPEGFixupTagsSubsamplingSkip(struct JPEGFixupTagsSubsamplingData* data, uint16 skiplength);
689
JPEGFixupTags(TIFF* tif)
691
#ifdef CHECK_JPEG_YCBCR_SUBSAMPLING
692
if ((tif->tif_dir.td_photometric==PHOTOMETRIC_YCBCR)&&
693
(tif->tif_dir.td_planarconfig==PLANARCONFIG_CONTIG)&&
694
(tif->tif_dir.td_samplesperpixel==3))
695
JPEGFixupTagsSubsampling(tif);
701
#ifdef CHECK_JPEG_YCBCR_SUBSAMPLING
704
JPEGFixupTagsSubsampling(TIFF* tif)
707
* Some JPEG-in-TIFF produces do not emit the YCBCRSUBSAMPLING values in
708
* the TIFF tags, but still use non-default (2,2) values within the jpeg
709
* data stream itself. In order for TIFF applications to work properly
710
* - for instance to get the strip buffer size right - it is imperative
711
* that the subsampling be available before we start reading the image
712
* data normally. This function will attempt to analyze the first strip in
713
* order to get the sampling values from the jpeg data stream.
715
* Note that JPEGPreDeocode() will produce a fairly loud warning when the
716
* discovered sampling does not match the default sampling (2,2) or whatever
717
* was actually in the tiff tags.
719
* See the bug in bugzilla for details:
721
* http://bugzilla.remotesensing.org/show_bug.cgi?id=168
723
* Frank Warmerdam, July 2002
724
* Joris Van Damme, May 2007
726
static const char module[] = "JPEGFixupTagsSubsampling";
727
struct JPEGFixupTagsSubsamplingData m;
729
_TIFFFillStriles( tif );
731
if( tif->tif_dir.td_stripbytecount == NULL
732
|| tif->tif_dir.td_stripbytecount[0] == 0 )
734
/* Do not even try to check if the first strip/tile does not
735
yet exist, as occurs when GDAL has created a new NULL file
742
m.buffer=_TIFFmalloc(m.buffersize);
745
TIFFWarningExt(tif->tif_clientdata,module,
746
"Unable to allocate memory for auto-correcting of subsampling values; auto-correcting skipped");
749
m.buffercurrentbyte=NULL;
751
m.fileoffset=tif->tif_dir.td_stripoffset[0];
753
m.filebytesleft=tif->tif_dir.td_stripbytecount[0];
754
if (!JPEGFixupTagsSubsamplingSec(&m))
755
TIFFWarningExt(tif->tif_clientdata,module,
756
"Unable to auto-correct subsampling values, likely corrupt JPEG compressed data in first strip/tile; auto-correcting skipped");
761
JPEGFixupTagsSubsamplingSec(struct JPEGFixupTagsSubsamplingData* data)
763
static const char module[] = "JPEGFixupTagsSubsamplingSec";
769
if (!JPEGFixupTagsSubsamplingReadByte(data,&m))
776
if (!JPEGFixupTagsSubsamplingReadByte(data,&m))
783
case JPEG_MARKER_SOI:
784
/* this type of marker has no data and should be skipped */
786
case JPEG_MARKER_COM:
787
case JPEG_MARKER_APP0:
788
case JPEG_MARKER_APP0+1:
789
case JPEG_MARKER_APP0+2:
790
case JPEG_MARKER_APP0+3:
791
case JPEG_MARKER_APP0+4:
792
case JPEG_MARKER_APP0+5:
793
case JPEG_MARKER_APP0+6:
794
case JPEG_MARKER_APP0+7:
795
case JPEG_MARKER_APP0+8:
796
case JPEG_MARKER_APP0+9:
797
case JPEG_MARKER_APP0+10:
798
case JPEG_MARKER_APP0+11:
799
case JPEG_MARKER_APP0+12:
800
case JPEG_MARKER_APP0+13:
801
case JPEG_MARKER_APP0+14:
802
case JPEG_MARKER_APP0+15:
803
case JPEG_MARKER_DQT:
804
case JPEG_MARKER_SOS:
805
case JPEG_MARKER_DHT:
806
case JPEG_MARKER_DRI:
807
/* this type of marker has data, but it has no use to us and should be skipped */
810
if (!JPEGFixupTagsSubsamplingReadWord(data,&n))
816
JPEGFixupTagsSubsamplingSkip(data,n);
819
case JPEG_MARKER_SOF0:
820
case JPEG_MARKER_SOF1:
821
/* this marker contains the subsampling factors we're scanning for */
827
if (!JPEGFixupTagsSubsamplingReadWord(data,&n))
829
if (n!=8+data->tif->tif_dir.td_samplesperpixel*3)
831
JPEGFixupTagsSubsamplingSkip(data,7);
832
if (!JPEGFixupTagsSubsamplingReadByte(data,&p))
836
JPEGFixupTagsSubsamplingSkip(data,1);
837
for (o=1; o<data->tif->tif_dir.td_samplesperpixel; o++)
839
JPEGFixupTagsSubsamplingSkip(data,1);
840
if (!JPEGFixupTagsSubsamplingReadByte(data,&p))
844
TIFFWarningExt(data->tif->tif_clientdata,module,
845
"Subsampling values inside JPEG compressed data have no TIFF equivalent, auto-correction of TIFF subsampling values failed");
848
JPEGFixupTagsSubsamplingSkip(data,1);
850
if (((ph!=1)&&(ph!=2)&&(ph!=4))||((pv!=1)&&(pv!=2)&&(pv!=4)))
852
TIFFWarningExt(data->tif->tif_clientdata,module,
853
"Subsampling values inside JPEG compressed data have no TIFF equivalent, auto-correction of TIFF subsampling values failed");
856
if ((ph!=data->tif->tif_dir.td_ycbcrsubsampling[0])||(pv!=data->tif->tif_dir.td_ycbcrsubsampling[1]))
858
TIFFWarningExt(data->tif->tif_clientdata,module,
859
"Auto-corrected former TIFF subsampling values [%d,%d] to match subsampling values inside JPEG compressed data [%d,%d]",
860
(int)data->tif->tif_dir.td_ycbcrsubsampling[0],
861
(int)data->tif->tif_dir.td_ycbcrsubsampling[1],
863
data->tif->tif_dir.td_ycbcrsubsampling[0]=ph;
864
data->tif->tif_dir.td_ycbcrsubsampling[1]=pv;
875
JPEGFixupTagsSubsamplingReadByte(struct JPEGFixupTagsSubsamplingData* data, uint8* result)
877
if (data->bufferbytesleft==0)
880
if (data->filebytesleft==0)
882
if (!data->filepositioned)
884
TIFFSeekFile(data->tif,data->fileoffset,SEEK_SET);
885
data->filepositioned=1;
888
if ((uint64)m>data->filebytesleft)
889
m=(uint32)data->filebytesleft;
890
assert(m<0x80000000UL);
891
if (TIFFReadFile(data->tif,data->buffer,(tmsize_t)m)!=(tmsize_t)m)
893
data->buffercurrentbyte=data->buffer;
894
data->bufferbytesleft=m;
896
data->filebytesleft-=m;
898
*result=*data->buffercurrentbyte;
899
data->buffercurrentbyte++;
900
data->bufferbytesleft--;
905
JPEGFixupTagsSubsamplingReadWord(struct JPEGFixupTagsSubsamplingData* data, uint16* result)
909
if (!JPEGFixupTagsSubsamplingReadByte(data,&ma))
911
if (!JPEGFixupTagsSubsamplingReadByte(data,&mb))
918
JPEGFixupTagsSubsamplingSkip(struct JPEGFixupTagsSubsamplingData* data, uint16 skiplength)
920
if ((uint32)skiplength<=data->bufferbytesleft)
922
data->buffercurrentbyte+=skiplength;
923
data->bufferbytesleft-=skiplength;
928
m=skiplength-data->bufferbytesleft;
929
if (m<=data->filebytesleft)
931
data->bufferbytesleft=0;
933
data->filebytesleft-=m;
934
data->filepositioned=0;
938
data->bufferbytesleft=0;
939
data->filebytesleft=0;
639
948
JPEGSetupDecode(TIFF* tif)
641
950
JPEGState* sp = JState(tif);
642
951
TIFFDirectory *td = &tif->tif_dir;
644
JPEGInitializeLibJPEG( tif, 0, 1 );
953
#if defined(JPEG_DUAL_MODE_8_12) && !defined(TIFFInitJPEG)
954
if( tif->tif_dir.td_bitspersample == 12 )
955
return TIFFReInitJPEG_12( tif, COMPRESSION_JPEG, 0 );
958
JPEGInitializeLibJPEG( tif, TRUE );
646
960
assert(sp != NULL);
647
961
assert(sp->cinfo.comm.is_decompressor);
756
1081
#ifdef JPEG_LIB_MK1
757
1082
if (12 != td->td_bitspersample && 8 != td->td_bitspersample) {
758
TIFFErrorExt(tif->tif_clientdata, module, "Improper JPEG data precision");
1083
TIFFErrorExt(tif->tif_clientdata, module, "Improper JPEG data precision");
761
sp->cinfo.d.data_precision = td->td_bitspersample;
762
sp->cinfo.d.bits_in_jsample = td->td_bitspersample;
1086
sp->cinfo.d.data_precision = td->td_bitspersample;
1087
sp->cinfo.d.bits_in_jsample = td->td_bitspersample;
764
1089
if (sp->cinfo.d.data_precision != td->td_bitspersample) {
765
TIFFErrorExt(tif->tif_clientdata, module, "Improper JPEG data precision");
1090
TIFFErrorExt(tif->tif_clientdata, module, "Improper JPEG data precision");
769
1094
if (td->td_planarconfig == PLANARCONFIG_CONTIG) {
770
1095
/* Component 0 should have expected sampling factors */
771
1096
if (sp->cinfo.d.comp_info[0].h_samp_factor != sp->h_sampling ||
772
1097
sp->cinfo.d.comp_info[0].v_samp_factor != sp->v_sampling) {
773
TIFFWarningExt(tif->tif_clientdata, module,
774
"Improper JPEG sampling factors %d,%d\n"
775
"Apparently should be %d,%d.",
776
sp->cinfo.d.comp_info[0].h_samp_factor,
777
sp->cinfo.d.comp_info[0].v_samp_factor,
778
sp->h_sampling, sp->v_sampling);
781
* There are potential security issues here
782
* for decoders that have already allocated
783
* buffers based on the expected sampling
784
* factors. Lets check the sampling factors
785
* dont exceed what we were expecting.
787
if (sp->cinfo.d.comp_info[0].h_samp_factor
789
|| sp->cinfo.d.comp_info[0].v_samp_factor
791
TIFFErrorExt(tif->tif_clientdata,
793
"Cannot honour JPEG sampling factors"
794
" that exceed those specified.");
799
* XXX: Files written by the Intergraph software
800
* has different sampling factors stored in the
801
* TIFF tags and in the JPEG structures. We will
802
* try to deduce Intergraph files by the presense
805
if (!_TIFFFindFieldInfo(tif, 33918, TIFF_ANY)) {
806
TIFFWarningExt(tif->tif_clientdata, module,
807
"Decompressor will try reading with "
809
sp->cinfo.d.comp_info[0].h_samp_factor,
810
sp->cinfo.d.comp_info[0].v_samp_factor);
812
sp->h_sampling = (uint16)
813
sp->cinfo.d.comp_info[0].h_samp_factor;
814
sp->v_sampling = (uint16)
815
sp->cinfo.d.comp_info[0].v_samp_factor;
1098
TIFFErrorExt(tif->tif_clientdata, module,
1099
"Improper JPEG sampling factors %d,%d\n"
1100
"Apparently should be %d,%d.",
1101
sp->cinfo.d.comp_info[0].h_samp_factor,
1102
sp->cinfo.d.comp_info[0].v_samp_factor,
1103
sp->h_sampling, sp->v_sampling);
818
1106
/* Rest should have sampling factors 1,1 */
819
1107
for (ci = 1; ci < sp->cinfo.d.num_components; ci++) {
878
1166
* "Standard" case: returned data is not downsampled.
880
1168
/*ARGSUSED*/ static int
881
JPEGDecode(TIFF* tif, tidata_t buf, tsize_t cc, tsample_t s)
883
JPEGState *sp = JState(tif);
1169
JPEGDecode(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s)
1171
JPEGState *sp = JState(tif);
1176
** Update available information, buffer may have been refilled
1177
** between decode requests
1179
sp->src.next_input_byte = (const JOCTET*) tif->tif_rawcp;
1180
sp->src.bytes_in_buffer = (size_t) tif->tif_rawcc;
1182
if( sp->bytesperline == 0 )
1185
nrows = cc / sp->bytesperline;
1186
if (cc % sp->bytesperline)
1187
TIFFWarningExt(tif->tif_clientdata, tif->tif_name, "fractional scanline not read");
1189
if( nrows > (tmsize_t) sp->cinfo.d.image_height )
1190
nrows = sp->cinfo.d.image_height;
1192
/* data is expected to be read in multiples of a scanline */
1195
JSAMPROW line_work_buf = NULL;
1198
* For 6B, only use temporary buffer for 12 bit imagery.
1199
* For Mk1 always use it.
1201
#if !defined(JPEG_LIB_MK1)
1202
if( sp->cinfo.d.data_precision == 12 )
1205
line_work_buf = (JSAMPROW)
1206
_TIFFmalloc(sizeof(short) * sp->cinfo.d.output_width
1207
* sp->cinfo.d.num_components );
1211
if( line_work_buf != NULL )
1214
* In the MK1 case, we aways read into a 16bit buffer, and then
1215
* pack down to 12bit or 8bit. In 6B case we only read into 16
1216
* bit buffer for 12bit data, which we need to repack.
1218
if (TIFFjpeg_read_scanlines(sp, &line_work_buf, 1) != 1)
1221
if( sp->cinfo.d.data_precision == 12 )
1223
int value_pairs = (sp->cinfo.d.output_width
1224
* sp->cinfo.d.num_components) / 2;
1227
for( iPair = 0; iPair < value_pairs; iPair++ )
1229
unsigned char *out_ptr =
1230
((unsigned char *) buf) + iPair * 3;
1231
JSAMPLE *in_ptr = line_work_buf + iPair * 2;
1233
out_ptr[0] = (in_ptr[0] & 0xff0) >> 4;
1234
out_ptr[1] = ((in_ptr[0] & 0xf) << 4)
1235
| ((in_ptr[1] & 0xf00) >> 8);
1236
out_ptr[2] = ((in_ptr[1] & 0xff) >> 0);
1239
else if( sp->cinfo.d.data_precision == 8 )
1241
int value_count = (sp->cinfo.d.output_width
1242
* sp->cinfo.d.num_components);
1245
for( iValue = 0; iValue < value_count; iValue++ )
1247
((unsigned char *) buf)[iValue] =
1248
line_work_buf[iValue] & 0xff;
1255
* In the libjpeg6b 8bit case. We read directly into the
1258
JSAMPROW bufptr = (JSAMPROW)buf;
1260
if (TIFFjpeg_read_scanlines(sp, &bufptr, 1) != 1)
1265
buf += sp->bytesperline;
1266
cc -= sp->bytesperline;
1267
} while (--nrows > 0);
1269
if( line_work_buf != NULL )
1270
_TIFFfree( line_work_buf );
1273
/* Update information on consumed data */
1274
tif->tif_rawcp = (uint8*) sp->src.next_input_byte;
1275
tif->tif_rawcc = sp->src.bytes_in_buffer;
1277
/* Close down the decompressor if we've finished the strip or tile. */
1278
return sp->cinfo.d.output_scanline < sp->cinfo.d.output_height
1279
|| TIFFjpeg_finish_decompress(sp);
1282
/*ARGSUSED*/ static int
1283
DecodeRowError(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s)
887
nrows = cc / sp->bytesperline;
888
if (cc % sp->bytesperline)
889
TIFFWarningExt(tif->tif_clientdata, tif->tif_name, "fractional scanline not read");
891
if( nrows > (int) sp->cinfo.d.image_height )
892
nrows = sp->cinfo.d.image_height;
894
/* data is expected to be read in multiples of a scanline */
897
JSAMPROW line_work_buf = NULL;
900
** For 6B, only use temporary buffer for 12 bit imagery.
901
** For Mk1 always use it.
903
#if !defined(JPEG_LIB_MK1)
904
if( sp->cinfo.d.data_precision == 12 )
907
line_work_buf = (JSAMPROW)
908
_TIFFmalloc(sizeof(short) * sp->cinfo.d.output_width
909
* sp->cinfo.d.num_components );
913
if( line_work_buf != NULL )
916
** In the MK1 case, we aways read into a 16bit buffer, and then
917
** pack down to 12bit or 8bit. In 6B case we only read into 16
918
** bit buffer for 12bit data, which we need to repack.
920
if (TIFFjpeg_read_scanlines(sp, &line_work_buf, 1) != 1)
923
if( sp->cinfo.d.data_precision == 12 )
925
int value_pairs = (sp->cinfo.d.output_width
926
* sp->cinfo.d.num_components) / 2;
929
for( iPair = 0; iPair < value_pairs; iPair++ )
931
unsigned char *out_ptr =
932
((unsigned char *) buf) + iPair * 3;
933
JSAMPLE *in_ptr = line_work_buf + iPair * 2;
935
out_ptr[0] = (in_ptr[0] & 0xff0) >> 4;
936
out_ptr[1] = ((in_ptr[0] & 0xf) << 4)
937
| ((in_ptr[1] & 0xf00) >> 8);
938
out_ptr[2] = ((in_ptr[1] & 0xff) >> 0);
941
else if( sp->cinfo.d.data_precision == 8 )
943
int value_count = (sp->cinfo.d.output_width
944
* sp->cinfo.d.num_components);
947
for( iValue = 0; iValue < value_count; iValue++ )
949
((unsigned char *) buf)[iValue] =
950
line_work_buf[iValue] & 0xff;
957
** In the libjpeg6b 8bit case. We read directly into the
960
JSAMPROW bufptr = (JSAMPROW)buf;
962
if (TIFFjpeg_read_scanlines(sp, &bufptr, 1) != 1)
967
buf += sp->bytesperline;
968
cc -= sp->bytesperline;
969
} while (--nrows > 0);
971
if( line_work_buf != NULL )
972
_TIFFfree( line_work_buf );
975
/* Close down the decompressor if we've finished the strip or tile. */
976
return sp->cinfo.d.output_scanline < sp->cinfo.d.output_height
977
|| TIFFjpeg_finish_decompress(sp);
1290
TIFFErrorExt(tif->tif_clientdata, "TIFFReadScanline",
1291
"scanline oriented access is not supported for downsampled JPEG compressed images, consider enabling TIFF_JPEGCOLORMODE as JPEGCOLORMODE_RGB." );
982
1297
* Returned data is downsampled per sampling factors.
984
1299
/*ARGSUSED*/ static int
985
JPEGDecodeRaw(TIFF* tif, tidata_t buf, tsize_t cc, tsample_t s)
1300
JPEGDecodeRaw(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s)
987
1302
JPEGState *sp = JState(tif);
991
nrows = cc / sp->bytesperline;
992
if (cc % sp->bytesperline)
993
TIFFWarningExt(tif->tif_clientdata, tif->tif_name, "fractional scanline not read");
995
if( nrows > (int) sp->cinfo.d.image_height )
996
nrows = sp->cinfo.d.image_height;
998
/* data is expected to be read in multiples of a scanline */
1306
/* data is expected to be read in multiples of a scanline */
1307
if ( (nrows = sp->cinfo.d.image_height) ) {
1000
1309
/* Cb,Cr both have sampling factors 1, so this is correct */
1001
1310
JDIMENSION clumps_per_line = sp->cinfo.d.comp_info[1].downsampled_width;
1002
1311
int samples_per_clump = sp->samplesperclump;
1313
#if defined(JPEG_LIB_MK1_OR_12BIT)
1005
1314
unsigned short* tmpbuf = _TIFFmalloc(sizeof(unsigned short) *
1006
sp->cinfo.d.output_width *
1007
sp->cinfo.d.num_components);
1315
sp->cinfo.d.output_width *
1316
sp->cinfo.d.num_components);
1318
TIFFErrorExt(tif->tif_clientdata, "JPEGDecodeRaw",
1011
1325
jpeg_component_info *compptr;
1012
1326
int ci, clumpoffset;
1328
if( cc < sp->bytesperline ) {
1329
TIFFErrorExt(tif->tif_clientdata, "JPEGDecodeRaw",
1330
"application buffer not large enough for all data.");
1014
1334
/* Reload downsampled-data buffer if needed */
1015
1335
if (sp->scancount >= DCTSIZE) {
1016
1336
int n = sp->cinfo.d.max_v_samp_factor * DCTSIZE;
1429
1764
* "Standard" case: incoming data is not downsampled.
1432
JPEGEncode(TIFF* tif, tidata_t buf, tsize_t cc, tsample_t s)
1767
JPEGEncode(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s)
1434
1769
JPEGState *sp = JState(tif);
1436
1771
JSAMPROW bufptr[1];
1772
short *line16 = NULL;
1773
int line16_count = 0;
1439
1776
assert(sp != NULL);
1440
1777
/* data is expected to be supplied in multiples of a scanline */
1441
1778
nrows = cc / sp->bytesperline;
1442
1779
if (cc % sp->bytesperline)
1443
TIFFWarningExt(tif->tif_clientdata, tif->tif_name, "fractional scanline discarded");
1780
TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
1781
"fractional scanline discarded");
1445
1783
/* The last strip will be limited to image size */
1446
1784
if( !isTiled(tif) && tif->tif_row+nrows > tif->tif_dir.td_imagelength )
1447
1785
nrows = tif->tif_dir.td_imagelength - tif->tif_row;
1787
if( sp->cinfo.c.data_precision == 12 )
1789
line16_count = (sp->bytesperline * 2) / 3;
1790
line16 = (short *) _TIFFmalloc(sizeof(short) * line16_count);
1791
// FIXME: undiagnosed malloc failure
1449
1794
while (nrows-- > 0) {
1796
if( sp->cinfo.c.data_precision == 12 )
1799
int value_pairs = line16_count / 2;
1802
bufptr[0] = (JSAMPROW) line16;
1804
for( iPair = 0; iPair < value_pairs; iPair++ )
1806
unsigned char *in_ptr =
1807
((unsigned char *) buf) + iPair * 3;
1808
JSAMPLE *out_ptr = (JSAMPLE *) (line16 + iPair * 2);
1810
out_ptr[0] = (in_ptr[0] << 4) | ((in_ptr[1] & 0xf0) >> 4);
1811
out_ptr[1] = ((in_ptr[1] & 0x0f) << 8) | in_ptr[2];
1450
1816
bufptr[0] = (JSAMPROW) buf;
1451
if (TIFFjpeg_write_scanlines(sp, bufptr, 1) != 1)
1455
buf += sp->bytesperline;
1818
if (TIFFjpeg_write_scanlines(sp, bufptr, 1) != 1)
1822
buf += sp->bytesperline;
1825
if( sp->cinfo.c.data_precision == 12 )
1827
_TIFFfree( line16 );
1659
2033
TIFFSetFieldBit(tif, FIELD_JPEGTABLES);
1661
2035
case TIFFTAG_JPEGQUALITY:
1662
sp->jpegquality = va_arg(ap, int);
2036
sp->jpegquality = (int) va_arg(ap, int);
1663
2037
return (1); /* pseudo tag */
1664
2038
case TIFFTAG_JPEGCOLORMODE:
1665
sp->jpegcolormode = va_arg(ap, int);
1666
JPEGResetUpsampled( tif );
2039
sp->jpegcolormode = (int) va_arg(ap, int);
2040
JPEGResetUpsampled( tif );
1667
2041
return (1); /* pseudo tag */
1668
2042
case TIFFTAG_PHOTOMETRIC:
1670
int ret_value = (*sp->vsetparent)(tif, tag, ap);
1671
JPEGResetUpsampled( tif );
2044
int ret_value = (*sp->vsetparent)(tif, tag, ap);
2045
JPEGResetUpsampled( tif );
1674
2048
case TIFFTAG_JPEGTABLESMODE:
1675
sp->jpegtablesmode = va_arg(ap, int);
2049
sp->jpegtablesmode = (int) va_arg(ap, int);
1676
2050
return (1); /* pseudo tag */
1677
2051
case TIFFTAG_YCBCRSUBSAMPLING:
1678
/* mark the fact that we have a real ycbcrsubsampling! */
2052
/* mark the fact that we have a real ycbcrsubsampling! */
1679
2053
sp->ycbcrsampling_fetched = 1;
1680
/* should we be recomputing upsampling info here? */
2054
/* should we be recomputing upsampling info here? */
1681
2055
return (*sp->vsetparent)(tif, tag, ap);
1682
case TIFFTAG_FAXRECVPARAMS:
1683
sp->recvparams = va_arg(ap, uint32);
1685
case TIFFTAG_FAXSUBADDRESS:
1686
_TIFFsetString(&sp->subaddress, va_arg(ap, char*));
1688
case TIFFTAG_FAXRECVTIME:
1689
sp->recvtime = va_arg(ap, uint32);
1691
case TIFFTAG_FAXDCS:
1692
_TIFFsetString(&sp->faxdcs, va_arg(ap, char*));
1695
2057
return (*sp->vsetparent)(tif, tag, ap);
1698
if ((fip = _TIFFFieldWithTag(tif, tag))) {
2060
if ((fip = TIFFFieldWithTag(tif, tag))) {
1699
2061
TIFFSetFieldBit(tif, fip->field_bit);
1709
* Some JPEG-in-TIFF produces do not emit the YCBCRSUBSAMPLING values in
1710
* the TIFF tags, but still use non-default (2,2) values within the jpeg
1711
* data stream itself. In order for TIFF applications to work properly
1712
* - for instance to get the strip buffer size right - it is imperative
1713
* that the subsampling be available before we start reading the image
1714
* data normally. This function will attempt to load the first strip in
1715
* order to get the sampling values from the jpeg data stream. Various
1716
* hacks are various places are done to ensure this function gets called
1717
* before the td_ycbcrsubsampling values are used from the directory structure,
1718
* including calling TIFFGetField() for the YCBCRSUBSAMPLING field from
1719
* TIFFStripSize(), and the printing code in tif_print.c.
1721
* Note that JPEGPreDeocode() will produce a fairly loud warning when the
1722
* discovered sampling does not match the default sampling (2,2) or whatever
1723
* was actually in the tiff tags.
1726
* o This code will cause one whole strip/tile of compressed data to be
1727
* loaded just to get the tags right, even if the imagery is never read.
1728
* It would be more efficient to just load a bit of the header, and
1729
* initialize things from that.
1731
* See the bug in bugzilla for details:
1733
* http://bugzilla.remotesensing.org/show_bug.cgi?id=168
1735
* Frank Warmerdam, July 2002
1739
JPEGFixupTestSubsampling( TIFF * tif )
1741
#ifdef CHECK_JPEG_YCBCR_SUBSAMPLING
1742
JPEGState *sp = JState(tif);
1743
TIFFDirectory *td = &tif->tif_dir;
1745
JPEGInitializeLibJPEG( tif, 0, 0 );
1748
* Some JPEG-in-TIFF files don't provide the ycbcrsampling tags,
1749
* and use a sampling schema other than the default 2,2. To handle
1750
* this we actually have to scan the header of a strip or tile of
1751
* jpeg data to get the sampling.
1753
if( !sp->cinfo.comm.is_decompressor
1754
|| sp->ycbcrsampling_fetched
1755
|| td->td_photometric != PHOTOMETRIC_YCBCR )
1758
sp->ycbcrsampling_fetched = 1;
1759
if( TIFFIsTiled( tif ) )
1761
if( !TIFFFillTile( tif, 0 ) )
1766
if( !TIFFFillStrip( tif, 0 ) )
1770
TIFFSetField( tif, TIFFTAG_YCBCRSUBSAMPLING,
1771
(uint16) sp->h_sampling, (uint16) sp->v_sampling );
1774
** We want to clear the loaded strip so the application has time
1775
** to set JPEGCOLORMODE or other behavior modifiers. This essentially
1776
** undoes the JPEGPreDecode triggers by TIFFFileStrip(). (#1936)
1778
tif->tif_curstrip = -1;
1780
#endif /* CHECK_JPEG_YCBCR_SUBSAMPLING */
1784
JPEGVGetField(TIFF* tif, ttag_t tag, va_list ap)
2071
JPEGVGetField(TIFF* tif, uint32 tag, va_list ap)
1786
2073
JPEGState* sp = JState(tif);
1828
2100
JPEGState* sp = JState(tif);
1830
2102
assert(sp != NULL);
1833
if (TIFFFieldSet(tif,FIELD_JPEGTABLES))
1834
fprintf(fd, " JPEG Tables: (%lu bytes)\n",
1835
(unsigned long) sp->jpegtables_length);
1836
if (TIFFFieldSet(tif,FIELD_RECVPARAMS))
1837
fprintf(fd, " Fax Receive Parameters: %08lx\n",
1838
(unsigned long) sp->recvparams);
1839
if (TIFFFieldSet(tif,FIELD_SUBADDRESS))
1840
fprintf(fd, " Fax SubAddress: %s\n", sp->subaddress);
1841
if (TIFFFieldSet(tif,FIELD_RECVTIME))
1842
fprintf(fd, " Fax Receive Time: %lu secs\n",
1843
(unsigned long) sp->recvtime);
1844
if (TIFFFieldSet(tif,FIELD_FAXDCS))
1845
fprintf(fd, " Fax DCS: %s\n", sp->faxdcs);
2106
if (TIFFFieldSet(tif,FIELD_JPEGTABLES))
2107
fprintf(fd, " JPEG Tables: (%lu bytes)\n",
2108
(unsigned long) sp->jpegtables_length);
2110
(*sp->printdir)(tif, fd, flags);