Logo Search packages:      
Sourcecode: kallery version File versions  Download package

bool KalleryFunctions::convertImages ( QProgressDialog *  progress  ) 

Converts, copies each image and creates the thumbnails if it was requested. This is the one place where we use the ImageMagick (the other is when we get the supported file list).

Parameters:
progress pointer to the progress dialog

Definition at line 460 of file kalleryfunctions.cpp.

References cancelPressed, OptionType::Convert, OptionType::convertImages, OptionType::CopyOnly, OptionType::copyright, OptionType::descList, OptionType::descSetting, OptionType::fullFormat, OptionType::fullHeight, OptionType::fullWidth, OptionType::galleryFile, OptionType::GenerateThumbnails, OptionType::imageDestDir, OptionType::imageList, OptionType::insertCopyright, OptionType::keepRatio, OptionType::LoadImageInfo, mainWidget, makeAbsolutePath(), OptionType::onlyLarger, KalleryGlobals::options, OptionType::printToStdOut, OptionType::quality, readImageInfo(), KalleryGlobals::ref(), OptionType::resize, slotProgressCancelled(), OptionType::thumbFormat, OptionType::thumbHeight, OptionType::thumbList, OptionType::thumbSetting, and OptionType::thumbWidth.

{
  QString fullName;
  QString thumbName;
  QString fileFormat;
  QString destDir;
  ExceptionInfo exception;
  Image *source;
  Image *thumbnail;
  ImageInfo *source_info;
  ImageInfo *thumbnail_info;
  OptionType options = KalleryGlobals::ref()->options;

  kdDebug() << "Starting the image conversion." << endl;

  bool printToStdOut = KalleryGlobals::ref()->options.printToStdOut;
  cancelPressed = false;
  connect(progress,SIGNAL(cancelled()),this,SLOT(slotProgressCancelled()));

  
  if (KalleryGlobals::ref()->options.descSetting == OptionType::LoadImageInfo)
      options.descList.clear();
  
  GetExceptionInfo(&exception);

  for (uint i = 0; i < options.imageList.count(); i++)
  {
    if (cancelPressed)
    {
      if (KMessageBox::questionYesNo(mainWidget,i18n("Do you really want to quit?"),
                                     i18n("Quit"), KGuiItem(i18n("&Quit"), BarIconSet("exit")),KGuiItem(i18n("&Don't quit"), BarIconSet("cancel"))) == KMessageBox::Yes)
      {
        return false;
      }
      cancelPressed = false;
    }
    progress->setProgress(i+1);
    progress->setLabelText(i18n("Processing file: [%1]").arg(QFileInfo(options.imageList[i]).fileName()));

    if (QFileInfo(options.imageList[i]).exists())
    {
      source_info = CloneImageInfo((ImageInfo *) 0);
      strcpy(source_info->filename,options.imageList[i].latin1());
      if (!SetImageInfo(source_info, MagickTrue,&exception))
      {
        MagickWarning(exception.severity,exception.reason,exception.description);
        KMessageBox::error(mainWidget,i18n("[%1  %2]\nSeverity %3\nSkipping to next image...") \
                                              .arg(exception.reason).arg(exception.description).arg(exception.severity),
                                              i18n("ImageMagick error"));
        DestroyImageInfo(source_info);
        continue;
       }

      kdDebug() << "source_info->filename = " <<  source_info->filename << endl;
      source  = ReadImage(source_info,&exception);

      if (source == 0)
      {
        MagickWarning(exception.severity,exception.reason,exception.description);
        KMessageBox::error(mainWidget,i18n("[%1  %2]\nSeverity %3\nSkipping to next image...") \
                                              .arg(exception.reason).arg(exception.description).arg(exception.severity),
                                              i18n("ImageMagick error"));
        DestroyImageInfo(source_info);
        continue;
      }
            
      kapp->processEvents(QEventLoop::ExcludeSocketNotifiers);
      if (options.thumbSetting == OptionType::GenerateThumbnails)
      {
        thumbnail = CloneImage(source,0,0, MagickFalse,&exception);
        thumbnail_info = CloneImageInfo(source_info);
        if (thumbnail == 0)
        {
          MagickWarning(exception.severity,exception.reason,exception.description);
          KMessageBox::error(mainWidget,i18n("[%1  %2]\nSeverity %3\nSkipping to next image...") \
                                              .arg(exception.reason).arg(exception.description).arg(exception.severity),
                                              i18n("ImageMagick error"));
          DestroyImageInfo(thumbnail_info);
          DestroyImageInfo(source_info);
          continue;
        }
        fileFormat = QString("%1x%2").arg(options.thumbWidth).arg(options.thumbHeight);
        TransformImage(&thumbnail,"",fileFormat.latin1());
        strcpy(thumbnail->magick,options.thumbFormat.upper().latin1());

        QString thumbName = options.thumbList[i].latin1();
        destDir = QFileInfo(thumbName).dirPath();
        makeAbsolutePath(QFileInfo(options.galleryFile).dirPath(),destDir);
        thumbName = destDir  + QFileInfo(thumbName).fileName();

        if (printToStdOut)
          printf("%s\n",thumbName.latin1());
        strcpy(thumbnail->filename,thumbName);
        if (WriteImage(thumbnail_info,thumbnail) == 0)
        {
          if (KMessageBox::questionYesNo(mainWidget,i18n("%1.  [%2]\nSeverity %3\n\nThe thumbnail was not written!\nDo you want to continue with gallery generation?") \
                                               .arg(thumbnail->exception.reason). \
                                               arg(thumbnail->exception.description). \
                                               arg(thumbnail->exception.severity),
                                               i18n("ImageMagick error")) == KMessageBox::No)
          {
            DestroyImageInfo(thumbnail_info);
            DestroyImage(thumbnail);
            return false;
          }
        }
        DestroyImageInfo(thumbnail_info);
        DestroyImage(thumbnail);
      } // End the thumbnail generation
      kapp->processEvents(QEventLoop::ExcludeSocketNotifiers);
      if (options.convertImages == OptionType::Convert)
      {
        fullName = options.imageDestDir+QFileInfo(options.imageList[i]).baseName()+"."+options.fullFormat;
        strcpy(source->magick,options.fullFormat.upper().latin1());
        if (options.resize)
        {
          fileFormat = QString("%1x%2").arg(options.fullWidth).arg(options.fullHeight);
          if (!options.keepRatio)
            fileFormat += "!";
          if (options.onlyLarger)
            fileFormat += ">";
          TransformImage(&source,"",fileFormat);   
          SetImageAttribute(source, "EXIF:ExifImageWidth", QString("%1").arg(source->columns).latin1());       
          SetImageAttribute(source, "EXIF:ExifImageLength", QString("%1").arg(source->rows).latin1());       
        }
        if (options.insertCopyright)
        {                    
          DrawInfo *drawThis;
          TypeMetric metrics;

          CloneString(&source_info->font,"helvetica");
          source_info->pointsize = 14;

          drawThis = CloneDrawInfo(source_info,0);
          GetExceptionInfo(&exception);
          QueryColorDatabase("white",&drawThis->fill,&exception);
          CloneString(&drawThis->text,(const char *)options.copyright.utf8());
//           GetExceptionInfo(&exception);
          GetTypeMetrics(source,drawThis,&metrics);
          CloneString(&drawThis->primitive,(const char *)QString("text %1,%2 \""+options.copyright+"\"").arg(source->columns-metrics.width-10).arg(source->rows-5).utf8());
          DrawImage(source,drawThis);
          DestroyDrawInfo(drawThis);
        } //end of the copyrigth text addition
        destDir = QFileInfo(fullName).dirPath();
        makeAbsolutePath(QFileInfo(options.galleryFile).dirPath(),destDir);
        fullName = destDir  + QFileInfo(fullName).fileName();
        //Replace the name, so it will point to the generated image
        // when we save the "_generated" project file.
        source_info->quality = options.quality;
        options.imageList[i] = fullName;

        if (printToStdOut)
          printf("%s\n",fullName.latin1());
        strcpy(source->filename,fullName.latin1());
        if (WriteImage(source_info,source) == 0)
        {
          if (KMessageBox::questionYesNo(mainWidget,i18n("%1.  [%2]\nSeverity %3\n\nNew image was not written!\nDo you want to continue with gallery generation?") \
                                               .arg(source->exception.reason). \
                                               arg(source->exception.description). \
                                               arg(source->exception.severity),
                                               i18n("ImageMagick error")) == KMessageBox::No)
          {
            DestroyImageInfo(source_info);
            DestroyImage(source);
            return false;
          }
        }
      } //end of the image convertion
      else
      {
        destDir = options.imageDestDir;
        makeAbsolutePath(QFileInfo(options.galleryFile).dirPath(),destDir);
        if ((options.convertImages == OptionType::CopyOnly) &&
           (destDir != QFileInfo(options.imageList[i]).dirPath()+"/"))
        {
          KIO::Job * job = KIO::file_copy(options.imageList[i],destDir+QFileInfo(options.imageList[i]).fileName(),-1,true,false,false);
          connect( job, SIGNAL( result( KIO::Job * ) ), mainWidget, SLOT( slotResult( KIO::Job * ) ) );
          kapp->ref();
          // Replace the name, so it will point to the generated image
          // when we save the "_generated" project file.
          options.imageList[i] = destDir+QFileInfo(options.imageList[i]).fileName();

          if (printToStdOut)
            printf("%s\n",options.imageList[i].latin1());
        }
      } //end of the image copy
      if (KalleryGlobals::ref()->options.descSetting == OptionType::LoadImageInfo)
        options.descList.append(readImageInfo(source, options.imageList[i]));
      DestroyImageInfo(source_info);
      DestroyImage(source);
    } //end of  "if the file exists"
  } //for

  if (KalleryGlobals::ref()->options.descSetting == OptionType::LoadImageInfo)
  {
    KalleryGlobals::ref()->options.descList = options.descList;
  }
  kdDebug() << "Image conversion ended successfully. " <<  endl;

  return true;
}


Generated by  Doxygen 1.6.0   Back to index