Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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

  • Loading branch information...
commit aabbb9b8f70d2da57896f5f4b9baa544b1e3fb42 2 parents 549c4bf + 6527af5
@tj tj authored
View
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);
+});
View
BIN  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.
View
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];
+ }
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.