Пример #1
0
        /*
        compresses a pframe
            */
        public void compressPframe(Block[,] Yblocks, Block[,] Cbblocks, Block[,] Crblocks, Point[,] vectors, int width, int height) {
            int horizontalBlocks = Yblocks.GetLength(0);
            int verticalBlocks = Yblocks.GetLength(1);

            Block[,] YdctBlocks = new Block[horizontalBlocks,verticalBlocks];
            Block[,] CbdctBlocks = new Block[horizontalBlocks, verticalBlocks];
            Block[,] CrdctBlocks = new Block[horizontalBlocks, verticalBlocks];

            List<int> YSaveBuffer = new List<int>();
            List<int> CbSaveBuffer = new List<int>();
            List<int> CrSaveBuffer = new List<int>();

            for (int y = 0; y < verticalBlocks; y++)
            {
                for (int x = 0; x < horizontalBlocks; x++)
                {
                    //Debug.WriteLine("-----------Starting------------");
                    YdctBlocks[x, y] = new Block();
                    CbdctBlocks[x, y] = new Block();
                    CrdctBlocks[x, y] = new Block();

                    for (int v = 0; v < 8; v++)
                    {
                        for (int u = 0; u < 8; u++)
                        {

                            YdctBlocks[x, y].set(u, v, applyDCTFormula(Yblocks[x, y], u, v));
                            CbdctBlocks[x, y].set(u, v, applyDCTFormula(Cbblocks[x, y], u, v));
                            CrdctBlocks[x, y].set(u, v, applyDCTFormula(Crblocks[x, y], u, v));

                        }
                        //Debug.WriteLine("");
                    }

                    YdctBlocks[x, y] = applyQuantization(YdctBlocks[x, y], luminance);
                    CbdctBlocks[x, y] = applyQuantization(CbdctBlocks[x, y], chrominance);
                    CrdctBlocks[x, y] = applyQuantization(CrdctBlocks[x, y], chrominance);


                    if (x == 0 && y == 0)
                    {
                        for (int q = 0; q < 8; q++)
                        {
                            for (int w = 0; w < 8; w++)
                            {
                                Debug.Write(YdctBlocks[x, y].get(q, w) + ",");
                                //Debug.Write(CbdctBlocks[x, y].get(q, w) + ",");
                            }
                            Debug.WriteLine("");
                        }

                        Debug.WriteLine("-------------------------------------");
                    }
                    int[] Yzig = applyZigZag(YdctBlocks[x, y]);


                    int[] Yencoded = runLengthEncode(Yzig);
                    YSaveBuffer.Add(Yencoded.Length);
                    for (int i = 0; i < Yencoded.Length; i++)
                    {
                        YSaveBuffer.Add(Yencoded[i]);
                    }

                    int[] Cbzig = new int[128];
                    int[] Crzig = new int[128];

                    int[] Cbencoded = new int[128];
                    int[] Crencoded = new int[128];


                    Cbzig = applyZigZag(CbdctBlocks[x, y]);
                    Crzig = applyZigZag(CrdctBlocks[x, y]);

                    Cbencoded = runLengthEncode(Cbzig);
                    Crencoded = runLengthEncode(Crzig);

                    //first save the length of the run length, so we know how far to read later
                    CbSaveBuffer.Add(Cbencoded.Length);
                    for (int i = 0; i < Cbencoded.Length; i++)
                    {
                        CbSaveBuffer.Add(Cbencoded[i]);
                    }
                    CrSaveBuffer.Add(Crencoded.Length);
                    for (int i = 0; i < Crencoded.Length; i++)
                    {
                        CrSaveBuffer.Add(Crencoded[i]);
                    }


                    if (x == 0 && y == 0)
                    {
                        for (int i = 0; i < Yzig.GetLength(0); i++)
                        //for (int i = 0; i < Cbzig.GetLength(0); i++)
                        {
                            Debug.Write((Yzig[i]) + ",");
                            //Debug.Write((Cbzig[i]) + ",");
                        }

                        Debug.WriteLine("");
                        Debug.WriteLine("-------------------------------------");



                        for (int i = 0; i < Yencoded.GetLength(0); i++)
                        //for (int i = 0; i < Cbencoded.GetLength(0); i++)
                        {
                            Debug.Write((Yencoded[i]) + ",");
                            //Debug.Write((Cbencoded[i]) + ",");
                        }

                        Debug.WriteLine("");
                        Debug.WriteLine("-------------------------------------");
                    }

                    //saveFile();
                    //--------------------------------------UNDO COMPRESSION---------------------------------------------------------------------




                }//x
            }//y            

            int position = 0;
            int totalCount = YSaveBuffer.Count + CbSaveBuffer.Count + CrSaveBuffer.Count;
            int[] toSave = new int[totalCount + 2];
            for (int i = 0; i < YSaveBuffer.Count; i++)
            {
                toSave[position++] = YSaveBuffer[i];
            }
            toSave[position++] = 127;
            for (int i = 0; i < CbSaveBuffer.Count; i++)
            {
                toSave[position++] = CbSaveBuffer[i];
            }
            toSave[position++] = 127;
            for (int i = 0; i < CrSaveBuffer.Count; i++)
            {
                toSave[position++] = CrSaveBuffer[i];
            }

            FileFunctions.saveCompressed(toSave, width, height);
            decodeSavePFrameArray(toSave, width, height);
            //decompress(Yencoded, Cbencoded, Crencoded, width, height);
        }
Пример #2
0
        /*
        Creates a bitmap from an array of 8*8 blocks.
            */
        public Bitmap createBitmapFromBlocks(Block[,] blocks, int imageWidth, int imageHeight) {
            Bitmap image = new Bitmap(imageWidth, imageHeight);
            int blocksVertical = blocks.GetLength(0);
            int blocksHorizontal  = blocks.GetLength(1);

            for (int y=0; y<blocksHorizontal; y++) {
                for (int x=0; x<blocksVertical; x++) {

                    for (int blockY=0; blockY<8; blockY++) {
                        for (int blockX=0; blockX<8; blockX++) {
                            if ( y * 8 + blockY >= imageHeight) continue;
                            if (x * 8 + blockX >= imageWidth) continue;
                            image.SetPixel(x*8+blockX, y*8+blockY, Color.FromArgb((int)blocks[x,y].get(blockX,blockY), (int)blocks[x, y].get(blockX, blockY), (int)blocks[x, y].get(blockX, blockY)));
                        }
                    }

                }
            }

            return image;
        }