[lua-torch-image] 01/07: New upstream version 0~20170420-g5aa1881
Zhou Mo
cdluminate-guest at moszumanska.debian.org
Sat May 20 09:01:57 UTC 2017
This is an automated email from the git hooks/post-receive script.
cdluminate-guest pushed a commit to branch master
in repository lua-torch-image.
commit 01203fa1a8361eb5180f07607fed4ca68c2b219d
Author: Zhou Mo <cdluminate at gmail.com>
Date: Sat May 20 08:49:18 2017 +0000
New upstream version 0~20170420-g5aa1881
---
CMakeLists.txt | 6 +++---
generic/image.c | 31 +++++++++++++++++--------------
generic/png.c | 53 ++++++++++++++++++++++++++++++++++++++++-------------
init.lua | 37 ++++++++++++++++++++++++++++---------
png.c | 21 ++++++++++++++++++++-
test/test.lua | 13 +++++++++++++
6 files changed, 121 insertions(+), 40 deletions(-)
diff --git a/CMakeLists.txt b/CMakeLists.txt
index d19b863..bc582c1 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -69,10 +69,10 @@ endif (JPEG_FOUND)
if (PNG_FOUND)
SET(src png.c)
include_directories (${PNG_INCLUDE_DIR})
- ADD_TORCH_PACKAGE(png "${src}" "${luasrc}" "Image Processing")
- TARGET_LINK_LIBRARIES(png luaT TH ${PNG_LIBRARIES})
+ ADD_TORCH_PACKAGE(lua_png "${src}" "${luasrc}" "Image Processing")
+ TARGET_LINK_LIBRARIES(lua_png luaT TH ${PNG_LIBRARIES})
IF(LUALIB)
- TARGET_LINK_LIBRARIES(png ${LUALIB})
+ TARGET_LINK_LIBRARIES(lua_png ${LUALIB})
ENDIF()
else (PNG_FOUND)
message ("WARNING: Could not find PNG libraries, PNG wrapper will not be installed")
diff --git a/generic/image.c b/generic/image.c
index 1f5163b..36b105c 100755
--- a/generic/image.c
+++ b/generic/image.c
@@ -2227,27 +2227,30 @@ int image_(Main_drawRect)(lua_State *L) {
int cg = luaL_checkint(L, 8);
int cb = luaL_checkint(L, 9);
- int offset = lineWidth / 2;
- int x1 = (int) MAX(0, x1long - offset - 1);
- int y1 = (int) MAX(0, y1long - offset - 1);
- int x2 = (int) MIN(output->size[2] - 1, x2long - offset - 1);
- int y2 = (int) MIN(output->size[1] - 1, y2long - offset - 1);
-
- int w = x2 - x1 + 1;
- int h = y2 - y1 + 1;
- for (int y = y1; y < y2 + lineWidth; y++) {
- for (int x = x1; x < x1 + lineWidth; x++) {
+ int loffset = lineWidth / 2 + 1;
+ int uoffset = lineWidth - loffset - 1;
+ int x1l = (int) MAX(0, x1long - loffset);
+ int y1l = (int) MAX(0, y1long - loffset);
+ int x1u = (int) MIN(output->size[2], x1long + uoffset + 1);
+ int y1u = (int) MIN(output->size[1], y1long + uoffset + 1);
+ int x2l = (int) MAX(0, x2long - loffset);
+ int y2l = (int) MAX(0, y2long - loffset);
+ int x2u = (int) MIN(output->size[2], x2long + uoffset + 1);
+ int y2u = (int) MIN(output->size[1], y2long + uoffset + 1);
+
+ for (int y = y1l; y < y2u; y++) {
+ for (int x = x1l; x < x1u; x++) {
image_(drawPixel)(output, y, x, cr, cg, cb);
}
- for (int x = x2; x < x2 + lineWidth; x++) {
+ for (int x = x2l; x < x2u; x++) {
image_(drawPixel)(output, y, x, cr, cg, cb);
}
}
- for (int x = x1; x < x2 + lineWidth; x++) {
- for (int y = y1; y < y1 + lineWidth; y++) {
+ for (int x = x1l; x < x2u; x++) {
+ for (int y = y1l; y < y1u; y++) {
image_(drawPixel)(output, y, x, cr, cg, cb);
}
- for (int y = y2; y < y2 + lineWidth; y++) {
+ for (int y = y2l; y < y2u; y++) {
image_(drawPixel)(output, y, x, cr, cg, cb);
}
}
diff --git a/generic/png.c b/generic/png.c
index 5613236..75782bb 100755
--- a/generic/png.c
+++ b/generic/png.c
@@ -198,11 +198,17 @@ static int libpng_(Main_load)(lua_State *L)
return 2;
}
+
static int libpng_(Main_save)(lua_State *L)
{
THTensor *tensor = luaT_checkudata(L, 2, torch_Tensor);
const char *file_name = luaL_checkstring(L, 1);
-
+ const int save_to_file = luaL_checkint(L, 3);
+
+ struct libpng_inmem_write_struct _inmem;
+
+ THByteTensor* tensor_dest = NULL;
+
int width=0, height=0;
png_byte color_type = 0;
png_byte bit_depth = 8;
@@ -211,11 +217,17 @@ static int libpng_(Main_save)(lua_State *L)
png_infop info_ptr;
png_bytep * row_pointers;
libpng_errmsg errmsg;
+ FILE *fp=NULL;
/* get dims and contiguous tensor */
THTensor *tensorc = THTensor_(newContiguous)(tensor);
real *tensor_data = THTensor_(data)(tensorc);
long depth=0;
+
+ if (save_to_file == 0) {
+ tensor_dest = luaT_checkudata(L, 4, "torch.ByteTensor");
+ }
+
if (tensorc->nDimension == 3) {
depth = tensorc->size[0];
height = tensorc->size[1];
@@ -234,27 +246,34 @@ static int libpng_(Main_save)(lua_State *L)
else if (depth == 3) color_type = PNG_COLOR_TYPE_RGB;
else if (depth == 1) color_type = PNG_COLOR_TYPE_GRAY;
- /* create file */
- FILE *fp = fopen(file_name, "wb");
- if (!fp)
- luaL_error(L, "[write_png_file] File %s could not be opened for writing", file_name);
-
/* initialize stuff */
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr)
luaL_error(L, "[write_png_file] png_create_write_struct failed");
-
+
png_set_error_fn(png_ptr, &errmsg, libpng_error_fn, NULL);
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
luaL_error(L, "[write_png_file] png_create_info_struct failed");
-
- if (setjmp(png_jmpbuf(png_ptr)))
- luaL_error(L, "[write_png_file] Error during init_io: %s", errmsg.str);
-
- png_init_io(png_ptr, fp);
+
+
+ /* create file */
+ if(save_to_file)
+ {
+ fp = fopen(file_name, "wb");
+ if (!fp)
+ luaL_error(L, "[write_png_file] File %s could not be opened for writing", file_name);
+
+ if (setjmp(png_jmpbuf(png_ptr)))
+ luaL_error(L, "[write_png_file] Error during init_io: %s", errmsg.str);
+ png_init_io(png_ptr, fp);
+ } else {
+ _inmem.inmem=NULL;
+ _inmem.inmem_size=0;
+ png_set_write_fn(png_ptr, &_inmem, libpng_userWriteData, NULL);
+ }
/* write header */
if (setjmp(png_jmpbuf(png_ptr)))
@@ -304,8 +323,16 @@ static int libpng_(Main_save)(lua_State *L)
free(row_pointers);
/* cleanup */
- fclose(fp);
+ if(fp) fclose(fp);
THTensor_(free)(tensorc);
+
+ if (save_to_file == 0) {
+
+ THByteTensor_resize1d(tensor_dest, _inmem.inmem_size); /* will fail if it's not a Byte Tensor */
+ unsigned char* tensor_dest_data = THByteTensor_data(tensor_dest);
+ memcpy(tensor_dest_data, _inmem.inmem, _inmem.inmem_size);
+ free(_inmem.inmem);
+ }
return 0;
}
diff --git a/init.lua b/init.lua
index e77f309..1ce9605 100644
--- a/init.lua
+++ b/init.lua
@@ -149,7 +149,7 @@ local function processPNG(img, depth, bit_depth, tensortype)
end
local function loadPNG(filename, depth, tensortype)
- if not xlua.require 'libpng' then
+ if not xlua.require 'liblua_png' then
dok.error('libpng package not found, please install libpng','image.loadPNG')
end
local load_from_file = 1
@@ -169,16 +169,17 @@ local function clampImage(tensor)
end
local function savePNG(filename, tensor)
- if not xlua.require 'libpng' then
+ if not xlua.require 'liblua_png' then
dok.error('libpng package not found, please install libpng','image.savePNG')
end
tensor = clampImage(tensor)
- tensor.libpng.save(filename, tensor)
+ local save_to_file = 1
+ tensor.libpng.save(filename, tensor, save_to_file)
end
rawset(image, 'savePNG', savePNG)
local function decompressPNG(tensor, depth, tensortype)
- if not xlua.require 'libpng' then
+ if not xlua.require 'liblua_png' then
dok.error('libpng package not found, please install libpng',
'image.decompressPNG')
end
@@ -197,12 +198,26 @@ end
rawset(image, 'decompressPNG', decompressPNG)
function image.getPNGsize(filename)
- if not xlua.require 'libpng' then
+ if not xlua.require 'liblua_png' then
dok.error('libpng package not found, please install libpng','image.getPNGsize')
end
return torch.Tensor().libpng.size(filename)
end
+local function compressPNG(tensor)
+ if not xlua.require 'liblua_png' then
+ dok.error('libpng package not found, please install libpng',
+ 'image.compressPNG')
+ end
+ tensor = clampImage(tensor)
+ local b = torch.ByteTensor()
+ local save_to_file = 0
+ tensor.libpng.save("", tensor, save_to_file, b)
+ return b
+end
+rawset(image, 'compressPNG', compressPNG)
+
+
local function processJPG(img, depth, tensortype)
local MAXVAL = 255
if tensortype ~= 'byte' then
@@ -1473,7 +1488,9 @@ local function toDisplayTensor(...)
for y = 1,ymaps do
for x = 1,xmaps do
if k > nmaps then break end
- grid:narrow(2,(y-1)*height+1+padding/2,height-padding):narrow(3,(x-1)*width+1+padding/2,width-padding):copy(packed[k])
+ grid:narrow(2, math.floor((y-1)*height+1+padding/2), math.floor(height-padding))
+ :narrow(3, math.floor((x-1)*width+1+padding/2), math.floor(width-padding))
+ :copy(packed[k])
k = k + 1
end
end
@@ -1493,7 +1510,9 @@ local function toDisplayTensor(...)
for y = 1,ymaps do
for x = 1,xmaps do
if k > nmaps then break end
- grid:narrow(1,(y-1)*height+1+padding/2,height-padding):narrow(2,(x-1)*width+1+padding/2,width-padding):copy(packed[k])
+ grid:narrow(1, math.floor((y-1)*height+1+padding/2),math.floor(height-padding))
+ :narrow(2,math.floor((x-1)*width+1+padding/2),math.floor(width-padding))
+ :copy(packed[k])
k = k + 1
end
end
@@ -1665,7 +1684,7 @@ local function lena()
local fname = 'grace_hopper_512'
if xlua.require 'libjpeg' then
lena = image.load(paths.concat(fpath(), 'assets', fname .. '.jpg'), 3)
- elseif xlua.require 'libpng' then
+ elseif xlua.require 'liblua_png' then
lena = image.load(paths.concat(fpath(), 'assets', fname .. '.png'), 3)
else
dok.error('no bindings available to load images (libjpeg AND libpng missing)', 'image.lena')
@@ -1684,7 +1703,7 @@ local function fabio()
local fname = 'fabio'
if xlua.require 'libjpeg' then
lena = image.load(paths.concat(fpath(), 'assets', fname .. '.jpg'), 1)
- elseif xlua.require 'libpng' then
+ elseif xlua.require 'liblua_png' then
lena = image.load(paths.concat(fpath(), 'assets', fname .. '.png'), 1)
else
dok.error('no bindings available to load images (libjpeg AND libpng missing)', 'image.fabio')
diff --git a/png.c b/png.c
index e69eee0..1e3f864 100644
--- a/png.c
+++ b/png.c
@@ -38,6 +38,25 @@ libpng_userReadData(png_structp pngPtrSrc, png_bytep dest, png_size_t length)
src->offset += length;
}
+
+struct libpng_inmem_write_struct
+{
+ unsigned char *inmem; /* destination memory (if saving to memory) */
+ unsigned long inmem_size; /* destination memory size (bytes) */
+};
+
+/*
+ * Call back for writing png data to memory
+ */
+static void libpng_userWriteData(png_structp png_ptr, png_bytep data, png_size_t length) {
+ struct libpng_inmem_write_struct *p = (struct libpng_inmem_write_struct*)png_get_io_ptr(png_ptr);
+ p->inmem=realloc(p->inmem,p->inmem_size+length);
+ memmove(p->inmem+p->inmem_size,data,length);
+ p->inmem_size+=length;
+}
+
+
+
/*
* Error message wrapper (single member struct to preserve `str` size info)
*/
@@ -61,7 +80,7 @@ libpng_error_fn(png_structp png_ptr, png_const_charp error_msg)
#include "generic/png.c"
#include "THGenerateAllTypes.h"
-DLL_EXPORT int luaopen_libpng(lua_State *L)
+DLL_EXPORT int luaopen_liblua_png(lua_State *L)
{
libpng_FloatMain_init(L);
libpng_DoubleMain_init(L);
diff --git a/test/test.lua b/test/test.lua
index 0a25cd9..839900a 100644
--- a/test/test.lua
+++ b/test/test.lua
@@ -410,8 +410,21 @@ function test.CompressAndDecompress()
img_compressed = image.compressJPG(img, quality)
local size_25 = img_compressed:size(1)
tester:assertlt(size_25, size_100, 'compressJPG quality setting error! ')
+
end
+function test.CompressAndDecompressPNG()
+ local img = image.lena()
+
+ local img_compressed_png = image.compressPNG(img)
+ local size_png = img_compressed_png:size(1)
+ local img_decompressed_png = image.decompressPNG(img_compressed_png)
+ local err_png = img_decompressed_png - img
+ local mean_err_png = err_png:mean()
+ local std_err_png = err_png:std()
+ tester:assertlt(mean_err_png, precision_mean, 'compressPNG error is too high! ')
+ tester:assertlt(std_err_png, precision_std, 'compressPNG error is too high! ')
+end
----------------------------------------------------------------------
-- Lab conversion test
-- These tests break if someone removes lena from the repo
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/lua-torch-image.git
More information about the debian-science-commits
mailing list