"""LCM type definitions
This file automatically generated by lcm.
DO NOT MODIFY BY HAND!!!!
"""

try:
    import cStringIO.StringIO as BytesIO
except ImportError:
    from io import BytesIO
import struct

class image_float32(object):
    __slots__ = ["utime", "width", "height", "image_size", "image"]

    __typenames__ = ["int64_t", "int32_t", "int32_t", "int32_t", "float"]

    __dimensions__ = [None, None, None, None, ["image_size"]]

    def __init__(self):
        self.utime = 0
        self.width = 0
        self.height = 0
        self.image_size = 0
        self.image = []

    def encode(self):
        buf = BytesIO()
        buf.write(image_float32._get_packed_fingerprint())
        self._encode_one(buf)
        return buf.getvalue()

    def _encode_one(self, buf):
        buf.write(struct.pack(">qiii", self.utime, self.width, self.height, self.image_size))
        buf.write(struct.pack('>%df' % self.image_size, *self.image[:self.image_size]))

    def decode(data):
        if hasattr(data, 'read'):
            buf = data
        else:
            buf = BytesIO(data)
        if buf.read(8) != image_float32._get_packed_fingerprint():
            raise ValueError("Decode error")
        return image_float32._decode_one(buf)
    decode = staticmethod(decode)

    def _decode_one(buf):
        self = image_float32()
        self.utime, self.width, self.height, self.image_size = struct.unpack(">qiii", buf.read(20))
        self.image = struct.unpack('>%df' % self.image_size, buf.read(self.image_size * 4))
        return self
    _decode_one = staticmethod(_decode_one)

    def _get_hash_recursive(parents):
        if image_float32 in parents: return 0
        tmphash = (0xbff98dd17a1f119e) & 0xffffffffffffffff
        tmphash  = (((tmphash<<1)&0xffffffffffffffff) + (tmphash>>63)) & 0xffffffffffffffff
        return tmphash
    _get_hash_recursive = staticmethod(_get_hash_recursive)
    _packed_fingerprint = None

    def _get_packed_fingerprint():
        if image_float32._packed_fingerprint is None:
            image_float32._packed_fingerprint = struct.pack(">Q", image_float32._get_hash_recursive([]))
        return image_float32._packed_fingerprint
    _get_packed_fingerprint = staticmethod(_get_packed_fingerprint)

    def get_hash(self):
        """Get the LCM hash of the struct"""
        return struct.unpack(">Q", image_float32._get_packed_fingerprint())[0]

