Solved

how to read .raw image file in C++

Posted on 2007-12-02
9
13,833 Views
Last Modified: 2013-12-16
Hello,

I am trying to open a .raw image file stored on my hard drive in a C++ program to manipulate it.
After opening the file, want to save its pixel information in a 2-D array. The image is a 256 X 256 with 8 bits per pixel and no header bytes.

I would like the array to be dynamic so that i am not limited to only a 256 X 256 image, but the 8-bit representation will be unchanged.

I figured using fstream in binary mode would work, but it doesn't seem too or maybe i am using it wrong.  

Can someone please show me how I can open the .raw file and then load it into a dynamic array and the needed libraries.  

If it makes any difference, I am using a window xp machine running Visual Studio .Net 2005 as my c++ editor and compiler

Thank you so much
Kevin Harrilal
0
Comment
Question by:krazieintent
  • 5
  • 4
9 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
could you post the code you have so far?
0
 
LVL 3

Author Comment

by:krazieintent
Comment Utility
sure here is what i have so far.

The code asks the user for the location of the file using the function openFile.

it checks to see if the file location is valid using checkFile, and then proceeds to open the file with the command

inputFile.open(filename.c_str(),ios_base::binary)

where inputFile is my connection to fstream, and filename is the string of the file path entered by the user.

i then create a 2d dynamic array and begin reading and storing the contents of the file with the readfile function.

My method for either opening or reading or both doesnt seem to work.

Thank you so much for your help in advance.
Kevin Harrilal
#include <iostream>

#include <fstream>

#include <string>
 

using namespace std;
 

void openFile(ifstream& inputFile);

//pre condition: user is ready to input filename.

//post condition: inputFile is connected to user input filename. This //opens file.
 

bool checkFile(string filename);

//pre condition: filename is corresponding to the name of a file on the //local machine

//post condition: return true if file does not exist, returns false if file does exist, does not open file
 

int ** readFile(ifstream& inputFile, int& size);

//pre condition: inputFile has been connected to a filename and size has //been initialized to 0.

//post condition: returns a pointer to a dynamic array of ints //containing the data in inputFile.
 
 

int main()

{

	ifstream inFile;

	openFile(inFile);

	int size(0);

	int **p;

	p=readFile(inFile,size);
 

//test to see data in p

	cout<<p[3];

}
 

void openFile(ifstream& inputFile)

{ 

	//local variable

	string filename;
 

	//user input

	cout<<"Please enter the file path for the .raw image : ";

	cin>>filename;

	cout<<endl<<endl;
 

	//continue to ask for file if file does not exist

	while (checkFile(filename))

	{

		cout<<"This File does not exist, please enter another filename: ";

		cin>>filename;

		cout<<endl<<endl;

	}
 

	inputFile.open(filename.c_str(),ios_base::binary);

}
 

bool checkFile(string filename)

{

	//open file, if it opens successfull it exists, if it fails it doesnt exist

	ifstream testFile;

	testFile.open(filename.c_str(),ios_base::binary );
 

	if(testFile)

	{

		return false;

	}

	else

	{
 

		return true;

	}
 

	testFile.close();

}
 

int **readFile(ifstream& inputFile, int& size)

{

	//local variables

	int temp;

	size=256;
 

	//dynamic 2d array of size rows x size columns

	int ** p = new int*[size];

	

	for(int i=0; i < size; i++)

		p[i] = new int[size];
 
 

	//read from file into temp student

	for(int j=0;j<size; j++)

	{

		for (int i=0;inputFile>>temp && i<size; i++)

			p[i][j]=temp;

	}	
 

	return p;

}

Open in new window

0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
Comment Utility
ok, as far you are reading 8 bit values, you should have an array of 'unsigned char' instead of 'int'.
also, an STL vector will give you more flexibility than a pure C array.

so you can have a function like:
       bool readFile(ifstream &inputFile, vector<vector<unsigned char>> &rawdata);

instead of this:
      int **p
you can declare as:
      vector<vector<unsigned char>> rawdata;

it is a nested vector collection, so you will be able to access it as rawdata[i][j];
now, your readFile function will require some changes to accomodate this arrangement:


#include "math.h"
 

bool readFile(ifstream &inputFile, vector<vector<unsigned char>> &rawdata);

{

   unsigned char temp;
 

 // get length of file:

  inputFile.seekg (0, ios::end);

  int length = inputFile.tellg();

  inputFile.seekg (0, ios::beg);
 

  // determine the number of rows and cols

  int size = (int)sqrt((double)length);

  // check if it is a perfect square

  if (size*size != length)

       return false;
 

  // resize the vector

  rawdata.resize(size);

  for (int i=0; i<size; i++)

       rawdata[i].resize(size);
 

   //read from file into vector

   for(int j=0;j<size; j++)

   {

      for (int i=0; i<size; i++)

      {

          inputFile.get(temp);

          p[j][i] = temp;

      }

   }	

   return true; 

}

Open in new window

0
 
LVL 3

Author Comment

by:krazieintent
Comment Utility
Thank you so much for your help.

I just have a couple of questions on your implementation. Can you explain to me why I should use char instead of int. Does it have to do with char being store as a 16-bit value in c++?

If this is true, using the get command, since it reads one character at a time, will it also read 16 bits at a time from the input file, which would be the equivalent of two pixels? Therefore every temp store would store two pixels and not one?

As far as my opening the file in binary mode, is that part correct?


Thank you so much for your help.


0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
you mentioned your image has  8 bits per pixel.
char has 8 bits (or unsigned char)
int has 32 bits.
 
0
 
LVL 3

Author Comment

by:krazieintent
Comment Utility
my other question is, if using unsigned chars, the pixel values will be output as ascii characters correct?
How could I extract the pixel values (0 to 255) from the character? would i use the math atoi() function or similar?

or could i just do another int static cast on the output for example cout<< (int) rawdata[0][3] because i dont think the atoi function operates on unsigned chars.

Also when running your code, I get the error of trying to convert unsigned char to char& in the inputFile.get(temp) call.

so i tried static casting to inputFile((char)temp&);

but the compiler tells me there is a syntax error ')'

any ideas?
0
 
LVL 3

Author Comment

by:krazieintent
Comment Utility
oh I though char was a 16-bit value not 8.

So to better understand, if i were using a 16-bit per pixel picture, i would make a vector of strings of length 2?

length 3 for 24 bit and so on?

Thank you so much
0
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
in C++ language, a char value is treated as integer for most situation:

char a, b, c;
a = 5;
b = 4;
c = a*b;  // result 20

but some displaying function can present them as ascii char, you can make a static cast with easy in those cases:

cout << (int)c;

>>Also when running your code, I get the error of trying to convert unsigned char to char& in
you can use char instead, but depending on compiler option it will be signed or unsigned. This will affect you if you want to make some math operations.

>>So to better understand, if i were using a 16-bit per pixel picture, i would make a vector of strings of length 2?
for 16 bit images, I suggest to use 'short int' instead
for 24 bit images, I suggest to keep in char, but with double capacity of course, because you can access RGB independent values with easy.
0
 
LVL 3

Author Comment

by:krazieintent
Comment Utility
Thank you so much for your help!  
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
One of Google's most recent algorithm changes affecting local searches is entitled "The Pigeon Update." This update has dramatically enhanced search inquires for the keyword "Yelp." Google searches with the word "Yelp" included will now yield Yelp a…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now