Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(80)

Unified Diff: src/codec/SkJpegCodec.cpp

Issue 1180983002: Switch SkJpegCode to libjpeg-turbo (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/codec/SkJpegCodec.cpp
diff --git a/src/codec/SkJpegCodec.cpp b/src/codec/SkJpegCodec.cpp
index c12587424ae8a72f919222f3f7e54580d1fa7a52..40b5b179e5327dc28831c44dbc68391dc524de33 100644
--- a/src/codec/SkJpegCodec.cpp
+++ b/src/codec/SkJpegCodec.cpp
@@ -15,53 +15,22 @@
#include "SkTemplates.h"
#include "SkTypes.h"
-// stdio is needed for jpeglib
+// stdio is needed for libjpeg-turbo
#include <stdio.h>
extern "C" {
#include "jerror.h"
- #include "jmorecfg.h"
#include "jpegint.h"
#include "jpeglib.h"
}
-// ANDROID_RGB
-// If this is defined in the jpeg headers it indicates that jpeg offers
-// support for two additional formats: JCS_RGBA_8888 and JCS_RGB_565.
-
-/*
- * Get the source configuarion for the swizzler
- */
-SkSwizzler::SrcConfig get_src_config(const jpeg_decompress_struct& dinfo) {
- if (JCS_CMYK == dinfo.out_color_space) {
- // We will need to perform a manual conversion
- return SkSwizzler::kRGBX;
- }
- if (3 == dinfo.out_color_components && JCS_RGB == dinfo.out_color_space) {
- return SkSwizzler::kRGB;
- }
-#ifdef ANDROID_RGB
- if (JCS_RGBA_8888 == dinfo.out_color_space) {
- return SkSwizzler::kRGBX;
- }
-
- if (JCS_RGB_565 == dinfo.out_color_space) {
- return SkSwizzler::kRGB_565;
- }
-#endif
- if (1 == dinfo.out_color_components && JCS_GRAYSCALE == dinfo.out_color_space) {
- return SkSwizzler::kGray;
- }
- return SkSwizzler::kUnknown;
-}
-
/*
* Convert a row of CMYK samples to RGBX in place.
* Note that this method moves the row pointer.
* @param width the number of pixels in the row that is being converted
* CMYK is stored as four bytes per pixel
*/
-static void convert_CMYK_to_RGB(uint8_t* row, uint32_t width) {
+static void convert_CMYK_to_RGBA(uint8_t* row, uint32_t width) {
scroggo 2015/06/16 20:39:22 The comment says RGBX. I think X implies A isn't t
msarett 2015/06/18 21:06:10 Done.
// We will implement a crude conversion from CMYK -> RGB using formulas
// from easyrgb.com.
//
@@ -168,8 +137,6 @@ SkJpegCodec::SkJpegCodec(const SkImageInfo& srcInfo, SkStream* stream,
JpegDecoderMgr* decoderMgr)
: INHERITED(srcInfo, stream)
, fDecoderMgr(decoderMgr)
- , fSwizzler(NULL)
- , fSrcRowBytes(0)
{}
/*
@@ -177,15 +144,40 @@ SkJpegCodec::SkJpegCodec(const SkImageInfo& srcInfo, SkStream* stream,
*/
SkISize SkJpegCodec::onGetScaledDimensions(float desiredScale) const {
// libjpeg supports scaling by 1/1, 1/2, 1/4, and 1/8, so we will support these as well
scroggo 2015/06/16 20:39:22 Update this comment?
msarett 2015/06/18 21:06:10 Done.
- long scale;
- if (desiredScale > 0.75f) {
- scale = 1;
+ long num;
+ long denom = 8;
+ if (desiredScale > 1.875f) {
scroggo 2015/06/16 20:39:22 Wait, libjpeg scales up? What is the advantage to
msarett 2015/06/18 21:06:10 Oops, I added this for testing jpeg_skip_scanlines
+ num = 16;
+ } else if (desiredScale > 1.75f) {
+ num = 15;
+ } else if (desiredScale > 1.625f) {
+ num = 14;
+ } else if (desiredScale > 1.5f) {
+ num = 13;
+ } else if (desiredScale > 1.375f) {
+ num = 12;
+ } else if (desiredScale > 1.25f) {
+ num = 11;
+ } else if (desiredScale > 1.125f) {
+ num = 10;
+ } else if (desiredScale > 1.0f) {
+ num = 9;
+ } else if (desiredScale > 0.875f) {
+ num = 8;
+ } else if (desiredScale > 0.75f) {
+ num = 7;
+ } else if (desiredScale > 0.625f) {
+ num = 6;
+ } else if (desiredScale > 0.5f) {
+ num = 5;
} else if (desiredScale > 0.375f) {
- scale = 2;
- } else if (desiredScale > 0.1875f) {
- scale = 4;
+ num = 4;
+ } else if (desiredScale > 0.25f) {
+ num = 3;
+ } else if (desiredScale > 0.125f) {
+ num = 2;
} else {
- scale = 8;
+ num = 1;
}
// Set up a fake decompress struct in order to use libjpeg to calculate output dimensions
@@ -195,8 +187,8 @@ SkISize SkJpegCodec::onGetScaledDimensions(float desiredScale) const {
dinfo.image_height = this->getInfo().height();
dinfo.global_state = DSTATE_READY;
dinfo.num_components = 0;
- dinfo.scale_num = 1;
- dinfo.scale_denom = scale;
+ dinfo.scale_num = num;
+ dinfo.scale_denom = denom;
jpeg_calc_output_dimensions(&dinfo);
// Return the calculated output dimensions for the given scale
@@ -204,31 +196,6 @@ SkISize SkJpegCodec::onGetScaledDimensions(float desiredScale) const {
}
/*
- * Checks if the conversion between the input image and the requested output
- * image has been implemented
- */
-static bool conversion_possible(const SkImageInfo& dst,
- const SkImageInfo& src) {
- // Ensure that the profile type is unchanged
- if (dst.profileType() != src.profileType()) {
- return false;
- }
-
- // Ensure that the alpha type is opaque
- if (kOpaque_SkAlphaType != dst.alphaType()) {
- return false;
- }
-
- // Always allow kN32 as the color type
- if (kN32_SkColorType == dst.colorType()) {
- return true;
- }
-
- // Otherwise require that the destination color type match our recommendation
- return dst.colorType() == src.colorType();
-}
-
-/*
* Handles rewinding the input stream if it is necessary
*/
bool SkJpegCodec::handleRewind() {
@@ -253,6 +220,60 @@ bool SkJpegCodec::handleRewind() {
}
/*
+ * Checks if the conversion between the input image and the requested output
+ * image has been implemented
+ * Sets the output color space
+ */
+bool SkJpegCodec::setOutputColorSpace(const SkImageInfo& dst) {
+ const SkImageInfo& src = this->getInfo();
+
+ // Ensure that the profile type is unchanged
+ if (dst.profileType() != src.profileType()) {
+ return false;
+ }
+
+ // Ensure that the alpha type is opaque
+ if (kOpaque_SkAlphaType != dst.alphaType()) {
+ return false;
+ }
+
+ // Check if we will decode to CMYK because a conversion to RGBA is not supported
+ bool isCMYK = JCS_CMYK == fDecoderMgr->dinfo()->jpeg_color_space ||
scroggo 2015/06/16 20:39:22 nit: I find this more readable if you use a tempor
msarett 2015/06/18 21:06:10 Done.
+ JCS_YCCK == fDecoderMgr->dinfo()->jpeg_color_space;
+
+ // Check for valid color types and set the output color space
+ switch (dst.colorType()) {
+ case kN32_SkColorType:
+ if (isCMYK) {
+ fDecoderMgr->dinfo()->out_color_space = JCS_CMYK;
+ } else {
+ fDecoderMgr->dinfo()->out_color_space = JCS_EXT_RGBA;
+ }
+ return true;
+ case kRGB_565_SkColorType:
+ if (isCMYK) {
+ return false;
+ } else {
+ //fDecoderMgr->dinfo()->out_color_space = JCS_EXT_RGBA;
+ // TODO (msarett):
scroggo 2015/06/16 20:39:22 What is the TODO here?
msarett 2015/06/18 21:06:10 This has been done! The original issue was caused
+ fDecoderMgr->dinfo()->out_color_space = JCS_RGB565;
+ }
+ return true;
+ case kGray_8_SkColorType:
+ if (isCMYK) {
+ return false;
+ } else {
+ // We will enable decodes to gray even if the image is color because this is
scroggo 2015/06/16 20:39:22 Does libjpeg(-turbo) support converting full color
msarett 2015/06/18 21:06:10 I have no recollection of writing this comment, an
+ // much faster than decoding to color and then converting
+ fDecoderMgr->dinfo()->out_color_space = JCS_GRAYSCALE;
+ }
+ return true;
+ default:
+ return false;
+ }
+}
+
+/*
* Checks if we can scale to the requested dimensions and scales the dimensions
* if possible
*/
@@ -260,37 +281,26 @@ bool SkJpegCodec::scaleToDimensions(uint32_t dstWidth, uint32_t dstHeight) {
// libjpeg can scale to 1/1, 1/2, 1/4, and 1/8
SkASSERT(1 == fDecoderMgr->dinfo()->scale_num);
SkASSERT(1 == fDecoderMgr->dinfo()->scale_denom);
+ fDecoderMgr->dinfo()->scale_denom = 8;
jpeg_calc_output_dimensions(fDecoderMgr->dinfo());
while (fDecoderMgr->dinfo()->output_width != dstWidth ||
fDecoderMgr->dinfo()->output_height != dstHeight) {
// Return a failure if we have tried all of the possible scales
- if (8 == fDecoderMgr->dinfo()->scale_denom ||
- dstWidth > fDecoderMgr->dinfo()->output_width ||
- dstHeight > fDecoderMgr->dinfo()->output_height) {
+ if (16 == fDecoderMgr->dinfo()->scale_num ||
+ dstWidth < fDecoderMgr->dinfo()->output_width ||
+ dstHeight < fDecoderMgr->dinfo()->output_height) {
return fDecoderMgr->returnFalse("could not scale to requested dimensions");
}
// Try the next scale
- fDecoderMgr->dinfo()->scale_denom *= 2;
+ fDecoderMgr->dinfo()->scale_num += 1;
jpeg_calc_output_dimensions(fDecoderMgr->dinfo());
}
return true;
}
/*
- * Create the swizzler based on the encoded format
- */
-void SkJpegCodec::initializeSwizzler(const SkImageInfo& dstInfo,
- void* dst, size_t dstRowBytes,
- const Options& options) {
- SkSwizzler::SrcConfig srcConfig = get_src_config(*fDecoderMgr->dinfo());
- fSwizzler.reset(SkSwizzler::CreateSwizzler(srcConfig, NULL, dstInfo, dst, dstRowBytes,
- options.fZeroInitialized));
- fSrcRowBytes = SkSwizzler::BytesPerPixel(srcConfig) * dstInfo.width();
-}
-
-/*
* Performs the jpeg decode
*/
SkCodec::Result SkJpegCodec::onGetPixels(const SkImageInfo& dstInfo,
@@ -310,13 +320,14 @@ SkCodec::Result SkJpegCodec::onGetPixels(const SkImageInfo& dstInfo,
return fDecoderMgr->returnFailure("setjmp", kInvalidInput);
}
- // Check if we can decode to the requested destination
- if (!conversion_possible(dstInfo, this->getInfo())) {
+ // Check if we can decode to the requested destination and set the output color space
+ if (!this->setOutputColorSpace(dstInfo)) {
return fDecoderMgr->returnFailure("conversion_possible", kInvalidConversion);
}
// Perform the necessary scaling
if (!this->scaleToDimensions(dstInfo.width(), dstInfo.height())) {
+ SkDebugf("POOPY\n");
scroggo 2015/06/16 20:39:22 lol
msarett 2015/06/18 21:06:10 Probably not necessary...
fDecoderMgr->returnFailure("cannot scale to requested dims", kInvalidScale);
scroggo 2015/06/16 20:39:22 Should this say *return* fDecoderMgr->returnFailur
msarett 2015/06/18 21:06:10 Yes!
}
@@ -325,54 +336,25 @@ SkCodec::Result SkJpegCodec::onGetPixels(const SkImageInfo& dstInfo,
return fDecoderMgr->returnFailure("startDecompress", kInvalidInput);
}
- // Create the swizzler
- this->initializeSwizzler(dstInfo, dst, dstRowBytes, options);
- if (NULL == fSwizzler) {
- return fDecoderMgr->returnFailure("getSwizzler", kUnimplemented);
- }
-
- // This is usually 1, but can also be 2 or 4.
- // If we wanted to always read one row at a time, we could, but we will save space and time
- // by using the recommendation from libjpeg.
- const uint32_t rowsPerDecode = dinfo->rec_outbuf_height;
- SkASSERT(rowsPerDecode <= 4);
-
- // Create a buffer to contain decoded rows (libjpeg requires a 2D array)
- SkASSERT(0 != fSrcRowBytes);
- SkAutoTDeleteArray<uint8_t> srcBuffer(SkNEW_ARRAY(uint8_t, fSrcRowBytes * rowsPerDecode));
- JSAMPLE* srcRows[4];
- uint8_t* srcPtr = srcBuffer.get();
- for (uint8_t i = 0; i < rowsPerDecode; i++) {
- srcRows[i] = (JSAMPLE*) srcPtr;
- srcPtr += fSrcRowBytes;
- }
+ // The recommended output buffer height should always be 1 in high quality modes
+ SkDEBUGCODE(const uint32_t rowsPerDecode = dinfo->rec_outbuf_height;)
scroggo 2015/06/16 20:39:22 This variable is never used after the assert. How
msarett 2015/06/18 21:06:10 Done.
+ SkASSERT(1 == rowsPerDecode);
- // Ensure that we loop enough times to decode all of the rows
- // libjpeg will prevent us from reading past the bottom of the image
+ // Perform the decode a single row at a time
uint32_t dstHeight = dstInfo.height();
- for (uint32_t y = 0; y < dstHeight + rowsPerDecode - 1; y += rowsPerDecode) {
+ JSAMPLE* dstRow = (JSAMPLE*) dst;
+ for (uint32_t y = 0; y < dstHeight; y++) {
// Read rows of the image
- uint32_t rowsDecoded = jpeg_read_scanlines(dinfo, srcRows, rowsPerDecode);
-
- // Convert to RGB if necessary
- if (JCS_CMYK == dinfo->out_color_space) {
- convert_CMYK_to_RGB(srcRows[0], dstInfo.width() * rowsDecoded);
- }
-
- // Swizzle to output destination
- for (uint32_t i = 0; i < rowsDecoded; i++) {
- fSwizzler->next(srcRows[i]);
- }
+ uint32_t rowsDecoded = jpeg_read_scanlines(dinfo, &dstRow, 1);
// If we cannot read enough rows, assume the input is incomplete
- if (rowsDecoded < rowsPerDecode && y + rowsDecoded < dstHeight) {
+ if (rowsDecoded != 1) {
// Fill the remainder of the image with black. This error handling
// behavior is unspecified but SkCodec consistently uses black as
// the fill color for opaque images. If the destination is kGray,
// the low 8 bits of SK_ColorBLACK will be used. Conveniently,
// these are zeros, which is the representation for black in kGray.
- SkSwizzler::Fill(fSwizzler->getDstRow(), dstInfo, dstRowBytes,
- dstHeight - y - rowsDecoded, SK_ColorBLACK, NULL);
+ SkSwizzler::Fill(dstRow, dstInfo, dstRowBytes, dstHeight - y, SK_ColorBLACK, NULL);
// Prevent libjpeg from failing on incomplete decode
dinfo->output_scanline = dstHeight;
@@ -381,6 +363,14 @@ SkCodec::Result SkJpegCodec::onGetPixels(const SkImageInfo& dstInfo,
jpeg_finish_decompress(dinfo);
return fDecoderMgr->returnFailure("Incomplete image data", kIncompleteInput);
}
+
+ // Convert to RGBA if necessary
+ if (JCS_CMYK == dinfo->out_color_space) {
+ convert_CMYK_to_RGBA(dstRow, dstInfo.width());
+ }
+
+ // Move to the next row
+ dstRow = SkTAddOffset<JSAMPLE>(dstRow, dstRowBytes);
}
jpeg_finish_decompress(dinfo);
@@ -396,8 +386,6 @@ public:
: INHERITED(dstInfo)
, fCodec(codec)
{
- fStorage.reset(fCodec->fSrcRowBytes);
- fSrcRow = static_cast<uint8_t*>(fStorage.get());
}
SkImageGenerator::Result onGetScanlines(void* dst, int count, size_t rowBytes) override {
@@ -407,23 +395,23 @@ public:
}
// Read rows one at a time
+ JSAMPLE* dstRow = (JSAMPLE*) dst;
for (int y = 0; y < count; y++) {
// Read row of the image
- uint32_t rowsDecoded = jpeg_read_scanlines(fCodec->fDecoderMgr->dinfo(), &fSrcRow, 1);
+ uint32_t rowsDecoded = jpeg_read_scanlines(fCodec->fDecoderMgr->dinfo(), &dstRow, 1);
scroggo 2015/06/16 20:39:23 Wait, we don't need to do any conversion? (besides
msarett 2015/06/18 21:06:10 Yes we do. As it happens, this works on Android b
if (rowsDecoded != 1) {
- SkSwizzler::Fill(dst, this->dstInfo(), rowBytes, count - y, SK_ColorBLACK, NULL);
+ SkSwizzler::Fill(dstRow, this->dstInfo(), rowBytes, count - y, SK_ColorBLACK, NULL);
+ fCodec->fDecoderMgr->dinfo()->output_scanline = this->dstInfo().height();
scroggo 2015/06/16 20:39:22 Do we need to call jpeg_finish_decompress? (whethe
msarett 2015/06/18 21:06:10 Yes we should call this! I think I wrongly assume
return SkImageGenerator::kIncompleteInput;
}
- // Convert to RGB if necessary
+ // Convert to RGBA if necessary
if (JCS_CMYK == fCodec->fDecoderMgr->dinfo()->out_color_space) {
- convert_CMYK_to_RGB(fSrcRow, dstInfo().width());
+ convert_CMYK_to_RGBA(dstRow, this->dstInfo().width());
}
- // Swizzle to output destination
- fCodec->fSwizzler->setDstRow(dst);
- fCodec->fSwizzler->next(fSrcRow);
- dst = SkTAddOffset<void>(dst, rowBytes);
+ // Move to the next row
+ dstRow = SkTAddOffset<JSAMPLE>(dstRow, rowBytes);
}
return SkImageGenerator::kSuccess;
@@ -435,10 +423,7 @@ public:
return fCodec->fDecoderMgr->returnFailure("setjmp", SkImageGenerator::kInvalidInput);
}
- // Read rows but ignore the output
- for (int y = 0; y < count; y++) {
- jpeg_read_scanlines(fCodec->fDecoderMgr->dinfo(), &fSrcRow, 1);
- }
+ jpeg_skip_scanlines(fCodec->fDecoderMgr->dinfo(), count);
return SkImageGenerator::kSuccess;
}
@@ -454,8 +439,6 @@ public:
private:
SkJpegCodec* fCodec; // unowned
- SkAutoMalloc fStorage;
- uint8_t* fSrcRow; // ptr into fStorage
typedef SkScanlineDecoder INHERITED;
};
@@ -475,15 +458,15 @@ SkScanlineDecoder* SkJpegCodec::onGetScanlineDecoder(const SkImageInfo& dstInfo,
return NULL;
}
- // Check if we can decode to the requested destination
- if (!conversion_possible(dstInfo, this->getInfo())) {
+ // Check if we can decode to the requested destination and set the output color space
+ if (!this->setOutputColorSpace(dstInfo)) {
SkCodecPrintf("Cannot convert to output type\n");
return NULL;
}
// Perform the necessary scaling
if (!this->scaleToDimensions(dstInfo.width(), dstInfo.height())) {
- SkCodecPrintf("Cannot scale ot output dimensions\n");
+ SkCodecPrintf("Cannot scale to output dimensions\n");
return NULL;
}
@@ -493,13 +476,6 @@ SkScanlineDecoder* SkJpegCodec::onGetScanlineDecoder(const SkImageInfo& dstInfo,
return NULL;
}
- // Create the swizzler
- this->initializeSwizzler(dstInfo, NULL, dstInfo.minRowBytes(), options);
- if (NULL == fSwizzler) {
- SkCodecPrintf("Could not create swizzler\n");
- return NULL;
- }
-
// Return the new scanline decoder
return SkNEW_ARGS(SkJpegScanlineDecoder, (dstInfo, this));
}

Powered by Google App Engine
This is Rietveld 408576698