C++ Convert Binary File to Image - c++

I'm trying to make a C++ console app that can convert binary(mp3) files to images. How can I read every binary character in the file, convert to hex, and then save it as an image. Here is what I want but in C++

You might find this tutorial helpful:
http://www.cplusplus.com/doc/tutorial/files/
(Scroll down to the section binary files)
Also, let me share my standard recommended links for people asking for aid on basic c++:
Full scale tutorial on c++
C++ Language Reference (including STL)
ANSI C Language reference for all those pesky C stuff that C++ keeps using

Create an image with an area big enough to fit the data in.
For each byte in the source file, set a pixel. You could do this a number of ways - monochrome, or take bytes in threes and write them as red, green and blue for a 24-bit colour image.
Save the image to disk, e.g. in PNG format using libpng.
If you want a more specific answer, you'll need to ask a more specific question.

Related

How can I display images on the output screen using ASCII code in C++?

Note---- I use BORLAND TURBO C++
I know that Turbo C++ is two decades old, and it does not support the latest C++ standard either. But since our school still uses it to teach students C++, I can't use any other compiler.
I want to display an image in Turbo C++ using ASCII text. I have already converted the image to ASCII code using an online converter. What should I do now to display it in my program?
I was thinking of something along these lines-
Copy the ASCII code to a txt file.
Read the text file using the getline function.
Display it on the output screen.
I thought it would work, but the difficulty arises when I copy the ASCII image to notepad. It destroys all the formatting of the ASCII image, and the image just becomes a rectangular block of text.
Is there any other way to achieve this? I do not want to use any other libraries if I can help it. I use the graphics.h library in Turbo C++. Also, I am a bit new to C++ (started learning it last year), so I would be grateful if you keep the answers as simple as possible.
It seems that I was copying the text into notepad the wrong way. The get-text option worked fine. I was able to copy the ascii image into notepad and display it in my program.
Well, the first option is to find another online converter which gives you a raw, copy-able, output. In that case you can use your approach.
The second way is to build an ascii converter yourself. The way I think the online tools convert an image into ascii characters is the following:
Build a table with relative brightness, so:
space is 0 brightness,
8 = 60% brightness
0 is 80% birghtness
# is 90% brightness
and go on like this.
Divide the image into blocks (for example 10*10 pixels)
Determine the average brightness of such a block
Find an ascii-character with approximately the same relative brightness.
print image.
this is more complicated, and if you are new you could probably try your approach first.

pixel conversion to raw bits (bit-stream)

I want to read the contents of every pixel in an image i have and convert it to a bit-stream (raw bits) or contain it in a 2-D array . Which would be the best place to start looking for such a conversion?
Specifics of the image : Standard test image called lena.bmp
size : 256 x 256
Bit depth of pixel : 8
Also I would like to know the importance of the number of bits per pixel with regards to this question since packing and unpacking will also be incorporated .
CImg is a nice simple, lightweight C++ library which can load and save a number of image formats (including BMP).
It's a single header file, so there's no need to compile or link the library. Just include the header, and you're good to go.
You should investigate OpenCV: a cross-platform computer vision library. It provides a C++ API as well as a C API, and it supports many image formats including bmp.
In the C++ interface, cv::Mat is the type that represents a 2D image. A simple application that loads and displays an image can be found here.
To learn how to access the matrix elements (pixels) you can check these threads:
OpenCV get pixel information from Mat image
Pixel access in OpenCV 2.2
Common Matrix Operations in OpenCV
OpenCV’s C++ interface offers a short introduction to cv::Mat. There has been many threads on Stackoverflow regarding OpenCV, there's a lot of valuable content around and you can benefit a lot by using the search box.
This page has a collection of books/tutorials/install guides focused on OpenCV, but this the newest official tutorial.

Making Gradient Images Quickly with C++

I'm currently working with Python to create images of gradients. However, for my uses I'm afraid that Python may just be too slow. I know that Python can be extended with C++ with relative ease.
So what are some quick ways to produce images of gradients in C++?
Writing a bitmap manually is quite easy. For a Windows Bitmap you need a 54 byte header and then an array of colour values. (.bmp file specs)
So, create a file, write the header, supply the pixel array. For a gradient generating the colour values should be fairly simple.
.pgm/.ppm files are even easier as they have much simpler headers.

Simple image analysis

I am looking for a method, software or library for simple image analysis.
The input image will be a white-colored background, and some random small black dots on it.
I need to generate a .txt file that represents these dots' coordinates. That is, if there are three dots in the image the output will be a text file that includes somehow a representation of three coordinates, (x1,y1), (x2,y2), and (x3,y3).
I have searched the web for hours and didn't find something appropriate, all I found was complex programs for image processing.
I've been told that it's easy to write code for this mission in MATLAB, but I'm unfamiliar with MATLAB.
Can this be done easily with C++, Java or C#?
Any good libraries?
It is quite simple in any language. Depending on the form of your input, you probably need to go over all of it (assuming it is a simple matrix - simply have two nested loops, one for the x coordinate and one for the y coordinate), whenever you encounter a black dot - simply output the current indexes which would be the x and y coordinates for the dot.
As to libraries, anything other than something to decode your input to the form of such a matrix (e.g. a JPEG decoder) would be overkill.
I don't think you would need image processing libraries for this kind of problem (somebody correct me if I am wrong) since these libraries may focus on image manipulation and not recognition. What you will need is a knowledge of the image format that you are supporting (how are they stored, how are they interpreted, etc) and basic C file system functions.
For example, if you are expecting a JPG file format you will simply calculate the padding for each scanline and reach each scan line one by one, and each pixel in the line one by one. You'd have to use two counters, one for the row and one for the column. If the pixel is simply not white, then you have your coordinate
This is something which should be very easy for you to do without any external software; something like
for(y in [0..height]) {
for(x in [0..width]) {
if(pixels[y][x].color == BLACK)
print("(%d, %d)", x, y);
}
}
would work.
The bitmap file format is quite easy to read.
http://en.wikipedia.org/wiki/BMP_file_format
You could just stream the bytes into an array using this info. I've written a few BMP readers; it is a trivial matter.
Also, although I cannot vouch for its ease of use as I've never used it before, I've heard that EasyBMP works fine too.
CImg library shold help you. From CImg FAQ:
1.1. What is the CImg Library ?
The CImg Library is an open-source C++ toolkit for image processing.
It mainly consists in a (big) single header file CImg.h providing a
set of C++ classes and functions that can be used in your own sources,
to load/save, manage/process and display generic images. It's actually
a very simple and pleasant toolkit for coding image processing stuffs
in C++ : Just include the header file CImg.h, and you are ready to
handle images in your C++ programs.

c++ read image pixels

I want a c++ Code to read every pixel from an image file. and i want to save the pixels like:
r[]
g[]
b[]
does somebody know how to do this?
The answer depends on the format of the image file. Is it a format which contains raw RGB data (such as uncompressed TGA)? Is it a YUV image? Is it a compressed image such as JPEG or PNG?
There are already plenty of C++ libraries out there which can read a wide variety of image file formats, and then provide the pixel-level access you require. Take a look at Adobe's GIL, or CImg for example.
There are many freely available libraries for reading different image file formats. Since you're using C++ you might want to look at Adobe's Generic Image Library (GIL) or even OpenCV.
This will sort you out, very easy to use and 'low level' image library:
http://easybmp.sourceforge.net/
Two libraries that I've used that jump to mind are:
ImageMagick
libGD
These libraries can handle a wide variety of image formats, depending on what you need.