png_codec.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright (c) 2011 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef UI_GFX_CODEC_PNG_CODEC_H_
  5. #define UI_GFX_CODEC_PNG_CODEC_H_
  6. #include <stddef.h>
  7. #include <string>
  8. #include <vector>
  9. #include "ui/gfx/codec/codec_export.h"
  10. class SkBitmap;
  11. namespace gfx {
  12. class Size;
  13. // Interface for encoding and decoding PNG data. This is a wrapper around
  14. // libpng, which has an inconvenient interface for callers. This is currently
  15. // designed for use in tests only (where we control the files), so the handling
  16. // isn't as robust as would be required for a browser (see Decode() for more).
  17. // WebKit has its own more complicated PNG decoder which handles, among other
  18. // things, partially downloaded data.
  19. class CODEC_EXPORT PNGCodec {
  20. public:
  21. static constexpr int DEFAULT_ZLIB_COMPRESSION = 6;
  22. enum ColorFormat {
  23. // 4 bytes per pixel, in RGBA order in memory regardless of endianness.
  24. FORMAT_RGBA,
  25. // 4 bytes per pixel, in BGRA order in memory regardless of endianness.
  26. // This is the default Windows DIB order.
  27. FORMAT_BGRA,
  28. // SkBitmap format. For Encode() kN32_SkColorType (4 bytes per pixel) and
  29. // kAlpha_8_SkColorType (1 byte per pixel) formats are supported.
  30. // kAlpha_8_SkColorType gets encoded into a grayscale PNG treating alpha as
  31. // the color intensity. For Decode() kN32_SkColorType is always used.
  32. FORMAT_SkBitmap
  33. };
  34. // Represents a comment in the tEXt ancillary chunk of the png.
  35. struct CODEC_EXPORT Comment {
  36. Comment(const std::string& k, const std::string& t);
  37. ~Comment();
  38. std::string key;
  39. std::string text;
  40. };
  41. PNGCodec(const PNGCodec&) = delete;
  42. PNGCodec& operator=(const PNGCodec&) = delete;
  43. // Encodes the given raw 'input' data, with each pixel being represented as
  44. // given in 'format'. The encoded PNG data will be written into the supplied
  45. // vector and true will be returned on success. On failure (false), the
  46. // contents of the output buffer are undefined.
  47. //
  48. // When writing alpha values, the input colors are assumed to be post
  49. // multiplied.
  50. //
  51. // size: dimensions of the image
  52. // row_byte_width: the width in bytes of each row. This may be greater than
  53. // w * bytes_per_pixel if there is extra padding at the end of each row
  54. // (often, each row is padded to the next machine word).
  55. // discard_transparency: when true, and when the input data format includes
  56. // alpha values, these alpha values will be discarded and only RGB will be
  57. // written to the resulting file. Otherwise, alpha values in the input
  58. // will be preserved.
  59. // comments: comments to be written in the png's metadata.
  60. static bool Encode(const unsigned char* input,
  61. ColorFormat format,
  62. const Size& size,
  63. int row_byte_width,
  64. bool discard_transparency,
  65. const std::vector<Comment>& comments,
  66. std::vector<unsigned char>* output);
  67. // Call PNGCodec::Encode on the supplied SkBitmap |input|, which is assumed to
  68. // be kN32_SkColorType, 32 bits per pixel. The params |discard_transparency|
  69. // and |output| are passed directly to Encode; refer to Encode for more
  70. // information.
  71. static bool EncodeBGRASkBitmap(const SkBitmap& input,
  72. bool discard_transparency,
  73. std::vector<unsigned char>* output);
  74. // Call PNGCodec::Encode on the supplied SkBitmap |input|. The difference
  75. // between this and the previous method is that this restricts compression to
  76. // zlib q1, which is just rle encoding.
  77. static bool FastEncodeBGRASkBitmap(const SkBitmap& input,
  78. bool discard_transparency,
  79. std::vector<unsigned char>* output);
  80. // Call PNGCodec::Encode on the supplied SkBitmap |input|, which is assumed to
  81. // be kAlpha_8_SkColorType, 8 bits per pixel. The bitmap is encoded as a
  82. // grayscale PNG with alpha used for color intensity. The |output| param is
  83. // passed directly to Encode; refer to Encode for more information.
  84. static bool EncodeA8SkBitmap(const SkBitmap& input,
  85. std::vector<unsigned char>* output);
  86. // Decodes the PNG data contained in input of length input_size. The
  87. // decoded data will be placed in *output with the dimensions in *w and *h
  88. // on success (returns true). This data will be written in the 'format'
  89. // format. On failure, the values of these output variables are undefined.
  90. //
  91. // This function may not support all PNG types, and it hasn't been tested
  92. // with a large number of images, so assume a new format may not work. It's
  93. // really designed to be able to read in something written by Encode() above.
  94. static bool Decode(const unsigned char* input, size_t input_size,
  95. ColorFormat format, std::vector<unsigned char>* output,
  96. int* w, int* h);
  97. // Decodes the PNG data directly into the passed in SkBitmap. This is
  98. // significantly faster than the vector<unsigned char> version of Decode()
  99. // above when dealing with PNG files that are >500K, which a lot of theme
  100. // images are. (There are a lot of themes that have a NTP image of about ~1
  101. // megabyte, and those require a 7-10 megabyte side buffer.)
  102. //
  103. // Returns true if data is non-null and can be decoded as a png, false
  104. // otherwise.
  105. static bool Decode(const unsigned char* input, size_t input_size,
  106. SkBitmap* bitmap);
  107. };
  108. } // namespace gfx
  109. #endif // UI_GFX_CODEC_PNG_CODEC_H_