我想从stbir_resize_uint8
(来自https://github.com/nothings/stb/blob/master/stb_image_resize.h)获得结果,将其压缩为 JPG 并将其存储到缓冲区而不将其保存到磁盘。
我已经得到了结果,stbir_resize_uint8
但unsigned char*
我不知道如何以 JPG 格式获取指向数据的指针。这可以用 stb_image_write.h 完成还是我需要另一个库?
我尝试stbi_write_jpg_to_func
从https://github.com/nothings/stb/blob/master/stb_image_write.h像这样使用。
//图像.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;
};
//图像.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;
}
这不会返回任何东西,但即使它确实返回了正确的结果,静态变量也非常难看。我如何在没有任何静态变量的情况下编写此方法?