Loading PNG files as OpenGL textures

classic Classic list List threaded Threaded
33 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Re: Loading PNG files as OpenGL textures

Ryan Joseph

> On Oct 12, 2015, at 2:25 PM, Michael Van Canneyt <[hidden email]> wrote:
>
> TFPCustomImage is an abstract image class, it provides no storage for the data. It can have several descendants such as TFPMemoryImage and TFPCompactImgRGBA8Bit.
> If you just want the raw data, create a TFPMemoryImage and access the Colors array property.

Sorry for the silence.

I’ve learned that the FPImage classes are pretty slow but I’d like to try them anyways first to see if they’re acceptable.

I don’t use Delphi so the FPC docs are a little confusing for me. There is a colors/pixels property but from what I can tell they map to getting a single pixel/color at a specific location, but the docs aren’t clear because the method arguments are never shown.

Do I need to loop through all the pixels or something and pack them into a pointer? There should be some a single continuous block of memory in this class but I can’t seem to find it using the colors/pixels property. What am I missing?

==========

http://lazarus-ccr.sourceforge.net/docs/fcl/fpimage/tfpmemoryimage.html
http://lazarus-ccr.sourceforge.net/docs/fcl/fpimage/tfpcustomimage.html


Regards,
        Ryan Joseph

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Loading PNG files as OpenGL textures

Ryan Joseph
In reply to this post by Reimar Grabowski

> On Oct 12, 2015, at 8:39 PM, Reimar Grabowski <[hidden email]> wrote:
>
> http://sourceforge.net/p/asmoday/code/HEAD/tree/trunk/asmtypes.pas#l203
>
> Not an example but a straightforward texture class implementation based on TFPCustomImage. Should work as is for bmp, jpg, png, tga, xpm, if not drop me a line.

Thanks, this is really helpful.

I see how you populated TFPCustomImage and generated “Data” to store the color values.

Is it necessary to build an entire subclass or can I just use TFPMemoryImage to achieve the same effect? I understand now TFPCustomImage is an abstract class but TFPMemoryImage apparently has stored values so I should be able to access the “Data” property but these seem to be accessors to getting single pixel values so I can’t just pass the entire block into glTexImage2D like you did.

Regards,
        Ryan Joseph

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Loading PNG files as OpenGL textures

Ryan Joseph
In reply to this post by Graeme Geldenhuys-6

> On Oct 12, 2015, at 5:52 PM, Graeme Geldenhuys <[hidden email]> wrote:
>
> http://members.upc.nl/h.speksnijder4/software/fpGUI/pngloader.html


Thanks for the links. BeRoPNG seems to load a PNG and the author states it’s fast and has no external dependancies. Fantastic.

According to Reimar’s code the raw image data is sufficient for glTexImage2D so I wonder if the data pointer returned from BeRoPNG would work also?

In my Apple CoreGraphics code I was drawing the image into a bitmap context and getting pixel data from the bitmap but that seems to be an extra step now.


Regards,
        Ryan Joseph

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Loading PNG files as OpenGL textures

Michael Van Canneyt
In reply to this post by Ryan Joseph


On Wed, 14 Oct 2015, Ryan Joseph wrote:

>
>> On Oct 12, 2015, at 8:39 PM, Reimar Grabowski <[hidden email]> wrote:
>>
>> http://sourceforge.net/p/asmoday/code/HEAD/tree/trunk/asmtypes.pas#l203
>>
>> Not an example but a straightforward texture class implementation based on TFPCustomImage. Should work as is for bmp, jpg, png, tga, xpm, if not drop me a line.
>
> Thanks, this is really helpful.
>
> I see how you populated TFPCustomImage and generated “Data” to store the color values.
>
> Is it necessary to build an entire subclass or can I just use
> TFPMemoryImage to achieve the same effect?  I understand now
> TFPCustomImage is an abstract class but TFPMemoryImage apparently has
> stored values so I should be able to access the “Data” property but these
> seem to be accessors to getting single pixel values so I can’t just pass
> the entire block into glTexImage2D like you did.
If you need access to the actual memory block, then you must create another subclass of TFPCustomImage.
TFPMemoryImage stores rgba in 4 word values which is the 'maximum' storage allowed by the API.
There are other subclasses with more common formats, see my other mail.

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Loading PNG files as OpenGL textures

Michael Van Canneyt
In reply to this post by Ryan Joseph


On Wed, 14 Oct 2015, Ryan Joseph wrote:

>
>> On Oct 12, 2015, at 2:25 PM, Michael Van Canneyt <[hidden email]> wrote:
>>
>> TFPCustomImage is an abstract image class, it provides no storage for the data. It can have several descendants such as TFPMemoryImage and TFPCompactImgRGBA8Bit.
>> If you just want the raw data, create a TFPMemoryImage and access the Colors array property.
>
> Sorry for the silence.
>
> I’ve learned that the FPImage classes are pretty slow but I’d like to try them anyways first to see if they’re acceptable.
>
> I don’t use Delphi so the FPC docs are a little confusing for me. There is a colors/pixels property but from what I can tell they map to getting a single pixel/color at a specific location, but the docs aren’t clear because the method arguments are never shown.
>
> Do I need to loop through all the pixels or something and pack them into a
> pointer?  There should be some a single continuous block of memory in this
> class but I can’t seem to find it using the colors/pixels property.  What
> am I missing?
Your are not missing anything, you saw correct.
TFPCompactImgRGBA8Bit is probably what you need, you'll need to subclass it to get direct access to the
   FData: PFPCompactImgRGBA8BitValue;
field, which is the memory buffer.

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Loading PNG files as OpenGL textures

Michael Van Canneyt


On Wed, 14 Oct 2015, Michael Van Canneyt wrote:

>
>
> On Wed, 14 Oct 2015, Ryan Joseph wrote:
>
>>
>>> On Oct 12, 2015, at 2:25 PM, Michael Van Canneyt <[hidden email]>
>>> wrote:
>>>
>>> TFPCustomImage is an abstract image class, it provides no storage for the
>>> data. It can have several descendants such as TFPMemoryImage and
>>> TFPCompactImgRGBA8Bit.
>>> If you just want the raw data, create a TFPMemoryImage and access the
>>> Colors array property.
>>
>> Sorry for the silence.
>>
>> I’ve learned that the FPImage classes are pretty slow but I’d like to try
>> them anyways first to see if they’re acceptable.
>>
>> I don’t use Delphi so the FPC docs are a little confusing for me. There is
>> a colors/pixels property but from what I can tell they map to getting a
>> single pixel/color at a specific location, but the docs aren’t clear
>> because the method arguments are never shown.
>> Do I need to loop through all the pixels or something and pack them into a
>> pointer?  There should be some a single continuous block of memory in this
>> class but I can’t seem to find it using the colors/pixels property.  What
>> am I missing?
>
> Your are not missing anything, you saw correct.
> TFPCompactImgRGBA8Bit is probably what you need, you'll need to subclass it
> to get direct access to the
>  FData: PFPCompactImgRGBA8BitValue; field, which is the memory buffer.
Type
TMyImage = Class(TFPCompactImgRGBA8Bit)
Public
   property Data :  PFPCompactImgRGBA8BitValue read FData;
end;


Should do it. Or if you don't need Alpha:

TMyImage = Class(TFPCompactImgRGB8Bit)
Public
   property Data :  PFPCompactImgRGB8BitValue read FData;
end;

You could throw in a property to get a direct pointer to a scanline.
I should maybe add that to the base classes...

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Loading PNG files as OpenGL textures

Graeme Geldenhuys-6
Hello Michael,

On 2015-10-14 at 08:49, Michael Van Canneyt wrote:
> You could throw in a property to get a direct pointer to a scanline.
> I should maybe add that to the base classes...

+1

fpGUI's Image class has both ImageData and ScanLine[] properties, and
they are very useful for fast access to the actual pixel data. When I
work with FPImage, I always have to go via Pixels[] array which I
assume will have a speed penalty.

ps:
    Thanks for the subclass idea to surface the Data property - that
    will be very useful too.


Regards,
  - Graeme -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

My public PGP key:  http://tinyurl.com/graeme-pgp

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Loading PNG files as OpenGL textures

Graeme Geldenhuys-6
In reply to this post by Ryan Joseph
Hello Ryan,

On 2015-10-14 at 08:54, Ryan Joseph wrote:
> According to Reimar’s code the raw image data is sufficient for
> glTexImage2D so I wonder if the data pointer returned from BeRoPNG
> would work also?


I assume it should work. I guess there is only one way to found out. ;-)



Regards,
  - Graeme -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

My public PGP key:  http://tinyurl.com/graeme-pgp

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Loading PNG files as OpenGL textures

Ryan Joseph

> On Oct 14, 2015, at 4:59 PM, Graeme Geldenhuys <[hidden email]> wrote:
>
> I assume it should work. I guess there is only one way to found out. ;-)

Yes it did work which is nice.

I didn’t do any speed tests yet but from what I read this unit is faster than FPImage and it's easier to implement also, being just a single function.

Thanks.

Regards,
        Ryan Joseph

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Loading PNG files as OpenGL textures

Ryan Joseph
In reply to this post by Michael Van Canneyt

> On Oct 14, 2015, at 1:49 PM, Michael Van Canneyt <[hidden email]> wrote:
>
> Type
> TMyImage = Class(TFPCompactImgRGBA8Bit)
> Public
>  property Data :  PFPCompactImgRGBA8BitValue read FData;
> end;
>
>
> Should do it. Or if you don't need Alpha:
>
> TMyImage = Class(TFPCompactImgRGB8Bit)
> Public
>  property Data :  PFPCompactImgRGB8BitValue read FData;
> end;
>
> You could throw in a property to get a direct pointer

TFPCompactImgRGBA8Bit descends from TFPMemoryImage I assume? I need to draw text as textures also and I found an example at http://wiki.freepascal.org/fcl-image#Drawing_text which uses TFPMemoryImage.

Thanks for the example, I’ll give this a try.


Regards,
        Ryan Joseph

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Loading PNG files as OpenGL textures

Mattias Gaertner
On Thu, 15 Oct 2015 15:30:41 +0700
Ryan Joseph <[hidden email]> wrote:

>
> > On Oct 14, 2015, at 1:49 PM, Michael Van Canneyt <[hidden email]> wrote:
> >
> > Type
> > TMyImage = Class(TFPCompactImgRGBA8Bit)
> > Public
> >  property Data :  PFPCompactImgRGBA8BitValue read FData;
> > end;
> >
> >
> > Should do it. Or if you don't need Alpha:
> >
> > TMyImage = Class(TFPCompactImgRGB8Bit)
> > Public
> >  property Data :  PFPCompactImgRGB8BitValue read FData;
> > end;
> >
> > You could throw in a property to get a direct pointer
>
> TFPCompactImgRGBA8Bit descends from TFPMemoryImage I assume?

Actually it works like TFPMemoryImage and descends from
TFPCustomImage. The only difference is that it uses 4*8=32bit per pixel
instead of 4*16bit.

It is only a few lines of code, so if you need a different pixel format
or scan lines it is really easy to create your own image class.

> I need to draw text as textures also and I found an example at
> http://wiki.freepascal.org/fcl-image#Drawing_text which uses
> TFPMemoryImage.


Mattias
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Loading PNG files as OpenGL textures

Ryan Joseph

> On Oct 15, 2015, at 4:38 PM, Mattias Gaertner <[hidden email]> wrote:
>
> Actually it works like TFPMemoryImage and descends from
> TFPCustomImage. The only difference is that it uses 4*8=32bit per pixel
> instead of 4*16bit.

Are any of these FPImage classes documented or should I just try to dig around the FPC sources to find these things? There are dozens of classes available at http://wiki.freepascal.org/fcl-image but I don’t know what any of them do.

Regards,
        Ryan Joseph

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Loading PNG files as OpenGL textures

Felipe Monteiro de Carvalho
On Fri, Oct 16, 2015 at 3:19 AM, Ryan Joseph <[hidden email]> wrote:
> Are any of these FPImage classes documented or should I just try to dig around the FPC sources to
> find these things? There are dozens of classes available at http://wiki.freepascal.org/fcl-image but I
> don’t know what any of them do.

The wiki page that you linked has some examples:
http://wiki.freepascal.org/fcl-image#Walk-through

--
Felipe Monteiro de Carvalho
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
12