Esempio n. 1
0
        public override byte[] Serialize(bool partofsomethingelse)
        {
            int  currentIndex = 0, length = 0;
            bool hasmetacomponents = false;

            byte[]        thischunk, scratch1, scratch2;
            List <byte[]> pieces = new List <byte[]>();
            GCHandle      h;

            //header
            if (header == null)
            {
                header = new Header();
            }
            pieces.Add(header.Serialize(true));
            //height
            scratch1 = new byte[Marshal.SizeOf(typeof(uint))];
            h        = GCHandle.Alloc(scratch1, GCHandleType.Pinned);
            Marshal.StructureToPtr(height, h.AddrOfPinnedObject(), false);
            h.Free();
            pieces.Add(scratch1);
            //width
            scratch1 = new byte[Marshal.SizeOf(typeof(uint))];
            h        = GCHandle.Alloc(scratch1, GCHandleType.Pinned);
            Marshal.StructureToPtr(width, h.AddrOfPinnedObject(), false);
            h.Free();
            pieces.Add(scratch1);
            //distortion_model
            if (distortion_model == null)
            {
                distortion_model = "";
            }
            scratch1  = Encoding.ASCII.GetBytes((string)distortion_model);
            thischunk = new byte[scratch1.Length + 4];
            scratch2  = BitConverter.GetBytes(scratch1.Length);
            Array.Copy(scratch1, 0, thischunk, 4, scratch1.Length);
            Array.Copy(scratch2, thischunk, 4);
            pieces.Add(thischunk);
            //D
            hasmetacomponents |= false;
            if (D == null)
            {
                D = new double[0];
            }
            pieces.Add(BitConverter.GetBytes(D.Length));
            for (int i = 0; i < D.Length; i++)
            {
                //D[i]
                scratch1 = new byte[Marshal.SizeOf(typeof(double))];
                h        = GCHandle.Alloc(scratch1, GCHandleType.Pinned);
                Marshal.StructureToPtr(D[i], h.AddrOfPinnedObject(), false);
                h.Free();
                pieces.Add(scratch1);
            }
            //K
            hasmetacomponents |= false;
            if (K == null)
            {
                K = new double[0];
            }
            for (int i = 0; i < K.Length; i++)
            {
                //K[i]
                scratch1 = new byte[Marshal.SizeOf(typeof(double))];
                h        = GCHandle.Alloc(scratch1, GCHandleType.Pinned);
                Marshal.StructureToPtr(K[i], h.AddrOfPinnedObject(), false);
                h.Free();
                pieces.Add(scratch1);
            }
            //R
            hasmetacomponents |= false;
            if (R == null)
            {
                R = new double[0];
            }
            for (int i = 0; i < R.Length; i++)
            {
                //R[i]
                scratch1 = new byte[Marshal.SizeOf(typeof(double))];
                h        = GCHandle.Alloc(scratch1, GCHandleType.Pinned);
                Marshal.StructureToPtr(R[i], h.AddrOfPinnedObject(), false);
                h.Free();
                pieces.Add(scratch1);
            }
            //P
            hasmetacomponents |= false;
            if (P == null)
            {
                P = new double[0];
            }
            for (int i = 0; i < P.Length; i++)
            {
                //P[i]
                scratch1 = new byte[Marshal.SizeOf(typeof(double))];
                h        = GCHandle.Alloc(scratch1, GCHandleType.Pinned);
                Marshal.StructureToPtr(P[i], h.AddrOfPinnedObject(), false);
                h.Free();
                pieces.Add(scratch1);
            }
            //binning_x
            scratch1 = new byte[Marshal.SizeOf(typeof(uint))];
            h        = GCHandle.Alloc(scratch1, GCHandleType.Pinned);
            Marshal.StructureToPtr(binning_x, h.AddrOfPinnedObject(), false);
            h.Free();
            pieces.Add(scratch1);
            //binning_y
            scratch1 = new byte[Marshal.SizeOf(typeof(uint))];
            h        = GCHandle.Alloc(scratch1, GCHandleType.Pinned);
            Marshal.StructureToPtr(binning_y, h.AddrOfPinnedObject(), false);
            h.Free();
            pieces.Add(scratch1);
            //roi
            if (roi == null)
            {
                roi = new Messages.sensor_msgs.RegionOfInterest();
            }
            pieces.Add(roi.Serialize(true));
            //combine every array in pieces into one array and return it
            int __a_b__f = pieces.Sum((__a_b__c) => __a_b__c.Length);
            int __a_b__e = 0;

            byte[] __a_b__d = new byte[__a_b__f];
            foreach (var __p__ in pieces)
            {
                Array.Copy(__p__, 0, __a_b__d, __a_b__e, __p__.Length);
                __a_b__e += __p__.Length;
            }
            return(__a_b__d);
        }
Esempio n. 2
0
        public override void Randomize()
        {
            int    arraylength = -1;
            Random rand        = new Random();
            int    strlength;

            byte[] strbuf, myByte;

            //header
            header = new Header();
            header.Randomize();
            //height
            height = (uint)rand.Next();
            //width
            width = (uint)rand.Next();
            //distortion_model
            strlength = rand.Next(100) + 1;
            strbuf    = new byte[strlength];
            rand.NextBytes(strbuf);  //fill the whole buffer with random bytes
            for (int __x__ = 0; __x__ < strlength; __x__++)
            {
                if (strbuf[__x__] == 0) //replace null chars with non-null random ones
                {
                    strbuf[__x__] = (byte)(rand.Next(254) + 1);
                }
            }
            strbuf[strlength - 1] = 0; //null terminate
            distortion_model      = Encoding.ASCII.GetString(strbuf);
            //D
            arraylength = rand.Next(10);
            if (D == null)
            {
                D = new double[arraylength];
            }
            else
            {
                Array.Resize(ref D, arraylength);
            }
            for (int i = 0; i < D.Length; i++)
            {
                //D[i]
                D[i] = (rand.Next() + rand.NextDouble());
            }
            //K
            if (K == null)
            {
                K = new double[9];
            }
            else
            {
                Array.Resize(ref K, 9);
            }
            for (int i = 0; i < K.Length; i++)
            {
                //K[i]
                K[i] = (rand.Next() + rand.NextDouble());
            }
            //R
            if (R == null)
            {
                R = new double[9];
            }
            else
            {
                Array.Resize(ref R, 9);
            }
            for (int i = 0; i < R.Length; i++)
            {
                //R[i]
                R[i] = (rand.Next() + rand.NextDouble());
            }
            //P
            if (P == null)
            {
                P = new double[12];
            }
            else
            {
                Array.Resize(ref P, 12);
            }
            for (int i = 0; i < P.Length; i++)
            {
                //P[i]
                P[i] = (rand.Next() + rand.NextDouble());
            }
            //binning_x
            binning_x = (uint)rand.Next();
            //binning_y
            binning_y = (uint)rand.Next();
            //roi
            roi = new Messages.sensor_msgs.RegionOfInterest();
            roi.Randomize();
        }
Esempio n. 3
0
        public override void Deserialize(byte[] SERIALIZEDSTUFF, ref int currentIndex)
        {
            int    arraylength       = -1;
            bool   hasmetacomponents = false;
            object __thing;
            int    piecesize = 0;

            byte[] thischunk, scratch1, scratch2;
            IntPtr h;

            //header
            header = new Header(SERIALIZEDSTUFF, ref currentIndex);
            //height
            piecesize = Marshal.SizeOf(typeof(uint));
            h         = IntPtr.Zero;
            if (SERIALIZEDSTUFF.Length - currentIndex != 0)
            {
                h = Marshal.AllocHGlobal(piecesize);
                Marshal.Copy(SERIALIZEDSTUFF, currentIndex, h, piecesize);
            }
            if (h == IntPtr.Zero)
            {
                throw new Exception("Alloc failed");
            }
            height = (uint)Marshal.PtrToStructure(h, typeof(uint));
            Marshal.FreeHGlobal(h);
            currentIndex += piecesize;
            //width
            piecesize = Marshal.SizeOf(typeof(uint));
            h         = IntPtr.Zero;
            if (SERIALIZEDSTUFF.Length - currentIndex != 0)
            {
                h = Marshal.AllocHGlobal(piecesize);
                Marshal.Copy(SERIALIZEDSTUFF, currentIndex, h, piecesize);
            }
            if (h == IntPtr.Zero)
            {
                throw new Exception("Alloc failed");
            }
            width = (uint)Marshal.PtrToStructure(h, typeof(uint));
            Marshal.FreeHGlobal(h);
            currentIndex += piecesize;
            //distortion_model
            distortion_model = "";
            piecesize        = BitConverter.ToInt32(SERIALIZEDSTUFF, currentIndex);
            currentIndex    += 4;
            distortion_model = Encoding.ASCII.GetString(SERIALIZEDSTUFF, currentIndex, piecesize);
            currentIndex    += piecesize;
            //D
            hasmetacomponents |= false;
            arraylength        = BitConverter.ToInt32(SERIALIZEDSTUFF, currentIndex);
            currentIndex      += Marshal.SizeOf(typeof(System.Int32));
            if (D == null)
            {
                D = new double[arraylength];
            }
            else
            {
                Array.Resize(ref D, arraylength);
            }
            for (int i = 0; i < D.Length; i++)
            {
                //D[i]
                piecesize = Marshal.SizeOf(typeof(double));
                h         = IntPtr.Zero;
                if (SERIALIZEDSTUFF.Length - currentIndex != 0)
                {
                    h = Marshal.AllocHGlobal(piecesize);
                    Marshal.Copy(SERIALIZEDSTUFF, currentIndex, h, piecesize);
                }
                if (h == IntPtr.Zero)
                {
                    throw new Exception("Alloc failed");
                }
                D[i] = (double)Marshal.PtrToStructure(h, typeof(double));
                Marshal.FreeHGlobal(h);
                currentIndex += piecesize;
            }
            //K
            hasmetacomponents |= false;
            if (K == null)
            {
                K = new double[9];
            }
            else
            {
                Array.Resize(ref K, 9);
            }
            for (int i = 0; i < K.Length; i++)
            {
                //K[i]
                piecesize = Marshal.SizeOf(typeof(double));
                h         = IntPtr.Zero;
                if (SERIALIZEDSTUFF.Length - currentIndex != 0)
                {
                    h = Marshal.AllocHGlobal(piecesize);
                    Marshal.Copy(SERIALIZEDSTUFF, currentIndex, h, piecesize);
                }
                if (h == IntPtr.Zero)
                {
                    throw new Exception("Alloc failed");
                }
                K[i] = (double)Marshal.PtrToStructure(h, typeof(double));
                Marshal.FreeHGlobal(h);
                currentIndex += piecesize;
            }
            //R
            hasmetacomponents |= false;
            if (R == null)
            {
                R = new double[9];
            }
            else
            {
                Array.Resize(ref R, 9);
            }
            for (int i = 0; i < R.Length; i++)
            {
                //R[i]
                piecesize = Marshal.SizeOf(typeof(double));
                h         = IntPtr.Zero;
                if (SERIALIZEDSTUFF.Length - currentIndex != 0)
                {
                    h = Marshal.AllocHGlobal(piecesize);
                    Marshal.Copy(SERIALIZEDSTUFF, currentIndex, h, piecesize);
                }
                if (h == IntPtr.Zero)
                {
                    throw new Exception("Alloc failed");
                }
                R[i] = (double)Marshal.PtrToStructure(h, typeof(double));
                Marshal.FreeHGlobal(h);
                currentIndex += piecesize;
            }
            //P
            hasmetacomponents |= false;
            if (P == null)
            {
                P = new double[12];
            }
            else
            {
                Array.Resize(ref P, 12);
            }
            for (int i = 0; i < P.Length; i++)
            {
                //P[i]
                piecesize = Marshal.SizeOf(typeof(double));
                h         = IntPtr.Zero;
                if (SERIALIZEDSTUFF.Length - currentIndex != 0)
                {
                    h = Marshal.AllocHGlobal(piecesize);
                    Marshal.Copy(SERIALIZEDSTUFF, currentIndex, h, piecesize);
                }
                if (h == IntPtr.Zero)
                {
                    throw new Exception("Alloc failed");
                }
                P[i] = (double)Marshal.PtrToStructure(h, typeof(double));
                Marshal.FreeHGlobal(h);
                currentIndex += piecesize;
            }
            //binning_x
            piecesize = Marshal.SizeOf(typeof(uint));
            h         = IntPtr.Zero;
            if (SERIALIZEDSTUFF.Length - currentIndex != 0)
            {
                h = Marshal.AllocHGlobal(piecesize);
                Marshal.Copy(SERIALIZEDSTUFF, currentIndex, h, piecesize);
            }
            if (h == IntPtr.Zero)
            {
                throw new Exception("Alloc failed");
            }
            binning_x = (uint)Marshal.PtrToStructure(h, typeof(uint));
            Marshal.FreeHGlobal(h);
            currentIndex += piecesize;
            //binning_y
            piecesize = Marshal.SizeOf(typeof(uint));
            h         = IntPtr.Zero;
            if (SERIALIZEDSTUFF.Length - currentIndex != 0)
            {
                h = Marshal.AllocHGlobal(piecesize);
                Marshal.Copy(SERIALIZEDSTUFF, currentIndex, h, piecesize);
            }
            if (h == IntPtr.Zero)
            {
                throw new Exception("Alloc failed");
            }
            binning_y = (uint)Marshal.PtrToStructure(h, typeof(uint));
            Marshal.FreeHGlobal(h);
            currentIndex += piecesize;
            //roi
            roi = new Messages.sensor_msgs.RegionOfInterest(SERIALIZEDSTUFF, ref currentIndex);
        }