|
@@ -23,8 +23,9 @@ misrepresented as being the original software.
|
|
--]]
|
|
--]]
|
|
|
|
|
|
local ImageData_mt, ffifuncspointer = ...
|
|
local ImageData_mt, ffifuncspointer = ...
|
|
|
|
+local ImageData = ImageData_mt.__index
|
|
|
|
|
|
-local tonumber, assert = tonumber, assert
|
|
|
|
|
|
+local tonumber, assert, error = tonumber, assert, error
|
|
local type, pcall = type, pcall
|
|
local type, pcall = type, pcall
|
|
|
|
|
|
local function inside(x, y, w, h)
|
|
local function inside(x, y, w, h)
|
|
@@ -33,7 +34,7 @@ end
|
|
|
|
|
|
-- Implement thread-safe ImageData:mapPixel regardless of whether the FFI is
|
|
-- Implement thread-safe ImageData:mapPixel regardless of whether the FFI is
|
|
-- used or not.
|
|
-- used or not.
|
|
-function ImageData_mt.__index:mapPixel(func, ix, iy, iw, ih)
|
|
|
|
|
|
+function ImageData:mapPixel(func, ix, iy, iw, ih)
|
|
local idw, idh = self:getDimensions()
|
|
local idw, idh = self:getDimensions()
|
|
|
|
|
|
ix = ix or 0
|
|
ix = ix or 0
|
|
@@ -41,13 +42,13 @@ function ImageData_mt.__index:mapPixel(func, ix, iy, iw, ih)
|
|
iw = iw or idw
|
|
iw = iw or idw
|
|
ih = ih or idh
|
|
ih = ih or idh
|
|
|
|
|
|
- assert(type(ix) == "number", "Invalid argument #2 to ImageData:mapPixel (expected number)")
|
|
|
|
- assert(type(iy) == "number", "Invalid argument #3 to ImageData:mapPixel (expected number)")
|
|
|
|
- assert(type(iw) == "number", "Invalid argument #4 to ImageData:mapPixel (expected number)")
|
|
|
|
- assert(type(ih) == "number", "Invalid argument #5 to ImageData:mapPixel (expected number)")
|
|
|
|
|
|
+ if type(ix) ~= "number" then error("Invalid argument #2 to ImageData:mapPixel (expected number)", 2) end
|
|
|
|
+ if type(iy) ~= "number" then error("Invalid argument #3 to ImageData:mapPixel (expected number)", 2) end
|
|
|
|
+ if type(iw) ~= "number" then error("Invalid argument #4 to ImageData:mapPixel (expected number)", 2) end
|
|
|
|
+ if type(ih) ~= "number" then error("Invalid argument #5 to ImageData:mapPixel (expected number)", 2) end
|
|
|
|
|
|
- assert(type(func) == "function", "Invalid argument #1 to ImageData:mapPixel (expected function)")
|
|
|
|
- assert(inside(ix, iy, idw, idh) and inside(ix+iw-1, iy+ih-1, idw, idh), "Invalid rectangle dimensions")
|
|
|
|
|
|
+ if type(func) ~= "function" then error("Invalid argument #1 to ImageData:mapPixel (expected function)", 2) end
|
|
|
|
+ if not (inside(ix, iy, idw, idh) and inside(ix+iw-1, iy+ih-1, idw, idh)) then error("Invalid rectangle dimensions", 2) end
|
|
|
|
|
|
-- performAtomic and mapPixelUnsafe have Lua-C API and FFI versions.
|
|
-- performAtomic and mapPixelUnsafe have Lua-C API and FFI versions.
|
|
self:_performAtomic(self._mapPixelUnsafe, self, func, ix, iy, iw, ih)
|
|
self:_performAtomic(self._mapPixelUnsafe, self, func, ix, iy, iw, ih)
|
|
@@ -85,9 +86,9 @@ local ffifuncs = ffi.cast("FFI_ImageData *", ffifuncspointer)
|
|
|
|
|
|
local pixelpointer = ffi.typeof("ImageData_Pixel *")
|
|
local pixelpointer = ffi.typeof("ImageData_Pixel *")
|
|
|
|
|
|
-local _getWidth = ImageData_mt.__index.getWidth
|
|
|
|
-local _getHeight = ImageData_mt.__index.getHeight
|
|
|
|
-local _getDimensions = ImageData_mt.__index.getDimensions
|
|
|
|
|
|
+local _getWidth = ImageData.getWidth
|
|
|
|
+local _getHeight = ImageData.getHeight
|
|
|
|
+local _getDimensions = ImageData.getDimensions
|
|
|
|
|
|
-- Table which holds ImageData objects as keys, and information about the objects
|
|
-- Table which holds ImageData objects as keys, and information about the objects
|
|
-- as values. Uses weak keys so the ImageData objects can still be GC'd properly.
|
|
-- as values. Uses weak keys so the ImageData objects can still be GC'd properly.
|
|
@@ -111,7 +112,7 @@ local objectcache = setmetatable({}, {
|
|
|
|
|
|
-- Overwrite existing functions with new FFI versions.
|
|
-- Overwrite existing functions with new FFI versions.
|
|
|
|
|
|
-function ImageData_mt.__index:_performAtomic(...)
|
|
|
|
|
|
+function ImageData:_performAtomic(...)
|
|
ffifuncs.lockMutex(self)
|
|
ffifuncs.lockMutex(self)
|
|
local success, err = pcall(...)
|
|
local success, err = pcall(...)
|
|
ffifuncs.unlockMutex(self)
|
|
ffifuncs.unlockMutex(self)
|
|
@@ -121,7 +122,7 @@ function ImageData_mt.__index:_performAtomic(...)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
|
|
-function ImageData_mt.__index:_mapPixelUnsafe(func, ix, iy, iw, ih)
|
|
|
|
|
|
+function ImageData:_mapPixelUnsafe(func, ix, iy, iw, ih)
|
|
local p = objectcache[self]
|
|
local p = objectcache[self]
|
|
local idw, idh = p.width, p.height
|
|
local idw, idh = p.width, p.height
|
|
|
|
|
|
@@ -139,9 +140,9 @@ function ImageData_mt.__index:_mapPixelUnsafe(func, ix, iy, iw, ih)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
|
|
-function ImageData_mt.__index:getPixel(x, y)
|
|
|
|
|
|
+function ImageData:getPixel(x, y)
|
|
local p = objectcache[self]
|
|
local p = objectcache[self]
|
|
- assert(inside(x, y, p.width, p.height), "Attempt to get out-of-range pixel!")
|
|
|
|
|
|
+ if not inside(x, y, p.width, p.height) then error("Attempt to get out-of-range pixel!", 2) end
|
|
|
|
|
|
ffifuncs.lockMutex(self)
|
|
ffifuncs.lockMutex(self)
|
|
local pixel = p.pointer[y * p.width + x]
|
|
local pixel = p.pointer[y * p.width + x]
|
|
@@ -153,9 +154,9 @@ end
|
|
|
|
|
|
local temppixel = ffi.new("ImageData_Pixel")
|
|
local temppixel = ffi.new("ImageData_Pixel")
|
|
|
|
|
|
-function ImageData_mt.__index:setPixel(x, y, r, g, b, a)
|
|
|
|
|
|
+function ImageData:setPixel(x, y, r, g, b, a)
|
|
local p = objectcache[self]
|
|
local p = objectcache[self]
|
|
- assert(inside(x, y, p.width, p.height), "Attempt to set out-of-range pixel!")
|
|
|
|
|
|
+ if not inside(x, y, p.width, p.height) then error("Attempt to set out-of-range pixel!", 2) end
|
|
|
|
|
|
if type(r) == "table" then
|
|
if type(r) == "table" then
|
|
local t = r
|
|
local t = r
|
|
@@ -172,15 +173,15 @@ function ImageData_mt.__index:setPixel(x, y, r, g, b, a)
|
|
ffifuncs.unlockMutex(self)
|
|
ffifuncs.unlockMutex(self)
|
|
end
|
|
end
|
|
|
|
|
|
-function ImageData_mt.__index:getWidth()
|
|
|
|
|
|
+function ImageData:getWidth()
|
|
return objectcache[self].width
|
|
return objectcache[self].width
|
|
end
|
|
end
|
|
|
|
|
|
-function ImageData_mt.__index:getHeight()
|
|
|
|
|
|
+function ImageData:getHeight()
|
|
return objectcache[self].height
|
|
return objectcache[self].height
|
|
end
|
|
end
|
|
|
|
|
|
-function ImageData_mt.__index:getDimensions()
|
|
|
|
|
|
+function ImageData:getDimensions()
|
|
local p = objectcache[self]
|
|
local p = objectcache[self]
|
|
return p.width, p.height
|
|
return p.width, p.height
|
|
end
|
|
end
|