Easier way for saving image in thread?

So I have a drawing programm, that needs to load in images & save them. Is there something that makes it faster. For example I have an 4k image, that needs to be saved. It takes like 3 seconds. And I do not want to use pointers or Something cause I’m dumb xd

The problem is it needs to load a image when it undos something or redos. It also needs to save an image when drawing something, for undo & redo to happen.

I have a “Layer/Canvas” class where all layers, all undo & drawing/erasing/typical artist stuff happens. But I cant do thread in a class. And then I need to use in the main script something like “Canvas.save(canvas.getCurrentLayer+”“+canvas.getCurrentUndoStep+”“)” as a thread and execute it from my class. But this doesn’t work, it just makes my whole system not work even tho when I don’t use thread it works.

Should I just have a PGraphics array for the undo steps? Only problem is I (think i) can’t have an array that stores arrays that stores tons of undo steps for 512 layers.

I’m coding the programm so it’s fast, everything works fine & smooth but sadly it needs to load looooong for undo & after every brush stroke.

Sorry if this is very complicated to read.

Also I can’t provide the source code, cause it’s over 2k lines. And mabye there is something like “saveToMemory” or “queue”.

Thank you for your patience <3

1 Like

I can’t speak to your specific problem, but I’ve written a drawing program with undo/redo and save/export. I use a Deque (double ended queue) to hold my undo redo steps and a custom Brush class that defines my strokes. All this is saved to a SQLite database. Only x/y coordinates, pen pressure, etc. are being saved so I’m not saving raster data unless I’m exporting. The time to save is pretty short. This also allows the database file to be read back in and edited later.

1 Like

Hi @Aryszin,

Are you modifying the raw pixels or you are drawing shapes with beginShape / vertex / curveVertex?

In the latter case it’s not necessary to store the whole canvas as pixels for undo/redo steps, you can just store the information needed to redraw those shapes (center, stroke points…).

For pixel based modifications, you can check this great thread:

  • Storing the whole canvas (with an array of PGraphics canvas copy) gets really expansive quickly
  • You can split your canvas and only store the modified tiles (cheaper)
  • You can store the information necessary to “erase” the drawing you just did (for example computing the difference between the previous pixels)
  • You can have a mix of the two previous techniques
4 Likes

I already implemented undo/redo :smiley:

All right, but your issue is that since you are saving a copy of the canvas for each user action it slows down the painting experience therefore your intention to save the image in a Thread.

You said it yourself :wink: :

you know what i mabye could try?
if something is drawn, it saves the image to a buffer, if the user does not draw, the programm saves the images to the drive. loading the images for undo is not a problem tbh.
i know its not the fastest you can get but i dont care lol

yes it’s a possibility if you don’t care much about drive storage or the time it takes to save a 4K image on disk :wink: