我想将像素从BMP1复制到BMP2,但复制的图像有问题。为什么?
注意:输入镜像为pf8bit;
TYPE
TPixArray = array[0..4095] of Byte;
PPixArray = ^TPixArray;
procedure Tfrm1.CopyImage;
VAR
BMP1, BMP2: TBitmap;
y, x: Integer;
LineI, LineO: PPixArray;
begin
BMP1:= TBitmap.Create;
BMP2:= TBitmap.Create;
TRY
BMP1.LoadFromFile('test.bmp');
BMP2.SetSize(BMP1.Width, BMP1.Height);
BMP2.PixelFormat:= BMP1.PixelFormat;
for y:= 0 to BMP1.Height -1 DO
begin
LineI := BMP1.ScanLine[y];
LineO := BMP2.ScanLine[y];
for x := 0 to BMP1.Width -1 DO
LineO[x]:= LineI[x];
end;
//BMP2.SaveToFile('out.bmp');
imgOut.Picture.Assign(BMP2); //TImage
FINALLY
FreeAndNil(BMP2);
FreeAndNil(BMP1);
END;
end;对于保存的图像,图形编辑器会显示“像素深度/颜色:索引,256色调色板”。
发布于 2020-08-12 23:00:11
值得指出的是,8位位图不一定是灰度级的。
Instead,它是一个具有最多256个条目的“颜色表”的位图,每个像素引用该表中的一个条目。因此,如果像素的值是185,这意味着它应该使用位图“颜色表”中位置185的颜色。因此,8位位图与16位、24位或32位位图的工作方式完全不同,后者没有颜色表,而是在每个像素上具有实际的RGB(A)值。
这种情况下的问题可能是目标像素图没有与源位图相同的颜色表。
实际上,我以前从未使用过8位位图和调色板,但我认为它很简单:
var
s, t: TBitmap;
y: Integer;
sp, tp: PByte;
x: Integer;
begin
s := TBitmap.Create;
try
s.LoadFromFile('C:\Users\Andreas Rejbrand\Desktop\bitmap.bmp');
Assert(s.PixelFormat = pf8bit);
t := TBitmap.Create;
try
t.PixelFormat := pf8bit;
t.SetSize(s.Width, s.Height);
t.Palette := s.Palette; // <-- Let the new image have the same colour table
for y := 0 to s.Height - 1 do
begin
sp := s.ScanLine[y];
tp := t.ScanLine[y];
for x := 0 to s.Width - 1 do
tp[x] := sp[x];
end;
t.SaveToFile('C:\Users\Andreas Rejbrand\Desktop\bitmap2.bmp');
finally
t.Free;
end;
finally
s.Free;
end;https://stackoverflow.com/questions/63378705
复制相似问题