Merge pull request #8175 from gerhardberger/createfrombuffer-bitmap
Add bitmap support to nativeImage.createFromBuffer
This commit is contained in:
commit
cd067bca37
4 changed files with 67 additions and 8 deletions
|
@ -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());
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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,
|
||||
|
@ -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));
|
||||
}
|
||||
|
|
|
@ -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)`
|
||||
|
||||
|
|
|
@ -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`
|
||||
|
|
|
@ -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})
|
||||
})
|
||||
})
|
||||
|
||||
|
|
Loading…
Reference in a new issue