generated from freudenreichan/info2Praktikum-NeuronalesNetz
Alles bis auf ClearSeries sollte funktionieren
This commit is contained in:
1 parent
1e7f637a08
commit
ad4874d309
2 files changed
+28
-18
No files matched your search
+28
-18
@@ -8,31 +8,29 @@
|
||||
|
||||
// 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){
|
||||
void readIndividualImage(unsigned int width, unsigned int height, FILE *file, GrayScaleImage *gimage){
|
||||
|
||||
GrayScalePixelType *buf = calloc(width * height, sizeof(unsigned char));
|
||||
if(!buf){
|
||||
fprintf(stderr, "calloc failed!\n");
|
||||
return NULL;
|
||||
return;
|
||||
}
|
||||
if(fread(buf, sizeof(char), width * height,file) != (width* height)){
|
||||
fprintf(stderr, "imageline not in the rigth format!\n");
|
||||
free(buf);
|
||||
return NULL;
|
||||
return;
|
||||
}
|
||||
|
||||
GrayScaleImage *gimage = calloc(sizeof(GrayScaleImage), 1);
|
||||
gimage = calloc(sizeof(GrayScaleImage), 1);
|
||||
if(gimage == NULL){
|
||||
fprintf(stderr, "calloc failed!\n");
|
||||
free(buf);
|
||||
return NULL;
|
||||
return;
|
||||
}
|
||||
|
||||
gimage->buffer = buf;
|
||||
gimage->width = width;
|
||||
gimage->height = height;
|
||||
|
||||
return gimage;
|
||||
//fprintf(stdout, "%d,%d\n", gimage->width, gimage->height);
|
||||
}
|
||||
|
||||
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
|
||||
@@ -56,23 +54,30 @@ GrayScaleImageSeries *readImages(const char *path)
|
||||
|
||||
//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))){
|
||||
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))){
|
||||
short shortbuffer[3];
|
||||
if(fread(shortbuffer, sizeof(short), 3, file) != 3){
|
||||
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->count = shortbuffer[0];
|
||||
unsigned int width = shortbuffer[1];
|
||||
unsigned int height = shortbuffer[2];
|
||||
//fprintf(stderr, "%d, %d, %d\n", series->count, width, height);
|
||||
|
||||
if(width == 0 || height == 0 || series->count == 0){
|
||||
fprintf(stderr, "number of images, width or heigth are 0!\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
series->labels = calloc(series->count, sizeof(char));
|
||||
if(series->labels == NULL){
|
||||
@@ -83,22 +88,27 @@ GrayScaleImageSeries *readImages(const char *path)
|
||||
|
||||
//Lies Bilder aus
|
||||
for (int i = 0; i < series->count; i++){
|
||||
if((series->images[i] = *readIndividualImage(width, height, file))){
|
||||
readIndividualImage(width, height, file, &series->images[i]);
|
||||
/*if(series->images[i] == NULL){
|
||||
clearSeries(series);
|
||||
return NULL;
|
||||
}
|
||||
}*/
|
||||
fread(&series->labels[i], sizeof(char), 1, file);
|
||||
//fprintf(stdout, "width: %d, height: %d\n", series->images[i].width, series->images[i].height);
|
||||
}
|
||||
|
||||
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]);
|
||||
fprintf(stdout, "%d\n", i);
|
||||
free(&series->images[i].buffer);
|
||||
fprintf(stdout, "%d\n", i);
|
||||
}
|
||||
free(series->labels);
|
||||
free(series);
|
||||
return;
|
||||
}
|
||||
Reference in new issue
Block a user