在照片编辑应用程序中实现 "undo" 功能的最佳方式是什么?

What's the best way to implement an "undo" feature in photo editing application?

显然,存储更改历史数组需要大量内存...这就是我让我的应用程序工作的方式,但似乎有更聪明的方法来执行此操作。

ArrayList<Photo> photoHistory = new ArrayList<>();
photoHistory.add(originalPhoto);
photoHistory.add(change1);
photoHistory.add(change2);

// bad implementation - lots of memory

也许只存储原始视图模型和当前视图模型并记录使用的 methods/filters?然后,当用户点击 'undo' 时,所做的更改总数和 运行 再次减一?这似乎也非常低效。

我想我只是在寻找有关如何实现软件应用程序的一般 'undo' 功能的建议。

这里是 GIMP 如何实现它的提示:

GIMP's implementation of Undo is rather sophisticated. Many operations require very little Undo memory (e.g., changing visibility of a layer), so you can perform long sequences of them before they drop out of the Undo History. Some operations, such as changing layer visibility, are compressed, so that doing them several times in a row produces only a single point in the Undo History. However, there are other operations that may consume a lot of undo memory. Most filters are implemented by plug-ins, so the GIMP core has no efficient way of knowing what changed. As such, there is no way to implement Undo except by memorizing the entire contents of the affected layer before and after the operation. You might only be able to perform a few such operations before they drop out of the Undo History.

Source

因此,为了尽可能优化,您必须根据要撤消的操作执行不同的操作。显示或隐藏图层可以用微不足道的 space 表示,但过滤整个图像可能需要存储整个图像的另一个副本。但是,如果您只过滤图像的一部分(或绘制图像的一小部分),也许您只需要存储那部分图像。