halcanary@google.com | ad04eb4 | 2013-11-21 15:32:08 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #ifndef SkDecodingImageGenerator_DEFINED |
| 9 | #define SkDecodingImageGenerator_DEFINED |
| 10 | |
halcanary@google.com | 3d50ea1 | 2014-01-02 13:15:13 +0000 | [diff] [blame] | 11 | #include "SkBitmap.h" |
halcanary@google.com | ad04eb4 | 2013-11-21 15:32:08 +0000 | [diff] [blame] | 12 | #include "SkImageGenerator.h" |
| 13 | |
halcanary@google.com | 3d50ea1 | 2014-01-02 13:15:13 +0000 | [diff] [blame] | 14 | class SkData; |
halcanary@google.com | 29d9693 | 2013-12-09 13:45:02 +0000 | [diff] [blame] | 15 | class SkStreamRewindable; |
halcanary@google.com | ad04eb4 | 2013-11-21 15:32:08 +0000 | [diff] [blame] | 16 | |
| 17 | /** |
halcanary@google.com | 3d50ea1 | 2014-01-02 13:15:13 +0000 | [diff] [blame] | 18 | * An implementation of SkImageGenerator that calls into |
| 19 | * SkImageDecoder. |
halcanary@google.com | ad04eb4 | 2013-11-21 15:32:08 +0000 | [diff] [blame] | 20 | */ |
| 21 | class SkDecodingImageGenerator : public SkImageGenerator { |
| 22 | public: |
halcanary@google.com | 3d50ea1 | 2014-01-02 13:15:13 +0000 | [diff] [blame] | 23 | virtual ~SkDecodingImageGenerator(); |
| 24 | virtual SkData* refEncodedData() SK_OVERRIDE; |
| 25 | // This implementaion of getInfo() always returns true. |
| 26 | virtual bool getInfo(SkImageInfo* info) SK_OVERRIDE; |
| 27 | virtual bool getPixels(const SkImageInfo& info, |
| 28 | void* pixels, |
| 29 | size_t rowBytes) SK_OVERRIDE; |
| 30 | /** |
| 31 | * These options will be passed on to the image decoder. The |
| 32 | * defaults are sensible. |
halcanary@google.com | d665dc4 | 2013-12-18 17:40:25 +0000 | [diff] [blame] | 33 | * |
halcanary@google.com | 3d50ea1 | 2014-01-02 13:15:13 +0000 | [diff] [blame] | 34 | * @param fSampleSize If set to > 1, tells the decoder to return a |
| 35 | * smaller than original bitmap, sampling 1 pixel for |
| 36 | * every size pixels. e.g. if sample size is set to 3, |
| 37 | * then the returned bitmap will be 1/3 as wide and high, |
| 38 | * and will contain 1/9 as many pixels as the original. |
| 39 | * Note: this is a hint, and the codec may choose to |
| 40 | * ignore this, or only approximate the sample size. |
| 41 | * |
| 42 | * @param fDitherImage Set to true if the the decoder should try to |
| 43 | * dither the resulting image when decoding to a smaller |
| 44 | * color-space. The default is true. |
| 45 | * |
| 46 | * @param fRequestedColorType If not given, then use whichever |
| 47 | * config the decoder wants. Else try to use this color |
| 48 | * type. If the decoder won't support this color type, |
| 49 | * SkDecodingImageGenerator::Create will return |
| 50 | * NULL. kIndex_8_SkColorType is not supported. |
| 51 | */ |
| 52 | struct Options { |
| 53 | Options() |
| 54 | : fSampleSize(1) |
| 55 | , fDitherImage(true) |
| 56 | , fUseRequestedColorType(false) |
| 57 | , fRequestedColorType() { } |
| 58 | Options(int sampleSize, bool dither) |
| 59 | : fSampleSize(sampleSize) |
| 60 | , fDitherImage(dither) |
| 61 | , fUseRequestedColorType(false) |
| 62 | , fRequestedColorType() { } |
| 63 | Options(int sampleSize, bool dither, SkColorType colorType) |
| 64 | : fSampleSize(sampleSize) |
| 65 | , fDitherImage(dither) |
| 66 | , fUseRequestedColorType(true) |
| 67 | , fRequestedColorType(colorType) { } |
| 68 | const int fSampleSize; |
| 69 | const bool fDitherImage; |
| 70 | const bool fUseRequestedColorType; |
| 71 | const SkColorType fRequestedColorType; |
| 72 | }; |
| 73 | |
| 74 | /** |
| 75 | * These two functions return a SkImageGenerator that calls into |
| 76 | * SkImageDecoder. They return NULL on failure. |
| 77 | * |
| 78 | * The SkData version of this function is preferred. If the stream |
| 79 | * has an underlying SkData (such as a SkMemoryStream) pass that in. |
| 80 | * |
| 81 | * This object will unref the stream when done or on failure. Since |
| 82 | * streams have internal state (position), the caller should not pass |
| 83 | * a shared stream in. Pass either a new duplicated stream in or |
| 84 | * transfer ownership of the stream. This factory asserts |
halcanary@google.com | 29d9693 | 2013-12-09 13:45:02 +0000 | [diff] [blame] | 85 | * stream->unique(). |
| 86 | * |
| 87 | * For example: |
| 88 | * SkStreamRewindable* stream; |
| 89 | * ... |
| 90 | * SkImageGenerator* gen |
halcanary@google.com | 3d50ea1 | 2014-01-02 13:15:13 +0000 | [diff] [blame] | 91 | * = SkDecodingImageGenerator::Create( |
| 92 | * stream->duplicate(), SkDecodingImageGenerator::Options()); |
halcanary@google.com | 29d9693 | 2013-12-09 13:45:02 +0000 | [diff] [blame] | 93 | * ... |
| 94 | * SkDELETE(gen); |
halcanary@google.com | 3d50ea1 | 2014-01-02 13:15:13 +0000 | [diff] [blame] | 95 | * |
| 96 | * @param Options (see above) |
| 97 | * |
| 98 | * @return NULL on failure, a new SkImageGenerator on success. |
halcanary@google.com | 29d9693 | 2013-12-09 13:45:02 +0000 | [diff] [blame] | 99 | */ |
halcanary@google.com | 3d50ea1 | 2014-01-02 13:15:13 +0000 | [diff] [blame] | 100 | static SkImageGenerator* Create(SkStreamRewindable* stream, |
| 101 | const Options& opt); |
halcanary@google.com | ad04eb4 | 2013-11-21 15:32:08 +0000 | [diff] [blame] | 102 | |
| 103 | /** |
halcanary@google.com | 3d50ea1 | 2014-01-02 13:15:13 +0000 | [diff] [blame] | 104 | * @param data Contains the encoded image data that will be used by |
| 105 | * the SkDecodingImageGenerator. Will be ref()ed by the |
| 106 | * SkImageGenerator constructor and and unref()ed on deletion. |
halcanary@google.com | ad04eb4 | 2013-11-21 15:32:08 +0000 | [diff] [blame] | 107 | */ |
halcanary@google.com | 3d50ea1 | 2014-01-02 13:15:13 +0000 | [diff] [blame] | 108 | static SkImageGenerator* Create(SkData* data, const Options& opt); |
halcanary@google.com | ad04eb4 | 2013-11-21 15:32:08 +0000 | [diff] [blame] | 109 | |
| 110 | private: |
halcanary@google.com | 3d50ea1 | 2014-01-02 13:15:13 +0000 | [diff] [blame] | 111 | SkData* fData; |
| 112 | SkStreamRewindable* fStream; |
| 113 | const SkImageInfo fInfo; |
| 114 | const int fSampleSize; |
| 115 | const bool fDitherImage; |
| 116 | const SkBitmap::Config fRequestedConfig; |
| 117 | SkDecodingImageGenerator(SkData* data, |
| 118 | SkStreamRewindable* stream, |
| 119 | const SkImageInfo& info, |
| 120 | int sampleSize, |
| 121 | bool ditherImage, |
| 122 | SkBitmap::Config requestedConfig); |
| 123 | static SkImageGenerator* Create(SkData*, SkStreamRewindable*, |
| 124 | const Options&); |
| 125 | typedef SkImageGenerator INHERITED; |
halcanary@google.com | ad04eb4 | 2013-11-21 15:32:08 +0000 | [diff] [blame] | 126 | }; |
halcanary@google.com | 3d50ea1 | 2014-01-02 13:15:13 +0000 | [diff] [blame] | 127 | |
| 128 | // // Example of most basic use case: |
| 129 | // |
| 130 | // bool install_data(SkData* data, SkBitmap* dst) { |
| 131 | // return SkInstallDiscardablePixelRef( |
| 132 | // SkDecodingImageGenerator::Create( |
| 133 | // data, SkDecodingImageGenerator::Options()), dst, NULL); |
| 134 | // } |
| 135 | // bool install_stream(SkStreamRewindable* stream, SkBitmap* dst) { |
| 136 | // return SkInstallDiscardablePixelRef( |
| 137 | // SkDecodingImageGenerator::Create( |
| 138 | // stream, SkDecodingImageGenerator::Options()), dst, NULL); |
| 139 | // } |
| 140 | |
halcanary@google.com | ad04eb4 | 2013-11-21 15:32:08 +0000 | [diff] [blame] | 141 | #endif // SkDecodingImageGenerator_DEFINED |