Project

General

Profile

Exif copying will not include profile data.

Added by Tim Zaman almost 8 years ago

Hi all,

I have a fairly straightforward piece of code that seems to work for copying Exif data from one image to another. However, although it seems to loop over all exif data, it does not include the (embedded) (icc) color profile and related information. What have i overlooked here? Congratulations on an excellent library.

      std::cerr << "processing file: " << from.c_str() << " and " << to.c_str() << std::endl;
       //get source and destination exif data
       //THROWS, if opening the file fails or it contains data of an unknown image type.
       Exiv2::Image::AutoPtr sourceimage = Exiv2::ImageFactory::open(from);
       Exiv2::Image::AutoPtr destimage = Exiv2::ImageFactory::open(to);
       //Callers must check the size of individual metadata types before accessing the data.
       //readMetadata THROWS an exception if opening or reading of the file fails or the image data is not valid (does not look like data of the specific image type).
       sourceimage->readMetadata();
       Exiv2::ExifData &src_exifData = sourceimage->exifData();
       if (src_exifData.empty()) {
              throw Exiv2::Error(1, "No exif data found in the image");
       }
       if (dont_overwrite) {
              //doesn't throw anything if it is empty
              destimage->readMetadata();
              //doesn't throw anything if it is empty
              Exiv2::ExifData &dest_exifData = destimage->exifData();
              //end delimiter for this source image data
              Exiv2::ExifData::const_iterator end_src = src_exifData.end();
              //for all the tags in the source exif data
              for (Exiv2::ExifData::const_iterator i = src_exifData.begin(); i != end_src; ++i) {
                            //check if current source key exists in destination file
                            Exiv2::ExifData::iterator maybe_exists = dest_exifData.findKey( Exiv2::ExifKey(i->key()) );
                            //if exists AND we are told not to overwrite
                            if (maybe_exists != dest_exifData.end()) {
                                   continue;
                            } else {
                                   //here we copy the value
                                   //we create a new tag in the destination file, the tag has the key of the source
                                   Exiv2::Exifdatum& dest_tag = dest_exifData[i->key()];
                                   //now the tag has also the value of the source
                                   dest_tag.setValue(&(i->value()));
                            }
              }
       } else {
              destimage->setExifData(src_exifData);
       }

       //THROWS Exiv2::Error if the operation fails
       destimage->writeMetadata();