fixed opj_malloc.h for macosx (bugfix provided by janpeder, thanks)
[openjpeg.git] / libopenjpeg / mct.c
1 /*
2  * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3  * Copyright (c) 2002-2007, Professor Benoit Macq
4  * Copyright (c) 2001-2003, David Janssens
5  * Copyright (c) 2002-2003, Yannick Verschueren
6  * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7  * Copyright (c) 2005, Herve Drolon, FreeImage Team
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #include "opj_includes.h"
33
34 /* <summary> */
35 /* This table contains the norms of the basis function of the reversible MCT. */
36 /* </summary> */
37 static const double mct_norms[3] = { 1.732, .8292, .8292 };
38
39 /* <summary> */
40 /* This table contains the norms of the basis function of the irreversible MCT. */
41 /* </summary> */
42 static const double mct_norms_real[3] = { 1.732, 1.805, 1.573 };
43
44 /* <summary> */
45 /* Foward reversible MCT. */
46 /* </summary> */
47 void mct_encode(
48                 int* restrict c0,
49                 int* restrict c1,
50                 int* restrict c2,
51                 int n)
52 {
53         int i;
54         for(i = 0; i < n; ++i) {
55                 int r = c0[i];
56                 int g = c1[i];
57                 int b = c2[i];
58                 int y = (r + (g * 2) + b) >> 2;
59                 int u = b - g;
60                 int v = r - g;
61                 c0[i] = y;
62                 c1[i] = u;
63                 c2[i] = v;
64         }
65 }
66
67 /* <summary> */
68 /* Inverse reversible MCT. */
69 /* </summary> */
70 void mct_decode(
71                 int* restrict c0,
72                 int* restrict c1, 
73                 int* restrict c2, 
74                 int n)
75 {
76         int i;
77         for (i = 0; i < n; ++i) {
78                 int y = c0[i];
79                 int u = c1[i];
80                 int v = c2[i];
81                 int g = y - ((u + v) >> 2);
82                 int r = v + g;
83                 int b = u + g;
84                 c0[i] = r;
85                 c1[i] = g;
86                 c2[i] = b;
87         }
88 }
89
90 /* <summary> */
91 /* Get norm of basis function of reversible MCT. */
92 /* </summary> */
93 double mct_getnorm(int compno) {
94         return mct_norms[compno];
95 }
96
97 /* <summary> */
98 /* Foward irreversible MCT. */
99 /* </summary> */
100 void mct_encode_real(
101                 int* restrict c0,
102                 int* restrict c1,
103                 int* restrict c2,
104                 int n)
105 {
106         int i;
107         for(i = 0; i < n; ++i) {
108                 int r = c0[i];
109                 int g = c1[i];
110                 int b = c2[i];
111                 int y =  fix_mul(r, 2449) + fix_mul(g, 4809) + fix_mul(b, 934);
112                 int u = -fix_mul(r, 1382) - fix_mul(g, 2714) + fix_mul(b, 4096);
113                 int v =  fix_mul(r, 4096) - fix_mul(g, 3430) - fix_mul(b, 666);
114                 c0[i] = y;
115                 c1[i] = u;
116                 c2[i] = v;
117         }
118 }
119
120 /* <summary> */
121 /* Inverse irreversible MCT. */
122 /* </summary> */
123 void mct_decode_real(
124                 float* restrict c0,
125                 float* restrict c1,
126                 float* restrict c2,
127                 int n)
128 {
129         int i;
130         for(i = 0; i < n; ++i) {
131                 float y = c0[i];
132                 float u = c1[i];
133                 float v = c2[i];
134                 float r = y + (v * 1.402f);
135                 float g = y - (u * 0.34413f) - (v * (0.71414f));
136                 float b = y + (u * 1.772f);
137                 c0[i] = r;
138                 c1[i] = g;
139                 c2[i] = b;
140         }
141 }
142
143 /* <summary> */
144 /* Get norm of basis function of irreversible MCT. */
145 /* </summary> */
146 double mct_getnorm_real(int compno) {
147         return mct_norms_real[compno];
148 }