Skip to content

Commit

Permalink
The colour space of raw import data may now be set. If the raw data i…
Browse files Browse the repository at this point in the history
…s in linear space it is converted to sRGB on import so it displays/saves correctly.
  • Loading branch information
bluescan committed Jun 25, 2024
1 parent 2becc5b commit 2ba09b7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 deletions.
8 changes: 4 additions & 4 deletions Src/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ void Config::ProfileData::Reset(Viewer::Profile profile, uint32 categories)
ImportRawPremultAlpha = false;
ImportRawDataOffset = 0;
ImportRawPixelFormat = int(tImage::tPixelFormat::R8G8B8A8);
ImportRawColourProfile = 0;
ImportRawColourSpace = int(tColourSpace::sRGB);
ImportRawFileType .Set(tSystem::tGetFileTypeName(tSystem::tFileType::TIFF));
}
}
Expand Down Expand Up @@ -612,7 +612,7 @@ void Config::ProfileData::Load(tExpression expr)
ReadItem(ImportRawPremultAlpha);
ReadItem(ImportRawDataOffset);
ReadItem(ImportRawPixelFormat);
ReadItem(ImportRawColourProfile);
ReadItem(ImportRawColourSpace);
ReadItem(ImportRawFileType);
ReadItem(ImportRawFilename);
ReadItem(LevelsPowerMidGamma);
Expand Down Expand Up @@ -663,7 +663,7 @@ void Config::ProfileData::Load(tExpression expr)
tiClamp (ImportRawHeight, 1, Viewer::Image::MaxDim);
tiClamp (ImportRawDataOffset, 0, Viewer::Image::MaxDim-1);
tiClamp (ImportRawPixelFormat, int(tImage::tPixelFormat::FirstContiguous), int(tImage::tPixelFormat::LastContiguous));
tiClamp (ImportRawColourProfile, 0, int(tColourProfile::NumProfiles) - 1);
tiClamp (ImportRawColourSpace, 0, int(tColourSpace::NumSpaces) - 1);
tiClamp (CropAnchor, -1, 9);
tiClamp (CropAspectRatio, int(tImage::tAspectRatio::Free), int(tImage::tAspectRatio::User));
tiClamp (CropAspectUserNum, 1, 99);
Expand Down Expand Up @@ -810,7 +810,7 @@ bool Config::ProfileData::Save(tExprWriter& writer) const
WriteItem(ImportRawPremultAlpha);
WriteItem(ImportRawDataOffset);
WriteItem(ImportRawPixelFormat);
WriteItem(ImportRawColourProfile);
WriteItem(ImportRawColourSpace);
WriteItem(ImportRawFileType);
WriteItem(ImportRawFilename);
WriteItem(LevelsPowerMidGamma);
Expand Down
6 changes: 3 additions & 3 deletions Src/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,9 @@ struct ProfileData
int ImportRawPixelFormat;
tImage::tPixelFormat GetImportRawPixelFormat() const { return tImage::tPixelFormat(ImportRawPixelFormat); }
void SetImportRawPixelFormat(RotateModeEnum fmt) { ImportRawPixelFormat = int(fmt); }
int ImportRawColourProfile;
tColourProfile GetImportRawColourProfile() const { return tColourProfile(ImportRawColourProfile); }
void SetImportRawColourProfile(tColourProfile prof) { ImportRawColourProfile = int(prof); }
int ImportRawColourSpace;
tColourSpace GetImportRawColourSpace() const { return tColourSpace(ImportRawColourSpace); }
void SetImportRawColourSpace(tColourSpace prof) { ImportRawColourSpace = int(prof); }
tString ImportRawFileType; // This is the destination filetype of the new image after import.
tString ImportRawFilename;

Expand Down
38 changes: 29 additions & 9 deletions Src/ImportRaw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ namespace ImportRaw
(
tList<tFrame>& frames, const tString& rawFile, tImage::tPixelFormat rawFmt,
int width, int height, int offset, bool mipmaps,
bool mipmapForceSameFrameSize, bool undoAlphaPremult
bool mipmapForceSameFrameSize, bool undoAlphaPremult, tColourSpace
);
tString MakeImportedFilename(tSystem::tFileType destType, const tString& rawFile);
bool CreateImportedFile(tList<tFrame>& frames, const tString& filename);
Expand Down Expand Up @@ -158,6 +158,20 @@ void Viewer::ShowImportRawOverlay(bool* popen, bool justOpened)
if (ImGui::InputInt("Height##ImportRaw", &profile.ImportRawHeight))
tiClamp(profile.ImportRawHeight, 1, Viewer::Image::MaxDim);

const char* colourSpaceItems[] = { "sRGB", "lRGB" };
int colSpace = (profile.GetImportRawColourSpace() == tColourSpace::lRGB) ? 1 : 0;
if (ImGui::Combo("Colour Space", &colSpace , colourSpaceItems, tNumElements(colourSpaceItems)))
{
profile.SetImportRawColourSpace((colSpace == 0) ? tColourSpace::sRGB : tColourSpace::lRGB);
}
ImGui::SameLine();
Gutil::HelpMark
(
"Specify the colour space of the raw pixels being imported. Colour data in\n"
"the viewer is in sRGB-space. If the imported colours are linear then\n"
"selecting lRGB will make sure the imported pixels are converted to sRGB."
);

bool fileTypeSupportsMipmaps = (dstType == tSystem::tFileType::TIFF) || (dstType == tSystem::tFileType::APNG) || (dstType == tSystem::tFileType::WEBP);
bool mipmapForceSameFrameSize = (dstType == tSystem::tFileType::APNG) || (dstType == tSystem::tFileType::WEBP);
if (!fileTypeSupportsMipmaps)
Expand Down Expand Up @@ -226,7 +240,7 @@ void Viewer::ShowImportRawOverlay(bool* popen, bool justOpened)
(
frames, profile.ImportRawFilename, profile.GetImportRawPixelFormat(),
profile.ImportRawWidth, profile.ImportRawHeight, profile.ImportRawDataOffset,
importMipmaps, mipmapForceSameFrameSize, profile.ImportRawPremultAlpha
importMipmaps, mipmapForceSameFrameSize, profile.ImportRawPremultAlpha, profile.GetImportRawColourSpace()
);
if (cr == ImportRaw::CreateResult::Success)
{
Expand Down Expand Up @@ -304,7 +318,6 @@ void Viewer::ShowImportRawOverlay(bool* popen, bool justOpened)
ImportRaw::ImportedFile.Clear();
*popen = false;
}
// @wip ImportRawColourProfile
}

ImGui::End();
Expand All @@ -315,7 +328,7 @@ ImportRaw::CreateResult ImportRaw::CreateFrames
(
tList<tFrame>& frames, const tString& rawFile, tImage::tPixelFormat rawFmt,
int width, int height, int offset, bool mipmaps,
bool mipmapForceSameFrameSize, bool undoAlphaPremult
bool mipmapForceSameFrameSize, bool undoAlphaPremult, tColourSpace space
)
{
if (rawFile.IsEmpty() || !tSystem::tFileExists(rawFile))
Expand Down Expand Up @@ -431,16 +444,23 @@ ImportRaw::CreateResult ImportRaw::CreateFrames

// Possibly undo alpha premultiplication by multiplying by the colour channels
// by the inverse of the alpha.
if (undoAlphaPremult)
if (undoAlphaPremult || (space == tColourSpace::lRGB))
{
for (int p = 0; p < frame->Width*frame->Height; p++)
{
tColour4b col = frame->Pixels[p];
tColour4f colf(col);
float invAlpha = (colf.A > 0.0f) ? 1.0f/colf.A : 1.0f;
colf.R *= invAlpha;
colf.G *= invAlpha;
colf.B *= invAlpha;
if (undoAlphaPremult)
{
float invAlpha = (colf.A > 0.0f) ? 1.0f/colf.A : 1.0f;
colf.R *= invAlpha;
colf.G *= invAlpha;
colf.B *= invAlpha;
}
if (space == tColourSpace::lRGB)
{
colf.LinearToSRGB(tCompBit_RGB);
}
col.Set(colf);
frame->Pixels[p] = col;
}
Expand Down

0 comments on commit 2ba09b7

Please sign in to comment.