/* * Copyright (c) 2002-2003 Michael David Adams. * All rights reserved. */ /* __START_OF_JASPER_LICENSE__ * * JasPer License Version 2.0 * * Copyright (c) 2001-2006 Michael David Adams * Copyright (c) 1999-2000 Image Power, Inc. * Copyright (c) 1999-2000 The University of British Columbia * * All rights reserved. * * Permission is hereby granted, free of charge, to any person (the * "User") obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without restriction, * including without limitation the rights to use, copy, modify, merge, * publish, distribute, and/or sell copies of the Software, and to permit * persons to whom the Software is furnished to do so, subject to the * following conditions: * * 1. The above copyright notices and this permission notice (which * includes the disclaimer below) shall be included in all copies or * substantial portions of the Software. * * 2. The name of a copyright holder shall not be used to endorse or * promote products derived from the Software without specific prior * written permission. * * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY. * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES, * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH, * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. * * __END_OF_JASPER_LICENSE__ */ /* * Color Management * * $Id$ */ /******************************************************************************\* Includes.\******************************************************************************/ #define JAS_FOR_INTERNAL_USE_ONLY #include "jasper/jas_cm.h"#include "jasper/jas_icc.h"#include "jasper/jas_malloc.h"#include "jasper/jas_math.h"#include "jasper/jas_debug.h" /******************************************************************************\* Function prototypes.\******************************************************************************/ static jas_cmprof_t *jas_cmprof_create(void);static void jas_cmshapmatlut_cleanup(jas_cmshapmatlut_t *);static jas_cmreal_t jas_cmshapmatlut_lookup(const jas_cmshapmatlut_t *lut, jas_cmreal_t x); static void jas_cmpxform_destroy(jas_cmpxform_t *pxform);static jas_cmpxform_t *jas_cmpxform_copy(jas_cmpxform_t *pxform); static void jas_cmshapmat_destroy(jas_cmpxform_t *pxform);static int jas_cmshapmat_apply(const jas_cmpxform_t *pxform, const jas_cmreal_t *in, jas_cmreal_t *out, unsigned cnt); static int jas_cmputint(long **bufptr, int sgnd, unsigned prec, long val);static int jas_cmgetint(const long **bufptr, int sgnd, unsigned prec, long *val);static int jas_cmpxformseq_append(jas_cmpxformseq_t *pxformseq, jas_cmpxformseq_t *othpxformseq);static int jas_cmpxformseq_appendcnvt(jas_cmpxformseq_t *pxformseq, unsigned, unsigned);static int jas_cmpxformseq_resize(jas_cmpxformseq_t *pxformseq, unsigned n); static int mono(const jas_iccprof_t *prof, int op, jas_cmpxformseq_t **pxformseq);static int triclr(const jas_iccprof_t *prof, int op, jas_cmpxformseq_t **retpxformseq); static void jas_cmpxformseq_destroy(jas_cmpxformseq_t *pxformseq);static int jas_cmpxformseq_delete(jas_cmpxformseq_t *pxformseq, unsigned i);static jas_cmpxformseq_t *jas_cmpxformseq_create(void);static jas_cmpxformseq_t *jas_cmpxformseq_copy(jas_cmpxformseq_t *pxformseq);static int jas_cmshapmat_invmat(jas_cmreal_t out[3][4], jas_cmreal_t in[3][4]);static int jas_cmpxformseq_insertpxform(jas_cmpxformseq_t *pxformseq, int i, jas_cmpxform_t *pxform); /******************************************************************************\* Function prototypes.\******************************************************************************/ #define SEQFWD(intent) (intent)#define SEQREV(intent) (4 + (intent))#define SEQSIM(intent) (8 + (intent))#define SEQGAM 12 #define fwdpxformseq(prof, intent) \ (((prof)->pxformseqs[SEQFWD(intent)]) ? \ ((prof)->pxformseqs[SEQFWD(intent)]) : \ ((prof)->pxformseqs[SEQFWD(0)])) #define revpxformseq(prof, intent) \ (((prof)->pxformseqs[SEQREV(intent)]) ? \ ((prof)->pxformseqs[SEQREV(intent)]) : \ ((prof)->pxformseqs[SEQREV(0)])) #define simpxformseq(prof, intent) \ (((prof)->pxformseqs[SEQSIM(intent)]) ? \ ((prof)->pxformseqs[SEQSIM(intent)]) : \ ((prof)->pxformseqs[SEQSIM(0)])) #define gampxformseq(prof) ((prof)->pxformseqs[SEQGAM]) static int icctoclrspc(jas_iccsig_t iccclrspc, jas_clrspc_t* clrspc, int refflag);static jas_cmpxform_t *jas_cmpxform_create0(void);static jas_cmpxform_t *jas_cmpxform_createshapmat(void);static void jas_cmshapmatlut_init(jas_cmshapmatlut_t *lut);static int jas_cmshapmatlut_set(jas_cmshapmatlut_t *lut, const jas_icccurv_t *curv);static jas_cmprof_t *jas_cmprof_createsycc(void); /******************************************************************************\*\******************************************************************************/ static const jas_cmpxformops_t shapmat_ops = { jas_cmshapmat_destroy, jas_cmshapmat_apply, 0}; /******************************************************************************\* Color profile class.\******************************************************************************/ jas_cmprof_t *jas_cmprof_createfromclrspc(jas_clrspc_t clrspc){ JAS_LOGDEBUGF(1, "jas_cmprof_createfromclrspc(%d)\n", clrspc); jas_iccprof_t *iccprof; jas_cmprof_t *prof; iccprof = 0; switch (clrspc) { case JAS_CLRSPC_SYCBCR: if (!(prof = jas_cmprof_createsycc())) { goto error; } break; default: if (!(iccprof = jas_iccprof_createfromclrspc(clrspc))) { goto error; } if (!(prof = jas_cmprof_createfromiccprof(iccprof))) { goto error; } jas_iccprof_destroy(iccprof); if (!jas_clrspc_isgeneric(clrspc)) { prof->clrspc = clrspc; } break; } return prof;error: if (iccprof) { jas_iccprof_destroy(iccprof); } return 0;} static jas_cmprof_t *jas_cmprof_createsycc(){ JAS_LOGDEBUGF(1, "jas_cmprof_createsycc()\n"); jas_cmprof_t *prof; jas_cmpxform_t *fwdpxform = 0; jas_cmpxform_t *revpxform = 0; jas_cmshapmat_t *fwdshapmat; jas_cmshapmat_t *revshapmat; if (!(prof = jas_cmprof_createfromclrspc(JAS_CLRSPC_SRGB))) { goto error; } prof->clrspc = JAS_CLRSPC_SYCBCR; assert(prof->numchans == 3 && prof->numrefchans == 3); assert(prof->refclrspc == JAS_CLRSPC_CIEXYZ); if (!(fwdpxform = jas_cmpxform_createshapmat())) { goto error; } fwdpxform->numinchans = 3; fwdpxform->numoutchans = 3; fwdshapmat = &fwdpxform->data.shapmat; fwdshapmat->mono = 0; fwdshapmat->order = 0; fwdshapmat->useluts = 0; fwdshapmat->usemat = 1; fwdshapmat->mat[0][0] = 1.0; fwdshapmat->mat[0][1] = 0.0; fwdshapmat->mat[0][2] = 1.402; fwdshapmat->mat[1][0] = 1.0; fwdshapmat->mat[1][1] = -0.34413; fwdshapmat->mat[1][2] = -0.71414; fwdshapmat->mat[2][0] = 1.0; fwdshapmat->mat[2][1] = 1.772; fwdshapmat->mat[2][2] = 0.0; fwdshapmat->mat[0][3] = -0.5 * (1.402); fwdshapmat->mat[1][3] = -0.5 * (-0.34413 - 0.71414); fwdshapmat->mat[2][3] = -0.5 * (1.772); if (!(revpxform = jas_cmpxform_createshapmat())) { goto error; } revpxform->numinchans = 3; revpxform->numoutchans = 3; revshapmat = &revpxform->data.shapmat; revshapmat->mono = 0; revshapmat->order = 1; revshapmat->useluts = 0; revshapmat->usemat = 1; if (jas_cmshapmat_invmat(revshapmat->mat, fwdshapmat->mat)) { goto error; } for (unsigned i = 0; i < JAS_CMXFORM_NUMINTENTS; ++i) { unsigned j = SEQFWD(i); if (prof->pxformseqs[j]) { if (jas_cmpxformseq_insertpxform(prof->pxformseqs[j], 0, fwdpxform)) { goto error; } } j = SEQREV(i); if (prof->pxformseqs[j]) { if (jas_cmpxformseq_insertpxform(prof->pxformseqs[j], -1, revpxform)) { goto error; } } } jas_cmpxform_destroy(fwdpxform); jas_cmpxform_destroy(revpxform); return prof;error: if (fwdpxform) { jas_cmpxform_destroy(fwdpxform); } if (revpxform) { jas_cmpxform_destroy(revpxform); } if (prof) { jas_cmprof_destroy(prof); } return 0;} jas_cmprof_t *jas_cmprof_createfromiccprof(const jas_iccprof_t *iccprof){ JAS_LOGDEBUGF(1, "jas_cmprof_createfromiccprof(%p)\n", iccprof); jas_cmprof_t *prof; jas_icchdr_t icchdr; jas_cmpxformseq_t *fwdpxformseq; jas_cmpxformseq_t *revpxformseq; fwdpxformseq = 0; revpxformseq = 0; if (!(prof = jas_cmprof_create())) { jas_logerrorf("error: cannot create CM profile\n"); goto error; } jas_iccprof_gethdr(iccprof, &icchdr); if (!(prof->iccprof = jas_iccprof_copy(iccprof))) { jas_logerrorf("error: cannot copy ICC profile\n"); goto error; } if (icctoclrspc(icchdr.colorspc, &prof->clrspc, 0)) { jas_logerrorf("error: unknown color profile\n"); goto error; } if (icctoclrspc(icchdr.refcolorspc, &prof->refclrspc, 1)) { jas_logerrorf("error: unknown reference color profile\n"); goto error; } prof->numchans = jas_clrspc_numchans(prof->clrspc); prof->numrefchans = jas_clrspc_numchans(prof->refclrspc); if (prof->numchans == 1) { if (mono(prof->iccprof, 0, &fwdpxformseq)) { jas_logerrorf("error: mono failed 1\n"); goto error; } if (mono(prof->iccprof, 1, &revpxformseq)) { jas_logerrorf("error: mono failed 2\n"); goto error; } } else if (prof->numchans == 3) { if (triclr(prof->iccprof, 0, &fwdpxformseq)) { jas_logerrorf("error: triclr failed 1\n"); goto error; } if (triclr(prof->iccprof, 1, &revpxformseq)) { jas_logerrorf("error: triclr failed 2\n"); goto error; } } prof->pxformseqs[SEQFWD(0)] = fwdpxformseq; prof->pxformseqs[SEQREV(0)] = revpxformseq; #if 0 if (prof->numchans > 1) { lut(prof->iccprof, 0, PER, &pxformseq); pxformseqs_set(prof, SEQFWD(PER), pxformseq); lut(prof->iccprof, 1, PER, &pxformseq); pxformseqs_set(prof, SEQREV(PER), pxformseq); lut(prof->iccprof, 0, CLR, &pxformseq); pxformseqs_set(prof, SEQREV(CLR), pxformseq); lut(prof->iccprof, 1, CLR, &pxformseq); pxformseqs_set(prof, SEQREV(CLR), pxformseq); lut(prof->iccprof, 0, SAT, &pxformseq); pxformseqs_set(prof, SEQREV(SAT), pxformseq); lut(prof->iccprof, 1, SAT, &pxformseq); pxformseqs_set(prof, SEQREV(SAT), pxformseq); }#endif return prof; error: if (fwdpxformseq) { jas_cmpxformseq_destroy(fwdpxformseq); } if (revpxformseq) { jas_cmpxformseq_destroy(revpxformseq); } if (prof) { jas_cmprof_destroy(prof); } return 0;} static jas_cmprof_t *jas_cmprof_create(){ jas_cmprof_t *prof; if (!(prof = jas_malloc(sizeof(jas_cmprof_t)))) { return 0; } memset(prof, 0, sizeof(jas_cmprof_t)); prof->iccprof = 0; for (unsigned i = 0; i < JAS_CMPROF_NUMPXFORMSEQS; ++i) { prof->pxformseqs[i] = 0; } return prof;} void jas_cmprof_destroy(jas_cmprof_t *prof){ for (unsigned i = 0; i < JAS_CMPROF_NUMPXFORMSEQS; ++i) { if (prof->pxformseqs[i]) { jas_cmpxformseq_destroy(prof->pxformseqs[i]); prof->pxformseqs[i] = 0; } } if (prof->iccprof) { jas_iccprof_destroy(prof->iccprof); } jas_free(prof);} jas_cmprof_t *jas_cmprof_copy(const jas_cmprof_t *prof){ jas_cmprof_t *newprof; if (!(newprof = jas_cmprof_create())) { goto error; } newprof->clrspc = prof->clrspc; newprof->numchans = prof->numchans; newprof->refclrspc = prof->refclrspc; newprof->numrefchans = prof->numrefchans; newprof->iccprof = jas_iccprof_copy(prof->iccprof); for (unsigned i = 0; i < JAS_CMPROF_NUMPXFORMSEQS; ++i) { if (prof->pxformseqs[i]) { if (!(newprof->pxformseqs[i] = jas_cmpxformseq_copy(prof->pxformseqs[i]))) goto error; } } return newprof;error: if (newprof) { jas_cmprof_destroy(newprof); } return 0;} /******************************************************************************\* Transform class.\******************************************************************************/ jas_cmxform_t *jas_cmxform_create(const jas_cmprof_t *inprof, const jas_cmprof_t *outprof, const jas_cmprof_t *prfprof, jas_cmxform_op_t op, jas_cmxform_intent_t intent, jas_cmxform_optm_t optimize){ jas_cmxform_t *xform; jas_cmpxformseq_t *inpxformseq; jas_cmpxformseq_t *outpxformseq; jas_cmpxformseq_t *altoutpxformseq; jas_cmpxformseq_t *prfpxformseq; JAS_UNUSED(optimize); const jas_cmxform_intent_t prfintent = intent; if (!(xform = jas_malloc(sizeof(jas_cmxform_t)))) { goto error; } if (!(xform->pxformseq = jas_cmpxformseq_create())) { goto error; } switch (op) { case JAS_CMXFORM_OP_FWD: inpxformseq = fwdpxformseq(inprof, intent); outpxformseq = revpxformseq(outprof, intent); if (!inpxformseq || !outpxformseq) { goto error; } if (jas_cmpxformseq_append(xform->pxformseq, inpxformseq) || jas_cmpxformseq_appendcnvt(xform->pxformseq, inprof->refclrspc, outprof->refclrspc) || jas_cmpxformseq_append(xform->pxformseq, outpxformseq)) { goto error; } xform->numinchans = jas_clrspc_numchans(inprof->clrspc); xform->numoutchans = jas_clrspc_numchans(outprof->clrspc); break; case JAS_CMXFORM_OP_REV: outpxformseq = fwdpxformseq(outprof, intent); inpxformseq = revpxformseq(inprof, intent); if (!outpxformseq || !inpxformseq) { goto error; } if (jas_cmpxformseq_append(xform->pxformseq, outpxformseq) || jas_cmpxformseq_appendcnvt(xform->pxformseq, outprof->refclrspc, inprof->refclrspc) || jas_cmpxformseq_append(xform->pxformseq, inpxformseq)) { goto error; } xform->numinchans = jas_clrspc_numchans(outprof->clrspc); xform->numoutchans = jas_clrspc_numchans(inprof->clrspc); break; case JAS_CMXFORM_OP_PROOF: assert(prfprof); inpxformseq = fwdpxformseq(inprof, intent); prfpxformseq = fwdpxformseq(prfprof, prfintent); if (!inpxformseq || !prfpxformseq) { goto error; } outpxformseq = simpxformseq(outprof, intent); altoutpxformseq = 0; if (!outpxformseq) { outpxformseq = revpxformseq(outprof, intent); altoutpxformseq = fwdpxformseq(outprof, intent); if (!outpxformseq || !altoutpxformseq) { goto error; } } if (jas_cmpxformseq_append(xform->pxformseq, inpxformseq) || jas_cmpxformseq_appendcnvt(xform->pxformseq, inprof->refclrspc, outprof->refclrspc)) { goto error; } if (altoutpxformseq) {Lines 1–500 of 1418. Load more
/* * Copyright (c) 2002-2003 Michael David Adams. * All rights reserved. */ /* __START_OF_JASPER_LICENSE__ * * JasPer License Version 2.0 * * Copyright (c) 2001-2006 Michael David Adams * Copyright (c) 1999-2000 Image Power, Inc. * Copyright (c) 1999-2000 The University of British Columbia * * All rights reserved. * * Permission is hereby granted, free of charge, to any person (the * "User") obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without restriction, * including without limitation the rights to use, copy, modify, merge, * publish, distribute, and/or sell copies of the Software, and to permit * persons to whom the Software is furnished to do so, subject to the * following conditions: * * 1. The above copyright notices and this permission notice (which * includes the disclaimer below) shall be included in all copies or * substantial portions of the Software. * * 2. The name of a copyright holder shall not be used to endorse or * promote products derived from the Software without specific prior * written permission. * * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY. * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES, * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH, * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. * * __END_OF_JASPER_LICENSE__ */ /* * Color Management * * $Id$ */ /******************************************************************************\ * Includes. \******************************************************************************/ #define JAS_FOR_INTERNAL_USE_ONLY #include "jasper/jas_cm.h" #include "jasper/jas_icc.h" #include "jasper/jas_malloc.h" #include "jasper/jas_math.h" #include "jasper/jas_debug.h" /******************************************************************************\ * Function prototypes. \******************************************************************************/ static jas_cmprof_t *jas_cmprof_create(void); static void jas_cmshapmatlut_cleanup(jas_cmshapmatlut_t *); static jas_cmreal_t jas_cmshapmatlut_lookup(const jas_cmshapmatlut_t *lut, jas_cmreal_t x); static void jas_cmpxform_destroy(jas_cmpxform_t *pxform); static jas_cmpxform_t *jas_cmpxform_copy(jas_cmpxform_t *pxform); static void jas_cmshapmat_destroy(jas_cmpxform_t *pxform); static int jas_cmshapmat_apply(const jas_cmpxform_t *pxform, const jas_cmreal_t *in, jas_cmreal_t *out, unsigned cnt); static int jas_cmputint(long **bufptr, int sgnd, unsigned prec, long val); static int jas_cmgetint(const long **bufptr, int sgnd, unsigned prec, long *val); static int jas_cmpxformseq_append(jas_cmpxformseq_t *pxformseq, jas_cmpxformseq_t *othpxformseq); static int jas_cmpxformseq_appendcnvt(jas_cmpxformseq_t *pxformseq, unsigned, unsigned); static int jas_cmpxformseq_resize(jas_cmpxformseq_t *pxformseq, unsigned n); static int mono(const jas_iccprof_t *prof, int op, jas_cmpxformseq_t **pxformseq); static int triclr(const jas_iccprof_t *prof, int op, jas_cmpxformseq_t **retpxformseq); static void jas_cmpxformseq_destroy(jas_cmpxformseq_t *pxformseq); static int jas_cmpxformseq_delete(jas_cmpxformseq_t *pxformseq, unsigned i); static jas_cmpxformseq_t *jas_cmpxformseq_create(void); static jas_cmpxformseq_t *jas_cmpxformseq_copy(jas_cmpxformseq_t *pxformseq); static int jas_cmshapmat_invmat(jas_cmreal_t out[3][4], jas_cmreal_t in[3][4]); static int jas_cmpxformseq_insertpxform(jas_cmpxformseq_t *pxformseq, int i, jas_cmpxform_t *pxform); /******************************************************************************\ * Function prototypes. \******************************************************************************/ #define SEQFWD(intent) (intent) #define SEQREV(intent) (4 + (intent)) #define SEQSIM(intent) (8 + (intent)) #define SEQGAM 12 #define fwdpxformseq(prof, intent) \ (((prof)->pxformseqs[SEQFWD(intent)]) ? \ ((prof)->pxformseqs[SEQFWD(intent)]) : \ ((prof)->pxformseqs[SEQFWD(0)])) #define revpxformseq(prof, intent) \ (((prof)->pxformseqs[SEQREV(intent)]) ? \ ((prof)->pxformseqs[SEQREV(intent)]) : \ ((prof)->pxformseqs[SEQREV(0)])) #define simpxformseq(prof, intent) \ (((prof)->pxformseqs[SEQSIM(intent)]) ? \ ((prof)->pxformseqs[SEQSIM(intent)]) : \ ((prof)->pxformseqs[SEQSIM(0)])) #define gampxformseq(prof) ((prof)->pxformseqs[SEQGAM]) static int icctoclrspc(jas_iccsig_t iccclrspc, jas_clrspc_t* clrspc, int refflag); static jas_cmpxform_t *jas_cmpxform_create0(void); static jas_cmpxform_t *jas_cmpxform_createshapmat(void); static void jas_cmshapmatlut_init(jas_cmshapmatlut_t *lut); static int jas_cmshapmatlut_set(jas_cmshapmatlut_t *lut, const jas_icccurv_t *curv); static jas_cmprof_t *jas_cmprof_createsycc(void); /******************************************************************************\ * \******************************************************************************/ static const jas_cmpxformops_t shapmat_ops = { jas_cmshapmat_destroy, jas_cmshapmat_apply, 0 }; /******************************************************************************\ * Color profile class. \******************************************************************************/ jas_cmprof_t *jas_cmprof_createfromclrspc(jas_clrspc_t clrspc) { JAS_LOGDEBUGF(1, "jas_cmprof_createfromclrspc(%d)\n", clrspc); jas_iccprof_t *iccprof; jas_cmprof_t *prof; iccprof = 0; switch (clrspc) { case JAS_CLRSPC_SYCBCR: if (!(prof = jas_cmprof_createsycc())) { goto error; } break; default: if (!(iccprof = jas_iccprof_createfromclrspc(clrspc))) { goto error; } if (!(prof = jas_cmprof_createfromiccprof(iccprof))) { goto error; } jas_iccprof_destroy(iccprof); if (!jas_clrspc_isgeneric(clrspc)) { prof->clrspc = clrspc; } break; } return prof; error: if (iccprof) { jas_iccprof_destroy(iccprof); } return 0; } static jas_cmprof_t *jas_cmprof_createsycc() { JAS_LOGDEBUGF(1, "jas_cmprof_createsycc()\n"); jas_cmprof_t *prof; jas_cmpxform_t *fwdpxform = 0; jas_cmpxform_t *revpxform = 0; jas_cmshapmat_t *fwdshapmat; jas_cmshapmat_t *revshapmat; if (!(prof = jas_cmprof_createfromclrspc(JAS_CLRSPC_SRGB))) { goto error; } prof->clrspc = JAS_CLRSPC_SYCBCR; assert(prof->numchans == 3 && prof->numrefchans == 3); assert(prof->refclrspc == JAS_CLRSPC_CIEXYZ); if (!(fwdpxform = jas_cmpxform_createshapmat())) { goto error; } fwdpxform->numinchans = 3; fwdpxform->numoutchans = 3; fwdshapmat = &fwdpxform->data.shapmat; fwdshapmat->mono = 0; fwdshapmat->order = 0; fwdshapmat->useluts = 0; fwdshapmat->usemat = 1; fwdshapmat->mat[0][0] = 1.0; fwdshapmat->mat[0][1] = 0.0; fwdshapmat->mat[0][2] = 1.402; fwdshapmat->mat[1][0] = 1.0; fwdshapmat->mat[1][1] = -0.34413; fwdshapmat->mat[1][2] = -0.71414; fwdshapmat->mat[2][0] = 1.0; fwdshapmat->mat[2][1] = 1.772; fwdshapmat->mat[2][2] = 0.0; fwdshapmat->mat[0][3] = -0.5 * (1.402); fwdshapmat->mat[1][3] = -0.5 * (-0.34413 - 0.71414); fwdshapmat->mat[2][3] = -0.5 * (1.772); if (!(revpxform = jas_cmpxform_createshapmat())) { goto error; } revpxform->numinchans = 3; revpxform->numoutchans = 3; revshapmat = &revpxform->data.shapmat; revshapmat->mono = 0; revshapmat->order = 1; revshapmat->useluts = 0; revshapmat->usemat = 1; if (jas_cmshapmat_invmat(revshapmat->mat, fwdshapmat->mat)) { goto error; } for (unsigned i = 0; i < JAS_CMXFORM_NUMINTENTS; ++i) { unsigned j = SEQFWD(i); if (prof->pxformseqs[j]) { if (jas_cmpxformseq_insertpxform(prof->pxformseqs[j], 0, fwdpxform)) { goto error; } } j = SEQREV(i); if (prof->pxformseqs[j]) { if (jas_cmpxformseq_insertpxform(prof->pxformseqs[j], -1, revpxform)) { goto error; } } } jas_cmpxform_destroy(fwdpxform); jas_cmpxform_destroy(revpxform); return prof; error: if (fwdpxform) { jas_cmpxform_destroy(fwdpxform); } if (revpxform) { jas_cmpxform_destroy(revpxform); } if (prof) { jas_cmprof_destroy(prof); } return 0; } jas_cmprof_t *jas_cmprof_createfromiccprof(const jas_iccprof_t *iccprof) { JAS_LOGDEBUGF(1, "jas_cmprof_createfromiccprof(%p)\n", iccprof); jas_cmprof_t *prof; jas_icchdr_t icchdr; jas_cmpxformseq_t *fwdpxformseq; jas_cmpxformseq_t *revpxformseq; fwdpxformseq = 0; revpxformseq = 0; if (!(prof = jas_cmprof_create())) { jas_logerrorf("error: cannot create CM profile\n"); goto error; } jas_iccprof_gethdr(iccprof, &icchdr); if (!(prof->iccprof = jas_iccprof_copy(iccprof))) { jas_logerrorf("error: cannot copy ICC profile\n"); goto error; } if (icctoclrspc(icchdr.colorspc, &prof->clrspc, 0)) { jas_logerrorf("error: unknown color profile\n"); goto error; } if (icctoclrspc(icchdr.refcolorspc, &prof->refclrspc, 1)) { jas_logerrorf("error: unknown reference color profile\n"); goto error; } prof->numchans = jas_clrspc_numchans(prof->clrspc); prof->numrefchans = jas_clrspc_numchans(prof->refclrspc); if (prof->numchans == 1) { if (mono(prof->iccprof, 0, &fwdpxformseq)) { jas_logerrorf("error: mono failed 1\n"); goto error; } if (mono(prof->iccprof, 1, &revpxformseq)) { jas_logerrorf("error: mono failed 2\n"); goto error; } } else if (prof->numchans == 3) { if (triclr(prof->iccprof, 0, &fwdpxformseq)) { jas_logerrorf("error: triclr failed 1\n"); goto error; } if (triclr(prof->iccprof, 1, &revpxformseq)) { jas_logerrorf("error: triclr failed 2\n"); goto error; } } prof->pxformseqs[SEQFWD(0)] = fwdpxformseq; prof->pxformseqs[SEQREV(0)] = revpxformseq; #if 0 if (prof->numchans > 1) { lut(prof->iccprof, 0, PER, &pxformseq); pxformseqs_set(prof, SEQFWD(PER), pxformseq); lut(prof->iccprof, 1, PER, &pxformseq); pxformseqs_set(prof, SEQREV(PER), pxformseq); lut(prof->iccprof, 0, CLR, &pxformseq); pxformseqs_set(prof, SEQREV(CLR), pxformseq); lut(prof->iccprof, 1, CLR, &pxformseq); pxformseqs_set(prof, SEQREV(CLR), pxformseq); lut(prof->iccprof, 0, SAT, &pxformseq); pxformseqs_set(prof, SEQREV(SAT), pxformseq); lut(prof->iccprof, 1, SAT, &pxformseq); pxformseqs_set(prof, SEQREV(SAT), pxformseq); } #endif return prof; error: if (fwdpxformseq) { jas_cmpxformseq_destroy(fwdpxformseq); } if (revpxformseq) { jas_cmpxformseq_destroy(revpxformseq); } if (prof) { jas_cmprof_destroy(prof); } return 0; } static jas_cmprof_t *jas_cmprof_create() { jas_cmprof_t *prof; if (!(prof = jas_malloc(sizeof(jas_cmprof_t)))) { return 0; } memset(prof, 0, sizeof(jas_cmprof_t)); prof->iccprof = 0; for (unsigned i = 0; i < JAS_CMPROF_NUMPXFORMSEQS; ++i) { prof->pxformseqs[i] = 0; } return prof; } void jas_cmprof_destroy(jas_cmprof_t *prof) { for (unsigned i = 0; i < JAS_CMPROF_NUMPXFORMSEQS; ++i) { if (prof->pxformseqs[i]) { jas_cmpxformseq_destroy(prof->pxformseqs[i]); prof->pxformseqs[i] = 0; } } if (prof->iccprof) { jas_iccprof_destroy(prof->iccprof); } jas_free(prof); } jas_cmprof_t *jas_cmprof_copy(const jas_cmprof_t *prof) { jas_cmprof_t *newprof; if (!(newprof = jas_cmprof_create())) { goto error; } newprof->clrspc = prof->clrspc; newprof->numchans = prof->numchans; newprof->refclrspc = prof->refclrspc; newprof->numrefchans = prof->numrefchans; newprof->iccprof = jas_iccprof_copy(prof->iccprof); for (unsigned i = 0; i < JAS_CMPROF_NUMPXFORMSEQS; ++i) { if (prof->pxformseqs[i]) { if (!(newprof->pxformseqs[i] = jas_cmpxformseq_copy(prof->pxformseqs[i]))) goto error; } } return newprof; error: if (newprof) { jas_cmprof_destroy(newprof); } return 0; } /******************************************************************************\ * Transform class. \******************************************************************************/ jas_cmxform_t *jas_cmxform_create(const jas_cmprof_t *inprof, const jas_cmprof_t *outprof, const jas_cmprof_t *prfprof, jas_cmxform_op_t op, jas_cmxform_intent_t intent, jas_cmxform_optm_t optimize) { jas_cmxform_t *xform; jas_cmpxformseq_t *inpxformseq; jas_cmpxformseq_t *outpxformseq; jas_cmpxformseq_t *altoutpxformseq; jas_cmpxformseq_t *prfpxformseq; JAS_UNUSED(optimize); const jas_cmxform_intent_t prfintent = intent; if (!(xform = jas_malloc(sizeof(jas_cmxform_t)))) { goto error; } if (!(xform->pxformseq = jas_cmpxformseq_create())) { goto error; } switch (op) { case JAS_CMXFORM_OP_FWD: inpxformseq = fwdpxformseq(inprof, intent); outpxformseq = revpxformseq(outprof, intent); if (!inpxformseq || !outpxformseq) { goto error; } if (jas_cmpxformseq_append(xform->pxformseq, inpxformseq) || jas_cmpxformseq_appendcnvt(xform->pxformseq, inprof->refclrspc, outprof->refclrspc) || jas_cmpxformseq_append(xform->pxformseq, outpxformseq)) { goto error; } xform->numinchans = jas_clrspc_numchans(inprof->clrspc); xform->numoutchans = jas_clrspc_numchans(outprof->clrspc); break; case JAS_CMXFORM_OP_REV: outpxformseq = fwdpxformseq(outprof, intent); inpxformseq = revpxformseq(inprof, intent); if (!outpxformseq || !inpxformseq) { goto error; } if (jas_cmpxformseq_append(xform->pxformseq, outpxformseq) || jas_cmpxformseq_appendcnvt(xform->pxformseq, outprof->refclrspc, inprof->refclrspc) || jas_cmpxformseq_append(xform->pxformseq, inpxformseq)) { goto error; } xform->numinchans = jas_clrspc_numchans(outprof->clrspc); xform->numoutchans = jas_clrspc_numchans(inprof->clrspc); break; case JAS_CMXFORM_OP_PROOF: assert(prfprof); inpxformseq = fwdpxformseq(inprof, intent); prfpxformseq = fwdpxformseq(prfprof, prfintent); if (!inpxformseq || !prfpxformseq) { goto error; } outpxformseq = simpxformseq(outprof, intent); altoutpxformseq = 0; if (!outpxformseq) { outpxformseq = revpxformseq(outprof, intent); altoutpxformseq = fwdpxformseq(outprof, intent); if (!outpxformseq || !altoutpxformseq) { goto error; } } if (jas_cmpxformseq_append(xform->pxformseq, inpxformseq) || jas_cmpxformseq_appendcnvt(xform->pxformseq, inprof->refclrspc, outprof->refclrspc)) { goto error; } if (altoutpxformseq) {