license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame^] | 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 |
| 3 | // found in the LICENSE file. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 4 | |
| 5 | #ifndef BASE_GFX_PNG_ENCODER_H__ |
| 6 | #define BASE_GFX_PNG_ENCODER_H__ |
| 7 | |
| 8 | #include <vector> |
| 9 | |
| 10 | #include "base/basictypes.h" |
| 11 | |
| 12 | // Interface for encoding PNG data. This is a wrapper around libpng, |
| 13 | // which has an inconvenient interface for callers. This is currently designed |
| 14 | // for use in tests only (where we control the files), so the handling isn't as |
| 15 | // robust as would be required for a browser (see Decode() for more). WebKit |
| 16 | // has its own more complicated PNG decoder which handles, among other things, |
| 17 | // partially downloaded data. |
| 18 | class PNGEncoder { |
| 19 | public: |
| 20 | enum ColorFormat { |
| 21 | // 3 bytes per pixel (packed), in RGB order regardless of endianness. |
| 22 | // This is the native JPEG format. |
| 23 | FORMAT_RGB, |
| 24 | |
| 25 | // 4 bytes per pixel, in RGBA order in memory regardless of endianness. |
| 26 | FORMAT_RGBA, |
| 27 | |
| 28 | // 4 bytes per pixel, in BGRA order in memory regardless of endianness. |
| 29 | // This is the default Windows DIB order. |
| 30 | FORMAT_BGRA |
| 31 | }; |
| 32 | |
| 33 | // Encodes the given raw 'input' data, with each pixel being represented as |
| 34 | // given in 'format'. The encoded PNG data will be written into the supplied |
| 35 | // vector and true will be returned on success. On failure (false), the |
| 36 | // contents of the output buffer are undefined. |
| 37 | // |
| 38 | // When writing alpha values, the input colors are assumed to be post |
| 39 | // multiplied. |
| 40 | // |
| 41 | // w, h: dimensions of the image |
| 42 | // row_byte_width: the width in bytes of each row. This may be greater than |
| 43 | // w * bytes_per_pixel if there is extra padding at the end of each row |
| 44 | // (often, each row is padded to the next machine word). |
| 45 | // discard_transparency: when true, and when the input data format includes |
| 46 | // alpha values, these alpha values will be discarded and only RGB will be |
| 47 | // written to the resulting file. Otherwise, alpha values in the input |
| 48 | // will be preserved. |
| 49 | static bool Encode(const unsigned char* input, ColorFormat format, |
| 50 | int w, int h, int row_byte_width, |
| 51 | bool discard_transparency, |
| 52 | std::vector<unsigned char>* output); |
| 53 | |
| 54 | private: |
| 55 | DISALLOW_EVIL_CONSTRUCTORS(PNGEncoder); |
| 56 | }; |
| 57 | |
| 58 | #endif // BASE_GFX_PNG_ENCODER_H__ |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame^] | 59 | |