OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_GFX_PNG_DECODER_H_ | 5 #ifndef APP_GFX_CODEC_PNG_CODEC_H_ |
6 #define BASE_GFX_PNG_DECODER_H_ | 6 #define APP_GFX_CODEC_PNG_CODEC_H_ |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 | 11 |
12 class SkBitmap; | 12 class SkBitmap; |
13 | 13 |
14 // Interface for decoding PNG data. This is a wrapper around libpng, | 14 namespace gfx { |
15 // which has an inconvenient interface for callers. This is currently designed | 15 |
16 // for use in tests only (where we control the files), so the handling isn't as | 16 // Interface for encoding and decoding PNG data. This is a wrapper around |
17 // robust as would be required for a browser (see Decode() for more). WebKit | 17 // libpng, which has an inconvenient interface for callers. This is currently |
18 // has its own more complicated PNG decoder which handles, among other things, | 18 // designed for use in tests only (where we control the files), so the handling |
19 // partially downloaded data. | 19 // isn't as robust as would be required for a browser (see Decode() for more). |
20 class PNGDecoder { | 20 // WebKit has its own more complicated PNG decoder which handles, among other |
| 21 // things, partially downloaded data. |
| 22 class PNGCodec { |
21 public: | 23 public: |
22 enum ColorFormat { | 24 enum ColorFormat { |
23 // 3 bytes per pixel (packed), in RGB order regardless of endianness. | 25 // 3 bytes per pixel (packed), in RGB order regardless of endianness. |
24 // This is the native JPEG format. | 26 // This is the native JPEG format. |
25 FORMAT_RGB, | 27 FORMAT_RGB, |
26 | 28 |
27 // 4 bytes per pixel, in RGBA order in memory regardless of endianness. | 29 // 4 bytes per pixel, in RGBA order in memory regardless of endianness. |
28 FORMAT_RGBA, | 30 FORMAT_RGBA, |
29 | 31 |
30 // 4 bytes per pixel, in BGRA order in memory regardless of endianness. | 32 // 4 bytes per pixel, in BGRA order in memory regardless of endianness. |
31 // This is the default Windows DIB order. | 33 // This is the default Windows DIB order. |
32 FORMAT_BGRA | 34 FORMAT_BGRA |
33 }; | 35 }; |
34 | 36 |
| 37 // Encodes the given raw 'input' data, with each pixel being represented as |
| 38 // given in 'format'. The encoded PNG data will be written into the supplied |
| 39 // vector and true will be returned on success. On failure (false), the |
| 40 // contents of the output buffer are undefined. |
| 41 // |
| 42 // When writing alpha values, the input colors are assumed to be post |
| 43 // multiplied. |
| 44 // |
| 45 // w, h: dimensions of the image |
| 46 // row_byte_width: the width in bytes of each row. This may be greater than |
| 47 // w * bytes_per_pixel if there is extra padding at the end of each row |
| 48 // (often, each row is padded to the next machine word). |
| 49 // discard_transparency: when true, and when the input data format includes |
| 50 // alpha values, these alpha values will be discarded and only RGB will be |
| 51 // written to the resulting file. Otherwise, alpha values in the input |
| 52 // will be preserved. |
| 53 static bool Encode(const unsigned char* input, ColorFormat format, |
| 54 int w, int h, int row_byte_width, |
| 55 bool discard_transparency, |
| 56 std::vector<unsigned char>* output); |
| 57 |
| 58 // Call PNGCodec::Encode on the supplied SkBitmap |input|, which is assumed |
| 59 // to be BGRA, 32 bits per pixel. The params |discard_transparency| and |
| 60 // |output| are passed directly to Encode; refer to Encode for more |
| 61 // information. During the call, an SkAutoLockPixels lock is held on |input|. |
| 62 static bool EncodeBGRASkBitmap(const SkBitmap& input, |
| 63 bool discard_transparency, |
| 64 std::vector<unsigned char>* output); |
| 65 |
35 // Decodes the PNG data contained in input of length input_size. The | 66 // Decodes the PNG data contained in input of length input_size. The |
36 // decoded data will be placed in *output with the dimensions in *w and *h | 67 // decoded data will be placed in *output with the dimensions in *w and *h |
37 // on success (returns true). This data will be written in the 'format' | 68 // on success (returns true). This data will be written in the 'format' |
38 // format. On failure, the values of these output variables are undefined. | 69 // format. On failure, the values of these output variables are undefined. |
39 // | 70 // |
40 // This function may not support all PNG types, and it hasn't been tested | 71 // This function may not support all PNG types, and it hasn't been tested |
41 // with a large number of images, so assume a new format may not work. It's | 72 // with a large number of images, so assume a new format may not work. It's |
42 // really designed to be able to read in something written by Encode() above. | 73 // really designed to be able to read in something written by Encode() above. |
43 static bool Decode(const unsigned char* input, size_t input_size, | 74 static bool Decode(const unsigned char* input, size_t input_size, |
44 ColorFormat format, std::vector<unsigned char>* output, | 75 ColorFormat format, std::vector<unsigned char>* output, |
45 int* w, int* h); | 76 int* w, int* h); |
46 | 77 |
47 // A convenience function for decoding PNGs as previously encoded by the PNG | 78 // A convenience function for decoding PNGs as previously encoded by the PNG |
48 // encoder. Chrome encodes png in the format PNGDecoder::FORMAT_BGRA. | 79 // encoder. Chrome encodes png in the format PNGCodec::FORMAT_BGRA. |
49 // | 80 // |
50 // Returns true if data is non-null and can be decoded as a png, false | 81 // Returns true if data is non-null and can be decoded as a png, false |
51 // otherwise. | 82 // otherwise. |
52 static bool Decode(const std::vector<unsigned char>* data, SkBitmap* icon); | 83 static bool Decode(const std::vector<unsigned char>* data, SkBitmap* icon); |
53 | 84 |
54 // Create a SkBitmap from a decoded BGRA DIB. The caller owns the returned | 85 // Create a SkBitmap from a decoded BGRA DIB. The caller owns the returned |
55 // SkBitmap. | 86 // SkBitmap. |
56 static SkBitmap* CreateSkBitmapFromBGRAFormat( | 87 static SkBitmap* CreateSkBitmapFromBGRAFormat( |
57 std::vector<unsigned char>& bgra, int width, int height); | 88 std::vector<unsigned char>& bgra, int width, int height); |
58 | 89 |
59 private: | 90 private: |
60 DISALLOW_COPY_AND_ASSIGN(PNGDecoder); | 91 DISALLOW_COPY_AND_ASSIGN(PNGCodec); |
61 }; | 92 }; |
62 | 93 |
63 #endif // BASE_GFX_PNG_DECODER_H_ | 94 } // namespace gfx |
| 95 |
| 96 #endif // APP_GFX_CODEC_PNG_CODEC_H_ |
OLD | NEW |