public FrameData(byte[] frameData, ImageSetting imgSetting) { RPixels = new List<byte>(); GPixels = new List<byte>(); BPixels = new List<byte>(); int rowLen = imgSetting.Height / imgSetting.BlockSize; int rowBytelen = rowLen / 8; if (rowLen % 8 != 0) rowBytelen++; int pos = 0; List<byte> rowData = new List<byte>(); for (int i = 0; i < rowBytelen; i++) { rowData.Add(frameData[i]); pos++; } BitArray rowBits = new BitArray(rowData.ToArray()); rowChangeBits = new BitArray(rowLen); int columnChange = 0; for (int i = 0; i < rowLen; i++) { rowChangeBits[i] = rowBits[i]; if (rowBits[i]) columnChange++; } int colNo = imgSetting.Width / imgSetting.BlockSize; int columnByteLen = columnChange * colNo / 8; if (columnChange * colNo % 8 != 0) columnByteLen++; List<byte> columnData = new List<byte>(); for (int j = 0; j < columnByteLen; j++) { columnData.Add(frameData[pos]); pos++; } int frameDataLen = frameData.Length; int colorSize = (frameDataLen - pos) / 3; if ((frameDataLen - pos) % 3 != 0) Console.WriteLine("not divisible bye 3" + (frameDataLen - pos) % 3); BitArray columnBits = new BitArray(columnData.ToArray()); columnChangeBits = new BitArray(columnChange * colNo); for (int i = 0; i < (columnChange * colNo); i++) { columnChangeBits[i] = columnBits[i]; } for (int i = 0; i < colorSize; i++) { RPixels.Add(frameData[pos]); pos++; } for (int i = 0; i < colorSize; i++) { GPixels.Add(frameData[pos]); pos++; } for (int i = 0; i < colorSize; i++) { BPixels.Add(frameData[pos]); pos++; } }
/* ImageFrame extractPixels(Image image, ImageSetting imageSetting) { ImageFrame imageFrame = new ImageFrame(); Bitmap source = new Bitmap(image); int RPixel; int GPixel; int BPixel; for (int i = 0; i < image.Width; i += imageSetting.BlockSize) for (int j = 0; j < image.Height; j += imageSetting.BlockSize) { RPixel = 0; GPixel = 0; BPixel = 0; for (int k = i; k < imageSetting.BlockSize; k++) for (int l = j; l < imageSetting.BlockSize; l++) { RPixel += source.GetPixel(k, l).R; GPixel += source.GetPixel(k, l).G; BPixel += source.GetPixel(k, l).B; } RPixel /= imageSetting.BlockSize; GPixel /= imageSetting.BlockSize; BPixel /= imageSetting.BlockSize; imageFrame.RPixels.Add(toByte(RPixel)); imageFrame.GPixels.Add(toByte(GPixel)); imageFrame.BPixels.Add(toByte(BPixel)); } return imageFrame; } */ Image reProcessFrame( Image prevImage, FrameData frameData, ImageSetting imageSetting) { Image image; Bitmap bitmap = new Bitmap(imageSetting.Width,imageSetting.Height); Bitmap prevBitmap = new Bitmap(prevImage); int RPixel; int GPixel; int BPixel; int columnChangeNum =0; int row =0; int changedBlockNum =0; bool rowChanges = false; bool blockChange = false; for (int i = 0; i < imageSetting.Height; i += imageSetting.BlockSize) { if (frameData.rowChangeBits[i]) { rowChanges = true; row++; } for (int j = 0; j < imageSetting.Width; j += imageSetting.BlockSize) { columnChangeNum = row * imageSetting.Width/imageSetting.BlockSize + j; blockChange = frameData.columnChangeBits[columnChangeNum]; for (int k = i; k < imageSetting.BlockSize; k++) for (int l = j; l < imageSetting.BlockSize; l++) { if (rowChanges && blockChange) { RPixel = frameData.RPixels[changedBlockNum]; GPixel = frameData.GPixels[changedBlockNum]; BPixel = frameData.BPixels[changedBlockNum]; ; changedBlockNum++; } else { RPixel = prevBitmap.GetPixel(k, l).R; GPixel = prevBitmap.GetPixel(k, l).G; BPixel = prevBitmap.GetPixel(k, l).B; } Color newcol = Color.FromArgb(RPixel, GPixel, BPixel); bitmap.SetPixel(k, l, newcol); } } } image = (Image)bitmap; return image; }
/*private void cmdImageSize_Click(object sender, EventArgs e) { try { byte[] buffer = new byte[1024]; int iRx = socClient.Receive(buffer); char[] chars = new char[iRx]; System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder(); int charLen = d.GetChars(buffer, 0, iRx, chars, 0); String szData = new String(chars); sze = Convert.ToInt32(szData); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } */ private void receiveImage(Object obj) { int time = 0; int FrameNumber; int currentFrame; char state = 'd'; byte special = 0; int recSize = 0; byte[] buffer = new byte[1024]; int prevFrame = 0; Boolean receive = false; Image image; FrameData frameData; int ticks = Environment.TickCount; try { socClient.ReceiveBufferSize = 1024; socClient.SendBufferSize = 1024; while (true) { recSize = 0; lock (this) { { FrameNumber = FRAMENUMBER; receive = onReceive; //currentFrame = (currentFrameNumber + FrameNumber) % FrameNumber; } ticks = Environment.TickCount; try { imageSetting = recImageSetting(); // MessageBox.Show(imageSetting.Width.ToString() + "X" + imageSetting.Height.ToString() + "X" + imageSetting.BlockSize.ToString() + "X" + imageSetting.ColorBits.ToString() + "ccccc"); } catch(Exception ex) { MessageBox.Show("Error in receiving imageSetting... in CCCCCCCCCCCCCCCCCCCCCCC"); } while (receive) { lock (this) { currentFrame = (currentFrameNumber + FrameNumber) % FrameNumber; // state = states[currentFrame]; } prevFrame = (currentFrame - 1 + FrameNumber) % FrameNumber; try { special = recSpecial(); // MessageBox.Show("ccccccccccccccSpecial" + special.ToString()); } catch (Exception ex) { MessageBox.Show("Error on receiving special" + ex.ToString()); } if (special == 0) { state = 'N'; } else if (special == 1) { state = 'P'; } else if (special == 2) { state = 'M'; } try { recSize = recieveSize(); // MessageBox.Show("Receive video size + " + recSize.ToString()); } catch (FormatException ex) { Console.WriteLine("NumberFormateException"); } List<byte> totalByte = new List<byte>(); int recQue = recSize / 1024; int recRem = recSize % 1024; int total = recQue * 1024 + recRem; Console.WriteLine("frame size in kb = ...." + recQue + "remainder = " + recRem + " and total = "+total); int count = 0; while(count!=recQue) { try { buffer = new byte[1024]; buffer = recData(); foreach (byte b in buffer) { totalByte.Add(b); } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } count++; } //remaining picture ko lagi........ buffer = new byte[recRem]; int iRx = socClient.Receive(buffer); foreach (byte b in buffer) { totalByte.Add(b); } MessageBox.Show("ccccccccccccafter receiving actual data" + recSize.ToString() + " " + totalByte[0] + " " + totalByte[recSize - 1]); Console.WriteLine(" Before calling sending bact to endccccccccccccccccccccccccc...."); try { sendData(BitConverter.GetBytes(special)); } catch (Exception ex) { Console.WriteLine("sending end error in cccccccccccccccccccccccccc" + ex.ToString()); } Console.WriteLine("ccccccccccccccccccccccccccccccccccccccccccc before conversion to image..."); if (state == 'N') { lock (this) { images[currentFrame] = images[prevFrame]; //imageFrames[currentFrame] = imageFrames[prevFrame]; framesData[currentFrame] = null; states[currentFrame] = 'N'; } } else if (state == 'P') { frameData = new FrameData(totalByte.ToArray(), imageSetting); lock (this) { framesData[currentFrame] = frameData; states[currentFrame] = 'P'; } } else if (state == 'M') { image = byteArrayToImage(totalByte.ToArray()); lock (this) { images[currentFrame] = image; framesData[currentFrame] = null; states[currentFrame] = 'M'; } } //Console.WriteLine("ccccccccccccccccccccccccccccccccccccccccccc After conversion to image...."); //Console.WriteLine("ccccccccccccccccccccccccccccccccccccccccccc after pictureBox1 called...."); //byData = System.Text.Encoding.ASCII.GetBytes("SHSHSHSH"); //Console.WriteLine("sending shshssh to server....."); //sendData(byData); //Console.WriteLine("after sending shshshshs to server........"); lock (this) { // images[currentFrame] = img; // states[currentFrame] = 'p'; currentFrameNumber = (currentFrameNumber + 1) % FrameNumber; } time = 1000 / FrameNumber - (Environment.TickCount - ticks); if (time > 0) Thread.Sleep(time); //else // Console.WriteLine("cccccccccccccccccccccccccccccccccccccccccccno sleeping..." + (-time).ToString()); ticks = Environment.TickCount; } } } } catch (SocketException ex) { MessageBox.Show("receiveImage() function \n"+"server has been closed.."); } catch (Exception ex) { MessageBox.Show("Error in receive Image!" + ex.ToString()); } }
ImageSetting recImageSetting() { byte[] bytes = recData(16); ImageSetting imageSetting = new ImageSetting(bytes); //imageSetting.Width = recSize(); //imageSetting.Height = recSize(); //imageSetting.BlockSize = recSize(); //imageSetting.ColorBits = recSize(); return imageSetting; }
private void ClientForm_Load(object sender, EventArgs e) { txtIPAddress.Text = getLocalIp(); onReceive = false; onConnect = false; images = new Image[FRAMENUMBER]; //imageFrames = new ImageFrame[FRAMENUMBER]; framesData = new FrameData[FRAMENUMBER]; imageSetting = new ImageSetting(); states = new char[FRAMENUMBER]; currentFrameNumber = 0; callBackReceive = new WaitCallback(receiveImage); callBackProcess = new WaitCallback(reProcessImage); callBackDisplay = new WaitCallback(displayImage); }