In my graphics app I’m working on, I had this idea that each time an effect is applied , store that new image in an the array. Now from my testing it works great for the undo going back to the source image. Even though this works my concern is with memory usage. My app isn’t an image editor like GIMP/PhotoShop/etc. It just gives images some rather cool effects. So memory usage might not be that big of an issue, being a user is mostly likely only going to use certain amount of effects per image. Certainly not all 100+ effects, at least I wouldn’t think any one would.
So would using an array this way be ok for undo/redo?
Because most Systems today use SDD or even faster Storage Solutions , i’d recommend to store past picture variants into a temporary folder, not in memory.
Most users may not use that many effects, but images today can become very large.
One method of Undo is complex, but involves considering changes as a list of instructions.
Simplifying:
Open picture1.png
Set drawing color to ‘white’
Draw a Circle x,y,x2,y2
Apply Blur effect with values A,B,C
These action descriptions are stored in a list.
Undo then becomes a case of going back to line 1 and executing the same instructions up to the second last line. Each kind of instruction goes to a method to implement that instruction.
Very little memory used. It also paves the way to script your app. But you need to code towards it - it can be messy to implement at a late stage.
You’re right to be concerned about memory usage. Why not consider a hybrid system that stores a limited number of the most recent undo states in memory and migrates the older ones off to disk? This could be transparent to the user.
if you use an array of variants, you can have actual images for say 10 of the slots, and as you add new images at one end, save the older ones as temp files, and replace their slots with the filenames.
That way you hold 10 images plus some strings, instead of {many} images in memory
If you undo as far back as filename, retrieve it from disc.