Exemplo n.º 1
0
        public static void Main(string[] args)
        {
            Bitmap bmp = new Bitmap("../../sample.png");

            Bitmap bmp2 = new Bitmap(2 * bmp.Width, 2 * bmp.Height, PixelFormat.Format32bppArgb);

            float[,] yArray2  = new float[bmp2.Width, bmp2.Height];
            float[,] cbArray2 = new float[bmp2.Width, bmp2.Height];
            float[,] crArray2 = new float[bmp2.Width, bmp2.Height];
            float[,] aArray2  = new float[bmp2.Width, bmp2.Height];

            ImageArrayConverter.BitmapToAYCbCrArrays(bmp, aArray2, yArray2, cbArray2, crArray2);

            Biorthogonal53Wavelet2D wavelet  = new Biorthogonal53Wavelet2D(bmp.Width, bmp.Height);
            Biorthogonal53Wavelet2D wavelet2 = new Biorthogonal53Wavelet2D(bmp2.Width, bmp2.Height);

            wavelet.TransformIsotropic2D(aArray2);
            wavelet.TransformIsotropic2D(yArray2);
            wavelet.TransformIsotropic2D(cbArray2);
            wavelet.TransformIsotropic2D(crArray2);

            wavelet2.BacktransformIsotropic2D(aArray2);
            wavelet2.BacktransformIsotropic2D(yArray2);
            wavelet2.BacktransformIsotropic2D(cbArray2);
            wavelet2.BacktransformIsotropic2D(crArray2);
            for (int y = 0; y < bmp2.Height; y++)
            {
                for (int x = 0; x < bmp2.Width; x++)
                {
                    aArray2[x, y]  *= 4.0f;
                    yArray2[x, y]  *= 4.0f;
                    cbArray2[x, y] *= 4.0f;
                    crArray2[x, y] *= 4.0f;
                }
            }
            ImageArrayConverter.AYCbCrArraysToBitmap(aArray2, yArray2, cbArray2, crArray2, bmp2);
            bmp2.Save("test.png", ImageFormat.Png);
        }
Exemplo n.º 2
0
		private static void applyAdaptiveDeadzone (float[,] array, int numCoeffs)
		{
			int width = array.GetLength (0);
			int height = array.GetLength (1);
			Biorthogonal53Wavelet2D wavelet53 = new Biorthogonal53Wavelet2D (width, height);
			OrderWavelet2D waveletOrder = new OrderWavelet2D (width, height);
            
			wavelet53.EnableCaching = true;
			waveletOrder.EnableCaching = true;
			wavelet53.TransformIsotropic2D (array);
			//Reverse the ordering of the coefficients
			waveletOrder.BacktransformIsotropic2D (array);
			//Use numCoeffs cofficient out of 64 (8x8) -> for e.g. numCoeffs = 5 this
			//means a compression to 7,8% of the original size
			waveletOrder.CropCoefficients (array, 7, 8);
			waveletOrder.TransformIsotropic2D (array);
			wavelet53.BacktransformIsotropic2D (array);
		}
Exemplo n.º 3
0
        private static void applyAdaptiveDeadzone(float[,] array, int numCoeffs)
        {
            int width  = array.GetLength(0);
            int height = array.GetLength(1);
            Biorthogonal53Wavelet2D wavelet53    = new Biorthogonal53Wavelet2D(width, height);
            OrderWavelet2D          waveletOrder = new OrderWavelet2D(width, height);

            wavelet53.EnableCaching    = true;
            waveletOrder.EnableCaching = true;
            wavelet53.TransformIsotropic2D(array);
            //Reverse the ordering of the coefficients
            waveletOrder.BacktransformIsotropic2D(array);
            //Use numCoeffs cofficient out of 64 (8x8) -> for e.g. numCoeffs = 5 this
            //means a compression to 7,8% of the original size
            waveletOrder.CropCoefficients(array, 7, 8);
            waveletOrder.TransformIsotropic2D(array);
            wavelet53.BacktransformIsotropic2D(array);
        }
Exemplo n.º 4
0
        private static void applyAdaptiveShapening(float[,] array, float position)
        {
            int width  = array.GetLength(0);
            int height = array.GetLength(1);
            Biorthogonal53Wavelet2D wavelet53    = new Biorthogonal53Wavelet2D(width, height);
            OrderWavelet2D          waveletOrder = new OrderWavelet2D(width, height);

            wavelet53.EnableCaching    = true;
            waveletOrder.EnableCaching = true;
            wavelet53.TransformIsotropic2D(array);
            //Reverse the ordering of the coefficients
            waveletOrder.BacktransformIsotropic2D(array);
            float[] scale = new float[8 * 8];

            for (int x = 0; x < 8 * 8; x++)
            {
                scale [x] = 1.0f + 2.0f / ((position - x) * (position - x) + 1.0f);
            }
            waveletOrder.ScaleCoefficients(array, scale, 8);
            waveletOrder.TransformIsotropic2D(array);
            wavelet53.BacktransformIsotropic2D(array);
        }
Exemplo n.º 5
0
        public static void SaveAsFile(string sFile, ref int[,] rgbArray, int width, int height, int quality)
        {
            int size           = width * height;
            int numRemainingY  = quality * size / 100;
            int numRemainingCg = quality * size / 25;
            int numRemainingCo = quality * size / 25;

            int newWidth, newHeight;
            // int[,] rgbArray = new int[width, height];
            // RGBColor.FillRGBArrayFromBitmap(bmp, ref rgbArray);
            FileStream   fs = new FileStream(sFile, FileMode.Create, FileAccess.Write, FileShare.None);
            MemoryStream ms = new MemoryStream();

            newWidth  = IntegerMath.ToPowerOf2(width);
            newHeight = IntegerMath.ToPowerOf2(height);

            float[,] imgOrgY  = new float[width, height];
            float[,] imgOrgCg = new float[width, height];
            float[,] imgOrgCo = new float[width, height];
            float[,] imgY     = new float[newWidth, newHeight];
            float[,] imgCg    = new float[newWidth, newHeight];
            float[,] imgCo    = new float[newWidth, newHeight];
            int[,] imgIY      = new int[newWidth, newHeight];
            int[,] imgICg     = new int[newWidth, newHeight];
            int[,] imgICo     = new int[newWidth, newHeight];
            //YCgCoColor.RGBArrayToYCgCoArrays(ref rgbArray, width, height, ref imgOrgY, ref imgOrgCg, ref imgOrgCo);
            YCbCrColor.RGBArrayToYCbCrArrays(ref rgbArray, width, height, ref imgOrgY, ref imgOrgCg, ref imgOrgCo);
            ArrayHelper2D.ResizeArray2D(ref imgOrgY, ref imgY, width, height, newWidth, newHeight, ArrayHelper2D.ResizeFilter.NONE_CLAMP);
            ArrayHelper2D.ResizeArray2D(ref imgOrgCg, ref imgCg, width, height, newWidth, newHeight, ArrayHelper2D.ResizeFilter.NONE_CLAMP);
            ArrayHelper2D.ResizeArray2D(ref imgOrgCo, ref imgCo, width, height, newWidth, newHeight, ArrayHelper2D.ResizeFilter.NONE_CLAMP);
            Biorthogonal53Wavelet2D wavelet = new Biorthogonal53Wavelet2D(newWidth, newHeight, 8, 8, true);

            wavelet.TransformIsotropic2D(ref imgY);
            wavelet.TransformIsotropic2D(ref imgCg);
            wavelet.TransformIsotropic2D(ref imgCo);
            ArrayHelper2D.RemoveSmallestCoefficients2D(ref imgY, newWidth, newHeight, numRemainingY);
            ArrayHelper2D.RemoveSmallestCoefficients2D(ref imgCg, newWidth, newHeight, numRemainingCg);
            ArrayHelper2D.RemoveSmallestCoefficients2D(ref imgCo, newWidth, newHeight, numRemainingCo);

            //simple quantification
            float quant = 1.0f / (float)((100.0f - quality) + 1.0f);

            for (int y = 0; y < newHeight; y++)
            {
                for (int x = 0; x < newWidth; x++)
                {
                    imgY[x, y]  *= quant;
                    imgCg[x, y] *= quant;
                    imgCo[x, y] *= quant;
                }
            }

            ArrayHelper2D.ConvertFloatToInt(ref imgY, ref imgIY, newWidth, newHeight);
            ArrayHelper2D.ConvertFloatToInt(ref imgCg, ref imgICg, newWidth, newHeight);
            ArrayHelper2D.ConvertFloatToInt(ref imgCo, ref imgICo, newWidth, newHeight);
            BinaryWriter writer = new BinaryWriter(ms);

            writer.Write(width);
            writer.Write(height);
            writer.Write(quality);
            for (int y = 0; y < newHeight; y++)
            {
                for (int x = 0; x < newWidth; x++)
                {
                    writer.Write(imgIY[x, y]);
                    writer.Write(imgICg[x, y]);
                    writer.Write(imgICo[x, y]);
                }
            }
            writer.Close();
            byte[] buffer     = ms.GetBuffer();
            byte[] compressed = Memory.CompressBuffer(ref buffer);
            fs.Write(compressed, 0, compressed.Length);
            fs.Close();
        }