Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d3e272a

Browse files
authored
feat: expose exif buffers (jpeg-js#47)
1 parent 7e7e2dc commit d3e272a

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

lib/decoder.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,16 @@ var JpegImage = (function jpegImage() {
657657
}
658658
}
659659
// TODO APP1 - Exif
660+
if (fileMarker === 0xFFE1) {
661+
if (appData[0] === 0x45 &&
662+
appData[1] === 0x78 &&
663+
appData[2] === 0x69 &&
664+
appData[3] === 0x66 &&
665+
appData[4] === 0) { // 'EXIF\x00'
666+
this.exifBuffer = appData.subarray(5, appData.length);
667+
}
668+
}
669+
660670
if (fileMarker === 0xFFEE) {
661671
if (appData[0] === 0x41 && appData[1] === 0x64 && appData[2] === 0x6F &&
662672
appData[3] === 0x62 && appData[4] === 0x65 && appData[5] === 0) { // 'Adobe\x00'
@@ -1050,6 +1060,7 @@ function decode(jpegData, opts) {
10501060
var image = {
10511061
width: decoder.width,
10521062
height: decoder.height,
1063+
exifBuffer: decoder.exifBuffer,
10531064
data: opts.useTArray ?
10541065
new Uint8Array(bytesNeeded) :
10551066
new Buffer(bytesNeeded)

lib/encoder.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,33 @@ function JPEGEncoder(quality) {
438438
writeByte(0); // thumbnwidth
439439
writeByte(0); // thumbnheight
440440
}
441-
441+
442+
function writeAPP1(exifBuffer) {
443+
if (!exifBuffer) return;
444+
445+
writeWord(0xFFE1); // APP1 marker
446+
447+
if (exifBuffer[0] === 0x45 &&
448+
exifBuffer[1] === 0x78 &&
449+
exifBuffer[2] === 0x69 &&
450+
exifBuffer[3] === 0x66) {
451+
// Buffer already starts with EXIF, just use it directly
452+
writeWord(exifBuffer.length + 2); // length is buffer + length itself!
453+
} else {
454+
// Buffer doesn't start with EXIF, write it for them
455+
writeWord(exifBuffer.length + 5 + 2); // length is buffer + EXIF\0 + length itself!
456+
writeByte(0x45); // E
457+
writeByte(0x78); // X
458+
writeByte(0x69); // I
459+
writeByte(0x66); // F
460+
writeByte(0); // = "EXIF",'\0'
461+
}
462+
463+
for (var i = 0; i < exifBuffer.length; i++) {
464+
writeByte(exifBuffer[i]);
465+
}
466+
}
467+
442468
function writeSOF0(width, height)
443469
{
444470
writeWord(0xFFC0); // marker
@@ -599,6 +625,7 @@ function JPEGEncoder(quality) {
599625
// Add JPEG headers
600626
writeWord(0xFFD8); // SOI
601627
writeAPP0();
628+
writeAPP1(image.exifBuffer);
602629
writeDQT();
603630
writeSOF0(image.width,image.height);
604631
writeDHT();

test/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,3 +243,13 @@ it('should be able to decode a JPEG into RGB', function(t) {
243243
t.assert(rawImageData.data instanceof Uint8Array, 'data is a typed array');
244244
t.end();
245245
});
246+
247+
it('should be able to encode/decode image with exif data', function(t) {
248+
var jpegData = fixture('grumpycat.jpg');
249+
var imageData = jpeg.decode(new Uint8Array(jpegData));
250+
t.assert(imageData.exifBuffer, 'decodes an exif buffer');
251+
var encodedData = jpeg.encode(imageData);
252+
var loopImageData = jpeg.decode(new Uint8Array(encodedData.data));
253+
t.bufferEqual(loopImageData.exifBuffer, imageData.exifBuffer);
254+
t.end();
255+
});

0 commit comments

Comments
 (0)