scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 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 SkCodec_DEFINED |
| 9 | #define SkCodec_DEFINED |
| 10 | |
| 11 | #include "SkImageGenerator.h" |
| 12 | #include "SkImageInfo.h" |
| 13 | #include "SkSize.h" |
scroggo | fffeede | 2015-03-18 10:50:37 -0700 | [diff] [blame] | 14 | #include "SkStream.h" |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 15 | #include "SkTemplates.h" |
| 16 | #include "SkTypes.h" |
| 17 | |
| 18 | class SkData; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 19 | |
| 20 | /** |
| 21 | * Abstraction layer directly on top of an image codec. |
| 22 | */ |
| 23 | class SkCodec : public SkImageGenerator { |
| 24 | public: |
| 25 | /** |
| 26 | * 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 | * |
| 29 | * If NULL is returned, the stream is deleted immediately. Otherwise, the |
| 30 | * SkCodec takes ownership of it, and will delete it when done with it. |
| 31 | */ |
| 32 | static SkCodec* NewFromStream(SkStream*); |
| 33 | |
| 34 | /** |
| 35 | * If this data represents an encoded image that we know how to decode, |
| 36 | * return an SkCodec that can decode it. Otherwise return NULL. |
| 37 | * |
| 38 | * Will take a ref if it returns a codec, else will not affect the data. |
| 39 | */ |
| 40 | static SkCodec* NewFromData(SkData*); |
| 41 | |
| 42 | /** |
| 43 | * 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 | * factor requested, so return a size that approximates that scale. |
| 46 | * |
| 47 | * FIXME: Move to SkImageGenerator? |
| 48 | */ |
scroggo | fffeede | 2015-03-18 10:50:37 -0700 | [diff] [blame] | 49 | SkISize getScaledDimensions(float desiredScale) const { |
| 50 | return this->onGetScaledDimensions(desiredScale); |
| 51 | } |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 52 | |
| 53 | protected: |
| 54 | SkCodec(const SkImageInfo&, SkStream*); |
| 55 | |
| 56 | /** |
| 57 | * The SkAlphaType is a conservative answer. i.e. it is possible that it |
| 58 | * initially returns a non-opaque answer, but completing the decode |
| 59 | * reveals that the image is actually opaque. |
| 60 | */ |
reed | 3ef71e3 | 2015-03-19 08:31:14 -0700 | [diff] [blame^] | 61 | #ifdef SK_SUPPORT_LEGACY_BOOL_ONGETINFO |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 62 | bool onGetInfo(SkImageInfo* info) SK_OVERRIDE { |
| 63 | *info = fInfo; |
| 64 | return true; |
| 65 | } |
reed | 3ef71e3 | 2015-03-19 08:31:14 -0700 | [diff] [blame^] | 66 | #endif |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 67 | |
| 68 | // Helper for subclasses. |
| 69 | const SkImageInfo& getOriginalInfo() { return fInfo; } |
| 70 | |
| 71 | virtual SkISize onGetScaledDimensions(float /* desiredScale */) const { |
| 72 | // By default, scaling is not supported. |
| 73 | return fInfo.dimensions(); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * If the stream was previously read, attempt to rewind. |
| 78 | * @returns: |
| 79 | * true |
| 80 | * - if the stream needed to be rewound, and the rewind |
| 81 | * succeeded. |
| 82 | * - if the stream did not need to be rewound. |
| 83 | * false |
| 84 | * - if the stream needed to be rewound, and rewind failed. |
| 85 | * Subclasses MUST call this function before reading the stream (e.g. in |
| 86 | * onGetPixels). If it returns false, onGetPixels should return |
| 87 | * kCouldNotRewind. |
| 88 | */ |
| 89 | bool SK_WARN_UNUSED_RESULT rewindIfNeeded(); |
| 90 | |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 91 | /* |
| 92 | * |
| 93 | * Get method for the input stream |
| 94 | * |
| 95 | */ |
| 96 | SkStream* stream() { |
| 97 | return fStream.get(); |
| 98 | } |
| 99 | |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 100 | private: |
scroggo | 9552662 | 2015-03-17 05:02:17 -0700 | [diff] [blame] | 101 | const SkImageInfo fInfo; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 102 | SkAutoTDelete<SkStream> fStream; |
scroggo | 9552662 | 2015-03-17 05:02:17 -0700 | [diff] [blame] | 103 | bool fNeedsRewind; |
reed | 3ef71e3 | 2015-03-19 08:31:14 -0700 | [diff] [blame^] | 104 | |
| 105 | typedef SkImageGenerator INHERITED; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 106 | }; |
| 107 | #endif // SkCodec_DEFINED |