| 1 |
/* WARNING: The type of JPEG encapsulation defined by the TIFF Version 6.0 |
| 2 |
specification is now totally obsolete and deprecated for new applications and |
| 3 |
images. This file was was created solely in order to read unconverted images |
| 4 |
still present on some users' computer systems. It will never be extended |
| 5 |
to write such files. Writing new-style JPEG compressed TIFFs is implemented |
| 6 |
in tif_jpeg.c. |
| 7 |
|
| 8 |
The code is carefully crafted to robustly read all gathered JPEG-in-TIFF |
| 9 |
testfiles, and anticipate as much as possible all other... But still, it may |
| 10 |
fail on some. If you encounter problems, please report them on the TIFF |
| 11 |
mailing list and/or to Joris Van Damme <[email protected]>. |
| 12 |
|
| 13 |
Please read the file called "TIFF Technical Note #2" if you need to be |
| 14 |
convinced this compression scheme is bad and breaks TIFF. That document |
| 15 |
is linked to from the LibTiff site <http://www.remotesensing.org/libtiff/> |
| 16 |
and from AWare Systems' TIFF section |
| 17 |
<http://www.awaresystems.be/imaging/tiff.html>. It is also absorbed |
| 18 |
in Adobe's specification supplements, marked "draft" up to this day, but |
| 19 |
supported by the TIFF community. |
| 20 |
|
| 21 |
This file interfaces with Release 6B of the JPEG Library written by the |
| 22 |
Independent JPEG Group. Previous versions of this file required a hack inside |
| 23 |
the LibJpeg library. This version no longer requires that. Remember to |
| 24 |
remove the hack if you update from the old version. |
| 25 |
|
| 26 |
Copyright (c) Joris Van Damme <[email protected]> |
| 27 |
Copyright (c) AWare Systems <http://www.awaresystems.be/> |
| 28 |
|
| 29 |
The licence agreement for this file is the same as the rest of the LibTiff |
| 30 |
library. |
| 31 |
|
| 32 |
IN NO EVENT SHALL JORIS VAN DAMME OR AWARE SYSTEMS BE LIABLE FOR |
| 33 |
ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, |
| 34 |
OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, |
| 35 |
WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF |
| 36 |
LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 37 |
OF THIS SOFTWARE. |
| 38 |
|
| 39 |
Joris Van Damme and/or AWare Systems may be available for custom |
| 40 |
development. If you like what you see, and need anything similar or related, |
| 41 |
contact <[email protected]>. |
| 42 |
*/ |
| 43 |
|
| 44 |
/* What is what, and what is not? |
| 45 |
|
| 46 |
This decoder starts with an input stream, that is essentially the |
| 47 |
JpegInterchangeFormat stream, if any, followed by the strile data, if any. |
| 48 |
This stream is read in OJPEGReadByte and related functions. |
| 49 |
|
| 50 |
It analyzes the start of this stream, until it encounters non-marker data, |
| 51 |
i.e. compressed image data. Some of the header markers it sees have no actual |
| 52 |
content, like the SOI marker, and APP/COM markers that really shouldn't even |
| 53 |
be there. Some other markers do have content, and the valuable bits and |
| 54 |
pieces of information in these markers are saved, checking all to verify that |
| 55 |
the stream is more or less within expected bounds. This happens inside the |
| 56 |
OJPEGReadHeaderInfoSecStreamXxx functions. |
| 57 |
|
| 58 |
Some OJPEG imagery contains no valid JPEG header markers. This situation is |
| 59 |
picked up on if we've seen no SOF marker when we're at the start of the |
| 60 |
compressed image data. In this case, the tables are read from JpegXxxTables |
| 61 |
tags, and the other bits and pieces of information is initialized to its most |
| 62 |
basic value. This is implemented in the OJPEGReadHeaderInfoSecTablesXxx |
| 63 |
functions. |
| 64 |
|
| 65 |
When this is complete, a good and valid JPEG header can be assembled, and |
| 66 |
this is passed through to LibJpeg. When that's done, the remainder of the |
| 67 |
input stream, i.e. the compressed image data, can be passed through |
| 68 |
unchanged. This is done in OJPEGWriteStream functions. |
| 69 |
|
| 70 |
LibTiff rightly expects to know the subsampling values before decompression. |
| 71 |
Just like in new-style JPEG-in-TIFF, though, or even more so, actually, the |
| 72 |
YCbCrsubsampling tag is notoriously unreliable. To correct these tag values |
| 73 |
with the ones inside the JPEG stream, the first part of the input stream is |
| 74 |
pre-scanned in OJPEGSubsamplingCorrect, making no note of any other data, |
| 75 |
reporting no warnings or errors, up to the point where either these values |
| 76 |
are read, or it's clear they aren't there. This means that some of the data |
| 77 |
is read twice, but we feel speed in correcting these values is important |
| 78 |
enough to warrant this sacrifice. Although there is currently no define or |
| 79 |
other configuration mechanism to disable this behavior, the actual header |
| 80 |
scanning is build to robustly respond with error report if it should |
| 81 |
encounter an uncorrected mismatch of subsampling values. See |
| 82 |
OJPEGReadHeaderInfoSecStreamSof. |
| 83 |
|
| 84 |
The restart interval and restart markers are the most tricky part... The |
| 85 |
restart interval can be specified in a tag. It can also be set inside the |
| 86 |
input JPEG stream. It can be used inside the input JPEG stream. If reading |
| 87 |
from strile data, we've consistently discovered the need to insert restart |
| 88 |
markers in between the different striles, as is also probably the most likely |
| 89 |
interpretation of the original TIFF 6.0 specification. With all this setting |
| 90 |
of interval, and actual use of markers that is not predictable at the time of |
| 91 |
valid JPEG header assembly, the restart thing may turn out the Achilles heel |
| 92 |
of this implementation. Fortunately, most OJPEG writer vendors succeed in |
| 93 |
reading back what they write, which may be the reason why we've been able to |
| 94 |
discover ways that seem to work. |
| 95 |
|
| 96 |
Some special provision is made for planarconfig separate OJPEG files. These |
| 97 |
seem to consistently contain header info, a SOS marker, a plane, SOS marker, |
| 98 |
plane, SOS, and plane. This may or may not be a valid JPEG configuration, we |
| 99 |
don't know and don't care. We want LibTiff to be able to access the planes |
| 100 |
individually, without huge buffering inside LibJpeg, anyway. So we compose |
| 101 |
headers to feed to LibJpeg, in this case, that allow us to pass a single |
| 102 |
plane such that LibJpeg sees a valid single-channel JPEG stream. Locating |
| 103 |
subsequent SOS markers, and thus subsequent planes, is done inside |
| 104 |
OJPEGReadSecondarySos. |
| 105 |
|
| 106 |
The benefit of the scheme is... that it works, basically. We know of no other |
| 107 |
that does. It works without checking software tag, or otherwise going about |
| 108 |
things in an OJPEG flavor specific manner. Instead, it is a single scheme, |
| 109 |
that covers the cases with and without JpegInterchangeFormat, with and |
| 110 |
without striles, with part of the header in JpegInterchangeFormat and |
| 111 |
remainder in first strile, etc. It is forgiving and robust, may likely work |
| 112 |
with OJPEG flavors we've not seen yet, and makes most out of the data. |
| 113 |
|
| 114 |
Another nice side-effect is that a complete JPEG single valid stream is build |
| 115 |
if planarconfig is not separate (vast majority). We may one day use that to |
| 116 |
build converters to JPEG, and/or to new-style JPEG compression inside TIFF. |
| 117 |
|
| 118 |
A disadvantage is the lack of random access to the individual striles. This |
| 119 |
is the reason for much of the complicated restart-and-position stuff inside |
| 120 |
OJPEGPreDecode. Applications would do well accessing all striles in order, as |
| 121 |
this will result in a single sequential scan of the input stream, and no |
| 122 |
restarting of LibJpeg decoding session. |
| 123 |
*/ |
| 124 |
|
| 125 |
#define WIN32_LEAN_AND_MEAN |
| 126 |
#define VC_EXTRALEAN |
| 127 |
|
| 128 |
#include "tiffiop.h" |
| 129 |
#ifdef OJPEG_SUPPORT |
| 130 |
|
| 131 |
/* Configuration defines here are: |
| 132 |
* JPEG_ENCAP_EXTERNAL: The normal way to call libjpeg, uses longjump. In some |
| 133 |
* environments, like eg LibTiffDelphi, this is not possible. For this reason, |
| 134 |
* the actual calls to libjpeg, with longjump stuff, are encapsulated in |
| 135 |
* dedicated functions. When JPEG_ENCAP_EXTERNAL is defined, these encapsulating |
| 136 |
* functions are declared external to this unit, and can be defined elsewhere to |
| 137 |
* use stuff other then longjump. The default mode, without JPEG_ENCAP_EXTERNAL, |
| 138 |
* implements the call encapsulators here, internally, with normal longjump. |
| 139 |
* SETJMP, LONGJMP, JMP_BUF: On some machines/environments a longjump equivalent |
| 140 |
* is conveniently available, but still it may be worthwhile to use _setjmp or |
| 141 |
* sigsetjmp in place of plain setjmp. These macros will make it easier. It is |
| 142 |
* useless to fiddle with these if you define JPEG_ENCAP_EXTERNAL. OJPEG_BUFFER: |
| 143 |
* Define the size of the desired buffer here. Should be small enough so as to |
| 144 |
* guarantee instant processing, optimal streaming and optimal use of processor |
| 145 |
* cache, but also big enough so as to not result in significant call overhead. |
| 146 |
* It should be at least a few bytes to accommodate some structures (this is |
| 147 |
* verified in asserts), but it would not be sensible to make it this small |
| 148 |
* anyway, and it should be at most 64K since it is indexed with uint16_t. We |
| 149 |
* recommend 2K. EGYPTIANWALK: You could also define EGYPTIANWALK here, but it |
| 150 |
* is not used anywhere and has absolutely no effect. That is why most people |
| 151 |
* insist the EGYPTIANWALK is a bit silly. |
| 152 |
*/ |
| 153 |
|
| 154 |
/* define LIBJPEG_ENCAP_EXTERNAL */ |
| 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 |
| 159 |
/* define EGYPTIANWALK */ |
| 160 |
|
| 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 |
| 173 |
|
| 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) |
| 181 |
|
| 182 |
static const TIFFField ojpegFields[] = { |
| 183 |
{TIFFTAG_JPEGIFOFFSET, 1, 1, TIFF_LONG8, 0, TIFF_SETGET_UINT64, |
| 184 |
TIFF_SETGET_UNDEFINED, FIELD_OJPEG_JPEGINTERCHANGEFORMAT, TRUE, FALSE, |
| 185 |
"JpegInterchangeFormat", NULL}, |
| 186 |
{TIFFTAG_JPEGIFBYTECOUNT, 1, 1, TIFF_LONG8, 0, TIFF_SETGET_UINT64, |
| 187 |
TIFF_SETGET_UNDEFINED, FIELD_OJPEG_JPEGINTERCHANGEFORMATLENGTH, TRUE, |
| 188 |
FALSE, "JpegInterchangeFormatLength", NULL}, |
| 189 |
{TIFFTAG_JPEGQTABLES, TIFF_VARIABLE2, TIFF_VARIABLE2, TIFF_LONG8, 0, |
| 190 |
TIFF_SETGET_C32_UINT64, TIFF_SETGET_UNDEFINED, FIELD_OJPEG_JPEGQTABLES, |
| 191 |
FALSE, TRUE, "JpegQTables", NULL}, |
| 192 |
{TIFFTAG_JPEGDCTABLES, TIFF_VARIABLE2, TIFF_VARIABLE2, TIFF_LONG8, 0, |
| 193 |
TIFF_SETGET_C32_UINT64, TIFF_SETGET_UNDEFINED, FIELD_OJPEG_JPEGDCTABLES, |
| 194 |
FALSE, TRUE, "JpegDcTables", NULL}, |
| 195 |
{TIFFTAG_JPEGACTABLES, TIFF_VARIABLE2, TIFF_VARIABLE2, TIFF_LONG8, 0, |
| 196 |
TIFF_SETGET_C32_UINT64, TIFF_SETGET_UNDEFINED, FIELD_OJPEG_JPEGACTABLES, |
| 197 |
FALSE, TRUE, "JpegAcTables", NULL}, |
| 198 |
{TIFFTAG_JPEGPROC, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, |
| 199 |
TIFF_SETGET_UNDEFINED, FIELD_OJPEG_JPEGPROC, FALSE, FALSE, "JpegProc", |
| 200 |
NULL}, |
| 201 |
{TIFFTAG_JPEGRESTARTINTERVAL, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, |
| 202 |
TIFF_SETGET_UNDEFINED, FIELD_OJPEG_JPEGRESTARTINTERVAL, FALSE, FALSE, |
| 203 |
"JpegRestartInterval", NULL}, |
| 204 |
}; |
| 205 |
|
| 206 |
#ifndef LIBJPEG_ENCAP_EXTERNAL |
| 207 |
#include <setjmp.h> |
| 208 |
#endif |
| 209 |
|
| 210 |
#include "jerror.h" |
| 211 |
#include "jpeglib.h" |
| 212 |
|
| 213 |
#ifndef TIFF_jpeg_source_mgr_defined |
| 214 |
#define TIFF_jpeg_source_mgr_defined |
| 215 |
typedef struct jpeg_source_mgr jpeg_source_mgr; |
| 216 |
#endif |
| 217 |
|
| 218 |
#ifndef TIFF_jpeg_error_mgr_defined |
| 219 |
#define TIFF_jpeg_error_mgr_defined |
| 220 |
typedef struct jpeg_error_mgr jpeg_error_mgr; |
| 221 |
#endif |
| 222 |
|
| 223 |
typedef struct jpeg_common_struct jpeg_common_struct; |
| 224 |
typedef struct jpeg_decompress_struct jpeg_decompress_struct; |
| 225 |
|
| 226 |
typedef enum |
| 227 |
{ |
| 228 |
osibsNotSetYet, |
| 229 |
osibsJpegInterchangeFormat, |
| 230 |
osibsStrile, |
| 231 |
osibsEof |
| 232 |
} OJPEGStateInBufferSource; |
| 233 |
|
| 234 |
typedef enum |
| 235 |
{ |
| 236 |
ososSoi, |
| 237 |
ososQTable0, |
| 238 |
ososQTable1, |
| 239 |
ososQTable2, |
| 240 |
ososQTable3, |
| 241 |
ososDcTable0, |
| 242 |
ososDcTable1, |
| 243 |
ososDcTable2, |
| 244 |
ososDcTable3, |
| 245 |
ososAcTable0, |
| 246 |
ososAcTable1, |
| 247 |
ososAcTable2, |
| 248 |
ososAcTable3, |
| 249 |
ososDri, |
| 250 |
ososSof, |
| 251 |
ososSos, |
| 252 |
ososCompressed, |
| 253 |
ososRst, |
| 254 |
ososEoi |
| 255 |
} OJPEGStateOutState; |
| 256 |
|
| 257 |
typedef struct |
| 258 |
{ |
| 259 |
TIFF *tif; |
| 260 |
int decoder_ok; |
| 261 |
int error_in_raw_data_decoding; |
| 262 |
#ifndef LIBJPEG_ENCAP_EXTERNAL |
| 263 |
JMP_BUF exit_jmpbuf; |
| 264 |
#endif |
| 265 |
TIFFVGetMethod vgetparent; |
| 266 |
TIFFVSetMethod vsetparent; |
| 267 |
TIFFPrintMethod printdir; |
| 268 |
uint64_t file_size; |
| 269 |
uint32_t image_width; |
| 270 |
uint32_t image_length; |
| 271 |
uint32_t strile_width; |
| 272 |
uint32_t strile_length; |
| 273 |
uint32_t strile_length_total; |
| 274 |
uint8_t samples_per_pixel; |
| 275 |
uint8_t plane_sample_offset; |
| 276 |
uint8_t samples_per_pixel_per_plane; |
| 277 |
uint64_t jpeg_interchange_format; |
| 278 |
uint64_t jpeg_interchange_format_length; |
| 279 |
uint8_t jpeg_proc; |
| 280 |
uint8_t subsamplingcorrect; |
| 281 |
uint8_t subsamplingcorrect_done; |
| 282 |
uint8_t subsampling_tag; |
| 283 |
uint8_t subsampling_hor; |
| 284 |
uint8_t subsampling_ver; |
| 285 |
uint8_t subsampling_force_desubsampling_inside_decompression; |
| 286 |
uint8_t qtable_offset_count; |
| 287 |
uint8_t dctable_offset_count; |
| 288 |
uint8_t actable_offset_count; |
| 289 |
uint64_t qtable_offset[3]; |
| 290 |
uint64_t dctable_offset[3]; |
| 291 |
uint64_t actable_offset[3]; |
| 292 |
uint8_t *qtable[4]; |
| 293 |
uint8_t *dctable[4]; |
| 294 |
uint8_t *actable[4]; |
| 295 |
uint16_t restart_interval; |
| 296 |
uint8_t restart_index; |
| 297 |
uint8_t sof_log; |
| 298 |
uint8_t sof_marker_id; |
| 299 |
uint32_t sof_x; |
| 300 |
uint32_t sof_y; |
| 301 |
uint8_t sof_c[3]; |
| 302 |
uint8_t sof_hv[3]; |
| 303 |
uint8_t sof_tq[3]; |
| 304 |
uint8_t sos_cs[3]; |
| 305 |
uint8_t sos_tda[3]; |
| 306 |
struct |
| 307 |
{ |
| 308 |
uint8_t log; |
| 309 |
OJPEGStateInBufferSource in_buffer_source; |
| 310 |
uint32_t in_buffer_next_strile; |
| 311 |
uint64_t in_buffer_file_pos; |
| 312 |
uint64_t in_buffer_file_togo; |
| 313 |
} sos_end[3]; |
| 314 |
uint8_t readheader_done; |
| 315 |
uint8_t writeheader_done; |
| 316 |
uint16_t write_cursample; |
| 317 |
uint32_t write_curstrile; |
| 318 |
uint8_t libjpeg_session_active; |
| 319 |
uint8_t libjpeg_jpeg_query_style; |
| 320 |
jpeg_error_mgr libjpeg_jpeg_error_mgr; |
| 321 |
jpeg_decompress_struct libjpeg_jpeg_decompress_struct; |
| 322 |
jpeg_source_mgr libjpeg_jpeg_source_mgr; |
| 323 |
uint8_t subsampling_convert_log; |
| 324 |
uint32_t subsampling_convert_ylinelen; |
| 325 |
uint32_t subsampling_convert_ylines; |
| 326 |
uint32_t subsampling_convert_clinelen; |
| 327 |
uint32_t subsampling_convert_clines; |
| 328 |
uint32_t subsampling_convert_ybuflen; |
| 329 |
uint32_t subsampling_convert_cbuflen; |
| 330 |
uint32_t subsampling_convert_ycbcrbuflen; |
| 331 |
uint8_t *subsampling_convert_ycbcrbuf; |
| 332 |
uint8_t *subsampling_convert_ybuf; |
| 333 |
uint8_t *subsampling_convert_cbbuf; |
| 334 |
uint8_t *subsampling_convert_crbuf; |
| 335 |
uint32_t subsampling_convert_ycbcrimagelen; |
| 336 |
uint8_t **subsampling_convert_ycbcrimage; |
| 337 |
uint32_t subsampling_convert_clinelenout; |
| 338 |
uint32_t subsampling_convert_state; |
| 339 |
uint32_t bytes_per_line; /* if the codec outputs subsampled data, a 'line' |
| 340 |
in bytes_per_line */ |
| 341 |
uint32_t lines_per_strile; /* and lines_per_strile means subsampling_ver |
| 342 |
desubsampled rows */ |
| 343 |
OJPEGStateInBufferSource in_buffer_source; |
| 344 |
uint32_t in_buffer_next_strile; |
| 345 |
uint32_t in_buffer_strile_count; |
| 346 |
uint64_t in_buffer_file_pos; |
| 347 |
uint8_t in_buffer_file_pos_log; |
| 348 |
uint64_t in_buffer_file_togo; |
| 349 |
uint16_t in_buffer_togo; |
| 350 |
uint8_t *in_buffer_cur; |
| 351 |
uint8_t in_buffer[OJPEG_BUFFER]; |
| 352 |
OJPEGStateOutState out_state; |
| 353 |
uint8_t out_buffer[OJPEG_BUFFER]; |
| 354 |
uint8_t *skip_buffer; |
| 355 |
} OJPEGState; |
| 356 |
|
| 357 |
static int OJPEGVGetField(TIFF *tif, uint32_t tag, va_list ap); |
| 358 |
static int OJPEGVSetField(TIFF *tif, uint32_t tag, va_list ap); |
| 359 |
static void OJPEGPrintDir(TIFF *tif, FILE *fd, long flags); |
| 360 |
|
| 361 |
static int OJPEGFixupTags(TIFF *tif); |
| 362 |
static int OJPEGSetupDecode(TIFF *tif); |
| 363 |
static int OJPEGPreDecode(TIFF *tif, uint16_t s); |
| 364 |
static int OJPEGPreDecodeSkipRaw(TIFF *tif); |
| 365 |
static int OJPEGPreDecodeSkipScanlines(TIFF *tif); |
| 366 |
static int OJPEGDecode(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s); |
| 367 |
static int OJPEGDecodeRaw(TIFF *tif, uint8_t *buf, tmsize_t cc); |
| 368 |
static int OJPEGDecodeScanlines(TIFF *tif, uint8_t *buf, tmsize_t cc); |
| 369 |
static void OJPEGPostDecode(TIFF *tif, uint8_t *buf, tmsize_t cc); |
| 370 |
static int OJPEGSetupEncode(TIFF *tif); |
| 371 |
static int OJPEGPreEncode(TIFF *tif, uint16_t s); |
| 372 |
static int OJPEGEncode(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s); |
| 373 |
static int OJPEGPostEncode(TIFF *tif); |
| 374 |
static void OJPEGCleanup(TIFF *tif); |
| 375 |
|
| 376 |
static void OJPEGSubsamplingCorrect(TIFF *tif); |
| 377 |
static int OJPEGReadHeaderInfo(TIFF *tif); |
| 378 |
static int OJPEGReadSecondarySos(TIFF *tif, uint16_t s); |
| 379 |
static int OJPEGWriteHeaderInfo(TIFF *tif); |
| 380 |
static void OJPEGLibjpegSessionAbort(TIFF *tif); |
| 381 |
|
| 382 |
static int OJPEGReadHeaderInfoSec(TIFF *tif); |
| 383 |
static int OJPEGReadHeaderInfoSecStreamDri(TIFF *tif); |
| 384 |
static int OJPEGReadHeaderInfoSecStreamDqt(TIFF *tif); |
| 385 |
static int OJPEGReadHeaderInfoSecStreamDht(TIFF *tif); |
| 386 |
static int OJPEGReadHeaderInfoSecStreamSof(TIFF *tif, uint8_t marker_id); |
| 387 |
static int OJPEGReadHeaderInfoSecStreamSos(TIFF *tif); |
| 388 |
static int OJPEGReadHeaderInfoSecTablesQTable(TIFF *tif); |
| 389 |
static int OJPEGReadHeaderInfoSecTablesDcTable(TIFF *tif); |
| 390 |
static int OJPEGReadHeaderInfoSecTablesAcTable(TIFF *tif); |
| 391 |
|
| 392 |
static int OJPEGReadBufferFill(OJPEGState *sp); |
| 393 |
static int OJPEGReadByte(OJPEGState *sp, uint8_t *byte); |
| 394 |
static int OJPEGReadBytePeek(OJPEGState *sp, uint8_t *byte); |
| 395 |
static void OJPEGReadByteAdvance(OJPEGState *sp); |
| 396 |
static int OJPEGReadWord(OJPEGState *sp, uint16_t *word); |
| 397 |
static int OJPEGReadBlock(OJPEGState *sp, uint16_t len, void *mem); |
| 398 |
static void OJPEGReadSkip(OJPEGState *sp, uint16_t len); |
| 399 |
|
| 400 |
static int OJPEGWriteStream(TIFF *tif, void **mem, uint32_t *len); |
| 401 |
static void OJPEGWriteStreamSoi(TIFF *tif, void **mem, uint32_t *len); |
| 402 |
static void OJPEGWriteStreamQTable(TIFF *tif, uint8_t table_index, void **mem, |
| 403 |
uint32_t *len); |
| 404 |
static void OJPEGWriteStreamDcTable(TIFF *tif, uint8_t table_index, void **mem, |
| 405 |
uint32_t *len); |
| 406 |
static void OJPEGWriteStreamAcTable(TIFF *tif, uint8_t table_index, void **mem, |
| 407 |
uint32_t *len); |
| 408 |
static void OJPEGWriteStreamDri(TIFF *tif, void **mem, uint32_t *len); |
| 409 |
static void OJPEGWriteStreamSof(TIFF *tif, void **mem, uint32_t *len); |
| 410 |
static void OJPEGWriteStreamSos(TIFF *tif, void **mem, uint32_t *len); |
| 411 |
static int OJPEGWriteStreamCompressed(TIFF *tif, void **mem, uint32_t *len); |
| 412 |
static void OJPEGWriteStreamRst(TIFF *tif, void **mem, uint32_t *len); |
| 413 |
static void OJPEGWriteStreamEoi(TIFF *tif, void **mem, uint32_t *len); |
| 414 |
|
| 415 |
#ifdef LIBJPEG_ENCAP_EXTERNAL |
| 416 |
extern int jpeg_create_decompress_encap(OJPEGState *sp, |
| 417 |
jpeg_decompress_struct *cinfo); |
| 418 |
extern int jpeg_read_header_encap(OJPEGState *sp, jpeg_decompress_struct *cinfo, |
| 419 |
uint8_t require_image); |
| 420 |
extern int jpeg_start_decompress_encap(OJPEGState *sp, |
| 421 |
jpeg_decompress_struct *cinfo); |
| 422 |
extern int jpeg_read_scanlines_encap(OJPEGState *sp, |
| 423 |
jpeg_decompress_struct *cinfo, |
| 424 |
void *scanlines, uint32_t max_lines); |
| 425 |
extern int jpeg_read_raw_data_encap(OJPEGState *sp, |
| 426 |
jpeg_decompress_struct *cinfo, void *data, |
| 427 |
uint32_t max_lines); |
| 428 |
extern void jpeg_encap_unwind(TIFF *tif); |
| 429 |
#else |
| 430 |
static int jpeg_create_decompress_encap(OJPEGState *sp, |
| 431 |
jpeg_decompress_struct *j); |
| 432 |
static int jpeg_read_header_encap(OJPEGState *sp, jpeg_decompress_struct *cinfo, |
| 433 |
uint8_t require_image); |
| 434 |
static int jpeg_start_decompress_encap(OJPEGState *sp, |
| 435 |
jpeg_decompress_struct *cinfo); |
| 436 |
static int jpeg_read_scanlines_encap(OJPEGState *sp, |
| 437 |
jpeg_decompress_struct *cinfo, |
| 438 |
void *scanlines, uint32_t max_lines); |
| 439 |
static int jpeg_read_raw_data_encap(OJPEGState *sp, |
| 440 |
jpeg_decompress_struct *cinfo, void *data, |
| 441 |
uint32_t max_lines); |
| 442 |
static void jpeg_encap_unwind(TIFF *tif); |
| 443 |
#endif |
| 444 |
|
| 445 |
static void OJPEGLibjpegJpegErrorMgrOutputMessage(jpeg_common_struct *cinfo); |
| 446 |
static void OJPEGLibjpegJpegErrorMgrErrorExit(jpeg_common_struct *cinfo); |
| 447 |
static void OJPEGLibjpegJpegSourceMgrInitSource(jpeg_decompress_struct *cinfo); |
| 448 |
static boolean |
| 449 |
OJPEGLibjpegJpegSourceMgrFillInputBuffer(jpeg_decompress_struct *cinfo); |
| 450 |
static void |
| 451 |
OJPEGLibjpegJpegSourceMgrSkipInputData(jpeg_decompress_struct *cinfo, |
| 452 |
long num_bytes); |
| 453 |
static boolean |
| 454 |
OJPEGLibjpegJpegSourceMgrResyncToRestart(jpeg_decompress_struct *cinfo, |
| 455 |
int desired); |
| 456 |
static void OJPEGLibjpegJpegSourceMgrTermSource(jpeg_decompress_struct *cinfo); |
| 457 |
|
| 458 |
int TIFFInitOJPEG(TIFF *tif, int scheme) |
| 459 |
{ |
| 460 |
static const char module[] = "TIFFInitOJPEG"; |
| 461 |
OJPEGState *sp; |
| 462 |
|
| 463 |
(void)scheme; |
| 464 |
assert(scheme == COMPRESSION_OJPEG); |
| 465 |
|
| 466 |
/* |
| 467 |
* Merge codec-specific tag information. |
| 468 |
*/ |
| 469 |
if (!_TIFFMergeFields(tif, ojpegFields, TIFFArrayCount(ojpegFields))) |
| 470 |
{ |
| 471 |
TIFFErrorExtR(tif, module, |
| 472 |
"Merging Old JPEG codec-specific tags failed"); |
| 473 |
return 0; |
| 474 |
} |
| 475 |
|
| 476 |
/* state block */ |
| 477 |
sp = _TIFFmallocExt(tif, sizeof(OJPEGState)); |
| 478 |
if (sp == NULL) |
| 479 |
{ |
| 480 |
TIFFErrorExtR(tif, module, "No space for OJPEG state block"); |
| 481 |
return (0); |
| 482 |
} |
| 483 |
_TIFFmemset(sp, 0, sizeof(OJPEGState)); |
| 484 |
sp->tif = tif; |
| 485 |
sp->jpeg_proc = 1; |
| 486 |
sp->subsampling_hor = 2; |
| 487 |
sp->subsampling_ver = 2; |
| 488 |
TIFFSetField(tif, TIFFTAG_YCBCRSUBSAMPLING, 2, 2); |
| 489 |
/* tif codec methods */ |
| 490 |
tif->tif_fixuptags = OJPEGFixupTags; |
| 491 |
tif->tif_setupdecode = OJPEGSetupDecode; |
| 492 |
tif->tif_predecode = OJPEGPreDecode; |
| 493 |
tif->tif_postdecode = OJPEGPostDecode; |
| 494 |
tif->tif_decoderow = OJPEGDecode; |
| 495 |
tif->tif_decodestrip = OJPEGDecode; |
| 496 |
tif->tif_decodetile = OJPEGDecode; |
| 497 |
tif->tif_setupencode = OJPEGSetupEncode; |
| 498 |
tif->tif_preencode = OJPEGPreEncode; |
| 499 |
tif->tif_postencode = OJPEGPostEncode; |
| 500 |
tif->tif_encoderow = OJPEGEncode; |
| 501 |
tif->tif_encodestrip = OJPEGEncode; |
| 502 |
tif->tif_encodetile = OJPEGEncode; |
| 503 |
tif->tif_cleanup = OJPEGCleanup; |
| 504 |
tif->tif_data = (uint8_t *)sp; |
| 505 |
/* tif tag methods */ |
| 506 |
sp->vgetparent = tif->tif_tagmethods.vgetfield; |
| 507 |
tif->tif_tagmethods.vgetfield = OJPEGVGetField; |
| 508 |
sp->vsetparent = tif->tif_tagmethods.vsetfield; |
| 509 |
tif->tif_tagmethods.vsetfield = OJPEGVSetField; |
| 510 |
sp->printdir = tif->tif_tagmethods.printdir; |
| 511 |
tif->tif_tagmethods.printdir = OJPEGPrintDir; |
| 512 |
/* Some OJPEG files don't have strip or tile offsets or bytecounts tags. |
| 513 |
Some others do, but have totally meaningless or corrupt values |
| 514 |
in these tags. In these cases, the JpegInterchangeFormat stream is |
| 515 |
reliable. In any case, this decoder reads the compressed data itself, |
| 516 |
from the most reliable locations, and we need to notify encapsulating |
| 517 |
LibTiff not to read raw strips or tiles for us. */ |
| 518 |
tif->tif_flags |= TIFF_NOREADRAW; |
| 519 |
return (1); |
| 520 |
} |
| 521 |
|
| 522 |
static int OJPEGVGetField(TIFF *tif, uint32_t tag, va_list ap) |
| 523 |
{ |
| 524 |
OJPEGState *sp = (OJPEGState *)tif->tif_data; |
| 525 |
switch (tag) |
| 526 |
{ |
| 527 |
case TIFFTAG_JPEGIFOFFSET: |
| 528 |
*va_arg(ap, uint64_t *) = (uint64_t)sp->jpeg_interchange_format; |
| 529 |
break; |
| 530 |
case TIFFTAG_JPEGIFBYTECOUNT: |
| 531 |
*va_arg(ap, uint64_t *) = |
| 532 |
(uint64_t)sp->jpeg_interchange_format_length; |
| 533 |
break; |
| 534 |
case TIFFTAG_YCBCRSUBSAMPLING: |
| 535 |
if (sp->subsamplingcorrect_done == 0) |
| 536 |
OJPEGSubsamplingCorrect(tif); |
| 537 |
*va_arg(ap, uint16_t *) = (uint16_t)sp->subsampling_hor; |
| 538 |
*va_arg(ap, uint16_t *) = (uint16_t)sp->subsampling_ver; |
| 539 |
break; |
| 540 |
case TIFFTAG_JPEGQTABLES: |
| 541 |
*va_arg(ap, uint32_t *) = (uint32_t)sp->qtable_offset_count; |
| 542 |
*va_arg(ap, const void **) = (const void *)sp->qtable_offset; |
| 543 |
break; |
| 544 |
case TIFFTAG_JPEGDCTABLES: |
| 545 |
*va_arg(ap, uint32_t *) = (uint32_t)sp->dctable_offset_count; |
| 546 |
*va_arg(ap, const void **) = (const void *)sp->dctable_offset; |
| 547 |
break; |
| 548 |
case TIFFTAG_JPEGACTABLES: |
| 549 |
*va_arg(ap, uint32_t *) = (uint32_t)sp->actable_offset_count; |
| 550 |
*va_arg(ap, const void **) = (const void *)sp->actable_offset; |
| 551 |
break; |
| 552 |
case TIFFTAG_JPEGPROC: |
| 553 |
*va_arg(ap, uint16_t *) = (uint16_t)sp->jpeg_proc; |
| 554 |
break; |
| 555 |
case TIFFTAG_JPEGRESTARTINTERVAL: |
| 556 |
*va_arg(ap, uint16_t *) = sp->restart_interval; |
| 557 |
break; |
| 558 |
default: |
| 559 |
return (*sp->vgetparent)(tif, tag, ap); |
| 560 |
} |
| 561 |
return (1); |
| 562 |
} |
| 563 |
|
| 564 |
static int OJPEGVSetField(TIFF *tif, uint32_t tag, va_list ap) |
| 565 |
{ |
| 566 |
static const char module[] = "OJPEGVSetField"; |
| 567 |
OJPEGState *sp = (OJPEGState *)tif->tif_data; |
| 568 |
uint32_t ma; |
| 569 |
uint64_t *mb; |
| 570 |
uint32_t n; |
| 571 |
const TIFFField *fip; |
| 572 |
|
| 573 |
switch (tag) |
| 574 |
{ |
| 575 |
case TIFFTAG_JPEGIFOFFSET: |
| 576 |
sp->jpeg_interchange_format = (uint64_t)va_arg(ap, uint64_t); |
| 577 |
break; |
| 578 |
case TIFFTAG_JPEGIFBYTECOUNT: |
| 579 |
sp->jpeg_interchange_format_length = (uint64_t)va_arg(ap, uint64_t); |
| 580 |
break; |
| 581 |
case TIFFTAG_YCBCRSUBSAMPLING: |
| 582 |
sp->subsampling_tag = 1; |
| 583 |
sp->subsampling_hor = (uint8_t)va_arg(ap, uint16_vap); |
| 584 |
sp->subsampling_ver = (uint8_t)va_arg(ap, uint16_vap); |
| 585 |
tif->tif_dir.td_ycbcrsubsampling[0] = sp->subsampling_hor; |
| 586 |
tif->tif_dir.td_ycbcrsubsampling[1] = sp->subsampling_ver; |
| 587 |
break; |
| 588 |
case TIFFTAG_JPEGQTABLES: |
| 589 |
ma = (uint32_t)va_arg(ap, uint32_t); |
| 590 |
if (ma != 0) |
| 591 |
{ |
| 592 |
if (ma > 3) |
| 593 |
{ |
| 594 |
TIFFErrorExtR(tif, module, |
| 595 |
"JpegQTables tag has incorrect count"); |
| 596 |
return (0); |
| 597 |
} |
| 598 |
sp->qtable_offset_count = (uint8_t)ma; |
| 599 |
mb = (uint64_t *)va_arg(ap, uint64_t *); |
| 600 |
for (n = 0; n < ma; n++) |
| 601 |
sp->qtable_offset[n] = mb[n]; |
| 602 |
} |
| 603 |
break; |
| 604 |
case TIFFTAG_JPEGDCTABLES: |
| 605 |
ma = (uint32_t)va_arg(ap, uint32_t); |
| 606 |
if (ma != 0) |
| 607 |
{ |
| 608 |
if (ma > 3) |
| 609 |
{ |
| 610 |
TIFFErrorExtR(tif, module, |
| 611 |
"JpegDcTables tag has incorrect count"); |
| 612 |
return (0); |
| 613 |
} |
| 614 |
sp->dctable_offset_count = (uint8_t)ma; |
| 615 |
mb = (uint64_t *)va_arg(ap, uint64_t *); |
| 616 |
for (n = 0; n < ma; n++) |
| 617 |
sp->dctable_offset[n] = mb[n]; |
| 618 |
} |
| 619 |
break; |
| 620 |
case TIFFTAG_JPEGACTABLES: |
| 621 |
ma = (uint32_t)va_arg(ap, uint32_t); |
| 622 |
if (ma != 0) |
| 623 |
{ |
| 624 |
if (ma > 3) |
| 625 |
{ |
| 626 |
TIFFErrorExtR(tif, module, |
| 627 |
"JpegAcTables tag has incorrect count"); |
| 628 |
return (0); |
| 629 |
} |
| 630 |
sp->actable_offset_count = (uint8_t)ma; |
| 631 |
mb = (uint64_t *)va_arg(ap, uint64_t *); |
| 632 |
for (n = 0; n < ma; n++) |
| 633 |
sp->actable_offset[n] = mb[n]; |
| 634 |
} |
| 635 |
break; |
| 636 |
case TIFFTAG_JPEGPROC: |
| 637 |
sp->jpeg_proc = (uint8_t)va_arg(ap, uint16_vap); |
| 638 |
break; |
| 639 |
case TIFFTAG_JPEGRESTARTINTERVAL: |
| 640 |
sp->restart_interval = (uint16_t)va_arg(ap, uint16_vap); |
| 641 |
break; |
| 642 |
default: |
| 643 |
return (*sp->vsetparent)(tif, tag, ap); |
| 644 |
} |
| 645 |
fip = TIFFFieldWithTag(tif, tag); |
| 646 |
if (fip == NULL) /* shouldn't happen */ |
| 647 |
return (0); |
| 648 |
TIFFSetFieldBit(tif, fip->field_bit); |
| 649 |
tif->tif_flags |= TIFF_DIRTYDIRECT; |
| 650 |
return (1); |
| 651 |
} |
| 652 |
|
| 653 |
static void OJPEGPrintDir(TIFF *tif, FILE *fd, long flags) |
| 654 |
{ |
| 655 |
OJPEGState *sp = (OJPEGState *)tif->tif_data; |
| 656 |
uint8_t m; |
| 657 |
(void)flags; |
| 658 |
assert(sp != NULL); |
| 659 |
if (TIFFFieldSet(tif, FIELD_OJPEG_JPEGINTERCHANGEFORMAT)) |
| 660 |
fprintf(fd, " JpegInterchangeFormat: %" PRIu64 "\n", |
| 661 |
(uint64_t)sp->jpeg_interchange_format); |
| 662 |
if (TIFFFieldSet(tif, FIELD_OJPEG_JPEGINTERCHANGEFORMATLENGTH)) |
| 663 |
fprintf(fd, " JpegInterchangeFormatLength: %" PRIu64 "\n", |
| 664 |
(uint64_t)sp->jpeg_interchange_format_length); |
| 665 |
if (TIFFFieldSet(tif, FIELD_OJPEG_JPEGQTABLES)) |
| 666 |
{ |
| 667 |
fprintf(fd, " JpegQTables:"); |
| 668 |
for (m = 0; m < sp->qtable_offset_count; m++) |
| 669 |
fprintf(fd, " %" PRIu64, (uint64_t)sp->qtable_offset[m]); |
| 670 |
fprintf(fd, "\n"); |
| 671 |
} |
| 672 |
if (TIFFFieldSet(tif, FIELD_OJPEG_JPEGDCTABLES)) |
| 673 |
{ |
| 674 |
fprintf(fd, " JpegDcTables:"); |
| 675 |
for (m = 0; m < sp->dctable_offset_count; m++) |
| 676 |
fprintf(fd, " %" PRIu64, (uint64_t)sp->dctable_offset[m]); |
| 677 |
fprintf(fd, "\n"); |
| 678 |
} |
| 679 |
if (TIFFFieldSet(tif, FIELD_OJPEG_JPEGACTABLES)) |
| 680 |
{ |
| 681 |
fprintf(fd, " JpegAcTables:"); |
| 682 |
for (m = 0; m < sp->actable_offset_count; m++) |
| 683 |
fprintf(fd, " %" PRIu64, (uint64_t)sp->actable_offset[m]); |
| 684 |
fprintf(fd, "\n"); |
| 685 |
} |
| 686 |
if (TIFFFieldSet(tif, FIELD_OJPEG_JPEGPROC)) |
| 687 |
fprintf(fd, " JpegProc: %" PRIu8 "\n", sp->jpeg_proc); |
| 688 |
if (TIFFFieldSet(tif, FIELD_OJPEG_JPEGRESTARTINTERVAL)) |
| 689 |
fprintf(fd, " JpegRestartInterval: %" PRIu16 "\n", |
| 690 |
sp->restart_interval); |
| 691 |
if (sp->printdir) |
| 692 |
(*sp->printdir)(tif, fd, flags); |
| 693 |
} |
| 694 |
|
| 695 |
static int OJPEGFixupTags(TIFF *tif) |
| 696 |
{ |
| 697 |
(void)tif; |
| 698 |
return (1); |
| 699 |
} |
| 700 |
|
| 701 |
static int OJPEGSetupDecode(TIFF *tif) |
| 702 |
{ |
| 703 |
static const char module[] = "OJPEGSetupDecode"; |
| 704 |
TIFFWarningExtR(tif, module, |
| 705 |
"Deprecated and troublesome old-style JPEG compression " |
| 706 |
"mode, please convert to new-style JPEG compression and " |
| 707 |
"notify vendor of writing software"); |
| 708 |
return (1); |
| 709 |
} |
| 710 |
|
| 711 |
static int OJPEGPreDecode(TIFF *tif, uint16_t s) |
| 712 |
{ |
| 713 |
OJPEGState *sp = (OJPEGState *)tif->tif_data; |
| 714 |
uint32_t m; |
| 715 |
if (sp->subsamplingcorrect_done == 0) |
| 716 |
OJPEGSubsamplingCorrect(tif); |
| 717 |
if (sp->readheader_done == 0) |
| 718 |
{ |
| 719 |
if (OJPEGReadHeaderInfo(tif) == 0) |
| 720 |
return (0); |
| 721 |
} |
| 722 |
if (sp->sos_end[s].log == 0) |
| 723 |
{ |
| 724 |
if (OJPEGReadSecondarySos(tif, s) == 0) |
| 725 |
return (0); |
| 726 |
} |
| 727 |
if (isTiled(tif)) |
| 728 |
m = tif->tif_curtile; |
| 729 |
else |
| 730 |
m = tif->tif_curstrip; |
| 731 |
if ((sp->writeheader_done != 0) && |
| 732 |
((sp->write_cursample != s) || (sp->write_curstrile > m))) |
| 733 |
{ |
| 734 |
if (sp->libjpeg_session_active != 0) |
| 735 |
OJPEGLibjpegSessionAbort(tif); |
| 736 |
sp->writeheader_done = 0; |
| 737 |
} |
| 738 |
if (sp->writeheader_done == 0) |
| 739 |
{ |
| 740 |
sp->plane_sample_offset = (uint8_t)s; |
| 741 |
sp->write_cursample = s; |
| 742 |
sp->write_curstrile = s * tif->tif_dir.td_stripsperimage; |
| 743 |
if ((sp->in_buffer_file_pos_log == 0) || |
| 744 |
(sp->in_buffer_file_pos - sp->in_buffer_togo != |
| 745 |
sp->sos_end[s].in_buffer_file_pos)) |
| 746 |
{ |
| 747 |
sp->in_buffer_source = sp->sos_end[s].in_buffer_source; |
| 748 |
sp->in_buffer_next_strile = sp->sos_end[s].in_buffer_next_strile; |
| 749 |
sp->in_buffer_file_pos = sp->sos_end[s].in_buffer_file_pos; |
| 750 |
sp->in_buffer_file_pos_log = 0; |
| 751 |
sp->in_buffer_file_togo = sp->sos_end[s].in_buffer_file_togo; |
| 752 |
sp->in_buffer_togo = 0; |
| 753 |
sp->in_buffer_cur = 0; |
| 754 |
} |
| 755 |
if (OJPEGWriteHeaderInfo(tif) == 0) |
| 756 |
return (0); |
| 757 |
} |
| 758 |
|
| 759 |
sp->subsampling_convert_state = 0; |
| 760 |
|
| 761 |
while (sp->write_curstrile < m) |
| 762 |
{ |
| 763 |
if (sp->libjpeg_jpeg_query_style == 0) |
| 764 |
{ |
| 765 |
if (OJPEGPreDecodeSkipRaw(tif) == 0) |
| 766 |
return (0); |
| 767 |
} |
| 768 |
else |
| 769 |
{ |
| 770 |
if (OJPEGPreDecodeSkipScanlines(tif) == 0) |
| 771 |
return (0); |
| 772 |
} |
| 773 |
sp->write_curstrile++; |
| 774 |
} |
| 775 |
sp->decoder_ok = 1; |
| 776 |
return (1); |
| 777 |
} |
| 778 |
|
| 779 |
static int OJPEGPreDecodeSkipRaw(TIFF *tif) |
| 780 |
{ |
| 781 |
OJPEGState *sp = (OJPEGState *)tif->tif_data; |
| 782 |
uint32_t m; |
| 783 |
m = sp->lines_per_strile; |
| 784 |
if (sp->subsampling_convert_state != 0) |
| 785 |
{ |
| 786 |
if (sp->subsampling_convert_clines - sp->subsampling_convert_state >= m) |
| 787 |
{ |
| 788 |
sp->subsampling_convert_state += m; |
| 789 |
if (sp->subsampling_convert_state == sp->subsampling_convert_clines) |
| 790 |
sp->subsampling_convert_state = 0; |
| 791 |
return (1); |
| 792 |
} |
| 793 |
m -= sp->subsampling_convert_clines - sp->subsampling_convert_state; |
| 794 |
sp->subsampling_convert_state = 0; |
| 795 |
sp->error_in_raw_data_decoding = 0; |
| 796 |
} |
| 797 |
while (m >= sp->subsampling_convert_clines) |
| 798 |
{ |
| 799 |
if (jpeg_read_raw_data_encap(sp, &(sp->libjpeg_jpeg_decompress_struct), |
| 800 |
sp->subsampling_convert_ycbcrimage, |
| 801 |
sp->subsampling_ver * 8) == 0) |
| 802 |
return (0); |
| 803 |
m -= sp->subsampling_convert_clines; |
| 804 |
} |
| 805 |
if (m > 0) |
| 806 |
{ |
| 807 |
if (jpeg_read_raw_data_encap(sp, &(sp->libjpeg_jpeg_decompress_struct), |
| 808 |
sp->subsampling_convert_ycbcrimage, |
| 809 |
sp->subsampling_ver * 8) == 0) |
| 810 |
return (0); |
| 811 |
sp->subsampling_convert_state = m; |
| 812 |
} |
| 813 |
return (1); |
| 814 |
} |
| 815 |
|
| 816 |
static int OJPEGPreDecodeSkipScanlines(TIFF *tif) |
| 817 |
{ |
| 818 |
static const char module[] = "OJPEGPreDecodeSkipScanlines"; |
| 819 |
OJPEGState *sp = (OJPEGState *)tif->tif_data; |
| 820 |
uint32_t m; |
| 821 |
if (sp->skip_buffer == NULL) |
| 822 |
{ |
| 823 |
sp->skip_buffer = _TIFFmallocExt(tif, sp->bytes_per_line); |
| 824 |
if (sp->skip_buffer == NULL) |
| 825 |
{ |
| 826 |
TIFFErrorExtR(tif, module, "Out of memory"); |
| 827 |
return (0); |
| 828 |
} |
| 829 |
} |
| 830 |
for (m = 0; m < sp->lines_per_strile; m++) |
| 831 |
{ |
| 832 |
if (jpeg_read_scanlines_encap(sp, &(sp->libjpeg_jpeg_decompress_struct), |
| 833 |
&sp->skip_buffer, 1) == 0) |
| 834 |
return (0); |
| 835 |
} |
| 836 |
return (1); |
| 837 |
} |
| 838 |
|
| 839 |
static int OJPEGDecode(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s) |
| 840 |
{ |
| 841 |
static const char module[] = "OJPEGDecode"; |
| 842 |
OJPEGState *sp = (OJPEGState *)tif->tif_data; |
| 843 |
(void)s; |
| 844 |
if (!sp->decoder_ok) |
| 845 |
{ |
| 846 |
memset(buf, 0, (size_t)cc); |
| 847 |
TIFFErrorExtR(tif, module, |
| 848 |
"Cannot decode: decoder not correctly initialized"); |
| 849 |
return 0; |
| 850 |
} |
| 851 |
if (sp->libjpeg_session_active == 0) |
| 852 |
{ |
| 853 |
memset(buf, 0, (size_t)cc); |
| 854 |
/* This should normally not happen, except that it does when */ |
| 855 |
/* using TIFFReadScanline() which calls OJPEGPostDecode() for */ |
| 856 |
/* each scanline, which assumes that a whole strile was read */ |
| 857 |
/* and may thus incorrectly consider it has read the whole image, |
| 858 |
* causing */ |
| 859 |
/* OJPEGLibjpegSessionAbort() to be called prematurely. */ |
| 860 |
/* Triggered by https://gitlab.com/libtiff/libtiff/-/issues/337 */ |
| 861 |
TIFFErrorExtR(tif, module, |
| 862 |
"Cannot decode: libjpeg_session_active == 0"); |
| 863 |
return 0; |
| 864 |
} |
| 865 |
if (sp->error_in_raw_data_decoding) |
| 866 |
{ |
| 867 |
memset(buf, 0, (size_t)cc); |
| 868 |
return 0; |
| 869 |
} |
| 870 |
if (sp->libjpeg_jpeg_query_style == 0) |
| 871 |
{ |
| 872 |
if (OJPEGDecodeRaw(tif, buf, cc) == 0) |
| 873 |
{ |
| 874 |
memset(buf, 0, (size_t)cc); |
| 875 |
return (0); |
| 876 |
} |
| 877 |
} |
| 878 |
else |
| 879 |
{ |
| 880 |
if (OJPEGDecodeScanlines(tif, buf, cc) == 0) |
| 881 |
{ |
| 882 |
memset(buf, 0, (size_t)cc); |
| 883 |
return (0); |
| 884 |
} |
| 885 |
} |
| 886 |
return (1); |
| 887 |
} |
| 888 |
|
| 889 |
static int OJPEGDecodeRaw(TIFF *tif, uint8_t *buf, tmsize_t cc) |
| 890 |
{ |
| 891 |
static const char module[] = "OJPEGDecodeRaw"; |
| 892 |
OJPEGState *sp = (OJPEGState *)tif->tif_data; |
| 893 |
uint8_t *m; |
| 894 |
tmsize_t n; |
| 895 |
uint8_t *oy; |
| 896 |
uint8_t *ocb; |
| 897 |
uint8_t *ocr; |
| 898 |
uint8_t *p; |
| 899 |
uint32_t q; |
| 900 |
uint8_t *r; |
| 901 |
uint8_t sx, sy; |
| 902 |
if (cc % sp->bytes_per_line != 0) |
| 903 |
{ |
| 904 |
TIFFErrorExtR(tif, module, "Fractional scanline not read"); |
| 905 |
return (0); |
| 906 |
} |
| 907 |
assert(cc > 0); |
| 908 |
m = buf; |
| 909 |
n = cc; |
| 910 |
do |
| 911 |
{ |
| 912 |
if (sp->subsampling_convert_state == 0) |
| 913 |
{ |
| 914 |
if (jpeg_read_raw_data_encap(sp, |
| 915 |
&(sp->libjpeg_jpeg_decompress_struct), |
| 916 |
sp->subsampling_convert_ycbcrimage, |
| 917 |
sp->subsampling_ver * 8) == 0) |
| 918 |
{ |
| 919 |
sp->error_in_raw_data_decoding = 1; |
| 920 |
return (0); |
| 921 |
} |
| 922 |
} |
| 923 |
oy = sp->subsampling_convert_ybuf + |
| 924 |
sp->subsampling_convert_state * sp->subsampling_ver * |
| 925 |
sp->subsampling_convert_ylinelen; |
| 926 |
ocb = sp->subsampling_convert_cbbuf + |
| 927 |
sp->subsampling_convert_state * sp->subsampling_convert_clinelen; |
| 928 |
ocr = sp->subsampling_convert_crbuf + |
| 929 |
sp->subsampling_convert_state * sp->subsampling_convert_clinelen; |
| 930 |
p = m; |
| 931 |
for (q = 0; q < sp->subsampling_convert_clinelenout; q++) |
| 932 |
{ |
| 933 |
r = oy; |
| 934 |
for (sy = 0; sy < sp->subsampling_ver; sy++) |
| 935 |
{ |
| 936 |
for (sx = 0; sx < sp->subsampling_hor; sx++) |
| 937 |
*p++ = *r++; |
| 938 |
r += sp->subsampling_convert_ylinelen - sp->subsampling_hor; |
| 939 |
} |
| 940 |
oy += sp->subsampling_hor; |
| 941 |
*p++ = *ocb++; |
| 942 |
*p++ = *ocr++; |
| 943 |
} |
| 944 |
sp->subsampling_convert_state++; |
| 945 |
if (sp->subsampling_convert_state == sp->subsampling_convert_clines) |
| 946 |
sp->subsampling_convert_state = 0; |
| 947 |
m += sp->bytes_per_line; |
| 948 |
n -= sp->bytes_per_line; |
| 949 |
} while (n > 0); |
| 950 |
return (1); |
| 951 |
} |
| 952 |
|
| 953 |
static int OJPEGDecodeScanlines(TIFF *tif, uint8_t *buf, tmsize_t cc) |
| 954 |
{ |
| 955 |
static const char module[] = "OJPEGDecodeScanlines"; |
| 956 |
OJPEGState *sp = (OJPEGState *)tif->tif_data; |
| 957 |
uint8_t *m; |
| 958 |
tmsize_t n; |
| 959 |
if (cc % sp->bytes_per_line != 0) |
| 960 |
{ |
| 961 |
TIFFErrorExtR(tif, module, "Fractional scanline not read"); |
| 962 |
return (0); |
| 963 |
} |
| 964 |
assert(cc > 0); |
| 965 |
m = buf; |
| 966 |
n = cc; |
| 967 |
do |
| 968 |
{ |
| 969 |
if (jpeg_read_scanlines_encap(sp, &(sp->libjpeg_jpeg_decompress_struct), |
| 970 |
&m, 1) == 0) |
| 971 |
return (0); |
| 972 |
m += sp->bytes_per_line; |
| 973 |
n -= sp->bytes_per_line; |
| 974 |
} while (n > 0); |
| 975 |
return (1); |
| 976 |
} |
| 977 |
|
| 978 |
static void OJPEGPostDecode(TIFF *tif, uint8_t *buf, tmsize_t cc) |
| 979 |
{ |
| 980 |
OJPEGState *sp = (OJPEGState *)tif->tif_data; |
| 981 |
(void)buf; |
| 982 |
(void)cc; |
| 983 |
/* This function somehow incorrectly assumes that a whole strile was read, |
| 984 |
*/ |
| 985 |
/* which is not true when TIFFReadScanline() is called, */ |
| 986 |
/* and may thus incorrectly consider it has read the whole image, causing */ |
| 987 |
/* OJPEGLibjpegSessionAbort() to be called prematurely. */ |
| 988 |
/* So this logic should be fixed to take into account cc, or disable */ |
| 989 |
/* the scan line reading interface. */ |
| 990 |
/* Triggered by https://gitlab.com/libtiff/libtiff/-/issues/337 */ |
| 991 |
sp->write_curstrile++; |
| 992 |
if (sp->write_curstrile % tif->tif_dir.td_stripsperimage == 0) |
| 993 |
{ |
| 994 |
assert(sp->libjpeg_session_active != 0); |
| 995 |
OJPEGLibjpegSessionAbort(tif); |
| 996 |
sp->writeheader_done = 0; |
| 997 |
} |
| 998 |
} |
| 999 |
|
| 1000 |
static int OJPEGSetupEncode(TIFF *tif) |
| 1001 |
{ |
| 1002 |
static const char module[] = "OJPEGSetupEncode"; |
| 1003 |
TIFFErrorExtR( |
| 1004 |
tif, module, |
| 1005 |
"OJPEG encoding not supported; use new-style JPEG compression instead"); |
| 1006 |
return (0); |
| 1007 |
} |
| 1008 |
|
| 1009 |
static int OJPEGPreEncode(TIFF *tif, uint16_t s) |
| 1010 |
{ |
| 1011 |
static const char module[] = "OJPEGPreEncode"; |
| 1012 |
(void)s; |
| 1013 |
TIFFErrorExtR( |
| 1014 |
tif, module, |
| 1015 |
"OJPEG encoding not supported; use new-style JPEG compression instead"); |
| 1016 |
return (0); |
| 1017 |
} |
| 1018 |
|
| 1019 |
static int OJPEGEncode(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s) |
| 1020 |
{ |
| 1021 |
static const char module[] = "OJPEGEncode"; |
| 1022 |
(void)buf; |
| 1023 |
(void)cc; |
| 1024 |
(void)s; |
| 1025 |
TIFFErrorExtR( |
| 1026 |
tif, module, |
| 1027 |
"OJPEG encoding not supported; use new-style JPEG compression instead"); |
| 1028 |
return (0); |
| 1029 |
} |
| 1030 |
|
| 1031 |
static int OJPEGPostEncode(TIFF *tif) |
| 1032 |
{ |
| 1033 |
static const char module[] = "OJPEGPostEncode"; |
| 1034 |
TIFFErrorExtR( |
| 1035 |
tif, module, |
| 1036 |
"OJPEG encoding not supported; use new-style JPEG compression instead"); |
| 1037 |
return (0); |
| 1038 |
} |
| 1039 |
|
| 1040 |
static void OJPEGCleanup(TIFF *tif) |
| 1041 |
{ |
| 1042 |
OJPEGState *sp = (OJPEGState *)tif->tif_data; |
| 1043 |
if (sp != 0) |
| 1044 |
{ |
| 1045 |
tif->tif_tagmethods.vgetfield = sp->vgetparent; |
| 1046 |
tif->tif_tagmethods.vsetfield = sp->vsetparent; |
| 1047 |
tif->tif_tagmethods.printdir = sp->printdir; |
| 1048 |
if (sp->qtable[0] != 0) |
| 1049 |
_TIFFfreeExt(tif, sp->qtable[0]); |
| 1050 |
if (sp->qtable[1] != 0) |
| 1051 |
_TIFFfreeExt(tif, sp->qtable[1]); |
| 1052 |
if (sp->qtable[2] != 0) |
| 1053 |
_TIFFfreeExt(tif, sp->qtable[2]); |
| 1054 |
if (sp->qtable[3] != 0) |
| 1055 |
_TIFFfreeExt(tif, sp->qtable[3]); |
| 1056 |
if (sp->dctable[0] != 0) |
| 1057 |
_TIFFfreeExt(tif, sp->dctable[0]); |
| 1058 |
if (sp->dctable[1] != 0) |
| 1059 |
_TIFFfreeExt(tif, sp->dctable[1]); |
| 1060 |
if (sp->dctable[2] != 0) |
| 1061 |
_TIFFfreeExt(tif, sp->dctable[2]); |
| 1062 |
if (sp->dctable[3] != 0) |
| 1063 |
_TIFFfreeExt(tif, sp->dctable[3]); |
| 1064 |
if (sp->actable[0] != 0) |
| 1065 |
_TIFFfreeExt(tif, sp->actable[0]); |
| 1066 |
if (sp->actable[1] != 0) |
| 1067 |
_TIFFfreeExt(tif, sp->actable[1]); |
| 1068 |
if (sp->actable[2] != 0) |
| 1069 |
_TIFFfreeExt(tif, sp->actable[2]); |
| 1070 |
if (sp->actable[3] != 0) |
| 1071 |
_TIFFfreeExt(tif, sp->actable[3]); |
| 1072 |
if (sp->libjpeg_session_active != 0) |
| 1073 |
OJPEGLibjpegSessionAbort(tif); |
| 1074 |
if (sp->subsampling_convert_ycbcrbuf != 0) |
| 1075 |
_TIFFfreeExt(tif, sp->subsampling_convert_ycbcrbuf); |
| 1076 |
if (sp->subsampling_convert_ycbcrimage != 0) |
| 1077 |
_TIFFfreeExt(tif, sp->subsampling_convert_ycbcrimage); |
| 1078 |
if (sp->skip_buffer != 0) |
| 1079 |
_TIFFfreeExt(tif, sp->skip_buffer); |
| 1080 |
_TIFFfreeExt(tif, sp); |
| 1081 |
tif->tif_data = NULL; |
| 1082 |
_TIFFSetDefaultCompressionState(tif); |
| 1083 |
} |
| 1084 |
} |
| 1085 |
|
| 1086 |
static void OJPEGSubsamplingCorrect(TIFF *tif) |
| 1087 |
{ |
| 1088 |
static const char module[] = "OJPEGSubsamplingCorrect"; |
| 1089 |
OJPEGState *sp = (OJPEGState *)tif->tif_data; |
| 1090 |
uint8_t mh; |
| 1091 |
uint8_t mv; |
| 1092 |
|
| 1093 |
assert(sp->subsamplingcorrect_done == 0); |
| 1094 |
if ((tif->tif_dir.td_samplesperpixel != 3) || |
| 1095 |
((tif->tif_dir.td_photometric != PHOTOMETRIC_YCBCR) && |
| 1096 |
(tif->tif_dir.td_photometric != PHOTOMETRIC_ITULAB))) |
| 1097 |
{ |
| 1098 |
if (sp->subsampling_tag != 0) |
| 1099 |
TIFFWarningExtR(tif, module, |
| 1100 |
"Subsampling tag not appropriate for this " |
| 1101 |
"Photometric and/or SamplesPerPixel"); |
| 1102 |
sp->subsampling_hor = 1; |
| 1103 |
sp->subsampling_ver = 1; |
| 1104 |
sp->subsampling_force_desubsampling_inside_decompression = 0; |
| 1105 |
} |
| 1106 |
else |
| 1107 |
{ |
| 1108 |
sp->subsamplingcorrect_done = 1; |
| 1109 |
mh = sp->subsampling_hor; |
| 1110 |
mv = sp->subsampling_ver; |
| 1111 |
sp->subsamplingcorrect = 1; |
| 1112 |
OJPEGReadHeaderInfoSec(tif); |
| 1113 |
if (sp->subsampling_force_desubsampling_inside_decompression != 0) |
| 1114 |
{ |
| 1115 |
sp->subsampling_hor = 1; |
| 1116 |
sp->subsampling_ver = 1; |
| 1117 |
} |
| 1118 |
sp->subsamplingcorrect = 0; |
| 1119 |
if (((sp->subsampling_hor != mh) || (sp->subsampling_ver != mv)) && |
| 1120 |
(sp->subsampling_force_desubsampling_inside_decompression == 0)) |
| 1121 |
{ |
| 1122 |
if (sp->subsampling_tag == 0) |
| 1123 |
TIFFWarningExtR( |
| 1124 |
tif, module, |
| 1125 |
"Subsampling tag is not set, yet subsampling inside JPEG " |
| 1126 |
"data [%" PRIu8 ",%" PRIu8 |
| 1127 |
"] does not match default values [2,2]; assuming " |
| 1128 |
"subsampling inside JPEG data is correct", |
| 1129 |
sp->subsampling_hor, sp->subsampling_ver); |
| 1130 |
else |
| 1131 |
TIFFWarningExtR( |
| 1132 |
tif, module, |
| 1133 |
"Subsampling inside JPEG data [%" PRIu8 ",%" PRIu8 |
| 1134 |
"] does not match subsampling tag values [%" PRIu8 |
| 1135 |
",%" PRIu8 |
| 1136 |
"]; assuming subsampling inside JPEG data is correct", |
| 1137 |
sp->subsampling_hor, sp->subsampling_ver, mh, mv); |
| 1138 |
} |
| 1139 |
if (sp->subsampling_force_desubsampling_inside_decompression != 0) |
| 1140 |
{ |
| 1141 |
if (sp->subsampling_tag == 0) |
| 1142 |
TIFFWarningExtR( |
| 1143 |
tif, module, |
| 1144 |
"Subsampling tag is not set, yet subsampling inside JPEG " |
| 1145 |
"data does not match default values [2,2] (nor any other " |
| 1146 |
"values allowed in TIFF); assuming subsampling inside JPEG " |
| 1147 |
"data is correct and desubsampling inside JPEG " |
| 1148 |
"decompression"); |
| 1149 |
else |
| 1150 |
TIFFWarningExtR( |
| 1151 |
tif, module, |
| 1152 |
"Subsampling inside JPEG data does not match subsampling " |
| 1153 |
"tag values [%" PRIu8 ",%" PRIu8 |
| 1154 |
"] (nor any other values allowed in TIFF); assuming " |
| 1155 |
"subsampling inside JPEG data is correct and desubsampling " |
| 1156 |
"inside JPEG decompression", |
| 1157 |
mh, mv); |
| 1158 |
} |
| 1159 |
if (sp->subsampling_force_desubsampling_inside_decompression == 0) |
| 1160 |
{ |
| 1161 |
if (sp->subsampling_hor < sp->subsampling_ver) |
| 1162 |
TIFFWarningExtR(tif, module, |
| 1163 |
"Subsampling values [%" PRIu8 ",%" PRIu8 |
| 1164 |
"] are not allowed in TIFF", |
| 1165 |
sp->subsampling_hor, sp->subsampling_ver); |
| 1166 |
} |
| 1167 |
} |
| 1168 |
sp->subsamplingcorrect_done = 1; |
| 1169 |
} |
| 1170 |
|
| 1171 |
static int OJPEGReadHeaderInfo(TIFF *tif) |
| 1172 |
{ |
| 1173 |
static const char module[] = "OJPEGReadHeaderInfo"; |
| 1174 |
OJPEGState *sp = (OJPEGState *)tif->tif_data; |
| 1175 |
assert(sp->readheader_done == 0); |
| 1176 |
sp->image_width = tif->tif_dir.td_imagewidth; |
| 1177 |
sp->image_length = tif->tif_dir.td_imagelength; |
| 1178 |
if (isTiled(tif)) |
| 1179 |
{ |
| 1180 |
sp->strile_width = tif->tif_dir.td_tilewidth; |
| 1181 |
sp->strile_length = tif->tif_dir.td_tilelength; |
| 1182 |
sp->strile_length_total = |
| 1183 |
((sp->image_length + sp->strile_length - 1) / sp->strile_length) * |
| 1184 |
sp->strile_length; |
| 1185 |
} |
| 1186 |
else |
| 1187 |
{ |
| 1188 |
sp->strile_width = sp->image_width; |
| 1189 |
sp->strile_length = tif->tif_dir.td_rowsperstrip; |
| 1190 |
if (sp->strile_length == (uint32_t)-1) |
| 1191 |
sp->strile_length = sp->image_length; |
| 1192 |
sp->strile_length_total = sp->image_length; |
| 1193 |
} |
| 1194 |
if (tif->tif_dir.td_samplesperpixel == 1) |
| 1195 |
{ |
| 1196 |
sp->samples_per_pixel = 1; |
| 1197 |
sp->plane_sample_offset = 0; |
| 1198 |
sp->samples_per_pixel_per_plane = sp->samples_per_pixel; |
| 1199 |
sp->subsampling_hor = 1; |
| 1200 |
sp->subsampling_ver = 1; |
| 1201 |
} |
| 1202 |
else |
| 1203 |
{ |
| 1204 |
if (tif->tif_dir.td_samplesperpixel != 3) |
| 1205 |
{ |
| 1206 |
TIFFErrorExtR(tif, module, |
| 1207 |
"SamplesPerPixel %" PRIu8 |
| 1208 |
" not supported for this compression scheme", |
| 1209 |
sp->samples_per_pixel); |
| 1210 |
return (0); |
| 1211 |
} |
| 1212 |
sp->samples_per_pixel = 3; |
| 1213 |
sp->plane_sample_offset = 0; |
| 1214 |
if (tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG) |
| 1215 |
sp->samples_per_pixel_per_plane = 3; |
| 1216 |
else |
| 1217 |
sp->samples_per_pixel_per_plane = 1; |
| 1218 |
} |
| 1219 |
if (sp->strile_length < sp->image_length) |
| 1220 |
{ |
| 1221 |
if (((sp->subsampling_hor != 1) && (sp->subsampling_hor != 2) && |
| 1222 |
(sp->subsampling_hor != 4)) || |
| 1223 |
((sp->subsampling_ver != 1) && (sp->subsampling_ver != 2) && |
| 1224 |
(sp->subsampling_ver != 4))) |
| 1225 |
{ |
| 1226 |
TIFFErrorExtR(tif, module, "Invalid subsampling values"); |
| 1227 |
return (0); |
| 1228 |
} |
| 1229 |
if (sp->strile_length % (sp->subsampling_ver * 8) != 0) |
| 1230 |
{ |
| 1231 |
TIFFErrorExtR(tif, module, |
| 1232 |
"Incompatible vertical subsampling and image " |
| 1233 |
"strip/tile length"); |
| 1234 |
return (0); |
| 1235 |
} |
| 1236 |
sp->restart_interval = |
| 1237 |
(uint16_t)(((sp->strile_width + sp->subsampling_hor * 8 - 1) / |
| 1238 |
(sp->subsampling_hor * 8)) * |
| 1239 |
(sp->strile_length / (sp->subsampling_ver * 8))); |
| 1240 |
} |
| 1241 |
if (OJPEGReadHeaderInfoSec(tif) == 0) |
| 1242 |
return (0); |
| 1243 |
sp->sos_end[0].log = 1; |
| 1244 |
sp->sos_end[0].in_buffer_source = sp->in_buffer_source; |
| 1245 |
sp->sos_end[0].in_buffer_next_strile = sp->in_buffer_next_strile; |
| 1246 |
sp->sos_end[0].in_buffer_file_pos = |
| 1247 |
sp->in_buffer_file_pos - sp->in_buffer_togo; |
| 1248 |
sp->sos_end[0].in_buffer_file_togo = |
| 1249 |
sp->in_buffer_file_togo + sp->in_buffer_togo; |
| 1250 |
sp->readheader_done = 1; |
| 1251 |
return (1); |
| 1252 |
} |
| 1253 |
|
| 1254 |
static int OJPEGReadSecondarySos(TIFF *tif, uint16_t s) |
| 1255 |
{ |
| 1256 |
OJPEGState *sp = (OJPEGState *)tif->tif_data; |
| 1257 |
uint8_t m; |
| 1258 |
assert(s > 0); |
| 1259 |
assert(s < 3); |
| 1260 |
assert(sp->sos_end[0].log != 0); |
| 1261 |
assert(sp->sos_end[s].log == 0); |
| 1262 |
sp->plane_sample_offset = (uint8_t)(s - 1); |
| 1263 |
while (sp->sos_end[sp->plane_sample_offset].log == 0) |
| 1264 |
sp->plane_sample_offset--; |
| 1265 |
sp->in_buffer_source = |
| 1266 |
sp->sos_end[sp->plane_sample_offset].in_buffer_source; |
| 1267 |
sp->in_buffer_next_strile = |
| 1268 |
sp->sos_end[sp->plane_sample_offset].in_buffer_next_strile; |
| 1269 |
sp->in_buffer_file_pos = |
| 1270 |
sp->sos_end[sp->plane_sample_offset].in_buffer_file_pos; |
| 1271 |
sp->in_buffer_file_pos_log = 0; |
| 1272 |
sp->in_buffer_file_togo = |
| 1273 |
sp->sos_end[sp->plane_sample_offset].in_buffer_file_togo; |
| 1274 |
sp->in_buffer_togo = 0; |
| 1275 |
sp->in_buffer_cur = 0; |
| 1276 |
while (sp->plane_sample_offset < s) |
| 1277 |
{ |
| 1278 |
do |
| 1279 |
{ |
| 1280 |
if (OJPEGReadByte(sp, &m) == 0) |
| 1281 |
return (0); |
| 1282 |
if (m == 255) |
| 1283 |
{ |
| 1284 |
do |
| 1285 |
{ |
| 1286 |
if (OJPEGReadByte(sp, &m) == 0) |
| 1287 |
return (0); |
| 1288 |
if (m != 255) |
| 1289 |
break; |
| 1290 |
} while (1); |
| 1291 |
if (m == JPEG_MARKER_SOS) |
| 1292 |
break; |
| 1293 |
} |
| 1294 |
} while (1); |
| 1295 |
sp->plane_sample_offset++; |
| 1296 |
if (OJPEGReadHeaderInfoSecStreamSos(tif) == 0) |
| 1297 |
return (0); |
| 1298 |
sp->sos_end[sp->plane_sample_offset].log = 1; |
| 1299 |
sp->sos_end[sp->plane_sample_offset].in_buffer_source = |
| 1300 |
sp->in_buffer_source; |
| 1301 |
sp->sos_end[sp->plane_sample_offset].in_buffer_next_strile = |
| 1302 |
sp->in_buffer_next_strile; |
| 1303 |
sp->sos_end[sp->plane_sample_offset].in_buffer_file_pos = |
| 1304 |
sp->in_buffer_file_pos - sp->in_buffer_togo; |
| 1305 |
sp->sos_end[sp->plane_sample_offset].in_buffer_file_togo = |
| 1306 |
sp->in_buffer_file_togo + sp->in_buffer_togo; |
| 1307 |
} |
| 1308 |
return (1); |
| 1309 |
} |
| 1310 |
|
| 1311 |
static int OJPEGWriteHeaderInfo(TIFF *tif) |
| 1312 |
{ |
| 1313 |
static const char module[] = "OJPEGWriteHeaderInfo"; |
| 1314 |
OJPEGState *sp = (OJPEGState *)tif->tif_data; |
| 1315 |
uint8_t **m; |
| 1316 |
uint32_t n; |
| 1317 |
/* if a previous attempt failed, don't try again */ |
| 1318 |
if (sp->libjpeg_session_active != 0) |
| 1319 |
return 0; |
| 1320 |
sp->out_state = ososSoi; |
| 1321 |
sp->restart_index = 0; |
| 1322 |
jpeg_std_error(&(sp->libjpeg_jpeg_error_mgr)); |
| 1323 |
sp->libjpeg_jpeg_error_mgr.output_message = |
| 1324 |
OJPEGLibjpegJpegErrorMgrOutputMessage; |
| 1325 |
sp->libjpeg_jpeg_error_mgr.error_exit = OJPEGLibjpegJpegErrorMgrErrorExit; |
| 1326 |
sp->libjpeg_jpeg_decompress_struct.err = &(sp->libjpeg_jpeg_error_mgr); |
| 1327 |
sp->libjpeg_jpeg_decompress_struct.client_data = (void *)tif; |
| 1328 |
if (jpeg_create_decompress_encap( |
| 1329 |
sp, &(sp->libjpeg_jpeg_decompress_struct)) == 0) |
| 1330 |
return (0); |
| 1331 |
sp->libjpeg_session_active = 1; |
| 1332 |
sp->libjpeg_jpeg_source_mgr.bytes_in_buffer = 0; |
| 1333 |
sp->libjpeg_jpeg_source_mgr.init_source = |
| 1334 |
OJPEGLibjpegJpegSourceMgrInitSource; |
| 1335 |
sp->libjpeg_jpeg_source_mgr.fill_input_buffer = |
| 1336 |
OJPEGLibjpegJpegSourceMgrFillInputBuffer; |
| 1337 |
sp->libjpeg_jpeg_source_mgr.skip_input_data = |
| 1338 |
OJPEGLibjpegJpegSourceMgrSkipInputData; |
| 1339 |
sp->libjpeg_jpeg_source_mgr.resync_to_restart = |
| 1340 |
OJPEGLibjpegJpegSourceMgrResyncToRestart; |
| 1341 |
sp->libjpeg_jpeg_source_mgr.term_source = |
| 1342 |
OJPEGLibjpegJpegSourceMgrTermSource; |
| 1343 |
sp->libjpeg_jpeg_decompress_struct.src = &(sp->libjpeg_jpeg_source_mgr); |
| 1344 |
if (jpeg_read_header_encap(sp, &(sp->libjpeg_jpeg_decompress_struct), 1) == |
| 1345 |
0) |
| 1346 |
return (0); |
| 1347 |
if ((sp->subsampling_force_desubsampling_inside_decompression == 0) && |
| 1348 |
(sp->samples_per_pixel_per_plane > 1)) |
| 1349 |
{ |
| 1350 |
sp->libjpeg_jpeg_decompress_struct.raw_data_out = 1; |
| 1351 |
#if JPEG_LIB_VERSION >= 70 |
| 1352 |
sp->libjpeg_jpeg_decompress_struct.do_fancy_upsampling = FALSE; |
| 1353 |
#endif |
| 1354 |
sp->libjpeg_jpeg_query_style = 0; |
| 1355 |
if (sp->subsampling_convert_log == 0) |
| 1356 |
{ |
| 1357 |
assert(sp->subsampling_convert_ycbcrbuf == 0); |
| 1358 |
assert(sp->subsampling_convert_ycbcrimage == 0); |
| 1359 |
/* Check for division by zero. */ |
| 1360 |
if (sp->subsampling_hor == 0 || sp->subsampling_ver == 0) |
| 1361 |
return (0); |
| 1362 |
sp->subsampling_convert_ylinelen = |
| 1363 |
((sp->strile_width + sp->subsampling_hor * 8 - 1) / |
| 1364 |
(sp->subsampling_hor * 8) * sp->subsampling_hor * 8); |
| 1365 |
sp->subsampling_convert_ylines = sp->subsampling_ver * 8; |
| 1366 |
sp->subsampling_convert_clinelen = |
| 1367 |
sp->subsampling_convert_ylinelen / sp->subsampling_hor; |
| 1368 |
sp->subsampling_convert_clines = 8; |
| 1369 |
sp->subsampling_convert_ybuflen = sp->subsampling_convert_ylinelen * |
| 1370 |
sp->subsampling_convert_ylines; |
| 1371 |
sp->subsampling_convert_cbuflen = sp->subsampling_convert_clinelen * |
| 1372 |
sp->subsampling_convert_clines; |
| 1373 |
sp->subsampling_convert_ycbcrbuflen = |
| 1374 |
sp->subsampling_convert_ybuflen + |
| 1375 |
2 * sp->subsampling_convert_cbuflen; |
| 1376 |
/* The calloc is not normally necessary, except in some edge/broken |
| 1377 |
* cases */ |
| 1378 |
/* for example for a tiled image of height 1 with a tile height of 1 |
| 1379 |
* and subsampling_hor=subsampling_ver=2 */ |
| 1380 |
/* In that case, libjpeg will only fill the 8 first lines of the 16 |
| 1381 |
* lines */ |
| 1382 |
/* See https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=16844 |
| 1383 |
*/ |
| 1384 |
/* Even if this case is allowed (?), its handling is broken because |
| 1385 |
* OJPEGPreDecode() should also likely */ |
| 1386 |
/* reset subsampling_convert_state to 0 when changing tile. */ |
| 1387 |
sp->subsampling_convert_ycbcrbuf = |
| 1388 |
_TIFFcallocExt(tif, 1, sp->subsampling_convert_ycbcrbuflen); |
| 1389 |
if (sp->subsampling_convert_ycbcrbuf == 0) |
| 1390 |
{ |
| 1391 |
TIFFErrorExtR(tif, module, "Out of memory"); |
| 1392 |
return (0); |
| 1393 |
} |
| 1394 |
sp->subsampling_convert_ybuf = sp->subsampling_convert_ycbcrbuf; |
| 1395 |
sp->subsampling_convert_cbbuf = |
| 1396 |
sp->subsampling_convert_ybuf + sp->subsampling_convert_ybuflen; |
| 1397 |
sp->subsampling_convert_crbuf = |
| 1398 |
sp->subsampling_convert_cbbuf + sp->subsampling_convert_cbuflen; |
| 1399 |
sp->subsampling_convert_ycbcrimagelen = |
| 1400 |
3 + sp->subsampling_convert_ylines + |
| 1401 |
2 * sp->subsampling_convert_clines; |
| 1402 |
sp->subsampling_convert_ycbcrimage = _TIFFmallocExt( |
| 1403 |
tif, sp->subsampling_convert_ycbcrimagelen * sizeof(uint8_t *)); |
| 1404 |
if (sp->subsampling_convert_ycbcrimage == 0) |
| 1405 |
{ |
| 1406 |
TIFFErrorExtR(tif, module, "Out of memory"); |
| 1407 |
return (0); |
| 1408 |
} |
| 1409 |
m = sp->subsampling_convert_ycbcrimage; |
| 1410 |
*m++ = (uint8_t *)(sp->subsampling_convert_ycbcrimage + 3); |
| 1411 |
*m++ = (uint8_t *)(sp->subsampling_convert_ycbcrimage + 3 + |
| 1412 |
sp->subsampling_convert_ylines); |
| 1413 |
*m++ = (uint8_t *)(sp->subsampling_convert_ycbcrimage + 3 + |
| 1414 |
sp->subsampling_convert_ylines + |
| 1415 |
sp->subsampling_convert_clines); |
| 1416 |
for (n = 0; n < sp->subsampling_convert_ylines; n++) |
| 1417 |
*m++ = sp->subsampling_convert_ybuf + |
| 1418 |
n * sp->subsampling_convert_ylinelen; |
| 1419 |
for (n = 0; n < sp->subsampling_convert_clines; n++) |
| 1420 |
*m++ = sp->subsampling_convert_cbbuf + |
| 1421 |
n * sp->subsampling_convert_clinelen; |
| 1422 |
for (n = 0; n < sp->subsampling_convert_clines; n++) |
| 1423 |
*m++ = sp->subsampling_convert_crbuf + |
| 1424 |
n * sp->subsampling_convert_clinelen; |
| 1425 |
sp->subsampling_convert_clinelenout = |
| 1426 |
sp->strile_width / sp->subsampling_hor + |
| 1427 |
((sp->strile_width % sp->subsampling_hor) != 0 ? 1 : 0); |
| 1428 |
sp->subsampling_convert_state = 0; |
| 1429 |
sp->error_in_raw_data_decoding = 0; |
| 1430 |
sp->bytes_per_line = |
| 1431 |
sp->subsampling_convert_clinelenout * |
| 1432 |
(sp->subsampling_ver * sp->subsampling_hor + 2); |
| 1433 |
sp->lines_per_strile = |
| 1434 |
sp->strile_length / sp->subsampling_ver + |
| 1435 |
((sp->strile_length % sp->subsampling_ver) != 0 ? 1 : 0); |
| 1436 |
sp->subsampling_convert_log = 1; |
| 1437 |
} |
| 1438 |
} |
| 1439 |
else |
| 1440 |
{ |
| 1441 |
sp->libjpeg_jpeg_decompress_struct.jpeg_color_space = JCS_UNKNOWN; |
| 1442 |
sp->libjpeg_jpeg_decompress_struct.out_color_space = JCS_UNKNOWN; |
| 1443 |
sp->libjpeg_jpeg_query_style = 1; |
| 1444 |
sp->bytes_per_line = sp->samples_per_pixel_per_plane * sp->strile_width; |
| 1445 |
sp->lines_per_strile = sp->strile_length; |
| 1446 |
} |
| 1447 |
if (jpeg_start_decompress_encap(sp, |
| 1448 |
&(sp->libjpeg_jpeg_decompress_struct)) == 0) |
| 1449 |
return (0); |
| 1450 |
if (sp->libjpeg_jpeg_decompress_struct.image_width != sp->strile_width) |
| 1451 |
{ |
| 1452 |
TIFFErrorExtR(tif, module, |
| 1453 |
"jpeg_start_decompress() returned image_width = %u, " |
| 1454 |
"expected %" PRIu32, |
| 1455 |
sp->libjpeg_jpeg_decompress_struct.image_width, |
| 1456 |
sp->strile_width); |
| 1457 |
return 0; |
| 1458 |
} |
| 1459 |
if (sp->libjpeg_jpeg_decompress_struct.max_h_samp_factor != |
| 1460 |
sp->subsampling_hor || |
| 1461 |
sp->libjpeg_jpeg_decompress_struct.max_v_samp_factor != |
| 1462 |
sp->subsampling_ver) |
| 1463 |
{ |
| 1464 |
TIFFErrorExtR(tif, module, |
| 1465 |
"jpeg_start_decompress() returned max_h_samp_factor = %d " |
| 1466 |
"and max_v_samp_factor = %d, expected %" PRIu8 |
| 1467 |
" and %" PRIu8, |
| 1468 |
sp->libjpeg_jpeg_decompress_struct.max_h_samp_factor, |
| 1469 |
sp->libjpeg_jpeg_decompress_struct.max_v_samp_factor, |
| 1470 |
sp->subsampling_hor, sp->subsampling_ver); |
| 1471 |
return 0; |
| 1472 |
} |
| 1473 |
|
| 1474 |
sp->writeheader_done = 1; |
| 1475 |
return (1); |
| 1476 |
} |
| 1477 |
|
| 1478 |
static void OJPEGLibjpegSessionAbort(TIFF *tif) |
| 1479 |
{ |
| 1480 |
OJPEGState *sp = (OJPEGState *)tif->tif_data; |
| 1481 |
assert(sp->libjpeg_session_active != 0); |
| 1482 |
jpeg_destroy((jpeg_common_struct *)(&(sp->libjpeg_jpeg_decompress_struct))); |
| 1483 |
sp->libjpeg_session_active = 0; |
| 1484 |
} |
| 1485 |
|
| 1486 |
static int OJPEGReadHeaderInfoSec(TIFF *tif) |
| 1487 |
{ |
| 1488 |
static const char module[] = "OJPEGReadHeaderInfoSec"; |
| 1489 |
OJPEGState *sp = (OJPEGState *)tif->tif_data; |
| 1490 |
uint8_t m; |
| 1491 |
uint16_t n; |
| 1492 |
uint8_t o; |
| 1493 |
if (sp->file_size == 0) |
| 1494 |
sp->file_size = TIFFGetFileSize(tif); |
| 1495 |
if (sp->jpeg_interchange_format != 0) |
| 1496 |
{ |
| 1497 |
if (sp->jpeg_interchange_format >= sp->file_size) |
| 1498 |
{ |
| 1499 |
sp->jpeg_interchange_format = 0; |
| 1500 |
sp->jpeg_interchange_format_length = 0; |
| 1501 |
} |
| 1502 |
else |
| 1503 |
{ |
| 1504 |
if ((sp->jpeg_interchange_format_length == 0) || |
| 1505 |
(sp->jpeg_interchange_format > |
| 1506 |
UINT64_MAX - sp->jpeg_interchange_format_length) || |
| 1507 |
(sp->jpeg_interchange_format + |
| 1508 |
sp->jpeg_interchange_format_length > |
| 1509 |
sp->file_size)) |
| 1510 |
sp->jpeg_interchange_format_length = |
| 1511 |
sp->file_size - sp->jpeg_interchange_format; |
| 1512 |
} |
| 1513 |
} |
| 1514 |
sp->in_buffer_source = osibsNotSetYet; |
| 1515 |
sp->in_buffer_next_strile = 0; |
| 1516 |
sp->in_buffer_strile_count = tif->tif_dir.td_nstrips; |
| 1517 |
sp->in_buffer_file_togo = 0; |
| 1518 |
sp->in_buffer_togo = 0; |
| 1519 |
do |
| 1520 |
{ |
| 1521 |
if (OJPEGReadBytePeek(sp, &m) == 0) |
| 1522 |
return (0); |
| 1523 |
if (m != 255) |
| 1524 |
break; |
| 1525 |
OJPEGReadByteAdvance(sp); |
| 1526 |
do |
| 1527 |
{ |
| 1528 |
if (OJPEGReadByte(sp, &m) == 0) |
| 1529 |
return (0); |
| 1530 |
} while (m == 255); |
| 1531 |
switch (m) |
| 1532 |
{ |
| 1533 |
case JPEG_MARKER_SOI: |
| 1534 |
/* this type of marker has no data, and should be skipped */ |
| 1535 |
break; |
| 1536 |
case JPEG_MARKER_COM: |
| 1537 |
case JPEG_MARKER_APP0: |
| 1538 |
case JPEG_MARKER_APP0 + 1: |
| 1539 |
case JPEG_MARKER_APP0 + 2: |
| 1540 |
case JPEG_MARKER_APP0 + 3: |
| 1541 |
case JPEG_MARKER_APP0 + 4: |
| 1542 |
case JPEG_MARKER_APP0 + 5: |
| 1543 |
case JPEG_MARKER_APP0 + 6: |
| 1544 |
case JPEG_MARKER_APP0 + 7: |
| 1545 |
case JPEG_MARKER_APP0 + 8: |
| 1546 |
case JPEG_MARKER_APP0 + 9: |
| 1547 |
case JPEG_MARKER_APP0 + 10: |
| 1548 |
case JPEG_MARKER_APP0 + 11: |
| 1549 |
case JPEG_MARKER_APP0 + 12: |
| 1550 |
case JPEG_MARKER_APP0 + 13: |
| 1551 |
case JPEG_MARKER_APP0 + 14: |
| 1552 |
case JPEG_MARKER_APP0 + 15: |
| 1553 |
/* this type of marker has data, but it has no use to us (and no |
| 1554 |
* place here) and should be skipped */ |
| 1555 |
if (OJPEGReadWord(sp, &n) == 0) |
| 1556 |
return (0); |
| 1557 |
if (n < 2) |
| 1558 |
{ |
| 1559 |
if (sp->subsamplingcorrect == 0) |
| 1560 |
TIFFErrorExtR(tif, module, "Corrupt JPEG data"); |
| 1561 |
return (0); |
| 1562 |
} |
| 1563 |
if (n > 2) |
| 1564 |
OJPEGReadSkip(sp, n - 2); |
| 1565 |
break; |
| 1566 |
case JPEG_MARKER_DRI: |
| 1567 |
if (OJPEGReadHeaderInfoSecStreamDri(tif) == 0) |
| 1568 |
return (0); |
| 1569 |
break; |
| 1570 |
case JPEG_MARKER_DQT: |
| 1571 |
if (OJPEGReadHeaderInfoSecStreamDqt(tif) == 0) |
| 1572 |
return (0); |
| 1573 |
break; |
| 1574 |
case JPEG_MARKER_DHT: |
| 1575 |
if (OJPEGReadHeaderInfoSecStreamDht(tif) == 0) |
| 1576 |
return (0); |
| 1577 |
break; |
| 1578 |
case JPEG_MARKER_SOF0: |
| 1579 |
case JPEG_MARKER_SOF1: |
| 1580 |
case JPEG_MARKER_SOF3: |
| 1581 |
if (OJPEGReadHeaderInfoSecStreamSof(tif, m) == 0) |
| 1582 |
return (0); |
| 1583 |
if (sp->subsamplingcorrect != 0) |
| 1584 |
return (1); |
| 1585 |
break; |
| 1586 |
case JPEG_MARKER_SOS: |
| 1587 |
if (sp->subsamplingcorrect != 0) |
| 1588 |
return (1); |
| 1589 |
assert(sp->plane_sample_offset == 0); |
| 1590 |
if (OJPEGReadHeaderInfoSecStreamSos(tif) == 0) |
| 1591 |
return (0); |
| 1592 |
break; |
| 1593 |
default: |
| 1594 |
TIFFErrorExtR(tif, module, |
| 1595 |
"Unknown marker type %" PRIu8 " in JPEG data", m); |
| 1596 |
return (0); |
| 1597 |
} |
| 1598 |
} while (m != JPEG_MARKER_SOS); |
| 1599 |
if (sp->subsamplingcorrect) |
| 1600 |
return (1); |
| 1601 |
if (sp->sof_log == 0) |
| 1602 |
{ |
| 1603 |
if (OJPEGReadHeaderInfoSecTablesQTable(tif) == 0) |
| 1604 |
return (0); |
| 1605 |
sp->sof_marker_id = JPEG_MARKER_SOF0; |
| 1606 |
for (o = 0; o < sp->samples_per_pixel; o++) |
| 1607 |
sp->sof_c[o] = o; |
| 1608 |
sp->sof_hv[0] = ((sp->subsampling_hor << 4) | sp->subsampling_ver); |
| 1609 |
for (o = 1; o < sp->samples_per_pixel; o++) |
| 1610 |
sp->sof_hv[o] = 17; |
| 1611 |
sp->sof_x = sp->strile_width; |
| 1612 |
sp->sof_y = sp->strile_length_total; |
| 1613 |
sp->sof_log = 1; |
| 1614 |
if (OJPEGReadHeaderInfoSecTablesDcTable(tif) == 0) |
| 1615 |
return (0); |
| 1616 |
if (OJPEGReadHeaderInfoSecTablesAcTable(tif) == 0) |
| 1617 |
return (0); |
| 1618 |
for (o = 1; o < sp->samples_per_pixel; o++) |
| 1619 |
sp->sos_cs[o] = o; |
| 1620 |
} |
| 1621 |
return (1); |
| 1622 |
} |
| 1623 |
|
| 1624 |
static int OJPEGReadHeaderInfoSecStreamDri(TIFF *tif) |
| 1625 |
{ |
| 1626 |
/* This could easily cause trouble in some cases... but no such cases have |
| 1627 |
occurred so far */ |
| 1628 |
static const char module[] = "OJPEGReadHeaderInfoSecStreamDri"; |
| 1629 |
OJPEGState *sp = (OJPEGState *)tif->tif_data; |
| 1630 |
uint16_t m; |
| 1631 |
if (OJPEGReadWord(sp, &m) == 0) |
| 1632 |
return (0); |
| 1633 |
if (m != 4) |
| 1634 |
{ |
| 1635 |
TIFFErrorExtR(tif, module, "Corrupt DRI marker in JPEG data"); |
| 1636 |
return (0); |
| 1637 |
} |
| 1638 |
if (OJPEGReadWord(sp, &m) == 0) |
| 1639 |
return (0); |
| 1640 |
sp->restart_interval = m; |
| 1641 |
return (1); |
| 1642 |
} |
| 1643 |
|
| 1644 |
static int OJPEGReadHeaderInfoSecStreamDqt(TIFF *tif) |
| 1645 |
{ |
| 1646 |
/* this is a table marker, and it is to be saved as a whole for exact |
| 1647 |
* pushing on the jpeg stream later on */ |
| 1648 |
static const char module[] = "OJPEGReadHeaderInfoSecStreamDqt"; |
| 1649 |
OJPEGState *sp = (OJPEGState *)tif->tif_data; |
| 1650 |
uint16_t m; |
| 1651 |
uint32_t na; |
| 1652 |
uint8_t *nb; |
| 1653 |
uint8_t o; |
| 1654 |
if (OJPEGReadWord(sp, &m) == 0) |
| 1655 |
return (0); |
| 1656 |
if (m <= 2) |
| 1657 |
{ |
| 1658 |
if (sp->subsamplingcorrect == 0) |
| 1659 |
TIFFErrorExtR(tif, module, "Corrupt DQT marker in JPEG data"); |
| 1660 |
return (0); |
| 1661 |
} |
| 1662 |
if (sp->subsamplingcorrect != 0) |
| 1663 |
OJPEGReadSkip(sp, m - 2); |
| 1664 |
else |
| 1665 |
{ |
| 1666 |
m -= 2; |
| 1667 |
do |
| 1668 |
{ |
| 1669 |
if (m < 65) |
| 1670 |
{ |
| 1671 |
TIFFErrorExtR(tif, module, "Corrupt DQT marker in JPEG data"); |
| 1672 |
return (0); |
| 1673 |
} |
| 1674 |
na = sizeof(uint32_t) + 69; |
| 1675 |
nb = _TIFFmallocExt(tif, na); |
| 1676 |
if (nb == 0) |
| 1677 |
{ |
| 1678 |
TIFFErrorExtR(tif, module, "Out of memory"); |
| 1679 |
return (0); |
| 1680 |
} |
| 1681 |
*(uint32_t *)nb = na; |
| 1682 |
nb[sizeof(uint32_t)] = 255; |
| 1683 |
nb[sizeof(uint32_t) + 1] = JPEG_MARKER_DQT; |
| 1684 |
nb[sizeof(uint32_t) + 2] = 0; |
| 1685 |
nb[sizeof(uint32_t) + 3] = 67; |
| 1686 |
if (OJPEGReadBlock(sp, 65, &nb[sizeof(uint32_t) + 4]) == 0) |
| 1687 |
{ |
| 1688 |
_TIFFfreeExt(tif, nb); |
| 1689 |
return (0); |
| 1690 |
} |
| 1691 |
o = nb[sizeof(uint32_t) + 4] & 15; |
| 1692 |
if (3 < o) |
| 1693 |
{ |
| 1694 |
TIFFErrorExtR(tif, module, "Corrupt DQT marker in JPEG data"); |
| 1695 |
_TIFFfreeExt(tif, nb); |
| 1696 |
return (0); |
| 1697 |
} |
| 1698 |
if (sp->qtable[o] != 0) |
| 1699 |
_TIFFfreeExt(tif, sp->qtable[o]); |
| 1700 |
sp->qtable[o] = nb; |
| 1701 |
m -= 65; |
| 1702 |
} while (m > 0); |
| 1703 |
} |
| 1704 |
return (1); |
| 1705 |
} |
| 1706 |
|
| 1707 |
static int OJPEGReadHeaderInfoSecStreamDht(TIFF *tif) |
| 1708 |
{ |
| 1709 |
/* this is a table marker, and it is to be saved as a whole for exact |
| 1710 |
* pushing on the jpeg stream later on */ |
| 1711 |
/* TODO: the following assumes there is only one table in this marker... but |
| 1712 |
* i'm not quite sure that assumption is guaranteed correct */ |
| 1713 |
static const char module[] = "OJPEGReadHeaderInfoSecStreamDht"; |
| 1714 |
OJPEGState *sp = (OJPEGState *)tif->tif_data; |
| 1715 |
uint16_t m; |
| 1716 |
uint32_t na; |
| 1717 |
uint8_t *nb; |
| 1718 |
uint8_t o; |
| 1719 |
if (OJPEGReadWord(sp, &m) == 0) |
| 1720 |
return (0); |
| 1721 |
if (m <= 2) |
| 1722 |
{ |
| 1723 |
if (sp->subsamplingcorrect == 0) |
| 1724 |
TIFFErrorExtR(tif, module, "Corrupt DHT marker in JPEG data"); |
| 1725 |
return (0); |
| 1726 |
} |
| 1727 |
if (sp->subsamplingcorrect != 0) |
| 1728 |
{ |
| 1729 |
OJPEGReadSkip(sp, m - 2); |
| 1730 |
} |
| 1731 |
else |
| 1732 |
{ |
| 1733 |
na = sizeof(uint32_t) + 2 + m; |
| 1734 |
nb = _TIFFmallocExt(tif, na); |
| 1735 |
if (nb == 0) |
| 1736 |
{ |
| 1737 |
TIFFErrorExtR(tif, module, "Out of memory"); |
| 1738 |
return (0); |
| 1739 |
} |
| 1740 |
*(uint32_t *)nb = na; |
| 1741 |
nb[sizeof(uint32_t)] = 255; |
| 1742 |
nb[sizeof(uint32_t) + 1] = JPEG_MARKER_DHT; |
| 1743 |
nb[sizeof(uint32_t) + 2] = (m >> 8); |
| 1744 |
nb[sizeof(uint32_t) + 3] = (m & 255); |
| 1745 |
if (OJPEGReadBlock(sp, m - 2, &nb[sizeof(uint32_t) + 4]) == 0) |
| 1746 |
{ |
| 1747 |
_TIFFfreeExt(tif, nb); |
| 1748 |
return (0); |
| 1749 |
} |
| 1750 |
o = nb[sizeof(uint32_t) + 4]; |
| 1751 |
if ((o & 240) == 0) |
| 1752 |
{ |
| 1753 |
if (3 < o) |
| 1754 |
{ |
| 1755 |
TIFFErrorExtR(tif, module, "Corrupt DHT marker in JPEG data"); |
| 1756 |
_TIFFfreeExt(tif, nb); |
| 1757 |
return (0); |
| 1758 |
} |
| 1759 |
if (sp->dctable[o] != 0) |
| 1760 |
_TIFFfreeExt(tif, sp->dctable[o]); |
| 1761 |
sp->dctable[o] = nb; |
| 1762 |
} |
| 1763 |
else |
| 1764 |
{ |
| 1765 |
if ((o & 240) != 16) |
| 1766 |
{ |
| 1767 |
TIFFErrorExtR(tif, module, "Corrupt DHT marker in JPEG data"); |
| 1768 |
_TIFFfreeExt(tif, nb); |
| 1769 |
return (0); |
| 1770 |
} |
| 1771 |
o &= 15; |
| 1772 |
if (3 < o) |
| 1773 |
{ |
| 1774 |
TIFFErrorExtR(tif, module, "Corrupt DHT marker in JPEG data"); |
| 1775 |
_TIFFfreeExt(tif, nb); |
| 1776 |
return (0); |
| 1777 |
} |
| 1778 |
if (sp->actable[o] != 0) |
| 1779 |
_TIFFfreeExt(tif, sp->actable[o]); |
| 1780 |
sp->actable[o] = nb; |
| 1781 |
} |
| 1782 |
} |
| 1783 |
return (1); |
| 1784 |
} |
| 1785 |
|
| 1786 |
static int OJPEGReadHeaderInfoSecStreamSof(TIFF *tif, uint8_t marker_id) |
| 1787 |
{ |
| 1788 |
/* this marker needs to be checked, and part of its data needs to be saved |
| 1789 |
* for regeneration later on */ |
| 1790 |
static const char module[] = "OJPEGReadHeaderInfoSecStreamSof"; |
| 1791 |
OJPEGState *sp = (OJPEGState *)tif->tif_data; |
| 1792 |
uint16_t m; |
| 1793 |
uint16_t n; |
| 1794 |
uint8_t o; |
| 1795 |
uint16_t p; |
| 1796 |
uint16_t q; |
| 1797 |
if (sp->sof_log != 0) |
| 1798 |
{ |
| 1799 |
TIFFErrorExtR(tif, module, "Corrupt JPEG data"); |
| 1800 |
return (0); |
| 1801 |
} |
| 1802 |
if (sp->subsamplingcorrect == 0) |
| 1803 |
sp->sof_marker_id = marker_id; |
| 1804 |
/* Lf: data length */ |
| 1805 |
if (OJPEGReadWord(sp, &m) == 0) |
| 1806 |
return (0); |
| 1807 |
if (m < 11) |
| 1808 |
{ |
| 1809 |
if (sp->subsamplingcorrect == 0) |
| 1810 |
TIFFErrorExtR(tif, module, "Corrupt SOF marker in JPEG data"); |
| 1811 |
return (0); |
| 1812 |
} |
| 1813 |
m -= 8; |
| 1814 |
if (m % 3 != 0) |
| 1815 |
{ |
| 1816 |
if (sp->subsamplingcorrect == 0) |
| 1817 |
TIFFErrorExtR(tif, module, "Corrupt SOF marker in JPEG data"); |
| 1818 |
return (0); |
| 1819 |
} |
| 1820 |
n = m / 3; |
| 1821 |
if (sp->subsamplingcorrect == 0) |
| 1822 |
{ |
| 1823 |
if (n != sp->samples_per_pixel) |
| 1824 |
{ |
| 1825 |
TIFFErrorExtR( |
| 1826 |
tif, module, |
| 1827 |
"JPEG compressed data indicates unexpected number of samples"); |
| 1828 |
return (0); |
| 1829 |
} |
| 1830 |
} |
| 1831 |
/* P: Sample precision */ |
| 1832 |
if (OJPEGReadByte(sp, &o) == 0) |
| 1833 |
return (0); |
| 1834 |
if (o != 8) |
| 1835 |
{ |
| 1836 |
if (sp->subsamplingcorrect == 0) |
| 1837 |
TIFFErrorExtR(tif, module, |
| 1838 |
"JPEG compressed data indicates unexpected number of " |
| 1839 |
"bits per sample"); |
| 1840 |
return (0); |
| 1841 |
} |
| 1842 |
/* Y: Number of lines, X: Number of samples per line */ |
| 1843 |
if (sp->subsamplingcorrect) |
| 1844 |
OJPEGReadSkip(sp, 4); |
| 1845 |
else |
| 1846 |
{ |
| 1847 |
/* Y: Number of lines */ |
| 1848 |
if (OJPEGReadWord(sp, &p) == 0) |
| 1849 |
return (0); |
| 1850 |
if (((uint32_t)p < sp->image_length) && |
| 1851 |
((uint32_t)p < sp->strile_length_total)) |
| 1852 |
{ |
| 1853 |
TIFFErrorExtR(tif, module, |
| 1854 |
"JPEG compressed data indicates unexpected height"); |
| 1855 |
return (0); |
| 1856 |
} |
| 1857 |
sp->sof_y = p; |
| 1858 |
/* X: Number of samples per line */ |
| 1859 |
if (OJPEGReadWord(sp, &p) == 0) |
| 1860 |
return (0); |
| 1861 |
if (((uint32_t)p < sp->image_width) && ((uint32_t)p < sp->strile_width)) |
| 1862 |
{ |
| 1863 |
TIFFErrorExtR(tif, module, |
| 1864 |
"JPEG compressed data indicates unexpected width"); |
| 1865 |
return (0); |
| 1866 |
} |
| 1867 |
if ((uint32_t)p > sp->strile_width) |
| 1868 |
{ |
| 1869 |
TIFFErrorExtR(tif, module, |
| 1870 |
"JPEG compressed data image width exceeds expected " |
| 1871 |
"image width"); |
| 1872 |
return (0); |
| 1873 |
} |
| 1874 |
sp->sof_x = p; |
| 1875 |
} |
| 1876 |
/* Nf: Number of image components in frame */ |
| 1877 |
if (OJPEGReadByte(sp, &o) == 0) |
| 1878 |
return (0); |
| 1879 |
if (o != n) |
| 1880 |
{ |
| 1881 |
if (sp->subsamplingcorrect == 0) |
| 1882 |
TIFFErrorExtR(tif, module, "Corrupt SOF marker in JPEG data"); |
| 1883 |
return (0); |
| 1884 |
} |
| 1885 |
/* per component stuff */ |
| 1886 |
/* TODO: double-check that flow implies that n cannot be as big as to make |
| 1887 |
* us overflow sof_c, sof_hv and sof_tq arrays */ |
| 1888 |
for (q = 0; q < n; q++) |
| 1889 |
{ |
| 1890 |
/* C: Component identifier */ |
| 1891 |
if (OJPEGReadByte(sp, &o) == 0) |
| 1892 |
return (0); |
| 1893 |
if (sp->subsamplingcorrect == 0) |
| 1894 |
sp->sof_c[q] = o; |
| 1895 |
/* H: Horizontal sampling factor, and V: Vertical sampling factor */ |
| 1896 |
if (OJPEGReadByte(sp, &o) == 0) |
| 1897 |
return (0); |
| 1898 |
if (sp->subsamplingcorrect != 0) |
| 1899 |
{ |
| 1900 |
if (q == 0) |
| 1901 |
{ |
| 1902 |
sp->subsampling_hor = (o >> 4); |
| 1903 |
sp->subsampling_ver = (o & 15); |
| 1904 |
if (((sp->subsampling_hor != 1) && (sp->subsampling_hor != 2) && |
| 1905 |
(sp->subsampling_hor != 4)) || |
| 1906 |
((sp->subsampling_ver != 1) && (sp->subsampling_ver != 2) && |
| 1907 |
(sp->subsampling_ver != 4))) |
| 1908 |
sp->subsampling_force_desubsampling_inside_decompression = |
| 1909 |
1; |
| 1910 |
} |
| 1911 |
else |
| 1912 |
{ |
| 1913 |
if (o != 17) |
| 1914 |
sp->subsampling_force_desubsampling_inside_decompression = |
| 1915 |
1; |
| 1916 |
} |
| 1917 |
} |
| 1918 |
else |
| 1919 |
{ |
| 1920 |
sp->sof_hv[q] = o; |
| 1921 |
if (sp->subsampling_force_desubsampling_inside_decompression == 0) |
| 1922 |
{ |
| 1923 |
if (q == 0) |
| 1924 |
{ |
| 1925 |
if (o != ((sp->subsampling_hor << 4) | sp->subsampling_ver)) |
| 1926 |
{ |
| 1927 |
TIFFErrorExtR(tif, module, |
| 1928 |
"JPEG compressed data indicates " |
| 1929 |
"unexpected subsampling values"); |
| 1930 |
return (0); |
| 1931 |
} |
| 1932 |
} |
| 1933 |
else |
| 1934 |
{ |
| 1935 |
if (o != 17) |
| 1936 |
{ |
| 1937 |
TIFFErrorExtR(tif, module, |
| 1938 |
"JPEG compressed data indicates " |
| 1939 |
"unexpected subsampling values"); |
| 1940 |
return (0); |
| 1941 |
} |
| 1942 |
} |
| 1943 |
} |
| 1944 |
} |
| 1945 |
/* Tq: Quantization table destination selector */ |
| 1946 |
if (OJPEGReadByte(sp, &o) == 0) |
| 1947 |
return (0); |
| 1948 |
if (sp->subsamplingcorrect == 0) |
| 1949 |
sp->sof_tq[q] = o; |
| 1950 |
} |
| 1951 |
if (sp->subsamplingcorrect == 0) |
| 1952 |
sp->sof_log = 1; |
| 1953 |
return (1); |
| 1954 |
} |
| 1955 |
|
| 1956 |
static int OJPEGReadHeaderInfoSecStreamSos(TIFF *tif) |
| 1957 |
{ |
| 1958 |
/* this marker needs to be checked, and part of its data needs to be saved |
| 1959 |
* for regeneration later on */ |
| 1960 |
static const char module[] = "OJPEGReadHeaderInfoSecStreamSos"; |
| 1961 |
OJPEGState *sp = (OJPEGState *)tif->tif_data; |
| 1962 |
uint16_t m; |
| 1963 |
uint8_t n; |
| 1964 |
uint8_t o; |
| 1965 |
assert(sp->subsamplingcorrect == 0); |
| 1966 |
if (sp->sof_log == 0) |
| 1967 |
{ |
| 1968 |
TIFFErrorExtR(tif, module, "Corrupt SOS marker in JPEG data"); |
| 1969 |
return (0); |
| 1970 |
} |
| 1971 |
/* Ls */ |
| 1972 |
if (OJPEGReadWord(sp, &m) == 0) |
| 1973 |
return (0); |
| 1974 |
if (m != 6 + sp->samples_per_pixel_per_plane * 2) |
| 1975 |
{ |
| 1976 |
TIFFErrorExtR(tif, module, "Corrupt SOS marker in JPEG data"); |
| 1977 |
return (0); |
| 1978 |
} |
| 1979 |
/* Ns */ |
| 1980 |
if (OJPEGReadByte(sp, &n) == 0) |
| 1981 |
return (0); |
| 1982 |
if (n != sp->samples_per_pixel_per_plane) |
| 1983 |
{ |
| 1984 |
TIFFErrorExtR(tif, module, "Corrupt SOS marker in JPEG data"); |
| 1985 |
return (0); |
| 1986 |
} |
| 1987 |
/* Cs, Td, and Ta */ |
| 1988 |
for (o = 0; o < sp->samples_per_pixel_per_plane; o++) |
| 1989 |
{ |
| 1990 |
/* Cs */ |
| 1991 |
if (OJPEGReadByte(sp, &n) == 0) |
| 1992 |
return (0); |
| 1993 |
sp->sos_cs[sp->plane_sample_offset + o] = n; |
| 1994 |
/* Td and Ta */ |
| 1995 |
if (OJPEGReadByte(sp, &n) == 0) |
| 1996 |
return (0); |
| 1997 |
sp->sos_tda[sp->plane_sample_offset + o] = n; |
| 1998 |
} |
| 1999 |
/* skip Ss, Se, Ah, en Al -> no check, as per Tom Lane recommendation, as |
| 2000 |
* per LibJpeg source */ |
| 2001 |
OJPEGReadSkip(sp, 3); |
| 2002 |
return (1); |
| 2003 |
} |
| 2004 |
|
| 2005 |
static int OJPEGReadHeaderInfoSecTablesQTable(TIFF *tif) |
| 2006 |
{ |
| 2007 |
static const char module[] = "OJPEGReadHeaderInfoSecTablesQTable"; |
| 2008 |
OJPEGState *sp = (OJPEGState *)tif->tif_data; |
| 2009 |
uint8_t m; |
| 2010 |
uint8_t n; |
| 2011 |
uint32_t oa; |
| 2012 |
uint8_t *ob; |
| 2013 |
uint32_t p; |
| 2014 |
if (sp->qtable_offset[0] == 0) |
| 2015 |
{ |
| 2016 |
TIFFErrorExtR(tif, module, "Missing JPEG tables"); |
| 2017 |
return (0); |
| 2018 |
} |
| 2019 |
sp->in_buffer_file_pos_log = 0; |
| 2020 |
for (m = 0; m < sp->samples_per_pixel; m++) |
| 2021 |
{ |
| 2022 |
if ((sp->qtable_offset[m] != 0) && |
| 2023 |
((m == 0) || (sp->qtable_offset[m] != sp->qtable_offset[m - 1]))) |
| 2024 |
{ |
| 2025 |
for (n = 0; n < m - 1; n++) |
| 2026 |
{ |
| 2027 |
if (sp->qtable_offset[m] == sp->qtable_offset[n]) |
| 2028 |
{ |
| 2029 |
TIFFErrorExtR(tif, module, "Corrupt JpegQTables tag value"); |
| 2030 |
return (0); |
| 2031 |
} |
| 2032 |
} |
| 2033 |
oa = sizeof(uint32_t) + 69; |
| 2034 |
ob = _TIFFmallocExt(tif, oa); |
| 2035 |
if (ob == 0) |
| 2036 |
{ |
| 2037 |
TIFFErrorExtR(tif, module, "Out of memory"); |
| 2038 |
return (0); |
| 2039 |
} |
| 2040 |
*(uint32_t *)ob = oa; |
| 2041 |
ob[sizeof(uint32_t)] = 255; |
| 2042 |
ob[sizeof(uint32_t) + 1] = JPEG_MARKER_DQT; |
| 2043 |
ob[sizeof(uint32_t) + 2] = 0; |
| 2044 |
ob[sizeof(uint32_t) + 3] = 67; |
| 2045 |
ob[sizeof(uint32_t) + 4] = m; |
| 2046 |
TIFFSeekFile(tif, sp->qtable_offset[m], SEEK_SET); |
| 2047 |
p = (uint32_t)TIFFReadFile(tif, &ob[sizeof(uint32_t) + 5], 64); |
| 2048 |
if (p != 64) |
| 2049 |
{ |
| 2050 |
_TIFFfreeExt(tif, ob); |
| 2051 |
return (0); |
| 2052 |
} |
| 2053 |
if (sp->qtable[m] != 0) |
| 2054 |
_TIFFfreeExt(tif, sp->qtable[m]); |
| 2055 |
sp->qtable[m] = ob; |
| 2056 |
sp->sof_tq[m] = m; |
| 2057 |
} |
| 2058 |
else |
| 2059 |
sp->sof_tq[m] = sp->sof_tq[m - 1]; |
| 2060 |
} |
| 2061 |
return (1); |
| 2062 |
} |
| 2063 |
|
| 2064 |
static int OJPEGReadHeaderInfoSecTablesDcTable(TIFF *tif) |
| 2065 |
{ |
| 2066 |
static const char module[] = "OJPEGReadHeaderInfoSecTablesDcTable"; |
| 2067 |
OJPEGState *sp = (OJPEGState *)tif->tif_data; |
| 2068 |
uint8_t m; |
| 2069 |
uint8_t n; |
| 2070 |
uint8_t o[16]; |
| 2071 |
uint32_t p; |
| 2072 |
uint32_t q; |
| 2073 |
uint32_t ra; |
| 2074 |
uint8_t *rb; |
| 2075 |
if (sp->dctable_offset[0] == 0) |
| 2076 |
{ |
| 2077 |
TIFFErrorExtR(tif, module, "Missing JPEG tables"); |
| 2078 |
return (0); |
| 2079 |
} |
| 2080 |
sp->in_buffer_file_pos_log = 0; |
| 2081 |
for (m = 0; m < sp->samples_per_pixel; m++) |
| 2082 |
{ |
| 2083 |
if ((sp->dctable_offset[m] != 0) && |
| 2084 |
((m == 0) || (sp->dctable_offset[m] != sp->dctable_offset[m - 1]))) |
| 2085 |
{ |
| 2086 |
for (n = 0; n < m - 1; n++) |
| 2087 |
{ |
| 2088 |
if (sp->dctable_offset[m] == sp->dctable_offset[n]) |
| 2089 |
{ |
| 2090 |
TIFFErrorExtR(tif, module, |
| 2091 |
"Corrupt JpegDcTables tag value"); |
| 2092 |
return (0); |
| 2093 |
} |
| 2094 |
} |
| 2095 |
TIFFSeekFile(tif, sp->dctable_offset[m], SEEK_SET); |
| 2096 |
p = (uint32_t)TIFFReadFile(tif, o, 16); |
| 2097 |
if (p != 16) |
| 2098 |
return (0); |
| 2099 |
q = 0; |
| 2100 |
for (n = 0; n < 16; n++) |
| 2101 |
q += o[n]; |
| 2102 |
ra = sizeof(uint32_t) + 21 + q; |
| 2103 |
rb = _TIFFmallocExt(tif, ra); |
| 2104 |
if (rb == 0) |
| 2105 |
{ |
| 2106 |
TIFFErrorExtR(tif, module, "Out of memory"); |
| 2107 |
return (0); |
| 2108 |
} |
| 2109 |
*(uint32_t *)rb = ra; |
| 2110 |
rb[sizeof(uint32_t)] = 255; |
| 2111 |
rb[sizeof(uint32_t) + 1] = JPEG_MARKER_DHT; |
| 2112 |
rb[sizeof(uint32_t) + 2] = (uint8_t)((19 + q) >> 8); |
| 2113 |
rb[sizeof(uint32_t) + 3] = ((19 + q) & 255); |
| 2114 |
rb[sizeof(uint32_t) + 4] = m; |
| 2115 |
for (n = 0; n < 16; n++) |
| 2116 |
rb[sizeof(uint32_t) + 5 + n] = o[n]; |
| 2117 |
p = (uint32_t)TIFFReadFile(tif, &(rb[sizeof(uint32_t) + 21]), q); |
| 2118 |
if (p != q) |
| 2119 |
{ |
| 2120 |
_TIFFfreeExt(tif, rb); |
| 2121 |
return (0); |
| 2122 |
} |
| 2123 |
if (sp->dctable[m] != 0) |
| 2124 |
_TIFFfreeExt(tif, sp->dctable[m]); |
| 2125 |
sp->dctable[m] = rb; |
| 2126 |
sp->sos_tda[m] = (m << 4); |
| 2127 |
} |
| 2128 |
else |
| 2129 |
sp->sos_tda[m] = sp->sos_tda[m - 1]; |
| 2130 |
} |
| 2131 |
return (1); |
| 2132 |
} |
| 2133 |
|
| 2134 |
static int OJPEGReadHeaderInfoSecTablesAcTable(TIFF *tif) |
| 2135 |
{ |
| 2136 |
static const char module[] = "OJPEGReadHeaderInfoSecTablesAcTable"; |
| 2137 |
OJPEGState *sp = (OJPEGState *)tif->tif_data; |
| 2138 |
uint8_t m; |
| 2139 |
uint8_t n; |
| 2140 |
uint8_t o[16]; |
| 2141 |
uint32_t p; |
| 2142 |
uint32_t q; |
| 2143 |
uint32_t ra; |
| 2144 |
uint8_t *rb; |
| 2145 |
if (sp->actable_offset[0] == 0) |
| 2146 |
{ |
| 2147 |
TIFFErrorExtR(tif, module, "Missing JPEG tables"); |
| 2148 |
return (0); |
| 2149 |
} |
| 2150 |
sp->in_buffer_file_pos_log = 0; |
| 2151 |
for (m = 0; m < sp->samples_per_pixel; m++) |
| 2152 |
{ |
| 2153 |
if ((sp->actable_offset[m] != 0) && |
| 2154 |
((m == 0) || (sp->actable_offset[m] != sp->actable_offset[m - 1]))) |
| 2155 |
{ |
| 2156 |
for (n = 0; n < m - 1; n++) |
| 2157 |
{ |
| 2158 |
if (sp->actable_offset[m] == sp->actable_offset[n]) |
| 2159 |
{ |
| 2160 |
TIFFErrorExtR(tif, module, |
| 2161 |
"Corrupt JpegAcTables tag value"); |
| 2162 |
return (0); |
| 2163 |
} |
| 2164 |
} |
| 2165 |
TIFFSeekFile(tif, sp->actable_offset[m], SEEK_SET); |
| 2166 |
p = (uint32_t)TIFFReadFile(tif, o, 16); |
| 2167 |
if (p != 16) |
| 2168 |
return (0); |
| 2169 |
q = 0; |
| 2170 |
for (n = 0; n < 16; n++) |
| 2171 |
q += o[n]; |
| 2172 |
ra = sizeof(uint32_t) + 21 + q; |
| 2173 |
rb = _TIFFmallocExt(tif, ra); |
| 2174 |
if (rb == 0) |
| 2175 |
{ |
| 2176 |
TIFFErrorExtR(tif, module, "Out of memory"); |
| 2177 |
return (0); |
| 2178 |
} |
| 2179 |
*(uint32_t *)rb = ra; |
| 2180 |
rb[sizeof(uint32_t)] = 255; |
| 2181 |
rb[sizeof(uint32_t) + 1] = JPEG_MARKER_DHT; |
| 2182 |
rb[sizeof(uint32_t) + 2] = (uint8_t)((19 + q) >> 8); |
| 2183 |
rb[sizeof(uint32_t) + 3] = ((19 + q) & 255); |
| 2184 |
rb[sizeof(uint32_t) + 4] = (16 | m); |
| 2185 |
for (n = 0; n < 16; n++) |
| 2186 |
rb[sizeof(uint32_t) + 5 + n] = o[n]; |
| 2187 |
p = (uint32_t)TIFFReadFile(tif, &(rb[sizeof(uint32_t) + 21]), q); |
| 2188 |
if (p != q) |
| 2189 |
{ |
| 2190 |
_TIFFfreeExt(tif, rb); |
| 2191 |
return (0); |
| 2192 |
} |
| 2193 |
if (sp->actable[m] != 0) |
| 2194 |
_TIFFfreeExt(tif, sp->actable[m]); |
| 2195 |
sp->actable[m] = rb; |
| 2196 |
sp->sos_tda[m] = (sp->sos_tda[m] | m); |
| 2197 |
} |
| 2198 |
else |
| 2199 |
sp->sos_tda[m] = (sp->sos_tda[m] | (sp->sos_tda[m - 1] & 15)); |
| 2200 |
} |
| 2201 |
return (1); |
| 2202 |
} |
| 2203 |
|
| 2204 |
static int OJPEGReadBufferFill(OJPEGState *sp) |
| 2205 |
{ |
| 2206 |
uint16_t m; |
| 2207 |
tmsize_t n; |
| 2208 |
/* TODO: double-check: when subsamplingcorrect is set, no call to |
| 2209 |
* TIFFErrorExt or TIFFWarningExt should be made in any other case, seek or |
| 2210 |
* read errors should be passed through */ |
| 2211 |
do |
| 2212 |
{ |
| 2213 |
if (sp->in_buffer_file_togo != 0) |
| 2214 |
{ |
| 2215 |
if (sp->in_buffer_file_pos_log == 0) |
| 2216 |
{ |
| 2217 |
TIFFSeekFile(sp->tif, sp->in_buffer_file_pos, SEEK_SET); |
| 2218 |
sp->in_buffer_file_pos_log = 1; |
| 2219 |
} |
| 2220 |
m = OJPEG_BUFFER; |
| 2221 |
if ((uint64_t)m > sp->in_buffer_file_togo) |
| 2222 |
m = (uint16_t)sp->in_buffer_file_togo; |
| 2223 |
n = TIFFReadFile(sp->tif, sp->in_buffer, (tmsize_t)m); |
| 2224 |
if (n == 0) |
| 2225 |
return (0); |
| 2226 |
assert(n > 0); |
| 2227 |
assert(n <= OJPEG_BUFFER); |
| 2228 |
assert(n < 65536); |
| 2229 |
assert((uint64_t)n <= sp->in_buffer_file_togo); |
| 2230 |
m = (uint16_t)n; |
| 2231 |
sp->in_buffer_togo = m; |
| 2232 |
sp->in_buffer_cur = sp->in_buffer; |
| 2233 |
sp->in_buffer_file_togo -= m; |
| 2234 |
sp->in_buffer_file_pos += m; |
| 2235 |
break; |
| 2236 |
} |
| 2237 |
sp->in_buffer_file_pos_log = 0; |
| 2238 |
switch (sp->in_buffer_source) |
| 2239 |
{ |
| 2240 |
case osibsNotSetYet: |
| 2241 |
if (sp->jpeg_interchange_format != 0) |
| 2242 |
{ |
| 2243 |
sp->in_buffer_file_pos = sp->jpeg_interchange_format; |
| 2244 |
sp->in_buffer_file_togo = |
| 2245 |
sp->jpeg_interchange_format_length; |
| 2246 |
} |
| 2247 |
sp->in_buffer_source = osibsJpegInterchangeFormat; |
| 2248 |
break; |
| 2249 |
case osibsJpegInterchangeFormat: |
| 2250 |
sp->in_buffer_source = osibsStrile; |
| 2251 |
break; |
| 2252 |
case osibsStrile: |
| 2253 |
if (sp->in_buffer_next_strile == sp->in_buffer_strile_count) |
| 2254 |
sp->in_buffer_source = osibsEof; |
| 2255 |
else |
| 2256 |
{ |
| 2257 |
int err = 0; |
| 2258 |
sp->in_buffer_file_pos = TIFFGetStrileOffsetWithErr( |
| 2259 |
sp->tif, sp->in_buffer_next_strile, &err); |
| 2260 |
if (err) |
| 2261 |
return 0; |
| 2262 |
if (sp->in_buffer_file_pos != 0) |
| 2263 |
{ |
| 2264 |
uint64_t bytecount = TIFFGetStrileByteCountWithErr( |
| 2265 |
sp->tif, sp->in_buffer_next_strile, &err); |
| 2266 |
if (err) |
| 2267 |
return 0; |
| 2268 |
if (sp->in_buffer_file_pos >= sp->file_size) |
| 2269 |
sp->in_buffer_file_pos = 0; |
| 2270 |
else if (bytecount == 0) |
| 2271 |
sp->in_buffer_file_togo = |
| 2272 |
sp->file_size - sp->in_buffer_file_pos; |
| 2273 |
else |
| 2274 |
{ |
| 2275 |
sp->in_buffer_file_togo = bytecount; |
| 2276 |
if (sp->in_buffer_file_togo == 0) |
| 2277 |
sp->in_buffer_file_pos = 0; |
| 2278 |
else if (sp->in_buffer_file_pos > |
| 2279 |
UINT64_MAX - sp->in_buffer_file_togo || |
| 2280 |
sp->in_buffer_file_pos + |
| 2281 |
sp->in_buffer_file_togo > |
| 2282 |
sp->file_size) |
| 2283 |
sp->in_buffer_file_togo = |
| 2284 |
sp->file_size - sp->in_buffer_file_pos; |
| 2285 |
} |
| 2286 |
} |
| 2287 |
sp->in_buffer_next_strile++; |
| 2288 |
} |
| 2289 |
break; |
| 2290 |
default: |
| 2291 |
return (0); |
| 2292 |
} |
| 2293 |
} while (1); |
| 2294 |
return (1); |
| 2295 |
} |
| 2296 |
|
| 2297 |
static int OJPEGReadByte(OJPEGState *sp, uint8_t *byte) |
| 2298 |
{ |
| 2299 |
if (sp->in_buffer_togo == 0) |
| 2300 |
{ |
| 2301 |
if (OJPEGReadBufferFill(sp) == 0) |
| 2302 |
return (0); |
| 2303 |
assert(sp->in_buffer_togo > 0); |
| 2304 |
} |
| 2305 |
*byte = *(sp->in_buffer_cur); |
| 2306 |
sp->in_buffer_cur++; |
| 2307 |
sp->in_buffer_togo--; |
| 2308 |
return (1); |
| 2309 |
} |
| 2310 |
|
| 2311 |
static int OJPEGReadBytePeek(OJPEGState *sp, uint8_t *byte) |
| 2312 |
{ |
| 2313 |
if (sp->in_buffer_togo == 0) |
| 2314 |
{ |
| 2315 |
if (OJPEGReadBufferFill(sp) == 0) |
| 2316 |
return (0); |
| 2317 |
assert(sp->in_buffer_togo > 0); |
| 2318 |
} |
| 2319 |
*byte = *(sp->in_buffer_cur); |
| 2320 |
return (1); |
| 2321 |
} |
| 2322 |
|
| 2323 |
static void OJPEGReadByteAdvance(OJPEGState *sp) |
| 2324 |
{ |
| 2325 |
assert(sp->in_buffer_togo > 0); |
| 2326 |
sp->in_buffer_cur++; |
| 2327 |
sp->in_buffer_togo--; |
| 2328 |
} |
| 2329 |
|
| 2330 |
static int OJPEGReadWord(OJPEGState *sp, uint16_t *word) |
| 2331 |
{ |
| 2332 |
uint8_t m; |
| 2333 |
if (OJPEGReadByte(sp, &m) == 0) |
| 2334 |
return (0); |
| 2335 |
*word = (m << 8); |
| 2336 |
if (OJPEGReadByte(sp, &m) == 0) |
| 2337 |
return (0); |
| 2338 |
*word |= m; |
| 2339 |
return (1); |
| 2340 |
} |
| 2341 |
|
| 2342 |
static int OJPEGReadBlock(OJPEGState *sp, uint16_t len, void *mem) |
| 2343 |
{ |
| 2344 |
uint16_t mlen; |
| 2345 |
uint8_t *mmem; |
| 2346 |
uint16_t n; |
| 2347 |
assert(len > 0); |
| 2348 |
mlen = len; |
| 2349 |
mmem = mem; |
| 2350 |
do |
| 2351 |
{ |
| 2352 |
if (sp->in_buffer_togo == 0) |
| 2353 |
{ |
| 2354 |
if (OJPEGReadBufferFill(sp) == 0) |
| 2355 |
return (0); |
| 2356 |
assert(sp->in_buffer_togo > 0); |
| 2357 |
} |
| 2358 |
n = mlen; |
| 2359 |
if (n > sp->in_buffer_togo) |
| 2360 |
n = sp->in_buffer_togo; |
| 2361 |
_TIFFmemcpy(mmem, sp->in_buffer_cur, n); |
| 2362 |
sp->in_buffer_cur += n; |
| 2363 |
sp->in_buffer_togo -= n; |
| 2364 |
mlen -= n; |
| 2365 |
mmem += n; |
| 2366 |
} while (mlen > 0); |
| 2367 |
return (1); |
| 2368 |
} |
| 2369 |
|
| 2370 |
static void OJPEGReadSkip(OJPEGState *sp, uint16_t len) |
| 2371 |
{ |
| 2372 |
uint16_t m; |
| 2373 |
uint16_t n; |
| 2374 |
m = len; |
| 2375 |
n = m; |
| 2376 |
if (n > sp->in_buffer_togo) |
| 2377 |
n = sp->in_buffer_togo; |
| 2378 |
sp->in_buffer_cur += n; |
| 2379 |
sp->in_buffer_togo -= n; |
| 2380 |
m -= n; |
| 2381 |
if (m > 0) |
| 2382 |
{ |
| 2383 |
assert(sp->in_buffer_togo == 0); |
| 2384 |
n = m; |
| 2385 |
if ((uint64_t)n > sp->in_buffer_file_togo) |
| 2386 |
n = (uint16_t)sp->in_buffer_file_togo; |
| 2387 |
sp->in_buffer_file_pos += n; |
| 2388 |
sp->in_buffer_file_togo -= n; |
| 2389 |
sp->in_buffer_file_pos_log = 0; |
| 2390 |
/* we don't skip past jpeginterchangeformat/strile block... |
| 2391 |
* if that is asked from us, we're dealing with totally bazurk |
| 2392 |
* data anyway, and we've not seen this happening on any |
| 2393 |
* testfile, so we might as well likely cause some other |
| 2394 |
* meaningless error to be passed at some later time |
| 2395 |
*/ |
| 2396 |
} |
| 2397 |
} |
| 2398 |
|
| 2399 |
static int OJPEGWriteStream(TIFF *tif, void **mem, uint32_t *len) |
| 2400 |
{ |
| 2401 |
OJPEGState *sp = (OJPEGState *)tif->tif_data; |
| 2402 |
*len = 0; |
| 2403 |
do |
| 2404 |
{ |
| 2405 |
assert(sp->out_state <= ososEoi); |
| 2406 |
switch (sp->out_state) |
| 2407 |
{ |
| 2408 |
case ososSoi: |
| 2409 |
OJPEGWriteStreamSoi(tif, mem, len); |
| 2410 |
break; |
| 2411 |
case ososQTable0: |
| 2412 |
OJPEGWriteStreamQTable(tif, 0, mem, len); |
| 2413 |
break; |
| 2414 |
case ososQTable1: |
| 2415 |
OJPEGWriteStreamQTable(tif, 1, mem, len); |
| 2416 |
break; |
| 2417 |
case ososQTable2: |
| 2418 |
OJPEGWriteStreamQTable(tif, 2, mem, len); |
| 2419 |
break; |
| 2420 |
case ososQTable3: |
| 2421 |
OJPEGWriteStreamQTable(tif, 3, mem, len); |
| 2422 |
break; |
| 2423 |
case ososDcTable0: |
| 2424 |
OJPEGWriteStreamDcTable(tif, 0, mem, len); |
| 2425 |
break; |
| 2426 |
case ososDcTable1: |
| 2427 |
OJPEGWriteStreamDcTable(tif, 1, mem, len); |
| 2428 |
break; |
| 2429 |
case ososDcTable2: |
| 2430 |
OJPEGWriteStreamDcTable(tif, 2, mem, len); |
| 2431 |
break; |
| 2432 |
case ososDcTable3: |
| 2433 |
OJPEGWriteStreamDcTable(tif, 3, mem, len); |
| 2434 |
break; |
| 2435 |
case ososAcTable0: |
| 2436 |
OJPEGWriteStreamAcTable(tif, 0, mem, len); |
| 2437 |
break; |
| 2438 |
case ososAcTable1: |
| 2439 |
OJPEGWriteStreamAcTable(tif, 1, mem, len); |
| 2440 |
break; |
| 2441 |
case ososAcTable2: |
| 2442 |
OJPEGWriteStreamAcTable(tif, 2, mem, len); |
| 2443 |
break; |
| 2444 |
case ososAcTable3: |
| 2445 |
OJPEGWriteStreamAcTable(tif, 3, mem, len); |
| 2446 |
break; |
| 2447 |
case ososDri: |
| 2448 |
OJPEGWriteStreamDri(tif, mem, len); |
| 2449 |
break; |
| 2450 |
case ososSof: |
| 2451 |
OJPEGWriteStreamSof(tif, mem, len); |
| 2452 |
break; |
| 2453 |
case ososSos: |
| 2454 |
OJPEGWriteStreamSos(tif, mem, len); |
| 2455 |
break; |
| 2456 |
case ososCompressed: |
| 2457 |
if (OJPEGWriteStreamCompressed(tif, mem, len) == 0) |
| 2458 |
return (0); |
| 2459 |
break; |
| 2460 |
case ososRst: |
| 2461 |
OJPEGWriteStreamRst(tif, mem, len); |
| 2462 |
break; |
| 2463 |
case ososEoi: |
| 2464 |
OJPEGWriteStreamEoi(tif, mem, len); |
| 2465 |
break; |
| 2466 |
} |
| 2467 |
} while (*len == 0); |
| 2468 |
return (1); |
| 2469 |
} |
| 2470 |
|
| 2471 |
static void OJPEGWriteStreamSoi(TIFF *tif, void **mem, uint32_t *len) |
| 2472 |
{ |
| 2473 |
OJPEGState *sp = (OJPEGState *)tif->tif_data; |
| 2474 |
assert(OJPEG_BUFFER >= 2); |
| 2475 |
sp->out_buffer[0] = 255; |
| 2476 |
sp->out_buffer[1] = JPEG_MARKER_SOI; |
| 2477 |
*len = 2; |
| 2478 |
*mem = (void *)sp->out_buffer; |
| 2479 |
sp->out_state++; |
| 2480 |
} |
| 2481 |
|
| 2482 |
static void OJPEGWriteStreamQTable(TIFF *tif, uint8_t table_index, void **mem, |
| 2483 |
uint32_t *len) |
| 2484 |
{ |
| 2485 |
OJPEGState *sp = (OJPEGState *)tif->tif_data; |
| 2486 |
if (sp->qtable[table_index] != 0) |
| 2487 |
{ |
| 2488 |
*mem = (void *)(sp->qtable[table_index] + sizeof(uint32_t)); |
| 2489 |
*len = *((uint32_t *)sp->qtable[table_index]) - sizeof(uint32_t); |
| 2490 |
} |
| 2491 |
sp->out_state++; |
| 2492 |
} |
| 2493 |
|
| 2494 |
static void OJPEGWriteStreamDcTable(TIFF *tif, uint8_t table_index, void **mem, |
| 2495 |
uint32_t *len) |
| 2496 |
{ |
| 2497 |
OJPEGState *sp = (OJPEGState *)tif->tif_data; |
| 2498 |
if (sp->dctable[table_index] != 0) |
| 2499 |
{ |
| 2500 |
*mem = (void *)(sp->dctable[table_index] + sizeof(uint32_t)); |
| 2501 |
*len = *((uint32_t *)sp->dctable[table_index]) - sizeof(uint32_t); |
| 2502 |
} |
| 2503 |
sp->out_state++; |
| 2504 |
} |
| 2505 |
|
| 2506 |
static void OJPEGWriteStreamAcTable(TIFF *tif, uint8_t table_index, void **mem, |
| 2507 |
uint32_t *len) |
| 2508 |
{ |
| 2509 |
OJPEGState *sp = (OJPEGState *)tif->tif_data; |
| 2510 |
if (sp->actable[table_index] != 0) |
| 2511 |
{ |
| 2512 |
*mem = (void *)(sp->actable[table_index] + sizeof(uint32_t)); |
| 2513 |
*len = *((uint32_t *)sp->actable[table_index]) - sizeof(uint32_t); |
| 2514 |
} |
| 2515 |
sp->out_state++; |
| 2516 |
} |
| 2517 |
|
| 2518 |
static void OJPEGWriteStreamDri(TIFF *tif, void **mem, uint32_t *len) |
| 2519 |
{ |
| 2520 |
OJPEGState *sp = (OJPEGState *)tif->tif_data; |
| 2521 |
assert(OJPEG_BUFFER >= 6); |
| 2522 |
if (sp->restart_interval != 0) |
| 2523 |
{ |
| 2524 |
sp->out_buffer[0] = 255; |
| 2525 |
sp->out_buffer[1] = JPEG_MARKER_DRI; |
| 2526 |
sp->out_buffer[2] = 0; |
| 2527 |
sp->out_buffer[3] = 4; |
| 2528 |
sp->out_buffer[4] = (sp->restart_interval >> 8); |
| 2529 |
sp->out_buffer[5] = (sp->restart_interval & 255); |
| 2530 |
*len = 6; |
| 2531 |
*mem = (void *)sp->out_buffer; |
| 2532 |
} |
| 2533 |
sp->out_state++; |
| 2534 |
} |
| 2535 |
|
| 2536 |
static void OJPEGWriteStreamSof(TIFF *tif, void **mem, uint32_t *len) |
| 2537 |
{ |
| 2538 |
OJPEGState *sp = (OJPEGState *)tif->tif_data; |
| 2539 |
uint8_t m; |
| 2540 |
assert(OJPEG_BUFFER >= 2 + 8 + sp->samples_per_pixel_per_plane * 3); |
| 2541 |
assert(255 >= 8 + sp->samples_per_pixel_per_plane * 3); |
| 2542 |
sp->out_buffer[0] = 255; |
| 2543 |
sp->out_buffer[1] = sp->sof_marker_id; |
| 2544 |
/* Lf */ |
| 2545 |
sp->out_buffer[2] = 0; |
| 2546 |
sp->out_buffer[3] = 8 + sp->samples_per_pixel_per_plane * 3; |
| 2547 |
/* P */ |
| 2548 |
sp->out_buffer[4] = 8; |
| 2549 |
/* Y */ |
| 2550 |
sp->out_buffer[5] = (uint8_t)(sp->sof_y >> 8); |
| 2551 |
sp->out_buffer[6] = (sp->sof_y & 255); |
| 2552 |
/* X */ |
| 2553 |
sp->out_buffer[7] = (uint8_t)(sp->sof_x >> 8); |
| 2554 |
sp->out_buffer[8] = (sp->sof_x & 255); |
| 2555 |
/* Nf */ |
| 2556 |
sp->out_buffer[9] = sp->samples_per_pixel_per_plane; |
| 2557 |
for (m = 0; m < sp->samples_per_pixel_per_plane; m++) |
| 2558 |
{ |
| 2559 |
/* C */ |
| 2560 |
sp->out_buffer[10 + m * 3] = sp->sof_c[sp->plane_sample_offset + m]; |
| 2561 |
/* H and V */ |
| 2562 |
sp->out_buffer[10 + m * 3 + 1] = |
| 2563 |
sp->sof_hv[sp->plane_sample_offset + m]; |
| 2564 |
/* Tq */ |
| 2565 |
sp->out_buffer[10 + m * 3 + 2] = |
| 2566 |
sp->sof_tq[sp->plane_sample_offset + m]; |
| 2567 |
} |
| 2568 |
*len = 10 + sp->samples_per_pixel_per_plane * 3; |
| 2569 |
*mem = (void *)sp->out_buffer; |
| 2570 |
sp->out_state++; |
| 2571 |
} |
| 2572 |
|
| 2573 |
static void OJPEGWriteStreamSos(TIFF *tif, void **mem, uint32_t *len) |
| 2574 |
{ |
| 2575 |
OJPEGState *sp = (OJPEGState *)tif->tif_data; |
| 2576 |
uint8_t m; |
| 2577 |
assert(OJPEG_BUFFER >= 2 + 6 + sp->samples_per_pixel_per_plane * 2); |
| 2578 |
assert(255 >= 6 + sp->samples_per_pixel_per_plane * 2); |
| 2579 |
sp->out_buffer[0] = 255; |
| 2580 |
sp->out_buffer[1] = JPEG_MARKER_SOS; |
| 2581 |
/* Ls */ |
| 2582 |
sp->out_buffer[2] = 0; |
| 2583 |
sp->out_buffer[3] = 6 + sp->samples_per_pixel_per_plane * 2; |
| 2584 |
/* Ns */ |
| 2585 |
sp->out_buffer[4] = sp->samples_per_pixel_per_plane; |
| 2586 |
for (m = 0; m < sp->samples_per_pixel_per_plane; m++) |
| 2587 |
{ |
| 2588 |
/* Cs */ |
| 2589 |
sp->out_buffer[5 + m * 2] = sp->sos_cs[sp->plane_sample_offset + m]; |
| 2590 |
/* Td and Ta */ |
| 2591 |
sp->out_buffer[5 + m * 2 + 1] = |
| 2592 |
sp->sos_tda[sp->plane_sample_offset + m]; |
| 2593 |
} |
| 2594 |
/* Ss */ |
| 2595 |
sp->out_buffer[5 + sp->samples_per_pixel_per_plane * 2] = 0; |
| 2596 |
/* Se */ |
| 2597 |
sp->out_buffer[5 + sp->samples_per_pixel_per_plane * 2 + 1] = 63; |
| 2598 |
/* Ah and Al */ |
| 2599 |
sp->out_buffer[5 + sp->samples_per_pixel_per_plane * 2 + 2] = 0; |
| 2600 |
*len = 8 + sp->samples_per_pixel_per_plane * 2; |
| 2601 |
*mem = (void *)sp->out_buffer; |
| 2602 |
sp->out_state++; |
| 2603 |
} |
| 2604 |
|
| 2605 |
static int OJPEGWriteStreamCompressed(TIFF *tif, void **mem, uint32_t *len) |
| 2606 |
{ |
| 2607 |
OJPEGState *sp = (OJPEGState *)tif->tif_data; |
| 2608 |
if (sp->in_buffer_togo == 0) |
| 2609 |
{ |
| 2610 |
if (OJPEGReadBufferFill(sp) == 0) |
| 2611 |
return (0); |
| 2612 |
assert(sp->in_buffer_togo > 0); |
| 2613 |
} |
| 2614 |
*len = sp->in_buffer_togo; |
| 2615 |
*mem = (void *)sp->in_buffer_cur; |
| 2616 |
sp->in_buffer_togo = 0; |
| 2617 |
if (sp->in_buffer_file_togo == 0) |
| 2618 |
{ |
| 2619 |
switch (sp->in_buffer_source) |
| 2620 |
{ |
| 2621 |
case osibsStrile: |
| 2622 |
if (sp->in_buffer_next_strile < sp->in_buffer_strile_count) |
| 2623 |
sp->out_state = ososRst; |
| 2624 |
else |
| 2625 |
sp->out_state = ososEoi; |
| 2626 |
break; |
| 2627 |
case osibsEof: |
| 2628 |
sp->out_state = ososEoi; |
| 2629 |
break; |
| 2630 |
default: |
| 2631 |
break; |
| 2632 |
} |
| 2633 |
} |
| 2634 |
return (1); |
| 2635 |
} |
| 2636 |
|
| 2637 |
static void OJPEGWriteStreamRst(TIFF *tif, void **mem, uint32_t *len) |
| 2638 |
{ |
| 2639 |
OJPEGState *sp = (OJPEGState *)tif->tif_data; |
| 2640 |
assert(OJPEG_BUFFER >= 2); |
| 2641 |
sp->out_buffer[0] = 255; |
| 2642 |
sp->out_buffer[1] = JPEG_MARKER_RST0 + sp->restart_index; |
| 2643 |
sp->restart_index++; |
| 2644 |
if (sp->restart_index == 8) |
| 2645 |
sp->restart_index = 0; |
| 2646 |
*len = 2; |
| 2647 |
*mem = (void *)sp->out_buffer; |
| 2648 |
sp->out_state = ososCompressed; |
| 2649 |
} |
| 2650 |
|
| 2651 |
static void OJPEGWriteStreamEoi(TIFF *tif, void **mem, uint32_t *len) |
| 2652 |
{ |
| 2653 |
OJPEGState *sp = (OJPEGState *)tif->tif_data; |
| 2654 |
assert(OJPEG_BUFFER >= 2); |
| 2655 |
sp->out_buffer[0] = 255; |
| 2656 |
sp->out_buffer[1] = JPEG_MARKER_EOI; |
| 2657 |
*len = 2; |
| 2658 |
*mem = (void *)sp->out_buffer; |
| 2659 |
} |
| 2660 |
|
| 2661 |
#ifndef LIBJPEG_ENCAP_EXTERNAL |
| 2662 |
static int jpeg_create_decompress_encap(OJPEGState *sp, |
| 2663 |
jpeg_decompress_struct *cinfo) |
| 2664 |
{ |
| 2665 |
if (SETJMP(sp->exit_jmpbuf)) |
| 2666 |
return 0; |
| 2667 |
else |
| 2668 |
{ |
| 2669 |
jpeg_create_decompress(cinfo); |
| 2670 |
return 1; |
| 2671 |
} |
| 2672 |
} |
| 2673 |
#endif |
| 2674 |
|
| 2675 |
#ifndef LIBJPEG_ENCAP_EXTERNAL |
| 2676 |
static int jpeg_read_header_encap(OJPEGState *sp, jpeg_decompress_struct *cinfo, |
| 2677 |
uint8_t require_image) |
| 2678 |
{ |
| 2679 |
if (SETJMP(sp->exit_jmpbuf)) |
| 2680 |
return 0; |
| 2681 |
else |
| 2682 |
{ |
| 2683 |
jpeg_read_header(cinfo, require_image); |
| 2684 |
return 1; |
| 2685 |
} |
| 2686 |
} |
| 2687 |
#endif |
| 2688 |
|
| 2689 |
#ifndef LIBJPEG_ENCAP_EXTERNAL |
| 2690 |
static int jpeg_start_decompress_encap(OJPEGState *sp, |
| 2691 |
jpeg_decompress_struct *cinfo) |
| 2692 |
{ |
| 2693 |
if (SETJMP(sp->exit_jmpbuf)) |
| 2694 |
return 0; |
| 2695 |
else |
| 2696 |
{ |
| 2697 |
jpeg_start_decompress(cinfo); |
| 2698 |
return 1; |
| 2699 |
} |
| 2700 |
} |
| 2701 |
#endif |
| 2702 |
|
| 2703 |
#ifndef LIBJPEG_ENCAP_EXTERNAL |
| 2704 |
static int jpeg_read_scanlines_encap(OJPEGState *sp, |
| 2705 |
jpeg_decompress_struct *cinfo, |
| 2706 |
void *scanlines, uint32_t max_lines) |
| 2707 |
{ |
| 2708 |
if (SETJMP(sp->exit_jmpbuf)) |
| 2709 |
return 0; |
| 2710 |
else |
| 2711 |
{ |
| 2712 |
jpeg_read_scanlines(cinfo, scanlines, max_lines); |
| 2713 |
return 1; |
| 2714 |
} |
| 2715 |
} |
| 2716 |
#endif |
| 2717 |
|
| 2718 |
#ifndef LIBJPEG_ENCAP_EXTERNAL |
| 2719 |
static int jpeg_read_raw_data_encap(OJPEGState *sp, |
| 2720 |
jpeg_decompress_struct *cinfo, void *data, |
| 2721 |
uint32_t max_lines) |
| 2722 |
{ |
| 2723 |
if (SETJMP(sp->exit_jmpbuf)) |
| 2724 |
return 0; |
| 2725 |
else |
| 2726 |
{ |
| 2727 |
jpeg_read_raw_data(cinfo, data, max_lines); |
| 2728 |
return 1; |
| 2729 |
} |
| 2730 |
} |
| 2731 |
#endif |
| 2732 |
|
| 2733 |
#ifndef LIBJPEG_ENCAP_EXTERNAL |
| 2734 |
static void jpeg_encap_unwind(TIFF *tif) |
| 2735 |
{ |
| 2736 |
OJPEGState *sp = (OJPEGState *)tif->tif_data; |
| 2737 |
LONGJMP(sp->exit_jmpbuf, 1); |
| 2738 |
} |
| 2739 |
#endif |
| 2740 |
|
| 2741 |
static void OJPEGLibjpegJpegErrorMgrOutputMessage(jpeg_common_struct *cinfo) |
| 2742 |
{ |
| 2743 |
char buffer[JMSG_LENGTH_MAX]; |
| 2744 |
(*cinfo->err->format_message)(cinfo, buffer); |
| 2745 |
TIFFWarningExtR(((TIFF *)(cinfo->client_data)), "LibJpeg", "%s", buffer); |
| 2746 |
} |
| 2747 |
|
| 2748 |
static void OJPEGLibjpegJpegErrorMgrErrorExit(jpeg_common_struct *cinfo) |
| 2749 |
{ |
| 2750 |
char buffer[JMSG_LENGTH_MAX]; |
| 2751 |
(*cinfo->err->format_message)(cinfo, buffer); |
| 2752 |
TIFFErrorExtR(((TIFF *)(cinfo->client_data)), "LibJpeg", "%s", buffer); |
| 2753 |
jpeg_encap_unwind((TIFF *)(cinfo->client_data)); |
| 2754 |
} |
| 2755 |
|
| 2756 |
static void OJPEGLibjpegJpegSourceMgrInitSource(jpeg_decompress_struct *cinfo) |
| 2757 |
{ |
| 2758 |
(void)cinfo; |
| 2759 |
} |
| 2760 |
|
| 2761 |
static boolean |
| 2762 |
OJPEGLibjpegJpegSourceMgrFillInputBuffer(jpeg_decompress_struct *cinfo) |
| 2763 |
{ |
| 2764 |
TIFF *tif = (TIFF *)cinfo->client_data; |
| 2765 |
OJPEGState *sp = (OJPEGState *)tif->tif_data; |
| 2766 |
void *mem = 0; |
| 2767 |
uint32_t len = 0U; |
| 2768 |
if (OJPEGWriteStream(tif, &mem, &len) == 0) |
| 2769 |
{ |
| 2770 |
TIFFErrorExtR(tif, "LibJpeg", "Premature end of JPEG data"); |
| 2771 |
jpeg_encap_unwind(tif); |
| 2772 |
} |
| 2773 |
sp->libjpeg_jpeg_source_mgr.bytes_in_buffer = len; |
| 2774 |
sp->libjpeg_jpeg_source_mgr.next_input_byte = mem; |
| 2775 |
return (1); |
| 2776 |
} |
| 2777 |
|
| 2778 |
static void |
| 2779 |
OJPEGLibjpegJpegSourceMgrSkipInputData(jpeg_decompress_struct *cinfo, |
| 2780 |
long num_bytes) |
| 2781 |
{ |
| 2782 |
TIFF *tif = (TIFF *)cinfo->client_data; |
| 2783 |
(void)num_bytes; |
| 2784 |
TIFFErrorExtR(tif, "LibJpeg", "Unexpected error"); |
| 2785 |
jpeg_encap_unwind(tif); |
| 2786 |
} |
| 2787 |
|
| 2788 |
#ifdef _MSC_VER |
| 2789 |
#pragma warning(push) |
| 2790 |
#pragma warning(disable : 4702) /* unreachable code */ |
| 2791 |
#endif |
| 2792 |
static boolean |
| 2793 |
OJPEGLibjpegJpegSourceMgrResyncToRestart(jpeg_decompress_struct *cinfo, |
| 2794 |
int desired) |
| 2795 |
{ |
| 2796 |
TIFF *tif = (TIFF *)cinfo->client_data; |
| 2797 |
(void)desired; |
| 2798 |
TIFFErrorExtR(tif, "LibJpeg", "Unexpected error"); |
| 2799 |
jpeg_encap_unwind(tif); |
| 2800 |
return (0); |
| 2801 |
} |
| 2802 |
#ifdef _MSC_VER |
| 2803 |
#pragma warning(pop) |
| 2804 |
#endif |
| 2805 |
|
| 2806 |
static void OJPEGLibjpegJpegSourceMgrTermSource(jpeg_decompress_struct *cinfo) |
| 2807 |
{ |
| 2808 |
(void)cinfo; |
| 2809 |
} |
| 2810 |
|
| 2811 |
#endif |