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.
5 #ifndef UI_GFX_CODEC_PNG_CODEC_H_
6 #define UI_GFX_CODEC_PNG_CODEC_H_
11 #include "base/basictypes.h"
12 #include "ui/gfx/gfx_export.h"
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 {
29 // 3 bytes per pixel (packed), in RGB order regardless of endianness.
30 // This is the native JPEG format.
33 // 4 bytes per pixel, in RGBA order in memory regardless of endianness.
36 // 4 bytes per pixel, in BGRA order in memory regardless of endianness.
37 // This is the default Windows DIB order.
40 // 4 bytes per pixel, in pre-multiplied kARGB_8888_Config format. For use
41 // with directly writing to a skia bitmap.
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);
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.
59 // When writing alpha values, the input colors are assumed to be post
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
70 // comments: comments to be written in the png's metadata.
71 static bool Encode(const unsigned char* input,
75 bool discard_transparency,
76 const std::vector<Comment>& comments,
77 std::vector<unsigned char>* output);
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);
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);
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.
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,
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.)
112 // Returns true if data is non-null and can be decoded as a png, false
114 static bool Decode(const unsigned char* input, size_t input_size,
118 DISALLOW_COPY_AND_ASSIGN(PNGCodec);
123 #endif // UI_GFX_CODEC_PNG_CODEC_H_