xref: /aosp_15_r20/external/skia/src/codec/SkIcoCodec.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 #ifndef SkIcoCodec_DEFINED
8 #define SkIcoCodec_DEFINED
9 
10 #include "include/codec/SkCodec.h"
11 #include "include/codec/SkEncodedImageFormat.h"
12 #include "include/core/SkSize.h"
13 #include "include/core/SkTypes.h"
14 #include "include/private/base/SkTArray.h"
15 
16 #include <cstddef>
17 #include <memory>
18 
19 class SkSampler;
20 class SkStream;
21 struct SkEncodedInfo;
22 struct SkImageInfo;
23 
24 /*
25  * This class implements the decoding for bmp images
26  */
27 class SkIcoCodec : public SkCodec {
28 public:
29     static bool IsIco(const void*, size_t);
30 
31     /*
32      * Assumes IsIco was called and returned true
33      * Creates an Ico decoder
34      * Reads enough of the stream to determine the image format
35      */
36     static std::unique_ptr<SkCodec> MakeFromStream(std::unique_ptr<SkStream>, Result*);
37 
38 protected:
39 
40     /*
41      * Chooses the best dimensions given the desired scale
42      */
43     SkISize onGetScaledDimensions(float desiredScale) const override;
44 
45     bool onDimensionsSupported(const SkISize&) override;
46 
47     /*
48      * Initiates the Ico decode
49      */
50     Result onGetPixels(const SkImageInfo& dstInfo, void* dst, size_t dstRowBytes, const Options&,
51             int*) override;
52 
onGetEncodedFormat()53     SkEncodedImageFormat onGetEncodedFormat() const override {
54         return SkEncodedImageFormat::kICO;
55     }
56 
57     SkScanlineOrder onGetScanlineOrder() const override;
58 
conversionSupported(const SkImageInfo &,bool,bool)59     bool conversionSupported(const SkImageInfo&, bool, bool) override {
60         // This will be checked by the embedded codec.
61         return true;
62     }
63 
64     // Handled by the embedded codec.
usesColorXform()65     bool usesColorXform() const override { return false; }
66 private:
67 
68     Result onStartScanlineDecode(const SkImageInfo& dstInfo,
69             const SkCodec::Options& options) override;
70 
71     int onGetScanlines(void* dst, int count, size_t rowBytes) override;
72 
73     bool onSkipScanlines(int count) override;
74 
75     Result onStartIncrementalDecode(const SkImageInfo& dstInfo, void* pixels, size_t rowBytes,
76             const SkCodec::Options&) override;
77 
78     Result onIncrementalDecode(int* rowsDecoded) override;
79 
80     SkSampler* getSampler(bool createIfNecessary) override;
81 
82     /*
83      * Searches fEmbeddedCodecs for a codec that matches requestedSize.
84      * The search starts at startIndex and ends when an appropriate codec
85      * is found, or we have reached the end of the array.
86      *
87      * @return the index of the matching codec or -1 if there is no
88      *         matching codec between startIndex and the end of
89      *         the array.
90      */
91     int chooseCodec(const SkISize& requestedSize, int startIndex);
92 
93     /*
94      * Constructor called by NewFromStream
95      * @param embeddedCodecs codecs for the embedded images, takes ownership
96      */
97     SkIcoCodec(SkEncodedInfo&& info,
98                std::unique_ptr<SkStream>,
99                std::unique_ptr<skia_private::TArray<std::unique_ptr<SkCodec>>> embeddedCodecs);
100 
101     std::unique_ptr<skia_private::TArray<std::unique_ptr<SkCodec>>> fEmbeddedCodecs;
102 
103     // fCurrCodec is owned by this class, but should not be an
104     // std::unique_ptr.  It will be deleted by the destructor of fEmbeddedCodecs.
105     SkCodec* fCurrCodec;
106 
107     using INHERITED = SkCodec;
108 };
109 #endif  // SkIcoCodec_DEFINED
110