Qt QImage how to display truncated png file? - c++

I am trying to read an image into a QImage from a partially downloaded file, to show progress as it downloads. With jpg images, I can display the content that has downloaded so far, but with png files I get the following error:
libpng error: Read Error
and then trying to use the QImage results in failure with:
Image is a null image
If I save the partially downloaded file, I can open it with every program I've tried, showing though sometimes logging a warning, like with gimp:
* (file-png:25459): WARNING **: Error loading PNG file: Read Error
Is there a way to get the same behavior with png's as with jpg's where the partially downloaded file is loaded and displayed as just the top half of the image? Is there a better way to do this with QImage? I have access to the image data in RAM as well, plus the length of how much has been received, if that helps.

Related

C++ displaying an image with SDL2 with different formats than .bmp

I am trying to display an image in SDL2 and the only guide I found was a guide about how to display a .bmp image. What I would like to do is displaying an image or setting a temporary image as a background. For example if I press LEFT_KEY the program should proceed to the next image. Any relevant guides or any examples you can share? It is not needed to set the image as a background (stretched), my goal is to display an image and being able to scroll through other images by pressing a key.
Use SDL_Image:
https://www.libsdl.org/projects/SDL_image/
Example:
Loading PNGs with SDL_image

Quil: Don't even open the window

I'm trying to use quil to procedurally generate images. I think it's really great, but I'm not trying to display the image at all. I'm just trying to save the image to a file.
Is it possible to not actually open the window and just save the file directly?

Unable to get CImage to work in Visual C++ 2005 (MFC)

I am using an example from MSDN, but I get "'CImage' : undeclared identifier"?
CImage myimage;
// load existing image
myimage.Load("image.bmp");
// save an image in BMP format
myimage.Save("c:\image1.bmp");
// save an image in BMP format
myimage.Save("c:\image2",ImageFormatBMP);
// save an image in JPEG format
myimage.Save("c:\image3.jpg");
// save an image in BMP format, even though jpg file extension is used
myimage.Save("c:\image4.jpg",ImageFormatBMP);
The documentation for CImage shows that it is defined in atlimage.h. You must #include that in your source. Also note the warning that you must include afxstr.h first to avoid errors.

QLabel doesn't show pixmap

I can't set a png or jpeg image to a QLabel on Windows 7 64.
The image is loaded fine. Indeed QPixmap::load returns true, which means that the loading worked, and QPixmap::width() returns the correct size of the image.
However the pixmap is not displayed when set to a QLabel. The label is visible, the text is displayed, but there is no image.
Even if I don't think it is related to missing plugins (the image is loaded fine) I have tried to add the image formats directory in the working directory like wrote in here
http://www.qtcentre.org/faq.php?faq=qt_images

Saving QPixmap as JPEG results in vertical lines

A user installed our application to a server PC, shared the install directory as network drive Z: and now opens the app from different client PCs all over his shop. Everything works fine except for a naggling bug on one of the PCs:
On the Problem PC, the app can load .jpg files for inventory items just fine and show the corresponding QPixmap in a QLabel. When the inventory item he is currently viewing does not yet have an image assigned to it, he can open a file dialog, choose an image and display that correctly. However, saving the (new/changed) QPixmap as a .jpg stores it to disc as a series of colored vertical lines on a black background:
Saving is done via QPixmap::save( const QString & fileName, ...) with the file name set programmatically to "<some_id>.jpg" to designate the desired file format. Returns true, but the resulting file looks like modern art.
However, saving images works fine on the server and the other clients.
Both the server and the Problem PC run Windows XP at an identical patch level.
Process Explorer shows identical DLLs for the app process on server and Problem PC except for the Problem PC using dnsapi.dll, which the server doesn't.
Process Explorer also shows that on both server and client the Qt DLL used to deal with JPEGs is
\Device\LanmanRedirector\<server>\<app>\plugins\imageformats\qjpeg4.dll, and a drive-wide search on the Problem PC for qjpeg*.dll came up empty, so the app should use the same JPEG handling code on both computers.
Any suggestions?
(EDIT: added OS and patch status to problem description.)
EDIT: Solution: on the problem machine we had a 16 bpp colour depth. Setting that to 32 bpp solved our problem instantly. I also replaced
_pm.save( sDestFileName )
with
_pm.toImage().convertToFormat( QImage::Format_RGB32 ).save( sDestFileName )
so that we won't run into this on every old client PC running less than 32 bpp.
Here are a few things to try to isolate the problem:
Copy qjpeg4.dll to the client's working directory
Is the image corrupted when saving as PNG or GIF?
Convert your data to a QImage and work from that.
Consider the byte format (I usually use ARGB32 because it is int-aligned)
To test where the problem occurs, you might try converting the image to JPG format then viewing it in a debugging window.
The function below is from an OSS program I wrote. You might try using it then displaying the contents of the QImage referred to in the second parameter:
//! Saves image with JPEG compression in given quality (0 - 100, Qt's scale)
//! #param[in] in The lossless input image
//! #param[out] out The image to save to using JPEG compression
//! #param quality The quality level (0 - 100, 0 the most compressed)
//! #return The size of the saved image
quint32 Window::imageSaveLossy(QImage &in, QImage &out, quint8 quality)
{
quint32 retval;
QByteArray ba;
QBuffer buffer(&ba);
buffer.open(QIODevice::WriteOnly);
QImage temp(in.size(), QImage::Format_ARGB32);
temp.fill(QColor(Qt::white).rgb());
QPainter painter(&temp);
painter.drawImage(0, 0, in);
temp.save(&buffer, "JPG", quality);
out.loadFromData(ba, "JPG");
retval = (quint32)ba.size();
buffer.close();
return retval;
}