如何通过 ScanLine 复制灰度位图
How to copy a gray-scale bitmap via ScanLine
我想将像素从 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;
对于保存的图像,图形编辑器显示“像素 depth/colors:索引,256 调色板”。
可能值得指出的是,8 位位图不一定是灰度的。
Instead,是一张“颜色table”的位图,最多由256个条目组成,每个像素代表这个table中的一个条目。因此,如果一个像素的值为 185,这意味着它应该使用位图“颜色 table”中位置 185 处的颜色。因此,与 16、24 或 32 位位图相比,8 位位图的工作方式完全不同,后者没有颜色 table,而是每个像素都有实际的 RGB(A) 值。
您遇到的问题可能是目标像素图的颜色 table 与源位图不同。
实际上我以前从未使用过 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;
我想将像素从 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;
对于保存的图像,图形编辑器显示“像素 depth/colors:索引,256 调色板”。
可能值得指出的是,8 位位图不一定是灰度的。
Instead,是一张“颜色table”的位图,最多由256个条目组成,每个像素代表这个table中的一个条目。因此,如果一个像素的值为 185,这意味着它应该使用位图“颜色 table”中位置 185 处的颜色。因此,与 16、24 或 32 位位图相比,8 位位图的工作方式完全不同,后者没有颜色 table,而是每个像素都有实际的 RGB(A) 值。
您遇到的问题可能是目标像素图的颜色 table 与源位图不同。
实际上我以前从未使用过 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;