C++: Convert base64 string into Image - c++

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.

Related

JPEG header construction

I'm trying to write a JPEG file.
After I acquire the raw image and processing it by DCT, quantization and huffman coding, I would like to save it in the correct format. I couldn't find any libraries that help me to write proper header file, insert the two tables that I used (huffman and quantization), so I started writing everything by myself.
I started with the SOI marker, frame header and so on, but when I try to open the JPEG image with a viewer, it shows nothing, even in the properties. There's no information shown (e.g. the image dimension), even if I insert this info like written in the official documentation.
How can I write a proper JPEG header?
P.S
I use C++ and Visual Studio 2010.
Simple answer - it's complicated.
I would start with libjpeg and use it to just give you the approriate header. You can also use it as a reference for what the internal header structs look like

How can I pass TIFF image data to JUCE (which does not support TIFF)?

I am using learning gui programming using c++ JUCE library. That library have supports for image file format(png, jpg). But I wants to learn how can I use other file format for example tiff.
After google I got libtiff.
My question is what will be the accurate approach for displaying this. Should I need to convert .tiff file into jpeg/png from tiff for doing this.
But I think this will require double processing.
Can anyone explain the raw/native/basic image file format so that I need to convert all images into that type and use it directly.
As I find something in winAPI for dealing with images in which they use image data from file format.
It will be very helpful if someone can let me know the approach for handling images data and displaying it.
Can anyone explain the raw/native/basic image file format so that I need to convert all images into that type and use it directly.
There is no "native" image file format, but RGB comes close (especially if you strip the headers to give just a Width×Height×Channels array of pixel values). You probably wouldn't want to use this for storing everything though as your buffers will be very large. Let your libraries handle storage.
It will be very helpful if someone can let me know the approach for handling images data and displaying it.
There is no "the" approach. C++ itself doesn't say anything about images, and there are loads of ways you can go about working with them. Your design will depend on your functional requirements specification and on what libraries you have available.
I am using learning gui programming using c++ JUCE library. That
library have supports for image file format(png, jpg). But I wants to
learn how can I use other file format for example tiff.
After google I got libtiff.
My question is what will be the accurate approach for displaying this.
Should I need to convert .tiff file into jpeg/png from tiff for doing
this.
But I think this will require double processing.
If you mean using libtiff to convert TIFF-format images to formats that JUCE supports, you're right in saying that this introduces an extra initial processing step. However, as far as you've said, it sounds like any possible performance hit through this will be vastly, wildly and hugely outweighed by the benefit of simplicity and clarity. So I'd just do that.
In order to do something like read *.tiff images and using them in an application build with the JUCE framework, I would suggest to create a new class derived from the base interface ImageFileFormat.
class MyTiffFormat : public ImageFileFormat
{
private:
MyTiffFormat( const MyTiffFormat& );
MyTiffFormat& operator=( const MyTiffFormat& );
public:
MyTiffFormat();
~MyTiffformat();
const String getFormatName();
bool canUnderStand();
Image decodeImage( InputStream& input );
bool writeImageToStream( const Image& source, OuptputStream& dest );
};
Implementing the function "Image decodeImage( InputSTeram& input )" is the point were you need something like libtiff. In the JUCE source tree you will find the implementation for PNG and the other supported formats in the folder: \juce\src\gui\graphics\imaging
More information on extending JUCE features can be found in the JUCE user forum.
Juce works great with pngs, jpgs, and gifs (not animated), and they can be read from file, or even "compiled" with the BinaryBuilder.
For example to load it from compiled c++ with BinaryBuilder.
someImage = ImageFileFormat::loadFrom (AppResources::image_png, AppResources::image_pngSize);
Check out the doxygen docs, they are quite helpful. to compile your images with BinaryBuilder the syntax is:
./BinaryBuilder someFolder otherFolder ClassName

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").

Get dimensions of JPEG in C++

I need to get the image dimensions of a JPEG in C++. I'm looking for either a fairly simple way to do it or a smallish library that provides that functionality. I'm working in C++ on OpenVMS, so any external libraries may have to be adapted to compile on our systems - so please don't post me links to big, closed source libraries!
Has anyone come across anything that might do the trick, or understand the JPEG file format (I think I probably mean the JFIF file format here) to tell me how I might go about rolling my own solution?
You have this C function which may extract the relevant data for you.
This is a C routine but should compile fine with C++.
Pass it a normal FILE pointer (from fopen) to the beginning of a jpeg file and two int pointers to be set with the image height and width.
Or you may find in the Boost library a jpeg class which has the right function (From Adobe Generic Image Library).
jpeg_read_dimensions
boost::gil::jpeg_read_dimensions (const char *filename)
Returns the width and height of the JPEG file at the specified location. Throws std::ios_base::failure if the location does not correspond to a valid JPEG file.
libjpeg is reasonably small, open source and available on OpenVMS. It's probably quicker to install it than to handle JPEG yourself.
Maybe libjpeg?
You should be able to use this jpeg lib with this patch for OpenVMS
No need for full libjpeg library just to get this information (unless you need to do something else with the images). ImageInfo might help you. It is a Java class, but there are ports for other languages, including C++.
As pointed out, Exif might change these information (eg. with orientation setting).
You may want to try GDAL library which serves as an abstraction layer for large number of raster data formats, mostly used in geospatial applications for GIS/RS.
GDAL provides number of APIs, for C, C++ and scripting languages. Of course, it supports JPEG images and its variants like JPEG2000 and more.
Here is a very simple example how to open JPEG image and query its dimensions:
#include <gdal_priv.h>
GDALAllRegister(); // call ones in your application
GDALDataset* ds = (GDALDataset*)GDALOpen("my.jpeg", GA_ReadOnly);
int width = ds->GetRasterXSize();
int height = ds->GetRasterYSize(),
int nbands = ds->GetRasterCount();
Check GDAL API tutorial for more complete example.

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.