Skip to content

Commit

Permalink
Merge pull request electron#8175 from gerhardberger/createfrombuffer-…
Browse files Browse the repository at this point in the history
…bitmap

Add bitmap support to nativeImage.createFromBuffer
  • Loading branch information
kevinsawicki authored Dec 14, 2016
2 parents 25feb92 + 98d4e18 commit cd067bc
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 8 deletions.
32 changes: 28 additions & 4 deletions atom/common/api/atom_api_native_image.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ float GetScaleFactorFromPath(const base::FilePath& path) {
bool AddImageSkiaRep(gfx::ImageSkia* image,
const unsigned char* data,
size_t size,
int width,
int height,
double scale_factor) {
std::unique_ptr<SkBitmap> decoded(new SkBitmap());

Expand All @@ -87,8 +89,17 @@ bool AddImageSkiaRep(gfx::ImageSkia* image,
// Try JPEG.
decoded = gfx::JPEGCodec::Decode(data, size);

if (!decoded)
return false;
if (!decoded) {
// Try Bitmap
if (width > 0 && height > 0) {
decoded.reset(new SkBitmap);
decoded->allocN32Pixels(width, height, false);
decoded->setPixels(
const_cast<void*>(reinterpret_cast<const void*>(data)));
} else {
return false;
}
}

image->AddRepresentation(gfx::ImageSkiaRep(*decoded, scale_factor));
return true;
Expand All @@ -104,7 +115,7 @@ bool AddImageSkiaRep(gfx::ImageSkia* image,
const unsigned char* data =
reinterpret_cast<const unsigned char*>(file_contents.data());
size_t size = file_contents.size();
return AddImageSkiaRep(image, data, size, scale_factor);
return AddImageSkiaRep(image, data, size, 0, 0, scale_factor);
}

bool PopulateImageSkiaRepsFromPath(gfx::ImageSkia* image,
Expand Down Expand Up @@ -395,13 +406,26 @@ mate::Handle<NativeImage> NativeImage::CreateFromPath(
// static
mate::Handle<NativeImage> NativeImage::CreateFromBuffer(
mate::Arguments* args, v8::Local<v8::Value> buffer) {
int width = 0;
int height = 0;
double scale_factor = 1.;
args->GetNext(&scale_factor);

mate::Dictionary options;
if (args->GetNext(&options)) {
options.Get("width", &width);
options.Get("height", &height);
options.Get("scaleFactor", &scale_factor);
} else {
// TODO(kevinsawicki): Remove in 2.0, deprecate before then with warnings
args->GetNext(&scale_factor);
}

gfx::ImageSkia image_skia;
AddImageSkiaRep(&image_skia,
reinterpret_cast<unsigned char*>(node::Buffer::Data(buffer)),
node::Buffer::Length(buffer),
width,
height,
scale_factor);
return Create(args->isolate(), gfx::Image(image_skia));
}
Expand Down
10 changes: 6 additions & 4 deletions docs/api/native-image.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,17 @@ let image = nativeImage.createFromPath('/Users/somebody/images/icon.png')
console.log(image)
```

### `nativeImage.createFromBuffer(buffer[, scaleFactor])`
### `nativeImage.createFromBuffer(buffer[, options])`

* `buffer` [Buffer][buffer]
* `scaleFactor` Double (optional)
* `options` Object (optional)
* `width` Integer (optional) - Required for bitmap buffers.
* `height` Integer (optional) - Required for bitmap buffers.
* `scaleFactor` Double (optional) - Defaults to 1.0.

Returns `NativeImage`

Creates a new `NativeImage` instance from `buffer`. The default `scaleFactor` is
1.0.
Creates a new `NativeImage` instance from `buffer`.

### `nativeImage.createFromDataURL(dataURL)`

Expand Down
7 changes: 7 additions & 0 deletions docs/tutorial/planned-breaking-changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ nativeImage.toPNG()
nativeImage.toJpeg()
// Replace with
nativeImage.toJPEG()

// Deprecated
nativeImage.createFromBuffer(buffer, 1.0)
// Replace with
nativeImage.createFromBuffer(buffer, {
scaleFactor: 1.0
})
```

## `process`
Expand Down
26 changes: 26 additions & 0 deletions spec/api-native-image-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,32 @@ describe('nativeImage module', () => {

const imageC = nativeImage.createFromBuffer(imageA.toJPEG(100))
assert.deepEqual(imageC.getSize(), {width: 538, height: 190})

const imageD = nativeImage.createFromBuffer(imageA.toBitmap(),
{width: 538, height: 190})
assert.deepEqual(imageD.getSize(), {width: 538, height: 190})

const imageE = nativeImage.createFromBuffer(imageA.toBitmap(),
{width: 100, height: 200})
assert.deepEqual(imageE.getSize(), {width: 100, height: 200})

const imageF = nativeImage.createFromBuffer(imageA.toBitmap())
assert(imageF.isEmpty())

const imageG = nativeImage.createFromBuffer(imageA.toPNG(),
{width: 100, height: 200})
assert.deepEqual(imageG.getSize(), {width: 538, height: 190})

const imageH = nativeImage.createFromBuffer(imageA.toJPEG(100),
{width: 100, height: 200})
assert.deepEqual(imageH.getSize(), {width: 538, height: 190})

const imageI = nativeImage.createFromBuffer(imageA.toBitmap(),
{width: 538, height: 190, scaleFactor: 2.0})
assert.deepEqual(imageI.getSize(), {width: 269, height: 95})

const imageJ = nativeImage.createFromBuffer(imageA.toPNG(), 2.0)
assert.deepEqual(imageJ.getSize(), {width: 269, height: 95})
})
})

Expand Down

0 comments on commit cd067bc

Please sign in to comment.