+
+
+} // namespace graphics
+} // namespace lyx
+
+
+namespace {
+
+string const findTargetFormat(string const & from)
+{
+ typedef lyx::graphics::Image::FormatList FormatList;
+ FormatList const formats = lyx::graphics::Image::loadableFormats();
+
+ // There must be a format to load from.
+ BOOST_ASSERT(!formats.empty());
+
+ // First ascertain if we can load directly with no conversion
+ FormatList::const_iterator it = formats.begin();
+ FormatList::const_iterator end = formats.end();
+ for (; it != end; ++it) {
+ if (from == *it)
+ return *it;
+ }
+
+ // So, we have to convert to a loadable format. Can we?
+ it = formats.begin();
+ for (; it != end; ++it) {
+ if (lyx::graphics::Converter::isReachable(from, *it))
+ return *it;
+ else
+ lyxerr[Debug::GRAPHICS]
+ << "Unable to convert from " << from
+ << " to " << *it << std::endl;
+ }
+
+ // Failed! so we have to try to convert it to PPM format
+ // with the standard converter
+ return string("ppm");
+}
+
+} // anon namespace
+
+
+namespace lyx {
+namespace graphics {
+
+void CacheItem::Impl::convertToDisplayFormat()
+{
+ setStatus(Converting);
+
+ // First, check that the file exists!
+ if (!IsFileReadable(filename_)) {
+ if (status_ != ErrorNoFile) {
+ setStatus(ErrorNoFile);
+ lyxerr[Debug::GRAPHICS]
+ << "\tThe file is not readable" << endl;
+ }
+ return;
+ }
+
+ // Make a local copy in case we unzip it
+ string filename;
+ if ((zipped_ = zippedFile(filename_))) {
+ unzipped_filename_ = tempName(string(), filename_);
+ if (unzipped_filename_.empty()) {
+ setStatus(ErrorConverting);
+ lyxerr[Debug::GRAPHICS]
+ << "\tCould not create temporary file." << endl;
+ return;
+ }
+ filename = unzipFile(filename_, unzipped_filename_);
+ } else
+ filename = filename_;
+
+ string const displayed_filename = MakeDisplayPath(filename_);
+ lyxerr[Debug::GRAPHICS] << "[GrahicsCacheItem::convertToDisplayFormat]\n"
+ << "\tAttempting to convert image file: " << filename
+ << "\n\twith displayed filename: " << displayed_filename
+ << endl;
+
+ string from = getExtFromContents(filename);
+ lyxerr[Debug::GRAPHICS]
+ << "\n\tThe file contains " << from << " format data." << endl;
+ string const to = findTargetFormat(from);
+
+ if (from == to) {
+ // No conversion needed!
+ lyxerr[Debug::GRAPHICS] << "\tNo conversion needed (from == to)!" << endl;
+ file_to_load_ = filename;
+ loadImage();
+ return;
+ }
+
+ lyxerr[Debug::GRAPHICS] << "\tConverting it to " << to << " format." << endl;
+ // Take only the filename part of the file, without path or extension.
+ string const temp = ChangeExtension(OnlyFilename(filename), string());
+
+ // Add some stuff to create a uniquely named temporary file.
+ // This file is deleted in loadImage after it is loaded into memory.
+ string const to_file_base = tempName(string(), temp);
+ remove_loaded_file_ = true;
+
+ // Remove the temp file, we only want the name...
+ // FIXME: This is unsafe!
+ unlink(to_file_base);
+
+ // Connect a signal to this->imageConverted and pass this signal to
+ // the graphics converter so that we can load the modified file
+ // on completion of the conversion process.
+ converter_.reset(new Converter(filename, to_file_base, from, to));
+ converter_->connect(boost::bind(&Impl::imageConverted, this, _1));
+ converter_->startConversion();
+}
+
+} // namespace graphics
+} // namespace lyx