|
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 |
|
11 #include "SkBitmap.h" |
|
12 #include "SkImageGenerator.h" |
|
13 |
|
14 class SkData; |
|
15 class SkStreamRewindable; |
|
16 |
|
17 /** |
|
18 * An implementation of SkImageGenerator that calls into |
|
19 * SkImageDecoder. |
|
20 */ |
|
21 class SkDecodingImageGenerator : public SkImageGenerator { |
|
22 public: |
|
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. |
|
33 * |
|
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 |
|
85 * stream->unique(). |
|
86 * |
|
87 * For example: |
|
88 * SkStreamRewindable* stream; |
|
89 * ... |
|
90 * SkImageGenerator* gen |
|
91 * = SkDecodingImageGenerator::Create( |
|
92 * stream->duplicate(), SkDecodingImageGenerator::Options()); |
|
93 * ... |
|
94 * SkDELETE(gen); |
|
95 * |
|
96 * @param Options (see above) |
|
97 * |
|
98 * @return NULL on failure, a new SkImageGenerator on success. |
|
99 */ |
|
100 static SkImageGenerator* Create(SkStreamRewindable* stream, |
|
101 const Options& opt); |
|
102 |
|
103 /** |
|
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. |
|
107 */ |
|
108 static SkImageGenerator* Create(SkData* data, const Options& opt); |
|
109 |
|
110 private: |
|
111 SkData* fData; |
|
112 SkStreamRewindable* fStream; |
|
113 const SkImageInfo fInfo; |
|
114 const int fSampleSize; |
|
115 const bool fDitherImage; |
|
116 |
|
117 SkDecodingImageGenerator(SkData* data, |
|
118 SkStreamRewindable* stream, |
|
119 const SkImageInfo& info, |
|
120 int sampleSize, |
|
121 bool ditherImage); |
|
122 static SkImageGenerator* Create(SkData*, SkStreamRewindable*, |
|
123 const Options&); |
|
124 typedef SkImageGenerator INHERITED; |
|
125 }; |
|
126 |
|
127 // // Example of most basic use case: |
|
128 // |
|
129 // bool install_data(SkData* data, SkBitmap* dst) { |
|
130 // return SkInstallDiscardablePixelRef( |
|
131 // SkDecodingImageGenerator::Create( |
|
132 // data, SkDecodingImageGenerator::Options()), dst, NULL); |
|
133 // } |
|
134 // bool install_stream(SkStreamRewindable* stream, SkBitmap* dst) { |
|
135 // return SkInstallDiscardablePixelRef( |
|
136 // SkDecodingImageGenerator::Create( |
|
137 // stream, SkDecodingImageGenerator::Options()), dst, NULL); |
|
138 // } |
|
139 |
|
140 #endif // SkDecodingImageGenerator_DEFINED |