| @@ -1,6 +1,10 @@ | |||||
| {*********************************************************** | {*********************************************************** | ||||
| glBitmap by Steffen Xonna aka Lossy eX (2003-2008) | glBitmap by Steffen Xonna aka Lossy eX (2003-2008) | ||||
| http://www.opengl24.de/index.php?cat=header&file=glbitmap | 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 contents of this file are used with permission, subject to | ||||
| the Mozilla Public License Version 1.1 (the "License"); you may | the Mozilla Public License Version 1.1 (the "License"); you may | ||||
| @@ -230,7 +234,7 @@ unit glBitmap; | |||||
| {.$DEFINE GLB_NATIVE_OGL_DYNAMIC} | {.$DEFINE GLB_NATIVE_OGL_DYNAMIC} | ||||
| // activate to enable the support for SDL_surfaces | // 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) | // activate to enable the support for TBitmap from Delphi (not lazarus) | ||||
| {.$DEFINE GLB_DELPHI} | {.$DEFINE GLB_DELPHI} | ||||
| @@ -239,7 +243,7 @@ unit glBitmap; | |||||
| ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||||
| // activate to enable the support of SDL_image to load files. (READ ONLY) | // activate to enable the support of SDL_image to load files. (READ ONLY) | ||||
| // If you enable SDL_image all other libraries will be ignored! | // 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/ | // 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 | // if you enable pngimage the libPNG will be ignored | ||||
| @@ -248,7 +252,7 @@ unit glBitmap; | |||||
| // activate to use the libPNG http://www.libpng.org/ | // activate to use the libPNG http://www.libpng.org/ | ||||
| // You will need an aditional header. | // You will need an aditional header. | ||||
| // http://www.opengl24.de/index.php?cat=header&file=libpng | // 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 | // if you enable delphi jpegs the libJPEG will be ignored | ||||
| {.$DEFINE GLB_DELPHI_JPEG} | {.$DEFINE GLB_DELPHI_JPEG} | ||||
| @@ -260,7 +264,7 @@ unit glBitmap; | |||||
| ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||||
| // PRIVATE: DO not change anything! ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |||||
| // PRIVATE: do not change anything! ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |||||
| ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||||
| // Delphi Versions | // Delphi Versions | ||||
| {$IFDEF fpc} | {$IFDEF fpc} | ||||
| @@ -677,12 +681,6 @@ var | |||||
| {$ENDIF} | {$ENDIF} | ||||
| type | type | ||||
| //////////////////////////////////////////////////////////////////////////////////////////////////// | |||||
| EglBitmapException = class(Exception); | |||||
| EglBitmapSizeToLargeException = class(EglBitmapException); | |||||
| EglBitmapNonPowerOfTwoException = class(EglBitmapException); | |||||
| EglBitmapUnsupportedFormat = class(EglBitmapException); | |||||
| //////////////////////////////////////////////////////////////////////////////////////////////////// | //////////////////////////////////////////////////////////////////////////////////////////////////// | ||||
| TglBitmapFormat = ( | TglBitmapFormat = ( | ||||
| tfEmpty = 0, //must be smallest value! | tfEmpty = 0, //must be smallest value! | ||||
| @@ -765,6 +763,14 @@ type | |||||
| nm3x3, | nm3x3, | ||||
| nm5x5); | nm5x5); | ||||
| //////////////////////////////////////////////////////////////////////////////////////////////////// | |||||
| EglBitmapException = class(Exception); | |||||
| EglBitmapSizeToLargeException = class(EglBitmapException); | |||||
| EglBitmapNonPowerOfTwoException = class(EglBitmapException); | |||||
| EglBitmapUnsupportedFormat = class(EglBitmapException) | |||||
| constructor Create(const aFormat: TglBitmapFormat); | |||||
| end; | |||||
| //////////////////////////////////////////////////////////////////////////////////////////////////// | //////////////////////////////////////////////////////////////////////////////////////////////////// | ||||
| TglBitmapColorRec = packed record | TglBitmapColorRec = packed record | ||||
| case Integer of | case Integer of | ||||
| @@ -1126,7 +1132,7 @@ implementation | |||||
| *) | *) | ||||
| uses | uses | ||||
| Math, syncobjs; | |||||
| Math, syncobjs, typinfo; | |||||
| type | type | ||||
| //////////////////////////////////////////////////////////////////////////////////////////////////// | //////////////////////////////////////////////////////////////////////////////////////////////////// | ||||
| @@ -1651,6 +1657,12 @@ var | |||||
| FormatDescriptorCS: TCriticalSection; | FormatDescriptorCS: TCriticalSection; | ||||
| FormatDescriptors: array[TglBitmapFormat] of TFormatDescriptor; | 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; | function glBitmapPosition(X, Y: Integer): TglBitmapPixelPosition; | ||||
| begin | begin | ||||
| @@ -1757,19 +1769,15 @@ begin | |||||
| tfS3tcDtx1RGBA, tfS3tcDtx3RGBA, tfS3tcDtx5RGBA]) then | tfS3tcDtx1RGBA, tfS3tcDtx3RGBA, tfS3tcDtx5RGBA]) then | ||||
| result := result + [ftDDS]; | result := result + [ftDDS]; | ||||
| (* TODO | |||||
| {$IFDEF GLB_SUPPORT_PNG_WRITE} | {$IFDEF GLB_SUPPORT_PNG_WRITE} | ||||
| if aFormat in [ | 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]; | result := result + [ftPNG]; | ||||
| {$ENDIF} | {$ENDIF} | ||||
| (* TODO | |||||
| {$IFDEF GLB_SUPPORT_JPEG_WRITE} | {$IFDEF GLB_SUPPORT_JPEG_WRITE} | ||||
| if Format in [ | if Format in [ | ||||
| tfAlpha4, tfAlpha8, tfAlpha12, tfAlpha16, | tfAlpha4, tfAlpha8, tfAlpha12, tfAlpha16, | ||||
| @@ -1779,16 +1787,6 @@ begin | |||||
| then | then | ||||
| result := result + [ftJPEG]; | result := result + [ftJPEG]; | ||||
| {$ENDIF} | {$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; | end; | ||||
| @@ -4220,7 +4218,7 @@ begin | |||||
| if fFormat = aValue then | if fFormat = aValue then | ||||
| exit; | exit; | ||||
| if TFormatDescriptor.Get(Format).PixelSize <> TFormatDescriptor.Get(aValue).PixelSize then | if TFormatDescriptor.Get(Format).PixelSize <> TFormatDescriptor.Get(aValue).PixelSize then | ||||
| raise EglBitmapUnsupportedFormat.Create('SetFormat'); | |||||
| raise EglBitmapUnsupportedFormat.Create(Format); | |||||
| SetDataPointer(Data, aValue, Width, Height); | SetDataPointer(Data, aValue, Width, Height); | ||||
| end; | end; | ||||
| @@ -4491,7 +4489,7 @@ procedure TglBitmap.SaveToStream(const aStream: TStream; const aFileType: TglBit | |||||
| begin | begin | ||||
| case aFileType of | case aFileType of | ||||
| {$IFDEF GLB_SUPPORT_PNG_WRITE} | {$IFDEF GLB_SUPPORT_PNG_WRITE} | ||||
| ftPNG: SavePng(aStream); | |||||
| ftPNG: SavePNG(aStream); | |||||
| {$ENDIF} | {$ENDIF} | ||||
| {$IFDEF GLB_SUPPORT_JPEG_WRITE} | {$IFDEF GLB_SUPPORT_JPEG_WRITE} | ||||
| ftJPEG: SaveJPEG(aStream); | ftJPEG: SaveJPEG(aStream); | ||||
| @@ -4609,7 +4607,7 @@ begin | |||||
| FormatDesc := TFormatDescriptor.Get(Format); | FormatDesc := TFormatDescriptor.Get(Format); | ||||
| if FormatDesc.IsCompressed then | if FormatDesc.IsCompressed then | ||||
| raise EglBitmapUnsupportedFormat.Create('AssignToSurface'); | |||||
| raise EglBitmapUnsupportedFormat.Create(Format); | |||||
| if Assigned(Data) then begin | if Assigned(Data) then begin | ||||
| case Trunc(FormatDesc.PixelSize) of | case Trunc(FormatDesc.PixelSize) of | ||||
| @@ -4618,7 +4616,7 @@ begin | |||||
| 3: TempDepth := 24; | 3: TempDepth := 24; | ||||
| 4: TempDepth := 32; | 4: TempDepth := 32; | ||||
| else | else | ||||
| raise EglBitmapUnsupportedFormat.Create('AssignToSurface'); | |||||
| raise EglBitmapUnsupportedFormat.Create(Format); | |||||
| end; | end; | ||||
| aSurface := SDL_CreateRGBSurface(SDL_SWSURFACE, Width, Height, TempDepth, | aSurface := SDL_CreateRGBSurface(SDL_SWSURFACE, Width, Height, TempDepth, | ||||
| @@ -4952,10 +4950,8 @@ end; | |||||
| ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||||
| function TglBitmap.AddAlphaFromFunc(const aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean; | function TglBitmap.AddAlphaFromFunc(const aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean; | ||||
| begin | 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); | result := AddFunc(Self, aFunc, false, TFormatDescriptor.Get(Format).WithAlpha, aArgs); | ||||
| end; | end; | ||||
| @@ -5132,7 +5128,7 @@ begin | |||||
| FormatDesc := TFormatDescriptor.Get(Format); | FormatDesc := TFormatDescriptor.Get(Format); | ||||
| if Assigned(Data) then begin | if Assigned(Data) then begin | ||||
| if FormatDesc.IsCompressed or not FormatDesc.HasAlpha then | if FormatDesc.IsCompressed or not FormatDesc.HasAlpha then | ||||
| raise EglBitmapUnsupportedFormat.Create('RemoveAlpha'); | |||||
| raise EglBitmapUnsupportedFormat.Create(Format); | |||||
| result := ConvertTo(FormatDesc.WithoutAlpha); | result := ConvertTo(FormatDesc.WithoutAlpha); | ||||
| end; | end; | ||||
| end; | end; | ||||
| @@ -5533,7 +5529,7 @@ var | |||||
| png_info: png_infop; | png_info: png_infop; | ||||
| TempHeight, TempWidth: Integer; | TempHeight, TempWidth: Integer; | ||||
| Format: TglBitmapInternalFormat; | |||||
| Format: TglBitmapFormat; | |||||
| png_data: pByte; | png_data: pByte; | ||||
| png_rows: array of pByte; | png_rows: array of pByte; | ||||
| @@ -5546,9 +5542,9 @@ begin | |||||
| try | try | ||||
| // signature | // 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 | if png_check_sig(@signature, 8) <> 0 then begin | ||||
| // png read struct | // png read struct | ||||
| @@ -5564,7 +5560,7 @@ begin | |||||
| end; | end; | ||||
| // set read callback | // 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 | // read informations | ||||
| png_read_info(png, png_info); | png_read_info(png, png_info); | ||||
| @@ -5742,27 +5738,30 @@ var | |||||
| LineSize: Integer; | LineSize: Integer; | ||||
| ColorType: Integer; | ColorType: Integer; | ||||
| Row: Integer; | Row: Integer; | ||||
| FormatDesc: TFormatDescriptor; | |||||
| begin | 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 | if not init_libPNG then | ||||
| raise Exception.Create('SavePNG - unable to initialize libPNG.'); | |||||
| raise Exception.Create('unable to initialize libPNG.'); | |||||
| try | try | ||||
| case FInternalFormat of | |||||
| ifAlpha, ifLuminance, ifDepth8: | |||||
| case Format of | |||||
| tfAlpha8, tfLuminance8: | |||||
| ColorType := PNG_COLOR_TYPE_GRAY; | ColorType := PNG_COLOR_TYPE_GRAY; | ||||
| ifLuminanceAlpha: | |||||
| tfLuminance8Alpha8: | |||||
| ColorType := PNG_COLOR_TYPE_GRAY_ALPHA; | ColorType := PNG_COLOR_TYPE_GRAY_ALPHA; | ||||
| ifBGR8, ifRGB8: | |||||
| tfBGR8, tfRGB8: | |||||
| ColorType := PNG_COLOR_TYPE_RGB; | ColorType := PNG_COLOR_TYPE_RGB; | ||||
| ifBGRA8, ifRGBA8: | |||||
| tfBGRA8, tfRGBA8: | |||||
| ColorType := PNG_COLOR_TYPE_RGBA; | ColorType := PNG_COLOR_TYPE_RGBA; | ||||
| else | else | ||||
| raise EglBitmapUnsupportedInternalFormat.Create('SavePng - ' + UNSUPPORTED_INTERNAL_FORMAT); | |||||
| raise EglBitmapUnsupportedFormat.Create(Format); | |||||
| end; | end; | ||||
| LineSize := Trunc(FormatGetSize(FInternalFormat) * Width); | |||||
| FormatDesc := TFormatDescriptor.Get(Format); | |||||
| LineSize := FormatDesc.GetSize(Width, 1); | |||||
| // creating array for scanline | // creating array for scanline | ||||
| SetLength(png_rows, Height); | SetLength(png_rows, Height); | ||||
| @@ -5785,12 +5784,12 @@ begin | |||||
| end; | end; | ||||
| // set read callback | // 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 | // set compression | ||||
| png_set_compression_level(png, 6); | png_set_compression_level(png, 6); | ||||
| if InternalFormat in [ifBGR8, ifBGRA8] then | |||||
| if Format in [tfBGR8, tfBGRA8] then | |||||
| png_set_bgr(png); | 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); | 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 | begin | ||||
| if not (ftBMP in FormatGetSupportedFiles(Format)) then | if not (ftBMP in FormatGetSupportedFiles(Format)) then | ||||
| raise EglBitmapUnsupportedFormat.Create('SaveBMP'); | |||||
| raise EglBitmapUnsupportedFormat.Create(Format); | |||||
| Converter := nil; | Converter := nil; | ||||
| FormatDesc := TFormatDescriptor.Get(Format); | FormatDesc := TFormatDescriptor.Get(Format); | ||||
| @@ -6624,7 +6623,7 @@ begin | |||||
| Info.biCompression := BMP_COMP_BITFIELDS; | Info.biCompression := BMP_COMP_BITFIELDS; | ||||
| end; | end; | ||||
| else | else | ||||
| raise EglBitmapUnsupportedFormat.Create('SaveBMP - ' + UNSUPPORTED_FORMAT); | |||||
| raise EglBitmapUnsupportedFormat.Create(Format); | |||||
| end; | end; | ||||
| Info.biXPelsPerMeter := 2835; | Info.biXPelsPerMeter := 2835; | ||||
| Info.biYPelsPerMeter := 2835; | Info.biYPelsPerMeter := 2835; | ||||
| @@ -7020,7 +7019,7 @@ var | |||||
| Converter: TFormatDescriptor; | Converter: TFormatDescriptor; | ||||
| begin | begin | ||||
| if not (ftTGA in FormatGetSupportedFiles(Format)) then | if not (ftTGA in FormatGetSupportedFiles(Format)) then | ||||
| raise EglBitmapUnsupportedFormat.Create('SaveTGA'); | |||||
| raise EglBitmapUnsupportedFormat.Create(Format); | |||||
| //prepare header | //prepare header | ||||
| FillChar(Header{%H-}, SizeOf(Header), 0); | FillChar(Header{%H-}, SizeOf(Header), 0); | ||||
| @@ -7336,7 +7335,7 @@ var | |||||
| FormatDesc: TFormatDescriptor; | FormatDesc: TFormatDescriptor; | ||||
| begin | begin | ||||
| if not (ftDDS in FormatGetSupportedFiles(Format)) then | if not (ftDDS in FormatGetSupportedFiles(Format)) then | ||||
| raise EglBitmapUnsupportedFormat.Create('SaveDDS'); | |||||
| raise EglBitmapUnsupportedFormat.Create(Format); | |||||
| FormatDesc := TFormatDescriptor.Get(Format); | FormatDesc := TFormatDescriptor.Get(Format); | ||||
| @@ -7479,7 +7478,7 @@ var | |||||
| begin | begin | ||||
| FormatDesc := TFormatDescriptor.Get(Format); | FormatDesc := TFormatDescriptor.Get(Format); | ||||
| if FormatDesc.IsCompressed then | if FormatDesc.IsCompressed then | ||||
| raise EglBitmapUnsupportedFormat.Create('TglBitmap2D.GrabScreen'); | |||||
| raise EglBitmapUnsupportedFormat.Create(Format); | |||||
| w := aRight - aLeft; | w := aRight - aLeft; | ||||
| h := aBottom - aTop; | h := aBottom - aTop; | ||||
| @@ -7729,10 +7728,8 @@ var | |||||
| end; | end; | ||||
| begin | 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 | if aScale > 100 then | ||||
| Rec.Scale := 100 | Rec.Scale := 100 | ||||