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 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
|
#include "jp2io.h"
#include "color.h"
#include "appglobal.h"
#include "loggingparams.h"
#include <errno.h>
#include <string.h>
#include <QFile>
#include <QDir>
#include <QDebug>
#define JP2_RFC3745_MAGIC "\x00\x00\x00\x0c\x6a\x50\x20\x20\x0d\x0a\x87\x0a"
#define JP2_MAGIC "\x0d\x0a\x87\x0a"
#define J2K_CODESTREAM_MAGIC "\xff\x4f\xff\x51"
static size_t opj_strnlen_s(const char *src, size_t max_len)
{
size_t len;
if (src == NULL) {
return 0U;
}
for (len = 0U; (*src != '\0') && (len < max_len); src++, len++);
return len;
}
static int opj_strcpy_s(char* dst, size_t dst_size, const char* src)
{
size_t src_len = 0U;
if ((dst == NULL) || (dst_size == 0U)) {
return EINVAL;
}
if (src == NULL) {
dst[0] = '\0';
return EINVAL;
}
src_len = opj_strnlen_s(src, dst_size);
if (src_len >= dst_size) {
return ERANGE;
}
memcpy(dst, src, src_len);
dst[src_len] = '\0';
return 0;
}
/**
sample error callback expecting a FILE* client object
*/
static void error_callback(const char *msg, void *client_data) {
(void)client_data;
Q_UNUSED( msg )
addToLog(QString("[ERROR] %1").arg(msg),LOGIMAG);
}
/**
sample warning callback expecting a FILE* client object
*/
static void warning_callback(const char *msg, void *client_data) {
(void)client_data;
Q_UNUSED( msg )
addToLog(QString("[WARNING] %1").arg(msg),LOGIMAG);
}
/**
sample debug callback expecting no client object
*/
static void info_callback(const char *msg, void *client_data) {
(void)client_data;
Q_UNUSED( msg )
addToLog(QString("[INFO] %1").arg(msg),LOGIMAG);
}
/*!
* Divide an integer by a power of 2 and round upwards.
*/
// -------------------- int_ceildivpow2() ------------------------------------
static inline int int_ceildivpow2(int a, int b) {
return (a + (1 << b) - 1) >> b;
}
// ---------------------------------------------------------------------------
jp2IO::jp2IO()
{
}
void jp2IO::init()
{
jp2Image = NULL;
l_stream = NULL; /* Stream */
l_codec = NULL; /* Handle to a decompressor */
cstr_index = NULL;
}
bool jp2IO::check(QString fileName)
{
quint64 size;
char data[12];
QFile fi(fileName);
set_default_parameters(¶meters); // set decoding parameters to default values
if(!fi.open(QIODevice::ReadOnly)) return false;
size=fi.read(data,12);
fi.close();
if(size!=12)
{
return false;
}
if (memcmp(data, JP2_RFC3745_MAGIC, 12) == 0 || memcmp(data, JP2_MAGIC, 4) == 0)
{
magicFormat = JP2_CFMT;
magicStr = ".jp2";
}
else if (memcmp(data, J2K_CODESTREAM_MAGIC, 4) == 0)
{
magicFormat = J2K_CFMT;
magicStr = ".j2k or .jpc or .j2c";
}
else return false;
parameters.decod_format=magicFormat;
return true;
}
void jp2IO::set_default_parameters(opj_decompress_parameters* parameters)
{
if (parameters)
{
memset(parameters, 0, sizeof(opj_decompress_parameters));
/* default decoding parameters (command line specific) */
parameters->decod_format = -1;
parameters->cod_format = -1;
/* default decoding parameters (core) */
opj_set_default_decoder_parameters(&(parameters->core));
}
}
void jp2IO::destroy_parameters(opj_decompress_parameters* parameters)
{
if (parameters)
{
if (parameters->precision)
{
free(parameters->precision);
parameters->precision = NULL;
}
}
}
void jp2IO::slotStart()
{
bool result;
result=check(threadFilename);
if(result)
{
*threadImage=decode(threadFilename).copy();
}
emit done(result,fromCache);
}
QImage jp2IO::decode(QString fileName)
{
QImage qimage;
int width; // image width and height
int wr, hr; // taken from OpenJPEG
init();
l_stream = opj_stream_create_default_file_stream(fileName.toLatin1(),1);
if (!l_stream)
{
addToLog( QString("ERROR -> failed to create the stream from the file %1").arg(parameters.infile),LOGALL);
destroy_parameters(¶meters);
return qimage;
}
/* decode the JPEG2000 stream */
/* ---------------------- */
switch(parameters.decod_format)
{
case J2K_CFMT: /* JPEG-2000 codestream */
{
/* Get a decoder handle */
l_codec = opj_create_decompress(OPJ_CODEC_J2K);
break;
}
case JP2_CFMT: /* JPEG 2000 compressed image data */
{
/* Get a decoder handle */
l_codec = opj_create_decompress(OPJ_CODEC_JP2);
break;
}
case JPT_CFMT: /* JPEG 2000, JPIP */
{
/* Get a decoder handle */
l_codec = opj_create_decompress(OPJ_CODEC_JPT);
break;
}
default:
// fprintf(stderr, "skipping file..\n");
destroy_parameters(¶meters);
opj_stream_destroy(l_stream);
}
/* catch events using our callbacks and give a local context */
opj_set_info_handler(l_codec, info_callback,00);
opj_set_warning_handler(l_codec, warning_callback,00);
opj_set_error_handler(l_codec, error_callback,00);
// t = opj_clock();
/* Setup the decoder decoding parameters using user parameters */
if ( !opj_setup_decoder(l_codec, &(parameters.core)) ){
addToLog( "ERROR -> opj_decompress: failed to setup the decoder",LOGALL);
destroy_parameters(¶meters);
opj_stream_destroy(l_stream);
opj_destroy_codec(l_codec);
return qimage;
}
/* Read the main header of the codestream and if necessary the JP2 boxes*/
if(! opj_read_header(l_stream, l_codec, &jp2Image))
{
addToLog( "ERROR -> opj_decompress: failed to read the header",LOGALL);
destroy_parameters(¶meters);
opj_stream_destroy(l_stream);
opj_destroy_codec(l_codec);
opj_image_destroy(jp2Image);
return qimage;
}
if (!parameters.nb_tile_to_decode)
{
/* Optional if you want decode the entire image */
if (!opj_set_decode_area(l_codec, jp2Image, (OPJ_INT32)parameters.DA_x0,
(OPJ_INT32)parameters.DA_y0, (OPJ_INT32)parameters.DA_x1, (OPJ_INT32)parameters.DA_y1))
{
addToLog( "ERROR -> opj_decompress: failed to set the decoded area",LOGALL);
destroy_parameters(¶meters);
opj_stream_destroy(l_stream);
opj_destroy_codec(l_codec);
opj_image_destroy(jp2Image);
return qimage;
}
/* Get the decoded image */
if (!(opj_decode(l_codec, l_stream, jp2Image) && opj_end_decompress(l_codec, l_stream)))
{
addToLog( "ERROR -> opj_decompress: failed to decode image!",LOGALL);
destroy_parameters(¶meters);
opj_destroy_codec(l_codec);
opj_stream_destroy(l_stream);
opj_image_destroy(jp2Image);
return qimage;
}
}
/* Close the byte stream */
opj_stream_destroy(l_stream);
if( jp2Image->color_space != OPJ_CLRSPC_SYCC
&& jp2Image->numcomps == 3 && jp2Image->comps[0].dx == jp2Image->comps[0].dy
&& jp2Image->comps[1].dx != 1 )
jp2Image->color_space = OPJ_CLRSPC_SYCC;
else if (jp2Image->numcomps <= 2)
jp2Image->color_space = OPJ_CLRSPC_GRAY;
if(jp2Image->color_space == OPJ_CLRSPC_SYCC){
color_sycc_to_rgb(jp2Image);
}
else if(jp2Image->color_space == OPJ_CLRSPC_EYCC){
color_esycc_to_rgb(jp2Image);
}
if(jp2Image->icc_profile_buf)
{
free(jp2Image->icc_profile_buf);
jp2Image->icc_profile_buf = NULL; jp2Image->icc_profile_len = 0;
}
/* Force RGB output */
/* ---------------- */
if (parameters.force_rgb)
{
switch (jp2Image->color_space) {
case OPJ_CLRSPC_SRGB:
break;
case OPJ_CLRSPC_GRAY:
jp2Image = convert_gray_to_rgb(jp2Image);
break;
default:
fprintf(stderr, "ERROR -> opj_decompress: don't know how to convert image to RGB colorspace!\n");
opj_image_destroy(jp2Image);
jp2Image = NULL;
break;
}
if (jp2Image == NULL) {
fprintf(stderr, "ERROR -> opj_decompress: failed to convert to RGB image!\n");
destroy_parameters(¶meters);
opj_destroy_codec(l_codec);
return qimage;
}
}
// decode image width and height
width = jp2Image->comps[0].w;
wr = int_ceildivpow2(jp2Image->comps[0].w, jp2Image->comps[0].factor);
// height = jp2Image->comps[0].h;
hr = int_ceildivpow2(jp2Image->comps[0].h, jp2Image->comps[0].factor);
// alloc memory for a new image
qimage=QImage(wr, hr, QImage::Format_RGB32);
// get a pointer to the first pixel data in result image
QRgb *bits = (QRgb *)qimage.bits();
for(int i = 0; i < wr * hr; i++) {
unsigned char R, G, B;
// calculate coords
int x = (i) % (wr);
// int y = height - (i) / (wr) - 1;
int y = (i) / (wr);
#define idx( _x, _y ) ( _x + _y * width )
R = jp2Image->comps[0].data[ idx( x, y ) ];
G = jp2Image->comps[1].data[ idx( x, y ) ];
B = jp2Image->comps[2].data[ idx( x, y ) ];
// fill the bit with read pallete
bits[i] = qRgb(R, G, B);
}
destroy_parameters(¶meters);
opj_destroy_codec(l_codec);
opj_image_destroy(jp2Image);
return qimage;
}
opj_image_t *jp2IO::convert_gray_to_rgb(opj_image_t* original)
{
OPJ_UINT32 compno;
opj_image_t* l_new_image = NULL;
opj_image_cmptparm_t* l_new_components = NULL;
l_new_components = (opj_image_cmptparm_t*)malloc((original->numcomps + 2U) * sizeof(opj_image_cmptparm_t));
if (l_new_components == NULL) {
fprintf(stderr, "ERROR -> opj_decompress: failed to allocate memory for RGB image!\n");
opj_image_destroy(original);
return NULL;
}
l_new_components[0].bpp = l_new_components[1].bpp = l_new_components[2].bpp = original->comps[0].bpp;
l_new_components[0].dx = l_new_components[1].dx = l_new_components[2].dx = original->comps[0].dx;
l_new_components[0].dy = l_new_components[1].dy = l_new_components[2].dy = original->comps[0].dy;
l_new_components[0].h = l_new_components[1].h = l_new_components[2].h = original->comps[0].h;
l_new_components[0].w = l_new_components[1].w = l_new_components[2].w = original->comps[0].w;
l_new_components[0].prec = l_new_components[1].prec = l_new_components[2].prec = original->comps[0].prec;
l_new_components[0].sgnd = l_new_components[1].sgnd = l_new_components[2].sgnd = original->comps[0].sgnd;
l_new_components[0].x0 = l_new_components[1].x0 = l_new_components[2].x0 = original->comps[0].x0;
l_new_components[0].y0 = l_new_components[1].y0 = l_new_components[2].y0 = original->comps[0].y0;
for(compno = 1U; compno < original->numcomps; ++compno) {
l_new_components[compno+2U].bpp = original->comps[compno].bpp;
l_new_components[compno+2U].dx = original->comps[compno].dx;
l_new_components[compno+2U].dy = original->comps[compno].dy;
l_new_components[compno+2U].h = original->comps[compno].h;
l_new_components[compno+2U].w = original->comps[compno].w;
l_new_components[compno+2U].prec = original->comps[compno].prec;
l_new_components[compno+2U].sgnd = original->comps[compno].sgnd;
l_new_components[compno+2U].x0 = original->comps[compno].x0;
l_new_components[compno+2U].y0 = original->comps[compno].y0;
}
l_new_image = opj_image_create(original->numcomps + 2U, l_new_components, OPJ_CLRSPC_SRGB);
free(l_new_components);
if (l_new_image == NULL) {
fprintf(stderr, "ERROR -> opj_decompress: failed to allocate memory for RGB image!\n");
opj_image_destroy(original);
return NULL;
}
l_new_image->x0 = original->x0;
l_new_image->x1 = original->x1;
l_new_image->y0 = original->y0;
l_new_image->y1 = original->y1;
l_new_image->comps[0].factor = l_new_image->comps[1].factor = l_new_image->comps[2].factor = original->comps[0].factor;
l_new_image->comps[0].alpha = l_new_image->comps[1].alpha = l_new_image->comps[2].alpha = original->comps[0].alpha;
l_new_image->comps[0].resno_decoded = l_new_image->comps[1].resno_decoded = l_new_image->comps[2].resno_decoded = original->comps[0].resno_decoded;
memcpy(l_new_image->comps[0].data, original->comps[0].data, original->comps[0].w * original->comps[0].h * sizeof(OPJ_INT32));
memcpy(l_new_image->comps[1].data, original->comps[0].data, original->comps[0].w * original->comps[0].h * sizeof(OPJ_INT32));
memcpy(l_new_image->comps[2].data, original->comps[0].data, original->comps[0].w * original->comps[0].h * sizeof(OPJ_INT32));
for(compno = 1U; compno < original->numcomps; ++compno) {
l_new_image->comps[compno+2U].factor = original->comps[compno].factor;
l_new_image->comps[compno+2U].alpha = original->comps[compno].alpha;
l_new_image->comps[compno+2U].resno_decoded = original->comps[compno].resno_decoded;
memcpy(l_new_image->comps[compno+2U].data, original->comps[compno].data, original->comps[compno].w * original->comps[compno].h * sizeof(OPJ_INT32));
}
opj_image_destroy(original);
return l_new_image;
}
QByteArray jp2IO::encode(QImage qimage, QImage &newImage,int &fileSize, int compressionRatio)
{
QByteArray byteArray;
QString fn=QString("%1/%2").arg(QDir::tempPath()).arg("qsstv.tmp");
// char *of=fn.toLatin1().data();
bool success;
init();
fileSize=0;
opj_set_default_encoder_parameters(&cparameters);
cparameters.cp_disto_alloc = 1;
createImage(qimage);
cparameters.cod_format=JP2_CFMT;
if(opj_strcpy_s(cparameters.outfile, sizeof(cparameters.outfile), fn.toLatin1().data()) != 0)
{
return byteArray;
}
raw_cp.rawBitDepth = 0;
raw_cp.rawComp = 0;
raw_cp.rawComps = 0;
raw_cp.rawHeight = 0;
raw_cp.rawSigned = 0;
raw_cp.rawWidth = 0;
// cparameters.tcp_mct = (char) 255; // This will be set later according to the input image or the provided option
cparameters.tcp_mct = (jp2Image->numcomps >= 3) ? 1 : 0;
cparameters.tcp_rates[0] = compressionRatio; /* MOD antonin : losslessbug */
cparameters.tcp_numlayers++;
cparameters.cp_disto_alloc = 1;
l_codec = opj_create_compress(OPJ_CODEC_JP2);
/* catch events using our callbacks and give a local context */
opj_set_info_handler(l_codec, info_callback,00);
opj_set_warning_handler(l_codec, warning_callback,00);
opj_set_error_handler(l_codec, error_callback,00);
/* open a byte stream for writing and allocate memory for all tiles */
if (! opj_setup_encoder(l_codec, &cparameters, jp2Image))
{
addToLog("failed to encode image: opj_setup_encoder",LOGIMAG);
opj_destroy_codec(l_codec);
opj_image_destroy(jp2Image);
return byteArray;
}
l_stream = opj_stream_create_default_file_stream(cparameters.outfile,OPJ_FALSE);
if (! l_stream)
{
opj_destroy_codec(l_codec);
opj_image_destroy(jp2Image);
return byteArray;
}
/* encode the image */
success = opj_start_compress(l_codec,jp2Image,l_stream);
if (!success)
{
addToLog("failed to encode image: opj_start_compress",LOGIMAG);
}
success = success && opj_encode(l_codec, l_stream);
if (!success)
{
addToLog("failed to encode image: opj_encode",LOGIMAG);
}
success = success && opj_end_compress(l_codec, l_stream);
if (!success)
{
addToLog("failed to encode image: opj_end_compress",LOGIMAG);
}
if (!success)
{
opj_stream_destroy(l_stream);
opj_destroy_codec(l_codec);
opj_image_destroy(jp2Image);
addToLog("failed to encode image",LOGIMAG);
remove(parameters.outfile);
return byteArray;
}
opj_stream_destroy(l_stream);
opj_destroy_codec(l_codec);
opj_image_destroy(jp2Image);
/* free user parameters structure */
if(cparameters.cp_comment) free(cparameters.cp_comment);
if(cparameters.cp_matrice) free(cparameters.cp_matrice);
if(raw_cp.rawComps) free(raw_cp.rawComps);
QFile fi(fn);
if(fi.open(QIODevice::ReadOnly))
{
byteArray=fi.readAll();
fileSize=byteArray.count();
}
fi.close();
check(fn);
newImage=decode(fn);
fi.remove();
remove(parameters.outfile);
return byteArray;
}
bool jp2IO::createImage(QImage qimage)
{
int i;
int numcmpts=3;
opj_image_cmptparm_t cmptparm[4]; /* maximum of 4 components */
memset(&cmptparm[0], 0, sizeof(cmptparm));
for(i = 0; i < 4; i++)
{
cmptparm[i].prec = 8;
cmptparm[i].bpp = 8;
cmptparm[i].sgnd = 0;
cmptparm[i].dx = (OPJ_UINT32)cparameters.subsampling_dx;
cmptparm[i].dy = (OPJ_UINT32)cparameters.subsampling_dy;
cmptparm[i].w = qimage.width();
cmptparm[i].h = qimage.height();
}
jp2Image = opj_image_create(numcmpts, &cmptparm[0],OPJ_CLRSPC_SRGB);
if(!jp2Image) return false;
if (numcmpts == 4)
{
jp2Image->comps[3].alpha = 1;
}
/* set image offset and reference grid */
jp2Image->x0 = (OPJ_UINT32)cparameters.image_offset_x0;
jp2Image->y0 = (OPJ_UINT32)cparameters.image_offset_y0;
jp2Image->x1 = jp2Image->x0 + (qimage.width() - 1U) * (OPJ_UINT32)cparameters.subsampling_dx + 1U;
jp2Image->y1 = jp2Image->y0 + (qimage.height() - 1U) * (OPJ_UINT32)cparameters.subsampling_dy + 1U;
int width = jp2Image->comps[0].w;
int height = jp2Image->comps[0].h;
QRgb *bits;
QRgb color;
int index = 0;
int x,y;
bits= (QRgb *)qimage.bits();
for(y = 0; y < height; y++)
{
for(x = 0; x < width; x++)
{
color=bits[x+y* width];
jp2Image->comps[0].data[index] = (OPJ_INT32)qRed(color); /* R */
jp2Image->comps[1].data[index] = (OPJ_INT32)qGreen(color); /* G */
jp2Image->comps[2].data[index] = (OPJ_INT32)qBlue(color); /* B */
index++;
}
}
return true;
}
|