-1

I'm using QImage class from Qt to display the picture on screen. For some reason I need to display even not fully loaded images (e.g. when some data blocks are absent).

I would like to see something like this in result:

partially loaded image

Standard image viewer for Windows can show me such broken images, but I can’t achieve same behavior with QImage. Image not displayed at all if broken. Is there a way to display a partially loaded image by QImage? Maybe I should use other Qt-related classes for that purpose?

αλεχολυτ
  • 4,792
  • 1
  • 35
  • 71
  • What image file format? – Scheff's Cat Sep 18 '21 at 07:30
  • @Scheff'sCat PNG in particular – αλεχολυτ Sep 18 '21 at 07:31
  • I once used [libpng](http://www.libpng.org/pub/png/libpng.html) to implement an image loader for my own image classes. I don't have experience concerning loading broken images. (I was fully satisfied when it did the job for non-corrupted files.) However, this might provide you the full control over what can be achieved at all. Once you have loaded an image into memory it's not so difficult to [wrap it into a QImage](https://stackoverflow.com/a/45552097/7478597) (and a [teaser](https://stackoverflow.com/a/48136780/7478597)). ;-) – Scheff's Cat Sep 18 '21 at 08:18
  • Googling a bit, I found something which reminded me to the existence of [QImageReader](https://doc.qt.io/qt-5/qimagereader.html#details) which is intended to provide more control on image file loading. However, this seems to turn out as a dead end: _If any error occurs when reading the image, read() will return a null QImage._ – Scheff's Cat Sep 18 '21 at 08:22
  • Where is your code ? what do you mean by "but I can archive same behavior with QImage" ? – Mohammad Kanan Sep 18 '21 at 22:16
  • 1
    @MohammadKanan I was assuming that _archive_ was a typo (or the result of that stupid Smartphone input correction). I read this as _achieve_. – Scheff's Cat Sep 19 '21 at 10:15
  • Do you mean you want to see loading png by delay? or do you want to set one image and see a broken image, which means that you want to create a broken Image? – Parisa.H.R Oct 05 '21 at 19:13
  • @Parisa.H.R I want to see as much image parts as I can for existing chunks of data – αλεχολυτ Oct 05 '21 at 19:21
  • Why `QImage`? It is necessary? If not - you can use your own `QWidget` with `paintEvent()` and draw with `QPainter` picture from `QByteArray` with `QPixmap`. This is do the job for me when I worked with IP-cams and it display broken images. – Shtol Krakov Oct 06 '21 at 15:17
  • Qt in general is high level like that. An incorrect image equal to invalid state of object (bad data, broken header, etc.) and thus it's replaced by null image (which is a correct state of object, an image of zero size). It's not just QImage, this ideology is red-lined through entirety of framework. Even exceptions are intercepted on their way through event loop stack, you can't unwind stack unmolested. – Swift - Friday Pie Oct 11 '21 at 17:20
  • @αλεχολυτ there is caveat with that. If jpeg, ,png or other compressed image is really broken (or intentionally tampered with) , your code may attempt to show "too much" and go out of bound, if left unchecked. There was a known exploit with `openjpeg` libraries where certain formats would expand into infinite or near-infinite sequences of data. – Swift - Friday Pie Oct 11 '21 at 17:27

1 Answers1

3

QImage is probably too high level for this. If you do not want to go at the level of individual libraries for each format (e.g. libpng), you should consider using CImg. It is a small header only c++ library to read and process images, which use the low level libraries available to read images. From a loaded CImg you should be able to get the data into a QPixmap or QImage to display it.

Laurent Jospin
  • 604
  • 5
  • 9