Example #1
0
        public void PPMtoJpg(string imageSrc, string imagedest)
        {
            Bitstream         bs  = new Bitstream();
            PlainFormatReader pfr = new PlainFormatReader(imageSrc);
            Mathloc           ml  = new Mathloc();
            Picture           pic = pfr.ReadPicture();

            int height = pic.Head.pixelMaxY;
            int width  = pic.Head.pixelMaxX;

            Color3[,] col3 = new Color3[height, width];
            byte[] colors = new byte[3];

            //convert RGB to YUV for each pixel
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    col3[y, x]   = pic.Data.PictureYX[y, x];
                    colors[0]    = col3[y, x].a; // R
                    colors[1]    = col3[y, x].b; // G
                    colors[2]    = col3[y, x].c; // B
                    colors       = ml.RGBToYUV(colors);
                    col3[y, x].a = colors[0];    // Y
                    col3[y, x].b = colors[1];    // U
                    col3[y, x].c = colors[2];    // V
                }
            }

            //convert float array to byte array
            float[,] yArray = new float[width, height]; // width and height vertauscht
            float[,] uArray = new float[width, height];
            float[,] vArray = new float[width, height];

            for (int j = 0; j < height; j++)
            {
                for (int i = 0; i < width; i++)
                {
                    yArray[i, j] = (float)col3[j, i].a;
                    uArray[i, j] = (float)col3[j, i].b;
                    vArray[i, j] = (float)col3[j, i].c;
                }
            }

            byte[,] yArrayEnde = new byte[width, height];
            byte[,] uArrayEnde = new byte[width, height];
            byte[,] vArrayEnde = new byte[width, height];

            //get float [number of 8x8][8x8]
            var yarry = aufteilen(yArray);
            var uarry = aufteilen(uArray);
            var varry = aufteilen(vArray);


            //geht beim 40x40 bild 25
            for (int y = 0; y < height / 8; y++)
            {
                for (int x = 0; x < width / 8; x++)
                {
                    //DCTDirect
                    for (int i = 0; i < ((height * width) / 64); i++)
                    {
                        yArray = DCT.DCTdirect(yarry[i]);
                        uArray = DCT.DCTdirect(uarry[i]);
                        vArray = DCT.DCTdirect(varry[i]);
                    }

                    //convert float array to byte array
                    byte[,] yArrayB = new byte[width, height];
                    byte[,] uArrayB = new byte[width, height];
                    byte[,] vArrayB = new byte[width, height];

                    for (int i = 0; i < width / 8; i++)
                    {
                        for (int j = 0; j < height / 8; j++)
                        {
                            yArrayB[i, j] = (byte)yArray[i, j];
                            uArrayB[i, j] = (byte)uArray[i, j];
                            vArrayB[i, j] = (byte)vArray[i, j];
                        }
                    }

                    for (int i = 0; i < 8; i++)
                    {
                        for (int j = 0; j < 8; j++)
                        {
                            yArrayEnde[i * x, j *y] = yArrayB[i, j];
                            uArrayEnde[i * x, j *y] = uArrayB[i, j];
                            vArrayEnde[i * x, j *y] = vArrayB[i, j];
                        }
                    }


                    byte[] yArrayB2 = new byte[64];
                    byte[] uArrayB2 = new byte[64];
                    byte[] vArrayB2 = new byte[64];

                    for (int i = 0; i < 8; i++)
                    {
                        for (int j = 0; j < 8; j++)
                        {
                            yArrayB2[ZigZagTable2[i * j]] = yArrayB[i, j];
                            uArrayB2[ZigZagTable2[i * j]] = uArrayB[i, j];
                            vArrayB2[ZigZagTable2[i * j]] = vArrayB[i, j];
                        }
                    }



                    // Create Huffmantables
                    //yArrayEnde = HuffmanCalc(YDCNodes, YDCValues, yArrayB2);
                    //yArrayEnde = HuffmanCalc(YDCNodes, YACValues, yArrayB2);

                    //uArrayEnde = HuffmanCalc(CbDCNodes, CbDCValues, uArrayB2);
                    //uArrayEnde = HuffmanCalc(CbACNodes, CbACValues, uArrayB2);

                    //vArrayEnde = HuffmanCalc(CbDCNodes, CbDCValues, vArrayB2);
                    //vArrayEnde = HuffmanCalc(CbACNodes, CbACValues, vArrayB2);
                }
            }


            //create JPG head
            PictureHead.CreateJPGHead(bs, (ushort)height, (ushort)width);


            //TODO: DCT
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    bs.AddByte((byte)yArrayEnde[x, y]);
                    bs.AddByte((byte)uArrayEnde[x, y]);
                    bs.AddByte((byte)vArrayEnde[x, y]);
                }
            }

            bs.AddShort(0xffd9); //End of Picture Marker
            bs.WriteToFile(imagedest);
        }
Example #2
0
        public void PPMtoJpg(string imageSrc, string imagedest)
        {
            Bitstream bs = new Bitstream();
            PlainFormatReader pfr = new PlainFormatReader(imageSrc);
            Mathloc ml = new Mathloc();
            Picture pic = pfr.ReadPicture();

            int height = pic.Head.pixelMaxY;
            int width = pic.Head.pixelMaxX;

            Color3[,] col3 = new Color3[height,width];
            byte[] colors = new byte[3];

            //convert RGB to YUV for each pixel
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    col3[y, x] = pic.Data.PictureYX[y, x];
                    colors[0] = col3[y, x].a; // R
                    colors[1] = col3[y, x].b; // G
                    colors[2] = col3[y, x].c; // B
                    colors = ml.RGBToYUV(colors);
                    col3[y, x].a = colors[0]; // Y
                    col3[y, x].b = colors[1]; // U
                    col3[y, x].c = colors[2]; // V
                }
            }

            //convert float array to byte array
            float[,] yArray = new float[width, height]; // width and height vertauscht
            float[,] uArray = new float[width, height];
            float[,] vArray = new float[width, height];

            for (int j = 0; j < height; j++)
            {
                for (int i = 0; i < width; i++)
                {
                    yArray[i, j] = (float)col3[j, i].a;
                    uArray[i, j] = (float)col3[j, i].b;
                    vArray[i, j] = (float)col3[j, i].c;
                }
            }

            byte[,] yArrayEnde = new byte[width, height];
            byte[,] uArrayEnde = new byte[width, height];
            byte[,] vArrayEnde = new byte[width, height];

            //get float [number of 8x8][8x8]
            var yarry = aufteilen(yArray);
            var uarry = aufteilen(uArray);
            var varry = aufteilen(vArray);

            //geht beim 40x40 bild 25
            for (int y = 0; y < height/8; y ++)
            {
                for (int x = 0; x < width/8; x ++)
                {

            //DCTDirect
            for (int i = 0; i < ((height * width) / 64); i++)
            {
                yArray = DCT.DCTdirect(yarry[i]);
                uArray = DCT.DCTdirect(uarry[i]);
                vArray = DCT.DCTdirect(varry[i]);
            }

            //convert float array to byte array
            byte[,] yArrayB = new byte[width, height];
            byte[,] uArrayB = new byte[width, height];
            byte[,] vArrayB = new byte[width, height];

            for (int i = 0; i < width/8; i++)
            {
                for (int j = 0; j < height/8; j++)
                {
                    yArrayB[i, j] = (byte)yArray[i, j];
                    uArrayB[i, j] = (byte)uArray[i, j];
                    vArrayB[i, j] = (byte)vArray[i, j];
                }
            }

                    for (int i = 0; i < 8; i++)
                    {
                        for (int j = 0; j < 8; j++)
                        {
                            yArrayEnde[i * x, j * y] = yArrayB[i, j];
                            uArrayEnde[i * x, j * y] = uArrayB[i, j];
                            vArrayEnde[i * x, j  *y] = vArrayB[i, j];
                        }
                    }

                    byte[] yArrayB2 = new byte[64];
            byte[] uArrayB2 = new byte[64];
            byte[] vArrayB2 = new byte[64];

            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                yArrayB2[ZigZagTable2[i*j]] = yArrayB[i, j];
                uArrayB2[ZigZagTable2[i*j]] = uArrayB[i, j];
                vArrayB2[ZigZagTable2[i*j]] = vArrayB[i, j];
                }
            }

                    // Create Huffmantables
                    //yArrayEnde = HuffmanCalc(YDCNodes, YDCValues, yArrayB2);
                    //yArrayEnde = HuffmanCalc(YDCNodes, YACValues, yArrayB2);

                    //uArrayEnde = HuffmanCalc(CbDCNodes, CbDCValues, uArrayB2);
                    //uArrayEnde = HuffmanCalc(CbACNodes, CbACValues, uArrayB2);

                    //vArrayEnde = HuffmanCalc(CbDCNodes, CbDCValues, vArrayB2);
                    //vArrayEnde = HuffmanCalc(CbACNodes, CbACValues, vArrayB2);

                }
            }

            //create JPG head
            PictureHead.CreateJPGHead(bs, (ushort)height, (ushort)width);

            //TODO: DCT
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    bs.AddByte((byte)yArrayEnde[x, y]);
                    bs.AddByte((byte)uArrayEnde[x, y]);
                    bs.AddByte((byte)vArrayEnde[x, y]);
                }
            }

            bs.AddShort(0xffd9); //End of Picture Marker
            bs.WriteToFile(imagedest);
        }