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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
|
/*
* This file is part of rasdaman community.
*
* Rasdaman community is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Rasdaman community is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with rasdaman community. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
rasdaman GmbH.
*
* For more information please see <http://www.rasdaman.org>
* or contact Peter Baumann via <baumann@rasdaman.com>.
/
/**
* SOURCE: jpeg.cc
*
* MODULE: conversion
*
* CLASSES: r_Conv_JPEG
*
* COMMENTS:
*
* Provides functions to convert data to JPEG and back
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <setjmp.h>
extern "C" {
#include "jpeglib.h"
}
#include "raslib/rminit.hh"
#include "raslib/parseparams.hh"
#include "conversion/jpeg.hh"
#include "conversion/memfs.hh"
#define JPEG_IO_BUFFER_SIZE 4096
// JPEG-interface in C-namespace (like JPEG lib)
extern "C" {
typedef struct my_compress_struct {
jpeg_compress_struct pub;
thandle_t handle;
JOCTET *buffer;
int bufferSize;
} my_compress_struct;
typedef struct my_decompress_struct {
jpeg_decompress_struct pub;
thandle_t handle;
JOCTET *buffer;
int bufferSize;
} my_decompress_struct;
typedef struct my_error_mgr {
jpeg_error_mgr pub;
jmp_buf setjmp_buffer;
} my_error_mgr;
// See jpeg library example
static void my_error_exit(jpeg_common_struct *cptr)
{
my_error_mgr *myerr = (my_error_mgr*)(cptr->err);
(*cptr->err->output_message)(cptr);
longjmp(myerr->setjmp_buffer, 1);
}
/*
* Memory IO wrapper functions, rely on memFS.
*/
// Destination manager methods
// cptr is actually a pointer to my_compress_struct
static void dm_init_destination(jpeg_compress_struct *cptr)
{
my_compress_struct *mptr = (my_compress_struct*)cptr;
if ((mptr->buffer = new JOCTET[JPEG_IO_BUFFER_SIZE]) == NULL)
{
RMInit::logOut << "r_Conv_JPEG@dm_init_destination(): out of memory" << endl;
throw r_Error(MEMMORYALLOCATIONERROR);
}
mptr->bufferSize = JPEG_IO_BUFFER_SIZE;
cptr->dest->next_output_byte = mptr->buffer; cptr->dest->free_in_buffer = mptr->bufferSize;
}
static boolean dm_empty_output_buffer(jpeg_compress_struct *cptr)
{
my_compress_struct *mptr = (my_compress_struct*)cptr;
boolean retval=FALSE;
if (memfs_write(mptr->handle, mptr->buffer, mptr->bufferSize) == mptr->bufferSize)
{
cptr->dest->next_output_byte = mptr->buffer; cptr->dest->free_in_buffer = mptr->bufferSize;
retval=TRUE;
}
return retval;
}
static void dm_term_destination(jpeg_compress_struct *cptr)
{
my_compress_struct *mptr = (my_compress_struct*)cptr;
if (cptr->dest->next_output_byte != mptr->buffer)
{
memfs_write(mptr->handle, mptr->buffer, (cptr->dest->next_output_byte - mptr->buffer));
}
delete [] mptr->buffer; mptr->buffer = NULL;
}
// Source manager methods
// dptr is actually a pointer to my_decompress_struct
static void sm_init_source(jpeg_decompress_struct *dptr)
{
my_decompress_struct *mptr = (my_decompress_struct*)dptr;
if ((mptr->buffer = new JOCTET[JPEG_IO_BUFFER_SIZE]) == NULL)
{
RMInit::logOut << "r_Conv_JPEG@sm_init_source(): out of memory" << endl;
throw r_Error(MEMMORYALLOCATIONERROR);
}
mptr->bufferSize = JPEG_IO_BUFFER_SIZE;
dptr->src->next_input_byte = mptr->buffer; dptr->src->bytes_in_buffer = 0;
}
// See jdatasrc.c
static boolean sm_fill_input_buffer(jpeg_decompress_struct *dptr)
{
my_decompress_struct *mptr = (my_decompress_struct*)dptr;
int read_bytes=0;
if ((read_bytes = memfs_chunk_read(mptr->handle, mptr->buffer, mptr->bufferSize)) != 0)
{
dptr->src->bytes_in_buffer = read_bytes;
}
else
{
mptr->buffer[0] = (JOCTET)0xff;
mptr->buffer[1] = (JOCTET)JPEG_EOI;
dptr->src->bytes_in_buffer = 2;
}
dptr->src->next_input_byte = mptr->buffer;
return TRUE;
}
// See jdatasrc.c
static void sm_skip_input_data(jpeg_decompress_struct *dptr, long num_bytes)
{
my_decompress_struct *mptr = (my_decompress_struct*)dptr;
if (num_bytes < dptr->src->bytes_in_buffer)
{
dptr->src->next_input_byte += num_bytes; dptr->src->bytes_in_buffer -= num_bytes;
}
else
{
int read_bytes=0;
num_bytes -= dptr->src->bytes_in_buffer;
dptr->src->next_input_byte = mptr->buffer;
while (num_bytes >= mptr->bufferSize)
{
memfs_chunk_seek(mptr->handle, mptr->bufferSize, SEEK_CUR);
num_bytes -= mptr->bufferSize;
}
read_bytes = memfs_chunk_read(mptr->handle, mptr->buffer, mptr->bufferSize);
if (read_bytes <= num_bytes)
{
mptr->buffer[0] = (JOCTET)0xff;
mptr->buffer[1] = (JOCTET)JPEG_EOI;
dptr->src->bytes_in_buffer = 2;
}
else
{
dptr->src->next_input_byte = mptr->buffer + num_bytes;
dptr->src->bytes_in_buffer = read_bytes - num_bytes;
}
}
}
/*static boolean sm_resync_to_restart(jpeg_decompress_struct *dptr, int desired)
{
return FALSE;
}*/
static void sm_term_source(jpeg_decompress_struct *dptr)
{
my_decompress_struct *mptr = (my_decompress_struct*)dptr;
delete [] mptr->buffer; mptr->buffer = NULL;
}
} // end of C namespace
/*
* r_Conv_JPEG class for converting MDD to JPEG and back
*/
void r_Conv_JPEG::initJPEG( void )
{
quality = 80;
if (params == NULL)
params = new r_Parse_Params;
params->add("quality", &quality, r_Parse_Params::param_type_int);
}
r_Conv_JPEG::r_Conv_JPEG(const char *src, const r_Minterval &interv, int tp) throw(r_Error)
: r_Convert_Memory(src, interv, tp)
{
initJPEG();
}
r_Conv_JPEG::r_Conv_JPEG(const char *src, const r_Minterval &interv, const r_Type *tp) throw(r_Error)
: r_Convert_Memory(src, interv, tp)
{
initJPEG();
}
r_Conv_JPEG::~r_Conv_JPEG(void)
{
}
r_convDesc &r_Conv_JPEG::convertTo( const char *options) throw(r_Error)
{
struct jpeg_destination_mgr destMgr;
struct jpeg_compress_struct *cptr=NULL;
struct jpeg_error_mgr *jptr=NULL;
my_error_mgr jerr;
my_compress_struct cinfo;
int width=0, height=0, lineAdd=0, pixelAdd=0;
int i=0, j=0, spp=0;
J_COLOR_SPACE jcs;
JSAMPROW row_pointers[1];
JSAMPLE *row=NULL, *rowPtr=NULL;
r_Long jpegSize=0;
row = NULL;
memset(&cinfo, 0, sizeof(my_compress_struct));
cinfo.handle = (thandle_t)handle;
cptr = (struct jpeg_compress_struct*)&cinfo.pub;
jptr = (struct jpeg_error_mgr*)&jerr.pub;
cptr->err = jpeg_std_error(jptr);
jptr->error_exit = my_error_exit;
params->process(options);
if (setjmp(jerr.setjmp_buffer))
{
if (row != NULL) {delete [] row; row = NULL;}
jpeg_abort_compress(cptr);
jpeg_destroy_compress(cptr);
// destination manager destructor might not be called on an abort
if (cinfo.buffer != NULL){delete [] cinfo.buffer; cinfo.buffer=NULL;}
RMInit::logOut << "r_Conv_JPEG::convertTo(" << options << "): unable to save the stack" << endl;
throw r_Error(r_Error::r_Error_General);
}
jpeg_create_compress(cptr);
jpeg_set_quality(cptr, quality, 0);
memfs_newfile(handle);
destMgr.init_destination = dm_init_destination;
destMgr.empty_output_buffer = dm_empty_output_buffer;
destMgr.term_destination = dm_term_destination;
cptr->dest = &destMgr;
width = (int)(desc.srcInterv[0].high() - desc.srcInterv[0].low() + 1);
height = (int)(desc.srcInterv[1].high() - desc.srcInterv[1].low() + 1);
cptr->image_width = (JDIMENSION)width; cptr->image_height = (JDIMENSION)height;
switch (desc.baseType)
{
case ctype_bool:
case ctype_char:
jcs = JCS_GRAYSCALE; spp = 1; lineAdd = 1; pixelAdd = height; break;
case ctype_rgb:
jcs = JCS_RGB; spp = 3; lineAdd = 3; pixelAdd = 3*height; break;
default:
RMInit::logOut << "r_Conv_JPEG::convertTo(" << options << "): unsupported base type" << endl;
throw r_Error(r_Error::r_Error_General);
}
cptr->input_components = spp; cptr->in_color_space = jcs;
jpeg_set_defaults(cptr);
jpeg_set_quality(cptr, quality, TRUE);
jpeg_start_compress(cptr, TRUE);
if ((row = new JSAMPLE[width * spp]) == NULL)
{
RMInit::logOut << "r_Conv_JPEG::convertTo(" << options << "): out of memory" << endl;
throw r_Error(MEMMORYALLOCATIONERROR);
}
row_pointers[0] = row;
const unsigned char *src=NULL, *srcPtr=NULL;
src = (const unsigned char*)desc.src;
for (j=0; j<height; j++, src += lineAdd)
{
srcPtr = src; rowPtr = row;
switch (desc.baseType)
{
case ctype_bool:
for (i=0; i<width; i++, srcPtr += pixelAdd)
{
*rowPtr++ = (*srcPtr == 0) ? 0 : 0xff;
}
break;
case ctype_char:
for (i=0; i<width; i++, srcPtr += pixelAdd)
{
*rowPtr++ = *srcPtr;
}
break;
case ctype_rgb:
for (i=0; i<width; i++, srcPtr += pixelAdd)
{
*rowPtr++ = srcPtr[0]; *rowPtr++ = srcPtr[1]; *rowPtr++ = srcPtr[2];
}
break;
}
jpeg_write_scanlines(cptr, row_pointers, 1);
}
delete [] row; row = NULL;
jpeg_finish_compress(cptr);
jpeg_destroy_compress(cptr);
jpegSize = memfs_size(handle);
if ((desc.dest = (char*)mystore.storage_alloc(jpegSize)) == NULL)
{
RMInit::logOut << "r_Conv_JPEG::convertTo(" << options << "): out of memory" << endl;
throw r_Error(MEMMORYALLOCATIONERROR);
}
memfs_seek(handle, 0, SEEK_SET);
memfs_read(handle, desc.dest, jpegSize);
desc.destInterv = r_Minterval(1);
desc.destInterv << r_Sinterval((r_Range)0, (r_Range)jpegSize - 1);
desc.destType = r_Type::get_any_type("char");
return desc;
}
r_convDesc &r_Conv_JPEG::convertFrom(const char *options) throw(r_Error)
{
struct jpeg_source_mgr srcMgr;
struct jpeg_decompress_struct *dptr=NULL;
struct jpeg_error_mgr *jptr=NULL;
my_error_mgr jerr;
my_decompress_struct cinfo;
int width=0, height=0, lineAdd=0, pixelAdd=0;
int i=0, j=0, spp=0;
J_COLOR_SPACE jcs;
JSAMPROW row_pointers[1];
JSAMPLE *row=NULL, *rowPtr=NULL;
char *dest=NULL, *destPtr=NULL;
row = NULL; desc.dest = NULL;
memset(&cinfo, 0, sizeof(my_decompress_struct));
cinfo.handle = (thandle_t)handle;
dptr = (struct jpeg_decompress_struct*)&cinfo.pub;
jptr = (struct jpeg_error_mgr*)&jerr.pub;
dptr->err = jpeg_std_error(jptr);
jptr->error_exit = my_error_exit;
if (setjmp(jerr.setjmp_buffer))
{
if (row != NULL) {delete [] row; row = NULL;}
if (desc.dest != NULL) {mystore.storage_free(desc.dest); desc.dest = NULL;}
jpeg_abort_decompress(dptr);
jpeg_destroy_decompress(dptr);
// Source manager destructor might not be called on an abort
if (cinfo.buffer != NULL) {delete [] cinfo.buffer; cinfo.buffer=NULL;}
RMInit::logOut << "r_Conv_JPEG::convertFrom(" << options << "): unable to save the stack" << endl;
throw r_Error(r_Error::r_Error_General);
}
jpeg_create_decompress(dptr);
memfs_chunk_initfs(handle, (char*)desc.src, (r_Long)(desc.srcInterv[0].high() - desc.srcInterv[0].low() + 1));
srcMgr.init_source = sm_init_source;
srcMgr.fill_input_buffer = sm_fill_input_buffer;
srcMgr.skip_input_data = sm_skip_input_data;
srcMgr.resync_to_restart = NULL; //sm_resync_to_restart;
srcMgr.term_source = sm_term_source;
dptr->src = &srcMgr;
jpeg_read_header(dptr, TRUE);
width = (int)(dptr->image_width); height = (int)(dptr->image_height);
if (dptr->num_components == 1)
{
desc.baseType = ctype_char;
lineAdd = 1; pixelAdd = height;
}
else
{
desc.baseType = ctype_rgb;
lineAdd = 3; pixelAdd = 3*height;
dptr->out_color_space = JCS_RGB;
}
jpeg_start_decompress(dptr);
spp = (int)(dptr->output_components);
row = new JSAMPLE[width * spp];
desc.dest = (char*)mystore.storage_alloc(width * height * spp);
if ((row == NULL) || (desc.dest == NULL))
{
if (row != NULL) {delete [] row; row = NULL;}
RMInit::logOut << "r_Conv_JPEG::convertFrom(): out of memory" << endl;
throw r_Error(MEMMORYALLOCATIONERROR);
}
row_pointers[0] = row; dest = desc.dest;
for (j=0; j<height; j++, dest += lineAdd)
{
jpeg_read_scanlines(dptr, row_pointers, 1);
destPtr = dest; rowPtr = row;
switch (spp)
{
case 1:
for (i=0; i<width; i++, destPtr += pixelAdd)
{
*destPtr = *rowPtr++;
}
break;
case 3:
for (i=0; i<width; i++, destPtr += pixelAdd)
{
destPtr[0] = *rowPtr++; destPtr[1] = *rowPtr++; destPtr[2] = *rowPtr++;
}
break;
}
}
delete [] row; row = NULL;
jpeg_finish_decompress(dptr);
jpeg_destroy_decompress(dptr);
desc.destInterv = r_Minterval(2);
desc.destInterv << r_Sinterval((r_Range)0, (r_Range)width-1)
<< r_Sinterval((r_Range)0, (r_Range)height-1);
desc.destType = get_external_type(desc.baseType);
return desc;
}
const char *r_Conv_JPEG::get_name( void ) const
{
return format_name_jpeg;
}
r_Data_Format r_Conv_JPEG::get_data_format( void ) const
{
return r_JPEG;
}
r_Convertor *r_Conv_JPEG::clone( void ) const
{
return new r_Conv_JPEG(desc.src, desc.srcInterv, desc.baseType);
}
|