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_wbmp_DEFINED
9 #define SkCodec_wbmp_DEFINED
10 
11 #include "SkCodec.h"
12 #include "SkColorSpace.h"
13 #include "SkSwizzler.h"
14 
15 class SkWbmpCodec final : public SkCodec {
16 public:
17     static bool IsWbmp(const void*, size_t);
18 
19     /*
20      * Assumes IsWbmp was called and returned true
21      * Creates a wbmp codec
22      * Takes ownership of the stream
23      */
24     static SkCodec* NewFromStream(SkStream*);
25 
26 protected:
27     SkEncodedImageFormat onGetEncodedFormat() const override;
28     Result onGetPixels(const SkImageInfo&, void*, size_t,
29                        const Options&, SkPMColor[], int*, int*) override;
30     bool onRewind() override;
31 private:
32     /*
33      * Returns a swizzler on success, nullptr on failure
34      */
35     SkSwizzler* initializeSwizzler(const SkImageInfo& info, const SkPMColor* ctable,
36                                    const Options& opts);
getSampler(bool createIfNecessary)37     SkSampler* getSampler(bool createIfNecessary) override {
38         SkASSERT(fSwizzler || !createIfNecessary);
39         return fSwizzler.get();
40     }
41 
42     /*
43      * Read a src row from the encoded stream
44      */
45     bool readRow(uint8_t* row);
46 
47     SkWbmpCodec(int width, int height, const SkEncodedInfo&, SkStream*);
48 
49     const size_t                fSrcRowBytes;
50 
51     // Used for scanline decodes:
52     std::unique_ptr<SkSwizzler> fSwizzler;
53     sk_sp<SkColorTable>         fColorTable;
54     SkAutoTMalloc<uint8_t>      fSrcBuffer;
55 
56     int onGetScanlines(void* dst, int count, size_t dstRowBytes) override;
57     bool onSkipScanlines(int count) override;
58     Result onStartScanlineDecode(const SkImageInfo& dstInfo, const Options& options,
59             SkPMColor inputColorTable[], int* inputColorCount) override;
60 
61     typedef SkCodec INHERITED;
62 };
63 
64 #endif  // SkCodec_wbmp_DEFINED
65