-
-
Notifications
You must be signed in to change notification settings - Fork 80
Line drawing, tests and circles improvement #378
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
59fad02
pbio/image: Implement line drawing.
schodet 4768bf3
pbio/image: Add Image test.
schodet d27d6a3
pbio/image: Add more tests.
schodet 8d9b7cd
pbio/image: Improve circle and disc shapes.
schodet 700dbb5
pbio/image: Use [in] instead of [in,out] for image parameter.
schodet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -262,6 +262,142 @@ void pbio_image_draw_vline(pbio_image_t *image, int x, int y, int l, | |||||
| } | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Draw a line with a flat slope (less or equal to 1). | ||||||
| * @param [in,out] image Image to draw into. | ||||||
| * @param [in] x1 X coordinate of the first end. | ||||||
| * @param [in] y1 Y coordinate of the first end. | ||||||
| * @param [in] x2 X coordinate of the second end. | ||||||
| * @param [in] y2 Y coordinate of the second end. | ||||||
| * @param [in] value Pixel value. | ||||||
| * | ||||||
| * This is an internal function, x2 must be greater or equal to x1. | ||||||
| * | ||||||
| * Clipping: drawing is clipped to image dimensions. | ||||||
| */ | ||||||
| static void pbio_image_draw_line_flat(pbio_image_t *image, int x1, int y1, | ||||||
| int x2, int y2, uint8_t value) { | ||||||
| int dx = x2 - x1; | ||||||
| int dy = y2 - y1; | ||||||
| int ydir = 1; | ||||||
| int x, y, err; | ||||||
|
|
||||||
| // Fall back to horizontal line, much faster. | ||||||
| if (dy == 0) { | ||||||
| pbio_image_draw_hline(image, x1, y1, dx + 1, value); | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| // Clipping, X out of image. | ||||||
| if (x1 >= image->width || x2 < 0) { | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| // Check Y direction. | ||||||
| if (dy < 0) { | ||||||
| dy = -dy; | ||||||
| ydir = -1; | ||||||
| } | ||||||
|
|
||||||
| // Error is scaled by 2 * dx, offset with one half to look at mid point. | ||||||
| err = -dx; | ||||||
| y = y1; | ||||||
|
|
||||||
| // Skip pixels left of image. | ||||||
| if (x1 < 0) { | ||||||
| err += -x1 * dy * 2; | ||||||
| int yskip = (err + dx * 2) / (dx * 2); | ||||||
| err -= yskip * dx * 2; | ||||||
| y += yskip * ydir; | ||||||
| x1 = 0; | ||||||
| } | ||||||
|
|
||||||
| // Skip pixels right of image. | ||||||
| if (x2 >= image->width) { | ||||||
| x2 = image->width - 1; | ||||||
| } | ||||||
|
|
||||||
| // Draw. | ||||||
| x = x1; | ||||||
| do { | ||||||
| pbio_image_draw_pixel(image, x, y, value); | ||||||
| err += dy * 2; | ||||||
| if (err >= 0) { | ||||||
| err -= dx * 2; | ||||||
| y += ydir; | ||||||
| } | ||||||
| x++; | ||||||
| } while (x <= x2); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Draw a line with a steep slope (more than 1). | ||||||
| * @param [in,out] image Image to draw into. | ||||||
|
||||||
| * @param [in,out] image Image to draw into. | |
| * @param [in] image Image to draw into. |
And everywhere else too.
Contributor
Author
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.
Done.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
[in,out]would requirepbio_image_t **imageand would return a different pointer than the one that was passed in.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 do not agree, the image is read and written, so it’s both in and out.
Random people on the internet seems to agree: https://stackoverflow.com/questions/47732665/is-that-an-in-or-in-out-parameter-doxygen-c 🙂
BTW, I find those in/out redundant for C, it’s usually clear from the function prototype.
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.
Sure, there is room for interpretation. I learned C# before C, so I've always used the C# semantics for in/out, which are well defined.
This is exactly why I don't consider
[in,out]correct here. It is only useful for something like:Where the precondition is that
*sizeis set to the size ofdatain bytes and the post-condition is that*sizeis set to the number of bytes actually read, which may be less than the input size. Just going by the signature alone, one would normally expectsize_t *sizeto be an out parameter so[in,out]makes it a bit more obvious that something unusual is going on.Anyway, I'm not going to make a big deal about it. I don't think we've used this 100% consistently throughout the code anyway. But, in the majority of cases, we have only used
[in]for cases where a struct is modified by a function. So it would be more consistent to stick with just[in].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 guess for C# structs though, we would need to call it
[in,out]like you are doing here since otherwise C# would pass the struct by value. 😄 It's just that normally something like the image type would be an object in C# rather than a struct. I consider passing a struct by reference in C like passing an object in C#. So, yeah, I get your point.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.
OK, made the change for the whole file.