Skip to content

Commit

Permalink
FXX: readImage bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
rodlie committed Mar 6, 2022
1 parent 1c82229 commit 94b7ae0
Showing 1 changed file with 102 additions and 114 deletions.
216 changes: 102 additions & 114 deletions src/FXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,102 +44,89 @@ FXX::Image FXX::readImage(const std::string &file,
bool readLayers)
{
FXX::Image result;
if (!file.empty()) {
std::vector<Magick::Image> layers;
Magick::Image image;
Magick::Blob output;
Magick::Blob preview;
try {
if (readLayers) {
Magick::readImages(&layers, file.c_str());
image = layers[0];
layers.erase(layers.begin());
} else {
image.read(file.c_str());
}
image.magick("MIFF");
}
catch(Magick::Error &error_ ) {
result.error.append(error_.what());
return result;
if (file.empty()) { return result; }
std::vector<Magick::Image> layers;
Magick::Image image;
Magick::Blob output;
Magick::Blob preview;
image.quiet(true);
try { image.read(file); }
catch(Magick::Error &error) {
result.error.append(error.what());
return result;
}
catch(Magick::Warning &warning ) { result.warning.append(warning.what()); }
try {
if (readLayers) {
Magick::ReadOptions options;
options.quiet(true);
Magick::readImages(&layers, file, options);
if (layers.size() == 1) { layers.clear(); }
//layers.erase(layers.begin());
}
catch(Magick::Warning &warn_ ) {
result.warning.append(warn_.what());
}
catch(Magick::Error &error_ ) {
result.error.append(error_.what());
return result;
}
catch(Magick::Warning &warn_ ) { result.warning.append(warn_.what()); }
try {
image.magick("MIFF");

// get colorspace
result.colorspace = readImageColorspaceType(image);

// get image layers
if (layers.size() > 0) { result.layers = layers; }

// get image channels
result.channels = readImageChannelCount(image);

// get image profile
std::vector<unsigned char> imageProfile = readImageColorProfile(image,
failsafe);
if (imageProfile.size()>0) {
Magick::Blob profile(imageProfile.data(),
imageProfile.size());
image.profile("ICC", profile);
result.iccInputBuffer = imageProfile;
} else {
result.error = "No input profile!";
return result;
}
try {
// get colorspace
result.colorspace = readImageColorspaceType(image);

// get image layers
if (layers.size()>0) {
for (unsigned long i=0;i<layers.size();++i) {
layers[i].magick("MIFF");
}
result.layers = layers;
}

// get image channels
result.channels = readImageChannelCount(image);

// get image profile
std::vector<unsigned char> imageProfile = readImageColorProfile(image,
failsafe);
if (imageProfile.size()>0) {
Magick::Blob profile(imageProfile.data(),
imageProfile.size());
image.profile("ICC", profile);
result.iccInputBuffer = imageProfile;
} else {
result.error = "On input profile!";
return result;
}

// get meta info
if (image.profile("exif").length()>0) {
result.hasEXIF = true;
} else {
result.hasEXIF = false;
}
if (image.profile("IPTC").length()>0) {
result.hasIPTC = true;
} else {
result.hasIPTC = false;
}
result.comment = image.comment();
result.width = image.columns();
result.height = image.rows();
result.depth = image.depth();
result.created = image.attribute("date:create");
result.modified = image.attribute("date:modified");
result.filename = image.fileName();
result.format = image.format();

// write original
image.write(&output);
unsigned char *imgBuffer = reinterpret_cast<unsigned char*>(const_cast<void*>(output.data()));
std::vector<unsigned char> imgData(imgBuffer, imgBuffer + output.length());
result.imageBuffer = imgData;

// get image specs
if (getInfo) {
result.info = identify(imgData);
}

// make a preview
if (image.depth()>8) { image.depth(8); }
image.magick("BMP");
image.write(&preview);
unsigned char *preBuffer = reinterpret_cast<unsigned char*>(const_cast<void*>(preview.data()));
std::vector<unsigned char> preData(preBuffer, preBuffer + preview.length());
result.previewBuffer = preData;
}
catch(Magick::Error &error_ ) {
result.error.append(error_.what());
}
catch(Magick::Warning &warn_ ) {
result.warning.append(warn_.what());
}
// get meta info
result.hasEXIF = (image.profile("exif").length() > 0);
result.hasEXIF = (image.profile("IPTC").length() > 0);
result.comment = image.comment();
result.width = image.columns();
result.height = image.rows();
result.depth = image.depth();
result.created = image.attribute("date:create");
result.modified = image.attribute("date:modified");
result.filename = file;
result.format = image.format();

// write original
image.write(&output);
unsigned char *imgBuffer = reinterpret_cast<unsigned char*>(const_cast<void*>(output.data()));
std::vector<unsigned char> imgData(imgBuffer, imgBuffer + output.length());
result.imageBuffer = imgData;

// get image specs
if (getInfo) { result.info = identify(imgData); }

// make a preview
if (image.depth()>8) { image.depth(8); }
image.magick("BMP");
image.write(&preview);
unsigned char *preBuffer = reinterpret_cast<unsigned char*>(const_cast<void*>(preview.data()));
std::vector<unsigned char> preData(preBuffer, preBuffer + preview.length());
result.previewBuffer = preData;
}
catch(Magick::Error &error_ ) { result.error.append(error_.what()); }
catch(Magick::Warning &warn_ ) { result.warning.append(warn_.what()); }

return result;
}

Expand Down Expand Up @@ -421,31 +408,10 @@ std::vector<unsigned char> FXX::readImageColorProfile(Magick::Image image,
{
std::vector<unsigned char> result;
try {
if (image.iccColorProfile().length()>0) { // has embedded color profile?
if (image.iccColorProfile().length()>0) { // has embedded color profile
unsigned char *iccBuffer = reinterpret_cast<unsigned char*>(const_cast<void*>(image.iccColorProfile().data()));
std::vector<unsigned char> iccData(iccBuffer, iccBuffer + image.iccColorProfile().length());
result = iccData;
} else { // apply failsafe profile if missing input profile
if (failsafe.iccRGB.size()==0 ||
failsafe.iccCMYK.size()==0 ||
failsafe.iccGRAY.size()==0)
{
std::cout << "no failsafe profiles!" << std::endl;
return result;
}
Magick::Blob profile;
switch(readImageColorspaceType(image)) {
case FXX::RGBColorSpace:
result = failsafe.iccRGB;
break;
case FXX::CMYKColorSpace:
result = failsafe.iccCMYK;
break;
case FXX::GRAYColorSpace:
result = failsafe.iccGRAY;
break;
default:;
}
}
}
catch(Magick::Error &error_ ) {
Expand All @@ -454,6 +420,28 @@ std::vector<unsigned char> FXX::readImageColorProfile(Magick::Image image,
catch(Magick::Warning &warn_ ) {
std::cout << warn_.what() << std::endl;
}
if (result.empty()) { // apply failsafe profile if missing input profile
if (failsafe.iccRGB.size()==0 ||
failsafe.iccCMYK.size()==0 ||
failsafe.iccGRAY.size()==0)
{
std::cout << "no failsafe profiles!" << std::endl;
return result;
}
Magick::Blob profile;
switch(readImageColorspaceType(image)) {
case FXX::RGBColorSpace:
result = failsafe.iccRGB;
break;
case FXX::CMYKColorSpace:
result = failsafe.iccCMYK;
break;
case FXX::GRAYColorSpace:
result = failsafe.iccGRAY;
break;
default:;
}
}
return result;
}

Expand Down

0 comments on commit 94b7ae0

Please sign in to comment.