[Solved] Read an image or pdf using C++ without external library


There is no difference what file you are reading opened in binary mode, there is only difference is how you should interpret the data you get from the file.

It’s significantly better to take ready to use library like e.g. libjpeg or whatever. There are plenty of them. But If you really want to do this, at first you should define suitable structures and constants (see links below) to make code to be convinient and useable. Then you just read the data and try to interpret it step by step. The code below is just pseudo code, I didn’t compile it.

#include <fstream>

// define header structure
struct jpeg_header 
{ 
  enum class marker: unsigned short { eoi = 0xffd8, sof0 = 0xffc0 ... };
  ...
};

bool is_eoi(unsigned short m) { return jpeg_header::eoi == m; }

jpeg_header read_jpeg_header(const std::string& fn)
{
    std::ifstream inf(fn, std::ifstream::binary);
    if (!inf) 
    {
        throw std::runtime_error("Can't open file: " + fn);
    }

    inf.exceptions(std::ifstream::failbit | std::ifstream::eofbit);

    unsigned short marker = inf.get() << 8;
    marker |= inf.get();
    if (!is_eoi(marker))
    {
        throw std::runtime_error("Invalid jpeg header");
    }
    ...

    jpeg_header header;
    // read further and fill header structure
    ...
    return header;
}

To read huge block of data use ifstream::read(), ifstream::readsome() methods. Here is the good example http://en.cppreference.com/w/cpp/io/basic_istream/read.
Those functions also work faster then stream iterators. It’s also better define your own exception classes derived from std::runtime_error.

For details on file formats you interested in look here

solved Read an image or pdf using C++ without external library