saving 12 bits data

Hello everyone
Happy new year!
Here is my problem:
I have 2D images where each pixel data is stored in an unsigned short (16bits) but the information in the image never exceed 12 bits. For compression purpose, I would like to save in file only 12bits for each pixel of my images.
How to shift the writing file pointer of only 12 bits?
Have you experience the same problem? any idea on how to do so?
Thanks

Alf
Hello,

i m not sure, i have understood your question properly, but still i have tried to write a code based on my understanding......

make sure to set structure alignment to 1 byte in your compiler settings....

this is just a sample, make adjustments.....
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include<stdio.h>
#include<fstream.h>

// STRUCTURE ALIGNMENT 1-byte set..... ///
struct two_chunk
{
	unsigned char chunk1_p1;
	unsigned char chunk1_p2:4;  // using only 4 bits out of byte.
	unsigned char chunk2_p1:4;  // using only 4 bits out of byte.
	unsigned char chunk2_p2;
};

//to be specific....
typedef unsigned short UINT16;


// function to write binary file.....
void function_to_write(UINT16 chunk1,UINT16 chunk2,fstream fd)
{
	struct two_chunk chunks;

	chunks.chunk1_p1 = chunk1 & 0x0FF;
	chunks.chunk1_p2 = ((chunk1 & 0xF00)>>8);
	chunks.chunk2_p1 = chunk2 & 0x00F;
	chunks.chunk2_p2 = ((chunk2 & 0xFF0)>>4);
	// now write chunks.....
	fd.write((char *)&chunks,sizeof(chunks));
}


// main function....
int main()
{

	UINT16 your_data_array[12] = {0xAAA,0xBBB,0xAAA,0xBBB,0xAAA,0xBBB,0xAAA,0xBBB,0xAAA,0xBBB,0xAAA,0xBBB};   // say you have 12 pixel information(12 bits each) stored in array.
	
	// open binary file for writing....
	fstream binary_file("c:\\test.dat",ios::out|ios::binary|ios::app);
	// write data loop
	for(int i =0;i<=11;i=i+2) // increment i with two, we are writing two chunks at a time....
	{
		// write two chunk at a time.....
		function_to_write(your_data_array[i],your_data_array[i+1],binary_file); // send two chunk at a time....
	}
	//close binary file....
	binary_file.close();
   
	return 0;
}


Hope this will help you...
maxchirag: Line 27 alone makes me want to puke.
1
2
3
4
5
6
7
8
9
10
11
//I rather oppose using UINT16, but I'll leave it for the sake of simplifying
//the example.
void function_to_write(UINT16 chunk1,UINT16 chunk2,fstream fd){
	unsigned char buf[]={
		chunk1&0xFF,
		//we know chunk1 is no bigger than 0xFFF, so no need to AND.
		(chunk1>>8)|(chunk2&0x0F),
		chunk2>>4
	};
	fd.write(buf,3);
}

It should be checked that the size of the bitmap is even. For 2D bitmaps, the only instance when this is not true is a size of 1x1.
looks good, gives wrong output........

li'l modification

(chunk1>>8)|((chunk2&0x0F)<<4)
Last edited on
No, that's wrong, too (sorta).
((chunk1>>8)<<4)|(chunk2&0x0F),
I think it's preferable that the byte's bits are adjacent to each other.
I would prefer to keep 4 bits of chunk1 as LSB and chunk2's 4 bits as MSB...

you can keep it as u like......
Last edited on
Topic archived. No new replies allowed.