Skip to content

Commit

Permalink
Merge pull request #175 from cartuchogl/exthint
Browse files Browse the repository at this point in the history
Allow srcFormat in identify
  • Loading branch information
elad committed Oct 12, 2017
2 parents 12abd25 + 15157e6 commit fe7234d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/imagemagick.cc
Expand Up @@ -61,6 +61,7 @@ struct im_ctx_base {
size_t length;
int debug;
int ignoreWarnings;
std::string srcFormat;

// generated blob by convert or composite
Magick::Blob dstBlob;
Expand Down Expand Up @@ -97,8 +98,6 @@ struct convert_im_ctx : im_ctx_base {
int density;
int flip;

std::string srcFormat;

convert_im_ctx() {}
};
// Extra context for composite
Expand Down Expand Up @@ -685,6 +684,12 @@ void DoIdentify(uv_work_t* req) {
Magick::Blob srcBlob( context->srcData, context->length );

Magick::Image image;

if( ! context->srcFormat.empty() ){
if (context->debug) printf( "reading with format: %s\n", context->srcFormat.c_str() );
image.magick( context->srcFormat.c_str() );
}

try {
image.read( srcBlob );
}
Expand Down Expand Up @@ -800,6 +805,10 @@ NAN_METHOD(Identify) {
context->debug = obj->Get( Nan::New<String>("debug").ToLocalChecked() )->Uint32Value();
context->ignoreWarnings = obj->Get( Nan::New<String>("ignoreWarnings").ToLocalChecked() )->Uint32Value();

Local<Value> srcFormatValue = obj->Get( Nan::New<String>("srcFormat").ToLocalChecked() );
context->srcFormat = !srcFormatValue->IsUndefined() ?
*String::Utf8Value(srcFormatValue) : "";

if (context->debug) printf( "debug: on\n" );
if (context->debug) printf( "ignoreWarnings: %d\n", context->ignoreWarnings );

Expand Down
Binary file added test/test.ext.tga
Binary file not shown.
29 changes: 29 additions & 0 deletions test/test.exthint.js
@@ -0,0 +1,29 @@
var test = require('tap').test;
var imagemagick = require('..');

process.chdir(__dirname);

test( 'identify results', function (t) {
t.plan(6);
var info;

try {
info = imagemagick.identify({
srcData: require('fs').readFileSync('test.ext.tga'),
});
} catch (ex) {
t.like(ex.message, /no decode delegate for this image format/, 'err message');
}

info = imagemagick.identify({
srcData: require('fs').readFileSync( 'test.ext.tga' ),
srcFormat: 'tga',
});

t.equal( info.width, 31, 'width is 31' );
t.equal( info.height, 16, 'height is 16' );
t.equal( info.depth, 8, 'depth is 8' );
t.equal( info.format, 'TGA', 'format is TGA' );
t.equal( info.exif.orientation, 0, 'orientation doesnt exist' );
t.end();
});

0 comments on commit fe7234d

Please sign in to comment.