Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/torch/image.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCédric Deltheil <cedric@moodstocks.com>2016-03-19 22:59:47 +0300
committerCédric Deltheil <cedric@moodstocks.com>2016-03-19 22:59:47 +0300
commit88de2816b4a56982dbd6cd57b67d63afebfcb206 (patch)
tree6f9400ce6dbb7354800c1a53faa175de051b6cdc
parent378c5c617c01f876b7162c43785f6a7693eee734 (diff)
jpeg: auto-detect jpeg_mem_src and jpeg_mem_dest
If not defined (e.g. libjpeg < v8) then return a meaningful error at runtime.
-rw-r--r--CMakeLists.txt11
-rwxr-xr-xgeneric/jpeg.c19
-rw-r--r--jpeg.c26
3 files changed, 53 insertions, 3 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index c9e3320..d19b863 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -46,6 +46,17 @@ ENDIF()
if (JPEG_FOUND)
SET(src jpeg.c)
include_directories (${JPEG_INCLUDE_DIR})
+ SET(CMAKE_REQUIRED_INCLUDES "${JPEG_INCLUDE_DIR}")
+ SET(CMAKE_REQUIRED_LIBRARIES "${JPEG_LIBRARY}")
+ INCLUDE(CheckSymbolExists)
+ CHECK_SYMBOL_EXISTS(jpeg_mem_src "stddef.h;stdio.h;jpeglib.h" HAVE_JPEG_MEM_SRC)
+ IF (HAVE_JPEG_MEM_SRC)
+ SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DHAVE_JPEG_MEM_SRC")
+ ENDIF (HAVE_JPEG_MEM_SRC)
+ CHECK_SYMBOL_EXISTS(jpeg_mem_dest "stddef.h;stdio.h;jpeglib.h" HAVE_JPEG_MEM_DEST)
+ IF (HAVE_JPEG_MEM_DEST)
+ SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DHAVE_JPEG_MEM_DEST")
+ ENDIF (HAVE_JPEG_MEM_DEST)
ADD_TORCH_PACKAGE(jpeg "${src}" "${luasrc}" "Image Processing")
TARGET_LINK_LIBRARIES(jpeg luaT TH ${JPEG_LIBRARIES})
IF(LUALIB)
diff --git a/generic/jpeg.c b/generic/jpeg.c
index e904171..1fd1e70 100755
--- a/generic/jpeg.c
+++ b/generic/jpeg.c
@@ -189,6 +189,14 @@ static int libjpeg_(Main_size)(lua_State *L)
static int libjpeg_(Main_load)(lua_State *L)
{
+ const int load_from_file = luaL_checkint(L, 1);
+
+#if !defined(HAVE_JPEG_MEM_SRC)
+ if (load_from_file != 1) {
+ luaL_error(L, JPEG_MEM_SRC_ERR_MSG);
+ }
+#endif
+
/* This struct contains the JPEG decompression parameters and pointers to
* working space (which is allocated as needed by the JPEG library).
*/
@@ -207,7 +215,6 @@ static int libjpeg_(Main_load)(lua_State *L)
int i, k;
THTensor *tensor = NULL;
- const int load_from_file = luaL_checkint(L, 1);
if (load_from_file == 1) {
const char *filename = luaL_checkstring(L, 2);
@@ -369,6 +376,14 @@ static int libjpeg_(Main_load)(lua_State *L)
*
*/
int libjpeg_(Main_save)(lua_State *L) {
+ const int save_to_file = luaL_checkint(L, 3);
+
+#if !defined(HAVE_JPEG_MEM_DEST)
+ if (save_to_file != 1) {
+ luaL_error(L, JPEG_MEM_DEST_ERR_MSG);
+ }
+#endif
+
unsigned char *inmem = NULL; /* destination memory (if saving to memory) */
unsigned long inmem_size = 0; /* destination memory size (bytes) */
@@ -378,8 +393,6 @@ int libjpeg_(Main_save)(lua_State *L) {
THTensor *tensorc = THTensor_(newContiguous)(tensor);
real *tensor_data = THTensor_(data)(tensorc);
- const int save_to_file = luaL_checkint(L, 3);
-
THByteTensor* tensor_dest = NULL;
if (save_to_file == 0) {
tensor_dest = luaT_checkudata(L, 5, "torch.ByteTensor");
diff --git a/jpeg.c b/jpeg.c
index d36f221..ae4ce14 100644
--- a/jpeg.c
+++ b/jpeg.c
@@ -13,6 +13,32 @@
#define torch_Tensor TH_CONCAT_STRING_3(torch., Real, Tensor)
#define libjpeg_(NAME) TH_CONCAT_3(libjpeg_, Real, NAME)
+static void
+jpeg_mem_src_dummy(j_decompress_ptr c, unsigned char *ibuf, unsigned long isiz)
+{
+}
+
+static void
+jpeg_mem_dest_dummy(j_compress_ptr c, unsigned char **obuf, unsigned long *osiz)
+{
+}
+
+#define JPEG_MEM_SRC_NOT_DEF "`jpeg_mem_src` is not defined."
+#define JPEG_MEM_DEST_NOT_DEF "`jpeg_mem_dest` is not defined."
+#define JPEG_REQUIRED_VERSION " Use libjpeg v8+, libjpeg-turbo 1.3+ or build" \
+ " libjpeg-turbo with `--with-mem-srcdst`."
+
+#define JPEG_MEM_SRC_ERR_MSG JPEG_MEM_SRC_NOT_DEF JPEG_REQUIRED_VERSION
+#define JPEG_MEM_DEST_ERR_MSG JPEG_MEM_DEST_NOT_DEF JPEG_REQUIRED_VERSION
+
+#if !defined(HAVE_JPEG_MEM_SRC)
+#define jpeg_mem_src jpeg_mem_src_dummy
+#endif
+
+#if !defined(HAVE_JPEG_MEM_DEST)
+#define jpeg_mem_dest jpeg_mem_dest_dummy
+#endif
+
#include "generic/jpeg.c"
#include "THGenerateAllTypes.h"