msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [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 | #include "SkCodec.h" |
| 9 | #include "SkImageInfo.h" |
| 10 | #include "SkStream.h" |
| 11 | #include "SkTypes.h" |
| 12 | |
| 13 | /* |
| 14 | * This class implements the decoding for bmp images |
| 15 | */ |
| 16 | class SkIcoCodec : public SkCodec { |
| 17 | public: |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 18 | static bool IsIco(const void*, size_t); |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 19 | |
| 20 | /* |
| 21 | * Assumes IsIco was called and returned true |
| 22 | * Creates an Ico decoder |
| 23 | * Reads enough of the stream to determine the image format |
| 24 | */ |
| 25 | static SkCodec* NewFromStream(SkStream*); |
| 26 | |
| 27 | protected: |
| 28 | |
| 29 | /* |
| 30 | * Chooses the best dimensions given the desired scale |
| 31 | */ |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 32 | SkISize onGetScaledDimensions(float desiredScale) const override; |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 33 | |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 34 | bool onDimensionsSupported(const SkISize&) override; |
| 35 | |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 36 | /* |
| 37 | * Initiates the Ico decode |
| 38 | */ |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 39 | Result onGetPixels(const SkImageInfo& dstInfo, void* dst, size_t dstRowBytes, const Options&, |
| 40 | SkPMColor*, int*, int*) override; |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 41 | |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame^] | 42 | SkEncodedImageFormat onGetEncodedFormat() const override { |
| 43 | return SkEncodedImageFormat::kICO; |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 44 | } |
| 45 | |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 46 | SkScanlineOrder onGetScanlineOrder() const override; |
| 47 | |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 48 | private: |
| 49 | |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 50 | Result onStartScanlineDecode(const SkImageInfo& dstInfo, const SkCodec::Options& options, |
| 51 | SkPMColor inputColorPtr[], int* inputColorCount) override; |
| 52 | |
| 53 | int onGetScanlines(void* dst, int count, size_t rowBytes) override; |
| 54 | |
| 55 | bool onSkipScanlines(int count) override; |
| 56 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 57 | Result onStartIncrementalDecode(const SkImageInfo& dstInfo, void* pixels, size_t rowBytes, |
| 58 | const SkCodec::Options&, SkPMColor*, int*) override; |
| 59 | |
| 60 | Result onIncrementalDecode(int* rowsDecoded) override; |
| 61 | |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 62 | SkSampler* getSampler(bool createIfNecessary) override; |
| 63 | |
| 64 | /* |
| 65 | * Searches fEmbeddedCodecs for a codec that matches requestedSize. |
| 66 | * The search starts at startIndex and ends when an appropriate codec |
| 67 | * is found, or we have reached the end of the array. |
| 68 | * |
| 69 | * @return the index of the matching codec or -1 if there is no |
| 70 | * matching codec between startIndex and the end of |
| 71 | * the array. |
| 72 | */ |
| 73 | int chooseCodec(const SkISize& requestedSize, int startIndex); |
| 74 | |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 75 | /* |
| 76 | * Constructor called by NewFromStream |
| 77 | * @param embeddedCodecs codecs for the embedded images, takes ownership |
| 78 | */ |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 79 | SkIcoCodec(int width, int height, const SkEncodedInfo& info, |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 80 | SkTArray<std::unique_ptr<SkCodec>, true>* embeddedCodecs, sk_sp<SkColorSpace> colorSpace); |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 81 | |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 82 | std::unique_ptr<SkTArray<std::unique_ptr<SkCodec>, true>> fEmbeddedCodecs; |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 83 | |
| 84 | // Only used by the scanline decoder. onStartScanlineDecode() will set |
| 85 | // fCurrScanlineCodec to one of the fEmbeddedCodecs, if it can find a |
| 86 | // codec of the appropriate size. We will use fCurrScanlineCodec for |
| 87 | // subsequent calls to onGetScanlines() or onSkipScanlines(). |
| 88 | // fCurrScanlineCodec is owned by this class, but should not be an |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 89 | // std::unique_ptr. It will be deleted by the destructor of fEmbeddedCodecs. |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 90 | SkCodec* fCurrScanlineCodec; |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 91 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 92 | // Only used by incremental decoder. onStartIncrementalDecode() will set |
| 93 | // fCurrIncrementalCodec to one of the fEmbeddedCodecs, if it can find a |
| 94 | // codec of the appropriate size. We will use fCurrIncrementalCodec for |
| 95 | // subsequent calls to incrementalDecode(). |
| 96 | // fCurrIncrementalCodec is owned by this class, but should not be an |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 97 | // std::unique_ptr. It will be deleted by the destructor of fEmbeddedCodecs. |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 98 | SkCodec* fCurrIncrementalCodec; |
| 99 | |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 100 | typedef SkCodec INHERITED; |
| 101 | }; |