Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Image loses color/brightness after converting to webp
Forum Updated to NodeBB v4.3 + New Features

Image loses color/brightness after converting to webp

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 265 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MyNameIsQt
    wrote on last edited by
    #1

    Converting an image to webp changes the original color. How can I solve this problem?
    left Jpeg, right: webp
    캡처sasdff.JPG

    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;
    
    C 1 Reply Last reply
    0
    • M MyNameIsQt

      Converting an image to webp changes the original color. How can I solve this problem?
      left Jpeg, right: webp
      캡처sasdff.JPG

      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;
      
      C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      @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().

      1 Reply Last reply
      3

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved