1 //
2 // (C) Jan de Vaan 2007-2010, all rights reserved. See the accompanying "License.txt" for licensed use.
3 //
4 
5 
6 #ifndef CHARLS_UTIL
7 #define CHARLS_UTIL
8 
9 #define INCLUDE_CSTDDEF
10 #include "dcmtk/ofstd/ofstdinc.h"
11 #include "pubtypes.h"
12 
13 #ifndef MAX
14 #define MAX(a,b)            (((a) > (b)) ? (a) : (b))
15 #endif
16 
17 #ifndef MIN
18 #define MIN(a,b)            (((a) < (b)) ? (a) : (b))
19 #endif
20 
21 #ifndef ABS
22 #define ABS(a)              (((a) > 0) ? (a) : -(a))
23 #endif
24 
25 class alloc_fail { };
26 
log_2(LONG n)27 inline LONG log_2(LONG n)
28 {
29 	LONG x = 0;
30 	while (n > (LONG(1) << x))
31 	{
32 		++x;
33 	}
34 	return x;
35 
36 }
37 
38 struct Size
39 {
SizeSize40 	Size(LONG width, LONG height) :
41 		cx(width),
42 		cy(height)
43 	{}
44 	LONG cx;
45 	LONG cy;
46 };
47 
48 
49 
Sign(LONG n)50 inline LONG Sign(LONG n)
51 	{ return (n >> (LONG_BITCOUNT-1)) | 1;}
52 
BitWiseSign(LONG i)53 inline LONG BitWiseSign(LONG i)
54 	{ return i >> (LONG_BITCOUNT-1); }
55 
56 
57 template<class SAMPLE>
58 struct Triplet
59 {
TripletTriplet60 	Triplet() :
61 		v1(0),
62 		v2(0),
63 		v3(0)
64 	{}
65 
TripletTriplet66 	Triplet(LONG x1, LONG x2, LONG x3) :
67 		v1((SAMPLE)x1),
68 		v2((SAMPLE)x2),
69 		v3((SAMPLE)x3)
70 	{}
71 
72 		union
73 		{
74 			SAMPLE v1;
75 			SAMPLE R;
76 		};
77 		union
78 		{
79 			SAMPLE v2;
80 			SAMPLE G;
81 		};
82 		union
83 		{
84 			SAMPLE v3;
85 			SAMPLE B;
86 		};
87 };
88 
89 inline bool operator==(const Triplet<BYTE>& lhs, const Triplet<BYTE>& rhs)
90 	{ return lhs.v1 == rhs.v1 && lhs.v2 == rhs.v2 && lhs.v3 == rhs.v3; }
91 
92 inline bool  operator!=(const Triplet<BYTE>& lhs, const Triplet<BYTE>& rhs)
93 	{ return !(lhs == rhs); }
94 
95 
96 template<class sample>
97 struct Quad : public Triplet<sample>
98 {
QuadQuad99 	Quad() :
100 		v4(0)
101 		{}
102 
QuadQuad103 	Quad(Triplet<sample> triplet, LONG alpha) : Triplet<sample>(triplet), A((sample)alpha)
104 		{}
105 
106 	union
107 	{
108 		sample v4;
109 		sample A;
110 	};
111 };
112 
113 
114 
115 template <int size>
116 struct FromBigEndian
117 {
118 };
119 
120 template <>
121 struct FromBigEndian<4>
122 {
123 	inlinehint static unsigned int Read(BYTE* pbyte)
124 	{
125 		return  (pbyte[0] << 24) + (pbyte[1] << 16) + (pbyte[2] << 8) + (pbyte[3] << 0);
126 	}
127 };
128 
129 
130 
131 template <>
132 struct FromBigEndian<8>
133 {
134 	inlinehint static size_t Read(BYTE* pbyte)
135 	{
136 		size_t a = FromBigEndian<4>::Read(&pbyte[0]);
137 		size_t b = FromBigEndian<4>::Read(&pbyte[4]);
138 		return ((a << 16) << 16) + b;
139 	}
140 };
141 
142 
143 class JlsException
144 {
145 public:
146 	JlsException(JLS_ERROR error) : _error(error)
147 		{ }
148 
149 	JLS_ERROR _error;
150 };
151 
152 
153 #endif
154