[fpc-pascal] Loading PNG files as OpenGL textures
Anthony Walter
sysrpl at gmail.com
Mon Oct 12 04:47:56 CEST 2015
Okay, I looked at my Darwin sources which use minimal wrappers around the
inbuilt Quartz classes to handle image loading, saving, and pixel data
access.
To load any image (png, jpg, bmp, gif, ect) you use a CGImageSourceRef
object.
>From file:
ImageSource :=
CGImageSourceCreateWithURL(UrlRefCreateFromFileSystem(FileName).Handle,
nil);
>From a stream:
ImageSource := CGImageSourceCreateWithData(DataRefCreate(Stream).Handle,
nil);
Then to access image data you need to create a bitmap context and tell the
image source to populate the bitmap. My bitmap context create looks like
this, which should give you a head start:
constructor TBitmapContextRef.Create(Image: IImageRef; ColorSpace:
TColorSpace = colorBGRA);
var
R: CGRect;
begin
Create(Image.Width, Image.Height);
R.origin.x := 0;
R.origin.y := 0;
R.size.width := Image.Width;
R.size.height := Image.Height;
CGContextDrawImage(Handle, R, Image.Handle);
end;
constructor TBitmapContextRef.Create(Width, Height: Integer; ColorSpace:
TColorSpace = colorBGRA);
var
PixelBytes: UIntPtr;
C: CGColorSpaceRef;
A: CGImageAlphaInfo;
R: CGContextRef;
begin
inherited Create(nil);
PixelBytes := Width * Height * 4;
FPixels := GetMem(PixelBytes);
FillZero(FPixels^, PixelBytes);
C := CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
A := ColorSpaceToAlpha(ColorSpace);
R := CGBitmapContextCreate(FPixels, Width, Height, 8, Width * 4, C, A);
CGColorSpaceRelease(C);
HandleOwn(R);
end;
https://developer.apple.com/library/mac/documentation/GraphicsImaging/Reference/CGImageSource/
https://developer.apple.com/library/mac/documentation/GraphicsImaging/Reference/CGBitmapContext/
You might be able to figure the rest out from there by yourself. If instead
of figuring things out for yourself, you might to use want the Darwin
graphics part of my library. For this you'll need two of my unit files:
The first unit wraps the ref object management pattern used by Darwin and
includes plus a few extra helper routines.
The second unit wraps the Quartz 2D objects and also includes a few simple
helper routines.
Let me know if you want to contribute and I can upload some code to a
repository on github. But please I would ask that if you have any
improvements to my code (such as wrapping more classes), that you share
those changes with everyone using the same github repository. I'll need you
github account names add you guys as contributors.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20151011/da2e0ebb/attachment.html>
More information about the fpc-pascal
mailing list