Call standard libjpeg/libjpeg-turbo APIs
authormsarett <msarett@google.com>
Tue, 1 Sep 2015 13:43:41 +0000 (06:43 -0700)
committerCommit bot <commit-bot@chromium.org>
Tue, 1 Sep 2015 13:43:41 +0000 (06:43 -0700)
BUG=skia:

Review URL: https://codereview.chromium.org/1322623004

gyp/codec.gyp
src/codec/SkJpegCodec.cpp
src/codec/SkJpegCodec.h
src/codec/SkJpegDecoderMgr.cpp
src/codec/SkJpegDecoderMgr.h
src/codec/SkJpegUtility_codec.cpp
src/codec/SkJpegUtility_codec.h

index a9a55d56386a28768b5189770ac97bc2a08af482..a99707e4eaefe4c6cb3834f48c19e00915a1dfaf 100644 (file)
@@ -81,8 +81,8 @@
             'export_dependent_settings': [
               'libjpeg-turbo.gyp:libjpeg-turbo',
             ],
-            'cflags': [
-              '-DTURBO_HAS_SKIP',
+            'defines': [
+              'TURBO_HAS_SKIP',
             ],
           }
         ]
index d985337775599fc68188a4c3f6831467a213bd23..2baf51a00925db4055e53158d040324f565f062f 100644 (file)
@@ -21,9 +21,7 @@
 #include <stdio.h>
 
 extern "C" {
-    #include "jpeglibmangler.h"
     #include "jerror.h"
-    #include "jpegint.h"
     #include "jpeglib.h"
 }
 
@@ -112,7 +110,7 @@ bool SkJpegCodec::ReadHeader(SkStream* stream, SkCodec** codecOut,
     decoderMgr->init();
 
     // Read the jpeg header
-    if (JPEG_HEADER_OK != chromium_jpeg_read_header(decoderMgr->dinfo(), true)) {
+    if (JPEG_HEADER_OK != jpeg_read_header(decoderMgr->dinfo(), true)) {
         return decoderMgr->returnFalse("read_header");
     }
 
@@ -147,6 +145,7 @@ SkJpegCodec::SkJpegCodec(const SkImageInfo& srcInfo, SkStream* stream,
         JpegDecoderMgr* decoderMgr)
     : INHERITED(srcInfo, stream)
     , fDecoderMgr(decoderMgr)
+    , fReadyState(decoderMgr->dinfo()->global_state)
 {}
 
 /*
@@ -188,11 +187,11 @@ SkISize SkJpegCodec::onGetScaledDimensions(float desiredScale) const {
     sk_bzero(&dinfo, sizeof(dinfo));
     dinfo.image_width = this->getInfo().width();
     dinfo.image_height = this->getInfo().height();
-    dinfo.global_state = DSTATE_READY;
+    dinfo.global_state = fReadyState;
     dinfo.num_components = 0;
     dinfo.scale_num = num;
     dinfo.scale_denom = denom;
-    chromium_jpeg_calc_output_dimensions(&dinfo);
+    jpeg_calc_output_dimensions(&dinfo);
 
     // Return the calculated output dimensions for the given scale
     return SkISize::Make(dinfo.output_width, dinfo.output_height);
@@ -274,7 +273,7 @@ bool SkJpegCodec::nativelyScaleToDimensions(uint32_t dstWidth, uint32_t dstHeigh
     // libjpeg-turbo can scale to 1/8, 1/4, 3/8, 1/2, 5/8, 3/4, 7/8, and 1/1
     fDecoderMgr->dinfo()->scale_denom = 8;
     fDecoderMgr->dinfo()->scale_num = 8;
-    chromium_jpeg_calc_output_dimensions(fDecoderMgr->dinfo());
+    jpeg_calc_output_dimensions(fDecoderMgr->dinfo());
     while (fDecoderMgr->dinfo()->output_width != dstWidth ||
             fDecoderMgr->dinfo()->output_height != dstHeight) {
 
@@ -284,13 +283,13 @@ bool SkJpegCodec::nativelyScaleToDimensions(uint32_t dstWidth, uint32_t dstHeigh
                 dstHeight > fDecoderMgr->dinfo()->output_height) {
             // reset native scale settings on failure because this may be supported by the swizzler 
             this->fDecoderMgr->dinfo()->scale_num = 8;
-            chromium_jpeg_calc_output_dimensions(this->fDecoderMgr->dinfo());
+            jpeg_calc_output_dimensions(this->fDecoderMgr->dinfo());
             return false;
         }
 
         // Try the next scale
         fDecoderMgr->dinfo()->scale_num -= 1;
-        chromium_jpeg_calc_output_dimensions(fDecoderMgr->dinfo());
+        jpeg_calc_output_dimensions(fDecoderMgr->dinfo());
     }
     return true;
 }
@@ -330,7 +329,7 @@ SkCodec::Result SkJpegCodec::onGetPixels(const SkImageInfo& dstInfo,
     }
 
     // Now, given valid output dimensions, we can start the decompress
-    if (!chromium_jpeg_start_decompress(dinfo)) {
+    if (!jpeg_start_decompress(dinfo)) {
         return fDecoderMgr->returnFailure("startDecompress", kInvalidInput);
     }
 
@@ -343,7 +342,7 @@ SkCodec::Result SkJpegCodec::onGetPixels(const SkImageInfo& dstInfo,
     JSAMPLE* dstRow = (JSAMPLE*) dst;
     for (uint32_t y = 0; y < dstHeight; y++) {
         // Read rows of the image
-        uint32_t rowsDecoded = chromium_jpeg_read_scanlines(dinfo, &dstRow, 1);
+        uint32_t rowsDecoded = jpeg_read_scanlines(dinfo, &dstRow, 1);
 
         // If we cannot read enough rows, assume the input is incomplete
         if (rowsDecoded != 1) {
@@ -362,7 +361,7 @@ SkCodec::Result SkJpegCodec::onGetPixels(const SkImageInfo& dstInfo,
             dinfo->output_scanline = dstHeight;
 
             // Finish the decode and indicate that the input was incomplete.
-            chromium_jpeg_finish_decompress(dinfo);
+            jpeg_finish_decompress(dinfo);
             return fDecoderMgr->returnFailure("Incomplete image data", kIncompleteInput);
         }
 
@@ -374,7 +373,7 @@ SkCodec::Result SkJpegCodec::onGetPixels(const SkImageInfo& dstInfo,
         // Move to the next row
         dstRow = SkTAddOffset<JSAMPLE>(dstRow, dstRowBytes);
     }
-    chromium_jpeg_finish_decompress(dinfo);
+    jpeg_finish_decompress(dinfo);
 
     return kSuccess;
 }
@@ -471,7 +470,7 @@ public:
         }
 
         // Now, given valid output dimensions, we can start the decompress
-        if (!chromium_jpeg_start_decompress(fCodec->fDecoderMgr->dinfo())) {
+        if (!jpeg_start_decompress(fCodec->fDecoderMgr->dinfo())) {
             SkCodecPrintf("start decompress failed\n");
             return SkCodec::kInvalidInput;
         }
@@ -490,7 +489,7 @@ public:
         // We may not have decoded the entire image.  Prevent libjpeg-turbo from failing on a
         // partial decode.
         fCodec->fDecoderMgr->dinfo()->output_scanline = fCodec->getInfo().height();
-        chromium_jpeg_finish_decompress(fCodec->fDecoderMgr->dinfo());
+        jpeg_finish_decompress(fCodec->fDecoderMgr->dinfo());
     }
 
     SkCodec::Result onGetScanlines(void* dst, int count, size_t rowBytes) override {
@@ -510,8 +509,7 @@ public:
 
         for (int y = 0; y < count; y++) {
             // Read row of the image
-            uint32_t rowsDecoded =
-                    chromium_jpeg_read_scanlines(fCodec->fDecoderMgr->dinfo(), &dstRow, 1);
+            uint32_t rowsDecoded = jpeg_read_scanlines(fCodec->fDecoderMgr->dinfo(), &dstRow, 1);
             if (rowsDecoded != 1) {
                 SkSwizzler::Fill(dstRow, this->dstInfo(), rowBytes, count - y,
                         SK_ColorBLACK, nullptr, fOpts.fZeroInitialized);
@@ -538,11 +536,11 @@ public:
 #ifndef TURBO_HAS_SKIP
 // TODO (msarett): Make this a member function and avoid reallocating the
 //                 memory buffer on each call to skip.
-#define chromium_jpeg_skip_scanlines(dinfo, count)                           \
+#define jpeg_skip_scanlines(dinfo, count)                                    \
     SkAutoMalloc storage(get_row_bytes(dinfo));                              \
     uint8_t* storagePtr = static_cast<uint8_t*>(storage.get());              \
     for (int y = 0; y < count; y++) {                                        \
-        chromium_jpeg_read_scanlines(dinfo, &storagePtr, 1);                 \
+        jpeg_read_scanlines(dinfo, &storagePtr, 1);                          \
     }
 #endif
 
@@ -552,7 +550,7 @@ public:
             return fCodec->fDecoderMgr->returnFailure("setjmp", SkCodec::kInvalidInput);
         }
 
-        chromium_jpeg_skip_scanlines(fCodec->fDecoderMgr->dinfo(), count);
+        jpeg_skip_scanlines(fCodec->fDecoderMgr->dinfo(), count);
 
         return SkCodec::kSuccess;
     }
index ce4fe56d58e552d70d2965e44e5924da4616c455..38e41e757cbe82334cb845d313cc54bd2ede1c68 100644 (file)
@@ -116,6 +116,9 @@ private:
     bool nativelyScaleToDimensions(uint32_t width, uint32_t height); 
 
     SkAutoTDelete<JpegDecoderMgr> fDecoderMgr;
+    // We will save the state of the decompress struct after reading the header.
+    // This allows us to safely call onGetScaledDimensions() at any time.
+    const int                     fReadyState;
     
     friend class SkJpegScanlineDecoder;
 
index 769e4c164510f2c34cd7d6855ab210ecd24950f0..9e79dda9585f0cfd62f9051d262b690537b7b5d6 100644 (file)
@@ -51,7 +51,7 @@ JpegDecoderMgr::JpegDecoderMgr(SkStream* stream)
     , fInit(false)
 {
     // Error manager must be set before any calls to libjeg in order to handle failures
-    fDInfo.err = chromium_jpeg_std_error(&fErrorMgr);
+    fDInfo.err = jpeg_std_error(&fErrorMgr);
     fErrorMgr.error_exit = skjpeg_err_exit;
 }
 
index a09a175b8dce7e84e295af497936b3befea7b238..8e938ad53549e04b8e38532e3c2787b7f5bbf1a2 100644 (file)
@@ -17,7 +17,6 @@
 #include <stdio.h>
 
 extern "C" {
-    #include "jpeglibmangler.h"
     #include "jpeglib.h"
 }
 
index 157366c735faedde1b9b26ac861f719e1066161d..75a562a00454f45f8f0904d2f8a055fc1f65b006 100644 (file)
@@ -73,7 +73,7 @@ skjpeg_source_mgr::skjpeg_source_mgr(SkStream* stream)
     init_source = sk_init_source;
     fill_input_buffer = sk_fill_input_buffer;
     skip_input_data = sk_skip_input_data;
-    resync_to_restart = chromium_jpeg_resync_to_restart;
+    resync_to_restart = jpeg_resync_to_restart;
     term_source = sk_term_source;
 }
 
index 4b3f1ab23b19f4a4944a552147f96797e0f3e02f..43391017b53cad8e0085cb2abbba66470050cae3 100644 (file)
@@ -16,7 +16,6 @@
 #include <stdio.h>
 
 extern "C" {
-    #include "jpeglibmangler.h"
     #include "jpeglib.h"
     #include "jerror.h"
 }