Skip to content

Commit

Permalink
ExifImage: I hate semicolons
Browse files Browse the repository at this point in the history
  • Loading branch information
lacombar committed Feb 23, 2015
1 parent af0f748 commit 2c143c6
Showing 1 changed file with 53 additions and 53 deletions.
106 changes: 53 additions & 53 deletions lib/exif/ExifImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var fs = require('fs'),
Events = require("events"),
EventEmitter = new Events.EventEmitter(),
BufferExtender = require('./Buffer'),
NumberExtender = require('./Number');
NumberExtender = require('./Number')

/**
* Represents an image with Exif information. When instantiating it you have to
Expand All @@ -27,11 +27,11 @@ function ExifImage (options, callback)
gps : {}, // GPS information
interoperability: {}, // Exif Interoperability information
makernote : {} // Makernote information
};
}

this.makernoteIfdData = null;
this.makernoteIfdData = null

this.ee = new Events.EventEmitter();
this.ee = new Events.EventEmitter()

this.JPEGSegments = []

Expand All @@ -40,31 +40,31 @@ function ExifImage (options, callback)
this.ee.on('fill-buffer', this.fillBuffer.bind(this))

if (!options)
var options = {};
var options = {}

if (!options.input)
throw new Error('You have to provide ian image, it is pretty hard to extract Exif data from nothing...');
throw new Error('You have to provide ian image, it is pretty hard to extract Exif data from nothing...')

if (typeof callback !== 'function')
throw new Error('You have to provide a callback function.');
throw new Error('You have to provide a callback function.')
this.callback = callback

this.load(options.input);
this.load(options.input)
}

module.exports = ExifImage;
module.exports = ExifImage

ExifImage.prototype.load = function (input)
{

if (input.constructor.name === 'Buffer') {
this.processBuffer(input);
this.processBuffer(input)
} else if (input.constructor.name === 'String') {
fs.open(input, 'r', this.processOpenedFile.bind(this))
} else {
this.callback(new Error('Given image is neither a buffer nor a file, please provide one of these.'));
this.callback(new Error('Given image is neither a buffer nor a file, please provide one of these.'))
}
};
}

ExifImage.prototype.processOpenedFile = function (err, fd) {

Expand Down Expand Up @@ -136,22 +136,22 @@ ExifImage.prototype.validateBuffer = function (offset, len)

ExifImage.prototype.processBuffer = function (data)
{
var mimeType, offset;
var mimeType, offset

offset = 0

var soiHdr = data.readUInt16BE(offset)
if (soiHdr == 0xFFD8) {
mimeType = 'image/jpeg';
mimeType = 'image/jpeg'
}

switch (mimeType) {
case "image/jpeg":
this.processJPEGImage(data, offset)
break;
break
default:
throw new Error('Unknown image format.');
break;
throw new Error('Unknown image format.')
break
}
}

Expand Down Expand Up @@ -198,12 +198,12 @@ ExifImage.prototype.processJPEGImage = function (data, offset)
throw new Error("Unknown segment header: " + segmentHdr.toHex())
}

segmentLength = segment.len;
segmentLength = segment.len
if (segmentLength == -1) {
if (!this.validateBuffer(offset, 2))
return

segmentLength = data.readUInt16BE(offset);
segmentLength = data.readUInt16BE(offset)
}

name = segment.name
Expand Down Expand Up @@ -237,23 +237,23 @@ ExifImage.prototype.processJPEG_APP1 = function (data)
var offset = 4

if (!data.startWithString('Exif\0\0', offset))
return;
return

offset += 6

try {
this.extractExifData(data.slice(offset, data.length));
this.extractExifData(data.slice(offset, data.length))
} catch (error) {
this.callback(error)
return
}

this.callback(false, this.exifData);
};
this.callback(false, this.exifData)
}

ExifImage.prototype.extractExifData = function (data)
{
var ifdOffset, numberOfEntries;
var ifdOffset, numberOfEntries

/*
* The TIFF header starts either with 0x4949 ("II") if data are stored
Expand All @@ -265,21 +265,21 @@ ExifImage.prototype.extractExifData = function (data)
} else if (identification == 0x4D4D) {
data.setEndian(data.Endian.BIG)
} else {
throw new Error('Invalid TIFF data! Expected 0x4949 or 0x4D4D but found 0x' + identification.toHex() + ".");
throw new Error('Invalid TIFF data! Expected 0x4949 or 0x4D4D but found 0x' + identification.toHex() + ".")
}

var versionOffset = 2
var version = data.readUInt16(versionOffset)
if (version != 42) {
var expected = (data.isBigEndian()) ? '0x002A' : '0x2A00';
throw new Error('Invalid TIFF version! Expected 42 but found 0x' + version.toHex() + ".");
var expected = (data.isBigEndian()) ? '0x002A' : '0x2A00'
throw new Error('Invalid TIFF version! Expected 42 but found 0x' + version.toHex() + ".")
}

/*
* IFD0
*/
ifdOffset = data.readUInt32(4)
numberOfEntries = this.extractExifIFDEntries(data, ifdOffset, this.exifData.image, ExifImage.TAGS.exif);
numberOfEntries = this.extractExifIFDEntries(data, ifdOffset, this.exifData.image, ExifImage.TAGS.exif)

/*
* IFD1 -
Expand All @@ -289,7 +289,7 @@ ExifImage.prototype.extractExifData = function (data)
/********************************* IFD1 **********************************/
var nextIfdOffset = data.readUInt32(ifdOffset + 2 + (numberOfEntries * 12))
if (nextIfdOffset != 0x00000000)
this.extractExifIFDEntries(data, nextIfdOffset, this.exifData.thumbnail, ExifImage.TAGS.exif);
this.extractExifIFDEntries(data, nextIfdOffset, this.exifData.thumbnail, ExifImage.TAGS.exif)

/*
* EXIF IFD -
Expand All @@ -298,7 +298,7 @@ ExifImage.prototype.extractExifData = function (data)
*/
var subIfdOffset = this.exifData.image[ExifImage.TAGS.exif[0x8769]]
if (typeof subIfdOffset != "undefined")
this.extractExifIFDEntries(data, subIfdOffset, this.exifData.exif, ExifImage.TAGS.exif);
this.extractExifIFDEntries(data, subIfdOffset, this.exifData.exif, ExifImage.TAGS.exif)

/*
* GPS IFD -
Expand All @@ -307,7 +307,7 @@ ExifImage.prototype.extractExifData = function (data)
*/
var subIfdOffset = this.exifData.image[ExifImage.TAGS.exif[0x8825]]
if (typeof subIfdOffset != "undefined")
this.extractExifIFDEntries(data, subIfdOffset, this.exifData.gps, ExifImage.TAGS.gps);
this.extractExifIFDEntries(data, subIfdOffset, this.exifData.gps, ExifImage.TAGS.gps)

/*
* Interoperability IFD -
Expand All @@ -316,7 +316,7 @@ ExifImage.prototype.extractExifData = function (data)
*/
var subIfdOffset = this.exifData.exif[ExifImage.TAGS.exif[0xA005]]
if (typeof subIfdOffset != "undefined")
this.extractExifIFDEntries(data, subIfdOffset, this.exifData.interoperability, ExifImage.TAGS.exif);
this.extractExifIFDEntries(data, subIfdOffset, this.exifData.interoperability, ExifImage.TAGS.exif)

/*
* Makernote IFD -
Expand All @@ -326,31 +326,31 @@ ExifImage.prototype.extractExifData = function (data)
*/
if (this.makernoteIfdData) {
for (var i = 0; i < ExifImage.MAKERNOTES.length; i++) {
var makernote = ExifImage.MAKERNOTES[i];
var makernote = ExifImage.MAKERNOTES[i]
if (this.makernoteIfdData.startWithString(makernote.prefix, 0)) {
this.extractMakernotes = require(makernote.library).extractMakernotes;
break;
this.extractMakernotes = require(makernote.library).extractMakernotes
break
}
}

if (typeof this.extractMakernotes != "undefined") {
this.exifData.makernote = this.extractMakernotes(this.makernoteIfdData);
this.exifData.makernote = this.extractMakernotes(this.makernoteIfdData)
} else {
this.exifData.makernote['error'] =
'Unable to extract Makernote information as it is in an unsupported or unrecognized format.';
'Unable to extract Makernote information as it is in an unsupported or unrecognized format.'
}
}
};
}

ExifImage.prototype.extractExifIFDEntries = function (data, offset, store, tags)
{
var entryCount = data.readUInt16(offset);
var entryCount = data.readUInt16(offset)

for (var i = 0; i < entryCount; i++) {
var start = offset + 2 + (i * 12)
var entry = this.extractExifEntry(data, start, tags);
var entry = this.extractExifEntry(data, start, tags)
if (entry && entry.tag.name !== null)
store[entry.tag.name] = entry.value;
store[entry.tag.name] = entry.value
}

return entryCount
Expand All @@ -375,24 +375,24 @@ ExifImage.prototype.extractExifEntry = function (data, entryOffset, tags)
if (typeof tags[field.tag.ID] == "function") {
field.tag.name = tags[field.tag.id](entry)
if (!field.tag.name) {
return false;
return false
}
} else {
field.tag.name = tags[field.tag.ID];
field.tag.name = tags[field.tag.ID]
}
} else {
return false;
return false
}

type = ExifImage.TIFFTYPES[field.type]
type.size = type.len * type.count

offset = entryOffset + 8
if (field.count > (4 / type.size))
offset = data.readUInt32(entryOffset + 8);
offset = data.readUInt32(entryOffset + 8)

if (offset >= data.length)
return false;
return false

if (field.tag.name === "MakerNote") {
this.makernoteIfdData = data.slice(offset, data.length)
Expand All @@ -404,9 +404,9 @@ ExifImage.prototype.extractExifEntry = function (data, entryOffset, tags)
for (i = 0; i < field.count; i++) {
for (j = 0; j < type.count; j++) {
var val = (type.signed) ? data.readInt(offset, type.len) :
data.readUInt(offset, type.len);
field.value.push(val);
offset += type.len;
data.readUInt(offset, type.len)
field.value.push(val)
offset += type.len
}
}

Expand All @@ -433,10 +433,10 @@ ExifImage.prototype.extractExifEntry = function (data, entryOffset, tags)

// If the value array has only one element we don't need an array
if (field.value.length == 1)
field.value = field.value[0];
field.value = field.value[0]

return field;
};
return field
}

ExifImage.TIFFTYPES =
{
Expand Down Expand Up @@ -936,4 +936,4 @@ ExifImage.TAGS =
0x001E : 'GPSDifferential',
0x001F : 'GPSHPositioningError'
}
};
}

0 comments on commit 2c143c6

Please sign in to comment.