diff --git a/examples/grayscale-image.js b/examples/grayscale-image.js new file mode 100644 index 0000000..5c37657 --- /dev/null +++ b/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); +}); \ No newline at end of file diff --git a/examples/images/grayscaleImage.jpg b/examples/images/grayscaleImage.jpg new file mode 100644 index 0000000..672c2a0 Binary files /dev/null and b/examples/images/grayscaleImage.jpg differ diff --git a/src/Image.cc b/src/Image.cc index 00d39bb..6938884 100644 --- a/src/Image.cc +++ b/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]; + } } }