I have a folder full of images that contain some metadata in the pixels that I'm trying to extract with my program. The program loops through each file, extracting the data. Problem is that some of the images are corrupted, and trying to extract data from them results in a segmentation fault.
Is there any way to catch this SIGSEGV signal and use it to ignore the current corrupted image and continue to the next noe?
try-catch will not help as this is a signal, not exception.
int Status::ExtractImageMetaData(cv::Mat src_in, int Index)
{
unsigned char *Image = (unsigned char *)(src_in.data);
unsigned char *StructByte = (unsigned char*) &ImageMetaData[Index];
uint32_t MetaOffset = (src_in.total() * src_in.elemSize()) - sizeof(ImageMetaData_t);
for(unsigned int i = 0; i < sizeof(ImageMetaData_t); i++)
{
StructByte[i] = Image[i + MetaOffset]; // SEGMENTATION FAULT HERE!
}
}
int main(int argc, char const *argv[])
{
if (imagePath == NULL){
imagePath = "/Pictures/metadataExtractionTest/sqlTest/";
}
// Iterate over all files in the "imagePath"
for (const auto & p : fs::directory_iterator(imagePath)){
string FullImageDir = p.path();
Mat Img = imread(FullImageDir.c_str());
int MetaStatus = SystemStatus.ExtractImageMetaData(Img, 0);
printf("done\n");
}
return 0;
}