2016-01-19 21:09:01 +01:00
|
|
|
// Copyright (c) 2016 The Chromium Embedded Framework Authors. All rights
|
|
|
|
// reserved. Use of this source code is governed by a BSD-style license that
|
|
|
|
// can be found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "libcef/browser/image_impl.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
2021-01-28 00:13:12 +01:00
|
|
|
#include "skia/ext/skia_utils_base.h"
|
2016-01-19 21:09:01 +01:00
|
|
|
#include "ui/gfx/codec/jpeg_codec.h"
|
|
|
|
#include "ui/gfx/codec/png_codec.h"
|
2022-03-26 02:12:30 +01:00
|
|
|
#include "ui/gfx/geometry/size.h"
|
2016-01-19 21:09:01 +01:00
|
|
|
#include "ui/gfx/image/image_png_rep.h"
|
|
|
|
#include "ui/gfx/image/image_skia.h"
|
2022-03-26 02:12:30 +01:00
|
|
|
#include "ui/gfx/image/image_skia_rep.h"
|
2016-01-19 21:09:01 +01:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
SkColorType GetSkColorType(cef_color_type_t color_type) {
|
|
|
|
switch (color_type) {
|
|
|
|
case CEF_COLOR_TYPE_RGBA_8888:
|
|
|
|
return kRGBA_8888_SkColorType;
|
|
|
|
case CEF_COLOR_TYPE_BGRA_8888:
|
|
|
|
return kBGRA_8888_SkColorType;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-05-08 17:22:46 +02:00
|
|
|
DCHECK(false);
|
2016-01-19 21:09:01 +01:00
|
|
|
return kUnknown_SkColorType;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkAlphaType GetSkAlphaType(cef_alpha_type_t alpha_type) {
|
|
|
|
switch (alpha_type) {
|
|
|
|
case CEF_ALPHA_TYPE_OPAQUE:
|
|
|
|
return kOpaque_SkAlphaType;
|
|
|
|
case CEF_ALPHA_TYPE_PREMULTIPLIED:
|
|
|
|
return kPremul_SkAlphaType;
|
|
|
|
case CEF_ALPHA_TYPE_POSTMULTIPLIED:
|
|
|
|
return kUnpremul_SkAlphaType;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-05-08 17:22:46 +02:00
|
|
|
DCHECK(false);
|
2016-01-19 21:09:01 +01:00
|
|
|
return kUnknown_SkAlphaType;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compress as PNG. Requires post-multiplied alpha.
|
|
|
|
bool PNGMethod(bool with_transparency,
|
|
|
|
const SkBitmap& bitmap,
|
|
|
|
std::vector<unsigned char>* compressed) {
|
|
|
|
return gfx::PNGCodec::Encode(
|
|
|
|
reinterpret_cast<unsigned char*>(bitmap.getPixels()),
|
2017-05-17 11:29:28 +02:00
|
|
|
bitmap.colorType() == kBGRA_8888_SkColorType ? gfx::PNGCodec::FORMAT_BGRA
|
|
|
|
: gfx::PNGCodec::FORMAT_RGBA,
|
2016-01-19 21:09:01 +01:00
|
|
|
gfx::Size(bitmap.width(), bitmap.height()),
|
|
|
|
static_cast<int>(bitmap.rowBytes()),
|
|
|
|
bitmap.alphaType() == kOpaque_SkAlphaType || !with_transparency,
|
2017-05-17 11:29:28 +02:00
|
|
|
std::vector<gfx::PNGCodec::Comment>(), compressed);
|
2016-01-19 21:09:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Compress as JPEG. This internally uses JCS_EXT_RGBX or JCS_EXT_BGRX which
|
|
|
|
// causes the alpha channel to be ignored. Requires post-multiplied alpha.
|
|
|
|
bool JPEGMethod(int quality,
|
|
|
|
const SkBitmap& bitmap,
|
|
|
|
std::vector<unsigned char>* compressed) {
|
2017-07-27 01:19:27 +02:00
|
|
|
return gfx::JPEGCodec::Encode(bitmap, quality, compressed);
|
2016-01-19 21:09:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// static
|
|
|
|
CefRefPtr<CefImage> CefImage::CreateImage() {
|
|
|
|
return new CefImageImpl();
|
|
|
|
}
|
|
|
|
|
|
|
|
CefImageImpl::CefImageImpl(const gfx::ImageSkia& image_skia)
|
2019-11-18 20:05:48 +01:00
|
|
|
: image_(image_skia) {}
|
2016-01-19 21:09:01 +01:00
|
|
|
|
|
|
|
bool CefImageImpl::IsEmpty() {
|
2019-11-18 20:05:48 +01:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2016-01-19 21:09:01 +01:00
|
|
|
return image_.IsEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefImageImpl::IsSame(CefRefPtr<CefImage> that) {
|
|
|
|
CefImageImpl* that_impl = static_cast<CefImageImpl*>(that.get());
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!that_impl) {
|
2016-01-19 21:09:01 +01:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-01-19 21:09:01 +01:00
|
|
|
|
|
|
|
// Quick check for the same object.
|
2023-01-02 23:59:03 +01:00
|
|
|
if (this == that_impl) {
|
2016-01-19 21:09:01 +01:00
|
|
|
return true;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-01-19 21:09:01 +01:00
|
|
|
|
2019-11-18 20:05:48 +01:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2016-01-19 21:09:01 +01:00
|
|
|
return image_.AsImageSkia().BackedBySameObjectAs(
|
|
|
|
that_impl->image_.AsImageSkia());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefImageImpl::AddBitmap(float scale_factor,
|
|
|
|
int pixel_width,
|
|
|
|
int pixel_height,
|
|
|
|
cef_color_type_t color_type,
|
|
|
|
cef_alpha_type_t alpha_type,
|
|
|
|
const void* pixel_data,
|
|
|
|
size_t pixel_data_size) {
|
|
|
|
const SkColorType ct = GetSkColorType(color_type);
|
|
|
|
const SkAlphaType at = GetSkAlphaType(alpha_type);
|
2017-05-17 11:29:28 +02:00
|
|
|
|
2016-01-19 21:09:01 +01:00
|
|
|
// Make sure the client passed in the expected values.
|
2023-01-02 23:59:03 +01:00
|
|
|
if (ct != kBGRA_8888_SkColorType && ct != kRGBA_8888_SkColorType) {
|
2016-01-19 21:09:01 +01:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
|
|
|
if (pixel_data_size != pixel_width * pixel_height * 4U) {
|
2016-01-19 21:09:01 +01:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-01-19 21:09:01 +01:00
|
|
|
|
|
|
|
SkBitmap bitmap;
|
|
|
|
if (!bitmap.tryAllocPixels(
|
|
|
|
SkImageInfo::Make(pixel_width, pixel_height, ct, at))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-10-20 19:45:20 +02:00
|
|
|
DCHECK_EQ(pixel_data_size, bitmap.computeByteSize());
|
2017-05-31 17:33:30 +02:00
|
|
|
memcpy(bitmap.getPixels(), pixel_data, pixel_data_size);
|
2016-01-19 21:09:01 +01:00
|
|
|
|
|
|
|
return AddBitmap(scale_factor, bitmap);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefImageImpl::AddPNG(float scale_factor,
|
|
|
|
const void* png_data,
|
|
|
|
size_t png_data_size) {
|
|
|
|
SkBitmap bitmap;
|
|
|
|
if (!gfx::PNGCodec::Decode(static_cast<const unsigned char*>(png_data),
|
|
|
|
png_data_size, &bitmap)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return AddBitmap(scale_factor, bitmap);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefImageImpl::AddJPEG(float scale_factor,
|
|
|
|
const void* jpeg_data,
|
|
|
|
size_t jpeg_data_size) {
|
2017-05-17 11:29:28 +02:00
|
|
|
std::unique_ptr<SkBitmap> bitmap(gfx::JPEGCodec::Decode(
|
|
|
|
static_cast<const unsigned char*>(jpeg_data), jpeg_data_size));
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!bitmap.get()) {
|
2016-01-19 21:09:01 +01:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-01-19 21:09:01 +01:00
|
|
|
|
|
|
|
return AddBitmap(scale_factor, *bitmap);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t CefImageImpl::GetWidth() {
|
2019-11-18 20:05:48 +01:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2016-01-19 21:09:01 +01:00
|
|
|
return image_.Width();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t CefImageImpl::GetHeight() {
|
2019-11-18 20:05:48 +01:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2016-01-19 21:09:01 +01:00
|
|
|
return image_.Height();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefImageImpl::HasRepresentation(float scale_factor) {
|
2019-11-18 20:05:48 +01:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2016-01-19 21:09:01 +01:00
|
|
|
return image_.AsImageSkia().HasRepresentation(scale_factor);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefImageImpl::RemoveRepresentation(float scale_factor) {
|
2019-11-18 20:05:48 +01:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2016-01-19 21:09:01 +01:00
|
|
|
gfx::ImageSkia image_skia = image_.AsImageSkia();
|
|
|
|
if (image_skia.HasRepresentation(scale_factor)) {
|
|
|
|
image_skia.RemoveRepresentation(scale_factor);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefImageImpl::GetRepresentationInfo(float scale_factor,
|
|
|
|
float& actual_scale_factor,
|
|
|
|
int& pixel_width,
|
|
|
|
int& pixel_height) {
|
2019-11-18 20:05:48 +01:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2016-01-19 21:09:01 +01:00
|
|
|
gfx::ImageSkia image_skia = image_.AsImageSkia();
|
2023-01-02 23:59:03 +01:00
|
|
|
if (image_skia.isNull()) {
|
2016-01-19 21:09:01 +01:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-01-19 21:09:01 +01:00
|
|
|
|
|
|
|
const gfx::ImageSkiaRep& rep = image_skia.GetRepresentation(scale_factor);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (rep.is_null()) {
|
2016-01-19 21:09:01 +01:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-01-19 21:09:01 +01:00
|
|
|
|
|
|
|
actual_scale_factor = rep.scale();
|
2018-10-24 22:37:39 +02:00
|
|
|
pixel_width = rep.GetBitmap().width();
|
|
|
|
pixel_height = rep.GetBitmap().height();
|
2016-01-19 21:09:01 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
CefRefPtr<CefBinaryValue> CefImageImpl::GetAsBitmap(float scale_factor,
|
|
|
|
cef_color_type_t color_type,
|
|
|
|
cef_alpha_type_t alpha_type,
|
|
|
|
int& pixel_width,
|
|
|
|
int& pixel_height) {
|
2016-01-19 21:09:01 +01:00
|
|
|
const SkColorType desired_ct = GetSkColorType(color_type);
|
|
|
|
const SkAlphaType desired_at = GetSkAlphaType(alpha_type);
|
|
|
|
|
2019-11-18 20:05:48 +01:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2016-01-19 21:09:01 +01:00
|
|
|
const SkBitmap* bitmap = GetBitmap(scale_factor);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!bitmap) {
|
2016-01-19 21:09:01 +01:00
|
|
|
return nullptr;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-01-19 21:09:01 +01:00
|
|
|
|
|
|
|
DCHECK(bitmap->readyToDraw());
|
|
|
|
|
|
|
|
pixel_width = bitmap->width();
|
|
|
|
pixel_height = bitmap->height();
|
|
|
|
|
|
|
|
if (bitmap->colorType() == desired_ct && bitmap->alphaType() == desired_at) {
|
|
|
|
// No conversion necessary.
|
2017-10-20 19:45:20 +02:00
|
|
|
return CefBinaryValue::Create(bitmap->getPixels(),
|
|
|
|
bitmap->computeByteSize());
|
2016-01-19 21:09:01 +01:00
|
|
|
} else {
|
|
|
|
SkBitmap desired_bitmap;
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!ConvertBitmap(*bitmap, &desired_bitmap, desired_ct, desired_at)) {
|
2016-01-19 21:09:01 +01:00
|
|
|
return nullptr;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-01-19 21:09:01 +01:00
|
|
|
DCHECK(desired_bitmap.readyToDraw());
|
|
|
|
return CefBinaryValue::Create(desired_bitmap.getPixels(),
|
2017-10-20 19:45:20 +02:00
|
|
|
desired_bitmap.computeByteSize());
|
2016-01-19 21:09:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefBinaryValue> CefImageImpl::GetAsPNG(float scale_factor,
|
|
|
|
bool with_transparency,
|
|
|
|
int& pixel_width,
|
|
|
|
int& pixel_height) {
|
2019-11-18 20:05:48 +01:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2016-01-19 21:09:01 +01:00
|
|
|
const SkBitmap* bitmap = GetBitmap(scale_factor);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!bitmap) {
|
2016-01-19 21:09:01 +01:00
|
|
|
return nullptr;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-01-19 21:09:01 +01:00
|
|
|
|
|
|
|
std::vector<unsigned char> compressed;
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!WritePNG(*bitmap, &compressed, with_transparency)) {
|
2016-01-19 21:09:01 +01:00
|
|
|
return nullptr;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-01-19 21:09:01 +01:00
|
|
|
|
|
|
|
pixel_width = bitmap->width();
|
|
|
|
pixel_height = bitmap->height();
|
|
|
|
|
|
|
|
return CefBinaryValue::Create(&compressed.front(), compressed.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefBinaryValue> CefImageImpl::GetAsJPEG(float scale_factor,
|
|
|
|
int quality,
|
|
|
|
int& pixel_width,
|
|
|
|
int& pixel_height) {
|
2019-11-18 20:05:48 +01:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2016-01-19 21:09:01 +01:00
|
|
|
const SkBitmap* bitmap = GetBitmap(scale_factor);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!bitmap) {
|
2016-01-19 21:09:01 +01:00
|
|
|
return nullptr;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-01-19 21:09:01 +01:00
|
|
|
|
|
|
|
std::vector<unsigned char> compressed;
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!WriteJPEG(*bitmap, &compressed, quality)) {
|
2016-01-19 21:09:01 +01:00
|
|
|
return nullptr;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-01-19 21:09:01 +01:00
|
|
|
|
|
|
|
pixel_width = bitmap->width();
|
|
|
|
pixel_height = bitmap->height();
|
|
|
|
|
|
|
|
return CefBinaryValue::Create(&compressed.front(), compressed.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefImageImpl::AddBitmaps(int32_t scale_1x_size,
|
|
|
|
const std::vector<SkBitmap>& bitmaps) {
|
|
|
|
if (scale_1x_size == 0) {
|
|
|
|
// Set the scale 1x size to the smallest bitmap pixel size.
|
|
|
|
int32_t min_size = std::numeric_limits<int32_t>::max();
|
|
|
|
for (const SkBitmap& bitmap : bitmaps) {
|
|
|
|
const int32_t size = std::max(bitmap.width(), bitmap.height());
|
2023-01-02 23:59:03 +01:00
|
|
|
if (size < min_size) {
|
2016-01-19 21:09:01 +01:00
|
|
|
min_size = size;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-01-19 21:09:01 +01:00
|
|
|
}
|
|
|
|
scale_1x_size = min_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const SkBitmap& bitmap : bitmaps) {
|
|
|
|
const int32_t size = std::max(bitmap.width(), bitmap.height());
|
2017-05-17 11:29:28 +02:00
|
|
|
const float scale_factor =
|
|
|
|
static_cast<float>(size) / static_cast<float>(scale_1x_size);
|
2016-01-19 21:09:01 +01:00
|
|
|
AddBitmap(scale_factor, bitmap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gfx::ImageSkia CefImageImpl::GetForced1xScaleRepresentation(
|
|
|
|
float scale_factor) const {
|
2019-11-18 20:05:48 +01:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2016-01-19 21:09:01 +01:00
|
|
|
if (scale_factor == 1.0f) {
|
|
|
|
// We can use the existing image without modification.
|
|
|
|
return image_.AsImageSkia();
|
|
|
|
}
|
|
|
|
|
|
|
|
const SkBitmap* bitmap = GetBitmap(scale_factor);
|
|
|
|
gfx::ImageSkia image_skia;
|
2023-01-02 23:59:03 +01:00
|
|
|
if (bitmap) {
|
2016-01-19 21:09:01 +01:00
|
|
|
image_skia.AddRepresentation(gfx::ImageSkiaRep(*bitmap, 1.0f));
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-01-19 21:09:01 +01:00
|
|
|
return image_skia;
|
|
|
|
}
|
|
|
|
|
2019-11-18 20:05:48 +01:00
|
|
|
gfx::ImageSkia CefImageImpl::AsImageSkia() const {
|
|
|
|
base::AutoLock lock_scope(lock_);
|
|
|
|
return image_.AsImageSkia();
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
bool CefImageImpl::AddBitmap(float scale_factor, const SkBitmap& bitmap) {
|
2016-08-08 14:56:16 +02:00
|
|
|
#if DCHECK_IS_ON()
|
2017-05-31 17:33:30 +02:00
|
|
|
DCHECK(bitmap.readyToDraw());
|
2016-08-08 14:56:16 +02:00
|
|
|
#endif
|
2016-01-19 21:09:01 +01:00
|
|
|
DCHECK(bitmap.colorType() == kBGRA_8888_SkColorType ||
|
|
|
|
bitmap.colorType() == kRGBA_8888_SkColorType);
|
|
|
|
|
2021-01-28 00:13:12 +01:00
|
|
|
// Convert to N32 (e.g. native encoding) format if not already in that format.
|
|
|
|
// N32 is expected by the Views framework and this early conversion avoids
|
|
|
|
// CHECKs in ImageSkiaRep and eventual conversion to N32 at some later point
|
|
|
|
// in the compositing pipeline.
|
|
|
|
SkBitmap n32_bitmap;
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!skia::SkBitmapToN32OpaqueOrPremul(bitmap, &n32_bitmap)) {
|
2021-01-28 00:13:12 +01:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2021-01-28 00:13:12 +01:00
|
|
|
|
|
|
|
gfx::ImageSkiaRep skia_rep(n32_bitmap, scale_factor);
|
2019-11-18 20:05:48 +01:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2016-01-19 21:09:01 +01:00
|
|
|
if (image_.IsEmpty()) {
|
2017-09-06 23:40:58 +02:00
|
|
|
image_ = gfx::Image(gfx::ImageSkia(skia_rep));
|
2016-01-19 21:09:01 +01:00
|
|
|
} else {
|
|
|
|
image_.AsImageSkia().AddRepresentation(skia_rep);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const SkBitmap* CefImageImpl::GetBitmap(float scale_factor) const {
|
2019-11-18 20:05:48 +01:00
|
|
|
lock_.AssertAcquired();
|
2016-01-19 21:09:01 +01:00
|
|
|
gfx::ImageSkia image_skia = image_.AsImageSkia();
|
2023-01-02 23:59:03 +01:00
|
|
|
if (image_skia.isNull()) {
|
2016-01-19 21:09:01 +01:00
|
|
|
return nullptr;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-01-19 21:09:01 +01:00
|
|
|
|
|
|
|
const gfx::ImageSkiaRep& rep = image_skia.GetRepresentation(scale_factor);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (rep.is_null()) {
|
2016-01-19 21:09:01 +01:00
|
|
|
return nullptr;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-01-19 21:09:01 +01:00
|
|
|
|
2018-10-24 22:37:39 +02:00
|
|
|
return &rep.GetBitmap();
|
2016-01-19 21:09:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
bool CefImageImpl::ConvertBitmap(const SkBitmap& src_bitmap,
|
|
|
|
SkBitmap* target_bitmap,
|
|
|
|
SkColorType target_ct,
|
|
|
|
SkAlphaType target_at) {
|
|
|
|
DCHECK(src_bitmap.readyToDraw());
|
|
|
|
DCHECK(src_bitmap.colorType() != target_ct ||
|
|
|
|
src_bitmap.alphaType() != target_at);
|
|
|
|
DCHECK(target_bitmap);
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
SkImageInfo target_info = SkImageInfo::Make(
|
|
|
|
src_bitmap.width(), src_bitmap.height(), target_ct, target_at);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!target_bitmap->tryAllocPixels(target_info)) {
|
2016-01-19 21:09:01 +01:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-01-19 21:09:01 +01:00
|
|
|
|
|
|
|
if (!src_bitmap.readPixels(target_info, target_bitmap->getPixels(),
|
|
|
|
target_bitmap->rowBytes(), 0, 0)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
DCHECK(target_bitmap->readyToDraw());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
bool CefImageImpl::WriteCompressedFormat(const SkBitmap& bitmap,
|
|
|
|
std::vector<unsigned char>* compressed,
|
2021-06-04 03:34:56 +02:00
|
|
|
CompressionMethod method) {
|
2016-01-19 21:09:01 +01:00
|
|
|
const SkBitmap* bitmap_ptr = nullptr;
|
|
|
|
SkBitmap bitmap_postalpha;
|
|
|
|
if (bitmap.alphaType() == kPremul_SkAlphaType) {
|
|
|
|
// Compression methods require post-multiplied alpha values.
|
|
|
|
if (!ConvertBitmap(bitmap, &bitmap_postalpha, bitmap.colorType(),
|
|
|
|
kUnpremul_SkAlphaType)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bitmap_ptr = &bitmap_postalpha;
|
|
|
|
} else {
|
|
|
|
bitmap_ptr = &bitmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
DCHECK(bitmap_ptr->readyToDraw());
|
|
|
|
DCHECK(bitmap_ptr->colorType() == kBGRA_8888_SkColorType ||
|
|
|
|
bitmap_ptr->colorType() == kRGBA_8888_SkColorType);
|
|
|
|
DCHECK(bitmap_ptr->alphaType() == kOpaque_SkAlphaType ||
|
|
|
|
bitmap_ptr->alphaType() == kUnpremul_SkAlphaType);
|
|
|
|
|
2021-06-04 03:34:56 +02:00
|
|
|
return std::move(method).Run(*bitmap_ptr, compressed);
|
2016-01-19 21:09:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
bool CefImageImpl::WritePNG(const SkBitmap& bitmap,
|
|
|
|
std::vector<unsigned char>* compressed,
|
|
|
|
bool with_transparency) {
|
|
|
|
return WriteCompressedFormat(bitmap, compressed,
|
2021-06-04 03:34:56 +02:00
|
|
|
base::BindOnce(PNGMethod, with_transparency));
|
2016-01-19 21:09:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
bool CefImageImpl::WriteJPEG(const SkBitmap& bitmap,
|
|
|
|
std::vector<unsigned char>* compressed,
|
|
|
|
int quality) {
|
|
|
|
return WriteCompressedFormat(bitmap, compressed,
|
2021-06-04 03:34:56 +02:00
|
|
|
base::BindOnce(JPEGMethod, quality));
|
2016-01-19 21:09:01 +01:00
|
|
|
}
|