Skip to content
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

Windows JPEG support #815

Closed
wants to merge 4 commits into from
Closed
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
24 changes: 22 additions & 2 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,18 @@
'with_jpeg%': 'false',
'with_gif%': 'false',
'with_pango%': 'false',
'with_freetype%': 'false'
'with_freetype%': 'false',
'variables': { # Nest jpeg_root to evaluate it before with_jpeg
'jpeg_root%': '<!(node ./util/win_jpeg_lookup)'
},
'jpeg_root%': '<(jpeg_root)', # Take value of nested variable
'conditions': [
['jpeg_root==""', {
'with_jpeg%': 'false'
}, {
'with_jpeg%': 'true'
}]
]
}
}, { # 'OS!="win"'
'variables': {
Expand Down Expand Up @@ -138,8 +149,17 @@
],
'conditions': [
['OS=="win"', {
'copies': [{
'destination': '<(PRODUCT_DIR)',
'files': [
'<(jpeg_root)/bin/jpeg62.dll',
]
}],
'include_dirs': [
'<(jpeg_root)/include'
],
'libraries': [
'-l<(GTK_Root)/lib/jpeg.lib'
'-l<(jpeg_root)/lib/jpeg.lib',
]
}, {
'libraries': [
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "canvas",
"description": "Canvas graphics API backed by Cairo",
"version": "1.5.0",
"version": "1.5.1",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't bump the version number, I'll do that as part of the release process

"author": "TJ Holowaychuk <[email protected]>",
"contributors": [
"Nathan Rajlich <[email protected]>",
Expand Down
26 changes: 24 additions & 2 deletions src/Image.cc
Original file line number Diff line number Diff line change
Expand Up @@ -640,8 +640,7 @@ Image::loadGIFFromBuffer(uint8_t *buf, unsigned len) {

// libjpeg 6.2 does not have jpeg_mem_src; define it ourselves here unless
// libjpeg 8 is installed.
#if JPEG_LIB_VERSION < 80

#if JPEG_LIB_VERSION < 80 && !defined(MEM_SRCDST_SUPPORTED)
/* Read JPEG image from a memory segment */
static void
init_source(j_decompress_ptr cinfo) {}
Expand Down Expand Up @@ -881,6 +880,27 @@ Image::loadJPEG(FILE *stream) {
cairo_status_t status;

if (data_mode == DATA_IMAGE) { // Can lazily read in the JPEG.
#if defined (_MSC_VER)
uint8_t *buf;
unsigned len;

fseek(stream, 0, SEEK_END);
len = ftell(stream);
fseek(stream, 0, SEEK_SET);

buf = (uint8_t *) malloc(len);

if (!buf) return CAIRO_STATUS_NO_MEMORY;

if (fread(buf, len, 1, stream) != 1) {
status = CAIRO_STATUS_READ_ERROR;
} else {
status = loadJPEGFromBuffer(buf, len);
}

fclose(stream);
free(buf);
#else
// JPEG setup
struct jpeg_decompress_struct args;
struct jpeg_error_mgr err;
Expand All @@ -896,6 +916,7 @@ Image::loadJPEG(FILE *stream) {

status = decodeJPEGIntoSurface(&args);
fclose(stream);
#endif
} else { // We'll need the actual source jpeg data, so read fully.
#if CAIRO_VERSION_MINOR >= 10
uint8_t *buf;
Expand All @@ -906,6 +927,7 @@ Image::loadJPEG(FILE *stream) {
fseek(stream, 0, SEEK_SET);

buf = (uint8_t *) malloc(len);

if (!buf) return CAIRO_STATUS_NO_MEMORY;

if (fread(buf, len, 1, stream) != 1) {
Expand Down
26 changes: 26 additions & 0 deletions test/image.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ var Canvas = require('../')

var png_checkers = __dirname + '/fixtures/checkers.png';
var png_clock = __dirname + '/fixtures/clock.png';
var jpg_face = __dirname + '/public/face.jpeg';

describe('Image', function () {
this.timeout(5000);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is quite a long time, is it really needed?

it('should require new', function () {
assert.throws(function () { Image(); }, TypeError);
});
Expand All @@ -19,6 +21,30 @@ describe('Image', function () {
assert.ok(Image instanceof Function);
});

it('Image set src to JPEG', function () {
var img = new Image
, onloadCalled = 0;

assert.strictEqual(null, img.onload);
assert.strictEqual(false, img.complete);

img.onload = function () {
onloadCalled += 1;
assert.strictEqual(img.src, jpg_face);
assert.strictEqual(485, img.width);
assert.strictEqual(401, img.height);
assert.strictEqual(true, img.complete);
};

img.onerror = function (e) {
console.error("ERROR", e); // temporary...
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about onerrorCalled = 0, onerrorCalled += 1 and assert.strictEqual(onerrorCalled, 0)?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@LinusU Missed your post ;-) I'll prep a fresh PR based on latest master when I find some time. Probably somewhere later this week.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you 💌

};

img.src = jpg_face;
assert.strictEqual(1, onloadCalled);
assert.strictEqual(img.src, jpg_face);
});

it('Image#onload', function () {
var img = new Image
, onloadCalled = 0;
Expand Down
21 changes: 21 additions & 0 deletions util/win_jpeg_lookup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
var fs = require('fs')
var paths = ['C:/libjpeg-turbo']

if (process.arch === 'x64') {
paths.unshift('C:/libjpeg-turbo64')
}

paths.forEach(function(path){
if (exists(path)) {
process.stdout.write(path)
process.exit()
}
})

function exists(path) {
try {
return fs.lstatSync(path).isDirectory()
} catch(e) {
return false
}
}