[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] ARGB vs. RGBA



Brian Fisher wrote:
Hmmm.. they both look wrong to me... they look like GBAR

I agree, they certainly look that way (and that's a snippet from the SVN trunk). :-)


I would think lil endian would be this:
RMask = 0xFF<<8, GMask = 0xFF<<16, BMask = 0xFF<<24, AMask = 0xFF

and big endian would be:
RMask = 0xFF<<16, GMask = 0xFF<<8, BMask = 0xFF, AMask = 0xFF<<24

so I think you are right about what the big-endian mask should be, but
it's not true that you just swap the argument order for big to little
endian - what you really do is swap the shifts (so shift 24 becomes
shift 0, 16 becomes 8, 8 becomes 16 and 0 becomes 24)

You're right, of course. Shame on me. :-)

so jakub, do you have code for a cairo/pygame sample that tests this?

Attached. This could be also tested on little endian (I don't know what's cairo order on little endians).


The image shown should be a triangle which is red, green, blue and black in consecutive quarters of the window (CW). Currently only one quarter is visible (the one which gets the 255 assignment into what pygame treats as the alpha channel).

--
regards,
Jakub Piotr CÅapa
#!/usr/bin/env python

import cairo

w, h = 128, 128

# Draw the image using cairo
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h)
ctx = cairo.Context(surface)
ctx.set_line_width(15)
ctx.move_to(w/2, h/3)
ctx.line_to(2*w/3, 2*h/3)
ctx.rel_line_to(-1*w/3, 0)
ctx.close_path()
ctx.stroke()
surface.write_to_png("triangle-cairo.png")

buf = surface.get_data ()

# Make some modifications using numpy
import numpy

a = numpy.frombuffer(buf, numpy.uint8)
a.shape = (w, h, 4)

a[:64,:64,0] = 255
a[:64,64:,1] = 255
a[64:,:64,2] = 255
a[64:,64:,3] = 255

# bork the first pixel to check byte orders
# when later retrieving this pixel under PIL
a[0,0,:] = [0,1,2,3]
surface.write_to_png("triangle-numpy.png")

# Alias the image as a pygame surface
import pygame
from time import sleep

# This works but copies the data:
# import re
# imsurf = pygame.image.fromstring(re.sub (r"(.)(...)", r"\2\1", buf), (w,h), "RGBA")

# This doesn't:
imsurf = pygame.image.frombuffer (buf, (w,h), "ARGB")

print imsurf.get_at ((0, 0))

pygame.display.init()
surface = pygame.display.set_mode((w,h), pygame.DOUBLEBUF, 32)

done = False
while not done:
  surface.fill((255,255,255))
  surface.blit(imsurf, (0,0))
  sleep(0.1)
  events = pygame.event.get()
  for event in events:
    if event.type == pygame.QUIT:
      done = True
  pygame.display.flip()