打印本文 打印本文 关闭窗口 关闭窗口
Delphi中如何编写图像解析组件
作者:武汉SEO闵涛  文章来源:敏韬网  点击数2524  更新时间:2009/4/23 18:37:53  文章录入:mintao  责任编辑:mintao
              //这里将顺序调整和R和B值的交换的代码进行了合并
            end;
          end;{end of RT_FORMAT_RGB}
          else
            RasError(''''不支持的文件格式!'''');
        end;{end of 32Bit}

      end;
      else
      begin
        FreeImage;
        RasError(''''不支持的文件格式!'''');
      end;
    end;
  end
  else
    RasError(''''不支持的文件格式!'''');

end;{end with}
end;

{上面的代码中多次出现如下代码:
if (Width mod 2)=1 then
begin
  Position := Position + 1;
end;
这是因为每行的数据都要按字对齐,既每行的数据都要用偶数的字节记录。当每个像素的颜色信息用1字节(8位)或3字节(24位)记录且每行像素数为奇数时,要补齐一个字节。所以这里跳过一个字节。
后面代码中的
if (Width mod 2) = 1 then
begin
  FillByte:=0;
  Stream.Write(FillByte,1);
end;
也是基于同一道理。}

procedure TRASGraphic.SaveToStream(Stream: TStream);
var
  Header: TRASHeader;
  Row8: PByte;
  Row24: PRGBTriple;
  Row32: PRGBQuad;
  FillByte: Byte;
  Y: Integer;
  I: Integer;
  Pal: TMaxLogPalette;
  R,G,B:array[0..255] of Byte;
begin
Header.Magic := $956AA659;
Header.Width := SwapLong(Width);
Header.Height := SwapLong(Height);
Header.RasType := SwapLong(RT_STANDARD);
if (PixelFormat = pf1bit) or (PixelFormat = pf4bit) then
  PixelFormat:=pf8bit
else if (PixelFormat <> pf8bit) and (PixelFormat <> pf24bit) and (PixelFormat <> pf32bit) then
  PixelFormat:=pf24bit;
case PixelFormat of
  pf8bit:
  begin
    Header.Length := SwapLong(Height*(Width+(Width mod 2)));
    Header.Depth := SwapLong(8);
    Header.MapType := SwapLong(RMT_EQUAL_RGB);
    Header.MapLength := SwapLong(3*256);
    Stream.WriteBuffer(Header,SizeOf(Header));
    GetPaletteEntries(Palette, 0, 256, Pal.palPalEntry);
    for I := 0 to 255 do
    begin
      R[I]:=Pal.palPalEntry[I].peRed;
      G[I]:=Pal.palPalEntry[I].peGreen;
      B[I]:=Pal.palPalEntry[I].peBlue;
    end;
    //相关调色板操作的API请查询MSDN
    Stream.WriteBuffer(R,256);
    Stream.WriteBuffer(G,256);
    Stream.WriteBuffer(B,256);
    for Y := 0 to Height-1 do
    begin
      Row8 := ScanLine[Y];
      Stream.WriteBuffer(Row8^,Width);
      if (Width mod 2) = 1 then
      begin
        FillByte:=0;
        Stream.Write(FillByte,1);
      end;
    end;
  end;
  pf32bit:
  begin
    Header.Length := SwapLong(Height*Width*4);
    Header.Depth := SwapLong(32);
    Header.MapType := SwapLong(RMT_NONE);
    Header.MapLength := 0;
    Stream.WriteBuffer(Header,SizeOf(Header));
    for Y := 0 to Height-1 do
    begin
      Row32 := ScanLine[Y];
      for I := 0 to Width-1 do
      begin
        Stream.WriteBuffer(Row32.rgbReserved,1);
        Stream.WriteBuffer(Row32^,3);
        Inc(Row32);
      end;
    end;
  end;
  else
  begin
    Header.Length := SwapLong(Height*Width*3);
    Header.Depth := SwapLong(24);
    Header.MapType := SwapLong(RMT_NONE);
    Header.MapLength := 0;
    Stream.WriteBuffer(Header,SizeOf(Header));
    for Y := 0 to Height-1 do
    begin
      Row24 := ScanLine[Y];
      Stream.WriteBuffer(Row24^,Width*3);
      if (Width mod 2) = 1 then
      begin
        FillByte:=0;
        Stream.Write(FillByte,1);
      end;     
    end;
  end;
end;
//SaveToStream基本上就是LoadFromStream的逆过程。

end;

initialization
  TPicture.RegisterFileFormat(''''RAS'''', ''''Sun RAS'''', TRASGraphic);
finalization
  TPicture.UnregisterGraphicClass(TRASGraphic);

加上这几句代码,一个完整的图像解析组件就完成了。

上一页  [1] [2] [3] 

打印本文 打印本文 关闭窗口 关闭窗口