Ejemplo n.º 1
0
        public static void WriteMapFloat(string path, MapHeader header, float[][] data)
        {
            Type ValueType = header.GetValueType();

            using (BinaryWriter Writer = new BinaryWriter(CreateWithBigBuffer(path)))
            {
                header.Write(Writer);
                long Elements = header.Dimensions.ElementsSlice();

                for (int z = 0; z < data.Length; z++)
                {
                    byte[] Bytes = new byte[Elements * ImageFormatsHelper.SizeOf(ValueType)];

                    unsafe
                    {
                        fixed (float* DataPtr = data[z])
                        fixed (byte* BytesPtr = Bytes)
                        {
                            float* DataP = DataPtr;

                            if (ValueType == typeof(byte))
                            {
                                byte* BytesP = BytesPtr;
                                for (long i = 0; i < Elements; i++)
                                    *BytesP++ = (byte)*DataP++;
                            }
                            else if (ValueType == typeof(short))
                            {
                                short* BytesP = (short*)BytesPtr;
                                for (long i = 0; i < Elements; i++)
                                    *BytesP++ = (short)*DataP++;
                            }
                            else if (ValueType == typeof(ushort))
                            {
                                ushort* BytesP = (ushort*)BytesPtr;
                                for (long i = 0; i < Elements; i++)
                                    *BytesP++ = (ushort)Math.Min(Math.Max(0f, *DataP++ * 16f), 65536f);
                            }
                            else if (ValueType == typeof(int))
                            {
                                int* BytesP = (int*)BytesPtr;
                                for (long i = 0; i < Elements; i++)
                                    *BytesP++ = (int)*DataP++;
                            }
                            else if (ValueType == typeof(float))
                            {
                                float* BytesP = (float*)BytesPtr;
                                for (long i = 0; i < Elements; i++)
                                    *BytesP++ = *DataP++;
                            }
                            else if (ValueType == typeof(double))
                            {
                                double* BytesP = (double*)BytesPtr;
                                for (long i = 0; i < Elements; i++)
                                    *BytesP++ = (double)*DataP++;
                            }
                        }
                    }

                    Writer.Write(Bytes);
                }
            }
        }