Esempio n. 1
0
        private static int[] ToPixelArray(PixelData pixelData)
        {
            int[] intArray;
            if (pixelData.Data.Value.IsSequence)
            {
                Sequence sq = (Sequence)pixelData.Data.Value[0];
                intArray = new int[sq.Count];
                for (int i = 0; i < sq.Count; i++)
                {
                    intArray[i] = Convert.ToInt32(sq[i].Value[0]);
                }
                return(intArray);
            }
            else if (pixelData.Data.Value.IsArray)
            {
                byte[][] bytesArray = pixelData.ToBytesArray();
                if (bytesArray != null && bytesArray.Length > 0)
                {
                    byte[] bytes = bytesArray[0];

                    int cellSize   = pixelData.BitsAllocated / 8;
                    int pixelCount = bytes.Length / cellSize;

                    intArray = new int[pixelCount];
                    int pixelIndex = 0;

                    // Byte array for a single cell/pixel value
                    byte[] cellData = new byte[cellSize];
                    for (int iByte = 0; iByte < bytes.Length; iByte++)
                    {
                        // Collect bytes for one cell (sample)
                        int index = iByte % cellSize;
                        cellData[index] = bytes[iByte];
                        // We have collected enough bytes for one cell => convert and add it to pixel array
                        if (index == cellSize - 1)
                        {
                            int cellValue = 0;
                            if (pixelData.BitsAllocated == 8)
                            {
                                cellValue = cellData[0];
                            }
                            else if (pixelData.BitsAllocated == 16)
                            {
                                cellValue = BitConverter.ToInt16(cellData, 0);
                            }
                            else if (pixelData.BitsAllocated == 32)
                            {
                                cellValue = BitConverter.ToInt32(cellData, 0);
                            }
                            else
                            {
                                Debug.LogError("Invalid format!");
                            }

                            intArray[pixelIndex] = cellValue;
                            pixelIndex++;
                        }
                    }
                    return(intArray);
                }
                else
                {
                    return(null);
                }
            }
            else
            {
                Debug.LogError("Pixel array is invalid");
                return(null);
            }
        }