Skip to content

Commit c810f8f

Browse files
authored
Merge pull request Automattic#841 from timknip/master
Add windows jpeg support
2 parents 9129399 + 42e9a74 commit c810f8f

File tree

4 files changed

+82
-4
lines changed

4 files changed

+82
-4
lines changed

binding.gyp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,18 @@
44
'variables': {
55
'GTK_Root%': 'C:/GTK', # Set the location of GTK all-in-one bundle
66
'with_jpeg%': 'false',
7-
'with_gif%': 'false'
7+
'with_gif%': 'false',
8+
'variables': { # Nest jpeg_root to evaluate it before with_jpeg
9+
'jpeg_root%': '<!(node ./util/win_jpeg_lookup)'
10+
},
11+
'jpeg_root%': '<(jpeg_root)', # Take value of nested variable
12+
'conditions': [
13+
['jpeg_root==""', {
14+
'with_jpeg%': 'false'
15+
}, {
16+
'with_jpeg%': 'true'
17+
}]
18+
]
819
}
920
}, { # 'OS!="win"'
1021
'variables': {
@@ -119,8 +130,17 @@
119130
],
120131
'conditions': [
121132
['OS=="win"', {
133+
'copies': [{
134+
'destination': '<(PRODUCT_DIR)',
135+
'files': [
136+
'<(jpeg_root)/bin/jpeg62.dll',
137+
]
138+
}],
139+
'include_dirs': [
140+
'<(jpeg_root)/include'
141+
],
122142
'libraries': [
123-
'-l<(GTK_Root)/lib/jpeg.lib'
143+
'-l<(jpeg_root)/lib/jpeg.lib',
124144
]
125145
}, {
126146
'libraries': [

src/Image.cc

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ Image::loadGIFFromBuffer(uint8_t *buf, unsigned len) {
640640

641641
// libjpeg 6.2 does not have jpeg_mem_src; define it ourselves here unless
642642
// libjpeg 8 is installed.
643-
#if JPEG_LIB_VERSION < 80
643+
#if JPEG_LIB_VERSION < 80 && !defined(MEM_SRCDST_SUPPORTED)
644644

645645
/* Read JPEG image from a memory segment */
646646
static void
@@ -880,7 +880,11 @@ cairo_status_t
880880
Image::loadJPEG(FILE *stream) {
881881
cairo_status_t status;
882882

883+
#if defined(_MSC_VER)
884+
if (false) { // Force using loadJPEGFromBuffer
885+
#else
883886
if (data_mode == DATA_IMAGE) { // Can lazily read in the JPEG.
887+
#endif
884888
// JPEG setup
885889
struct jpeg_decompress_struct args;
886890
struct jpeg_error_mgr err;
@@ -915,7 +919,13 @@ Image::loadJPEG(FILE *stream) {
915919
if (!status) status = assignDataAsMime(buf, len, CAIRO_MIME_TYPE_JPEG);
916920
} else if (DATA_MIME == data_mode) {
917921
status = decodeJPEGBufferIntoMimeSurface(buf, len);
918-
} else {
922+
}
923+
#if defined(_MSC_VER)
924+
else if (DATA_IMAGE == data_mode) {
925+
status = loadJPEGFromBuffer(buf, len);
926+
}
927+
#endif
928+
else {
919929
status = CAIRO_STATUS_READ_ERROR;
920930
}
921931

test/image.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var Canvas = require('../')
99

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

1314
describe('Image', function () {
1415
it('should require new', function () {
@@ -19,6 +20,32 @@ describe('Image', function () {
1920
assert.ok(Image instanceof Function);
2021
});
2122

23+
it('Image set src to JPEG', function () {
24+
var img = new Image
25+
, onloadCalled = 0
26+
, onerrorCalled = 0;
27+
28+
assert.strictEqual(null, img.onload);
29+
assert.strictEqual(false, img.complete);
30+
31+
img.onload = function () {
32+
onloadCalled += 1;
33+
assert.strictEqual(img.src, jpg_face);
34+
assert.strictEqual(485, img.width);
35+
assert.strictEqual(401, img.height);
36+
assert.strictEqual(true, img.complete);
37+
};
38+
39+
img.onerror = function (e) {
40+
onerrorCalled += 1;
41+
};
42+
43+
img.src = jpg_face;
44+
assert.strictEqual(1, onloadCalled);
45+
assert.strictEqual(0, onerrorCalled);
46+
assert.strictEqual(img.src, jpg_face);
47+
});
48+
2249
it('Image#onload', function () {
2350
var img = new Image
2451
, onloadCalled = 0;

util/win_jpeg_lookup.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
var fs = require('fs')
2+
var paths = ['C:/libjpeg-turbo']
3+
4+
if (process.arch === 'x64') {
5+
paths.unshift('C:/libjpeg-turbo64')
6+
}
7+
8+
paths.forEach(function(path){
9+
if (exists(path)) {
10+
process.stdout.write(path)
11+
process.exit()
12+
}
13+
})
14+
15+
function exists(path) {
16+
try {
17+
return fs.lstatSync(path).isDirectory()
18+
} catch(e) {
19+
return false
20+
}
21+
}

0 commit comments

Comments
 (0)