Skip to content

Commit

Permalink
Merge 6f2715b into acc6059
Browse files Browse the repository at this point in the history
  • Loading branch information
gasi committed May 6, 2015
2 parents acc6059 + 6f2715b commit be5f437
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 10 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,10 @@ Do not enlarge the output image if the input image width *or* height are already

This is equivalent to GraphicsMagick's `>` geometry option: "change the dimensions of the image only if its width or height exceeds the geometry specification".

#### withoutGaussianBlur()

Do not apply a Gaussian blur for large image reductions. Applying a Gaussian blur on images with alpha channel can introduce dark ‘fringing’ around bright parts.

#### blur([sigma])

When used without parameters, performs a fast, mild blur of the output image. This typically reduces performance by 10%.
Expand Down
10 changes: 10 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ var Sharp = function(input) {
heightPost: -1,
width: -1,
height: -1,
withoutGaussianBlur: false,
canvas: 'crop',
gravity: 0,
angle: 0,
Expand Down Expand Up @@ -257,6 +258,15 @@ Sharp.prototype.withoutEnlargement = function(withoutEnlargement) {
return this;
};

/*
Do not apply a Gaussian blur. This is useful to avoid dark ‘fringing’ around bright pixels when doing
large reductions on images with transparency.
*/
Sharp.prototype.withoutGaussianBlur = function(withoutGaussianBlur) {
this.options.withoutGaussianBlur = (typeof withoutGaussianBlur === 'boolean') ? withoutGaussianBlur : true;
return this;
};

/*
Blur the output image.
Call without a sigma to use a fast, mild blur.
Expand Down
5 changes: 4 additions & 1 deletion src/resize.cc
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ struct ResizeBaton {
bool flop;
bool progressive;
bool withoutEnlargement;
bool withoutGaussianBlur;
VipsAccess accessMethod;
int quality;
int compressionLevel;
Expand Down Expand Up @@ -127,6 +128,7 @@ struct ResizeBaton {
flop(false),
progressive(false),
withoutEnlargement(false),
withoutGaussianBlur(false),
quality(80),
compressionLevel(6),
withoutAdaptiveFiltering(false),
Expand Down Expand Up @@ -485,7 +487,7 @@ class ResizeWorker : public NanAsyncWorker {
// Use average of x and y residuals to compute sigma for Gaussian blur
double residual = (xresidual + yresidual) / 2.0;
// Apply Gaussian blur before large affine reductions
if (residual < 1.0) {
if (residual < 1.0 && !baton->withoutGaussianBlur) {
// Calculate standard deviation
double sigma = ((1.0 / residual) - 0.4) / 3.0;
if (sigma >= 0.3) {
Expand Down Expand Up @@ -1229,6 +1231,7 @@ NAN_METHOD(resize) {
baton->overlayPath = *String::Utf8Value(options->Get(NanNew<String>("overlayPath"))->ToString());
// Resize options
baton->withoutEnlargement = options->Get(NanNew<String>("withoutEnlargement"))->BooleanValue();
baton->withoutGaussianBlur = options->Get(NanNew<String>("withoutGaussianBlur"))->BooleanValue();
baton->gravity = options->Get(NanNew<String>("gravity"))->Int32Value();
baton->interpolator = *String::Utf8Value(options->Get(NanNew<String>("interpolator"))->ToString());
// Operators
Expand Down
Binary file added test/fixtures/alpha-resizing-2048x1536-paper.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions test/fixtures/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ module.exports = {
inputPngWithTransparency: getPath('blackbug.png'), // public domain
inputPngWithGreyAlpha: getPath('grey-8bit-alpha.png'),
inputPngWithOneColor: getPath('2x2_fdcce6.png'),
inputPngWithFineBrightDetails: getPath('alpha-resizing-2048x1536-paper.png'),
inputPngOverlayLayer0: getPath('alpha-layer-0-background.png'),
inputPngOverlayLayer1: getPath('alpha-layer-1-fill.png'),
inputPngOverlayLayer2: getPath('alpha-layer-2-ink.png'),
Expand Down
38 changes: 29 additions & 9 deletions test/unit/resize.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ describe('Resize dimensions', function() {
done();
});
});

it('Downscale width and height, ignoring aspect ratio', function(done) {
sharp(fixtures.inputJpg).resize(320, 320).ignoreAspectRatio().toBuffer(function(err, data, info) {
if (err) throw err;
Expand All @@ -270,7 +270,27 @@ describe('Resize dimensions', function() {
done();
});
});


it('Large reduction of transparent image with Gaussian blur', function(done) {
sharp(fixtures.inputPngWithFineBrightDetails).resize(1024, 768).interpolateWith('bicubic').toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual('png', info.format);
assert.strictEqual(1024, info.width);
assert.strictEqual(768, info.height);
fixtures.assertSimilar(fixtures.expected('alpha-resizing-bicubic-gaussian-blur-1024x768.png'), data, {threshold: 0}, done);
});
});

it('Large reduction of transparent image without Gaussian blur', function(done) {
sharp(fixtures.inputPngWithFineBrightDetails).resize(1024, 768).interpolateWith('bicubic').withoutGaussianBlur().toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual('png', info.format);
assert.strictEqual(1024, info.width);
assert.strictEqual(768, info.height);
fixtures.assertSimilar(fixtures.expected('alpha-resizing-bicubic-no-gaussian-blur-1024x768.png'), data, {threshold: 0}, done);
});
});

it('Downscale width, ignoring aspect ratio', function(done) {
sharp(fixtures.inputJpg).resize(320).ignoreAspectRatio().toBuffer(function(err, data, info) {
if (err) throw err;
Expand All @@ -281,7 +301,7 @@ describe('Resize dimensions', function() {
done();
});
});

it('Downscale height, ignoring aspect ratio', function(done) {
sharp(fixtures.inputJpg).resize(null, 320).ignoreAspectRatio().toBuffer(function(err, data, info) {
if (err) throw err;
Expand All @@ -292,7 +312,7 @@ describe('Resize dimensions', function() {
done();
});
});

it('Upscale width and height, ignoring aspect ratio', function(done) {
sharp(fixtures.inputJpg).resize(3000, 3000).ignoreAspectRatio().toBuffer(function(err, data, info) {
if (err) throw err;
Expand All @@ -303,7 +323,7 @@ describe('Resize dimensions', function() {
done();
});
});

it('Upscale width, ignoring aspect ratio', function(done) {
sharp(fixtures.inputJpg).resize(3000).ignoreAspectRatio().toBuffer(function(err, data, info) {
if (err) throw err;
Expand All @@ -314,7 +334,7 @@ describe('Resize dimensions', function() {
done();
});
});

it('Upscale height, ignoring aspect ratio', function(done) {
sharp(fixtures.inputJpg).resize(null, 3000).ignoreAspectRatio().toBuffer(function(err, data, info) {
if (err) throw err;
Expand All @@ -325,7 +345,7 @@ describe('Resize dimensions', function() {
done();
});
});

it('Downscale width, upscale height, ignoring aspect ratio', function(done) {
sharp(fixtures.inputJpg).resize(320, 3000).ignoreAspectRatio().toBuffer(function(err, data, info) {
if (err) throw err;
Expand All @@ -336,7 +356,7 @@ describe('Resize dimensions', function() {
done();
});
});

it('Upscale width, downscale height, ignoring aspect ratio', function(done) {
sharp(fixtures.inputJpg).resize(3000, 320).ignoreAspectRatio().toBuffer(function(err, data, info) {
if (err) throw err;
Expand All @@ -347,7 +367,7 @@ describe('Resize dimensions', function() {
done();
});
});

it('Identity transform, ignoring aspect ratio', function(done) {
sharp(fixtures.inputJpg).ignoreAspectRatio().toBuffer(function(err, data, info) {
if (err) throw err;
Expand Down

0 comments on commit be5f437

Please sign in to comment.