1 /*
2  * Copyright 2017 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 SkHeifCodec_DEFINED
9 #define SkHeifCodec_DEFINED
10 
11 #include "SkCodec.h"
12 #include "SkEncodedOrigin.h"
13 #include "SkImageInfo.h"
14 #include "SkSwizzler.h"
15 #include "SkStream.h"
16 
17 #if __has_include("HeifDecoderAPI.h")
18     #include "HeifDecoderAPI.h"
19 #else
20     #include "SkStubHeifDecoderAPI.h"
21 #endif
22 
23 class SkHeifCodec : public SkCodec {
24 public:
25     static bool IsHeif(const void*, size_t);
26 
27     /*
28      * Assumes IsHeif was called and returned true.
29      */
30     static std::unique_ptr<SkCodec> MakeFromStream(std::unique_ptr<SkStream>, Result*);
31 
32 protected:
33 
34     Result onGetPixels(
35             const SkImageInfo& dstInfo,
36             void* dst, size_t dstRowBytes,
37             const Options& options,
38             int* rowsDecoded) override;
39 
40     SkEncodedImageFormat onGetEncodedFormat() const override {
41         return SkEncodedImageFormat::kHEIF;
42     }
43 
44     bool conversionSupported(const SkImageInfo&, bool, bool) override;
45 
46 private:
47     /*
48      * Creates an instance of the decoder
49      * Called only by NewFromStream
50      */
51     SkHeifCodec(SkEncodedInfo&&, HeifDecoder*, SkEncodedOrigin);
52 
53     void initializeSwizzler(const SkImageInfo& dstInfo, const Options& options);
54     void allocateStorage(const SkImageInfo& dstInfo);
55     int readRows(const SkImageInfo& dstInfo, void* dst,
56             size_t rowBytes, int count, const Options&);
57 
58     /*
59      * Scanline decoding.
60      */
61     SkSampler* getSampler(bool createIfNecessary) override;
62     Result onStartScanlineDecode(const SkImageInfo& dstInfo,
63             const Options& options) override;
64     int onGetScanlines(void* dst, int count, size_t rowBytes) override;
65     bool onSkipScanlines(int count) override;
66 
67     std::unique_ptr<HeifDecoder>       fHeifDecoder;
68     HeifFrameInfo                      fFrameInfo;
69     SkAutoTMalloc<uint8_t>             fStorage;
70     uint8_t*                           fSwizzleSrcRow;
71     uint32_t*                          fColorXformSrcRow;
72 
73     std::unique_ptr<SkSwizzler>        fSwizzler;
74 
75     typedef SkCodec INHERITED;
76 };
77 
78 #endif // SkHeifCodec_DEFINED
79