Skip to content

Fix gamma correction on export (18.0) #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: Houdini18.0
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 50 additions & 23 deletions src/ROP/ROP_GLTF_Image.C
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ ROP_GLTF_Image::OutputImageToStream(const UT_String &filename,
file_parms.setDataType(theWorkFormat::img_data_format);
file_parms.setOption("quality", std::to_string(parms.quality).c_str());

if (!format->formatStoresColorSpace())
file_parms.adjustGammaForFormat(stat, format, IMG_DT_ANY);

IMG_File *file = IMG_File::create(os, stat, &file_parms, format);

if (!file)
Expand Down Expand Up @@ -157,14 +160,29 @@ ROP_GLTF_Image::CreateMappedTexture(
for (auto it = rasters.begin(); it != rasters.end(); it++)
{
const UT_Array<UT_SharedPtr<PXL_Raster>> &planes = *it;
if (planes.size() == 0 ||
planes[0]->getPacking() != PACK_RGB)
{
continue;
}
if (planes.size() == 0 ||
planes[0]->getPacking() != PACK_RGB)
{
continue;
}

if (new_raster->getColorSpace() == PXL_CS_UNKNOWN)
{
new_raster->setColorSpace(planes[0]->getColorSpace(), planes[0]->getColorSpaceGamma());
}
else if (new_raster->getColorSpace() != planes[0]->getColorSpace() || new_raster->getColorSpaceGamma() != planes[0]->getColorSpaceGamma())
{
UT_String error;
error.sprintf("Color space and/or gamma mismatch: %s (%f) and %s (%f)!",
PXLgetColorSpaceName(new_raster->getColorSpace()),
new_raster->getColorSpaceGamma(),
PXLgetColorSpaceName(planes[0]->getColorSpace()),
planes[0]->getColorSpaceGamma());
errormgr.AddError(UT_ERROR_MESSAGE, error);
}

auto from_channel = mappings[idx].from_channel;
auto to_channel = mappings[idx].to_channel;
auto from_channel = mappings[idx].from_channel;
auto to_channel = mappings[idx].to_channel;

fill_parms.mySource = planes[0]->getPixel(0, 0, from_channel);
fill_parms.myDest = new_raster->getPixel(0, 0, to_channel);
Expand All @@ -180,6 +198,9 @@ ROP_GLTF_Image::CreateMappedTexture(

IMG_Stat stat(xres, yres, theWorkFormat::img_data_format, IMG_RGB);

// Propagate color space and gamma from raster to the only plane
stat.getPlane(0)->setColorSpace(new_raster->getColorSpace(), new_raster->getColorSpaceGamma());

// Apply transformations
UT_Array<UT_SharedPtr<PXL_Raster>> planes { new_raster };

Expand All @@ -198,6 +219,9 @@ ROP_GLTF_Image::CreateMappedTexture(
file_parms.setDataType(theWorkFormat::img_data_format);
file_parms.setOption("quality", std::to_string(parms.quality).c_str());

if (!format->formatStoresColorSpace())
file_parms.adjustGammaForFormat(stat, format, IMG_DT_ANY);

file = IMG_File::create(os, stat, &file_parms, format);

UT_Array<PXL_Raster *> new_raster_arr {new_raster.get()};
Expand Down Expand Up @@ -375,19 +399,21 @@ ROP_GLTF_Image::GetImageRastersFromCOP(
if (color_plane)
{
color_raster = UT_SharedPtr<TIL_Raster>(
new TIL_Raster(pxl_model,
theWorkFormat::px_data_format, xres, yres),
[=](TIL_Raster *r){}
);

// Pack both A and C planes into a single raster
if (!is->getImage(color_raster.get(), time, xres, yres, *color_plane, 0,
0, 0, xres-1 , yres -1, 1.0, true))
{
return false;
}

rasters.append(color_raster);
new TIL_Raster(pxl_model, theWorkFormat::px_data_format, xres, yres),
[=](TIL_Raster *r){}
);

// Pack both A and C planes into a single raster
if (!is->getImage(color_raster.get(), time, xres, yres, *color_plane, 0,
0, 0, xres-1 , yres -1, 1.0, true))
{
return false;
}

// Propagate color space and gamma from raster to the only plane
stat.getPlane(0)->setColorSpace(color_raster->getColorSpace(), color_raster->getColorSpaceGamma());

rasters.append(color_raster);
}

return true;
Expand All @@ -410,12 +436,12 @@ ROP_GLTF_Image::ApplyTransformations(
continue;
}

exint inc = 1;
exint inc = 1;
if (packing == PACK_RGB)
inc = 3;
else if (packing == PACK_RGBA)
inc = 4;
// Otherwise the raster is interleaved so the increment is 1
// Otherwise the raster is interleaved so the increment is 1

PXL_FillParms fill_parms;
fill_parms.setDestType(raster->getFormat());
Expand All @@ -432,10 +458,11 @@ ROP_GLTF_Image::ApplyTransformations(
if (parms.roundUpPowerOfTwo || xres > parms.max_res || yres > parms.max_res)
{
xres = std::min(NextPowerOfTwo(xres), parms.max_res);
yres = std::min(NextPowerOfTwo(yres), parms.max_res);
yres = std::min(NextPowerOfTwo(yres), parms.max_res);
for (exint idx = 0; idx < rasters.size(); idx++)
{
auto dest = UT_SharedPtr<PXL_Raster>(new PXL_Raster());
dest->setColorSpace(rasters[idx]->getColorSpace(), rasters[idx]->getColorSpaceGamma());
TIL_Raster::scaleRasterToSize(dest.get(), rasters[idx].get(), xres, yres);
rasters[idx] = dest;
}
Expand Down