Esempio n. 1
0
        private void convertFromTextToImageFileButton_Click2(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(_ImportTextFilePath))
            {
                MessageBox.Show(this, "No input path", "Choose a file to input to.", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (string.IsNullOrWhiteSpace(_OutputImageFilePath))
            {
                MessageBox.Show(this, "No output path", "Choose a file to output to.", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            var compressedByteArray = File.ReadAllBytes(_ImportTextFilePath);

            var inputByteArray = Compression.Decompress(compressedByteArray);

            //First byte is if it's encrypted
            bool isEncrypted = Convert.ToBoolean(inputByteArray[0]);

            // next 4 bytes contains the width
            int width = Utilities.ConvertByteArrayToInt32(new byte[] { inputByteArray[1], inputByteArray[2], inputByteArray[3], inputByteArray[4] });

            // next 4 bytes contains the height
            int height = Utilities.ConvertByteArrayToInt32(new byte[] { inputByteArray[5], inputByteArray[6], inputByteArray[7], inputByteArray[8] });

            // next byte is image type
            ImageType imageType = ImageTypeDictionary.GetImageFromInt(inputByteArray[9]);

            bool ignoreAlpha = Convert.ToBoolean(inputByteArray[10]);

            _OutputImageFilePath = _OutputImageFilePath + "." + imageType.ToString();

            int currentIndex = 11;

            _ColorDictionary = new ColorDictionaryEncoder(8);

            // bytes 11 - 1285 contain the byte dictionary library
            for (int i = 0; i < Byte.MaxValue; i++)
            {
                int currentDictionaryIndex = currentIndex + (i * 5);
                var key = inputByteArray[currentDictionaryIndex + 0];
                var a   = inputByteArray[currentDictionaryIndex + 1];
                var b   = inputByteArray[currentDictionaryIndex + 2];
                var g   = inputByteArray[currentDictionaryIndex + 3];
                var r   = inputByteArray[currentDictionaryIndex + 4];

                _ColorDictionary.AddDecoderEntry(key, a, b, g, r);
            }

            currentIndex = 1286;

            Bitmap outputImage = new Bitmap(width, height);

            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    byte  currentByte = inputByteArray[currentIndex];
                    Color outputColor = _ColorDictionary.GetColorFromByte(currentByte);

                    outputImage.SetPixel(i, j, outputColor);

                    currentIndex++;
                }

                int progress = (int)((i * 1.0 / width) * 100);
                progressBar.Value  = progress;
                progressLabel.Text = progress.ToString() + " %";
                progressLabel.Refresh();
            }

            progressBar.Value  = 0;
            progressLabel.Text = "Done";

            outputImage.Save(_OutputImageFilePath);
        }