📄 jas_cm.c
字号:
/* * 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$ */#include <jasper/jas_config.h>#include <math.h>#include <stdlib.h>#include <assert.h>#include <jasper/jas_cm.h>#include <jasper/jas_icc.h>#include <jasper/jas_init.h>#include <jasper/jas_stream.h>#include <jasper/jas_malloc.h>#include <jasper/jas_math.h>static jas_cmprof_t *jas_cmprof_create(void);static void jas_cmshapmatlut_cleanup(jas_cmshapmatlut_t *);static jas_cmreal_t jas_cmshapmatlut_lookup(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(jas_cmpxform_t *pxform, jas_cmreal_t *in, jas_cmreal_t *out, int cnt);static int jas_cmputint(long **bufptr, int sgnd, int prec, long val);static int jas_cmgetint(long **bufptr, int sgnd, int 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, int, int);static int jas_cmpxformseq_resize(jas_cmpxformseq_t *pxformseq, int n);static int mono(jas_iccprof_t *prof, int op, jas_cmpxformseq_t **pxformseq);static int triclr(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, int 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);#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(int iccclrspc, 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, jas_icccurv_t *curv);static jas_cmpxformops_t shapmat_ops = {jas_cmshapmat_destroy, jas_cmshapmat_apply, 0};static jas_cmprof_t *jas_cmprof_createsycc(void);/******************************************************************************\* Color profile class.\******************************************************************************/jas_cmprof_t *jas_cmprof_createfromclrspc(int clrspc){ jas_iccprof_t *iccprof; jas_cmprof_t *prof; iccprof = 0; prof = 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); iccprof = 0; 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_cmprof_t *prof; jas_cmpxform_t *fwdpxform; jas_cmpxform_t *revpxform; jas_cmshapmat_t *fwdshapmat; jas_cmshapmat_t *revshapmat; int i; int j; 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; jas_cmshapmat_invmat(revshapmat->mat, fwdshapmat->mat); for (i = 0; i < JAS_CMXFORM_NUMINTENTS; ++i) { 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: return 0;}jas_cmprof_t *jas_cmprof_createfromiccprof(jas_iccprof_t *iccprof){ jas_cmprof_t *prof; jas_icchdr_t icchdr; jas_cmpxformseq_t *fwdpxformseq; jas_cmpxformseq_t *revpxformseq; prof = 0; fwdpxformseq = 0; revpxformseq = 0; if (!(prof = jas_cmprof_create())) goto error; jas_iccprof_gethdr(iccprof, &icchdr); if (!(prof->iccprof = jas_iccprof_copy(iccprof))) goto error; prof->clrspc = icctoclrspc(icchdr.colorspc, 0); prof->refclrspc = icctoclrspc(icchdr.refcolorspc, 1); prof->numchans = jas_clrspc_numchans(prof->clrspc); prof->numrefchans = jas_clrspc_numchans(prof->refclrspc); if (prof->numchans == 1) { if (mono(prof->iccprof, 0, &fwdpxformseq)) goto error; if (mono(prof->iccprof, 1, &revpxformseq)) goto error; } else if (prof->numchans == 3) { if (triclr(prof->iccprof, 0, &fwdpxformseq)) goto error; if (triclr(prof->iccprof, 1, &revpxformseq)) 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(){ int i; 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 (i = 0; i < JAS_CMPROF_NUMPXFORMSEQS; ++i) prof->pxformseqs[i] = 0; return prof;}void jas_cmprof_destroy(jas_cmprof_t *prof){ int i; for (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(jas_cmprof_t *prof){ jas_cmprof_t *newprof; int i; 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 (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: return 0;}/******************************************************************************\* Transform class.\******************************************************************************/jas_cmxform_t *jas_cmxform_create(jas_cmprof_t *inprof, jas_cmprof_t *outprof, jas_cmprof_t *prfprof, int op, int intent, int optimize){ jas_cmxform_t *xform; jas_cmpxformseq_t *inpxformseq; jas_cmpxformseq_t *outpxformseq; jas_cmpxformseq_t *altoutpxformseq; jas_cmpxformseq_t *prfpxformseq; int prfintent; /* Avoid compiler warnings about unused parameters. */ optimize = 0; 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))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -