我用 Jpeg Turbo 编写了一个函数来允许它进行压缩,它看起来像这样:
bool JPEGCompress::compress(const uint8_t* data, size_t width, size_t height, ImageFrame::image_type type, std::string& out)
{
// Prealloc if not
out.reserve(1 << 24); // Preallocate 16 MB output buffer
// Allocate memory for compressed output
int subsamp = TJSAMP_420;
auto outsize_max = tjBufSize(width, height, subsamp);
if (size_t(-1) == outsize_max) {
fmt::print("Size out of bounds: w={} h={} ss={}\n", width, height, subsamp);
return false;
}
out.resize(outsize_max);
// Select Pixel Format
int pix_fmt = -1;
if(type == ImageFrame::image_type::ImageColor)
pix_fmt = TJPF_RGB;
else if(type == ImageFrame::image_type::ImageGray)
pix_fmt = TJPF_GRAY;
else{
fmt::print("Compression doesn't work for this format: {}", ImageFrame::convEnum(type));
return false;
}
// Compress
auto outptr = (uint8_t*) out.data();
auto outsize = outsize_max;
if (-1 == tjCompress2(
compressor_, data, width, 0, height, pix_fmt,
&outptr, &outsize, subsamp, quality_, TJFLAG_NOREALLOC)) {
fmt::print("Error encoding image: {}\n", tjGetErrorStr2(compressor_));
return false;
}
out.resize(outsize);
return true;
}
它适用于 RGB 图像,但不适用于灰度图像
Error encoding image: Unsupported color conversion request
我不知道我在图书馆做错了什么