/*1* Copyright (c) 1999-2000 Image Power, Inc. and the University of2* British Columbia.3* Copyright (c) 2001-2003 Michael David Adams.4* All rights reserved.5*/67/* __START_OF_JASPER_LICENSE__8*9* JasPer License Version 2.010*11* Copyright (c) 2001-2006 Michael David Adams12* Copyright (c) 1999-2000 Image Power, Inc.13* Copyright (c) 1999-2000 The University of British Columbia14*15* All rights reserved.16*17* Permission is hereby granted, free of charge, to any person (the18* "User") obtaining a copy of this software and associated documentation19* files (the "Software"), to deal in the Software without restriction,20* including without limitation the rights to use, copy, modify, merge,21* publish, distribute, and/or sell copies of the Software, and to permit22* persons to whom the Software is furnished to do so, subject to the23* following conditions:24*25* 1. The above copyright notices and this permission notice (which26* includes the disclaimer below) shall be included in all copies or27* substantial portions of the Software.28*29* 2. The name of a copyright holder shall not be used to endorse or30* promote products derived from the Software without specific prior31* written permission.32*33* THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS34* LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER35* THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS36* "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING37* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A38* PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO39* EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL40* INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING41* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,42* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION43* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE44* PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE45* THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY.46* EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS47* BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL48* PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS49* GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE50* ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE51* IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL52* SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES,53* AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL54* SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH55* THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH,56* PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH57* RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY58* EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.59*60* __END_OF_JASPER_LICENSE__61*/6263/*64* Multicomponent Transform Code65*66* $Id: jpc_mct.c,v 1.2 2008-05-26 09:40:52 vp153 Exp $67*/6869/******************************************************************************\70* Includes.71\******************************************************************************/7273#include <assert.h>7475#include "jasper/jas_seq.h"7677#include "jpc_fix.h"78#include "jpc_mct.h"7980/******************************************************************************\81* Code.82\******************************************************************************/8384/* Compute the forward RCT. */8586void jpc_rct(jas_matrix_t *c0, jas_matrix_t *c1, jas_matrix_t *c2)87{88int numrows;89int numcols;90int i;91int j;92jpc_fix_t *c0p;93jpc_fix_t *c1p;94jpc_fix_t *c2p;9596numrows = jas_matrix_numrows(c0);97numcols = jas_matrix_numcols(c0);9899/* All three matrices must have the same dimensions. */100assert(jas_matrix_numrows(c1) == numrows && jas_matrix_numcols(c1) == numcols101&& jas_matrix_numrows(c2) == numrows && jas_matrix_numcols(c2) == numcols);102103for (i = 0; i < numrows; i++) {104c0p = jas_matrix_getref(c0, i, 0);105c1p = jas_matrix_getref(c1, i, 0);106c2p = jas_matrix_getref(c2, i, 0);107for (j = numcols; j > 0; --j) {108int r;109int g;110int b;111int y;112int u;113int v;114r = *c0p;115g = *c1p;116b = *c2p;117y = (r + (g << 1) + b) >> 2;118u = b - g;119v = r - g;120*c0p++ = y;121*c1p++ = u;122*c2p++ = v;123}124}125}126127/* Compute the inverse RCT. */128129void jpc_irct(jas_matrix_t *c0, jas_matrix_t *c1, jas_matrix_t *c2)130{131int numrows;132int numcols;133int i;134int j;135jpc_fix_t *c0p;136jpc_fix_t *c1p;137jpc_fix_t *c2p;138139numrows = jas_matrix_numrows(c0);140numcols = jas_matrix_numcols(c0);141142/* All three matrices must have the same dimensions. */143assert(jas_matrix_numrows(c1) == numrows && jas_matrix_numcols(c1) == numcols144&& jas_matrix_numrows(c2) == numrows && jas_matrix_numcols(c2) == numcols);145146for (i = 0; i < numrows; i++) {147c0p = jas_matrix_getref(c0, i, 0);148c1p = jas_matrix_getref(c1, i, 0);149c2p = jas_matrix_getref(c2, i, 0);150for (j = numcols; j > 0; --j) {151int r;152int g;153int b;154int y;155int u;156int v;157y = *c0p;158u = *c1p;159v = *c2p;160g = y - ((u + v) >> 2);161r = v + g;162b = u + g;163*c0p++ = r;164*c1p++ = g;165*c2p++ = b;166}167}168}169170void jpc_ict(jas_matrix_t *c0, jas_matrix_t *c1, jas_matrix_t *c2)171{172int numrows;173int numcols;174int i;175int j;176jpc_fix_t r;177jpc_fix_t g;178jpc_fix_t b;179jpc_fix_t y;180jpc_fix_t u;181jpc_fix_t v;182jpc_fix_t *c0p;183jpc_fix_t *c1p;184jpc_fix_t *c2p;185186numrows = jas_matrix_numrows(c0);187assert(jas_matrix_numrows(c1) == numrows && jas_matrix_numrows(c2) == numrows);188numcols = jas_matrix_numcols(c0);189assert(jas_matrix_numcols(c1) == numcols && jas_matrix_numcols(c2) == numcols);190for (i = 0; i < numrows; ++i) {191c0p = jas_matrix_getref(c0, i, 0);192c1p = jas_matrix_getref(c1, i, 0);193c2p = jas_matrix_getref(c2, i, 0);194for (j = numcols; j > 0; --j) {195r = *c0p;196g = *c1p;197b = *c2p;198y = jpc_fix_add3(jpc_fix_mul(jpc_dbltofix(0.299), r), jpc_fix_mul(jpc_dbltofix(0.587), g),199jpc_fix_mul(jpc_dbltofix(0.114), b));200u = jpc_fix_add3(jpc_fix_mul(jpc_dbltofix(-0.16875), r), jpc_fix_mul(jpc_dbltofix(-0.33126), g),201jpc_fix_mul(jpc_dbltofix(0.5), b));202v = jpc_fix_add3(jpc_fix_mul(jpc_dbltofix(0.5), r), jpc_fix_mul(jpc_dbltofix(-0.41869), g),203jpc_fix_mul(jpc_dbltofix(-0.08131), b));204*c0p++ = y;205*c1p++ = u;206*c2p++ = v;207}208}209}210211void jpc_iict(jas_matrix_t *c0, jas_matrix_t *c1, jas_matrix_t *c2)212{213int numrows;214int numcols;215int i;216int j;217jpc_fix_t r;218jpc_fix_t g;219jpc_fix_t b;220jpc_fix_t y;221jpc_fix_t u;222jpc_fix_t v;223jpc_fix_t *c0p;224jpc_fix_t *c1p;225jpc_fix_t *c2p;226227numrows = jas_matrix_numrows(c0);228assert(jas_matrix_numrows(c1) == numrows && jas_matrix_numrows(c2) == numrows);229numcols = jas_matrix_numcols(c0);230assert(jas_matrix_numcols(c1) == numcols && jas_matrix_numcols(c2) == numcols);231for (i = 0; i < numrows; ++i) {232c0p = jas_matrix_getref(c0, i, 0);233c1p = jas_matrix_getref(c1, i, 0);234c2p = jas_matrix_getref(c2, i, 0);235for (j = numcols; j > 0; --j) {236y = *c0p;237u = *c1p;238v = *c2p;239r = jpc_fix_add(y, jpc_fix_mul(jpc_dbltofix(1.402), v));240g = jpc_fix_add3(y, jpc_fix_mul(jpc_dbltofix(-0.34413), u),241jpc_fix_mul(jpc_dbltofix(-0.71414), v));242b = jpc_fix_add(y, jpc_fix_mul(jpc_dbltofix(1.772), u));243*c0p++ = r;244*c1p++ = g;245*c2p++ = b;246}247}248}249250jpc_fix_t jpc_mct_getsynweight(int mctid, int cmptno)251{252jpc_fix_t synweight;253254synweight = JPC_FIX_ONE;255switch (mctid) {256case JPC_MCT_RCT:257switch (cmptno) {258case 0:259synweight = jpc_dbltofix(sqrt(3.0));260break;261case 1:262synweight = jpc_dbltofix(sqrt(0.6875));263break;264case 2:265synweight = jpc_dbltofix(sqrt(0.6875));266break;267}268break;269case JPC_MCT_ICT:270switch (cmptno) {271case 0:272synweight = jpc_dbltofix(sqrt(3.0000));273break;274case 1:275synweight = jpc_dbltofix(sqrt(3.2584));276break;277case 2:278synweight = jpc_dbltofix(sqrt(2.4755));279break;280}281break;282#if 0283default:284synweight = JPC_FIX_ONE;285break;286#endif287}288289return synweight;290}291292293