Skip to content

windows jpeg support #841

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 1 commit into from
May 3, 2017
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
24 changes: 22 additions & 2 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,18 @@
'variables': {
'GTK_Root%': 'C:/GTK', # Set the location of GTK all-in-one bundle
'with_jpeg%': 'false',
'with_gif%': 'false'
'with_gif%': '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 @@ -119,8 +130,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
14 changes: 12 additions & 2 deletions src/Image.cc
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +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
Expand Down Expand Up @@ -880,7 +880,11 @@ cairo_status_t
Image::loadJPEG(FILE *stream) {
cairo_status_t status;

#if defined(_MSC_VER)
if (false) { // Force using loadJPEGFromBuffer
#else
if (data_mode == DATA_IMAGE) { // Can lazily read in the JPEG.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why not put the #if defined(_MSC_VER) inside of this this if-statment and avoid if (false) { and duplicating if (data_mode == DATA_IMAGE) {?

Copy link
Author

Choose a reason for hiding this comment

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

@LinusU To prevent repeating the buffer reading stuff https://github.com/Automattic/node-canvas/blob/master/src/Image.cc#L901-L911
Can of course refactor if you wish. Let me know.

#endif
// JPEG setup
struct jpeg_decompress_struct args;
struct jpeg_error_mgr err;
Expand Down Expand Up @@ -915,7 +919,13 @@ Image::loadJPEG(FILE *stream) {
if (!status) status = assignDataAsMime(buf, len, CAIRO_MIME_TYPE_JPEG);
} else if (DATA_MIME == data_mode) {
status = decodeJPEGBufferIntoMimeSurface(buf, len);
} else {
}
#if defined(_MSC_VER)
else if (DATA_IMAGE == data_mode) {
status = loadJPEGFromBuffer(buf, len);
}
#endif
else {
status = CAIRO_STATUS_READ_ERROR;
}

Expand Down
27 changes: 27 additions & 0 deletions test/image.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var Canvas = require('../')

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

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

it('Image set src to JPEG', function () {
var img = new Image
, onloadCalled = 0
, onerrorCalled = 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) {
onerrorCalled += 1;
};

img.src = jpg_face;
assert.strictEqual(1, onloadCalled);
assert.strictEqual(0, onerrorCalled);
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
}
}