예제 #1
0
 /// <summary>
 /// Return grid corresponding to map's selected layer
 /// </summary>
 /// <param name="targetLayer"></param>
 /// <returns></returns>
 public UInt16[,] getMapLayer(int targetLayer)
 {
     UInt16[,] mapLayer = new UInt16[25, 18];
     UInt16[] targetMapLayer = mapInfoArray[targetLayer];
     for (int i = 0; i < mapLayer.GetLength(0); i++)
     {
         for (int j = 0; j < mapLayer.GetLength(1); j++)
         {
             mapLayer[i, j] = targetMapLayer[j + i * 18];
         }
     }
     return(mapLayer);
 }
예제 #2
0
        /// <summary>
        /// Return grid corresponding to selected room's selected layer.
        /// </summary>
        /// <param name="targetLayer"></param>
        /// <returns></returns>
        public UInt16[,] getRoomLayer(int targetLayer)
        {
            UInt16[,] roomLayer = new UInt16[20, verticalHeights[currentRoomY]];
            int index = verticalHeights.Take <int>(currentRoomY).Sum() + 4000 * currentRoomX;

            for (int i = 0; i < roomLayer.GetLength(0); i++)
            {
                for (int j = 0; j < roomLayer.GetLength(1); j++)
                {
                    int currentStep = index + j + i * 200;
                    roomLayer[i, j] = roomLayerArray[targetLayer][currentStep];
                }
            }
            return(roomLayer);
        }
예제 #3
0
        /// <summary>
        /// Alpaca Extension - Convert a byte array to a 2D or 3D mage array based on the array metadata.
        /// </summary>
        /// <param name="imageBytes">byte array to convert</param>
        /// <returns>2D or 3D array as specified in the array metadata.</returns>
        /// <exception cref="InvalidValueException">The byte array is null.</exception>
        public static Array ToImageArray(this byte[] imageBytes)
        {
            ImageArrayElementTypes imageElementType;
            ImageArrayElementTypes transmissionElementType;
            int rank;
            int dimension1;
            int dimension2;
            int dimension3;
            int dataStart;

            // Validate the incoming array
            if (imageBytes is null)
            {
                throw new InvalidValueException("ToImageArray - Supplied array is null.");
            }
            if (imageBytes.Length <= ARRAY_METADATAV1_LENGTH)
            {
                throw new InvalidValueException($"ToImageArray - Supplied array does not exceed the size of the mandatory metadata. Arrays must contain at least {ARRAY_METADATAV1_LENGTH} bytes. The supplied array has a length of {imageBytes.Length}.");
            }

            int metadataVersion = imageBytes.GetMetadataVersion();

            // Get the metadata version and extract the supplied values
            switch (metadataVersion)
            {
            case 1:
                ArrayMetadataV1 metadataV1 = imageBytes.GetMetadataV1();
                // Set the array type, rank and dimensions
                imageElementType        = metadataV1.ImageElementType;
                transmissionElementType = metadataV1.TransmissionElementType;
                rank       = metadataV1.Rank;
                dimension1 = metadataV1.Dimension1;
                dimension2 = metadataV1.Dimension2;
                dimension3 = metadataV1.Dimension3;
                dataStart  = metadataV1.DataStart;

                Debug.WriteLine($"ToImageArray - Element type: {imageElementType} Transmission type: {transmissionElementType}");

                break;

            default:
                throw new InvalidValueException($"ToImageArray - The supplied array contains an unsupported metadata version number: {metadataVersion}. This component supports metadata version 1.");
            }

            // Validate the metadata
            if (imageElementType == ImageArrayElementTypes.Unknown)
            {
                throw new InvalidValueException("ToImageArray - ImageArrayElementType is 0, meaning ImageArrayElementTypes.Unknown");
            }
            if (imageElementType > Enum.GetValues(typeof(ImageArrayElementTypes)).Cast <ImageArrayElementTypes>().Max())
            {
                throw new InvalidValueException($"ToImageArray - The ImageArrayElementType value {((int)imageElementType)} is outside the valid range 0 to {Enum.GetValues(typeof(ImageArrayElementTypes)).Cast<ImageArrayElementTypes>().Max()}");
            }
            if (transmissionElementType == ImageArrayElementTypes.Unknown)
            {
                throw new InvalidValueException("ToImageArray - The TransmissionElementType is 0, meaning ImageArrayElementTypes.Unknown");
            }

            // Convert the returned byte[] into the form that the client is expecting
            if ((imageElementType == ImageArrayElementTypes.Int32) & (transmissionElementType == ImageArrayElementTypes.Int16)) // Handle the special case where Int32 has been converted to Int16 for transmission
            {
                switch (rank)
                {
                case 2:     // Rank 2
                    Int16[,] short2dArray = new Int16[dimension1, dimension2];
                    Buffer.BlockCopy(imageBytes, dataStart, short2dArray, 0, imageBytes.Length - dataStart);

                    int[,] int2dArray = new int[dimension1, dimension2];
                    Parallel.For(0, short2dArray.GetLength(0), (i) =>
                    {
                        for (int j = 0; j < short2dArray.GetLength(1); j++)
                        {
                            int2dArray[i, j] = short2dArray[i, j];
                        }
                    });
                    return(int2dArray);

                case 3:     // Rank 3
                    Int16[,,] short3dArray = new Int16[dimension1, dimension2, dimension3];
                    Buffer.BlockCopy(imageBytes, dataStart, short3dArray, 0, imageBytes.Length - dataStart);

                    int[,,] int3dArray = new int[dimension1, dimension2, dimension3];
                    Parallel.For(0, short3dArray.GetLength(0), (i) =>
                    {
                        for (int j = 0; j < short3dArray.GetLength(1); j++)
                        {
                            for (int k = 0; k < short3dArray.GetLength(2); k++)
                            {
                                int3dArray[i, j, k] = short3dArray[i, j, k];
                            }
                        }
                    });
                    return(int3dArray);

                default:
                    throw new InvalidValueException($"ToImageArray - Returned array cannot be handled because it does not have a rank of 2 or 3. Returned array rank:{rank}.");
                }
            }
            else if ((imageElementType == ImageArrayElementTypes.Int32) & (transmissionElementType == ImageArrayElementTypes.UInt16)) // Handle the special case where Int32 values has been converted to UInt16 for transmission
            {
                switch (rank)
                {
                case 2:     // Rank 2
                    UInt16[,] uInt16Array2D = new UInt16[dimension1, dimension2];
                    Buffer.BlockCopy(imageBytes, dataStart, uInt16Array2D, 0, imageBytes.Length - dataStart);

                    int[,] int2dArray = new int[dimension1, dimension2];
                    Parallel.For(0, uInt16Array2D.GetLength(0), (i) =>
                    {
                        for (int j = 0; j < uInt16Array2D.GetLength(1); j++)
                        {
                            int2dArray[i, j] = uInt16Array2D[i, j];
                        }
                    });
                    return(int2dArray);

                case 3:     // Rank 3
                    UInt16[,,] uInt16Array3D = new UInt16[dimension1, dimension2, dimension3];
                    Buffer.BlockCopy(imageBytes, dataStart, uInt16Array3D, 0, imageBytes.Length - dataStart);

                    int[,,] int3dArray = new int[dimension1, dimension2, dimension3];
                    Parallel.For(0, uInt16Array3D.GetLength(0), (i) =>
                    {
                        for (int j = 0; j < uInt16Array3D.GetLength(1); j++)
                        {
                            for (int k = 0; k < uInt16Array3D.GetLength(2); k++)
                            {
                                int3dArray[i, j, k] = uInt16Array3D[i, j, k];
                            }
                        }
                    });
                    return(int3dArray);

                default:
                    throw new InvalidValueException($"ToImageArray - Returned array cannot be handled because it does not have a rank of 2 or 3. Returned array rank:{rank}.");
                }
            }
            else // Handle all other cases where the expected array type and the transmitted array type are the same
            {
                if (imageElementType == transmissionElementType) // Required and transmitted array element types are the same
                {
                    switch (imageElementType)
                    {
                    case ImageArrayElementTypes.Byte:
                        switch (rank)
                        {
                        case 2:         // Rank 2
                            byte[,] byte2dArray = new byte[dimension1, dimension2];
                            Buffer.BlockCopy(imageBytes, dataStart, byte2dArray, 0, imageBytes.Length - dataStart);
                            return(byte2dArray);

                        case 3:         // Rank 3
                            byte[,,] byte3dArray = new byte[dimension1, dimension2, dimension3];
                            Buffer.BlockCopy(imageBytes, dataStart, byte3dArray, 0, imageBytes.Length - dataStart);
                            return(byte3dArray);

                        default:
                            throw new InvalidValueException($"ToImageArray - Returned byte array cannot be handled because it does not have a rank of 2 or 3. Returned array rank:{rank}.");
                        }

                    case ImageArrayElementTypes.Int16:
                        switch (rank)
                        {
                        case 2:         // Rank 2
                            short[,] short2dArray = new short[dimension1, dimension2];
                            Buffer.BlockCopy(imageBytes, dataStart, short2dArray, 0, imageBytes.Length - dataStart);
                            return(short2dArray);

                        case 3:         // Rank 3
                            short[,,] short3dArray = new short[dimension1, dimension2, dimension3];
                            Buffer.BlockCopy(imageBytes, dataStart, short3dArray, 0, imageBytes.Length - dataStart);
                            return(short3dArray);

                        default:
                            throw new InvalidValueException($"ToImageArray - Returned Int16 array cannot be handled because it does not have a rank of 2 or 3. Returned array rank:{rank}.");
                        }

                    case ImageArrayElementTypes.UInt16:
                        switch (rank)
                        {
                        case 2:         // Rank 2
                            UInt16[,] uInt16Array2D = new UInt16[dimension1, dimension2];
                            Buffer.BlockCopy(imageBytes, dataStart, uInt16Array2D, 0, imageBytes.Length - dataStart);
                            return(uInt16Array2D);

                        case 3:         // Rank 3
                            UInt16[,,] uInt16Array3D = new UInt16[dimension1, dimension2, dimension3];
                            Buffer.BlockCopy(imageBytes, dataStart, uInt16Array3D, 0, imageBytes.Length - dataStart);
                            return(uInt16Array3D);

                        default:
                            throw new InvalidValueException($"ToImageArray - Returned UInt16 array cannot be handled because it does not have a rank of 2 or 3. Returned array rank:{rank}.");
                        }

                    case ImageArrayElementTypes.Int32:
                        switch (rank)
                        {
                        case 2:         // Rank 2
                            int[,] int2dArray = new int[dimension1, dimension2];
                            Buffer.BlockCopy(imageBytes, dataStart, int2dArray, 0, imageBytes.Length - dataStart);
                            return(int2dArray);

                        case 3:         // Rank 3
                            int[,,] int3dArray = new int[dimension1, dimension2, dimension3];
                            Buffer.BlockCopy(imageBytes, dataStart, int3dArray, 0, imageBytes.Length - dataStart);
                            return(int3dArray);

                        default:
                            throw new InvalidValueException($"ToImageArray - Returned Int32 array cannot be handled because it does not have a rank of 2 or 3. Returned array rank:{rank}.");
                        }

                    case ImageArrayElementTypes.UInt32:
                        switch (rank)
                        {
                        case 2:         // Rank 2
                            UInt32[,] uInt32Array2D = new UInt32[dimension1, dimension2];
                            Buffer.BlockCopy(imageBytes, dataStart, uInt32Array2D, 0, imageBytes.Length - dataStart);
                            return(uInt32Array2D);

                        case 3:         // Rank 3
                            UInt32[,,] uInt32Array3D = new UInt32[dimension1, dimension2, dimension3];
                            Buffer.BlockCopy(imageBytes, dataStart, uInt32Array3D, 0, imageBytes.Length - dataStart);
                            return(uInt32Array3D);

                        default:
                            throw new InvalidValueException($"ToImageArray - Returned UInt32 array cannot be handled because it does not have a rank of 2 or 3. Returned array rank:{rank}.");
                        }

                    case ImageArrayElementTypes.Int64:
                        switch (rank)
                        {
                        case 2:         // Rank 2
                            Int64[,] int642dArray = new Int64[dimension1, dimension2];
                            Buffer.BlockCopy(imageBytes, dataStart, int642dArray, 0, imageBytes.Length - dataStart);
                            return(int642dArray);

                        case 3:         // Rank 3
                            Int64[,,] int643dArray = new Int64[dimension1, dimension2, dimension3];
                            Buffer.BlockCopy(imageBytes, dataStart, int643dArray, 0, imageBytes.Length - dataStart);
                            return(int643dArray);

                        default:
                            throw new InvalidValueException($"ToImageArray - Returned Int64 array cannot be handled because it does not have a rank of 2 or 3. Returned array rank:{rank}.");
                        }

                    case ImageArrayElementTypes.Single:
                        switch (rank)
                        {
                        case 2:         // Rank 2
                            Single[,] single2dArray = new Single[dimension1, dimension2];
                            Buffer.BlockCopy(imageBytes, dataStart, single2dArray, 0, imageBytes.Length - dataStart);
                            return(single2dArray);

                        case 3:         // Rank 3
                            Single[,,] single3dArray = new Single[dimension1, dimension2, dimension3];
                            Buffer.BlockCopy(imageBytes, dataStart, single3dArray, 0, imageBytes.Length - dataStart);
                            return(single3dArray);

                        default:
                            throw new InvalidValueException($"ToImageArray - Returned Int64 array cannot be handled because it does not have a rank of 2 or 3. Returned array rank:{rank}.");
                        }

                    case ImageArrayElementTypes.Double:
                        switch (rank)
                        {
                        case 2:         // Rank 2
                            Double[,] double2dArray = new Double[dimension1, dimension2];
                            Buffer.BlockCopy(imageBytes, dataStart, double2dArray, 0, imageBytes.Length - dataStart);
                            return(double2dArray);

                        case 3:         // Rank 3
                            Double[,,] double3dArray = new Double[dimension1, dimension2, dimension3];
                            Buffer.BlockCopy(imageBytes, dataStart, double3dArray, 0, imageBytes.Length - dataStart);
                            return(double3dArray);

                        default:
                            throw new InvalidValueException($"ToImageArray - Returned Int64 array cannot be handled because it does not have a rank of 2 or 3. Returned array rank:{rank}.");
                        }

                    case ImageArrayElementTypes.Decimal:
                        switch (rank)
                        {
                        case 2:         // Rank 2
                            Decimal[,] decimal2dArray = new Decimal[dimension1, dimension2];
                            Buffer.BlockCopy(imageBytes, dataStart, decimal2dArray, 0, imageBytes.Length - dataStart);
                            return(decimal2dArray);

                        case 3:         // Rank 3
                            Decimal[,,] decimal3dArray = new Decimal[dimension1, dimension2, dimension3];
                            Buffer.BlockCopy(imageBytes, dataStart, decimal3dArray, 0, imageBytes.Length - dataStart);
                            return(decimal3dArray);

                        default:
                            throw new InvalidValueException($"ToImageArray - Returned Int64 array cannot be handled because it does not have a rank of 2 or 3. Returned array rank:{rank}.");
                        }

                    default:
                        throw new InvalidValueException($"ToImageArray - The device has returned an unsupported image array element type: {imageElementType}.");
                    }
                }
                else // An unsupported combination of array element types has been returned
                {
                    throw new InvalidValueException($"ToImageArray - The device has returned an unsupported combination of Output type: {imageElementType} and Transmission type: {transmissionElementType}.");
                }
            }
        }