|
1 /* |
|
2 * Copyright (c) 2003, 2004 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: TJ2kUtils class used to provide common public static |
|
15 * functions used by all classes in JP2KCodec. |
|
16 * |
|
17 */ |
|
18 |
|
19 |
|
20 // INCLUDE FILES |
|
21 #include "JP2KUtils.h" |
|
22 |
|
23 // EXTERNAL DATA STRUCTURES |
|
24 |
|
25 // EXTERNAL FUNCTION PROTOTYPES |
|
26 |
|
27 // CONSTANTS |
|
28 const TUint32 KLog2Table[] = { 0,1,2,4,8,16,32,64,128,256,512,1024,2048, |
|
29 4096,8192,16384,32768,65536,131072,262144, |
|
30 524288,1048576,2097152,4194304,8388608, |
|
31 16777216,33554432,67108864,134217728, |
|
32 268435456,536870912,1073741824, |
|
33 KMaxTUint32 }; |
|
34 |
|
35 // MACROS |
|
36 |
|
37 // LOCAL CONSTANTS AND MACROS |
|
38 |
|
39 // MODULE DATA STRUCTURES |
|
40 |
|
41 // LOCAL FUNCTION PROTOTYPES |
|
42 |
|
43 // FORWARD DECLARATIONS |
|
44 |
|
45 // CONSTANTS |
|
46 |
|
47 // ============================ MEMBER FUNCTIONS =============================== |
|
48 |
|
49 // ----------------------------------------------------------------------------- |
|
50 // TJ2kUtils::Ceil |
|
51 // Get the ceiling between two integers. |
|
52 // (other items were commented in a header). |
|
53 // ----------------------------------------------------------------------------- |
|
54 // |
|
55 TInt32 TJ2kUtils::Ceil( TInt32 aL, TInt32 aR ) |
|
56 { |
|
57 // Check that no divided by zero calculation are done. |
|
58 if ( aR == 0 ) |
|
59 { |
|
60 User::Leave( KErrCorrupt ); |
|
61 } |
|
62 |
|
63 if ( aL < 0 ) |
|
64 { |
|
65 if ( aR < 0 ) |
|
66 { |
|
67 aL = -aL; |
|
68 aR = -aR; |
|
69 return ( aL % aR ) ? ( aL / aR ) + 1 : aL / aR; |
|
70 } |
|
71 else |
|
72 { |
|
73 return -( ( -aL ) / aR ); |
|
74 } |
|
75 } |
|
76 else if ( aR < 0 ) |
|
77 { |
|
78 return -( aL / ( -aR ) ); |
|
79 } |
|
80 else |
|
81 { |
|
82 return ( aL % aR ) ? ( aL / aR ) + 1 : aL / aR; |
|
83 } |
|
84 } |
|
85 |
|
86 // ----------------------------------------------------------------------------- |
|
87 // TJ2kUtils::Floor |
|
88 // Get the floor between two integers. |
|
89 // (other items were commented in a header). |
|
90 // ----------------------------------------------------------------------------- |
|
91 // |
|
92 TInt32 TJ2kUtils::Floor( TInt32 aL, TInt32 aR ) |
|
93 { |
|
94 // Check that no divided by zero calculation are done. |
|
95 if ( aR == 0 ) |
|
96 { |
|
97 User::Leave( KErrCorrupt ); |
|
98 } |
|
99 |
|
100 if ( ( aL < 0 || aR < 0 ) && !( aL < 0 && aR < 0 ) ) |
|
101 { |
|
102 return aL / aR - 1; |
|
103 } |
|
104 else |
|
105 { |
|
106 return aL / aR; |
|
107 } |
|
108 } |
|
109 |
|
110 // ----------------------------------------------------------------------------- |
|
111 // TJ2kUtils::Div |
|
112 // Get the quotient and remainder. |
|
113 // (other items were commented in a header). |
|
114 // ----------------------------------------------------------------------------- |
|
115 // |
|
116 TDiv TJ2kUtils::Div( TInt aNum, TInt aDenom ) |
|
117 { |
|
118 // Check that no divided by zero calculation are done. |
|
119 if ( aDenom == 0 ) |
|
120 { |
|
121 User::Leave( KErrCorrupt ); |
|
122 } |
|
123 |
|
124 TDiv aDiv; |
|
125 aDiv.quot = aNum / aDenom; |
|
126 aDiv.rem = aNum % aDenom; |
|
127 |
|
128 /* |
|
129 * The ANSI standard says that |r.quot| <= |n/d|, where |
|
130 * n/d is to be computed in infinite precision. In other |
|
131 * words, we should always truncate the quotient towards |
|
132 * 0, never -infinity. |
|
133 * |
|
134 * Machine division and remainer may work either way when |
|
135 * one or both of n or d is negative. If only one is |
|
136 * negative and r.quot has been truncated towards -inf, |
|
137 * r.rem will have the same sign as denom and the opposite |
|
138 * sign of num; if both are negative and r.quot has been |
|
139 * truncated towards -inf, r.rem will be positive ( will |
|
140 * have the opposite sign of num ). These are considered |
|
141 * `wrong'. |
|
142 * |
|
143 * If both are num and denom are positive, r will always |
|
144 * be positive. |
|
145 * |
|
146 * This all boils down to: |
|
147 * if num >= 0, but r.rem < 0, we got the wrong answer. |
|
148 * In that case, to get the right answer, add 1 to r.quot and |
|
149 * subtract denom from r.rem. |
|
150 */ |
|
151 if ( aNum >= 0 && aDiv.rem < 0 ) |
|
152 { |
|
153 aDiv.quot++; |
|
154 aDiv.rem -= aDenom; |
|
155 } |
|
156 return aDiv; |
|
157 } |
|
158 |
|
159 // ----------------------------------------------------------------------------- |
|
160 // TJ2kUtils::Log2 |
|
161 // Get he log2 value of an integer. |
|
162 // (other items were commented in a header). |
|
163 // ----------------------------------------------------------------------------- |
|
164 // |
|
165 TInt32 TJ2kUtils::Log2( TUint32 aI ) |
|
166 { |
|
167 TInt32 index = 0; |
|
168 TInt32 count = sizeof( KLog2Table ) / sizeof( TUint32 ); |
|
169 for ( index = 0; index < count; ++index ) |
|
170 { |
|
171 if ( KLog2Table[index] > aI ) |
|
172 { |
|
173 break; //lint !e960 Break is OK. |
|
174 } |
|
175 } |
|
176 return index - 2; |
|
177 } |
|
178 |
|
179 // ----------------------------------------------------------------------------- |
|
180 // TJ2kUtils::Alloc2DArrayL |
|
181 // Allocate 2-D array. |
|
182 // (other items were commented in a header). |
|
183 // ----------------------------------------------------------------------------- |
|
184 // |
|
185 TPrecInt** TJ2kUtils::Alloc2DArrayL( TInt aRowSize, TInt aColSize ) |
|
186 { |
|
187 TPrecInt **ptr = STATIC_CAST( TPrecInt**, User::Alloc( aRowSize * sizeof( TPrecInt* ) ) ); |
|
188 if ( !ptr ) |
|
189 { |
|
190 User::Leave( KErrNoMemory ); |
|
191 } |
|
192 CleanupStack::PushL( ptr ); |
|
193 |
|
194 // If first allocation fail, it will not reach here |
|
195 TPrecInt *ptrRow = STATIC_CAST( TPrecInt*, User::Alloc( aRowSize * aColSize * sizeof( TPrecInt ) ) ); |
|
196 if ( !ptrRow ) |
|
197 { |
|
198 User::Leave( KErrNoMemory ); |
|
199 } |
|
200 Mem::FillZ( ptrRow, ( aRowSize * aColSize * sizeof( TPrecInt ) ) ); |
|
201 for ( TInt i = 0; i < aRowSize; ++i ) |
|
202 { |
|
203 ptr[i] = ptrRow + ( i * aColSize ); |
|
204 } |
|
205 |
|
206 CleanupStack::Pop( 1 ); |
|
207 return ptr; |
|
208 } |
|
209 |
|
210 // ----------------------------------------------------------------------------- |
|
211 // TJ2kUtils::Free2DArray |
|
212 // Free 2-D array. |
|
213 // (other items were commented in a header). |
|
214 // ----------------------------------------------------------------------------- |
|
215 // |
|
216 void TJ2kUtils::Free2DArray( TPrecInt **aPtr ) |
|
217 { |
|
218 if ( aPtr ) |
|
219 { |
|
220 if ( aPtr[0] ) |
|
221 { |
|
222 User::Free( aPtr[0] ); |
|
223 } |
|
224 User::Free( aPtr ); |
|
225 } |
|
226 } |
|
227 |
|
228 // ----------------------------------------------------------------------------- |
|
229 // TJ2kUtils::Free2DArray |
|
230 // Wrapper to free 2-D array when put into TCleanupItem. |
|
231 // (other items were commented in a header). |
|
232 // ----------------------------------------------------------------------------- |
|
233 // |
|
234 void TJ2kUtils::Free2DArray( TAny *aPtr ) //lint !e1714 Referenced from CleanupStack item. |
|
235 { |
|
236 Free2DArray( ( TPrecInt ** )aPtr ); |
|
237 } |
|
238 |