|
|
|
@@ -7,16 +7,87 @@
|
|
|
|
|
#define FILE_HEADER_STRING "__info2_image_file_format__"
|
|
|
|
|
|
|
|
|
|
// TODO Implementieren Sie geeignete Hilfsfunktionen für das Lesen der Bildserie aus einer Datei
|
|
|
|
|
int readHeader(FILE *file)
|
|
|
|
|
{
|
|
|
|
|
char fileHeader[32];
|
|
|
|
|
|
|
|
|
|
fread(fileHeader, sizeof(char), strlen(FILE_HEADER_STRING), file);
|
|
|
|
|
|
|
|
|
|
if(strncmp(FILE_HEADER_STRING, fileHeader, strlen(FILE_HEADER_STRING))){
|
|
|
|
|
return -1;
|
|
|
|
|
} else{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsigned short readUnShortOfFile(FILE *file){
|
|
|
|
|
unsigned short number = 0;
|
|
|
|
|
|
|
|
|
|
if(fread(&number, sizeof(unsigned short), 1, file) != 1){
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int readOneImage(GrayScaleImageSeries *series, FILE *file, unsigned short width, unsigned short height, unsigned short i){
|
|
|
|
|
|
|
|
|
|
series->images[i].buffer = malloc(width * height * sizeof(GrayScalePixelType));
|
|
|
|
|
fread(series->images[i].buffer, sizeof(GrayScalePixelType), width * height, file);
|
|
|
|
|
fread(&series->labels[i], sizeof(unsigned char), 1, file);
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
|
|
|
|
|
GrayScaleImageSeries *readImages(const char *path)
|
|
|
|
|
{
|
|
|
|
|
GrayScaleImageSeries *series = NULL;
|
|
|
|
|
FILE *file;
|
|
|
|
|
unsigned short width, height;
|
|
|
|
|
|
|
|
|
|
file = fopen(path, "rb");
|
|
|
|
|
|
|
|
|
|
if(file == NULL || readHeader(file) == -1) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
series = malloc(sizeof(GrayScaleImageSeries));
|
|
|
|
|
|
|
|
|
|
series->count = readUnShortOfFile(file);
|
|
|
|
|
width = readUnShortOfFile(file);
|
|
|
|
|
height = readUnShortOfFile(file);
|
|
|
|
|
|
|
|
|
|
if(series->count == -1 || width == -1 || height == -1){
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
series->images = malloc(series->count * sizeof(GrayScaleImage));
|
|
|
|
|
series->labels = malloc(series->count * sizeof(unsigned char));
|
|
|
|
|
|
|
|
|
|
for(int i = 0; i < series->count; i++){
|
|
|
|
|
series->images[i].width = width;
|
|
|
|
|
series->images[i].height = height;
|
|
|
|
|
readOneImage(series, file, width, height, i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return series;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
|
|
|
|
|
void clearSeries(GrayScaleImageSeries *series)
|
|
|
|
|
{
|
|
|
|
|
if(series == NULL){
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for(int i = 0; i < series->count; i++){
|
|
|
|
|
free(series->images[i].buffer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
free(series->images);
|
|
|
|
|
free(series->labels);
|
|
|
|
|
free(series);
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|