Created
January 19, 2012 19:33
-
-
Save jorgenpt/1642022 to your computer and use it in GitHub Desktop.
Things you shouldn't use Ruby for, #63
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Returns the framebuffer as an array of floats ranging from 0 to 1, ordered | |
# like [A, R, G, B, A, R, G, B, ..] | |
def to_a | |
red_mask = mask(@header.red_length) | |
green_mask = mask(@header.green_length) | |
blue_mask = mask(@header.blue_length) | |
alpha_mask = mask(@header.alpha_length) | |
red_max = (1 << @header.red_length) - 1.0 | |
green_max = (1 << @header.green_length) - 1.0 | |
blue_max = (1 << @header.blue_length) - 1.0 | |
alpha_max = (1 << @header.alpha_length) - 1.0 | |
format = nil | |
case @header.bpp | |
when 16 | |
format = 'v*' | |
when 32 | |
format = 'V*' | |
else | |
raise InvalidDataError, "Unsupported bpp: #{@header.bpp}" | |
end | |
data = @framebuffer.unpack(format).collect do |rgb| | |
red = ((rgb >> @header.red_offset) & red_mask) / red_max | |
green = ((rgb >> @header.green_offset) & green_mask) / green_max | |
blue = ((rgb >> @header.blue_offset) & blue_mask) / blue_max | |
if @header.alpha_length > 0 | |
alpha = ((rgb >> @header.alpha_offset) & alpha_mask) / alpha_max | |
end | |
[alpha, red, green, blue] | |
end | |
data.flatten | |
end | |
def mask(length) | |
(1 << length) - 1 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment