Originally Posted by
Polar Xpress
To do it in the manner you mentioned, I would still need to iterate over each pixel to get its color, convert it to gray scale, and then set the pixel to the new gray value, correct? In doing it that way, it didn't seem particularly faster, unfortunately. It did, however, help me discover the usefulness of MufasaBitmap, as it has a couple other functions I've been in need of.
I gave a hint (the idea was for you to look into TMufasaBitmap and explore it -- You seemed eager to learn so I didn't want to post a full on answer). Anyway, here's how to do it with Simba's internals (just as fast as a plugin).
Simba Code:
function grayScale: Integer;
var
Area: TBox;
Bmp: TMufasaBitmap;
begin
GetClientDimensions(Area.X2, Area.Y2);
Bmp := GetMufasaBitmap(BitmapFromClient(Area.X1, Area.Y1, Area.X2 - 1, Area.Y2 - 1));
Bmp.GreyScale();
Result := Bmp.getIndex;
//DisplayDebugImgWindow(Area.X2 - 1, Area.Y2 - 1);
//DrawBitmapDebugImg(Result);
end;
var
Bmp: Integer;
begin
Bmp := GrayScale;
FreeBitmap(Bmp);
end.
Even with iteration.. it'd still be faster than what you have because what I said does not make a matrix COPY of the bitmap's pixels and modifies it. It just modifies the already existing bitmap's underlying pixels.. For even more speed, you could also use the bitmap's pointer to the pixels and change it to gray as well. None of these options allocate extra memory.
All three options are faster. Unfortunately I don't have time to write you the example via Pointer. These two examples should be more than enough to get you started. (Note: the first example is faster).
Simba Code:
function grayScale: Integer;
var
I, J: Integer;
Area: TBox;
Bmp: TMufasaBitmap;
begin
GetClientDimensions(Area.X2, Area.Y2);
Bmp := GetMufasaBitmap(BitmapFromClient(Area.X1, Area.Y1, Area.X2 - 1, Area.Y2 - 1));
for I := 0 to Area.X2 - 1 do
for J := 0 to Area.Y2 - 1 do
Bmp.SetPixel(I, J, ColorToGray(Bmp.GetPixel(I, J))); //ColorToGray returns byte.
Result := Bmp.getIndex;
//DisplayDebugImgWindow(Area.X2 - 1, Area.Y2 - 1);
//DrawBitmapDebugImg(Result);
end;
var
Bmp: Integer;
begin
Bmp := GrayScale;
FreeBitmap(Bmp);
end.
@Wizzup?; ColorToGray seems to be red-scaling/red-tinting the bitmap.