[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

how to read .raw image file in C++

Posted on 2007-12-02
9
Medium Priority
?
14,793 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
ID: 20392141
could you post the code you have so far?
0
 
LVL 3

Author Comment

by:krazieintent
ID: 20392446
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 2000 total points
ID: 20392556
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
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 
LVL 3

Author Comment

by:krazieintent
ID: 20392752
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20392764
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
ID: 20392832
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
ID: 20392840
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
ID: 20392893
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
ID: 20393097
Thank you so much for your help!  
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.
Suggested Courses

834 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