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 #include "SkCodec.h" |
| 9 #include "SkImageGenerator.h" |
| 10 #include "SkImageInfo.h" |
| 11 |
| 12 class SkStream; |
| 13 |
| 14 class SkScanlineDecoder : public SkNoncopyable { |
| 15 public: |
| 16 // Note for implementations: An SkScanlineDecoder should be deleted |
| 17 // *before* its associated SkCodec, in case the order matters. |
| 18 virtual ~SkScanlineDecoder() {} |
| 19 |
| 20 /** |
| 21 * Write the next scanline into dst. |
| 22 * |
| 23 * @param dst Must be non-null, and large enough to hold a scanline based |
| 24 * on the info used to create this object. |
| 25 * @return kSuccess or error code explaining failure. |
| 26 */ |
| 27 SkImageGenerator::Result getNextScanline(void* dst) { |
| 28 return this->onGetNextScanline(dst); |
| 29 } |
| 30 |
| 31 /** |
| 32 * Write the next countLines scanlines into dst. |
| 33 * |
| 34 * @param dst Must be non-null, and large enough to hold countLines |
| 35 * scanlines based on rowBytes. |
| 36 * @param countLines Number of lines to write. |
| 37 * @param rowBytes Number of bytes per row. |
| 38 */ |
| 39 SkImageGenerator::Result getScanlines(void* dst, int countLines, size_t rowB
ytes) { |
| 40 // Should we keep track of how many scanlines have been read, at |
| 41 // least in debug mode? Then we can make an even smarter check |
| 42 // regarding countLines. |
| 43 if (rowBytes < fDstInfo.minRowBytes() || countLines < 0 |
| 44 || countLines > fDstInfo.height()) { |
| 45 return SkCodec::kInvalidParameters; |
| 46 } |
| 47 return this->onGetScanlines(dst, countLines, rowBytes); |
| 48 } |
| 49 |
| 50 /** |
| 51 * Skip count scanlines. |
| 52 * |
| 53 * The default version just calls onGetScanline and discards the dst. |
| 54 */ |
| 55 SkImageGenerator::Result skipScanlines(int count) { |
| 56 return this->onSkipScanlines(count); |
| 57 } |
| 58 |
| 59 /** |
| 60 * Call this function after reading/skipping all scanlines. |
| 61 */ |
| 62 SkImageGenerator::Result finish() { |
| 63 return this->onFinish(); |
| 64 } |
| 65 |
| 66 /** |
| 67 * 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 |
| 69 * less than 100%. This function can be called *after* decoding to |
| 70 * determine if such an image truly had alpha. Calling it before decoding |
| 71 * is undefined. |
| 72 */ |
| 73 bool reallyHasAlpha() const { |
| 74 return this->onReallyHasAlpha(); |
| 75 } |
| 76 |
| 77 protected: |
| 78 SkScanlineDecoder(const SkImageInfo& original, const SkImageInfo& requested) |
| 79 : fSrcInfo(original) |
| 80 , fDstInfo(requested) {} |
| 81 |
| 82 virtual bool onReallyHasAlpha() const { return false; } |
| 83 |
| 84 private: |
| 85 // FIXME: Is fSrcInfo needed for subsetting? Not needed so far... |
| 86 const SkImageInfo fSrcInfo; |
| 87 const SkImageInfo fDstInfo; |
| 88 |
| 89 virtual SkImageGenerator::Result onGetNextScanline(void* dst) = 0; |
| 90 |
| 91 // Naive default version just calls onGetNextScanline on temp memory. |
| 92 virtual SkImageGenerator::Result onSkipScanlines(int count) { |
| 93 SkAutoMalloc storage(fDstInfo.minRowBytes()); |
| 94 for (int i = 0; i < count; ++i) { |
| 95 SkImageGenerator::Result res = this->onGetNextScanline(storage.get()
); |
| 96 if (res != SkImageGenerator::kSuccess) { |
| 97 return res; |
| 98 } |
| 99 } |
| 100 return SkCodec::kSuccess; |
| 101 } |
| 102 |
| 103 // Naive default version just calls onGetNextScanline |
| 104 virtual SkImageGenerator::Result onGetScanlines(void* dst, int countLines, s
ize_t rowBytes) { |
| 105 void* dstRow = dst; |
| 106 for (int i = 0; i < countLines; ++i) { |
| 107 const SkImageGenerator::Result result = this->onGetNextScanline(dstR
ow); |
| 108 if (result != SkCodec::kSuccess) { |
| 109 return result; |
| 110 } |
| 111 dstRow = SkTAddOffset<void*>(dstRow, rowBytes); |
| 112 } |
| 113 return SkCodec::kSuccess; |
| 114 } |
| 115 |
| 116 virtual SkImageGenerator::Result onFinish() { |
| 117 // Return success by default, since this may not be necessary for all co
decs. |
| 118 return SkImageGenerator::kSuccess; |
| 119 } |
| 120 }; |
OLD | NEW |