#include #include #include #include #include "imageInput.h" #define BUFFER_SIZE 100 #define FILE_HEADER_STRING "__info2_image_file_format__" /// @brief Gets the next char value from specified and opened file in the moment that is only ONE short int. /// @param openedFile stream FILE, from which to read static int getCharValueFromFile(FILE* openedFile) { int addToFile = 0; if (openedFile == NULL) return 0; addToFile = fgetc(openedFile); addToFile += fgetc(openedFile) * 256; return addToFile; } GrayScaleImageSeries* readImages(const char *path) { GrayScaleImageSeries *series = NULL; // uint16_t FILE* openFile; openFile = fopen(path, "rb"); // file Could not be opened/does not exist if (openFile != NULL) { char* actualFileTag = malloc(strlen(FILE_HEADER_STRING)+1 * sizeof(char)); int numberOfImages = 0; int width = 0; int height = 0; for (int i = 0; i < strlen(FILE_HEADER_STRING); i++) { char tmpFileChar = fgetc(openFile); if (tmpFileChar == EOF) { fclose(openFile); return NULL; } actualFileTag[i] = tmpFileChar; } actualFileTag[strlen(FILE_HEADER_STRING)] = '\0'; // checks if the files are equal int fileTagEqual = strcmp(actualFileTag, FILE_HEADER_STRING); //we only need the fileTag to verify its an image for our series. free(actualFileTag); actualFileTag = NULL; //only if equal, the method continues if (fileTagEqual == 0) { numberOfImages = getCharValueFromFile(openFile); // no Images in series -> No image-series if (numberOfImages == 0) { fclose(openFile); return NULL; } width = getCharValueFromFile(openFile); height = getCharValueFromFile(openFile); // no height/width --> impossible file if(height == 0 || width == 0) { fclose(openFile); return NULL; } //all the starting parameters are set --> the images can be read and stored series = malloc(sizeof(GrayScaleImageSeries)); series->count = 0; series->images = malloc(numberOfImages * sizeof(GrayScaleImage)); series->labels = malloc(numberOfImages * sizeof(unsigned char)); for (int i = 0; i < numberOfImages; i++) { series->images[i].height = height; series->images[i].width = width; series->images[i].buffer = malloc(width * height); for (int j = 0; j < (width * height); j++) { // allocating the actual matrix image for image series->images[i].buffer[j] = fgetc(openFile); } //rest of the values that only affect the image itself series->labels[i] = fgetc(openFile); series->count++; } } // the fclose happens here, since every other path closes the file beforehand fclose (openFile); } return series; } void clearSeries(GrayScaleImageSeries *series) { if (series == NULL) return; for (int i = 0; i < series->count; i++) { free(series->images[i].buffer); series->images[i].buffer = NULL; } free(series->images); series->images = NULL; free(series->labels); series->labels = NULL; free(series); series = NULL; }