Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: nativeImage.crop().toBitmap() returning garbage #25773

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 14 additions & 7 deletions shell/common/api/electron_api_native_image.cc
Expand Up @@ -180,13 +180,20 @@ v8::Local<v8::Value> NativeImage::ToBitmap(gin::Arguments* args) {

const SkBitmap bitmap =
image_.AsImageSkia().GetRepresentation(scale_factor).GetBitmap();
SkPixelRef* ref = bitmap.pixelRef();
if (!ref)
return node::Buffer::New(args->isolate(), 0).ToLocalChecked();
return node::Buffer::Copy(args->isolate(),
reinterpret_cast<const char*>(ref->pixels()),
bitmap.computeByteSize())
.ToLocalChecked();

SkImageInfo info =
SkImageInfo::MakeN32Premul(bitmap.width(), bitmap.height());

auto array_buffer =
v8::ArrayBuffer::New(args->isolate(), info.computeMinByteSize());
auto backing_store = array_buffer->GetBackingStore();
if (bitmap.readPixels(info, backing_store->Data(), info.minRowBytes(), 0,
0)) {
return node::Buffer::New(args->isolate(), array_buffer, 0,
info.computeMinByteSize())
.ToLocalChecked();
}
return node::Buffer::New(args->isolate(), 0).ToLocalChecked();
}

v8::Local<v8::Value> NativeImage::ToJPEG(v8::Isolate* isolate, int quality) {
Expand Down
6 changes: 6 additions & 0 deletions spec/api-native-image-spec.js
Expand Up @@ -498,6 +498,12 @@ describe('nativeImage module', () => {
expect(cropB.getSize()).to.deep.equal({ width: 25, height: 64 });
expect(cropA.toPNG().equals(cropB.toPNG())).to.be.false();
});

it('toBitmap() returns a buffer of the right size', () => {
const image = nativeImage.createFromPath(path.join(__dirname, 'fixtures', 'assets', 'logo.png'));
const crop = image.crop({ width: 25, height: 64, x: 0, y: 0 });
expect(crop.toBitmap().length).to.equal(25 * 64 * 4);
});
});

describe('getAspectRatio()', () => {
Expand Down