forked from torch/image
-
Notifications
You must be signed in to change notification settings - Fork 0
/
png.c
83 lines (69 loc) · 1.81 KB
/
png.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <TH.h>
#include <luaT.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#define PNG_DEBUG 3
#include <png.h>
#define torch_(NAME) TH_CONCAT_3(torch_, Real, NAME)
#define torch_Tensor TH_CONCAT_STRING_3(torch., Real, Tensor)
#define libpng_(NAME) TH_CONCAT_3(libpng_, Real, NAME)
/*
* Bookkeeping struct for reading png data from memory
*/
typedef struct {
unsigned char* buffer;
png_size_t offset;
png_size_t length;
} libpng_inmem_buffer;
/*
* Call back for reading png data from memory
*/
static void
libpng_userReadData(png_structp pngPtrSrc, png_bytep dest, png_size_t length)
{
libpng_inmem_buffer* src = png_get_io_ptr(pngPtrSrc);
assert(src->offset+length <= src->length);
memcpy(dest, src->buffer + src->offset, length);
src->offset += length;
}
/*
* Error message wrapper (single member struct to preserve `str` size info)
*/
typedef struct {
char str[256];
} libpng_errmsg;
/*
* Custom error handling function (see `png_set_error_fn`)
*/
static void
libpng_error_fn(png_structp png_ptr, png_const_charp error_msg)
{
libpng_errmsg *errmsg = png_get_error_ptr(png_ptr);
int max = sizeof(errmsg->str) - 1;
strncpy(errmsg->str, error_msg, max);
errmsg->str[max] = '\0';
longjmp(png_jmpbuf(png_ptr), 1);
}
#include "generic/png.c"
#include "THGenerateAllTypes.h"
DLL_EXPORT int luaopen_libpng(lua_State *L)
{
libpng_FloatMain_init(L);
libpng_DoubleMain_init(L);
libpng_ByteMain_init(L);
lua_newtable(L);
lua_pushvalue(L, -1);
lua_setglobal(L, "libpng");
lua_newtable(L);
luaT_setfuncs(L, libpng_DoubleMain__, 0);
lua_setfield(L, -2, "double");
lua_newtable(L);
luaT_setfuncs(L, libpng_FloatMain__, 0);
lua_setfield(L, -2, "float");
lua_newtable(L);
luaT_setfuncs(L, libpng_ByteMain__, 0);
lua_setfield(L, -2, "byte");
return 1;
}