0

I have a raw-12 file , I want to take input of 12-bit at a time and then convert it to in 8-bit in c++.
This is how i am taking input.

int main()
{   
     string s;
     fstream f ("pagan.raw12", std::ios::binary|std::ios::in);
     if (!f)
         std::cout<<"\n oops!! file does not exit";
     else
         std::cout<<"\n ready to go!!"

     std::cin.ignore();

     getline(cin,s);

     std::cout<<s;

     f.close();

     return 0;
}
  • 1. It's unlikely that anyone here will know what a 'raw-12' file is. 2. getline won't work on binary data that, definitionally, not newline-separated. – bmargulies Sep 02 '18 at 20:41
  • I guess you would read 3 bytes (24 bits) at a time and split that into 2 samples of 12 bits each and store each one in its own unsigned short (16-bit). – Mark Setchell Sep 02 '18 at 20:45
  • Example of reading specific number of bytes here... https://stackoverflow.com/a/2974659/2836621 – Mark Setchell Sep 02 '18 at 20:55
  • https://wiki.apertus.org/index.php/RAW12 – Retired Ninja Sep 02 '18 at 22:18
  • Do you have the size of the file in bytes? Do you know the dimensions of the image it represents? Does the image need de-bayering or is that what you want to do yourself? Do you have a sample file to test with? – Mark Setchell Sep 03 '18 at 09:50
  • yes @MarkSetchell size of file : 18 megabytes and dimensions are 4096*3072. and bebayering is alo required . yes i have the sample file. please guide This is problem statement : https://lab.apertus.org/T872 –  Sep 03 '18 at 10:08
  • See if you can work out how to define the input and output image width and height. Then see if you can allocate space for the output image which you will need to pass to LodePNG. Then you need to decide if you are going to a) read the entire 18MB into memory and twiddle bits with pointers, or b) read 3 bytes at a time and save memory but maybe go a bit slower. – Mark Setchell Sep 03 '18 at 10:30
  • What we want is to take 12 bit and then convert it into 8 bit by bit manipulation and store them in some array or vector , then we will do our extraction and be dayering as given in task –  Sep 03 '18 at 10:52
  • I don't understand how to split 24 bits into two , can u explain –  Sep 03 '18 at 10:58

1 Answers1

0

You can read 3 bytes from your stream like this:

unsigned char buf[3];

f.read(reinterpret_cast<char*>(buf), 3);

Then you will have 3 bytes as shown in red:

enter image description here

You want the two new 12-bit samples shown in blue, labelled n0 and n1:

enter image description here

But you want to reduce them to 8-bit, so you need to discard the lowest 4-bits show in green, labelled d0 and d1. That will leave you with the two new 8-bit samples shown in magenta and labelled a0 and a1:

a0 =  b[0];
a1 = (b[1] & 0xf) << 4) | ((b[2] >> 4) & 0xf);
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432