Skip to content

Commit

Permalink
Simplify StaySequential operation (#4074)
Browse files Browse the repository at this point in the history
  • Loading branch information
kleisauke authored Apr 19, 2024
1 parent 36e60bf commit aa8bc19
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 21 deletions.
9 changes: 4 additions & 5 deletions src/common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1081,11 +1081,10 @@ namespace sharp {
/*
Ensure decoding remains sequential.
*/
VImage StaySequential(VImage image, VipsAccess access, bool condition) {
static const char* meta = "sharp-copy-memory";
if (access == VIPS_ACCESS_SEQUENTIAL && condition && image.get_typeof(meta) != G_TYPE_INT) {
image = image.copy_memory();
image.set(meta, 1);
VImage StaySequential(VImage image, bool condition) {
if (vips_image_is_sequential(image.get_image()) && condition) {
image = image.copy_memory().copy();
image.remove(VIPS_META_SEQUENTIAL);
}
return image;
}
Expand Down
2 changes: 1 addition & 1 deletion src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ namespace sharp {
/*
Ensure decoding remains sequential.
*/
VImage StaySequential(VImage image, VipsAccess access, bool condition = TRUE);
VImage StaySequential(VImage image, bool condition = TRUE);

} // namespace sharp

Expand Down
4 changes: 2 additions & 2 deletions src/operations.cc
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ namespace sharp {
return image.conv(blur);
} else {
// Slower, accurate Gaussian blur
return StaySequential(image, VIPS_ACCESS_SEQUENTIAL).gaussblur(sigma);
return StaySequential(image).gaussblur(sigma);
}
}

Expand Down Expand Up @@ -386,7 +386,7 @@ namespace sharp {
pages.reserve(nPages);

// Split the image into cropped frames
image = StaySequential(image, VIPS_ACCESS_SEQUENTIAL);
image = StaySequential(image);
for (int i = 0; i < nPages; i++) {
pages.push_back(
image.extract_area(left, *pageHeight * i + top, width, height));
Expand Down
26 changes: 13 additions & 13 deletions src/pipeline.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class PipelineWorker : public Napi::AsyncWorker {
baton->rotationAngle != 0.0);

if (shouldRotateBefore) {
image = sharp::StaySequential(image, access,
image = sharp::StaySequential(image,
rotation != VIPS_ANGLE_D0 ||
autoRotation != VIPS_ANGLE_D0 ||
autoFlip ||
Expand Down Expand Up @@ -134,7 +134,7 @@ class PipelineWorker : public Napi::AsyncWorker {
// Trim
if (baton->trimThreshold >= 0.0) {
MultiPageUnsupported(nPages, "Trim");
image = sharp::StaySequential(image, access);
image = sharp::StaySequential(image);
image = sharp::Trim(image, baton->trimBackground, baton->trimThreshold, baton->trimLineArt);
baton->trimOffsetLeft = image.xoffset();
baton->trimOffsetTop = image.yoffset();
Expand Down Expand Up @@ -397,7 +397,7 @@ class PipelineWorker : public Napi::AsyncWorker {
->set("kernel", baton->kernel));
}

image = sharp::StaySequential(image, access,
image = sharp::StaySequential(image,
autoRotation != VIPS_ANGLE_D0 ||
baton->flip ||
autoFlip ||
Expand Down Expand Up @@ -500,7 +500,7 @@ class PipelineWorker : public Napi::AsyncWorker {

// Attention-based or Entropy-based crop
MultiPageUnsupported(nPages, "Resize strategy");
image = sharp::StaySequential(image, access);
image = sharp::StaySequential(image);
image = image.smartcrop(baton->width, baton->height, VImage::option()
->set("interesting", baton->position == 16 ? VIPS_INTERESTING_ENTROPY : VIPS_INTERESTING_ATTENTION)
->set("premultiplied", shouldPremultiplyAlpha)
Expand All @@ -519,7 +519,7 @@ class PipelineWorker : public Napi::AsyncWorker {
// Rotate post-extract non-90 angle
if (!baton->rotateBeforePreExtract && baton->rotationAngle != 0.0) {
MultiPageUnsupported(nPages, "Rotate");
image = sharp::StaySequential(image, access);
image = sharp::StaySequential(image);
std::vector<double> background;
std::tie(image, background) = sharp::ApplyAlpha(image, baton->rotationBackground, shouldPremultiplyAlpha);
image = image.rotate(baton->rotationAngle, VImage::option()->set("background", background));
Expand All @@ -543,7 +543,7 @@ class PipelineWorker : public Napi::AsyncWorker {
// Affine transform
if (!baton->affineMatrix.empty()) {
MultiPageUnsupported(nPages, "Affine");
image = sharp::StaySequential(image, access);
image = sharp::StaySequential(image);
std::vector<double> background;
std::tie(image, background) = sharp::ApplyAlpha(image, baton->affineBackground, shouldPremultiplyAlpha);
vips::VInterpolate interp = vips::VInterpolate::new_from_name(
Expand All @@ -566,7 +566,7 @@ class PipelineWorker : public Napi::AsyncWorker {
std::vector<double> background;
std::tie(image, background) = sharp::ApplyAlpha(image, baton->extendBackground, shouldPremultiplyAlpha);

image = sharp::StaySequential(image, baton->input->access, nPages > 1);
image = sharp::StaySequential(image, nPages > 1);
image = nPages > 1
? sharp::EmbedMultiPage(image,
baton->extendLeft, baton->extendTop, baton->width, baton->height,
Expand All @@ -575,7 +575,7 @@ class PipelineWorker : public Napi::AsyncWorker {
VImage::option()->set("extend", baton->extendWith)->set("background", background));
} else {
std::vector<double> ignoredBackground(1);
image = sharp::StaySequential(image, baton->input->access);
image = sharp::StaySequential(image);
image = nPages > 1
? sharp::EmbedMultiPage(image,
baton->extendLeft, baton->extendTop, baton->width, baton->height,
Expand Down Expand Up @@ -671,7 +671,7 @@ class PipelineWorker : public Napi::AsyncWorker {
if (across != 0 || down != 0) {
int left;
int top;
compositeImage = sharp::StaySequential(compositeImage, access).replicate(across, down);
compositeImage = sharp::StaySequential(compositeImage).replicate(across, down);
if (composite->hasOffset) {
std::tie(left, top) = sharp::CalculateCrop(
compositeImage.width(), compositeImage.height(), image.width(), image.height(),
Expand Down Expand Up @@ -729,13 +729,13 @@ class PipelineWorker : public Napi::AsyncWorker {

// Apply normalisation - stretch luminance to cover full dynamic range
if (baton->normalise) {
image = sharp::StaySequential(image, access);
image = sharp::StaySequential(image);
image = sharp::Normalise(image, baton->normaliseLower, baton->normaliseUpper);
}

// Apply contrast limiting adaptive histogram equalization (CLAHE)
if (baton->claheWidth != 0 && baton->claheHeight != 0) {
image = sharp::StaySequential(image, access);
image = sharp::StaySequential(image);
image = sharp::Clahe(image, baton->claheWidth, baton->claheHeight, baton->claheMaxSlope);
}

Expand Down Expand Up @@ -1005,7 +1005,7 @@ class PipelineWorker : public Napi::AsyncWorker {
if (!sharp::HasAlpha(image)) {
baton->tileBackground.pop_back();
}
image = sharp::StaySequential(image, access, baton->tileAngle != 0);
image = sharp::StaySequential(image, baton->tileAngle != 0);
vips::VOption *options = BuildOptionsDZ(baton);
VipsArea *area = reinterpret_cast<VipsArea*>(image.dzsave_buffer(options));
baton->bufferOut = static_cast<char*>(area->data);
Expand Down Expand Up @@ -1207,7 +1207,7 @@ class PipelineWorker : public Napi::AsyncWorker {
if (!sharp::HasAlpha(image)) {
baton->tileBackground.pop_back();
}
image = sharp::StaySequential(image, access, baton->tileAngle != 0);
image = sharp::StaySequential(image, baton->tileAngle != 0);
vips::VOption *options = BuildOptionsDZ(baton);
image.dzsave(const_cast<char*>(baton->fileOut.data()), options);
baton->formatOut = "dz";
Expand Down

0 comments on commit aa8bc19

Please sign in to comment.