48#define OPJ_CLRSPC_GRAY CLRSPC_GRAY
49#define OPJ_CLRSPC_SRGB CLRSPC_SRGB
67 int *out_r,
int *out_g,
int *out_b)
71 cb -= offset; cr -= offset;
72 r = y + qFloor (1.402 *
double (cr));
73 if(r < 0) r = 0;
else if(r > upb) r = upb; *out_r = r;
75 g = y - qFloor (0.344 *
double (cb) + 0.714 *
double (cr));
76 if(g < 0) g = 0;
else if(g > upb) g = upb; *out_g = g;
78 b = y + qFloor (1.772 *
double (cb));
79 if(b < 0) b = 0;
else if(b > upb) b = upb; *out_b = b;
84 int *d0, *d1, *d2, *r, *g, *b;
85 const int *y, *cb, *cr;
86 int maxw, maxh, max, i, offset, upb;
88 i = qFloor (img->comps[0].prec);
89 offset = 1<<(i - 1); upb = (1<<i)-1;
91 maxw = qFloor (img->comps[0].w);
92 maxh = qFloor (img->comps[0].h);
95 y = img->comps[0].data;
96 cb = img->comps[1].data;
97 cr = img->comps[2].data;
99 d0 = r =
static_cast<int*
> (malloc(
sizeof(
int) *
static_cast<size_t> (max)));
100 d1 = g =
static_cast<int*
> (malloc(
sizeof(
int) *
static_cast<size_t> (max)));
101 d2 = b =
static_cast<int*
> (malloc(
sizeof(
int) *
static_cast<size_t> (max)));
103 for(i = 0; i < max; ++i)
107 ++y; ++cb; ++cr; ++r; ++g; ++b;
109 free(img->comps[0].data); img->comps[0].data = d0;
110 free(img->comps[1].data); img->comps[1].data = d1;
111 free(img->comps[2].data); img->comps[2].data = d2;
117 int *d0, *d1, *d2, *r, *g, *b;
118 const int *y, *cb, *cr;
119 int maxw, maxh, max, offset, upb;
122 i = qFloor (img->comps[0].prec);
123 offset = 1<<(i - 1); upb = (1<<i)-1;
125 maxw = qFloor (img->comps[0].w);
126 maxh = qFloor (img->comps[0].h);
129 y = img->comps[0].data;
130 cb = img->comps[1].data;
131 cr = img->comps[2].data;
133 d0 = r =
static_cast<int*
> (malloc(
sizeof(
int) *
static_cast<size_t> (max)));
134 d1 = g =
static_cast<int*
> (malloc(
sizeof(
int) *
static_cast<size_t> (max)));
135 d2 = b =
static_cast<int*
> (malloc(
sizeof(
int) *
static_cast<size_t> (max)));
137 for(i=0; i < maxh; ++i)
139 for(j=0; j < maxw; j += 2)
147 ++y; ++r; ++g; ++b; ++cb; ++cr;
150 free(img->comps[0].data); img->comps[0].data = d0;
151 free(img->comps[1].data); img->comps[1].data = d1;
152 free(img->comps[2].data); img->comps[2].data = d2;
154#if defined(USE_JPWL) || defined(USE_MJ2)
155 img->comps[1].w = maxw; img->comps[1].h = maxh;
156 img->comps[2].w = maxw; img->comps[2].h = maxh;
158 img->comps[1].w =
static_cast<OPJ_UINT32
> (maxw);
159 img->comps[1].h =
static_cast<OPJ_UINT32
> (maxh);
160 img->comps[2].w =
static_cast<OPJ_UINT32
> (maxw);
161 img->comps[2].h =
static_cast<OPJ_UINT32
> (maxh);
163 img->comps[1].dx = img->comps[0].dx;
164 img->comps[2].dx = img->comps[0].dx;
165 img->comps[1].dy = img->comps[0].dy;
166 img->comps[2].dy = img->comps[0].dy;
172 int *d0, *d1, *d2, *r, *g, *b, *nr, *ng, *nb;
173 const int *y, *cb, *cr, *ny;
174 int maxw, maxh, max, offset, upb;
177 i = qFloor (img->comps[0].prec);
178 offset = 1<<(i - 1); upb = (1<<i)-1;
180 maxw = qFloor (img->comps[0].w);
181 maxh = qFloor (img->comps[0].h);
184 y = img->comps[0].data;
185 cb = img->comps[1].data;
186 cr = img->comps[2].data;
188 d0 = r =
static_cast<int*
> (malloc(
sizeof(
int) *
static_cast<size_t> (max)));
189 d1 = g =
static_cast<int*
> (malloc(
sizeof(
int) *
static_cast<size_t> (max)));
190 d2 = b =
static_cast<int*
> (malloc(
sizeof(
int) *
static_cast<size_t> (max)));
192 for(i=0; i < maxh; i += 2)
195 nr = r + maxw; ng = g + maxw; nb = b + maxw;
197 for(j=0; j < maxw; j += 2)
207 sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
209 ++ny; ++nr; ++ng; ++nb;
211 sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
213 ++ny; ++nr; ++ng; ++nb; ++cb; ++cr;
215 y += maxw; r += maxw; g += maxw; b += maxw;
217 free(img->comps[0].data); img->comps[0].data = d0;
218 free(img->comps[1].data); img->comps[1].data = d1;
219 free(img->comps[2].data); img->comps[2].data = d2;
221#if defined(USE_JPWL) || defined(USE_MJ2)
222 img->comps[1].w = maxw; img->comps[1].h = maxh;
223 img->comps[2].w = maxw; img->comps[2].h = maxh;
225 img->comps[1].w =
static_cast<OPJ_UINT32
> (maxw);
226 img->comps[1].h =
static_cast<OPJ_UINT32
> (maxh);
227 img->comps[2].w =
static_cast<OPJ_UINT32
> (maxw);
228 img->comps[2].h =
static_cast<OPJ_UINT32
> (maxh);
230 img->comps[1].dx = img->comps[0].dx;
231 img->comps[2].dx = img->comps[0].dx;
232 img->comps[1].dy = img->comps[0].dy;
233 img->comps[2].dy = img->comps[0].dy;
239 if(img->numcomps < 3)
241 img->color_space = OPJ_CLRSPC_GRAY;
245 if((img->comps[0].dx == 1)
246 && (img->comps[1].dx == 2)
247 && (img->comps[2].dx == 2)
248 && (img->comps[0].dy == 1)
249 && (img->comps[1].dy == 2)
250 && (img->comps[2].dy == 2))
255 if((img->comps[0].dx == 1)
256 && (img->comps[1].dx == 2)
257 && (img->comps[2].dx == 2)
258 && (img->comps[0].dy == 1)
259 && (img->comps[1].dy == 1)
260 && (img->comps[2].dy == 1))
265 if((img->comps[0].dx == 1)
266 && (img->comps[1].dx == 1)
267 && (img->comps[2].dx == 1)
268 && (img->comps[0].dy == 1)
269 && (img->comps[1].dy == 1)
270 && (img->comps[2].dy == 1))
276 fprintf(stderr,
"%s:%d:color_sycc_to_rgb\n\tCAN NOT CONVERT\n",
282 img->color_space = OPJ_CLRSPC_SRGB;
285#if defined(OPJ_HAVE_LIBLCMS2) || defined(OPJ_HAVE_LIBLCMS1)
286#ifdef OPJ_HAVE_LIBLCMS1
288#define cmsSigXYZData icSigXYZData
289#define cmsSigLabData icSigLabData
290#define cmsSigCmykData icSigCmykData
291#define cmsSigYCbCrData icSigYCbCrData
292#define cmsSigLuvData icSigLuvData
293#define cmsSigGrayData icSigGrayData
294#define cmsSigRgbData icSigRgbData
295#define cmsUInt32Number DWORD
297#define cmsColorSpaceSignature icColorSpaceSignature
298#define cmsGetHeaderRenderingIntent cmsTakeRenderingIntent
304 cmsHPROFILE in_prof, out_prof;
305 cmsHTRANSFORM transform;
306 cmsColorSpaceSignature in_space, out_space;
307 cmsUInt32Number intent, in_type, out_type, nr_samples;
309 int prec, i, max, max_w, max_h;
310 OPJ_COLOR_SPACE oldspace;
313 cmsOpenProfileFromMem(image->icc_profile_buf, image->icc_profile_len);
315 if(in_prof ==
nullptr)
return;
317 in_space = cmsGetPCS(in_prof);
318 out_space = cmsGetColorSpace(in_prof);
319 intent = cmsGetHeaderRenderingIntent(in_prof);
322 max_w = qFloor (image->comps[0].w);
323 max_h = qFloor (image->comps[0].h);
324 prec = qFloor (image->comps[0].prec);
325 oldspace = image->color_space;
327 if(out_space == cmsSigRgbData)
331 in_type = TYPE_RGB_8;
332 out_type = TYPE_RGB_8;
336 in_type = TYPE_RGB_16;
337 out_type = TYPE_RGB_16;
339 out_prof = cmsCreate_sRGBProfile();
340 image->color_space = OPJ_CLRSPC_SRGB;
343 if(out_space == cmsSigGrayData)
345 in_type = TYPE_GRAY_8;
346 out_type = TYPE_RGB_8;
347 out_prof = cmsCreate_sRGBProfile();
348 image->color_space = OPJ_CLRSPC_SRGB;
351 if(out_space == cmsSigYCbCrData)
353 in_type = TYPE_YCbCr_16;
354 out_type = TYPE_RGB_16;
355 out_prof = cmsCreate_sRGBProfile();
356 image->color_space = OPJ_CLRSPC_SRGB;
366 transform = cmsCreateTransform(in_prof, in_type,
367 out_prof, out_type, intent, 0);
369#ifdef OPJ_HAVE_LIBLCMS2
371 cmsCloseProfile(in_prof);
372 cmsCloseProfile(out_prof);
375 if(transform ==
nullptr)
377 image->color_space = oldspace;
378#ifdef OPJ_HAVE_LIBLCMS1
379 cmsCloseProfile(in_prof);
380 cmsCloseProfile(out_prof);
385 if(image->numcomps > 2)
389 unsigned char *inbuf, *outbuf, *in, *out;
391 nr_samples = (cmsUInt32Number)max * 3 * (cmsUInt32Number)
sizeof(
unsigned char);
392 in = inbuf = (
unsigned char*)malloc(nr_samples);
393 out = outbuf = (
unsigned char*)malloc(nr_samples);
395 r = image->comps[0].data;
396 g = image->comps[1].data;
397 b = image->comps[2].data;
399 for(i = 0; i < max; ++i)
401 *in++ = (
unsigned char)*r++;
402 *in++ = (
unsigned char)*g++;
403 *in++ = (
unsigned char)*b++;
406 cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
408 r = image->comps[0].data;
409 g = image->comps[1].data;
410 b = image->comps[2].data;
412 for(i = 0; i < max; ++i)
414 *r++ = qFloor (*out++);
415 *g++ = qFloor (*out++);
416 *b++ = qFloor (*out++);
418 free(inbuf); free(outbuf);
422 unsigned short *inbuf, *outbuf, *in, *out;
424 nr_samples = (cmsUInt32Number)max * 3 * (cmsUInt32Number)
sizeof(
unsigned short);
425 in = inbuf = (
unsigned short*)malloc(nr_samples);
426 out = outbuf = (
unsigned short*)malloc(nr_samples);
428 r = image->comps[0].data;
429 g = image->comps[1].data;
430 b = image->comps[2].data;
432 for(i = 0; i < max; ++i)
434 *in++ = (
unsigned short)*r++;
435 *in++ = (
unsigned short)*g++;
436 *in++ = (
unsigned short)*b++;
439 cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
441 r = image->comps[0].data;
442 g = image->comps[1].data;
443 b = image->comps[2].data;
445 for(i = 0; i < max; ++i)
447 *r++ = qFloor (*out++);
448 *g++ = qFloor (*out++);
449 *b++ = qFloor (*out++);
451 free(inbuf); free(outbuf);
456 unsigned char *in, *inbuf, *out, *outbuf;
458 nr_samples = (cmsUInt32Number)max * 3 *
sizeof(
unsigned char);
459 in = inbuf = (
unsigned char*)malloc(nr_samples);
460 out = outbuf = (
unsigned char*)malloc(nr_samples);
462 image->comps = (opj_image_comp_t*)
463 realloc(image->comps, (image->numcomps+2)*
sizeof(opj_image_comp_t));
465 if(image->numcomps == 2)
466 image->comps[3] = image->comps[1];
468 image->comps[1] = image->comps[0];
469 image->comps[2] = image->comps[0];
471 image->comps[1].data = (
int*)calloc((
size_t)max,
sizeof(
int));
472 image->comps[2].data = (
int*)calloc((
size_t)max,
sizeof(
int));
474 image->numcomps += 2;
476 r = image->comps[0].data;
478 for(i = 0; i < max; ++i)
480 *in++ = (
unsigned char)*r++;
482 cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
484 r = image->comps[0].data;
485 g = image->comps[1].data;
486 b = image->comps[2].data;
488 for(i = 0; i < max; ++i)
490 *r++ = qFloor (*out++);
491 *g++ = qFloor (*out++);
492 *b++ = qFloor (*out++);
494 free(inbuf); free(outbuf);
498 cmsDeleteTransform(transform);
500#ifdef OPJ_HAVE_LIBLCMS1
501 cmsCloseProfile(in_prof);
502 cmsCloseProfile(out_prof);
void sycc_to_rgb(int offset, int upb, int y, int cb, int cr, int *out_r, int *out_g, int *out_b)
void color_sycc_to_rgb(opj_image_t *img)
void sycc420_to_rgb(opj_image_t *img)
void sycc444_to_rgb(opj_image_t *img)
void sycc422_to_rgb(opj_image_t *img)
void color_apply_icc_profile(opj_image_t *image)