public void EqualsTest() { var data = new Int16[, ] { { 0, 0 }, { -1, -1 }, { 1, 1 } }; var length = data.GetLength(0); for (var i = 0; i < length; i++) { InteropByte interopByte = data[i, 0]; InteropByte interopByte2 = data[i, 1]; Assert.IsTrue(interopByte.Equals(interopByte2)); } }
public Boolean runTest() { Console.Error.WriteLine(s_strTFPath + " " + s_strTFName + " , for " + s_strClassMethod + " , Source ver " + s_strDtTmVer); int iCountErrors = 0; int iCountTestcases = 0; String strLoc = "Loc_000oo"; String strBaseLoc; short[] in2Arr = new Int16[10]; int[] in4Arr = new Int32[5]; long[] in8Arr = new Int64[0]; String[] strArr = new String[6]; Boolean[] boArr = new Boolean[3]; Double[] dblArr = new Double[2]; Single[] snglArr = new Single[32000]; Char[] chArr = new Char[10000]; int rank; try { LABEL_860_GENERAL: do { strLoc = "Loc_819yt"; rank = -1; in2Arr = new Int16[5]; iCountTestcases++; try { in2Arr.GetLength(rank); iCountErrors++; Console.WriteLine(s_strTFAbbrev + "Err_499ws! , GetLength==" + in2Arr.Length); } catch (IndexOutOfRangeException ioorExc) {} catch (Exception exc) { iCountErrors++; Console.WriteLine(s_strTFAbbrev + "Err_758! exc==" + exc); } strLoc = "Loc_819ee"; rank = 1; in2Arr = new Int16[5]; iCountTestcases++; try { in2Arr.GetLength(rank); iCountErrors++; Console.WriteLine(s_strTFAbbrev + "Err_500ws! , GetLength==" + in2Arr.Length); } catch (IndexOutOfRangeException ioorExc) {} catch (Exception exc) { iCountErrors++; Console.WriteLine(s_strTFAbbrev + "Err_750! exc==" + exc); } strLoc = "Loc_482wu"; rank = 0; in2Arr = new Int16[10]; iCountTestcases++; if (in2Arr.GetLength(rank) != 10) { iCountErrors++; Console.WriteLine(s_strTFAbbrev + "Err_481ua! , GetLength==" + in2Arr.Length); } strLoc = "Loc_471ay"; in4Arr = new Int32[5]; iCountTestcases++; if (in4Arr.GetLength(rank) != 5) { iCountErrors++; Console.WriteLine(s_strTFAbbrev + "Err_29qaq! , GetLength==" + in4Arr.Length); } strLoc = "Loc_982uq"; in8Arr = new Int64[0]; iCountTestcases++; if (in8Arr.GetLength(rank) != 0) { iCountErrors++; Console.WriteLine(s_strTFAbbrev + "Err_237sy! , GetLength==" + in8Arr.Length); } strLoc = "Loc_172ms"; boArr = new Boolean[3]; iCountTestcases++; if (boArr.GetLength(rank) != 3) { iCountErrors++; Console.WriteLine(s_strTFAbbrev + "Err_382! , GetLength==" + boArr.Length); } strLoc = "Loc_49su"; dblArr = new Double[2]; iCountTestcases++; if (dblArr.GetLength(rank) != 2) { iCountErrors++; Console.WriteLine(s_strTFAbbrev + "Err_200su! , GetLength==" + dblArr.Length); } strLoc = "Loc_371su"; snglArr = new Single[32000]; iCountTestcases++; if (snglArr.GetLength(rank) != 32000) { iCountErrors++; Console.WriteLine(s_strTFAbbrev + "Err_319aw! , GetLength==" + snglArr.Length); } strLoc = "Loc_129wi"; strArr = new String[5]; strArr[2] = null; iCountTestcases++; if (strArr.GetLength(rank) != 5) { iCountErrors++; Console.WriteLine(s_strTFAbbrev + "Err_71ahw! , GetLength==" + strArr.Length); } } while (false); } catch (Exception exc_general) { ++iCountErrors; Console.WriteLine(s_strTFAbbrev + " Error Err_8888yyy! strLoc==" + strLoc + ", exc_general==" + exc_general); } if (iCountErrors == 0) { return(true); } else { return(false); } }
/// <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}."); } } }