Initial commit

This commit is contained in:
alexpete
2021-03-05 11:26:34 -08:00
commit a10351f38d
27091 changed files with 5521199 additions and 0 deletions
@@ -0,0 +1,690 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include "ImageProcessing_precompiled.h"
#include <AzCore/Debug/Trace.h>
#include <AzCore/Math/MathUtils.h>
#include <AzFramework/StringFunc/StringFunc.h>
#include <ImageLoader/ImageLoaders.h>
#include <Atom/ImageProcessing/ImageObject.h>
#include <Processing/PixelFormatInfo.h>
#include <Processing/DDSHeader.h>
#include <Processing/ImageFlags.h>
#include <QString>
namespace ImageProcessingAtom
{
namespace DdsLoader
{
IImageObject* CreateImageFromHeaderLegacy(DDS_HEADER_LEGACY& header, DDS_HEADER_DXT10& exthead)
{
EPixelFormat eFormat = ePixelFormat_Unknown;
AZ::u32 dwWidth, dwMips, dwHeight;
AZ::u32 imageFlags = header.dwReserved1;
AZ::Color colMinARGB, colMaxARGB;
dwWidth = header.dwWidth;
dwHeight = header.dwHeight;
dwMips = 1;
if (header.dwHeaderFlags & DDS_HEADER_FLAGS_MIPMAP)
{
dwMips = header.dwMipMapCount;
}
if ((header.dwSurfaceFlags & DDS_SURFACE_FLAGS_CUBEMAP) && (header.dwCubemapFlags & DDS_CUBEMAP_ALLFACES))
{
AZ_Assert(header.dwReserved1 & EIF_Cubemap, "Image flag should have cubemap flag");
dwHeight *= 6;
}
colMinARGB = AZ::Color(header.cMinColor[0], header.cMinColor[1], header.cMinColor[2], header.cMinColor[3]);
colMaxARGB = AZ::Color(header.cMaxColor[0], header.cMaxColor[1], header.cMaxColor[2], header.cMaxColor[3]);
//get pixel format
{
// DX10 formats
if (header.ddspf.dwFourCC == FOURCC_DX10)
{
AZ::u32 dxgiFormat = exthead.dxgiFormat;
//remove the SRGB from dxgi format and add sRGB to image flag
if (dxgiFormat == DXGI_FORMAT_R8G8B8A8_UNORM_SRGB)
{
dxgiFormat = DXGI_FORMAT_R8G8B8A8_UNORM;
}
else if (dxgiFormat == DXGI_FORMAT_BC1_UNORM_SRGB)
{
dxgiFormat = DXGI_FORMAT_BC1_UNORM;
}
else if (dxgiFormat == DXGI_FORMAT_BC2_UNORM_SRGB)
{
dxgiFormat = DXGI_FORMAT_BC2_UNORM;
}
else if (dxgiFormat == DXGI_FORMAT_BC3_UNORM_SRGB)
{
dxgiFormat = DXGI_FORMAT_BC3_UNORM;
}
else if (dxgiFormat == DXGI_FORMAT_BC7_UNORM_SRGB)
{
dxgiFormat = DXGI_FORMAT_BC7_UNORM;
}
//add rgb flag if the dxgiformat was changed (which means it was sRGB format) above
if (dxgiFormat != exthead.dxgiFormat)
{
AZ_Assert(imageFlags & EIF_SRGBRead, "Image flags should have SRGBRead flag");
imageFlags |= EIF_SRGBRead;
}
//check all the pixel formats and find matching one
if (dxgiFormat != DXGI_FORMAT_UNKNOWN)
{
int i = 0;
for (i; i < ePixelFormat_Count; i++)
{
const PixelFormatInfo* info = CPixelFormats::GetInstance().GetPixelFormatInfo((EPixelFormat)i);
if (info->d3d10Format == dxgiFormat)
{
eFormat = (EPixelFormat)i;
break;
}
}
if (i == ePixelFormat_Count)
{
AZ_Error("Image Processing", false, "Unhandled d3d10 format: %d", dxgiFormat);
return nullptr;
}
}
}
else
{
//for non-dx10 formats, use fourCC to find out its pixel formats
//go through all pixel formats and find a match with the fourcc
for (AZ::u32 formatIdx = 0; formatIdx < ePixelFormat_Count; formatIdx++)
{
const PixelFormatInfo* info = CPixelFormats::GetInstance().GetPixelFormatInfo((EPixelFormat)formatIdx);
if (header.ddspf.dwFourCC == info->fourCC)
{
eFormat = (EPixelFormat)formatIdx;
break;
}
}
//legacy formats. This section is only used for load dds files converted by RC.exe
//our save to dds file function won't use any of these fourcc
if (eFormat == ePixelFormat_Unknown)
{
if (header.ddspf.dwFourCC == FOURCC_DXT1)
{
eFormat = ePixelFormat_BC1;
}
else if (header.ddspf.dwFourCC == FOURCC_DXT5)
{
eFormat = ePixelFormat_BC3;
}
else if (header.ddspf.dwFourCC == FOURCC_3DCP)
{
eFormat = ePixelFormat_BC4;
}
else if (header.ddspf.dwFourCC == FOURCC_3DC)
{
eFormat = ePixelFormat_BC5;
}
else if (header.ddspf.dwFourCC == DDS_FOURCC_R32F)
{
eFormat = ePixelFormat_R32F;
}
else if (header.ddspf.dwFourCC == DDS_FOURCC_G32R32F)
{
eFormat = ePixelFormat_R32G32F;
}
else if (header.ddspf.dwFourCC == DDS_FOURCC_A32B32G32R32F)
{
eFormat = ePixelFormat_R32G32B32A32F;
}
else if (header.ddspf.dwFourCC == DDS_FOURCC_R16F)
{
eFormat = ePixelFormat_R16F;
}
else if (header.ddspf.dwFourCC == DDS_FOURCC_G16R16F)
{
eFormat = ePixelFormat_R16G16F;
}
else if (header.ddspf.dwFourCC == DDS_FOURCC_A16B16G16R16F)
{
eFormat = ePixelFormat_R16G16B16A16F;
}
else if (header.ddspf.dwFourCC == DDS_FOURCC_A16B16G16R16)
{
eFormat = ePixelFormat_R16G16B16A16;
}
else if ((header.ddspf.dwFlags == DDS_RGBA || header.ddspf.dwFlags == DDS_RGB)
&& header.ddspf.dwRGBBitCount == 32)
{
if (header.ddspf.dwRBitMask == 0x00ff0000)
{
eFormat = ePixelFormat_B8G8R8A8;
}
else
{
eFormat = ePixelFormat_R8G8B8A8;
}
}
else if (header.ddspf.dwFlags == DDS_LUMINANCEA && header.ddspf.dwRGBBitCount == 8)
{
eFormat = ePixelFormat_R8G8;
}
else if (header.ddspf.dwFlags == DDS_LUMINANCE && header.ddspf.dwRGBBitCount == 8)
{
eFormat = ePixelFormat_A8;
}
else if ((header.ddspf.dwFlags == DDS_A || header.ddspf.dwFlags == DDS_A_ONLY || header.ddspf.dwFlags == (DDS_A | DDS_A_ONLY)) && header.ddspf.dwRGBBitCount == 8)
{
eFormat = ePixelFormat_A8;
}
}
}
}
if (eFormat == ePixelFormat_Unknown)
{
AZ_Error("Image Processing", false, "Unhandled dds pixel format fourCC: %d, flags: %d",
header.ddspf.dwFourCC, header.ddspf.dwFlags);
return nullptr;
}
IImageObject* newImage = IImageObject::CreateImage(dwWidth, dwHeight, dwMips, eFormat);
if (dwMips != newImage->GetMipCount())
{
AZ_Error("Image Processing", false, "Mipcount from image data doesn't match image size and pixelformat");
delete newImage;
return nullptr;
}
//set properties
newImage->SetImageFlags(imageFlags);
newImage->SetAverageBrightness(header.fAvgBrightness);
newImage->SetColorRange(colMinARGB, colMaxARGB);
newImage->SetNumPersistentMips(header.bNumPersistentMips);
return newImage;
}
bool IsExtensionSupported(const char* extension)
{
QString ext = QString(extension).toLower();
// This is the list of file extensions supported by this loader
return ext == "dds";
}
IImageObject* LoadImageFromFileLegacy(const AZStd::string& filename)
{
AZ::IO::SystemFile file;
file.Open(filename.c_str(), AZ::IO::SystemFile::SF_OPEN_READ_ONLY);
AZ::IO::SystemFileStream fileLoadStream(&file, true);
if (!fileLoadStream.IsOpen())
{
AZ_Warning("Image Processing", false, "%s: failed to open file %s", __FUNCTION__, filename.c_str());
return nullptr;
}
AZStd::string ext = "";
AzFramework::StringFunc::Path::GetExtension(filename.c_str(), ext, false);
bool isAlphaImage = (ext == "a");
IImageObject* imageObj = LoadImageFromFileStreamLegacy(fileLoadStream);
//load mips from seperated files if it's splitted
if (imageObj && imageObj->HasImageFlags(EIF_Splitted))
{
AZStd::string baseName;
if (isAlphaImage)
{
baseName = filename.substr(0, filename.size() - 2);
}
else
{
baseName = filename;
}
AZ::u32 externalMipCount = 0;
if (imageObj->GetNumPersistentMips() < imageObj->GetMipCount())
{
externalMipCount = imageObj->GetMipCount() - imageObj->GetNumPersistentMips();
}
//load other mips from files with number extensions
for (AZ::u32 mipIdx = 1; mipIdx <= externalMipCount; mipIdx++)
{
AZ::u32 mip = externalMipCount - mipIdx;
AZStd::string mipFileName = AZStd::string::format("%s.%d%s", baseName.c_str(), mipIdx, isAlphaImage ? "a" : "");
AZ::IO::SystemFile mipFile;
mipFile.Open(mipFileName.c_str(), AZ::IO::SystemFile::SF_OPEN_READ_ONLY);
AZ::IO::SystemFileStream mipFileLoadStream(&mipFile, true);
if (!mipFileLoadStream.IsOpen())
{
AZ_Warning("Image Processing", false, "%s: failed to open mip file %s", __FUNCTION__, mipFileName.c_str());
break;
}
AZ::u32 pitch;
AZ::u8* mem;
imageObj->GetImagePointer(mip, mem, pitch);
AZ::u32 bufSize = imageObj->GetMipBufSize(mip);
mipFileLoadStream.Read(bufSize, mem);
}
}
return imageObj;
}
IImageObject* LoadImageFromFileStreamLegacy(AZ::IO::SystemFileStream& fileLoadStream)
{
if (fileLoadStream.GetLength() - fileLoadStream.GetCurPos() < sizeof(DDS_FILE_DESC_LEGACY))
{
AZ_Error("Image Processing", false, "%s: Trying to load a none-DDS file", __FUNCTION__);
return nullptr;
}
DDS_FILE_DESC_LEGACY desc;
DDS_HEADER_DXT10 exthead;
AZ::IO::SizeType startPos = fileLoadStream.GetCurPos();
fileLoadStream.Read(sizeof(desc.dwMagic), &desc.dwMagic);
if (desc.dwMagic != FOURCC_DDS)
{
desc.dwMagic = FOURCC_DDS;
//the old cry .a file doesn't have "DDS " in the beginning of the file.
//so reset to previous position
fileLoadStream.Seek(startPos, AZ::IO::GenericStream::ST_SEEK_BEGIN);
}
fileLoadStream.Read(sizeof(desc.header), &desc.header);
if (!desc.IsValid())
{
AZ_Error("Image Processing", false, "%s: Trying to load a none-DDS file", __FUNCTION__);
return nullptr;
}
if (desc.header.IsDX10Ext())
{
fileLoadStream.Read(sizeof(exthead), &exthead);
}
IImageObject* outImage = CreateImageFromHeaderLegacy(desc.header, exthead);
if (outImage == nullptr)
{
return nullptr;
}
//load mip data
AZ::u32 mipStart = 0;
//There are at least three lowest mips are in the file if it was splitted. This is to load splitted dds file exported by legacy rc.exe
int numPersistentMips = outImage->GetNumPersistentMips();
if (numPersistentMips == 0 && outImage->HasImageFlags(EIF_Splitted))
{
outImage->SetNumPersistentMips(3);
}
if (outImage->HasImageFlags(EIF_Splitted)
&& outImage->GetMipCount() > outImage->GetNumPersistentMips())
{
mipStart = outImage->GetMipCount() - outImage->GetNumPersistentMips();
}
AZ::u32 faces = 1;
if (outImage->HasImageFlags(EIF_Cubemap))
{
faces = 6;
}
for (AZ::u32 face = 0; face < faces; face++)
{
for (AZ::u32 mip = mipStart; mip < outImage->GetMipCount(); ++mip)
{
AZ::u32 pitch;
AZ::u8* mem;
outImage->GetImagePointer(mip, mem, pitch);
AZ::u32 faceBufSize = outImage->GetMipBufSize(mip) / faces;
fileLoadStream.Read(faceBufSize, mem + faceBufSize * face);
}
}
return outImage;
}
IImageObject* LoadAttachedImageFromDdsFileLegacy(const AZStd::string& filename, IImageObjectPtr originImage)
{
if (originImage == nullptr)
{
return nullptr;
}
AZ_Assert(originImage->HasImageFlags(EIF_AttachedAlpha),
"this function should only be called for origin image loaded from same file with attached alpha flag");
AZ::IO::SystemFile file;
file.Open(filename.c_str(), AZ::IO::SystemFile::SF_OPEN_READ_ONLY);
AZ::IO::SystemFileStream fileLoadStream(&file, true);
if (!fileLoadStream.IsOpen())
{
AZ_Warning("Image Processing", false, "%s: failed to open file %s", __FUNCTION__, filename.c_str());
return nullptr;
}
DDS_FILE_DESC_LEGACY desc;
DDS_HEADER_DXT10 exthead;
fileLoadStream.Read(sizeof(desc), &desc);
if (desc.dwMagic != FOURCC_DDS)
{
AZ_Error("Image Processing", false, "%s:Trying to load a none-DDS file", __FUNCTION__);
return nullptr;
}
if (desc.header.IsDX10Ext())
{
fileLoadStream.Read(sizeof(exthead), &exthead);
}
//skip size for originImage's mip data
for (AZ::u32 mip = 0; mip < originImage->GetMipCount(); ++mip)
{
AZ::u32 bufSize = originImage->GetMipBufSize(mip);
fileLoadStream.Seek(bufSize, AZ::IO::GenericStream::ST_SEEK_CUR);
}
IImageObject* alphaImage = nullptr;
AZ::u32 marker = 0;
fileLoadStream.Read(4, &marker);
if (marker == FOURCC_CExt) // marker for the start of Crytek Extended data
{
fileLoadStream.Read(4, &marker);
if (FOURCC_AttC == marker) // Attached Channel chunk
{
AZ::u32 size = 0;
fileLoadStream.Read(4, &size);
alphaImage = LoadImageFromFileStreamLegacy(fileLoadStream);
fileLoadStream.Read(4, &marker);
}
if (FOURCC_CEnd == marker) // marker for the end of Crytek Extended data
{
fileLoadStream.Read(4, &marker);
}
}
return alphaImage;
}
// Create an image object from standard dds header
IImageObject* CreateImageFromHeader(DDS_HEADER& header, DDS_HEADER_DXT10& exthead)
{
if ((header.dwCaps & DDS_SURFACE_FLAGS_TEXTURE) != DDS_SURFACE_FLAGS_TEXTURE ||
(header.dwFlags & DDS_HEADER_FLAGS_TEXTURE) != DDS_HEADER_FLAGS_TEXTURE)
{
AZ_Error("Image Processing", false, "This dds file is not a valid texture");
return nullptr;
}
EPixelFormat format = ePixelFormat_Unknown;
uint32_t imageFlags = 0;
uint32_t width = header.dwWidth;
uint32_t height = header.dwHeight;
uint32_t mips = 1;
if (header.dwFlags & DDS_HEADER_FLAGS_MIPMAP)
{
mips = header.dwMipMapCount;
}
if ((header.dwCaps & DDS_SURFACE_FLAGS_CUBEMAP) && (header.dwCaps_2 & DDS_CUBEMAP))
{
if ((header.dwCaps_2 & DDS_CUBEMAP_ALLFACES) != DDS_CUBEMAP_ALLFACES)
{
AZ_Error("Image Processing", false, "Only support cubemap in dds file with all faces");
return nullptr;
}
imageFlags |= EIF_Cubemap;
height *= 6;
}
// Get pixel format
if (header.ddspf.dwFlags & DDS_FOURCC)
{
// dx10 formats
if (header.ddspf.dwFourCC == FOURCC_DX10)
{
uint32_t dxgiFormat = exthead.dxgiFormat;
//remove the SRGB from dxgi format and add sRGB to image flag
if (dxgiFormat == DXGI_FORMAT_R8G8B8A8_UNORM_SRGB)
{
dxgiFormat = DXGI_FORMAT_R8G8B8A8_UNORM;
}
else if (dxgiFormat == DXGI_FORMAT_BC1_UNORM_SRGB)
{
dxgiFormat = DXGI_FORMAT_BC1_UNORM;
}
else if (dxgiFormat == DXGI_FORMAT_BC2_UNORM_SRGB)
{
dxgiFormat = DXGI_FORMAT_BC2_UNORM;
}
else if (dxgiFormat == DXGI_FORMAT_BC3_UNORM_SRGB)
{
dxgiFormat = DXGI_FORMAT_BC3_UNORM;
}
else if (dxgiFormat == DXGI_FORMAT_BC7_UNORM_SRGB)
{
dxgiFormat = DXGI_FORMAT_BC7_UNORM;
}
//add rgb flag if the dxgiformat was changed (which means it was sRGB format) above
if (dxgiFormat != exthead.dxgiFormat)
{
imageFlags |= EIF_SRGBRead;
}
//check all the pixel formats and find matching one
if (dxgiFormat != DXGI_FORMAT_UNKNOWN)
{
uint32_t i = 0;
for (i; i < ePixelFormat_Count; i++)
{
const PixelFormatInfo* info = CPixelFormats::GetInstance().GetPixelFormatInfo((EPixelFormat)i);
if (info->d3d10Format == dxgiFormat)
{
format = (EPixelFormat)i;
break;
}
}
if (i == ePixelFormat_Count)
{
AZ_Error("Image Processing", false, "Unhandled d3d10 format: %d", dxgiFormat);
return nullptr;
}
}
}
else if (header.ddspf.dwFourCC == FOURCC_DXT1)
{
format = ePixelFormat_BC1;
}
else if (header.ddspf.dwFourCC == FOURCC_DXT5)
{
format = ePixelFormat_BC3;
}
else if (header.ddspf.dwFourCC == FOURCC_3DCP)
{
format = ePixelFormat_BC4;
}
else if (header.ddspf.dwFourCC == FOURCC_3DC)
{
format = ePixelFormat_BC5;
}
else if (header.ddspf.dwFourCC == DDS_FOURCC_R32F)
{
format = ePixelFormat_R32F;
}
else if (header.ddspf.dwFourCC == DDS_FOURCC_G32R32F)
{
format = ePixelFormat_R32G32F;
}
else if (header.ddspf.dwFourCC == DDS_FOURCC_A32B32G32R32F)
{
format = ePixelFormat_R32G32B32A32F;
}
else if (header.ddspf.dwFourCC == DDS_FOURCC_R16F)
{
format = ePixelFormat_R16F;
}
else if (header.ddspf.dwFourCC == DDS_FOURCC_G16R16F)
{
format = ePixelFormat_R16G16F;
}
else if (header.ddspf.dwFourCC == DDS_FOURCC_A16B16G16R16F)
{
format = ePixelFormat_R16G16B16A16F;
}
else if (header.ddspf.dwFourCC == DDS_FOURCC_A16B16G16R16)
{
format = ePixelFormat_R16G16B16A16;
}
}
else
{
if ((header.ddspf.dwFlags == DDS_RGBA || header.ddspf.dwFlags == DDS_RGB))
{
if (header.ddspf.dwRBitMask == 0x00ff0000 || header.ddspf.dwGBitMask == 0x00ff0000 || header.ddspf.dwBBitMask == 0x00ff0000)
{
format = (header.ddspf.dwRGBBitCount == 32) ? ePixelFormat_B8G8R8A8 : ePixelFormat_B8G8R8;
}
else if (header.ddspf.dwBBitMask == 0x00ff0000 || header.ddspf.dwGBitMask == 0x00ff0000 || header.ddspf.dwRBitMask == 0x00ff0000)
{
format = (header.ddspf.dwRGBBitCount == 32) ? ePixelFormat_R8G8B8A8 : ePixelFormat_R8G8B8;
}
}
else if (header.ddspf.dwFlags == DDS_LUMINANCEA && header.ddspf.dwRGBBitCount == 8)
{
format = ePixelFormat_R8G8;
}
else if (header.ddspf.dwFlags == DDS_LUMINANCE && header.ddspf.dwRGBBitCount == 8)
{
format = ePixelFormat_A8;
}
else if ((header.ddspf.dwFlags == DDS_A || header.ddspf.dwFlags == DDS_A_ONLY || header.ddspf.dwFlags == (DDS_A | DDS_A_ONLY)) && header.ddspf.dwRGBBitCount == 8)
{
format = ePixelFormat_A8;
}
}
if (format == ePixelFormat_Unknown)
{
AZ_Error("Image Processing", false, "Unhandled dds pixel format fourCC: %d, flags: %d",
header.ddspf.dwFourCC, header.ddspf.dwFlags);
return nullptr;
}
// Resize to block size for compressed format. This could happened to those 1x1 bc1 dds files
// [GFX TODO] [ATOM-181] We may want to add padding support for bc formats so we can remove this temporary fix
const PixelFormatInfo* const pFormatInfo = CPixelFormats::GetInstance().GetPixelFormatInfo(format);
if (width < pFormatInfo->blockWidth && height < pFormatInfo->blockHeight)
{
width = pFormatInfo->blockWidth;
height = pFormatInfo->blockHeight;
}
IImageObject* newImage = IImageObject::CreateImage(width, height, mips, format);
//set properties
newImage->SetImageFlags(imageFlags);
return newImage;
}
IImageObject* LoadImageFromFile(const AZStd::string& filename)
{
AZ::IO::SystemFile file;
file.Open(filename.c_str(), AZ::IO::SystemFile::SF_OPEN_READ_ONLY);
AZ::IO::SystemFileStream fileLoadStream(&file, true);
if (!fileLoadStream.IsOpen())
{
AZ_Warning("Image Processing", false, "%s: failed to open file %s", __FUNCTION__, filename.c_str());
return nullptr;
}
DDS_FILE_DESC desc;
DDS_HEADER_DXT10 exthead;
AZ::IO::SizeType startPos = fileLoadStream.GetCurPos();
fileLoadStream.Read(sizeof(desc), &desc);
if (desc.dwMagic != FOURCC_DDS || !desc.IsValid())
{
AZ_Error("Image Processing", false, "%s: Trying to load a none-DDS file", __FUNCTION__);
return nullptr;
}
if (desc.header.IsDX10Ext())
{
fileLoadStream.Read(sizeof(exthead), &exthead);
}
IImageObject* outImage = CreateImageFromHeader(desc.header, exthead);
if (outImage == nullptr)
{
return nullptr;
}
AZ::u32 faces = 1;
if (outImage->HasImageFlags(EIF_Cubemap))
{
faces = 6;
}
for (AZ::u32 face = 0; face < faces; face++)
{
for (AZ::u32 mip = 0; mip < outImage->GetMipCount(); ++mip)
{
AZ::u32 pitch;
AZ::u8* mem;
outImage->GetImagePointer(mip, mem, pitch);
AZ::u32 faceBufSize = outImage->GetMipBufSize(mip) / faces;
if (fileLoadStream.GetLength() - fileLoadStream.GetCurPos() < faceBufSize)
{
delete outImage;
AZ_Error("Image Processing", false, "DdsLoader: load mip data error");
return nullptr;
}
fileLoadStream.Read(faceBufSize, mem + faceBufSize * face);
}
}
return outImage;
}
}// namespace ImageDDS
} //namespace ImageProcessingAtom
@@ -0,0 +1,198 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include "ImageProcessing_precompiled.h"
#include <ImageLoader/ImageLoaders.h>
#include <Atom/ImageProcessing/ImageObject.h>
#include <QString>
// From OpenEXR third party library
#include <OpenEXR/ImfArray.h>
#include <OpenEXR/ImfChannelList.h>
#include <OpenEXR/ImfHeader.h>
#include <OpenEXR/ImfInputFile.h>
#include <OpenEXR/ImfRgba.h>
#include <OpenEXR/ImfStandardAttributes.h>
#include <OpenEXR/ImfTestFile.h>
#include <OpenEXR/ImfTiledInputFile.h>
// define imf namespace with current library version
#define Imf OPENEXR_IMF_INTERNAL_NAMESPACE
#define ImfMath IMATH_INTERNAL_NAMESPACE
namespace ImageProcessingAtom
{
namespace ExrLoader
{
bool IsExtensionSupported(const char* extension)
{
QString ext = QString(extension).toLower();
// This is the list of file extensions supported by this loader
return ext == "exr";
}
IImageObject* LoadImageFromScanlineFile(const AZStd::string& filename)
{
try
{
Imf::InputFile exrFile(filename.c_str());
if (!exrFile.isComplete())
{
AZ_Error("Image Processing", false, "ExrLoader: uncompleted exr file [%s]", filename.c_str());
return NULL;
}
const Imf::Header& header = exrFile.header();
// Get Channel information for RGBA
const Imf::Channel* channels[4];
channels[0] = header.channels().findChannel("R");
channels[1] = header.channels().findChannel("G");
channels[2] = header.channels().findChannel("B");
channels[3] = header.channels().findChannel("A");
// Initialize pixel format to invalid one
Imf::PixelType pixelType = Imf::NUM_PIXELTYPES;
bool hasChannels = false;
for (int32_t idx = 0; idx < 4; idx++)
{
if (channels[idx])
{
if (hasChannels)
{
if (pixelType != channels[idx]->type)
{
// return null if there are different pixel types in different channels
AZ_Error("Image Processing", false, "load exr file error: image "
"channels have different data types", filename.c_str());
return nullptr;
}
}
else
{
pixelType = channels[idx]->type;
hasChannels = true;
}
}
}
if (!hasChannels)
{
// return null if there are no rgba channels
AZ_Error("Image Processing", false, "load exr file error: exr image doesn't contain "
"any rgba channels", filename.c_str());
return nullptr;
}
// Find the EPixelFormat to matching the format
EPixelFormat format = ePixelFormat_Unknown;
int32_t pixelSize = 0;
if (pixelType == Imf::FLOAT)
{
format = EPixelFormat::ePixelFormat_R32G32B32A32F;
pixelSize = 16;
}
else if (pixelType == Imf::HALF)
{
format = EPixelFormat::ePixelFormat_R16G16B16A16F;
pixelSize = 8;
}
else
{
AZ_Error("Image Processing", false, "load exr file error: unsupported exr pixel format [%d]", pixelType);
return nullptr;
}
// Get the image size
int width, height;
ImfMath::Box2i dw = header.dataWindow();
width = dw.max.x - dw.min.x + 1;
height = dw.max.y - dw.min.y + 1;
int dx = dw.min.x;
int dy = dw.min.y;
// Create IImageObject
IImageObject* newImage = IImageObject::CreateImage(width, height, 1, format);
// Setup Imf FrameBuffer for loading data
char* pixels = new char[width * height * pixelSize];
Imf::FrameBuffer frameBuffer;
size_t xStride = pixelSize;
size_t yStride = pixelSize * width;
int32_t channelPixelSize = pixelSize / 4;
char* base = pixels;
frameBuffer.insert("R",
Imf::Slice(pixelType, base, xStride, yStride));
frameBuffer.insert("G",
Imf::Slice(pixelType, base + channelPixelSize, xStride, yStride));
frameBuffer.insert("B",
Imf::Slice(pixelType, base + channelPixelSize * 2, xStride, yStride));
// Insert A with default value of 1
frameBuffer.insert("A",
Imf::Slice(pixelType, base + channelPixelSize * 3, xStride, yStride, 1, 1, 1.0));
exrFile.setFrameBuffer(frameBuffer);
exrFile.readPixels(0, height - 1);
// save pixel data to newImage's mipmap data buffer
AZ::u32 pitch;
AZ::u8* mem;
newImage->GetImagePointer(0, mem, pitch);
memcpy(mem, base, newImage->GetMipBufSize(0));
delete [] pixels;
return newImage;
}
catch (...)
{
AZ_Error("Image Processing", false, "ExrLoader: load exr file [%s] error", filename.c_str());
return nullptr;
}
}
IImageObject* LoadImageFromFile(const AZStd::string& filename)
{
// In the current implementation it supports load one flat image with one or few of rgba channels.
// It wont handle multi-part, deep image or some arbitrary channels. It also won't handle layers.
// It's often the environment map wasn't saved with "envmap" header, so we are not trying get the information.
// Get exr file feature information
bool isTiled, isDeep, isMultiPart;
bool isExr = Imf::isOpenExrFile(filename.c_str(), isTiled, isDeep, isMultiPart);
if (!isExr)
{
AZ_Error("Image Processing", false, "ExrLoader: file [%s] is not a valid exr file", filename.c_str());
return NULL;
}
if (isDeep || isMultiPart || isTiled)
{
if (isTiled)
{
AZ_Error("Image Processing", false, "ExrLoader doesn't support tiled exr file [%s]", filename.c_str());
}
else
{
AZ_Error("Image Processing", false, "ExrLoader: file [%s] has unsupported deep or multi-part information", filename.c_str());
}
return NULL;
}
return LoadImageFromScanlineFile(filename);
}
}// namespace ExrLoader
} //namespace ImageProcessingAtom
#undef Imf
#undef ImfMath
@@ -0,0 +1,88 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include <ImageProcessing_precompiled.h>
#include <ImageLoader/ImageLoaders.h>
#include <Atom/ImageProcessing/ImageObject.h>
// warning C4251: class QT_Type needs to have dll-interface to be used by clients of class 'QT_Type'
AZ_PUSH_DISABLE_WARNING(4251, "-Wunknown-warning-option")
#include <QFileInfo>
AZ_POP_DISABLE_WARNING
namespace ImageProcessingAtom
{
IImageObject* LoadImageFromFile(const AZStd::string& filename)
{
QFileInfo fileInfo(filename.c_str());
QString ext = fileInfo.suffix();
if (!fileInfo.exists())
{
return nullptr;
}
if (TIFFLoader::IsExtensionSupported(ext.toUtf8()))
{
return TIFFLoader::LoadImageFromTIFF(filename);
}
else if (DdsLoader::IsExtensionSupported(ext.toUtf8()))
{
return DdsLoader::LoadImageFromFile(filename);
}
else if (QtImageLoader::IsExtensionSupported(ext.toUtf8()))
{
return QtImageLoader::LoadImageFromFile(filename);
}
else if (ExrLoader::IsExtensionSupported(ext.toUtf8()))
{
return ExrLoader::LoadImageFromFile(filename);
}
AZ_Warning("ImageProcessing", false, "No proper image loader to load file: %s", filename.c_str());
return nullptr;
}
bool IsExtensionSupported(const char* extension)
{
if (TIFFLoader::IsExtensionSupported(extension))
{
return true;
}
else if (DdsLoader::IsExtensionSupported(extension))
{
return true;
}
else if (QtImageLoader::IsExtensionSupported(extension))
{
return true;
}
else if (ExrLoader::IsExtensionSupported(extension))
{
return true;
}
return false;
}
const AZStd::string LoadEmbeddedSettingFromFile(const AZStd::string& filename)
{
QFileInfo fileInfo(filename.c_str());
QString ext = fileInfo.suffix();
if (TIFFLoader::IsExtensionSupported(ext.toUtf8()))
{
return TIFFLoader::LoadSettingFromTIFF(filename);
}
return "";
}
}// namespace ImageProcessingAtom
@@ -0,0 +1,64 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#pragma once
#include <AzCore/std/string/string.h>
#include <AzCore/IO/SystemFile.h>
#include <AzCore/IO/GenericStreams.h>
#include <Atom/ImageProcessing/ImageObject.h>
namespace ImageProcessingAtom
{
IImageObject* LoadImageFromFile(const AZStd::string& filename);
bool IsExtensionSupported(const char* extension);
const AZStd::string LoadEmbeddedSettingFromFile(const AZStd::string& filename);
// Tiff loader. The loader support uncompressed tiff with with 1~4 channels and 8bit and 16bit uint or 16bits and 32bits float per channel
// QImage also support tiff (tiff plugin), but it only supports 8bits uint
namespace TIFFLoader
{
bool IsExtensionSupported(const char* extension);
// Load a tiff file to an image object.
IImageObject* LoadImageFromTIFF(const AZStd::string& filename);
// Load embedded .exportsettings string from tiff which was exported by deprecated feature of CryTif plugin.
const AZStd::string LoadSettingFromTIFF(const AZStd::string& filename);
};// namespace TIFFLoader
// Image loader through Qt's QImage with image formats supported native and through plugins
namespace QtImageLoader
{
bool IsExtensionSupported(const char* extension);
// Load image file which supported by QtImage to an image object
IImageObject* LoadImageFromFile(const AZStd::string& filename);
};// namespace QtImageLoader
// Load dds files to ImageObject. The QtImageLoader can load dds file but it only can load dds with non-compressed formats.
namespace DdsLoader
{
bool IsExtensionSupported(const char* extension);
IImageObject* LoadImageFromFile(const AZStd::string& filename);
// These functions are for loading legacy lumberyard dds files
IImageObject* LoadImageFromFileLegacy(const AZStd::string& filename);
IImageObject* LoadImageFromFileStreamLegacy(AZ::IO::SystemFileStream& fileLoadStream);
IImageObject* LoadAttachedImageFromDdsFileLegacy(const AZStd::string& filename, IImageObjectPtr originImage);
};// namespace DdsLoader
// Load .exr files to an image object
namespace ExrLoader
{
bool IsExtensionSupported(const char* extension);
IImageObject* LoadImageFromFile(const AZStd::string& filename);
};// namespace ExrLoader
}// namespace ImageProcessingAtom
@@ -0,0 +1,80 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include "ImageProcessing_precompiled.h"
#include <ImageLoader/ImageLoaders.h>
#include <Atom/ImageProcessing/ImageObject.h>
// warning C4251: class QT_Type needs to have dll-interface to be used by clients of class 'QT_Type'
AZ_PUSH_DISABLE_WARNING(4251, "-Wunknown-warning-option")
#include <QImage>
#include <QImageReader>
AZ_POP_DISABLE_WARNING
///////////////////////////////////////////////////////////////////////////////////
namespace ImageProcessingAtom
{
namespace QtImageLoader
{
IImageObject* LoadImageFromFile(const AZStd::string& filename)
{
//try to open the image
QImage qimage(filename.c_str());
if (qimage.isNull())
{
return NULL;
}
//convert to format which compatiable our pixel format
QImage::Format format = qimage.format();
if (qimage.format() != QImage::Format_RGBA8888)
{
qimage = qimage.convertToFormat(QImage::Format_RGBA8888);
}
//create a new image object
IImageObject* pImage = IImageObject::CreateImage(qimage.width(), qimage.height(), 1,
ePixelFormat_R8G8B8A8);
//get a pointer to the image objects pixel data
uint8* pDst;
uint32 dwPitch;
pImage->GetImagePointer(0, pDst, dwPitch);
//copy the qImage into the image object
for (uint32 dwY = 0; dwY < (uint32)qimage.height(); ++dwY)
{
uint8* dstLine = &pDst[dwPitch * dwY];
uchar* srcLine = qimage.scanLine(dwY);
memcpy(dstLine, srcLine, dwPitch);
}
return pImage;
}
bool IsExtensionSupported(const char* extension)
{
QList<QByteArray> imgFormats = QImageReader::supportedImageFormats();
for (int i = 0; i < imgFormats.size(); ++i)
{
if (imgFormats[i].toLower().toStdString() == QString(extension).toLower().toStdString())
{
return true;
}
}
return false;
}
}//namespace QtImageLoader
} //namespace ImageProcessingAtom
@@ -0,0 +1,644 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include "ImageProcessing_precompiled.h"
#include <AzCore/Debug/Trace.h>
#include <AzCore/Math/MathUtils.h>
#include <ImageLoader/ImageLoaders.h>
#include <Atom/ImageProcessing/ImageObject.h>
#include <QString>
#include <libtiff/tiffio.h> // TIFF library
namespace ImageProcessingAtom
{
namespace TIFFLoader
{
class TiffFileRead
{
public:
TiffFileRead(const AZStd::string& filename)
: m_tif(nullptr)
{
m_tif = TIFFOpen(filename.c_str(), "r");
}
~TiffFileRead()
{
if (m_tif != nullptr)
{
TIFFClose(m_tif);
}
}
TIFF* GetTiff()
{
return m_tif;
}
private:
TIFF* m_tif;
};
bool IsExtensionSupported(const char* extension)
{
QString ext = QString(extension).toLower();
// This is the list of file extensions supported by this loader
return ext == "tif" || ext == "tiff";
}
// loads simple RAWImage from 8bit uint tiff source raster
static IImageObject* Load8BitImageFromTIFF(TIFF* tif);
// loads simple FloatImage from 16bit uint tiff source raster
static IImageObject* Load16BitImageFromTIFF(TIFF* tif);
// loads simple FloatImage from 16f HDR tiff source raster
static IImageObject* Load16BitHDRImageFromTIFF(TIFF* tif);
// loads simple FloatImage from 32f HDR tiff source raster
static IImageObject* Load32BitHDRImageFromTIFF(TIFF* tif);
IImageObject* LoadImageFromTIFF(const AZStd::string& filename)
{
TiffFileRead tiffRead(filename);
TIFF* tif = tiffRead.GetTiff();
IImageObject* pRet = nullptr;
if (!tif)
{
AZ_Warning("Image Processing", false, "%s: Open tiff failed (%s)", __FUNCTION__, filename.c_str());
return pRet;
}
uint32_t dwBitsPerChannel = 0;
uint32_t dwChannels = 0;
uint32_t dwFormat = 0;
TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &dwChannels);
TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &dwBitsPerChannel);
TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &dwFormat);
if (dwChannels != 1 && dwChannels != 2 && dwChannels != 3 && dwChannels != 4)
{
AZ_Warning("Image Processing", false, "Unsupported TIFF pixel format (channel count: %d)", dwChannels);
return pRet;
}
uint32_t dwWidth = 0;
uint32_t dwHeight = 0;
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &dwWidth);
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &dwHeight);
if (dwWidth <= 0 || dwHeight <= 0)
{
AZ_Error("Image Processing", false, "%s failed (empty image)", __FUNCTION__);
return pRet;
}
const char* pFormatText;
if (dwBitsPerChannel == 8)
{
// R8, GR8, BGR8, BGRA8
pFormatText = "8-bit";
pRet = Load8BitImageFromTIFF(tif);
}
else if (dwBitsPerChannel == 16)
{
// A/L/R16, R16F, GR16, GR16f, ARGB16, ARGB16f
if (dwFormat == SAMPLEFORMAT_IEEEFP)
{
pFormatText = "16-bit float";
pRet = Load16BitHDRImageFromTIFF(tif);
}
else
{
pFormatText = "16-bit int";
pRet = Load16BitImageFromTIFF(tif);
}
}
else if (dwBitsPerChannel == 32 && dwFormat == SAMPLEFORMAT_IEEEFP)
{
// A/L/R32f, GR32f, ARGB32f
pFormatText = "32-bit float";
pRet = Load32BitHDRImageFromTIFF(tif);
}
else
{
AZ_Error("Image Processing", false, "File %s has unsupported TIFF pixel format. sample channels: %d,\
bits per channel: %d, sample format: %d", filename.c_str(), dwChannels, dwBitsPerChannel, dwFormat);
return pRet;
}
if (pRet == nullptr)
{
AZ_Error("Image Processing", false, "Failed to read TIFF pixels");
return pRet;
}
return pRet;
}
static IImageObject* Load8BitImageFromTIFF(TIFF* tif)
{
uint32_t dwChannels = 0;
uint32_t dwPhotometric = 0;
TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &dwChannels);
TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &dwPhotometric);
uint32_t dwWidth = 0;
uint32_t dwHeight = 0;
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &dwWidth);
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &dwHeight);
EPixelFormat eFormat = ePixelFormat_R8G8B8X8;
if (dwChannels == 1 && dwPhotometric != PHOTOMETRIC_MINISBLACK)
{
eFormat = ePixelFormat_R8;
}
else if (dwChannels == 4)
{
eFormat = ePixelFormat_R8G8B8A8;
}
IImageObject* pRet = IImageObject::CreateImage(dwWidth, dwHeight, 1, eFormat);
uint8_t* dst;
uint32_t dwPitch;
pRet->GetImagePointer(0, dst, dwPitch);
AZStd::vector<uint8_t> buf(dwPitch);
for (uint32_t dwY = 0; dwY < dwHeight; ++dwY)
{
TIFFReadScanline(tif, &buf[0], dwY, 0); // read raw row
const uint8_t* srcLine = &buf[0];
uint8_t* dstLine = &dst[dwPitch * dwY];
if (dwChannels == 1)
{
if (dwPhotometric != PHOTOMETRIC_MINISBLACK)
{
for (uint32_t dwX = 0; dwX < dwWidth; ++dwX)
{
dstLine[0] = srcLine[0];
dstLine += 1;
srcLine += dwChannels;
}
}
else
{
for (uint32_t dwX = 0; dwX < dwWidth; ++dwX)
{
dstLine[0] = srcLine[0];
dstLine[1] = srcLine[0];
dstLine[2] = srcLine[0];
dstLine[3] = 0xFF;
dstLine += 4;
srcLine += dwChannels;
}
}
}
else if (dwChannels == 2)
{
if (dwPhotometric == PHOTOMETRIC_SEPARATED)
{
for (uint32_t dwX = 0; dwX < dwWidth; ++dwX)
{
// convert CMY to RGB (PHOTOMETRIC_SEPARATED refers to inks in TIFF, the value is inverted)
dstLine[0] = 0xFF - srcLine[0];
dstLine[1] = 0xFF - srcLine[1];
dstLine[2] = 0x00;
dstLine[3] = 0xFF;
dstLine += 4;
srcLine += dwChannels;
}
}
}
else
{
for (uint32_t dwX = 0; dwX < dwWidth; ++dwX)
{
dstLine[0] = srcLine[0];
dstLine[1] = srcLine[1];
dstLine[2] = srcLine[2];
dstLine[3] = (dwChannels == 3) ? 0xFF : srcLine[3];
dstLine += 4;
srcLine += dwChannels;
}
}
}
return pRet;
}
static IImageObject* Load16BitImageFromTIFF(TIFF* tif)
{
uint32_t dwChannels = 0;
uint32_t dwPhotometric = 0;
TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &dwChannels);
TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &dwPhotometric);
uint32_t dwWidth = 0;
uint32_t dwHeight = 0;
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &dwWidth);
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &dwHeight);
EPixelFormat eFormat = ePixelFormat_R16G16B16A16;
if (dwChannels == 1 && dwPhotometric != PHOTOMETRIC_MINISBLACK)
{
eFormat = ePixelFormat_R16;
}
IImageObject* pRet = IImageObject::CreateImage(dwWidth, dwHeight, 1, eFormat);
uint8_t* dst;
uint32_t dwPitch;
pRet->GetImagePointer(0, dst, dwPitch);
AZStd::vector<char> buf(dwPitch);
for (uint32_t dwY = 0; dwY < dwHeight; ++dwY)
{
TIFFReadScanline(tif, &buf[0], dwY, 0); // read raw row
const uint16_t* srcLine = (const uint16_t*)&buf[0];
uint16_t* dstLine = (uint16_t*)&dst[dwPitch * dwY];
if (dwChannels == 1)
{
if (dwPhotometric != PHOTOMETRIC_MINISBLACK)
{
for (uint32_t dwX = 0; dwX < dwWidth; ++dwX)
{
dstLine[0] = srcLine[0];
dstLine += 1;
srcLine += dwChannels;
}
}
else
{
for (uint32_t dwX = 0; dwX < dwWidth; ++dwX)
{
dstLine[0] = srcLine[0];
dstLine[1] = srcLine[0];
dstLine[2] = srcLine[0];
dstLine[3] = 0xFFFF;
dstLine += 4;
srcLine += dwChannels;
}
}
}
else if (dwChannels == 2)
{
if (dwPhotometric == PHOTOMETRIC_SEPARATED)
{
for (uint32_t dwX = 0; dwX < dwWidth; ++dwX)
{
//convert CMY to RGB (PHOTOMETRIC_SEPARATED refers to inks in TIFF, the value is inverted)
dstLine[0] = 0xFFFF - srcLine[0];
dstLine[1] = 0xFFFF - srcLine[1];
dstLine[2] = 0x0000;
dstLine[3] = 0xFFFF;
dstLine += 4;
srcLine += dwChannels;
}
}
}
else
{
for (uint32_t dwX = 0; dwX < dwWidth; ++dwX)
{
dstLine[0] = srcLine[0];
dstLine[1] = srcLine[1];
dstLine[2] = srcLine[2];
dstLine[3] = (dwChannels == 3) ? 0xFFFF : srcLine[3];
dstLine += 4;
srcLine += dwChannels;
}
}
}
return pRet;
}
static IImageObject* Load16BitHDRImageFromTIFF(TIFF* tif)
{
uint32_t dwChannels = 0;
uint32_t dwPhotometric = 0;
TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &dwChannels);
TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &dwPhotometric);
uint32_t dwWidth = 0;
uint32_t dwHeight = 0;
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &dwWidth);
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &dwHeight);
EPixelFormat eFormat = ePixelFormat_R16G16B16A16F;
if (dwChannels == 1 && dwPhotometric != PHOTOMETRIC_MINISBLACK)
{
eFormat = ePixelFormat_R16F;
}
IImageObject* pRet = IImageObject::CreateImage(dwWidth, dwHeight, 1, eFormat);
uint8_t* dst;
uint32_t dwPitch;
pRet->GetImagePointer(0, dst, dwPitch);
AZStd::vector<char> buf(dwPitch);
static const uint16_t zero = 0;
static const uint16_t one = 1;
for (uint32_t dwY = 0; dwY < dwHeight; ++dwY)
{
TIFFReadScanline(tif, &buf[0], dwY, 0); // read raw row
const uint16_t* srcLine = (const uint16_t*)&buf[0];
uint16_t* dstLine = (uint16_t*)&dst[dwPitch * dwY];
if (dwChannels == 1)
{
if (dwPhotometric != PHOTOMETRIC_MINISBLACK)
{
for (uint32_t dwX = 0; dwX < dwWidth; ++dwX)
{
dstLine[0] = srcLine[0];
dstLine += 1;
srcLine += dwChannels;
}
}
else
{
for (uint32_t dwX = 0; dwX < dwWidth; ++dwX)
{
dstLine[0] = srcLine[0];
dstLine[1] = srcLine[0];
dstLine[2] = srcLine[0];
dstLine[3] = one;
dstLine += 4;
srcLine += dwChannels;
}
}
}
else if (dwChannels == 2)
{
if (dwPhotometric == PHOTOMETRIC_SEPARATED)
{
for (uint32_t dwX = 0; dwX < dwWidth; ++dwX)
{
//but convert CMY to RGB (PHOTOMETRIC_SEPARATED refers to inks in TIFF, the value is inverted)
dstLine[0] = uint16_t(1.0f - srcLine[0]);
dstLine[1] = uint16_t(1.0f - srcLine[1]);
dstLine[2] = zero;
dstLine[3] = one;
dstLine += 4;
srcLine += dwChannels;
}
}
}
else
{
for (uint32_t dwX = 0; dwX < dwWidth; ++dwX)
{
dstLine[0] = srcLine[0];
dstLine[1] = srcLine[1];
dstLine[2] = srcLine[2];
dstLine[3] = (dwChannels == 3) ? one : srcLine[3];
dstLine += 4;
srcLine += dwChannels;
}
}
}
return pRet;
}
static IImageObject* Load32BitHDRImageFromTIFF(TIFF* tif)
{
uint32_t dwChannels = 0;
uint32_t dwPhotometric = 0;
TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &dwChannels);
TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &dwPhotometric);
uint32_t dwWidth = 0;
uint32_t dwHeight = 0;
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &dwWidth);
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &dwHeight);
EPixelFormat eFormat = ePixelFormat_R32G32B32A32F;
if (dwChannels == 1 && dwPhotometric != PHOTOMETRIC_MINISBLACK)
{
eFormat = ePixelFormat_R32F;
}
IImageObject* pRet = IImageObject::CreateImage(dwWidth, dwHeight, 1, eFormat);
uint8_t* dst;
uint32_t dwPitch;
pRet->GetImagePointer(0, dst, dwPitch);
AZStd::vector<char> buf(dwPitch);
for (uint32_t dwY = 0; dwY < dwHeight; ++dwY)
{
TIFFReadScanline(tif, &buf[0], dwY, 0); // read raw row
const float* srcLine = (const float*)&buf[0];
float* dstLine = (float*)&dst[dwPitch * dwY];
if (dwChannels == 1)
{
if (dwPhotometric != PHOTOMETRIC_MINISBLACK)
{
for (uint32_t dwX = 0; dwX < dwWidth; ++dwX)
{
// clamp negative values
const float v = AZ::GetMax(srcLine[0], 0.0f);
dstLine[0] = v;
++dstLine;
++srcLine;
}
}
else
{
for (uint32_t dwX = 0; dwX < dwWidth; ++dwX)
{
// clamp negative values
const float v = AZ::GetMax(srcLine[0], 0.0f);
dstLine[0] = v;
dstLine[1] = v;
dstLine[2] = v;
dstLine[3] = 1.0f;
dstLine += 4;
++srcLine;
}
}
}
else if (dwChannels == 2)
{
if (dwPhotometric == PHOTOMETRIC_SEPARATED)
{
for (uint32_t dwX = 0; dwX < dwWidth; ++dwX)
{
//convert CMY to RGB (PHOTOMETRIC_SEPARATED refers to inks in TIFF, the value is inverted)
dstLine[0] = 1.0f - AZ::GetMax(srcLine[0], 0.0f);
dstLine[1] = 1.0f - AZ::GetMax(srcLine[1], 0.0f);
dstLine[2] = 0.0f;
dstLine[3] = 1.0f;
dstLine += 4;
srcLine += dwChannels;
}
}
}
else
{
for (uint32_t dwX = 0; dwX < dwWidth; ++dwX)
{
// clamp negative values; don't swap red and blue -> RGB(A)
dstLine[0] = AZ::GetMax(srcLine[0], 0.0f);
dstLine[1] = AZ::GetMax(srcLine[1], 0.0f);
dstLine[2] = AZ::GetMax(srcLine[2], 0.0f);
dstLine[3] = (dwChannels == 3) ? 1.0f : AZ::GetMax(srcLine[3], 0.0f);
dstLine += 4;
srcLine += dwChannels;
}
}
}
return pRet;
}
const AZStd::string LoadSettingFromTIFF(const AZStd::string& filename)
{
AZStd::string setting = "";
TiffFileRead tiffRead(filename);
TIFF* tif = tiffRead.GetTiff();
if (tif == nullptr)
{
return setting;
}
// get image metadata
const unsigned char* buffer = nullptr;
unsigned int bufferLength = 0;
if (!TIFFGetField(tif, TIFFTAG_PHOTOSHOP, &bufferLength, &buffer)) // 34377 IPTC TAG
{
return setting;
}
const unsigned char* const bufferEnd = buffer + bufferLength;
// detailed structure here:
// https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/#50577409_pgfId-1037504
while (buffer < bufferEnd)
{
const unsigned char* const bufferStart = buffer;
// sanity check
if (buffer[0] != '8' || buffer[1] != 'B' || buffer[2] != 'I' || buffer[3] != 'M')
{
AZ_Warning("Image Processing", false, "Invalid Photoshop TIFF file [%s]!", filename.c_str());
return setting;
}
buffer += 4;
// get image resource id
const unsigned short resourceId = (((unsigned short)buffer[0]) << 8) | (unsigned short)buffer[1];
buffer += 2;
// get size of pascal string
const unsigned int dwNameSize = (unsigned int)buffer[0];
++buffer;
// get pascal string
AZStd::string szName(buffer, buffer + dwNameSize);
buffer += dwNameSize;
// align 2 bytes
if ((buffer - bufferStart) & 1)
{
++buffer;
}
// get size of resource data
const unsigned int dwSize =
(((unsigned int)buffer[0]) << 24) |
(((unsigned int)buffer[1]) << 16) |
(((unsigned int)buffer[2]) << 8) |
(unsigned int)buffer[3];
buffer += 4;
// IPTC-NAA record. Contains the [File Info...] information. Old RC use this section to store the setting string.
if (resourceId == 0x0404)
{
const unsigned char* const iptcBufferStart = buffer;
// Old RC uses IPTC ApplicationRecord tags SpecialInstructions to store the setting string
// IPTC Details: https://iptc.org/std/photometadata/specification/mapping/iptc-pmd-newsmlg2.html
unsigned int iptcPos = 0;
while (iptcPos + 5 < dwSize)
{
int marker = iptcBufferStart[iptcPos++];
int recordNumber = iptcBufferStart[iptcPos++];
int dataSetNumber = iptcBufferStart[iptcPos++];
int fieldLength = (iptcBufferStart[iptcPos++] << 8);
fieldLength += iptcBufferStart[iptcPos++];
// Ignore fields other than SpecialInstructions
if (marker != 0x1C || recordNumber != 0x02 || dataSetNumber != 0x28)
{
iptcPos += fieldLength;
continue;
}
//save the setting string before close file
setting = AZStd::string(iptcBufferStart + iptcPos, iptcBufferStart + iptcPos + fieldLength);
return setting;
}
}
buffer += dwSize;
// align 2 bytes
if ((buffer - bufferStart) & 1)
{
++buffer;
}
}
return setting;
}
}// namespace ImageTIFF
} //namespace ImageProcessingAtom