Skip to content

Commit

Permalink
Skip normalise operation for images with one colour
Browse files Browse the repository at this point in the history
It didn't play nicely with premultiplication
  • Loading branch information
lovell committed May 20, 2015
1 parent 449def9 commit 1b1215c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 63 deletions.
17 changes: 0 additions & 17 deletions src/composite.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,21 +128,13 @@ int Composite(VipsObject *context, VipsImage *srcPremultiplied, VipsImage *dstPr
int Premultiply(VipsObject *context, VipsImage *image, VipsImage **out) {
VipsImage *imagePremultiplied;

// Trivial case: Copy images without alpha channel:
if (!HasAlpha(image)) {
return vips_image_write(image, *out);
}

#if (VIPS_MAJOR_VERSION >= 9 || (VIPS_MAJOR_VERSION >= 8 && VIPS_MINOR_VERSION >= 1))

if (vips_premultiply(image, &imagePremultiplied, NULL))
return -1;

#else

if (image->Bands != 4)
return -1;

VipsImage *imageRGB;
if (vips_extract_band(image, &imageRGB, 0, "n", NUM_COLOR_BANDS, NULL))
return -1;
Expand Down Expand Up @@ -181,21 +173,13 @@ int Premultiply(VipsObject *context, VipsImage *image, VipsImage **out) {
int Unpremultiply(VipsObject *context, VipsImage *image, VipsImage **out) {
VipsImage *imageUnpremultiplied;

// Trivial case: Copy images without alpha channel:
if (!HasAlpha(image)) {
return vips_image_write(image, *out);
}

#if (VIPS_MAJOR_VERSION >= 9 || (VIPS_MAJOR_VERSION >= 8 && VIPS_MINOR_VERSION >= 1))

if (vips_unpremultiply(image, &imageUnpremultiplied, NULL))
return -1;

#else

if (image->Bands != 4)
return -1;

VipsImage *imageRGBPremultipliedTransformed;
if (vips_extract_band(image, &imageRGBPremultipliedTransformed, 0, "n", NUM_COLOR_BANDS, NULL))
return -1;
Expand All @@ -219,7 +203,6 @@ int Unpremultiply(VipsObject *context, VipsImage *image, VipsImage **out) {
if (vips_bandjoin2(imageRGBUnpremultipliedTransformed, imageAlphaTransformed, &imageUnpremultiplied, NULL))
return -1;


#endif

// Return a reference to the unpremultiplied output image:
Expand Down
41 changes: 18 additions & 23 deletions src/resize.cc
Original file line number Diff line number Diff line change
Expand Up @@ -763,17 +763,10 @@ class ResizeWorker : public NanAsyncWorker {
return Error();
}
vips_object_local(hook, stats);

double min = *VIPS_MATRIX(stats, 0, 0);
double max = *VIPS_MATRIX(stats, 1, 0);

VipsImage *normalized;
if (min == max) {
// Range of zero: create black image
if (vips_black(&normalized, image->Xsize, image->Ysize, "bands", 1, NULL )) {
return Error();
}
vips_object_local(hook, normalized);
} else {
if (min != max) {
double f = 100.0 / (max - min);
double a = -(min * f);

Expand All @@ -788,27 +781,29 @@ class ResizeWorker : public NanAsyncWorker {
return Error();
}
vips_object_local(hook, normalizedLab);

VipsImage *normalized;
if (vips_colourspace(normalizedLab, &normalized, typeBeforeNormalize, NULL)) {
return Error();
}
vips_object_local(hook, normalized);
}

if (HasAlpha(image)) {
VipsImage *alpha;
if (vips_extract_band(image, &alpha, image->Bands - 1, "n", 1, NULL)) {
return Error();
}
vips_object_local(hook, alpha);
if (HasAlpha(image)) {
VipsImage *alpha;
if (vips_extract_band(image, &alpha, image->Bands - 1, "n", 1, NULL)) {
return Error();
}
vips_object_local(hook, alpha);

VipsImage *normalizedAlpha;
if (vips_bandjoin2(normalized, alpha, &normalizedAlpha, NULL)) {
return Error();
VipsImage *normalizedAlpha;
if (vips_bandjoin2(normalized, alpha, &normalizedAlpha, NULL)) {
return Error();
}
vips_object_local(hook, normalizedAlpha);
image = normalizedAlpha;
} else {
image = normalized;
}
vips_object_local(hook, normalizedAlpha);
image = normalizedAlpha;
} else {
image = normalized;
}
}
#endif
Expand Down
30 changes: 7 additions & 23 deletions test/unit/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,31 +98,15 @@ describe('Normalization', function () {
});
});

it('returns a black image for images with only one color', function (done) {
it('does not alter images with only one color', function (done) {
var output = fixtures.path('output.unmodified-png-with-one-color.png');
sharp(fixtures.inputPngWithOneColor)
.normalize()
.toBuffer()
.bind({})
.then(function (imageData) {
this.imageData = imageData;
return sharp(imageData)
.metadata();
})
.then(function (metadata) {
assert.strictEqual(false, metadata.hasAlpha);
assert.strictEqual(3, metadata.channels);
assert.strictEqual('srgb', metadata.space);
})
.then(function () {
return sharp(this.imageData)
.raw()
.toBuffer();
})
.then(function (rawData) {
var blackBuffer = new Buffer([0,0,0, 0,0,0, 0,0,0, 0,0,0]);
assert.strictEqual(blackBuffer.toString(), rawData.toString());
})
.finally(done);
.toFile(output, function(err, info) {
if (err) done(err);
fixtures.assertMaxColourDistance(output, fixtures.inputPngWithOneColor, 0);
done();
});
});

}
Expand Down

0 comments on commit 1b1215c

Please sign in to comment.