scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 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 | #include "SkCodec.h" |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 9 | #include "SkColorSpaceXform.h" |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 10 | #include "SkColorTable.h" |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 11 | #include "SkPngChunkReader.h" |
scroggo | 1dd3ea9 | 2015-03-20 11:55:55 -0700 | [diff] [blame] | 12 | #include "SkEncodedFormat.h" |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 13 | #include "SkImageInfo.h" |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 14 | #include "SkRefCnt.h" |
| 15 | #include "SkSwizzler.h" |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 16 | |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 17 | class SkStream; |
| 18 | |
| 19 | class SkPngCodec : public SkCodec { |
| 20 | public: |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 21 | static bool IsPng(const char*, size_t); |
scroggo | 9b2cdbf | 2015-07-10 12:07:02 -0700 | [diff] [blame] | 22 | |
scroggo | 1c005e4 | 2015-08-04 09:24:45 -0700 | [diff] [blame] | 23 | // Assume IsPng was called and returned true. |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 24 | static SkCodec* NewFromStream(SkStream*, SkPngChunkReader* = NULL); |
scroggo | 1c005e4 | 2015-08-04 09:24:45 -0700 | [diff] [blame] | 25 | |
scroggo | 9b2cdbf | 2015-07-10 12:07:02 -0700 | [diff] [blame] | 26 | virtual ~SkPngCodec(); |
| 27 | |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 28 | protected: |
mtklein | 6dc5b9a | 2016-08-24 12:22:32 -0700 | [diff] [blame] | 29 | // 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 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 42 | Result onGetPixels(const SkImageInfo&, void*, size_t, const Options&, SkPMColor*, int*, int*) |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 43 | override; |
| 44 | SkEncodedFormat onGetEncodedFormat() const override { return kPNG_SkEncodedFormat; } |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 45 | bool onRewind() override; |
scroggo | c5560be | 2016-02-03 09:42:42 -0800 | [diff] [blame] | 46 | uint32_t onGetFillValue(SkColorType) const override; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 47 | |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 48 | // 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); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 51 | SkSampler* getSampler(bool createIfNecessary) override { |
| 52 | SkASSERT(fSwizzler); |
| 53 | return fSwizzler; |
| 54 | } |
msarett | 35bb74b | 2016-08-22 07:41:28 -0700 | [diff] [blame] | 55 | void allocateStorage(); |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 56 | |
| 57 | virtual int readRows(const SkImageInfo& dstInfo, void* dst, size_t rowBytes, int count, |
| 58 | int startRow) = 0; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 59 | |
msarett | 549ca32 | 2016-08-17 08:54:08 -0700 | [diff] [blame] | 60 | SkPngCodec(const SkEncodedInfo&, const SkImageInfo&, SkStream*, SkPngChunkReader*, |
mtklein | 6dc5b9a | 2016-08-24 12:22:32 -0700 | [diff] [blame] | 61 | void* png_ptr, void* info_ptr, int, int); |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 62 | |
mtklein | 6dc5b9a | 2016-08-24 12:22:32 -0700 | [diff] [blame] | 63 | SkAutoTUnref<SkPngChunkReader> fPngChunkReader; |
| 64 | voidp fPng_ptr; |
| 65 | voidp fInfo_ptr; |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 66 | |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 67 | // These are stored here so they can be used both by normal decoding and scanline decoding. |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 68 | 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; |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 75 | |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 76 | const int fNumberPasses; |
| 77 | int fBitDepth; |
scroggo | 6f29a3c | 2015-07-07 06:09:08 -0700 | [diff] [blame] | 78 | |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 79 | private: |
msarett | dcd5e65 | 2016-08-22 08:48:40 -0700 | [diff] [blame] | 80 | bool createColorTable(const SkImageInfo& dstInfo, int* ctableCount); |
scroggo | 3eada2a | 2015-04-01 09:33:23 -0700 | [diff] [blame] | 81 | void destroyReadStruct(); |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 82 | |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 83 | typedef SkCodec INHERITED; |
| 84 | }; |