コード例 #1
0
        //Decompress byte array into aBitmap with help of width, length and bitdepth
        public static AccessibleBitmap Decompress(byte[] source, int width, int height, int pixelBytes)
        {
            //Create a empty AccessibleBitmapBytewise class with the correct dimensions and color channels
            AccessibleBitmapBytewise outputBitmap = new AccessibleBitmapBytewise(new AccessibleBitmap(width, height, pixelBytes));

            //Initialize
            byte[] inBytes;             //Stores the bytes to feed to the decompressor
            byte[] outBytes = source;   //Stores the bytes which are left by the previous decompressed channel: starts with all the input data

            //Loop trough all color channels
            for (int i = 0; i < pixelBytes; i++)
            {
                //Read compression type from outBytes & copy the rest of them to inBytes so they can be used for the next channel
                int compressionType = outBytes[0];
                inBytes = new byte[outBytes.Length - 1];
                Array.Copy(outBytes, 1, inBytes, 0, inBytes.Length);

                //Decompress using the correct compression type
                switch (compressionType)
                {
                //Uncompressed
                case 0:
                    outputBitmap = UncompressedBitmapCompressorBytewise.Decompress(inBytes, outputBitmap, out outBytes, i);
                    break;

                //Individual compressed bit channels added together
                case 1:
                    outputBitmap = BitLayerVaryingCompressor.Decompress(inBytes, outputBitmap, out outBytes, i);
                    break;

                //Color channel compressed as integers
                case 2:
                    outputBitmap = ByteArrayCompressorBytewise.Decompress(inBytes, outputBitmap, out outBytes, i);
                    break;

                //Run length compression
                case 3:
                    outputBitmap = RunLengthEncodingCompressorBytewise.Decompress(inBytes, outputBitmap, out outBytes, i);
                    break;

                //Run length encoding vertical
                case 4:
                    outputBitmap = RunLengthEncodingCompressorVerticalBytewise.Decompress(inBytes, outputBitmap, out outBytes, i);
                    break;

                //To add a decompression type add a new case like the existing ones

                //Unknown compression type: error
                default:
                    throw new Exception("Unexisting compression type");
                }
            }
            return(outputBitmap.GetAccessibleBitmap());
        }