title: Read raw data from jpeg file using jpeg_read_raw_data function
body: <p>Sorry for my bad english...</p>
  
  <p>So, I try to decompress 128x128 jpeg image that was created in Microsoft Paint. The image is all black.</p>
  
  <p>I use xawtv sources as example to create my own jpeg to yuv decompressor (<a href="http://xawtv.sourcearchive.com/documentation/3.95.dfsg.1/conv-mjpeg_8c-source.html" rel="nofollow noreferrer">http://xawtv.sourcearchive.com/documentation/3.95.dfsg.1/conv-mjpeg_8c-source.html</a>). Decompressor works only with 4:2:0 level of yuv subsampling.</p>
  
  <p>Application throws access violation in jpeg_read_raw_data function when i is equal to 112. Can anybody help to resolve this problem?</p>
  
  <pre><code>#include &lt;fstream&gt;
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;vector&gt;
  
  #include "jpeglib.h"
  
  typedef std::vector&lt;unsigned char&gt; buffer;
  
  class decompressor
  {
  public:
  
      decompressor(buffer&amp; ibuf)
            : ibuf_(ibuf), yuvbuf_(3)
      {
          info_.err = jpeg_std_error(&amp;e_);
  
          jpeg_create_decompress(&amp;info_);
  
          jpeg_mem_src(&amp;info_, &amp;ibuf_[0], ibuf_.size());
      }
  
      virtual ~decompressor()
      {
          jpeg_destroy_decompress(&amp;info_);
      }
  
      virtual void decompress(buffer&amp; obuf)
      {
          jpeg_read_header(&amp;info_, 1);
  
          info_.raw_data_out = 1;
  
          jpeg_start_decompress(&amp;info_);
  
          obuf.resize(
              info_.output_width *
              info_.output_height *
              info_.output_components);
  
          for (std::size_t i = 0; i &lt; yuvbuf_.size(); ++i)
          {
              yuvbuf_[i].resize(info_.output_width);
              yuvptr_[i] = &amp;yuvbuf_[i][0];
          }
  
          unsigned char* row = &amp;obuf[0];
  
          for (std::size_t i = 0; i &lt; info_.output_height; ++i, row += info_.output_width)
          {
              yuvptr_[0][i] = row;
          }
  
          row = &amp;obuf[0] + info_.output_width * info_.output_height;
  
          for (std::size_t i = 0; i &lt; info_.output_height; i += 2, row += info_.output_width / 2)
          {
              yuvptr_[1][i / 2] = row;
          }
  
          row = &amp;obuf[0] + info_.output_width * info_.output_height * 5 / 4;
  
          for (std::size_t i = 0; i &lt; info_.output_height; i += 2, row += info_.output_width / 2)
          {
              yuvptr_[2][i / 2] = row;
          }
  
          for (std::size_t i = 0; i &lt; info_.output_height; i += 16)
          {
              jpeg_read_raw_data(&amp;info_, yuvptr_, 16);
  
              yuvptr_[0] += 16;
              yuvptr_[1] += 8;
              yuvptr_[2] += 8;
          }
  
          jpeg_finish_decompress(&amp;info_);
      }
  
  protected:
  
      jpeg_decompress_struct info_;
  
      jpeg_error_mgr e_;
  
      buffer&amp; ibuf_;
  
      std::vector&lt;
          std::vector&lt;unsigned char*&gt;
      &gt; yuvbuf_;
  
      unsigned char** yuvptr_[3];
  };
  
  int main()
  {
      std::string filename;
  
      filename = "i.jpg";
  
      std::fstream ifs(filename.c_str(), std::ios::in | std::ios::binary);
      if (!ifs)
      {
          std::cout &lt;&lt; "can not open file " &lt;&lt; filename;
          return 1;
      }
  
      filename = "o.dat";
  
      std::fstream ofs(filename.c_str(), std::ios::out | std::ios::binary);
      if (!ofs)
      {
          std::cout &lt;&lt; "can not open file " &lt;&lt; filename;
          return 1;
      }
  
      std::size_t size;
  
      ifs.seekg(0, std::ios::end);
      size = ifs.tellg();
      ifs.seekg(0);
  
      buffer ibuf(size);
      buffer obuf;
  
      ifs.read((char*)&amp;ibuf[0], ibuf.size());
  
      decompressor d(ibuf);
  
      d.decompress(obuf);
  
      ofs.write((char*)&amp;obuf[0], obuf.size());
  
      return 1;
  }
  </code></pre>