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/)
+modified by Delphi OpenGL Community (http://delphigl.com/) (2013)
------------------------------------------------------------
The contents of this file are used with permission, subject to
obtain a copy of the License at
http://www.mozilla.org/MPL/MPL-1.1.html
------------------------------------------------------------
-Version 2.0.3
+Version 3.0.0 unstable
------------------------------------------------------------
History
+20-11-2013
+- refactoring of the complete library
21-03-2010
- The define GLB_DELPHI dosn't check versions anymore. If you say you are using delphi
then it's your problem if that isn't true. This prevents the unit for incompatibility
{.$DEFINE GLB_DELPHI}
// activate to enable the support for TLazIntfImage from Lazarus
-{$DEFINE GLB_LAZARUS}
+{.$DEFINE GLB_LAZARUS}
+// 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}
+// 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}
{$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}
{$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}
{$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}
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) OR
+ DEFINED(GLB_DELPHI))} 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, Types, {$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;
EglBitmapSizeToLarge = class(EglBitmap);
EglBitmapNonPowerOfTwo = class(EglBitmap);
EglBitmapUnsupportedFormat = class(EglBitmap)
+ public
constructor Create(const aFormat: TglBitmapFormat); overload;
constructor Create(const aMsg: String; const aFormat: TglBitmapFormat); overload;
end;
Y : Word;
end;
+ TglBitmapFormatDescriptor = class(TObject)
+ protected
+ function GetIsCompressed: Boolean; virtual; abstract;
+ function GetHasAlpha: Boolean; virtual; abstract;
+
+ function GetglDataFormat: GLenum; virtual; abstract;
+ function GetglFormat: GLenum; virtual; abstract;
+ function GetglInternalFormat: GLenum; virtual; abstract;
+ public
+ property IsCompressed: Boolean read GetIsCompressed;
+ property HasAlpha: Boolean read GetHasAlpha;
+
+ property glFormat: GLenum read GetglFormat;
+ property glInternalFormat: GLenum read GetglInternalFormat;
+ property glDataFormat: GLenum read GetglDataFormat;
+ end;
+
////////////////////////////////////////////////////////////////////////////////////////////////////
TglBitmap = class;
TglBitmapFunctionRec = record
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TglBitmap = class
+ private
+ function GetFormatDesc: TglBitmapFormatDescriptor;
protected
fID: GLuint;
fTarget: GLuint;
fAnisotropic: Integer;
fDeleteTextureOnFree: Boolean;
+ fFreeDataOnDestroy: Boolean;
fFreeDataAfterGenTexture: Boolean;
fData: PByte;
fIsResident: Boolean;
procedure SetCustomData(const aValue: Pointer);
procedure SetCustomName(const aValue: String);
procedure SetCustomNameW(const aValue: WideString);
+ procedure SetFreeDataOnDestroy(const aValue: Boolean);
procedure SetDeleteTextureOnFree(const aValue: Boolean);
procedure SetFormat(const aValue: TglBitmapFormat);
procedure SetFreeDataAfterGenTexture(const aValue: Boolean);
property MipMap: TglBitmapMipMap read fMipMap write SetMipMap;
property Anisotropic: Integer read fAnisotropic write SetAnisotropic;
+ property FormatDesc: TglBitmapFormatDescriptor read GetFormatDesc;
+
property Filename: String read fFilename;
property CustomName: String read fCustomName write SetCustomName;
property CustomNameW: WideString read fCustomNameW write SetCustomNameW;
property CustomData: Pointer read fCustomData write SetCustomData;
property DeleteTextureOnFree: Boolean read fDeleteTextureOnFree write SetDeleteTextureOnFree;
+ property FreeDataOnDestroy: Boolean read fFreeDataOnDestroy write SetFreeDataOnDestroy;
property FreeDataAfterGenTexture: Boolean read fFreeDataAfterGenTexture write SetFreeDataAfterGenTexture;
property Dimension: TglBitmapPixelPosition read fDimension;
constructor Create; overload;
constructor Create(const aFileName: String); overload;
constructor Create(const aStream: TStream); overload;
- constructor Create(const aSize: TglBitmapPixelPosition; const aFormat: TglBitmapFormat); overload;
+ constructor Create(const aSize: TglBitmapPixelPosition; const aFormat: TglBitmapFormat; aData: PByte = nil); overload;
constructor Create(const aSize: TglBitmapPixelPosition; const aFormat: TglBitmapFormat; const aFunc: TglBitmapFunction; const aArgs: Pointer = nil); overload;
constructor Create(const aInstance: Cardinal; const aResource: String; const aResType: PChar = nil); overload;
constructor Create(const aInstance: Cardinal; const aResourceID: Integer; const aResType: PChar); overload;
implementation
uses
- Math, syncobjs, typinfo;
+ Math, syncobjs, typinfo
+ {$IFDEF GLB_DELPHI}, Types{$ENDIF};
type
{$IFNDEF fpc}
1: (arr: array[0..3] of Byte);
end;
- TFormatDescriptor = class(TObject)
+ TFormatDescriptor = class(TglBitmapFormatDescriptor)
private
function GetRedMask: QWord;
function GetGreenMask: QWord;
fRange: TglBitmapColorRec;
fShift: TShiftRec;
- fglFormat: Cardinal;
- fglInternalFormat: Cardinal;
- fglDataFormat: Cardinal;
+ fglFormat: GLenum;
+ fglInternalFormat: GLenum;
+ fglDataFormat: GLenum;
+
+ function GetIsCompressed: Boolean; override;
+ function GetHasAlpha: Boolean; override;
+
+ function GetglFormat: GLenum; override;
+ function GetglInternalFormat: GLenum; override;
+ function GetglDataFormat: GLenum; override;
function GetComponents: Integer; virtual;
public
property RGBInverted: TglBitmapFormat read fRGBInverted;
property Components: Integer read GetComponents;
property PixelSize: Single read fPixelSize;
- property IsCompressed: Boolean read fIsCompressed;
-
- property glFormat: Cardinal read fglFormat;
- property glInternalFormat: Cardinal read fglInternalFormat;
- property glDataFormat: Cardinal read fglDataFormat;
property Range: TglBitmapColorRec read fRange;
property Shift: TShiftRec read fShift;
procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); virtual; abstract;
function GetSize(const aSize: TglBitmapPixelPosition): Integer; overload; virtual;
- function GetSize(const aWidth, aHeight: Integer): Integer; overload; virtual;
+ function GetSize(const aWidth, aHeight: Integer): Integer; overload; virtual;
function CreateMappingData: Pointer; virtual;
procedure FreeMappingData(var aMappingData: Pointer); virtual;
function IsEmpty: Boolean; virtual;
- function HasAlpha: Boolean; virtual;
function MaskMatch(const aRedMask, aGreenMask, aBlueMask, aAlphaMask: QWord): Boolean; virtual;
procedure PreparePixel(out aPixel: TglBitmapPixelData); virtual;
var
GL_LibHandle: Pointer = nil;
-function glbGetProcAddress(aProcName: PChar; aLibHandle: Pointer = nil): Pointer;
+function glbGetProcAddress(aProcName: PAnsiChar; aLibHandle: Pointer = nil; const aRaiseOnErr: Boolean = true): Pointer;
begin
if not Assigned(aLibHandle) then
aLibHandle := GL_LibHandle;
result := dlsym(aLibHandle, aProcName);
{$IFEND}
- if not Assigned(result) then
+ if not Assigned(result) and aRaiseOnErr then
raise EglBitmap.Create('unable to load procedure form library: ' + aProcName);
end;
if not Assigned(GLU_LibHandle) then
raise EglBitmap.Create('unable to load library: ' + libglu);
- try
- {$IF DEFINED(GLB_WIN)}
- wglGetProcAddress := glbGetProcAddress('wglGetProcAddress');
- {$ELSEIF DEFINED(GLB_LINUX)}
- glXGetProcAddress := glbGetProcAddress('glXGetProcAddress');
- glXGetProcAddressARB := glbGetProcAddress('glXGetProcAddressARB');
- {$IFEND}
-
- glEnable := glbGetProcAddress('glEnable');
- glDisable := glbGetProcAddress('glDisable');
- glGetString := glbGetProcAddress('glGetString');
- glGetIntegerv := glbGetProcAddress('glGetIntegerv');
- glTexParameteri := glbGetProcAddress('glTexParameteri');
- glTexParameteriv := glbGetProcAddress('glTexParameteriv');
- glTexParameterfv := glbGetProcAddress('glTexParameterfv');
- glGetTexParameteriv := glbGetProcAddress('glGetTexParameteriv');
- glGetTexParameterfv := glbGetProcAddress('glGetTexParameterfv');
- glGetTexLevelParameteriv := glbGetProcAddress('glGetTexLevelParameteriv');
- glGetTexLevelParameterfv := glbGetProcAddress('glGetTexLevelParameterfv');
- glTexGeni := glbGetProcAddress('glTexGeni');
- glGenTextures := glbGetProcAddress('glGenTextures');
- glBindTexture := glbGetProcAddress('glBindTexture');
- glDeleteTextures := glbGetProcAddress('glDeleteTextures');
- glAreTexturesResident := glbGetProcAddress('glAreTexturesResident');
- glReadPixels := glbGetProcAddress('glReadPixels');
- glPixelStorei := glbGetProcAddress('glPixelStorei');
- glTexImage1D := glbGetProcAddress('glTexImage1D');
- glTexImage2D := glbGetProcAddress('glTexImage2D');
- glGetTexImage := glbGetProcAddress('glGetTexImage');
-
- gluBuild1DMipmaps := glbGetProcAddress('gluBuild1DMipmaps', GLU_LibHandle);
- gluBuild2DMipmaps := glbGetProcAddress('gluBuild2DMipmaps', GLU_LibHandle);
- finally
- glbFreeLibrary(GL_LibHandle);
- glbFreeLibrary(GLU_LibHandle);
- end;
+{$IF DEFINED(GLB_WIN)}
+ wglGetProcAddress := glbGetProcAddress('wglGetProcAddress');
+{$ELSEIF DEFINED(GLB_LINUX)}
+ glXGetProcAddress := glbGetProcAddress('glXGetProcAddress');
+ glXGetProcAddressARB := glbGetProcAddress('glXGetProcAddressARB');
+{$IFEND}
+
+ glEnable := glbGetProcAddress('glEnable');
+ glDisable := glbGetProcAddress('glDisable');
+ glGetString := glbGetProcAddress('glGetString');
+ glGetIntegerv := glbGetProcAddress('glGetIntegerv');
+ glTexParameteri := glbGetProcAddress('glTexParameteri');
+ glTexParameteriv := glbGetProcAddress('glTexParameteriv');
+ glTexParameterfv := glbGetProcAddress('glTexParameterfv');
+ glGetTexParameteriv := glbGetProcAddress('glGetTexParameteriv');
+ glGetTexParameterfv := glbGetProcAddress('glGetTexParameterfv');
+ glGetTexLevelParameteriv := glbGetProcAddress('glGetTexLevelParameteriv');
+ glGetTexLevelParameterfv := glbGetProcAddress('glGetTexLevelParameterfv');
+ glTexGeni := glbGetProcAddress('glTexGeni');
+ glGenTextures := glbGetProcAddress('glGenTextures');
+ glBindTexture := glbGetProcAddress('glBindTexture');
+ glDeleteTextures := glbGetProcAddress('glDeleteTextures');
+ glAreTexturesResident := glbGetProcAddress('glAreTexturesResident');
+ glReadPixels := glbGetProcAddress('glReadPixels');
+ glPixelStorei := glbGetProcAddress('glPixelStorei');
+ glTexImage1D := glbGetProcAddress('glTexImage1D');
+ glTexImage2D := glbGetProcAddress('glTexImage2D');
+ glGetTexImage := glbGetProcAddress('glGetTexImage');
+
+ gluBuild1DMipmaps := glbGetProcAddress('gluBuild1DMipmaps', GLU_LibHandle);
+ gluBuild2DMipmaps := glbGetProcAddress('gluBuild2DMipmaps', GLU_LibHandle);
end;
{$ENDIF}
glCompressedTexImage2D := glbGetProcAddress('glCompressedTexImage2D');
glGetCompressedTexImage := glbGetProcAddress('glGetCompressedTexImage');
end else begin
- glCompressedTexImage1D := glbGetProcAddress('glCompressedTexImage1DARB');
- glCompressedTexImage2D := glbGetProcAddress('glCompressedTexImage2DARB');
- glGetCompressedTexImage := glbGetProcAddress('glGetCompressedTexImageARB');
+ glCompressedTexImage1D := glbGetProcAddress('glCompressedTexImage1DARB', nil, false);
+ glCompressedTexImage2D := glbGetProcAddress('glCompressedTexImage2DARB', nil, false);
+ glGetCompressedTexImage := glbGetProcAddress('glGetCompressedTexImageARB', nil, false);
end;
end;
{$ENDIF}
end;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+function TFormatDescriptor.GetIsCompressed: Boolean;
+begin
+ result := fIsCompressed;
+end;
+
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+function TFormatDescriptor.GetHasAlpha: Boolean;
+begin
+ result := (fRange.a > 0);
+end;
+
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+function TFormatDescriptor.GetglFormat: GLenum;
+begin
+ result := fglFormat;
+end;
+
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+function TFormatDescriptor.GetglInternalFormat: GLenum;
+begin
+ result := fglInternalFormat;
+end;
+
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+function TFormatDescriptor.GetglDataFormat: GLenum;
+begin
+ result := fglDataFormat;
+end;
+
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function TFormatDescriptor.GetComponents: Integer;
var
i: Integer;
end;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-function TFormatDescriptor.HasAlpha: Boolean;
-begin
- result := (fRange.a > 0);
-end;
-
-//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function TFormatDescriptor.MaskMatch(const aRedMask, aGreenMask, aBlueMask, aAlphaMask: QWord): Boolean;
begin
result := false;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//TglBitmap - PROTECTED///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+function TglBitmap.GetFormatDesc: TglBitmapFormatDescriptor;
+begin
+ result := TFormatDescriptor.Get(Format);
+end;
+
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function TglBitmap.GetWidth: Integer;
begin
if (ffX in fDimension.Fields) then
end;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+procedure TglBitmap.SetFreeDataOnDestroy(const aValue: Boolean);
+begin
+ if fFreeDataOnDestroy = aValue then
+ exit;
+ fFreeDataOnDestroy := aValue;
+end;
+
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
procedure TglBitmap.SetDeleteTextureOnFree(const aValue: Boolean);
begin
if fDeleteTextureOnFree = aValue then
fData := aData;
end;
- FillChar(fDimension, SizeOf(fDimension), 0);
if not Assigned(fData) then begin
- fFormat := tfEmpty;
fPixelSize := 0;
fRowSize := 0;
end else begin
+ FillChar(fDimension, SizeOf(fDimension), 0);
if aWidth <> -1 then begin
fDimension.Fields := fDimension.Fields + [ffX];
fDimension.X := aWidth;
fTarget := 0;
fIsResident := false;
- fFormat := glBitmapGetDefaultFormat;
fMipMap := glBitmapDefaultMipmap;
fFreeDataAfterGenTexture := glBitmapGetDefaultFreeDataAfterGenTexture;
fDeleteTextureOnFree := glBitmapGetDefaultDeleteTextureOnFree;
var
NewData: PByte;
begin
- NewData := nil;
- SetDataPointer(NewData, tfEmpty); //be careful, Data could be freed by this method
+ if fFreeDataOnDestroy then begin
+ NewData := nil;
+ SetDataPointer(NewData, tfEmpty); //be careful, Data could be freed by this method
+ end;
if (fID > 0) and fDeleteTextureOnFree then
glDeleteTextures(1, @fID);
inherited BeforeDestruction;
FreeMem(tmpData);
raise;
end;
- AddFunc(Self, aFunc, false, Format, aArgs);
+ AddFunc(Self, aFunc, false, aFormat, aArgs);
end;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
tfRGBA8, tfBGRA8:
aBitmap.PixelFormat := pf32bit;
else
- raise EglBitmapException.Create('AssignToBitmap - Invalid Pixelformat.');
+ raise EglBitmap.Create('AssignToBitmap - Invalid Pixelformat.');
end;
pSource := Data;
pf32bit:
IntFormat := tfBGRA8;
else
- raise EglBitmapException.Create('AssignFromBitmap - Invalid Pixelformat.');
+ raise EglBitmap.Create('AssignFromBitmap - Invalid Pixelformat.');
end;
TempWidth := aBitmap.Width;
Inc(pSource);
end;
end;
- end;
+ end;
result := true;
end;
end;
rid.Width := Width;
rid.Height := Height;
- rid.Depth := CountSetBits(FormatDesc.Range.r or FormatDesc.Range.g or FormatDesc.Range.b or FormatDesc.Range.a);
+ rid.Depth := CountSetBits(FormatDesc.RedMask or FormatDesc.GreenMask or FormatDesc.BlueMask or FormatDesc.AlphaMask);
rid.BitOrder := riboBitsInOrder;
rid.ByteOrder := riboLSBFirst;
rid.LineOrder := riloTopToBottom;
FormatDesc: TFormatDescriptor;
ImageData: PByte;
ImageSize: Integer;
+ CanCopy: Boolean;
+
+ procedure CopyConvert;
+ var
+ bfFormat: TbmpBitfieldFormat;
+ pSourceLine, pDestLine: PByte;
+ pSourceMD, pDestMD: Pointer;
+ x, y: Integer;
+ pixel: TglBitmapPixelData;
+ begin
+ bfFormat := TbmpBitfieldFormat.Create;
+ with aImage.DataDescription do begin
+ bfFormat.RedMask := ((1 shl RedPrec) - 1) shl RedShift;
+ bfFormat.GreenMask := ((1 shl GreenPrec) - 1) shl GreenShift;
+ bfFormat.BlueMask := ((1 shl BluePrec) - 1) shl BlueShift;
+ bfFormat.AlphaMask := ((1 shl AlphaPrec) - 1) shl AlphaShift;
+ bfFormat.PixelSize := BitsPerPixel / 8;
+ end;
+ pSourceMD := bfFormat.CreateMappingData;
+ pDestMD := FormatDesc.CreateMappingData;
+ try
+ for y := 0 to aImage.Height-1 do begin
+ pSourceLine := aImage.PixelData + y * aImage.DataDescription.BytesPerLine;
+ pDestLine := ImageData + y * Round(FormatDesc.PixelSize * aImage.Width);
+ for x := 0 to aImage.Width-1 do begin
+ bfFormat.Unmap(pSourceLine, pixel, pSourceMD);
+ FormatDesc.Map(pixel, pDestLine, pDestMD);
+ end;
+ end;
+ finally
+ FormatDesc.FreeMappingData(pDestMD);
+ bfFormat.FreeMappingData(pSourceMD);
+ bfFormat.Free;
+ end;
+ end;
+
begin
result := false;
if not Assigned(aImage) then
if (f = tfEmpty) then
exit;
+ CanCopy :=
+ (Round(FormatDesc.PixelSize * 8) = aImage.DataDescription.Depth) and
+ (aImage.DataDescription.BitsPerPixel = aImage.DataDescription.Depth);
+
ImageSize := FormatDesc.GetSize(aImage.Width, aImage.Height);
ImageData := GetMem(ImageSize);
try
- Move(aImage.PixelData^, ImageData^, (aImage.Width * aImage.Height * aImage.DataDescription.BitsPerPixel) shr 3);
+ if CanCopy then
+ Move(aImage.PixelData^, ImageData^, ImageSize)
+ else
+ CopyConvert;
SetDataPointer(ImageData, f, aImage.Width, aImage.Height); //be careful, Data could be freed by this method
except
if Assigned(ImageData) then
var
FS: TFileStream;
begin
- FS := TFileStream.Create(FileName, fmOpenRead);
+ FS := TFileStream.Create(aFileName, fmOpenRead);
try
result := AddAlphaFromStream(FS, aFunc, aArgs);
finally
begin
if aUseRGB or aUseAlpha then
AddFunc(glBitmapInvertFunc, false, {%H-}Pointer(
- ((PtrInt(aUseAlpha) and 1) shl 1) or
- (PtrInt(aUseRGB) and 1) ));
+ ((Byte(aUseAlpha) and 1) shl 1) or
+ (Byte(aUseRGB) and 1) ));
end;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
glbReadOpenGLExtensions;
{$ENDIF}
inherited Create;
+ fFormat := glBitmapGetDefaultFormat;
+ fFreeDataOnDestroy := true;
end;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
constructor TglBitmap.Create(const aFileName: String);
begin
Create;
- LoadFromFile(FileName);
+ LoadFromFile(aFileName);
end;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
end;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-constructor TglBitmap.Create(const aSize: TglBitmapPixelPosition; const aFormat: TglBitmapFormat);
+constructor TglBitmap.Create(const aSize: TglBitmapPixelPosition; const aFormat: TglBitmapFormat; aData: PByte);
var
- Image: PByte;
ImageSize: Integer;
begin
Create;
- ImageSize := TFormatDescriptor.Get(aFormat).GetSize(aSize);
- GetMem(Image, ImageSize);
- try
- FillChar(Image^, ImageSize, #$FF);
- SetDataPointer(Image, aFormat, aSize.X, aSize.Y); //be careful, Data could be freed by this method
- except
- if Assigned(Image) then
- FreeMem(Image);
- raise;
+ if not Assigned(aData) then begin
+ ImageSize := TFormatDescriptor.Get(aFormat).GetSize(aSize);
+ GetMem(aData, ImageSize);
+ try
+ FillChar(aData^, ImageSize, #$FF);
+ SetDataPointer(aData, aFormat, aSize.X, aSize.Y); //be careful, Data could be freed by this method
+ except
+ if Assigned(aData) then
+ FreeMem(aData);
+ raise;
+ end;
+ end else begin
+ SetDataPointer(aData, aFormat, aSize.X, aSize.Y); //be careful, Data could be freed by this method
+ fFreeDataOnDestroy := false;
end;
end;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-constructor TglBitmap.Create(const aSize: TglBitmapPixelPosition; const aFormat: TglBitmapFormat;
- const aFunc: TglBitmapFunction; const aArgs: Pointer);
+constructor TglBitmap.Create(const aSize: TglBitmapPixelPosition; const aFormat: TglBitmapFormat; const aFunc: TglBitmapFunction; const aArgs: Pointer);
begin
Create;
LoadFromFunc(aSize, aFunc, aFormat, aArgs);
end;
{$IFDEF GLB_SUPPORT_PNG_READ}
-{$IF DEFINED(GLB_SDL_IMAGE)}
+{$IF DEFINED(GLB_LAZ_PNG)}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//PNG/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function TglBitmap.LoadPNG(const aStream: TStream): Boolean;
+const
+ MAGIC_LEN = 8;
+ PNG_MAGIC: String[MAGIC_LEN] = #$89#$50#$4E#$47#$0D#$0A#$1A#$0A;
+var
+ png: TPortableNetworkGraphic;
+ intf: TLazIntfImage;
+ StreamPos: Int64;
+ magic: String[MAGIC_LEN];
+begin
+ result := true;
+ StreamPos := aStream.Position;
+
+ SetLength(magic, MAGIC_LEN);
+ aStream.Read(magic[1], MAGIC_LEN);
+ aStream.Position := StreamPos;
+ if (magic <> PNG_MAGIC) then begin
+ result := false;
+ exit;
+ end;
+
+ 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;
// read informations
png_read_info(png, png_info);
- // size
+ // size
TempHeight := png_get_image_height(png, png_info);
TempWidth := png_get_image_width(png, png_info);
end;
{$ENDIF}
-{$IF DEFINED(GLB_LIB_PNG)}
+{$IF DEFINED(GLB_LAZ_PNG)}
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+procedure TglBitmap.SavePNG(const aStream: TStream);
+var
+ png: TPortableNetworkGraphic;
+ intf: TLazIntfImage;
+ raw: TRawImage;
+begin
+ png := TPortableNetworkGraphic.Create;
+ intf := TLazIntfImage.Create(0, 0);
+ try
+ if not AssignToLazIntfImage(intf) then
+ raise EglBitmap.Create('unable to create LazIntfImage from glBitmap');
+ intf.GetRawImage(raw);
+ png.LoadFromRawImage(raw, false);
+ png.SaveToStream(aStream);
+ finally
+ png.Free;
+ intf.Free;
+ end;
+end;
+
+{$ELSEIF DEFINED(GLB_LIB_PNG)}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
procedure TglBitmap.SavePNG(const aStream: TStream);
var
{$ENDIF}
{$IFDEF GLB_SUPPORT_JPEG_READ}
-{$IF DEFINED(GLB_SDL_IMAGE)}
+{$IF DEFINED(GLB_LAZ_JPEG)}
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+function TglBitmap.LoadJPEG(const aStream: TStream): Boolean;
+const
+ MAGIC_LEN = 2;
+ JPEG_MAGIC: String[MAGIC_LEN] = #$FF#$D8;
+var
+ jpeg: TJPEGImage;
+ intf: TLazIntfImage;
+ StreamPos: Int64;
+ magic: String[MAGIC_LEN];
+begin
+ result := true;
+ StreamPos := aStream.Position;
+
+ SetLength(magic, MAGIC_LEN);
+ aStream.Read(magic[1], MAGIC_LEN);
+ aStream.Position := StreamPos;
+ if (magic <> JPEG_MAGIC) then begin
+ result := false;
+ exit;
+ end;
+
+ 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
{$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;
+ raw: TRawImage;
+begin
+ jpeg := TJPEGImage.Create;
+ intf := TLazIntfImage.Create(0, 0);
+ try
+ if not AssignToLazIntfImage(intf) then
+ raise EglBitmap.Create('unable to create LazIntfImage from glBitmap');
+ intf.GetRawImage(raw);
+ jpeg.LoadFromRawImage(raw, false);
+ jpeg.SaveToStream(aStream);
+ finally
+ intf.Free;
+ jpeg.Free;
+ end;
+end;
+
+{$ELSEIF DEFINED(GLB_LIB_JPEG)}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
procedure TglBitmap.SaveJPEG(const aStream: TStream);
var
var
Header: TBMPHeader;
Info: TBMPInfo;
- Converter: TbmpColorTableFormat;
+ Converter: TFormatDescriptor;
FormatDesc: TFormatDescriptor;
SourceFD, DestFD: Pointer;
pData, srcData, dstData, ConvertBuffer: pByte;
Info.biBitCount := 4;
Header.bfSize := Header.bfSize + 16 * SizeOf(Cardinal);
Header.bfOffBits := Header.bfOffBits + 16 * SizeOf(Cardinal); //16 ColorTable entries
- Converter := TbmpColorTableFormat.Create;
- Converter.PixelSize := 0.5;
- Converter.Format := Format;
- Converter.Range := glBitmapColorRec($F, $F, $F, $0);
- Converter.CreateColorTable;
+ Converter := TbmpColorTableFormat.Create;
+ with (Converter as TbmpColorTableFormat) do begin
+ PixelSize := 0.5;
+ Format := Format;
+ Range := glBitmapColorRec($F, $F, $F, $0);
+ CreateColorTable;
+ end;
end;
tfR3G3B2, tfLuminance8: begin
Info.biBitCount := 8;
Header.bfSize := Header.bfSize + 256 * SizeOf(Cardinal);
Header.bfOffBits := Header.bfOffBits + 256 * SizeOf(Cardinal); //256 ColorTable entries
- Converter := TbmpColorTableFormat.Create;
- Converter.PixelSize := 1;
- Converter.Format := Format;
- if (Format = tfR3G3B2) then begin
- Converter.Range := glBitmapColorRec($7, $7, $3, $0);
- Converter.Shift := glBitmapShiftRec(0, 3, 6, 0);
- end else
- Converter.Range := glBitmapColorRec($FF, $FF, $FF, $0);
- Converter.CreateColorTable;
+ Converter := TbmpColorTableFormat.Create;
+ with (Converter as TbmpColorTableFormat) do begin
+ PixelSize := 1;
+ Format := Format;
+ if (Format = tfR3G3B2) then begin
+ Range := glBitmapColorRec($7, $7, $3, $0);
+ Shift := glBitmapShiftRec(0, 3, 6, 0);
+ end else
+ Range := glBitmapColorRec($FF, $FF, $FF, $0);
+ CreateColorTable;
+ end;
end;
tfRGB4, tfRGB5, tfR5G6B5, tfRGB5A1, tfRGBA4,
tfBGR8, tfRGB8: begin
Info.biBitCount := 24;
+ if (Format = tfRGB8) then
+ Converter := TfdBGR8.Create; //use BGR8 Format Descriptor to Swap RGB Values
end;
tfRGB10, tfRGB10A2, tfRGBA8,
aStream.Write(Info, SizeOf(Info));
// colortable
- if Assigned(Converter) then
- aStream.Write(Converter.ColorTable[0].b,
- SizeOf(TbmpColorTableEnty) * Length(Converter.ColorTable));
+ if Assigned(Converter) and (Converter is TbmpColorTableFormat) then
+ with (Converter as TbmpColorTableFormat) do
+ aStream.Write(ColorTable[0].b,
+ SizeOf(TbmpColorTableEnty) * Length(ColorTable));
// bitmasks
if Info.biCompression = BMP_COMP_BITFIELDS then begin
if Header.ImageID <> 0 then // skip image ID
aStream.Position := aStream.Position + Header.ImageID;
- tgaFormat := tfEmpty;
+ tgaFormat := tfEmpty;
case Header.Bpp of
8: if IsGrayFormat then case (Header.ImageDesc and $F) of
0: tgaFormat := tfLuminance8;
begin
// Upload data
FormatDesc := TFormatDescriptor.Get(Format);
- if FormatDesc.IsCompressed then
+ if FormatDesc.IsCompressed then begin
+ if not Assigned(glCompressedTexImage1D) then
+ raise EglBitmap.Create('compressed formats not supported by video adapter');
glCompressedTexImage1D(Target, 0, FormatDesc.glInternalFormat, Width, 0, FormatDesc.GetSize(Width, 1), Data)
- else if aBuildWithGlu then
+ end else if aBuildWithGlu then
gluBuild1DMipmaps(Target, FormatDesc.glInternalFormat, Width, FormatDesc.glFormat, FormatDesc.glDataFormat, Data)
else
glTexImage1D(Target, 0, FormatDesc.glInternalFormat, Width, 0, FormatDesc.glFormat, FormatDesc.glDataFormat, Data);
FormatDesc := TFormatDescriptor.Get(Format);
if FormatDesc.IsCompressed then begin
+ if not Assigned(glCompressedTexImage2D) then
+ raise EglBitmap.Create('compressed formats not supported by video adapter');
glCompressedTexImage2D(aTarget, 0, FormatDesc.glInternalFormat, Width, Height, 0, FormatDesc.GetSize(fDimension), Data)
end else if aBuildWithGlu then begin
gluBuild2DMipmaps(aTarget, FormatDesc.Components, Width, Height,
Size, w, h: Integer;
FormatDesc: TFormatDescriptor;
begin
- FormatDesc := TFormatDescriptor.Get(Format);
+ FormatDesc := TFormatDescriptor.Get(aFormat);
if FormatDesc.IsCompressed then
- raise EglBitmapUnsupportedFormat.Create(Format);
+ raise EglBitmapUnsupportedFormat.Create(aFormat);
w := aRight - aLeft;
h := aBottom - aTop;
try
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glReadPixels(aLeft, aTop, w, h, FormatDesc.glFormat, FormatDesc.glDataFormat, Temp);
- SetDataPointer(Temp, Format, w, h); //be careful, Data could be freed by this method
+ SetDataPointer(Temp, aFormat, w, h); //be careful, Data could be freed by this method
FlipVert;
except
if Assigned(Temp) then
FormatDesc := TFormatDescriptor.Get(IntFormat);
GetMem(Temp, FormatDesc.GetSize(TempWidth, TempHeight));
try
- if FormatDesc.IsCompressed then
+ if FormatDesc.IsCompressed then begin
+ if not Assigned(glGetCompressedTexImage) then
+ raise EglBitmap.Create('compressed formats not supported by video adapter');
glGetCompressedTexImage(Target, 0, Temp)
- else
- glGetTexImage(Target, 0, FormatDesc.glInternalFormat, FormatDesc.glDataFormat, Temp);
+ end else
+ glGetTexImage(Target, 0, FormatDesc.glFormat, FormatDesc.glDataFormat, Temp);
SetDataPointer(Temp, IntFormat, TempWidth, TempHeight); //be careful, Data could be freed by this method
except
if Assigned(Temp) then
finalization
TFormatDescriptor.Finalize;
+{$IFDEF GLB_NATIVE_OGL}
+ if Assigned(GL_LibHandle) then
+ glbFreeLibrary(GL_LibHandle);
+
{$IFDEF GLB_NATIVE_OGL_DYNAMIC}
+ if Assigned(GLU_LibHandle) then
+ glbFreeLibrary(GLU_LibHandle);
FreeAndNil(InitOpenGLCS);
{$ENDIF}
+{$ENDIF}
end.
-