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