Carlos Enrique Hernández Ibarra bio photo

Carlos Enrique Hernández Ibarra

Passionated Software Engineer with experience in the Automotive Industry. Interested in the state of the art of design, development, and test of technology based on C/C++, Autosar, and Matlab/Simulink. Nevertheless, always with an open mind to new technologies, industries and solutions.

Email LinkedIn Github

Introduction

UndoImage is a software program that performs various image manipulations. Additionally, it utilizes the Command Pattern to undo and redo commands.

The Command Pattern encapsulates operations as objects with a common execution interface. This enables the positioning of operations in a queue, initiating saved operations, and undoing the results of a previously executed operation.

One of the Qt classes that can implement this pattern is QUndoCommand. QUndoCommand represents an action of dedicated operations and serves as the base class for all operations stored in a QUndoStack. QUndoCommand utilizes the functions undo() to undo operations and redo() to redo operations.

In UndoImage, six different image manipulators are employed, each of which publicly inherits from QUndoCommand:

  • AdjustColors: For each RGBA pixel, the colors (Red, Green, and Blue) are adjusted based on user-specified values. AdjustColors

  • MirrorPixels: Performs a pixel copy to simulate a mirror effect, either horizontally or vertically. Mirror

  • Monochrome: Created by setting all three colors to the same value. However, to prevent the image from becoming overly dark, they are multiplied by different factors: Red *= 0.30, Green *= 0.59, and Blue *= 0.11. Subsequently, the three resulting values are added to obtain luminance, which is then set as the value for each color. 03

Negative: Converts the tricolor image to its negative. Each color value (v) is replaced by 255 - v. 04

ScrambleColor: For each pixel, permutes the values of the colors among themselves. 05

TriColor: For each pixel, compares its color intensity (average of its 3 colors), and depending on established hysteresis, reduces its colors to zero. 06

Development

Each of the manipulators will inherit the undo() and redo() functions virtually, and they will add a specialized function for the operation. It’s also important to store the image in a member variable to have a backup of the current image. 07

Among all manipulators, in all undo() functions, an assignment of the backup image to the current image will be performed. Meanwhile, the redo() function will execute the specialized operation, in this case, adjust(). 08

The parameters will come from spin boxes in the user interface window. Upon pressing a trigger button, the respective instance of QUndoCommand will be stored in the QUndoStack, and the window will be refreshed to display the changes. 09