public void EncodeFrame(int frameNo, Bitmap inp, IEntropyCodec c) { if (inp == null || c == null) { return; } int width = inp.Width; int height = inp.Height; if (width != frameWidth || height != frameHeight) { return; } // !!!{{ TODO: add the encoding code here // frame header: [ MAGIC_FRAME, frameNo ] c.PutBits(MAGIC_FRAME, 32); c.PutBits(frameNo, 16); for (int y = 0; y < frameHeight; y++) { for (int x = 0; x < frameWidth; x++) { byte gr = (byte)(inp.GetPixel(x, y).GetBrightness() * 255.0f); c.Put(gr); } } // !!!}} }
public Bitmap DecodeFrame(int frameNo, IEntropyCodec c) { if (c == null) { return(null); } // !!!{{ TODO: add the decoding code here // Check the frame header: uint magic = (uint)c.GetBits(32); if (magic != MAGIC_FRAME) { return(null); } int frNo = (int)c.GetBits(16); if (frNo != frameNo) { return(null); } Bitmap result = new Bitmap(frameWidth, frameHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb); for (int y = 0; y < frameHeight; y++) { for (int x = 0; x < frameWidth; x++) { int gr = c.Get(); result.SetPixel(x, y, Color.FromArgb(gr, gr, gr)); } } return(result); // !!!}} }
private void buttonEncode_Click(object sender, EventArgs e) { string fn = string.Format(textInputMask.Text, 0); if (!File.Exists(fn)) { MessageBox.Show("Invalid input files!"); return; } string cd = Directory.GetCurrentDirectory(); SaveFileDialog sfd = new SaveFileDialog(); sfd.Title = "Save Video File"; sfd.Filter = "BIN Files|*.bin"; sfd.AddExtension = true; sfd.FileName = videoFileName; if (sfd.ShowDialog() != DialogResult.OK) { return; } videoFileName = Path.GetFullPath(sfd.FileName); FileStream fs = new FileStream(videoFileName, FileMode.Create); VideoCodec vc = new VideoCodec(); Directory.SetCurrentDirectory(cd); labelSpeed.Text = "Encoding.."; Stopwatch sw = new Stopwatch(); sw.Start(); Bitmap frameImage = (Bitmap)Image.FromFile(fn); IEntropyCodec s = vc.EncodeHeader(frameImage.Width, frameImage.Height, (float)numericFps.Value, fs); int i = 0; do { vc.EncodeFrame(i, frameImage, s); frameImage.Dispose(); frameImage = null; // next frame: fn = string.Format(textInputMask.Text, ++i); if (!File.Exists(fn)) { break; } frameImage = (Bitmap)Image.FromFile(fn); }while (true); if (frameImage != null) { frameImage.Dispose(); } s.Close(); labelSpeed.Text = string.Format("Encoded {0} frames in {1:f} s!", i, (float)(sw.ElapsedMilliseconds * 0.001)); sw.Stop(); fs.Close(); }