3

I have a flattened (1D) U32 encoded image array which has r, b, and b 8-bit channel values encoded into the first 24 bits of each U32. I would like to expand this array into and array of U8s that each store a separate r, g, or b value (0-255). The issue is that I need this to happen really fast (hundreds of times per second on an old computer) and the method I created is slow.

I am a novice at labview, so I am not exactly sure what a faster way to do this is.

I have successfully accomplished this by creating a U8 array, iterating through each index of the U32 Image array and assigning the corresponding 3 rgb values to the appropriate index in the U8 array using a shift-register. I attempted to use the In Place Element Structure (which would presumably not require copying data in between loops like shift), but I did not know how to make it work inside the loop and when I tried to return the last array from the loop, only the last element was modified.

Here is the first, working method I described above:

dropbox VI image link (update i have reputation now)

In c/c++, it would be pretty simple (something like this):


uint8_t* convert_img(uint32_t img[640*480]){
  uint8_t *img_u8 = new uint8_t[640*480*3];
  for (int i=0; i<640*480; ++i){
    img_u8[i*3] = img[i] & 0xff; // r
    img_u8[i*3 + 1] = (img[i] >> 8) & 0xff; // g
    img_u8[i*3 + 2] = (img[i] >> 16) & 0xff; // b
  }
  return img_u8;
}

The working labview example above only runs at 20 Hz! I think this is super slow for such a simple operation. Does anyone with more experience have a suggestion of how I can make this happen quickly with labview code?

Alec Graves
  • 152
  • 8

2 Answers2

4

I would do it like this: U32 to U8

enter image description here

The steps are:

  • Flatten To String - endian chooses which order the bytes are in
  • Unflatten From String - into a 1D U8 array
  • Decimate 1D Array - creates 4 1D arrays
  • Reshape Array - turns into 640x480 arrays

Should be plenty fast enough.

Pirate X
  • 3,023
  • 5
  • 33
  • 60
  • Thank you! This is much faster. This method was even faster than Flatten to 1D u32 Array > Type cast to uint8 array. – Alec Graves Aug 25 '19 at 01:50
2

I expect that the fastest option would be using the Split Numbers primitive to break the U32s into U8s, but you would need to actually test:

enter image description here

Also note that testing performance is not as easy as you might think, although if you're looking at the overall rate, you're probably fine with the basic testing.

Yair
  • 2,266
  • 12
  • 12