Robby
12/12/2004 12:09:00 PM
It seems a little odd to me that the matrix set in
ImageAttributes.SetColorMatrix is not being used for matrix multiplication
in Graphics.DrawImage on the pixel values.
In particular the matrix,
Dim mx As ColorMatrix = _
New ColorMatrix(New Single()() _
{New Single() {-1, 0, 0, 0, 0}, _
New Single() {0, -1, 0, 0, 0}, _
New Single() {0, 0, -1, 0, 0}, _
New Single() {0, 0, 0, 1, 0}, _
New Single() {0, 0, 0, 0, 1}})
almost produces a "negative" image when used with Graphics.DrawImage in an
ImageAttribute. However, this matrix clearly should not produce a negative
image as it gives a pixel of {r, g, b} a new value of {-r, -g, -b} and we
all know that the valid values are 0 to 255 so negative values are not
possible. The correct matrix to use by matrix multiplication as defined in
the Framework SDK (and mathematically with row RGB vectors) should be,
Dim mx As ColorMatrix = _
New ColorMatrix(New Single()() _
{New Single() {-1, 0, 0, 0, 0}, _
New Single() {0, -1, 0, 0, 0}, _
New Single() {0, 0, -1, 0, 0}, _
New Single() {0, 0, 0, 1, 0}, _
New Single() {255, 255, 255, 0, 1}})
which would give the correct values of a "negative" pixel of {r, g, b} a new
value of {255-r, 255-g, 255-b}. However, this ColorMatrix maps all pixels
to {255, 255, 255}.
It seems to me that when confronted with a negative diagonal entry (ie
ColorMatrix(0,0), ColorMatrix(1,1), ... ) that the Framework is doing
something "clever" with that value instead of simply multiplying it. This
cleverness results in it mapping both starting values of 255 and 0 to a new
value of 0. This has obvious problems. Bob Powell's site offers a clever
work around that rescales the RGB values from 0 to 255 down to 1 to 255.
This brings with it a new set of problems when taking the negative image of
the negative image as colors do not match back to their originals correctly
as there is know way of knowing whether to map a 1 to 0 or to 1. Also it
seems that successive image negations noticeablely darken the image.
Anyway, I said all this simply to ask if anyone knows how to turn off this
special case of negative multiplication in Graphics.DrawImage when using
ImageAttributes.SetColorMatrix. I've searched the Framework and have come
up empty handed. Hopefully someone out there can help.
Thanks in advance
Robby