blob: e4df8230766a53f6928a3f750fb6044e28674f14 [file] [log] [blame]
scroggof24f2242015-03-03 08:59:20 -08001/*
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"
msarettd1ec89b2016-08-03 12:59:27 -07009#include "SkColorSpaceXform.h"
scroggo05245902015-03-25 11:11:52 -070010#include "SkColorTable.h"
scroggocf98fa92015-11-23 08:14:40 -080011#include "SkPngChunkReader.h"
scroggo1dd3ea92015-03-20 11:55:55 -070012#include "SkEncodedFormat.h"
scroggof24f2242015-03-03 08:59:20 -080013#include "SkImageInfo.h"
scroggo05245902015-03-25 11:11:52 -070014#include "SkRefCnt.h"
15#include "SkSwizzler.h"
scroggof24f2242015-03-03 08:59:20 -080016
scroggof24f2242015-03-03 08:59:20 -080017class SkStream;
18
19class SkPngCodec : public SkCodec {
20public:
scroggodb30be22015-12-08 18:54:13 -080021 static bool IsPng(const char*, size_t);
scroggo9b2cdbf2015-07-10 12:07:02 -070022
scroggo1c005e42015-08-04 09:24:45 -070023 // Assume IsPng was called and returned true.
scroggocf98fa92015-11-23 08:14:40 -080024 static SkCodec* NewFromStream(SkStream*, SkPngChunkReader* = NULL);
scroggo1c005e42015-08-04 09:24:45 -070025
scroggo9b2cdbf2015-07-10 12:07:02 -070026 virtual ~SkPngCodec();
27
scroggof24f2242015-03-03 08:59:20 -080028protected:
mtklein6dc5b9a2016-08-24 12:22:32 -070029 // We hold the png_ptr and info_ptr as voidp to avoid having to include png.h
30 // or forward declare their types here. voidp auto-casts to the real pointer types.
31 struct voidp {
32 voidp(void* ptr) : fPtr(ptr) {}
33
34 template <typename T>
35 operator T*() const { return (T*)fPtr; }
36
37 explicit operator bool() const { return fPtr != nullptr; }
38
39 void* fPtr;
40 };
41
msarette6dd0042015-10-09 11:07:34 -070042 Result onGetPixels(const SkImageInfo&, void*, size_t, const Options&, SkPMColor*, int*, int*)
mtklein36352bf2015-03-25 18:17:31 -070043 override;
44 SkEncodedFormat onGetEncodedFormat() const override { return kPNG_SkEncodedFormat; }
scroggob427db12015-08-12 07:24:13 -070045 bool onRewind() override;
scroggoc5560be2016-02-03 09:42:42 -080046 uint32_t onGetFillValue(SkColorType) const override;
scroggo46c57472015-09-30 08:57:13 -070047
msarettd1ec89b2016-08-03 12:59:27 -070048 // Helper to set up swizzler, color xforms, and color table. Also calls png_read_update_info.
49 bool initializeXforms(const SkImageInfo& requestedInfo, const Options&, SkPMColor* colorPtr,
50 int* colorCount);
msarette6dd0042015-10-09 11:07:34 -070051 SkSampler* getSampler(bool createIfNecessary) override {
52 SkASSERT(fSwizzler);
53 return fSwizzler;
54 }
msarett35bb74b2016-08-22 07:41:28 -070055 void allocateStorage();
msarettd1ec89b2016-08-03 12:59:27 -070056
57 virtual int readRows(const SkImageInfo& dstInfo, void* dst, size_t rowBytes, int count,
58 int startRow) = 0;
scroggo46c57472015-09-30 08:57:13 -070059
msarett549ca322016-08-17 08:54:08 -070060 SkPngCodec(const SkEncodedInfo&, const SkImageInfo&, SkStream*, SkPngChunkReader*,
mtklein6dc5b9a2016-08-24 12:22:32 -070061 void* png_ptr, void* info_ptr, int, int);
scroggod8d68552016-06-06 11:26:17 -070062
mtklein6dc5b9a2016-08-24 12:22:32 -070063 SkAutoTUnref<SkPngChunkReader> fPngChunkReader;
64 voidp fPng_ptr;
65 voidp fInfo_ptr;
scroggo05245902015-03-25 11:11:52 -070066
scroggod8d68552016-06-06 11:26:17 -070067 // These are stored here so they can be used both by normal decoding and scanline decoding.
msarettd1ec89b2016-08-03 12:59:27 -070068 SkAutoTUnref<SkColorTable> fColorTable; // May be unpremul.
69 SkAutoTDelete<SkSwizzler> fSwizzler;
70 std::unique_ptr<SkColorSpaceXform> fColorXform;
71 SkAutoTMalloc<uint8_t> fStorage;
72 uint8_t* fSwizzlerSrcRow;
73 uint32_t* fColorXformSrcRow;
74 size_t fSrcRowBytes;
scroggo05245902015-03-25 11:11:52 -070075
msarettd1ec89b2016-08-03 12:59:27 -070076 const int fNumberPasses;
77 int fBitDepth;
scroggo6f29a3c2015-07-07 06:09:08 -070078
msarettd1ec89b2016-08-03 12:59:27 -070079private:
msarettdcd5e652016-08-22 08:48:40 -070080 bool createColorTable(const SkImageInfo& dstInfo, int* ctableCount);
scroggo3eada2a2015-04-01 09:33:23 -070081 void destroyReadStruct();
scroggo05245902015-03-25 11:11:52 -070082
scroggof24f2242015-03-03 08:59:20 -080083 typedef SkCodec INHERITED;
84};