44 #include "Jpeg2000Color.h" 47 #define OPJ_CLRSPC_GRAY CLRSPC_GRAY 48 #define OPJ_CLRSPC_SRGB CLRSPC_SRGB 65 void sycc_to_rgb(
int offset,
int upb,
int y,
int cb,
int cr,
66 int *out_r,
int *out_g,
int *out_b)
70 cb -= offset; cr -= offset;
71 r = y + (int)(1.402 * (
float)cr);
72 if(r < 0) r = 0;
else if(r > upb) r = upb; *out_r = r;
74 g = y - (int)(0.344 * (
float)cb + 0.714 * (float)cr);
75 if(g < 0) g = 0;
else if(g > upb) g = upb; *out_g = g;
77 b = y + (int)(1.772 * (
float)cb);
78 if(b < 0) b = 0;
else if(b > upb) b = upb; *out_b = b;
81 void sycc444_to_rgb(opj_image_t *img)
83 int *d0, *d1, *d2, *r, *g, *b;
84 const int *y, *cb, *cr;
85 int maxw, maxh, max, i, offset, upb;
87 i = (int)img->comps[0].prec;
88 offset = 1<<(i - 1); upb = (1<<i)-1;
90 maxw = (int)img->comps[0].w; maxh = (
int)img->comps[0].h;
93 y = img->comps[0].data;
94 cb = img->comps[1].data;
95 cr = img->comps[2].data;
97 d0 = r = (
int*)malloc(
sizeof(
int) * (size_t)max);
98 d1 = g = (
int*)malloc(
sizeof(
int) * (size_t)max);
99 d2 = b = (
int*)malloc(
sizeof(
int) * (size_t)max);
101 for(i = 0; i < max; ++i)
103 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
105 ++y; ++cb; ++cr; ++r; ++g; ++b;
107 free(img->comps[0].data); img->comps[0].data = d0;
108 free(img->comps[1].data); img->comps[1].data = d1;
109 free(img->comps[2].data); img->comps[2].data = d2;
113 void sycc422_to_rgb(opj_image_t *img)
115 int *d0, *d1, *d2, *r, *g, *b;
116 const int *y, *cb, *cr;
117 int maxw, maxh, max, offset, upb;
120 i = (int)img->comps[0].prec;
121 offset = 1<<(i - 1); upb = (1<<i)-1;
123 maxw = (int)img->comps[0].w; maxh = (
int)img->comps[0].h;
126 y = img->comps[0].data;
127 cb = img->comps[1].data;
128 cr = img->comps[2].data;
130 d0 = r = (
int*)malloc(
sizeof(
int) * (size_t)max);
131 d1 = g = (
int*)malloc(
sizeof(
int) * (size_t)max);
132 d2 = b = (
int*)malloc(
sizeof(
int) * (size_t)max);
134 for(i=0; i < maxh; ++i)
136 for(j=0; j < maxw; j += 2)
138 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
142 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
144 ++y; ++r; ++g; ++b; ++cb; ++cr;
147 free(img->comps[0].data); img->comps[0].data = d0;
148 free(img->comps[1].data); img->comps[1].data = d1;
149 free(img->comps[2].data); img->comps[2].data = d2;
151 #if defined(USE_JPWL) || defined(USE_MJ2) 152 img->comps[1].w = maxw; img->comps[1].h = maxh;
153 img->comps[2].w = maxw; img->comps[2].h = maxh;
155 img->comps[1].w = (OPJ_UINT32)maxw; img->comps[1].h = (OPJ_UINT32)maxh;
156 img->comps[2].w = (OPJ_UINT32)maxw; img->comps[2].h = (OPJ_UINT32)maxh;
158 img->comps[1].dx = img->comps[0].dx;
159 img->comps[2].dx = img->comps[0].dx;
160 img->comps[1].dy = img->comps[0].dy;
161 img->comps[2].dy = img->comps[0].dy;
165 void sycc420_to_rgb(opj_image_t *img)
167 int *d0, *d1, *d2, *r, *g, *b, *nr, *ng, *nb;
168 const int *y, *cb, *cr, *ny;
169 int maxw, maxh, max, offset, upb;
172 i = (int)img->comps[0].prec;
173 offset = 1<<(i - 1); upb = (1<<i)-1;
175 maxw = (int)img->comps[0].w; maxh = (
int)img->comps[0].h;
178 y = img->comps[0].data;
179 cb = img->comps[1].data;
180 cr = img->comps[2].data;
182 d0 = r = (
int*)malloc(
sizeof(
int) * (size_t)max);
183 d1 = g = (
int*)malloc(
sizeof(
int) * (size_t)max);
184 d2 = b = (
int*)malloc(
sizeof(
int) * (size_t)max);
186 for(i=0; i < maxh; i += 2)
189 nr = r + maxw; ng = g + maxw; nb = b + maxw;
191 for(j=0; j < maxw; j += 2)
193 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
197 sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
201 sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
203 ++ny; ++nr; ++ng; ++nb;
205 sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
207 ++ny; ++nr; ++ng; ++nb; ++cb; ++cr;
209 y += maxw; r += maxw; g += maxw; b += maxw;
211 free(img->comps[0].data); img->comps[0].data = d0;
212 free(img->comps[1].data); img->comps[1].data = d1;
213 free(img->comps[2].data); img->comps[2].data = d2;
215 #if defined(USE_JPWL) || defined(USE_MJ2) 216 img->comps[1].w = maxw; img->comps[1].h = maxh;
217 img->comps[2].w = maxw; img->comps[2].h = maxh;
219 img->comps[1].w = (OPJ_UINT32)maxw; img->comps[1].h = (OPJ_UINT32)maxh;
220 img->comps[2].w = (OPJ_UINT32)maxw; img->comps[2].h = (OPJ_UINT32)maxh;
222 img->comps[1].dx = img->comps[0].dx;
223 img->comps[2].dx = img->comps[0].dx;
224 img->comps[1].dy = img->comps[0].dy;
225 img->comps[2].dy = img->comps[0].dy;
229 void color_sycc_to_rgb(opj_image_t *img)
231 if(img->numcomps < 3)
233 img->color_space = OPJ_CLRSPC_GRAY;
237 if((img->comps[0].dx == 1)
238 && (img->comps[1].dx == 2)
239 && (img->comps[2].dx == 2)
240 && (img->comps[0].dy == 1)
241 && (img->comps[1].dy == 2)
242 && (img->comps[2].dy == 2))
247 if((img->comps[0].dx == 1)
248 && (img->comps[1].dx == 2)
249 && (img->comps[2].dx == 2)
250 && (img->comps[0].dy == 1)
251 && (img->comps[1].dy == 1)
252 && (img->comps[2].dy == 1))
257 if((img->comps[0].dx == 1)
258 && (img->comps[1].dx == 1)
259 && (img->comps[2].dx == 1)
260 && (img->comps[0].dy == 1)
261 && (img->comps[1].dy == 1)
262 && (img->comps[2].dy == 1))
268 fprintf(stderr,
"%s:%d:color_sycc_to_rgb\n\tCAN NOT CONVERT\n",
274 img->color_space = OPJ_CLRSPC_SRGB;
277 #if defined(OPJ_HAVE_LIBLCMS2) || defined(OPJ_HAVE_LIBLCMS1) 278 #ifdef OPJ_HAVE_LIBLCMS1 280 #define cmsSigXYZData icSigXYZData 281 #define cmsSigLabData icSigLabData 282 #define cmsSigCmykData icSigCmykData 283 #define cmsSigYCbCrData icSigYCbCrData 284 #define cmsSigLuvData icSigLuvData 285 #define cmsSigGrayData icSigGrayData 286 #define cmsSigRgbData icSigRgbData 287 #define cmsUInt32Number DWORD 289 #define cmsColorSpaceSignature icColorSpaceSignature 290 #define cmsGetHeaderRenderingIntent cmsTakeRenderingIntent 294 void color_apply_icc_profile(opj_image_t *image)
296 cmsHPROFILE in_prof, out_prof;
297 cmsHTRANSFORM transform;
298 cmsColorSpaceSignature in_space, out_space;
299 cmsUInt32Number intent, in_type, out_type, nr_samples;
301 int prec, i, max, max_w, max_h;
302 OPJ_COLOR_SPACE oldspace;
305 cmsOpenProfileFromMem(image->icc_profile_buf, image->icc_profile_len);
307 if(in_prof == NULL)
return;
309 in_space = cmsGetPCS(in_prof);
310 out_space = cmsGetColorSpace(in_prof);
311 intent = cmsGetHeaderRenderingIntent(in_prof);
314 max_w = (int)image->comps[0].w;
315 max_h = (
int)image->comps[0].h;
316 prec = (int)image->comps[0].prec;
317 oldspace = image->color_space;
319 if(out_space == cmsSigRgbData)
323 in_type = TYPE_RGB_8;
324 out_type = TYPE_RGB_8;
328 in_type = TYPE_RGB_16;
329 out_type = TYPE_RGB_16;
331 out_prof = cmsCreate_sRGBProfile();
332 image->color_space = OPJ_CLRSPC_SRGB;
335 if(out_space == cmsSigGrayData)
337 in_type = TYPE_GRAY_8;
338 out_type = TYPE_RGB_8;
339 out_prof = cmsCreate_sRGBProfile();
340 image->color_space = OPJ_CLRSPC_SRGB;
343 if(out_space == cmsSigYCbCrData)
345 in_type = TYPE_YCbCr_16;
346 out_type = TYPE_RGB_16;
347 out_prof = cmsCreate_sRGBProfile();
348 image->color_space = OPJ_CLRSPC_SRGB;
358 transform = cmsCreateTransform(in_prof, in_type,
359 out_prof, out_type, intent, 0);
361 #ifdef OPJ_HAVE_LIBLCMS2 363 cmsCloseProfile(in_prof);
364 cmsCloseProfile(out_prof);
367 if(transform == NULL)
369 image->color_space = oldspace;
370 #ifdef OPJ_HAVE_LIBLCMS1 371 cmsCloseProfile(in_prof);
372 cmsCloseProfile(out_prof);
377 if(image->numcomps > 2)
381 unsigned char *inbuf, *outbuf, *in, *out;
383 nr_samples = (cmsUInt32Number)max * 3 * (cmsUInt32Number)
sizeof(
unsigned char);
384 in = inbuf = (
unsigned char*)malloc(nr_samples);
385 out = outbuf = (
unsigned char*)malloc(nr_samples);
387 r = image->comps[0].data;
388 g = image->comps[1].data;
389 b = image->comps[2].data;
391 for(i = 0; i < max; ++i)
393 *in++ = (
unsigned char)*r++;
394 *in++ = (
unsigned char)*g++;
395 *in++ = (
unsigned char)*b++;
398 cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
400 r = image->comps[0].data;
401 g = image->comps[1].data;
402 b = image->comps[2].data;
404 for(i = 0; i < max; ++i)
410 free(inbuf); free(outbuf);
414 unsigned short *inbuf, *outbuf, *in, *out;
416 nr_samples = (cmsUInt32Number)max * 3 * (cmsUInt32Number)
sizeof(
unsigned short);
417 in = inbuf = (
unsigned short*)malloc(nr_samples);
418 out = outbuf = (
unsigned short*)malloc(nr_samples);
420 r = image->comps[0].data;
421 g = image->comps[1].data;
422 b = image->comps[2].data;
424 for(i = 0; i < max; ++i)
426 *in++ = (
unsigned short)*r++;
427 *in++ = (
unsigned short)*g++;
428 *in++ = (
unsigned short)*b++;
431 cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
433 r = image->comps[0].data;
434 g = image->comps[1].data;
435 b = image->comps[2].data;
437 for(i = 0; i < max; ++i)
443 free(inbuf); free(outbuf);
448 unsigned char *in, *inbuf, *out, *outbuf;
450 nr_samples = (cmsUInt32Number)max * 3 *
sizeof(
unsigned char);
451 in = inbuf = (
unsigned char*)malloc(nr_samples);
452 out = outbuf = (
unsigned char*)malloc(nr_samples);
454 image->comps = (opj_image_comp_t*)
455 realloc(image->comps, (image->numcomps+2)*
sizeof(opj_image_comp_t));
457 if(image->numcomps == 2)
458 image->comps[3] = image->comps[1];
460 image->comps[1] = image->comps[0];
461 image->comps[2] = image->comps[0];
463 image->comps[1].data = (
int*)calloc((
size_t)max,
sizeof(int));
464 image->comps[2].data = (
int*)calloc((
size_t)max,
sizeof(int));
466 image->numcomps += 2;
468 r = image->comps[0].data;
470 for(i = 0; i < max; ++i)
472 *in++ = (
unsigned char)*r++;
474 cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
476 r = image->comps[0].data;
477 g = image->comps[1].data;
478 b = image->comps[2].data;
480 for(i = 0; i < max; ++i)
482 *r++ = (int)*out++; *g++ = (int)*out++; *b++ = (int)*out++;
484 free(inbuf); free(outbuf);
488 cmsDeleteTransform(transform);
490 #ifdef OPJ_HAVE_LIBLCMS1 491 cmsCloseProfile(in_prof);
492 cmsCloseProfile(out_prof);