Skip to content

Commit

Permalink
avoid turning grayscale inputs into sRGB by normalizing
Browse files Browse the repository at this point in the history
  • Loading branch information
bkw committed Apr 16, 2015
1 parent b688993 commit a0788a0
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/resize.cc
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,11 @@ class ResizeWorker : public NanAsyncWorker {

// Apply normalization
if (baton->normalize) {
VipsInterpretation typeBeforeNormalize = image->Type;
if (typeBeforeNormalize == VIPS_INTERPRETATION_RGB) {
typeBeforeNormalize = VIPS_INTERPRETATION_sRGB;
}

// normalize the luminance band in LAB space:
VipsImage *lab;
if (vips_colourspace(image, &lab, VIPS_INTERPRETATION_LAB, NULL)) {
Expand Down Expand Up @@ -739,6 +744,12 @@ class ResizeWorker : public NanAsyncWorker {
}
vips_object_local(hook, normalized);

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

if (HasAlpha(image)) {
VipsImage *alpha;
if (vips_extract_band(image, &alpha, image->Bands - 1, "n", 1, NULL)) {
Expand All @@ -747,18 +758,18 @@ class ResizeWorker : public NanAsyncWorker {
vips_object_local(hook, alpha);

VipsImage *normalizedAlpha;
if (vips_bandjoin2(normalized, alpha, &normalizedAlpha, NULL)) {
if (vips_bandjoin2(origFormat, alpha, &normalizedAlpha, NULL)) {
return Error();
}
vips_object_local(hook, normalizedAlpha);
image = normalizedAlpha;
} else {
image = normalized;
image = origFormat;
}
}

// Convert image to sRGB, if not already
if (image->Type != VIPS_INTERPRETATION_sRGB) {
if (image->Type != VIPS_INTERPRETATION_sRGB && image->Type != VIPS_INTERPRETATION_B_W) {
// Switch intrepretation to sRGB
VipsImage *rgb;
if (vips_colourspace(image, &rgb, VIPS_INTERPRETATION_sRGB, NULL)) {
Expand Down Expand Up @@ -825,10 +836,10 @@ class ResizeWorker : public NanAsyncWorker {
#if (VIPS_MAJOR_VERSION >= 8 || (VIPS_MAJOR_VERSION >= 7 && VIPS_MINOR_VERSION >= 42))
} else if (baton->output == "__raw") {
// Write raw, uncompressed image data to buffer
if (baton->greyscale) {
if (baton->greyscale || image->Type == VIPS_INTERPRETATION_B_W) {
// Extract first band for greyscale image
VipsImage *grey;
if (vips_extract_band(image, &grey, 1, NULL)) {
if (vips_extract_band(image, &grey, 0, NULL)) {
return Error();
}
vips_object_local(hook, grey);
Expand Down

0 comments on commit a0788a0

Please sign in to comment.