reading a binary image

Hello People

I am trying to read a binary image. It stores the pixel values in two byte signed integers. so they are ranging from -32768 to 32767. When i run the program, these are the results for a line in an image:


original values             what i get from my program

-32768  			-32768  
-32768  			-32768  
-32768  			-32768  
-32768  			-32768  
-32768  			-32768  
-32768  			-32768  
-32768  			-32768  
275         -----error---       -32749  
274  			        274  
268  			        268  
270  			        270  
270  			        270  
271  			        271  
273  			        273  


so as you can see, the numbers are wrong after some dark pixels. Another problem is later on in the same line, some pixel values are again wrong. like this one:


original values         what i get from my program

265			265
263			263
265			265
259			259
257			257
255   -----error---     511
252			252
244			244
231			231
226			226
226			226
224			224
226			226
232			232
235			235
236			236
228			228
203			203
212			212
240			240
249			249
266   -----error---     10
267			267
260			260
249   -----error---     505
243			243
241			241
249			249



I use ifstream open to open the image. Then seekg to go to the begining of a line. And this code to read the pixel value:

1
2
3
4
5
6
7
8
9
10
11
12
template <typename T>
T ReadPV(ifstream& s, ptrdiff_t size)
{
	T ret = 0;
	for(int i = 0; i < size; ++i)
	{
		ret <<= 8;
		ret |= (unsigned char)s.get();
	}

	return ret;
}


and i use it like this:

1
2
3
pixelValue = ReadPV<int>(inputImage,2);

//second parameter is 2 because each pixel is stored as two byte signed integers 


Any idea what can be the problem??

Thanks
Kenter
Last edited on
I have two questions.

1) How are your the files written? If two-byte integers are written like this:
1
2
int16_t x = 259;
outFile.write((const char*)&x, sizeof(x));

and you are working on little-endian architecture (most likely you are), then you assemble it in incorrect order (you first read the least-significant byte, but treat it as most-significant one).

Also it is weird that you get any negative values if you only read 2 bytes into (normally) 4-byte integer - the sign is stored in the most significant bit which is never written to and stays zero, signifying that it's a positive number.

2) Are you sure you open your file in binary mode?

So, to sum up. In order to help you, we need to know:
- how the file is written
- what is your architecture
- some other obscure parts of your code :)

PS: use int16_t for two-byte integers

I dont know how the file is written. But i found out that its format is little endian. I am working on X86-Linux system. so my sytem should be also little endian. i hope this assumption is true.

i changed the code to
 
int16_t pixelValue = ReadPV<int16_t>(inputImage,2);

nothing changed.

i think i should change the order of bytes while reading. how can i do this?

if you need more of my code, i can give it just let me know which part..

thanks.

i solved the problem :). There was a confusion with little and big endian.

Thanks for your help and cheers
Topic archived. No new replies allowed.