[SyncFS] Build indexes from FileTracker entries on disk.
[chromium-blink-merge.git] / ui / gfx / codec / png_codec.h
blob5f849c462fc024748b6f9230c0775e997556b2ba
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_
8 #include <string>
9 #include <vector>
11 #include "base/basictypes.h"
12 #include "ui/gfx/gfx_export.h"
14 class SkBitmap;
16 namespace gfx {
18 class Size;
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,
33 // 4 bytes per pixel, in RGBA order in memory regardless of endianness.
34 FORMAT_RGBA,
36 // 4 bytes per pixel, in BGRA order in memory regardless of endianness.
37 // This is the default Windows DIB order.
38 FORMAT_BGRA,
40 // SkBitmap format. For Encode() kARGB_8888_Config (4 bytes per pixel) and
41 // kA8_Config (1 byte per pixel) formats are supported. kA8_Config gets
42 // encoded into a grayscale PNG treating alpha as the color intensity.
43 // For Decode() kARGB_8888_Config is always used.
44 FORMAT_SkBitmap
47 // Represents a comment in the tEXt ancillary chunk of the png.
48 struct GFX_EXPORT Comment {
49 Comment(const std::string& k, const std::string& t);
50 ~Comment();
52 std::string key;
53 std::string text;
56 // Encodes the given raw 'input' data, with each pixel being represented as
57 // given in 'format'. The encoded PNG data will be written into the supplied
58 // vector and true will be returned on success. On failure (false), the
59 // contents of the output buffer are undefined.
61 // When writing alpha values, the input colors are assumed to be post
62 // multiplied.
64 // size: dimensions of the image
65 // row_byte_width: the width in bytes of each row. This may be greater than
66 // w * bytes_per_pixel if there is extra padding at the end of each row
67 // (often, each row is padded to the next machine word).
68 // discard_transparency: when true, and when the input data format includes
69 // alpha values, these alpha values will be discarded and only RGB will be
70 // written to the resulting file. Otherwise, alpha values in the input
71 // will be preserved.
72 // comments: comments to be written in the png's metadata.
73 static bool Encode(const unsigned char* input,
74 ColorFormat format,
75 const Size& size,
76 int row_byte_width,
77 bool discard_transparency,
78 const std::vector<Comment>& comments,
79 std::vector<unsigned char>* output);
81 // Call PNGCodec::Encode on the supplied SkBitmap |input|, which is assumed
82 // to be kARGB_8888_Config, 32 bits per pixel. The params
83 // |discard_transparency| and |output| are passed directly to Encode; refer to
84 // Encode for more information. During the call, an SkAutoLockPixels lock
85 // is held on |input|.
86 static bool EncodeBGRASkBitmap(const SkBitmap& input,
87 bool discard_transparency,
88 std::vector<unsigned char>* output);
90 // Call PNGCodec::Encode on the supplied SkBitmap |input|. The difference
91 // between this and the previous method is that this restricts compression to
92 // zlib q1, which is just rle encoding.
93 static bool FastEncodeBGRASkBitmap(const SkBitmap& input,
94 bool discard_transparency,
95 std::vector<unsigned char>* output);
97 // Call PNGCodec::Encode on the supplied SkBitmap |input|, which is assumed
98 // to be kA8_Config, 8 bits per pixel. The bitmap is encoded as a grayscale
99 // PNG with alpha used for color intensity. The |output| param is passed
100 // directly to Encode; refer to Encode for more information. During the call,
101 // an SkAutoLockPixels lock is held on |input|.
102 static bool EncodeA8SkBitmap(const SkBitmap& input,
103 std::vector<unsigned char>* output);
105 // Decodes the PNG data contained in input of length input_size. The
106 // decoded data will be placed in *output with the dimensions in *w and *h
107 // on success (returns true). This data will be written in the 'format'
108 // format. On failure, the values of these output variables are undefined.
110 // This function may not support all PNG types, and it hasn't been tested
111 // with a large number of images, so assume a new format may not work. It's
112 // really designed to be able to read in something written by Encode() above.
113 static bool Decode(const unsigned char* input, size_t input_size,
114 ColorFormat format, std::vector<unsigned char>* output,
115 int* w, int* h);
117 // Decodes the PNG data directly into the passed in SkBitmap. This is
118 // significantly faster than the vector<unsigned char> version of Decode()
119 // above when dealing with PNG files that are >500K, which a lot of theme
120 // images are. (There are a lot of themes that have a NTP image of about ~1
121 // megabyte, and those require a 7-10 megabyte side buffer.)
123 // Returns true if data is non-null and can be decoded as a png, false
124 // otherwise.
125 static bool Decode(const unsigned char* input, size_t input_size,
126 SkBitmap* bitmap);
128 private:
129 DISALLOW_COPY_AND_ASSIGN(PNGCodec);
132 } // namespace gfx
134 #endif // UI_GFX_CODEC_PNG_CODEC_H_