I have a byte array and need to fill an image data struct using it. The below method is written to fill the structure.
In the code, char *sfimageData is the byte array received from a USB camera. I need to generate this C++ struct. The method is working properly but I need to review this.
typedef struct image_struct
{
// Header
uint32_t id;
uint32_t timestamp;
uint16_t nHeight;
uint16_t nWidth;
// Buffer
float *depth_map;
uint16_t *amplitude_map;
uint16_t getWidth(){ return nWidth;};
uint16_t getHeight(){return nHeight;};
} IMAGE_STRUCT;
This method is working to fill the struct member:
void atImageEvent( short eventType, int size, short imageFormat, char* sfimageData, int dataLength, std::string& pScannerData )
{
std::string image_format;
switch ( imageFormat )
{
case JPEG_FILE_SECTION:
image_format = ".jpg";
break;
case TIFF_FILE_SECTION:
image_format = ".tiff";
break;
case BMP_FILE_SECTION:
image_format = ".bmp";
break;
case RAW_FILE_SELECTION:
image_format = ".deb";
break;
case RAW_PROCESSED_FILE_SELECTION:
image_format = ".dpf";
break;
case DEPTH_FRAME_FILE_SELECTION:
image_format = ".dpf";
break;
default:
cout << "Different type of image format, but saving as .dpf." << endl;
image_format = ".dpf";
break;
}
string sFileName;
stringstream ss;
ss << "imagedata";
ss << this->iImageFileNumber++;
ss << image_format;
sFileName = ss.str();
// cast image data into depth frame buffer.
IMAGE_STRUCT output_image_struct;
// define lengths for image struct
int dataIndex=0;
int idLength = sizeof(uint32_t);
int tstampLength = sizeof(uint32_t);
int heightLength = sizeof(uint16_t);
int widthLength = sizeof(uint16_t);
uint8_t *id, *timeStamp, *imgHeight, *imgWidth, *depthFrameData, *amplitudeData;
id = (uint8_t *)malloc(sizeof(uint32_t));
memset(id, 0x00, idLength);
dataIndex = idLength;
memcpy(id, sfimageData, sizeof(uint32_t));
union
{
uint32_t _id;
uint8_t *_byteArray;
}uId;
memcpy(&uId._byteArray, id, sizeof(uint32_t));
free(id);
output_image_struct.id = uId._id;
timeStamp = (uint8_t *)malloc(sizeof(uint32_t));
memset(timeStamp, 0x00, tstampLength);
memcpy(timeStamp, sfimageData+dataIndex, sizeof(uint32_t));
dataIndex += tstampLength;
union
{
uint32_t _timeStamp;
uint8_t *_byteArray;
}uTimeStamp;
memcpy(&uTimeStamp._byteArray, timeStamp, tstampLength);
free(timeStamp);
output_image_struct.timestamp = uTimeStamp._timeStamp;
imgHeight = (uint8_t *)malloc(heightLength);
memcpy(imgHeight, sfimageData+dataIndex, sizeof(uint16_t));
dataIndex += heightLength;
union
{
uint16_t _imgHeight;
uint8_t *_byteArray;
}uImgHeight;
memcpy(&uImgHeight._byteArray, imgHeight, heightLength);
free(imgHeight);
output_image_struct.nHeight = uImgHeight._imgHeight;
imgWidth = (uint8_t *)malloc(widthLength);
memset(imgWidth, 0x00, widthLength);
memcpy(imgWidth, sfimageData+dataIndex, sizeof(uint16_t));
dataIndex += widthLength;
union
{
uint32_t _imgWidth;
uint8_t *_byteArray;
}uImgWidth;
memcpy(&uImgWidth._byteArray, imgWidth, widthLength);
free(imgWidth);
output_image_struct.nWidth = uImgWidth._imgWidth;
int depthFrameLength = output_depth_frame.nHeight * output_depth_frame.nWidth * sizeof(float);
int amplitudeMapLength = output_depth_frame.nHeight * output_depth_frame.nWidth * sizeof(uint16_t);
depthFrameData = (uint8_t *)malloc(depthFrameLength);
memset(depthFrameData, 0x00, depthFrameLength);
memcpy(depthFrameData, sfimageData+dataIndex, depthFrameLength);
dataIndex += depthFrameLength;
union
{
uint8_t *byteArray;
float *floatArray;
} uDepthFrame;
uDepthFrame.byteArray = depthFrameData;
output_image_struct.depth_map = uDepthFrame.floatArray;
amplitudeData = (uint8_t *)malloc(amplitudeMapLength);
memset(amplitudeData, 0x00, amplitudeMapLength);
memcpy(amplitudeData, sfimageData + dataIndex, amplitudeMapLength);
dataIndex += amplitudeMapLength;
union
{
uint8_t *byteArray;
uint16_t *uint16Array;
} uAmplitude;
uAmplitude.byteArray = amplitudeData;
output_image_struct.amplitude_map = uAmplitude.uint16Array;
int middleIndex = (output_image_struct.getHeight()+1)*output_image_struct.getWidth()/2;
// Print output_image_struct (primitive members)
cout << "-----------------------------------------------------" << endl;
cout << "ID: " << output_image_struct.id << endl;
cout << "TimeStamp: " << output_image_struct.timestamp << endl;
cout << "Img Height: " << output_image_struct.nHeight << endl;
cout << "Img Width: " << output_image_struct.nWidth << endl;
cout << "First Index: " << 0 << " value: " << output_image_struct.depth_map[0] << endl;
cout << "Middle Index: " << middleIndex << " value: " << output_image_struct.depth_map[middleIndex] << endl;
cout << "last Index: " << (output_image_struct.getHeight() * output_image_struct.getWidth()) - 1 << " value: " << output_image_struct.depth_map[ (output_image_struct.getHeight() * output_image_struct.getWidth()) - 1 ] << endl;
cout << "-----------------------------------------------------" << endl;
// print data if interested
for(int x=0; x<depthFrameLength/sizeof(float); x++)
{
cout << " " << output_depth_frame.depth_map[x];
}
for(int x=0; x<amplitudeMapLength/sizeof(uint16_t); x++)
{
cout << " " << output_image_struct.amplitude_map[x];
}
cout << "END ********************" << endl;
free(depthFrameData);
free(amplitudeData);
}
Here, I have use many malloc and memcpy calls as well. Is it a bad practice when writing good code?
#includeand amain()function. That's not ready for review. \$\endgroup\$