A 2D matrix can be stored in a 1D vector as a sequence of consecutive fixed-width rows, and the formula for accessing position (x, y)
is y*width + x
.
Here's a visual example. Suppose you have a 3x3 matrix filled with 0
and a $
at position (1, 2)
(that's column first, row second, using 0-based indices). You can store it as a 1D vector as follows:
[0 0 0 0 0 0 0 $ 0]
And here it is visually re-arranged to make sense as a 3x3 matrix:
y
[0 0 0 0
0 0 0 1
0 $ 0] 2
x 0 1 2
Because it is a 1D vector (see first figure), we need a single index to get at that $
value. But because we think of it as a 2D matrix, all we have is the (1, 2)
location. We need a formula to convert it to a single index.
That formula is the y*width + x
, like you saw. It roughly means "y
is how many full rows I have to skip, before moving to the correct column using x
". Note the formula as written works because we are using 0-based indices, otherwise it requires minor modification.
So in this example, y = 2
, x = 1
and width = 3
:
2 * 3 + 1 = 7
And index 7
of our original vector [0 0 0 0 0 0 0 $ 0]
has the value $
that we wanted. (Again, note 7
is a zero-based index).
Best Answer
Actually, there are two very different algorithms in Adobe Photoshop CS3 or later.
The old one just shifts linearly the color of a pixel. The second one works proportionally. If your intent is to understand, in general, how contrast is applied, use the legacy contrast only. Here's an image showing a difference:
When using legacy contrast, you see that it's much more intuitive from developers point of view. It actually lightens the dark pixels, and darkens the light ones when reducing the contrast. When increasing the contrast, you will see the same happening with gray color (turning lighter, then white at 100%), while blank and white staying the same.
From the point of view of photographers, on the other hand, this doesn't make any sense. As you can see below, when applied to real images, the new contrast algorithm has much better visual results than the old one:
See also:
Better Brightness And Contrast In Photoshop CS3: describes the difference between legacy and new contrast in Photoshop,
Brightness Calculation in Digital Image Processing (PDF)is explaining scientifically the brightness and contrast editing algorithms.