Matlab: Retrieve data from libstruct field where actual data exceeds struct layout

조회 수: 3 (최근 30일)
I am failing to access image pixel data using the C interface of LibRaw from within MATLAB.
Function signature:
DllDef libraw_processed_image_t* libraw_dcraw_make_mem_image(libraw_data_t* lr, int *errc);
Struct definition:
typedef struct
{
enum LibRaw_image_formats type;
ushort height,
width,
colors,
bits;
unsigned int data_size;
unsigned char data[1];
}libraw_processed_image_t;
Invoking the function call from MATLAB:
perror = libpointer('int32Ptr',0);
pim = calllib('libraw', 'libraw_dcraw_make_mem_image', obj.handle, perror);
image = zeros(pim.Value.data_size, 'uint8');
% copy *pim.Value.data_size* bytes of memory starting at *pim.Value.data* into image
I am at a loss as to how I access/copy the memory starting at the address of pim.Value.data. Unsurprisingly MATLAB treats it as a single byte

답변 (1개)

Philip Borghesani
Philip Borghesani 2018년 3월 28일
The placement of a small array at the end of a structure and indexing into it is a common practice that is not supported by the C standard or by MATLAB. There are extensions that have been added to C99 for 'flexible array members' that MATLAB may not be able to support ether.
It is possible there may be a way to extract the data in pim using MATLAB but I am not sure. You may be better off creating a mex file that directly accesses libraw or using an entirely different way to load the image into MATLAB.
Things to try:
  • If your images are all the same size then edit the header or prototype file to hard code the correct number of bytes for your camera.
  • You may be able to use the reshape method on libpointer to change the size of data.
  댓글 수: 1
shaun prickett
shaun prickett 2018년 3월 28일
Thank you for your answer, I'm going to leave it open for the time being for any more suggestions. I'll experiment with reshape and I've just noticed setdatatype which also looks promising. I also quite like your 'header hack' / prototype suggestion as an easy fix

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Call C from MATLAB에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by