Skip to content

Commit

Permalink
Merge pull request #10 from thiagoarrais/detailed-identify
Browse files Browse the repository at this point in the history
Detailed identify
  • Loading branch information
rsms committed May 17, 2011
2 parents 72553e2 + cccaf3d commit ed0d06f
Showing 1 changed file with 45 additions and 12 deletions.
57 changes: 45 additions & 12 deletions imagemagick.js
Expand Up @@ -69,15 +69,48 @@ function exec2(file, args /*, options, callback */) {
if (callback) callback(e, stdout, stderr);
}
});

return child;
};


function parseIdentify(input) {
var lines = input.split("\n"),
prop = {},
props = [prop],
prevIndent = 0,
indents = [indent],
currentLine, comps, indent;

lines.shift(); //drop first line (Image: name.jpg)

for (i in lines) {
currentLine = lines[i];
if (currentLine.length > 0) {
indent = currentLine.search(/\S/);
comps = currentLine.split(': ');
if (indent > prevIndent) indents.push(indent);
while (indent < prevIndent) {
indents.pop();
prop = props.pop();
prevIndent = indents[indents.length - 1];
}
if (comps.length < 2) {
props.push(prop);
prop = prop[currentLine.split(':')[0].trim().toLowerCase()] = {};
} else {
prop[comps[0].trim().toLowerCase()] = comps[1].trim()
}
prevIndent = indent;
}
}
return props[0];
};

exports.identify = function(pathOrArgs, callback) {
var isCustom = Array.isArray(pathOrArgs),
isData,
args = isCustom ? ([]).concat(pathOrArgs) : [pathOrArgs];
args = isCustom ? ([]).concat(pathOrArgs) : ['-verbose', pathOrArgs];

if (typeof args[args.length-1] === 'object') {
isData = true;
Expand All @@ -87,19 +120,19 @@ exports.identify = function(pathOrArgs, callback) {
throw new Error('first argument is missing the "data" member');
}
var proc = exec2(exports.identify.path, args, {timeout:120000}, function(err, stdout, stderr) {
var result;
var result, geometry;
if (!err) {
if (isCustom) {
result = stdout;
} else {
var v = stdout.split(/ +/),
x = v[2].split(/x/);
result = {
format: v[1],
width: parseInt(x[0]),
height: parseInt(x[1]),
depth: parseInt(v[4]),
};
result = parseIdentify(stdout);
geometry = result['geometry'].split(/x/);

result.format = result.format.match(/\S*/)[0]
result.width = parseInt(geometry[0]);
result.height = parseInt(geometry[1]);
result.depth = parseInt(result.depth);
if (result.quality !== undefined) result.quality = parseInt(result.quality) / 100;
}
}
callback(err, result);
Expand Down Expand Up @@ -143,7 +176,7 @@ var exifFieldConverters = {
saturation:Number, sharpness:Number, subjectDistanceRange:Number,
subSecTime:Number, subSecTimeDigitized:Number, subSecTimeOriginal:Number,
whiteBalance:Number, sceneCaptureType:Number,

// Dates
dateTime:ExifDate, dateTimeDigitized:ExifDate, dateTimeOriginal:ExifDate
};
Expand Down

0 comments on commit ed0d06f

Please sign in to comment.