SMIL  0.9.1
DBinary.h
1 /*
2  * Copyright (c) 2011-2016, Matthieu FAESSEL and ARMINES
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of Matthieu FAESSEL, or ARMINES nor the
14  * names of its contributors may be used to endorse or promote products
15  * derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 
30 #ifndef _DBINARY_BIN_H
31 #define _DBINARY_BIN_H
32 
33 #include <cstring>
34 #include <iostream>
35 
36 #include <limits>
37 
38 #include "Core/include/private/DTypes.hpp"
39 #include "Core/include/private/DMemory.hpp"
40 
41 using namespace std;
42 
43 namespace smil
44 {
45 
46  #ifdef USE_64BIT_IDS
47  // typedef UINT8 INT_TYPE;
48  typedef UINT8 BIN_TYPE;
49  #else
50  typedef UINT8 BIN_TYPE;
51  #endif // USE_64BIT_IDS
52 
53  struct bitIndex
54  {
55  BIN_TYPE *byte;
56  unsigned int index;
57 
58  bitIndex& operator=(bool val)
59  {
60  if (val)
61  (*byte) |= (1UL<<index);
62  else
63  (*byte) &= ~(1UL<<index);
64  return *this;
65  }
66  operator bool()
67  {
68  return (*byte) & (1UL<<index);
69  }
70  };
71 
72  struct BIN
73  {
74  BIN_TYPE val;
75 
76  BIN(BIN_TYPE v = numeric_limits<BIN_TYPE>::min()) : val(v) {}
77  BIN(bool b) : val(b ? this->max() : this->min()) {}
78  BIN(double v) : val(v==0 ? this->min() : this->max()) {}
79 
80  static const BIN_TYPE SIZE = sizeof(BIN_TYPE)*CHAR_BIT;
81 
82  static inline BIN_TYPE min() { return numeric_limits<BIN_TYPE>::min(); }
83  static inline BIN_TYPE max() { return numeric_limits<BIN_TYPE>::max(); }
84 
86  static const BIN_TYPE MS_BIT = (1UL << (SIZE - 2));
88  static const BIN_TYPE LS_BIT = 0x01;
89 
90 
91  typedef BIN_TYPE Type;
92  typedef Type *lineType;
93  typedef lineType *sliceType;
94 
95  static inline BIN_TYPE binLen(BIN_TYPE bitCount) { return (bitCount-1)/BIN::SIZE + 1; }
96 
97  inline bitIndex& operator[] (UINT8 pos)
98  {
99  static bitIndex b;
100  b.byte = &val;
101  b.index = pos;
102  return b;
103  }
104  ostream& printSelf(ostream &os=cout)
105  {
106  for (int i=0;i<SIZE;i++)
107  os << this->operator[](i) << " ";
108  return os;
109  }
110  inline BIN& operator=(BIN_TYPE v)
111  {
112  val = v;
113  return *this;
114  }
115  inline BIN& operator=(bool b)
116  {
117  val = b ? this->max() : this->min();
118  return *this;
119  }
120  inline BIN& operator=(const char* s)
121  {
122  UINT iMax = strlen(s) < SIZE ? strlen(s) : SIZE;
123 
124  val = 0;
125  for (UINT i=0;i<iMax;i++)
126  val += (s[i]-48) * (1<<i);
127  return *this;
128  }
129  // inline operator bool()
130  // {
131  // return val!=0;
132  // }
133  };
134 
135  inline ostream& operator << (ostream &os, BIN &b)
136  {
137  return b.printSelf(os);
138  }
139 
140  // #define BIN BIN<UINT32>
141 
142 
143 
144  //
145  // inline RES_T setPixel(UINT x, UINT y, UINT z, pixelType value)
146  // {
147  // if (x>=width || y>=height || z>=depth)
148  // return RES_ERR;
149  // pixels[z*width*height+y*width+x] = value;
150  // modified();
151  // return RES_OK;
152  // }
153  // inline RES_T setPixel(UINT x, UINT y, pixelType value)
154  // {
155  // if (x>=width || y>=height)
156  // return RES_ERR;
157  // pixels[height+y*width+x] = value;
158  // modified();
159  // return RES_OK;
160  // }
161  // inline RES_T setPixel(UINT offset, pixelType value)
162  // {
163  // if (offset >= pixelCount)
164  // return RES_ERR;
165  // pixels[offset] = value;
166  // modified();
167  // return RES_OK;
168  // }
169 
170 } // namespace smil
171 
172 
173 #endif // _DBINARY_BIN_H
Definition: DColorConvert.h:38
Definition: DBinary.h:72
Definition: DBinary.h:53