pupyC3D.decoder

  1# pupyC3D (c) by Antoine MARIN antoine.marin@univ-rennes2.fr
  2#
  3# pupyC3D is licensed under a
  4# Creative Commons Attribution-NonCommercial 4.0 International License.
  5#
  6# You should have received a copy of the license along with this
  7# work. If not, see <https://creativecommons.org/licenses/by-nc/4.0/>.
  8
  9import struct as _struct
 10
 11# Processor type constants for C3D file format
 12PROCESSOR_INTEL = 84
 13PROCESSOR_DEC = 85
 14PROCESSOR_MIPS = 86
 15
 16
 17class ProcStream(object):
 18    """Binary stream processor for reading and writing C3D data.
 19    
 20    Provides methods for reading/writing various data types from/to
 21    binary streams with proper byte ordering and encoding.
 22    
 23    Attributes:
 24        handle: File handle for binary I/O operations
 25    """
 26
 27    def __init__(self, handle):
 28        """Initialize stream processor.
 29        
 30        Args:
 31            handle: Binary file handle (opened in 'rb' or 'wb' mode)
 32        """
 33        self.handle = handle
 34
 35    def close_handle(self):
 36        """Close the file handle."""
 37        self.handle.close()
 38
 39    def get_int8(self):
 40        """Read signed 8-bit integer.
 41        
 42        Returns:
 43            int: Signed 8-bit integer value
 44        """
 45        return _struct.unpack('b', self.handle.read(1))[0]
 46
 47    def get_uint8(self):
 48        """Read unsigned 8-bit integer.
 49        
 50        Returns:
 51            int: Unsigned 8-bit integer value
 52        """
 53        return _struct.unpack('B', self.handle.read(1))[0]
 54
 55    def get_int16(self):
 56        return _struct.unpack('h', self.handle.read(2))[0]
 57
 58    def get_uint16(self):
 59        return _struct.unpack('H', self.handle.read(2))[0]
 60
 61    def get_int32(self):
 62        return _struct.unpack('i', self.handle.read(4))[0]
 63
 64    def get_uint32(self):
 65        return _struct.unpack('I', self.handle.read(4))[0]
 66
 67    def get_float(self):
 68        """Read 32-bit floating point value.
 69        
 70        Returns:
 71            float: 32-bit float value
 72        """
 73        return _struct.unpack('f', self.handle.read(4))[0]
 74
 75    def get_string(self, numChar):
 76        """Read string of specified length.
 77        
 78        Args:
 79            numChar (int): Number of characters to read
 80            
 81        Returns:
 82            str: Decoded string using latin1 encoding
 83        """
 84        return self.handle.read(numChar).decode('latin1')
 85
 86    def write_int8(self, data):
 87        val = _struct.pack('b', data)
 88        self.handle.write(val)
 89
 90    def write_uint8(self, data):
 91        val = _struct.pack('B', data)
 92        self.handle.write(val)
 93
 94    def write_uint16(self, data):
 95        val = _struct.pack('H', data)
 96        self.handle.write(val)
 97
 98    def write_float(self, data):
 99        val = _struct.pack('f', data)
100        self.handle.write(val)
101
102    def write_string(self, data):
103        """Write string to binary stream.
104        
105        Args:
106            data (str): String to write, encoded as ASCII
107        """
108        self.handle.write(data.encode('ascii'))
109
110
111class DecoderIntel(ProcStream):
112    """Intel processor-specific decoder for C3D files.
113    
114    Uses little-endian byte ordering (Intel/x86 format).
115    """
116
117    def __init__(self, handle):
118        """Initialize Intel decoder.
119        
120        Args:
121            handle: Binary file handle
122        """
123        super(DecoderIntel, self).__init__(handle)
124
125
126class DecoderDec(ProcStream):
127    """DEC processor-specific decoder for C3D files.
128    
129    Uses DEC-specific floating point format with special handling.
130    """
131
132    def __init__(self, handle):
133        """Initialize DEC decoder.
134        
135        Args:
136            handle: Binary file handle
137        """
138        super(DecoderDec, self).__init__(handle)
139
140    def get_float(self):
141        """Read DEC-format floating point value.
142        
143        DEC format requires byte swapping and scaling by 1/4.
144        
145        Returns:
146            float: Converted DEC float value
147        """
148        tmp = self.handle.read(4)
149        tmp = tmp[2:] + tmp[:2]
150        val = _struct.unpack('f', tmp)[0]
151        return val/4.
152
153
154class DecoderMips(ProcStream):
155    """MIPS processor-specific decoder for C3D files.
156    
157    Uses big-endian byte ordering (MIPS format).
158    """
159
160    def __init__(self, handle):
161        """Initialize MIPS decoder.
162        
163        Args:
164            handle: Binary file handle
165        """
166        super(DecoderMips, self).__init__(handle)
167
168    def get_int8(self):
169        """Read signed 8-bit integer with big-endian byte order.
170        
171        Returns:
172            int: Signed 8-bit integer value
173        """
174        return _struct.unpack('>b', self.handle.read(1))[0]
175
176    def get_uint8(self):
177        return _struct.unpack('>B', self.handle.read(1))[0]
178
179    def get_uint16(self):
180        return _struct.unpack('>H', self.handle.read(2))[0]
181
182    def get_float(self):
183        """Read 32-bit float with big-endian byte order.
184        
185        Returns:
186            float: 32-bit float value
187        """
188        return _struct.unpack('>f', self.handle.read(4))[0]
PROCESSOR_INTEL = 84
PROCESSOR_DEC = 85
PROCESSOR_MIPS = 86
class ProcStream:
 18class ProcStream(object):
 19    """Binary stream processor for reading and writing C3D data.
 20    
 21    Provides methods for reading/writing various data types from/to
 22    binary streams with proper byte ordering and encoding.
 23    
 24    Attributes:
 25        handle: File handle for binary I/O operations
 26    """
 27
 28    def __init__(self, handle):
 29        """Initialize stream processor.
 30        
 31        Args:
 32            handle: Binary file handle (opened in 'rb' or 'wb' mode)
 33        """
 34        self.handle = handle
 35
 36    def close_handle(self):
 37        """Close the file handle."""
 38        self.handle.close()
 39
 40    def get_int8(self):
 41        """Read signed 8-bit integer.
 42        
 43        Returns:
 44            int: Signed 8-bit integer value
 45        """
 46        return _struct.unpack('b', self.handle.read(1))[0]
 47
 48    def get_uint8(self):
 49        """Read unsigned 8-bit integer.
 50        
 51        Returns:
 52            int: Unsigned 8-bit integer value
 53        """
 54        return _struct.unpack('B', self.handle.read(1))[0]
 55
 56    def get_int16(self):
 57        return _struct.unpack('h', self.handle.read(2))[0]
 58
 59    def get_uint16(self):
 60        return _struct.unpack('H', self.handle.read(2))[0]
 61
 62    def get_int32(self):
 63        return _struct.unpack('i', self.handle.read(4))[0]
 64
 65    def get_uint32(self):
 66        return _struct.unpack('I', self.handle.read(4))[0]
 67
 68    def get_float(self):
 69        """Read 32-bit floating point value.
 70        
 71        Returns:
 72            float: 32-bit float value
 73        """
 74        return _struct.unpack('f', self.handle.read(4))[0]
 75
 76    def get_string(self, numChar):
 77        """Read string of specified length.
 78        
 79        Args:
 80            numChar (int): Number of characters to read
 81            
 82        Returns:
 83            str: Decoded string using latin1 encoding
 84        """
 85        return self.handle.read(numChar).decode('latin1')
 86
 87    def write_int8(self, data):
 88        val = _struct.pack('b', data)
 89        self.handle.write(val)
 90
 91    def write_uint8(self, data):
 92        val = _struct.pack('B', data)
 93        self.handle.write(val)
 94
 95    def write_uint16(self, data):
 96        val = _struct.pack('H', data)
 97        self.handle.write(val)
 98
 99    def write_float(self, data):
100        val = _struct.pack('f', data)
101        self.handle.write(val)
102
103    def write_string(self, data):
104        """Write string to binary stream.
105        
106        Args:
107            data (str): String to write, encoded as ASCII
108        """
109        self.handle.write(data.encode('ascii'))

Binary stream processor for reading and writing C3D data.

Provides methods for reading/writing various data types from/to binary streams with proper byte ordering and encoding.

Attributes: handle: File handle for binary I/O operations

ProcStream(handle)
28    def __init__(self, handle):
29        """Initialize stream processor.
30        
31        Args:
32            handle: Binary file handle (opened in 'rb' or 'wb' mode)
33        """
34        self.handle = handle

Initialize stream processor.

Args: handle: Binary file handle (opened in 'rb' or 'wb' mode)

handle
def close_handle(self):
36    def close_handle(self):
37        """Close the file handle."""
38        self.handle.close()

Close the file handle.

def get_int8(self):
40    def get_int8(self):
41        """Read signed 8-bit integer.
42        
43        Returns:
44            int: Signed 8-bit integer value
45        """
46        return _struct.unpack('b', self.handle.read(1))[0]

Read signed 8-bit integer.

Returns: int: Signed 8-bit integer value

def get_uint8(self):
48    def get_uint8(self):
49        """Read unsigned 8-bit integer.
50        
51        Returns:
52            int: Unsigned 8-bit integer value
53        """
54        return _struct.unpack('B', self.handle.read(1))[0]

Read unsigned 8-bit integer.

Returns: int: Unsigned 8-bit integer value

def get_int16(self):
56    def get_int16(self):
57        return _struct.unpack('h', self.handle.read(2))[0]
def get_uint16(self):
59    def get_uint16(self):
60        return _struct.unpack('H', self.handle.read(2))[0]
def get_int32(self):
62    def get_int32(self):
63        return _struct.unpack('i', self.handle.read(4))[0]
def get_uint32(self):
65    def get_uint32(self):
66        return _struct.unpack('I', self.handle.read(4))[0]
def get_float(self):
68    def get_float(self):
69        """Read 32-bit floating point value.
70        
71        Returns:
72            float: 32-bit float value
73        """
74        return _struct.unpack('f', self.handle.read(4))[0]

Read 32-bit floating point value.

Returns: float: 32-bit float value

def get_string(self, numChar):
76    def get_string(self, numChar):
77        """Read string of specified length.
78        
79        Args:
80            numChar (int): Number of characters to read
81            
82        Returns:
83            str: Decoded string using latin1 encoding
84        """
85        return self.handle.read(numChar).decode('latin1')

Read string of specified length.

Args: numChar (int): Number of characters to read

Returns: str: Decoded string using latin1 encoding

def write_int8(self, data):
87    def write_int8(self, data):
88        val = _struct.pack('b', data)
89        self.handle.write(val)
def write_uint8(self, data):
91    def write_uint8(self, data):
92        val = _struct.pack('B', data)
93        self.handle.write(val)
def write_uint16(self, data):
95    def write_uint16(self, data):
96        val = _struct.pack('H', data)
97        self.handle.write(val)
def write_float(self, data):
 99    def write_float(self, data):
100        val = _struct.pack('f', data)
101        self.handle.write(val)
def write_string(self, data):
103    def write_string(self, data):
104        """Write string to binary stream.
105        
106        Args:
107            data (str): String to write, encoded as ASCII
108        """
109        self.handle.write(data.encode('ascii'))

Write string to binary stream.

Args: data (str): String to write, encoded as ASCII

class DecoderIntel(ProcStream):
112class DecoderIntel(ProcStream):
113    """Intel processor-specific decoder for C3D files.
114    
115    Uses little-endian byte ordering (Intel/x86 format).
116    """
117
118    def __init__(self, handle):
119        """Initialize Intel decoder.
120        
121        Args:
122            handle: Binary file handle
123        """
124        super(DecoderIntel, self).__init__(handle)

Intel processor-specific decoder for C3D files.

Uses little-endian byte ordering (Intel/x86 format).

DecoderIntel(handle)
118    def __init__(self, handle):
119        """Initialize Intel decoder.
120        
121        Args:
122            handle: Binary file handle
123        """
124        super(DecoderIntel, self).__init__(handle)

Initialize Intel decoder.

Args: handle: Binary file handle

class DecoderDec(ProcStream):
127class DecoderDec(ProcStream):
128    """DEC processor-specific decoder for C3D files.
129    
130    Uses DEC-specific floating point format with special handling.
131    """
132
133    def __init__(self, handle):
134        """Initialize DEC decoder.
135        
136        Args:
137            handle: Binary file handle
138        """
139        super(DecoderDec, self).__init__(handle)
140
141    def get_float(self):
142        """Read DEC-format floating point value.
143        
144        DEC format requires byte swapping and scaling by 1/4.
145        
146        Returns:
147            float: Converted DEC float value
148        """
149        tmp = self.handle.read(4)
150        tmp = tmp[2:] + tmp[:2]
151        val = _struct.unpack('f', tmp)[0]
152        return val/4.

DEC processor-specific decoder for C3D files.

Uses DEC-specific floating point format with special handling.

DecoderDec(handle)
133    def __init__(self, handle):
134        """Initialize DEC decoder.
135        
136        Args:
137            handle: Binary file handle
138        """
139        super(DecoderDec, self).__init__(handle)

Initialize DEC decoder.

Args: handle: Binary file handle

def get_float(self):
141    def get_float(self):
142        """Read DEC-format floating point value.
143        
144        DEC format requires byte swapping and scaling by 1/4.
145        
146        Returns:
147            float: Converted DEC float value
148        """
149        tmp = self.handle.read(4)
150        tmp = tmp[2:] + tmp[:2]
151        val = _struct.unpack('f', tmp)[0]
152        return val/4.

Read DEC-format floating point value.

DEC format requires byte swapping and scaling by 1/4.

Returns: float: Converted DEC float value

class DecoderMips(ProcStream):
155class DecoderMips(ProcStream):
156    """MIPS processor-specific decoder for C3D files.
157    
158    Uses big-endian byte ordering (MIPS format).
159    """
160
161    def __init__(self, handle):
162        """Initialize MIPS decoder.
163        
164        Args:
165            handle: Binary file handle
166        """
167        super(DecoderMips, self).__init__(handle)
168
169    def get_int8(self):
170        """Read signed 8-bit integer with big-endian byte order.
171        
172        Returns:
173            int: Signed 8-bit integer value
174        """
175        return _struct.unpack('>b', self.handle.read(1))[0]
176
177    def get_uint8(self):
178        return _struct.unpack('>B', self.handle.read(1))[0]
179
180    def get_uint16(self):
181        return _struct.unpack('>H', self.handle.read(2))[0]
182
183    def get_float(self):
184        """Read 32-bit float with big-endian byte order.
185        
186        Returns:
187            float: 32-bit float value
188        """
189        return _struct.unpack('>f', self.handle.read(4))[0]

MIPS processor-specific decoder for C3D files.

Uses big-endian byte ordering (MIPS format).

DecoderMips(handle)
161    def __init__(self, handle):
162        """Initialize MIPS decoder.
163        
164        Args:
165            handle: Binary file handle
166        """
167        super(DecoderMips, self).__init__(handle)

Initialize MIPS decoder.

Args: handle: Binary file handle

def get_int8(self):
169    def get_int8(self):
170        """Read signed 8-bit integer with big-endian byte order.
171        
172        Returns:
173            int: Signed 8-bit integer value
174        """
175        return _struct.unpack('>b', self.handle.read(1))[0]

Read signed 8-bit integer with big-endian byte order.

Returns: int: Signed 8-bit integer value

def get_uint8(self):
177    def get_uint8(self):
178        return _struct.unpack('>B', self.handle.read(1))[0]

Read unsigned 8-bit integer.

Returns: int: Unsigned 8-bit integer value

def get_uint16(self):
180    def get_uint16(self):
181        return _struct.unpack('>H', self.handle.read(2))[0]
def get_float(self):
183    def get_float(self):
184        """Read 32-bit float with big-endian byte order.
185        
186        Returns:
187            float: 32-bit float value
188        """
189        return _struct.unpack('>f', self.handle.read(4))[0]

Read 32-bit float with big-endian byte order.

Returns: float: 32-bit float value