cimg.cxx 3.6.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CImg.h +70659 -0
- package/Licence_CeCILL-C_V1-en.txt +508 -0
- package/Licence_CeCILL_V2-en.txt +504 -0
- package/README.md +44 -0
- package/README.txt +180 -0
- package/package.json +15 -0
- package/plugins/add_fileformat.h +79 -0
- package/plugins/bayer.h +212 -0
- package/plugins/chlpca.h +323 -0
- package/plugins/cvMat.h +350 -0
- package/plugins/draw_gradient.h +269 -0
- package/plugins/inpaint.h +508 -0
- package/plugins/ipl.h +309 -0
- package/plugins/ipl_alt.h +122 -0
- package/plugins/jpeg_buffer.h +377 -0
- package/plugins/loop_macros.h +24166 -0
- package/plugins/matlab.h +287 -0
- package/plugins/nlmeans.h +242 -0
- package/plugins/skeleton.h +587 -0
- package/plugins/tiff_stream.h +196 -0
- package/plugins/tinymatwriter.h +109 -0
- package/plugins/vrml.h +894 -0
- package/plugins/vtk.h +103 -0
|
@@ -0,0 +1,377 @@
|
|
|
1
|
+
/*
|
|
2
|
+
#
|
|
3
|
+
# File : jpeg_buffer.h
|
|
4
|
+
# ( C++ header file - CImg plug-in )
|
|
5
|
+
#
|
|
6
|
+
# Description : This CImg plug-in provide functions to load and save jpeg images
|
|
7
|
+
# directly from/to memory buffers of JOCTET buffers, using the
|
|
8
|
+
# JPEG library (required to compile !)
|
|
9
|
+
# This file is a part of the CImg Library project.
|
|
10
|
+
# ( http://cimg.eu )
|
|
11
|
+
#
|
|
12
|
+
# Copyright : Paolo Prete
|
|
13
|
+
# ( p4olo_prete@yahoo.it )
|
|
14
|
+
#
|
|
15
|
+
# License : CeCILL v2.0
|
|
16
|
+
# ( http://www.cecill.info/licences/Licence_CeCILL_V2-en.html )
|
|
17
|
+
#
|
|
18
|
+
# This software is governed by the CeCILL license under French law and
|
|
19
|
+
# abiding by the rules of distribution of free software. You can use,
|
|
20
|
+
# modify and/ or redistribute the software under the terms of the CeCILL
|
|
21
|
+
# license as circulated by CEA, CNRS and INRIA at the following URL
|
|
22
|
+
# "http://www.cecill.info".
|
|
23
|
+
#
|
|
24
|
+
# As a counterpart to the access to the source code and rights to copy,
|
|
25
|
+
# modify and redistribute granted by the license, users are provided only
|
|
26
|
+
# with a limited warranty and the software's author, the holder of the
|
|
27
|
+
# economic rights, and the successive licensors have only limited
|
|
28
|
+
# liability.
|
|
29
|
+
#
|
|
30
|
+
# In this respect, the user's attention is drawn to the risks associated
|
|
31
|
+
# with loading, using, modifying and/or developing or reproducing the
|
|
32
|
+
# software by the user in light of its specific status of free software,
|
|
33
|
+
# that may mean that it is complicated to manipulate, and that also
|
|
34
|
+
# therefore means that it is reserved for developers and experienced
|
|
35
|
+
# professionals having in-depth computer knowledge. Users are therefore
|
|
36
|
+
# encouraged to load and test the software's suitability as regards their
|
|
37
|
+
# requirements in conditions enabling the security of their systems and/or
|
|
38
|
+
# data to be ensured and, more generally, to use and operate it in the
|
|
39
|
+
# same conditions as regards security.
|
|
40
|
+
#
|
|
41
|
+
# The fact that you are presently reading this means that you have had
|
|
42
|
+
# knowledge of the CeCILL license and that you accept its terms.
|
|
43
|
+
#
|
|
44
|
+
*/
|
|
45
|
+
|
|
46
|
+
/*-----------------------------------------------------------------------------------
|
|
47
|
+
|
|
48
|
+
IMPORTANT NOTE :
|
|
49
|
+
|
|
50
|
+
You *need* to include the following lines in your own code to use this plugin :
|
|
51
|
+
|
|
52
|
+
#include <cstdio>
|
|
53
|
+
#include <jpeglib.h>
|
|
54
|
+
#include <jerror.h>
|
|
55
|
+
|
|
56
|
+
(see example file provided in examples/use_jpeg_buffer.cpp).
|
|
57
|
+
|
|
58
|
+
------------------------------------------------------------------------------------*/
|
|
59
|
+
|
|
60
|
+
#ifndef cimg_plugin_jpeg_buffer
|
|
61
|
+
#define cimg_plugin_jpeg_buffer
|
|
62
|
+
|
|
63
|
+
///////////////////////////////////////////////////////////////////////////////////////
|
|
64
|
+
//
|
|
65
|
+
// extension of libjpeg (helper functions for loading images from JOCTET arrays)
|
|
66
|
+
// hacked from
|
|
67
|
+
// http://www.koders.com/cpp/fidB5A4549ABB5CB01824058F57A43D095D3F95AB40.aspx
|
|
68
|
+
//
|
|
69
|
+
///////////////////////////////////////////////////////////////////////////////////////
|
|
70
|
+
|
|
71
|
+
#define INPUT_BUF_SIZE 4096
|
|
72
|
+
|
|
73
|
+
struct my_source_mem {
|
|
74
|
+
struct jpeg_source_mgr pub; // Public fields
|
|
75
|
+
int indexinmem;
|
|
76
|
+
JOCTET *inmem; // Source stream
|
|
77
|
+
JOCTET *buffer; // Start of buffer
|
|
78
|
+
int lenght; // Size of buffer in memory
|
|
79
|
+
boolean start_of_file; // Have we gotten any data yet?
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
struct my_source_mgr {
|
|
83
|
+
struct jpeg_source_mgr pub; // public fields
|
|
84
|
+
FILE *infile; // source stream
|
|
85
|
+
JOCTET *buffer; // start of buffer
|
|
86
|
+
boolean start_of_file; // have we gotten any data yet?
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
typedef my_source_mem *my_src_mptr;
|
|
90
|
+
typedef my_source_mgr *my_src_ptr;
|
|
91
|
+
|
|
92
|
+
static boolean fill_minput_buffer(j_decompress_ptr cinfo) {
|
|
93
|
+
my_src_mptr src = (my_src_mptr) cinfo->src;
|
|
94
|
+
size_t nbytes;
|
|
95
|
+
if (src->indexinmem + INPUT_BUF_SIZE>src->lenght) nbytes=src->lenght - src->indexinmem;
|
|
96
|
+
else nbytes = INPUT_BUF_SIZE;
|
|
97
|
+
std::memcpy(src->buffer,src->inmem,nbytes);
|
|
98
|
+
src->inmem += nbytes;
|
|
99
|
+
src->indexinmem += (int)nbytes;
|
|
100
|
+
src->pub.next_input_byte = src->buffer;
|
|
101
|
+
src->pub.bytes_in_buffer = INPUT_BUF_SIZE;
|
|
102
|
+
src->start_of_file = FALSE;
|
|
103
|
+
return TRUE;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
static void skip_minput_data(j_decompress_ptr cinfo, long num_bytes) {
|
|
107
|
+
my_src_ptr src = (my_src_ptr)cinfo->src;
|
|
108
|
+
if (num_bytes > 0) {
|
|
109
|
+
while (num_bytes > (long) src->pub.bytes_in_buffer) {
|
|
110
|
+
num_bytes -= (long) src->pub.bytes_in_buffer;
|
|
111
|
+
fill_minput_buffer(cinfo);
|
|
112
|
+
// note we assume that fill_input_buffer will never return FALSE,
|
|
113
|
+
// so suspension need not be handled.
|
|
114
|
+
//
|
|
115
|
+
}
|
|
116
|
+
src->pub.next_input_byte += (size_t) num_bytes;
|
|
117
|
+
src->pub.bytes_in_buffer -= (size_t) num_bytes;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
static void init_msource(j_decompress_ptr cinfo) {
|
|
122
|
+
my_src_mptr src = (my_src_mptr)cinfo->src;
|
|
123
|
+
src->start_of_file = TRUE;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
static void term_source(j_decompress_ptr) {
|
|
127
|
+
// no work necessary here
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
static void jpeg_mem_src(j_decompress_ptr cinfo, JOCTET * memptr,int lenght) {
|
|
131
|
+
my_src_mptr src;
|
|
132
|
+
|
|
133
|
+
// The source object and input buffer are made permanent so that a series
|
|
134
|
+
//of JPEG images can be read from the same file by calling jpeg_stdio_src
|
|
135
|
+
// only before the first one. (If we discarded the buffer at the end of
|
|
136
|
+
// one image, we'd likely lose the start of the next one.)
|
|
137
|
+
// This makes it unsafe to use this manager and a different source
|
|
138
|
+
// manager serially with the same JPEG object. Caveat programmer.
|
|
139
|
+
//
|
|
140
|
+
|
|
141
|
+
// first time for this JPEG object?
|
|
142
|
+
if (cinfo->src == NULL) {
|
|
143
|
+
cinfo->src = (struct jpeg_source_mgr*)(*cinfo->mem->alloc_small)((j_common_ptr) cinfo,
|
|
144
|
+
JPOOL_PERMANENT,sizeof(my_source_mem));
|
|
145
|
+
src = (my_src_mptr) cinfo->src;
|
|
146
|
+
src->buffer = (JOCTET *)(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo,
|
|
147
|
+
JPOOL_PERMANENT,INPUT_BUF_SIZE * sizeof(JOCTET));
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
src = (my_src_mptr) cinfo->src;
|
|
151
|
+
src->pub.init_source = init_msource;
|
|
152
|
+
src->pub.fill_input_buffer = fill_minput_buffer;
|
|
153
|
+
src->pub.skip_input_data = skip_minput_data;
|
|
154
|
+
//src->pub.resync_to_restart = jpeg_resync_to_restart; // use default method
|
|
155
|
+
src->pub.term_source = term_source;
|
|
156
|
+
src->inmem = memptr;
|
|
157
|
+
src->indexinmem = 0;
|
|
158
|
+
src->lenght = lenght;
|
|
159
|
+
src->pub.bytes_in_buffer = 0; // forces fill_input_buffer on first read
|
|
160
|
+
src->pub.next_input_byte = NULL; // until buffer loaded
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// The following declarations and 5 functions are jpeg related
|
|
164
|
+
// functions used by put_jpeg_grey_memory and put_jpeg_yuv420p_memory
|
|
165
|
+
//
|
|
166
|
+
struct mem_destination_mgr {
|
|
167
|
+
struct jpeg_destination_mgr pub;
|
|
168
|
+
JOCTET *buf;
|
|
169
|
+
size_t bufsize;
|
|
170
|
+
size_t jpegsize;
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
typedef mem_destination_mgr *mem_dest_ptr;
|
|
174
|
+
|
|
175
|
+
static void init_destination(j_compress_ptr cinfo) {
|
|
176
|
+
mem_dest_ptr dest = (mem_dest_ptr) cinfo->dest;
|
|
177
|
+
dest->pub.next_output_byte = dest->buf;
|
|
178
|
+
dest->pub.free_in_buffer = dest->bufsize;
|
|
179
|
+
dest->jpegsize = 0;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
static boolean empty_output_buffer(j_compress_ptr cinfo) {
|
|
183
|
+
mem_dest_ptr dest = (mem_dest_ptr) cinfo->dest;
|
|
184
|
+
dest->pub.next_output_byte = dest->buf;
|
|
185
|
+
dest->pub.free_in_buffer = dest->bufsize;
|
|
186
|
+
return FALSE;
|
|
187
|
+
ERREXIT(cinfo, JERR_BUFFER_SIZE);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
static void term_destination(j_compress_ptr cinfo) {
|
|
191
|
+
mem_dest_ptr dest = (mem_dest_ptr) cinfo->dest;
|
|
192
|
+
dest->jpegsize = dest->bufsize - dest->pub.free_in_buffer;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
static void jpeg_mem_dest(j_compress_ptr cinfo, JOCTET* buf, size_t bufsize) {
|
|
196
|
+
mem_dest_ptr dest;
|
|
197
|
+
if (cinfo->dest == NULL) {
|
|
198
|
+
cinfo->dest = (struct jpeg_destination_mgr *)
|
|
199
|
+
(*cinfo->mem->alloc_small)((j_common_ptr)cinfo,JPOOL_PERMANENT,sizeof(mem_destination_mgr));
|
|
200
|
+
}
|
|
201
|
+
dest = (mem_dest_ptr) cinfo->dest;
|
|
202
|
+
dest->pub.init_destination = init_destination;
|
|
203
|
+
dest->pub.empty_output_buffer = empty_output_buffer;
|
|
204
|
+
dest->pub.term_destination = term_destination;
|
|
205
|
+
dest->buf = buf;
|
|
206
|
+
dest->bufsize = bufsize;
|
|
207
|
+
dest->jpegsize = 0;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
static unsigned jpeg_mem_size(j_compress_ptr cinfo) {
|
|
211
|
+
mem_dest_ptr dest = (mem_dest_ptr) cinfo->dest;
|
|
212
|
+
return dest->jpegsize;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/////////////////////////////////////////////////////////////////
|
|
216
|
+
//
|
|
217
|
+
// Define main CImg plugin functions.
|
|
218
|
+
// (you should use these functions only in your own code)
|
|
219
|
+
//
|
|
220
|
+
/////////////////////////////////////////////////////////////////
|
|
221
|
+
|
|
222
|
+
//! Load image from a jpeg-coded memory buffer.
|
|
223
|
+
/**
|
|
224
|
+
\param buffer Memory buffer containing the jpeg-coded image data.
|
|
225
|
+
\param buffer_size Size of the memory buffer, in bytes.
|
|
226
|
+
**/
|
|
227
|
+
static CImg get_load_jpeg_buffer(const JOCTET *const buffer, const unsigned buffer_size) {
|
|
228
|
+
struct jpeg_decompress_struct cinfo;
|
|
229
|
+
struct jpeg_error_mgr jerr;
|
|
230
|
+
cinfo.err = jpeg_std_error(&jerr);
|
|
231
|
+
jpeg_create_decompress(&cinfo);
|
|
232
|
+
jpeg_mem_src(&cinfo,const_cast<JOCTET*>(buffer),buffer_size);
|
|
233
|
+
jpeg_read_header(&cinfo,TRUE);
|
|
234
|
+
jpeg_start_decompress(&cinfo);
|
|
235
|
+
|
|
236
|
+
const unsigned int row_stride = cinfo.output_width * cinfo.output_components;
|
|
237
|
+
const size_t siz = safe_size(cinfo.output_width,cinfo.output_height,1,cinfo.output_components);
|
|
238
|
+
JOCTET *buf = new JOCTET[siz];
|
|
239
|
+
const JOCTET *buf2 = buf;
|
|
240
|
+
JSAMPROW row_pointer[1];
|
|
241
|
+
while (cinfo.output_scanline < cinfo.output_height) {
|
|
242
|
+
row_pointer[0] = buf + cinfo.output_scanline*row_stride;
|
|
243
|
+
jpeg_read_scanlines(&cinfo,row_pointer,1);
|
|
244
|
+
}
|
|
245
|
+
jpeg_finish_decompress(&cinfo);
|
|
246
|
+
jpeg_destroy_decompress(&cinfo);
|
|
247
|
+
|
|
248
|
+
CImg<T> dest(cinfo.output_width,cinfo.output_height,1,cinfo.output_components);
|
|
249
|
+
switch (dest.spectrum()) {
|
|
250
|
+
case 1: {
|
|
251
|
+
T *ptr_g = dest.data(0,0,0,0);
|
|
252
|
+
cimg_foroff(dest,off) *(ptr_g++) = (T)*(buf2++);
|
|
253
|
+
} break;
|
|
254
|
+
case 3: {
|
|
255
|
+
T
|
|
256
|
+
*ptr_r = dest.data(0,0,0,0),
|
|
257
|
+
*ptr_g = dest.data(0,0,0,1),
|
|
258
|
+
*ptr_b = dest.data(0,0,0,2);
|
|
259
|
+
cimg_forXY(dest,x,y) {
|
|
260
|
+
*(ptr_r++) = (T)*(buf2++);
|
|
261
|
+
*(ptr_g++) = (T)*(buf2++);
|
|
262
|
+
*(ptr_b++) = (T)*(buf2++);
|
|
263
|
+
}
|
|
264
|
+
} break;
|
|
265
|
+
case 4: {
|
|
266
|
+
T
|
|
267
|
+
*ptr_r = dest.data(0,0,0,0),
|
|
268
|
+
*ptr_g = dest.data(0,0,0,1),
|
|
269
|
+
*ptr_b = dest.data(0,0,0,2),
|
|
270
|
+
*ptr_a = dest.data(0,0,0,3);
|
|
271
|
+
cimg_forXY(dest,x,y) {
|
|
272
|
+
*(ptr_r++) = (T)*(buf2++);
|
|
273
|
+
*(ptr_g++) = (T)*(buf2++);
|
|
274
|
+
*(ptr_b++) = (T)*(buf2++);
|
|
275
|
+
*(ptr_a++) = (T)*(buf2++);
|
|
276
|
+
}
|
|
277
|
+
} break;
|
|
278
|
+
}
|
|
279
|
+
delete[] buf;
|
|
280
|
+
|
|
281
|
+
return dest;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
//! Load image from a jpeg-coded memory buffer (in-place version)
|
|
285
|
+
/**
|
|
286
|
+
\param buffer Memory buffer containing the jpeg-coded image data.
|
|
287
|
+
\param buffer_size Size of the memory buffer, in bytes.
|
|
288
|
+
**/
|
|
289
|
+
CImg& load_jpeg_buffer(const JOCTET *const buffer, const unsigned buffer_size) {
|
|
290
|
+
return get_load_jpeg_buffer(buffer,buffer_size).move_to(*this);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
//! Save image in a memory buffer, directly as a jpeg-coded file
|
|
294
|
+
/**
|
|
295
|
+
\param buffer Memory buffer that will be written with the jpeg-coded image data.
|
|
296
|
+
\param buffer_size Initial size of the memory buffer. When the function returns, the variable
|
|
297
|
+
contains the effective length needed to fill the buffer.
|
|
298
|
+
\param quality Quality of the jpeg compression.
|
|
299
|
+
**/
|
|
300
|
+
const CImg& save_jpeg_buffer(JOCTET *const buffer, unsigned int &buffer_size, const int quality=100) const {
|
|
301
|
+
|
|
302
|
+
// Fill pixel buffer
|
|
303
|
+
JOCTET *buf;
|
|
304
|
+
unsigned int dimbuf=0;
|
|
305
|
+
J_COLOR_SPACE colortype=JCS_RGB;
|
|
306
|
+
switch (spectrum()) {
|
|
307
|
+
case 1: {
|
|
308
|
+
// Greyscale images
|
|
309
|
+
JOCTET *buf2 = buf = new JOCTET[width()*height()*(dimbuf=1)];
|
|
310
|
+
const T
|
|
311
|
+
*ptr_g = data();
|
|
312
|
+
colortype = JCS_GRAYSCALE;
|
|
313
|
+
cimg_foroff(*this,off) *(buf2++) = (JOCTET)*(ptr_g++);
|
|
314
|
+
} break;
|
|
315
|
+
case 2:
|
|
316
|
+
case 3: {
|
|
317
|
+
// RGB images
|
|
318
|
+
JOCTET *buf2 = buf = new JOCTET[width()*height()*(dimbuf=3)];
|
|
319
|
+
const T
|
|
320
|
+
*ptr_r = data(0,0,0,0),
|
|
321
|
+
*ptr_g = data(0,0,0,1),
|
|
322
|
+
*ptr_b = data(0,0,0,spectrum()>2?2:0);
|
|
323
|
+
colortype = JCS_RGB;
|
|
324
|
+
cimg_forXY(*this,x,y) {
|
|
325
|
+
*(buf2++) = (JOCTET)*(ptr_r++);
|
|
326
|
+
*(buf2++) = (JOCTET)*(ptr_g++);
|
|
327
|
+
*(buf2++) = (JOCTET)*(ptr_b++);
|
|
328
|
+
}
|
|
329
|
+
} break;
|
|
330
|
+
default: {
|
|
331
|
+
// YCMYK images
|
|
332
|
+
JOCTET *buf2 = buf = new JOCTET[width()*height()*(dimbuf=4)];
|
|
333
|
+
const T
|
|
334
|
+
*ptr_r = data(0,0,0,0),
|
|
335
|
+
*ptr_g = data(0,0,0,1),
|
|
336
|
+
*ptr_b = data(0,0,0,2),
|
|
337
|
+
*ptr_a = data(0,0,0,3);
|
|
338
|
+
colortype = JCS_CMYK;
|
|
339
|
+
cimg_forXY(*this,x,y) {
|
|
340
|
+
*(buf2++) = (JOCTET)*(ptr_r++);
|
|
341
|
+
*(buf2++) = (JOCTET)*(ptr_g++);
|
|
342
|
+
*(buf2++) = (JOCTET)*(ptr_b++);
|
|
343
|
+
*(buf2++) = (JOCTET)*(ptr_a++);
|
|
344
|
+
}
|
|
345
|
+
} break;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
// Call libjpeg functions
|
|
349
|
+
struct jpeg_compress_struct cinfo;
|
|
350
|
+
struct jpeg_error_mgr jerr;
|
|
351
|
+
cinfo.err = jpeg_std_error(&jerr);
|
|
352
|
+
jpeg_create_compress(&cinfo);
|
|
353
|
+
jpeg_mem_dest(&cinfo, buffer, buffer_size);
|
|
354
|
+
cinfo.image_width = width();
|
|
355
|
+
cinfo.image_height = height();
|
|
356
|
+
cinfo.input_components = dimbuf;
|
|
357
|
+
cinfo.in_color_space = colortype;
|
|
358
|
+
jpeg_set_defaults(&cinfo);
|
|
359
|
+
jpeg_set_quality(&cinfo,quality<100?quality:100,TRUE);
|
|
360
|
+
jpeg_start_compress(&cinfo,TRUE);
|
|
361
|
+
|
|
362
|
+
const unsigned int row_stride = width()*dimbuf;
|
|
363
|
+
JSAMPROW row_pointer[1];
|
|
364
|
+
while (cinfo.next_scanline < cinfo.image_height) {
|
|
365
|
+
row_pointer[0] = &buf[cinfo.next_scanline*row_stride];
|
|
366
|
+
jpeg_write_scanlines(&cinfo,row_pointer,1);
|
|
367
|
+
}
|
|
368
|
+
jpeg_finish_compress(&cinfo);
|
|
369
|
+
delete[] buf;
|
|
370
|
+
buffer_size = jpeg_mem_size(&cinfo);
|
|
371
|
+
jpeg_destroy_compress(&cinfo);
|
|
372
|
+
return *this;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
// End of the plug-in
|
|
376
|
+
//-------------------
|
|
377
|
+
#endif /* cimg_plugin_jpeg_buffer */
|