You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
exiv2/src/tiffimage.cpp

351 lines
12 KiB
C++

// ***************************************************************** -*- C++ -*-
/*
* Copyright (C) 2004-2021 Exiv2 authors
* This program is part of the Exiv2 distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301 USA.
*/
// *****************************************************************************
// included header files
#include "config.h"
#include "tiffimage.hpp"
#include "tiffimage_int.hpp"
#include "tiffcomposite_int.hpp"
#include "tiffvisitor_int.hpp"
#include "orfimage.hpp"
#include "makernote_int.hpp"
#include "nikonmn_int.hpp"
#include "image.hpp"
#include "image_int.hpp"
#include "error.hpp"
#include "futils.hpp"
#include "types.hpp"
#include "basicio.hpp"
#include "i18n.h" // NLS support.
// + standard includes
#include <string>
#include <iostream>
#include <iomanip>
#include <cassert>
#include <cstdarg>
/* --------------------------------------------------------------------------
Todo:
+ CR2 Makernotes don't seem to have a next pointer but Canon Jpeg Makernotes
do. What a mess. (That'll become an issue when it comes to writing to CR2)
+ Sony makernotes in RAW files do not seem to have header like those in Jpegs.
And maybe no next pointer either.
in crwimage.* :
+ Fix CiffHeader according to TiffHeader
+ Combine Error(kerNotAJpeg) and Error(kerNotACrwImage), add format argument %1
+ Search crwimage for todos, fix writeMetadata comment
+ rename loadStack to getPath for consistency
-------------------------------------------------------------------------- */
// *****************************************************************************
// class member definitions
namespace Exiv2 {
using namespace Internal;
TiffImage::TiffImage(BasicIo::UniquePtr io, bool /*create*/)
Fix issues detected with PVS-Studio + other little improvements (#1689) * avoid re-declaration of constant variables * Replace pthreads critical section with std::mutex * ci - better naming * cmake - increase minimum version to 3.11. Use project DESCRIPTION * fix - do not treat string::find() return type as bool * remove conditions that were always true * remove condition that were always false * Remove EXV_HAVE_GMTIME_R which is not used anymore * pixelWidth_ was inherited from Exiv2::Image The width & height variables in the TiffImage class need to be mutable to be able to change their values on the getters pixelHeight() and pixelWidth() ... Do not ask me why ... * Remove superfluous if * pvs:V766 item with identical key added already * pvs:V730 not all members were initialized (time) * pvs:V730 not all members are initialized * pvs:v668 no point in testing pointer against null after new * pvs:V1048 variable assigned the same value * replace c-style dynamic vector with std one * pvs:547 fakeData is always true * Remove useless constructor in derived class * pvs:V690 modern way to disable copy-ctor * Replace malloc/free with new/delete. No need to check for null * pvs:V1028 cast operands and not result * Remove custom MIN/MAX functions * pvs:V595 pointer used before verified against null * pvs: index used before being checked * pvs:V1028 possible overflow. Cast operands * pvs:v575 potential null pointer passed to other functions * pvs:V547 deal with always true/false expressions * pvs:V560 part of conditional expressions always false or true * pvs:V701 possible break in realloc -> move to std::vector * Make some classes 'final' * Replace sprintf with std::to_string() * fix compilation on windows
4 years ago
: Image(ImageType::tiff, mdExif | mdIptc | mdXmp, std::move(io))
, pixelWidthPrimary_(0)
, pixelHeightPrimary_(0)
{
Fix issues detected with PVS-Studio + other little improvements (#1689) * avoid re-declaration of constant variables * Replace pthreads critical section with std::mutex * ci - better naming * cmake - increase minimum version to 3.11. Use project DESCRIPTION * fix - do not treat string::find() return type as bool * remove conditions that were always true * remove condition that were always false * Remove EXV_HAVE_GMTIME_R which is not used anymore * pixelWidth_ was inherited from Exiv2::Image The width & height variables in the TiffImage class need to be mutable to be able to change their values on the getters pixelHeight() and pixelWidth() ... Do not ask me why ... * Remove superfluous if * pvs:V766 item with identical key added already * pvs:V730 not all members were initialized (time) * pvs:V730 not all members are initialized * pvs:v668 no point in testing pointer against null after new * pvs:V1048 variable assigned the same value * replace c-style dynamic vector with std one * pvs:547 fakeData is always true * Remove useless constructor in derived class * pvs:V690 modern way to disable copy-ctor * Replace malloc/free with new/delete. No need to check for null * pvs:V1028 cast operands and not result * Remove custom MIN/MAX functions * pvs:V595 pointer used before verified against null * pvs: index used before being checked * pvs:V1028 possible overflow. Cast operands * pvs:v575 potential null pointer passed to other functions * pvs:V547 deal with always true/false expressions * pvs:V560 part of conditional expressions always false or true * pvs:V701 possible break in realloc -> move to std::vector * Make some classes 'final' * Replace sprintf with std::to_string() * fix compilation on windows
4 years ago
} // TiffImage::TiffImage
//! Structure for TIFF compression to MIME type mappings
struct MimeTypeList {
//! Comparison operator for compression
bool operator==(int compression) const { return compression_ == compression; }
int compression_; //!< TIFF compression
const char* mimeType_; //!< MIME type
};
//! List of TIFF compression to MIME type mappings
constexpr MimeTypeList mimeTypeList[] = {
{32770, "image/x-samsung-srw"},
{34713, "image/x-nikon-nef"},
{65535, "image/x-pentax-pef"},
};
std::string TiffImage::mimeType() const
{
if (!mimeType_.empty()) return mimeType_;
mimeType_ = std::string("image/tiff");
std::string key = "Exif." + primaryGroup() + ".Compression";
auto md = exifData_.findKey(ExifKey(key));
if (md != exifData_.end() && md->count() > 0) {
const MimeTypeList* i = find(mimeTypeList, static_cast<int>(md->toLong()));
if (i) mimeType_ = std::string(i->mimeType_);
}
return mimeType_;
}
std::string TiffImage::primaryGroup() const
{
if (!primaryGroup_.empty()) return primaryGroup_;
static const char* keys[] = {
"Exif.Image.NewSubfileType",
"Exif.SubImage1.NewSubfileType",
"Exif.SubImage2.NewSubfileType",
"Exif.SubImage3.NewSubfileType",
15 years ago
"Exif.SubImage4.NewSubfileType",
"Exif.SubImage5.NewSubfileType",
"Exif.SubImage6.NewSubfileType",
"Exif.SubImage7.NewSubfileType",
"Exif.SubImage8.NewSubfileType",
"Exif.SubImage9.NewSubfileType"
};
// Find the group of the primary image, default to "Image"
primaryGroup_ = std::string("Image");
for (auto&& i : keys) {
auto md = exifData_.findKey(ExifKey(i));
// Is it the primary image?
if (md != exifData_.end() && md->count() > 0 && md->toLong() == 0) {
// Sometimes there is a JPEG primary image; that's not our first choice
primaryGroup_ = md->groupName();
std::string key = "Exif." + primaryGroup_ + ".JPEGInterchangeFormat";
if (exifData_.findKey(ExifKey(key)) == exifData_.end()) break;
}
}
return primaryGroup_;
}
int TiffImage::pixelWidth() const
{
Fix issues detected with PVS-Studio + other little improvements (#1689) * avoid re-declaration of constant variables * Replace pthreads critical section with std::mutex * ci - better naming * cmake - increase minimum version to 3.11. Use project DESCRIPTION * fix - do not treat string::find() return type as bool * remove conditions that were always true * remove condition that were always false * Remove EXV_HAVE_GMTIME_R which is not used anymore * pixelWidth_ was inherited from Exiv2::Image The width & height variables in the TiffImage class need to be mutable to be able to change their values on the getters pixelHeight() and pixelWidth() ... Do not ask me why ... * Remove superfluous if * pvs:V766 item with identical key added already * pvs:V730 not all members were initialized (time) * pvs:V730 not all members are initialized * pvs:v668 no point in testing pointer against null after new * pvs:V1048 variable assigned the same value * replace c-style dynamic vector with std one * pvs:547 fakeData is always true * Remove useless constructor in derived class * pvs:V690 modern way to disable copy-ctor * Replace malloc/free with new/delete. No need to check for null * pvs:V1028 cast operands and not result * Remove custom MIN/MAX functions * pvs:V595 pointer used before verified against null * pvs: index used before being checked * pvs:V1028 possible overflow. Cast operands * pvs:v575 potential null pointer passed to other functions * pvs:V547 deal with always true/false expressions * pvs:V560 part of conditional expressions always false or true * pvs:V701 possible break in realloc -> move to std::vector * Make some classes 'final' * Replace sprintf with std::to_string() * fix compilation on windows
4 years ago
if (pixelWidthPrimary_ != 0) {
return pixelWidthPrimary_;
}
ExifKey key(std::string("Exif.") + primaryGroup() + std::string(".ImageWidth"));
auto imageWidth = exifData_.findKey(key);
if (imageWidth != exifData_.end() && imageWidth->count() > 0) {
Fix issues detected with PVS-Studio + other little improvements (#1689) * avoid re-declaration of constant variables * Replace pthreads critical section with std::mutex * ci - better naming * cmake - increase minimum version to 3.11. Use project DESCRIPTION * fix - do not treat string::find() return type as bool * remove conditions that were always true * remove condition that were always false * Remove EXV_HAVE_GMTIME_R which is not used anymore * pixelWidth_ was inherited from Exiv2::Image The width & height variables in the TiffImage class need to be mutable to be able to change their values on the getters pixelHeight() and pixelWidth() ... Do not ask me why ... * Remove superfluous if * pvs:V766 item with identical key added already * pvs:V730 not all members were initialized (time) * pvs:V730 not all members are initialized * pvs:v668 no point in testing pointer against null after new * pvs:V1048 variable assigned the same value * replace c-style dynamic vector with std one * pvs:547 fakeData is always true * Remove useless constructor in derived class * pvs:V690 modern way to disable copy-ctor * Replace malloc/free with new/delete. No need to check for null * pvs:V1028 cast operands and not result * Remove custom MIN/MAX functions * pvs:V595 pointer used before verified against null * pvs: index used before being checked * pvs:V1028 possible overflow. Cast operands * pvs:v575 potential null pointer passed to other functions * pvs:V547 deal with always true/false expressions * pvs:V560 part of conditional expressions always false or true * pvs:V701 possible break in realloc -> move to std::vector * Make some classes 'final' * Replace sprintf with std::to_string() * fix compilation on windows
4 years ago
pixelWidthPrimary_ = static_cast<int>(imageWidth->toLong());
}
Fix issues detected with PVS-Studio + other little improvements (#1689) * avoid re-declaration of constant variables * Replace pthreads critical section with std::mutex * ci - better naming * cmake - increase minimum version to 3.11. Use project DESCRIPTION * fix - do not treat string::find() return type as bool * remove conditions that were always true * remove condition that were always false * Remove EXV_HAVE_GMTIME_R which is not used anymore * pixelWidth_ was inherited from Exiv2::Image The width & height variables in the TiffImage class need to be mutable to be able to change their values on the getters pixelHeight() and pixelWidth() ... Do not ask me why ... * Remove superfluous if * pvs:V766 item with identical key added already * pvs:V730 not all members were initialized (time) * pvs:V730 not all members are initialized * pvs:v668 no point in testing pointer against null after new * pvs:V1048 variable assigned the same value * replace c-style dynamic vector with std one * pvs:547 fakeData is always true * Remove useless constructor in derived class * pvs:V690 modern way to disable copy-ctor * Replace malloc/free with new/delete. No need to check for null * pvs:V1028 cast operands and not result * Remove custom MIN/MAX functions * pvs:V595 pointer used before verified against null * pvs: index used before being checked * pvs:V1028 possible overflow. Cast operands * pvs:v575 potential null pointer passed to other functions * pvs:V547 deal with always true/false expressions * pvs:V560 part of conditional expressions always false or true * pvs:V701 possible break in realloc -> move to std::vector * Make some classes 'final' * Replace sprintf with std::to_string() * fix compilation on windows
4 years ago
return pixelWidthPrimary_;
}
int TiffImage::pixelHeight() const
{
Fix issues detected with PVS-Studio + other little improvements (#1689) * avoid re-declaration of constant variables * Replace pthreads critical section with std::mutex * ci - better naming * cmake - increase minimum version to 3.11. Use project DESCRIPTION * fix - do not treat string::find() return type as bool * remove conditions that were always true * remove condition that were always false * Remove EXV_HAVE_GMTIME_R which is not used anymore * pixelWidth_ was inherited from Exiv2::Image The width & height variables in the TiffImage class need to be mutable to be able to change their values on the getters pixelHeight() and pixelWidth() ... Do not ask me why ... * Remove superfluous if * pvs:V766 item with identical key added already * pvs:V730 not all members were initialized (time) * pvs:V730 not all members are initialized * pvs:v668 no point in testing pointer against null after new * pvs:V1048 variable assigned the same value * replace c-style dynamic vector with std one * pvs:547 fakeData is always true * Remove useless constructor in derived class * pvs:V690 modern way to disable copy-ctor * Replace malloc/free with new/delete. No need to check for null * pvs:V1028 cast operands and not result * Remove custom MIN/MAX functions * pvs:V595 pointer used before verified against null * pvs: index used before being checked * pvs:V1028 possible overflow. Cast operands * pvs:v575 potential null pointer passed to other functions * pvs:V547 deal with always true/false expressions * pvs:V560 part of conditional expressions always false or true * pvs:V701 possible break in realloc -> move to std::vector * Make some classes 'final' * Replace sprintf with std::to_string() * fix compilation on windows
4 years ago
if (pixelHeightPrimary_ != 0) {
return pixelHeightPrimary_;
}
ExifKey key(std::string("Exif.") + primaryGroup() + std::string(".ImageLength"));
auto imageHeight = exifData_.findKey(key);
if (imageHeight != exifData_.end() && imageHeight->count() > 0) {
Fix issues detected with PVS-Studio + other little improvements (#1689) * avoid re-declaration of constant variables * Replace pthreads critical section with std::mutex * ci - better naming * cmake - increase minimum version to 3.11. Use project DESCRIPTION * fix - do not treat string::find() return type as bool * remove conditions that were always true * remove condition that were always false * Remove EXV_HAVE_GMTIME_R which is not used anymore * pixelWidth_ was inherited from Exiv2::Image The width & height variables in the TiffImage class need to be mutable to be able to change their values on the getters pixelHeight() and pixelWidth() ... Do not ask me why ... * Remove superfluous if * pvs:V766 item with identical key added already * pvs:V730 not all members were initialized (time) * pvs:V730 not all members are initialized * pvs:v668 no point in testing pointer against null after new * pvs:V1048 variable assigned the same value * replace c-style dynamic vector with std one * pvs:547 fakeData is always true * Remove useless constructor in derived class * pvs:V690 modern way to disable copy-ctor * Replace malloc/free with new/delete. No need to check for null * pvs:V1028 cast operands and not result * Remove custom MIN/MAX functions * pvs:V595 pointer used before verified against null * pvs: index used before being checked * pvs:V1028 possible overflow. Cast operands * pvs:v575 potential null pointer passed to other functions * pvs:V547 deal with always true/false expressions * pvs:V560 part of conditional expressions always false or true * pvs:V701 possible break in realloc -> move to std::vector * Make some classes 'final' * Replace sprintf with std::to_string() * fix compilation on windows
4 years ago
pixelHeightPrimary_ = imageHeight->toLong();
}
Fix issues detected with PVS-Studio + other little improvements (#1689) * avoid re-declaration of constant variables * Replace pthreads critical section with std::mutex * ci - better naming * cmake - increase minimum version to 3.11. Use project DESCRIPTION * fix - do not treat string::find() return type as bool * remove conditions that were always true * remove condition that were always false * Remove EXV_HAVE_GMTIME_R which is not used anymore * pixelWidth_ was inherited from Exiv2::Image The width & height variables in the TiffImage class need to be mutable to be able to change their values on the getters pixelHeight() and pixelWidth() ... Do not ask me why ... * Remove superfluous if * pvs:V766 item with identical key added already * pvs:V730 not all members were initialized (time) * pvs:V730 not all members are initialized * pvs:v668 no point in testing pointer against null after new * pvs:V1048 variable assigned the same value * replace c-style dynamic vector with std one * pvs:547 fakeData is always true * Remove useless constructor in derived class * pvs:V690 modern way to disable copy-ctor * Replace malloc/free with new/delete. No need to check for null * pvs:V1028 cast operands and not result * Remove custom MIN/MAX functions * pvs:V595 pointer used before verified against null * pvs: index used before being checked * pvs:V1028 possible overflow. Cast operands * pvs:v575 potential null pointer passed to other functions * pvs:V547 deal with always true/false expressions * pvs:V560 part of conditional expressions always false or true * pvs:V701 possible break in realloc -> move to std::vector * Make some classes 'final' * Replace sprintf with std::to_string() * fix compilation on windows
4 years ago
return pixelHeightPrimary_;
}
void TiffImage::setComment(const std::string& /*comment*/)
{
// not supported
throw(Error(kerInvalidSettingForImage, "Image comment", "TIFF"));
}
void TiffImage::readMetadata()
{
#ifdef EXIV2_DEBUG_MESSAGES
std::cerr << "Reading TIFF file " << io_->path() << "\n";
#endif
7 years ago
if (io_->open() != 0) {
throw Error(kerDataSourceOpenFailed, io_->path(), strError());
}
IoCloser closer(*io_);
// Ensure that this is the correct image type
if (!isTiffType(*io_, false)) {
7 years ago
if (io_->error() || io_->eof())
throw Error(kerFailedToReadImageData);
throw Error(kerNotAnImage, "TIFF");
}
clearMetadata();
ByteOrder bo =
TiffParser::decode(exifData_, iptcData_, xmpData_, io_->mmap(), static_cast<uint32_t>(io_->size()));
setByteOrder(bo);
// read profile from the metadata
Exiv2::ExifKey key("Exif.Image.InterColorProfile");
auto pos = exifData_.findKey(key);
7 years ago
if ( pos != exifData_.end() ) {
long size = pos->count() * pos->typeSize();
if (size == 0) {
throw Error(kerFailedToReadImageData);
}
iccProfile_.alloc(size);
pos->copy(iccProfile_.data(),bo);
}
7 years ago
}
void TiffImage::writeMetadata()
{
#ifdef EXIV2_DEBUG_MESSAGES
std::cerr << "Writing TIFF file " << io_->path() << "\n";
#endif
ByteOrder bo = byteOrder();
byte* pData = nullptr;
long size = 0;
IoCloser closer(*io_);
if (io_->open() == 0) {
// Ensure that this is the correct image type
if (isTiffType(*io_, false)) {
pData = io_->mmap(true);
size = static_cast<long>(io_->size());
TiffHeader tiffHeader;
if (0 == tiffHeader.read(pData, 8)) {
bo = tiffHeader.byteOrder();
}
}
}
if (bo == invalidByteOrder) {
bo = littleEndian;
}
setByteOrder(bo);
// fixup ICC profile
Exiv2::ExifKey key("Exif.Image.InterColorProfile");
auto pos = exifData_.findKey(key);
bool found = pos != exifData_.end();
if ( iccProfileDefined() ) {
Exiv2::DataValue value(iccProfile_.c_data(), iccProfile_.size());
if ( found ) pos->setValue(&value);
else exifData_.add(key,&value);
} else {
if ( found ) exifData_.erase(pos);
}
// set usePacket to influence TiffEncoder::encodeXmp() called by TiffVisitor.encode()
xmpData().usePacket(writeXmpFromPacket());
TiffParser::encode(*io_, pData, size, bo, exifData_, iptcData_, xmpData_); // may throw
} // TiffImage::writeMetadata
ByteOrder TiffParser::decode(
ExifData& exifData,
IptcData& iptcData,
XmpData& xmpData,
const byte* pData,
uint32_t size
)
{
uint32_t root = Tag::root;
// #1402 Fujifilm RAF. Change root when parsing embedded tiff
Exiv2::ExifKey key("Exif.Image.Make");
if (exifData.findKey(key) != exifData.end()) {
if ( exifData.findKey(key)->toString() == "FUJIFILM" ) {
root = Tag::fuji;
}
}
return TiffParserWorker::decode(exifData,
iptcData,
xmpData,
pData,
size,
root,
TiffMapping::findDecoder);
} // TiffParser::decode
WriteMethod TiffParser::encode(
BasicIo& io,
const byte* pData,
uint32_t size,
ByteOrder byteOrder,
const ExifData& exifData,
const IptcData& iptcData,
const XmpData& xmpData
)
{
// Copy to be able to modify the Exif data
ExifData ed = exifData;
// Delete IFDs which do not occur in TIFF images
static const IfdId filteredIfds[] = {
panaRawId
};
for (auto&& filteredIfd : filteredIfds) {
#ifdef EXIV2_DEBUG_MESSAGES
std::cerr << "Warning: Exif IFD " << filteredIfd << " not encoded\n";
#endif
ed.erase(std::remove_if(ed.begin(), ed.end(), FindExifdatum(filteredIfd)), ed.end());
}
std::unique_ptr<TiffHeaderBase> header(new TiffHeader(byteOrder));
return TiffParserWorker::encode(io, pData, size, ed, iptcData, xmpData, Tag::root, TiffMapping::findEncoder,
header.get(), nullptr);
} // TiffParser::encode
// *************************************************************************
// free functions
Image::UniquePtr newTiffInstance(BasicIo::UniquePtr io, bool create)
{
Image::UniquePtr image(new TiffImage(std::move(io), create));
if (!image->good()) {
image.reset();
}
return image;
}
bool isTiffType(BasicIo& iIo, bool advance)
{
const int32_t len = 8;
byte buf[len];
iIo.read(buf, len);
if (iIo.error() || iIo.eof()) {
return false;
}
TiffHeader tiffHeader;
bool rc = tiffHeader.read(buf, len);
if (!advance || !rc) {
iIo.seek(-len, BasicIo::cur);
}
return rc;
}
void TiffImage::printStructure(std::ostream& out, Exiv2::PrintStructureOption option,int depth)
{
if (io_->open() != 0) throw Error(kerDataSourceOpenFailed, io_->path(), strError());
// Ensure that this is the correct image type
if (imageType() == ImageType::none) {
if (!isTiffType(*io_, false)) {
if (io_->error() || io_->eof())
throw Error(kerFailedToReadImageData);
throw Error(kerNotAJpeg);
}
}
io_->seek(0,BasicIo::beg);
printTiffStructure(io(),out,option,depth-1);
}
} // namespace Exiv2