ziggystarman 12 Junior Poster in Training

Hello

I'm looking for the correct method/way for reading & writing raw JPEG image binary data.

I am obtaining the raw data via TCP stream, the official JPEG specs say that the raw JPEG image data starts at HEX value 0xffd8 & ends at HEX value 0xffd9 as below...

Start of raw JPEG data
001bae0: 0000 0001 [B][U]ffd8[/U][/B] ffdb 0084 0003 0202 0202  ................
001baf0: 0103 0202 0203 0303 0304 0704 0404 0404  ................
001bb00: 0906 0605 070a 090b 0b0a 090a 0a0c 0d11  ................
001bb10: 0e0c 0c10 0c0a 0a0f 140f 1011 1213 1313  ................
001bb20: 0b0e 1516 1512 1611 1213 1201 0303 0304  ................
001bb30: 0304 0804 0408 120c............

End of raw JPEG data
001c5c0: 4a13 befb 8935 1d65 fd76 11ad a38e 3cb2  J....5.e.v....<.
001c5d0: b018 39ca 8246 0fa7 6a85 d115 b0ae 7923  ..9..F..j.....y#
001c5e0: 040e 8295 eced d0a9 2b59 247f [B][U]ffd9[/U][/B] 0000  ........+Y$.....

I parsed this incoming data into a string and then write this data to an image file using the method below:

myfile.open ("myimage.jpg",  ios::binary);
for(int i=0; i<raw_data.length(); ++i) {
             myfile << raw_data[i];
}
myfile.close();

I then open up this file using HEXedit to confirm that the file only contains HEX values ranging from 0xffd8 to 0xffd9 which it does & that is what is expected, however the file is not recognised as a valid JPEG image.

I was wondering if any one knew the correct way to after parsing the data how to read this raw data and what is the correct way to write this raw data back to a file if not with the same code as above.

Further more I have used the above code to read & write the raw data from a image file directly on local machine & it works fine reconstructing & writing a new image.

PS
I would be very very grateful of any help with this matter
Regards
Steve

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.