SkPngCodec.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. #ifndef SkPngCodec_DEFINED
  8. #define SkPngCodec_DEFINED
  9. #include "include/codec/SkCodec.h"
  10. #include "include/core/SkEncodedImageFormat.h"
  11. #include "include/core/SkImageInfo.h"
  12. #include "include/core/SkPngChunkReader.h"
  13. #include "include/core/SkRefCnt.h"
  14. #include "src/codec/SkColorTable.h"
  15. #include "src/codec/SkSwizzler.h"
  16. class SkStream;
  17. class SkPngCodec : public SkCodec {
  18. public:
  19. static bool IsPng(const char*, size_t);
  20. // Assume IsPng was called and returned true.
  21. static std::unique_ptr<SkCodec> MakeFromStream(std::unique_ptr<SkStream>, Result*,
  22. SkPngChunkReader* = nullptr);
  23. // FIXME (scroggo): Temporarily needed by AutoCleanPng.
  24. void setIdatLength(size_t len) { fIdatLength = len; }
  25. ~SkPngCodec() override;
  26. protected:
  27. // We hold the png_ptr and info_ptr as voidp to avoid having to include png.h
  28. // or forward declare their types here. voidp auto-casts to the real pointer types.
  29. struct voidp {
  30. voidp(void* ptr) : fPtr(ptr) {}
  31. template <typename T>
  32. operator T*() const { return (T*)fPtr; }
  33. explicit operator bool() const { return fPtr != nullptr; }
  34. void* fPtr;
  35. };
  36. SkPngCodec(SkEncodedInfo&&, std::unique_ptr<SkStream>, SkPngChunkReader*,
  37. void* png_ptr, void* info_ptr, int bitDepth);
  38. Result onGetPixels(const SkImageInfo&, void*, size_t, const Options&, int*)
  39. override;
  40. SkEncodedImageFormat onGetEncodedFormat() const override { return SkEncodedImageFormat::kPNG; }
  41. bool onRewind() override;
  42. SkSampler* getSampler(bool createIfNecessary) override;
  43. void applyXformRow(void* dst, const void* src);
  44. voidp png_ptr() { return fPng_ptr; }
  45. voidp info_ptr() { return fInfo_ptr; }
  46. SkSwizzler* swizzler() { return fSwizzler.get(); }
  47. // Initialize variables used by applyXformRow.
  48. void initializeXformParams();
  49. /**
  50. * Pass available input to libpng to process it.
  51. *
  52. * libpng will call any relevant callbacks installed. This will continue decoding
  53. * until it reaches the end of the file, or until a callback tells libpng to stop.
  54. */
  55. bool processData();
  56. Result onStartIncrementalDecode(const SkImageInfo& dstInfo, void* pixels, size_t rowBytes,
  57. const SkCodec::Options&) override;
  58. Result onIncrementalDecode(int*) override;
  59. sk_sp<SkPngChunkReader> fPngChunkReader;
  60. voidp fPng_ptr;
  61. voidp fInfo_ptr;
  62. // These are stored here so they can be used both by normal decoding and scanline decoding.
  63. sk_sp<SkColorTable> fColorTable; // May be unpremul.
  64. std::unique_ptr<SkSwizzler> fSwizzler;
  65. SkAutoTMalloc<uint8_t> fStorage;
  66. void* fColorXformSrcRow;
  67. const int fBitDepth;
  68. private:
  69. enum XformMode {
  70. // Requires only a swizzle pass.
  71. kSwizzleOnly_XformMode,
  72. // Requires only a color xform pass.
  73. kColorOnly_XformMode,
  74. // Requires a swizzle and a color xform.
  75. kSwizzleColor_XformMode,
  76. };
  77. bool createColorTable(const SkImageInfo& dstInfo);
  78. // Helper to set up swizzler, color xforms, and color table. Also calls png_read_update_info.
  79. SkCodec::Result initializeXforms(const SkImageInfo& dstInfo, const Options&);
  80. void initializeSwizzler(const SkImageInfo& dstInfo, const Options&, bool skipFormatConversion);
  81. void allocateStorage(const SkImageInfo& dstInfo);
  82. void destroyReadStruct();
  83. virtual Result decodeAllRows(void* dst, size_t rowBytes, int* rowsDecoded) = 0;
  84. virtual void setRange(int firstRow, int lastRow, void* dst, size_t rowBytes) = 0;
  85. virtual Result decode(int* rowsDecoded) = 0;
  86. XformMode fXformMode;
  87. int fXformWidth;
  88. size_t fIdatLength;
  89. bool fDecodedIdat;
  90. typedef SkCodec INHERITED;
  91. };
  92. #endif // SkPngCodec_DEFINED