/src/jasper/src/libjasper/jpc/jpc_mct.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 1999-2000 Image Power, Inc. and the University of |
3 | | * British Columbia. |
4 | | * Copyright (c) 2001-2003 Michael David Adams. |
5 | | * All rights reserved. |
6 | | */ |
7 | | |
8 | | /* __START_OF_JASPER_LICENSE__ |
9 | | * |
10 | | * JasPer License Version 2.0 |
11 | | * |
12 | | * Copyright (c) 2001-2006 Michael David Adams |
13 | | * Copyright (c) 1999-2000 Image Power, Inc. |
14 | | * Copyright (c) 1999-2000 The University of British Columbia |
15 | | * |
16 | | * All rights reserved. |
17 | | * |
18 | | * Permission is hereby granted, free of charge, to any person (the |
19 | | * "User") obtaining a copy of this software and associated documentation |
20 | | * files (the "Software"), to deal in the Software without restriction, |
21 | | * including without limitation the rights to use, copy, modify, merge, |
22 | | * publish, distribute, and/or sell copies of the Software, and to permit |
23 | | * persons to whom the Software is furnished to do so, subject to the |
24 | | * following conditions: |
25 | | * |
26 | | * 1. The above copyright notices and this permission notice (which |
27 | | * includes the disclaimer below) shall be included in all copies or |
28 | | * substantial portions of the Software. |
29 | | * |
30 | | * 2. The name of a copyright holder shall not be used to endorse or |
31 | | * promote products derived from the Software without specific prior |
32 | | * written permission. |
33 | | * |
34 | | * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS |
35 | | * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER |
36 | | * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS |
37 | | * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
38 | | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
39 | | * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO |
40 | | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL |
41 | | * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING |
42 | | * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, |
43 | | * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION |
44 | | * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE |
45 | | * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE |
46 | | * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY. |
47 | | * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS |
48 | | * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL |
49 | | * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS |
50 | | * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE |
51 | | * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE |
52 | | * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL |
53 | | * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES, |
54 | | * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL |
55 | | * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH |
56 | | * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH, |
57 | | * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH |
58 | | * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY |
59 | | * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. |
60 | | * |
61 | | * __END_OF_JASPER_LICENSE__ |
62 | | */ |
63 | | |
64 | | /* |
65 | | * Multicomponent Transform Code |
66 | | * |
67 | | * $Id$ |
68 | | */ |
69 | | |
70 | | /******************************************************************************\ |
71 | | * Includes. |
72 | | \******************************************************************************/ |
73 | | |
74 | | #include "jpc_mct.h" |
75 | | #include "jpc_fix.h" |
76 | | |
77 | | #include "jasper/jas_seq.h" |
78 | | |
79 | | #include <assert.h> |
80 | | #include <math.h> |
81 | | |
82 | | /******************************************************************************\ |
83 | | * Code. |
84 | | \******************************************************************************/ |
85 | | |
86 | | /* Compute the forward RCT. */ |
87 | | |
88 | | void jpc_rct(jas_matrix_t *c0, jas_matrix_t *c1, jas_matrix_t *c2) |
89 | 424 | { |
90 | 424 | int numrows; |
91 | 424 | int numcols; |
92 | 424 | int i; |
93 | 424 | int j; |
94 | 424 | jpc_fix_t *c0p; |
95 | 424 | jpc_fix_t *c1p; |
96 | 424 | jpc_fix_t *c2p; |
97 | | |
98 | 424 | numrows = jas_matrix_numrows(c0); |
99 | 424 | numcols = jas_matrix_numcols(c0); |
100 | | |
101 | | /* All three matrices must have the same dimensions. */ |
102 | 424 | assert(jas_matrix_numrows(c1) == numrows && jas_matrix_numcols(c1) == numcols |
103 | 424 | && jas_matrix_numrows(c2) == numrows && jas_matrix_numcols(c2) == numcols); |
104 | | |
105 | 144k | for (i = 0; i < numrows; i++) { |
106 | 144k | c0p = jas_matrix_getref(c0, i, 0); |
107 | 144k | c1p = jas_matrix_getref(c1, i, 0); |
108 | 144k | c2p = jas_matrix_getref(c2, i, 0); |
109 | 109M | for (j = numcols; j > 0; --j) { |
110 | 109M | int r; |
111 | 109M | int g; |
112 | 109M | int b; |
113 | 109M | int y; |
114 | 109M | int u; |
115 | 109M | int v; |
116 | 109M | r = *c0p; |
117 | 109M | g = *c1p; |
118 | 109M | b = *c2p; |
119 | | //y = (r + (g << 1) + b) >> 2; |
120 | 109M | y = jpc_fix_asr(r + jpc_fix_asl(g, 1) + b, 2); |
121 | 109M | u = b - g; |
122 | 109M | v = r - g; |
123 | 109M | *c0p++ = y; |
124 | 109M | *c1p++ = u; |
125 | 109M | *c2p++ = v; |
126 | 109M | } |
127 | 144k | } |
128 | 424 | } |
129 | | |
130 | | /* Compute the inverse RCT. */ |
131 | | |
132 | | void jpc_irct(jas_matrix_t *c0, jas_matrix_t *c1, jas_matrix_t *c2) |
133 | 874 | { |
134 | 874 | int numrows; |
135 | 874 | int numcols; |
136 | 874 | int i; |
137 | 874 | int j; |
138 | 874 | jpc_fix_t *c0p; |
139 | 874 | jpc_fix_t *c1p; |
140 | 874 | jpc_fix_t *c2p; |
141 | | |
142 | 874 | numrows = jas_matrix_numrows(c0); |
143 | 874 | numcols = jas_matrix_numcols(c0); |
144 | | |
145 | | /* All three matrices must have the same dimensions. */ |
146 | 874 | assert(jas_matrix_numrows(c1) == numrows && jas_matrix_numcols(c1) == numcols |
147 | 874 | && jas_matrix_numrows(c2) == numrows && jas_matrix_numcols(c2) == numcols); |
148 | | |
149 | 106k | for (i = 0; i < numrows; i++) { |
150 | 105k | c0p = jas_matrix_getref(c0, i, 0); |
151 | 105k | c1p = jas_matrix_getref(c1, i, 0); |
152 | 105k | c2p = jas_matrix_getref(c2, i, 0); |
153 | 16.8M | for (j = numcols; j > 0; --j) { |
154 | 16.7M | int r; |
155 | 16.7M | int g; |
156 | 16.7M | int b; |
157 | 16.7M | int y; |
158 | 16.7M | int u; |
159 | 16.7M | int v; |
160 | 16.7M | y = *c0p; |
161 | 16.7M | u = *c1p; |
162 | 16.7M | v = *c2p; |
163 | | //g = y - ((u + v) >> 2); |
164 | 16.7M | g = y - jpc_fix_asr(u + v, 2); |
165 | 16.7M | r = v + g; |
166 | 16.7M | b = u + g; |
167 | 16.7M | *c0p++ = r; |
168 | 16.7M | *c1p++ = g; |
169 | 16.7M | *c2p++ = b; |
170 | 16.7M | } |
171 | 105k | } |
172 | 874 | } |
173 | | |
174 | | void jpc_ict(jas_matrix_t *c0, jas_matrix_t *c1, jas_matrix_t *c2) |
175 | 0 | { |
176 | 0 | int numrows; |
177 | 0 | int numcols; |
178 | 0 | int i; |
179 | 0 | int j; |
180 | 0 | jpc_fix_t r; |
181 | 0 | jpc_fix_t g; |
182 | 0 | jpc_fix_t b; |
183 | 0 | jpc_fix_t y; |
184 | 0 | jpc_fix_t u; |
185 | 0 | jpc_fix_t v; |
186 | 0 | jpc_fix_t *c0p; |
187 | 0 | jpc_fix_t *c1p; |
188 | 0 | jpc_fix_t *c2p; |
189 | |
|
190 | 0 | numrows = jas_matrix_numrows(c0); |
191 | 0 | assert(jas_matrix_numrows(c1) == numrows && jas_matrix_numrows(c2) == numrows); |
192 | 0 | numcols = jas_matrix_numcols(c0); |
193 | 0 | assert(jas_matrix_numcols(c1) == numcols && jas_matrix_numcols(c2) == numcols); |
194 | 0 | for (i = 0; i < numrows; ++i) { |
195 | 0 | c0p = jas_matrix_getref(c0, i, 0); |
196 | 0 | c1p = jas_matrix_getref(c1, i, 0); |
197 | 0 | c2p = jas_matrix_getref(c2, i, 0); |
198 | 0 | for (j = numcols; j > 0; --j) { |
199 | 0 | r = *c0p; |
200 | 0 | g = *c1p; |
201 | 0 | b = *c2p; |
202 | 0 | y = jpc_fix_add3(jpc_fix_mul(jpc_dbltofix(0.299), r), jpc_fix_mul(jpc_dbltofix(0.587), g), |
203 | 0 | jpc_fix_mul(jpc_dbltofix(0.114), b)); |
204 | 0 | u = jpc_fix_add3(jpc_fix_mul(jpc_dbltofix(-0.16875), r), jpc_fix_mul(jpc_dbltofix(-0.33126), g), |
205 | 0 | jpc_fix_mul(jpc_dbltofix(0.5), b)); |
206 | 0 | v = jpc_fix_add3(jpc_fix_mul(jpc_dbltofix(0.5), r), jpc_fix_mul(jpc_dbltofix(-0.41869), g), |
207 | 0 | jpc_fix_mul(jpc_dbltofix(-0.08131), b)); |
208 | 0 | *c0p++ = y; |
209 | 0 | *c1p++ = u; |
210 | 0 | *c2p++ = v; |
211 | 0 | } |
212 | 0 | } |
213 | 0 | } |
214 | | |
215 | | void jpc_iict(jas_matrix_t *c0, jas_matrix_t *c1, jas_matrix_t *c2) |
216 | 3.22k | { |
217 | 3.22k | int numrows; |
218 | 3.22k | int numcols; |
219 | 3.22k | int i; |
220 | 3.22k | int j; |
221 | 3.22k | jpc_fix_t r; |
222 | 3.22k | jpc_fix_t g; |
223 | 3.22k | jpc_fix_t b; |
224 | 3.22k | jpc_fix_t y; |
225 | 3.22k | jpc_fix_t u; |
226 | 3.22k | jpc_fix_t v; |
227 | 3.22k | jpc_fix_t *c0p; |
228 | 3.22k | jpc_fix_t *c1p; |
229 | 3.22k | jpc_fix_t *c2p; |
230 | | |
231 | 3.22k | numrows = jas_matrix_numrows(c0); |
232 | 3.22k | assert(jas_matrix_numrows(c1) == numrows && jas_matrix_numrows(c2) == numrows); |
233 | 3.22k | numcols = jas_matrix_numcols(c0); |
234 | 3.22k | assert(jas_matrix_numcols(c1) == numcols && jas_matrix_numcols(c2) == numcols); |
235 | 177k | for (i = 0; i < numrows; ++i) { |
236 | 173k | c0p = jas_matrix_getref(c0, i, 0); |
237 | 173k | c1p = jas_matrix_getref(c1, i, 0); |
238 | 173k | c2p = jas_matrix_getref(c2, i, 0); |
239 | 44.1M | for (j = numcols; j > 0; --j) { |
240 | 44.0M | y = *c0p; |
241 | 44.0M | u = *c1p; |
242 | 44.0M | v = *c2p; |
243 | 44.0M | r = jpc_fix_add(y, jpc_fix_mul(jpc_dbltofix(1.402), v)); |
244 | 44.0M | g = jpc_fix_add3(y, jpc_fix_mul(jpc_dbltofix(-0.34413), u), |
245 | 44.0M | jpc_fix_mul(jpc_dbltofix(-0.71414), v)); |
246 | 44.0M | b = jpc_fix_add(y, jpc_fix_mul(jpc_dbltofix(1.772), u)); |
247 | 44.0M | *c0p++ = r; |
248 | 44.0M | *c1p++ = g; |
249 | 44.0M | *c2p++ = b; |
250 | 44.0M | } |
251 | 173k | } |
252 | 3.22k | } |