Last active
August 29, 2015 14:02
-
-
Save muhammadyaseen/322c38e942bb98aa8574 to your computer and use it in GitHub Desktop.
Demonstrates creation on data packets from JPEG and reassembling of data packets into JPEG
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.IO; | |
namespace JPEGProcess | |
{ | |
/// <summary> | |
/// Can be used to: | |
/// 1. show HEX representation of jpeg's on console | |
/// 2. create data packets of 32bit from jpeg image | |
/// 3. reassemble data packets into a valid jpeg image | |
/// 4. show hex representaion of byte array on console | |
/// </summary> | |
/// | |
/// <author> | |
/// Muhammad Yaseen | |
/// </author> | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.WriteLine("Select : "); | |
Console.WriteLine("1. Create packets from existing JPEG "); | |
Console.WriteLine("2. Create JPEG from packets "); | |
string choice = Console.ReadLine(); | |
if (choice == "1") | |
{ | |
createPackets(@"E:\created.jpg"); | |
} | |
if (choice == "2") | |
{ | |
reconstructImageFromPackets(@"E:\packets"); | |
} | |
} | |
private static void reconstructImageFromPackets(string packetPath) | |
{ | |
//get all files under the 'packetPath' directory, each file represents a 32-bit data packet. | |
string[] camdataFiles = Directory.GetFiles(packetPath, "*.camdata").ToArray(); //10 packets for testing purpose | |
List<byte> imageBytes = new List<byte>(); | |
byte[] imageBytesArray = new byte[camdataFiles.Length * 2]; | |
foreach (string file in camdataFiles) | |
{ | |
//we have to read each data packet, remove unecessary data, and reassemble the image | |
Console.WriteLine("Reading : " + file); | |
BinaryReader camdataReader = new BinaryReader(File.OpenRead(file)); | |
short serialNumber = camdataReader.ReadInt16(); //first two bytes are serial | |
Console.WriteLine( "This is packet number : " + serialNumber); | |
byte[] imageData = new byte[2]; //next two bytes are image data | |
camdataReader.Read(imageData, 0, 2); | |
string packetContents = BitConverter.ToString(imageData); | |
Console.WriteLine("Packet Contents : {0}", packetContents.Replace('-', ' ')); | |
imageBytes.AddRange(imageData); | |
//add bytes to array in proper location | |
//we need to store the image data read so far, and when all files have been read, we write the read data to a single file. | |
imageBytesArray[serialNumber * 2 - 2] = imageData[0]; | |
imageBytesArray[serialNumber * 2 - 1] = imageData[1]; | |
Console.WriteLine(); | |
} | |
Console.WriteLine("==== packet Hex ====="); | |
showHEXofByteData(imageBytesArray); | |
Console.WriteLine("==== direct Hex ====="); | |
showHEXofJPEG(@"E:\created.jpg"); | |
Console.WriteLine("Writing packets to image file"); | |
BinaryWriter jpegWriter = new BinaryWriter(File.Open(@"E:\reassembled\output.jpg", FileMode.Create)); | |
jpegWriter.Write(imageBytesArray.ToArray()); | |
jpegWriter.Flush(); | |
jpegWriter.Close(); | |
} | |
private static void createPackets(string fileName) | |
{ | |
/* Packet structure: | |
* | |
* First 2 bytes are for serial number. | |
* Last two bytes are image data | |
* | |
* Packet: | |
* +===========================================+ | |
* | | | | |
* | 2 byte serial | 2 bytes image data | | |
* | | | | |
* +===========================================+ | |
*/ | |
Stream jpegStream = File.OpenRead(fileName); | |
ushort readBytes = 0; | |
ushort packetsCreated = 0; | |
while ( readBytes < jpegStream.Length ) | |
{ | |
//create a packet | |
Stream packet = File.Open(@"E:\packets\packet-" + ++packetsCreated + ".camdata", FileMode.Create); | |
//and a writerm which will write data to that packet | |
BinaryWriter packetWriter = new BinaryWriter(packet); | |
//first 2-bytes is a 'ushort' type designating the Serial Number | |
packetWriter.Write(packetsCreated); | |
//Next two bytes are Actual Image Data | |
byte[] imageBytes = new byte[2]; | |
jpegStream.Read(imageBytes, 0 , 2); | |
packetWriter.Write(imageBytes); | |
packetWriter.Flush(); | |
//packetWriter.Close(); | |
packet.Flush(); | |
//packet.Close(); | |
readBytes += 2; | |
} | |
jpegStream.Close(); | |
} | |
private static void showHEXofJPEG(string fileName) | |
{ | |
//Get the hex representation of specified image | |
var converted = BitConverter.ToString(File.ReadAllBytes(fileName)); | |
//remove the '-' character inserted by ToString() | |
List<string> lines = converted.Split(new char[] { '-' }).ToList(); | |
string line = String.Empty; | |
//print in lines of 15 HEX chars | |
for (int i = 0; i < lines.Count(); i++) | |
{ | |
Console.Write(lines[i] + " "); | |
if ((i + 1) % 15 == 0) Console.WriteLine(); | |
} | |
Console.WriteLine(); | |
} | |
private static void showHEXofByteData(byte[] bytes) | |
{ | |
//Get the hex representation of specified image | |
var converted = BitConverter.ToString(bytes); | |
//remove the '-' character inserted by ToString() | |
List<string> lines = converted.Split(new char[] { '-' }).ToList(); | |
string line = String.Empty; | |
//print in lines of 15 HEX chars | |
for (int i = 0; i < lines.Count(); i++) | |
{ | |
Console.Write(lines[i] + " "); | |
if ((i + 1) % 15 == 0) Console.WriteLine(); | |
} | |
Console.WriteLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment