views:

394

answers:

5

The question probably applies to drawing systems in general. I was wondering how the undo functionality is implemented in PS. Does the program take snapshots of the canvas before each operation? If so, wouldn't this lead to huge memory requirements? I've looked into the Command pattern, but I can't quite see how this would be applied to drawing.

Regards, Menno

+9  A: 

It's called the command pattern. It's simple to implement as useful for any sort of editor.

Photoshop applies stacked transformations upon the original image. One opetation one command. It simply unapplies the transformation when you undo. So it just keeps the original and latest versions, but I guess it might cache the last few versions just for performance.

Robert Gould
I've implemented this in a WYSIWYG editor. It seems impossible at first, but once youg grasp the pattern, it's really pretty easy.
Jon B
That's my same experience, I thought it was never going to work, and that there had to be a secret ingridient, but once I finished it just worked :)
Robert Gould
Actually, the current version is only existing version and the command history is actually a list of reversed commands. At least that's how a word processor would do it.
jmucchiello
for a word processor or any editor I think you are right, but when you click revert to original photoshop is too fast, and it's not accessing the HD (I think) so I imagine it's caches as an optimization
Robert Gould
@Robert: Unapplying a transformation may not always be possible, for example, how do you unapply a blur? The information is just lost and is not recoverable.
Lazer
+3  A: 

Since some operations will be non-reversable and as you say snapshoting the entire image every time would be out of the question then the only other alternative I can see would be a stack of deltas. A delta being the set of masks containing the modified pixels prior to the operation. Of course many operations may be reversable so their deltas could be optimised.

AnthonyWJones
Also Photoshop uses the hard disk for undo information, so no additional memory requirements there.
Joey
+1  A: 

Photoshop uses History to track their actions. These also serve as Undo as you can go back in history at any point. You can set the size of history in preferences.

I also suggest you look into Adobe Version Cue as a tool for retrospect undo or versions, it's built into the suite for that sole purpose. http://en.wikipedia.org/wiki/Adobe_Version_Cue

Codex73
A: 

I really don't know but you can go to http://paint.net/ and get its source code they have the same feature

Yassir
+1  A: 

I'm not sure how Photoshop implements undo, but the Paint node within Apple's Shake compositing application is pretty easy to explain:

  • Each stoke is stored as a series of points, along with some information like stroke-colour, brush-size etc.
  • When you draw a stoke, the changes are made on the current image
  • Every x strokes (10 I think) the current image is cached into memory
  • When you undo, it redraws the last ~9 stokes on the previous cached image.

There are two problems with this:

  • When you undo more than 10 times, it has to recalculate the whole image. With thousands of strokes this can cause a several second pause
  • With Shake, you save the setup file, containing the stroke information - not the actual pixel values.. Then means you have to recalculate the whole image whenever you reopen the Paint node, or render the image (not nearly as big a problem as the undo thing, however)

Well, there is a third problem, that being Shake is horribly buggy and poorly implemented in many areas, the Paint node beign one of them - so I'm not sure how good an implementation this is, but I can't imagine Photoshop being too dissimilar (albeit far better optimised)

dbr