-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
feat: margin parameter for trim method #4484
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,3 +17,5 @@ package-lock.json | |
| .astro | ||
| docs/dist | ||
| release-notes.md | ||
| .cache | ||
| compile_commands.json | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -89,7 +89,8 @@ | |
| "Lachlan Newman <[email protected]>", | ||
| "Dennis Beatty <[email protected]>", | ||
| "Ingvar Stepanyan <[email protected]>", | ||
| "Don Denton <[email protected]>" | ||
| "Don Denton <[email protected]>", | ||
| "Dmytro Tiapukhin <[email protected]>" | ||
| ], | ||
| "scripts": { | ||
| "build": "node install/build.js", | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -285,7 +285,7 @@ namespace sharp { | |
| /* | ||
| Trim an image | ||
| */ | ||
| VImage Trim(VImage image, std::vector<double> background, double threshold, bool const lineArt) { | ||
| VImage Trim(VImage image, std::vector<double> background, double threshold, bool const lineArt, int const margin) { | ||
| if (image.width() < 3 && image.height() < 3) { | ||
| throw VError("Image to trim must be at least 3x3 pixels"); | ||
| } | ||
|
|
@@ -304,6 +304,13 @@ namespace sharp { | |
| } else { | ||
| background.resize(image.bands()); | ||
| } | ||
| auto applyMargin = [&](int left, int top, int width, int height) { | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. debatable, could be moved to a separate utility method, let me know what you think. |
||
| int const marginLeft = std::max(0, left - margin); | ||
| int const marginTop = std::max(0, top - margin); | ||
| int const marginWidth = std::min(image.width(), left + width + margin) - marginLeft; | ||
| int const marginHeight = std::min(image.height(), top + height + margin) - marginTop; | ||
| return std::make_tuple(marginLeft, marginTop, marginWidth, marginHeight); | ||
| }; | ||
| int left, top, width, height; | ||
| left = image.find_trim(&top, &width, &height, VImage::option() | ||
| ->set("background", background) | ||
|
|
@@ -324,15 +331,22 @@ namespace sharp { | |
| int const topB = std::min(top, topA); | ||
| int const widthB = std::max(left + width, leftA + widthA) - leftB; | ||
| int const heightB = std::max(top + height, topA + heightA) - topB; | ||
| return image.extract_area(leftB, topB, widthB, heightB); | ||
|
|
||
| // Combined bounding box + margin | ||
| auto [ml, mt, mw, mh] = applyMargin(leftB, topB, widthB, heightB); | ||
| return image.extract_area(ml, mt, mw, mh); | ||
| } else { | ||
| // Use alpha only | ||
| return image.extract_area(leftA, topA, widthA, heightA); | ||
| // Bounding box + margin | ||
| auto [ml, mt, mw, mh] = applyMargin(leftA, topA, widthA, heightA); | ||
| return image.extract_area(ml, mt, mw, mh); | ||
| } | ||
| } | ||
| } | ||
| if (width > 0 && height > 0) { | ||
| return image.extract_area(left, top, width, height); | ||
| // Bounding box + margin | ||
| auto [ml, mt, mw, mh] = applyMargin(left, top, width, height); | ||
| return image.extract_area(ml, mt, mw, mh); | ||
| } | ||
| return image; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -598,7 +598,7 @@ const vertexSplitQuadraticBasisSpline: string = sharp.interpolators.vertexSplitQ | |
| // Triming | ||
| sharp(input).trim({ background: '#000' }).toBuffer(); | ||
| sharp(input).trim({ threshold: 10, lineArt: true }).toBuffer(); | ||
| sharp(input).trim({ background: '#bf1942', threshold: 30 }).toBuffer(); | ||
| sharp(input).trim({ background: '#bf1942', threshold: 30, margin: 20 }).toBuffer(); | ||
|
|
||
| // Text input | ||
| sharp({ | ||
|
|
@@ -705,20 +705,20 @@ sharp(input) | |
| // https://github.com/lovell/sharp/pull/4048 | ||
| sharp(input).composite([ | ||
| { | ||
| input: 'image.gif', | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. VSCode insisted on removing these extra spaces when opening the file. Let me know if I should revert these. |
||
| animated: true, | ||
| limitInputPixels: 536805378, | ||
| density: 144, | ||
| input: 'image.gif', | ||
| animated: true, | ||
| limitInputPixels: 536805378, | ||
| density: 144, | ||
| failOn: "warning", | ||
| autoOrient: true | ||
| } | ||
| ]) | ||
| sharp(input).composite([ | ||
| { | ||
| input: 'image.png', | ||
| input: 'image.png', | ||
| animated: false, | ||
| limitInputPixels: 178935126, | ||
| density: 72, | ||
| limitInputPixels: 178935126, | ||
| density: 72, | ||
| failOn: "truncated" | ||
| } | ||
| ]) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I use
clangfor suggestions and type checking:.cachefolder by defaultcompile_commands.jsonflags to know where to pull 3rd-party header files from.I thought future contributors may benefit from this change.