Image use question

Hello, i played with JUCE a bit and it’s very fun, but i have so many quiestions, that i don’t even know where to start :slight_smile:, from what i saw in demo example and have read here on forum, images should be temporary and allocated on the stack. Does that mean, that i need to create images, read, decode, then process them right in paint function? Isn’t that not efficient? And as i understand, i need to create 1 Graphics object and then to use that context to manipulate all this images, right? Should i somehow to release/delete that Graphics context in the end of paint function? What i did in other framework, i simply loaded images in constructor of my class into memory bitmaps on the heap, do all processing with those bitmaps in constructor and then i can simply copy stuff from those cached images whenever i want to main bitmap, can i have same approach in JUCE? , i do not want to do the same calculations all over again on every paint function call, i want just to copy portions of bitmaps from permanently saved in memory bitmaps. Or maybe there’s better approach how to work with images? Also if all images are on the stack, it may lead to stack overflow, no? Thanks.

“Stack” is sometimes used here a bit misleadingly unfortunately. Often people really mean the objects are supposed to be used as “values”. There is no need to use Image as a heap allocated object via a pointer, as Image already internally handles the heap allocations. (And there’s also a built-in mechanism to share the heap allocated data when making copies of the Images etc.)

So you can have an Image object as a member variable of your class just fine and predraw/preload whatever you want into that, for example in your constructor.

There is also the ImageCache object which can be handy and with that you might actually be using Images as local (stack) variables, as it doesn’t really cost anything to get a copy of the image from the cache and destroy the copy.

1 Like

Thank you, i guess i start to understand now, will try to work with it later. Should i delete/release Graphics g object, or it also handles itself, when it goes out of scope?

If it’s the Graphics object you are passed in the paint() call, that’s handled automatically. (There are fairly few instances when you would ever be creating the Graphics objects yourself.)

1 Like

you can use a Graphics-object to draw on an image, like this:
auto image = Image(Image::ARGB, bounds);
Graphics g{ image };

i find this very useful for drawing background images in the resized method before painting it to the screen in paint. that way you can preveent it from repainting your image when you turn sliders. especially useful when using randomizers for drawing an image.

you can also make a 2d for loop going through all x and y indexes of the image to operate processes for each pixel. that’s nice for adding postprocessing effects like blur or posterize imo. theirfore you don’t need Graphics

1 Like