generated from freudenreichan/info2Praktikum-NeuronalesNetz
104 lines
3.0 KiB
C
104 lines
3.0 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include "imageInput.h"
|
|
|
|
#define BUFFER_SIZE 100
|
|
#define FILE_HEADER_STRING "__info2_image_file_format__"
|
|
|
|
// TODO Implementieren Sie geeignete Hilfsfunktionen für das Lesen der Bildserie aus einer Datei
|
|
|
|
static GrayScaleImage *readIndividualImage(unsigned int width, unsigned int height, FILE *file){
|
|
|
|
GrayScalePixelType *buf = calloc(width * height, sizeof(unsigned char));
|
|
if(!buf){
|
|
fprintf(stderr, "calloc failed!\n");
|
|
return NULL;
|
|
}
|
|
if(fread(buf, sizeof(char), width * height,file) != (width* height)){
|
|
fprintf(stderr, "imageline not in the rigth format!\n");
|
|
free(buf);
|
|
return NULL;
|
|
}
|
|
|
|
GrayScaleImage *gimage = calloc(sizeof(GrayScaleImage), 1);
|
|
if(gimage == NULL){
|
|
fprintf(stderr, "calloc failed!\n");
|
|
free(buf);
|
|
return NULL;
|
|
}
|
|
|
|
gimage->buffer = buf;
|
|
gimage->width = width;
|
|
gimage->height = height;
|
|
|
|
return gimage;
|
|
}
|
|
|
|
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
|
|
GrayScaleImageSeries *readImages(const char *path)
|
|
{
|
|
GrayScaleImageSeries *series = NULL;
|
|
|
|
series = calloc(1, sizeof(GrayScaleImageSeries));
|
|
if(series == NULL){
|
|
fprintf(stderr, "Couldn't allocate enough space!\n");
|
|
return NULL;
|
|
}
|
|
series->count = 0;
|
|
|
|
FILE *file = fopen(path, "rb");
|
|
if(file == NULL){
|
|
fprintf(stderr, "%s could not be opened!\n", path);
|
|
clearSeries(series);
|
|
return NULL;
|
|
}
|
|
|
|
//Schaue ob die erste Zeile der richtige Header ist.
|
|
char buffer[32];
|
|
size_t bytes = fread(buffer, sizeof(char), strlen(FILE_HEADER_STRING), file);
|
|
if(!strncmp(buffer, FILE_HEADER_STRING, strlen(FILE_HEADER_STRING))){
|
|
fprintf(stderr, "__info2_image_file_format__ not found!\n");
|
|
clearSeries(series);
|
|
return NULL;
|
|
}
|
|
|
|
if((bytes = fread(buffer, sizeof(short), 3, file)) != (3*sizeof(short))){
|
|
fprintf(stderr, "Couldn't read number of images/sizes of Images!\n");
|
|
clearSeries(series);
|
|
return NULL;
|
|
}
|
|
|
|
//Merke menge an Bildern und Länge und Breite der Bilder
|
|
series->count = (short)buffer[0];
|
|
unsigned int width = (short)buffer[1];
|
|
unsigned int height = (short)buffer[2];
|
|
|
|
series->labels = calloc(series->count, sizeof(char));
|
|
if(series->labels == NULL){
|
|
fprintf(stderr, "calloc failed!\n");
|
|
clearSeries(series);
|
|
return NULL;
|
|
}
|
|
|
|
//Lies Bilder aus
|
|
for (int i = 0; i < series->count; i++){
|
|
if((series->images[i] = *readIndividualImage(width, height, file))){
|
|
clearSeries(series);
|
|
return NULL;
|
|
}
|
|
fread(&series->labels[i], sizeof(char), 1, file);
|
|
}
|
|
|
|
return series;
|
|
}
|
|
|
|
// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
|
|
void clearSeries(GrayScaleImageSeries *series)
|
|
{
|
|
for(int i = 0; i < series->count; i++){
|
|
free(&series->images[i]);
|
|
}
|
|
free(series->labels);
|
|
free(series);
|
|
} |