diff --git a/README.md b/README.md index 185e9b6..3544a40 100644 --- a/README.md +++ b/README.md @@ -173,6 +173,10 @@ The `options` argument can have following values: blur: optional. ex: 0.8 strip: optional. default: false. strips comments out from image. rotate: optional. degrees. + brightness: optional. -100-100 integer. + contrast: optional. -100-100 integer. + opacity: optional. 0-100 integer. + opacityColor: optional. String: x11color. flip: optional. vertical flip, true or false. autoOrient: optional. default: false. Auto rotate and flip using orientation info. colorspace: optional. String: Out file use that colorspace ['CMYK', 'sRGB', ...] diff --git a/src/imagemagick.cc b/src/imagemagick.cc index 1e7c718..a4b6019 100644 --- a/src/imagemagick.cc +++ b/src/imagemagick.cc @@ -81,6 +81,7 @@ struct convert_im_ctx : im_ctx_base { unsigned int height; unsigned int xoffset; unsigned int yoffset; + unsigned int opacity; bool strip; bool trim; bool autoOrient; @@ -91,6 +92,10 @@ struct convert_im_ctx : im_ctx_base { std::string filter; std::string blur; std::string background; + std::string brightness; + std::string contrast; + std::string fuzz; + std::string opacityColor; Magick::ColorspaceType colorspace; unsigned int quality; int rotate; @@ -248,6 +253,14 @@ void DoConvert(uv_work_t* req) { image.strip(); } + if (context->opacity) { + unsigned int opacity = context->opacity; + Magick::Color penColor = context->opacityColor; + + if (debug) printf( "opacity: %d\n", opacity ); + image.colorize(opacity, penColor); + } + if ( context->trim ) { if (debug) printf( "trim: true\n" ); double trimFuzz = context->trimFuzz; @@ -302,6 +315,30 @@ void DoConvert(uv_work_t* req) { return; } } + if( ! context->contrast.empty() && ! context->brightness.empty()) { + double contrast = atof (context->contrast.c_str()); + if (debug) printf( "contrast: %.1f\n", contrast ); + double brightness = atof (context->brightness.c_str()); + if (debug) printf( "brightness: %.1f\n", brightness ); + //image.blur(0, blur); + image.brightnessContrast(brightness, contrast); + } + else if( ! context->contrast.empty() && context->brightness.empty()) { + double contrast = atof (context->contrast.c_str()); + if (debug) printf( "contrast: %.1f\n", contrast ); + double brightness = 0.0; + if (debug) printf( "brightness: %.1f\n", brightness ); + //image.blur(0, blur); + image.brightnessContrast(brightness, contrast); + } + else if(context->contrast.empty() && ! context->brightness.empty()) { + double contrast = 0.0; + if (debug) printf( "contrast: %.1f\n", contrast ); + double brightness = atof (context->brightness.c_str()); + if (debug) printf( "brightness: %.1f\n", brightness ); + //image.blur(0, blur); + image.brightnessContrast(brightness, contrast); + } if( ! context->blur.empty() ) { double blur = atof (context->blur.c_str()); @@ -598,6 +635,7 @@ NAN_METHOD(Convert) { context->srcData = Buffer::Data(srcData); context->length = Buffer::Length(srcData); context->debug = obj->Get( Nan::New("debug").ToLocalChecked() )->Uint32Value(); + context->opacity = obj->Get( Nan::New("opacity").ToLocalChecked() )->Uint32Value(); context->ignoreWarnings = obj->Get( Nan::New("ignoreWarnings").ToLocalChecked() )->Uint32Value(); context->maxMemory = obj->Get( Nan::New("maxMemory").ToLocalChecked() )->Uint32Value(); context->width = obj->Get( Nan::New("width").ToLocalChecked() )->Uint32Value(); @@ -630,6 +668,30 @@ NAN_METHOD(Convert) { context->blur = strs.str(); } + // manage brightness as string for detect is empty + Local brightnessValue = obj->Get( Nan::New("brightness").ToLocalChecked() ); + context->brightness = ""; + if ( ! brightnessValue->IsUndefined() ) { + double brightnessD = brightnessValue->NumberValue(); + std::ostringstream strs; + strs << brightnessD; + context->brightness = strs.str(); + } + + // manage contrast as string for detect is empty + Local contrastValue = obj->Get( Nan::New("contrast").ToLocalChecked() ); + context->contrast = ""; + if ( ! contrastValue->IsUndefined() ) { + double contrastD = contrastValue->NumberValue(); + std::ostringstream strs; + strs << contrastD; + context->contrast = strs.str(); + } + + Local opacityColorValue = obj->Get( Nan::New("opacityColor").ToLocalChecked() ); + context->opacityColor = !opacityColorValue->IsUndefined() ? + *String::Utf8Value(opacityColorValue) : "transparent"; + Local resizeStyleValue = obj->Get( Nan::New("resizeStyle").ToLocalChecked() ); context->resizeStyle = !resizeStyleValue->IsUndefined() ? *String::Utf8Value(resizeStyleValue) : "aspectfill"; diff --git a/test/test.brightness.js b/test/test.brightness.js new file mode 100644 index 0000000..6d4916a --- /dev/null +++ b/test/test.brightness.js @@ -0,0 +1,46 @@ +var test = require('tap').test + , imagemagick = require('..') + , debug = true +; + +process.chdir(__dirname); + +console.log("image magick's version is: " + imagemagick.version()); +var versions = imagemagick.version().split("."); + +function saveToFileIfDebug (buffer, file) { + if (debug) { + require('fs').writeFileSync( file, buffer, 'binary' ); + console.log( "wrote file: "+file ); + } +} + +test( 'convert brightness to 100', function (t) { + var buffer = imagemagick.convert({ + srcData: require('fs').readFileSync( "test.png" ), // 58x66 + width: 100, + height: 100, + quality: 80, + format: 'PNG', + brightness: 100, + debug: debug + }); + t.equal( Buffer.isBuffer(buffer), true, 'buffer is Buffer' ); + saveToFileIfDebug( buffer, "out.png-brightness-high.png" ); + t.end(); +}); + +test( 'convert brightness to -100', function (t) { + var buffer = imagemagick.convert({ + srcData: require('fs').readFileSync( "test.png" ), // 58x66 + width: 100, + height: 100, + quality: 80, + format: 'PNG', + brightness: -100, + debug: debug + }); + t.equal( Buffer.isBuffer(buffer), true, 'buffer is Buffer' ); + saveToFileIfDebug( buffer, "out.png-brightness-low.png" ); + t.end(); +}); \ No newline at end of file diff --git a/test/test.contrast.js b/test/test.contrast.js new file mode 100644 index 0000000..4878fce --- /dev/null +++ b/test/test.contrast.js @@ -0,0 +1,46 @@ +var test = require('tap').test + , imagemagick = require('..') + , debug = true +; + +process.chdir(__dirname); + +console.log("image magick's version is: " + imagemagick.version()); +var versions = imagemagick.version().split("."); + +function saveToFileIfDebug (buffer, file) { + if (debug) { + require('fs').writeFileSync( file, buffer, 'binary' ); + console.log( "wrote file: "+file ); + } +} + +test( 'convert contrast to 100', function (t) { + var buffer = imagemagick.convert({ + srcData: require('fs').readFileSync( "test.png" ), // 58x66 + width: 100, + height: 100, + quality: 80, + format: 'PNG', + contrast: 100, + debug: debug + }); + t.equal( Buffer.isBuffer(buffer), true, 'buffer is Buffer' ); + saveToFileIfDebug( buffer, "out.png-contrast-high.png" ); + t.end(); +}); + +test( 'convert contrast to -100', function (t) { + var buffer = imagemagick.convert({ + srcData: require('fs').readFileSync( "test.png" ), // 58x66 + width: 100, + height: 100, + quality: 80, + format: 'PNG', + contrast: -100, + debug: debug + }); + t.equal( Buffer.isBuffer(buffer), true, 'buffer is Buffer' ); + saveToFileIfDebug( buffer, "out.png-contrast-low.png" ); + t.end(); +}); \ No newline at end of file diff --git a/test/test.opacity.js b/test/test.opacity.js new file mode 100644 index 0000000..c030196 --- /dev/null +++ b/test/test.opacity.js @@ -0,0 +1,49 @@ +var test = require('tap').test + , imagemagick = require('..') + , debug = true +; + +process.chdir(__dirname); + +console.log("image magick's version is: " + imagemagick.version()); +var versions = imagemagick.version().split("."); + +function saveToFileIfDebug (buffer, file) { + if (debug) { + require('fs').writeFileSync( file, buffer, 'binary' ); + console.log( "wrote file: "+file ); + } +} + +test( 'convert opacity transparent', function (t) { + var buffer = imagemagick.convert({ + srcData: require('fs').readFileSync( "test.opacity.png" ), // 58x66 + width: 100, + height: 100, + quality: 80, + format: 'PNG', + opacity: 40, + debug: debug + }); + t.equal( Buffer.isBuffer(buffer), true, 'buffer is Buffer' ); + saveToFileIfDebug( buffer, "out.png-opacity-transparent.png" ); + t.end(); +}); + +test( 'convert opacity color', function (t) { + var buffer = imagemagick.convert({ + srcData: require('fs').readFileSync( "test.opacity.png" ), // 58x66 + blur: 0, + rotate: 0, + brightness: 0, + contrast: 0, + opacity: 75, + opacityColor: '#FF0000', + debug: true, + ignoreWarnings: true + }); + t.equal( Buffer.isBuffer(buffer), true, 'buffer is Buffer' ); + saveToFileIfDebug( buffer, "out.png-opacity-color.png" ); + t.end(); +}); +