From 9dad15b306ffd7460232ddbbe4ffecd7e35bf866 Mon Sep 17 00:00:00 2001 From: Bergmann89 Date: Thu, 21 Nov 2013 21:32:48 +0100 Subject: [PATCH] * JPEG and PNG support with Lazarus Graphics unit --- glBitmap.pas | 201 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 187 insertions(+), 14 deletions(-) diff --git a/glBitmap.pas b/glBitmap.pas index 1a2f7d6..0221b4d 100644 --- a/glBitmap.pas +++ b/glBitmap.pas @@ -252,6 +252,10 @@ unit glBitmap; +// activate to enable Lazarus TPortableNetworkGraphic support +// if you enable this pngImage and libPNG will be ignored +{$DEFINE GLB_LAZ_PNG} + // activate to enable png support with the unit pngimage -> http://pngdelphi.sourceforge.net/ // if you enable pngimage the libPNG will be ignored {.$DEFINE GLB_PNGIMAGE} @@ -262,6 +266,10 @@ unit glBitmap; +// activate to enable Lazarus TJPEGImage support +// if you enable this delphi jpegs and libJPEG will be ignored +{$DEFINE GLB_LAZ_JPEG} + // if you enable delphi jpegs the libJPEG will be ignored {.$DEFINE GLB_DELPHI_JPEG} @@ -306,18 +314,32 @@ unit glBitmap; {$MESSAGE warn 'SDL_image won''t work without SDL. SDL will be activated.'} {$DEFINE GLB_SDL} {$ENDIF} + + {$IFDEF GLB_LAZ_PNG} + {$MESSAGE warn 'The Lazarus TPortableNetworkGraphics will be ignored because you are using SDL_image.'} + {$undef GLB_LAZ_PNG} + {$ENDIF} + {$IFDEF GLB_PNGIMAGE} {$MESSAGE warn 'The unit pngimage will be ignored because you are using SDL_image.'} {$undef GLB_PNGIMAGE} {$ENDIF} + + {$IFDEF GLB_LAZ_JPEG} + {$MESSAGE warn 'The Lazarus TJPEGImage will be ignored because you are using SDL_image.'} + {$undef GLB_LAZ_JPEG} + {$ENDIF} + {$IFDEF GLB_DELPHI_JPEG} {$MESSAGE warn 'The unit JPEG will be ignored because you are using SDL_image.'} {$undef GLB_DELPHI_JPEG} {$ENDIF} + {$IFDEF GLB_LIB_PNG} {$MESSAGE warn 'The library libPNG will be ignored because you are using SDL_image.'} {$undef GLB_LIB_PNG} {$ENDIF} + {$IFDEF GLB_LIB_JPEG} {$MESSAGE warn 'The library libJPEG will be ignored because you are using SDL_image.'} {$undef GLB_LIB_JPEG} @@ -327,6 +349,27 @@ unit glBitmap; {$DEFINE GLB_SUPPORT_JPEG_READ} {$ENDIF} +// Lazarus TPortableNetworkGraphic +{$IFDEF GLB_LAZ_PNG} + {$IFNDEF GLB_LAZARUS} + {$MESSAGE warn 'Lazarus TPortableNetworkGraphic won''t work without Lazarus. Lazarus will be activated.'} + {$DEFINE GLB_LAZARUS} + {$ENDIF} + + {$IFDEF GLB_PNGIMAGE} + {$MESSAGE warn 'The pngimage will be ignored if you are using Lazarus TPortableNetworkGraphic.'} + {$undef GLB_PNGIMAGE} + {$ENDIF} + + {$IFDEF GLB_LIB_PNG} + {$MESSAGE warn 'The library libPNG will be ignored if you are using Lazarus TPortableNetworkGraphic.'} + {$undef GLB_LIB_PNG} + {$ENDIF} + + {$DEFINE GLB_SUPPORT_PNG_READ} + {$DEFINE GLB_SUPPORT_PNG_WRITE} +{$ENDIF} + // PNG Image {$IFDEF GLB_PNGIMAGE} {$IFDEF GLB_LIB_PNG} @@ -344,6 +387,27 @@ unit glBitmap; {$DEFINE GLB_SUPPORT_PNG_WRITE} {$ENDIF} +// Lazarus TJPEGImage +{$IFDEF GLB_LAZ_JPEG} + {$IFNDEF GLB_LAZARUS} + {$MESSAGE warn 'Lazarus TJPEGImage won''t work without Lazarus. Lazarus will be activated.'} + {$DEFINE GLB_LAZARUS} + {$ENDIF} + + {$IFDEF GLB_DELPHI_JPEG} + {$MESSAGE warn 'The Delphi JPEGImage will be ignored if you are using the Lazarus TJPEGImage.'} + {$undef GLB_DELPHI_JPEG} + {$ENDIF} + + {$IFDEF GLB_LIB_JPEG} + {$MESSAGE warn 'The library libJPEG will be ignored if you are using the Lazarus TJPEGImage.'} + {$undef GLB_LIB_JPEG} + {$ENDIF} + + {$DEFINE GLB_SUPPORT_JPEG_READ} + {$DEFINE GLB_SUPPORT_JPEG_WRITE} +{$ENDIF} + // JPEG Image {$IFDEF GLB_DELPHI_JPEG} {$IFDEF GLB_LIB_JPEG} @@ -377,19 +441,19 @@ unit glBitmap; interface uses - {$IFNDEF GLB_NATIVE_OGL} dglOpenGL, {$ENDIF} + {$IFNDEF GLB_NATIVE_OGL} dglOpenGL, {$ENDIF} {$IF DEFINED(GLB_WIN) AND - DEFINED(GLB_NATIVE_OGL)} windows, {$IFEND} + DEFINED(GLB_NATIVE_OGL)} windows, {$IFEND} - {$IFDEF GLB_SDL} SDL, {$ENDIF} - {$IFDEF GLB_LAZARUS} IntfGraphics, GraphType, {$ENDIF} - {$IFDEF GLB_DELPHI} Dialogs, Graphics, {$ENDIF} + {$IFDEF GLB_SDL} SDL, {$ENDIF} + {$IFDEF GLB_LAZARUS} IntfGraphics, GraphType, Graphics, {$ENDIF} + {$IFDEF GLB_DELPHI} Dialogs, Graphics, {$ENDIF} - {$IFDEF GLB_SDL_IMAGE} SDL_image, {$ENDIF} - {$IFDEF GLB_PNGIMAGE} pngimage, {$ENDIF} - {$IFDEF GLB_LIB_PNG} libPNG, {$ENDIF} - {$IFDEF GLB_DELPHI_JPEG} JPEG, {$ENDIF} - {$IFDEF GLB_LIB_JPEG} libJPEG, {$ENDIF} + {$IFDEF GLB_SDL_IMAGE} SDL_image, {$ENDIF} + {$IFDEF GLB_PNGIMAGE} pngimage, {$ENDIF} + {$IFDEF GLB_LIB_PNG} libPNG, {$ENDIF} + {$IFDEF GLB_DELPHI_JPEG} JPEG, {$ENDIF} + {$IFDEF GLB_LIB_JPEG} libJPEG, {$ENDIF} Classes, SysUtils; @@ -5621,11 +5685,45 @@ begin end; {$IFDEF GLB_SUPPORT_PNG_READ} -{$IF DEFINED(GLB_SDL_IMAGE)} +{$IF DEFINED(GLB_LAZ_PNG)} ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //PNG///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function TglBitmap.LoadPNG(const aStream: TStream): Boolean; +var + png: TPortableNetworkGraphic; + intf: TLazIntfImage; + StreamPos: Int64; +begin + result := true; + StreamPos := aStream.Position; + png := TPortableNetworkGraphic.Create; + try try + png.LoadFromStream(aStream); + intf := png.CreateIntfImage; + try try + AssignFromLazIntfImage(intf); + except + result := false; + aStream.Position := StreamPos; + exit; + end; + finally + intf.Free; + end; + except + result := false; + aStream.Position := StreamPos; + exit; + end; + finally + png.Free; + end; +end; + +{$ELSEIF DEFINED(GLB_SDL_IMAGE)} +////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +function TglBitmap.LoadPNG(const aStream: TStream): Boolean; var Surface: PSDL_Surface; RWops: PSDL_RWops; @@ -5866,7 +5964,27 @@ begin end; {$ENDIF} -{$IF DEFINED(GLB_LIB_PNG)} +{$IF DEFINED(GLB_LAZ_PNG)} +////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +procedure TglBitmap.SavePNG(const aStream: TStream); +var + png: TPortableNetworkGraphic; + intf: TLazIntfImage; +begin + png := TPortableNetworkGraphic.Create; + intf := TLazIntfImage.Create(0, 0); + try + if not AssignToLazIntfImage(intf) then + raise EglBitmap.Create('unable to create LazIntfImage from glBitmap'); + png.LoadFromIntfImage(intf); + png.SaveToStream(aStream); + finally + png.Free; + intf.Free; + end; +end; + +{$ELSEIF DEFINED(GLB_LIB_PNG)} ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TglBitmap.SavePNG(const aStream: TStream); var @@ -6156,7 +6274,42 @@ end; {$ENDIF} {$IFDEF GLB_SUPPORT_JPEG_READ} -{$IF DEFINED(GLB_SDL_IMAGE)} +{$IF DEFINED(GLB_LAZ_JPEG)} +////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +function TglBitmap.LoadJPEG(const aStream: TStream): Boolean; +var + jpeg: TJPEGImage; + intf: TLazIntfImage; + StreamPos: Int64; +begin + result := true; + StreamPos := aStream.Position; + jpeg := TJPEGImage.Create; + try try + jpeg.LoadFromStream(aStream); + intf := TLazIntfImage.Create(0, 0); + try try + intf.LoadFromBitmap(jpeg.BitmapHandle, jpeg.MaskHandle); + AssignFromLazIntfImage(intf); + except + result := false; + aStream.Position := StreamPos; + exit; + end; + finally + intf.Free; + end; + except + result := false; + aStream.Position := StreamPos; + exit; + end; + finally + jpeg.Free; + end; +end; + +{$ELSEIF DEFINED(GLB_SDL_IMAGE)} ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function TglBitmap.LoadJPEG(const aStream: TStream): Boolean; var @@ -6334,7 +6487,27 @@ end; {$ENDIF} {$IFDEF GLB_SUPPORT_JPEG_WRITE} -{$IF DEFINED(GLB_LIB_JPEG)} +{$IF DEFINED(GLB_LAZ_JPEG)} +////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +procedure TglBitmap.SaveJPEG(const aStream: TStream); +var + jpeg: TJPEGImage; + intf: TLazIntfImage; +begin + jpeg := TJPEGImage.Create; + intf := TLazIntfImage.Create(0, 0); + try + if not AssignToLazIntfImage(intf) then + raise EglBitmap.Create('unable to create LazIntfImage from glBitmap'); + jpeg.LoadFromIntfImage(intf); + jpeg.SaveToStream(aStream); + finally + intf.Free; + jpeg.Free; + end; +end; + +{$ELSEIF DEFINED(GLB_LIB_JPEG)} ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TglBitmap.SaveJPEG(const aStream: TStream); var