| 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 #include "base/basictypes.h" | 5 #include "base/basictypes.h" |
| 6 #include "base/gfx/png_encoder.h" | 6 #include "base/gfx/png_encoder.h" |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/scoped_ptr.h" |
| 8 #include "third_party/skia/include/core/SkBitmap.h" | 9 #include "third_party/skia/include/core/SkBitmap.h" |
| 9 | 10 |
| 10 extern "C" { | 11 extern "C" { |
| 11 #include "third_party/libpng/png.h" | 12 #include "third_party/libpng/png.h" |
| 12 } | 13 } |
| 13 | 14 |
| 14 namespace { | 15 namespace { |
| 15 | 16 |
| 16 // Converts BGRA->RGBA and RGBA->BGRA. | 17 // Converts BGRA->RGBA and RGBA->BGRA. |
| 17 void ConvertBetweenBGRAandRGBA(const unsigned char* input, int pixel_width, | 18 void ConvertBetweenBGRAandRGBA(const unsigned char* input, int pixel_width, |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 } | 191 } |
| 191 | 192 |
| 192 png_write_end(png_ptr, info_ptr); | 193 png_write_end(png_ptr, info_ptr); |
| 193 return true; | 194 return true; |
| 194 } | 195 } |
| 195 | 196 |
| 196 // static | 197 // static |
| 197 bool PNGEncoder::EncodeBGRASkBitmap(const SkBitmap& input, | 198 bool PNGEncoder::EncodeBGRASkBitmap(const SkBitmap& input, |
| 198 bool discard_transparency, | 199 bool discard_transparency, |
| 199 std::vector<unsigned char>* output) { | 200 std::vector<unsigned char>* output) { |
| 200 SkAutoLockPixels input_lock(input); | 201 static const int bbp = 4; |
| 201 DCHECK(input.empty() || input.bytesPerPixel() == 4); | 202 |
| 202 return Encode(static_cast<unsigned char*>(input.getPixels()), | 203 SkAutoLockPixels lock_input(input); |
| 203 PNGEncoder::FORMAT_BGRA, input.width(), input.height(), | 204 DCHECK(input.empty() || input.bytesPerPixel() == bbp); |
| 204 input.rowBytes(), discard_transparency, output); | 205 |
| 206 // SkBitmaps are premultiplied, we need to unpremultiply them. |
| 207 scoped_array<unsigned char> divided( |
| 208 new unsigned char[input.width() * input.height() * bbp]); |
| 209 |
| 210 int i = 0; |
| 211 for (int y = 0; y < input.height(); y++) { |
| 212 for (int x = 0; x < input.width(); x++) { |
| 213 uint32 pixel = input.getAddr32(0, y)[x]; |
| 214 |
| 215 int alpha = SkColorGetA(pixel); |
| 216 if (alpha != 0 && alpha != 255) { |
| 217 divided[i + 0] = (SkColorGetR(pixel) << 8) / alpha; |
| 218 divided[i + 1] = (SkColorGetG(pixel) << 8) / alpha; |
| 219 divided[i + 2] = (SkColorGetB(pixel) << 8) / alpha; |
| 220 divided[i + 3] = alpha; |
| 221 } else { |
| 222 divided[i + 0] = SkColorGetR(pixel); |
| 223 divided[i + 1] = SkColorGetG(pixel); |
| 224 divided[i + 2] = SkColorGetB(pixel); |
| 225 divided[i + 3] = alpha; |
| 226 } |
| 227 i += bbp; |
| 228 } |
| 229 } |
| 230 |
| 231 return Encode(divided.get(), |
| 232 PNGEncoder::FORMAT_RGBA, input.width(), input.height(), |
| 233 input.width() * bbp, discard_transparency, output); |
| 205 } | 234 } |
| OLD | NEW |