2010-12-08 12:06:41 +01:00
|
|
|
/*
|
2014-04-03 17:30:57 +02:00
|
|
|
* The copyright in this software is being made available under the 2-clauses
|
|
|
|
* BSD License, included below. This software may be subject to other third
|
|
|
|
* party and contributor rights, including patent rights, and no such rights
|
|
|
|
* are granted under this license.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
|
|
|
|
* Copyright (c) 2002-2014, Professor Benoit Macq
|
2010-12-08 12:06:41 +01:00
|
|
|
* Copyright (c) 2001-2003, David Janssens
|
|
|
|
* Copyright (c) 2002-2003, Yannick Verschueren
|
2014-04-03 17:30:57 +02:00
|
|
|
* Copyright (c) 2003-2007, Francois-Olivier Devaux
|
|
|
|
* Copyright (c) 2003-2014, Antonin Descampe
|
2010-12-08 12:06:41 +01:00
|
|
|
* Copyright (c) 2005, Herve Drolon, FreeImage Team
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <math.h>
|
2014-03-12 14:55:41 +01:00
|
|
|
#include <assert.h>
|
2010-12-08 12:06:41 +01:00
|
|
|
|
2013-03-03 18:55:35 +01:00
|
|
|
#include "opj_apps_config.h"
|
2010-12-08 12:06:41 +01:00
|
|
|
#include "openjpeg.h"
|
|
|
|
#include "color.h"
|
|
|
|
|
2013-03-03 18:55:35 +01:00
|
|
|
#ifdef OPJ_HAVE_LIBLCMS2
|
2010-12-08 12:06:41 +01:00
|
|
|
#include <lcms2.h>
|
|
|
|
#endif
|
2013-03-03 18:55:35 +01:00
|
|
|
#ifdef OPJ_HAVE_LIBLCMS1
|
2010-12-08 12:06:41 +01:00
|
|
|
#include <lcms.h>
|
|
|
|
#endif
|
|
|
|
|
2012-11-16 10:31:12 +01:00
|
|
|
#ifdef OPJ_USE_LEGACY
|
2012-11-16 09:36:23 +01:00
|
|
|
#define OPJ_CLRSPC_GRAY CLRSPC_GRAY
|
|
|
|
#define OPJ_CLRSPC_SRGB CLRSPC_SRGB
|
|
|
|
#endif
|
|
|
|
|
2010-12-08 12:06:41 +01:00
|
|
|
/*--------------------------------------------------------
|
2012-08-30 18:51:30 +02:00
|
|
|
Matrix for sYCC, Amendment 1 to IEC 61966-2-1
|
2010-12-08 12:06:41 +01:00
|
|
|
|
|
|
|
Y : 0.299 0.587 0.114 :R
|
|
|
|
Cb: -0.1687 -0.3312 0.5 :G
|
|
|
|
Cr: 0.5 -0.4187 -0.0812 :B
|
|
|
|
|
|
|
|
Inverse:
|
|
|
|
|
|
|
|
R: 1 -3.68213e-05 1.40199 :Y
|
|
|
|
G: 1.00003 -0.344125 -0.714128 :Cb - 2^(prec - 1)
|
|
|
|
B: 0.999823 1.77204 -8.04142e-06 :Cr - 2^(prec - 1)
|
|
|
|
|
|
|
|
-----------------------------------------------------------*/
|
|
|
|
static void sycc_to_rgb(int offset, int upb, int y, int cb, int cr,
|
|
|
|
int *out_r, int *out_g, int *out_b)
|
|
|
|
{
|
|
|
|
int r, g, b;
|
|
|
|
|
|
|
|
cb -= offset; cr -= offset;
|
|
|
|
r = y + (int)(1.402 * (float)cr);
|
|
|
|
if(r < 0) r = 0; else if(r > upb) r = upb; *out_r = r;
|
|
|
|
|
|
|
|
g = y - (int)(0.344 * (float)cb + 0.714 * (float)cr);
|
|
|
|
if(g < 0) g = 0; else if(g > upb) g = upb; *out_g = g;
|
|
|
|
|
|
|
|
b = y + (int)(1.772 * (float)cb);
|
|
|
|
if(b < 0) b = 0; else if(b > upb) b = upb; *out_b = b;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sycc444_to_rgb(opj_image_t *img)
|
|
|
|
{
|
|
|
|
int *d0, *d1, *d2, *r, *g, *b;
|
|
|
|
const int *y, *cb, *cr;
|
2016-04-30 00:33:27 +02:00
|
|
|
size_t maxw, maxh, max, i;
|
2014-11-18 01:07:50 +01:00
|
|
|
int offset, upb;
|
2010-12-08 12:06:41 +01:00
|
|
|
|
2014-11-18 01:07:50 +01:00
|
|
|
upb = (int)img->comps[0].prec;
|
|
|
|
offset = 1<<(upb - 1); upb = (1<<upb)-1;
|
2010-12-08 12:06:41 +01:00
|
|
|
|
2016-04-30 00:33:27 +02:00
|
|
|
maxw = (size_t)img->comps[0].w; maxh = (size_t)img->comps[0].h;
|
2010-12-08 12:06:41 +01:00
|
|
|
max = maxw * maxh;
|
|
|
|
|
|
|
|
y = img->comps[0].data;
|
|
|
|
cb = img->comps[1].data;
|
|
|
|
cr = img->comps[2].data;
|
|
|
|
|
2016-04-30 00:33:27 +02:00
|
|
|
d0 = r = (int*)malloc(sizeof(int) * max);
|
|
|
|
d1 = g = (int*)malloc(sizeof(int) * max);
|
|
|
|
d2 = b = (int*)malloc(sizeof(int) * max);
|
2010-12-08 12:06:41 +01:00
|
|
|
|
2016-04-29 23:49:17 +02:00
|
|
|
if(r == NULL || g == NULL || b == NULL) goto fails;
|
|
|
|
|
2014-11-18 01:07:50 +01:00
|
|
|
for(i = 0U; i < max; ++i)
|
|
|
|
{
|
|
|
|
sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
|
|
|
|
++y; ++cb; ++cr; ++r; ++g; ++b;
|
|
|
|
}
|
2010-12-08 12:06:41 +01:00
|
|
|
free(img->comps[0].data); img->comps[0].data = d0;
|
|
|
|
free(img->comps[1].data); img->comps[1].data = d1;
|
|
|
|
free(img->comps[2].data); img->comps[2].data = d2;
|
2016-04-30 00:33:27 +02:00
|
|
|
img->color_space = OPJ_CLRSPC_SRGB;
|
2016-04-29 23:49:17 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
fails:
|
2016-04-30 00:33:27 +02:00
|
|
|
free(r);
|
|
|
|
free(g);
|
|
|
|
free(b);
|
2010-12-08 12:06:41 +01:00
|
|
|
}/* sycc444_to_rgb() */
|
|
|
|
|
|
|
|
static void sycc422_to_rgb(opj_image_t *img)
|
|
|
|
{
|
|
|
|
int *d0, *d1, *d2, *r, *g, *b;
|
|
|
|
const int *y, *cb, *cr;
|
2016-04-30 00:33:27 +02:00
|
|
|
size_t maxw, maxh, max, offx, loopmaxw;
|
2014-11-18 01:07:50 +01:00
|
|
|
int offset, upb;
|
2016-04-30 00:33:27 +02:00
|
|
|
size_t i;
|
2010-12-08 12:06:41 +01:00
|
|
|
|
2014-11-18 01:07:50 +01:00
|
|
|
upb = (int)img->comps[0].prec;
|
|
|
|
offset = 1<<(upb - 1); upb = (1<<upb)-1;
|
2010-12-08 12:06:41 +01:00
|
|
|
|
2016-04-30 00:33:27 +02:00
|
|
|
maxw = (size_t)img->comps[0].w; maxh = (size_t)img->comps[0].h;
|
2010-12-08 12:06:41 +01:00
|
|
|
max = maxw * maxh;
|
|
|
|
|
|
|
|
y = img->comps[0].data;
|
|
|
|
cb = img->comps[1].data;
|
|
|
|
cr = img->comps[2].data;
|
|
|
|
|
2016-04-30 00:33:27 +02:00
|
|
|
d0 = r = (int*)malloc(sizeof(int) * max);
|
|
|
|
d1 = g = (int*)malloc(sizeof(int) * max);
|
|
|
|
d2 = b = (int*)malloc(sizeof(int) * max);
|
2010-12-08 12:06:41 +01:00
|
|
|
|
2016-04-29 23:49:17 +02:00
|
|
|
if(r == NULL || g == NULL || b == NULL) goto fails;
|
|
|
|
|
2016-04-30 00:33:27 +02:00
|
|
|
/* if img->x0 is odd, then first column shall use Cb/Cr = 0 */
|
|
|
|
offx = img->x0 & 1U;
|
|
|
|
loopmaxw = maxw - offx;
|
|
|
|
|
2014-11-18 01:07:50 +01:00
|
|
|
for(i=0U; i < maxh; ++i)
|
2014-10-21 17:22:22 +02:00
|
|
|
{
|
2016-04-30 00:33:27 +02:00
|
|
|
size_t j;
|
|
|
|
|
|
|
|
if (offx > 0U) {
|
|
|
|
sycc_to_rgb(offset, upb, *y, 0, 0, r, g, b);
|
|
|
|
++y; ++r; ++g; ++b;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(j=0U; j < (loopmaxw & ~(size_t)1U); j += 2U)
|
2014-10-21 17:22:22 +02:00
|
|
|
{
|
|
|
|
sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
|
|
|
|
++y; ++r; ++g; ++b;
|
|
|
|
sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
|
|
|
|
++y; ++r; ++g; ++b; ++cb; ++cr;
|
|
|
|
}
|
2016-04-30 00:33:27 +02:00
|
|
|
if (j < loopmaxw) {
|
2014-10-21 17:22:22 +02:00
|
|
|
sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
|
|
|
|
++y; ++r; ++g; ++b; ++cb; ++cr;
|
|
|
|
}
|
|
|
|
}
|
2016-04-30 00:33:27 +02:00
|
|
|
|
2010-12-08 12:06:41 +01:00
|
|
|
free(img->comps[0].data); img->comps[0].data = d0;
|
|
|
|
free(img->comps[1].data); img->comps[1].data = d1;
|
|
|
|
free(img->comps[2].data); img->comps[2].data = d2;
|
|
|
|
|
2016-04-30 00:33:27 +02:00
|
|
|
img->comps[1].w = img->comps[2].w = img->comps[0].w;
|
|
|
|
img->comps[1].h = img->comps[2].h = img->comps[0].h;
|
|
|
|
img->comps[1].dx = img->comps[2].dx = img->comps[0].dx;
|
|
|
|
img->comps[1].dy = img->comps[2].dy = img->comps[0].dy;
|
|
|
|
img->color_space = OPJ_CLRSPC_SRGB;
|
2016-04-29 23:49:17 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
fails:
|
2016-04-30 00:33:27 +02:00
|
|
|
free(r);
|
|
|
|
free(g);
|
|
|
|
free(b);
|
2010-12-08 12:06:41 +01:00
|
|
|
}/* sycc422_to_rgb() */
|
|
|
|
|
|
|
|
static void sycc420_to_rgb(opj_image_t *img)
|
|
|
|
{
|
|
|
|
int *d0, *d1, *d2, *r, *g, *b, *nr, *ng, *nb;
|
|
|
|
const int *y, *cb, *cr, *ny;
|
2016-04-30 00:33:27 +02:00
|
|
|
size_t maxw, maxh, max, offx, loopmaxw, offy, loopmaxh;
|
2014-11-18 01:07:50 +01:00
|
|
|
int offset, upb;
|
2016-04-30 00:33:27 +02:00
|
|
|
size_t i;
|
2010-12-08 12:06:41 +01:00
|
|
|
|
2014-11-18 01:07:50 +01:00
|
|
|
upb = (int)img->comps[0].prec;
|
|
|
|
offset = 1<<(upb - 1); upb = (1<<upb)-1;
|
2010-12-08 12:06:41 +01:00
|
|
|
|
2016-04-30 00:33:27 +02:00
|
|
|
maxw = (size_t)img->comps[0].w; maxh = (size_t)img->comps[0].h;
|
2010-12-08 12:06:41 +01:00
|
|
|
max = maxw * maxh;
|
|
|
|
|
|
|
|
y = img->comps[0].data;
|
|
|
|
cb = img->comps[1].data;
|
|
|
|
cr = img->comps[2].data;
|
|
|
|
|
2016-04-30 00:33:27 +02:00
|
|
|
d0 = r = (int*)malloc(sizeof(int) * max);
|
|
|
|
d1 = g = (int*)malloc(sizeof(int) * max);
|
|
|
|
d2 = b = (int*)malloc(sizeof(int) * max);
|
|
|
|
|
|
|
|
if (r == NULL || g == NULL || b == NULL) goto fails;
|
|
|
|
|
|
|
|
/* if img->x0 is odd, then first column shall use Cb/Cr = 0 */
|
|
|
|
offx = img->x0 & 1U;
|
|
|
|
loopmaxw = maxw - offx;
|
|
|
|
/* if img->y0 is odd, then first line shall use Cb/Cr = 0 */
|
|
|
|
offy = img->y0 & 1U;
|
|
|
|
loopmaxh = maxh - offy;
|
|
|
|
|
|
|
|
if (offy > 0U) {
|
|
|
|
size_t j;
|
|
|
|
|
|
|
|
for(j=0; j < maxw; ++j)
|
|
|
|
{
|
|
|
|
sycc_to_rgb(offset, upb, *y, 0, 0, r, g, b);
|
|
|
|
++y; ++r; ++g; ++b;
|
|
|
|
}
|
|
|
|
}
|
2016-04-29 23:49:17 +02:00
|
|
|
|
2016-04-30 00:33:27 +02:00
|
|
|
for(i=0U; i < (loopmaxh & ~(size_t)1U); i += 2U)
|
2014-10-21 17:22:22 +02:00
|
|
|
{
|
2016-04-30 00:33:27 +02:00
|
|
|
size_t j;
|
|
|
|
|
2014-10-21 17:22:22 +02:00
|
|
|
ny = y + maxw;
|
|
|
|
nr = r + maxw; ng = g + maxw; nb = b + maxw;
|
2016-04-30 00:33:27 +02:00
|
|
|
|
|
|
|
if (offx > 0U) {
|
|
|
|
sycc_to_rgb(offset, upb, *y, 0, 0, r, g, b);
|
|
|
|
++y; ++r; ++g; ++b;
|
|
|
|
sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
|
|
|
|
++ny; ++nr; ++ng; ++nb;
|
|
|
|
}
|
2010-12-08 12:06:41 +01:00
|
|
|
|
2016-04-30 00:33:27 +02:00
|
|
|
for(j=0; j < (loopmaxw & ~(size_t)1U); j += 2U)
|
2014-10-21 17:22:22 +02:00
|
|
|
{
|
|
|
|
sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
|
|
|
|
++y; ++r; ++g; ++b;
|
|
|
|
sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
|
|
|
|
++y; ++r; ++g; ++b;
|
2010-12-08 12:06:41 +01:00
|
|
|
|
2014-10-21 17:22:22 +02:00
|
|
|
sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
|
|
|
|
++ny; ++nr; ++ng; ++nb;
|
|
|
|
sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
|
|
|
|
++ny; ++nr; ++ng; ++nb; ++cb; ++cr;
|
|
|
|
}
|
2016-04-30 00:33:27 +02:00
|
|
|
if(j < loopmaxw)
|
2014-10-21 17:22:22 +02:00
|
|
|
{
|
|
|
|
sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
|
|
|
|
++y; ++r; ++g; ++b;
|
|
|
|
|
|
|
|
sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
|
|
|
|
++ny; ++nr; ++ng; ++nb; ++cb; ++cr;
|
|
|
|
}
|
|
|
|
y += maxw; r += maxw; g += maxw; b += maxw;
|
|
|
|
}
|
2016-04-30 00:33:27 +02:00
|
|
|
if(i < loopmaxh)
|
2014-11-18 01:07:50 +01:00
|
|
|
{
|
2016-04-30 00:33:27 +02:00
|
|
|
size_t j;
|
|
|
|
|
|
|
|
for(j=0U; j < (maxw & ~(size_t)1U); j += 2U)
|
2014-10-21 17:22:22 +02:00
|
|
|
{
|
|
|
|
sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
|
|
|
|
|
|
|
|
++y; ++r; ++g; ++b;
|
|
|
|
|
|
|
|
sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
|
|
|
|
|
|
|
|
++y; ++r; ++g; ++b; ++cb; ++cr;
|
|
|
|
}
|
|
|
|
if(j < maxw)
|
|
|
|
{
|
|
|
|
sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
|
|
|
|
}
|
|
|
|
}
|
2010-12-08 12:06:41 +01:00
|
|
|
|
|
|
|
free(img->comps[0].data); img->comps[0].data = d0;
|
|
|
|
free(img->comps[1].data); img->comps[1].data = d1;
|
|
|
|
free(img->comps[2].data); img->comps[2].data = d2;
|
|
|
|
|
2016-04-30 00:33:27 +02:00
|
|
|
img->comps[1].w = img->comps[2].w = img->comps[0].w;
|
|
|
|
img->comps[1].h = img->comps[2].h = img->comps[0].h;
|
|
|
|
img->comps[1].dx = img->comps[2].dx = img->comps[0].dx;
|
|
|
|
img->comps[1].dy = img->comps[2].dy = img->comps[0].dy;
|
|
|
|
img->color_space = OPJ_CLRSPC_SRGB;
|
2016-04-29 23:49:17 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
fails:
|
2016-04-30 00:33:27 +02:00
|
|
|
free(r);
|
|
|
|
free(g);
|
|
|
|
free(b);
|
2010-12-08 12:06:41 +01:00
|
|
|
}/* sycc420_to_rgb() */
|
|
|
|
|
|
|
|
void color_sycc_to_rgb(opj_image_t *img)
|
|
|
|
{
|
2014-11-18 01:07:50 +01:00
|
|
|
if(img->numcomps < 3)
|
|
|
|
{
|
|
|
|
img->color_space = OPJ_CLRSPC_GRAY;
|
|
|
|
return;
|
|
|
|
}
|
2010-12-08 12:06:41 +01:00
|
|
|
|
|
|
|
if((img->comps[0].dx == 1)
|
|
|
|
&& (img->comps[1].dx == 2)
|
|
|
|
&& (img->comps[2].dx == 2)
|
|
|
|
&& (img->comps[0].dy == 1)
|
|
|
|
&& (img->comps[1].dy == 2)
|
|
|
|
&& (img->comps[2].dy == 2))/* horizontal and vertical sub-sample */
|
|
|
|
{
|
2014-11-18 01:07:50 +01:00
|
|
|
sycc420_to_rgb(img);
|
2010-12-08 12:06:41 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if((img->comps[0].dx == 1)
|
|
|
|
&& (img->comps[1].dx == 2)
|
|
|
|
&& (img->comps[2].dx == 2)
|
|
|
|
&& (img->comps[0].dy == 1)
|
|
|
|
&& (img->comps[1].dy == 1)
|
|
|
|
&& (img->comps[2].dy == 1))/* horizontal sub-sample only */
|
|
|
|
{
|
2014-11-18 01:07:50 +01:00
|
|
|
sycc422_to_rgb(img);
|
2010-12-08 12:06:41 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if((img->comps[0].dx == 1)
|
|
|
|
&& (img->comps[1].dx == 1)
|
|
|
|
&& (img->comps[2].dx == 1)
|
|
|
|
&& (img->comps[0].dy == 1)
|
|
|
|
&& (img->comps[1].dy == 1)
|
|
|
|
&& (img->comps[2].dy == 1))/* no sub-sample */
|
|
|
|
{
|
2014-11-18 01:07:50 +01:00
|
|
|
sycc444_to_rgb(img);
|
2010-12-08 12:06:41 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-11-18 01:07:50 +01:00
|
|
|
fprintf(stderr,"%s:%d:color_sycc_to_rgb\n\tCAN NOT CONVERT\n", __FILE__,__LINE__);
|
|
|
|
return;
|
2010-12-08 12:06:41 +01:00
|
|
|
}
|
|
|
|
}/* color_sycc_to_rgb() */
|
|
|
|
|
2013-03-17 18:03:00 +01:00
|
|
|
#if defined(OPJ_HAVE_LIBLCMS2) || defined(OPJ_HAVE_LIBLCMS1)
|
2015-07-31 01:31:12 +02:00
|
|
|
|
2013-03-17 18:03:00 +01:00
|
|
|
#ifdef OPJ_HAVE_LIBLCMS1
|
2010-12-08 12:06:41 +01:00
|
|
|
/* Bob Friesenhahn proposed:*/
|
|
|
|
#define cmsSigXYZData icSigXYZData
|
|
|
|
#define cmsSigLabData icSigLabData
|
|
|
|
#define cmsSigCmykData icSigCmykData
|
|
|
|
#define cmsSigYCbCrData icSigYCbCrData
|
|
|
|
#define cmsSigLuvData icSigLuvData
|
|
|
|
#define cmsSigGrayData icSigGrayData
|
|
|
|
#define cmsSigRgbData icSigRgbData
|
|
|
|
#define cmsUInt32Number DWORD
|
|
|
|
|
|
|
|
#define cmsColorSpaceSignature icColorSpaceSignature
|
|
|
|
#define cmsGetHeaderRenderingIntent cmsTakeRenderingIntent
|
|
|
|
|
2013-03-17 18:03:00 +01:00
|
|
|
#endif /* OPJ_HAVE_LIBLCMS1 */
|
2010-12-08 12:06:41 +01:00
|
|
|
|
2014-03-12 14:55:41 +01:00
|
|
|
/*#define DEBUG_PROFILE*/
|
2010-12-08 12:06:41 +01:00
|
|
|
void color_apply_icc_profile(opj_image_t *image)
|
|
|
|
{
|
|
|
|
cmsHPROFILE in_prof, out_prof;
|
|
|
|
cmsHTRANSFORM transform;
|
|
|
|
cmsColorSpaceSignature in_space, out_space;
|
2016-04-29 23:49:17 +02:00
|
|
|
cmsUInt32Number intent, in_type, out_type;
|
2010-12-08 12:06:41 +01:00
|
|
|
int *r, *g, *b;
|
2016-04-29 23:49:17 +02:00
|
|
|
size_t nr_samples;
|
|
|
|
int prec, i, max, max_w, max_h, ok = 0;
|
|
|
|
OPJ_COLOR_SPACE new_space;
|
2010-12-08 12:06:41 +01:00
|
|
|
|
|
|
|
in_prof =
|
|
|
|
cmsOpenProfileFromMem(image->icc_profile_buf, image->icc_profile_len);
|
2014-03-12 14:55:41 +01:00
|
|
|
#ifdef DEBUG_PROFILE
|
|
|
|
FILE *icm = fopen("debug.icm","wb");
|
|
|
|
fwrite( image->icc_profile_buf,1, image->icc_profile_len,icm);
|
|
|
|
fclose(icm);
|
|
|
|
#endif
|
2011-07-03 19:56:49 +02:00
|
|
|
|
|
|
|
if(in_prof == NULL) return;
|
|
|
|
|
2010-12-08 12:06:41 +01:00
|
|
|
in_space = cmsGetPCS(in_prof);
|
|
|
|
out_space = cmsGetColorSpace(in_prof);
|
|
|
|
intent = cmsGetHeaderRenderingIntent(in_prof);
|
|
|
|
|
|
|
|
|
2014-03-10 15:05:25 +01:00
|
|
|
max_w = (int)image->comps[0].w;
|
|
|
|
max_h = (int)image->comps[0].h;
|
|
|
|
prec = (int)image->comps[0].prec;
|
2010-12-08 12:06:41 +01:00
|
|
|
|
|
|
|
if(out_space == cmsSigRgbData) /* enumCS 16 */
|
|
|
|
{
|
2015-07-31 01:31:12 +02:00
|
|
|
if( prec <= 8 )
|
|
|
|
{
|
2014-03-12 15:18:25 +01:00
|
|
|
in_type = TYPE_RGB_8;
|
|
|
|
out_type = TYPE_RGB_8;
|
2015-07-31 01:31:12 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-12-08 12:06:41 +01:00
|
|
|
in_type = TYPE_RGB_16;
|
|
|
|
out_type = TYPE_RGB_16;
|
2015-07-31 01:31:12 +02:00
|
|
|
}
|
2010-12-08 12:06:41 +01:00
|
|
|
out_prof = cmsCreate_sRGBProfile();
|
2016-04-29 23:49:17 +02:00
|
|
|
new_space = OPJ_CLRSPC_SRGB;
|
2010-12-08 12:06:41 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if(out_space == cmsSigGrayData) /* enumCS 17 */
|
|
|
|
{
|
|
|
|
in_type = TYPE_GRAY_8;
|
|
|
|
out_type = TYPE_RGB_8;
|
|
|
|
out_prof = cmsCreate_sRGBProfile();
|
2016-04-29 23:49:17 +02:00
|
|
|
new_space = OPJ_CLRSPC_SRGB;
|
2010-12-08 12:06:41 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if(out_space == cmsSigYCbCrData) /* enumCS 18 */
|
|
|
|
{
|
|
|
|
in_type = TYPE_YCbCr_16;
|
|
|
|
out_type = TYPE_RGB_16;
|
|
|
|
out_prof = cmsCreate_sRGBProfile();
|
2016-04-29 23:49:17 +02:00
|
|
|
new_space = OPJ_CLRSPC_SRGB;
|
2010-12-08 12:06:41 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_PROFILE
|
|
|
|
fprintf(stderr,"%s:%d: color_apply_icc_profile\n\tICC Profile has unknown "
|
|
|
|
"output colorspace(%#x)(%c%c%c%c)\n\tICC Profile ignored.\n",
|
|
|
|
__FILE__,__LINE__,out_space,
|
2015-07-31 01:31:12 +02:00
|
|
|
(out_space>>24) & 0xff,(out_space>>16) & 0xff,
|
|
|
|
(out_space>>8) & 0xff, out_space & 0xff);
|
2010-12-08 12:06:41 +01:00
|
|
|
#endif
|
2016-04-29 23:49:17 +02:00
|
|
|
cmsCloseProfile(in_prof);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(out_prof == NULL)
|
|
|
|
{
|
|
|
|
cmsCloseProfile(in_prof);
|
|
|
|
|
2010-12-08 12:06:41 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG_PROFILE
|
|
|
|
fprintf(stderr,"%s:%d:color_apply_icc_profile\n\tchannels(%d) prec(%d) w(%d) h(%d)"
|
|
|
|
"\n\tprofile: in(%p) out(%p)\n",__FILE__,__LINE__,image->numcomps,prec,
|
2015-07-31 01:31:12 +02:00
|
|
|
max_w,max_h, (void*)in_prof,(void*)out_prof);
|
2010-12-08 12:06:41 +01:00
|
|
|
|
|
|
|
fprintf(stderr,"\trender_intent (%u)\n\t"
|
|
|
|
"color_space: in(%#x)(%c%c%c%c) out:(%#x)(%c%c%c%c)\n\t"
|
|
|
|
" type: in(%u) out:(%u)\n",
|
2015-07-31 01:31:12 +02:00
|
|
|
intent,
|
|
|
|
in_space,
|
|
|
|
(in_space>>24) & 0xff,(in_space>>16) & 0xff,
|
|
|
|
(in_space>>8) & 0xff, in_space & 0xff,
|
2010-12-08 12:06:41 +01:00
|
|
|
|
2015-07-31 01:31:12 +02:00
|
|
|
out_space,
|
|
|
|
(out_space>>24) & 0xff,(out_space>>16) & 0xff,
|
|
|
|
(out_space>>8) & 0xff, out_space & 0xff,
|
2010-12-08 12:06:41 +01:00
|
|
|
|
2015-07-31 01:31:12 +02:00
|
|
|
in_type,out_type
|
2010-12-08 12:06:41 +01:00
|
|
|
);
|
2014-03-10 09:13:05 +01:00
|
|
|
#else
|
|
|
|
(void)prec;
|
|
|
|
(void)in_space;
|
2010-12-08 12:06:41 +01:00
|
|
|
#endif /* DEBUG_PROFILE */
|
|
|
|
|
|
|
|
transform = cmsCreateTransform(in_prof, in_type,
|
|
|
|
out_prof, out_type, intent, 0);
|
|
|
|
|
2013-03-17 18:03:00 +01:00
|
|
|
#ifdef OPJ_HAVE_LIBLCMS2
|
2010-12-08 12:06:41 +01:00
|
|
|
/* Possible for: LCMS_VERSION >= 2000 :*/
|
|
|
|
cmsCloseProfile(in_prof);
|
|
|
|
cmsCloseProfile(out_prof);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if(transform == NULL)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_PROFILE
|
|
|
|
fprintf(stderr,"%s:%d:color_apply_icc_profile\n\tcmsCreateTransform failed. "
|
|
|
|
"ICC Profile ignored.\n",__FILE__,__LINE__);
|
|
|
|
#endif
|
2016-04-29 23:49:17 +02:00
|
|
|
|
2013-03-17 18:03:00 +01:00
|
|
|
#ifdef OPJ_HAVE_LIBLCMS1
|
2010-12-08 12:06:41 +01:00
|
|
|
cmsCloseProfile(in_prof);
|
|
|
|
cmsCloseProfile(out_prof);
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(image->numcomps > 2)/* RGB, RGBA */
|
|
|
|
{
|
2015-07-31 01:31:12 +02:00
|
|
|
if( prec <= 8 )
|
|
|
|
{
|
2014-03-12 15:18:25 +01:00
|
|
|
unsigned char *inbuf, *outbuf, *in, *out;
|
2016-04-29 23:49:17 +02:00
|
|
|
|
2014-03-12 15:18:25 +01:00
|
|
|
max = max_w * max_h;
|
2016-04-29 23:49:17 +02:00
|
|
|
nr_samples = (size_t)(max * 3 * sizeof(unsigned char));
|
2014-03-12 15:18:25 +01:00
|
|
|
in = inbuf = (unsigned char*)malloc(nr_samples);
|
|
|
|
out = outbuf = (unsigned char*)malloc(nr_samples);
|
|
|
|
|
2016-04-29 23:49:17 +02:00
|
|
|
if(inbuf == NULL || outbuf == NULL) goto fails0;
|
|
|
|
|
2014-03-12 15:18:25 +01:00
|
|
|
r = image->comps[0].data;
|
|
|
|
g = image->comps[1].data;
|
|
|
|
b = image->comps[2].data;
|
|
|
|
|
|
|
|
for(i = 0; i < max; ++i)
|
2015-07-31 01:31:12 +02:00
|
|
|
{
|
2014-03-12 15:18:25 +01:00
|
|
|
*in++ = (unsigned char)*r++;
|
|
|
|
*in++ = (unsigned char)*g++;
|
|
|
|
*in++ = (unsigned char)*b++;
|
2015-07-31 01:31:12 +02:00
|
|
|
}
|
2014-03-12 15:18:25 +01:00
|
|
|
|
|
|
|
cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
|
|
|
|
|
|
|
|
r = image->comps[0].data;
|
|
|
|
g = image->comps[1].data;
|
|
|
|
b = image->comps[2].data;
|
|
|
|
|
|
|
|
for(i = 0; i < max; ++i)
|
2015-07-31 01:31:12 +02:00
|
|
|
{
|
2014-03-12 15:18:25 +01:00
|
|
|
*r++ = (int)*out++;
|
|
|
|
*g++ = (int)*out++;
|
|
|
|
*b++ = (int)*out++;
|
2015-07-31 01:31:12 +02:00
|
|
|
}
|
2016-04-29 23:49:17 +02:00
|
|
|
ok = 1;
|
|
|
|
|
|
|
|
fails0:
|
|
|
|
if(inbuf) free(inbuf);
|
|
|
|
if(outbuf) free(outbuf);
|
2015-07-31 01:31:12 +02:00
|
|
|
}
|
2016-04-29 23:49:17 +02:00
|
|
|
else /* prec > 8 */
|
2015-07-31 01:31:12 +02:00
|
|
|
{
|
2010-12-08 12:06:41 +01:00
|
|
|
unsigned short *inbuf, *outbuf, *in, *out;
|
2016-04-29 23:49:17 +02:00
|
|
|
|
2014-03-03 13:00:24 +01:00
|
|
|
max = max_w * max_h;
|
2016-04-29 23:49:17 +02:00
|
|
|
nr_samples = (size_t)(max * 3 * sizeof(unsigned short));
|
2010-12-08 12:06:41 +01:00
|
|
|
in = inbuf = (unsigned short*)malloc(nr_samples);
|
|
|
|
out = outbuf = (unsigned short*)malloc(nr_samples);
|
|
|
|
|
2016-04-29 23:49:17 +02:00
|
|
|
if(inbuf == NULL || outbuf == NULL) goto fails1;
|
|
|
|
|
2010-12-08 12:06:41 +01:00
|
|
|
r = image->comps[0].data;
|
|
|
|
g = image->comps[1].data;
|
|
|
|
b = image->comps[2].data;
|
|
|
|
|
|
|
|
for(i = 0; i < max; ++i)
|
2015-07-31 01:31:12 +02:00
|
|
|
{
|
2010-12-08 12:06:41 +01:00
|
|
|
*in++ = (unsigned short)*r++;
|
|
|
|
*in++ = (unsigned short)*g++;
|
|
|
|
*in++ = (unsigned short)*b++;
|
2015-07-31 01:31:12 +02:00
|
|
|
}
|
2010-12-08 12:06:41 +01:00
|
|
|
|
2014-03-10 15:05:25 +01:00
|
|
|
cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
|
2010-12-08 12:06:41 +01:00
|
|
|
|
|
|
|
r = image->comps[0].data;
|
|
|
|
g = image->comps[1].data;
|
|
|
|
b = image->comps[2].data;
|
|
|
|
|
|
|
|
for(i = 0; i < max; ++i)
|
2015-07-31 01:31:12 +02:00
|
|
|
{
|
2010-12-08 12:06:41 +01:00
|
|
|
*r++ = (int)*out++;
|
|
|
|
*g++ = (int)*out++;
|
|
|
|
*b++ = (int)*out++;
|
2015-07-31 01:31:12 +02:00
|
|
|
}
|
2016-04-29 23:49:17 +02:00
|
|
|
ok = 1;
|
|
|
|
|
|
|
|
fails1:
|
|
|
|
if(inbuf) free(inbuf);
|
|
|
|
if(outbuf) free(outbuf);
|
2015-07-31 01:31:12 +02:00
|
|
|
}
|
2010-12-08 12:06:41 +01:00
|
|
|
}
|
2016-04-29 23:49:17 +02:00
|
|
|
else /* image->numcomps <= 2 : GRAY, GRAYA */
|
2010-12-08 12:06:41 +01:00
|
|
|
{
|
2016-04-29 23:49:17 +02:00
|
|
|
if(prec <= 8)
|
|
|
|
{
|
2010-12-08 12:06:41 +01:00
|
|
|
unsigned char *in, *inbuf, *out, *outbuf;
|
2016-04-29 23:49:17 +02:00
|
|
|
opj_image_comp_t *new_comps;
|
|
|
|
|
2015-07-31 01:31:12 +02:00
|
|
|
max = max_w * max_h;
|
2016-04-29 23:49:17 +02:00
|
|
|
nr_samples = (size_t)(max * 3 * sizeof(unsigned char));
|
2010-12-08 12:06:41 +01:00
|
|
|
in = inbuf = (unsigned char*)malloc(nr_samples);
|
|
|
|
out = outbuf = (unsigned char*)malloc(nr_samples);
|
2016-04-29 23:49:17 +02:00
|
|
|
g = (int*)calloc((size_t)max, sizeof(int));
|
|
|
|
b = (int*)calloc((size_t)max, sizeof(int));
|
|
|
|
|
|
|
|
if(inbuf == NULL || outbuf == NULL || g == NULL || b == NULL) goto fails2;
|
2010-12-08 12:06:41 +01:00
|
|
|
|
2016-04-29 23:49:17 +02:00
|
|
|
new_comps = (opj_image_comp_t*)
|
2010-12-08 12:06:41 +01:00
|
|
|
realloc(image->comps, (image->numcomps+2)*sizeof(opj_image_comp_t));
|
|
|
|
|
2016-04-29 23:49:17 +02:00
|
|
|
if(new_comps == NULL) goto fails2;
|
|
|
|
|
|
|
|
image->comps = new_comps;
|
|
|
|
|
2010-12-08 12:06:41 +01:00
|
|
|
if(image->numcomps == 2)
|
|
|
|
image->comps[3] = image->comps[1];
|
|
|
|
|
|
|
|
image->comps[1] = image->comps[0];
|
|
|
|
image->comps[2] = image->comps[0];
|
|
|
|
|
2016-04-29 23:49:17 +02:00
|
|
|
image->comps[1].data = g;
|
|
|
|
image->comps[2].data = b;
|
2010-12-08 12:06:41 +01:00
|
|
|
|
|
|
|
image->numcomps += 2;
|
|
|
|
|
|
|
|
r = image->comps[0].data;
|
|
|
|
|
|
|
|
for(i = 0; i < max; ++i)
|
2016-04-29 23:49:17 +02:00
|
|
|
{
|
2010-12-08 12:06:41 +01:00
|
|
|
*in++ = (unsigned char)*r++;
|
2016-04-29 23:49:17 +02:00
|
|
|
}
|
2014-03-10 15:05:25 +01:00
|
|
|
cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
|
2010-12-08 12:06:41 +01:00
|
|
|
|
|
|
|
r = image->comps[0].data;
|
|
|
|
g = image->comps[1].data;
|
|
|
|
b = image->comps[2].data;
|
|
|
|
|
|
|
|
for(i = 0; i < max; ++i)
|
2016-04-29 23:49:17 +02:00
|
|
|
{
|
2010-12-08 12:06:41 +01:00
|
|
|
*r++ = (int)*out++; *g++ = (int)*out++; *b++ = (int)*out++;
|
2016-04-29 23:49:17 +02:00
|
|
|
}
|
|
|
|
r = g = b = NULL;
|
|
|
|
ok = 1;
|
|
|
|
|
|
|
|
fails2:
|
|
|
|
if(inbuf) free(inbuf);
|
|
|
|
if(outbuf) free(outbuf);
|
|
|
|
if(g) free(g);
|
|
|
|
if(b) free(b);
|
2010-12-08 12:06:41 +01:00
|
|
|
}
|
2016-04-29 23:49:17 +02:00
|
|
|
else /* prec > 8 */
|
|
|
|
{
|
|
|
|
unsigned short *in, *inbuf, *out, *outbuf;
|
|
|
|
opj_image_comp_t *new_comps;
|
|
|
|
|
|
|
|
max = max_w * max_h;
|
|
|
|
nr_samples = (size_t)(max * 3 * sizeof(unsigned short));
|
|
|
|
in = inbuf = (unsigned short*)malloc(nr_samples);
|
|
|
|
out = outbuf = (unsigned short*)malloc(nr_samples);
|
|
|
|
g = (int*)calloc((size_t)max, sizeof(int));
|
|
|
|
b = (int*)calloc((size_t)max, sizeof(int));
|
|
|
|
|
|
|
|
if(inbuf == NULL || outbuf == NULL || g == NULL || b == NULL) goto fails3;
|
|
|
|
|
|
|
|
new_comps = (opj_image_comp_t*)
|
|
|
|
realloc(image->comps, (image->numcomps+2)*sizeof(opj_image_comp_t));
|
|
|
|
|
|
|
|
if(new_comps == NULL) goto fails3;
|
|
|
|
|
|
|
|
image->comps = new_comps;
|
|
|
|
|
|
|
|
if(image->numcomps == 2)
|
|
|
|
image->comps[3] = image->comps[1];
|
|
|
|
|
|
|
|
image->comps[1] = image->comps[0];
|
|
|
|
image->comps[2] = image->comps[0];
|
|
|
|
|
|
|
|
image->comps[1].data = g;
|
|
|
|
image->comps[2].data = b;
|
|
|
|
|
|
|
|
image->numcomps += 2;
|
|
|
|
|
|
|
|
r = image->comps[0].data;
|
|
|
|
|
|
|
|
for(i = 0; i < max; ++i)
|
|
|
|
{
|
|
|
|
*in++ = (unsigned short)*r++;
|
|
|
|
}
|
|
|
|
cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
|
2010-12-08 12:06:41 +01:00
|
|
|
|
2016-04-29 23:49:17 +02:00
|
|
|
r = image->comps[0].data;
|
|
|
|
g = image->comps[1].data;
|
|
|
|
b = image->comps[2].data;
|
|
|
|
|
|
|
|
for(i = 0; i < max; ++i)
|
|
|
|
{
|
|
|
|
*r++ = (int)*out++; *g++ = (int)*out++; *b++ = (int)*out++;
|
|
|
|
}
|
|
|
|
r = g = b = NULL;
|
|
|
|
ok = 1;
|
|
|
|
|
|
|
|
fails3:
|
|
|
|
if(inbuf) free(inbuf);
|
|
|
|
if(outbuf) free(outbuf);
|
|
|
|
if(g) free(g);
|
|
|
|
if(b) free(b);
|
|
|
|
}
|
|
|
|
}/* if(image->numcomps > 2) */
|
2010-12-08 12:06:41 +01:00
|
|
|
|
|
|
|
cmsDeleteTransform(transform);
|
|
|
|
|
2013-03-17 18:03:00 +01:00
|
|
|
#ifdef OPJ_HAVE_LIBLCMS1
|
2010-12-08 12:06:41 +01:00
|
|
|
cmsCloseProfile(in_prof);
|
|
|
|
cmsCloseProfile(out_prof);
|
|
|
|
#endif
|
2016-04-29 23:49:17 +02:00
|
|
|
if(ok)
|
|
|
|
{
|
|
|
|
image->color_space = new_space;
|
|
|
|
}
|
2010-12-08 12:06:41 +01:00
|
|
|
}/* color_apply_icc_profile() */
|
|
|
|
|
2015-09-13 14:51:00 +02:00
|
|
|
void color_cielab_to_rgb(opj_image_t *image)
|
2015-07-31 01:31:12 +02:00
|
|
|
{
|
|
|
|
int *row;
|
|
|
|
int enumcs, numcomps;
|
2016-04-29 23:49:17 +02:00
|
|
|
OPJ_COLOR_SPACE new_space;
|
|
|
|
|
2015-08-21 21:41:50 +02:00
|
|
|
numcomps = (int)image->numcomps;
|
2015-08-21 20:17:55 +02:00
|
|
|
|
2015-07-31 01:31:12 +02:00
|
|
|
if(numcomps != 3)
|
2015-08-21 20:17:55 +02:00
|
|
|
{
|
|
|
|
fprintf(stderr,"%s:%d:\n\tnumcomps %d not handled. Quitting.\n",
|
|
|
|
__FILE__,__LINE__,numcomps);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-31 01:31:12 +02:00
|
|
|
row = (int*)image->icc_profile_buf;
|
|
|
|
enumcs = row[0];
|
2015-08-21 20:17:55 +02:00
|
|
|
|
2015-09-09 23:38:46 +02:00
|
|
|
if(enumcs == 14) /* CIELab */
|
2015-08-21 20:17:55 +02:00
|
|
|
{
|
|
|
|
int *L, *a, *b, *red, *green, *blue;
|
|
|
|
int *src0, *src1, *src2, *dst0, *dst1, *dst2;
|
|
|
|
double rl, ol, ra, oa, rb, ob, prec0, prec1, prec2;
|
|
|
|
double minL, maxL, mina, maxa, minb, maxb;
|
|
|
|
unsigned int default_type;
|
|
|
|
unsigned int i, max;
|
|
|
|
cmsHPROFILE in, out;
|
|
|
|
cmsHTRANSFORM transform;
|
|
|
|
cmsUInt16Number RGB[3];
|
|
|
|
cmsCIELab Lab;
|
|
|
|
|
|
|
|
in = cmsCreateLab4Profile(NULL);
|
2016-04-29 23:49:17 +02:00
|
|
|
if(in == NULL){
|
|
|
|
return;
|
|
|
|
}
|
2015-08-21 20:17:55 +02:00
|
|
|
out = cmsCreate_sRGBProfile();
|
2016-04-29 23:49:17 +02:00
|
|
|
if(out == NULL){
|
|
|
|
cmsCloseProfile(in);
|
|
|
|
return;
|
|
|
|
}
|
2015-08-21 20:17:55 +02:00
|
|
|
transform = cmsCreateTransform(in, TYPE_Lab_DBL, out, TYPE_RGB_16, INTENT_PERCEPTUAL, 0);
|
|
|
|
|
2015-09-13 14:42:56 +02:00
|
|
|
#ifdef OPJ_HAVE_LIBLCMS2
|
2015-08-21 20:17:55 +02:00
|
|
|
cmsCloseProfile(in);
|
|
|
|
cmsCloseProfile(out);
|
2015-07-31 01:31:12 +02:00
|
|
|
#endif
|
2015-08-21 20:17:55 +02:00
|
|
|
if(transform == NULL)
|
|
|
|
{
|
2015-09-13 14:42:56 +02:00
|
|
|
#ifdef OPJ_HAVE_LIBLCMS1
|
2015-08-21 20:17:55 +02:00
|
|
|
cmsCloseProfile(in);
|
|
|
|
cmsCloseProfile(out);
|
2015-07-31 01:31:12 +02:00
|
|
|
#endif
|
2015-08-21 20:17:55 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-04-29 23:49:17 +02:00
|
|
|
new_space = OPJ_CLRSPC_SRGB;
|
|
|
|
|
2015-08-21 20:17:55 +02:00
|
|
|
prec0 = (double)image->comps[0].prec;
|
|
|
|
prec1 = (double)image->comps[1].prec;
|
|
|
|
prec2 = (double)image->comps[2].prec;
|
|
|
|
|
2015-08-21 21:41:50 +02:00
|
|
|
default_type = (unsigned int)row[1];
|
2015-08-21 20:17:55 +02:00
|
|
|
|
2015-10-12 21:25:53 +02:00
|
|
|
if(default_type == 0x44454600)/* DEF : default */
|
2015-08-21 20:17:55 +02:00
|
|
|
{
|
|
|
|
rl = 100; ra = 170; rb = 200;
|
|
|
|
ol = 0;
|
|
|
|
oa = pow(2, prec1 - 1);
|
|
|
|
ob = pow(2, prec2 - 2) + pow(2, prec2 - 3);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rl = row[2]; ra = row[4]; rb = row[6];
|
|
|
|
ol = row[3]; oa = row[5]; ob = row[7];
|
|
|
|
}
|
|
|
|
|
|
|
|
L = src0 = image->comps[0].data;
|
|
|
|
a = src1 = image->comps[1].data;
|
|
|
|
b = src2 = image->comps[2].data;
|
|
|
|
|
|
|
|
max = image->comps[0].w * image->comps[0].h;
|
|
|
|
|
|
|
|
red = dst0 = (int*)malloc(max * sizeof(int));
|
|
|
|
green = dst1 = (int*)malloc(max * sizeof(int));
|
|
|
|
blue = dst2 = (int*)malloc(max * sizeof(int));
|
2016-04-29 23:49:17 +02:00
|
|
|
|
|
|
|
if(red == NULL || green == NULL || blue == NULL) goto fails;
|
|
|
|
|
2015-08-21 20:17:55 +02:00
|
|
|
minL = -(rl * ol)/(pow(2, prec0)-1);
|
|
|
|
maxL = minL + rl;
|
|
|
|
|
|
|
|
mina = -(ra * oa)/(pow(2, prec1)-1);
|
|
|
|
maxa = mina + ra;
|
|
|
|
|
|
|
|
minb = -(rb * ob)/(pow(2, prec2)-1);
|
|
|
|
maxb = minb + rb;
|
|
|
|
|
|
|
|
for(i = 0; i < max; ++i)
|
|
|
|
{
|
|
|
|
Lab.L = minL + (double)(*L) * (maxL - minL)/(pow(2, prec0)-1); ++L;
|
|
|
|
Lab.a = mina + (double)(*a) * (maxa - mina)/(pow(2, prec1)-1); ++a;
|
|
|
|
Lab.b = minb + (double)(*b) * (maxb - minb)/(pow(2, prec2)-1); ++b;
|
|
|
|
|
|
|
|
cmsDoTransform(transform, &Lab, RGB, 1);
|
|
|
|
|
|
|
|
*red++ = RGB[0];
|
|
|
|
*green++ = RGB[1];
|
|
|
|
*blue++ = RGB[2];
|
|
|
|
}
|
|
|
|
cmsDeleteTransform(transform);
|
2015-09-13 14:42:56 +02:00
|
|
|
#ifdef OPJ_HAVE_LIBLCMS1
|
2015-08-21 20:17:55 +02:00
|
|
|
cmsCloseProfile(in);
|
|
|
|
cmsCloseProfile(out);
|
2015-07-31 01:31:12 +02:00
|
|
|
#endif
|
2015-08-21 20:17:55 +02:00
|
|
|
free(src0); image->comps[0].data = dst0;
|
|
|
|
free(src1); image->comps[1].data = dst1;
|
|
|
|
free(src2); image->comps[2].data = dst2;
|
|
|
|
|
2016-04-29 23:49:17 +02:00
|
|
|
image->color_space = new_space;
|
2015-08-21 20:17:55 +02:00
|
|
|
image->comps[0].prec = 16;
|
|
|
|
image->comps[1].prec = 16;
|
|
|
|
image->comps[2].prec = 16;
|
|
|
|
|
|
|
|
return;
|
2016-04-29 23:49:17 +02:00
|
|
|
|
|
|
|
fails:
|
|
|
|
cmsDeleteTransform(transform);
|
|
|
|
#ifdef OPJ_HAVE_LIBLCMS1
|
|
|
|
cmsCloseProfile(in);
|
|
|
|
cmsCloseProfile(out);
|
|
|
|
#endif
|
|
|
|
if(red) free(red);
|
|
|
|
if(green) free(green);
|
|
|
|
if(blue) free(blue);
|
|
|
|
return;
|
2015-08-21 20:17:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr,"%s:%d:\n\tenumCS %d not handled. Ignoring.\n", __FILE__,__LINE__, enumcs);
|
2016-04-29 23:49:17 +02:00
|
|
|
}/* color_cielab_to_rgb() */
|
2015-07-31 01:31:12 +02:00
|
|
|
|
2015-10-12 21:25:53 +02:00
|
|
|
#endif /* OPJ_HAVE_LIBLCMS2 || OPJ_HAVE_LIBLCMS1 */
|
2015-07-31 01:31:12 +02:00
|
|
|
|
|
|
|
void color_cmyk_to_rgb(opj_image_t *image)
|
|
|
|
{
|
|
|
|
float C, M, Y, K;
|
2015-08-21 20:17:55 +02:00
|
|
|
float sC, sM, sY, sK;
|
|
|
|
unsigned int w, h, max, i;
|
2015-07-31 01:31:12 +02:00
|
|
|
|
|
|
|
w = image->comps[0].w;
|
|
|
|
h = image->comps[0].h;
|
|
|
|
|
2015-08-21 20:17:55 +02:00
|
|
|
if(image->numcomps < 4) return;
|
2015-07-31 01:31:12 +02:00
|
|
|
|
|
|
|
max = w * h;
|
2015-08-21 20:17:55 +02:00
|
|
|
|
|
|
|
sC = 1.0F / (float)((1 << image->comps[0].prec) - 1);
|
|
|
|
sM = 1.0F / (float)((1 << image->comps[1].prec) - 1);
|
|
|
|
sY = 1.0F / (float)((1 << image->comps[2].prec) - 1);
|
|
|
|
sK = 1.0F / (float)((1 << image->comps[3].prec) - 1);
|
2015-07-31 01:31:12 +02:00
|
|
|
|
|
|
|
for(i = 0; i < max; ++i)
|
2015-08-21 20:17:55 +02:00
|
|
|
{
|
|
|
|
/* CMYK values from 0 to 1 */
|
|
|
|
C = (float)(image->comps[0].data[i]) * sC;
|
|
|
|
M = (float)(image->comps[1].data[i]) * sM;
|
|
|
|
Y = (float)(image->comps[2].data[i]) * sY;
|
|
|
|
K = (float)(image->comps[3].data[i]) * sK;
|
|
|
|
|
|
|
|
/* Invert all CMYK values */
|
|
|
|
C = 1.0F - C;
|
|
|
|
M = 1.0F - M;
|
|
|
|
Y = 1.0F - Y;
|
|
|
|
K = 1.0F - K;
|
|
|
|
|
|
|
|
/* CMYK -> RGB : RGB results from 0 to 255 */
|
|
|
|
image->comps[0].data[i] = (int)(255.0F * C * K); /* R */
|
|
|
|
image->comps[1].data[i] = (int)(255.0F * M * K); /* G */
|
|
|
|
image->comps[2].data[i] = (int)(255.0F * Y * K); /* B */
|
|
|
|
}
|
2015-07-31 01:31:12 +02:00
|
|
|
|
2015-08-21 20:17:55 +02:00
|
|
|
free(image->comps[3].data); image->comps[3].data = NULL;
|
|
|
|
image->comps[0].prec = 8;
|
|
|
|
image->comps[1].prec = 8;
|
|
|
|
image->comps[2].prec = 8;
|
|
|
|
image->numcomps -= 1;
|
2015-07-31 01:31:12 +02:00
|
|
|
image->color_space = OPJ_CLRSPC_SRGB;
|
2015-08-21 20:17:55 +02:00
|
|
|
|
|
|
|
for (i = 3; i < image->numcomps; ++i) {
|
|
|
|
memcpy(&(image->comps[i]), &(image->comps[i+1]), sizeof(image->comps[i]));
|
|
|
|
}
|
2015-07-31 01:31:12 +02:00
|
|
|
|
2015-10-12 21:25:53 +02:00
|
|
|
}/* color_cmyk_to_rgb() */
|
2015-07-31 01:31:12 +02:00
|
|
|
|
2015-10-12 21:25:53 +02:00
|
|
|
/*
|
|
|
|
* This code has been adopted from sjpx_openjpeg.c of ghostscript
|
|
|
|
*/
|
2015-07-31 01:31:12 +02:00
|
|
|
void color_esycc_to_rgb(opj_image_t *image)
|
|
|
|
{
|
2015-08-21 20:17:55 +02:00
|
|
|
int y, cb, cr, sign1, sign2, val;
|
|
|
|
unsigned int w, h, max, i;
|
|
|
|
int flip_value = (1 << (image->comps[0].prec-1));
|
2015-08-23 11:43:38 +02:00
|
|
|
int max_value = (1 << image->comps[0].prec) - 1;
|
2015-08-21 20:17:55 +02:00
|
|
|
|
2016-04-29 23:51:14 +02:00
|
|
|
if (
|
|
|
|
(image->numcomps < 3)
|
|
|
|
|| (image->comps[0].dx != image->comps[1].dx) || (image->comps[0].dx != image->comps[2].dx)
|
|
|
|
|| (image->comps[0].dy != image->comps[1].dy) || (image->comps[0].dy != image->comps[2].dy)
|
|
|
|
) {
|
|
|
|
fprintf(stderr,"%s:%d:color_esycc_to_rgb\n\tCAN NOT CONVERT\n", __FILE__,__LINE__);
|
|
|
|
return;
|
|
|
|
}
|
2015-08-21 20:17:55 +02:00
|
|
|
|
|
|
|
w = image->comps[0].w;
|
|
|
|
h = image->comps[0].h;
|
|
|
|
|
2015-08-21 21:41:50 +02:00
|
|
|
sign1 = (int)image->comps[1].sgnd;
|
|
|
|
sign2 = (int)image->comps[2].sgnd;
|
2015-08-21 20:17:55 +02:00
|
|
|
|
|
|
|
max = w * h;
|
|
|
|
|
|
|
|
for(i = 0; i < max; ++i)
|
|
|
|
{
|
|
|
|
|
|
|
|
y = image->comps[0].data[i]; cb = image->comps[1].data[i]; cr = image->comps[2].data[i];
|
|
|
|
|
|
|
|
if( !sign1) cb -= flip_value;
|
|
|
|
if( !sign2) cr -= flip_value;
|
|
|
|
|
|
|
|
val = (int)
|
|
|
|
((float)y - (float)0.0000368 * (float)cb
|
|
|
|
+ (float)1.40199 * (float)cr + (float)0.5);
|
|
|
|
|
|
|
|
if(val > max_value) val = max_value; else if(val < 0) val = 0;
|
|
|
|
image->comps[0].data[i] = val;
|
|
|
|
|
|
|
|
val = (int)
|
|
|
|
((float)1.0003 * (float)y - (float)0.344125 * (float)cb
|
|
|
|
- (float)0.7141128 * (float)cr + (float)0.5);
|
|
|
|
|
|
|
|
if(val > max_value) val = max_value; else if(val < 0) val = 0;
|
|
|
|
image->comps[1].data[i] = val;
|
|
|
|
|
|
|
|
val = (int)
|
|
|
|
((float)0.999823 * (float)y + (float)1.77204 * (float)cb
|
|
|
|
- (float)0.000008 *(float)cr + (float)0.5);
|
|
|
|
|
|
|
|
if(val > max_value) val = max_value; else if(val < 0) val = 0;
|
|
|
|
image->comps[2].data[i] = val;
|
|
|
|
}
|
2015-07-31 01:31:12 +02:00
|
|
|
image->color_space = OPJ_CLRSPC_SRGB;
|
2010-12-08 12:06:41 +01:00
|
|
|
|
2015-10-12 21:25:53 +02:00
|
|
|
}/* color_esycc_to_rgb() */
|