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

Unified Diff: src/codec/SkJpegCodec.cpp

Issue 1549473003: Add getYUV8Planes() API to SkCodec (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 5 years 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 7db772da6386ad9a3b0002d5334598a625fd6fa2..1db618b532c22be3c4392c91d1c5bb3664f3b933 100644
--- a/src/codec/SkJpegCodec.cpp
+++ b/src/codec/SkJpegCodec.cpp
@@ -452,3 +452,185 @@ bool SkJpegCodec::onSkipScanlines(int count) {
return (uint32_t) count == jpeg_skip_scanlines(fDecoderMgr->dinfo(), count);
}
+
+static bool is_yuv_supported(jpeg_decompress_struct* dinfo) {
+ // Scaling is not supported in raw data mode.
+ SkASSERT(dinfo->scale_num == dinfo->scale_denom);
+
+ // I can't imagine that this would ever change, but we do depend on it.
+ SkASSERT(8 == DCTSIZE);
+
+ if (JCS_YCbCr != dinfo->jpeg_color_space) {
+ return false;
+ }
+
+ SkASSERT(3 == dinfo->num_components);
+ SkASSERT(dinfo->comp_info);
+
+ // FIXME: Support YUV for progressive images. The fix might be as easy as
+ // removing this check.
msarett 2015/12/22 21:01:36 Chromium supports progressive images (in a differe
+ if (dinfo->comps_in_scan < dinfo->num_components || dinfo->progressive_mode) {
+ return false;
+ }
+
+ // TODO: It is possible to perform a YUV decode for any combination of
+ // horizontal and vertical sampling that is supported by
+ // libjpeg/libjpeg-turbo. However, we will start by supporting
+ // only the common cases.
msarett 2015/12/22 21:01:36 Chromium and SkImageDecoder only support these com
+ bool unitUV = (1 == dinfo->comp_info[1].h_samp_factor) &&
+ (1 == dinfo->comp_info[1].v_samp_factor) &&
+ (1 == dinfo->comp_info[2].h_samp_factor) &&
+ (1 == dinfo->comp_info[2].v_samp_factor);
+
+ int hSampY = dinfo->comp_info[0].h_samp_factor;
+ int vSampY = dinfo->comp_info[0].v_samp_factor;
+ // TODO: Chromium reports the YUV subsampling factors to the client.
+ // Ex: 444, 422, 420, etc.
+ // Could it be useful to do the same here?
+ bool commonY = (1 == hSampY && 1 == vSampY) ||
+ (2 == hSampY && 1 == vSampY) ||
+ (2 == hSampY && 2 == vSampY) ||
+ (1 == hSampY && 2 == vSampY) ||
+ (4 == hSampY && 1 == vSampY) ||
+ (4 == hSampY && 2 == vSampY);
+
+ return unitUV && commonY;
+}
+
+bool SkJpegCodec::onGetYUV8Sizes(SkISize sizes[3]) const {
+ jpeg_decompress_struct* dinfo = fDecoderMgr->dinfo();
+ if (!is_yuv_supported(dinfo)) {
+ return false;
+ }
+
+ sizes[0].set(dinfo->comp_info[0].width_in_blocks * 8, dinfo->comp_info[0].downsampled_height);
+ sizes[1].set(dinfo->comp_info[1].width_in_blocks * 8, dinfo->comp_info[1].downsampled_height);
+ sizes[2].set(dinfo->comp_info[2].width_in_blocks * 8, dinfo->comp_info[2].downsampled_height);
+ return true;
+}
+
+SkCodec::Result SkJpegCodec::onGetYUV8Planes(SkISize sizes[3], void* pixels[3], size_t rowBytes[3],
+ SkYUVColorSpace* colorSpace) {
+ SkISize recommendedSizes[3];
+ // This will check is_yuv_supported(), so we don't need to here.
+ bool supportsYUV = this->onGetYUV8Sizes(recommendedSizes);
+ if (!supportsYUV || memcmp((const void*) recommendedSizes, (const void*) sizes, sizeof(sizes)))
+ {
+ fDecoderMgr->returnFailure("onGetYUV8Sizes", kInvalidInput);
+ }
+
+ // Set the jump location for libjpeg errors
+ if (setjmp(fDecoderMgr->getJmpBuf())) {
+ return fDecoderMgr->returnFailure("setjmp", kInvalidInput);
+ }
+
+ // Get a pointer to the decompress info since we will use it quite frequently
+ jpeg_decompress_struct* dinfo = fDecoderMgr->dinfo();
+
+ dinfo->raw_data_out = TRUE;
+ if (!jpeg_start_decompress(dinfo)) {
+ return fDecoderMgr->returnFailure("startDecompress", kInvalidInput);
+ }
+
+ // A previous implementation claims that the return value of is_yuv_supported()
+ // may change after calling jpeg_start_decompress(). It looks to me like this
+ // was caused by a bug in the old code, but we'll be safe and check here.
+ SkASSERT(is_yuv_supported(dinfo));
+
+ // TODO: As mentioned in is_yuv_supported(), we are requiring that U and V be
+ // the same size and Y be equal to the image size (possibly with padding
+ // on the width). This is by far the most common case, but technically we
+ // could support other combinations of sampling factors.
+ SkASSERT(sizes[1] == sizes[2]);
+ SkASSERT((uint32_t) sizes[0].height() == dinfo->output_height);
+
+ // Build a JSAMPIMAGE to handle output from libjpeg-turbo. A JSAMPIMAGE has
+ // a 2-D array of pixels for each of the components (Y, U, V) in the image.
+ // Cheat Sheet:
+ // JSAMPIMAGE == JSAMPLEARRAY* == JSAMPROW** == JSAMPLE***
+ JSAMPARRAY yuv[3];
+ // Set aside enough space for pointers to rows of Y, U, and V.
+ JSAMPROW rowptrs[16 + 8 + 8];
+ // TODO: Note that if we support more sampling factors, the amount of space set
+ // aside here and/or which pointers we use for Y, U, and V may change.
+ // This would also add complexity to the code below.
+ yuv[0] = &rowptrs[0]; // Y rows (8 or 16)
+ yuv[1] = &rowptrs[16]; // U rows (8)
+ yuv[2] = &rowptrs[24]; // V rows (8)
+
+ // Initialize rowptrs.
+ int numYRowsPerBlock = 8 * dinfo->comp_info[0].v_samp_factor;
+ for (int i = 0; i < numYRowsPerBlock; i++) {
+ rowptrs[i] = (JSAMPROW) SkTAddOffset<void>(pixels[0], i * rowBytes[0]);
+ }
+ for (int i = 0; i < 8; i++) {
+ rowptrs[i + 16] =(JSAMPROW) SkTAddOffset<void>(pixels[1], i * rowBytes[1]);
+ rowptrs[i + 24] = (JSAMPROW) SkTAddOffset<void>(pixels[2], i * rowBytes[2]);
+ }
+
+ // After each loop iteration, we will increment pointers to Y, U, and V.
+ int blockIncrementY = numYRowsPerBlock * rowBytes[0];
+ int blockIncrementU = 8 * rowBytes[1];
+ int blockIncrementV = 8 * rowBytes[2];
+
+ uint32_t numRowsPerBlock = numYRowsPerBlock;
+ // We intentionally round down here. As a final step, we may need to decode one
+ // more partial block.
+ int numIters = dinfo->output_height / numRowsPerBlock;
+ int iters = 0;
+ while (iters < numIters) {
+ JDIMENSION linesRead = jpeg_read_raw_data(dinfo, yuv, numRowsPerBlock);
+ if (linesRead < numRowsPerBlock) {
+ // FIXME: We will treat this as an error for now.
+ // Can we fill memory and still display an image?
+ return kIncompleteInput;
+ }
+
+ // Update rowptrs.
+ for (int i = 0; i < numYRowsPerBlock; i++) {
+ rowptrs[i] += blockIncrementY;
+ }
+ for (int i = 0; i < 8; i++) {
+ rowptrs[i + 16] += blockIncrementU;
+ rowptrs[i + 24] += blockIncrementV;
+ }
+ iters++;
+ }
+
+ uint32_t remainingRows = dinfo->output_height - dinfo->output_scanline;
+ SkASSERT(remainingRows == dinfo->output_height % numRowsPerBlock);
+ SkASSERT(dinfo->output_scanline == numIters * numRowsPerBlock);
+ if (remainingRows > 0) {
+ // libjpeg-turbo needs memory to be padded by the block sizes. We will fulfill
+ // this requirement using a dummy row buffer.
+ SkAutoTMalloc<JSAMPLE> dummyRow(rowBytes[0]);
+ for (int i = remainingRows; i < numYRowsPerBlock; i++) {
+ rowptrs[i] = dummyRow.get();
+ }
+ int remainingUVRows = dinfo->comp_info[1].downsampled_height - 8 * numIters;
+ for (int i = remainingUVRows; i < 8; i++) {
+ rowptrs[i + 16] = dummyRow.get();
+ rowptrs[i + 24] = dummyRow.get();
+ }
+
+ JDIMENSION linesRead = jpeg_read_raw_data(dinfo, yuv, numRowsPerBlock);
+ if (linesRead < remainingRows) {
+ // FIXME: We will treat this as an error for now.
+ // Can we fill memory and still display an image?
+ return kIncompleteInput;
+ }
+ }
+
+ // FIXME: This is ugly. We should find a solution where the input memory is
+ // padded appropriately, but we are able to report the true width from
+ // the start.
+ sizes[0].fWidth = dinfo->comp_info[0].downsampled_width;
+ sizes[1].fWidth = dinfo->comp_info[1].downsampled_width;
+ sizes[2].fWidth = dinfo->comp_info[2].downsampled_width;
+
+ if (nullptr != colorSpace) {
+ *colorSpace = kJPEG_SkYUVColorSpace;
msarett 2015/12/22 21:01:36 Can someone comment on if/why this is useful? Wou
+ }
+
+ return kSuccess;
+}

Powered by Google App Engine
This is Rietveld 408576698