From: Bergmann89 Date: Sat, 16 Nov 2013 21:06:11 +0000 (+0100) Subject: * added LibPNG Support X-Git-Tag: v3.0.0~14 X-Git-Url: https://git.delphigl.com/?a=commitdiff_plain;h=77bababf6dbb72075657d5c12282462a96f7b854;p=glBitmap.git * added LibPNG Support --- diff --git a/glBitmap.pas b/glBitmap.pas index 19dba74..5bfb8e7 100644 --- a/glBitmap.pas +++ b/glBitmap.pas @@ -1,6 +1,10 @@ {*********************************************************** glBitmap by Steffen Xonna aka Lossy eX (2003-2008) http://www.opengl24.de/index.php?cat=header&file=glbitmap + +modified by Delphi OpenGL Community (http://delphigl.com/) + + ------------------------------------------------------------ The contents of this file are used with permission, subject to the Mozilla Public License Version 1.1 (the "License"); you may @@ -230,7 +234,7 @@ unit glBitmap; {.$DEFINE GLB_NATIVE_OGL_DYNAMIC} // activate to enable the support for SDL_surfaces -{$DEFINE GLB_SDL} +{.$DEFINE GLB_SDL} // activate to enable the support for TBitmap from Delphi (not lazarus) {.$DEFINE GLB_DELPHI} @@ -239,7 +243,7 @@ unit glBitmap; ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // activate to enable the support of SDL_image to load files. (READ ONLY) // If you enable SDL_image all other libraries will be ignored! -{$DEFINE GLB_SDL_IMAGE} +{.$DEFINE GLB_SDL_IMAGE} // activate to enable png support with the unit pngimage. You can download it from http://pngdelphi.sourceforge.net/ // if you enable pngimage the libPNG will be ignored @@ -248,7 +252,7 @@ unit glBitmap; // activate to use the libPNG http://www.libpng.org/ // You will need an aditional header. // http://www.opengl24.de/index.php?cat=header&file=libpng -{.$DEFINE GLB_LIB_PNG} +{$DEFINE GLB_LIB_PNG} // if you enable delphi jpegs the libJPEG will be ignored {.$DEFINE GLB_DELPHI_JPEG} @@ -260,7 +264,7 @@ unit glBitmap; ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -// PRIVATE: DO not change anything! ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// PRIVATE: do not change anything! ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Delphi Versions {$IFDEF fpc} @@ -678,12 +682,6 @@ var type //////////////////////////////////////////////////////////////////////////////////////////////////// - EglBitmapException = class(Exception); - EglBitmapSizeToLargeException = class(EglBitmapException); - EglBitmapNonPowerOfTwoException = class(EglBitmapException); - EglBitmapUnsupportedFormat = class(EglBitmapException); - -//////////////////////////////////////////////////////////////////////////////////////////////////// TglBitmapFormat = ( tfEmpty = 0, //must be smallest value! @@ -765,6 +763,14 @@ type nm3x3, nm5x5); + //////////////////////////////////////////////////////////////////////////////////////////////////// + EglBitmapException = class(Exception); + EglBitmapSizeToLargeException = class(EglBitmapException); + EglBitmapNonPowerOfTwoException = class(EglBitmapException); + EglBitmapUnsupportedFormat = class(EglBitmapException) + constructor Create(const aFormat: TglBitmapFormat); + end; + //////////////////////////////////////////////////////////////////////////////////////////////////// TglBitmapColorRec = packed record case Integer of @@ -1126,7 +1132,7 @@ implementation *) uses - Math, syncobjs; + Math, syncobjs, typinfo; type //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -1651,6 +1657,12 @@ var FormatDescriptorCS: TCriticalSection; FormatDescriptors: array[TglBitmapFormat] of TFormatDescriptor; +////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +constructor EglBitmapUnsupportedFormat.Create(const aFormat: TglBitmapFormat); +begin + inherited Create(GetEnumName(TypeInfo(TglBitmapFormat), Integer(aFormat))); +end; + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function glBitmapPosition(X, Y: Integer): TglBitmapPixelPosition; begin @@ -1757,19 +1769,15 @@ begin tfS3tcDtx1RGBA, tfS3tcDtx3RGBA, tfS3tcDtx5RGBA]) then result := result + [ftDDS]; - (* TODO {$IFDEF GLB_SUPPORT_PNG_WRITE} if aFormat in [ - tfAlpha4, tfAlpha8, tfAlpha12, tfAlpha16, - tfLuminance4, tfLuminance8, tfLuminance12, tfLuminance16, - tfuminance4Alpha4, tfLuminance6Alpha2, tfLuminance8Alpha8, tfLuminance12Alpha4, tfLuminance12Alpha12, tfLuminance16Alpha16, - tfR3G3B2, tfRGB4, tfRGB5, tfRGB8, tfRGB10, tfRGB12, tfRGB16, - tfRGBA2, tfRGBA4, tfRGB5A1, tfRGBA8, tfRGB10A2, tfRGBA12, tfRGBA16, - tfDepth16, tfDepth24, tfDepth32] - then + tfAlpha8, tfLuminance8, tfLuminance8Alpha8, + tfRGB8, tfRGBA8, + tfBGR8, tfBGRA8] then result := result + [ftPNG]; {$ENDIF} +(* TODO {$IFDEF GLB_SUPPORT_JPEG_WRITE} if Format in [ tfAlpha4, tfAlpha8, tfAlpha12, tfAlpha16, @@ -1779,16 +1787,6 @@ begin then result := result + [ftJPEG]; {$ENDIF} - - if aFormat in [ - tfAlpha4, tfAlpha8, tfAlpha12, tfAlpha16, - tfLuminance4, tfLuminance8, tfLuminance12, tfLuminance16, - tfuminance4Alpha4, tfLuminance6Alpha2, tfLuminance8Alpha8, tfLuminance12Alpha4, tfLuminance12Alpha12, tfLuminance16Alpha16, - tfR3G3B2, tfRGB4, tfRGB5, tfRGB8, tfRGB10, tfRGB12, tfRGB16, - tfRGBA2, tfRGBA4, tfRGB5A1, tfRGBA8, tfRGB10A2, tfRGBA12, tfRGBA16, - tfDepth16, tfDepth24, tfDepth32] - then - result := result + [ftDDS, ftTGA, ftBMP]; *) end; @@ -4220,7 +4218,7 @@ begin if fFormat = aValue then exit; if TFormatDescriptor.Get(Format).PixelSize <> TFormatDescriptor.Get(aValue).PixelSize then - raise EglBitmapUnsupportedFormat.Create('SetFormat'); + raise EglBitmapUnsupportedFormat.Create(Format); SetDataPointer(Data, aValue, Width, Height); end; @@ -4491,7 +4489,7 @@ procedure TglBitmap.SaveToStream(const aStream: TStream; const aFileType: TglBit begin case aFileType of {$IFDEF GLB_SUPPORT_PNG_WRITE} - ftPNG: SavePng(aStream); + ftPNG: SavePNG(aStream); {$ENDIF} {$IFDEF GLB_SUPPORT_JPEG_WRITE} ftJPEG: SaveJPEG(aStream); @@ -4609,7 +4607,7 @@ begin FormatDesc := TFormatDescriptor.Get(Format); if FormatDesc.IsCompressed then - raise EglBitmapUnsupportedFormat.Create('AssignToSurface'); + raise EglBitmapUnsupportedFormat.Create(Format); if Assigned(Data) then begin case Trunc(FormatDesc.PixelSize) of @@ -4618,7 +4616,7 @@ begin 3: TempDepth := 24; 4: TempDepth := 32; else - raise EglBitmapUnsupportedFormat.Create('AssignToSurface'); + raise EglBitmapUnsupportedFormat.Create(Format); end; aSurface := SDL_CreateRGBSurface(SDL_SWSURFACE, Width, Height, TempDepth, @@ -4952,10 +4950,8 @@ end; ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function TglBitmap.AddAlphaFromFunc(const aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean; begin - (* TODO - if not FormatIsUncompressed(InternalFormat) then - raise EglBitmapUnsupportedFormatFormat.Create('AddAlphaFromFunc - ' + UNSUPPORTED_FORMAT); - *) + if TFormatDescriptor.Get(Format).IsCompressed then + raise EglBitmapUnsupportedFormat.Create(Format); result := AddFunc(Self, aFunc, false, TFormatDescriptor.Get(Format).WithAlpha, aArgs); end; @@ -5132,7 +5128,7 @@ begin FormatDesc := TFormatDescriptor.Get(Format); if Assigned(Data) then begin if FormatDesc.IsCompressed or not FormatDesc.HasAlpha then - raise EglBitmapUnsupportedFormat.Create('RemoveAlpha'); + raise EglBitmapUnsupportedFormat.Create(Format); result := ConvertTo(FormatDesc.WithoutAlpha); end; end; @@ -5533,7 +5529,7 @@ var png_info: png_infop; TempHeight, TempWidth: Integer; - Format: TglBitmapInternalFormat; + Format: TglBitmapFormat; png_data: pByte; png_rows: array of pByte; @@ -5546,9 +5542,9 @@ begin try // signature - StreamPos := Stream.Position; - Stream.Read(signature, 8); - Stream.Position := StreamPos; + StreamPos := aStream.Position; + aStream.Read(signature, 8); + aStream.Position := StreamPos; if png_check_sig(@signature, 8) <> 0 then begin // png read struct @@ -5564,7 +5560,7 @@ begin end; // set read callback - png_set_read_fn(png, stream, glBitmap_libPNG_read_func); + png_set_read_fn(png, aStream, glBitmap_libPNG_read_func); // read informations png_read_info(png, png_info); @@ -5742,27 +5738,30 @@ var LineSize: Integer; ColorType: Integer; Row: Integer; + FormatDesc: TFormatDescriptor; begin - if not (ftPNG in FormatGetSupportedFiles (InternalFormat)) then - raise EglBitmapUnsupportedInternalFormat.Create('SavePng - ' + UNSUPPORTED_INTERNAL_FORMAT); + if not (ftPNG in FormatGetSupportedFiles(Format)) then + raise EglBitmapUnsupportedFormat.Create(Format); if not init_libPNG then - raise Exception.Create('SavePNG - unable to initialize libPNG.'); + raise Exception.Create('unable to initialize libPNG.'); try - case FInternalFormat of - ifAlpha, ifLuminance, ifDepth8: + case Format of + tfAlpha8, tfLuminance8: ColorType := PNG_COLOR_TYPE_GRAY; - ifLuminanceAlpha: + tfLuminance8Alpha8: ColorType := PNG_COLOR_TYPE_GRAY_ALPHA; - ifBGR8, ifRGB8: + tfBGR8, tfRGB8: ColorType := PNG_COLOR_TYPE_RGB; - ifBGRA8, ifRGBA8: + tfBGRA8, tfRGBA8: ColorType := PNG_COLOR_TYPE_RGBA; else - raise EglBitmapUnsupportedInternalFormat.Create('SavePng - ' + UNSUPPORTED_INTERNAL_FORMAT); + raise EglBitmapUnsupportedFormat.Create(Format); end; - LineSize := Trunc(FormatGetSize(FInternalFormat) * Width); + + FormatDesc := TFormatDescriptor.Get(Format); + LineSize := FormatDesc.GetSize(Width, 1); // creating array for scanline SetLength(png_rows, Height); @@ -5785,12 +5784,12 @@ begin end; // set read callback - png_set_write_fn(png, stream, glBitmap_libPNG_write_func, nil); + png_set_write_fn(png, aStream, glBitmap_libPNG_write_func, nil); // set compression png_set_compression_level(png, 6); - if InternalFormat in [ifBGR8, ifBGRA8] then + if Format in [tfBGR8, tfBGRA8] then png_set_bgr(png); png_set_IHDR(png, png_info, Width, Height, 8, ColorType, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); @@ -6559,7 +6558,7 @@ var begin if not (ftBMP in FormatGetSupportedFiles(Format)) then - raise EglBitmapUnsupportedFormat.Create('SaveBMP'); + raise EglBitmapUnsupportedFormat.Create(Format); Converter := nil; FormatDesc := TFormatDescriptor.Get(Format); @@ -6624,7 +6623,7 @@ begin Info.biCompression := BMP_COMP_BITFIELDS; end; else - raise EglBitmapUnsupportedFormat.Create('SaveBMP - ' + UNSUPPORTED_FORMAT); + raise EglBitmapUnsupportedFormat.Create(Format); end; Info.biXPelsPerMeter := 2835; Info.biYPelsPerMeter := 2835; @@ -7020,7 +7019,7 @@ var Converter: TFormatDescriptor; begin if not (ftTGA in FormatGetSupportedFiles(Format)) then - raise EglBitmapUnsupportedFormat.Create('SaveTGA'); + raise EglBitmapUnsupportedFormat.Create(Format); //prepare header FillChar(Header{%H-}, SizeOf(Header), 0); @@ -7336,7 +7335,7 @@ var FormatDesc: TFormatDescriptor; begin if not (ftDDS in FormatGetSupportedFiles(Format)) then - raise EglBitmapUnsupportedFormat.Create('SaveDDS'); + raise EglBitmapUnsupportedFormat.Create(Format); FormatDesc := TFormatDescriptor.Get(Format); @@ -7479,7 +7478,7 @@ var begin FormatDesc := TFormatDescriptor.Get(Format); if FormatDesc.IsCompressed then - raise EglBitmapUnsupportedFormat.Create('TglBitmap2D.GrabScreen'); + raise EglBitmapUnsupportedFormat.Create(Format); w := aRight - aLeft; h := aBottom - aTop; @@ -7729,10 +7728,8 @@ var end; begin - (* TODO Compression - if not FormatIsUncompressed(InternalFormat) then - raise EglBitmapUnsupportedFormatFormat.Create('TglBitmap2D.ToNormalMap - ' + UNSUPPORTED_FORMAT); - *) + if TFormatDescriptor.Get(Format).IsCompressed then + raise EglBitmapUnsupportedFormat.Create(Format); if aScale > 100 then Rec.Scale := 100