How do I create a QIcon from binary data? - c++

I have a custom file format which stores some images. I load these images into memory during the operation of my program. The images are loaded in binary format (i.e. the same way they would appear on disk). I know what format the images are in (pretty much all jpeg). The problem is that I dont know the width/height, but I want to display the images without writing them to some temp file on disk. Anyone know how to do this?

You can get the size of a qIcon using actual size
http://qt-project.org/doc/qt-5.0/qicon.html#actualSize
You shouldn't have to write anything to disk, you just need to read your binary into something like QIcon, so that you can use QTs methods to get the width and height of the image. You just need to get the data into one of QTs data structures.
Check out QImage and QPixmap, I think they have methods for reading in binary. Or you can check this SO post here: QImage from unsigned char buffer (jpg format)

Related

Writing image data to the disk as fast as possible

I am writing an application that generates a huge amount of images. Each frame is 1280x800 pixels large and has 1 byte per pixel for color information (greyscale). Each of the frames must be written to disk.
Currently I simply dump the raw pixel data to a binary file on the disk. The file can then be viewed with a special viewer I also created.
This is a very unsatisfactory solution, since the images can't be viewed/processed directly. They always have to run through my custom viewer/converter.
Is there an image format I could use to write my images to disk that:
Is fast to be written (no compression etc.)
Does not increase the final file's size much
Supports dumping my raw pixel buffer in there (no alignemnt changes etc.)
Can be read by common applications (Windows Explorer, Paint, Photoshop etc.)
I already tried to use .png, but the file generation takes much too long due to the compression.
Have a look at the binary Portable GrayMap (P5) format. It consists of an extremely simple header followed by raw image data (without any alignment requirements), and is widely supported by image viewers.
Both bmp and tiff can be used to save raw data. Bmp has the oddity of having image upside down, unless height is negative. And tiff has plenty of encoding options. It should be anyway feasible to reverse engineer the format to be used as a template, where the image data is copy pasted. So no need to use a library: just a header, image data and an optional footer concatenated.

Qt reading many images optimization - how to only read the size?

The title sums this one up. If I'm loading ~200 images of various size. How can I load just the header so I can know the size of each image?
Currently I find it takes a lot of cpu/memory and IO to load them all in to memory just for the size (I'm trying to generate an atlas from them).
QImage doesn't seem to have a way to do this. QImageReader sounded like it was what I wanted, yet this still seems to just go ahead and read the whole image, so not really sure what its purpose is. Is there another class or some way to use either of the class I've mentioned to only grab the image size from header?
How can I load just the header so I can know the size of each image?
Apparently it looks like you have assumed that image file header(first few bytes of) contains the size of the image. This does not hold true(at least not for all image format type). I checked it for few of formats(PNG).
Currently I find it takes a lot of cpu/memory and IO to load them all
in to memory just for the size
As you have mentioned that you are trying to load around ~200 image at one time just to find the size. This design does not looks good and we should try to decompose our problem into the smaller one. So here the efficient approach might be to open one file and find the size store into some data structure and close the file. If there is other part of your program which needs that ~200 image should be loaded into the memory then we should try to think on how can we avoid it.
QImage doesn't seem to have a way to do this?
It does not have as there seem to be no portable/consistent way to do it for all type of image format. However if you are aware about any file format which contains the header you may write small helper function which can open the file and read the header and find the size. But this helper function would be very specific to a particular type of image format and we may need to write different logic to read the header(all image formats have different header size and information).

How can I read image metadata from binary blobs?

I have some images (in PNG and JPG format), stored as blobs in the database. I am retrieving them with a query and would like to take action by reading the metadata without writing the image to disk.
I am looking for the file type and image width.
You should be abe to convert the data to a ColdFusion Image type using the ImageNew function as documented here (set the source to be the variable you pulled out of the query).
Once you have the image, you can use the ImageInfo function to retrieve image properties.
This will give you the width. CF won't tell you the original file format, though--it might be easiest to look at magic numbers for that. Wikipedia gives a good summary of what those are and what the values for jpeg and png are: http://en.wikipedia.org/wiki/Magic_number_(programming)

Read image and access bytes in boost::gil

Is it possible to:
read an image given by just a filename (not knowing the image format) to a 2d matrix rgb uncompressed form (e.g. read an JPG to a 2d array)
access the bytes of that image, copy them, change them... (e.g. inverse the colors, I need a pointer to the image bytes, setters/getters won't do )
save those bytes to any given image format (e.g. save the inversed image to PNG)
Is it possible with boost::gil ? Maybe there is a different library more appropriate for such a task?
Sample code would be highly appreciated.
Yes, you can do all that in boost::gil.
What you should know though, is that boost::gil is only a universal interface and doesn't handle reading/writing images all by itself. You still need to use a second library, e.g. libpng..
Yes, yes and yes.
There are functions that enable you to read and write JPEG, TIFF and PNG images: see here.
For the second bullet, it is what just GIL is meant to do. You can manipulate images using its facilities (click here).

How do you place EXIF tags into a JPG, having the raw jpeg buffer in C++?

I am having a bit of a problem.
I get a RAW char* buffer from a camera and I need to add this tags before I can save it to disk. Writing the file to disk and reading it back again is not an option, as this will happen thousands of times.
The buffer data I receive from the camera does not contain any EXIF information, apart from the Width, Height and Pixels per Inch.
Any ideas? (C++)
Look at this PDF, on page 20 you have a diagram showing you were to place or modify your exif information. What is the difference with a file on disk ?
Does the JPEG buffer of your camera contain an EXIF section already ?
What's the difference? Why would doing it to a file on the disk be any different from doing it in memory?
Just do whatever it is you do after you read the file from the disk..
As far as I know EXIF data in JPEG is continuous subpart of file.
So
prepare EXIF data in memory
write part of JPEG file upto EXIF
write prepared EXIF
write rest of JPEG file
You might want to take a look into Exiv2 library. I know it can work on files but I suppose it also has functions to work on memory buffers.