0

I want to convert about 8.5 Million float to uint8_t. I am processing a 4K image and comparing with results from another tool. My results differ in some pixels with the results from the other tools by just one unit. EG, one pixel of my result image is 46 while the pixel of the image obtained by the other tool is 47. So I need to round my value and store it in the uint8_t array. I was doing it with static_cast<uint8_t>("float value") but that take additional 80ms. Hod do I do it?

Pedro Pereira
  • 312
  • 5
  • 21
  • 1
    `static_cast("float value")` is a *truncation*, and there were already many questions about that https://stackoverflow.com/q/78619/995714 https://stackoverflow.com/q/9344709/995714 https://stackoverflow.com/a/429812/995714 https://stackoverflow.com/q/41144668/995714 if you want to round (i.e. `static_cast(float_value + 0.5f)`) then see [a fast method to round a double to a 32-bit int explained](https://stackoverflow.com/q/17035464/995714) – phuclv Mar 02 '18 at 13:17
  • as you're converting a huge number of values, obviously scalar conversion like that is not a good choice. Use SIMD (x86 `_mm_cvtepi32_ps`) or GPGPU – phuclv Mar 02 '18 at 13:30

1 Answers1

0

static_cast("float value") is a truncation, and there were already many questions about that stackoverflow.com/q/78619/995714 stackoverflow.com/q/9344709/995714 stackoverflow.com/a/429812/995714 stackoverflow.com/q/41144668/995714 if you want to round (i.e. static_cast(float_value + 0.5f)) then see a fast method to round a double to a 32-bit int explained

– phuclv

Pedro Pereira
  • 312
  • 5
  • 21