2001-06-20 00:53:26 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
2012-05-21 11:12:37 +00:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2001-06-20 00:53:26 +00:00
|
|
|
|
2012-10-01 12:16:03 +00:00
|
|
|
#include "ImageLogging.h"
|
2012-04-03 21:57:22 +00:00
|
|
|
#include "nsJPEGDecoder.h"
|
2013-08-25 07:19:43 +00:00
|
|
|
#include "Orientation.h"
|
|
|
|
#include "EXIF.h"
|
2001-06-20 00:53:26 +00:00
|
|
|
|
|
|
|
#include "nsIInputStream.h"
|
|
|
|
|
|
|
|
#include "nspr.h"
|
|
|
|
#include "nsCRT.h"
|
2007-04-29 15:51:35 +00:00
|
|
|
#include "gfxColor.h"
|
2001-06-20 00:53:26 +00:00
|
|
|
|
2002-07-04 20:31:18 +00:00
|
|
|
#include "jerror.h"
|
|
|
|
|
2007-07-23 22:02:17 +00:00
|
|
|
#include "gfxPlatform.h"
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include "iccjpeg.h"
|
|
|
|
}
|
|
|
|
|
2012-09-14 19:54:55 +00:00
|
|
|
#if defined(IS_BIG_ENDIAN)
|
|
|
|
#define MOZ_JCS_EXT_NATIVE_ENDIAN_XRGB JCS_EXT_XRGB
|
|
|
|
#else
|
|
|
|
#define MOZ_JCS_EXT_NATIVE_ENDIAN_XRGB JCS_EXT_BGRX
|
|
|
|
#endif
|
|
|
|
|
2010-08-23 02:30:46 +00:00
|
|
|
static void cmyk_convert_rgb(JSAMPROW row, JDIMENSION width);
|
|
|
|
|
|
|
|
namespace mozilla {
|
2012-01-06 16:02:27 +00:00
|
|
|
namespace image {
|
2001-06-20 00:53:26 +00:00
|
|
|
|
|
|
|
#if defined(PR_LOGGING)
|
2012-10-29 23:32:10 +00:00
|
|
|
static PRLogModuleInfo *
|
|
|
|
GetJPEGLog()
|
|
|
|
{
|
|
|
|
static PRLogModuleInfo *sJPEGLog;
|
|
|
|
if (!sJPEGLog)
|
|
|
|
sJPEGLog = PR_NewLogModule("JPEGDecoder");
|
|
|
|
return sJPEGLog;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PRLogModuleInfo *
|
|
|
|
GetJPEGDecoderAccountingLog()
|
|
|
|
{
|
|
|
|
static PRLogModuleInfo *sJPEGDecoderAccountingLog;
|
|
|
|
if (!sJPEGDecoderAccountingLog)
|
|
|
|
sJPEGDecoderAccountingLog = PR_NewLogModule("JPEGDecoderAccounting");
|
|
|
|
return sJPEGDecoderAccountingLog;
|
|
|
|
}
|
2001-06-20 00:53:26 +00:00
|
|
|
#else
|
2012-10-29 23:32:10 +00:00
|
|
|
#define GetJPEGLog()
|
|
|
|
#define GetJPEGDecoderAccountingLog()
|
2001-06-20 00:53:26 +00:00
|
|
|
#endif
|
|
|
|
|
2011-03-23 16:20:43 +00:00
|
|
|
static qcms_profile*
|
|
|
|
GetICCProfile(struct jpeg_decompress_struct &info)
|
|
|
|
{
|
|
|
|
JOCTET* profilebuf;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t profileLength;
|
2012-07-30 14:20:58 +00:00
|
|
|
qcms_profile* profile = nullptr;
|
2011-03-23 16:20:43 +00:00
|
|
|
|
|
|
|
if (read_icc_profile(&info, &profilebuf, &profileLength)) {
|
2011-03-24 01:54:04 +00:00
|
|
|
profile = qcms_profile_from_memory(profilebuf, profileLength);
|
2011-03-23 16:20:43 +00:00
|
|
|
free(profilebuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
return profile;
|
|
|
|
}
|
2001-06-20 00:53:26 +00:00
|
|
|
|
2003-09-18 20:38:36 +00:00
|
|
|
METHODDEF(void) init_source (j_decompress_ptr jd);
|
|
|
|
METHODDEF(boolean) fill_input_buffer (j_decompress_ptr jd);
|
|
|
|
METHODDEF(void) skip_input_data (j_decompress_ptr jd, long num_bytes);
|
|
|
|
METHODDEF(void) term_source (j_decompress_ptr jd);
|
|
|
|
METHODDEF(void) my_error_exit (j_common_ptr cinfo);
|
2001-06-20 00:53:26 +00:00
|
|
|
|
|
|
|
/* Normal JFIF markers can't have more bytes than this. */
|
2012-08-22 15:56:38 +00:00
|
|
|
#define MAX_JPEG_MARKER_LENGTH (((uint32_t)1 << 16) - 1)
|
2001-06-20 00:53:26 +00:00
|
|
|
|
|
|
|
|
2013-01-18 21:47:18 +00:00
|
|
|
nsJPEGDecoder::nsJPEGDecoder(RasterImage& aImage, Decoder::DecodeStyle aDecodeStyle)
|
|
|
|
: Decoder(aImage)
|
2012-11-19 01:18:52 +00:00
|
|
|
, mDecodeStyle(aDecodeStyle)
|
2001-06-20 00:53:26 +00:00
|
|
|
{
|
|
|
|
mState = JPEG_HEADER;
|
2011-10-17 14:59:28 +00:00
|
|
|
mReading = true;
|
2012-07-30 14:20:58 +00:00
|
|
|
mImageData = nullptr;
|
2001-06-20 00:53:26 +00:00
|
|
|
|
|
|
|
mBytesToSkip = 0;
|
|
|
|
memset(&mInfo, 0, sizeof(jpeg_decompress_struct));
|
2007-01-04 20:03:50 +00:00
|
|
|
memset(&mSourceMgr, 0, sizeof(mSourceMgr));
|
2007-01-31 14:53:21 +00:00
|
|
|
mInfo.client_data = (void*)this;
|
2001-06-20 00:53:26 +00:00
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
mSegment = nullptr;
|
2008-01-24 15:59:32 +00:00
|
|
|
mSegmentLen = 0;
|
2001-06-20 00:53:26 +00:00
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
mBackBuffer = nullptr;
|
2001-06-20 00:53:26 +00:00
|
|
|
mBackBufferLen = mBackBufferSize = mBackBufferUnreadLen = 0;
|
2007-07-23 22:02:17 +00:00
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
mInProfile = nullptr;
|
|
|
|
mTransform = nullptr;
|
2007-10-19 00:36:34 +00:00
|
|
|
|
2011-01-13 01:45:13 +00:00
|
|
|
mCMSMode = 0;
|
|
|
|
|
2012-10-29 23:32:10 +00:00
|
|
|
PR_LOG(GetJPEGDecoderAccountingLog(), PR_LOG_DEBUG,
|
2007-10-19 00:36:34 +00:00
|
|
|
("nsJPEGDecoder::nsJPEGDecoder: Creating JPEG decoder %p",
|
|
|
|
this));
|
2001-06-20 00:53:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsJPEGDecoder::~nsJPEGDecoder()
|
|
|
|
{
|
2010-08-23 02:30:46 +00:00
|
|
|
// Step 8: Release JPEG decompression object
|
2012-07-30 14:20:58 +00:00
|
|
|
mInfo.src = nullptr;
|
2010-08-23 02:30:46 +00:00
|
|
|
jpeg_destroy_decompress(&mInfo);
|
|
|
|
|
2007-01-04 20:03:50 +00:00
|
|
|
PR_FREEIF(mBackBuffer);
|
2007-07-23 22:02:17 +00:00
|
|
|
if (mTransform)
|
2009-04-07 16:02:11 +00:00
|
|
|
qcms_transform_release(mTransform);
|
2007-07-23 22:02:17 +00:00
|
|
|
if (mInProfile)
|
2009-04-07 16:02:11 +00:00
|
|
|
qcms_profile_release(mInProfile);
|
2007-10-19 00:36:34 +00:00
|
|
|
|
2012-10-29 23:32:10 +00:00
|
|
|
PR_LOG(GetJPEGDecoderAccountingLog(), PR_LOG_DEBUG,
|
2007-10-19 00:36:34 +00:00
|
|
|
("nsJPEGDecoder::~nsJPEGDecoder: Destroying JPEG decoder %p",
|
|
|
|
this));
|
2001-06-20 00:53:26 +00:00
|
|
|
}
|
|
|
|
|
2011-09-22 20:25:56 +00:00
|
|
|
Telemetry::ID
|
|
|
|
nsJPEGDecoder::SpeedHistogram()
|
|
|
|
{
|
|
|
|
return Telemetry::IMAGE_DECODE_SPEED_JPEG;
|
|
|
|
}
|
2001-06-20 00:53:26 +00:00
|
|
|
|
2010-09-12 15:22:30 +00:00
|
|
|
void
|
2010-08-23 02:30:46 +00:00
|
|
|
nsJPEGDecoder::InitInternal()
|
2001-06-20 00:53:26 +00:00
|
|
|
{
|
2011-01-13 01:45:13 +00:00
|
|
|
mCMSMode = gfxPlatform::GetCMSMode();
|
|
|
|
if ((mDecodeFlags & DECODER_NO_COLORSPACE_CONVERSION) != 0)
|
|
|
|
mCMSMode = eCMSMode_Off;
|
|
|
|
|
2001-06-20 00:53:26 +00:00
|
|
|
/* We set up the normal JPEG error routines, then override error_exit. */
|
|
|
|
mInfo.err = jpeg_std_error(&mErr.pub);
|
|
|
|
/* mInfo.err = jpeg_std_error(&mErr.pub); */
|
|
|
|
mErr.pub.error_exit = my_error_exit;
|
|
|
|
/* Establish the setjmp return context for my_error_exit to use. */
|
|
|
|
if (setjmp(mErr.setjmp_buffer)) {
|
|
|
|
/* If we get here, the JPEG code has signaled an error.
|
|
|
|
* We need to clean up the JPEG object, close the input file, and return.
|
|
|
|
*/
|
2010-09-12 15:22:27 +00:00
|
|
|
PostDecoderError(NS_ERROR_FAILURE);
|
2010-09-12 15:22:30 +00:00
|
|
|
return;
|
2001-06-20 00:53:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Step 1: allocate and initialize JPEG decompression object */
|
|
|
|
jpeg_create_decompress(&mInfo);
|
2007-01-04 20:03:50 +00:00
|
|
|
/* Set the source manager */
|
|
|
|
mInfo.src = &mSourceMgr;
|
2001-06-20 00:53:26 +00:00
|
|
|
|
|
|
|
/* Step 2: specify data source (eg, a file) */
|
|
|
|
|
|
|
|
/* Setup callback functions. */
|
2007-01-04 20:03:50 +00:00
|
|
|
mSourceMgr.init_source = init_source;
|
|
|
|
mSourceMgr.fill_input_buffer = fill_input_buffer;
|
|
|
|
mSourceMgr.skip_input_data = skip_input_data;
|
|
|
|
mSourceMgr.resync_to_restart = jpeg_resync_to_restart;
|
|
|
|
mSourceMgr.term_source = term_source;
|
2001-06-20 00:53:26 +00:00
|
|
|
|
2011-11-08 18:24:35 +00:00
|
|
|
/* Record app markers for ICC data */
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t m = 0; m < 16; m++)
|
2007-07-23 22:02:17 +00:00
|
|
|
jpeg_save_markers(&mInfo, JPEG_APP0 + m, 0xFFFF);
|
2001-06-20 00:53:26 +00:00
|
|
|
}
|
|
|
|
|
2010-09-12 15:22:30 +00:00
|
|
|
void
|
2010-08-23 02:30:46 +00:00
|
|
|
nsJPEGDecoder::FinishInternal()
|
2001-06-20 00:53:26 +00:00
|
|
|
{
|
2010-08-23 02:30:45 +00:00
|
|
|
/* If we're not in any sort of error case, flush the decoder.
|
|
|
|
*
|
|
|
|
* XXXbholley - It seems wrong that this should be necessary, but at the
|
|
|
|
* moment I'm just folding the contents of Flush() into Close() so that
|
|
|
|
* we can get rid of it.
|
|
|
|
*/
|
|
|
|
if ((mState != JPEG_DONE && mState != JPEG_SINK_NON_JPEG_TRAILER) &&
|
|
|
|
(mState != JPEG_ERROR) &&
|
2010-08-23 02:30:46 +00:00
|
|
|
!IsSizeDecode())
|
2012-07-30 14:20:58 +00:00
|
|
|
this->Write(nullptr, 0);
|
2001-06-20 00:53:26 +00:00
|
|
|
}
|
|
|
|
|
2010-09-12 15:22:30 +00:00
|
|
|
void
|
2012-08-22 15:56:38 +00:00
|
|
|
nsJPEGDecoder::WriteInternal(const char *aBuffer, uint32_t aCount)
|
2008-01-24 15:59:32 +00:00
|
|
|
{
|
2009-10-16 02:54:44 +00:00
|
|
|
mSegment = (const JOCTET *)aBuffer;
|
|
|
|
mSegmentLen = aCount;
|
2001-06-20 00:53:26 +00:00
|
|
|
|
2010-09-12 15:22:31 +00:00
|
|
|
NS_ABORT_IF_FALSE(!HasError(), "Shouldn't call WriteInternal after error!");
|
2010-09-12 15:22:31 +00:00
|
|
|
|
2009-10-16 02:54:44 +00:00
|
|
|
/* Return here if there is a fatal error within libjpeg. */
|
2002-07-04 20:31:18 +00:00
|
|
|
nsresult error_code;
|
2012-08-07 17:17:27 +00:00
|
|
|
// This cast to nsresult makes sense because setjmp() returns whatever we
|
|
|
|
// passed to longjmp(), which was actually an nsresult.
|
|
|
|
if ((error_code = (nsresult)setjmp(mErr.setjmp_buffer)) != NS_OK) {
|
2002-07-04 20:31:18 +00:00
|
|
|
if (error_code == NS_ERROR_FAILURE) {
|
2010-09-12 15:22:27 +00:00
|
|
|
PostDataError();
|
2009-10-16 02:54:44 +00:00
|
|
|
/* Error due to corrupt stream - return NS_OK and consume silently
|
|
|
|
so that libpr0n doesn't throw away a partial image load */
|
|
|
|
mState = JPEG_SINK_NON_JPEG_TRAILER;
|
2012-10-29 23:32:10 +00:00
|
|
|
PR_LOG(GetJPEGDecoderAccountingLog(), PR_LOG_DEBUG,
|
2007-10-19 00:36:34 +00:00
|
|
|
("} (setjmp returned NS_ERROR_FAILURE)"));
|
2010-09-12 15:22:30 +00:00
|
|
|
return;
|
2002-07-04 20:31:18 +00:00
|
|
|
} else {
|
|
|
|
/* Error due to reasons external to the stream (probably out of
|
|
|
|
memory) - let libpr0n attempt to clean up, even though
|
|
|
|
mozilla is seconds away from falling flat on its face. */
|
2010-09-12 15:22:27 +00:00
|
|
|
PostDecoderError(error_code);
|
2009-10-16 02:54:44 +00:00
|
|
|
mState = JPEG_ERROR;
|
2012-10-29 23:32:10 +00:00
|
|
|
PR_LOG(GetJPEGDecoderAccountingLog(), PR_LOG_DEBUG,
|
2007-10-19 00:36:34 +00:00
|
|
|
("} (setjmp returned an error)"));
|
2010-09-12 15:22:30 +00:00
|
|
|
return;
|
2002-07-04 20:31:18 +00:00
|
|
|
}
|
2001-06-20 00:53:26 +00:00
|
|
|
}
|
|
|
|
|
2012-10-29 23:32:10 +00:00
|
|
|
PR_LOG(GetJPEGLog(), PR_LOG_DEBUG,
|
2009-10-16 02:54:44 +00:00
|
|
|
("[this=%p] nsJPEGDecoder::Write -- processing JPEG data\n", this));
|
2001-06-20 00:53:26 +00:00
|
|
|
|
|
|
|
switch (mState) {
|
|
|
|
case JPEG_HEADER:
|
|
|
|
{
|
2012-10-29 23:32:10 +00:00
|
|
|
LOG_SCOPE(GetJPEGLog(), "nsJPEGDecoder::Write -- entering JPEG_HEADER case");
|
2001-06-20 00:53:26 +00:00
|
|
|
|
|
|
|
/* Step 3: read file parameters with jpeg_read_header() */
|
2007-10-19 00:36:34 +00:00
|
|
|
if (jpeg_read_header(&mInfo, TRUE) == JPEG_SUSPENDED) {
|
2012-10-29 23:32:10 +00:00
|
|
|
PR_LOG(GetJPEGDecoderAccountingLog(), PR_LOG_DEBUG,
|
2007-10-19 00:36:34 +00:00
|
|
|
("} (JPEG_SUSPENDED)"));
|
2010-09-12 15:22:30 +00:00
|
|
|
return; /* I/O suspension */
|
2007-10-19 00:36:34 +00:00
|
|
|
}
|
2001-06-20 00:53:26 +00:00
|
|
|
|
2010-08-23 02:30:46 +00:00
|
|
|
// Post our size to the superclass
|
2013-08-25 07:19:43 +00:00
|
|
|
PostSize(mInfo.image_width, mInfo.image_height, ReadOrientationFromEXIF());
|
2011-05-11 09:46:59 +00:00
|
|
|
if (HasError()) {
|
2012-05-19 19:32:37 +00:00
|
|
|
// Setting the size led to an error.
|
2011-05-11 09:46:59 +00:00
|
|
|
mState = JPEG_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
2009-09-12 22:44:18 +00:00
|
|
|
|
2010-08-23 02:30:46 +00:00
|
|
|
/* If we're doing a size decode, we're done. */
|
|
|
|
if (IsSizeDecode())
|
2010-09-12 15:22:30 +00:00
|
|
|
return;
|
2009-09-12 22:44:18 +00:00
|
|
|
|
|
|
|
/* We're doing a full decode. */
|
2011-03-23 16:20:43 +00:00
|
|
|
if (mCMSMode != eCMSMode_Off &&
|
2012-07-30 14:20:58 +00:00
|
|
|
(mInProfile = GetICCProfile(mInfo)) != nullptr) {
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t profileSpace = qcms_profile_get_color_space(mInProfile);
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mismatch = false;
|
2007-07-23 22:02:17 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG_tor
|
|
|
|
fprintf(stderr, "JPEG profileSpace: 0x%08X\n", profileSpace);
|
|
|
|
#endif
|
|
|
|
switch (mInfo.jpeg_color_space) {
|
2003-04-16 10:47:27 +00:00
|
|
|
case JCS_GRAYSCALE:
|
2007-07-23 22:02:17 +00:00
|
|
|
if (profileSpace == icSigRgbData)
|
|
|
|
mInfo.out_color_space = JCS_RGB;
|
|
|
|
else if (profileSpace != icSigGrayData)
|
2011-10-17 14:59:28 +00:00
|
|
|
mismatch = true;
|
2007-07-23 22:02:17 +00:00
|
|
|
break;
|
2003-04-16 10:47:27 +00:00
|
|
|
case JCS_RGB:
|
2007-07-23 22:02:17 +00:00
|
|
|
if (profileSpace != icSigRgbData)
|
2011-10-17 14:59:28 +00:00
|
|
|
mismatch = true;
|
2007-07-23 22:02:17 +00:00
|
|
|
break;
|
2003-04-16 10:47:27 +00:00
|
|
|
case JCS_YCbCr:
|
2007-07-23 22:02:17 +00:00
|
|
|
if (profileSpace == icSigRgbData)
|
|
|
|
mInfo.out_color_space = JCS_RGB;
|
2009-04-07 16:02:11 +00:00
|
|
|
else
|
2011-11-08 18:24:35 +00:00
|
|
|
// qcms doesn't support ycbcr
|
2011-10-17 14:59:28 +00:00
|
|
|
mismatch = true;
|
2003-04-16 10:47:27 +00:00
|
|
|
break;
|
|
|
|
case JCS_CMYK:
|
|
|
|
case JCS_YCCK:
|
2011-11-08 18:24:35 +00:00
|
|
|
// qcms doesn't support cmyk
|
|
|
|
mismatch = true;
|
2007-07-23 22:02:17 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
mState = JPEG_ERROR;
|
2010-09-12 15:22:27 +00:00
|
|
|
PostDataError();
|
2012-10-29 23:32:10 +00:00
|
|
|
PR_LOG(GetJPEGDecoderAccountingLog(), PR_LOG_DEBUG,
|
2007-10-19 00:36:34 +00:00
|
|
|
("} (unknown colorpsace (1))"));
|
2010-09-12 15:22:30 +00:00
|
|
|
return;
|
2007-07-23 22:02:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!mismatch) {
|
2009-04-07 16:02:11 +00:00
|
|
|
qcms_data_type type;
|
2007-07-23 22:02:17 +00:00
|
|
|
switch (mInfo.out_color_space) {
|
|
|
|
case JCS_GRAYSCALE:
|
2009-04-07 16:02:11 +00:00
|
|
|
type = QCMS_DATA_GRAY_8;
|
2007-07-23 22:02:17 +00:00
|
|
|
break;
|
|
|
|
case JCS_RGB:
|
2009-04-07 16:02:11 +00:00
|
|
|
type = QCMS_DATA_RGB_8;
|
2007-07-23 22:02:17 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
mState = JPEG_ERROR;
|
2010-09-12 15:22:27 +00:00
|
|
|
PostDataError();
|
2012-10-29 23:32:10 +00:00
|
|
|
PR_LOG(GetJPEGDecoderAccountingLog(), PR_LOG_DEBUG,
|
2007-10-19 00:36:34 +00:00
|
|
|
("} (unknown colorpsace (2))"));
|
2010-09-12 15:22:30 +00:00
|
|
|
return;
|
2007-07-23 22:02:17 +00:00
|
|
|
}
|
2009-04-07 16:02:11 +00:00
|
|
|
#if 0
|
2009-04-23 17:38:11 +00:00
|
|
|
/* We don't currently support CMYK profiles. The following
|
|
|
|
* code dealt with lcms types. Add something like this
|
|
|
|
* back when we gain support for CMYK.
|
|
|
|
*/
|
2007-09-24 21:42:39 +00:00
|
|
|
/* Adobe Photoshop writes YCCK/CMYK files with inverted data */
|
|
|
|
if (mInfo.out_color_space == JCS_CMYK)
|
2007-07-23 22:02:17 +00:00
|
|
|
type |= FLAVOR_SH(mInfo.saw_Adobe_marker ? 1 : 0);
|
2009-04-07 16:02:11 +00:00
|
|
|
#endif
|
2007-07-23 22:02:17 +00:00
|
|
|
|
2008-07-23 17:33:12 +00:00
|
|
|
if (gfxPlatform::GetCMSOutputProfile()) {
|
|
|
|
|
|
|
|
/* Calculate rendering intent. */
|
|
|
|
int intent = gfxPlatform::GetRenderingIntent();
|
|
|
|
if (intent == -1)
|
2009-04-07 16:02:11 +00:00
|
|
|
intent = qcms_profile_get_rendering_intent(mInProfile);
|
2008-07-23 17:33:12 +00:00
|
|
|
|
|
|
|
/* Create the color management transform. */
|
2009-04-07 16:02:11 +00:00
|
|
|
mTransform = qcms_transform_create(mInProfile,
|
2007-07-23 22:02:17 +00:00
|
|
|
type,
|
|
|
|
gfxPlatform::GetCMSOutputProfile(),
|
2009-04-07 16:02:11 +00:00
|
|
|
QCMS_DATA_RGB_8,
|
|
|
|
(qcms_intent)intent);
|
2008-07-23 17:33:12 +00:00
|
|
|
}
|
2007-07-23 22:02:17 +00:00
|
|
|
} else {
|
|
|
|
#ifdef DEBUG_tor
|
|
|
|
fprintf(stderr, "ICM profile colorspace mismatch\n");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mTransform) {
|
|
|
|
switch (mInfo.jpeg_color_space) {
|
|
|
|
case JCS_GRAYSCALE:
|
|
|
|
case JCS_RGB:
|
|
|
|
case JCS_YCbCr:
|
2012-09-14 19:54:55 +00:00
|
|
|
// if we're not color managing we can decode directly to
|
|
|
|
// MOZ_JCS_EXT_NATIVE_ENDIAN_XRGB
|
|
|
|
if (mCMSMode != eCMSMode_All) {
|
|
|
|
mInfo.out_color_space = MOZ_JCS_EXT_NATIVE_ENDIAN_XRGB;
|
|
|
|
mInfo.out_color_components = 4;
|
|
|
|
} else {
|
|
|
|
mInfo.out_color_space = JCS_RGB;
|
|
|
|
}
|
2007-07-23 22:02:17 +00:00
|
|
|
break;
|
2008-02-14 20:38:19 +00:00
|
|
|
case JCS_CMYK:
|
|
|
|
case JCS_YCCK:
|
|
|
|
/* libjpeg can convert from YCCK to CMYK, but not to RGB */
|
|
|
|
mInfo.out_color_space = JCS_CMYK;
|
|
|
|
break;
|
2003-04-16 10:47:27 +00:00
|
|
|
default:
|
|
|
|
mState = JPEG_ERROR;
|
2010-09-12 15:22:27 +00:00
|
|
|
PostDataError();
|
2012-10-29 23:32:10 +00:00
|
|
|
PR_LOG(GetJPEGDecoderAccountingLog(), PR_LOG_DEBUG,
|
2007-10-19 00:36:34 +00:00
|
|
|
("} (unknown colorpsace (3))"));
|
2010-09-12 15:22:30 +00:00
|
|
|
return;
|
2007-07-23 22:02:17 +00:00
|
|
|
break;
|
|
|
|
}
|
2003-04-16 10:47:27 +00:00
|
|
|
}
|
|
|
|
|
2001-06-20 00:53:26 +00:00
|
|
|
/*
|
|
|
|
* Don't allocate a giant and superfluous memory buffer
|
2012-11-19 01:18:52 +00:00
|
|
|
* when not doing a progressive decode.
|
2001-06-20 00:53:26 +00:00
|
|
|
*/
|
2012-11-19 01:18:52 +00:00
|
|
|
mInfo.buffered_image = mDecodeStyle == PROGRESSIVE && jpeg_has_multiple_scans(&mInfo);
|
2001-06-20 00:53:26 +00:00
|
|
|
|
|
|
|
/* Used to set up image size so arrays can be allocated */
|
|
|
|
jpeg_calc_output_dimensions(&mInfo);
|
|
|
|
|
2013-02-02 01:06:30 +00:00
|
|
|
if (!mImageData) {
|
Bug 753 - Remove nsIImage, gfxIImageFrame, and their implementations, and expose an equivalent api on imgIContainer. r=roc,josh,bz,longsonr,vlad,karlt,jimm,bsmedberg,mfinkle,peterw,peterv sr=vlad,roc
--HG--
rename : gfx/src/shared/gfxImageFrame.cpp => modules/libpr0n/src/imgFrame.cpp
rename : gfx/src/shared/gfxImageFrame.h => modules/libpr0n/src/imgFrame.h
2009-07-21 01:50:15 +00:00
|
|
|
mState = JPEG_ERROR;
|
2010-09-12 15:22:27 +00:00
|
|
|
PostDecoderError(NS_ERROR_OUT_OF_MEMORY);
|
2012-10-29 23:32:10 +00:00
|
|
|
PR_LOG(GetJPEGDecoderAccountingLog(), PR_LOG_DEBUG,
|
Bug 753 - Remove nsIImage, gfxIImageFrame, and their implementations, and expose an equivalent api on imgIContainer. r=roc,josh,bz,longsonr,vlad,karlt,jimm,bsmedberg,mfinkle,peterw,peterv sr=vlad,roc
--HG--
rename : gfx/src/shared/gfxImageFrame.cpp => modules/libpr0n/src/imgFrame.cpp
rename : gfx/src/shared/gfxImageFrame.h => modules/libpr0n/src/imgFrame.h
2009-07-21 01:50:15 +00:00
|
|
|
("} (could not initialize image frame)"));
|
2010-09-12 15:22:30 +00:00
|
|
|
return;
|
2007-10-19 00:36:34 +00:00
|
|
|
}
|
2001-06-20 00:53:26 +00:00
|
|
|
|
2012-10-29 23:32:10 +00:00
|
|
|
PR_LOG(GetJPEGDecoderAccountingLog(), PR_LOG_DEBUG,
|
2009-10-16 02:54:44 +00:00
|
|
|
(" JPEGDecoderAccounting: nsJPEGDecoder::Write -- created image frame with %ux%u pixels",
|
Bug 753 - Remove nsIImage, gfxIImageFrame, and their implementations, and expose an equivalent api on imgIContainer. r=roc,josh,bz,longsonr,vlad,karlt,jimm,bsmedberg,mfinkle,peterw,peterv sr=vlad,roc
--HG--
rename : gfx/src/shared/gfxImageFrame.cpp => modules/libpr0n/src/imgFrame.cpp
rename : gfx/src/shared/gfxImageFrame.h => modules/libpr0n/src/imgFrame.h
2009-07-21 01:50:15 +00:00
|
|
|
mInfo.image_width, mInfo.image_height));
|
|
|
|
|
2001-06-20 00:53:26 +00:00
|
|
|
mState = JPEG_START_DECOMPRESS;
|
|
|
|
}
|
2002-07-04 20:31:18 +00:00
|
|
|
|
2001-06-20 00:53:26 +00:00
|
|
|
case JPEG_START_DECOMPRESS:
|
|
|
|
{
|
2012-10-29 23:32:10 +00:00
|
|
|
LOG_SCOPE(GetJPEGLog(), "nsJPEGDecoder::Write -- entering JPEG_START_DECOMPRESS case");
|
2001-06-20 00:53:26 +00:00
|
|
|
/* Step 4: set parameters for decompression */
|
|
|
|
|
|
|
|
/* FIXME -- Should reset dct_method and dither mode
|
|
|
|
* for final pass of progressive JPEG
|
|
|
|
*/
|
2002-01-16 04:54:44 +00:00
|
|
|
mInfo.dct_method = JDCT_ISLOW;
|
|
|
|
mInfo.dither_mode = JDITHER_FS;
|
2002-06-25 18:28:09 +00:00
|
|
|
mInfo.do_fancy_upsampling = TRUE;
|
2001-06-20 00:53:26 +00:00
|
|
|
mInfo.enable_2pass_quant = FALSE;
|
|
|
|
mInfo.do_block_smoothing = TRUE;
|
|
|
|
|
|
|
|
/* Step 5: Start decompressor */
|
2007-10-19 00:36:34 +00:00
|
|
|
if (jpeg_start_decompress(&mInfo) == FALSE) {
|
2012-10-29 23:32:10 +00:00
|
|
|
PR_LOG(GetJPEGDecoderAccountingLog(), PR_LOG_DEBUG,
|
2007-10-19 00:36:34 +00:00
|
|
|
("} (I/O suspension after jpeg_start_decompress())"));
|
2010-09-12 15:22:30 +00:00
|
|
|
return; /* I/O suspension */
|
2007-10-19 00:36:34 +00:00
|
|
|
}
|
2001-06-20 00:53:26 +00:00
|
|
|
|
2008-01-30 04:41:42 +00:00
|
|
|
|
|
|
|
/* If this is a progressive JPEG ... */
|
|
|
|
mState = mInfo.buffered_image ? JPEG_DECOMPRESS_PROGRESSIVE : JPEG_DECOMPRESS_SEQUENTIAL;
|
2001-06-20 00:53:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case JPEG_DECOMPRESS_SEQUENTIAL:
|
|
|
|
{
|
|
|
|
if (mState == JPEG_DECOMPRESS_SEQUENTIAL)
|
|
|
|
{
|
2012-10-29 23:32:10 +00:00
|
|
|
LOG_SCOPE(GetJPEGLog(), "nsJPEGDecoder::Write -- JPEG_DECOMPRESS_SEQUENTIAL case");
|
2002-07-04 20:31:18 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool suspend;
|
2010-09-12 15:22:30 +00:00
|
|
|
OutputScanlines(&suspend);
|
2008-09-10 20:23:29 +00:00
|
|
|
|
|
|
|
if (suspend) {
|
2012-10-29 23:32:10 +00:00
|
|
|
PR_LOG(GetJPEGDecoderAccountingLog(), PR_LOG_DEBUG,
|
2007-10-19 00:36:34 +00:00
|
|
|
("} (I/O suspension after OutputScanlines() - SEQUENTIAL)"));
|
2010-09-12 15:22:30 +00:00
|
|
|
return; /* I/O suspension */
|
2007-10-19 00:36:34 +00:00
|
|
|
}
|
2002-07-04 20:31:18 +00:00
|
|
|
|
2001-06-20 00:53:26 +00:00
|
|
|
/* If we've completed image output ... */
|
|
|
|
NS_ASSERTION(mInfo.output_scanline == mInfo.output_height, "We didn't process all of the data!");
|
2002-07-04 20:31:18 +00:00
|
|
|
mState = JPEG_DONE;
|
2001-06-20 00:53:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case JPEG_DECOMPRESS_PROGRESSIVE:
|
|
|
|
{
|
|
|
|
if (mState == JPEG_DECOMPRESS_PROGRESSIVE)
|
|
|
|
{
|
2012-10-29 23:32:10 +00:00
|
|
|
LOG_SCOPE(GetJPEGLog(), "nsJPEGDecoder::Write -- JPEG_DECOMPRESS_PROGRESSIVE case");
|
2002-06-20 23:44:24 +00:00
|
|
|
|
2002-06-29 22:14:10 +00:00
|
|
|
int status;
|
|
|
|
do {
|
|
|
|
status = jpeg_consume_input(&mInfo);
|
2002-07-04 20:31:18 +00:00
|
|
|
} while ((status != JPEG_SUSPENDED) &&
|
|
|
|
(status != JPEG_REACHED_EOI));
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
if (mInfo.output_scanline == 0) {
|
|
|
|
int scan = mInfo.input_scan_number;
|
|
|
|
|
|
|
|
/* if we haven't displayed anything yet (output_scan_number==0)
|
|
|
|
and we have enough data for a complete scan, force output
|
|
|
|
of the last full scan */
|
|
|
|
if ((mInfo.output_scan_number == 0) &&
|
|
|
|
(scan > 1) &&
|
|
|
|
(status != JPEG_REACHED_EOI))
|
|
|
|
scan--;
|
|
|
|
|
2007-10-19 00:36:34 +00:00
|
|
|
if (!jpeg_start_output(&mInfo, scan)) {
|
2012-10-29 23:32:10 +00:00
|
|
|
PR_LOG(GetJPEGDecoderAccountingLog(), PR_LOG_DEBUG,
|
2007-10-19 00:36:34 +00:00
|
|
|
("} (I/O suspension after jpeg_start_output() - PROGRESSIVE)"));
|
2010-09-12 15:22:30 +00:00
|
|
|
return; /* I/O suspension */
|
2007-10-19 00:36:34 +00:00
|
|
|
}
|
2002-07-04 20:31:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mInfo.output_scanline == 0xffffff)
|
|
|
|
mInfo.output_scanline = 0;
|
2002-06-20 23:44:24 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool suspend;
|
2010-09-12 15:22:30 +00:00
|
|
|
OutputScanlines(&suspend);
|
2008-09-10 20:23:29 +00:00
|
|
|
|
|
|
|
if (suspend) {
|
2002-07-04 20:31:18 +00:00
|
|
|
if (mInfo.output_scanline == 0) {
|
|
|
|
/* didn't manage to read any lines - flag so we don't call
|
|
|
|
jpeg_start_output() multiple times for the same scan */
|
|
|
|
mInfo.output_scanline = 0xffffff;
|
|
|
|
}
|
2012-10-29 23:32:10 +00:00
|
|
|
PR_LOG(GetJPEGDecoderAccountingLog(), PR_LOG_DEBUG,
|
2007-10-19 00:36:34 +00:00
|
|
|
("} (I/O suspension after OutputScanlines() - PROGRESSIVE)"));
|
2010-09-12 15:22:30 +00:00
|
|
|
return; /* I/O suspension */
|
2002-07-04 20:31:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mInfo.output_scanline == mInfo.output_height)
|
2002-06-29 22:14:10 +00:00
|
|
|
{
|
2007-10-19 00:36:34 +00:00
|
|
|
if (!jpeg_finish_output(&mInfo)) {
|
2012-10-29 23:32:10 +00:00
|
|
|
PR_LOG(GetJPEGDecoderAccountingLog(), PR_LOG_DEBUG,
|
2007-10-19 00:36:34 +00:00
|
|
|
("} (I/O suspension after jpeg_finish_output() - PROGRESSIVE)"));
|
2010-09-12 15:22:30 +00:00
|
|
|
return; /* I/O suspension */
|
2007-10-19 00:36:34 +00:00
|
|
|
}
|
2002-07-04 20:31:18 +00:00
|
|
|
|
|
|
|
if (jpeg_input_complete(&mInfo) &&
|
|
|
|
(mInfo.input_scan_number == mInfo.output_scan_number))
|
|
|
|
break;
|
|
|
|
|
|
|
|
mInfo.output_scanline = 0;
|
2001-08-21 19:45:18 +00:00
|
|
|
}
|
2001-06-20 00:53:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mState = JPEG_DONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case JPEG_DONE:
|
|
|
|
{
|
2012-10-29 23:32:10 +00:00
|
|
|
LOG_SCOPE(GetJPEGLog(), "nsJPEGDecoder::ProcessData -- entering JPEG_DONE case");
|
2001-06-20 00:53:26 +00:00
|
|
|
|
|
|
|
/* Step 7: Finish decompression */
|
|
|
|
|
2007-10-19 00:36:34 +00:00
|
|
|
if (jpeg_finish_decompress(&mInfo) == FALSE) {
|
2012-10-29 23:32:10 +00:00
|
|
|
PR_LOG(GetJPEGDecoderAccountingLog(), PR_LOG_DEBUG,
|
2007-10-19 00:36:34 +00:00
|
|
|
("} (I/O suspension after jpeg_finish_decompress() - DONE)"));
|
2010-09-12 15:22:30 +00:00
|
|
|
return; /* I/O suspension */
|
2007-10-19 00:36:34 +00:00
|
|
|
}
|
2001-06-20 00:53:26 +00:00
|
|
|
|
|
|
|
mState = JPEG_SINK_NON_JPEG_TRAILER;
|
|
|
|
|
|
|
|
/* we're done dude */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case JPEG_SINK_NON_JPEG_TRAILER:
|
2012-10-29 23:32:10 +00:00
|
|
|
PR_LOG(GetJPEGLog(), PR_LOG_DEBUG,
|
2008-01-24 15:59:32 +00:00
|
|
|
("[this=%p] nsJPEGDecoder::ProcessData -- entering JPEG_SINK_NON_JPEG_TRAILER case\n", this));
|
2001-06-20 00:53:26 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JPEG_ERROR:
|
2010-09-12 15:22:27 +00:00
|
|
|
NS_ABORT_IF_FALSE(0, "Should always return immediately after error and not re-enter decoder");
|
2001-06-20 00:53:26 +00:00
|
|
|
}
|
|
|
|
|
2012-10-29 23:32:10 +00:00
|
|
|
PR_LOG(GetJPEGDecoderAccountingLog(), PR_LOG_DEBUG,
|
2007-10-19 00:36:34 +00:00
|
|
|
("} (end of function)"));
|
2010-09-12 15:22:30 +00:00
|
|
|
return;
|
2001-06-20 00:53:26 +00:00
|
|
|
}
|
|
|
|
|
2013-08-25 07:19:43 +00:00
|
|
|
Orientation
|
|
|
|
nsJPEGDecoder::ReadOrientationFromEXIF()
|
|
|
|
{
|
|
|
|
jpeg_saved_marker_ptr marker;
|
|
|
|
|
|
|
|
// Locate the APP1 marker, where EXIF data is stored, in the marker list.
|
|
|
|
for (marker = mInfo.marker_list ; marker != nullptr ; marker = marker->next) {
|
|
|
|
if (marker->marker == JPEG_APP0 + 1)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we're at the end of the list, there's no EXIF data.
|
|
|
|
if (!marker)
|
|
|
|
return Orientation();
|
|
|
|
|
|
|
|
// Extract the orientation information.
|
|
|
|
EXIFData exif = EXIFParser::Parse(marker->data,
|
|
|
|
static_cast<uint32_t>(marker->data_length));
|
|
|
|
return exif.orientation;
|
|
|
|
}
|
|
|
|
|
2009-09-12 22:44:18 +00:00
|
|
|
void
|
2010-09-12 15:22:30 +00:00
|
|
|
nsJPEGDecoder::NotifyDone()
|
2009-09-12 22:44:18 +00:00
|
|
|
{
|
2013-06-17 20:49:04 +00:00
|
|
|
PostFrameStop(FrameBlender::kFrameOpaque);
|
2010-09-12 15:22:30 +00:00
|
|
|
PostDecodeDone();
|
2009-09-12 22:44:18 +00:00
|
|
|
}
|
2001-06-20 00:53:26 +00:00
|
|
|
|
2010-09-12 15:22:30 +00:00
|
|
|
void
|
2011-09-29 06:19:26 +00:00
|
|
|
nsJPEGDecoder::OutputScanlines(bool* suspend)
|
2001-06-20 00:53:26 +00:00
|
|
|
{
|
2011-10-17 14:59:28 +00:00
|
|
|
*suspend = false;
|
2008-09-10 20:23:29 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
const uint32_t top = mInfo.output_scanline;
|
2006-03-27 19:04:51 +00:00
|
|
|
|
2002-07-04 20:31:18 +00:00
|
|
|
while ((mInfo.output_scanline < mInfo.output_height)) {
|
2008-01-30 04:41:42 +00:00
|
|
|
/* Use the Cairo image buffer as scanline buffer */
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t *imageRow = ((uint32_t*)mImageData) +
|
2007-08-25 20:07:37 +00:00
|
|
|
(mInfo.output_scanline * mInfo.output_width);
|
|
|
|
|
2012-09-14 19:54:55 +00:00
|
|
|
if (mInfo.out_color_space == MOZ_JCS_EXT_NATIVE_ENDIAN_XRGB) {
|
2008-01-30 04:41:42 +00:00
|
|
|
/* Special case: scanline will be directly converted into packed ARGB */
|
|
|
|
if (jpeg_read_scanlines(&mInfo, (JSAMPARRAY)&imageRow, 1) != 1) {
|
2011-10-17 14:59:28 +00:00
|
|
|
*suspend = true; /* suspend */
|
2008-01-30 04:41:42 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
continue; /* all done for this row! */
|
|
|
|
}
|
|
|
|
|
2007-08-25 20:07:37 +00:00
|
|
|
JSAMPROW sampleRow = (JSAMPROW)imageRow;
|
2008-02-14 20:38:19 +00:00
|
|
|
if (mInfo.output_components == 3) {
|
2007-08-25 20:07:37 +00:00
|
|
|
/* Put the pixels at end of row to enable in-place expansion */
|
|
|
|
sampleRow += mInfo.output_width;
|
|
|
|
}
|
|
|
|
|
2008-01-24 15:59:32 +00:00
|
|
|
/* Request one scanline. Returns 0 or 1 scanlines. */
|
2007-08-25 20:07:37 +00:00
|
|
|
if (jpeg_read_scanlines(&mInfo, &sampleRow, 1) != 1) {
|
2011-10-17 14:59:28 +00:00
|
|
|
*suspend = true; /* suspend */
|
2001-06-20 00:53:26 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-07-23 22:02:17 +00:00
|
|
|
if (mTransform) {
|
2007-08-25 20:07:37 +00:00
|
|
|
JSAMPROW source = sampleRow;
|
2007-07-23 22:02:17 +00:00
|
|
|
if (mInfo.out_color_space == JCS_GRAYSCALE) {
|
2007-08-25 20:07:37 +00:00
|
|
|
/* Convert from the 1byte grey pixels at begin of row
|
|
|
|
to the 3byte RGB byte pixels at 'end' of row */
|
|
|
|
sampleRow += mInfo.output_width;
|
|
|
|
}
|
2009-04-07 16:02:11 +00:00
|
|
|
qcms_transform_data(mTransform, source, sampleRow, mInfo.output_width);
|
2007-09-24 21:42:39 +00:00
|
|
|
/* Move 3byte RGB data to end of row */
|
|
|
|
if (mInfo.out_color_space == JCS_CMYK) {
|
|
|
|
memmove(sampleRow + mInfo.output_width,
|
|
|
|
sampleRow,
|
|
|
|
3 * mInfo.output_width);
|
|
|
|
sampleRow += mInfo.output_width;
|
|
|
|
}
|
2008-02-14 20:38:19 +00:00
|
|
|
} else {
|
|
|
|
if (mInfo.out_color_space == JCS_CMYK) {
|
|
|
|
/* Convert from CMYK to RGB */
|
|
|
|
/* We cannot convert directly to Cairo, as the CMSRGBTransform may wants to do a RGB transform... */
|
|
|
|
/* Would be better to have platform CMSenabled transformation from CMYK to (A)RGB... */
|
2011-11-08 18:24:35 +00:00
|
|
|
cmyk_convert_rgb((JSAMPROW)imageRow, mInfo.output_width);
|
2008-02-14 20:38:19 +00:00
|
|
|
sampleRow += mInfo.output_width;
|
|
|
|
}
|
2011-01-13 01:45:13 +00:00
|
|
|
if (mCMSMode == eCMSMode_All) {
|
2008-02-14 20:38:19 +00:00
|
|
|
/* No embedded ICC profile - treat as sRGB */
|
2009-04-07 16:02:11 +00:00
|
|
|
qcms_transform *transform = gfxPlatform::GetCMSRGBTransform();
|
2008-02-14 20:38:19 +00:00
|
|
|
if (transform) {
|
2009-04-07 16:02:11 +00:00
|
|
|
qcms_transform_data(transform, sampleRow, sampleRow, mInfo.output_width);
|
2008-02-14 20:38:19 +00:00
|
|
|
}
|
2007-07-23 22:02:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-21 10:26:31 +00:00
|
|
|
// counter for while() loops below
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t idx = mInfo.output_width;
|
2007-12-21 10:26:31 +00:00
|
|
|
|
2008-01-30 06:22:23 +00:00
|
|
|
// copy as bytes until source pointer is 32-bit-aligned
|
2008-01-31 01:04:36 +00:00
|
|
|
for (; (NS_PTR_TO_UINT32(sampleRow) & 0x3) && idx; --idx) {
|
2012-09-06 05:31:29 +00:00
|
|
|
*imageRow++ = gfxPackedPixel(0xFF, sampleRow[0], sampleRow[1], sampleRow[2]);
|
2008-01-30 06:22:23 +00:00
|
|
|
sampleRow += 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy pixels in blocks of 4
|
|
|
|
while (idx >= 4) {
|
|
|
|
GFX_BLOCK_RGB_TO_FRGB(sampleRow, imageRow);
|
2007-12-21 10:26:31 +00:00
|
|
|
idx -= 4;
|
|
|
|
sampleRow += 12;
|
|
|
|
imageRow += 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy remaining pixel(s)
|
|
|
|
while (idx--) {
|
|
|
|
// 32-bit read of final pixel will exceed buffer, so read bytes
|
2012-09-06 05:31:29 +00:00
|
|
|
*imageRow++ = gfxPackedPixel(0xFF, sampleRow[0], sampleRow[1], sampleRow[2]);
|
2007-08-25 20:07:37 +00:00
|
|
|
sampleRow += 3;
|
2006-03-25 00:34:48 +00:00
|
|
|
}
|
2001-06-20 00:53:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (top != mInfo.output_scanline) {
|
2004-11-15 04:16:07 +00:00
|
|
|
nsIntRect r(0, top, mInfo.output_width, mInfo.output_scanline-top);
|
2010-08-24 21:12:04 +00:00
|
|
|
PostInvalidation(r);
|
2001-06-20 00:53:26 +00:00
|
|
|
}
|
Bug 753 - Remove nsIImage, gfxIImageFrame, and their implementations, and expose an equivalent api on imgIContainer. r=roc,josh,bz,longsonr,vlad,karlt,jimm,bsmedberg,mfinkle,peterw,peterv sr=vlad,roc
--HG--
rename : gfx/src/shared/gfxImageFrame.cpp => modules/libpr0n/src/imgFrame.cpp
rename : gfx/src/shared/gfxImageFrame.h => modules/libpr0n/src/imgFrame.h
2009-07-21 01:50:15 +00:00
|
|
|
|
2001-06-20 00:53:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Override the standard error method in the IJG JPEG decoder code. */
|
2003-09-18 20:38:36 +00:00
|
|
|
METHODDEF(void)
|
2001-06-20 00:53:26 +00:00
|
|
|
my_error_exit (j_common_ptr cinfo)
|
|
|
|
{
|
|
|
|
decoder_error_mgr *err = (decoder_error_mgr *) cinfo->err;
|
|
|
|
|
2002-06-29 22:14:10 +00:00
|
|
|
/* Convert error to a browser error code */
|
2007-01-04 20:03:50 +00:00
|
|
|
nsresult error_code = err->pub.msg_code == JERR_OUT_OF_MEMORY
|
|
|
|
? NS_ERROR_OUT_OF_MEMORY
|
|
|
|
: NS_ERROR_FAILURE;
|
2001-06-20 00:53:26 +00:00
|
|
|
|
2002-07-04 20:31:18 +00:00
|
|
|
#ifdef DEBUG
|
2001-06-20 00:53:26 +00:00
|
|
|
char buffer[JMSG_LENGTH_MAX];
|
|
|
|
|
|
|
|
/* Create the message */
|
2007-01-04 20:03:50 +00:00
|
|
|
(*err->pub.format_message) (cinfo, buffer);
|
2001-06-20 00:53:26 +00:00
|
|
|
|
|
|
|
fprintf(stderr, "JPEG decoding error:\n%s\n", buffer);
|
|
|
|
#endif
|
|
|
|
|
2012-08-07 17:17:27 +00:00
|
|
|
/* Return control to the setjmp point. We pass an nsresult masquerading as
|
|
|
|
* an int, which works because the setjmp() caller casts it back. */
|
|
|
|
longjmp(err->setjmp_buffer, static_cast<int>(error_code));
|
2001-06-20 00:53:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/*-----------------------------------------------------------------------------
|
|
|
|
* This is the callback routine from the IJG JPEG library used to supply new
|
|
|
|
* data to the decompressor when its input buffer is exhausted. It juggles
|
|
|
|
* multiple buffers in an attempt to avoid unnecessary copying of input data.
|
|
|
|
*
|
|
|
|
* (A simpler scheme is possible: It's much easier to use only a single
|
|
|
|
* buffer; when fill_input_buffer() is called, move any unconsumed data
|
|
|
|
* (beyond the current pointer/count) down to the beginning of this buffer and
|
|
|
|
* then load new data into the remaining buffer space. This approach requires
|
|
|
|
* a little more data copying but is far easier to get right.)
|
|
|
|
*
|
|
|
|
* At any one time, the JPEG decompressor is either reading from the necko
|
|
|
|
* input buffer, which is volatile across top-level calls to the IJG library,
|
|
|
|
* or the "backtrack" buffer. The backtrack buffer contains the remaining
|
|
|
|
* unconsumed data from the necko buffer after parsing was suspended due
|
|
|
|
* to insufficient data in some previous call to the IJG library.
|
|
|
|
*
|
|
|
|
* When suspending, the decompressor will back up to a convenient restart
|
|
|
|
* point (typically the start of the current MCU). The variables
|
|
|
|
* next_input_byte & bytes_in_buffer indicate where the restart point will be
|
|
|
|
* if the current call returns FALSE. Data beyond this point must be
|
|
|
|
* rescanned after resumption, so it must be preserved in case the decompressor
|
|
|
|
* decides to backtrack.
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* TRUE if additional data is available, FALSE if no data present and
|
|
|
|
* the JPEG library should therefore suspend processing of input stream
|
|
|
|
*---------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2001-10-27 17:24:19 +00:00
|
|
|
/* data source manager method */
|
2001-06-20 00:53:26 +00:00
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/* data source manager method
|
2003-04-16 10:47:27 +00:00
|
|
|
Initialize source. This is called by jpeg_read_header() before any
|
|
|
|
data is actually read. May leave
|
|
|
|
bytes_in_buffer set to 0 (in which case a fill_input_buffer() call
|
|
|
|
will occur immediately).
|
2001-06-20 00:53:26 +00:00
|
|
|
*/
|
2003-09-18 20:38:36 +00:00
|
|
|
METHODDEF(void)
|
2001-06-20 00:53:26 +00:00
|
|
|
init_source (j_decompress_ptr jd)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/* data source manager method
|
2003-04-16 10:47:27 +00:00
|
|
|
Skip num_bytes worth of data. The buffer pointer and count should
|
|
|
|
be advanced over num_bytes input bytes, refilling the buffer as
|
|
|
|
needed. This is used to skip over a potentially large amount of
|
|
|
|
uninteresting data (such as an APPn marker). In some applications
|
|
|
|
it may be possible to optimize away the reading of the skipped data,
|
|
|
|
but it's not clear that being smart is worth much trouble; large
|
|
|
|
skips are uncommon. bytes_in_buffer may be zero on return.
|
|
|
|
A zero or negative skip count should be treated as a no-op.
|
2001-06-20 00:53:26 +00:00
|
|
|
*/
|
2003-09-18 20:38:36 +00:00
|
|
|
METHODDEF(void)
|
2001-06-20 00:53:26 +00:00
|
|
|
skip_input_data (j_decompress_ptr jd, long num_bytes)
|
|
|
|
{
|
2007-01-04 20:03:50 +00:00
|
|
|
struct jpeg_source_mgr *src = jd->src;
|
2007-01-31 14:53:21 +00:00
|
|
|
nsJPEGDecoder *decoder = (nsJPEGDecoder *)(jd->client_data);
|
2001-06-20 00:53:26 +00:00
|
|
|
|
2007-01-04 20:03:50 +00:00
|
|
|
if (num_bytes > (long)src->bytes_in_buffer) {
|
2001-06-20 00:53:26 +00:00
|
|
|
/*
|
|
|
|
* Can't skip it all right now until we get more data from
|
|
|
|
* network stream. Set things up so that fill_input_buffer
|
|
|
|
* will skip remaining amount.
|
|
|
|
*/
|
2007-01-04 20:03:50 +00:00
|
|
|
decoder->mBytesToSkip = (size_t)num_bytes - src->bytes_in_buffer;
|
|
|
|
src->next_input_byte += src->bytes_in_buffer;
|
|
|
|
src->bytes_in_buffer = 0;
|
2001-06-20 00:53:26 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
/* Simple case. Just advance buffer pointer */
|
|
|
|
|
2007-01-04 20:03:50 +00:00
|
|
|
src->bytes_in_buffer -= (size_t)num_bytes;
|
|
|
|
src->next_input_byte += num_bytes;
|
2001-06-20 00:53:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/* data source manager method
|
2003-04-16 10:47:27 +00:00
|
|
|
This is called whenever bytes_in_buffer has reached zero and more
|
|
|
|
data is wanted. In typical applications, it should read fresh data
|
|
|
|
into the buffer (ignoring the current state of next_input_byte and
|
|
|
|
bytes_in_buffer), reset the pointer & count to the start of the
|
|
|
|
buffer, and return TRUE indicating that the buffer has been reloaded.
|
|
|
|
It is not necessary to fill the buffer entirely, only to obtain at
|
|
|
|
least one more byte. bytes_in_buffer MUST be set to a positive value
|
|
|
|
if TRUE is returned. A FALSE return should only be used when I/O
|
|
|
|
suspension is desired.
|
2001-06-20 00:53:26 +00:00
|
|
|
*/
|
2003-09-18 20:38:36 +00:00
|
|
|
METHODDEF(boolean)
|
2001-06-20 00:53:26 +00:00
|
|
|
fill_input_buffer (j_decompress_ptr jd)
|
|
|
|
{
|
2007-01-04 20:03:50 +00:00
|
|
|
struct jpeg_source_mgr *src = jd->src;
|
2007-01-31 14:53:21 +00:00
|
|
|
nsJPEGDecoder *decoder = (nsJPEGDecoder *)(jd->client_data);
|
2001-06-20 00:53:26 +00:00
|
|
|
|
2007-01-04 20:03:50 +00:00
|
|
|
if (decoder->mReading) {
|
2008-01-24 15:59:32 +00:00
|
|
|
const JOCTET *new_buffer = decoder->mSegment;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t new_buflen = decoder->mSegmentLen;
|
2007-01-04 20:03:50 +00:00
|
|
|
|
|
|
|
if (!new_buffer || new_buflen == 0)
|
2011-10-17 14:59:28 +00:00
|
|
|
return false; /* suspend */
|
2001-06-20 00:53:26 +00:00
|
|
|
|
2008-01-24 15:59:32 +00:00
|
|
|
decoder->mSegmentLen = 0;
|
2001-06-20 00:53:26 +00:00
|
|
|
|
2007-01-04 20:03:50 +00:00
|
|
|
if (decoder->mBytesToSkip) {
|
|
|
|
if (decoder->mBytesToSkip < new_buflen) {
|
|
|
|
/* All done skipping bytes; Return what's left. */
|
|
|
|
new_buffer += decoder->mBytesToSkip;
|
|
|
|
new_buflen -= decoder->mBytesToSkip;
|
|
|
|
decoder->mBytesToSkip = 0;
|
|
|
|
} else {
|
|
|
|
/* Still need to skip some more data in the future */
|
|
|
|
decoder->mBytesToSkip -= (size_t)new_buflen;
|
2011-10-17 14:59:28 +00:00
|
|
|
return false; /* suspend */
|
2001-06-20 00:53:26 +00:00
|
|
|
}
|
2007-01-04 20:03:50 +00:00
|
|
|
}
|
2001-06-20 00:53:26 +00:00
|
|
|
|
2007-01-04 20:03:50 +00:00
|
|
|
decoder->mBackBufferUnreadLen = src->bytes_in_buffer;
|
2001-06-20 00:53:26 +00:00
|
|
|
|
2007-01-04 20:03:50 +00:00
|
|
|
src->next_input_byte = new_buffer;
|
|
|
|
src->bytes_in_buffer = (size_t)new_buflen;
|
2011-10-17 14:59:28 +00:00
|
|
|
decoder->mReading = false;
|
2001-06-20 00:53:26 +00:00
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
return true;
|
2007-01-04 20:03:50 +00:00
|
|
|
}
|
2001-06-20 00:53:26 +00:00
|
|
|
|
2008-01-24 15:59:32 +00:00
|
|
|
if (src->next_input_byte != decoder->mSegment) {
|
2007-01-04 20:03:50 +00:00
|
|
|
/* Backtrack data has been permanently consumed. */
|
|
|
|
decoder->mBackBufferUnreadLen = 0;
|
|
|
|
decoder->mBackBufferLen = 0;
|
|
|
|
}
|
2001-06-20 00:53:26 +00:00
|
|
|
|
2007-01-04 20:03:50 +00:00
|
|
|
/* Save remainder of netlib buffer in backtrack buffer */
|
2012-08-22 15:56:38 +00:00
|
|
|
const uint32_t new_backtrack_buflen = src->bytes_in_buffer + decoder->mBackBufferLen;
|
2007-01-04 20:03:50 +00:00
|
|
|
|
|
|
|
/* Make sure backtrack buffer is big enough to hold new data. */
|
|
|
|
if (decoder->mBackBufferSize < new_backtrack_buflen) {
|
2008-01-24 15:59:32 +00:00
|
|
|
/* Check for malformed MARKER segment lengths, before allocating space for it */
|
|
|
|
if (new_backtrack_buflen > MAX_JPEG_MARKER_LENGTH) {
|
|
|
|
my_error_exit((j_common_ptr)(&decoder->mInfo));
|
2001-06-20 00:53:26 +00:00
|
|
|
}
|
2008-01-19 22:41:35 +00:00
|
|
|
|
2008-01-24 15:59:32 +00:00
|
|
|
/* Round up to multiple of 256 bytes. */
|
|
|
|
const size_t roundup_buflen = ((new_backtrack_buflen + 255) >> 8) << 8;
|
|
|
|
JOCTET *buf = (JOCTET *)PR_REALLOC(decoder->mBackBuffer, roundup_buflen);
|
|
|
|
/* Check for OOM */
|
|
|
|
if (!buf) {
|
|
|
|
decoder->mInfo.err->msg_code = JERR_OUT_OF_MEMORY;
|
2008-01-19 22:41:35 +00:00
|
|
|
my_error_exit((j_common_ptr)(&decoder->mInfo));
|
|
|
|
}
|
2008-01-24 15:59:32 +00:00
|
|
|
decoder->mBackBuffer = buf;
|
|
|
|
decoder->mBackBufferSize = roundup_buflen;
|
2001-06-20 00:53:26 +00:00
|
|
|
}
|
|
|
|
|
2008-01-24 15:59:32 +00:00
|
|
|
/* Copy remainder of netlib segment into backtrack buffer. */
|
2007-01-04 20:03:50 +00:00
|
|
|
memmove(decoder->mBackBuffer + decoder->mBackBufferLen,
|
|
|
|
src->next_input_byte,
|
|
|
|
src->bytes_in_buffer);
|
|
|
|
|
|
|
|
/* Point to start of data to be rescanned. */
|
|
|
|
src->next_input_byte = decoder->mBackBuffer + decoder->mBackBufferLen - decoder->mBackBufferUnreadLen;
|
|
|
|
src->bytes_in_buffer += decoder->mBackBufferUnreadLen;
|
|
|
|
decoder->mBackBufferLen = (size_t)new_backtrack_buflen;
|
2011-10-17 14:59:28 +00:00
|
|
|
decoder->mReading = true;
|
2007-01-04 20:03:50 +00:00
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
return false;
|
2001-06-20 00:53:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/* data source manager method */
|
|
|
|
/*
|
|
|
|
* Terminate source --- called by jpeg_finish_decompress() after all
|
|
|
|
* data has been read to clean up JPEG source manager. NOT called by
|
|
|
|
* jpeg_abort() or jpeg_destroy().
|
|
|
|
*/
|
2003-09-18 20:38:36 +00:00
|
|
|
METHODDEF(void)
|
2001-06-20 00:53:26 +00:00
|
|
|
term_source (j_decompress_ptr jd)
|
|
|
|
{
|
2007-01-31 14:53:21 +00:00
|
|
|
nsJPEGDecoder *decoder = (nsJPEGDecoder *)(jd->client_data);
|
2001-06-20 00:53:26 +00:00
|
|
|
|
2009-10-16 02:54:44 +00:00
|
|
|
// This function shouldn't be called if we ran into an error we didn't
|
|
|
|
// recover from.
|
|
|
|
NS_ABORT_IF_FALSE(decoder->mState != JPEG_ERROR,
|
|
|
|
"Calling term_source on a JPEG with mState == JPEG_ERROR!");
|
2008-01-21 09:48:18 +00:00
|
|
|
|
2010-09-12 15:22:30 +00:00
|
|
|
// Notify using a helper method to get around protectedness issues.
|
|
|
|
decoder->NotifyDone();
|
2001-06-20 00:53:26 +00:00
|
|
|
}
|
2008-01-30 04:41:42 +00:00
|
|
|
|
2012-01-06 16:02:27 +00:00
|
|
|
} // namespace image
|
2010-08-23 02:30:46 +00:00
|
|
|
} // namespace mozilla
|
|
|
|
|
2008-01-30 04:41:42 +00:00
|
|
|
|
2011-11-08 18:24:35 +00:00
|
|
|
/**************** Inverted CMYK -> RGB conversion **************/
|
2008-02-14 20:38:19 +00:00
|
|
|
/*
|
2011-11-08 18:24:35 +00:00
|
|
|
* Input is (Inverted) CMYK stored as 4 bytes per pixel.
|
2008-02-14 20:38:19 +00:00
|
|
|
* Output is RGB stored as 3 bytes per pixel.
|
|
|
|
* @param row Points to row buffer containing the CMYK bytes for each pixel in the row.
|
|
|
|
* @param width Number of pixels in the row.
|
|
|
|
*/
|
2011-11-08 18:24:35 +00:00
|
|
|
static void cmyk_convert_rgb(JSAMPROW row, JDIMENSION width)
|
2008-02-14 20:38:19 +00:00
|
|
|
{
|
|
|
|
/* Work from end to front to shrink from 4 bytes per pixel to 3 */
|
|
|
|
JSAMPROW in = row + width*4;
|
|
|
|
JSAMPROW out = in;
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t i = width; i > 0; i--) {
|
2008-02-14 20:38:19 +00:00
|
|
|
in -= 4;
|
|
|
|
out -= 3;
|
|
|
|
|
|
|
|
// Source is 'Inverted CMYK', output is RGB.
|
|
|
|
// See: http://www.easyrgb.com/math.php?MATH=M12#text12
|
|
|
|
// Or: http://www.ilkeratalay.com/colorspacesfaq.php#rgb
|
|
|
|
|
|
|
|
// From CMYK to CMY
|
|
|
|
// C = ( C * ( 1 - K ) + K )
|
|
|
|
// M = ( M * ( 1 - K ) + K )
|
|
|
|
// Y = ( Y * ( 1 - K ) + K )
|
|
|
|
|
|
|
|
// From Inverted CMYK to CMY is thus:
|
|
|
|
// C = ( (1-iC) * (1 - (1-iK)) + (1-iK) ) => 1 - iC*iK
|
|
|
|
// Same for M and Y
|
|
|
|
|
|
|
|
// Convert from CMY (0..1) to RGB (0..1)
|
|
|
|
// R = 1 - C => 1 - (1 - iC*iK) => iC*iK
|
|
|
|
// G = 1 - M => 1 - (1 - iM*iK) => iM*iK
|
|
|
|
// B = 1 - Y => 1 - (1 - iY*iK) => iY*iK
|
|
|
|
|
|
|
|
// Convert from Inverted CMYK (0..255) to RGB (0..255)
|
2012-08-22 15:56:38 +00:00
|
|
|
const uint32_t iC = in[0];
|
|
|
|
const uint32_t iM = in[1];
|
|
|
|
const uint32_t iY = in[2];
|
|
|
|
const uint32_t iK = in[3];
|
2008-02-14 20:38:19 +00:00
|
|
|
out[0] = iC*iK/255; // Red
|
|
|
|
out[1] = iM*iK/255; // Green
|
|
|
|
out[2] = iY*iK/255; // Blue
|
|
|
|
}
|
|
|
|
}
|