15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2011 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef UI_GFX_CODEC_PNG_CODEC_H_ 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define UI_GFX_CODEC_PNG_CODEC_H_ 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string> 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector> 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h" 1258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "ui/gfx/gfx_export.h" 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class SkBitmap; 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace gfx { 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class Size; 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Interface for encoding and decoding PNG data. This is a wrapper around 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// libpng, which has an inconvenient interface for callers. This is currently 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// designed for use in tests only (where we control the files), so the handling 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// isn't as robust as would be required for a browser (see Decode() for more). 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// WebKit has its own more complicated PNG decoder which handles, among other 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// things, partially downloaded data. 264e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)class GFX_EXPORT PNGCodec { 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) enum ColorFormat { 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // 3 bytes per pixel (packed), in RGB order regardless of endianness. 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // This is the native JPEG format. 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) FORMAT_RGB, 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // 4 bytes per pixel, in RGBA order in memory regardless of endianness. 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) FORMAT_RGBA, 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // 4 bytes per pixel, in BGRA order in memory regardless of endianness. 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // This is the default Windows DIB order. 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) FORMAT_BGRA, 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // SkBitmap format. For Encode() kARGB_8888_Config (4 bytes per pixel) and 415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // kA8_Config (1 byte per pixel) formats are supported. kA8_Config gets 425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // encoded into a grayscale PNG treating alpha as the color intensity. 435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // For Decode() kARGB_8888_Config is always used. 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) FORMAT_SkBitmap 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) }; 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Represents a comment in the tEXt ancillary chunk of the png. 484e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) struct GFX_EXPORT Comment { 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Comment(const std::string& k, const std::string& t); 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ~Comment(); 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string key; 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string text; 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) }; 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Encodes the given raw 'input' data, with each pixel being represented as 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // given in 'format'. The encoded PNG data will be written into the supplied 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // vector and true will be returned on success. On failure (false), the 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // contents of the output buffer are undefined. 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // When writing alpha values, the input colors are assumed to be post 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // multiplied. 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // size: dimensions of the image 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // row_byte_width: the width in bytes of each row. This may be greater than 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // w * bytes_per_pixel if there is extra padding at the end of each row 675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // (often, each row is padded to the next machine word). 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // discard_transparency: when true, and when the input data format includes 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // alpha values, these alpha values will be discarded and only RGB will be 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // written to the resulting file. Otherwise, alpha values in the input 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // will be preserved. 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // comments: comments to be written in the png's metadata. 73424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) static bool Encode(const unsigned char* input, 74424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) ColorFormat format, 75424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) const Size& size, 76424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) int row_byte_width, 77424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) bool discard_transparency, 78424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) const std::vector<Comment>& comments, 79424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) std::vector<unsigned char>* output); 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Call PNGCodec::Encode on the supplied SkBitmap |input|, which is assumed 825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // to be kARGB_8888_Config, 32 bits per pixel. The params 835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // |discard_transparency| and |output| are passed directly to Encode; refer to 845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Encode for more information. During the call, an SkAutoLockPixels lock 855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // is held on |input|. 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static bool EncodeBGRASkBitmap(const SkBitmap& input, 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool discard_transparency, 885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::vector<unsigned char>* output); 895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 903551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) // Call PNGCodec::Encode on the supplied SkBitmap |input|. The difference 913551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) // between this and the previous method is that this restricts compression to 923551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) // zlib q1, which is just rle encoding. 933551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) static bool FastEncodeBGRASkBitmap(const SkBitmap& input, 943551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) bool discard_transparency, 953551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) std::vector<unsigned char>* output); 963551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Call PNGCodec::Encode on the supplied SkBitmap |input|, which is assumed 985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // to be kA8_Config, 8 bits per pixel. The bitmap is encoded as a grayscale 995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // PNG with alpha used for color intensity. The |output| param is passed 1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // directly to Encode; refer to Encode for more information. During the call, 1015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // an SkAutoLockPixels lock is held on |input|. 1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) static bool EncodeA8SkBitmap(const SkBitmap& input, 1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) std::vector<unsigned char>* output); 1045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Decodes the PNG data contained in input of length input_size. The 1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // decoded data will be placed in *output with the dimensions in *w and *h 1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // on success (returns true). This data will be written in the 'format' 1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // format. On failure, the values of these output variables are undefined. 1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // 1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // This function may not support all PNG types, and it hasn't been tested 1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // with a large number of images, so assume a new format may not work. It's 1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // really designed to be able to read in something written by Encode() above. 1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static bool Decode(const unsigned char* input, size_t input_size, 1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ColorFormat format, std::vector<unsigned char>* output, 1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int* w, int* h); 1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Decodes the PNG data directly into the passed in SkBitmap. This is 1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // significantly faster than the vector<unsigned char> version of Decode() 1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // above when dealing with PNG files that are >500K, which a lot of theme 1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // images are. (There are a lot of themes that have a NTP image of about ~1 1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // megabyte, and those require a 7-10 megabyte side buffer.) 1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // 1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns true if data is non-null and can be decoded as a png, false 1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // otherwise. 1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static bool Decode(const unsigned char* input, size_t input_size, 1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) SkBitmap* bitmap); 1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private: 1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DISALLOW_COPY_AND_ASSIGN(PNGCodec); 1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace gfx 1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif // UI_GFX_CODEC_PNG_CODEC_H_ 135