Sign in Join
Wiki copytemporary

Developing with Graphics/nl: what changed

Revision 2 to 3. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11## Overview
22
33Deze pagina is de start voor lessen die betrekking hebben op het manipuleren van Bitmap en plaatjes in andere formats. Ik ben geen graphics programmeur, dus iedereen die wel veel met graphics programmeert zou ik willen vragen om deze pagina uit te breiden. Gewoon door een link te maken in de sectie hieronder en dan een Wiki-pagina te vullen. Op deze pagina zal ik een klein voorbeeldje geven van hoe je een fading plaatje kunt maken.
44
55## Lessen in het programmeren met Graphics
66
77- Werken met een bitmap
88-
99
1010## Werken met TBitmap
1111
1212Allereerst even een FAQ beantwoorden: Waarom is er (nog) geen ScanLine in Lazarus? Zoals bekend is, moet Lazarus platform onafhankelijk zijn. Dit heeft tot gevolg tot methodes die gebruik maken van de Windows API niet tot de mogelijkheden behoren. ScanLine is zo'n method. ScanLine werkt met DIBs (Device Independant Bitmap) en gebruikt daarbij een aantal functies uit de GDI32.dll.
1313
1414### Een "fading" voorbeeld
1515
1616Zoals gezegd zou ik hier laten zien hoe je een plaatje langzaam kunt laten opkomen. Een echte Nederlandse term for fading ken ik niet echt. In Delphi zou je volgende doen, omdat het pixel voor pixel benaderen van een plaatje erg langzaam is:
1717
18-`type`\
19-`  PRGBTripleArray = ^TRGBTripleArray;`\
20-`  TRGBTripleArray = array[0..32767] of TRGBTriple;`\
18+``` pascal
19+type
20+ PRGBTripleArray = ^TRGBTripleArray;
21+ TRGBTripleArray = array[0..32767] of TRGBTriple;
2122
22-`procedure TForm1.FadeIn(aBitMap: TBitMap);`\
23-`var`\
24-`  Bitmap, BaseBitmap: TBitmap;`\
25-`  Row, BaseRow: PRGBTripleArray;`\
26-`  x, y, step: integer;`\
27-`begin`\
28-`  Bitmap := TBitmap.Create;`\
29-`  try`\
30-`    Bitmap.PixelFormat := pf32bit;  //  or pf24bit`\
31-`    Bitmap.Assign(aBitMap);`\
32-`    BaseBitmap := TBitmap.Create;`\
33-`    try`\
34-`      BaseBitmap.PixelFormat := pf32bit;`\
35-`      BaseBitmap.Assign(Bitmap);`\
36-`      for step := 0 to 32 do begin`\
37-`        for y := 0 to (Bitmap.Height - 1) do begin`\
38-`          BaseRow := BaseBitmap.Scanline[y];`\
39-`          Row := Bitmap.Scanline[y];`\
40-`          for x := 0 to (Bitmap.Width - 1) do begin`\
41-`            Row[x].rgbtRed := (step * BaseRow[x].rgbtRed) shr 5;`\
42-`            Row[x].rgbtGreen := (step * BaseRow[x].rgbtGreen) shr 5; // Fading`\
43-`            Row[x].rgbtBlue := (step * BaseRow[x].rgbtBlue) shr 5;`\
44-`          end;`\
45-`        end;`\
46-`        Form1.Canvas.Draw(0, 0, Bitmap);`\
47-`        InvalidateRect(Form1.Handle, nil, False);`\
48-`        RedrawWindow(Form1.Handle, nil, 0, RDW_UPDATENOW);`\
49-`      end;`\
50-`    finally`\
51-`      BaseBitmap.Free;`\
52-`    end;`\
53-`  finally`\
54-`    Bitmap.Free;`\
55-`  end;`\
56-`end;`
23+procedure TForm1.FadeIn(aBitMap: TBitMap);
24+var
25+ Bitmap, BaseBitmap: TBitmap;
26+ Row, BaseRow: PRGBTripleArray;
27+ x, y, step: integer;
28+begin
29+ Bitmap := TBitmap.Create;
30+ try
31+ Bitmap.PixelFormat := pf32bit; // or pf24bit
32+ Bitmap.Assign(aBitMap);
33+ BaseBitmap := TBitmap.Create;
34+ try
35+ BaseBitmap.PixelFormat := pf32bit;
36+ BaseBitmap.Assign(Bitmap);
37+ for step := 0 to 32 do begin
38+ for y := 0 to (Bitmap.Height - 1) do begin
39+ BaseRow := BaseBitmap.Scanline[y];
40+ Row := Bitmap.Scanline[y];
41+ for x := 0 to (Bitmap.Width - 1) do begin
42+ Row[x].rgbtRed := (step * BaseRow[x].rgbtRed) shr 5;
43+ Row[x].rgbtGreen := (step * BaseRow[x].rgbtGreen) shr 5; // Fading
44+ Row[x].rgbtBlue := (step * BaseRow[x].rgbtBlue) shr 5;
45+ end;
46+ end;
47+ Form1.Canvas.Draw(0, 0, Bitmap);
48+ InvalidateRect(Form1.Handle, nil, False);
49+ RedrawWindow(Form1.Handle, nil, 0, RDW_UPDATENOW);
50+ end;
51+ finally
52+ BaseBitmap.Free;
53+ end;
54+ finally
55+ Bitmap.Free;
56+ end;
57+end;
58+```
5759
5860In Lazarus is er geen ScanLine en moeten we het dus op een andere manier doen. Hoewel deze manier niet zo snel is als de ScanLine methode is die wel veel sneller dan de Pixel\[x,y\] methode in Delphi.
5961
60-`procedure TForm1.FadeIn(ABitMap: TBitMap);`\
61-`var`\
62-`  SrcIntfImg, TempIntfImg: TLazIntfImage;`\
63-`  ImgHandle,ImgMaskHandle: HBitmap;`\
64-`  FadeStep: Integer;`\
65-`  px, py: Integer;`\
66-`  CurColor: TFPColor;`\
67-`  TempBitmap: TBitmap;`\
68-`begin`\
69-`  SrcIntfImg:=TLazIntfImage.Create(0,0);`\
70-`  SrcIntfImg.LoadFromBitmap(ABitmap.Handle,ABitmap.MaskHandle);`\
71-`  TempIntfImg:=TLazIntfImage.Create(0,0);`\
72-`  TempIntfImg.LoadFromBitmap(ABitmap.Handle,ABitmap.MaskHandle);`\
73-`  TempBitmap:=TBitmap.Create;`\
74-`  for FadeStep:=1 to 32 do begin`\
75-`    for py:=0 to SrcIntfImg.Height-1 do begin`\
76-`      for px:=0 to SrcIntfImg.Width-1 do begin`\
77-`        CurColor:=SrcIntfImg.Colors[px,py];`\
78-`        CurColor.Red:=(CurColor.Red*FadeStep) shr 5;`\
79-`        CurColor.Green:=(CurColor.Green*FadeStep) shr 5;`\
80-`        CurColor.Blue:=(CurColor.Blue*FadeStep) shr 5;`\
81-`        TempIntfImg.Colors[px,py]:=CurColor;`\
82-`      end;`\
83-`    end;`\
84-`    TempIntfImg.CreateBitmap(ImgHandle,ImgMaskHandle,false);`\
85-`    TempBitmap.Handle:=ImgHandle;`\
86-`    TempBitmap.MaskHandle:=ImgMaskHandle;`\
87-`    Canvas.Draw(0,0,TempBitmap);`\
88-`  end;`\
89-`  SrcIntfImg.Free;`\
90-`  TempIntfImg.Free;`\
91-`  TempBitmap.Free;`\
92-`end;`
62+``` pascal
63+procedure TForm1.FadeIn(ABitMap: TBitMap);
64+var
65+ SrcIntfImg, TempIntfImg: TLazIntfImage;
66+ ImgHandle,ImgMaskHandle: HBitmap;
67+ FadeStep: Integer;
68+ px, py: Integer;
69+ CurColor: TFPColor;
70+ TempBitmap: TBitmap;
71+begin
72+ SrcIntfImg:=TLazIntfImage.Create(0,0);
73+ SrcIntfImg.LoadFromBitmap(ABitmap.Handle,ABitmap.MaskHandle);
74+ TempIntfImg:=TLazIntfImage.Create(0,0);
75+ TempIntfImg.LoadFromBitmap(ABitmap.Handle,ABitmap.MaskHandle);
76+ TempBitmap:=TBitmap.Create;
77+ for FadeStep:=1 to 32 do begin
78+ for py:=0 to SrcIntfImg.Height-1 do begin
79+ for px:=0 to SrcIntfImg.Width-1 do begin
80+ CurColor:=SrcIntfImg.Colors[px,py];
81+ CurColor.Red:=(CurColor.Red*FadeStep) shr 5;
82+ CurColor.Green:=(CurColor.Green*FadeStep) shr 5;
83+ CurColor.Blue:=(CurColor.Blue*FadeStep) shr 5;
84+ TempIntfImg.Colors[px,py]:=CurColor;
85+ end;
86+ end;
87+ TempIntfImg.CreateBitmap(ImgHandle,ImgMaskHandle,false);
88+ TempBitmap.Handle:=ImgHandle;
89+ TempBitmap.MaskHandle:=ImgMaskHandle;
90+ Canvas.Draw(0,0,TempBitmap);
91+ end;
92+ SrcIntfImg.Free;
93+ TempIntfImg.Free;
94+ TempBitmap.Free;
95+end;
96+```
9397
9498De Lazarus code op deze pagina is overgenomen uit het \$LazarusPath/examples/lazintfimage/fadein1.lpi project. Dus als je echt een vliegende start in het programmeren van plaatjes wilt hebben moet dat voorbeeld eens bestuderen.
9599
96100---
97101*From the Free Pascal wiki, page "Developing with Graphics/nl", as it stood on 2005-10-23 after a change by Sekelsenmat, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history