Skip to content

Commit

Permalink
fix small bug, add fast path for alpha blitting with values 0, 0xFF
Browse files Browse the repository at this point in the history
The default alpha value returned by ColorNN:getAlpha() is 0xFF now.
Also, add fast path (avoid complex calculation) for alpha-blitting
with alpha=0xFF and alpha=0.
  • Loading branch information
hwhw committed Oct 4, 2014
1 parent b7f548d commit e8385ad
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions ffi/blitbuffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ function ColorRGB32_mt.__index:getColorRGB32() return self end
function Color4L_mt.__index:getR() return self:getColor8().a end
Color4L_mt.__index.getG = Color4L_mt.__index.getR
Color4L_mt.__index.getB = Color4L_mt.__index.getR
function Color4L_mt.__index:getAlpha() return intt(0) end
function Color4L_mt.__index:getAlpha() return intt(0xFF) end
Color4U_mt.__index.getR = Color4L_mt.__index.getR
Color4U_mt.__index.getG = Color4L_mt.__index.getR
Color4U_mt.__index.getB = Color4L_mt.__index.getR
Expand Down Expand Up @@ -604,6 +604,10 @@ function BB_mt.__index:setPixel(x, y, color)
self:getPixelP(px, py)[0]:set(color)
end
function BB_mt.__index:setPixelAdd(x, y, color, alpha)
-- fast path:
if alpha == 0 then return
elseif alpha == 0xFF then return self:setPixel(x, y, color)
end
-- this method works with a grayscale value
local px, py = self:getPhysicalCoordinates(x, y)
color = color:getColor8A()
Expand All @@ -612,10 +616,14 @@ function BB_mt.__index:setPixelAdd(x, y, color, alpha)
self:getPixelP(px, py)[0]:blend(color)
end
function BBRGB16_mt.__index:setPixelAdd(x, y, color, alpha)
-- fast path:
if alpha == 0 then return
elseif alpha == 0xFF then return self:setPixel(x, y, color)
end
-- this method uses a RGB color value
local px, py = self:getPhysicalCoordinates(x, y)
color = color:getColorRGB32()
if self:getInverse() == 1 then color = color:invert() end
color = color:getColorRGB32()
color.alpha = alpha
self:getPixelP(px, py)[0]:blend(color)
end
Expand All @@ -627,10 +635,17 @@ function BB_mt.__index:setPixelBlend(x, y, color)
self:getPixelP(px, py)[0]:blend(color)
end
function BB_mt.__index:setPixelColorize(x, y, mask, color)
local px, py = self:getPhysicalCoordinates(x, y)
-- use 8bit grayscale pixel value as alpha for blitting
color.alpha = mask:getColor8().a
self:getPixelP(px, py)[0]:blend(color)
local alpha = mask:getColor8().a
-- fast path:
if alpha == 0 then return end
local px, py = self:getPhysicalCoordinates(x, y)
if alpha == 0xFF then
self:getPixelP(px, py)[0]:set(color)
else
color.alpha = alpha
self:getPixelP(px, py)[0]:blend(color)
end
end
function BB_mt.__index:setPixelInverted(x, y, color)
self:setPixel(x, y, color:invert())
Expand Down

0 comments on commit e8385ad

Please sign in to comment.