Image loses color/brightness after converting to webp
Unsolved
General and Desktop
-
Converting an image to webp changes the original color. How can I solve this problem?
left Jpeg, right: webp
QImageReader reader(m_strBkgndImagePath); std::shared_ptr<QImage> image = std::make_shared<QImage>(reader.read()); int width = image->size().width(); int height = image->size().height(); WebPConfig config; WebPConfigPreset(&config, WEBP_PRESET_DEFAULT, 80/*quality*/); config.lossless = 0; // Use lossy compression config.alpha_quality = 100; // Maximum alpha quality config.use_sharp_yuv = 1; // Use RGB color space // Set up memory writer WebPMemoryWriter writer; WebPMemoryWriterInit(&writer); WebPPicture pic; WebPPictureInit(&pic); // Set up picture pic.use_argb = 1; pic.width = width; pic.height = height; pic.colorspace = WEBP_YUV420; pic.writer = WebPMemoryWrite; pic.custom_ptr = &writer; // Copy image data to picture const uint8_t* src = reinterpret_cast<const uint8_t*>(image->constBits()); const int src_stride = width * 4; if (!WebPPictureImportRGBA(&pic, src, src_stride)) { WebPPictureFree(&pic); return false; } // Compress the image if (!WebPEncode(&config, &pic)) { WebPPictureFree(&pic); return false; } // Write the compressed image data to file QFileInfo fileInfo(m_strBkgndImagePath); QString webpFileName = fileInfo.path() + QDir::separator() + fileInfo.baseName() + ".webp"; cout << webpFileName.toStdString() << endl; FILE* file = fopen(webpFileName.toStdString().c_str(), "wb"); if (!file) { WebPPictureFree(&pic); return false; } const size_t output_size = writer.size; if (output_size > 0) { const size_t bytes_written = fwrite(writer.mem, 1, output_size, file); if (bytes_written != output_size) { fclose(file); WebPPictureFree(&pic); return false; } m_bkgndHeader.ImageSize = output_size; } fclose(file); WebPPictureFree(&pic); return true;
-
@MyNameIsQt I would be guessing that the QImage internal data is stored in ARGB layout (A = 0xff probably), and you are asking the foreign library to treat that as RGBA. You lose the blue and get a strong red. Take a look at
QImage::format()
.