Storing binary data in a PNG file using QImage::setText() - c++

I need to read/store binary data in the metadata of a PNG image. Specifically, it will be gzipped data which has been serialized by Google's protobuf library.
I'm using the QImage library of Qt5 to load and store the images, so it would be extremely convenient if I could find a way to use some Qt library to add this binary data to the QImage I'm already using.
I see that the QImage class has a setText method which appears to do exactly what I want, except for one caveat: It takes a QString as an argument and not a QByteArray, and the QString constructor mangles my binary data.
How can I force QString to preserve my binary data, both when storing and loading? If that's not possible, is there some other way in Qt5 to add metadata to a PNG image?

You could try to encode the data as Base64 before converting to QString, and then decode when reading back. I do understand that this significantly reduces the benefits of your gzip compression, but at least you could try to see if that helps.

Related

C++: Convert base64 string into Image

is there some way to convert a base64 string into an image; in Visual C++? I got an image as a result of getting the image from an url, encoded as a base64 so, I need to get it back.
I'm really lost in this matter; I'm using Visual C++ 2010.
So far I've been digging about GDI+ but I don't know if it is correct.
Thanks.
Convert into bytes, convert bytes into bitmap, (optional) bitblt bitmap onto window.
How do I base64 encode (decode) in C?
How can I create an Image in GDI+ from a Base64-Encoded string in C++?
You need to know what type of image the resulting bytes are. Then you need to find an algorithm that can understand that, or just save the bytes as a file type (if it is already built with header info and everything).
If it's just a bitmap (like a MFC bitmap), you'll need a way to convert that into an image, if you intend to save it. If you just intend to display it and it's already a bitmap, then just use the GDI methods.
Boost is a great set of libraries when it comes C++ and it's documentation can be found here.
So we have a encode and decode supported by Boost.
A more detailed implementation of Boost Base64 encode-decode can be found here.
Another useful library is of Microsoft CPPRESTSDK, also known as Casablanca project. Under this they have defined a utility::conversion namespace which deals with all different type of encoding-decoding which one can come across while developing an API. Among this from_base64 (const utility::string_t &str), which can be used to decode the given base64 string to a byte array.

Can we load, display and manipulate image's matrix without using any library in c++?

is it possible to do changes to image's matrix without using any library in c++? to load and display image as well?
Sure. Grab a copy of the specification for whatever image format you're interested and write the read/write functions yourself.
Note that to write display functionality without an external library you'll likely need to run your code in kernel mode to get to the frame buffer memory, but that can certainly be done.
Not that you'd necessarily want to do it that way...
Like any typical file, an image file is simply made up of bytes; there is nothing special about an image file.
In my opinion, the most difficult part of reading/writing image files without the use of a library is understanding the file format. Once you understand the format, all you need to do is define appropriate data structures and read the image data into them (for more advanced formats you may have to do some extra work e.g. decompression).
The simplest image format to work with would have to be PPM. It's a pretty bad format but it's nice and easy to read in and write back to a file.
http://netpbm.sourceforge.net/doc/ppm.html
Apart from that, bitmaps are also pretty simple to work with. Like Drew said, just download a copy of the specification and work from there.
As for displaying images, I think you're best off using a library or framework unless you want to see how it's done for the sake of learning.

Boost::GIL How to save image as JPEG or PNG into char*?

So I see samples all around on saving into file. But I wonder if it is possible to save into char* or string instead of file - so to say keep it in memory?
There doesn't seem to be anything to facilitate this in boost itself. All I/O seems to be based on supplying filenames.
However, there seems to be an extension here called io_new that has streams based I/O.
See documentation here for an example (search for "Reading And Writing In-Memory Buffers").

C++ Importing and Renaming/Resaving an Image

Greetings all,
I am currently a rising Sophomore (CS major), and this summer, I'm trying to teach myself C++ (my school codes mainly in Java).
I have read many guides on C++ and gotten to the part with ofstream, saving and editing .txt files.
Now, I am interested in simply importing an image (jpeg, bitmap, not really important) and renaming the aforementioned image.
I have googled, asked around but to no avail.
Is this process possible without the download of external libraries (I dled CImg)?
Any hints or tips on how to expedite my goal would be much appreciated
Renaming an image is typically about the same as renaming any other file.
If you want to do more than that, you can also change the data in the Title field of the IPTC metadata. This does not require JPEG decoding, or anything like that -- you need to know the file format well enough to be able to find the IPTC metadata, and study the IPTC format well enough to find the Title field, but that's about all. Exactly how you'll get to the IPTC metadata will vary -- navigating a TIFF (for one example) takes a fair amount of code all by itself.
When you say "renaming the aforementioned image," do you mean changing metadata in the image file, or just changing the file name? If you are referring to metadata, then you need to either understand the file format or use a library that understands the file format. It's going to be different for each type of image file. If you basically just want to copy a file, you can either stream the contents from one file stream to another, or use a file system API.
std::ifstream infs("input.txt", std::ios::binary);
std::ofstream outfs("output.txt", std::ios::binary);
outfs << insfs.rdbuf();
An example of a file system API is CopyFile on Win32.
It's possible without libraries - you just need the image specs and 'C', the question is why?
Targa or bmp are probably the easiest, it's just a header and the image data as a binary block of values.
Gif, jpeg and png are more complex - the data is compressed

Decode JPEG to obtain uncompressed data

I want to decode JPEG files and obtain uncompressed decoded output in BMP/RGB format.I am using GNU/Linux, and C/C++.
I had a look at libjpeg, but there seemed not to be any good documentation available.
So my questions are:
Where is documentation on libjpeg?
Can you suggest other C-based jpeg-decompression libraries?
The documentation for libjpeg comes with the source-code. Since you haven't found it yet:
Download the source-code archive and open the file libjpeg.doc. It's a plain ASCII file, not a word document, so better open it in notepad or another ASCII editor.
There are some other .doc files as well. Most of them aren't that interesting though.
Unfortunately I cannot recommend any other library besides libjpeg. I tried a couple of alternatives, but Libjpeg always won. Is pretty easy to work with once you have the basics done. Also it's the most complete and most stable jpeg library out there.
MagickWand is the C API for ImageMagick:
http://imagemagick.org/script/magick-wand.php
I have not used it, but the documentation looks quite extensive.
You should check out Qt's QImage. It has a pretty easy interface that makes this task really easy. Setup is pretty simple for every platform.
If Qt is overkill, you can try Magick++ http://www.imagemagick.org/Magick++/. It supports similar operations and is also well suited for that sort of task. The last time I used it, I struggled a bit with dependencies for it on Windows, but don't recall much trouble on Linux.
For Magick++'s Image class, the function you probably want is getConstPixels.
I have code that you can copy ( or just use as a reference ) for loading a jpeg image using the libjpeg library.
You can browse the code here: http://code.google.com/p/kgui/source/browse/trunk/kguiimage.cpp
Just look for the function LoadJPGImage.
The code is setup to handle c++ binding of my DataHandle class to it for loading the image, that way the image can be a file or data already in memory or whatever.
A slightly out of the box solution is to acquire a copy of the netpbm tools, which transform images from pretty much any format to any other format via one of several very simple intermediate formats. They work well from the shell, and are most often used in pipes to read some arbitrary image, perform an operation on it, and write it out to some other format.
The pbm formats can be as simple as a plain ASCII header followed by the RGB data in ASCII or binary. They are intended to be simple enough to use without required a library to implement.
JPEG is supported in netpbm by read and write filters that are implemented on top of libjpeg.