0

I have a container file which consists of a header (plain text data) and a bunch of JPGs, divided by few tenth of zeroes.

I currently have some code in Python, which do the following:

  • skip zero and header on needed;
  • seeks SOI (0xFF, 0xD8) marker;
  • seeks EOI (0xFF, 0xD9) marker;
  • using feed, from Pillow library, function to read image itself and save to write it to file;
  • repeat until all images saved.

I want to rewrite it in C++, but stuck with reading JPG, I want complete analogue of feed function: C++ library that can read JPG between start and stop from fstream or FILE *.

Is there something like this in boost::GIL? Or any other suggestion.

m0nhawk
  • 22,980
  • 9
  • 45
  • 73
  • Do you actually need to parse and decode the JPEG images, or would just extracting and saving the un-decoded files be enough? – Ilmari Karonen Aug 01 '14 at 11:21
  • @IlmariKaronen Parse and decode, so it is guarantee to be a correct JPG (can skip malformed one). I have no problem saving un-decoded files. – m0nhawk Aug 01 '14 at 11:24
  • Hmm... if GIL can't do it, [libjpeg](http://stackoverflow.com/questions/15101281/libjpeg-api-and-reference) surely can. However, it's a rather low-level C library, so it may or may not be easy (depending on how familiar you are with such things). – Ilmari Karonen Aug 01 '14 at 11:31
  • Ps. While looking for the SOI marker is a pretty good way to find possible starting points (especially if you check for the following `JFIF`/`Exif` chunk, too), just blindly looking for the EOI marker is *not* a reliable way to tell where the image ends. In particular, besides the possibility of the marker occurring in the image data by chance, it's also quite possible for a JPEG image to have *another* JPEG image (such as a thumbnail) nested inside it. The only *reliable* way to tell where the image ends is to incrementally parse the data stream until you find the end (or an error). – Ilmari Karonen Aug 01 '14 at 11:36
  • @IlmariKaronen That's why I want to also parse it, with current version (writing un-decoded data) I sometimes get malformed JPGs (thanks for pointing out why this can be, I'm not very familiar with JPG format). – m0nhawk Aug 01 '14 at 11:47
  • You could probably get away with just parsing the JPEG chunk structure, without actually decoding the image data. That should be doable in a few dozen lines of C/C++ code, even without any libraries. – Ilmari Karonen Aug 01 '14 at 12:19

1 Answers1

1

I myself would use Qt library, specifically QImageReader/QImageWritter. They can read from (and write to) QFile, which can be used to search (and eventually manipulate) the file before image reading.

Qt gives massive support to construct GUI as well. So you could easily display e.g. image preview to the user, while allowing him to type in file or even path name or easily select them via file browser widget.

If you plan more high-level work in C++, Qt is my recommended first vote general purpose library. Plenty of opportunities once you make Qt linking (which can be little tricky in non-linux-distro environments, but is doable in day(s) anyways). Qt learning base looks huge at first, but the documentation is great and the curve can be pretty steep.

pmendl
  • 136
  • 7