关于反色(100分)

  • 主题发起人 主题发起人 fc_long
  • 开始时间 开始时间
F

fc_long

Unregistered / Unconfirmed
GUEST, unregistred user!
如何将Bitmap中的黑白两种颜色对调,要快速的方法。
 
for J:=0 to image1.width-1 do
for K:=0 to image1.Height-1 do
begin
if image1.Picture.Bitmap.Canvas.Pixels[j,k]=clwhite then
image1.Picture.Bitmap.Canvas.Pixels[j,k]:=clblack;
if image1.Picture.Bitmap.Canvas.Pixels[j,k]=clblackthen
image1.Picture.Bitmap.Canvas.Pixels[j,k]:=clwhite ;
end;
 
使用循環的方式,將每個像素的RGB分量值計算出來,然後每個分量值都被255減,再將減後的結果畫出一張新圖就是反色圖,這個方法適用與任何bmp格式的圖片
 
如果是索引色,直接写调色板最快
 
BOOL BitBlt(
HDC hdcDest, // handle to destination DC
int nXDest, // x-coord of destination upper-left corner
int nYDest, // y-coord of destination upper-left corner
int nWidth, // width of destination rectangle
int nHeight, // height of destination rectangle
HDC hdcSrc, // handle to source DC
int nXSrc, // x-coordinate of source upper-left corner
int nYSrc, // y-coordinate of source upper-left corner
DWORD dwRop // raster operation code
);
用这个API,系统会尽可能启用硬件加速。
 
r:=255-getRvalue(TColor(Value));
g:=255-getRvalue(TColor(Value));
b:=255-getRvalue(TColor(Value));
Color:=TColor(RGB(r,g,b));
 
to showton:
这种方法就是我目前用的方法, 速度慢.
to zhu_jy:
能不能说的详细一些.
xusong168:
能不能解释一下DWORD dwRop // raster operation code; 有几种模式, 分别为何种意思.
 
不要用pixels,速度慢。

用scanline速度非常快。。。。
 
第一种办法:
procedure TImageView.ImageInvert(Bitmap: TBitmap);
var
TempBitmap: TBitmap;
begin
//此种办法最为简单,而且速度快
TempBitmap := TBitmap.Create;
try
Bitmap.Canvas.CopyMode := cmNotSrcCopy;
TempBitmap.Assign(Bitmap);
Bitmap.Canvas.Draw(0, 0, TempBitmap);
finally
TempBitmap.Free;
end;
Bitmap.Canvas.CopyMode := cmSrcCopy;
end;

第二种办法:scanline

procedure TImageView.ImageInvert();
var
pRGB: pByteArray;
i, j: Integer;
begin
Image.Picture.BitMap.PixelFormat := pf24BIT;
for j := 0 to Image.Picture.Bitmap.Height - 1 do
begin
pRGB := Image.Picture.Bitmap.ScanLine[j];
for i := 0 to Image.Picture.Bitmap.Width * 3 - 1 do
begin
pRGB^ := 255 - pRGB^;
Inc(pRGB);
end;
end;
Image.Refresh;
end;
 
to 还是朋友:
我只是黑色和白色取反, 其他的颜色不变, 请提示.
 
判斷RGB分量值是否為0或者255,0為黑,255為白
 
如果位图为索引色的话,表示每个点的不是实际的RGB值,而是一个颜色的索引。
所以只要直接改位图的索引表,把黑色改为白色,把白色改为黑色,就可以了。
这样是不是很快?
 
接受答案了.
 
后退
顶部