Baked image is upside down

Hi,

I baked a texture with code. I then baked the same texture in blender.
The problem is, the image is upside down when I baked with code. This is how I am saving the output after baking a texture with cycles. Any clue where I went wrong?

Thanks in advance.

 int channels = 4;
  ImageSpec spec(buffer_params.width, buffer_params.height, channels, TypeDesc::FLOAT);
  out->open(file_path, spec);
  // out->write_image(TypeDesc::FLOAT, pixels);

  out->write_image(TypeDesc::FLOAT, pixels, AutoStride, AutoStride, AutoStride);

You need to convert between different conventions, see this line from cycles_standalone.cpp.

  /* conversion for different top/bottom convention */
  out->write_image(
      TypeDesc::UINT8, pixels + (h - 1) * w * channels, AutoStride, -w * channels, AutoStride);
1 Like

Thank you @brecht

I tried this. It did not work.
I even tried with UINT8. No use.
Images are coming bad.

float *pixels = get_data();
int w = buffer_params.width;
  int h = buffer_params.height;
  int channels = 4;

  ImageSpec spec(w, h, channels, TypeDesc::FLOAT);
  out->open(file_path, spec);

  /* conversion for different top/bottom convention */
  out->write_image(TypeDesc::FLOAT, pixels + ((h-1) * w * channels), AutoStride, -w * channels, AutoStride);

  out->close();

The below code worked.

int w = buffer_params.width;
  int h = buffer_params.height;
  int channels = 4;

      ImageSpec spec(w, h, channels, TypeDesc::FLOAT);
      out->open(file_path, spec);
      int scanlinesize = w * channels * sizeof(float);
      out->write_image(TypeDesc::FLOAT,
                       (char *)pixels + ((h - 1) * scanlinesize),  // offset to last
                       AutoStride,                                 // default x stride
                       -scanlinesize,                              // special y stride
                       AutoStride);                                // default z stride

      out->close();