Reading and saving camera data using Argus

Hello,

I am using argus/samples/yuvjpeg as a reference to save yuv420 (only intensity for now) into file. Here is the snippet of what I am doing:

...
Image *image = iFrame->getImage();
IImage *iImage = interface_cast<IImage>(image);
IImage2D *iImage2D = interface_cast<IImage2D>(image);
const int32_t Y=0;
const uint64_t size = iImage->getBufferSize(Y)
const char *d = static_cast<const char*>(iImage->mapBuffer(Y));
file->write(d, size);   //save to file
...

When I view the file using imageJ, it looks scrambled. My frame setting is 640x480. The size = 768*512=393216, so in imageJ, I set my width=768, height=512.

What am I doing wrong? The image looks scrambled and I don’t know how pixels are mapped or place in iImage->mapBuffer(0)?

Thanks

I figured it out and now it work! Here’s the snippet that worked for me:

...
Image *image = iFrame->getImage();
IImage *iImage = interface_cast<IImage>(image);
IImage2D *iImage2D = interface_cast<IImage2D>(image);
NV::IImageNativeBuffer *iNativeBuffer = interface_cast<NV::IIMageNativeBuffer>(iFrame->getImage());
int32_t fd = iNativeBuffer->createNvBuffer(iImage2D->getSize(),
                                           NvBufferColorFormat_YUV420,
                                           NvBufferLayout_Pitch);
const uint32_t Y=0; //Y plane
void *data_ptr = NULL;
NvBufferMemMap(fd, Y, NvBufferMem_Read, &data_ptr);
NvBufferMemSyncForCpu(fd, Y, &data_ptr);  //Without this, I see random black lines(corrupted?) in image!!
const uint64_t buff_size = iImage->getBufferSize(Y);
file->write(data_ptr, buff_size);   //save to file
...

The called to NvBufferMemSyncForCpu(…) is required. I found that without sync, there are random black lines of various lengths are in the image. If the above can be improved further, please let me know.
Thanks.