PDA

View Full Version : Show a bitmap on a TPaintBox



Ñuño Martínez
25-09-2008, 03:28 PM
Well, I'm using Lazarus but I think the question is Ok here.

I did a program to manage data files that include bitmaps. I use a TPaintBox to show these images on the window but it's too slow because I'm using Allegro.pas not the TImage widget to load and manage the bitmap images. The function that manages the "OnPaint" message of the TPaintBox is similar than this (sort of pseudocode):
procedure TForm1.PaintBox1Paint(Sender: TObject);
VAR
X, Y: INTEGER;
ImageBox: TImageBox;
begin
ImageBox := Sender;
FOR Y := 0 TO (Image^.h - 1) DO
FOR X := 0 TO (Image^.w - 1) DO
ImageBox.canvas.drawpixel (X, Y, al_getpixel (Image, X, Y));
end;
That is, I copy the image pixel-to-pixel.

As I've said this is too slow and I'm wondering if there's a faster way to do it. The "Image" object also has a pointer to the raw data named "data" that stores the color information in 8, 15, 16, 24 or 32 bits-per-pixel.

May be I can do something as:procedure TForm1.PaintBox1Paint(Sender: TObject);
VAR
ImageBox: TImageBox;
Copy: AL_BITMAPptr;
begin
Copy := copy_bitmap_to_32bpp (Image);
ImageBox := Sender;
ImageBox.Canvas.DrawRawImage (Copy^w, Copy^.h, Copy^.data); { Is there such method? }
al_destroy_bitmap (Copy);
end;
Where copy_bitmap_to_32bpp creates a copy of "Image" in 32bpp for sure and the "DrawRawImgage" method is an optimised function that draws a "raw 32bpp bitmap" in the TPaintBox' canvas.

How can I do that?

chronozphere
25-09-2008, 04:33 PM
I think it's faster to create a TBitmap, and copy your data into it (using scanline), and draw the bitmap onto the paintbox by calling (Canvas.Draw(X, Y, *Bitmap Here*), if i recall correctly).

I'm sure you can find plenty examples using TBitmap's scanline functionality. Check google and the delphi help. ;)

Good luck!

cronodragon
25-09-2008, 04:43 PM
I'll save you some googling time. This reference is very cool, check it out:

http://www.efg2.com/Lab/ImageProcessing/Scanline.htm

Ñuño Martínez
26-09-2008, 09:02 AM
Thanks. I'll read it.

[edit] Seems as Lazarus' TBitmap hasn't the scanline property or I can't find it. :(

cronodragon
26-09-2008, 02:03 PM
That's right, you have to use TLazInfImage instead. You can keep using TBitmap, but when you need the Scanline, just create a copy in a TLazInfImage and work on it... I don't understand why they made it that way, but I use it in my own engine and works fine. You can see more about that in:

http://wiki.lazarus.freepascal.org/Developing_with_Graphics/es
http://www.lazarus.freepascal.org/lcl_doc/TLazIntfImage.html

Saludos!! :D

Ñuño Martínez
26-09-2008, 05:22 PM
I see. That's weird but may be they do this way to keep compatibility with the Free Pascal RTL. :?

Gracias. :)

Ñuño Martínez
26-09-2008, 05:22 PM
I see. That's weird but may be they do this way to keep compatibility with the Free Pascal RTL. :?

Gracias. :)

Ñuño Martínez
26-09-2008, 05:23 PM
Something was wrong with my connexion :?

I see. That's weird but may be they do this way to keep compatibility with the Free Pascal RTL. :?

Gracias. :)