From 9fedc8650167bdde4bdee7faa6dbf812d541ccce Mon Sep 17 00:00:00 2001 From: Tim Knip Date: Sat, 24 Sep 2016 17:48:58 +0200 Subject: [PATCH 1/4] windows jpeg support, use npm install --with_jpeg=true --- binding.gyp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/binding.gyp b/binding.gyp index 66f0115ed..636b49e90 100755 --- a/binding.gyp +++ b/binding.gyp @@ -3,6 +3,7 @@ ['OS=="win"', { 'variables': { 'GTK_Root%': 'C:/GTK', # Set the location of GTK all-in-one bundle + 'jpeg_root%': 'c:/libjpeg-turbo64', 'with_jpeg%': 'false', 'with_gif%': 'false', 'with_pango%': 'false', @@ -138,6 +139,15 @@ ], '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' ] From 50b080b54055a7ece8bdc47e3df6c1442844bc0d Mon Sep 17 00:00:00 2001 From: Tim Knip Date: Sat, 24 Sep 2016 17:50:12 +0200 Subject: [PATCH 2/4] v1.5.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 5ce2e0a8d..31dd0a59c 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "canvas", "description": "Canvas graphics API backed by Cairo", - "version": "1.5.0", + "version": "1.5.1", "author": "TJ Holowaychuk ", "contributors": [ "Nathan Rajlich ", From 0700b76dd159d83a30a4d50e179e0bac19766d87 Mon Sep 17 00:00:00 2001 From: Tim Knip Date: Sun, 25 Sep 2016 12:52:52 +0200 Subject: [PATCH 3/4] win32/jpeg: force Image::loadJPEGFromBuffer when loading from file --- binding.gyp | 2 +- src/Image.cc | 16 +++++++++++++--- test/image.test.js | 26 ++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/binding.gyp b/binding.gyp index 636b49e90..3c9715fe1 100755 --- a/binding.gyp +++ b/binding.gyp @@ -149,7 +149,7 @@ '<(jpeg_root)/include' ], 'libraries': [ - '-l<(GTK_Root)/lib/jpeg.lib' + '-l<(jpeg_root)/lib/jpeg.lib', ] }, { 'libraries': [ diff --git a/src/Image.cc b/src/Image.cc index 0147f878f..39a568969 100644 --- a/src/Image.cc +++ b/src/Image.cc @@ -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) {} @@ -880,7 +879,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. +#endif // JPEG setup struct jpeg_decompress_struct args; struct jpeg_error_mgr err; @@ -906,6 +909,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) { @@ -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; } diff --git a/test/image.test.js b/test/image.test.js index ae39d6207..1ff2e100d 100644 --- a/test/image.test.js +++ b/test/image.test.js @@ -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); it('should require new', function () { assert.throws(function () { Image(); }, TypeError); }); @@ -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... + }; + + img.src = jpg_face; + assert.strictEqual(1, onloadCalled); + assert.strictEqual(img.src, jpg_face); + }); + it('Image#onload', function () { var img = new Image , onloadCalled = 0; From 024f30cc7ab699f03adaae6d5aedab991f18c9ac Mon Sep 17 00:00:00 2001 From: Tim Knip Date: Sun, 25 Sep 2016 13:35:46 +0200 Subject: [PATCH 4/4] adding win_jpeg_lookup.js --- binding.gyp | 14 ++++++++++++-- src/Image.cc | 34 +++++++++++++++++++++++----------- util/win_jpeg_lookup.js | 21 +++++++++++++++++++++ 3 files changed, 56 insertions(+), 13 deletions(-) create mode 100644 util/win_jpeg_lookup.js diff --git a/binding.gyp b/binding.gyp index 3c9715fe1..04531d71f 100755 --- a/binding.gyp +++ b/binding.gyp @@ -3,11 +3,21 @@ ['OS=="win"', { 'variables': { 'GTK_Root%': 'C:/GTK', # Set the location of GTK all-in-one bundle - 'jpeg_root%': 'c:/libjpeg-turbo64', '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%': '= 10 uint8_t *buf; @@ -919,13 +937,7 @@ Image::loadJPEG(FILE *stream) { if (!status) status = assignDataAsMime(buf, len, CAIRO_MIME_TYPE_JPEG); } else if (DATA_MIME == data_mode) { status = decodeJPEGBufferIntoMimeSurface(buf, len); - } -#if defined(_MSC_VER) - else if (DATA_IMAGE == data_mode) { - status = loadJPEGFromBuffer(buf, len); - } -#endif - else { + } else { status = CAIRO_STATUS_READ_ERROR; } diff --git a/util/win_jpeg_lookup.js b/util/win_jpeg_lookup.js new file mode 100644 index 000000000..990c491ae --- /dev/null +++ b/util/win_jpeg_lookup.js @@ -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 + } +}