generated from freudenreichan/info2Praktikum-NeuronalesNetz
Got a compiled Version that runs all the Test. Still 4 Test Failures present
This commit is contained in:
parent
65da22f564
commit
f52de5e656
75
imageInput.c
75
imageInput.c
@ -31,31 +31,36 @@ int checkString (FILE *file){ //Checks if String at the start of File equals exp
|
|||||||
}
|
}
|
||||||
|
|
||||||
void setParameters(FILE* file, unsigned int* imageCount, unsigned int* width, unsigned int* height);
|
void setParameters(FILE* file, unsigned int* imageCount, unsigned int* width, unsigned int* height);
|
||||||
void allocateMemory(GrayScaleImageSeries *series, const int imageCount, const int width, const int height);
|
int allocateMemory(GrayScaleImageSeries *series, const int imageCount, const int width, const int height);
|
||||||
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
|
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
|
||||||
|
|
||||||
|
|
||||||
GrayScaleImageSeries *readImages(const char *path)
|
GrayScaleImageSeries *readImages(const char *path)
|
||||||
{
|
{
|
||||||
FILE* file = fopen(path, "rb");
|
FILE* file = fopen(path, "rb");
|
||||||
if(!(checkString(file)))
|
if(!(checkString(file))){
|
||||||
fclose(file);
|
fclose(file);
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
// TODO open binary file from file name
|
// TODO open binary file from file name
|
||||||
unsigned int width, height; //uninitialised Variables, will be set by setparamters
|
unsigned int width, height; //uninitialised Variables, will be set by setparamters
|
||||||
|
|
||||||
|
|
||||||
GrayscaleImageSeries *series = malloc(sizeof(GrayScaleImageSeries)); //Reserving memory for series
|
GrayScaleImageSeries *series = malloc(sizeof(GrayScaleImageSeries)); //Reserving memory for series
|
||||||
|
|
||||||
setParameters(file, &(series->count), &width, &height); //setting parameters taken from image file
|
setParameters(file, &(series->count), &width, &height); //setting parameters taken from image file
|
||||||
|
|
||||||
if(!allocateMemory(series, width, height)){
|
if(!allocateMemory(series, series->count, width, height)){
|
||||||
clearSeries(series); //If Memory couldnt be correctly allocated a clearing is necessary
|
clearSeries(series); //If Memory couldnt be correctly allocated a clearing is necessary
|
||||||
|
fclose(file);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//Getting image data
|
//Getting image data
|
||||||
for(int i = 0; i < series.count ; i++){ //Iterating for every image
|
for(unsigned int i = 0; i < series->count ; i++){ //Iterating for every image
|
||||||
for(int j = 0; j < series.count; j++){ //Iterating for every pixel
|
for(unsigned int j = 0; j < width * height; j++){ //Iterating for every pixel
|
||||||
fread(&(series->images[i].buffer[j]), sizeof(GrayScalePixelType), 1, file);
|
fread(&(series->images[i].buffer[j]), sizeof(GrayScalePixelType), 1, file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -66,19 +71,19 @@ GrayScaleImageSeries *readImages(const char *path)
|
|||||||
// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
|
// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
|
||||||
void clearSeries(GrayScaleImageSeries *series)
|
void clearSeries(GrayScaleImageSeries *series)
|
||||||
{
|
{
|
||||||
printf("ClearSeries BP1\n");
|
if (!series)
|
||||||
for(int i = series->count - 1; i >= 0; i--)
|
return;
|
||||||
|
|
||||||
|
//Freeing Pixelbuffer for every image
|
||||||
|
for(unsigned int i = 0; i < series->count; i++)
|
||||||
{
|
{
|
||||||
free(series->images+series->count*sizeof(GrayScaleImage)*i);//Muss Falsch sein
|
free(series->images[i].buffer);
|
||||||
free(series->labels+series->count*sizeof(unsigned char)*i);
|
|
||||||
}
|
}
|
||||||
printf("ClearSeries BP2\n");
|
free(series->images); //Freeing Images array
|
||||||
series->images = NULL;
|
|
||||||
series->labels = NULL;
|
|
||||||
|
|
||||||
free(series);
|
free(series->labels); //Freeing Labels
|
||||||
|
free(series); //Freeing Main Structure
|
||||||
|
|
||||||
series = NULL;
|
|
||||||
}
|
}
|
||||||
void setParameters(FILE* file, unsigned int* imageCount, unsigned int* width, unsigned int* height) { // sets the parameters
|
void setParameters(FILE* file, unsigned int* imageCount, unsigned int* width, unsigned int* height) { // sets the parameters
|
||||||
|
|
||||||
@ -86,6 +91,13 @@ void setParameters(FILE* file, unsigned int* imageCount, unsigned int* width, un
|
|||||||
|
|
||||||
fseek(file, 27, SEEK_SET);
|
fseek(file, 27, SEEK_SET);
|
||||||
fread(buffer, 1, 3, file);
|
fread(buffer, 1, 3, file);
|
||||||
|
if(fread(buffer, 1, 3, file) != 3){
|
||||||
|
*imageCount = 0;
|
||||||
|
*width = 0;
|
||||||
|
*height = 0;
|
||||||
|
|
||||||
|
|
||||||
|
return;} //Stops if file is too short and clean up
|
||||||
|
|
||||||
*imageCount = (int) buffer[0];
|
*imageCount = (int) buffer[0];
|
||||||
|
|
||||||
@ -97,14 +109,33 @@ void setParameters(FILE* file, unsigned int* imageCount, unsigned int* width, un
|
|||||||
|
|
||||||
// TODO read from file and write in 2d arry, write label in labels array, repeat for imageCount -> done in readImages
|
// TODO read from file and write in 2d arry, write label in labels array, repeat for imageCount -> done in readImages
|
||||||
}
|
}
|
||||||
// change this to createMatrix?
|
|
||||||
void allocateMemory(GrayScaleImageSeries *series, const int imageCount, const int width, const int height) {
|
|
||||||
series->images = malloc (sizeof(GrayScaleImage) * imageCount); //Reserving Memory for Images Array
|
|
||||||
series->labels = malloc (series->count * sizeof(unsigned char)); //One Label for every Image
|
|
||||||
|
|
||||||
for (int i = 0; i < imageCount; i++) {
|
int allocateMemory(GrayScaleImageSeries *series, const int imageCount, const int width, const int height) {
|
||||||
series->images[i].buffer = (unsigned char *) malloc(width * height * sizeof(unsigned char)); // allocates memory for every image in the series
|
series->count = imageCount; //counts number of images in series
|
||||||
|
|
||||||
|
series->images = malloc (sizeof(GrayScaleImage) * imageCount); //Reserving Memory for Images Array
|
||||||
|
if (series->images == NULL)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
|
||||||
|
series->labels = malloc (series->count * sizeof(unsigned char)); //One Label for every Image
|
||||||
|
if (series->labels == NULL){
|
||||||
|
return 0;}
|
||||||
|
|
||||||
|
for (int i = 0; i < imageCount; i++) { //Reserving Memory for every Images Pixelbuffer and setting width and height
|
||||||
|
series->images[i].width = width;
|
||||||
|
series->images[i].height = height;
|
||||||
|
|
||||||
|
series->images[i].buffer = malloc(width * height * sizeof(unsigned char)); // allocates memory for every image in the series
|
||||||
|
if (series->images[i].buffer == NULL){
|
||||||
|
for (int j = 0; j < i; j++){
|
||||||
|
free(series->images[j].buffer);
|
||||||
|
}
|
||||||
|
free(series->images);
|
||||||
|
free(series->labels);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user