generated from freudenreichan/info2Praktikum-NeuronalesNetz
implemented header check
This commit is contained in:
1 parent
1e9579db2b
commit
290b0d649e
1 file changed
+78
@@ -8,15 +8,93 @@
|
||||
|
||||
// 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){
|
||||
|
||||
char *buf = calloc(width * height, sizeof(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;
|
||||
}
|
||||
|
||||
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*szieof(short))){
|
||||
fprintf(stderr, "Couldn't read number of images/sizes of Images!\n");
|
||||
clearSeries(series);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
series->count = (short)buffer[0];
|
||||
unsigned int width = (short)buffer[1];
|
||||
unsigned int height = (short)buffer[2];
|
||||
|
||||
series->labels = calloc(count, sizeof(char));
|
||||
if(series->labels == NULL){
|
||||
fprintf(stderr, "calloc failed!\n");
|
||||
clearSeries(series);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for (int i = 0; i < count; i++){
|
||||
if(series->images[i] = readIndividualImage(width, heigth, file) == NULL){
|
||||
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->GrayScaleImage[i]);
|
||||
}
|
||||
free(series);
|
||||
}
|
||||
Reference in new issue
Block a user