forked from liuliu/ccv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathccv_io.c
298 lines (289 loc) · 7.52 KB
/
ccv_io.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include "ccv.h"
#include "ccv_internal.h"
#ifdef HAVE_LIBPNG
#ifdef __APPLE__
#include "TargetConditionals.h"
#if TARGET_OS_IPHONE
// iOS
#elif TARGET_IPHONE_SIMULATOR
// iOS Simulator
#elif TARGET_OS_MAC
#include <zlib.h>
#include <png.h>
#else
// Unsupported platform
#endif
#else
#include <zlib.h>
#include <png.h>
#endif
#include "io/_ccv_io_libpng.inc"
#endif
#ifdef HAVE_LIBJPEG
#include <jpeglib.h>
#include "io/_ccv_io_libjpeg.inc"
#endif
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
#include <sys/param.h>
#endif
#include "io/_ccv_io_bmp.inc"
#include "io/_ccv_io_binary.inc"
#include "io/_ccv_io_raw.inc"
static int _ccv_read_and_close_fd(FILE* fd, ccv_dense_matrix_t** x, int type)
{
int ctype = (type & 0xF00) ? CCV_8U | ((type & 0xF00) >> 8) : 0;
if ((type & 0XFF) == CCV_IO_ANY_FILE)
{
unsigned char sig[8];
(void) fread(sig, 1, 8, fd);
if (memcmp(sig, "\x89\x50\x4e\x47\xd\xa\x1a\xa", 8) == 0)
type = CCV_IO_PNG_FILE;
else if (memcmp(sig, "\xff\xd8\xff", 3) == 0)
type = CCV_IO_JPEG_FILE;
else if (memcmp(sig, "BM", 2) == 0)
type = CCV_IO_BMP_FILE;
else if (memcmp(sig, "CCVBINDM", 8) == 0)
type = CCV_IO_BINARY_FILE;
fseek(fd, 0, SEEK_SET);
}
switch (type & 0XFF)
{
#ifdef HAVE_LIBJPEG
case CCV_IO_JPEG_FILE:
_ccv_read_jpeg_fd(fd, x, ctype);
break;
#endif
#ifdef HAVE_LIBPNG
case CCV_IO_PNG_FILE:
_ccv_read_png_fd(fd, x, ctype);
break;
#endif
case CCV_IO_BMP_FILE:
_ccv_read_bmp_fd(fd, x, ctype);
break;
case CCV_IO_BINARY_FILE:
_ccv_read_binary_fd(fd, x, ctype);
}
if (*x != 0)
ccv_make_matrix_immutable(*x);
if (type & CCV_IO_ANY_FILE)
fclose(fd);
return CCV_IO_FINAL;
}
static int _ccv_read_raw(ccv_dense_matrix_t** x, void* data, int type, int rows, int cols, int scanline)
{
assert(rows > 0 && cols > 0 && scanline > 0);
if (type & CCV_IO_NO_COPY)
{
// there is no conversion that we can apply if it is NO_COPY mode
// NO_COPY mode generate an "unreusable" matrix, which requires you to
// manually release its data block (which is, in fact the same data
// block you passed in)
int ctype = CCV_8U | CCV_C1;
switch (type & 0xFF)
{
case CCV_IO_RGB_RAW:
case CCV_IO_BGR_RAW:
ctype = CCV_8U | CCV_C3;
break;
case CCV_IO_RGBA_RAW:
case CCV_IO_ARGB_RAW:
case CCV_IO_BGRA_RAW:
case CCV_IO_ABGR_RAW:
ctype = CCV_8U | CCV_C4;
break;
case CCV_IO_GRAY_RAW:
default:
/* default one */
break;
}
*x = ccv_dense_matrix_new(rows, cols, ctype | CCV_NO_DATA_ALLOC, data, 0);
(*x)->step = scanline;
} else {
switch (type & 0xFF)
{
case CCV_IO_RGB_RAW:
_ccv_read_rgb_raw(x, data, type, rows, cols, scanline);
break;
case CCV_IO_RGBA_RAW:
_ccv_read_rgba_raw(x, data, type, rows, cols, scanline);
break;
case CCV_IO_ARGB_RAW:
_ccv_read_argb_raw(x, data, type, rows, cols, scanline);
break;
case CCV_IO_BGR_RAW:
_ccv_read_bgr_raw(x, data, type, rows, cols, scanline);
break;
case CCV_IO_BGRA_RAW:
_ccv_read_bgra_raw(x, data, type, rows, cols, scanline);
break;
case CCV_IO_ABGR_RAW:
_ccv_read_abgr_raw(x, data, type, rows, cols, scanline);
break;
case CCV_IO_GRAY_RAW:
_ccv_read_gray_raw(x, data, type, rows, cols, scanline);
break;
}
}
if (*x != 0)
ccv_make_matrix_immutable(*x);
return CCV_IO_FINAL;
}
#if defined(__APPLE__) || defined(__OpenBSD__) || defined(__FreeBSD__)
typedef struct {
char* buffer;
off_t pos;
size_t size;
} ccv_io_mem_t;
static int readfn(void* context, char* buf, int size)
{
ccv_io_mem_t* mem = (ccv_io_mem_t*)context;
if (size + mem->pos > mem->size)
size = mem->size - mem->pos;
memcpy(buf, mem->buffer + mem->pos, size);
mem->pos += size;
return size;
}
static off_t seekfn(void* context, off_t off, int whence)
{
ccv_io_mem_t* mem = (ccv_io_mem_t*)context;
off_t pos;
switch (whence)
{
case SEEK_SET:
pos = off;
break;
case SEEK_CUR:
pos = mem->pos + off;
break;
case SEEK_END:
pos = mem->size + off;
break;
}
if (pos >= mem->size)
return -1;
mem->pos = pos;
return pos;
}
static int writefn(void* context, const char* buf, int size)
{
ccv_io_mem_t* mem = (ccv_io_mem_t*)context;
if (size + mem->pos > mem->size)
return -1;
memcpy(mem->buffer + mem->pos, buf, size);
mem->pos += size;
return size;
}
#endif
int ccv_read_impl(const void* in, ccv_dense_matrix_t** x, int type, int rows, int cols, int scanline)
{
FILE* fd = 0;
if (type & CCV_IO_ANY_FILE)
{
assert(rows == 0 && cols == 0 && scanline == 0);
fd = fopen((const char*)in, "rb");
if (!fd)
return CCV_IO_ERROR;
return _ccv_read_and_close_fd(fd, x, type);
} else if (type & CCV_IO_ANY_STREAM) {
assert(rows > 8 && cols == 0 && scanline == 0);
assert((type & 0xFF) != CCV_IO_DEFLATE_STREAM); // deflate stream (compressed stream) is not supported yet
#if _XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L || defined(__APPLE__) || defined(__OpenBSD__) || defined(__FreeBSD__)
// this is only supported by glibc
#if _XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L
fd = fmemopen((void*)in, (size_t)rows, "rb");
#else
ccv_io_mem_t mem = {
.size = rows,
.pos = 0,
.buffer = (char*)in,
};
fd = funopen(&mem, readfn, 0, seekfn, 0);
#endif
if (!fd)
return CCV_IO_ERROR;
// mimicking itself as a "file"
type = (type & ~0x10) | 0x20;
return _ccv_read_and_close_fd(fd, x, type);
#endif
} else if (type & CCV_IO_ANY_RAW) {
return _ccv_read_raw(x, (void*)in /* it can be modifiable if it is NO_COPY mode */, type, rows, cols, scanline);
}
return CCV_IO_UNKNOWN;
}
int ccv_write(ccv_dense_matrix_t* mat, char* const out, size_t* const len, int type, void* conf)
{
FILE* fd = 0;
#if defined(__APPLE__) || defined(__OpenBSD__) || defined(__FreeBSD__)
ccv_io_mem_t mem = {0};
#endif
if (type & CCV_IO_ANY_FILE)
{
fd = fopen(out, "wb");
if (!fd)
return CCV_IO_ERROR;
} else if ((type & CCV_IO_ANY_STREAM) && type != CCV_IO_PLAIN_STREAM) {
assert(len);
assert((type & 0xFF) != CCV_IO_DEFLATE_STREAM); // deflate stream (compressed stream) is not supported yet
#if _XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L || defined(__APPLE__) || defined(__OpenBSD__) || defined(__FreeBSD__)
// this is only supported by glibc
#if _XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L
fd = fmemopen((void*)out, *len, "wb");
#else
mem.size = *len;
mem.buffer = out;
fd = funopen(&mem, 0, writefn, seekfn, 0);
#endif
#endif
}
int err = 0;
switch (type)
{
case CCV_IO_JPEG_FILE:
#ifdef HAVE_LIBJPEG
err = _ccv_write_jpeg_fd(mat, fd, conf);
if (len != 0)
*len = 0;
#else
assert(0 && "ccv_write requires libjpeg support for JPEG format");
#endif
break;
case CCV_IO_PNG_FILE:
#ifdef HAVE_LIBPNG
err = _ccv_write_png_fd(mat, fd, conf);
if (len != 0)
*len = 0;
#else
assert(0 && "ccv_write requires libpng support for PNG format");
#endif
break;
case CCV_IO_BINARY_FILE:
_ccv_write_binary_fd(mat, fd, conf);
if (len != 0)
*len = 0;
break;
case CCV_IO_JPEG_STREAM:
#ifdef HAVE_LIBJPEG
err = _ccv_write_jpeg_fd(mat, fd, conf);
#else
assert(0 && "ccv_write requires libjpeg support for JPEG format");
#endif
break;
case CCV_IO_PNG_STREAM:
#ifdef HAVE_LIBPNG
err = _ccv_write_png_fd(mat, fd, conf);
#else
assert(0 && "ccv_write requires libpng support for PNG format");
#endif
break;
case CCV_IO_PLAIN_STREAM:
err = _ccv_write_plain_stream(mat, out, *len);
*len = 20 + mat->step * mat->rows;
break;
}
if ((type & CCV_IO_ANY_STREAM) && type != CCV_IO_PLAIN_STREAM)
*len = (size_t)ftell(fd);
if (fd)
fclose(fd);
return err != 0 ? CCV_IO_ERROR : CCV_IO_FINAL;
}