aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/codec/SkCodec_libpng.h
blob: 8ef1ae2c17d3ed2e39f64cce2032f868b4f9030e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
 * Copyright 2015 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkCodec.h"
#include "SkColorTable.h"
#include "SkEncodedFormat.h"
#include "SkImageInfo.h"
#include "SkRefCnt.h"
#include "SkSwizzler.h"

#ifdef SKIA_PNG_PREFIXED
    // this must proceed png.h
    #include "pngprefix.h"
#endif
#include "png.h"

class SkScanlineDecoder;
class SkStream;

class SkPngCodec : public SkCodec {
public:
    static bool IsPng(SkStream*);

    // Assume IsPng was called and returned true.
    static SkCodec* NewFromStream(SkStream*);
    static SkScanlineDecoder* NewSDFromStream(SkStream*);

    virtual ~SkPngCodec();

protected:
    Result onGetPixels(const SkImageInfo&, void*, size_t, const Options&, SkPMColor*, int*)
            override;
    SkEncodedFormat onGetEncodedFormat() const override { return kPNG_SkEncodedFormat; }
    bool onRewind() override;
    bool onReallyHasAlpha() const final;

    // Helper to set up swizzler and color table. Also calls png_read_update_info.
    Result initializeSwizzler(const SkImageInfo& requestedInfo, const Options&,
                              SkPMColor*, int* ctableCount);

    SkPngCodec(const SkImageInfo&, SkStream*, png_structp, png_infop, int, int);

    png_structp png_ptr() { return fPng_ptr; }
    SkSwizzler* swizzler() { return fSwizzler; }
    SkSwizzler::SrcConfig srcConfig() const { return fSrcConfig; }
    int numberPasses() const { return fNumberPasses; }

    enum AlphaState {
        // This class has done no decoding, or threw away its knowledge (in
        // scanline decodes).
        kUnknown_AlphaState,
        // This class found the image (possibly partial, in the case of a
        // scanline decode) to be opaque.
        kOpaque_AlphaState,
        // Ths class found the image to have alpha.
        kHasAlpha_AlphaState,
    };

    virtual AlphaState alphaInScanlineDecode() const = 0;

private:
    png_structp                 fPng_ptr;
    png_infop                   fInfo_ptr;

    // These are stored here so they can be used both by normal decoding and scanline decoding.
    SkAutoTUnref<SkColorTable>  fColorTable;    // May be unpremul.
    SkAutoTDelete<SkSwizzler>   fSwizzler;

    SkSwizzler::SrcConfig       fSrcConfig;
    const int                   fNumberPasses;
    int                         fBitDepth;

    AlphaState                  fAlphaState;

    bool decodePalette(bool premultiply, int* ctableCount);
    void destroyReadStruct();

    typedef SkCodec INHERITED;
};