{***********************************************************
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
{.$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}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 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
// 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}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-// PRIVATE: DO not change anything! //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+// PRIVATE: do not change anything! //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Delphi Versions
{$IFDEF fpc}
type
////////////////////////////////////////////////////////////////////////////////////////////////////
- EglBitmapException = class(Exception);
- EglBitmapSizeToLargeException = class(EglBitmapException);
- EglBitmapNonPowerOfTwoException = class(EglBitmapException);
- EglBitmapUnsupportedFormat = class(EglBitmapException);
-
-////////////////////////////////////////////////////////////////////////////////////////////////////
TglBitmapFormat = (
tfEmpty = 0, //must be smallest value!
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
*)
uses
- Math, syncobjs;
+ Math, syncobjs, typinfo;
type
////////////////////////////////////////////////////////////////////////////////////////////////////
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
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,
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;
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;
begin
case aFileType of
{$IFDEF GLB_SUPPORT_PNG_WRITE}
- ftPNG: SavePng(aStream);
+ ftPNG: SavePNG(aStream);
{$ENDIF}
{$IFDEF GLB_SUPPORT_JPEG_WRITE}
ftJPEG: SaveJPEG(aStream);
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
3: TempDepth := 24;
4: TempDepth := 32;
else
- raise EglBitmapUnsupportedFormat.Create('AssignToSurface');
+ raise EglBitmapUnsupportedFormat.Create(Format);
end;
aSurface := SDL_CreateRGBSurface(SDL_SWSURFACE, Width, Height, TempDepth,
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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;
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;
png_info: png_infop;
TempHeight, TempWidth: Integer;
- Format: TglBitmapInternalFormat;
+ Format: TglBitmapFormat;
png_data: pByte;
png_rows: array of pByte;
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
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);
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);
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);
begin
if not (ftBMP in FormatGetSupportedFiles(Format)) then
- raise EglBitmapUnsupportedFormat.Create('SaveBMP');
+ raise EglBitmapUnsupportedFormat.Create(Format);
Converter := nil;
FormatDesc := TFormatDescriptor.Get(Format);
Info.biCompression := BMP_COMP_BITFIELDS;
end;
else
- raise EglBitmapUnsupportedFormat.Create('SaveBMP - ' + UNSUPPORTED_FORMAT);
+ raise EglBitmapUnsupportedFormat.Create(Format);
end;
Info.biXPelsPerMeter := 2835;
Info.biYPelsPerMeter := 2835;
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);
FormatDesc: TFormatDescriptor;
begin
if not (ftDDS in FormatGetSupportedFiles(Format)) then
- raise EglBitmapUnsupportedFormat.Create('SaveDDS');
+ raise EglBitmapUnsupportedFormat.Create(Format);
FormatDesc := TFormatDescriptor.Get(Format);
begin
FormatDesc := TFormatDescriptor.Get(Format);
if FormatDesc.IsCompressed then
- raise EglBitmapUnsupportedFormat.Create('TglBitmap2D.GrabScreen');
+ raise EglBitmapUnsupportedFormat.Create(Format);
w := aRight - aLeft;
h := aBottom - aTop;
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