⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usercontrol1.cs

📁 mpeg4压缩算法程序
💻 CS
📖 第 1 页 / 共 2 页
字号:

		private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			int i, j;
			int dimX = mode;
			int dimY = mode;
			int xInit, yInit;

			xInit = xPos * mode;
			yInit = yPos * mode;
			Bitmap img = new Bitmap(dimX,dimY);
			Color clr = new Color();
			Bitmap pbImg = new Bitmap(pictureBox1.Image);

			for (i=0; i<dimY; i++)
				for (j=0; j<dimX; j++)
				{
					clr = pbImg.GetPixel((xPos*dimX)+j,(yPos*dimY)+i);
					img.SetPixel(j, i, clr);
				}

			pictureBox2.Image = img;
			if (mode == 8)
                label2.Text = "Zoom In Size: 8x8 pixels";
			else
				label2.Text = "Zoom In Size: 16x16 pixels";
		}

		private void pictureBox2_Click(object sender, System.EventArgs e)
		{}

		private void pictureBox2_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
		{}

		private void pictureBox2_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			this.Cursor = Cursors.Cross;
			
			int xPixel, yPixel;
			byte pixelR, pixelG, pixelB;
			string s2;

			Bitmap img = new Bitmap(pictureBox2.Image);

			xPixel = (e.X * mode) / 128;
			yPixel = (e.Y * mode) / 128;
			pixelR = img.GetPixel(xPixel, yPixel).R;
			pixelG = img.GetPixel(xPixel, yPixel).G;
			pixelB = img.GetPixel(xPixel, yPixel).B;

			s2 = "Pixel (R,G,B): (" + pixelR.ToString() + "," + 
					pixelG.ToString() + "," + pixelB.ToString() + ")";
			textBox3.Text = s2;
		}

		private void textBox3_TextChanged(object sender, System.EventArgs e)
		{}

		// Function that converts the image in pictureBox1 to an
		// MPEG format image.  The number of frames to write to the
		// output file is specified by nPictures.
		public void WriteMPEGSequence()
		{
			int i;
			int j, k1, k2;
			long j2;
			byte tempByte;

			int ACSIZE = 1764;
			byte[] leftoverBits = new byte[10];
			byte[] DCbits = new byte[24];
			byte[] ACbits = new byte[ACSIZE];

			int DCY, DCCR, DCCB, lastDCY, lastDCCR, lastDCCB;
			int hblock, vblock;
			byte[,] Y = new byte[16,16];
			byte[,] CR = new byte[8,8];
			byte[,] CB = new byte[8,8];
			byte[,] block = new byte[8,8];
			double[,] S = new double[8,8];
			int[,] Q = new int[8,8];
			int[] ZZ = new int[64];
			long imageBytes = 0;
			long compressedBytes = 0;
			double compressPercent = 0.0;
			string mBox = null;
			long bitRate;

			MPEGFunctions MPEG = new MPEGFunctions();

			// Retrieve image from pictureBox1
			Bitmap img = new Bitmap(pictureBox1.Image);
			string fileName;
			imageBytes = img.Height * img.Width * 3;

			// Use SaveFile dialog to specify output file name
			SaveFileDialog dlg = new SaveFileDialog();
			if (dlg.ShowDialog() != DialogResult.OK)
				return;
			fileName = dlg.FileName;

            // Create output file and Memory Stream to write encoded image to
			BinaryWriter bw = new BinaryWriter(File.Create(fileName));
			MemoryStream ms = new MemoryStream();
			FileInfo info = new FileInfo(fileName);

			this.Cursor = Cursors.WaitCursor;

			//	Set up variables to encode image into MPEG frame
			lastDCY = 128;
			lastDCCR = lastDCCB = 128;
			for (i=0; i<10; i++)
				leftoverBits[i] = 255;
			for (i=0; i<24; i++)
				ACbits[i] = 255;
			for (i=0; i<24; i++)
				DCbits[i] = 255;

			outBytes = 20;

			// Write MPEG picture and slice headers to MemoryStream
			for (i=0; i<10; i++)
				MPEG.picHeaderBits[i+32] = (byte) ((0 & (int) Math.Pow (2,9-i)) >> (9-i));
			MPEG.writeToMS(leftoverBits, MPEG.picHeaderBits, ACbits, ref outBytes);
			MPEG.writeToMS(leftoverBits, MPEG.sliceHeaderBits, ACbits, ref outBytes);

			// Do this for each 16x16 pixel block in the bitmap file
			for (vblock=0; vblock<img.Height/16; vblock++)
				for (hblock=0; hblock<img.Width/16; hblock++)
				{
					//	Write 2 bits for Macroblock header to leftoverbits
					//	leftoverbits = '1', '1';
					MPEG.writeMbHeader(leftoverBits);

					//	Fill the Y[] array with a 16x16 block of RGB values
					Y = MPEG.getYMatrix(img, vblock, hblock);
					//	Fill the CR and CB arrays with 8x8 blocks by subsampling 
					//	the RGB array
					CR = MPEG.getCRMatrix(img, vblock, hblock);
					CB = MPEG.getCBMatrix(img, vblock, hblock);

					// First calculate DCTs for the 4 Y blocks
					for (k1=0; k1<2; k1++)
						for (k2=0; k2<2; k2++)
						{
							//	Put 8x8 Y blocks into the block[] array and
							//	then calculate the DCT and quantize the result
							for (i=0; i<8; i++)
								for (j=0; j<8; j++)
									block[i,j] = Y[(k1*8 + i),(k2*8 + j)];
							S = MPEG.calculateDCT(block);
							Q = MPEG.Quantize(S);

							//	Section to differentially Huffman encode DC values
							//	DC is the diffential value for the DC coefficient
							//	lastDC is the running total of the full magnitude
							//	Then send the DC value to DCHuffmanEncode
							for (i=0; i<24; i++)
								DCbits[i] = 255;
							DCY = Q[0,0] - lastDCY;
							lastDCY += DCY;
							DCbits = MPEG.DCHuffmanEncode(DCY, MPEG.DCLumCode, MPEG.DCLumSize);

							//	Section to encode AC Huffman values
							//	Put the AC coefficients into the ACarray[]
							//	in zigzag order, then Huffman encode the
							//	resulting array.
							for (i=0; i<ACSIZE; i++)
								ACbits[i] = 255;
							ZZ = MPEG.Zigzag(Q);
							ACbits = MPEG.ACHuffmanEncode(ZZ);

							//	Write the encoded bits to the MemoryStream
							MPEG.writeToMS(leftoverBits, DCbits, ACbits, ref outBytes);
						}

					// Now calculate the DCT for the CB array and quantize
					S = MPEG.calculateDCT(CB);
					Q = MPEG.Quantize(S);

					//	Encode DC value
					for (i=0; i<24; i++)
						DCbits[i] = 255;
					DCCB = Q[0,0] - lastDCCB;
					lastDCCB += DCCB;
					DCbits = MPEG.DCHuffmanEncode(DCCB, MPEG.DCChromCode, MPEG.DCChromSize);

					//	Encode AC values
					for (i=0; i<ACSIZE; i++)
						ACbits[i] = 255;
					ZZ = MPEG.Zigzag(Q);
					ACbits = MPEG.ACHuffmanEncode(ZZ);

					//	Write the encoded bits to the MemoryStream
					MPEG.writeToMS(leftoverBits, DCbits, ACbits, ref outBytes);

					// Now calculate the DCT for the CR array and quantize
					S = MPEG.calculateDCT(CR);
					Q = MPEG.Quantize(S);
					
					// Encode DC value
					for (i=0; i<24; i++)
						DCbits[i] = 255;
					DCCR = Q[0,0] - lastDCCR;
					lastDCCR += DCCR;
					DCbits = MPEG.DCHuffmanEncode(DCCR, MPEG.DCChromCode, MPEG.DCChromSize);

					//	Encode AC values
					for (i=0; i<ACSIZE; i++)
						ACbits[i] = 255;
					ZZ = MPEG.Zigzag(Q);
					ACbits = MPEG.ACHuffmanEncode(ZZ);

					//	Write the encoded bits to the MemoryStream
					MPEG.writeToMS(leftoverBits, DCbits, ACbits, ref outBytes);
				}

			// Write EOP bits to the MemoryStream
			MPEG.writeEOP(leftoverBits, MPEG.EOPBits);				
			outBytes++;

            //	Put memory stream (which contains the encoded image) into buffer
			ms = MPEG.getMS();
			byte[] buffer = new Byte[ms.Length];
			buffer = ms.ToArray();

			//	Set MPEG Sequence Header bits to correct image size
			j = 2048;
			for (i=0; i<12; i++)
			{
				MPEG.seqHeaderBits[i+32] = (byte) ((j&img.Width) >> (11-i));
				MPEG.seqHeaderBits[i+44] = (byte) ((j&img.Height) >> (11-i));
				j >>= 1;
			}

			//	Set MPEG Sequence Header bits to bitRate value
			bitRate = ms.Length * 30 * 8 / 400;
			j2 = 131072;
			for (i=0; i<18; i++)
			{
				MPEG.seqHeaderBits[i+64] = (byte) ((j2&bitRate) >> (17-i));
				j2 >>= 1;
			}

			//	Write MPEG Sequence header to file
			for (i=0; i<12; i++)
			{
				tempByte = 0;
				for (j=0; j<8; j++)
					tempByte = (byte) (tempByte*2 + MPEG.seqHeaderBits[i*8 + j]);
				bw.Write(tempByte);
			}
	
			//	Write MPEG GOP header to file
			for (i=0; i<8; i++)
			{
				tempByte = 0;
				for (j=0; j<8; j++)
					tempByte = (byte) (tempByte*2 + MPEG.GOPHeaderBits[i*8 + j]);
				bw.Write(tempByte);
			}

			//	Fix the picture header for each MPEG frame and write 
			//	the buffer to the file
			for (i=0; i<nPictures; i++)
			{
				for (j=0; j<10; j++)
					MPEG.picHeaderBits[j+32] = (byte) ((i & (int) Math.Pow (2,9-j)) >> (9-j));
				for (j=0; j<4; j++)
				{
					tempByte = 0;
					for (k1=0; k1<8; k1++)
						tempByte = (byte) (2*tempByte + MPEG.picHeaderBits[j*8 + k1]);
					buffer[j] = tempByte;
				}
				bw.Write(buffer);
			}

			// Write the End Of Sequence header
			bw.Write((byte) 0x00);
			bw.Write((byte) 0x00);
			bw.Write((byte) 0x01);
			bw.Write((byte) 0xb7);
			bw.Close();

			this.Cursor = Cursors.Arrow;

			// Calculate some output statistics
			outBytes += 4;
			compressedBytes = outBytes - 20 - 8 - 4;
			compressPercent = 100.0 - (double) (compressedBytes * 100.0 / imageBytes);

			if (nPictures == 1)
			{
				mBox = "Original image bytes (24 color bitmap): " + imageBytes.ToString() + "\n";
				mBox += "Compressed image bytes: " + compressedBytes.ToString() + "\n";
				mBox += "Compression Percentage: " + compressPercent.ToString();
			}
			else
			{
				mBox += "Input File Size: " + inputFileLength.ToString() + "\n";
				mBox += "Output File Size: " + info.Length.ToString() + "\n";
			}

			MessageBox.Show(mBox, "Compression Statistics");
		}

		public void WriteJPEG()
		{
			// Save Loaded Image as JPEG
			Bitmap img = new Bitmap(pictureBox1.Image);
			string fileName;

			SaveFileDialog dlg = new SaveFileDialog();
			if (dlg.ShowDialog() != DialogResult.OK)
				return;
			fileName = dlg.FileName;

			img.Save(fileName, ImageFormat.Jpeg);
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -