Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
1.00/5 (5 votes)
See more:
Hi Every one

I want to develop a c program which converts an image file into text file, and when a text file is supplied that will convert into image from which the text file had generated.

Any idea and helping material will be appreciated.


Regards
TanzeelurRehman
Posted
Comments
ZurdoDev 28-Sep-12 14:27pm    
What would the text file look like? Do you literally mean you want to convert images into ascii images? What are you doing?
Sergey Alexandrovich Kryukov 28-Sep-12 15:02pm    
I answered based on guesswork. Maybe I wasn't right, not 100% sure, please see...
--SA
TanzeelurRehman 28-Sep-12 15:09pm    
Thanks for your reply, i just want to convert image into ascii text file and the convert to image when needed by another module, first i want to convert image file into ascii text file, in another module i want to convert that ascii text file into image file

Regards
TanzeelurRehman
ZurdoDev 28-Sep-12 15:12pm    
As in ascii art? http://www.ascii-art-generator.org/
pasztorpisti 28-Sep-12 14:47pm    
ASCII art? :-) But that is usually lossy...

So you wanna generate ascii art? Here is how to do that: First you have to read in the picture file and convert it if necessary so that you have the width and height of the picture and the color (usually RGB) for each pixel in a 2 dimensional array (the dimensions are the width and height of the picture). It might sound easy to "just read in a picture file" but it is very tricky even for the simplest formats like .bmp and .tiff or .tga if you want to handle every cases. What you think, why are there programs out there that specialize on picture viewing? Because if you want to handle all picture formats without the help of some libraries you will spend your remaining life (at least a few years) writing libraries that handle picture formats before you can create your ascii generator prog. :-) For this simple reason I recommend you to handle only a simple format that doesn't emply compression and other tricks, for example the windows BMP format. Even in BMP files there are 1, 2, 4, 8, 16, 24, and 32 bit color depth representations that totally differ in binary, and it might contain RLE compression (and a lot of other things if we supported various other headers: http://en.wikipedia.org/wiki/BMP_file_format[^]). For first handle just BMP files without RLE (I never supported RLE compression in my progs) and restrict the color depth to 32 bits to make your work easier. So support just pure 32 bit bmp files. Search for bmp tutorials using google, I wont write the 1000th bmp tutorial. Its not just reading in bytes from a file and using them as color values, basically every picture file format contains a header at the beginning of the file and different kind of other garbage here and there... So use google.

Lets say you successfully allocated your memory and read in the color values of each pixels into memory and by specifying a row (Y) and column (X) value you can pick the color (RGB) of a pixel from your picture. To make ascii art you have to be able to convert every pixel into grayscale (the Red, Green, and Blue - aka RGB - components of it into intesity), there are a lot of methods to do that (http://en.wikipedia.org/wiki/Grayscale[^]). The color of a pixel consists of 3 values (3 channels - at least in RGB color space) and you will convert it to a single value (1 channel) - intensity. This is already a lossy conversion. The R G and B values in bmp files are bytes, this means that their range is [0..255]. Your resulting intensity is also a byte with range [0..255]. The linked wiki article mentions the method I always used to convert RGB to a single grayscale intensity value: INTENSITY = R*0.3 + G*0.6 + B*0.1
With byte magic in C its something like the following:
C++
// note: 75+155+25 == 255, the values I used here are inaccurate, I just guessed
// use the wiki page to get more accurate distribution
unsigned char intensity = (unsigned char)(((int)R*75 + (int)G*155 + (int)B*25) / 255);

Lets say you converted each pixel of your picture into a byte (grayscale intensity value). The next step is to associate an ascii character with each intesity value from 0 to 255. The problem is that you have only a limited number of ascii characters, much less than 256 that you need so first you have to downsample your "256 color" grascale bitmap to use much less intensity values (this is a lossy conversion like when you convert a 32 bit color bitmap into a 1 or 4 or 16 or whatever color bitmap). During downsampling you can achieve much better quality by applying dithering but that goes far beyond what fits into this mini beginner tutorial so I will use a much simpler method. Instead of 256 values in my grayscale image I want only 8, so I divide each intensity value by 32. After this each intesity value will be in the range [0..7].

After this I associate all the intensity values with an ascii character:
0: space
1: .
2: :
3: i
4: o
5: V
6: M
7: @

In plain C this conversion looks like this:
C++
char ascii_pixel = " .:ioVM@"[intensity_value];


Notes: You can use more than 8 intensity values since you have more ascii characters, I used 8 just for the sake of simplicity. You could use 16 by dividing the intesity values just by 16 or you could use 32 by dividing intesity by 8. How to pick characters for your intensity values? Each ascii character itself is also a small image formed from black and white pixels. The ratio of the black and white pixels and the way they are distributed on the small image of the ascii character can define an intensity when you look at it from a distance like you doo it with ascii art. Don't bother with finding out your own set of ascii characters for example if you wanna use 16 ascii characters for your conversion, you should rather search with google to find 16 right ascii values for your job.

Since the conversion is quite lossy you can not reverse it fully. If you understood the above process then the reversing process should be straightforward. Note that the method I described is quite a simple one, you can involve dithering and different image processing algorithms (like edge detection) to make the conversion of different kind of images better. If you are showing ascii art on the monitor with some character video mode then you can use colors with your ascii characters. Ascii art can be used to play video in character mode! For other image to ascii art conversion methods you can check out this: http://en.wikipedia.org/wiki/ASCII_art[^]

Other note: Converting a big (lets say 1024x768) picture with this method can lead to a huge file, for this reason you might want to shrink your picture before conversion so that its width is around 80-100 pixels because if you view a text file not much more characters fit on the screen even with huge monitors. There are a lot of image rescale methods (http://en.wikipedia.org/wiki/Image_scaling[^]) but discussing them is unfortunately out of the scope of this tutorial. You can simulate rescaling by giving only small pictures as an input to your program or by rescaling it with a good photoshop like software. :-)

Other useful references:
http://www.cprogramming.com/[^]
http://tipsandtricks.runicsoft.com/Cpp/BitmapTutorial.html[^]
 
Share this answer
 
v5
Comments
Espen Harlinn 29-Sep-12 18:50pm    
5'ed! :-D
pasztorpisti 29-Sep-12 18:55pm    
Thank you! :-)
TanzeelurRehman 30-Sep-12 4:10am    
Hmmm that is what i want. Thank you @pasztorpisti
pasztorpisti 30-Sep-12 4:39am    
You are welcome!
This is not "convert", this is OCR — Optical Character Recognition:
http://en.wikipedia.org/wiki/Optical_character_recognition[^].

The is the list of well-known products, some are open-source:
http://en.wikipedia.org/wiki/Comparison_of_optical_character_recognition_software[^].

You can try to find some other library, but I must warn you: everything from the open source I tried so far did not satisfy me:
http://bit.ly/Sip6Do[^].

Good luck,
—SA
 
Share this answer
 
Comments
pasztorpisti 28-Sep-12 15:14pm    
5ed, you must be right - after reading the question I haven't associated it with OCR at all...
Sergey Alexandrovich Kryukov 28-Sep-12 16:09pm    
Thank you very much.
What the... ah, 1-voter's attack, I see...
--SA
fjdiewornncalwe 28-Sep-12 20:58pm    
+5. I'm glad you understood what he wanted. Until I read your answer, OCR didn't even register with me.
Sergey Alexandrovich Kryukov 28-Sep-12 21:16pm    
Well, hopefully. We did not see if anything is formally accepted (hey, Tanzeelur, how about the green button? :-)
Thank you, Marcus.
--SA
TanzeelurRehman 29-Sep-12 0:18am    
Thank you for your reply

I am going to elaborate it in step by step way what i have to develop

1. A c program which will ask for an image file.
2. when the image file is provided, the program will convert it into text file something like the conversion in

http://www.ascii-art-generator.org/

3. Another module in the program should convert the converted text file in step 1 and step 2 into image file again. it doesn't matter if the picture quality loss if the text file is converted into image again.

So i need help. How can i achieve this? what should be the logic?

Regards
TanzeelurRehman
Reading all, I didn't understand your question.
What exactly do you want?
1. do you want to read text from picture(I mean the view)
2. or do you want to convert your binary file into text file

if it is 1 then as SA said

but if it is two then read base64 encoding
 
Share this answer
 
v2
Comments
TanzeelurRehman 29-Sep-12 0:16am    
Thank you for your reply

I am going to elaborate it in step by step way what i have to develop

1. A c program which will ask for an image file.
2. when the image file is provided, the program will convert it into text file something like the conversion in

http://www.ascii-art-generator.org/

3. Another module in the program should convert the converted text file in step 1 and step 2 into image file again.

So i need help. How can i achieve this? what should be the logic?

Regards
TanzeelurRehman
Mohibur Rashid 29-Sep-12 2:14am    
Now this one make sense.......... but cant help you much with your question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900