jpc_mct.c 9.0 KB

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