Browse Source

Merge branch 'master' of github.com:LearnBoost/node-canvas

v1.x
TJ Holowaychuk 12 years ago
parent
commit
aabbb9b8f7
  1. 22
      examples/grayscale-image.js
  2. BIN
      examples/images/grayscaleImage.jpg
  3. 20
      src/Image.cc

22
examples/grayscale-image.js

@ -0,0 +1,22 @@
/**
* Passing grayscale image through canvas. Image should remain a gray square.
* If image is distorted with lines, then grayscale images are being distorted.
*/
var Canvas = require('../lib/canvas')
, Image = Canvas.Image
, canvas = new Canvas(288, 288)
, ctx = canvas.getContext('2d')
, fs = require('fs');
var grayScaleImage = fs.readFileSync(__dirname + '/images/grayscaleImage.jpg');
img = new Image;
img.src = grayScaleImage;
ctx.drawImage(img, 0, 0);
var out = fs.createWriteStream(__dirname + '/passedThroughGrayscale.jpg')
, stream = canvas.createJPEGStream();
stream.on('data', function(chunk){
out.write(chunk);
});

BIN
examples/images/grayscaleImage.jpg

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

20
src/Image.cc

@ -692,12 +692,20 @@ Image::decodeJPEGIntoSurface(jpeg_decompress_struct *info) {
jpeg_read_scanlines(info, &src, 1);
uint32_t *row = (uint32_t *)(data + stride * y);
for (int x = 0; x < width; ++x) {
int bx = 3 * x;
uint32_t *pixel = row + x;
*pixel = 255 << 24
| src[bx + 0] << 16
| src[bx + 1] << 8
| src[bx + 2];
if (info->jpeg_color_space == 1) {
uint32_t *pixel = row + x;
*pixel = 255 << 24
| src[x] << 16
| src[x] << 8
| src[x];
} else {
int bx = 3 * x;
uint32_t *pixel = row + x;
*pixel = 255 << 24
| src[bx + 0] << 16
| src[bx + 1] << 8
| src[bx + 2];
}
}
}

Loading…
Cancel
Save