refactor: spanify image utils (#44127)

* refactor: electron::util::AddImageSkiaRepFromJPEG() takes a span arg

* refactor: electron::util::AddImageSkiaRepFromPNG() takes a span arg

* refactor: electron::util::AddImageSkiaRepFromBuffer() takes a span arg

* feat: add Node-buffer-to-base-span helper function

* refactor: electron::api::NativeImage::CreateFromPNG() now takes a span param

* refactor: electron::api::NativeImage::CreateFromJPEG() now takes a span param

* refactor: use base::as_byte_span()

* fix: -Wunsafe-buffer-usage warning in NativeImage::CreateFromNamedImage()

Warning fixed by this commit:

../../electron/shell/common/api/electron_api_native_image_mac.mm:131:11: error: function introduces unsafe buffer manipulation [-Werror,-Wunsafe-buffer-usage]
  131 |           {reinterpret_cast<const uint8_t*>((char*)[png_data bytes]),
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  132 |            [png_data length]});
      |            ~~~~~~~~~~~~~~~~~~
../../electron/shell/common/api/electron_api_native_image_mac.mm:131:11: note: See //docs/unsafe_buffers.md for help.

* chore: add // SAFETY comment for Node-buffer-to-span func

* chore: add // SAFETY comment for NSData-to-span func
This commit is contained in:
Charles Kerr 2024-10-10 08:34:55 -05:00 committed by GitHub
parent d51ef7d794
commit 3d2f68a9df
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 77 additions and 57 deletions

View file

@ -56,11 +56,10 @@ float GetScaleFactorFromPath(const base::FilePath& path) {
}
bool AddImageSkiaRepFromPNG(gfx::ImageSkia* image,
const unsigned char* data,
size_t size,
const base::span<const uint8_t> data,
double scale_factor) {
SkBitmap bitmap;
if (!gfx::PNGCodec::Decode(data, size, &bitmap))
if (!gfx::PNGCodec::Decode(data.data(), data.size(), &bitmap))
return false;
image->AddRepresentation(gfx::ImageSkiaRep(bitmap, scale_factor));
@ -68,10 +67,9 @@ bool AddImageSkiaRepFromPNG(gfx::ImageSkia* image,
}
bool AddImageSkiaRepFromJPEG(gfx::ImageSkia* image,
const unsigned char* data,
size_t size,
const base::span<const uint8_t> data,
double scale_factor) {
auto bitmap = gfx::JPEGCodec::Decode(data, size);
auto bitmap = gfx::JPEGCodec::Decode(data.data(), data.size());
if (!bitmap)
return false;
@ -89,29 +87,28 @@ bool AddImageSkiaRepFromJPEG(gfx::ImageSkia* image,
}
bool AddImageSkiaRepFromBuffer(gfx::ImageSkia* image,
const unsigned char* data,
size_t size,
const base::span<const uint8_t> data,
int width,
int height,
double scale_factor) {
// Try PNG first.
if (AddImageSkiaRepFromPNG(image, data, size, scale_factor))
if (AddImageSkiaRepFromPNG(image, data, scale_factor))
return true;
// Try JPEG second.
if (AddImageSkiaRepFromJPEG(image, data, size, scale_factor))
if (AddImageSkiaRepFromJPEG(image, data, scale_factor))
return true;
if (width == 0 || height == 0)
return false;
auto info = SkImageInfo::MakeN32(width, height, kPremul_SkAlphaType);
if (size < info.computeMinByteSize())
if (data.size() < info.computeMinByteSize())
return false;
SkBitmap bitmap;
bitmap.allocN32Pixels(width, height, false);
bitmap.writePixels({info, data, bitmap.rowBytes()});
bitmap.writePixels({info, data.data(), bitmap.rowBytes()});
image->AddRepresentation(gfx::ImageSkiaRep(bitmap, scale_factor));
return true;
@ -127,11 +124,8 @@ bool AddImageSkiaRepFromPath(gfx::ImageSkia* image,
return false;
}
const auto* data =
reinterpret_cast<const unsigned char*>(file_contents.data());
size_t size = file_contents.size();
return AddImageSkiaRepFromBuffer(image, data, size, 0, 0, scale_factor);
return AddImageSkiaRepFromBuffer(image, base::as_byte_span(file_contents), 0,
0, scale_factor);
}
bool PopulateImageSkiaRepsFromPath(gfx::ImageSkia* image,