3

My project is to batch resize a big number (30000) of small jpeg images (400*300) to 15 different destination sizes (which are smaller than the source).

I first created a multi-threded command line tool based on CImg and libjpg to batch process the images. And is worked as a charm on my 4*2 cores Ubuntu notepad (20 minutes).

Now the client wants a GUI for the tool, with progress bars, estimated time, previews, ... and so on! And want it to work on Windows too. He wants a sort of image manager.

I started porting the pure C++ command line to Qt and its QImage class (QtConcurrent::run for multi-threading), and here the problem : On the same machine it runs 3 times slower on Windows 7 x64 (about 50 minutes)!

So how to use Qt for reading images and CImg for resizeing them? And will it run faster?

Yassine Zaroui
  • 403
  • 3
  • 14

2 Answers2

3

Here is what I have done :

void convert(QStringList files, QString destDir) {
    foreach (QString file, files) {
      CImg<unsigned char> image(file.toStdString().c_str());
      QString destFile = destDir + QFileInfo(file).fileName();
      image.get_resize(200, 200, -100, -100, 1).save_jpeg(destFile.toStdString().c_str(), 70);
  }
}

instread of :

QImage img;
foreach (QString file, files) {
img.load(file);
QString destFile = destDir + QFileInfo(file).fileName();
img.scaled(200, 200, Qt::IgnoreAspectRatio, Qt::FastTransformation).save(destFile, 0, 70);
}

It runs 2 to 3 times faster

Yassine Zaroui
  • 403
  • 3
  • 14
2

If it will run faster is difficult to tell without measuring (though I would guess it is), but it is nonetheless a good idea. Create a interface (API not GUI) for your command line tool, build the gui parts in Qt and call the API of your image resizing backend from it.

This is a good (I would even call it a fundamental principle) design for application development anyway. For such a small project it is sometimes overkill, but since you already have the "backend" part anyways your fine.

Fabio Fracassi
  • 3,791
  • 1
  • 18
  • 17
  • First, As you said, it is a small project and I don't want to manage two different independent projects (API & GUI)! Seceond, the command line is linux only now, I will have to "windowsize" it. And last but not least, I am beginning Qt, I still don't know how to work with external APIs and make it cross-platform. – Yassine Zaroui Jul 12 '11 at 12:33
  • 1
    @yzaroui: just turn your current implementation into a library. Then, write a brand new GUI program, which will utilise its functions (and a set of library functions is an API by definition). – vines Jul 12 '11 at 12:42
  • as @vines says, you do not need to manage them independently, turn them into a library, or just include everything into one project. The separation is purely conceptual. There is nothing special in using neither internal nor external APIs and Qt doesn't change that. – Fabio Fracassi Jul 12 '11 at 12:52