Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add brightness, contrast and opacity to convert method #182

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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', ...]
Expand Down
62 changes: 62 additions & 0 deletions src/imagemagick.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -598,6 +635,7 @@ NAN_METHOD(Convert) {
context->srcData = Buffer::Data(srcData);
context->length = Buffer::Length(srcData);
context->debug = obj->Get( Nan::New<String>("debug").ToLocalChecked() )->Uint32Value();
context->opacity = obj->Get( Nan::New<String>("opacity").ToLocalChecked() )->Uint32Value();
context->ignoreWarnings = obj->Get( Nan::New<String>("ignoreWarnings").ToLocalChecked() )->Uint32Value();
context->maxMemory = obj->Get( Nan::New<String>("maxMemory").ToLocalChecked() )->Uint32Value();
context->width = obj->Get( Nan::New<String>("width").ToLocalChecked() )->Uint32Value();
Expand Down Expand Up @@ -630,6 +668,30 @@ NAN_METHOD(Convert) {
context->blur = strs.str();
}

// manage brightness as string for detect is empty
Local<Value> brightnessValue = obj->Get( Nan::New<String>("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<Value> contrastValue = obj->Get( Nan::New<String>("contrast").ToLocalChecked() );
context->contrast = "";
if ( ! contrastValue->IsUndefined() ) {
double contrastD = contrastValue->NumberValue();
std::ostringstream strs;
strs << contrastD;
context->contrast = strs.str();
}

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

Local<Value> resizeStyleValue = obj->Get( Nan::New<String>("resizeStyle").ToLocalChecked() );
context->resizeStyle = !resizeStyleValue->IsUndefined() ?
*String::Utf8Value(resizeStyleValue) : "aspectfill";
Expand Down
46 changes: 46 additions & 0 deletions test/test.brightness.js
Original file line number Diff line number Diff line change
@@ -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();
});
46 changes: 46 additions & 0 deletions test/test.contrast.js
Original file line number Diff line number Diff line change
@@ -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();
});
49 changes: 49 additions & 0 deletions test/test.opacity.js
Original file line number Diff line number Diff line change
@@ -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();
});