UndoImage is a SW program that manipulates images. Moreover, it uses the Command Pattern to undo and redo commands.
The Command Patter encapsulates operations as objects in a common execution interface. This makes posssible position the operations in a queue, starting saved operations and undoing the results from a operation previosuly executed.
One of the Qt clases that can implement this pattern is QUndoCommand. Q UndoCommand represents an action of dedicated operations, and it is the key class for all saved operation in a QUndoStack. QUndoCommand uses the functions undo() to undo operations and redo() to redo operations.
In the program UndoImage, we use 6 different manipulations of image, each of them inherate publicly the QUndoCommand:
- AdjustColors. From each RGBA pixel, the colors (Red, Green and Blue) are fixed with specific values from the user.
- MirrorPixels. Realize a copy of pixels to mimic a mirror, either horizontally or vertically.
- Monochrome. It is produced by establishing the 3 colors with the same value, however, to avoid that the image turn out to be too blanky, we multiply the different factos with fixed factors: Red*=0.30, Green*=0.59 y Blue*=0.11. Then the 3 values are sume and they result to luminosity. The value established by each color.
- Negative. Convert the tricolor image to its negative. It replaces all the color value (v) by 255-(v).
- Scramble Color. For each pixel, permute the values of the colors among them.
- TriColor. For each pixel, compare its intensity of color ci (mean of the 3 colors) and depending of the hysteresis, reduce its color to 0.
Each of the handlers inherite from 2 functions undo() and redo() virtually. They will add a specialized function of the operation. It is important to save the image of each membber to have a backup of the actual image.
All of the handlers, in each undo() function, there will be an assigment of the backup image in the actual image. While the redo() performs a specialized function, in this case adjust().
The parameters provide the spinboxs of the user window. When pressing the trigger button, it will store the respective instance of QUndoCommand in the QUndoStack and it will refresh the window to show the changes.
Challenge: How to make a border detection image?
Ezust, A., & Ezust, P. (2012). An introduction to design patterns in C with Qt. Upper Saddle River, NJ: Prentice Hall.