-
Notifications
You must be signed in to change notification settings - Fork 40
Image rescaling for IMAGE() #241
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: master
Are you sure you want to change the base?
Conversation
src/ui/image.cpp
Outdated
@@ -211,9 +214,46 @@ ImageBuffer *get_image(unsigned bid) { | |||
return result; | |||
} | |||
|
|||
void scaleImage(ImageBuffer* image, var_num_t scaling) { | |||
if(scaling == 1.0 || scaling <= 0.0) { |
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.
Could you please change all the occurrences "if("
to "if ("
(ie add the space)
(same with for(
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.
done.
|
||
uint8_t* scaledImage = (uint8_t *)malloc(w * h * 4); | ||
if(!scaledImage) { | ||
err_throw(ERR_IMAGE_LOAD, "Failed to allocate RAM"); |
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.
This sets up for SmallBASIC try/catch handling but isn't itself a throw
like with java etc.
So you still need to avoid invoking the code below. So either use else { ...
or return
here.
In this case a simple return is the simplest solution.
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.
Thanks, I didn't know that. I changed it.
Thanks for this, looks useful. Could you please also include an example? |
I added examples to the source code. I hope that's what you want. Later I'll add a description to the our website. I can also create an example in smallbasic.samples. |
Hi Chris,
I have different Android devices with different screen sizes and pixels. I was missing an easy way to rescale an image to adapt for the screen size and pixel size.
I added an optional parameter to the IMAGE() function to scale the image. The algorithm is almost "nearest neighbor". Instead of round() I use floor(). This is much faster and was easier to implement. Getting the scaling with all possible cases of IMAGE() working, was not as easy as I thought in the beginning. That is the reason, why the same lines of code are repeated several times in the source file.
Syntax is as following:
Best regards, Joerg