image-headers
Advanced tools
Comparing version
@@ -32,5 +32,11 @@ // Generated by CoffeeScript 1.4.0 | ||
this.stream_index = 0; | ||
this.marker = 0; | ||
this.marker_offset = 0; | ||
this.marker_size = 0; | ||
this.jpeg = { | ||
marker: 0, | ||
marker_offset: 0, | ||
marker_size: 0 | ||
}; | ||
this.png = { | ||
start: 0, | ||
length: 0 | ||
}; | ||
this.height = null; | ||
@@ -74,23 +80,27 @@ this.width = null; | ||
local_this = this; | ||
if ((this.exif_buffer != null) && this.exif_buffer.toString("utf-8", 0, 4) === "Exif") { | ||
return new exif.ExifImage({ | ||
exif_buffer: this.exif_buffer | ||
}, function(err, exif_data) { | ||
var orientation_tag, temp_w; | ||
if ((exif_data != null) && (exif_data.image != null)) { | ||
orientation_tag = exif_data.image.filter(function(tag) { | ||
return tag.tagName === "Orientation"; | ||
}); | ||
if (orientation_tag.length === 1) { | ||
local_this.orientation = orientation_tag[0].value; | ||
if (local_this.orientation === 6 || local_this.orientation === 8) { | ||
temp_w = local_this.width; | ||
local_this.width = local_this.height; | ||
local_this.height = temp_w; | ||
if (this.mode === ImageHeaders.modes.jpeg) { | ||
if ((this.exif_buffer != null) && this.exif_buffer.toString("utf-8", 0, 4) === "Exif") { | ||
return new exif.ExifImage({ | ||
exif_buffer: this.exif_buffer | ||
}, function(err, exif_data) { | ||
var orientation_tag, temp_w; | ||
if ((exif_data != null) && (exif_data.image != null)) { | ||
orientation_tag = exif_data.image.filter(function(tag) { | ||
return tag.tagName === "Orientation"; | ||
}); | ||
if (orientation_tag.length === 1) { | ||
local_this.orientation = orientation_tag[0].value; | ||
if (local_this.orientation === 6 || local_this.orientation === 8) { | ||
temp_w = local_this.width; | ||
local_this.width = local_this.height; | ||
local_this.height = temp_w; | ||
} | ||
} | ||
} | ||
} | ||
local_this.exif_data = exif_data; | ||
return callback(err, local_this); | ||
}); | ||
local_this.exif_data = exif_data; | ||
return callback(err, local_this); | ||
}); | ||
} | ||
} else { | ||
return callback(null, local_this); | ||
} | ||
@@ -116,6 +126,6 @@ }; | ||
var length, position; | ||
if (this.marker === 0 && b === 255) { | ||
this.marker = b; | ||
if (this.jpeg.marker === 0 && b === 255) { | ||
this.jpeg.marker = b; | ||
return; | ||
} else if (this.marker === 255) { | ||
} else if (this.jpeg.marker === 255) { | ||
switch (b) { | ||
@@ -135,28 +145,29 @@ case 0x00: | ||
case 0xFF: | ||
this.clear_marker(); | ||
this.clear_jpeg_marker(); | ||
return; | ||
default: | ||
this.marker = b; | ||
this.marker_offset = i; | ||
this.marker_size = 0; | ||
this.jpeg.marker = b; | ||
this.jpeg.marker_offset = i; | ||
this.jpeg.marker_size = 0; | ||
} | ||
} | ||
if (this.marker === 0xDA && this.marker_offset - i === 0) { | ||
if (this.jpeg.marker === 0xDA && this.jpeg.marker_offset - i === 0) { | ||
this.finished = true; | ||
return; | ||
} | ||
position = i - this.marker_offset; | ||
position = i - this.jpeg.marker_offset; | ||
if (position === 2) { | ||
length = this.buffer.readUInt16BE(this.marker_offset + 1); | ||
this.marker_size = length; | ||
switch (this.marker) { | ||
length = this.buffer.readUInt16BE(this.jpeg.marker_offset + 1); | ||
this.jpeg.marker_size = length; | ||
switch (this.jpeg.marker) { | ||
case 0xE1: | ||
if (!(this.exif_buffer != null)) { | ||
this.exif_bytes = this.marker_size - 2; | ||
this.exif_bytes = this.jpeg.marker_size - 2; | ||
this.exif_buffer = new Buffer(this.exif_bytes); | ||
return this.clear_marker(); | ||
return this.clear_jpeg_marker(); | ||
} | ||
} | ||
} else { | ||
if (this.marker_size > 0 && position === this.marker_size) { | ||
switch (this.marker) { | ||
if (this.jpeg.marker_size > 0 && position === this.jpeg.marker_size) { | ||
switch (this.jpeg.marker) { | ||
case 0xC0: | ||
@@ -177,3 +188,3 @@ case 0xC1: | ||
} | ||
return this.clear_marker(); | ||
return this.clear_jpeg_marker(); | ||
} | ||
@@ -183,4 +194,42 @@ } | ||
ImageHeaders.prototype.check_gif_state = function(b, i) { | ||
if (i === 10) { | ||
this.width = this.buffer.readUInt16LE(6); | ||
this.height = this.buffer.readUInt16LE(8); | ||
return this.finished = true; | ||
} | ||
}; | ||
ImageHeaders.prototype.check_png_state = function(b, i) { | ||
var length, offset; | ||
if (i < 8) { | ||
return; | ||
} | ||
if (this.png.start === 0) { | ||
this.png.start = i; | ||
return 0; | ||
} | ||
offset = i - this.png.start; | ||
switch (offset) { | ||
case 3: | ||
length = this.buffer.readUInt32BE(this.png.start); | ||
return this.png.length = length; | ||
case 7: | ||
return this.png.marker = this.buffer.toString("utf8", this.png.start + 4, this.png.start + 8); | ||
case this.png.length + 8: | ||
if (this.png.marker === "IHDR") { | ||
this.width = this.buffer.readUInt32BE(this.png.start + 8); | ||
this.height = this.buffer.readUInt32BE(this.png.start + 12); | ||
return this.finished = true; | ||
} | ||
break; | ||
case this.png.length + 12: | ||
return this.clear_png_marker(); | ||
} | ||
}; | ||
ImageHeaders.prototype.check_tiff_state = function(b, i) {}; | ||
ImageHeaders.prototype.identify_format = function(b) { | ||
var i, _i, _ref, _results; | ||
var buf_as_string, i, _i, _ref, _results; | ||
if (this.stream_index === 1) { | ||
@@ -190,2 +239,13 @@ if (this.buffer[0] === 0xFF && this.buffer[1] === 0xD8) { | ||
} | ||
} else if (this.stream_index === 2) { | ||
buf_as_string = this.buffer.toString("utf8", 0, 3); | ||
if (buf_as_string === "GIF") { | ||
this.mode = ImageHeaders.modes.gif; | ||
} else if (buf_as_string === "II*" || buf_as_string === "MM*") { | ||
this.mode = ImageHeaders.modes.tiff; | ||
} | ||
} else if (this.stream_index === 7) { | ||
if (this.buffer.toString("hex", 0, 8) === "89504e470d0a1a0a") { | ||
this.mode = ImageHeaders.modes.png; | ||
} | ||
} | ||
@@ -205,13 +265,19 @@ if (this.stream_index > 10) { | ||
ImageHeaders.prototype.clear_marker = function() { | ||
this.marker = 0; | ||
this.marker_offset = 0; | ||
return this.marker_size = 0; | ||
ImageHeaders.prototype.clear_jpeg_marker = function() { | ||
this.jpeg.marker = 0; | ||
this.jpeg.marker_offset = 0; | ||
return this.jpeg.marker_size = 0; | ||
}; | ||
ImageHeaders.prototype.parse_jpeg_sofn = function() { | ||
this.height = this.buffer.readUInt16BE(this.marker_offset + 4); | ||
return this.width = this.buffer.readUInt16BE(this.marker_offset + 6); | ||
this.height = this.buffer.readUInt16BE(this.jpeg.marker_offset + 4); | ||
return this.width = this.buffer.readUInt16BE(this.jpeg.marker_offset + 6); | ||
}; | ||
ImageHeaders.prototype.clear_png_marker = function() { | ||
this.png.start = 0; | ||
this.png.length = 0; | ||
return this.png.marker = null; | ||
}; | ||
return ImageHeaders; | ||
@@ -218,0 +284,0 @@ |
{ | ||
"name": "image-headers", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Parse image headers from a stream without loading the entire image.", | ||
"keywords": ["exif", "jpeg", "jpg", "gif"], | ||
"keywords": ["exif", "jpeg", "jpg", "gif", "png", "streaming"], | ||
"repository": { | ||
@@ -7,0 +7,0 @@ "type": "git", |
39270
6.18%260
31.31%