Skip to content

Support reading CMYK, YCCK JPEGs #1246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ canvas.createJPEGStream() // new
* Provide better, Node.js core-style coded errors for failed sys calls. (For
example, provide an error with code 'ENOENT' if setting `img.src` to a path
that does not exist.)
* Support reading CMYK, YCCK JPEGs.

### Added
* Prebuilds (#992) with different libc versions to the prebuilt binary (#1140)
Expand Down
103 changes: 62 additions & 41 deletions src/Image.cc
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,8 @@ Image::clearData() {
_surface = NULL;
}

free(_data);
_data = NULL;
delete[] _data;
_data = nullptr;

free(filename);
filename = NULL;
Expand Down Expand Up @@ -356,7 +356,7 @@ Image::readPNG(void *c, uint8_t *data, unsigned int len) {

Image::Image() {
filename = NULL;
_data = NULL;
_data = nullptr;
_data_len = 0;
_surface = NULL;
width = height = 0;
Expand Down Expand Up @@ -613,7 +613,7 @@ Image::loadGIFFromBuffer(uint8_t *buf, unsigned len) {
return CAIRO_STATUS_INVALID_SIZE;
}

uint8_t *data = (uint8_t *) malloc(naturalWidth * naturalHeight * 4);
uint8_t *data = new uint8_t[naturalWidth * naturalHeight * 4];
if (!data) {
GIF_CLOSE_FILE(gif);
this->errorInfo.set(NULL, "malloc", errno);
Expand Down Expand Up @@ -719,7 +719,7 @@ Image::loadGIFFromBuffer(uint8_t *buf, unsigned len) {
cairo_status_t status = cairo_surface_status(_surface);

if (status) {
free(data);
delete[] data;
return status;
}

Expand Down Expand Up @@ -777,25 +777,36 @@ static void jpeg_mem_src (j_decompress_ptr cinfo, void* buffer, long nbytes) {

#endif

void Image::jpegToARGB(jpeg_decompress_struct* args, uint8_t* data, uint8_t* src, JPEGDecodeL decode) {
int stride = naturalWidth * 4;
for (int y = 0; y < naturalHeight; ++y) {
jpeg_read_scanlines(args, &src, 1);
uint32_t *row = (uint32_t*)(data + stride * y);
for (int x = 0; x < naturalWidth; ++x) {
int bx = args->output_components * x;
row[x] = decode(src + bx);
}
}
}

/*
* Takes an initialised jpeg_decompress_struct and decodes the
* data into _surface.
*/

cairo_status_t
Image::decodeJPEGIntoSurface(jpeg_decompress_struct *args) {
int stride = naturalWidth * 4;
cairo_status_t status;

uint8_t *data = (uint8_t *) malloc(naturalWidth * naturalHeight * 4);
cairo_status_t status = CAIRO_STATUS_SUCCESS;

uint8_t *data = new uint8_t[naturalWidth * naturalHeight * 4];
if (!data) {
jpeg_abort_decompress(args);
jpeg_destroy_decompress(args);
this->errorInfo.set(NULL, "malloc", errno);
return CAIRO_STATUS_NO_MEMORY;
}

uint8_t *src = (uint8_t *) malloc(naturalWidth * args->output_components);
uint8_t *src = new uint8_t[naturalWidth * args->output_components];
if (!src) {
free(data);
jpeg_abort_decompress(args);
Expand All @@ -804,46 +815,56 @@ Image::decodeJPEGIntoSurface(jpeg_decompress_struct *args) {
return CAIRO_STATUS_NO_MEMORY;
}

for (int y = 0; y < naturalHeight; ++y) {
jpeg_read_scanlines(args, &src, 1);
uint32_t *row = (uint32_t *)(data + stride * y);
for (int x = 0; x < naturalWidth; ++x) {
if (args->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];
}
}
// These are the three main cases to handle. libjpeg converts YCCK to CMYK
// and YCbCr to RGB by default.
switch (args->out_color_space) {
case JCS_CMYK:
jpegToARGB(args, data, src, [](uint8_t const* src) {
uint16_t k = static_cast<uint16_t>(src[3]);
uint8_t r = k * src[0] / 255;
uint8_t g = k * src[1] / 255;
uint8_t b = k * src[2] / 255;
return 255 << 24 | r << 16 | g << 8 | b;
});
break;
case JCS_RGB:
jpegToARGB(args, data, src, [](uint8_t const* src) {
uint8_t r = src[0], g = src[1], b = src[2];
return 255 << 24 | r << 16 | g << 8 | b;
});
break;
case JCS_GRAYSCALE:
jpegToARGB(args, data, src, [](uint8_t const* src) {
uint8_t v = src[0];
return 255 << 24 | v << 16 | v << 8 | v;
});
break;
default:
this->errorInfo.set("Unsupported JPEG encoding");
status = CAIRO_STATUS_READ_ERROR;
break;
}

_surface = cairo_image_surface_create_for_data(
data
, CAIRO_FORMAT_ARGB32
, naturalWidth
, naturalHeight
, cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, naturalWidth));
if (!status) {
_surface = cairo_image_surface_create_for_data(
data
, CAIRO_FORMAT_ARGB32
, naturalWidth
, naturalHeight
, cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, naturalWidth));
}

jpeg_finish_decompress(args);
jpeg_destroy_decompress(args);
status = cairo_surface_status(_surface);

delete[] src;

if (status) {
free(data);
free(src);
delete[] data;
return status;
}

free(src);

_data = data;

return CAIRO_STATUS_SUCCESS;
Expand Down Expand Up @@ -908,7 +929,7 @@ Image::decodeJPEGBufferIntoMimeSurface(uint8_t *buf, unsigned len) {
// Data alloc
// 8 pixels per byte using Alpha Channel format to reduce memory requirement.
int buf_size = naturalHeight * cairo_format_stride_for_width(CAIRO_FORMAT_A1, naturalWidth);
uint8_t *data = (uint8_t *) malloc(buf_size);
uint8_t *data = new uint8_t[buf_size];
if (!data) {
this->errorInfo.set(NULL, "malloc", errno);
return CAIRO_STATUS_NO_MEMORY;
Expand All @@ -928,7 +949,7 @@ Image::decodeJPEGBufferIntoMimeSurface(uint8_t *buf, unsigned len) {
cairo_status_t status = cairo_surface_status(_surface);

if (status) {
free(data);
delete[] data;
return status;
}

Expand Down
6 changes: 4 additions & 2 deletions src/Image.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "Canvas.h"
#include "CanvasError.h"
#include <functional>

#ifdef HAVE_JPEG
#include <jpeglib.h>
Expand All @@ -34,7 +35,7 @@
#endif
#endif


using JPEGDecodeL = std::function<uint32_t (uint8_t* const src)>;

class Image: public Nan::ObjectWrap {
public:
Expand Down Expand Up @@ -81,6 +82,7 @@ class Image: public Nan::ObjectWrap {
#ifdef HAVE_JPEG
cairo_status_t loadJPEGFromBuffer(uint8_t *buf, unsigned len);
cairo_status_t loadJPEG(FILE *stream);
void jpegToARGB(jpeg_decompress_struct* args, uint8_t* data, uint8_t* src, JPEGDecodeL decode);
cairo_status_t decodeJPEGIntoSurface(jpeg_decompress_struct *info);
#if CAIRO_VERSION_MINOR >= 10
cairo_status_t decodeJPEGBufferIntoMimeSurface(uint8_t *buf, unsigned len);
Expand Down Expand Up @@ -115,7 +117,7 @@ class Image: public Nan::ObjectWrap {

private:
cairo_surface_t *_surface;
uint8_t *_data;
uint8_t *_data = nullptr;
int _data_len;
#ifdef HAVE_RSVG
RsvgHandle *_rsvg;
Expand Down
Binary file added test/fixtures/grayscale.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/fixtures/ycck.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading