title: Can i write a JPG image to memory with stb_image_write.h?
body: <p>I want to get the result from <code>stbir_resize_uint8</code> (from <a href="https://github.com/nothings/stb/blob/master/stb_image_resize.h" rel="nofollow noreferrer">https://github.com/nothings/stb/blob/master/stb_image_resize.h</a>) compress it to JPG and store that to a buffer without saving it to disk.</p>
  
  <p>I already have the result from <code>stbir_resize_uint8</code> as a <code>unsigned char*</code> but i don't know how to get a pointer to the data as JPG. Can this be done with stb_image_write.h or i need another library?</p>
  
  <p>I tried using <code>stbi_write_jpg_to_func</code> from <a href="https://github.com/nothings/stb/blob/master/stb_image_write.h" rel="nofollow noreferrer">https://github.com/nothings/stb/blob/master/stb_image_write.h</a> like this.</p>
  
  <p>//Image.h</p>
  
  <pre><code>static unsigned char memoryJPG[32768];
  static int thumbSize = 0;
  
  struct ImageInfo
  {
      unsigned char* data;
      int size;
  };
  
  class Image 
  {
      public:
      unsigned char* writeToMemoryAsJPG(const int quality = 90);
  
      private:
      unsigned char* mImageData = nullptr;
      int mWidth;
      int mHeight;
      int mDepth;
  };
  </code></pre>
  
  <p>//Image.cpp</p>
  
  <pre><code>ImageInfo Image::writeToMemoryAsJPG(const int quality)
  {
      auto rv = stbi_write_jpg_to_func([](void *context, void *data, int size)
      {
          if (size &gt; 32768) size = 32768;
          memcpy(memoryJPG, data, size);
          thumbSize = size;
      }, nullptr, mWidth, mHeight, mDepth, mImageData, quality);
  
      ImageInfo ii;
      ii.data = memoryJPG;
      ii.size = thumbSize;
      return ii;
  }
  </code></pre>
  
  <p>This doesn't return anything, but even if it did returned a correct result the static variables are really ugly.
  How can i write this method without any static variables?</p>