By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,376 Members | 1,599 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,376 IT Pros & Developers. It's quick & easy.

Read in image ... get ascii values

P: 175
I want to read in an image file ( bmp, jpeg, ... ) so I can retain it's ascii / binary values, and alter the image. How can I do this in C++ ( I'm using Visual Studio, various versions ).

so basically, I want to make a big matrix of values in an array.

I know this can be done using a hex editor, but in C++ I'm not sure how to get the ascii values. Any help is appreciated.
Sep 4 '08 #1
Share this Question
Share on Google+
10 Replies

Expert 100+
P: 469
I don't quite understand this question. You're trying to read a file into an array, so can you just make an array of chars and read into that? The binary and ASCII values would be retained, right? I don't see what you need to do with ASCII values.
Sep 4 '08 #2

P: 175
The reason I want to get the actual values is because in my code I want to be able to change the color of the image (by changing the ascii values) and output that image with my changes.
Sep 4 '08 #3

Expert 100+
P: 469
chars store the ASCII values of the characters you put into them. If you read the file into an array of chars, then you will have the ASCII values of all the characters in the file, and you can work with them just as if they were ints.
Sep 4 '08 #4

P: 175
I understand the concept ... what I'm asking is HOW to read in an image file so that I CAN get the ascii values.

when I try to read it in by line or by character, it only gives one total ascii digit, ONE, there should be thousands ... so my problem is reading it IN, not working with the values, I have no problem with that
Sep 5 '08 #5

Expert 100+
P: 849
Can you show us the code you use to read the file in? We're (unfortunately) neither clairvoyant nor telepathic.
Sep 5 '08 #6

P: 175
sure, essentially ( off the top of my head ), the code I'm using is ...

Expand|Select|Wrap|Line Numbers
  2. ...
  3. #include <fstream>
  4. ...
  7. readFile()
  8. {
  9.     ifstream in;
  10. "C:\Documents and Settings\ ... \ image.jpg" );
  12.     char data[80];
  14.     while( in.good() )
  15.     {
  16.         in.getline( data, 80 );
  17.         cout << atoi( data ) << endl;
  18.     }
  20.     in.close();
  21. }
that's basically the idea ... however, I have a feeling this isn't the way to read in images, so that's why I first asked the question ... I appreciate the responses.
Sep 5 '08 #7

Expert 100+
P: 469
The code you posted won't work because:
1: Every line after the first line gets written over the previous line. You readline the line into data, and then the next iteration of the loop, you readline right over it again. It's probably better to read into a vector or dynamic array than a static array, because image files can be huge. You could push_back all the bytes into a char vector with the ifstream::get function.
2: The way you're outputting the ASCII values is wrong; the atoi function takes a c-string such as "8132" and converts it to the int value 8132. Instead, loop through all the characters in data, and for each one, static_cast it to an int and output it.
Hope this helps.
Sep 5 '08 #8

Expert Mod 5K+
P: 8,916
The should be no mention of ASCII when reading data from an image file. ASCII is purely one of serveral ways of interpreting binary data as text.

With an image file there is no text just binary data representing coloured pixels.

You MUST open the file in binary or you WILL get corruption of the data.

You should not attempt to read the file as though it were text (i.e. by using getline), you should read it as the thing it is, binary data.
Sep 5 '08 #9

P: 175
I'm not reading into an array and overwriting it ... I just typed that code to give an idea of what I was doing, but thank you for that.

Banfa, that was what I was looking for, whenever I read the file, it came out as either garbage or nothing. Thank you.
Sep 5 '08 #10

P: 27
If u want to open and do sum opeartion in a image file , better open it in a Binary modde ... open ("image.jpg", b) ..

Try this ...
Sep 10 '08 #11

Post your reply

Sign in to post your reply or Sign up for a free account.