What is the correct way to convert a YCBCR_420_888 image to RAW10 format?

43 Views Asked by At

I'm having trouble converting the image correctly. Is that even possible? There is no answer to this question on the Internet. The difference between image formats is catastrophically large. If RAW10 is to be taken from the camera sensor, then maybe conversion is not possible? How do I correctly calculate the size of a RAW10 image?

I calculate it this way:

    width * height

Not working:

    width * height * 5 / 2

Not working:

    width * height * sizeof(uint16_t)

Here's my non-working function.

    void ConvertYCbCr420888ToRaw10(const android_ycbcr & srcYCbCr,
      std::vector < uint8_t > & dst_raw, int width,
      int height) {
      uint8_t * yPlane = static_cast < uint8_t * > (srcYCbCr.y);
      uint8_t * cbPlane = static_cast < uint8_t * > (srcYCbCr.cb);
      uint8_t * crPlane = static_cast < uint8_t * > (srcYCbCr.cr);
      for (int y = 0; y < height; ++y) {
        for (int x = 0; x < width; ++x) {
          dst_raw[y * width + x] = yPlane[y * width + x];
        }
      }

      for (int y = 0; y < height / 2; ++y) {
        for (int x = 0; x < width / 2; ++x) {
          uint8_t cb = cbPlane[y * width / 2 + x];
          uint8_t cr = crPlane[y * width / 2 + x];

          uint16_t cb10 = (cb << 2) | ((cb >> 6) & 0x03);
          uint16_t cr10 = (cr << 2) | ((cr >> 6) & 0x03);

          dst_raw[height * width + y * width / 2 + x] = (cb10 >> 8) & 0xFF;
          dst_raw[height * width + y * width / 2 + x + 1] = cb10 & 0xFF;
          dst_raw[height * width + y * width / 2 + width / 2 + x] = (cr10 >> 8) & 0xFF;
          dst_raw[height * width + y * width / 2 + width / 2 + x + 1] = cr10 & 0xFF;
        }
      }
    }

For example, the image canvas size is 4000x3000. Then this function fills 70% of the image with lilac color. The other 30% is empty. The image is distorted with stripes on the diagonal.

0

There are 0 best solutions below