125#define WIN32_LEAN_AND_MEAN
155#define SETJMP(jbuf) setjmp(jbuf)
156#define LONGJMP(jbuf, code) longjmp(jbuf, code)
157#define JMP_BUF jmp_buf
158#define OJPEG_BUFFER 2048
161#define JPEG_MARKER_SOF0 0xC0
162#define JPEG_MARKER_SOF1 0xC1
163#define JPEG_MARKER_SOF3 0xC3
164#define JPEG_MARKER_DHT 0xC4
165#define JPEG_MARKER_RST0 0XD0
166#define JPEG_MARKER_SOI 0xD8
167#define JPEG_MARKER_EOI 0xD9
168#define JPEG_MARKER_SOS 0xDA
169#define JPEG_MARKER_DQT 0xDB
170#define JPEG_MARKER_DRI 0xDD
171#define JPEG_MARKER_APP0 0xE0
172#define JPEG_MARKER_COM 0xFE
174#define FIELD_OJPEG_JPEGINTERCHANGEFORMAT (FIELD_CODEC + 0)
175#define FIELD_OJPEG_JPEGINTERCHANGEFORMATLENGTH (FIELD_CODEC + 1)
176#define FIELD_OJPEG_JPEGQTABLES (FIELD_CODEC + 2)
177#define FIELD_OJPEG_JPEGDCTABLES (FIELD_CODEC + 3)
178#define FIELD_OJPEG_JPEGACTABLES (FIELD_CODEC + 4)
179#define FIELD_OJPEG_JPEGPROC (FIELD_CODEC + 5)
180#define FIELD_OJPEG_JPEGRESTARTINTERVAL (FIELD_CODEC + 6)
184 FIELD_OJPEG_JPEGINTERCHANGEFORMAT,
TRUE,
FALSE,
"JpegInterchangeFormat",
187 FIELD_OJPEG_JPEGINTERCHANGEFORMATLENGTH,
TRUE,
FALSE,
188 "JpegInterchangeFormatLength",
NULL},
191 "JpegQTables",
NULL},
194 "JpegDcTables",
NULL},
197 "JpegAcTables",
NULL},
201 FIELD_OJPEG_JPEGRESTARTINTERVAL,
FALSE,
FALSE,
"JpegRestartInterval",
205#ifndef LIBJPEG_ENCAP_EXTERNAL
212#ifndef TIFF_jpeg_source_mgr_defined
213#define TIFF_jpeg_source_mgr_defined
217#ifndef TIFF_jpeg_error_mgr_defined
218#define TIFF_jpeg_error_mgr_defined
228 osibsJpegInterchangeFormat,
231} OJPEGStateInBufferSource;
260 int error_in_raw_data_decoding;
261#ifndef LIBJPEG_ENCAP_EXTERNAL
275 uint8_t samples_per_pixel_per_plane;
277 uint64_t jpeg_interchange_format_length;
280 uint8_t subsamplingcorrect_done;
284 uint8_t subsampling_force_desubsampling_inside_decompression;
308 OJPEGStateInBufferSource in_buffer_source;
317 uint8_t libjpeg_session_active;
318 uint8_t libjpeg_jpeg_query_style;
322 uint8_t subsampling_convert_log;
323 uint32_t subsampling_convert_ylinelen;
324 uint32_t subsampling_convert_ylines;
325 uint32_t subsampling_convert_clinelen;
326 uint32_t subsampling_convert_clines;
327 uint32_t subsampling_convert_ybuflen;
328 uint32_t subsampling_convert_cbuflen;
329 uint32_t subsampling_convert_ycbcrbuflen;
330 uint8_t *subsampling_convert_ycbcrbuf;
331 uint8_t *subsampling_convert_ybuf;
332 uint8_t *subsampling_convert_cbbuf;
333 uint8_t *subsampling_convert_crbuf;
334 uint32_t subsampling_convert_ycbcrimagelen;
335 uint8_t **subsampling_convert_ycbcrimage;
336 uint32_t subsampling_convert_clinelenout;
342 OJPEGStateInBufferSource in_buffer_source;
346 uint8_t in_buffer_file_pos_log;
351 OJPEGStateOutState out_state;
360static int OJPEGFixupTags(
TIFF *tif);
361static int OJPEGSetupDecode(
TIFF *tif);
363static int OJPEGPreDecodeSkipRaw(
TIFF *tif);
364static int OJPEGPreDecodeSkipScanlines(
TIFF *tif);
369static int OJPEGSetupEncode(
TIFF *tif);
372static int OJPEGPostEncode(
TIFF *tif);
373static void OJPEGCleanup(
TIFF *tif);
375static void OJPEGSubsamplingCorrect(
TIFF *tif);
376static int OJPEGReadHeaderInfo(
TIFF *tif);
378static int OJPEGWriteHeaderInfo(
TIFF *tif);
379static void OJPEGLibjpegSessionAbort(
TIFF *tif);
381static int OJPEGReadHeaderInfoSec(
TIFF *tif);
382static int OJPEGReadHeaderInfoSecStreamDri(
TIFF *tif);
383static int OJPEGReadHeaderInfoSecStreamDqt(
TIFF *tif);
384static int OJPEGReadHeaderInfoSecStreamDht(
TIFF *tif);
385static int OJPEGReadHeaderInfoSecStreamSof(
TIFF *tif,
uint8_t marker_id);
386static int OJPEGReadHeaderInfoSecStreamSos(
TIFF *tif);
387static int OJPEGReadHeaderInfoSecTablesQTable(
TIFF *tif);
388static int OJPEGReadHeaderInfoSecTablesDcTable(
TIFF *tif);
389static int OJPEGReadHeaderInfoSecTablesAcTable(
TIFF *tif);
391static int OJPEGReadBufferFill(OJPEGState *
sp);
392static int OJPEGReadByte(OJPEGState *
sp,
uint8_t *
byte);
393static int OJPEGReadBytePeek(OJPEGState *
sp,
uint8_t *
byte);
394static void OJPEGReadByteAdvance(OJPEGState *
sp);
401static void OJPEGWriteStreamQTable(
TIFF *tif,
uint8_t table_index,
void **
mem,
403static void OJPEGWriteStreamDcTable(
TIFF *tif,
uint8_t table_index,
void **
mem,
405static void OJPEGWriteStreamAcTable(
TIFF *tif,
uint8_t table_index,
void **
mem,
414#ifdef LIBJPEG_ENCAP_EXTERNAL
415extern int jpeg_create_decompress_encap(OJPEGState *
sp,
419extern int jpeg_start_decompress_encap(OJPEGState *
sp,
421extern int jpeg_read_scanlines_encap(OJPEGState *
sp,
424extern int jpeg_read_raw_data_encap(OJPEGState *
sp,
427extern void jpeg_encap_unwind(
TIFF *tif);
429static int jpeg_create_decompress_encap(OJPEGState *
sp,
433static int jpeg_start_decompress_encap(OJPEGState *
sp,
435static int jpeg_read_scanlines_encap(OJPEGState *
sp,
438static int jpeg_read_raw_data_encap(OJPEGState *
sp,
441static void jpeg_encap_unwind(
TIFF *tif);
459 static const char module[] =
"TIFFInitOJPEG";
471 "Merging Old JPEG codec-specific tags failed");
485 sp->subsampling_hor = 2;
486 sp->subsampling_ver = 2;
534 if (
sp->subsamplingcorrect_done == 0)
535 OJPEGSubsamplingCorrect(tif);
541 *
va_arg(
ap,
const void **) = (
const void *)
sp->qtable_offset;
545 *
va_arg(
ap,
const void **) = (
const void *)
sp->dctable_offset;
549 *
va_arg(
ap,
const void **) = (
const void *)
sp->actable_offset;
558 return (*
sp->vgetparent)(tif,
tag,
ap);
565 static const char module[] =
"OJPEGVSetField";
581 sp->subsampling_tag = 1;
594 "JpegQTables tag has incorrect count");
599 for (
n = 0;
n < ma;
n++)
600 sp->qtable_offset[
n] = mb[
n];
610 "JpegDcTables tag has incorrect count");
615 for (
n = 0;
n < ma;
n++)
616 sp->dctable_offset[
n] = mb[
n];
626 "JpegAcTables tag has incorrect count");
631 for (
n = 0;
n < ma;
n++)
632 sp->actable_offset[
n] = mb[
n];
642 return (*
sp->vsetparent)(tif,
tag,
ap);
658 if (
TIFFFieldSet(tif, FIELD_OJPEG_JPEGINTERCHANGEFORMAT))
661 if (
TIFFFieldSet(tif, FIELD_OJPEG_JPEGINTERCHANGEFORMATLENGTH))
663 (
uint64_t)
sp->jpeg_interchange_format_length);
667 for (
m = 0;
m <
sp->qtable_offset_count;
m++)
674 for (
m = 0;
m <
sp->dctable_offset_count;
m++)
681 for (
m = 0;
m <
sp->actable_offset_count;
m++)
689 sp->restart_interval);
694static int OJPEGFixupTags(
TIFF *tif)
700static int OJPEGSetupDecode(
TIFF *tif)
702 static const char module[] =
"OJPEGSetupDecode";
704 "Deprecated and troublesome old-style JPEG compression "
705 "mode, please convert to new-style JPEG compression and "
706 "notify vendor of writing software");
714 if (
sp->subsamplingcorrect_done == 0)
715 OJPEGSubsamplingCorrect(tif);
716 if (
sp->readheader_done == 0)
718 if (OJPEGReadHeaderInfo(tif) == 0)
721 if (
sp->sos_end[
s].log == 0)
723 if (OJPEGReadSecondarySos(tif,
s) == 0)
730 if ((
sp->writeheader_done != 0) &&
731 ((
sp->write_cursample !=
s) || (
sp->write_curstrile >
m)))
733 if (
sp->libjpeg_session_active != 0)
734 OJPEGLibjpegSessionAbort(tif);
735 sp->writeheader_done = 0;
737 if (
sp->writeheader_done == 0)
740 sp->write_cursample =
s;
742 if ((
sp->in_buffer_file_pos_log == 0) ||
743 (
sp->in_buffer_file_pos -
sp->in_buffer_togo !=
744 sp->sos_end[
s].in_buffer_file_pos))
746 sp->in_buffer_source =
sp->sos_end[
s].in_buffer_source;
747 sp->in_buffer_next_strile =
sp->sos_end[
s].in_buffer_next_strile;
748 sp->in_buffer_file_pos =
sp->sos_end[
s].in_buffer_file_pos;
749 sp->in_buffer_file_pos_log = 0;
750 sp->in_buffer_file_togo =
sp->sos_end[
s].in_buffer_file_togo;
751 sp->in_buffer_togo = 0;
752 sp->in_buffer_cur = 0;
754 if (OJPEGWriteHeaderInfo(tif) == 0)
758 sp->subsampling_convert_state = 0;
760 while (
sp->write_curstrile <
m)
762 if (
sp->libjpeg_jpeg_query_style == 0)
764 if (OJPEGPreDecodeSkipRaw(tif) == 0)
769 if (OJPEGPreDecodeSkipScanlines(tif) == 0)
772 sp->write_curstrile++;
778static int OJPEGPreDecodeSkipRaw(
TIFF *tif)
782 m =
sp->lines_per_strile;
783 if (
sp->subsampling_convert_state != 0)
785 if (
sp->subsampling_convert_clines -
sp->subsampling_convert_state >=
m)
787 sp->subsampling_convert_state +=
m;
788 if (
sp->subsampling_convert_state ==
sp->subsampling_convert_clines)
789 sp->subsampling_convert_state = 0;
792 m -=
sp->subsampling_convert_clines -
sp->subsampling_convert_state;
793 sp->subsampling_convert_state = 0;
794 sp->error_in_raw_data_decoding = 0;
796 while (
m >=
sp->subsampling_convert_clines)
798 if (jpeg_read_raw_data_encap(
sp, &(
sp->libjpeg_jpeg_decompress_struct),
799 sp->subsampling_convert_ycbcrimage,
800 sp->subsampling_ver * 8) == 0)
802 m -=
sp->subsampling_convert_clines;
806 if (jpeg_read_raw_data_encap(
sp, &(
sp->libjpeg_jpeg_decompress_struct),
807 sp->subsampling_convert_ycbcrimage,
808 sp->subsampling_ver * 8) == 0)
810 sp->subsampling_convert_state =
m;
815static int OJPEGPreDecodeSkipScanlines(
TIFF *tif)
817 static const char module[] =
"OJPEGPreDecodeSkipScanlines";
823 if (
sp->skip_buffer ==
NULL)
829 for (
m = 0;
m <
sp->lines_per_strile;
m++)
831 if (jpeg_read_scanlines_encap(
sp, &(
sp->libjpeg_jpeg_decompress_struct),
832 &
sp->skip_buffer, 1) == 0)
840 static const char module[] =
"OJPEGDecode";
847 "Cannot decode: decoder not correctly initialized");
850 if (
sp->libjpeg_session_active == 0)
861 "Cannot decode: libjpeg_session_active == 0");
864 if (
sp->error_in_raw_data_decoding)
869 if (
sp->libjpeg_jpeg_query_style == 0)
871 if (OJPEGDecodeRaw(tif,
buf,
cc) == 0)
879 if (OJPEGDecodeScanlines(tif,
buf,
cc) == 0)
890 static const char module[] =
"OJPEGDecodeRaw";
901 if (
cc %
sp->bytes_per_line != 0)
911 if (
sp->subsampling_convert_state == 0)
913 if (jpeg_read_raw_data_encap(
sp,
914 &(
sp->libjpeg_jpeg_decompress_struct),
915 sp->subsampling_convert_ycbcrimage,
916 sp->subsampling_ver * 8) == 0)
918 sp->error_in_raw_data_decoding = 1;
922 oy =
sp->subsampling_convert_ybuf +
923 sp->subsampling_convert_state *
sp->subsampling_ver *
924 sp->subsampling_convert_ylinelen;
925 ocb =
sp->subsampling_convert_cbbuf +
926 sp->subsampling_convert_state *
sp->subsampling_convert_clinelen;
927 ocr =
sp->subsampling_convert_crbuf +
928 sp->subsampling_convert_state *
sp->subsampling_convert_clinelen;
930 for (
q = 0;
q <
sp->subsampling_convert_clinelenout;
q++)
933 for (sy = 0; sy <
sp->subsampling_ver; sy++)
935 for (sx = 0; sx <
sp->subsampling_hor; sx++)
937 r +=
sp->subsampling_convert_ylinelen -
sp->subsampling_hor;
939 oy +=
sp->subsampling_hor;
943 sp->subsampling_convert_state++;
944 if (
sp->subsampling_convert_state ==
sp->subsampling_convert_clines)
945 sp->subsampling_convert_state = 0;
946 m +=
sp->bytes_per_line;
947 n -=
sp->bytes_per_line;
954 static const char module[] =
"OJPEGDecodeScanlines";
958 if (
cc %
sp->bytes_per_line != 0)
968 if (jpeg_read_scanlines_encap(
sp, &(
sp->libjpeg_jpeg_decompress_struct),
971 m +=
sp->bytes_per_line;
972 n -=
sp->bytes_per_line;
990 sp->write_curstrile++;
993 assert(
sp->libjpeg_session_active != 0);
994 OJPEGLibjpegSessionAbort(tif);
995 sp->writeheader_done = 0;
999static int OJPEGSetupEncode(
TIFF *tif)
1001 static const char module[] =
"OJPEGSetupEncode";
1004 "OJPEG encoding not supported; use new-style JPEG compression instead");
1010 static const char module[] =
"OJPEGPreEncode";
1014 "OJPEG encoding not supported; use new-style JPEG compression instead");
1020 static const char module[] =
"OJPEGEncode";
1026 "OJPEG encoding not supported; use new-style JPEG compression instead");
1030static int OJPEGPostEncode(
TIFF *tif)
1032 static const char module[] =
"OJPEGPostEncode";
1035 "OJPEG encoding not supported; use new-style JPEG compression instead");
1039static void OJPEGCleanup(
TIFF *tif)
1041 OJPEGState *
sp = (OJPEGState *)tif->
tif_data;
1047 if (
sp->qtable[0] != 0)
1049 if (
sp->qtable[1] != 0)
1051 if (
sp->qtable[2] != 0)
1053 if (
sp->qtable[3] != 0)
1055 if (
sp->dctable[0] != 0)
1057 if (
sp->dctable[1] != 0)
1059 if (
sp->dctable[2] != 0)
1061 if (
sp->dctable[3] != 0)
1063 if (
sp->actable[0] != 0)
1065 if (
sp->actable[1] != 0)
1067 if (
sp->actable[2] != 0)
1069 if (
sp->actable[3] != 0)
1071 if (
sp->libjpeg_session_active != 0)
1072 OJPEGLibjpegSessionAbort(tif);
1073 if (
sp->subsampling_convert_ycbcrbuf != 0)
1075 if (
sp->subsampling_convert_ycbcrimage != 0)
1077 if (
sp->skip_buffer != 0)
1085static void OJPEGSubsamplingCorrect(
TIFF *tif)
1087 static const char module[] =
"OJPEGSubsamplingCorrect";
1088 OJPEGState *
sp = (OJPEGState *)tif->
tif_data;
1092 assert(
sp->subsamplingcorrect_done == 0);
1097 if (
sp->subsampling_tag != 0)
1099 "Subsampling tag not appropriate for this "
1100 "Photometric and/or SamplesPerPixel");
1101 sp->subsampling_hor = 1;
1102 sp->subsampling_ver = 1;
1103 sp->subsampling_force_desubsampling_inside_decompression = 0;
1107 sp->subsamplingcorrect_done = 1;
1108 mh =
sp->subsampling_hor;
1109 mv =
sp->subsampling_ver;
1110 sp->subsamplingcorrect = 1;
1111 OJPEGReadHeaderInfoSec(tif);
1112 if (
sp->subsampling_force_desubsampling_inside_decompression != 0)
1114 sp->subsampling_hor = 1;
1115 sp->subsampling_ver = 1;
1117 sp->subsamplingcorrect = 0;
1118 if (((
sp->subsampling_hor != mh) || (
sp->subsampling_ver != mv)) &&
1119 (
sp->subsampling_force_desubsampling_inside_decompression == 0))
1121 if (
sp->subsampling_tag == 0)
1124 "Subsampling tag is not set, yet subsampling inside JPEG "
1126 "] does not match default values [2,2]; assuming "
1127 "subsampling inside JPEG data is correct",
1128 sp->subsampling_hor,
sp->subsampling_ver);
1132 "Subsampling inside JPEG data [%" PRIu8 ",%" PRIu8
1133 "] does not match subsampling tag values [%" PRIu8
1135 "]; assuming subsampling inside JPEG data is correct",
1136 sp->subsampling_hor,
sp->subsampling_ver, mh, mv);
1138 if (
sp->subsampling_force_desubsampling_inside_decompression != 0)
1140 if (
sp->subsampling_tag == 0)
1143 "Subsampling tag is not set, yet subsampling inside JPEG "
1144 "data does not match default values [2,2] (nor any other "
1145 "values allowed in TIFF); assuming subsampling inside JPEG "
1146 "data is correct and desubsampling inside JPEG "
1151 "Subsampling inside JPEG data does not match subsampling "
1153 "] (nor any other values allowed in TIFF); assuming "
1154 "subsampling inside JPEG data is correct and desubsampling "
1155 "inside JPEG decompression",
1158 if (
sp->subsampling_force_desubsampling_inside_decompression == 0)
1160 if (
sp->subsampling_hor <
sp->subsampling_ver)
1163 "] are not allowed in TIFF",
1164 sp->subsampling_hor,
sp->subsampling_ver);
1167 sp->subsamplingcorrect_done = 1;
1170static int OJPEGReadHeaderInfo(
TIFF *tif)
1172 static const char module[] =
"OJPEGReadHeaderInfo";
1173 OJPEGState *
sp = (OJPEGState *)tif->
tif_data;
1181 sp->strile_length_total =
1182 ((
sp->image_length +
sp->strile_length - 1) /
sp->strile_length) *
1187 sp->strile_width =
sp->image_width;
1190 sp->strile_length =
sp->image_length;
1191 sp->strile_length_total =
sp->image_length;
1195 sp->samples_per_pixel = 1;
1196 sp->plane_sample_offset = 0;
1197 sp->samples_per_pixel_per_plane =
sp->samples_per_pixel;
1198 sp->subsampling_hor = 1;
1199 sp->subsampling_ver = 1;
1206 "SamplesPerPixel %" PRIu8
1207 " not supported for this compression scheme",
1208 sp->samples_per_pixel);
1211 sp->samples_per_pixel = 3;
1212 sp->plane_sample_offset = 0;
1214 sp->samples_per_pixel_per_plane = 3;
1216 sp->samples_per_pixel_per_plane = 1;
1218 if (
sp->strile_length <
sp->image_length)
1220 if (((
sp->subsampling_hor != 1) && (
sp->subsampling_hor != 2) &&
1221 (
sp->subsampling_hor != 4)) ||
1222 ((
sp->subsampling_ver != 1) && (
sp->subsampling_ver != 2) &&
1223 (
sp->subsampling_ver != 4)))
1228 if (
sp->strile_length % (
sp->subsampling_ver * 8) != 0)
1231 "Incompatible vertical subsampling and image "
1232 "strip/tile length");
1235 sp->restart_interval =
1236 (
uint16_t)(((
sp->strile_width +
sp->subsampling_hor * 8 - 1) /
1237 (
sp->subsampling_hor * 8)) *
1238 (
sp->strile_length / (
sp->subsampling_ver * 8)));
1240 if (OJPEGReadHeaderInfoSec(tif) == 0)
1242 sp->sos_end[0].log = 1;
1243 sp->sos_end[0].in_buffer_source =
sp->in_buffer_source;
1244 sp->sos_end[0].in_buffer_next_strile =
sp->in_buffer_next_strile;
1245 sp->sos_end[0].in_buffer_file_pos =
1246 sp->in_buffer_file_pos -
sp->in_buffer_togo;
1247 sp->sos_end[0].in_buffer_file_togo =
1248 sp->in_buffer_file_togo +
sp->in_buffer_togo;
1249 sp->readheader_done = 1;
1255 OJPEGState *
sp = (OJPEGState *)tif->
tif_data;
1262 while (
sp->sos_end[
sp->plane_sample_offset].log == 0)
1263 sp->plane_sample_offset--;
1264 sp->in_buffer_source =
1265 sp->sos_end[
sp->plane_sample_offset].in_buffer_source;
1266 sp->in_buffer_next_strile =
1267 sp->sos_end[
sp->plane_sample_offset].in_buffer_next_strile;
1268 sp->in_buffer_file_pos =
1269 sp->sos_end[
sp->plane_sample_offset].in_buffer_file_pos;
1270 sp->in_buffer_file_pos_log = 0;
1271 sp->in_buffer_file_togo =
1272 sp->sos_end[
sp->plane_sample_offset].in_buffer_file_togo;
1273 sp->in_buffer_togo = 0;
1274 sp->in_buffer_cur = 0;
1275 while (
sp->plane_sample_offset <
s)
1279 if (OJPEGReadByte(
sp, &
m) == 0)
1285 if (OJPEGReadByte(
sp, &
m) == 0)
1290 if (
m == JPEG_MARKER_SOS)
1294 sp->plane_sample_offset++;
1295 if (OJPEGReadHeaderInfoSecStreamSos(tif) == 0)
1297 sp->sos_end[
sp->plane_sample_offset].log = 1;
1298 sp->sos_end[
sp->plane_sample_offset].in_buffer_source =
1299 sp->in_buffer_source;
1300 sp->sos_end[
sp->plane_sample_offset].in_buffer_next_strile =
1301 sp->in_buffer_next_strile;
1302 sp->sos_end[
sp->plane_sample_offset].in_buffer_file_pos =
1303 sp->in_buffer_file_pos -
sp->in_buffer_togo;
1304 sp->sos_end[
sp->plane_sample_offset].in_buffer_file_togo =
1305 sp->in_buffer_file_togo +
sp->in_buffer_togo;
1310static int OJPEGWriteHeaderInfo(
TIFF *tif)
1312 static const char module[] =
"OJPEGWriteHeaderInfo";
1313 OJPEGState *
sp = (OJPEGState *)tif->
tif_data;
1317 if (
sp->libjpeg_session_active != 0)
1319 sp->out_state = ososSoi;
1320 sp->restart_index = 0;
1322 sp->libjpeg_jpeg_error_mgr.output_message =
1323 OJPEGLibjpegJpegErrorMgrOutputMessage;
1324 sp->libjpeg_jpeg_error_mgr.error_exit = OJPEGLibjpegJpegErrorMgrErrorExit;
1325 sp->libjpeg_jpeg_decompress_struct.err = &(
sp->libjpeg_jpeg_error_mgr);
1326 sp->libjpeg_jpeg_decompress_struct.client_data = (
void *)tif;
1327 if (jpeg_create_decompress_encap(
1328 sp, &(
sp->libjpeg_jpeg_decompress_struct)) == 0)
1330 sp->libjpeg_session_active = 1;
1331 sp->libjpeg_jpeg_source_mgr.bytes_in_buffer = 0;
1332 sp->libjpeg_jpeg_source_mgr.init_source =
1333 OJPEGLibjpegJpegSourceMgrInitSource;
1334 sp->libjpeg_jpeg_source_mgr.fill_input_buffer =
1335 OJPEGLibjpegJpegSourceMgrFillInputBuffer;
1336 sp->libjpeg_jpeg_source_mgr.skip_input_data =
1337 OJPEGLibjpegJpegSourceMgrSkipInputData;
1338 sp->libjpeg_jpeg_source_mgr.resync_to_restart =
1339 OJPEGLibjpegJpegSourceMgrResyncToRestart;
1340 sp->libjpeg_jpeg_source_mgr.term_source =
1341 OJPEGLibjpegJpegSourceMgrTermSource;
1342 sp->libjpeg_jpeg_decompress_struct.src = &(
sp->libjpeg_jpeg_source_mgr);
1343 if (jpeg_read_header_encap(
sp, &(
sp->libjpeg_jpeg_decompress_struct), 1) ==
1346 if ((
sp->subsampling_force_desubsampling_inside_decompression == 0) &&
1347 (
sp->samples_per_pixel_per_plane > 1))
1349 sp->libjpeg_jpeg_decompress_struct.raw_data_out = 1;
1350#if JPEG_LIB_VERSION >= 70
1351 sp->libjpeg_jpeg_decompress_struct.do_fancy_upsampling =
FALSE;
1353 sp->libjpeg_jpeg_query_style = 0;
1354 if (
sp->subsampling_convert_log == 0)
1356 assert(
sp->subsampling_convert_ycbcrbuf == 0);
1357 assert(
sp->subsampling_convert_ycbcrimage == 0);
1359 if (
sp->subsampling_hor == 0 ||
sp->subsampling_ver == 0)
1361 sp->subsampling_convert_ylinelen =
1362 ((
sp->strile_width +
sp->subsampling_hor * 8 - 1) /
1363 (
sp->subsampling_hor * 8) *
sp->subsampling_hor * 8);
1364 sp->subsampling_convert_ylines =
sp->subsampling_ver * 8;
1365 sp->subsampling_convert_clinelen =
1366 sp->subsampling_convert_ylinelen /
sp->subsampling_hor;
1367 sp->subsampling_convert_clines = 8;
1368 sp->subsampling_convert_ybuflen =
sp->subsampling_convert_ylinelen *
1369 sp->subsampling_convert_ylines;
1370 sp->subsampling_convert_cbuflen =
sp->subsampling_convert_clinelen *
1371 sp->subsampling_convert_clines;
1372 sp->subsampling_convert_ycbcrbuflen =
1373 sp->subsampling_convert_ybuflen +
1374 2 *
sp->subsampling_convert_cbuflen;
1386 sp->subsampling_convert_ycbcrbuf =
1388 if (
sp->subsampling_convert_ycbcrbuf == 0)
1393 sp->subsampling_convert_ybuf =
sp->subsampling_convert_ycbcrbuf;
1394 sp->subsampling_convert_cbbuf =
1395 sp->subsampling_convert_ybuf +
sp->subsampling_convert_ybuflen;
1396 sp->subsampling_convert_crbuf =
1397 sp->subsampling_convert_cbbuf +
sp->subsampling_convert_cbuflen;
1398 sp->subsampling_convert_ycbcrimagelen =
1399 3 +
sp->subsampling_convert_ylines +
1400 2 *
sp->subsampling_convert_clines;
1402 tif,
sp->subsampling_convert_ycbcrimagelen *
sizeof(
uint8_t *));
1403 if (
sp->subsampling_convert_ycbcrimage == 0)
1408 m =
sp->subsampling_convert_ycbcrimage;
1409 *
m++ = (
uint8_t *)(
sp->subsampling_convert_ycbcrimage + 3);
1410 *
m++ = (
uint8_t *)(
sp->subsampling_convert_ycbcrimage + 3 +
1411 sp->subsampling_convert_ylines);
1412 *
m++ = (
uint8_t *)(
sp->subsampling_convert_ycbcrimage + 3 +
1413 sp->subsampling_convert_ylines +
1414 sp->subsampling_convert_clines);
1415 for (
n = 0;
n <
sp->subsampling_convert_ylines;
n++)
1416 *
m++ =
sp->subsampling_convert_ybuf +
1417 n *
sp->subsampling_convert_ylinelen;
1418 for (
n = 0;
n <
sp->subsampling_convert_clines;
n++)
1419 *
m++ =
sp->subsampling_convert_cbbuf +
1420 n *
sp->subsampling_convert_clinelen;
1421 for (
n = 0;
n <
sp->subsampling_convert_clines;
n++)
1422 *
m++ =
sp->subsampling_convert_crbuf +
1423 n *
sp->subsampling_convert_clinelen;
1424 sp->subsampling_convert_clinelenout =
1425 sp->strile_width /
sp->subsampling_hor +
1426 ((
sp->strile_width %
sp->subsampling_hor) != 0 ? 1 : 0);
1427 sp->subsampling_convert_state = 0;
1428 sp->error_in_raw_data_decoding = 0;
1429 sp->bytes_per_line =
1430 sp->subsampling_convert_clinelenout *
1431 (
sp->subsampling_ver *
sp->subsampling_hor + 2);
1432 sp->lines_per_strile =
1433 sp->strile_length /
sp->subsampling_ver +
1434 ((
sp->strile_length %
sp->subsampling_ver) != 0 ? 1 : 0);
1435 sp->subsampling_convert_log = 1;
1440 sp->libjpeg_jpeg_decompress_struct.jpeg_color_space =
JCS_UNKNOWN;
1441 sp->libjpeg_jpeg_decompress_struct.out_color_space =
JCS_UNKNOWN;
1442 sp->libjpeg_jpeg_query_style = 1;
1443 sp->bytes_per_line =
sp->samples_per_pixel_per_plane *
sp->strile_width;
1444 sp->lines_per_strile =
sp->strile_length;
1446 if (jpeg_start_decompress_encap(
sp,
1447 &(
sp->libjpeg_jpeg_decompress_struct)) == 0)
1449 if (
sp->libjpeg_jpeg_decompress_struct.image_width !=
sp->strile_width)
1452 "jpeg_start_decompress() returned image_width = %u, "
1454 sp->libjpeg_jpeg_decompress_struct.image_width,
1458 if (
sp->libjpeg_jpeg_decompress_struct.max_h_samp_factor !=
1459 sp->subsampling_hor ||
1460 sp->libjpeg_jpeg_decompress_struct.max_v_samp_factor !=
1461 sp->subsampling_ver)
1464 "jpeg_start_decompress() returned max_h_samp_factor = %d "
1465 "and max_v_samp_factor = %d, expected %" PRIu8
1467 sp->libjpeg_jpeg_decompress_struct.max_h_samp_factor,
1468 sp->libjpeg_jpeg_decompress_struct.max_v_samp_factor,
1469 sp->subsampling_hor,
sp->subsampling_ver);
1473 sp->writeheader_done = 1;
1477static void OJPEGLibjpegSessionAbort(
TIFF *tif)
1479 OJPEGState *
sp = (OJPEGState *)tif->
tif_data;
1480 assert(
sp->libjpeg_session_active != 0);
1482 sp->libjpeg_session_active = 0;
1485static int OJPEGReadHeaderInfoSec(
TIFF *tif)
1487 static const char module[] =
"OJPEGReadHeaderInfoSec";
1488 OJPEGState *
sp = (OJPEGState *)tif->
tif_data;
1492 if (
sp->file_size == 0)
1494 if (
sp->jpeg_interchange_format != 0)
1496 if (
sp->jpeg_interchange_format >=
sp->file_size)
1498 sp->jpeg_interchange_format = 0;
1499 sp->jpeg_interchange_format_length = 0;
1503 if ((
sp->jpeg_interchange_format_length == 0) ||
1504 (
sp->jpeg_interchange_format >
1506 (
sp->jpeg_interchange_format +
1507 sp->jpeg_interchange_format_length >
1509 sp->jpeg_interchange_format_length =
1510 sp->file_size -
sp->jpeg_interchange_format;
1513 sp->in_buffer_source = osibsNotSetYet;
1514 sp->in_buffer_next_strile = 0;
1516 sp->in_buffer_file_togo = 0;
1517 sp->in_buffer_togo = 0;
1520 if (OJPEGReadBytePeek(
sp, &
m) == 0)
1524 OJPEGReadByteAdvance(
sp);
1527 if (OJPEGReadByte(
sp, &
m) == 0)
1532 case JPEG_MARKER_SOI:
1535 case JPEG_MARKER_COM:
1536 case JPEG_MARKER_APP0:
1537 case JPEG_MARKER_APP0 + 1:
1538 case JPEG_MARKER_APP0 + 2:
1539 case JPEG_MARKER_APP0 + 3:
1540 case JPEG_MARKER_APP0 + 4:
1541 case JPEG_MARKER_APP0 + 5:
1542 case JPEG_MARKER_APP0 + 6:
1543 case JPEG_MARKER_APP0 + 7:
1544 case JPEG_MARKER_APP0 + 8:
1545 case JPEG_MARKER_APP0 + 9:
1546 case JPEG_MARKER_APP0 + 10:
1547 case JPEG_MARKER_APP0 + 11:
1548 case JPEG_MARKER_APP0 + 12:
1549 case JPEG_MARKER_APP0 + 13:
1550 case JPEG_MARKER_APP0 + 14:
1551 case JPEG_MARKER_APP0 + 15:
1554 if (OJPEGReadWord(
sp, &
n) == 0)
1558 if (
sp->subsamplingcorrect == 0)
1563 OJPEGReadSkip(
sp,
n - 2);
1565 case JPEG_MARKER_DRI:
1566 if (OJPEGReadHeaderInfoSecStreamDri(tif) == 0)
1569 case JPEG_MARKER_DQT:
1570 if (OJPEGReadHeaderInfoSecStreamDqt(tif) == 0)
1573 case JPEG_MARKER_DHT:
1574 if (OJPEGReadHeaderInfoSecStreamDht(tif) == 0)
1577 case JPEG_MARKER_SOF0:
1578 case JPEG_MARKER_SOF1:
1579 case JPEG_MARKER_SOF3:
1580 if (OJPEGReadHeaderInfoSecStreamSof(tif,
m) == 0)
1582 if (
sp->subsamplingcorrect != 0)
1585 case JPEG_MARKER_SOS:
1586 if (
sp->subsamplingcorrect != 0)
1588 assert(
sp->plane_sample_offset == 0);
1589 if (OJPEGReadHeaderInfoSecStreamSos(tif) == 0)
1594 "Unknown marker type %" PRIu8 " in JPEG data",
m);
1597 }
while (
m != JPEG_MARKER_SOS);
1598 if (
sp->subsamplingcorrect)
1600 if (
sp->sof_log == 0)
1602 if (OJPEGReadHeaderInfoSecTablesQTable(tif) == 0)
1604 sp->sof_marker_id = JPEG_MARKER_SOF0;
1605 for (o = 0; o <
sp->samples_per_pixel; o++)
1607 sp->sof_hv[0] = ((
sp->subsampling_hor << 4) |
sp->subsampling_ver);
1608 for (o = 1; o <
sp->samples_per_pixel; o++)
1610 sp->sof_x =
sp->strile_width;
1611 sp->sof_y =
sp->strile_length_total;
1613 if (OJPEGReadHeaderInfoSecTablesDcTable(tif) == 0)
1615 if (OJPEGReadHeaderInfoSecTablesAcTable(tif) == 0)
1617 for (o = 1; o <
sp->samples_per_pixel; o++)
1623static int OJPEGReadHeaderInfoSecStreamDri(
TIFF *tif)
1627 static const char module[] =
"OJPEGReadHeaderInfoSecStreamDri";
1628 OJPEGState *
sp = (OJPEGState *)tif->
tif_data;
1630 if (OJPEGReadWord(
sp, &
m) == 0)
1637 if (OJPEGReadWord(
sp, &
m) == 0)
1639 sp->restart_interval =
m;
1643static int OJPEGReadHeaderInfoSecStreamDqt(
TIFF *tif)
1647 static const char module[] =
"OJPEGReadHeaderInfoSecStreamDqt";
1648 OJPEGState *
sp = (OJPEGState *)tif->
tif_data;
1653 if (OJPEGReadWord(
sp, &
m) == 0)
1657 if (
sp->subsamplingcorrect == 0)
1661 if (
sp->subsamplingcorrect != 0)
1662 OJPEGReadSkip(
sp,
m - 2);
1682 nb[
sizeof(
uint32_t) + 1] = JPEG_MARKER_DQT;
1685 if (OJPEGReadBlock(
sp, 65, &nb[
sizeof(
uint32_t) + 4]) == 0)
1697 if (
sp->qtable[o] != 0)
1706static int OJPEGReadHeaderInfoSecStreamDht(
TIFF *tif)
1712 static const char module[] =
"OJPEGReadHeaderInfoSecStreamDht";
1713 OJPEGState *
sp = (OJPEGState *)tif->
tif_data;
1718 if (OJPEGReadWord(
sp, &
m) == 0)
1722 if (
sp->subsamplingcorrect == 0)
1726 if (
sp->subsamplingcorrect != 0)
1728 OJPEGReadSkip(
sp,
m - 2);
1741 nb[
sizeof(
uint32_t) + 1] = JPEG_MARKER_DHT;
1744 if (OJPEGReadBlock(
sp,
m - 2, &nb[
sizeof(
uint32_t) + 4]) == 0)
1758 if (
sp->dctable[o] != 0)
1760 sp->dctable[o] = nb;
1764 if ((o & 240) != 16)
1777 if (
sp->actable[o] != 0)
1779 sp->actable[o] = nb;
1785static int OJPEGReadHeaderInfoSecStreamSof(
TIFF *tif,
uint8_t marker_id)
1789 static const char module[] =
"OJPEGReadHeaderInfoSecStreamSof";
1790 OJPEGState *
sp = (OJPEGState *)tif->
tif_data;
1796 if (
sp->sof_log != 0)
1801 if (
sp->subsamplingcorrect == 0)
1802 sp->sof_marker_id = marker_id;
1804 if (OJPEGReadWord(
sp, &
m) == 0)
1808 if (
sp->subsamplingcorrect == 0)
1815 if (
sp->subsamplingcorrect == 0)
1820 if (
sp->subsamplingcorrect == 0)
1822 if (
n !=
sp->samples_per_pixel)
1826 "JPEG compressed data indicates unexpected number of samples");
1831 if (OJPEGReadByte(
sp, &o) == 0)
1835 if (
sp->subsamplingcorrect == 0)
1837 "JPEG compressed data indicates unexpected number of "
1842 if (
sp->subsamplingcorrect)
1843 OJPEGReadSkip(
sp, 4);
1847 if (OJPEGReadWord(
sp, &
p) == 0)
1849 if (((
uint32_t)p < sp->image_length) &&
1850 ((
uint32_t)p < sp->strile_length_total))
1853 "JPEG compressed data indicates unexpected height");
1858 if (OJPEGReadWord(
sp, &
p) == 0)
1863 "JPEG compressed data indicates unexpected width");
1869 "JPEG compressed data image width exceeds expected "
1876 if (OJPEGReadByte(
sp, &o) == 0)
1880 if (
sp->subsamplingcorrect == 0)
1887 for (
q = 0;
q <
n;
q++)
1890 if (OJPEGReadByte(
sp, &o) == 0)
1892 if (
sp->subsamplingcorrect == 0)
1895 if (OJPEGReadByte(
sp, &o) == 0)
1897 if (
sp->subsamplingcorrect != 0)
1901 sp->subsampling_hor = (o >> 4);
1902 sp->subsampling_ver = (o & 15);
1903 if (((
sp->subsampling_hor != 1) && (
sp->subsampling_hor != 2) &&
1904 (
sp->subsampling_hor != 4)) ||
1905 ((
sp->subsampling_ver != 1) && (
sp->subsampling_ver != 2) &&
1906 (
sp->subsampling_ver != 4)))
1907 sp->subsampling_force_desubsampling_inside_decompression =
1913 sp->subsampling_force_desubsampling_inside_decompression =
1920 if (
sp->subsampling_force_desubsampling_inside_decompression == 0)
1924 if (o != ((
sp->subsampling_hor << 4) |
sp->subsampling_ver))
1927 "JPEG compressed data indicates "
1928 "unexpected subsampling values");
1937 "JPEG compressed data indicates "
1938 "unexpected subsampling values");
1945 if (OJPEGReadByte(
sp, &o) == 0)
1947 if (
sp->subsamplingcorrect == 0)
1950 if (
sp->subsamplingcorrect == 0)
1955static int OJPEGReadHeaderInfoSecStreamSos(
TIFF *tif)
1959 static const char module[] =
"OJPEGReadHeaderInfoSecStreamSos";
1960 OJPEGState *
sp = (OJPEGState *)tif->
tif_data;
1964 assert(
sp->subsamplingcorrect == 0);
1965 if (
sp->sof_log == 0)
1971 if (OJPEGReadWord(
sp, &
m) == 0)
1973 if (
m != 6 +
sp->samples_per_pixel_per_plane * 2)
1979 if (OJPEGReadByte(
sp, &
n) == 0)
1981 if (
n !=
sp->samples_per_pixel_per_plane)
1987 for (o = 0; o <
sp->samples_per_pixel_per_plane; o++)
1990 if (OJPEGReadByte(
sp, &
n) == 0)
1992 sp->sos_cs[
sp->plane_sample_offset + o] =
n;
1994 if (OJPEGReadByte(
sp, &
n) == 0)
1996 sp->sos_tda[
sp->plane_sample_offset + o] =
n;
2000 OJPEGReadSkip(
sp, 3);
2004static int OJPEGReadHeaderInfoSecTablesQTable(
TIFF *tif)
2006 static const char module[] =
"OJPEGReadHeaderInfoSecTablesQTable";
2007 OJPEGState *
sp = (OJPEGState *)tif->
tif_data;
2013 if (
sp->qtable_offset[0] == 0)
2018 sp->in_buffer_file_pos_log = 0;
2019 for (
m = 0;
m <
sp->samples_per_pixel;
m++)
2021 if ((
sp->qtable_offset[
m] != 0) &&
2022 ((
m == 0) || (
sp->qtable_offset[
m] !=
sp->qtable_offset[
m - 1])))
2024 for (
n = 0;
n <
m - 1;
n++)
2026 if (
sp->qtable_offset[
m] ==
sp->qtable_offset[
n])
2041 ob[
sizeof(
uint32_t) + 1] = JPEG_MARKER_DQT;
2052 if (
sp->qtable[
m] != 0)
2058 sp->sof_tq[
m] =
sp->sof_tq[
m - 1];
2063static int OJPEGReadHeaderInfoSecTablesDcTable(
TIFF *tif)
2065 static const char module[] =
"OJPEGReadHeaderInfoSecTablesDcTable";
2066 OJPEGState *
sp = (OJPEGState *)tif->
tif_data;
2074 if (
sp->dctable_offset[0] == 0)
2079 sp->in_buffer_file_pos_log = 0;
2080 for (
m = 0;
m <
sp->samples_per_pixel;
m++)
2082 if ((
sp->dctable_offset[
m] != 0) &&
2083 ((
m == 0) || (
sp->dctable_offset[
m] !=
sp->dctable_offset[
m - 1])))
2085 for (
n = 0;
n <
m - 1;
n++)
2087 if (
sp->dctable_offset[
m] ==
sp->dctable_offset[
n])
2090 "Corrupt JpegDcTables tag value");
2099 for (
n = 0;
n < 16;
n++)
2110 rb[
sizeof(
uint32_t) + 1] = JPEG_MARKER_DHT;
2112 rb[
sizeof(
uint32_t) + 3] = ((19 +
q) & 255);
2114 for (
n = 0;
n < 16;
n++)
2122 if (
sp->dctable[
m] != 0)
2124 sp->dctable[
m] = rb;
2125 sp->sos_tda[
m] = (
m << 4);
2128 sp->sos_tda[
m] =
sp->sos_tda[
m - 1];
2133static int OJPEGReadHeaderInfoSecTablesAcTable(
TIFF *tif)
2135 static const char module[] =
"OJPEGReadHeaderInfoSecTablesAcTable";
2136 OJPEGState *
sp = (OJPEGState *)tif->
tif_data;
2144 if (
sp->actable_offset[0] == 0)
2149 sp->in_buffer_file_pos_log = 0;
2150 for (
m = 0;
m <
sp->samples_per_pixel;
m++)
2152 if ((
sp->actable_offset[
m] != 0) &&
2153 ((
m == 0) || (
sp->actable_offset[
m] !=
sp->actable_offset[
m - 1])))
2155 for (
n = 0;
n <
m - 1;
n++)
2157 if (
sp->actable_offset[
m] ==
sp->actable_offset[
n])
2160 "Corrupt JpegAcTables tag value");
2169 for (
n = 0;
n < 16;
n++)
2180 rb[
sizeof(
uint32_t) + 1] = JPEG_MARKER_DHT;
2182 rb[
sizeof(
uint32_t) + 3] = ((19 +
q) & 255);
2184 for (
n = 0;
n < 16;
n++)
2192 if (
sp->actable[
m] != 0)
2194 sp->actable[
m] = rb;
2195 sp->sos_tda[
m] = (
sp->sos_tda[
m] |
m);
2198 sp->sos_tda[
m] = (
sp->sos_tda[
m] | (
sp->sos_tda[
m - 1] & 15));
2203static int OJPEGReadBufferFill(OJPEGState *
sp)
2212 if (
sp->in_buffer_file_togo != 0)
2214 if (
sp->in_buffer_file_pos_log == 0)
2217 sp->in_buffer_file_pos_log = 1;
2230 sp->in_buffer_togo =
m;
2231 sp->in_buffer_cur =
sp->in_buffer;
2232 sp->in_buffer_file_togo -=
m;
2233 sp->in_buffer_file_pos +=
m;
2236 sp->in_buffer_file_pos_log = 0;
2237 switch (
sp->in_buffer_source)
2239 case osibsNotSetYet:
2240 if (
sp->jpeg_interchange_format != 0)
2242 sp->in_buffer_file_pos =
sp->jpeg_interchange_format;
2243 sp->in_buffer_file_togo =
2244 sp->jpeg_interchange_format_length;
2246 sp->in_buffer_source = osibsJpegInterchangeFormat;
2248 case osibsJpegInterchangeFormat:
2249 sp->in_buffer_source = osibsStrile;
2252 if (
sp->in_buffer_next_strile ==
sp->in_buffer_strile_count)
2253 sp->in_buffer_source = osibsEof;
2258 sp->tif,
sp->in_buffer_next_strile, &
err);
2261 if (
sp->in_buffer_file_pos != 0)
2264 sp->tif,
sp->in_buffer_next_strile, &
err);
2267 if (
sp->in_buffer_file_pos >=
sp->file_size)
2268 sp->in_buffer_file_pos = 0;
2269 else if (bytecount == 0)
2270 sp->in_buffer_file_togo =
2271 sp->file_size -
sp->in_buffer_file_pos;
2274 sp->in_buffer_file_togo = bytecount;
2275 if (
sp->in_buffer_file_togo == 0)
2276 sp->in_buffer_file_pos = 0;
2277 else if (
sp->in_buffer_file_pos >
2279 sp->in_buffer_file_pos +
2280 sp->in_buffer_file_togo >
2282 sp->in_buffer_file_togo =
2283 sp->file_size -
sp->in_buffer_file_pos;
2286 sp->in_buffer_next_strile++;
2296static int OJPEGReadByte(OJPEGState *
sp,
uint8_t *
byte)
2298 if (
sp->in_buffer_togo == 0)
2300 if (OJPEGReadBufferFill(
sp) == 0)
2304 *
byte = *(
sp->in_buffer_cur);
2305 sp->in_buffer_cur++;
2306 sp->in_buffer_togo--;
2310static int OJPEGReadBytePeek(OJPEGState *
sp,
uint8_t *
byte)
2312 if (
sp->in_buffer_togo == 0)
2314 if (OJPEGReadBufferFill(
sp) == 0)
2318 *
byte = *(
sp->in_buffer_cur);
2322static void OJPEGReadByteAdvance(OJPEGState *
sp)
2325 sp->in_buffer_cur++;
2326 sp->in_buffer_togo--;
2332 if (OJPEGReadByte(
sp, &
m) == 0)
2335 if (OJPEGReadByte(
sp, &
m) == 0)
2351 if (
sp->in_buffer_togo == 0)
2353 if (OJPEGReadBufferFill(
sp) == 0)
2358 if (
n >
sp->in_buffer_togo)
2359 n =
sp->in_buffer_togo;
2361 sp->in_buffer_cur +=
n;
2362 sp->in_buffer_togo -=
n;
2375 if (
n >
sp->in_buffer_togo)
2376 n =
sp->in_buffer_togo;
2377 sp->in_buffer_cur +=
n;
2378 sp->in_buffer_togo -=
n;
2386 sp->in_buffer_file_pos +=
n;
2387 sp->in_buffer_file_togo -=
n;
2388 sp->in_buffer_file_pos_log = 0;
2400 OJPEGState *
sp = (OJPEGState *)tif->
tif_data;
2405 switch (
sp->out_state)
2408 OJPEGWriteStreamSoi(tif,
mem,
len);
2411 OJPEGWriteStreamQTable(tif, 0,
mem,
len);
2414 OJPEGWriteStreamQTable(tif, 1,
mem,
len);
2417 OJPEGWriteStreamQTable(tif, 2,
mem,
len);
2420 OJPEGWriteStreamQTable(tif, 3,
mem,
len);
2423 OJPEGWriteStreamDcTable(tif, 0,
mem,
len);
2426 OJPEGWriteStreamDcTable(tif, 1,
mem,
len);
2429 OJPEGWriteStreamDcTable(tif, 2,
mem,
len);
2432 OJPEGWriteStreamDcTable(tif, 3,
mem,
len);
2435 OJPEGWriteStreamAcTable(tif, 0,
mem,
len);
2438 OJPEGWriteStreamAcTable(tif, 1,
mem,
len);
2441 OJPEGWriteStreamAcTable(tif, 2,
mem,
len);
2444 OJPEGWriteStreamAcTable(tif, 3,
mem,
len);
2447 OJPEGWriteStreamDri(tif,
mem,
len);
2450 OJPEGWriteStreamSof(tif,
mem,
len);
2453 OJPEGWriteStreamSos(tif,
mem,
len);
2455 case ososCompressed:
2456 if (OJPEGWriteStreamCompressed(tif,
mem,
len) == 0)
2460 OJPEGWriteStreamRst(tif,
mem,
len);
2463 OJPEGWriteStreamEoi(tif,
mem,
len);
2466 }
while (*
len == 0);
2472 OJPEGState *
sp = (OJPEGState *)tif->
tif_data;
2473 assert(OJPEG_BUFFER >= 2);
2474 sp->out_buffer[0] = 255;
2475 sp->out_buffer[1] = JPEG_MARKER_SOI;
2477 *
mem = (
void *)
sp->out_buffer;
2481static void OJPEGWriteStreamQTable(
TIFF *tif,
uint8_t table_index,
void **
mem,
2484 OJPEGState *
sp = (OJPEGState *)tif->
tif_data;
2485 if (
sp->qtable[table_index] != 0)
2487 *
mem = (
void *)(
sp->qtable[table_index] +
sizeof(
uint32_t));
2493static void OJPEGWriteStreamDcTable(
TIFF *tif,
uint8_t table_index,
void **
mem,
2496 OJPEGState *
sp = (OJPEGState *)tif->
tif_data;
2497 if (
sp->dctable[table_index] != 0)
2499 *
mem = (
void *)(
sp->dctable[table_index] +
sizeof(
uint32_t));
2505static void OJPEGWriteStreamAcTable(
TIFF *tif,
uint8_t table_index,
void **
mem,
2508 OJPEGState *
sp = (OJPEGState *)tif->
tif_data;
2509 if (
sp->actable[table_index] != 0)
2511 *
mem = (
void *)(
sp->actable[table_index] +
sizeof(
uint32_t));
2519 OJPEGState *
sp = (OJPEGState *)tif->
tif_data;
2520 assert(OJPEG_BUFFER >= 6);
2521 if (
sp->restart_interval != 0)
2523 sp->out_buffer[0] = 255;
2524 sp->out_buffer[1] = JPEG_MARKER_DRI;
2525 sp->out_buffer[2] = 0;
2526 sp->out_buffer[3] = 4;
2527 sp->out_buffer[4] = (
sp->restart_interval >> 8);
2528 sp->out_buffer[5] = (
sp->restart_interval & 255);
2530 *
mem = (
void *)
sp->out_buffer;
2537 OJPEGState *
sp = (OJPEGState *)tif->
tif_data;
2539 assert(OJPEG_BUFFER >= 2 + 8 +
sp->samples_per_pixel_per_plane * 3);
2540 assert(255 >= 8 +
sp->samples_per_pixel_per_plane * 3);
2541 sp->out_buffer[0] = 255;
2542 sp->out_buffer[1] =
sp->sof_marker_id;
2544 sp->out_buffer[2] = 0;
2545 sp->out_buffer[3] = 8 +
sp->samples_per_pixel_per_plane * 3;
2547 sp->out_buffer[4] = 8;
2550 sp->out_buffer[6] = (
sp->sof_y & 255);
2553 sp->out_buffer[8] = (
sp->sof_x & 255);
2555 sp->out_buffer[9] =
sp->samples_per_pixel_per_plane;
2556 for (
m = 0;
m <
sp->samples_per_pixel_per_plane;
m++)
2559 sp->out_buffer[10 +
m * 3] =
sp->sof_c[
sp->plane_sample_offset +
m];
2561 sp->out_buffer[10 +
m * 3 + 1] =
2562 sp->sof_hv[
sp->plane_sample_offset +
m];
2564 sp->out_buffer[10 +
m * 3 + 2] =
2565 sp->sof_tq[
sp->plane_sample_offset +
m];
2567 *
len = 10 +
sp->samples_per_pixel_per_plane * 3;
2568 *
mem = (
void *)
sp->out_buffer;
2574 OJPEGState *
sp = (OJPEGState *)tif->
tif_data;
2576 assert(OJPEG_BUFFER >= 2 + 6 +
sp->samples_per_pixel_per_plane * 2);
2577 assert(255 >= 6 +
sp->samples_per_pixel_per_plane * 2);
2578 sp->out_buffer[0] = 255;
2579 sp->out_buffer[1] = JPEG_MARKER_SOS;
2581 sp->out_buffer[2] = 0;
2582 sp->out_buffer[3] = 6 +
sp->samples_per_pixel_per_plane * 2;
2584 sp->out_buffer[4] =
sp->samples_per_pixel_per_plane;
2585 for (
m = 0;
m <
sp->samples_per_pixel_per_plane;
m++)
2588 sp->out_buffer[5 +
m * 2] =
sp->sos_cs[
sp->plane_sample_offset +
m];
2590 sp->out_buffer[5 +
m * 2 + 1] =
2591 sp->sos_tda[
sp->plane_sample_offset +
m];
2594 sp->out_buffer[5 +
sp->samples_per_pixel_per_plane * 2] = 0;
2596 sp->out_buffer[5 +
sp->samples_per_pixel_per_plane * 2 + 1] = 63;
2598 sp->out_buffer[5 +
sp->samples_per_pixel_per_plane * 2 + 2] = 0;
2599 *
len = 8 +
sp->samples_per_pixel_per_plane * 2;
2600 *
mem = (
void *)
sp->out_buffer;
2606 OJPEGState *
sp = (OJPEGState *)tif->
tif_data;
2607 if (
sp->in_buffer_togo == 0)
2609 if (OJPEGReadBufferFill(
sp) == 0)
2613 *
len =
sp->in_buffer_togo;
2614 *
mem = (
void *)
sp->in_buffer_cur;
2615 sp->in_buffer_togo = 0;
2616 if (
sp->in_buffer_file_togo == 0)
2618 switch (
sp->in_buffer_source)
2621 if (
sp->in_buffer_next_strile <
sp->in_buffer_strile_count)
2622 sp->out_state = ososRst;
2624 sp->out_state = ososEoi;
2627 sp->out_state = ososEoi;
2638 OJPEGState *
sp = (OJPEGState *)tif->
tif_data;
2639 assert(OJPEG_BUFFER >= 2);
2640 sp->out_buffer[0] = 255;
2641 sp->out_buffer[1] = JPEG_MARKER_RST0 +
sp->restart_index;
2642 sp->restart_index++;
2643 if (
sp->restart_index == 8)
2644 sp->restart_index = 0;
2646 *
mem = (
void *)
sp->out_buffer;
2647 sp->out_state = ososCompressed;
2652 OJPEGState *
sp = (OJPEGState *)tif->
tif_data;
2653 assert(OJPEG_BUFFER >= 2);
2654 sp->out_buffer[0] = 255;
2655 sp->out_buffer[1] = JPEG_MARKER_EOI;
2657 *
mem = (
void *)
sp->out_buffer;
2660#ifndef LIBJPEG_ENCAP_EXTERNAL
2661static int jpeg_create_decompress_encap(OJPEGState *
sp,
2664 if (SETJMP(
sp->exit_jmpbuf))
2674#ifndef LIBJPEG_ENCAP_EXTERNAL
2678 if (SETJMP(
sp->exit_jmpbuf))
2688#ifndef LIBJPEG_ENCAP_EXTERNAL
2689static int jpeg_start_decompress_encap(OJPEGState *
sp,
2692 if (SETJMP(
sp->exit_jmpbuf))
2696 jpeg_start_decompress(cinfo);
2702#ifndef LIBJPEG_ENCAP_EXTERNAL
2703static int jpeg_read_scanlines_encap(OJPEGState *
sp,
2707 if (SETJMP(
sp->exit_jmpbuf))
2717#ifndef LIBJPEG_ENCAP_EXTERNAL
2718static int jpeg_read_raw_data_encap(OJPEGState *
sp,
2722 if (SETJMP(
sp->exit_jmpbuf))
2732#ifndef LIBJPEG_ENCAP_EXTERNAL
2733static void jpeg_encap_unwind(
TIFF *tif)
2735 OJPEGState *
sp = (OJPEGState *)tif->
tif_data;
2736 LONGJMP(
sp->exit_jmpbuf, 1);
2743 (*cinfo->err->format_message)(cinfo,
buffer);
2750 (*cinfo->err->format_message)(cinfo,
buffer);
2752 jpeg_encap_unwind((
TIFF *)(cinfo->client_data));
2763 TIFF *tif = (
TIFF *)cinfo->client_data;
2764 OJPEGState *
sp = (OJPEGState *)tif->
tif_data;
2767 if (OJPEGWriteStream(tif, &
mem, &
len) == 0)
2769 TIFFErrorExtR(tif,
"LibJpeg",
"Premature end of JPEG data");
2770 jpeg_encap_unwind(tif);
2772 sp->libjpeg_jpeg_source_mgr.bytes_in_buffer =
len;
2773 sp->libjpeg_jpeg_source_mgr.next_input_byte =
mem;
2781 TIFF *tif = (
TIFF *)cinfo->client_data;
2784 jpeg_encap_unwind(tif);
2788#pragma warning(push)
2789#pragma warning(disable : 4702)
2795 TIFF *tif = (
TIFF *)cinfo->client_data;
2798 jpeg_encap_unwind(tif);
int WINAPIV fprintf(FILE *file, const char *format,...)
for(i=0;i< ARRAY_SIZE(offsets);i++)
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
GLdouble GLdouble GLdouble r
GLdouble GLdouble GLdouble GLdouble q
GLenum GLuint GLenum GLsizei const GLchar * buf
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint GLint GLint j
static unsigned char * in_buffer
jpeg_destroy(j_common_ptr cinfo)
jpeg_read_header(j_decompress_ptr cinfo, boolean require_image)
jpeg_read_scanlines(j_decompress_ptr cinfo, JSAMPARRAY scanlines, JDIMENSION max_lines)
jpeg_read_raw_data(j_decompress_ptr cinfo, JSAMPIMAGE data, JDIMENSION max_lines)
jpeg_std_error(struct jpeg_error_mgr *err)
#define jpeg_create_decompress(cinfo)
JSAMPARRAY JDIMENSION max_lines
static HANDLE PIO_APC_ROUTINE PVOID PIO_STATUS_BLOCK ULONG PVOID ULONG PVOID out_buffer
static unsigned int file_size
uint32_t td_stripsperimage
uint16_t td_ycbcrsubsampling[2]
uint16_t td_samplesperpixel
unsigned int restart_interval
TIFFCodeMethod tif_encodestrip
TIFFCodeMethod tif_encodetile
TIFFTagMethods tif_tagmethods
TIFFPreMethod tif_preencode
TIFFBoolMethod tif_fixuptags
TIFFPreMethod tif_predecode
TIFFCodeMethod tif_decodestrip
TIFFPostMethod tif_postdecode
TIFFCodeMethod tif_decoderow
TIFFBoolMethod tif_setupencode
TIFFBoolMethod tif_postencode
TIFFCodeMethod tif_encoderow
TIFFVoidMethod tif_cleanup
TIFFBoolMethod tif_setupdecode
TIFFCodeMethod tif_decodetile
void _TIFFSetDefaultCompressionState(TIFF *tif)
int TIFFSetField(TIFF *tif, uint32_t tag,...)
#define TIFFFieldSet(tif, field)
#define TIFFSetFieldBit(tif, field)
const TIFFField * TIFFFieldWithTag(TIFF *tif, uint32_t tag)
int _TIFFMergeFields(TIFF *tif, const TIFFField info[], uint32_t n)
uint64_t TIFFGetStrileOffsetWithErr(TIFF *tif, uint32_t strile, int *pbErr)
uint64_t TIFFGetStrileByteCountWithErr(TIFF *tif, uint32_t strile, int *pbErr)
void TIFFErrorExtR(TIFF *tif, const char *module, const char *fmt,...)
void _TIFFfreeExt(TIFF *tif, void *p)
void * _TIFFmallocExt(TIFF *tif, tmsize_t s)
void * _TIFFcallocExt(TIFF *tif, tmsize_t nmemb, tmsize_t siz)
void _TIFFmemset(void *p, int v, tmsize_t c)
void _TIFFmemcpy(void *d, const void *s, tmsize_t c)
void TIFFWarningExtR(TIFF *tif, const char *module, const char *fmt,...)
#define TIFFTAG_JPEGDCTABLES
#define COMPRESSION_OJPEG
#define TIFFTAG_JPEGQTABLES
#define PHOTOMETRIC_ITULAB
#define TIFFTAG_JPEGIFBYTECOUNT
#define TIFFTAG_JPEGACTABLES
#define TIFFTAG_JPEGRESTARTINTERVAL
#define PHOTOMETRIC_YCBCR
#define TIFFTAG_JPEGIFOFFSET
#define TIFFTAG_YCBCRSUBSAMPLING
#define PLANARCONFIG_CONTIG
void(* TIFFPrintMethod)(TIFF *, FILE *, long)
int(* TIFFVSetMethod)(TIFF *, uint32_t, va_list)
int(* TIFFVGetMethod)(TIFF *, uint32_t, va_list)
#define TIFFReadFile(tif, buf, size)
#define TIFFGetFileSize(tif)
#define TIFFSeekFile(tif, off, whence)
#define TIFFArrayCount(a)
void int int ULONGLONG int va_list * ap