I want to get the result from stbir_resize_uint8
(from https://github.com/nothings/stb/blob/master/stb_image_resize.h) compress it to JPG and store that to a buffer without saving it to disk.
I already have the result from stbir_resize_uint8
as a unsigned char*
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?
I tried using stbi_write_jpg_to_func
from https://github.com/nothings/stb/blob/master/stb_image_write.h like this.
//Image.h
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;
};
//Image.cpp
ImageInfo Image::writeToMemoryAsJPG(const int quality)
{
auto rv = stbi_write_jpg_to_func([](void *context, void *data, int size)
{
if (size > 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;
}
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?