Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Delphi 使用扫描线快速交换红色/蓝色字节的方法_Delphi_Colors_Pixel_Scanline - Fatal编程技术网

Delphi 使用扫描线快速交换红色/蓝色字节的方法

Delphi 使用扫描线快速交换红色/蓝色字节的方法,delphi,colors,pixel,scanline,Delphi,Colors,Pixel,Scanline,目前,我循环Canvas.Pixels[]属性并读取画布上的每个像素以交换红/蓝字节(出于特定原因)。然而,每张图片平均需要2秒,我有8000多张图片需要转换(隔夜)。我知道我可以使用扫描线的方法来更快地完成这项工作,但我对扫描线一无所知-这是一种比我熟悉的低得多的编码水平。最快的方法是什么?我愿意等一段时间,让这段时间过去,但如果我能把这段时间缩短一半或更多,那就太好了 现在,这是我使用的程序: procedure SwapBytes(var Bmp: TBitmap); var X, Y

目前,我循环
Canvas.Pixels[]
属性并读取画布上的每个像素以交换红/蓝字节(出于特定原因)。然而,每张图片平均需要2秒,我有8000多张图片需要转换(隔夜)。我知道我可以使用
扫描线
的方法来更快地完成这项工作,但我对
扫描线
一无所知-这是一种比我熟悉的低得多的编码水平。最快的方法是什么?我愿意等一段时间,让这段时间过去,但如果我能把这段时间缩短一半或更多,那就太好了

现在,这是我使用的程序:

procedure SwapBytes(var Bmp: TBitmap);
var
  X, Y: Integer;
  R, G, B: Byte;
  C: TColor;
begin
  for Y := 0 to Bmp.Height - 1 do begin
    for X := 0 to Bmp.Width - 1 do begin
      C:= Bmp.Canvas.Pixels[X,Y];
      R:= GetRValue(C);
      G:= GetGValue(C);
      B:= GetBValue(C);
      Bmp.Canvas.Pixels[X,Y]:= RGB(B, G, R)
    end;
  end;
end;

补充说明:超过8000张图像的初始转换是我为什么需要这个的第一步。然而,我也将在我们的软件中使用相同的东西,根据需要自动转换现场的任何图像。因此,第三方转换器无法工作,因为我无法将其分发给我们的客户。

我将尝试以下方法。此版本仅适用于24位位图:

procedure SwapRedBluePixels(ABitmap: TBitmap);
var
  X: Integer;
  Y: Integer;
  Red: Byte;
  Pixel: PRGBTriple;
begin
  // check for the bit depth, it must be 24-bit if you use PRGBTriple pointer
  // for line scan; if it wouldn't the iterated line pointers would point to 
  // another place in the memory
  if ABitmap.PixelFormat <> pf24bit then
  begin
    ShowMessage('Your bitmap has color depth different from 24-bit');
    Exit;
  end;
  // iterate through the image vertically
  for Y := 0 to (ABitmap.Height - 1) do
  begin
    // access the line of pixels and get the pointer to the first pixel of 
    // that line
    Pixel := ABitmap.ScanLine[Y];
    // iterate through the scanned line pixels horizontally
    for X := 0 to (ABitmap.Width - 1) do
    begin
      // store the pixel's red channel value
      Red := Pixel.rgbtRed;
      // modify the pixel's red channel value
      Pixel.rgbtRed := Pixel.rgbtBlue;
      // modify the pixel's blue channel value
      Pixel.rgbtBlue := Red;
      // increment to get the next pixel pointer of the scanned line
      Inc(Pixel);
    end;
  end;
end;

我想试试下面的东西。此版本仅适用于24位位图:

procedure SwapRedBluePixels(ABitmap: TBitmap);
var
  X: Integer;
  Y: Integer;
  Red: Byte;
  Pixel: PRGBTriple;
begin
  // check for the bit depth, it must be 24-bit if you use PRGBTriple pointer
  // for line scan; if it wouldn't the iterated line pointers would point to 
  // another place in the memory
  if ABitmap.PixelFormat <> pf24bit then
  begin
    ShowMessage('Your bitmap has color depth different from 24-bit');
    Exit;
  end;
  // iterate through the image vertically
  for Y := 0 to (ABitmap.Height - 1) do
  begin
    // access the line of pixels and get the pointer to the first pixel of 
    // that line
    Pixel := ABitmap.ScanLine[Y];
    // iterate through the scanned line pixels horizontally
    for X := 0 to (ABitmap.Width - 1) do
    begin
      // store the pixel's red channel value
      Red := Pixel.rgbtRed;
      // modify the pixel's red channel value
      Pixel.rgbtRed := Pixel.rgbtBlue;
      // modify the pixel's blue channel value
      Pixel.rgbtBlue := Red;
      // increment to get the next pixel pointer of the scanned line
      Inc(Pixel);
    end;
  end;
end;

中有一个示例显示了如何使用扫描线和访问RGB三元组。哦,你假装正在编写一个实用程序来转换图像。“我有8000多张照片需要转换(一夜之间)”。没关系。@KenWhite谢谢,仔细看了看。。。也是一本关于扫描线的好读物。@Jerry Dodge,如果你破解
jpeg.pas
源代码,你甚至可能不需要它。。。(参见您的另一个问题)中有一个示例显示了使用扫描线和访问RGB三元组。哦,您假装正在编写一个实用程序来转换图像。“我有8000多张照片需要转换(一夜之间)”。没关系。@KenWhite谢谢,仔细看了看。。。也是一本关于扫描线的好读物。@Jerry Dodge,如果你破解
jpeg.pas
源代码,你甚至可能不需要它。。。(见你的另一个问题)看起来不错,谢谢。但是我想到了一件事,
ScanLine
有没有一种方法可以只读取一个通道(1个代表红色,1个代表蓝色),然后交换这些行?@Jerry红色和蓝色没有单独的行。这是一行像素。位图就是这样。@AndriyM事实上不,这就是
ScanLine
的美妙之处,它直接作用于它来自的图像。@AndriyM,它们从未被取出。:)
ScanLine
是一个指针,指向一行像素的实际位图内存。@François,非常好,谢谢。包含alpha的位图不太常见。最常见的是24位。我会稍微修改一下我的答案(我只是从我以前的帖子中抄来的)。看起来不错,谢谢。但是我想到了一件事,
ScanLine
有没有一种方法可以只读取一个通道(1个代表红色,1个代表蓝色),然后交换这些行?@Jerry红色和蓝色没有单独的行。这是一行像素。位图就是这样。@AndriyM事实上不,这就是
ScanLine
的美妙之处,它直接作用于它来自的图像。@AndriyM,它们从未被取出。:)
ScanLine
是一个指针,指向一行像素的实际位图内存。@François,非常好,谢谢。包含alpha的位图不太常见。最常见的是24位。我会稍微修改一下我的答案(我只是从我以前的帖子中抄来的)。