2 * Copyright (c) 2006-2009, Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include "OwnArrayPtr.h"
35 #include "PNGImageEncoder.h"
46 // Converts BGRA->RGBA and RGBA->BGRA.
47 static void convertBetweenBGRAandRGBA(const unsigned char* input, int numberOfPixels,
48 unsigned char* output)
50 for (int x = 0; x < numberOfPixels; x++) {
51 const unsigned char* pixelIn = &input[x * 4];
52 unsigned char* pixelOut = &output[x * 4];
53 pixelOut[0] = pixelIn[2];
54 pixelOut[1] = pixelIn[1];
55 pixelOut[2] = pixelIn[0];
56 pixelOut[3] = pixelIn[3];
60 // Encoder --------------------------------------------------------------------
62 // This section of the code is based on nsPNGEncoder.cpp in Mozilla
63 // (Copyright 2005 Google Inc.)
65 // Passed around as the io_ptr in the png structs so our callbacks know where
67 struct PNGEncoderState {
68 PNGEncoderState(Vector<unsigned char>* o) : m_out(o) {}
69 Vector<unsigned char>* m_out;
72 // Called by libpng to flush its internal buffer to ours.
73 void encoderWriteCallback(png_structp png, png_bytep data, png_size_t size)
75 PNGEncoderState* state = static_cast<PNGEncoderState*>(png_get_io_ptr(png));
78 size_t oldSize = state->m_out->size();
79 state->m_out->resize(oldSize + size);
80 memcpy(&(*state->m_out)[oldSize], data, size);
83 // Automatically destroys the given write structs on destruction to make
84 // cleanup and error handling code cleaner.
85 class PNGWriteStructDestroyer {
87 PNGWriteStructDestroyer(png_struct** ps, png_info** pi)
92 ~PNGWriteStructDestroyer() {
93 png_destroy_write_struct(m_pngStruct, m_pngInfo);
97 png_struct** m_pngStruct;
102 bool PNGImageEncoder::encode(const SkBitmap& image, Vector<unsigned char>* output)
104 if (image.config() != SkBitmap::kARGB_8888_Config)
105 return false; // Only support ARGB at 8 bpp now.
108 bool result = PNGImageEncoder::encode(static_cast<unsigned char*>(
109 image.getPixels()), IntSize(image.width(), image.height()),
110 image.rowBytes(), output);
111 image.unlockPixels();
116 bool PNGImageEncoder::encode(const unsigned char* input, const IntSize& size,
118 Vector<unsigned char>* output)
120 int inputColorComponents = 4;
121 int outputColorComponents = 4;
122 int pngOutputColorType = PNG_COLOR_TYPE_RGB_ALPHA;
123 IntSize imageSize(size);
124 imageSize.clampNegativeToZero();
126 // Row stride should be at least as long as the length of the data.
127 if (inputColorComponents * imageSize.width() > bytesPerRow) {
132 png_struct* pngPtr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
139 png_info* infoPtr = png_create_info_struct(pngPtr);
141 png_destroy_write_struct(&pngPtr, NULL);
144 PNGWriteStructDestroyer destroyer(&pngPtr, &infoPtr);
146 if (setjmp(png_jmpbuf(pngPtr))) {
147 // The destroyer will ensure that the structures are cleaned up in this
148 // case, even though we may get here as a jump from random parts of the
149 // PNG library called below.
153 // Set our callback for libpng to give us the data.
154 PNGEncoderState state(output);
155 png_set_write_fn(pngPtr, &state, encoderWriteCallback, NULL);
157 png_set_IHDR(pngPtr, infoPtr, imageSize.width(), imageSize.height(), 8, pngOutputColorType,
158 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
159 PNG_FILTER_TYPE_DEFAULT);
160 png_write_info(pngPtr, infoPtr);
162 OwnArrayPtr<unsigned char> rowPixels(new unsigned char[imageSize.width() * outputColorComponents]);
163 for (int y = 0; y < imageSize.height(); y ++) {
164 convertBetweenBGRAandRGBA(&input[y * bytesPerRow], imageSize.width(), rowPixels.get());
165 png_write_row(pngPtr, rowPixels.get());
168 png_write_end(pngPtr, infoPtr);
172 } // namespace WebCore