Skip to content

Commit

Permalink
Merge branch 'master' of github.com:LearnBoost/node-canvas
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Sep 6, 2012
2 parents 549c4bf + 6527af5 commit aabbb9b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
22 changes: 22 additions & 0 deletions 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);
});
Binary file added examples/images/grayscaleImage.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 14 additions & 6 deletions src/Image.cc
Expand Up @@ -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];
}
}
}

Expand Down

0 comments on commit aabbb9b

Please sign in to comment.