Imlib2 to OpenGL textures

A

AlexG

Hi,

I'm using Imlib2 to load textures for openGL. Imlib2's raw data output
seems to be four unsigned bytes for each pixel in the order: ARGB
(where A=alpha), whilst OpenGL expects RGBA. The code below adds a new
method to Imlib2::Image to move the alpha byte to the end of each pixel
(i.e. it turns 255 100 100 100 into 100 100 100 255) and seems to work
OK. My question is: Is there a more elegant (and/or quicker) way to do
this byte swapping? How do other people do this conversion?

class Imlib2::Image
def rgba_data
new_data = ""
i = 0
a = ""
self.data.each_byte do |byte|
i += 1
if i % 3 == 0
new_data << a
a = byte
else
new_data << byte
end
end
new_data << a
end
end

Alex Gutteridge

[1]: http://www.pablotron.org/software/imlib2-ruby/doc/
 
M

Matthew Desmarais

Hi Alex,

Have you tried something like this?

class Imlib2::Image
def rgba_data
self.data[1..3] << self.data[0]
end
end

Hope that helps...

Matthew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top