Skip to content

Commit

Permalink
Fixes #12 and closes #37. (Merge branch 'olivierdagenais-resizer/issu…
Browse files Browse the repository at this point in the history
…e12')

Only the changes to Core/ImageBuilder.cs are being brought forward.  The unit tests have "off by one" issues on my machine... individual pixels will have a red, green, or blue value that's one off (96 vs. 97, 0 vs. 1, and 13 vs. 14).  You'd never see these differences, but they are enough to make the tests fail.

The recently-added layout regression tests in Samples/_PluginTestingApp (from Issue #67, 965a854) now show the "BAD (current)" tests failing, and the "GOOD (desired)" tests passing; we're getting the sizing/cropping behavior we want.  (I'll clean up the tests to remove the now-out-of-date "BAD" tests as a separate commit.)
  • Loading branch information
JaredReisinger committed Dec 11, 2013
2 parents 7e396e5 + c45cab3 commit 8b82a84
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions Core/ImageBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,7 @@ protected override RequestedAction LayoutImage(ImageState s) {
//Zoom factor
double zoom = s.settings.Get<double>("zoom", 1);

ScaleMode scale = s.settings.Scale;
//The target size for the image
SizeF targetSize = new SizeF(-1, -1);
//Target area for the image
Expand All @@ -1020,6 +1021,7 @@ protected override RequestedAction LayoutImage(ImageState s) {
//We first calculate the largest size the image can be under the width/height/maxwidth/maxheight restrictions.
//- pretending stretch=fill and scale=both

// TODO: why are doubles used here? It just means lots of casts further down
//Temp vars - results stored in targetSize and areaSize
double width = s.settings.Width;
double height = s.settings.Height;
Expand Down Expand Up @@ -1056,10 +1058,28 @@ protected override RequestedAction LayoutImage(ImageState s) {
} else if (fit == FitMode.Crop) {
//We autocrop - so both target and area match the requested size
areaSize = targetSize = new SizeF((float)width, (float)height);
//Determine the size of the area we are copying
Size sourceSize = PolygonMath.RoundPoints(PolygonMath.ScaleInside(areaSize, manualCropSize));
//Center the portion we are copying within the manualCropSize
s.copyRect = PolygonMath.ToRectangle(PolygonMath.AlignWith(new RectangleF(0, 0, sourceSize.Width, sourceSize.Height), s.copyRect, s.settings.Anchor));
var minWidth = Math.Min(manualCropSize.Width, (float)width);
var minHeight = Math.Min(manualCropSize.Height, (float)height);
if (scale == ScaleMode.DownscaleOnly
&& ((manualCropSize.Width <= (float)width) != (manualCropSize.Height <= (float)height))) {
// one of the dimensions is <= than the source (the other is not) and we're downscaling
// TODO: what happens if mode=crop;scale=down but the target is larger than the source?
areaSize = targetSize = new SizeF(minWidth, minHeight);
manualCropRect = new RectangleF(0, 0, minWidth, minHeight);
s.copyRect = PolygonMath.ToRectangle(PolygonMath.AlignWith(manualCropRect, s.copyRect, s.settings.Anchor));
}
else if (scale == ScaleMode.UpscaleCanvas) {
targetSize = new SizeF(minWidth, minHeight);
manualCropRect = new RectangleF(0, 0, minWidth, minHeight);
s.copyRect = PolygonMath.ToRectangle(PolygonMath.AlignWith(manualCropRect, s.copyRect, s.settings.Anchor));
}
else
{
//Determine the size of the area we are copying
Size sourceSize = PolygonMath.RoundPoints(PolygonMath.ScaleInside(areaSize, manualCropSize));
//Center the portion we are copying within the manualCropSize
s.copyRect = PolygonMath.ToRectangle(PolygonMath.AlignWith(new RectangleF(0, 0, sourceSize.Width, sourceSize.Height), s.copyRect, s.settings.Anchor));
}

} else { //Stretch and carve both act like stretching, so do that:
areaSize = targetSize = new SizeF((float)width, (float)height);
Expand Down

0 comments on commit 8b82a84

Please sign in to comment.