/// <summary>
        /// Forward application of the algorithm
        /// </summary>
        /// <param name="parOriginalImage">Original image stream</param>
        /// <param name="parArguments">List of an arguments (long qualityLevel, ImageFormat interimFormat)</param>
        /// <returns>Compressed image stream</returns>
        public override Stream CompressImage(Stream parOriginalImage, List <object> parArguments)
        {
            Bitmap original        = new Bitmap(parOriginalImage);
            int    originalWidth   = original.Width;
            int    originalHeight  = original.Height;
            int    moddedWidth     = (originalWidth + 1) / 2;
            int    moddedHeight    = originalHeight;
            byte   lastColumnIsOdd = (byte)(originalWidth % 2);
            Bitmap modded          = new Bitmap(moddedWidth, moddedHeight, original.PixelFormat);

            BitmapData bdOriginal = original.LockBits(new Rectangle(0, 0, originalWidth, originalHeight),
                                                      ImageLockMode.ReadOnly,
                                                      PixelFormat.Format32bppArgb);

            int[] bitsOriginal = new int[originalWidth * originalHeight];
            Marshal.Copy(bdOriginal.Scan0, bitsOriginal, 0, bitsOriginal.Length);
            // --/--
            BitmapData bdModded = modded.LockBits(new Rectangle(0, 0, moddedWidth, moddedHeight),
                                                  ImageLockMode.ReadWrite,
                                                  PixelFormat.Format32bppArgb);

            int[] bitsModded = new int[moddedWidth * moddedHeight];
            Marshal.Copy(bdModded.Scan0, bitsModded, 0, bitsModded.Length);

            for (int i = 0; i < moddedHeight; i += 2)
            {
                for (int j = 0; j < moddedWidth; j++)
                {
                    bitsModded[i * moddedWidth + j] = bitsOriginal[i * moddedWidth * 2 + j * 2];
                    if ((j * 2 + 1) < originalWidth)
                    {
                        bitsModded[(i + 1) * moddedWidth + j] = bitsOriginal[(i + 1) * moddedWidth * 2 + j * 2 + 1];
                    }
                }
            }
            Marshal.Copy(bitsModded, 0, bdModded.Scan0, bitsModded.Length);
            original.UnlockBits(bdOriginal);
            modded.UnlockBits(bdModded);

            Stream      interlacedImage = new MemoryStream();
            ImageFormat format          = (ImageFormat)parArguments[1];

            modded.Save(interlacedImage, format);
            interlacedImage.WriteByte(lastColumnIsOdd);

            GZIP   gzip             = GZIP.GetInstance();
            int    compressionLevel = (int)parArguments[0];
            Stream compressedImage  = gzip.CompressImage(interlacedImage, new List <object>()
            {
                compressionLevel
            });

            return(compressedImage);
        }
Example #2
0
        /// <summary>
        /// Forward application of the algorithm
        /// </summary>
        /// <param name="parOriginalImage">Original image stream</param>
        /// <param name="parArguments">List of an arguments (long qualityLevel, ImageFormat interimFormat)</param>
        /// <returns>Compressed image stream</returns>
        public override Stream CompressImage(Stream parOriginalImage, List <object> parArguments)
        {
            Bitmap original = new Bitmap(parOriginalImage);
            int    levels   = (int)parArguments[0];

            HaarWavelet.ApplyTransform(ref original, true, levels);

            Stream      waveletStream = new MemoryStream();
            ImageFormat format        = (ImageFormat)parArguments[2];

            original.Save(waveletStream, format);

            GZIP   gzip             = GZIP.GetInstance();
            int    compressionLevel = (int)parArguments[1];
            Stream compressedImage  = gzip.CompressImage(waveletStream, new List <object>()
            {
                compressionLevel
            });

            return(compressedImage);
        }