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 SkScanlineDecoder_DEFINED | 8 #ifndef SkScanlineDecoder_DEFINED |
9 #define SkScanlineDecoder_DEFINED | 9 #define SkScanlineDecoder_DEFINED |
10 | 10 |
11 #include "SkTypes.h" | 11 #include "SkTypes.h" |
12 #include "SkCodec.h" | 12 #include "SkCodec.h" |
13 #include "SkTemplates.h" | 13 #include "SkTemplates.h" |
14 #include "SkImageInfo.h" | 14 #include "SkImageInfo.h" |
15 | 15 |
16 class SkScanlineDecoder : public SkNoncopyable { | 16 class SkScanlineDecoder : public SkNoncopyable { |
17 public: | 17 public: |
18 /** | 18 /** |
| 19 * If this stream represents an encoded image that we know how to decode |
| 20 * in scanlines, return an SkScanlineDecoder that can decode it. Otherwise |
| 21 * return NULL. |
| 22 * |
| 23 * start() must be called in order to decode any scanlines. |
| 24 * |
| 25 * If NULL is returned, the stream is deleted immediately. Otherwise, the |
| 26 * SkScanlineDecoder takes ownership of it, and will delete it when done |
| 27 * with it. |
| 28 */ |
| 29 static SkScanlineDecoder* NewFromStream(SkStream*); |
| 30 |
| 31 /** |
| 32 * Similar to NewFromStream, but reads from an SkData. |
| 33 * |
| 34 * Will take a ref if it returns a scanline decoder, else will not affect |
| 35 * the data. |
| 36 */ |
| 37 static SkScanlineDecoder* NewFromData(SkData*); |
| 38 |
| 39 /** |
19 * Clean up after reading/skipping scanlines. | 40 * Clean up after reading/skipping scanlines. |
20 * | 41 * |
21 * It is possible that not all scanlines will have been read/skipped. In | 42 * It is possible that not all scanlines will have been read/skipped. In |
22 * fact, in the case of subset decodes, it is likely that there will be | 43 * fact, in the case of subset decodes, it is likely that there will be |
23 * scanlines at the bottom of the image that have been ignored. | 44 * scanlines at the bottom of the image that have been ignored. |
24 */ | 45 */ |
25 virtual ~SkScanlineDecoder() {} | 46 virtual ~SkScanlineDecoder() {} |
26 | 47 |
27 /** | 48 /** |
| 49 * Returns the default info, corresponding to the encoded data. |
| 50 */ |
| 51 const SkImageInfo& getInfo() { return fSrcInfo; } |
| 52 |
| 53 /** |
| 54 * Initialize on the first scanline, with the specified options. |
| 55 * |
| 56 * This must be called in order to call getScanlnies or skipScanlines. If |
| 57 * it has been called before, this will require rewinding the stream. |
| 58 * |
| 59 * @param dstInfo Info of the destination. If the dimensions do not match |
| 60 * those of getInfo, this implies a scale. |
| 61 * @param options Contains decoding options, including if memory is zero |
| 62 * initialized. |
| 63 * @param ctable A pointer to a color table. When dstInfo.colorType() is |
| 64 * kIndex8, this should be non-NULL and have enough storage for 256 |
| 65 * colors. The color table will be populated after decoding the palett
e. |
| 66 * @param ctableCount A pointer to the size of the color table. When |
| 67 * dstInfo.colorType() is kIndex8, this should be non-NULL. It will |
| 68 * be modified to the true size of the color table (<= 256) after |
| 69 * decoding the palette. |
| 70 * @return Enum representing success or reason for failure. |
| 71 */ |
| 72 SkCodec::Result start(const SkImageInfo& dstInfo, const SkCodec::Options* op
tions, |
| 73 SkPMColor ctable[], int* ctableCount); |
| 74 |
| 75 /** |
| 76 * Simplified version of start() that asserts that info is NOT |
| 77 * kIndex8_SkColorType and uses the default Options. |
| 78 */ |
| 79 SkCodec::Result start(const SkImageInfo& dstInfo); |
| 80 |
| 81 /** |
28 * Write the next countLines scanlines into dst. | 82 * Write the next countLines scanlines into dst. |
29 * | 83 * |
| 84 * Not valid to call before calling start(). |
| 85 * |
30 * @param dst Must be non-null, and large enough to hold countLines | 86 * @param dst Must be non-null, and large enough to hold countLines |
31 * scanlines of size rowBytes. | 87 * scanlines of size rowBytes. |
32 * @param countLines Number of lines to write. | 88 * @param countLines Number of lines to write. |
33 * @param rowBytes Number of bytes per row. Must be large enough to hold | 89 * @param rowBytes Number of bytes per row. Must be large enough to hold |
34 * a scanline based on the SkImageInfo used to create this object. | 90 * a scanline based on the SkImageInfo used to create this object. |
35 */ | 91 */ |
36 SkCodec::Result getScanlines(void* dst, int countLines, size_t rowBytes) { | 92 SkCodec::Result getScanlines(void* dst, int countLines, size_t rowBytes) { |
| 93 SkASSERT(!fDstInfo.isEmpty()); |
37 if ((rowBytes < fDstInfo.minRowBytes() && countLines > 1 ) || countLines
<= 0 | 94 if ((rowBytes < fDstInfo.minRowBytes() && countLines > 1 ) || countLines
<= 0 |
38 || fCurrScanline + countLines > fDstInfo.height()) { | 95 || fCurrScanline + countLines > fDstInfo.height()) { |
39 return SkCodec::kInvalidParameters; | 96 return SkCodec::kInvalidParameters; |
40 } | 97 } |
41 const SkCodec::Result result = this->onGetScanlines(dst, countLines, row
Bytes); | 98 const SkCodec::Result result = this->onGetScanlines(dst, countLines, row
Bytes); |
42 fCurrScanline += countLines; | 99 fCurrScanline += countLines; |
43 return result; | 100 return result; |
44 } | 101 } |
45 | 102 |
46 /** | 103 /** |
47 * Skip count scanlines. | 104 * Skip count scanlines. |
48 * | 105 * |
| 106 * Not valid to call before calling start(). |
| 107 * |
49 * The default version just calls onGetScanlines and discards the dst. | 108 * The default version just calls onGetScanlines and discards the dst. |
50 * NOTE: If skipped lines are the only lines with alpha, this default | 109 * NOTE: If skipped lines are the only lines with alpha, this default |
51 * will make reallyHasAlpha return true, when it could have returned | 110 * will make reallyHasAlpha return true, when it could have returned |
52 * false. | 111 * false. |
53 */ | 112 */ |
54 SkCodec::Result skipScanlines(int countLines) { | 113 SkCodec::Result skipScanlines(int countLines) { |
| 114 SkASSERT(!fDstInfo.isEmpty()); |
55 if (fCurrScanline + countLines > fDstInfo.height()) { | 115 if (fCurrScanline + countLines > fDstInfo.height()) { |
56 // Arguably, we could just skip the scanlines which are remaining, | 116 // Arguably, we could just skip the scanlines which are remaining, |
57 // and return kSuccess. We choose to return invalid so the client | 117 // and return kSuccess. We choose to return invalid so the client |
58 // can catch their bug. | 118 // can catch their bug. |
59 return SkCodec::kInvalidParameters; | 119 return SkCodec::kInvalidParameters; |
60 } | 120 } |
61 const SkCodec::Result result = this->onSkipScanlines(countLines); | 121 const SkCodec::Result result = this->onSkipScanlines(countLines); |
62 fCurrScanline += countLines; | 122 fCurrScanline += countLines; |
63 return result; | 123 return result; |
64 } | 124 } |
65 | 125 |
66 /** | 126 /** |
67 * Some images may initially report that they have alpha due to the format | 127 * Some images may initially report that they have alpha due to the format |
68 * of the encoded data, but then never use any colors which have alpha | 128 * of the encoded data, but then never use any colors which have alpha |
69 * less than 100%. This function can be called *after* decoding to | 129 * less than 100%. This function can be called *after* decoding to |
70 * determine if such an image truly had alpha. Calling it before decoding | 130 * determine if such an image truly had alpha. Calling it before decoding |
71 * is undefined. | 131 * is undefined. |
72 * FIXME: see skbug.com/3582. | 132 * FIXME: see skbug.com/3582. |
73 */ | 133 */ |
74 bool reallyHasAlpha() const { | 134 bool reallyHasAlpha() const { |
75 return this->onReallyHasAlpha(); | 135 return this->onReallyHasAlpha(); |
76 } | 136 } |
77 | 137 |
78 protected: | 138 protected: |
79 SkScanlineDecoder(const SkImageInfo& requested) | 139 SkScanlineDecoder(const SkImageInfo& srcInfo) |
80 : fDstInfo(requested) | 140 : fSrcInfo(srcInfo) |
| 141 , fDstInfo() |
81 , fCurrScanline(0) {} | 142 , fCurrScanline(0) {} |
82 | 143 |
83 virtual bool onReallyHasAlpha() const { return false; } | 144 virtual bool onReallyHasAlpha() const { return false; } |
84 | 145 |
85 const SkImageInfo& dstInfo() const { return fDstInfo; } | 146 const SkImageInfo& dstInfo() const { return fDstInfo; } |
86 | 147 |
87 private: | 148 private: |
88 const SkImageInfo fDstInfo; | 149 const SkImageInfo fSrcInfo; |
| 150 SkImageInfo fDstInfo; |
89 int fCurrScanline; | 151 int fCurrScanline; |
90 | 152 |
| 153 virtual SkCodec::Result onStart(const SkImageInfo& dstInfo, |
| 154 const SkCodec::Options& options, |
| 155 SkPMColor ctable[], int* ctableCount) = 0; |
| 156 |
91 // Naive default version just calls onGetScanlines on temp memory. | 157 // Naive default version just calls onGetScanlines on temp memory. |
92 virtual SkCodec::Result onSkipScanlines(int countLines) { | 158 virtual SkCodec::Result onSkipScanlines(int countLines) { |
93 SkAutoMalloc storage(fDstInfo.minRowBytes()); | 159 SkAutoMalloc storage(fDstInfo.minRowBytes()); |
94 // Note that we pass 0 to rowBytes so we continue to use the same memory
. | 160 // Note that we pass 0 to rowBytes so we continue to use the same memory
. |
95 // Also note that while getScanlines checks that rowBytes is big enough, | 161 // Also note that while getScanlines checks that rowBytes is big enough, |
96 // onGetScanlines bypasses that check. | 162 // onGetScanlines bypasses that check. |
97 // Calling the virtual method also means we do not double count | 163 // Calling the virtual method also means we do not double count |
98 // countLines. | 164 // countLines. |
99 return this->onGetScanlines(storage.get(), countLines, 0); | 165 return this->onGetScanlines(storage.get(), countLines, 0); |
100 } | 166 } |
101 | 167 |
102 virtual SkCodec::Result onGetScanlines(void* dst, int countLines, | 168 virtual SkCodec::Result onGetScanlines(void* dst, int countLines, |
103 size_t rowBytes) = 0; | 169 size_t rowBytes) = 0; |
104 | 170 |
105 }; | 171 }; |
106 #endif // SkScanlineDecoder_DEFINED | 172 #endif // SkScanlineDecoder_DEFINED |
OLD | NEW |