Open main menu

CDOT Wiki β

Changes

Savy Cat

2,073 bytes added, 19:41, 25 March 2018
Rotate90
</nowiki>
==== Pre-profile Running Display Test ====We can un-comment the "test" section in Rotate.cpp to attempt reading read a .jpg, verify stored colour channel values that were storedare correct, and otherwise make sure everything the rotation is working as expected. Here is Tiny-Shay.jpg, 30px x 21px top-down image of my cat laying on the floor. Mousing over a pixel will display the X and Y coordinates, along with the corresponding red, green, blue values.
[[File:Verify-1.png|800px]]
[[File:Verify-2.png|800px]]
 
I verify three more rotations work as expected, resulting in 180°, 270°, and back to the original image with no loss or value changes.
 
==== CImg In Memory ====
To understand how an instance of the CImg class is stored in memory, [http://cimg.eu/reference/group__cimg__storage.html this article from CImg library site] does a very good job explaining it.
 
Essentially, CImg is a 4 dimensional array of dimensions (image width x image height x depth x colour channels). Multiply this by the size per pixel (one byte in our case) to get overall size of the variable. For 2 dimensional images (which is what we are working with), depth has a value of 1, resulting in a 3 dimensional array. The number of channels is 3, one for each primary colour: (red, green, and blue). This can be visualized as three 2D matrix where the value of each matrix at any specified point represents RGB values of one pixel at that same location. In the following code, we allocate space for the rotated image, knowing its width will become its height, and height become its width. 1 specifies the depth, 3 specifies number of colour channels, and 0 is the default value to initialize each element.
 
cimg_library::CImg<PX_TYPE> img_tiny90(img_tiny.height(), img_tiny.width(), 1, 3, 0);
 
Much like any dimensional array, CImg is stored in memory as a single dimensional array. It stores all of the red values, followed by all green values, followed by all blue values. It uses row major indexing, and the first value begins at 0 (not 1).
 
To access the first red pixel I could write:
 
img_tiny90(0, 0, 0, 0)
 
Red pixel at (1, 1):
 
img_tiny90(1, 1, 0, 0)
 
First green pixel:
 
img_tiny90(0, 0, 0, 1)
 
Third blue pixel:
 
img_tiny90(2, 0, 0, 2)
 
For any location at x & y, with width of image, height of image, z (number of colour channels):
 
return x + y * w + w * h * z;
 
The first portion of the index equation should look familiar (x + y * w) for indexing a square 2D matrix. Adding the result of (w * h * z) enables this to work for a rectangular matrix of z (3) dimensions.
93
edits