Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Adding support for grayscale JPEG images. #209

Merged
merged 1 commit into from

2 participants

@markcochran

If a grayscale image is currently passed through node-canvas it gets distorted due to their not being 4 channels per pixel. The added code will check the JPEG color space and correctly set the rgb and alpha pixel values for a grayscale image.

@tj tj merged commit 6527af5 into from
@tj
tj commented

thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 30, 2012
  1. @markcochran
This page is out of date. Refresh to see the latest.
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];
+ }
}
}
Something went wrong with that request. Please try again.