file io - How to write only regularly spaced items from a char buffer to disk in C++ -


How can I write every third item in a four buffer to file quickly in C ++?

I get a three-channel image from my camera, but each channel has the same information (the image is grayscale). To save space, I want to write only one channel in the disc and write it fast, because it is real-time, part of the data collection system.

The C ++ offStream :: List command only writes the infected blocks of binary data, so my current code writes all three channels and runs very slowly:

  char * Data = getDataFromCamera (); Int Datacize = ImageWood * ImageHight * ImageChannel; Std :: offstream output; Output.open (filename, std :: ios :: out | std :: ios :: binary); Output.write (data, datasis);  

I like being able to change the last line like a call:

  int skipSize = imageChannels; Output. Written (data, datasys, skip seiz);  

Where skipSize must be typed only to write every third in the output file. However, I am unable to find any work that does this.

I would love to hear any idea about writing a channel quickly on the disk. Thank you. Assuming your buffer is a 24-bit RGB, and you have 32-bit processors (so that 32-bit processors, Operations on BIT organizations are the most efficient).

For the fastest speed, work with a 12-byte piece at a time. In twelve bytes, we have 4 pixels, such as:

  AAABBBCCCDD  

Which 3 32-bit values ​​are:

  

We want to convert it to ABCD (a 32-bit value).

We can make ABCD by applying a mask, each input and orring.

  ABCD = A000 | 0 BC 00 | In C ++, with a little-endian processor, I think this would be:  
  unsigned integer turn 12grayBytesInto4ColorBytes (unsigned intel buff [3]) {returns (buff [0] and 0x000000 FF) / Masks are reversed due to short-endianes. (Buff [1] and 0x00FFFF00). (Buf [2] in & amp; 0xFF000000); }  

It's faster to do another conversion into another buffer and then dump it to disk instead of going directly to disk.


Comments