# PNG to OpenGL Texture (libpng)

**URL:** <https://www.ruby-forum.com/t/png-to-opengl-texture-libpng/61881>\
**Category:** Ruby\
**Created:** [May 29, 2006, 9:11am UTC](https://www.ruby-forum.com/t/png-to-opengl-texture-libpng/61881 "2006-05-29T09:11:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![xavier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/2bfe46/32.png) [@xavier](https://www.ruby-forum.com/u/xavier)\
**Post date:** [May 29, 2006, 9:11am UTC](https://www.ruby-forum.com/t/png-to-opengl-texture-libpng/61881/1 "2006-05-29T09:11:51Z")

</div>

Hi,  
I’m trying to load a PNG file into an OpenGL texture using  
ruby-libpng-0.3.3  
I can’t quite figure out how to convert libpng data to the correct  
format.  
I’ve dumped png.read\_image to screen and it looks like my picture (with  
numbers instead of colors). At the moment I’m trying to use this index  
as my Red index (just to get something displaying) but am getting a  
white square instead. I’m already using textures for fonts, so I know  
that bit works.

Any Ideas?

As an aside, if anyone has a better x-platform way of doing this I’m all  
ears…

- Xavier

# Load a test png

rpng = Reader.new(‘res/img/test.png’);

width, height, bit\_depth, color\_type,  
interlace\_type, compression\_type, filter\_type = rpng.get\_IHDR

puts width # 20#for i in (0…height)

# buffer += data[i].pack(“C\*”)

#end  
puts height # 20  
puts bit\_depth # 8  
puts color\_type # 6 … not sure what this means yet  
#for i in (0…height)

# buffer += data[i].pack(“C\*”)

#end

# Create and bind to OGL texture

@testogl = GL::GenTextures(1)[0];  
GL.BindTexture(GL::TEXTURE\_2D, @testogl);  
GL.TexParameteri(GL::TEXTURE\_2D, GL::TEXTURE\_WRAP\_S, GL::CLAMP);  
GL.TexParameteri(GL::TEXTURE\_2D, GL::TEXTURE\_WRAP\_T, GL::CLAMP);  
GL.TexParameteri(GL::TEXTURE\_2D, GL::TEXTURE\_MAG\_FILTER,GL::LINEAR);  
GL.TexParameteri(GL::TEXTURE\_2D, GL::TEXTURE\_MIN\_FILTER,GL::LINEAR);

# Abomination follows

data = rpng.read\_image  
data2 = data.flatten  
data3 = “”  
for i in (0…data2.length-1)  
data3 += " "  
data3[i] = data2[i]  
end

GL.TexImage2D(GL::TEXTURE\_2D, 0, GL::RED, width,  
height, 0, GL::RED, GL::UNSIGNED\_BYTE, data);

---

<div class="post-metadata">

**Author:** ![xavier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/2bfe46/32.png) [@xavier](https://www.ruby-forum.com/u/xavier)\
**Post date:** [May 30, 2006, 12:28am UTC](https://www.ruby-forum.com/t/png-to-opengl-texture-libpng/61881/2 "2006-05-30T00:28:12Z")

</div>

Hi,

On 5/29/06, Xavier [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Any Ideas?
> 
> As an aside, if anyone has a better x-platform way of doing this I’m all  
> ears…
> 
> - Xavier

With RMagick you can load image data in a  
suitable format for gl textures like this  
(dunno if there’s a better way):

require ‘RMagick’

image = RMagick::Image.read(filename).first  
rgba\_data = image.to\_blob{|i|  
i.format = “RGBA”  
i.depth = 8  
}

And with Imlib2:

require ‘imlib2’

image = Imlib2::Image.load(filename)  
data = image.data # IIRC the channel order depends on system endianness  
here

No idea about libpng, sorry :I

HTH,  
Ilmari
