OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016 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. |
| 4 |
| 5 #include "ui/gfx/codec/install_skia_codec.h" |
| 6 |
| 7 #include "third_party/skia/include/core/SkBitmap.h" |
| 8 #include "third_party/skia/include/core/SkStream.h" |
| 9 #include "ui/gfx/codec/jpeg_codec.h" |
| 10 #include "ui/gfx/codec/png_codec.h" |
| 11 #include "ui/gfx/geometry/size.h" |
| 12 |
| 13 bool gfx::EncodeImage(SkWStream* dst, |
| 14 const SkPixmap& pixmap, |
| 15 SkEncodedImageFormat format, |
| 16 int quality) { |
| 17 if (kN32_SkColorType != pixmap.colorType() || |
| 18 (kPremul_SkAlphaType != pixmap.alphaType() && |
| 19 kOpaque_SkAlphaType != pixmap.alphaType())) { |
| 20 return false; |
| 21 } |
| 22 std::vector<unsigned char> buffer; |
| 23 switch (format) { |
| 24 case SkEncodedImageFormat::kPNG: |
| 25 return gfx::PNGCodec::Encode( |
| 26 reinterpret_cast<const unsigned char*>(pixmap.addr()), |
| 27 gfx::PNGCodec::FORMAT_SkBitmap, |
| 28 gfx::Size(pixmap.width(), pixmap.height()), |
| 29 static_cast<int>(pixmap.rowBytes()), false, |
| 30 std::vector<gfx::PNGCodec::Comment>(), &buffer) && |
| 31 dst->write(buffer.data(), buffer.size()); |
| 32 case SkEncodedImageFormat::kJPEG: |
| 33 return gfx::JPEGCodec::Encode( |
| 34 reinterpret_cast<const unsigned char*>(pixmap.addr()), |
| 35 gfx::JPEGCodec::FORMAT_SkBitmap, pixmap.width(), |
| 36 pixmap.height(), static_cast<int>(pixmap.rowBytes()), quality, |
| 37 &buffer) && |
| 38 dst->write(buffer.data(), buffer.size()); |
| 39 default: |
| 40 return false; |
| 41 } |
| 42 } |
OLD | NEW |