I am trying to extract quality layers from a JPEG 2000 filestream, which is contained in a .j2k file for testing. I am trying to do this in order to learn how to transmit the filestream, and eventually to perform Region of Interest (ROI) selection on it. I want to do these things without decoding, and right now the only utility I have is the OpenJPEG library.
I've used the image_to_j2k utility (linux) to transform a test image into a filestream contained in a .j2k file. I've then read the .j2k file into a buffer, in binary mode:
long fsize = get_file_size("img.j2k"); //This does what it's supposed to
char* buffer = new char[fsize];
ifstream in ("img.j2k", ios::in | ios::binary);
in.read(buffer, fsize); //The entire file goes into the buffer
ofstream out1("out1.j2k");
ofstream out2("out2.j2k");
ofstream out3("out3.j2k");
//This is where I try to truncate the filestream
out1.write(buffer, fsize); //Write the entire file to out1.j2k - this works
out2.write(buffer, 11032); //Write 11032 bytes of the filestream to out2.j2k - this does not to what I thought it would
out3.write(buffer, 14714); //Write 14714 bytes of the filestream to out2.j2k - this does not to what I thought it would
in.close();
out1.flush();out1.close();
out2.flush();out2.close();
out3.flush();out3.close();
The number of bytes written to the out2 and out3 files are not chosen at random - they come from an index file that OpenJPEG makes whilst compressing. The thought was that if I took the file from the beginning and read it up to a certain point where the index file tells me there is an "end_pos" marker corresponding to the end of a quality layer, I would simulate an unfinished wireless transmission of the file - this is the end goal, to transmit the file wirelessly out in the forest and show the image in progressively better quality on a handheld device or laptop somewhere else in the forest. The result of trying to use j2k_to_image on the out2.j2k and out3.j2k files is:
[ERROR] JPWL: bad tile byte size (1307053 bytes against 10911 bytes left)
[ERROR] 00000081; expected a marker instead of 1
ERROR -> j2k_to_image: failed to decode image!
Am I going about this the entirely wrong way? Not using JPEG 2000 is out of the question. Thankful for any answers, I've really gone through documentation on this thing but can't find this detail.