Rgb565 to rgb888 error in the Nicla Vision

I’m running into a problem converting raw rgb565 into rgb888. I’m getting psychedelic looking images:

I followed the advice from this forum post, but I have not been able to fix the problem. Has anyone else delt with this before? How does OpenMV transfer the raw camera bytes into jpg format?

void convertRGB565To888(const uint8_t* rgbData, File outFile) { // need to abrastc height and width
  
  //Write the BMP header
  for(int i = 0; i < 54; i++) {
    char ch = pgm_read_byte(&bmp_header[i]);
    outFile.write((uint8_t*)&ch,1);
  }
  
  for(int i = 0; i < 240; i++) {
    for(int j = 0; j < 320; j++) {
      int bytePosition = (i * 320 + j) * 2; // each pixel is 2 bytes in RGB565
      char VH = rgbData[bytePosition];
      char VL = rgbData[bytePosition + 1]; //low byte

       // RGB565 to RGB888 Conversion
      byte b = (((VH & 0x1F) * 527) + 23) >> 6;
      //b = 0;
      byte r = ((((VL >> 3) & 0x1F) * 527) + 23) >> 6; 
      //r = 0;
      byte g = ((((((VH & 0xF0) >> 5) | ((VL & 0x0F) << 3)) & 0x3F) * 259) + 33) >> 6;
      //g = 0;
      
      //Write image data to file
      outFile.write(b);
      outFile.write(g);
      outFile.write(r);     
    }
  outFile.flush();
  outFile.close();
  Serial.println("Image converted to RGB888 and written to file.");
  }
}

...

  // WHERE THE FUNC IS CALLED;
  uint8_t* frameData = fb.getBuffer();

  // convert 565 to 888
  File rgb888File = SD.open("rgb888.bmp", FILE_WRITE);
  if (rgb888File) {
    convertRGB565To888(frameData, rgb888File);
    rgb888File.flush();
  } else {
    Serial.println("Failed to open rgb888_image.bmp for writing.");
    return;
  }

Hi, we don’t support the Nicla when programmed with Arduino C code.

As for your issue, that typically happens when things are byte reversed. Check how you are accessing the hi/lo bytes of the RGB565 data.

Thanks, that was the issue in the end!