| OLD | NEW | 
|---|
| 1 /* | 1 /* | 
| 2  * Copyright 2007 The Android Open Source Project | 2  * Copyright 2007 The Android Open Source Project | 
| 3  * | 3  * | 
| 4  * Use of this source code is governed by a BSD-style license that can be | 4  * Use of this source code is governed by a BSD-style license that can be | 
| 5  * found in the LICENSE file. | 5  * found in the LICENSE file. | 
| 6  */ | 6  */ | 
| 7 | 7 | 
| 8 | 8 | 
| 9 #include "SkImageDecoder.h" | 9 #include "SkImageDecoder.h" | 
| 10 #include "SkImageEncoder.h" | 10 #include "SkImageEncoder.h" | 
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 47 SK_CONF_DECLARE(bool, c_suppressJPEGImageDecoderWarnings, | 47 SK_CONF_DECLARE(bool, c_suppressJPEGImageDecoderWarnings, | 
| 48                 "images.jpeg.suppressDecoderWarnings", | 48                 "images.jpeg.suppressDecoderWarnings", | 
| 49                 DEFAULT_FOR_SUPPRESS_JPEG_IMAGE_DECODER_WARNINGS, | 49                 DEFAULT_FOR_SUPPRESS_JPEG_IMAGE_DECODER_WARNINGS, | 
| 50                 "Suppress most JPG warnings when calling decode functions."); | 50                 "Suppress most JPG warnings when calling decode functions."); | 
| 51 SK_CONF_DECLARE(bool, c_suppressJPEGImageDecoderErrors, | 51 SK_CONF_DECLARE(bool, c_suppressJPEGImageDecoderErrors, | 
| 52                 "images.jpeg.suppressDecoderErrors", | 52                 "images.jpeg.suppressDecoderErrors", | 
| 53                 DEFAULT_FOR_SUPPRESS_JPEG_IMAGE_DECODER_ERRORS, | 53                 DEFAULT_FOR_SUPPRESS_JPEG_IMAGE_DECODER_ERRORS, | 
| 54                 "Suppress most JPG error messages when decode " | 54                 "Suppress most JPG error messages when decode " | 
| 55                 "function fails."); | 55                 "function fails."); | 
| 56 | 56 | 
|  | 57 #if defined(SK_BUILD_FOR_ANDROID) && !defined(SK_JPEG_NO_INDEX_SUPPORTED) | 
|  | 58 #define SK_JPEG_INDEX_SUPPORTED | 
|  | 59 #endif | 
|  | 60 | 
| 57 ////////////////////////////////////////////////////////////////////////// | 61 ////////////////////////////////////////////////////////////////////////// | 
| 58 ////////////////////////////////////////////////////////////////////////// | 62 ////////////////////////////////////////////////////////////////////////// | 
| 59 | 63 | 
| 60 static void do_nothing_emit_message(jpeg_common_struct*, int) { | 64 static void do_nothing_emit_message(jpeg_common_struct*, int) { | 
| 61     /* do nothing */ | 65     /* do nothing */ | 
| 62 } | 66 } | 
| 63 static void do_nothing_output_message(j_common_ptr) { | 67 static void do_nothing_output_message(j_common_ptr) { | 
| 64     /* do nothing */ | 68     /* do nothing */ | 
| 65 } | 69 } | 
| 66 | 70 | 
| (...skipping 11 matching lines...) Expand all  Loading... | 
| 78     } | 82     } | 
| 79     /* To suppress error messages with a SK_DEBUG binary, set the | 83     /* To suppress error messages with a SK_DEBUG binary, set the | 
| 80      * environment variable "skia_images_jpeg_suppressDecoderErrors" | 84      * environment variable "skia_images_jpeg_suppressDecoderErrors" | 
| 81      * to "true".  Inside a program that links to skia: | 85      * to "true".  Inside a program that links to skia: | 
| 82      * SK_CONF_SET("images.jpeg.suppressDecoderErrors", true); */ | 86      * SK_CONF_SET("images.jpeg.suppressDecoderErrors", true); */ | 
| 83     if (c_suppressJPEGImageDecoderErrors) { | 87     if (c_suppressJPEGImageDecoderErrors) { | 
| 84         cinfo->err->output_message = &do_nothing_output_message; | 88         cinfo->err->output_message = &do_nothing_output_message; | 
| 85     } | 89     } | 
| 86 } | 90 } | 
| 87 | 91 | 
| 88 #ifdef SK_BUILD_FOR_ANDROID | 92 #ifdef SK_JPEG_INDEX_SUPPORTED | 
| 89 class SkJPEGImageIndex { | 93 class SkJPEGImageIndex { | 
| 90 public: | 94 public: | 
| 91     // Takes ownership of stream. | 95     // Takes ownership of stream. | 
| 92     SkJPEGImageIndex(SkStreamRewindable* stream, SkImageDecoder* decoder) | 96     SkJPEGImageIndex(SkStreamRewindable* stream, SkImageDecoder* decoder) | 
| 93         : fSrcMgr(stream, decoder) | 97         : fSrcMgr(stream, decoder) | 
| 94         , fStream(stream) | 98         , fStream(stream) | 
| 95         , fInfoInitialized(false) | 99         , fInfoInitialized(false) | 
| 96         , fHuffmanCreated(false) | 100         , fHuffmanCreated(false) | 
| 97         , fDecompressStarted(false) | 101         , fDecompressStarted(false) | 
| 98         { | 102         { | 
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 196     huffman_index fHuffmanIndex; | 200     huffman_index fHuffmanIndex; | 
| 197     bool fInfoInitialized; | 201     bool fInfoInitialized; | 
| 198     bool fHuffmanCreated; | 202     bool fHuffmanCreated; | 
| 199     bool fDecompressStarted; | 203     bool fDecompressStarted; | 
| 200     SkDEBUGCODE(bool fReadHeaderSucceeded;) | 204     SkDEBUGCODE(bool fReadHeaderSucceeded;) | 
| 201 }; | 205 }; | 
| 202 #endif | 206 #endif | 
| 203 | 207 | 
| 204 class SkJPEGImageDecoder : public SkImageDecoder { | 208 class SkJPEGImageDecoder : public SkImageDecoder { | 
| 205 public: | 209 public: | 
| 206 #ifdef SK_BUILD_FOR_ANDROID | 210 #ifdef SK_JPEG_INDEX_SUPPORTED | 
| 207     SkJPEGImageDecoder() { | 211     SkJPEGImageDecoder() { | 
| 208         fImageIndex = nullptr; | 212         fImageIndex = nullptr; | 
| 209         fImageWidth = 0; | 213         fImageWidth = 0; | 
| 210         fImageHeight = 0; | 214         fImageHeight = 0; | 
| 211     } | 215     } | 
| 212 | 216 | 
| 213     virtual ~SkJPEGImageDecoder() { delete fImageIndex; } | 217     virtual ~SkJPEGImageDecoder() { delete fImageIndex; } | 
| 214 #endif | 218 #endif | 
| 215 | 219 | 
| 216     Format getFormat() const override { | 220     Format getFormat() const override { | 
| 217         return kJPEG_Format; | 221         return kJPEG_Format; | 
| 218     } | 222     } | 
| 219 | 223 | 
| 220 protected: | 224 protected: | 
| 221 #ifdef SK_BUILD_FOR_ANDROID | 225 #ifdef SK_JPEG_INDEX_SUPPORTED | 
| 222     bool onBuildTileIndex(SkStreamRewindable *stream, int *width, int *height) o
      verride; | 226     bool onBuildTileIndex(SkStreamRewindable *stream, int *width, int *height) o
      verride; | 
| 223     bool onDecodeSubset(SkBitmap* bitmap, const SkIRect& rect) override; | 227     bool onDecodeSubset(SkBitmap* bitmap, const SkIRect& rect) override; | 
| 224 #endif | 228 #endif | 
| 225     Result onDecode(SkStream* stream, SkBitmap* bm, Mode) override; | 229     Result onDecode(SkStream* stream, SkBitmap* bm, Mode) override; | 
| 226     bool onDecodeYUV8Planes(SkStream* stream, SkISize componentSizes[3], | 230     bool onDecodeYUV8Planes(SkStream* stream, SkISize componentSizes[3], | 
| 227                             void* planes[3], size_t rowBytes[3], | 231                             void* planes[3], size_t rowBytes[3], | 
| 228                             SkYUVColorSpace* colorSpace) override; | 232                             SkYUVColorSpace* colorSpace) override; | 
| 229 | 233 | 
| 230 private: | 234 private: | 
| 231 #ifdef SK_BUILD_FOR_ANDROID | 235 #ifdef SK_JPEG_INDEX_SUPPORTED | 
| 232     SkJPEGImageIndex* fImageIndex; | 236     SkJPEGImageIndex* fImageIndex; | 
| 233     int fImageWidth; | 237     int fImageWidth; | 
| 234     int fImageHeight; | 238     int fImageHeight; | 
| 235 #endif | 239 #endif | 
| 236 | 240 | 
| 237     /** | 241     /** | 
| 238      *  Determine the appropriate bitmap colortype and out_color_space based on | 242      *  Determine the appropriate bitmap colortype and out_color_space based on | 
| 239      *  both the preference of the caller and the jpeg_color_space on the | 243      *  both the preference of the caller and the jpeg_color_space on the | 
| 240      *  jpeg_decompress_struct passed in. | 244      *  jpeg_decompress_struct passed in. | 
| 241      *  Must be called after jpeg_read_header. | 245      *  Must be called after jpeg_read_header. | 
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 288     for (int i = 0; i < count; i++) { | 292     for (int i = 0; i < count; i++) { | 
| 289         JSAMPLE* rowptr = (JSAMPLE*)buffer; | 293         JSAMPLE* rowptr = (JSAMPLE*)buffer; | 
| 290         int row_count = jpeg_read_scanlines(cinfo, &rowptr, 1); | 294         int row_count = jpeg_read_scanlines(cinfo, &rowptr, 1); | 
| 291         if (1 != row_count) { | 295         if (1 != row_count) { | 
| 292             return false; | 296             return false; | 
| 293         } | 297         } | 
| 294     } | 298     } | 
| 295     return true; | 299     return true; | 
| 296 } | 300 } | 
| 297 | 301 | 
| 298 #ifdef SK_BUILD_FOR_ANDROID | 302 #ifdef SK_JPEG_INDEX_SUPPORTED | 
| 299 static bool skip_src_rows_tile(jpeg_decompress_struct* cinfo, | 303 static bool skip_src_rows_tile(jpeg_decompress_struct* cinfo, | 
| 300                                huffman_index *index, void* buffer, int count) { | 304                                huffman_index *index, void* buffer, int count) { | 
| 301     for (int i = 0; i < count; i++) { | 305     for (int i = 0; i < count; i++) { | 
| 302         JSAMPLE* rowptr = (JSAMPLE*)buffer; | 306         JSAMPLE* rowptr = (JSAMPLE*)buffer; | 
| 303         int row_count = jpeg_read_tile_scanline(cinfo, index, &rowptr); | 307         int row_count = jpeg_read_tile_scanline(cinfo, index, &rowptr); | 
| 304         if (1 != row_count) { | 308         if (1 != row_count) { | 
| 305             return false; | 309             return false; | 
| 306         } | 310         } | 
| 307     } | 311     } | 
| 308     return true; | 312     return true; | 
| (...skipping 13 matching lines...) Expand all  Loading... | 
| 322                  cinfo.err->msg_code, buffer, caller, width, height); | 326                  cinfo.err->msg_code, buffer, caller, width, height); | 
| 323     } | 327     } | 
| 324 } | 328 } | 
| 325 | 329 | 
| 326 static bool return_false(const jpeg_decompress_struct& cinfo, | 330 static bool return_false(const jpeg_decompress_struct& cinfo, | 
| 327                          const char caller[]) { | 331                          const char caller[]) { | 
| 328     print_jpeg_decoder_errors(cinfo, 0, 0, caller); | 332     print_jpeg_decoder_errors(cinfo, 0, 0, caller); | 
| 329     return false; | 333     return false; | 
| 330 } | 334 } | 
| 331 | 335 | 
| 332 #ifdef SK_BUILD_FOR_ANDROID | 336 #ifdef SK_JPEG_INDEX_SUPPORTED | 
| 333 static bool return_false(const jpeg_decompress_struct& cinfo, | 337 static bool return_false(const jpeg_decompress_struct& cinfo, | 
| 334                          const SkBitmap& bm, const char caller[]) { | 338                          const SkBitmap& bm, const char caller[]) { | 
| 335     print_jpeg_decoder_errors(cinfo, bm.width(), bm.height(), caller); | 339     print_jpeg_decoder_errors(cinfo, bm.width(), bm.height(), caller); | 
| 336     return false; | 340     return false; | 
| 337 } | 341 } | 
| 338 #endif | 342 #endif | 
| 339 | 343 | 
| 340 static SkImageDecoder::Result return_failure(const jpeg_decompress_struct& cinfo
      , | 344 static SkImageDecoder::Result return_failure(const jpeg_decompress_struct& cinfo
      , | 
| 341                                              const SkBitmap& bm, const char call
      er[]) { | 345                                              const SkBitmap& bm, const char call
      er[]) { | 
| 342     print_jpeg_decoder_errors(cinfo, bm.width(), bm.height(), caller); | 346     print_jpeg_decoder_errors(cinfo, bm.width(), bm.height(), caller); | 
| (...skipping 589 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 932 | 936 | 
| 933     if (nullptr != colorSpace) { | 937     if (nullptr != colorSpace) { | 
| 934         *colorSpace = kJPEG_SkYUVColorSpace; | 938         *colorSpace = kJPEG_SkYUVColorSpace; | 
| 935     } | 939     } | 
| 936 | 940 | 
| 937     return true; | 941     return true; | 
| 938 } | 942 } | 
| 939 | 943 | 
| 940 /////////////////////////////////////////////////////////////////////////////// | 944 /////////////////////////////////////////////////////////////////////////////// | 
| 941 | 945 | 
| 942 #ifdef SK_BUILD_FOR_ANDROID | 946 #ifdef SK_JPEG_INDEX_SUPPORTED | 
| 943 bool SkJPEGImageDecoder::onBuildTileIndex(SkStreamRewindable* stream, int *width
      , int *height) { | 947 bool SkJPEGImageDecoder::onBuildTileIndex(SkStreamRewindable* stream, int *width
      , int *height) { | 
| 944     SkAutoTDelete<SkJPEGImageIndex> imageIndex(new SkJPEGImageIndex(stream, this
      )); | 948     SkAutoTDelete<SkJPEGImageIndex> imageIndex(new SkJPEGImageIndex(stream, this
      )); | 
| 945 | 949 | 
| 946     skjpeg_error_mgr sk_err; | 950     skjpeg_error_mgr sk_err; | 
| 947     set_error_mgr(imageIndex->cinfo(), &sk_err); | 951     set_error_mgr(imageIndex->cinfo(), &sk_err); | 
| 948 | 952 | 
| 949     // All objects need to be instantiated before this setjmp call so that | 953     // All objects need to be instantiated before this setjmp call so that | 
| 950     // they will be cleaned up properly if an error occurs. | 954     // they will be cleaned up properly if an error occurs. | 
| 951     if (setjmp(sk_err.fJmpBuf)) { | 955     if (setjmp(sk_err.fJmpBuf)) { | 
| 952         return false; | 956         return false; | 
| (...skipping 493 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 1446     return SkImageDecoder::kUnknown_Format; | 1450     return SkImageDecoder::kUnknown_Format; | 
| 1447 } | 1451 } | 
| 1448 | 1452 | 
| 1449 static SkImageEncoder* sk_libjpeg_efactory(SkImageEncoder::Type t) { | 1453 static SkImageEncoder* sk_libjpeg_efactory(SkImageEncoder::Type t) { | 
| 1450     return (SkImageEncoder::kJPEG_Type == t) ? new SkJPEGImageEncoder : nullptr; | 1454     return (SkImageEncoder::kJPEG_Type == t) ? new SkJPEGImageEncoder : nullptr; | 
| 1451 } | 1455 } | 
| 1452 | 1456 | 
| 1453 static SkImageDecoder_DecodeReg gDReg(sk_libjpeg_dfactory); | 1457 static SkImageDecoder_DecodeReg gDReg(sk_libjpeg_dfactory); | 
| 1454 static SkImageDecoder_FormatReg gFormatReg(get_format_jpeg); | 1458 static SkImageDecoder_FormatReg gFormatReg(get_format_jpeg); | 
| 1455 static SkImageEncoder_EncodeReg gEReg(sk_libjpeg_efactory); | 1459 static SkImageEncoder_EncodeReg gEReg(sk_libjpeg_efactory); | 
| OLD | NEW | 
|---|