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/