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/base/ui_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 UI_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 UI_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 // Calls PNGCodec::EncodeWithCompressionLevel with the default compression 55 // level. 56 static bool Encode(const unsigned char* input, 57 ColorFormat format, 58 const Size& size, 59 int row_byte_width, 60 bool discard_transparency, 61 const std::vector<Comment>& comments, 62 std::vector<unsigned char>* output); 63 64 // Encodes the given raw 'input' data, with each pixel being represented as 65 // given in 'format'. The encoded PNG data will be written into the supplied 66 // vector and true will be returned on success. On failure (false), the 67 // contents of the output buffer are undefined. 68 // 69 // When writing alpha values, the input colors are assumed to be post 70 // multiplied. 71 // 72 // size: dimensions of the image 73 // row_byte_width: the width in bytes of each row. This may be greater than 74 // w * bytes_per_pixel if there is extra padding at the end of each row 75 // (often, each row is padded to the next machine word). 76 // discard_transparency: when true, and when the input data format includes 77 // alpha values, these alpha values will be discarded and only RGB will be 78 // written to the resulting file. Otherwise, alpha values in the input 79 // will be preserved. 80 // comments: comments to be written in the png's metadata. 81 // compression_level: An integer between -1 and 9, corresponding to zlib's 82 // compression levels. -1 is the default. 83 static bool EncodeWithCompressionLevel(const unsigned char* input, 84 ColorFormat format, 85 const Size& size, 86 int row_byte_width, 87 bool discard_transparency, 88 const std::vector<Comment>& comments, 89 int compression_level, 90 std::vector<unsigned char>* output); 91 92 // Call PNGCodec::Encode on the supplied SkBitmap |input|, which is assumed 93 // to be BGRA, 32 bits per pixel. The params |discard_transparency| and 94 // |output| are passed directly to Encode; refer to Encode for more 95 // information. During the call, an SkAutoLockPixels lock is held on |input|. 96 static bool EncodeBGRASkBitmap(const SkBitmap& input, 97 bool discard_transparency, 98 std::vector<unsigned char>* output); 99 100 // Decodes the PNG data contained in input of length input_size. The 101 // decoded data will be placed in *output with the dimensions in *w and *h 102 // on success (returns true). This data will be written in the 'format' 103 // format. On failure, the values of these output variables are undefined. 104 // 105 // This function may not support all PNG types, and it hasn't been tested 106 // with a large number of images, so assume a new format may not work. It's 107 // really designed to be able to read in something written by Encode() above. 108 static bool Decode(const unsigned char* input, size_t input_size, 109 ColorFormat format, std::vector<unsigned char>* output, 110 int* w, int* h); 111 112 // Decodes the PNG data directly into the passed in SkBitmap. This is 113 // significantly faster than the vector<unsigned char> version of Decode() 114 // above when dealing with PNG files that are >500K, which a lot of theme 115 // images are. (There are a lot of themes that have a NTP image of about ~1 116 // megabyte, and those require a 7-10 megabyte side buffer.) 117 // 118 // Returns true if data is non-null and can be decoded as a png, false 119 // otherwise. 120 static bool Decode(const unsigned char* input, size_t input_size, 121 SkBitmap* bitmap); 122 123 // Create a SkBitmap from a decoded BGRA DIB. The caller owns the returned 124 // SkBitmap. 125 static SkBitmap* CreateSkBitmapFromBGRAFormat( 126 std::vector<unsigned char>& bgra, int width, int height); 127 128 private: 129 DISALLOW_COPY_AND_ASSIGN(PNGCodec); 130 }; 131 132 } // namespace gfx 133 134 #endif // UI_GFX_CODEC_PNG_CODEC_H_ 135