/src/freeimage-svn/FreeImage/trunk/Source/OpenEXR/IlmImf/ImfTimeCode.cpp
Line  | Count  | Source  | 
1  |  | ///////////////////////////////////////////////////////////////////////////  | 
2  |  | //  | 
3  |  | // Copyright (c) 2004, Industrial Light & Magic, a division of Lucas  | 
4  |  | // Digital Ltd. LLC  | 
5  |  | //   | 
6  |  | // All rights reserved.  | 
7  |  | //   | 
8  |  | // Redistribution and use in source and binary forms, with or without  | 
9  |  | // modification, are permitted provided that the following conditions are  | 
10  |  | // met:  | 
11  |  | // *       Redistributions of source code must retain the above copyright  | 
12  |  | // notice, this list of conditions and the following disclaimer.  | 
13  |  | // *       Redistributions in binary form must reproduce the above  | 
14  |  | // copyright notice, this list of conditions and the following disclaimer  | 
15  |  | // in the documentation and/or other materials provided with the  | 
16  |  | // distribution.  | 
17  |  | // *       Neither the name of Industrial Light & Magic nor the names of  | 
18  |  | // its contributors may be used to endorse or promote products derived  | 
19  |  | // from this software without specific prior written permission.   | 
20  |  | //   | 
21  |  | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS  | 
22  |  | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT  | 
23  |  | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR  | 
24  |  | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT  | 
25  |  | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,  | 
26  |  | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT  | 
27  |  | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,  | 
28  |  | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY  | 
29  |  | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT  | 
30  |  | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE  | 
31  |  | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  | 
32  |  | //  | 
33  |  | ///////////////////////////////////////////////////////////////////////////  | 
34  |  |  | 
35  |  |  | 
36  |  | //-----------------------------------------------------------------------------  | 
37  |  | //  | 
38  |  | //  class TimeCode  | 
39  |  | //    | 
40  |  | //-----------------------------------------------------------------------------  | 
41  |  |  | 
42  |  | #include <ImfTimeCode.h>  | 
43  |  | #include "Iex.h"  | 
44  |  | #include "ImfNamespace.h"  | 
45  |  |  | 
46  |  | OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_ENTER  | 
47  |  |  | 
48  |  |      | 
49  |  | TimeCode::TimeCode ()  | 
50  | 0  | { | 
51  | 0  |     _time = 0;  | 
52  | 0  |     _user = 0;  | 
53  | 0  | }  | 
54  |  |  | 
55  |  |  | 
56  |  | TimeCode::TimeCode  | 
57  |  |     (int hours,  | 
58  |  |      int minutes,  | 
59  |  |      int seconds,  | 
60  |  |      int frame,  | 
61  |  |      bool dropFrame,  | 
62  |  |      bool colorFrame,  | 
63  |  |      bool fieldPhase,  | 
64  |  |      bool bgf0,  | 
65  |  |      bool bgf1,  | 
66  |  |      bool bgf2,  | 
67  |  |      int binaryGroup1,  | 
68  |  |      int binaryGroup2,  | 
69  |  |      int binaryGroup3,  | 
70  |  |      int binaryGroup4,  | 
71  |  |      int binaryGroup5,  | 
72  |  |      int binaryGroup6,  | 
73  |  |      int binaryGroup7,  | 
74  |  |      int binaryGroup8)  | 
75  | 0  | { | 
76  | 0  |     setHours (hours);  | 
77  | 0  |     setMinutes (minutes);  | 
78  | 0  |     setSeconds (seconds);  | 
79  | 0  |     setFrame (frame);  | 
80  | 0  |     setDropFrame (dropFrame);  | 
81  | 0  |     setColorFrame (colorFrame);  | 
82  | 0  |     setFieldPhase (fieldPhase);  | 
83  | 0  |     setBgf0 (bgf0);  | 
84  | 0  |     setBgf1 (bgf1);  | 
85  | 0  |     setBgf2 (bgf2);  | 
86  | 0  |     setBinaryGroup (1, binaryGroup1);  | 
87  | 0  |     setBinaryGroup (2, binaryGroup2);  | 
88  | 0  |     setBinaryGroup (3, binaryGroup3);  | 
89  | 0  |     setBinaryGroup (4, binaryGroup4);  | 
90  | 0  |     setBinaryGroup (5, binaryGroup5);  | 
91  | 0  |     setBinaryGroup (6, binaryGroup6);  | 
92  | 0  |     setBinaryGroup (7, binaryGroup7);  | 
93  | 0  |     setBinaryGroup (8, binaryGroup8);  | 
94  | 0  | }  | 
95  |  |  | 
96  |  |  | 
97  |  | TimeCode::TimeCode  | 
98  |  |     (unsigned int timeAndFlags,  | 
99  |  |      unsigned int userData,  | 
100  |  |      Packing packing)  | 
101  | 0  | { | 
102  | 0  |     setTimeAndFlags (timeAndFlags, packing);  | 
103  | 0  |     setUserData (userData);  | 
104  | 0  | }  | 
105  |  |  | 
106  |  |  | 
107  |  | TimeCode::TimeCode (const TimeCode &other)  | 
108  | 0  | { | 
109  | 0  |     _time = other._time;  | 
110  | 0  |     _user = other._user;  | 
111  | 0  | }  | 
112  |  |  | 
113  |  |  | 
114  |  | TimeCode &  | 
115  |  | TimeCode::operator = (const TimeCode &other)  | 
116  | 0  | { | 
117  | 0  |     _time = other._time;  | 
118  | 0  |     _user = other._user;  | 
119  | 0  |     return *this;  | 
120  | 0  | }  | 
121  |  |  | 
122  |  |       | 
123  |  | bool  | 
124  |  | TimeCode::operator == (const TimeCode & c) const  | 
125  | 0  | { | 
126  | 0  |     return (_time == c._time && _user == c._user);  | 
127  | 0  | }  | 
128  |  |  | 
129  |  |  | 
130  |  | bool  | 
131  |  | TimeCode::operator != (const TimeCode & c) const  | 
132  | 0  | { | 
133  | 0  |     return (_time != c._time || _user != c._user);  | 
134  | 0  | }  | 
135  |  |       | 
136  |  |       | 
137  |  |  | 
138  |  | namespace { | 
139  |  |  | 
140  |  | unsigned int  | 
141  |  | bitField (unsigned int value, int minBit, int maxBit)  | 
142  | 0  | { | 
143  | 0  |     int shift = minBit;  | 
144  | 0  |     unsigned int mask = (~(~0U << (maxBit - minBit + 1)) << minBit);  | 
145  | 0  |     return (value & mask) >> shift;  | 
146  | 0  | }  | 
147  |  |  | 
148  |  |  | 
149  |  | void  | 
150  |  | setBitField (unsigned int &value, int minBit, int maxBit, unsigned int field)  | 
151  | 0  | { | 
152  | 0  |     int shift = minBit;  | 
153  | 0  |     unsigned int mask = (~(~0U << (maxBit - minBit + 1)) << minBit);  | 
154  | 0  |     value = ((value & ~mask) | ((field << shift) & mask));  | 
155  | 0  | }  | 
156  |  |  | 
157  |  |  | 
158  |  | int  | 
159  |  | bcdToBinary (unsigned int bcd)  | 
160  | 0  | { | 
161  | 0  |     return int ((bcd & 0x0f) + 10 * ((bcd >> 4) & 0x0f));  | 
162  | 0  | }  | 
163  |  |  | 
164  |  |  | 
165  |  | unsigned int  | 
166  |  | binaryToBcd (int binary)  | 
167  | 0  | { | 
168  | 0  |     int units = binary % 10;  | 
169  | 0  |     int tens = (binary / 10) % 10;  | 
170  | 0  |     return (unsigned int) (units | (tens << 4));  | 
171  | 0  | }  | 
172  |  |  | 
173  |  |  | 
174  |  | } // namespace  | 
175  |  |  | 
176  |  |  | 
177  |  | int  | 
178  |  | TimeCode::hours () const  | 
179  | 0  | { | 
180  | 0  |     return bcdToBinary (bitField (_time, 24, 29));  | 
181  | 0  | }  | 
182  |  |  | 
183  |  |  | 
184  |  | void  | 
185  |  | TimeCode::setHours (int value)  | 
186  | 0  | { | 
187  | 0  |     if (value < 0 || value > 23)  | 
188  | 0  |   throw IEX_NAMESPACE::ArgExc ("Cannot set hours field in time code. " | 
189  | 0  |          "New value is out of range.");  | 
190  |  |  | 
191  | 0  |     setBitField (_time, 24, 29, binaryToBcd (value));  | 
192  | 0  | }  | 
193  |  |  | 
194  |  |  | 
195  |  | int  | 
196  |  | TimeCode::minutes () const  | 
197  | 0  | { | 
198  | 0  |     return bcdToBinary (bitField (_time, 16, 22));  | 
199  | 0  | }  | 
200  |  |  | 
201  |  |  | 
202  |  | void  | 
203  |  | TimeCode::setMinutes (int value)  | 
204  | 0  | { | 
205  | 0  |     if (value < 0 || value > 59)  | 
206  | 0  |   throw IEX_NAMESPACE::ArgExc ("Cannot set minutes field in time code. " | 
207  | 0  |          "New value is out of range.");  | 
208  |  |  | 
209  | 0  |     setBitField (_time, 16, 22, binaryToBcd (value));  | 
210  | 0  | }  | 
211  |  |  | 
212  |  |  | 
213  |  | int  | 
214  |  | TimeCode::seconds () const  | 
215  | 0  | { | 
216  | 0  |     return bcdToBinary (bitField (_time, 8, 14));  | 
217  | 0  | }  | 
218  |  |  | 
219  |  |  | 
220  |  | void  | 
221  |  | TimeCode::setSeconds (int value)  | 
222  | 0  | { | 
223  | 0  |     if (value < 0 || value > 59)  | 
224  | 0  |   throw IEX_NAMESPACE::ArgExc ("Cannot set seconds field in time code. " | 
225  | 0  |          "New value is out of range.");  | 
226  |  |  | 
227  | 0  |     setBitField (_time, 8, 14, binaryToBcd (value));  | 
228  | 0  | }  | 
229  |  |  | 
230  |  |  | 
231  |  | int  | 
232  |  | TimeCode::frame () const  | 
233  | 0  | { | 
234  | 0  |     return bcdToBinary (bitField (_time, 0, 5));  | 
235  | 0  | }  | 
236  |  |  | 
237  |  |  | 
238  |  | void  | 
239  |  | TimeCode::setFrame (int value)  | 
240  | 0  | { | 
241  | 0  |     if (value < 0 || value > 59)  | 
242  | 0  |   throw IEX_NAMESPACE::ArgExc ("Cannot set frame field in time code. " | 
243  | 0  |          "New value is out of range.");  | 
244  |  |  | 
245  | 0  |     setBitField (_time, 0, 5, binaryToBcd (value));  | 
246  | 0  | }  | 
247  |  |  | 
248  |  |  | 
249  |  | bool  | 
250  |  | TimeCode::dropFrame () const  | 
251  | 0  | { | 
252  | 0  |     return !!bitField (_time, 6, 6);  | 
253  | 0  | }  | 
254  |  |  | 
255  |  |  | 
256  |  | void  | 
257  |  | TimeCode::setDropFrame (bool value)  | 
258  | 0  | { | 
259  | 0  |     setBitField (_time, 6, 6, (unsigned int) !!value);  | 
260  | 0  | }  | 
261  |  |  | 
262  |  |  | 
263  |  | bool  | 
264  |  | TimeCode::colorFrame () const  | 
265  | 0  | { | 
266  | 0  |     return !!bitField (_time, 7, 7);  | 
267  | 0  | }  | 
268  |  |  | 
269  |  |  | 
270  |  | void  | 
271  |  | TimeCode::setColorFrame (bool value)  | 
272  | 0  | { | 
273  | 0  |     setBitField (_time, 7, 7, (unsigned int) !!value);  | 
274  | 0  | }  | 
275  |  |  | 
276  |  |  | 
277  |  | bool  | 
278  |  | TimeCode::fieldPhase () const  | 
279  | 0  | { | 
280  | 0  |     return !!bitField (_time, 15, 15);  | 
281  | 0  | }  | 
282  |  |  | 
283  |  |  | 
284  |  | void  | 
285  |  | TimeCode::setFieldPhase (bool value)  | 
286  | 0  | { | 
287  | 0  |     setBitField (_time, 15, 15, (unsigned int) !!value);  | 
288  | 0  | }  | 
289  |  |  | 
290  |  |  | 
291  |  | bool  | 
292  |  | TimeCode::bgf0 () const  | 
293  | 0  | { | 
294  | 0  |     return !!bitField (_time, 23, 23);  | 
295  | 0  | }  | 
296  |  |  | 
297  |  |  | 
298  |  | void  | 
299  |  | TimeCode::setBgf0 (bool value)  | 
300  | 0  | { | 
301  | 0  |     setBitField (_time, 23, 23, (unsigned int) !!value);  | 
302  | 0  | }  | 
303  |  |  | 
304  |  |  | 
305  |  | bool  | 
306  |  | TimeCode::bgf1 () const  | 
307  | 0  | { | 
308  | 0  |     return!!bitField (_time, 30, 30);  | 
309  | 0  | }  | 
310  |  |  | 
311  |  |  | 
312  |  | void  | 
313  |  | TimeCode::setBgf1 (bool value)  | 
314  | 0  | { | 
315  | 0  |     setBitField (_time, 30, 30, (unsigned int) !!value);  | 
316  | 0  | }  | 
317  |  |  | 
318  |  |  | 
319  |  | bool  | 
320  |  | TimeCode::bgf2 () const  | 
321  | 0  | { | 
322  | 0  |     return !!bitField (_time, 31, 31);  | 
323  | 0  | }  | 
324  |  |  | 
325  |  |  | 
326  |  | void  | 
327  |  | TimeCode::setBgf2 (bool value)  | 
328  | 0  | { | 
329  | 0  |     setBitField (_time, 31, 31, (unsigned int) !!value);  | 
330  | 0  | }  | 
331  |  |  | 
332  |  |  | 
333  |  | int  | 
334  |  | TimeCode::binaryGroup (int group) const  | 
335  | 0  | { | 
336  | 0  |     if (group < 1 || group > 8)  | 
337  | 0  |   throw IEX_NAMESPACE::ArgExc ("Cannot extract binary group from time code " | 
338  | 0  |                "user data.  Group number is out of range.");  | 
339  |  |  | 
340  | 0  |     int minBit = 4 * (group - 1);  | 
341  | 0  |     int maxBit = minBit + 3;  | 
342  | 0  |     return int (bitField (_user, minBit, maxBit));  | 
343  | 0  | }  | 
344  |  |  | 
345  |  |  | 
346  |  | void  | 
347  |  | TimeCode::setBinaryGroup (int group, int value)  | 
348  | 0  | { | 
349  | 0  |     if (group < 1 || group > 8)  | 
350  | 0  |   throw IEX_NAMESPACE::ArgExc ("Cannot extract binary group from time code " | 
351  | 0  |                "user data.  Group number is out of range.");  | 
352  |  |  | 
353  | 0  |     int minBit = 4 * (group - 1);  | 
354  | 0  |     int maxBit = minBit + 3;  | 
355  | 0  |     setBitField (_user, minBit, maxBit, (unsigned int) value);  | 
356  | 0  | }  | 
357  |  |  | 
358  |  |  | 
359  |  | unsigned int  | 
360  |  | TimeCode::timeAndFlags (Packing packing) const  | 
361  | 0  | { | 
362  | 0  |     if (packing == TV50_PACKING)  | 
363  | 0  |     { | 
364  | 0  |   unsigned int t = _time;  | 
365  |  | 
  | 
366  | 0  |   t &= ~((1 << 6) | (1 << 15) | (1 << 23) | (1 << 30) | (1 << 31));  | 
367  |  | 
  | 
368  | 0  |   t |= ((unsigned int) bgf0() << 15);  | 
369  | 0  |   t |= ((unsigned int) bgf2() << 23);  | 
370  | 0  |   t |= ((unsigned int) bgf1() << 30);  | 
371  | 0  |   t |= ((unsigned int) fieldPhase() << 31);  | 
372  |  | 
  | 
373  | 0  |   return t;  | 
374  | 0  |     }  | 
375  | 0  |     if (packing == FILM24_PACKING)  | 
376  | 0  |     { | 
377  | 0  |   return _time & ~((1 << 6) | (1 << 7));  | 
378  | 0  |     }  | 
379  | 0  |     else // packing == TV60_PACKING  | 
380  | 0  |     { | 
381  | 0  |   return _time;  | 
382  | 0  |     }  | 
383  | 0  | }  | 
384  |  |  | 
385  |  |  | 
386  |  | void  | 
387  |  | TimeCode::setTimeAndFlags (unsigned int value, Packing packing)  | 
388  | 0  | { | 
389  | 0  |     if (packing == TV50_PACKING)  | 
390  | 0  |     { | 
391  | 0  |   _time = value &  | 
392  | 0  |      ~((1 << 6) | (1 << 15) | (1 << 23) | (1 << 30) | (1 << 31));  | 
393  |  | 
  | 
394  | 0  |   if (value & (1 << 15))  | 
395  | 0  |       setBgf0 (true);  | 
396  |  | 
  | 
397  | 0  |   if (value & (1 << 23))  | 
398  | 0  |       setBgf2 (true);  | 
399  |  | 
  | 
400  | 0  |   if (value & (1 << 30))  | 
401  | 0  |       setBgf1 (true);  | 
402  |  | 
  | 
403  | 0  |   if (value & (1 << 31))  | 
404  | 0  |       setFieldPhase (true);  | 
405  | 0  |     }  | 
406  | 0  |     else if (packing == FILM24_PACKING)  | 
407  | 0  |     { | 
408  | 0  |   _time = value & ~((1 << 6) | (1 << 7));  | 
409  | 0  |     }  | 
410  | 0  |     else // packing == TV60_PACKING  | 
411  | 0  |     { | 
412  | 0  |   _time = value;  | 
413  | 0  |     }  | 
414  | 0  | }  | 
415  |  |  | 
416  |  |  | 
417  |  | unsigned int  | 
418  |  | TimeCode::userData () const  | 
419  | 0  | { | 
420  | 0  |     return _user;  | 
421  | 0  | }  | 
422  |  |  | 
423  |  |  | 
424  |  | void  | 
425  |  | TimeCode::setUserData (unsigned int value)  | 
426  | 0  | { | 
427  | 0  |     _user = value;  | 
428  | 0  | }  | 
429  |  |  | 
430  |  |  | 
431  |  | OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_EXIT  |