diff --git a/binding.gyp b/binding.gyp index 66f0115ed..04531d71f 100755 --- a/binding.gyp +++ b/binding.gyp @@ -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%': '", "contributors": [ "Nathan Rajlich ", diff --git a/src/Image.cc b/src/Image.cc index 0147f878f..ab45ae36f 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) {} @@ -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; @@ -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; @@ -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) { 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; 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 + } +}