OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
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 #ifndef SkDecodingImageGenerator_DEFINED | 8 #ifndef SkDecodingImageGenerator_DEFINED |
9 #define SkDecodingImageGenerator_DEFINED | 9 #define SkDecodingImageGenerator_DEFINED |
10 | 10 |
11 #include "SkBitmap.h" | 11 #include "SkDiscardableMemory.h" |
12 #include "SkImageGenerator.h" | 12 #include "SkImageGenerator.h" |
| 13 #include "SkImageInfo.h" |
13 | 14 |
14 class SkData; | 15 class SkBitmap; |
15 class SkStreamRewindable; | 16 class SkStreamRewindable; |
16 | 17 |
17 /** | 18 /** |
18 * An implementation of SkImageGenerator that calls into | 19 * Calls into SkImageDecoder::DecodeMemoryToTarget to implement a |
19 * SkImageDecoder. | 20 * SkImageGenerator |
20 */ | 21 */ |
21 class SkDecodingImageGenerator : public SkImageGenerator { | 22 class SkDecodingImageGenerator : public SkImageGenerator { |
22 public: | 23 public: |
23 virtual ~SkDecodingImageGenerator(); | 24 /* |
24 virtual SkData* refEncodedData() SK_OVERRIDE; | 25 * The constructor will take a reference to the SkData. The |
25 // This implementaion of getInfo() always returns true. | 26 * destructor will unref() it. |
26 virtual bool getInfo(SkImageInfo* info) SK_OVERRIDE; | 27 */ |
27 virtual bool getPixels(const SkImageInfo& info, | 28 explicit SkDecodingImageGenerator(SkData* data); |
28 void* pixels, | 29 |
29 size_t rowBytes) SK_OVERRIDE; | 30 /* |
30 /** | 31 * The SkData version of this constructor is preferred. If the |
31 * These options will be passed on to the image decoder. The | 32 * stream has an underlying SkData (such as a SkMemoryStream) |
32 * defaults are sensible. | 33 * pass that in. |
33 * | 34 * |
34 * @param fSampleSize If set to > 1, tells the decoder to return a | 35 * This object will unref the stream when done. Since streams |
35 * smaller than original bitmap, sampling 1 pixel for | 36 * have internal state (position), the caller should not pass a |
36 * every size pixels. e.g. if sample size is set to 3, | 37 * shared stream in. Pass either a new duplicated stream in or |
37 * then the returned bitmap will be 1/3 as wide and high, | 38 * transfer ownership of the stream. In the latter case, be sure |
38 * and will contain 1/9 as many pixels as the original. | 39 * that there are no other consumers of the stream who will |
39 * Note: this is a hint, and the codec may choose to | 40 * modify the stream's position. This constructor asserts |
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 | |
85 * stream->unique(). | 41 * stream->unique(). |
86 * | 42 * |
87 * For example: | 43 * For example: |
88 * SkStreamRewindable* stream; | 44 * SkStreamRewindable* stream; |
89 * ... | 45 * ... |
90 * SkImageGenerator* gen | 46 * SkImageGenerator* gen |
91 * = SkDecodingImageGenerator::Create( | 47 * = SkNEW_ARGS(SkDecodingImageGenerator, |
92 * stream->duplicate(), SkDecodingImageGenerator::Options()); | 48 * (stream->duplicate())); |
93 * ... | 49 * ... |
94 * SkDELETE(gen); | 50 * SkDELETE(gen); |
95 * | |
96 * @param Options (see above) | |
97 * | |
98 * @return NULL on failure, a new SkImageGenerator on success. | |
99 */ | 51 */ |
100 static SkImageGenerator* Create(SkStreamRewindable* stream, | 52 explicit SkDecodingImageGenerator(SkStreamRewindable* stream); |
101 const Options& opt); | 53 |
| 54 virtual ~SkDecodingImageGenerator(); |
| 55 |
| 56 virtual SkData* refEncodedData() SK_OVERRIDE; |
| 57 |
| 58 virtual bool getInfo(SkImageInfo* info) SK_OVERRIDE; |
| 59 |
| 60 virtual bool getPixels(const SkImageInfo& info, |
| 61 void* pixels, |
| 62 size_t rowBytes) SK_OVERRIDE; |
102 | 63 |
103 /** | 64 /** |
104 * @param data Contains the encoded image data that will be used by | 65 * Install the SkData into the destination bitmap, using a new |
105 * the SkDecodingImageGenerator. Will be ref()ed by the | 66 * SkDiscardablePixelRef and a new SkDecodingImageGenerator. |
106 * SkImageGenerator constructor and and unref()ed on deletion. | 67 * |
| 68 * @param data Contains the encoded image data that will be used |
| 69 * by the SkDecodingImageGenerator. Will be ref()ed. |
| 70 * |
| 71 * @param destination Upon success, this bitmap will be |
| 72 * configured and have a pixelref installed. |
| 73 * |
| 74 * @param factory If not NULL, this object will be used as a |
| 75 * source of discardable memory when decoding. If NULL, then |
| 76 * SkDiscardableMemory::Create() will be called. |
| 77 * |
| 78 * @return true iff successful. |
107 */ | 79 */ |
108 static SkImageGenerator* Create(SkData* data, const Options& opt); | 80 static bool Install(SkData* data, SkBitmap* destination, |
| 81 SkDiscardableMemory::Factory* factory = NULL); |
| 82 /** |
| 83 * Install the stream into the destination bitmap, using a new |
| 84 * SkDiscardablePixelRef and a new SkDecodingImageGenerator. |
| 85 * |
| 86 * The SkData version of this function is preferred. If the |
| 87 * stream has an underlying SkData (such as a SkMemoryStream) |
| 88 * pass that in. |
| 89 * |
| 90 * @param stream The source of encoded data that will be passed |
| 91 * to the decoder. The installed SkDecodingImageGenerator will |
| 92 * unref the stream when done. If false is returned, this |
| 93 * function will perform the unref. Since streams have internal |
| 94 * state (position), the caller should not pass a shared stream |
| 95 * in. Pass either a new duplicated stream in or transfer |
| 96 * ownership of the stream. In the latter case, be sure that |
| 97 * there are no other consumers of the stream who will modify the |
| 98 * stream's position. This function will fail if |
| 99 * (!stream->unique()). |
| 100 * |
| 101 * @param destination Upon success, this bitmap will be |
| 102 * configured and have a pixelref installed. |
| 103 * |
| 104 * @param factory If not NULL, this object will be used as a |
| 105 * source of discardable memory when decoding. If NULL, then |
| 106 * SkDiscardableMemory::Create() will be called. |
| 107 * |
| 108 * @return true iff successful. |
| 109 */ |
| 110 static bool Install(SkStreamRewindable* stream, SkBitmap* destination, |
| 111 SkDiscardableMemory::Factory* factory = NULL); |
109 | 112 |
110 private: | 113 private: |
111 SkData* fData; | 114 SkData* fData; |
112 SkStreamRewindable* fStream; | 115 SkStreamRewindable* fStream; |
113 const SkImageInfo fInfo; | 116 SkImageInfo fInfo; |
114 const int fSampleSize; | 117 bool fHasInfo; |
115 const bool fDitherImage; | 118 bool fDoCopyTo; |
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; | |
126 }; | 119 }; |
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 | |
141 #endif // SkDecodingImageGenerator_DEFINED | 120 #endif // SkDecodingImageGenerator_DEFINED |
OLD | NEW |