Escolar Documentos
Profissional Documentos
Cultura Documentos
Cd56 Bruno Imagens1 Listagens
Cd56 Bruno Imagens1 Listagens
2
PosY := 0; IncX := ((Origem.Width - 1) * 65536) div Dest.Width; IncY := ((Origem.Height - 1) * 65536) div Dest.Height; for y := 0 to Dest.Height - 1 do begin PixelAtual:= Dest.ScanLine[y]; PosX := 0; Aux := PosY div 65536; y1 := Origem.ScanLine[Aux]; { Temos que tomar cuidado com a ltima linha para no pegarmos alm do Bitmap } if Aux < Origem.Height - 1 then Inc(Aux); y2 := Origem.ScanLine[Aux]; z2 := (PosY and $FFFF) + 1; iz2 := ((not PosY) and $FFFF) + 1; for x:= 0 to Dest.Width - 1 do begin PosXReal := PosX div 65536; Z := PosX and $FFFF; { Clculo dos pesos } PesoX2Y1 := (iz2*z) shr 16; PesoX1Y1 := iz2 - PesoX2Y1; PesoX2Y2 := (z2*z) shr 16; PesoX1Y2 := z2 - PesoX2Y2; { Vamos fazer as somas ponderadas de cada cor } PixelAtual.b := (y1[PosXReal].b * PesoX1Y1 + y1[PosXReal + 1].b * PesoX2Y1 + y2[PosXReal].b * PesoX1Y2 + y2[PosXReal + 1].b * PesoX2Y2) div 65536; PixelAtual.g := (y1[PosXReal].g * PesoX1Y1 + y1[PosXReal + 1].g * PesoX2Y1 + y2[PosXReal].g * PesoX1Y2 + y2[PosXReal + 1].g * PesoX2Y2) div 65536; PixelAtual.r :=(y1[PosXReal].r * PesoX1Y1 + y1[PosXReal + 1].r * PesoX2Y1 + y2[PosXReal].r * PesoX1Y2 + y2[PosXReal + 1].r * PesoX2Y2) div 65536; Inc(PosX, IncX); { Aqui est um dos macetes de manipulao de imagens: Temos um ponteiro para o pixel atual e simplesmente incrementamo-no } Inc(PixelAtual); end; Inc(PosY, IncY); end; end; procedure StretchBilinear(Origem: TGraphic; Dest: TBitmap; Largura, Altura: integer); var CopiaOrigem: TBitmap; begin if (Origem is TBitmap) then CopiaOrigem := TBitmap(Origem) else begin { Se o nosso Origem no for um TBitmap, temos que copiar para um } CopiaOrigem := TBitmap.Create; CopiaOrigem.Width := Origem.Width; CopiaOrigem.Height := Origem.Height; CopiaOrigem.Canvas.Draw(0, 0, Origem); end; try { Vamos trabalhar somente com 24 bits } CopiaOrigem.PixelFormat := pf24bit; Dest.FreeImage; Dest.PixelFormat := pf24bit; Dest.Width := Largura; Dest.Height := Altura; StretchBilinear24(CopiaOrigem, Dest); Dest.Canvas.Draw(0, 0, Dest); finally if not (Origem is TBitmap) then CopiaOrigem.Free; end; end; end.
3
Listagem 3. Implementao dos mtodos GeraImagem e InvalidaRect
procedure TForm1.GeraImagem(indice: integer); var Tempo: Cardinal; Fat: Double; begin InvalidaRect(indice); { Calcula o tamanho do Bitmap achatado } if indice = 1 then Fat := ScrollBar1.Position / 100 else Fat := ScrollBar2.Position / 100; FRects[indice] := Rect(0, 0, Round(FPicture.Width * Fat), Round(FPicture.Height * Fat)); { Inicializao do clculo do tempo } Tempo := GetTickCount; if Indice = 1 then begin { Stretch normal do Delphi } FBitmaps[1].Width := FRects[1].Right; FBitmaps[1].Height := FRects[1].Bottom; FBitmaps[1].Canvas.StretchDraw(FRects[1], FPicture.Graphic); end else begin { Stretch bilinear } StretchBilinear(FPicture.Graphic, FBitmaps[2], FRects[2].Right, FRects[2].Bottom); end; Tempo := GetTickCount - Tempo; if Indice = 1 then begin Label5.Caption := IntToStr(Tempo) + ' milissegundos'; OffsetRect(FRects[1], Label5.Left, TopoImagem); end else begin Label6.Caption := IntToStr(Tempo) + ' milissegundos'; OffsetRect(FRects[2], Label6.Left, TopoImagem); end; { Posiciona o retnculo do bitmap no formulrio } InvalidaRect(indice); end; procedure TForm1.InvalidaRect(i: integer); begin InvalidateRect(Handle, @FRects[i], True); end;
4
procedure TForm1.FormDestroy(Sender: TObject); begin FBitmaps[1].Free; FBitmaps[2].Free; FPicture.Free; end; procedure TForm1.FormPaint(Sender: TObject); begin Canvas.Draw(FRects[0].Left, FRects[0].Top, FPicture.Graphic); Canvas.Draw(FRects[1].Left, FRects[1].Top, FBitmaps[1]); Canvas.Draw(FRects[2].Left, FRects[2].Top, FBitmaps[2]); end;