当PixelFormat为pf8bit时,在扫描线中显示错误

时间:2016-06-16 01:24:54

标签: delphi

我使用this code来扫描非常快的位图。

一切都很好当PixelFormat = pf24bit但我的程序只使用256图像颜色,因此PixelFormat = pf8bit。

当PixelFormat = pf8bit时,此代码显示错误。

  var
    w, h: Integer;
    CurrRow, OffSet: Integer;
    x: byte;
    pRed, pGreen, pBlue: PByte;
  begin
    CurrRow := Integer(aBitmap.Scanline[0]);
    OffSet := Integer(aBitmap.Scanline[1]) - CurrRow;
    Result:= False;
    for h := 0 to aBitmap.Height - 1 do
    begin
      for w := 0 to aBitmap.Width - 1 do
      begin
        pBlue := PByte(CurrRow + w * 3);
        pGreen := PByte(CurrRow + w * 3 + 1);
        pRed := PByte(CurrRow + w * 3 + 2);
      end;
      inc(CurrRow, OffSet);
    end;

现在我使用此代码交换颜色但不起作用,因为扫描线不检查颜色

  procedure ReplaceColor(aBitmap: TBitmap; swap1, swap2: TRGBQuad);
  var
  w, h, k: Integer;
  pScanline: pByte;
  Red, Green, Blue, palindex: Byte;
  PalEntries: array[0..255] of TRGBQuad;
begin
  if aBitmap.Palette <> 0 then
    GetPaletteEntries(aBitmap.Palette, 0, 255, PalEntries);

  for h := 0 to aBitmap.Height - 1 do
  begin
    pScanline := pByte(aBitmap.Scanline[h]);

    for w := 0 to aBitmap.Width - 1 do
    begin

           Blue:=  PalEntries[pScanline^].rgbBlue ;
           Red:=  PalEntries[pScanline^].rgbRed ;
           Green:=  PalEntries[pScanline^].rgbGreen ;

          if (Blue = swap1.rgbBlue) and (Red = swap1.rgbRed) and
            (Green = swap1.rgbGreen) then
          begin
            Blue := swap2.rgbBlue;

            Green := swap2.rgbGreen;

            Red := swap2.rgbRed;

          end
          else if (Blue = swap2.rgbBlue) and (Red = swap2.rgbRed) and
            (Green = swap2.rgbGreen) then
          begin
            Blue := swap1.rgbBlue;

            Green := swap1.rgbGreen;

            Red := swap1.rgbRed;

          end;

         Inc(pScanline);
    end;
  end;
end;

1 个答案:

答案 0 :(得分:5)

pf8bit的代码失败,因为它没有写入处理pf8bit。编写它来处理pf24bit

代码期望每个扫描线由包含实际RGB值的width个3字节(24位)像素组成。但在pf8bit中,每个扫描行包含1个字节(8位)像素,这些像素是位图调色板的索引。你完全没有考虑到这一点。

尝试更像这样的东西:

var
  w, h: Integer;
  pScanline: PByte;
  Red, Green, Blue: Byte;
  PalEntries: array[0..255] of TRGBQuad;
begin
  Result := False;

  if aBitmap.Palette <> 0 then
    GetPaletteEntries(aBitmap.Palette, 0, 255, PalEntries);

  for h := 0 to aBitmap.Height - 1 do
  begin
    pScanline := PByte(aBitmap.Scanline[h]);
    for w := 0 to aBitmap.Width - 1 do
    begin
      case aBitmap.PixelFormat of
        pf8Bit: begin
          Blue := PalEntries[pScanline^].rgbBlue;
          Green := PalEntries[pScanline^].rgbGreen;
          Red := PalEntries[pScanline^].rgbRed;
          Inc(pScanline);
        end;
        pf24Bit: begin
          Blue := PRGBTriple(pScanline).rgbtBlue;
          Green := PRGBTriple(pScanline).rgbtGreen;
          Red := PRGBTriple(pScanline).rgbtRed;
          Inc(pScanline, SizeOf(TRGBTriple));
        end;
        // etc for other color depths...
      end;
    end;
  end;
end;

您需要对位图的实际工作方式进行一些研究,特别是与各种颜色深度的扫描线格式有关。

Bitmaps overview