[fpc-pascal] Save image into BMP or JPEG using Free Pascal (Save screenshot of OpenGL scene)
Michalis Kamburelis
michalis.kambi at gmail.com
Wed Feb 20 06:35:56 CET 2008
Jakub Marian wrote:
> Hello,
>
> I need to save data stored in memory into .bmp or (which would be the better
> variant) into .jpg file. Actually what I want to do is to save the OpenGL
> screen (probably using glReadPixels() function) into image. I've downloaded
> PasJPEG, but it doesn't seem to be working. When I try to compile example
> program for PasJPEG, I get: 'Error: Wrong number of parameters specified for
> call to "error_exit"'
>
> Do you know any efficient way to save glReadPixels into .bmp or .jpeg file?
> It would be very helpful for me.
>
A little shameless plug, but I have implemented in my engine exactly
what you're looking for. See [http://vrmlengine.sourceforge.net/], in
engine sources look in kambi_vrml_game_engine/images/ subdirectory.
Images.pas unit there is used to load/save images to various formats,
including .bmp and .jpeg. BMP handling is done in images_bmp.inc file,
and JPEG is inside images_jpeg.inc. JPEG support is quite a hack
(although works successfully since a long time :) ) --- I use PasJPEG
(see KambiPasJpeg unit) to just convert betweeen bmp<->jpeg, this allows
me to save/load jpeg files by saving/loading bmp files under the hood.
The image format in the memory is clearly specified, as the main use of
this unit is to cooperate with OpenGL (for loading images, textures,
saving screenshots etc.). See TImage class comments, basically RawPixels
is simply a pointer to an array of Width * Height pixel values, and each
descendant of TImage specifies the exact layout of "pixel" --- e.g. for
TRGBImage a pixel is just 3 bytes, for red/green/blue.
The way to grab screenshot to TRGBImage is inside
kambi_vrml_game_engine/opengl/glimages.pas unit, see SaveScreen_noflush.
Yeah, it's basically just a glReadPixels call, with some safeguarding
(setting OpenGL read buffer and pixel store pack parameters).
Hope this will help,
Michalis
More information about the fpc-pascal
mailing list