#include "SkColorPriv.h"
#include "SkImageEncoderFns.h"
+#include "SkImageInfoPriv.h"
+#include "SkJpegEncoder.h"
#include "SkJPEGWriteUtility.h"
#include "SkStream.h"
#include "SkTemplates.h"
#include "jerror.h"
}
-/**
- * Returns true if |info| is supported by the jpeg encoder and false otherwise.
- * |jpegColorType| will be set to the proper libjpeg-turbo type for input to the library.
- * |numComponents| will be set to the number of components in the |jpegColorType|.
- * |proc| will be set if we need to pre-convert the input before passing to
- * libjpeg-turbo. Otherwise will be set to nullptr.
- */
-// TODO (skbug.com/1501):
-// Should we fail on non-opaque encodes?
-// Or should we change alpha behavior (ex: unpremultiply when the input is premul)?
-// Or is ignoring the alpha type and alpha channel ok here?
-static bool set_encode_config(J_COLOR_SPACE* jpegColorType, int* numComponents,
- transform_scanline_proc* proc, const SkImageInfo& info) {
- *proc = nullptr;
- switch (info.colorType()) {
+// This warning triggers false postives way too often in here.
+#if defined(__GNUC__) && !defined(__clang__)
+ #pragma GCC diagnostic ignored "-Wclobbered"
+#endif
+
+class SkJpegEncoderMgr : SkNoncopyable {
+public:
+
+ /*
+ * Create the decode manager
+ * Does not take ownership of stream
+ */
+ static std::unique_ptr<SkJpegEncoderMgr> Make(SkWStream* stream) {
+ return std::unique_ptr<SkJpegEncoderMgr>(new SkJpegEncoderMgr(stream));
+ }
+
+ bool setParams(const SkImageInfo& srcInfo);
+
+ jpeg_compress_struct* cinfo() { return &fCInfo; }
+
+ jmp_buf& jmpBuf() { return fErrMgr.fJmpBuf; }
+
+ transform_scanline_proc proc() const { return fProc; }
+
+ ~SkJpegEncoderMgr() {
+ jpeg_destroy_compress(&fCInfo);
+ }
+
+private:
+
+ SkJpegEncoderMgr(SkWStream* stream)
+ : fDstMgr(stream)
+ , fProc(nullptr)
+ {
+ fCInfo.err = jpeg_std_error(&fErrMgr);
+ fErrMgr.error_exit = skjpeg_error_exit;
+ jpeg_create_compress(&fCInfo);
+ fCInfo.dest = &fDstMgr;
+ }
+
+ jpeg_compress_struct fCInfo;
+ skjpeg_error_mgr fErrMgr;
+ skjpeg_destination_mgr fDstMgr;
+ transform_scanline_proc fProc;
+};
+
+bool SkJpegEncoderMgr::setParams(const SkImageInfo& srcInfo) {
+ J_COLOR_SPACE jpegColorType = JCS_EXT_RGBA;
+ int numComponents = 0;
+ switch (srcInfo.colorType()) {
case kRGBA_8888_SkColorType:
- *jpegColorType = JCS_EXT_RGBA;
- *numComponents = 4;
- return true;
+ jpegColorType = JCS_EXT_RGBA;
+ numComponents = 4;
+ break;
case kBGRA_8888_SkColorType:
- *jpegColorType = JCS_EXT_BGRA;
- *numComponents = 4;
- return true;
+ jpegColorType = JCS_EXT_BGRA;
+ numComponents = 4;
+ break;
case kRGB_565_SkColorType:
- *proc = transform_scanline_565;
- *jpegColorType = JCS_RGB;
- *numComponents = 3;
- return true;
+ fProc = transform_scanline_565;
+ jpegColorType = JCS_RGB;
+ numComponents = 3;
+ break;
case kARGB_4444_SkColorType:
- *proc = transform_scanline_444;
- *jpegColorType = JCS_RGB;
- *numComponents = 3;
- return true;
+ fProc = transform_scanline_444;
+ jpegColorType = JCS_RGB;
+ numComponents = 3;
+ break;
case kIndex_8_SkColorType:
- *proc = transform_scanline_index8_opaque;
- *jpegColorType = JCS_RGB;
- *numComponents = 3;
- return true;
+ fProc = transform_scanline_index8_opaque;
+ jpegColorType = JCS_RGB;
+ numComponents = 3;
+ break;
case kGray_8_SkColorType:
- SkASSERT(info.isOpaque());
- *jpegColorType = JCS_GRAYSCALE;
- *numComponents = 1;
- return true;
+ SkASSERT(srcInfo.isOpaque());
+ jpegColorType = JCS_GRAYSCALE;
+ numComponents = 1;
+ break;
case kRGBA_F16_SkColorType:
- if (!info.colorSpace() || !info.colorSpace()->gammaIsLinear()) {
+ if (!srcInfo.colorSpace() || !srcInfo.colorSpace()->gammaIsLinear()) {
return false;
}
- *proc = transform_scanline_F16_to_8888;
- *jpegColorType = JCS_EXT_RGBA;
- *numComponents = 4;
- return true;
+ fProc = transform_scanline_F16_to_8888;
+ jpegColorType = JCS_EXT_RGBA;
+ numComponents = 4;
+ break;
default:
return false;
}
+ fCInfo.image_width = srcInfo.width();
+ fCInfo.image_height = srcInfo.height();
+ fCInfo.in_color_space = jpegColorType;
+ fCInfo.input_components = numComponents;
+ jpeg_set_defaults(&fCInfo);
+ // Tells libjpeg-turbo to compute optimal Huffman coding tables
+ // for the image. This improves compression at the cost of
+ // slower encode performance.
+ fCInfo.optimize_coding = TRUE;
+ return true;
}
-bool SkEncodeImageAsJPEG(SkWStream* stream, const SkPixmap& pixmap, const SkEncodeOptions& opts) {
- if (SkTransferFunctionBehavior::kRespect == opts.fUnpremulBehavior) {
- // Respecting the transfer function requries a color space. It's not actually critical
- // in the jpeg case (since jpegs are opaque), but Skia color correct behavior generally
- // requires pixels to be tagged with color spaces.
- if (!pixmap.colorSpace() || (!pixmap.colorSpace()->gammaCloseToSRGB() &&
- !pixmap.colorSpace()->gammaIsLinear())) {
- return false;
- }
- }
+class SkJpegEncoder_Base : public SkJpegEncoder {
+public:
+ SkJpegEncoder_Base(std::unique_ptr<SkJpegEncoderMgr> encoderMgr, const SkPixmap& src);
- return SkEncodeImageAsJPEG(stream, pixmap, 100);
-}
+ bool onEncodeRows(int numRows);
-bool SkEncodeImageAsJPEG(SkWStream* stream, const SkPixmap& pixmap, int quality) {
- if (!pixmap.addr()) {
- return false;
- }
- jpeg_compress_struct cinfo;
- skjpeg_error_mgr sk_err;
- skjpeg_destination_mgr sk_wstream(stream);
+private:
+ std::unique_ptr<SkJpegEncoderMgr> fEncoderMgr;
+ SkPixmap fSrc;
+ int fCurrRow;
+ SkAutoTMalloc<uint8_t> fStorage;
+};
- // Declare before calling setjmp.
- SkAutoTMalloc<uint8_t> storage;
-
- cinfo.err = jpeg_std_error(&sk_err);
- sk_err.error_exit = skjpeg_error_exit;
- if (setjmp(sk_err.fJmpBuf)) {
- return false;
+std::unique_ptr<SkJpegEncoder> SkJpegEncoder::Make(SkWStream* dst, const SkPixmap& src,
+ const Options& options) {
+ if (!SkImageInfoIsValidAllowNumericalCS(src.info()) || !src.addr() ||
+ src.rowBytes() < src.info().minRowBytes()) {
+ return nullptr;
}
- J_COLOR_SPACE jpegColorSpace;
- int numComponents;
- transform_scanline_proc proc;
- if (!set_encode_config(&jpegColorSpace, &numComponents, &proc, pixmap.info())) {
- return false;
+ std::unique_ptr<SkJpegEncoderMgr> encoderMgr = SkJpegEncoderMgr::Make(dst);
+ if (setjmp(encoderMgr->jmpBuf())) {
+ return nullptr;
}
- jpeg_create_compress(&cinfo);
- cinfo.dest = &sk_wstream;
- cinfo.image_width = pixmap.width();
- cinfo.image_height = pixmap.height();
- cinfo.input_components = numComponents;
- cinfo.in_color_space = jpegColorSpace;
-
- jpeg_set_defaults(&cinfo);
-
- // Tells libjpeg-turbo to compute optimal Huffman coding tables
- // for the image. This improves compression at the cost of
- // slower encode performance.
- cinfo.optimize_coding = TRUE;
- jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */);
+ if (!encoderMgr->setParams(src.info())) {
+ return nullptr;
+ }
- jpeg_start_compress(&cinfo, TRUE);
+ jpeg_set_quality(encoderMgr->cinfo(), options.fQuality, TRUE);
+ jpeg_start_compress(encoderMgr->cinfo(), TRUE);
- if (pixmap.colorSpace()) {
- sk_sp<SkData> icc = icc_from_color_space(*pixmap.colorSpace());
+ if (src.colorSpace()) {
+ sk_sp<SkData> icc = icc_from_color_space(*src.colorSpace());
if (icc) {
// Create a contiguous block of memory with the icc signature followed by the profile.
sk_sp<SkData> markerData =
*ptr++ = 1; // Out of one total markers.
memcpy(ptr, icc->data(), icc->size());
- jpeg_write_marker(&cinfo, kICCMarker, markerData->bytes(), markerData->size());
+ jpeg_write_marker(encoderMgr->cinfo(), kICCMarker, markerData->bytes(),
+ markerData->size());
}
}
- if (proc) {
- storage.reset(numComponents * pixmap.width());
+ return std::unique_ptr<SkJpegEncoder>(new SkJpegEncoder_Base(std::move(encoderMgr), src));
+}
+
+
+SkJpegEncoder_Base::SkJpegEncoder_Base(std::unique_ptr<SkJpegEncoderMgr> encoderMgr,
+ const SkPixmap& src)
+ : fEncoderMgr(std::move(encoderMgr))
+ , fSrc(src)
+ , fCurrRow(0)
+ , fStorage(fEncoderMgr->proc() ? fEncoderMgr->cinfo()->input_components*src.width() : 0)
+{}
+
+bool SkJpegEncoder::encodeRows(int numRows) {
+ return ((SkJpegEncoder_Base*) this)->onEncodeRows(numRows);
+}
+
+bool SkJpegEncoder_Base::onEncodeRows(int numRows) {
+ SkASSERT(numRows > 0 && fCurrRow < fSrc.height());
+ if (numRows <= 0 || fCurrRow >= fSrc.height()) {
+ return false;
+ }
+
+ if (fCurrRow + numRows > fSrc.height()) {
+ numRows = fSrc.height() - fCurrRow;
+ }
+
+ if (setjmp(fEncoderMgr->jmpBuf())) {
+ // Short circuit any future calls after failing.
+ fCurrRow = fSrc.height();
+ return false;
}
- const void* srcRow = pixmap.addr();
- const SkPMColor* colors = pixmap.ctable() ? pixmap.ctable()->readColors() : nullptr;
- while (cinfo.next_scanline < cinfo.image_height) {
+ const void* srcRow = fSrc.addr(0, fCurrRow);
+ const SkPMColor* colors = fSrc.ctable() ? fSrc.ctable()->readColors() : nullptr;
+ for (int i = 0; i < numRows; i++) {
JSAMPLE* jpegSrcRow = (JSAMPLE*) srcRow;
- if (proc) {
- proc((char*)storage.get(), (const char*)srcRow, pixmap.width(), numComponents, colors);
- jpegSrcRow = storage.get();
+ if (fEncoderMgr->proc()) {
+ fEncoderMgr->proc()((char*)fStorage.get(), (const char*)srcRow, fSrc.width(),
+ fEncoderMgr->cinfo()->input_components, colors);
+ jpegSrcRow = fStorage.get();
}
- (void) jpeg_write_scanlines(&cinfo, &jpegSrcRow, 1);
- srcRow = SkTAddOffset<const void>(srcRow, pixmap.rowBytes());
+ jpeg_write_scanlines(fEncoderMgr->cinfo(), &jpegSrcRow, 1);
+ srcRow = SkTAddOffset<const void>(srcRow, fSrc.rowBytes());
}
- jpeg_finish_compress(&cinfo);
- jpeg_destroy_compress(&cinfo);
+ fCurrRow += numRows;
+ if (fCurrRow == fSrc.height()) {
+ jpeg_finish_compress(fEncoderMgr->cinfo());
+ }
return true;
}
+
+bool SkJpegEncoder::Encode(SkWStream* dst, const SkPixmap& src, const Options& options) {
+ auto encoder = SkJpegEncoder::Make(dst, src, options);
+ return encoder.get() && encoder->encodeRows(src.height());
+}
+
#endif