OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 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 SkCodec_DEFINED | 8 #ifndef SkCodec_DEFINED |
9 #define SkCodec_DEFINED | 9 #define SkCodec_DEFINED |
10 | 10 |
11 #include "SkImageGenerator.h" | 11 #include "SkImageGenerator.h" |
12 #include "SkImageInfo.h" | 12 #include "SkImageInfo.h" |
13 #include "SkSize.h" | 13 #include "SkSize.h" |
14 #include "SkTemplates.h" | 14 #include "SkTemplates.h" |
15 #include "SkTypes.h" | 15 #include "SkTypes.h" |
16 | 16 |
17 class SkData; | 17 class SkData; |
18 class SkScanlineDecoder; | |
18 class SkStream; | 19 class SkStream; |
19 | 20 |
20 /** | 21 /** |
21 * Abstraction layer directly on top of an image codec. | 22 * Abstraction layer directly on top of an image codec. |
22 */ | 23 */ |
23 class SkCodec : public SkImageGenerator { | 24 class SkCodec : public SkImageGenerator { |
24 public: | 25 public: |
25 /** | 26 /** |
26 * If this stream represents an encoded image that we know how to decode, | 27 * If this stream represents an encoded image that we know how to decode, |
27 * return an SkCodec that can decode it. Otherwise return NULL. | 28 * return an SkCodec that can decode it. Otherwise return NULL. |
(...skipping 13 matching lines...) Expand all Loading... | |
41 | 42 |
42 /** | 43 /** |
43 * Return a size that approximately supports the desired scale factor. | 44 * Return a size that approximately supports the desired scale factor. |
44 * The codec may not be able to scale efficiently to the exact scale | 45 * The codec may not be able to scale efficiently to the exact scale |
45 * factor requested, so return a size that approximates that scale. | 46 * factor requested, so return a size that approximates that scale. |
46 * | 47 * |
47 * FIXME: Move to SkImageGenerator? | 48 * FIXME: Move to SkImageGenerator? |
48 */ | 49 */ |
49 SkISize getScaledDimensions(float desiredScale) const; | 50 SkISize getScaledDimensions(float desiredScale) const; |
50 | 51 |
52 /** | |
53 * Create a new scanline decoder. | |
54 * | |
55 * Create a new object which can be used to decode individual scanlines. | |
56 * | |
57 * @param dstInfo Info of the destination. If the dimensions do not match | |
58 * those of getInfo (or origSubset, if provided), this implies a scale. | |
59 * @param origSubset If non null, represents a subset of the original | |
60 * unscaled image for the scanline decoder to decode. | |
61 * @return New SkScanlineDecoder, or NULL on failure. | |
62 * | |
63 * NOTE: If any rows were previously decoded, this requires rewinding the | |
64 * SkStream. | |
65 * | |
66 * NOTE: The lifetime of the scanline decoder is tied to the lifetime of | |
67 * the SkCodec. Do not attempt to use after the SkCodec has been deleted. | |
68 * Further, the SkScanlineDecoder should be deleted *before* the SkCodec, | |
69 * in case the implementation depends on the ordering. | |
70 */ | |
71 SkScanlineDecoder* getScanlineDecoder(const SkImageInfo& dstInfo, | |
72 const SkIRect* origSubset = NULL); | |
scroggo
2015/03/18 20:19:06
I haven't yet handled the subset case, but I wante
| |
73 | |
74 /** | |
75 * Some images may initially report that they have alpha due to the format | |
76 * of the encoded data, but then never use any colors which have alpha | |
77 * less than 100%. This function can be called *after* decoding to | |
78 * determine if such an image truly had alpha. Calling it before decoding | |
79 * is undefined. | |
80 */ | |
81 bool reallyHasAlpha() const { | |
scroggo
2015/03/18 20:19:06
This maybe should have been a separate change, but
| |
82 return this->onReallyHasAlpha(); | |
83 } | |
84 | |
51 protected: | 85 protected: |
52 SkCodec(const SkImageInfo&, SkStream*); | 86 SkCodec(const SkImageInfo&, SkStream*); |
53 | 87 |
54 /** | 88 /** |
55 * The SkAlphaType is a conservative answer. i.e. it is possible that it | 89 * The SkAlphaType is a conservative answer. i.e. it is possible that it |
56 * initially returns a non-opaque answer, but completing the decode | 90 * initially returns a non-opaque answer, but completing the decode |
57 * reveals that the image is actually opaque. | 91 * reveals that the image is actually opaque. |
58 */ | 92 */ |
59 bool onGetInfo(SkImageInfo* info) SK_OVERRIDE { | 93 bool onGetInfo(SkImageInfo* info) SK_OVERRIDE { |
60 *info = fInfo; | 94 *info = fInfo; |
61 return true; | 95 return true; |
62 } | 96 } |
63 | 97 |
64 // Helper for subclasses. | 98 // Helper for subclasses. |
65 const SkImageInfo& getOriginalInfo() { return fInfo; } | 99 const SkImageInfo& getOriginalInfo() { return fInfo; } |
66 | 100 |
67 virtual SkISize onGetScaledDimensions(float /* desiredScale */) const { | 101 virtual SkISize onGetScaledDimensions(float /* desiredScale */) const { |
68 // By default, scaling is not supported. | 102 // By default, scaling is not supported. |
69 return fInfo.dimensions(); | 103 return fInfo.dimensions(); |
70 } | 104 } |
71 | 105 |
72 /** | 106 /** |
107 * Override if your codec supports scanline decoding. | |
108 * | |
109 * No need to call rewindIfNeeded(), which will have already been called | |
110 * by the base class. | |
111 * | |
112 * @param dstInfo Info of the destination. If the dimensions do not match | |
113 * those of getInfo (or origSubset, if different), this implies a | |
114 * scale. | |
115 * @param origSubset Subset of the original info to decode. The base class | |
116 * will have already intersected with getInfo (or set to match if the | |
117 * caller supplied NULL). | |
118 * @return New SkScanlineDecoder on success, NULL otherwise. | |
119 */ | |
120 virtual SkScanlineDecoder* onGetScanlineDecoder(const SkImageInfo& dstInfo, | |
121 const SkIRect& origSubset) { | |
122 return NULL; | |
123 } | |
124 | |
125 virtual bool onReallyHasAlpha() const { return false; } | |
126 | |
127 /** | |
73 * If the stream was previously read, attempt to rewind. | 128 * If the stream was previously read, attempt to rewind. |
74 * @returns: | 129 * @returns: |
75 * true | 130 * true |
76 * - if the stream needed to be rewound, and the rewind | 131 * - if the stream needed to be rewound, and the rewind |
77 * succeeded. | 132 * succeeded. |
78 * - if the stream did not need to be rewound. | 133 * - if the stream did not need to be rewound. |
79 * false | 134 * false |
80 * - if the stream needed to be rewound, and rewind failed. | 135 * - if the stream needed to be rewound, and rewind failed. |
81 * Subclasses MUST call this function before reading the stream (e.g. in | 136 * Subclasses MUST call this function before reading the stream (e.g. in |
82 * onGetPixels). If it returns false, onGetPixels should return | 137 * onGetPixels). If it returns false, onGetPixels should return |
83 * kCouldNotRewind. | 138 * kCouldNotRewind. |
84 */ | 139 */ |
85 bool SK_WARN_UNUSED_RESULT rewindIfNeeded(); | 140 bool SK_WARN_UNUSED_RESULT rewindIfNeeded(); |
86 | 141 |
87 private: | 142 private: |
88 const SkImageInfo fInfo; | 143 const SkImageInfo fInfo; |
89 SkAutoTDelete<SkStream> fStream; | 144 SkAutoTDelete<SkStream> fStream; |
90 bool fNeedsRewind; | 145 bool fNeedsRewind; |
91 }; | 146 }; |
92 #endif // SkCodec_DEFINED | 147 #endif // SkCodec_DEFINED |
OLD | NEW |