/src/mozilla-central/image/decoders/nsJPEGDecoder.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
2 | | * |
3 | | * This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #include "ImageLogging.h" // Must appear first. |
8 | | |
9 | | #include "nsJPEGDecoder.h" |
10 | | |
11 | | #include <cstdint> |
12 | | |
13 | | #include "imgFrame.h" |
14 | | #include "Orientation.h" |
15 | | #include "EXIF.h" |
16 | | |
17 | | #include "nsIInputStream.h" |
18 | | |
19 | | #include "nspr.h" |
20 | | #include "nsCRT.h" |
21 | | #include "gfxColor.h" |
22 | | |
23 | | #include "jerror.h" |
24 | | |
25 | | #include "gfxPlatform.h" |
26 | | #include "mozilla/EndianUtils.h" |
27 | | #include "mozilla/gfx/Types.h" |
28 | | #include "mozilla/Telemetry.h" |
29 | | |
30 | | extern "C" { |
31 | | #include "iccjpeg.h" |
32 | | } |
33 | | |
34 | | #if MOZ_BIG_ENDIAN |
35 | | #define MOZ_JCS_EXT_NATIVE_ENDIAN_XRGB JCS_EXT_XRGB |
36 | | #else |
37 | 0 | #define MOZ_JCS_EXT_NATIVE_ENDIAN_XRGB JCS_EXT_BGRX |
38 | | #endif |
39 | | |
40 | | static void cmyk_convert_rgb(JSAMPROW row, JDIMENSION width); |
41 | | |
42 | | using mozilla::gfx::SurfaceFormat; |
43 | | |
44 | | namespace mozilla { |
45 | | namespace image { |
46 | | |
47 | | static mozilla::LazyLogModule sJPEGLog("JPEGDecoder"); |
48 | | |
49 | | static mozilla::LazyLogModule sJPEGDecoderAccountingLog("JPEGDecoderAccounting"); |
50 | | |
51 | | static qcms_profile* |
52 | | GetICCProfile(struct jpeg_decompress_struct& info) |
53 | 0 | { |
54 | 0 | JOCTET* profilebuf; |
55 | 0 | uint32_t profileLength; |
56 | 0 | qcms_profile* profile = nullptr; |
57 | 0 |
|
58 | 0 | if (read_icc_profile(&info, &profilebuf, &profileLength)) { |
59 | 0 | profile = qcms_profile_from_memory(profilebuf, profileLength); |
60 | 0 | free(profilebuf); |
61 | 0 | } |
62 | 0 |
|
63 | 0 | return profile; |
64 | 0 | } |
65 | | |
66 | | METHODDEF(void) init_source (j_decompress_ptr jd); |
67 | | METHODDEF(boolean) fill_input_buffer (j_decompress_ptr jd); |
68 | | METHODDEF(void) skip_input_data (j_decompress_ptr jd, long num_bytes); |
69 | | METHODDEF(void) term_source (j_decompress_ptr jd); |
70 | | METHODDEF(void) my_error_exit (j_common_ptr cinfo); |
71 | | |
72 | | // Normal JFIF markers can't have more bytes than this. |
73 | 0 | #define MAX_JPEG_MARKER_LENGTH (((uint32_t)1 << 16) - 1) |
74 | | |
75 | | nsJPEGDecoder::nsJPEGDecoder(RasterImage* aImage, |
76 | | Decoder::DecodeStyle aDecodeStyle) |
77 | | : Decoder(aImage) |
78 | | , mLexer(Transition::ToUnbuffered(State::FINISHED_JPEG_DATA, |
79 | | State::JPEG_DATA, |
80 | | SIZE_MAX), |
81 | | Transition::TerminateSuccess()) |
82 | | , mProfile(nullptr) |
83 | | , mProfileLength(0) |
84 | | , mDecodeStyle(aDecodeStyle) |
85 | 0 | { |
86 | 0 | this->mErr.pub.error_exit = nullptr; |
87 | 0 | this->mErr.pub.emit_message = nullptr; |
88 | 0 | this->mErr.pub.output_message = nullptr; |
89 | 0 | this->mErr.pub.format_message = nullptr; |
90 | 0 | this->mErr.pub.reset_error_mgr = nullptr; |
91 | 0 | this->mErr.pub.msg_code = 0; |
92 | 0 | this->mErr.pub.trace_level = 0; |
93 | 0 | this->mErr.pub.num_warnings = 0; |
94 | 0 | this->mErr.pub.jpeg_message_table = nullptr; |
95 | 0 | this->mErr.pub.last_jpeg_message = 0; |
96 | 0 | this->mErr.pub.addon_message_table = nullptr; |
97 | 0 | this->mErr.pub.first_addon_message = 0; |
98 | 0 | this->mErr.pub.last_addon_message = 0; |
99 | 0 | mState = JPEG_HEADER; |
100 | 0 | mReading = true; |
101 | 0 | mImageData = nullptr; |
102 | 0 |
|
103 | 0 | mBytesToSkip = 0; |
104 | 0 | memset(&mInfo, 0, sizeof(jpeg_decompress_struct)); |
105 | 0 | memset(&mSourceMgr, 0, sizeof(mSourceMgr)); |
106 | 0 | mInfo.client_data = (void*)this; |
107 | 0 |
|
108 | 0 | mSegment = nullptr; |
109 | 0 | mSegmentLen = 0; |
110 | 0 |
|
111 | 0 | mBackBuffer = nullptr; |
112 | 0 | mBackBufferLen = mBackBufferSize = mBackBufferUnreadLen = 0; |
113 | 0 |
|
114 | 0 | mInProfile = nullptr; |
115 | 0 | mTransform = nullptr; |
116 | 0 |
|
117 | 0 | mCMSMode = 0; |
118 | 0 |
|
119 | 0 | MOZ_LOG(sJPEGDecoderAccountingLog, LogLevel::Debug, |
120 | 0 | ("nsJPEGDecoder::nsJPEGDecoder: Creating JPEG decoder %p", |
121 | 0 | this)); |
122 | 0 | } |
123 | | |
124 | | nsJPEGDecoder::~nsJPEGDecoder() |
125 | 0 | { |
126 | 0 | // Step 8: Release JPEG decompression object |
127 | 0 | mInfo.src = nullptr; |
128 | 0 | jpeg_destroy_decompress(&mInfo); |
129 | 0 |
|
130 | 0 | free(mBackBuffer); |
131 | 0 | mBackBuffer = nullptr; |
132 | 0 | if (mTransform) { |
133 | 0 | qcms_transform_release(mTransform); |
134 | 0 | } |
135 | 0 | if (mInProfile) { |
136 | 0 | qcms_profile_release(mInProfile); |
137 | 0 | } |
138 | 0 |
|
139 | 0 | MOZ_LOG(sJPEGDecoderAccountingLog, LogLevel::Debug, |
140 | 0 | ("nsJPEGDecoder::~nsJPEGDecoder: Destroying JPEG decoder %p", |
141 | 0 | this)); |
142 | 0 | } |
143 | | |
144 | | Maybe<Telemetry::HistogramID> |
145 | | nsJPEGDecoder::SpeedHistogram() const |
146 | 0 | { |
147 | 0 | return Some(Telemetry::IMAGE_DECODE_SPEED_JPEG); |
148 | 0 | } |
149 | | |
150 | | nsresult |
151 | | nsJPEGDecoder::InitInternal() |
152 | 0 | { |
153 | 0 | mCMSMode = gfxPlatform::GetCMSMode(); |
154 | 0 | if (GetSurfaceFlags() & SurfaceFlags::NO_COLORSPACE_CONVERSION) { |
155 | 0 | mCMSMode = eCMSMode_Off; |
156 | 0 | } |
157 | 0 |
|
158 | 0 | // We set up the normal JPEG error routines, then override error_exit. |
159 | 0 | mInfo.err = jpeg_std_error(&mErr.pub); |
160 | 0 | // mInfo.err = jpeg_std_error(&mErr.pub); |
161 | 0 | mErr.pub.error_exit = my_error_exit; |
162 | 0 | // Establish the setjmp return context for my_error_exit to use. |
163 | 0 | if (setjmp(mErr.setjmp_buffer)) { |
164 | 0 | // If we get here, the JPEG code has signaled an error, and initialization |
165 | 0 | // has failed. |
166 | 0 | return NS_ERROR_FAILURE; |
167 | 0 | } |
168 | 0 | |
169 | 0 | // Step 1: allocate and initialize JPEG decompression object |
170 | 0 | jpeg_create_decompress(&mInfo); |
171 | 0 | // Set the source manager |
172 | 0 | mInfo.src = &mSourceMgr; |
173 | 0 |
|
174 | 0 | // Step 2: specify data source (eg, a file) |
175 | 0 |
|
176 | 0 | // Setup callback functions. |
177 | 0 | mSourceMgr.init_source = init_source; |
178 | 0 | mSourceMgr.fill_input_buffer = fill_input_buffer; |
179 | 0 | mSourceMgr.skip_input_data = skip_input_data; |
180 | 0 | mSourceMgr.resync_to_restart = jpeg_resync_to_restart; |
181 | 0 | mSourceMgr.term_source = term_source; |
182 | 0 |
|
183 | 0 | // Record app markers for ICC data |
184 | 0 | for (uint32_t m = 0; m < 16; m++) { |
185 | 0 | jpeg_save_markers(&mInfo, JPEG_APP0 + m, 0xFFFF); |
186 | 0 | } |
187 | 0 |
|
188 | 0 | return NS_OK; |
189 | 0 | } |
190 | | |
191 | | nsresult |
192 | | nsJPEGDecoder::FinishInternal() |
193 | 0 | { |
194 | 0 | // If we're not in any sort of error case, force our state to JPEG_DONE. |
195 | 0 | if ((mState != JPEG_DONE && mState != JPEG_SINK_NON_JPEG_TRAILER) && |
196 | 0 | (mState != JPEG_ERROR) && |
197 | 0 | !IsMetadataDecode()) { |
198 | 0 | mState = JPEG_DONE; |
199 | 0 | } |
200 | 0 |
|
201 | 0 | return NS_OK; |
202 | 0 | } |
203 | | |
204 | | LexerResult |
205 | | nsJPEGDecoder::DoDecode(SourceBufferIterator& aIterator, IResumable* aOnResume) |
206 | 0 | { |
207 | 0 | MOZ_ASSERT(!HasError(), "Shouldn't call DoDecode after error!"); |
208 | 0 |
|
209 | 0 | return mLexer.Lex(aIterator, aOnResume, |
210 | 0 | [=](State aState, const char* aData, size_t aLength) { |
211 | 0 | switch (aState) { |
212 | 0 | case State::JPEG_DATA: |
213 | 0 | return ReadJPEGData(aData, aLength); |
214 | 0 | case State::FINISHED_JPEG_DATA: |
215 | 0 | return FinishedJPEGData(); |
216 | 0 | } |
217 | 0 | MOZ_CRASH("Unknown State"); |
218 | 0 | }); |
219 | 0 | } |
220 | | |
221 | | LexerTransition<nsJPEGDecoder::State> |
222 | | nsJPEGDecoder::ReadJPEGData(const char* aData, size_t aLength) |
223 | 0 | { |
224 | 0 | mSegment = reinterpret_cast<const JOCTET*>(aData); |
225 | 0 | mSegmentLen = aLength; |
226 | 0 |
|
227 | 0 | // Return here if there is a fatal error within libjpeg. |
228 | 0 | nsresult error_code; |
229 | 0 | // This cast to nsresult makes sense because setjmp() returns whatever we |
230 | 0 | // passed to longjmp(), which was actually an nsresult. |
231 | 0 | if ((error_code = static_cast<nsresult>(setjmp(mErr.setjmp_buffer))) != NS_OK) { |
232 | 0 | if (error_code == NS_ERROR_FAILURE) { |
233 | 0 | // Error due to corrupt data. Make sure that we don't feed any more data |
234 | 0 | // to libjpeg-turbo. |
235 | 0 | mState = JPEG_SINK_NON_JPEG_TRAILER; |
236 | 0 | MOZ_LOG(sJPEGDecoderAccountingLog, LogLevel::Debug, |
237 | 0 | ("} (setjmp returned NS_ERROR_FAILURE)")); |
238 | 0 | } else { |
239 | 0 | // Error for another reason. (Possibly OOM.) |
240 | 0 | mState = JPEG_ERROR; |
241 | 0 | MOZ_LOG(sJPEGDecoderAccountingLog, LogLevel::Debug, |
242 | 0 | ("} (setjmp returned an error)")); |
243 | 0 | } |
244 | 0 |
|
245 | 0 | return Transition::TerminateFailure(); |
246 | 0 | } |
247 | 0 |
|
248 | 0 | MOZ_LOG(sJPEGLog, LogLevel::Debug, |
249 | 0 | ("[this=%p] nsJPEGDecoder::Write -- processing JPEG data\n", this)); |
250 | 0 |
|
251 | 0 | switch (mState) { |
252 | 0 | case JPEG_HEADER: { |
253 | 0 | LOG_SCOPE((mozilla::LogModule*)sJPEGLog, "nsJPEGDecoder::Write -- entering JPEG_HEADER" |
254 | 0 | " case"); |
255 | 0 |
|
256 | 0 | // Step 3: read file parameters with jpeg_read_header() |
257 | 0 | if (jpeg_read_header(&mInfo, TRUE) == JPEG_SUSPENDED) { |
258 | 0 | MOZ_LOG(sJPEGDecoderAccountingLog, LogLevel::Debug, |
259 | 0 | ("} (JPEG_SUSPENDED)")); |
260 | 0 | return Transition::ContinueUnbuffered(State::JPEG_DATA); // I/O suspension |
261 | 0 | } |
262 | 0 |
|
263 | 0 | // Post our size to the superclass |
264 | 0 | PostSize(mInfo.image_width, mInfo.image_height, |
265 | 0 | ReadOrientationFromEXIF()); |
266 | 0 | if (HasError()) { |
267 | 0 | // Setting the size led to an error. |
268 | 0 | mState = JPEG_ERROR; |
269 | 0 | return Transition::TerminateFailure(); |
270 | 0 | } |
271 | 0 | |
272 | 0 | // If we're doing a metadata decode, we're done. |
273 | 0 | if (IsMetadataDecode()) { |
274 | 0 | return Transition::TerminateSuccess(); |
275 | 0 | } |
276 | 0 | |
277 | 0 | // We're doing a full decode. |
278 | 0 | if (mCMSMode != eCMSMode_Off && |
279 | 0 | (mInProfile = GetICCProfile(mInfo)) != nullptr) { |
280 | 0 | uint32_t profileSpace = qcms_profile_get_color_space(mInProfile); |
281 | 0 | bool mismatch = false; |
282 | 0 |
|
283 | | #ifdef DEBUG_tor |
284 | | fprintf(stderr, "JPEG profileSpace: 0x%08X\n", profileSpace); |
285 | | #endif |
286 | | switch (mInfo.jpeg_color_space) { |
287 | 0 | case JCS_GRAYSCALE: |
288 | 0 | if (profileSpace == icSigRgbData) { |
289 | 0 | mInfo.out_color_space = JCS_RGB; |
290 | 0 | } else if (profileSpace != icSigGrayData) { |
291 | 0 | mismatch = true; |
292 | 0 | } |
293 | 0 | break; |
294 | 0 | case JCS_RGB: |
295 | 0 | if (profileSpace != icSigRgbData) { |
296 | 0 | mismatch = true; |
297 | 0 | } |
298 | 0 | break; |
299 | 0 | case JCS_YCbCr: |
300 | 0 | if (profileSpace == icSigRgbData) { |
301 | 0 | mInfo.out_color_space = JCS_RGB; |
302 | 0 | } else { |
303 | 0 | // qcms doesn't support ycbcr |
304 | 0 | mismatch = true; |
305 | 0 | } |
306 | 0 | break; |
307 | 0 | case JCS_CMYK: |
308 | 0 | case JCS_YCCK: |
309 | 0 | // qcms doesn't support cmyk |
310 | 0 | mismatch = true; |
311 | 0 | break; |
312 | 0 | default: |
313 | 0 | mState = JPEG_ERROR; |
314 | 0 | MOZ_LOG(sJPEGDecoderAccountingLog, LogLevel::Debug, |
315 | 0 | ("} (unknown colorpsace (1))")); |
316 | 0 | return Transition::TerminateFailure(); |
317 | 0 | } |
318 | 0 |
|
319 | 0 | if (!mismatch) { |
320 | 0 | qcms_data_type type; |
321 | 0 | switch (mInfo.out_color_space) { |
322 | 0 | case JCS_GRAYSCALE: |
323 | 0 | type = QCMS_DATA_GRAY_8; |
324 | 0 | break; |
325 | 0 | case JCS_RGB: |
326 | 0 | type = QCMS_DATA_RGB_8; |
327 | 0 | break; |
328 | 0 | default: |
329 | 0 | mState = JPEG_ERROR; |
330 | 0 | MOZ_LOG(sJPEGDecoderAccountingLog, LogLevel::Debug, |
331 | 0 | ("} (unknown colorpsace (2))")); |
332 | 0 | return Transition::TerminateFailure(); |
333 | 0 | } |
334 | | #if 0 |
335 | | // We don't currently support CMYK profiles. The following |
336 | | // code dealt with lcms types. Add something like this |
337 | | // back when we gain support for CMYK. |
338 | | |
339 | | // Adobe Photoshop writes YCCK/CMYK files with inverted data |
340 | | if (mInfo.out_color_space == JCS_CMYK) { |
341 | | type |= FLAVOR_SH(mInfo.saw_Adobe_marker ? 1 : 0); |
342 | | } |
343 | | #endif |
344 | | |
345 | 0 | if (gfxPlatform::GetCMSOutputProfile()) { |
346 | 0 |
|
347 | 0 | // Calculate rendering intent. |
348 | 0 | int intent = gfxPlatform::GetRenderingIntent(); |
349 | 0 | if (intent == -1) { |
350 | 0 | intent = qcms_profile_get_rendering_intent(mInProfile); |
351 | 0 | } |
352 | 0 |
|
353 | 0 | // Create the color management transform. |
354 | 0 | mTransform = qcms_transform_create(mInProfile, |
355 | 0 | type, |
356 | 0 | gfxPlatform::GetCMSOutputProfile(), |
357 | 0 | QCMS_DATA_RGB_8, |
358 | 0 | (qcms_intent)intent); |
359 | 0 | } |
360 | 0 | } else { |
361 | | #ifdef DEBUG_tor |
362 | | fprintf(stderr, "ICM profile colorspace mismatch\n"); |
363 | | #endif |
364 | | } |
365 | 0 | } |
366 | 0 |
|
367 | 0 | if (!mTransform) { |
368 | 0 | switch (mInfo.jpeg_color_space) { |
369 | 0 | case JCS_GRAYSCALE: |
370 | 0 | case JCS_RGB: |
371 | 0 | case JCS_YCbCr: |
372 | 0 | // if we're not color managing we can decode directly to |
373 | 0 | // MOZ_JCS_EXT_NATIVE_ENDIAN_XRGB |
374 | 0 | if (mCMSMode != eCMSMode_All) { |
375 | 0 | mInfo.out_color_space = MOZ_JCS_EXT_NATIVE_ENDIAN_XRGB; |
376 | 0 | mInfo.out_color_components = 4; |
377 | 0 | } else { |
378 | 0 | mInfo.out_color_space = JCS_RGB; |
379 | 0 | } |
380 | 0 | break; |
381 | 0 | case JCS_CMYK: |
382 | 0 | case JCS_YCCK: |
383 | 0 | // libjpeg can convert from YCCK to CMYK, but not to RGB |
384 | 0 | mInfo.out_color_space = JCS_CMYK; |
385 | 0 | break; |
386 | 0 | default: |
387 | 0 | mState = JPEG_ERROR; |
388 | 0 | MOZ_LOG(sJPEGDecoderAccountingLog, LogLevel::Debug, |
389 | 0 | ("} (unknown colorpsace (3))")); |
390 | 0 | return Transition::TerminateFailure(); |
391 | 0 | } |
392 | 0 | } |
393 | 0 |
|
394 | 0 | // Don't allocate a giant and superfluous memory buffer |
395 | 0 | // when not doing a progressive decode. |
396 | 0 | mInfo.buffered_image = mDecodeStyle == PROGRESSIVE && |
397 | 0 | jpeg_has_multiple_scans(&mInfo); |
398 | 0 |
|
399 | 0 | /* Used to set up image size so arrays can be allocated */ |
400 | 0 | jpeg_calc_output_dimensions(&mInfo); |
401 | 0 |
|
402 | 0 | MOZ_ASSERT(!mImageData, "Already have a buffer allocated?"); |
403 | 0 | nsresult rv = AllocateFrame(OutputSize(), FullOutputFrame(), |
404 | 0 | SurfaceFormat::B8G8R8X8); |
405 | 0 | if (NS_FAILED(rv)) { |
406 | 0 | mState = JPEG_ERROR; |
407 | 0 | MOZ_LOG(sJPEGDecoderAccountingLog, LogLevel::Debug, |
408 | 0 | ("} (could not initialize image frame)")); |
409 | 0 | return Transition::TerminateFailure(); |
410 | 0 | } |
411 | 0 |
|
412 | 0 | MOZ_ASSERT(mImageData, "Should have a buffer now"); |
413 | 0 |
|
414 | 0 | if (mDownscaler) { |
415 | 0 | nsresult rv = mDownscaler->BeginFrame(Size(), Nothing(), |
416 | 0 | mImageData, |
417 | 0 | /* aHasAlpha = */ false); |
418 | 0 | if (NS_FAILED(rv)) { |
419 | 0 | mState = JPEG_ERROR; |
420 | 0 | return Transition::TerminateFailure(); |
421 | 0 | } |
422 | 0 | } |
423 | 0 | |
424 | 0 | MOZ_LOG(sJPEGDecoderAccountingLog, LogLevel::Debug, |
425 | 0 | (" JPEGDecoderAccounting: nsJPEGDecoder::" |
426 | 0 | "Write -- created image frame with %ux%u pixels", |
427 | 0 | mInfo.image_width, mInfo.image_height)); |
428 | 0 |
|
429 | 0 | mState = JPEG_START_DECOMPRESS; |
430 | 0 | MOZ_FALLTHROUGH; // to start decompressing. |
431 | 0 | } |
432 | 0 |
|
433 | 0 | case JPEG_START_DECOMPRESS: { |
434 | 0 | LOG_SCOPE((mozilla::LogModule*)sJPEGLog, "nsJPEGDecoder::Write -- entering" |
435 | 0 | " JPEG_START_DECOMPRESS case"); |
436 | 0 | // Step 4: set parameters for decompression |
437 | 0 |
|
438 | 0 | // FIXME -- Should reset dct_method and dither mode |
439 | 0 | // for final pass of progressive JPEG |
440 | 0 |
|
441 | 0 | mInfo.dct_method = JDCT_ISLOW; |
442 | 0 | mInfo.dither_mode = JDITHER_FS; |
443 | 0 | mInfo.do_fancy_upsampling = TRUE; |
444 | 0 | mInfo.enable_2pass_quant = FALSE; |
445 | 0 | mInfo.do_block_smoothing = TRUE; |
446 | 0 |
|
447 | 0 | // Step 5: Start decompressor |
448 | 0 | if (jpeg_start_decompress(&mInfo) == FALSE) { |
449 | 0 | MOZ_LOG(sJPEGDecoderAccountingLog, LogLevel::Debug, |
450 | 0 | ("} (I/O suspension after jpeg_start_decompress())")); |
451 | 0 | return Transition::ContinueUnbuffered(State::JPEG_DATA); // I/O suspension |
452 | 0 | } |
453 | 0 |
|
454 | 0 | // If this is a progressive JPEG ... |
455 | 0 | mState = mInfo.buffered_image ? |
456 | 0 | JPEG_DECOMPRESS_PROGRESSIVE : JPEG_DECOMPRESS_SEQUENTIAL; |
457 | 0 | MOZ_FALLTHROUGH; // to decompress sequential JPEG. |
458 | 0 | } |
459 | 0 |
|
460 | 0 | case JPEG_DECOMPRESS_SEQUENTIAL: { |
461 | 0 | if (mState == JPEG_DECOMPRESS_SEQUENTIAL) { |
462 | 0 | LOG_SCOPE((mozilla::LogModule*)sJPEGLog, "nsJPEGDecoder::Write -- " |
463 | 0 | "JPEG_DECOMPRESS_SEQUENTIAL case"); |
464 | 0 |
|
465 | 0 | bool suspend; |
466 | 0 | OutputScanlines(&suspend); |
467 | 0 |
|
468 | 0 | if (suspend) { |
469 | 0 | MOZ_LOG(sJPEGDecoderAccountingLog, LogLevel::Debug, |
470 | 0 | ("} (I/O suspension after OutputScanlines() - SEQUENTIAL)")); |
471 | 0 | return Transition::ContinueUnbuffered(State::JPEG_DATA); // I/O suspension |
472 | 0 | } |
473 | 0 |
|
474 | 0 | // If we've completed image output ... |
475 | 0 | NS_ASSERTION(mInfo.output_scanline == mInfo.output_height, |
476 | 0 | "We didn't process all of the data!"); |
477 | 0 | mState = JPEG_DONE; |
478 | 0 | } |
479 | 0 | MOZ_FALLTHROUGH; // to decompress progressive JPEG. |
480 | 0 | } |
481 | 0 |
|
482 | 0 | case JPEG_DECOMPRESS_PROGRESSIVE: { |
483 | 0 | if (mState == JPEG_DECOMPRESS_PROGRESSIVE) { |
484 | 0 | LOG_SCOPE((mozilla::LogModule*)sJPEGLog, |
485 | 0 | "nsJPEGDecoder::Write -- JPEG_DECOMPRESS_PROGRESSIVE case"); |
486 | 0 |
|
487 | 0 | int status; |
488 | 0 | do { |
489 | 0 | status = jpeg_consume_input(&mInfo); |
490 | 0 | } while ((status != JPEG_SUSPENDED) && |
491 | 0 | (status != JPEG_REACHED_EOI)); |
492 | 0 |
|
493 | 0 | for (;;) { |
494 | 0 | if (mInfo.output_scanline == 0) { |
495 | 0 | int scan = mInfo.input_scan_number; |
496 | 0 |
|
497 | 0 | // if we haven't displayed anything yet (output_scan_number==0) |
498 | 0 | // and we have enough data for a complete scan, force output |
499 | 0 | // of the last full scan |
500 | 0 | if ((mInfo.output_scan_number == 0) && |
501 | 0 | (scan > 1) && |
502 | 0 | (status != JPEG_REACHED_EOI)) |
503 | 0 | scan--; |
504 | 0 |
|
505 | 0 | if (!jpeg_start_output(&mInfo, scan)) { |
506 | 0 | MOZ_LOG(sJPEGDecoderAccountingLog, LogLevel::Debug, |
507 | 0 | ("} (I/O suspension after jpeg_start_output() -" |
508 | 0 | " PROGRESSIVE)")); |
509 | 0 | return Transition::ContinueUnbuffered(State::JPEG_DATA); // I/O suspension |
510 | 0 | } |
511 | 0 | } |
512 | 0 |
|
513 | 0 | if (mInfo.output_scanline == 0xffffff) { |
514 | 0 | mInfo.output_scanline = 0; |
515 | 0 | } |
516 | 0 |
|
517 | 0 | bool suspend; |
518 | 0 | OutputScanlines(&suspend); |
519 | 0 |
|
520 | 0 | if (suspend) { |
521 | 0 | if (mInfo.output_scanline == 0) { |
522 | 0 | // didn't manage to read any lines - flag so we don't call |
523 | 0 | // jpeg_start_output() multiple times for the same scan |
524 | 0 | mInfo.output_scanline = 0xffffff; |
525 | 0 | } |
526 | 0 | MOZ_LOG(sJPEGDecoderAccountingLog, LogLevel::Debug, |
527 | 0 | ("} (I/O suspension after OutputScanlines() - PROGRESSIVE)")); |
528 | 0 | return Transition::ContinueUnbuffered(State::JPEG_DATA); // I/O suspension |
529 | 0 | } |
530 | 0 |
|
531 | 0 | if (mInfo.output_scanline == mInfo.output_height) { |
532 | 0 | if (!jpeg_finish_output(&mInfo)) { |
533 | 0 | MOZ_LOG(sJPEGDecoderAccountingLog, LogLevel::Debug, |
534 | 0 | ("} (I/O suspension after jpeg_finish_output() -" |
535 | 0 | " PROGRESSIVE)")); |
536 | 0 | return Transition::ContinueUnbuffered(State::JPEG_DATA); // I/O suspension |
537 | 0 | } |
538 | 0 |
|
539 | 0 | if (jpeg_input_complete(&mInfo) && |
540 | 0 | (mInfo.input_scan_number == mInfo.output_scan_number)) |
541 | 0 | break; |
542 | 0 | |
543 | 0 | mInfo.output_scanline = 0; |
544 | 0 | if (mDownscaler) { |
545 | 0 | mDownscaler->ResetForNextProgressivePass(); |
546 | 0 | } |
547 | 0 | } |
548 | 0 | } |
549 | 0 |
|
550 | 0 | mState = JPEG_DONE; |
551 | 0 | } |
552 | 0 | MOZ_FALLTHROUGH; // to finish decompressing. |
553 | 0 | } |
554 | 0 |
|
555 | 0 | case JPEG_DONE: { |
556 | 0 | LOG_SCOPE((mozilla::LogModule*)sJPEGLog, "nsJPEGDecoder::ProcessData -- entering" |
557 | 0 | " JPEG_DONE case"); |
558 | 0 |
|
559 | 0 | // Step 7: Finish decompression |
560 | 0 |
|
561 | 0 | if (jpeg_finish_decompress(&mInfo) == FALSE) { |
562 | 0 | MOZ_LOG(sJPEGDecoderAccountingLog, LogLevel::Debug, |
563 | 0 | ("} (I/O suspension after jpeg_finish_decompress() - DONE)")); |
564 | 0 | return Transition::ContinueUnbuffered(State::JPEG_DATA); // I/O suspension |
565 | 0 | } |
566 | 0 |
|
567 | 0 | // Make sure we don't feed any more data to libjpeg-turbo. |
568 | 0 | mState = JPEG_SINK_NON_JPEG_TRAILER; |
569 | 0 |
|
570 | 0 | // We're done. |
571 | 0 | return Transition::TerminateSuccess(); |
572 | 0 | } |
573 | 0 | case JPEG_SINK_NON_JPEG_TRAILER: |
574 | 0 | MOZ_LOG(sJPEGLog, LogLevel::Debug, |
575 | 0 | ("[this=%p] nsJPEGDecoder::ProcessData -- entering" |
576 | 0 | " JPEG_SINK_NON_JPEG_TRAILER case\n", this)); |
577 | 0 |
|
578 | 0 | MOZ_ASSERT_UNREACHABLE("Should stop getting data after entering state " |
579 | 0 | "JPEG_SINK_NON_JPEG_TRAILER"); |
580 | 0 |
|
581 | 0 | return Transition::TerminateSuccess(); |
582 | 0 |
|
583 | 0 | case JPEG_ERROR: |
584 | 0 | MOZ_ASSERT_UNREACHABLE("Should stop getting data after entering state " |
585 | 0 | "JPEG_ERROR"); |
586 | 0 |
|
587 | 0 | return Transition::TerminateFailure(); |
588 | 0 | } |
589 | 0 |
|
590 | 0 | MOZ_ASSERT_UNREACHABLE("Escaped the JPEG decoder state machine"); |
591 | 0 | return Transition::TerminateFailure(); |
592 | 0 | } |
593 | | |
594 | | LexerTransition<nsJPEGDecoder::State> |
595 | | nsJPEGDecoder::FinishedJPEGData() |
596 | 0 | { |
597 | 0 | // Since we set up an unbuffered read for SIZE_MAX bytes, if we actually read |
598 | 0 | // all that data something is really wrong. |
599 | 0 | MOZ_ASSERT_UNREACHABLE("Read the entire address space?"); |
600 | 0 | return Transition::TerminateFailure(); |
601 | 0 | } |
602 | | |
603 | | Orientation |
604 | | nsJPEGDecoder::ReadOrientationFromEXIF() |
605 | 0 | { |
606 | 0 | jpeg_saved_marker_ptr marker; |
607 | 0 |
|
608 | 0 | // Locate the APP1 marker, where EXIF data is stored, in the marker list. |
609 | 0 | for (marker = mInfo.marker_list ; marker != nullptr ; marker = marker->next) { |
610 | 0 | if (marker->marker == JPEG_APP0 + 1) { |
611 | 0 | break; |
612 | 0 | } |
613 | 0 | } |
614 | 0 |
|
615 | 0 | // If we're at the end of the list, there's no EXIF data. |
616 | 0 | if (!marker) { |
617 | 0 | return Orientation(); |
618 | 0 | } |
619 | 0 | |
620 | 0 | // Extract the orientation information. |
621 | 0 | EXIFData exif = EXIFParser::Parse(marker->data, |
622 | 0 | static_cast<uint32_t>(marker->data_length)); |
623 | 0 | return exif.orientation; |
624 | 0 | } |
625 | | |
626 | | void |
627 | | nsJPEGDecoder::NotifyDone() |
628 | 0 | { |
629 | 0 | PostFrameStop(Opacity::FULLY_OPAQUE); |
630 | 0 | PostDecodeDone(); |
631 | 0 | } |
632 | | |
633 | | void |
634 | | nsJPEGDecoder::FinishRow(uint32_t aLastSourceRow) |
635 | 0 | { |
636 | 0 | if (mDownscaler) { |
637 | 0 | mDownscaler->CommitRow(); |
638 | 0 | if (mDownscaler->HasInvalidation()) { |
639 | 0 | DownscalerInvalidRect invalidRect = mDownscaler->TakeInvalidRect(); |
640 | 0 | PostInvalidation(invalidRect.mOriginalSizeRect, |
641 | 0 | Some(invalidRect.mTargetSizeRect)); |
642 | 0 | MOZ_ASSERT(!mDownscaler->HasInvalidation()); |
643 | 0 | } |
644 | 0 | } else if (aLastSourceRow != mInfo.output_scanline) { |
645 | 0 | PostInvalidation(nsIntRect(0, aLastSourceRow, |
646 | 0 | mInfo.output_width, |
647 | 0 | mInfo.output_scanline - aLastSourceRow)); |
648 | 0 | } |
649 | 0 | } |
650 | | |
651 | | void |
652 | | nsJPEGDecoder::OutputScanlines(bool* suspend) |
653 | 0 | { |
654 | 0 | *suspend = false; |
655 | 0 |
|
656 | 0 | while ((mInfo.output_scanline < mInfo.output_height)) { |
657 | 0 | const uint32_t top = mInfo.output_scanline; |
658 | 0 | uint32_t* imageRow = nullptr; |
659 | 0 | if (mDownscaler) { |
660 | 0 | imageRow = reinterpret_cast<uint32_t*>(mDownscaler->RowBuffer()); |
661 | 0 | } else { |
662 | 0 | imageRow = reinterpret_cast<uint32_t*>(mImageData) + |
663 | 0 | (mInfo.output_scanline * mInfo.output_width); |
664 | 0 | } |
665 | 0 |
|
666 | 0 | MOZ_ASSERT(imageRow, "Should have a row buffer here"); |
667 | 0 |
|
668 | 0 | if (mInfo.out_color_space == MOZ_JCS_EXT_NATIVE_ENDIAN_XRGB) { |
669 | 0 | // Special case: scanline will be directly converted into packed ARGB |
670 | 0 | if (jpeg_read_scanlines(&mInfo, (JSAMPARRAY)&imageRow, 1) != 1) { |
671 | 0 | *suspend = true; // suspend |
672 | 0 | break; |
673 | 0 | } |
674 | 0 | FinishRow(top); |
675 | 0 | continue; // all done for this row! |
676 | 0 | } |
677 | 0 | |
678 | 0 | JSAMPROW sampleRow = (JSAMPROW)imageRow; |
679 | 0 | if (mInfo.output_components == 3) { |
680 | 0 | // Put the pixels at end of row to enable in-place expansion |
681 | 0 | sampleRow += mInfo.output_width; |
682 | 0 | } |
683 | 0 |
|
684 | 0 | // Request one scanline. Returns 0 or 1 scanlines. |
685 | 0 | if (jpeg_read_scanlines(&mInfo, &sampleRow, 1) != 1) { |
686 | 0 | *suspend = true; // suspend |
687 | 0 | break; |
688 | 0 | } |
689 | 0 | |
690 | 0 | if (mTransform) { |
691 | 0 | JSAMPROW source = sampleRow; |
692 | 0 | if (mInfo.out_color_space == JCS_GRAYSCALE) { |
693 | 0 | // Convert from the 1byte grey pixels at begin of row |
694 | 0 | // to the 3byte RGB byte pixels at 'end' of row |
695 | 0 | sampleRow += mInfo.output_width; |
696 | 0 | } |
697 | 0 | qcms_transform_data(mTransform, source, sampleRow, mInfo.output_width); |
698 | 0 | // Move 3byte RGB data to end of row |
699 | 0 | if (mInfo.out_color_space == JCS_CMYK) { |
700 | 0 | memmove(sampleRow + mInfo.output_width, |
701 | 0 | sampleRow, |
702 | 0 | 3 * mInfo.output_width); |
703 | 0 | sampleRow += mInfo.output_width; |
704 | 0 | } |
705 | 0 | } else { |
706 | 0 | if (mInfo.out_color_space == JCS_CMYK) { |
707 | 0 | // Convert from CMYK to RGB |
708 | 0 | // We cannot convert directly to Cairo, as the CMSRGBTransform |
709 | 0 | // may wants to do a RGB transform... |
710 | 0 | // Would be better to have platform CMSenabled transformation |
711 | 0 | // from CMYK to (A)RGB... |
712 | 0 | cmyk_convert_rgb((JSAMPROW)imageRow, mInfo.output_width); |
713 | 0 | sampleRow += mInfo.output_width; |
714 | 0 | } |
715 | 0 | if (mCMSMode == eCMSMode_All) { |
716 | 0 | // No embedded ICC profile - treat as sRGB |
717 | 0 | qcms_transform* transform = gfxPlatform::GetCMSRGBTransform(); |
718 | 0 | if (transform) { |
719 | 0 | qcms_transform_data(transform, sampleRow, sampleRow, |
720 | 0 | mInfo.output_width); |
721 | 0 | } |
722 | 0 | } |
723 | 0 | } |
724 | 0 |
|
725 | 0 | // counter for while() loops below |
726 | 0 | uint32_t idx = mInfo.output_width; |
727 | 0 |
|
728 | 0 | // copy as bytes until source pointer is 32-bit-aligned |
729 | 0 | for (; (NS_PTR_TO_UINT32(sampleRow) & 0x3) && idx; --idx) { |
730 | 0 | *imageRow++ = gfxPackedPixel(0xFF, sampleRow[0], sampleRow[1], |
731 | 0 | sampleRow[2]); |
732 | 0 | sampleRow += 3; |
733 | 0 | } |
734 | 0 |
|
735 | 0 | // copy pixels in blocks of 4 |
736 | 0 | while (idx >= 4) { |
737 | 0 | GFX_BLOCK_RGB_TO_FRGB(sampleRow, imageRow); |
738 | 0 | idx -= 4; |
739 | 0 | sampleRow += 12; |
740 | 0 | imageRow += 4; |
741 | 0 | } |
742 | 0 |
|
743 | 0 | // copy remaining pixel(s) |
744 | 0 | while (idx--) { |
745 | 0 | // 32-bit read of final pixel will exceed buffer, so read bytes |
746 | 0 | *imageRow++ = gfxPackedPixel(0xFF, sampleRow[0], sampleRow[1], |
747 | 0 | sampleRow[2]); |
748 | 0 | sampleRow += 3; |
749 | 0 | } |
750 | 0 |
|
751 | 0 | FinishRow(top); |
752 | 0 | } |
753 | 0 | } |
754 | | |
755 | | // Override the standard error method in the IJG JPEG decoder code. |
756 | | METHODDEF(void) |
757 | | my_error_exit (j_common_ptr cinfo) |
758 | 0 | { |
759 | 0 | decoder_error_mgr* err = (decoder_error_mgr*) cinfo->err; |
760 | 0 |
|
761 | 0 | // Convert error to a browser error code |
762 | 0 | nsresult error_code = err->pub.msg_code == JERR_OUT_OF_MEMORY |
763 | 0 | ? NS_ERROR_OUT_OF_MEMORY |
764 | 0 | : NS_ERROR_FAILURE; |
765 | 0 |
|
766 | | #ifdef DEBUG |
767 | | char buffer[JMSG_LENGTH_MAX]; |
768 | | |
769 | | // Create the message |
770 | | (*err->pub.format_message) (cinfo, buffer); |
771 | | |
772 | | fprintf(stderr, "JPEG decoding error:\n%s\n", buffer); |
773 | | #endif |
774 | |
|
775 | 0 | // Return control to the setjmp point. We pass an nsresult masquerading as |
776 | 0 | // an int, which works because the setjmp() caller casts it back. |
777 | 0 | longjmp(err->setjmp_buffer, static_cast<int>(error_code)); |
778 | 0 | } |
779 | | |
780 | | /******************************************************************************* |
781 | | * This is the callback routine from the IJG JPEG library used to supply new |
782 | | * data to the decompressor when its input buffer is exhausted. It juggles |
783 | | * multiple buffers in an attempt to avoid unnecessary copying of input data. |
784 | | * |
785 | | * (A simpler scheme is possible: It's much easier to use only a single |
786 | | * buffer; when fill_input_buffer() is called, move any unconsumed data |
787 | | * (beyond the current pointer/count) down to the beginning of this buffer and |
788 | | * then load new data into the remaining buffer space. This approach requires |
789 | | * a little more data copying but is far easier to get right.) |
790 | | * |
791 | | * At any one time, the JPEG decompressor is either reading from the necko |
792 | | * input buffer, which is volatile across top-level calls to the IJG library, |
793 | | * or the "backtrack" buffer. The backtrack buffer contains the remaining |
794 | | * unconsumed data from the necko buffer after parsing was suspended due |
795 | | * to insufficient data in some previous call to the IJG library. |
796 | | * |
797 | | * When suspending, the decompressor will back up to a convenient restart |
798 | | * point (typically the start of the current MCU). The variables |
799 | | * next_input_byte & bytes_in_buffer indicate where the restart point will be |
800 | | * if the current call returns FALSE. Data beyond this point must be |
801 | | * rescanned after resumption, so it must be preserved in case the decompressor |
802 | | * decides to backtrack. |
803 | | * |
804 | | * Returns: |
805 | | * TRUE if additional data is available, FALSE if no data present and |
806 | | * the JPEG library should therefore suspend processing of input stream |
807 | | ******************************************************************************/ |
808 | | |
809 | | /******************************************************************************/ |
810 | | /* data source manager method */ |
811 | | /******************************************************************************/ |
812 | | |
813 | | /******************************************************************************/ |
814 | | /* data source manager method |
815 | | Initialize source. This is called by jpeg_read_header() before any |
816 | | data is actually read. May leave |
817 | | bytes_in_buffer set to 0 (in which case a fill_input_buffer() call |
818 | | will occur immediately). |
819 | | */ |
820 | | METHODDEF(void) |
821 | | init_source (j_decompress_ptr jd) |
822 | 0 | { |
823 | 0 | } |
824 | | |
825 | | /******************************************************************************/ |
826 | | /* data source manager method |
827 | | Skip num_bytes worth of data. The buffer pointer and count should |
828 | | be advanced over num_bytes input bytes, refilling the buffer as |
829 | | needed. This is used to skip over a potentially large amount of |
830 | | uninteresting data (such as an APPn marker). In some applications |
831 | | it may be possible to optimize away the reading of the skipped data, |
832 | | but it's not clear that being smart is worth much trouble; large |
833 | | skips are uncommon. bytes_in_buffer may be zero on return. |
834 | | A zero or negative skip count should be treated as a no-op. |
835 | | */ |
836 | | METHODDEF(void) |
837 | | skip_input_data (j_decompress_ptr jd, long num_bytes) |
838 | 0 | { |
839 | 0 | struct jpeg_source_mgr* src = jd->src; |
840 | 0 | nsJPEGDecoder* decoder = (nsJPEGDecoder*)(jd->client_data); |
841 | 0 |
|
842 | 0 | if (num_bytes > (long)src->bytes_in_buffer) { |
843 | 0 | // Can't skip it all right now until we get more data from |
844 | 0 | // network stream. Set things up so that fill_input_buffer |
845 | 0 | // will skip remaining amount. |
846 | 0 | decoder->mBytesToSkip = (size_t)num_bytes - src->bytes_in_buffer; |
847 | 0 | src->next_input_byte += src->bytes_in_buffer; |
848 | 0 | src->bytes_in_buffer = 0; |
849 | 0 |
|
850 | 0 | } else { |
851 | 0 | // Simple case. Just advance buffer pointer |
852 | 0 |
|
853 | 0 | src->bytes_in_buffer -= (size_t)num_bytes; |
854 | 0 | src->next_input_byte += num_bytes; |
855 | 0 | } |
856 | 0 | } |
857 | | |
858 | | /******************************************************************************/ |
859 | | /* data source manager method |
860 | | This is called whenever bytes_in_buffer has reached zero and more |
861 | | data is wanted. In typical applications, it should read fresh data |
862 | | into the buffer (ignoring the current state of next_input_byte and |
863 | | bytes_in_buffer), reset the pointer & count to the start of the |
864 | | buffer, and return TRUE indicating that the buffer has been reloaded. |
865 | | It is not necessary to fill the buffer entirely, only to obtain at |
866 | | least one more byte. bytes_in_buffer MUST be set to a positive value |
867 | | if TRUE is returned. A FALSE return should only be used when I/O |
868 | | suspension is desired. |
869 | | */ |
870 | | METHODDEF(boolean) |
871 | | fill_input_buffer (j_decompress_ptr jd) |
872 | 0 | { |
873 | 0 | struct jpeg_source_mgr* src = jd->src; |
874 | 0 | nsJPEGDecoder* decoder = (nsJPEGDecoder*)(jd->client_data); |
875 | 0 |
|
876 | 0 | if (decoder->mReading) { |
877 | 0 | const JOCTET* new_buffer = decoder->mSegment; |
878 | 0 | uint32_t new_buflen = decoder->mSegmentLen; |
879 | 0 |
|
880 | 0 | if (!new_buffer || new_buflen == 0) { |
881 | 0 | return false; // suspend |
882 | 0 | } |
883 | 0 | |
884 | 0 | decoder->mSegmentLen = 0; |
885 | 0 |
|
886 | 0 | if (decoder->mBytesToSkip) { |
887 | 0 | if (decoder->mBytesToSkip < new_buflen) { |
888 | 0 | // All done skipping bytes; Return what's left. |
889 | 0 | new_buffer += decoder->mBytesToSkip; |
890 | 0 | new_buflen -= decoder->mBytesToSkip; |
891 | 0 | decoder->mBytesToSkip = 0; |
892 | 0 | } else { |
893 | 0 | // Still need to skip some more data in the future |
894 | 0 | decoder->mBytesToSkip -= (size_t)new_buflen; |
895 | 0 | return false; // suspend |
896 | 0 | } |
897 | 0 | } |
898 | 0 | |
899 | 0 | decoder->mBackBufferUnreadLen = src->bytes_in_buffer; |
900 | 0 |
|
901 | 0 | src->next_input_byte = new_buffer; |
902 | 0 | src->bytes_in_buffer = (size_t)new_buflen; |
903 | 0 | decoder->mReading = false; |
904 | 0 |
|
905 | 0 | return true; |
906 | 0 | } |
907 | 0 | |
908 | 0 | if (src->next_input_byte != decoder->mSegment) { |
909 | 0 | // Backtrack data has been permanently consumed. |
910 | 0 | decoder->mBackBufferUnreadLen = 0; |
911 | 0 | decoder->mBackBufferLen = 0; |
912 | 0 | } |
913 | 0 |
|
914 | 0 | // Save remainder of netlib buffer in backtrack buffer |
915 | 0 | const uint32_t new_backtrack_buflen = src->bytes_in_buffer + |
916 | 0 | decoder->mBackBufferLen; |
917 | 0 |
|
918 | 0 | // Make sure backtrack buffer is big enough to hold new data. |
919 | 0 | if (decoder->mBackBufferSize < new_backtrack_buflen) { |
920 | 0 | // Check for malformed MARKER segment lengths, before allocating space |
921 | 0 | // for it |
922 | 0 | if (new_backtrack_buflen > MAX_JPEG_MARKER_LENGTH) { |
923 | 0 | my_error_exit((j_common_ptr)(&decoder->mInfo)); |
924 | 0 | } |
925 | 0 |
|
926 | 0 | // Round up to multiple of 256 bytes. |
927 | 0 | const size_t roundup_buflen = ((new_backtrack_buflen + 255) >> 8) << 8; |
928 | 0 | JOCTET* buf = (JOCTET*) realloc(decoder->mBackBuffer, roundup_buflen); |
929 | 0 | // Check for OOM |
930 | 0 | if (!buf) { |
931 | 0 | decoder->mInfo.err->msg_code = JERR_OUT_OF_MEMORY; |
932 | 0 | my_error_exit((j_common_ptr)(&decoder->mInfo)); |
933 | 0 | } |
934 | 0 | decoder->mBackBuffer = buf; |
935 | 0 | decoder->mBackBufferSize = roundup_buflen; |
936 | 0 | } |
937 | 0 |
|
938 | 0 | // Ensure we actually have a backtrack buffer. Without it, then we know that |
939 | 0 | // there is no data to copy and bytes_in_buffer is already zero. |
940 | 0 | if (decoder->mBackBuffer) { |
941 | 0 | // Copy remainder of netlib segment into backtrack buffer. |
942 | 0 | memmove(decoder->mBackBuffer + decoder->mBackBufferLen, |
943 | 0 | src->next_input_byte, |
944 | 0 | src->bytes_in_buffer); |
945 | 0 | } else { |
946 | 0 | MOZ_ASSERT(src->bytes_in_buffer == 0); |
947 | 0 | MOZ_ASSERT(decoder->mBackBufferLen == 0); |
948 | 0 | MOZ_ASSERT(decoder->mBackBufferUnreadLen == 0); |
949 | 0 | } |
950 | 0 |
|
951 | 0 | // Point to start of data to be rescanned. |
952 | 0 | src->next_input_byte = decoder->mBackBuffer + decoder->mBackBufferLen - |
953 | 0 | decoder->mBackBufferUnreadLen; |
954 | 0 | src->bytes_in_buffer += decoder->mBackBufferUnreadLen; |
955 | 0 | decoder->mBackBufferLen = (size_t)new_backtrack_buflen; |
956 | 0 | decoder->mReading = true; |
957 | 0 |
|
958 | 0 | return false; |
959 | 0 | } |
960 | | |
961 | | /******************************************************************************/ |
962 | | /* data source manager method */ |
963 | | /* |
964 | | * Terminate source --- called by jpeg_finish_decompress() after all |
965 | | * data has been read to clean up JPEG source manager. NOT called by |
966 | | * jpeg_abort() or jpeg_destroy(). |
967 | | */ |
968 | | METHODDEF(void) |
969 | | term_source (j_decompress_ptr jd) |
970 | 0 | { |
971 | 0 | nsJPEGDecoder* decoder = (nsJPEGDecoder*)(jd->client_data); |
972 | 0 |
|
973 | 0 | // This function shouldn't be called if we ran into an error we didn't |
974 | 0 | // recover from. |
975 | 0 | MOZ_ASSERT(decoder->mState != JPEG_ERROR, |
976 | 0 | "Calling term_source on a JPEG with mState == JPEG_ERROR!"); |
977 | 0 |
|
978 | 0 | // Notify using a helper method to get around protectedness issues. |
979 | 0 | decoder->NotifyDone(); |
980 | 0 | } |
981 | | |
982 | | } // namespace image |
983 | | } // namespace mozilla |
984 | | |
985 | | ///*************** Inverted CMYK -> RGB conversion ************************* |
986 | | /// Input is (Inverted) CMYK stored as 4 bytes per pixel. |
987 | | /// Output is RGB stored as 3 bytes per pixel. |
988 | | /// @param row Points to row buffer containing the CMYK bytes for each pixel |
989 | | /// in the row. |
990 | | /// @param width Number of pixels in the row. |
991 | | static void cmyk_convert_rgb(JSAMPROW row, JDIMENSION width) |
992 | 0 | { |
993 | 0 | // Work from end to front to shrink from 4 bytes per pixel to 3 |
994 | 0 | JSAMPROW in = row + width*4; |
995 | 0 | JSAMPROW out = in; |
996 | 0 |
|
997 | 0 | for (uint32_t i = width; i > 0; i--) { |
998 | 0 | in -= 4; |
999 | 0 | out -= 3; |
1000 | 0 |
|
1001 | 0 | // Source is 'Inverted CMYK', output is RGB. |
1002 | 0 | // See: http://www.easyrgb.com/math.php?MATH=M12#text12 |
1003 | 0 | // Or: http://www.ilkeratalay.com/colorspacesfaq.php#rgb |
1004 | 0 |
|
1005 | 0 | // From CMYK to CMY |
1006 | 0 | // C = ( C * ( 1 - K ) + K ) |
1007 | 0 | // M = ( M * ( 1 - K ) + K ) |
1008 | 0 | // Y = ( Y * ( 1 - K ) + K ) |
1009 | 0 |
|
1010 | 0 | // From Inverted CMYK to CMY is thus: |
1011 | 0 | // C = ( (1-iC) * (1 - (1-iK)) + (1-iK) ) => 1 - iC*iK |
1012 | 0 | // Same for M and Y |
1013 | 0 |
|
1014 | 0 | // Convert from CMY (0..1) to RGB (0..1) |
1015 | 0 | // R = 1 - C => 1 - (1 - iC*iK) => iC*iK |
1016 | 0 | // G = 1 - M => 1 - (1 - iM*iK) => iM*iK |
1017 | 0 | // B = 1 - Y => 1 - (1 - iY*iK) => iY*iK |
1018 | 0 |
|
1019 | 0 | // Convert from Inverted CMYK (0..255) to RGB (0..255) |
1020 | 0 | const uint32_t iC = in[0]; |
1021 | 0 | const uint32_t iM = in[1]; |
1022 | 0 | const uint32_t iY = in[2]; |
1023 | 0 | const uint32_t iK = in[3]; |
1024 | 0 | out[0] = iC*iK/255; // Red |
1025 | 0 | out[1] = iM*iK/255; // Green |
1026 | 0 | out[2] = iY*iK/255; // Blue |
1027 | 0 | } |
1028 | 0 | } |