Skip to content

Commit

Permalink
Random shot in the dark...
Browse files Browse the repository at this point in the history
  • Loading branch information
NiLuJe committed Feb 20, 2019
1 parent 76de0f1 commit f5f47a0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
1 change: 1 addition & 0 deletions ffi-cdecl/lodepng_decl.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ cdecl_type(LodePNGInfo)
cdecl_struct(LodePNGState)
cdecl_type(LodePNGState)
cdecl_func(lodepng_error_text)
cdecl_func(lodepng_load_file)
cdecl_func(lodepng_decode32_file)
cdecl_func(lodepng_decode32)
cdecl_func(lodepng_decode24_file)
Expand Down
1 change: 1 addition & 0 deletions ffi/lodepng_h.lua
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ struct LodePNGState {
};
typedef struct LodePNGState LodePNGState;
const char *lodepng_error_text(unsigned int);
unsigned int lodepng_load_file(unsigned char **, unsigned int *, const char *);
unsigned int lodepng_decode32_file(unsigned char **, unsigned int *, unsigned int *, const char *);
unsigned int lodepng_decode32(unsigned char **, unsigned int *, unsigned int *, const unsigned char *, unsigned int);
unsigned int lodepng_decode24_file(unsigned char **, unsigned int *, unsigned int *, const char *);
Expand Down
14 changes: 11 additions & 3 deletions ffi/png.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,25 @@ end

function Png.decodeFromFile(filename, req_n)
-- Read the file
--[[
local fh = io.open(filename, "rb")
if not fh then
return false, "couldn't open PNG file"
end
local fdata = fh:read("*a")
fh:close()
--]]
local png = ffi.new("unsigned char*[1]")
local pngsize = ffi.new("unsigned int[1]")
local rf = lodepng.lodepng_load_file(png, pngsize, filename)
if rf ~= 0 then
return false, ffi.string(lodepng.lodepng_error_text(rf))
end
local ptr = ffi.new("unsigned char*[1]")
local width = ffi.new("int[1]")
local height = ffi.new("int[1]")
local state = ffi.new("LodePNGState[1]")
local ptr = ffi.new("unsigned char*[1]")
local out_n = req_n
-- Init the state
Expand All @@ -51,7 +59,7 @@ function Png.decodeFromFile(filename, req_n)
state[0].info_raw.bitdepth = 8
-- Inspect the PNG data first, to see if we can avoid a color-type conversion
local err = lodepng.lodepng_inspect(width, height, state, ffi.cast("const unsigned char*", fdata), #fdata);
local err = lodepng.lodepng_inspect(width, height, state, png[0], pngsize[0]);
if err ~= 0 then
return false, ffi.string(lodepng.lodepng_error_text(err))
end
Expand Down Expand Up @@ -96,7 +104,7 @@ function Png.decodeFromFile(filename, req_n)
return false, "requested an invalid number of color components"
end
local re = lodepng.lodepng_decode(ptr, width, height, state, ffi.cast("const unsigned char*", fdata), #fdata)
local re = lodepng.lodepng_decode(ptr, width, height, state, png[0], pngsize[0])
lodepng.lodepng_state_cleanup(state)
if re ~= 0 then
return false, ffi.string(lodepng.lodepng_error_text(re))
Expand Down

0 comments on commit f5f47a0

Please sign in to comment.