Skip to content

Commit 83db5f8

Browse files
authored
Ensure withoutReduction does not interfere with contain/crop/embed (#3081)
1 parent 7eb5efa commit 83db5f8

4 files changed

Lines changed: 14 additions & 54 deletions

File tree

src/common.cc

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,7 @@ namespace sharp {
885885
}
886886

887887
std::pair<double, double> ResolveShrink(int width, int height, int targetWidth, int targetHeight,
888-
Canvas canvas, bool swap, bool withoutEnlargement) {
888+
Canvas canvas, bool swap, bool withoutEnlargement, bool withoutReduction) {
889889
if (swap) {
890890
// Swap input width and height when requested.
891891
std::swap(width, height);
@@ -940,9 +940,14 @@ namespace sharp {
940940
}
941941
}
942942

943-
// We should not enlarge (oversample) the output image,
944-
// if withoutEnlargement is specified.
945-
if (withoutEnlargement) {
943+
// We should not reduce or enlarge the output image, if
944+
// withoutReduction or withoutEnlargement is specified.
945+
if (withoutReduction) {
946+
// Equivalent of VIPS_SIZE_UP
947+
hshrink = std::min(1.0, hshrink);
948+
vshrink = std::min(1.0, vshrink);
949+
} else if (withoutEnlargement) {
950+
// Equivalent of VIPS_SIZE_DOWN
946951
hshrink = std::max(1.0, hshrink);
947952
vshrink = std::max(1.0, vshrink);
948953
}

src/common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ namespace sharp {
345345
the required thumbnail width/height and canvas mode.
346346
*/
347347
std::pair<double, double> ResolveShrink(int width, int height, int targetWidth, int targetHeight,
348-
Canvas canvas, bool swap, bool withoutEnlargement);
348+
Canvas canvas, bool swap, bool withoutEnlargement, bool withoutReduction);
349349

350350
} // namespace sharp
351351

src/pipeline.cc

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -138,17 +138,6 @@ class PipelineWorker : public Napi::AsyncWorker {
138138
pageHeight = inputHeight;
139139
}
140140

141-
// If withoutReduction is specified,
142-
// Override target width and height if less than respective value from input file
143-
if (baton->withoutReduction) {
144-
if (baton->width < inputWidth) {
145-
baton->width = inputWidth;
146-
}
147-
if (baton->height < inputHeight) {
148-
baton->height = inputHeight;
149-
}
150-
}
151-
152141
// Scaling calculations
153142
double hshrink;
154143
double vshrink;
@@ -161,7 +150,7 @@ class PipelineWorker : public Napi::AsyncWorker {
161150
// Shrink to pageHeight, so we work for multi-page images
162151
std::tie(hshrink, vshrink) = sharp::ResolveShrink(
163152
inputWidth, pageHeight, targetResizeWidth, targetResizeHeight,
164-
baton->canvas, swap, baton->withoutEnlargement);
153+
baton->canvas, swap, baton->withoutEnlargement, baton->withoutReduction);
165154

166155
// The jpeg preload shrink.
167156
int jpegShrinkOnLoad = 1;
@@ -286,7 +275,7 @@ class PipelineWorker : public Napi::AsyncWorker {
286275
// Shrink to pageHeight, so we work for multi-page images
287276
std::tie(hshrink, vshrink) = sharp::ResolveShrink(
288277
inputWidth, pageHeight, targetResizeWidth, targetResizeHeight,
289-
baton->canvas, swap, baton->withoutEnlargement);
278+
baton->canvas, swap, baton->withoutEnlargement, baton->withoutReduction);
290279

291280
int targetHeight = static_cast<int>(std::rint(static_cast<double>(pageHeight) / vshrink));
292281
int targetPageHeight = targetHeight;

test/unit/resize.js

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ describe('Resize dimensions', function () {
368368
assert.strictEqual(true, data.length > 0);
369369
assert.strictEqual('jpeg', info.format);
370370
assert.strictEqual(2800, info.width);
371-
assert.strictEqual(2225, info.height);
371+
assert.strictEqual(2286, info.height);
372372
done();
373373
});
374374
});
@@ -383,46 +383,12 @@ describe('Resize dimensions', function () {
383383
if (err) throw err;
384384
assert.strictEqual(true, data.length > 0);
385385
assert.strictEqual('jpeg', info.format);
386-
assert.strictEqual(2725, info.width);
386+
assert.strictEqual(2817, info.width);
387387
assert.strictEqual(2300, info.height);
388388
done();
389389
});
390390
});
391391

392-
it('Do not crop when fit = cover and withoutReduction = true and width >= outputWidth, and height < outputHeight', function (done) {
393-
sharp(fixtures.inputJpg)
394-
.resize({
395-
width: 3000,
396-
height: 1000,
397-
withoutReduction: true
398-
})
399-
.toBuffer(function (err, data, info) {
400-
if (err) throw err;
401-
assert.strictEqual(true, data.length > 0);
402-
assert.strictEqual('jpeg', info.format);
403-
assert.strictEqual(3000, info.width);
404-
assert.strictEqual(2225, info.height);
405-
done();
406-
});
407-
});
408-
409-
it('Do not crop when fit = cover and withoutReduction = true and width < outputWidth, and height >= outputHeight', function (done) {
410-
sharp(fixtures.inputJpg)
411-
.resize({
412-
width: 1500,
413-
height: 2226,
414-
withoutReduction: true
415-
})
416-
.toBuffer(function (err, data, info) {
417-
if (err) throw err;
418-
assert.strictEqual(true, data.length > 0);
419-
assert.strictEqual('jpeg', info.format);
420-
assert.strictEqual(2725, info.width);
421-
assert.strictEqual(2226, info.height);
422-
done();
423-
});
424-
});
425-
426392
it('Do enlarge when input width is less than output width', function (done) {
427393
sharp(fixtures.inputJpg)
428394
.resize({

0 commit comments

Comments
 (0)