OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2015 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 SkScanlineDecoder_DEFINED |
| 9 #define SkScanlineDecoder_DEFINED |
| 10 |
| 11 #include "SkTypes.h" |
| 12 #include "SkTemplates.h" |
| 13 #include "SkImageGenerator.h" |
| 14 #include "SkImageInfo.h" |
| 15 |
| 16 class SkScanlineDecoder : public SkNoncopyable { |
| 17 public: |
| 18 // Note for implementations: An SkScanlineDecoder will be deleted by (and |
| 19 // therefore *before*) its associated SkCodec, in case the order matters. |
| 20 virtual ~SkScanlineDecoder() {} |
| 21 |
| 22 /** |
| 23 * Write the next countLines scanlines into dst. |
| 24 * |
| 25 * @param dst Must be non-null, and large enough to hold countLines |
| 26 * scanlines of size rowBytes. |
| 27 * @param countLines Number of lines to write. |
| 28 * @param rowBytes Number of bytes per row. Must be large enough to hold |
| 29 * a scanline based on the SkImageInfo used to create this object. |
| 30 */ |
| 31 SkImageGenerator::Result getScanlines(void* dst, int countLines, size_t rowB
ytes) { |
| 32 // Should we keep track of how many scanlines have been read, at |
| 33 // least in debug mode? Then we can make an even smarter check |
| 34 // regarding countLines. |
| 35 if ((rowBytes < fDstInfo.minRowBytes() && countLines > 1 ) || countLines
<= 0 |
| 36 || countLines > fDstInfo.height()) { |
| 37 return SkImageGenerator::kInvalidParameters; |
| 38 } |
| 39 return this->onGetScanlines(dst, countLines, rowBytes); |
| 40 } |
| 41 |
| 42 /** |
| 43 * Skip count scanlines. |
| 44 * |
| 45 * The default version just calls onGetScanlines and discards the dst. |
| 46 * NOTE: If skipped lines are the only lines with alpha, this default |
| 47 * will make reallyHasAlpha return true, when it could have returned |
| 48 * false. |
| 49 */ |
| 50 SkImageGenerator::Result skipScanlines(int count) { |
| 51 return this->onSkipScanlines(count); |
| 52 } |
| 53 |
| 54 /** |
| 55 * Call this function after reading/skipping all scanlines. |
| 56 */ |
| 57 SkImageGenerator::Result finish() { |
| 58 return this->onFinish(); |
| 59 } |
| 60 |
| 61 /** |
| 62 * Some images may initially report that they have alpha due to the format |
| 63 * of the encoded data, but then never use any colors which have alpha |
| 64 * less than 100%. This function can be called *after* decoding to |
| 65 * determine if such an image truly had alpha. Calling it before decoding |
| 66 * is undefined. |
| 67 */ |
| 68 bool reallyHasAlpha() const { |
| 69 return this->onReallyHasAlpha(); |
| 70 } |
| 71 |
| 72 protected: |
| 73 SkScanlineDecoder(const SkImageInfo& requested) |
| 74 : fDstInfo(requested) {} |
| 75 |
| 76 virtual bool onReallyHasAlpha() const { return false; } |
| 77 |
| 78 private: |
| 79 const SkImageInfo fDstInfo; |
| 80 |
| 81 // Naive default version just calls onGetScanlines on temp memory. |
| 82 virtual SkImageGenerator::Result onSkipScanlines(int count) { |
| 83 SkAutoMalloc storage(fDstInfo.minRowBytes()); |
| 84 // Note that we pass 0 to rowBytes so we continue to use the same memory
. |
| 85 // Also note that while getScanlines checks that rowBytes is big enough, |
| 86 // onGetScanlines bypasses that check. |
| 87 return this->onGetScanlines(storage.get(), count, 0); |
| 88 } |
| 89 |
| 90 virtual SkImageGenerator::Result onGetScanlines(void* dst, int countLines, |
| 91 size_t rowBytes) = 0; |
| 92 |
| 93 virtual SkImageGenerator::Result onFinish() { |
| 94 // Return success by default, since this may not be necessary for all co
decs. |
| 95 return SkImageGenerator::kSuccess; |
| 96 } |
| 97 }; |
| 98 #endif // SkScanlineDecoder_DEFINED |
OLD | NEW |