c++imageqtimage-processingopenexr

Conversion form RGB to HDR image format (EXR format) in C++


I am converting a RGB image to EXR format, using openexr, as follows:

int w = 1024;
int h = 768;

Array2D<Rgba> p (h, w);
QString fileName = "Penguins.jpg";
QImage Image = QImage(fileName);


QRgb c;

for (int y = 0; y < h; ++y)
{
    for (int x = 0; x < w; ++x)
    {
        c = Image.pixel(x,y);
        Rgba &p = px[y][x];
        p.r = qRed(c)/255.0;
        p.g = qGreen(c)/255.0;
        p.b = qBlue(c)/255.0;
        p.a = 1;
    }
}

However, the converted image has different color, compare to the result of the graphics editor software, such as Adobe Photoshop. Below, you can see the given image, and the converted one (opened in Adobe Photoshop): Given Image .jpg Converted Image .exr


Solution

  • The RGB values contained in most common image formats such as JPEG are gamma corrected. The RGB values in OpenEXR are linear. You need to do a conversion on each pixel to make it linear.

    The proper transformation to linear would be the sRGB formula. However for a quick test you can approximate it by taking the power of 2.2:

        p.r = pow(qRed(c)/255.0, 2.2);
        p.g = pow(qGreen(c)/255.0, 2.2);
        p.b = pow(qBlue(c)/255.0, 2.2);