1 ///////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2013 Academy of Motion Picture Arts and Sciences
3 // ("A.M.P.A.S."). Portions contributed by others as indicated.
4 // All rights reserved.
5 //
6 // A worldwide, royalty-free, non-exclusive right to copy, modify, create
7 // derivatives, and use, in source and binary forms, is hereby granted,
8 // subject to acceptance of this license. Performance of any of the
9 // aforementioned acts indicates acceptance to be bound by the following
10 // terms and conditions:
11 //
12 //  * Copies of source code, in whole or in part, must retain the
13 //    above copyright notice, this list of conditions and the
14 //    Disclaimer of Warranty.
15 //
16 //  * Use in binary form must retain the above copyright notice,
17 //    this list of conditions and the Disclaimer of Warranty in the
18 //    documentation and/or other materials provided with the distribution.
19 //
20 //  * Nothing in this license shall be deemed to grant any rights to
21 //    trademarks, copyrights, patents, trade secrets or any other
22 //    intellectual property of A.M.P.A.S. or any contributors, except
23 //    as expressly stated herein.
24 //
25 //  * Neither the name "A.M.P.A.S." nor the name of any other
26 //    contributors to this software may be used to endorse or promote
27 //    products derivative of or based on this software without express
28 //    prior written permission of A.M.P.A.S. or the contributors, as
29 //    appropriate.
30 //
31 // This license shall be construed pursuant to the laws of the State of
32 // California, and any disputes related thereto shall be subject to the
33 // jurisdiction of the courts therein.
34 //
35 // Disclaimer of Warranty: THIS SOFTWARE IS PROVIDED BY A.M.P.A.S. AND
36 // CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
37 // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
38 // FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT ARE DISCLAIMED. IN NO
39 // EVENT SHALL A.M.P.A.S., OR ANY CONTRIBUTORS OR DISTRIBUTORS, BE LIABLE
40 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, RESITUTIONARY,
41 // OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
42 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
43 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
44 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
46 // THE POSSIBILITY OF SUCH DAMAGE.
47 //
48 // WITHOUT LIMITING THE GENERALITY OF THE FOREGOING, THE ACADEMY
49 // SPECIFICALLY DISCLAIMS ANY REPRESENTATIONS OR WARRANTIES WHATSOEVER
50 // RELATED TO PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS IN THE ACADEMY
51 // COLOR ENCODING SYSTEM, OR APPLICATIONS THEREOF, HELD BY PARTIES OTHER
52 // THAN A.M.P.A.S., WHETHER DISCLOSED OR UNDISCLOSED.
53 ///////////////////////////////////////////////////////////////////////////
54 
55 #if !defined(CTL_DPX_RAW_INTERNAL_INCLUDE)
56 #define CRL_DPX_RAW_INTERNAL_INCLUDE
57 
58 namespace ctl {
59 
60 namespace dpxi {
61 
swap64(void * _s)62 inline void swap64(void *_s) {
63 	uint64_t s=*((uint64_t*)_s);
64 
65 	s=(((s&0xffffffff00000000LL)>>32) | ((s&0x00000000ffffffffLL)<<32));
66 	s=(((s&0xffff0000ffff0000LL)>>16) | ((s&0x0000ffff0000ffffLL)<<16));
67 	s=(((s&0xff00ff00ff00ff00LL)>>8)  | ((s&0x00ff00ff00ff00ffLL)<<8));
68 
69 	*((uint64_t*)_s)=s;
70 }
71 
swap32(void * _s)72 inline void swap32(void *_s) {
73 	uint32_t s=*((uint32_t*)_s);
74 
75 	s=(((s&0xffff0000)>>16) | ((s&0x0000ffff)<<16));
76 	s=(((s&0xff00ff00)>>8)  | ((s&0x00ff00ff)<<8));
77 
78 	*((uint32_t*)_s)=s;
79 }
80 
swap16(void * _s)81 inline void swap16(void *_s) {
82 	uint16_t s=*((uint16_t*)_s);
83 
84 	s=(((s&0xff00)>>8)  | ((s&0x00ff)<<8));
85 
86 	*((uint16_t*)_s)=s;
87 }
88 
89 template <class T>
read_ptr(std::istream * i,T * data,uint64_t count,bool swap)90 void read_ptr(std::istream *i, T *data, uint64_t count, bool swap) {
91 	uint64_t u;
92 	T *s;
93 
94 	s=data;
95 
96 	i->read((char *)s, sizeof(T)*count);
97 
98 	if(swap && sizeof(T)==2) {
99 		for(u=0; u<count; u++) {
100 			swap16(s++);
101 		}
102 	} else if(swap && sizeof(T)==4) {
103 		for(u=0; u<count; u++) {
104 			swap32(s++);
105 		}
106 	} else if(swap && sizeof(T)==8) {
107 		for(u=0; u<count; u++) {
108 			swap64(s++);
109 		}
110 	}
111 }
112 
113 template <class T>
write_ptr(std::ostream * o,const T * _data,uint64_t count,bool swap)114 void write_ptr(std::ostream *o, const T *_data, uint64_t count, bool swap) {
115 	uint64_t u;
116 	dpx::fb<uint8_t> data;
117 	T *s;
118 
119 	data.init(count*sizeof(T), 1, 1);
120 
121 	if(swap && sizeof(T)!=1) {
122 		data.init(count*sizeof(T), 1, 1);
123 		memcpy(data, _data, count*sizeof(T));
124 		s=(T *)data.ptr();
125 		switch(sizeof(T)) {
126 			case 2:
127 				for(u=0; u<count; u++) {
128 					swap16(s++);
129 				}
130 				break;
131 
132 			case 4:
133 				for(u=0; u<count; u++) {
134 					swap32(s++);
135 				}
136 				break;
137 
138 			case 8:
139 				for(u=0; u<count; u++) {
140 					swap64(s++);
141 				}
142 				break;
143 			case 1:
144 				break;
145 		}
146 		_data=(const T *)data.ptr();
147 	}
148 
149 	o->write((const char *)_data, count*sizeof(T));
150 }
151 
152 uint8_t read_uint8(std::istream *is, bool need_byteswap);
153 uint16_t read_uint16(std::istream *is, bool need_byteswap);
154 uint32_t read_uint32(std::istream *is, bool need_byteswap);
155 float32_t read_float32(std::istream *is, bool need_byteswap);
156 void read_string(std::istream *is, char *bytes, int len_plus_one);
157 
158 void write_uint8(std::ostream *os, uint8_t i, bool need_byteswap);
159 void write_uint16(std::ostream *os, uint16_t i, bool need_byteswap);
160 void write_uint32(std::ostream *os, uint32_t i, bool need_byteswap);
161 void write_float32(std::ostream *os, float32_t f, bool need_byteswap);
162 void write_string(std::ostream *os, char *bytes, int len_plus_one);
163 void write_fill(std::ostream *os, char b, int count);
164 
165 }
166 
167 }
168 
169 #endif
170