Krita Source Code Documentation
Loading...
Searching...
No Matches
KisExifIO Class Reference

#include <kis_exif_io.h>

+ Inheritance diagram for KisExifIO:

Public Member Functions

bool canSaveAllEntries (KisMetaData::Store *store) const override
 
QString id () const override
 
 KisExifIO ()
 
bool loadFrom (KisMetaData::Store *store, QIODevice *ioDevice) const override
 
QString name () const override
 
bool saveTo (const KisMetaData::Store *store, QIODevice *ioDevice, HeaderType headerType=NoHeader) const override
 
bool supportLoading () const override
 
bool supportSaving () const override
 
BackendType type () const override
 
 ~KisExifIO () override
 

Additional Inherited Members

Detailed Description

Definition at line 17 of file kis_exif_io.h.

Constructor & Destructor Documentation

◆ KisExifIO()

KisExifIO::KisExifIO ( )

Definition at line 412 of file kis_exif_io.cpp.

◆ ~KisExifIO()

KisExifIO::~KisExifIO ( )
override

Definition at line 417 of file kis_exif_io.cpp.

418{
419}

Member Function Documentation

◆ canSaveAllEntries()

bool KisExifIO::canSaveAllEntries ( KisMetaData::Store * store) const
overridevirtual
Parameters
storethe list of metadata
Returns
true if this backend is capable of saving all the metadata of the store

Implements KisMetaData::IOBackend.

Definition at line 557 of file kis_exif_io.cpp.

558{
559 return false; // It's a known fact that exif can't save all information, but TODO: write the check
560}

◆ id()

QString KisExifIO::id ( ) const
inlineoverridevirtual

Implements KisMetaData::IOBackend.

Definition at line 22 of file kis_exif_io.h.

23 {
24 return "exif";
25 }

◆ loadFrom()

bool KisExifIO::loadFrom ( KisMetaData::Store * store,
QIODevice * ioDevice ) const
overridevirtual
Parameters
storethe list of metadata to load
ioDevicethe device from where the metadata will be loaded
Returns
true if the load was successful

Implements KisMetaData::IOBackend.

Definition at line 562 of file kis_exif_io.cpp.

563{
564 if (!ioDevice->open(QIODevice::ReadOnly)) {
565 return false;
566 }
567 QByteArray arr(ioDevice->readAll());
568 Exiv2::ExifData exifData;
569 Exiv2::ByteOrder byteOrder;
570#if !EXIV2_TEST_VERSION(0, 18, 0)
571 exifData.load((const Exiv2::byte *)arr.data(), arr.size());
572 byteOrder = exifData.byteOrder();
573#else
574 try {
575 byteOrder =
576 Exiv2::ExifParser::decode(exifData, (const Exiv2::byte *)arr.data(), static_cast<uint32_t>(arr.size()));
577 } catch (const std::exception &ex) {
578 warnKrita << "Received exception trying to parse exiv data" << ex.what();
579 return false;
580 } catch (...) {
581 dbgKrita << "Received unknown exception trying to parse exiv data";
582 return false;
583 }
584#endif
585 dbgMetaData << "Byte order = " << byteOrder << ppVar(Exiv2::bigEndian) << ppVar(Exiv2::littleEndian);
586 dbgMetaData << "There are" << exifData.count() << " entries in the exif section";
587 const KisMetaData::Schema *tiffSchema =
589 Q_ASSERT(tiffSchema);
590 const KisMetaData::Schema *exifSchema =
592 Q_ASSERT(exifSchema);
593 const KisMetaData::Schema *dcSchema =
595 Q_ASSERT(dcSchema);
596 const KisMetaData::Schema *xmpSchema =
598 Q_ASSERT(xmpSchema);
599 const KisMetaData::Schema *makerNoteSchema =
601 Q_ASSERT(makerNoteSchema);
602
603 for (const Exiv2::Exifdatum &it : exifData) {
604 const uint16_t tag = it.tag();
605
608 || it.tagName() == "0x0000") {
609 dbgMetaData << it.key().c_str() << " is ignored";
610 } else if (tag == Exif::Photo::MakerNote) {
611 store->addEntry({makerNoteSchema, "RawData", exivValueToKMDValue(it.getValue(), false)});
612 } else if (tag == Exif::Image::DateTime) { // load as xmp:ModifyDate
613 store->addEntry({xmpSchema, "ModifyDate", exivValueToKMDValue(it.getValue(), false)});
614 } else if (tag == Exif::Image::ImageDescription) { // load as "dc:description"
615 store->addEntry({dcSchema, "description", exivValueToKMDValue(it.getValue(), false)});
616 } else if (tag == Exif::Image::Software) { // load as "xmp:CreatorTool"
617 store->addEntry({xmpSchema, "CreatorTool", exivValueToKMDValue(it.getValue(), false)});
618 } else if (tag == Exif::Image::Artist) { // load as dc:creator
619 QList<KisMetaData::Value> creators = {exivValueToKMDValue(it.getValue(), false)};
620 store->addEntry({dcSchema, "creator", {creators, KisMetaData::Value::OrderedArray}});
621 } else if (tag == Exif::Image::Copyright) { // load as dc:rights
622 store->addEntry({dcSchema, "rights", exivValueToKMDValue(it.getValue(), false)});
623 } else if (it.groupName() == "Image") {
624 // Tiff tags
625 const QString fixedTN(it.tagName().c_str());
626 if (tag == Exif::Image::ExifTag || tag == Exif::Image::GPSTag) {
627 dbgMetaData << "Ignoring " << it.key().c_str();
628 } else if (KisMetaData::Entry::isValidName(fixedTN)) {
629 store->addEntry({tiffSchema, fixedTN, exivValueToKMDValue(it.getValue(), false)});
630 } else {
631 dbgMetaData << "Invalid tag name: " << fixedTN;
632 }
633 } else if (it.groupName() == "Photo") {
634 // Exif tags
635 KisMetaData::Value metaDataValue;
637 metaDataValue = exifVersionToKMDValue(it.getValue());
638 } else if (tag == Exif::Photo::FileSource) {
639 metaDataValue = KisMetaData::Value(3);
640 } else if (tag == Exif::Photo::SceneType) {
641 metaDataValue = KisMetaData::Value(1);
642 } else if (tag == Exif::Photo::ComponentsConfiguration) {
643 metaDataValue = exifArrayToKMDIntOrderedArray(it.getValue());
644 } else if (tag == Exif::Photo::OECF) {
645 metaDataValue = exifOECFToKMDOECFStructure(it.getValue(), byteOrder);
647 metaDataValue = exivValueToKMDValue(it.getValue(), false);
648 } else if (tag == Exif::Photo::DeviceSettingDescription) {
649 metaDataValue = deviceSettingDescriptionExifToKMD(it.getValue());
650 } else if (tag == Exif::Photo::CFAPattern) {
651 metaDataValue = cfaPatternExifToKMD(it.getValue(), byteOrder);
652 } else if (tag == Exif::Photo::Flash) {
653 metaDataValue = flashExifToKMD(it.getValue());
654 } else if (tag == Exif::Photo::UserComment) {
655 if (it.getValue()->typeId() != Exiv2::undefined) {
656 KisMetaData::Value vUC = exivValueToKMDValue(it.getValue(), false);
657 Q_ASSERT(vUC.type() == KisMetaData::Value::Variant);
658 QVariant commentVar = vUC.asVariant();
659 QString comment;
660 if (commentVar.type() == QMetaType::QString) {
661 comment = commentVar.toString();
662 } else if (commentVar.type() == QMetaType::QByteArray) {
663 const QByteArray commentString = commentVar.toByteArray();
664 comment = QString::fromLatin1(commentString.constData(), commentString.size());
665 } else {
666 warnKrita << "KisExifIO: Unhandled UserComment value type.";
667 }
668 KisMetaData::Value vcomment(comment);
669 vcomment.addPropertyQualifier("xml:lang", KisMetaData::Value("x-default"));
671 alt.append(vcomment);
673 }
674 } else {
675 bool forceSeq = false;
677 if (tag == Exif::Photo::ISOSpeedRatings) {
678 forceSeq = true;
680 }
681 metaDataValue = exivValueToKMDValue(it.getValue(), forceSeq, arrayType);
682 }
683 if (tag == Exif::Photo::InteroperabilityTag || tag == 0xea1d
684 || metaDataValue.type() == KisMetaData::Value::Invalid) { // InteroperabilityTag isn't useful for XMP,
685 // 0xea1d isn't a valid Exif tag
686 warnMetaData << "Ignoring " << it.key().c_str();
687
688 } else {
689 store->addEntry({exifSchema, it.tagName().c_str(), metaDataValue});
690 }
691 } else if (it.groupName() == "Thumbnail") {
692 dbgMetaData << "Ignoring thumbnail tag :" << it.key().c_str();
693 } else if (it.groupName() == "GPSInfo") {
694 store->addEntry({exifSchema, it.tagName().c_str(), exivValueToKMDValue(it.getValue(), false)});
695 } else {
696 dbgMetaData << "Unknown exif tag, cannot load:" << it.key().c_str();
697 }
698 }
699 ioDevice->close();
700 return true;
701}
static bool isValidName(const QString &_name)
static KisMetaData::SchemaRegistry * instance()
const Schema * schemaFromUri(const QString &uri) const
static const QString TIFFSchemaUri
static const QString DublinCoreSchemaUri
static const QString EXIFSchemaUri
static const QString XMPSchemaUri
static const QString MakerNoteSchemaUri
bool addEntry(const Entry &entry)
QVariant asVariant() const
ValueType
Define the possible value type.
ValueType type() const
#define dbgKrita
Definition kis_debug.h:49
#define warnMetaData
Definition kis_debug.h:109
#define warnKrita
Definition kis_debug.h:94
#define ppVar(var)
Definition kis_debug.h:163
#define dbgMetaData
Definition kis_debug.h:64
KisMetaData::Value deviceSettingDescriptionExifToKMD(const Exiv2::Value::AutoPtr value)
KisMetaData::Value exifOECFToKMDOECFStructure(const Exiv2::Value::AutoPtr value, Exiv2::ByteOrder order)
KisMetaData::Value exifArrayToKMDIntOrderedArray(const Exiv2::Value::AutoPtr value)
KisMetaData::Value cfaPatternExifToKMD(const Exiv2::Value::AutoPtr value, Exiv2::ByteOrder order)
KisMetaData::Value flashExifToKMD(const Exiv2::Value::AutoPtr value)
KisMetaData::Value exifVersionToKMDValue(const Exiv2::Value::AutoPtr value)
KisMetaData::Value exivValueToKMDValue(const Exiv2::Value::AutoPtr &value, bool forceSeq, KisMetaData::Value::ValueType arrayType=KisMetaData::Value::UnorderedArray)
const uint16_t DateTime
const uint16_t StripByteCounts
const uint16_t RowsPerStrip
const uint16_t StripOffsets
const uint16_t JPEGInterchangeFormat
const uint16_t ImageDescription
const uint16_t GPSTag
const uint16_t ExifTag
const uint16_t JPEGInterchangeFormatLength
const uint16_t Copyright
const uint16_t Artist
const uint16_t Software
const uint16_t DateTimeDigitized
const uint16_t UserComment
const uint16_t DeviceSettingDescription
const uint16_t ComponentsConfiguration
const uint16_t FileSource
const uint16_t FlashpixVersion
const uint16_t SceneType
const uint16_t Flash
const uint16_t CFAPattern
const uint16_t ISOSpeedRatings
const uint16_t InteroperabilityTag
const uint16_t ExifVersion
const uint16_t MakerNote
const uint16_t DateTimeOriginal
const uint16_t OECF

References KisMetaData::Store::addEntry(), KisMetaData::Value::addPropertyQualifier(), Exif::Image::Artist, KisMetaData::Value::asVariant(), Exif::Photo::CFAPattern, cfaPatternExifToKMD(), Exif::Photo::ComponentsConfiguration, Exif::Image::Copyright, Exif::Image::DateTime, Exif::Photo::DateTimeDigitized, Exif::Photo::DateTimeOriginal, dbgKrita, dbgMetaData, Exif::Photo::DeviceSettingDescription, deviceSettingDescriptionExifToKMD(), KisMetaData::Schema::DublinCoreSchemaUri, exifArrayToKMDIntOrderedArray(), exifOECFToKMDOECFStructure(), KisMetaData::Schema::EXIFSchemaUri, Exif::Image::ExifTag, Exif::Photo::ExifVersion, exifVersionToKMDValue(), exivValueToKMDValue(), Exif::Photo::FileSource, Exif::Photo::Flash, flashExifToKMD(), Exif::Photo::FlashpixVersion, Exif::Image::GPSTag, Exif::Image::ImageDescription, KisMetaData::SchemaRegistry::instance(), Exif::Photo::InteroperabilityTag, KisMetaData::Value::Invalid, Exif::Photo::ISOSpeedRatings, KisMetaData::Entry::isValidName(), Exif::Image::JPEGInterchangeFormat, Exif::Image::JPEGInterchangeFormatLength, KisMetaData::Value::LangArray, Exif::Photo::MakerNote, KisMetaData::Schema::MakerNoteSchemaUri, Exif::Photo::OECF, KisMetaData::Value::OrderedArray, ppVar, Exif::Image::RowsPerStrip, Exif::Photo::SceneType, KisMetaData::SchemaRegistry::schemaFromUri(), Exif::Image::Software, Exif::Image::StripByteCounts, Exif::Image::StripOffsets, KisMetaData::Schema::TIFFSchemaUri, KisMetaData::Value::type(), KisMetaData::Value::UnorderedArray, Exif::Photo::UserComment, KisMetaData::Value::Variant, warnKrita, warnMetaData, and KisMetaData::Schema::XMPSchemaUri.

◆ name()

QString KisExifIO::name ( ) const
inlineoverridevirtual

Implements KisMetaData::IOBackend.

Definition at line 26 of file kis_exif_io.h.

27 {
28 return i18n("Exif");
29 }

◆ saveTo()

bool KisExifIO::saveTo ( const KisMetaData::Store * store,
QIODevice * ioDevice,
HeaderType headerType = NoHeader ) const
overridevirtual
Parameters
storethe list of metadata to save
ioDevicethe device to where the metadata will be saved
headerTypedetermine if an header must be prepend to the binary header, and if it does, which type of header
Returns
true if the save was successful (XXX: actually, all backends always return true...)

Implements KisMetaData::IOBackend.

Definition at line 421 of file kis_exif_io.cpp.

422{
423 ioDevice->open(QIODevice::WriteOnly);
424 Exiv2::ExifData exifData;
425 if (headerType == KisMetaData::IOBackend::JpegHeader) {
426 QByteArray header(6, 0);
427 header[0] = 0x45;
428 header[1] = 0x78;
429 header[2] = 0x69;
430 header[3] = 0x66;
431 header[4] = 0x00;
432 header[5] = 0x00;
433 ioDevice->write(header);
434 }
435
436 for (const KisMetaData::Entry &entry : *store) {
437 try {
438 dbgMetaData << "Trying to save: " << entry.name() << " of " << entry.schema()->prefix() << ":"
439 << entry.schema()->uri();
440 QString exivKey;
441 if (entry.schema()->uri() == KisMetaData::Schema::TIFFSchemaUri) {
442 exivKey = "Exif.Image." + entry.name();
443 } else if (entry.schema()->uri()
444 == KisMetaData::Schema::EXIFSchemaUri) { // Distinguish between exif and gps
445 if (entry.name().left(3) == "GPS") {
446 exivKey = "Exif.GPSInfo." + entry.name();
447 } else {
448 exivKey = "Exif.Photo." + entry.name();
449 }
450 } else if (entry.schema()->uri() == KisMetaData::Schema::DublinCoreSchemaUri) {
451 if (entry.name() == "description") {
452 exivKey = "Exif.Image.ImageDescription";
453 } else if (entry.name() == "creator") {
454 exivKey = "Exif.Image.Artist";
455 } else if (entry.name() == "rights") {
456 exivKey = "Exif.Image.Copyright";
457 }
458 } else if (entry.schema()->uri() == KisMetaData::Schema::XMPSchemaUri) {
459 if (entry.name() == "ModifyDate") {
460 exivKey = "Exif.Image.DateTime";
461 } else if (entry.name() == "CreatorTool") {
462 exivKey = "Exif.Image.Software";
463 }
464 } else if (entry.schema()->uri() == KisMetaData::Schema::MakerNoteSchemaUri) {
465 if (entry.name() == "RawData") {
466 exivKey = "Exif.Photo.MakerNote";
467 }
468 }
469 dbgMetaData << "Saving " << entry.name() << " to " << exivKey;
470 if (exivKey.isEmpty()) {
471 dbgMetaData << entry.qualifiedName() << " is unsavable to EXIF";
472 } else {
473 Exiv2::ExifKey exifKey(qPrintable(exivKey));
474 Exiv2::Value *v = 0;
475 if (exivKey == "Exif.Photo.ExifVersion" || exivKey == "Exif.Photo.FlashpixVersion") {
476 v = kmdValueToExifVersion(entry.value());
477 } else if (exivKey == "Exif.Photo.FileSource") {
478 char s[] = {0x03};
479 v = new Exiv2::DataValue((const Exiv2::byte *)s, 1);
480 } else if (exivKey == "Exif.Photo.SceneType") {
481 char s[] = {0x01};
482 v = new Exiv2::DataValue((const Exiv2::byte *)s, 1);
483 } else if (exivKey == "Exif.Photo.ComponentsConfiguration") {
484 v = kmdIntOrderedArrayToExifArray(entry.value());
485 } else if (exivKey == "Exif.Image.Artist") { // load as dc:creator
486 KisMetaData::Value creator = entry.value();
487 if (entry.value().asArray().size() > 0) {
488 creator = entry.value().asArray()[0];
489 }
490#if !EXIV2_TEST_VERSION(0, 21, 0)
491 v = kmdValueToExivValue(creator, Exiv2::ExifTags::tagType(exifKey.tag(), exifKey.ifdId()));
492#else
493 v = kmdValueToExivValue(creator, exifKey.defaultTypeId());
494#endif
495 } else if (exivKey == "Exif.Photo.OECF") {
496 v = kmdOECFStructureToExifOECF(entry.value());
497 } else if (exivKey == "Exif.Photo.DeviceSettingDescription") {
498 v = deviceSettingDescriptionKMDToExif(entry.value());
499 } else if (exivKey == "Exif.Photo.CFAPattern") {
500 v = cfaPatternKMDToExif(entry.value());
501 } else if (exivKey == "Exif.Photo.Flash") {
502 v = flashKMDToExif(entry.value());
503 } else if (exivKey == "Exif.Photo.UserComment") {
504 Q_ASSERT(entry.value().type() == KisMetaData::Value::LangArray);
505 QMap<QString, KisMetaData::Value> langArr = entry.value().asLangArray();
506 if (langArr.contains("x-default")) {
507#if !EXIV2_TEST_VERSION(0, 21, 0)
508 v = kmdValueToExivValue(langArr.value("x-default"),
509 Exiv2::ExifTags::tagType(exifKey.tag(), exifKey.ifdId()));
510#else
511 v = kmdValueToExivValue(langArr.value("x-default"), exifKey.defaultTypeId());
512#endif
513 } else if (langArr.size() > 0) {
514#if !EXIV2_TEST_VERSION(0, 21, 0)
515 v = kmdValueToExivValue(langArr.begin().value(),
516 Exiv2::ExifTags::tagType(exifKey.tag(), exifKey.ifdId()));
517#else
518 v = kmdValueToExivValue(langArr.begin().value(), exifKey.defaultTypeId());
519#endif
520 }
521 } else {
522 dbgMetaData << exifKey.tag();
523#if !EXIV2_TEST_VERSION(0, 21, 0)
524 v = kmdValueToExivValue(entry.value(), Exiv2::ExifTags::tagType(exifKey.tag(), exifKey.ifdId()));
525#else
526 v = kmdValueToExivValue(entry.value(), exifKey.defaultTypeId());
527#endif
528 }
529 if (v && v->typeId() != Exiv2::invalidTypeId) {
530 dbgMetaData << "Saving key" << exivKey << " of KMD value" << entry.value();
531 exifData.add(exifKey, v);
532 } else {
533 dbgMetaData << "No exif value was created for" << entry.qualifiedName() << " as"
534 << exivKey; // << " of KMD value" << entry.value();
535 }
536 }
537#if EXIV2_TEST_VERSION(0,28,0)
538 } catch (Exiv2::Error &e) {
539#else
540 } catch (Exiv2::AnyError &e) {
541#endif
542 dbgMetaData << "exiv error " << e.what();
543 }
544 }
545#if !EXIV2_TEST_VERSION(0, 18, 0)
546 Exiv2::DataBuf rawData = exifData.copy();
547 ioDevice->write((const char *)rawData.pData_, rawData.size_);
548#else
549 Exiv2::Blob rawData;
550 Exiv2::ExifParser::encode(rawData, Exiv2::littleEndian, exifData);
551 ioDevice->write((const char *)&*rawData.begin(), static_cast<int>(rawData.size()));
552#endif
553 ioDevice->close();
554 return true;
555}
qreal v
@ JpegHeader
Append Jpeg-style header.
QList< KisMetaData::Value > asArray() const
Exiv2::Value * deviceSettingDescriptionKMDToExif(const KisMetaData::Value &value)
Exiv2::Value * kmdIntOrderedArrayToExifArray(const KisMetaData::Value &value)
Exiv2::Value * cfaPatternKMDToExif(const KisMetaData::Value &value)
Exiv2::Value * flashKMDToExif(const KisMetaData::Value &value)
Exiv2::Value * kmdOECFStructureToExifOECF(const KisMetaData::Value &value)
Exiv2::Value * kmdValueToExifVersion(const KisMetaData::Value &value)
Exiv2::Value * kmdValueToExivValue(const KisMetaData::Value &value, Exiv2::TypeId type)
Convert a KisMetaData to an Exiv value.

References KisMetaData::Value::asArray(), cfaPatternKMDToExif(), dbgMetaData, deviceSettingDescriptionKMDToExif(), KisMetaData::Schema::DublinCoreSchemaUri, KisMetaData::Schema::EXIFSchemaUri, flashKMDToExif(), KisMetaData::IOBackend::JpegHeader, kmdIntOrderedArrayToExifArray(), kmdOECFStructureToExifOECF(), kmdValueToExifVersion(), kmdValueToExivValue(), KisMetaData::Value::LangArray, KisMetaData::Schema::MakerNoteSchemaUri, KisMetaData::Schema::TIFFSchemaUri, v, and KisMetaData::Schema::XMPSchemaUri.

◆ supportLoading()

bool KisExifIO::supportLoading ( ) const
inlineoverridevirtual
Returns
true if this backend support loading

Implements KisMetaData::IOBackend.

Definition at line 40 of file kis_exif_io.h.

41 {
42 return true;
43 }

◆ supportSaving()

bool KisExifIO::supportSaving ( ) const
inlineoverridevirtual
Returns
tell if this backend support saving

Implements KisMetaData::IOBackend.

Definition at line 34 of file kis_exif_io.h.

35 {
36 return true;
37 }

◆ type()

BackendType KisExifIO::type ( ) const
inlineoverridevirtual
Returns
the type of the backend

Implements KisMetaData::IOBackend.

Definition at line 30 of file kis_exif_io.h.

References KisMetaData::IOBackend::Binary.


The documentation for this class was generated from the following files: