generated from freudenreichan/info2Praktikum-NeuronalesNetz
Added functionality to imageInput.c
This commit is contained in:
parent
419296af9e
commit
22b79b5cd3
75
imageInput.c
75
imageInput.c
@ -7,26 +7,18 @@
|
|||||||
#define FILE_HEADER_STRING "__info2_image_file_format__"
|
#define FILE_HEADER_STRING "__info2_image_file_format__"
|
||||||
|
|
||||||
|
|
||||||
/// @brief Gets the next char value from specified and opened file
|
/// @brief Gets the next char value from specified and opened file in the moment that is only ONE short int.
|
||||||
/// @param openedFile stream FILE, from which to read
|
/// @param openedFile stream FILE, from which to read
|
||||||
/// @param iterations how many chars are being taken from (1 char equals 2 Hexdecimals equals 8bit)
|
static int getCharValueFromFile(FILE* openedFile) {
|
||||||
static int getCharValueFromFile(FILE* openedFile, int iterations) {
|
|
||||||
int addToFile = 0;
|
int addToFile = 0;
|
||||||
|
|
||||||
if (openedFile == NULL) return 0;
|
if (openedFile == NULL) return 0;
|
||||||
|
|
||||||
for (int i = 0; i < iterations; i++) {
|
addToFile = fgetc(openedFile);
|
||||||
int tmp = fgetc(openedFile);
|
addToFile += fgetc(openedFile) * 256;
|
||||||
//If the File ends, the method returns a '0'
|
|
||||||
if (openedFile == EOF) return addToFile;
|
|
||||||
|
|
||||||
addToFile += tmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
return addToFile;
|
return addToFile;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
|
|
||||||
GrayScaleImageSeries *readImages(const char *path)
|
GrayScaleImageSeries *readImages(const char *path)
|
||||||
{
|
{
|
||||||
GrayScaleImageSeries *series = NULL;
|
GrayScaleImageSeries *series = NULL;
|
||||||
@ -38,78 +30,93 @@ GrayScaleImageSeries *readImages(const char *path)
|
|||||||
// file Could not be opened/does not exist
|
// file Could not be opened/does not exist
|
||||||
if (openFile != NULL) {
|
if (openFile != NULL) {
|
||||||
|
|
||||||
char* actualFileTag = malloc(strlen(FILE_HEADER_STRING) * sizeof(char));
|
char* actualFileTag = malloc(strlen(FILE_HEADER_STRING)+1 * sizeof(char));
|
||||||
int numberOfImages = 0;
|
int numberOfImages = 0;
|
||||||
int width = 0;
|
int width = 0;
|
||||||
int height = 0;
|
int height = 0;
|
||||||
|
|
||||||
for (int i = 0; i < strlen(FILE_HEADER_STRING); i++) {
|
for (int i = 0; i < strlen(FILE_HEADER_STRING); i++) {
|
||||||
actualFileTag += fgetc(openFile);
|
char tmpFileChar = fgetc(openFile);
|
||||||
|
|
||||||
|
if (tmpFileChar == EOF) {
|
||||||
|
fclose(openFile);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
actualFileTag[i] = tmpFileChar;
|
||||||
}
|
}
|
||||||
|
actualFileTag[strlen(FILE_HEADER_STRING)] = '\0';
|
||||||
|
|
||||||
// checks if the files are equal: strcmp should return '0' --> convert it to '1' for 'true'
|
// checks if the files are equal: strcmp should return '0' --> convert it to '1' for 'true'
|
||||||
int fileTagEqual = !strcmp(actualFileTag, FILE_HEADER_STRING);
|
int fileTagEqual = strcmp(actualFileTag, FILE_HEADER_STRING);
|
||||||
|
|
||||||
//we only need the fileTag to verify its an image for our series.
|
//we only need the fileTag to verify its an image for our series.
|
||||||
free(actualFileTag);
|
free(actualFileTag);
|
||||||
actualFileTag = NULL;
|
actualFileTag = NULL;
|
||||||
|
|
||||||
|
|
||||||
if (fileTagEqual) {
|
if (fileTagEqual == 0) {
|
||||||
numberOfImages = getCharValueFromFile(openFile, 2);
|
numberOfImages = getCharValueFromFile(openFile);
|
||||||
|
|
||||||
// no Images in series -> No image-series
|
// no Images in series -> No image-series
|
||||||
if (numberOfImages == 0) {
|
if (numberOfImages == 0) {
|
||||||
fclose(openFile);
|
fclose(openFile);
|
||||||
return series;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
width = getCharValueFromFile(openFile, 2);
|
width = getCharValueFromFile(openFile);
|
||||||
height = getCharValueFromFile(openFile, 2);
|
height = getCharValueFromFile(openFile);
|
||||||
|
|
||||||
// no height/width --> impossible file
|
// no height/width --> impossible file
|
||||||
if(height == 0 || width == 0) {
|
if(height == 0 || width == 0) {
|
||||||
fclose(openFile);
|
fclose(openFile);
|
||||||
return series;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
//all the starting parameters are set --> the images can be read and stored
|
//all the starting parameters are set --> the images can be read and stored
|
||||||
GrayScaleImage* images = malloc(numberOfImages * sizeof(GrayScaleImage));
|
// GrayScaleImage* images = malloc(numberOfImages * sizeof(GrayScaleImage));
|
||||||
unsigned char* labels = malloc(numberOfImages * sizeof(unsigned char));
|
// unsigned char* labels = malloc(numberOfImages * sizeof(unsigned char));
|
||||||
|
series = malloc(sizeof(GrayScaleImageSeries));
|
||||||
|
|
||||||
series->count = 0;
|
series->count = 0;
|
||||||
series->images = images;
|
series->images = malloc(numberOfImages * sizeof(GrayScaleImage));
|
||||||
series->labels = labels;
|
series->labels = malloc(numberOfImages * sizeof(unsigned char));
|
||||||
|
|
||||||
|
printf(" %d , %d , %d \n", width, height, numberOfImages);
|
||||||
|
|
||||||
for (int i = 0; i < numberOfImages; i++) {
|
for (int i = 0; i < numberOfImages; i++) {
|
||||||
for (int j = 0; j < width * height; j++) {
|
series->images[i].height = height;
|
||||||
// allocating the actual matrix image for image
|
series->images[i].width = width;
|
||||||
images[i].buffer = malloc(width * height);
|
series->images[i].buffer = malloc(width * height);
|
||||||
|
|
||||||
images[i].height = height;
|
for (int j = 0; j < (width * height); j++) {
|
||||||
images[i].width = width;
|
// allocating the actual matrix image for image
|
||||||
images[i].buffer[j] = fgetc(openFile);
|
series->images[i].buffer[j] = fgetc(openFile);
|
||||||
}
|
}
|
||||||
//rest of the values that only affect the image itself
|
//rest of the values that only affect the image itself
|
||||||
series->labels[i] = fgetc(openFile);
|
series->labels[i] = fgetc(openFile);
|
||||||
series->count++;
|
series->count++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fclose (openFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose(openFile);
|
|
||||||
return series;
|
return series;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
|
|
||||||
void clearSeries(GrayScaleImageSeries *series)
|
void clearSeries(GrayScaleImageSeries *series)
|
||||||
{
|
{
|
||||||
|
if (series == NULL) return;
|
||||||
|
|
||||||
for (int i = 0; i < series->count; i++) {
|
for (int i = 0; i < series->count; i++) {
|
||||||
free(series->images[i].buffer);
|
free(series->images[i].buffer);
|
||||||
|
series->images[i].buffer = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
free(series->images);
|
free(series->images);
|
||||||
series->images = NULL;
|
series->images = NULL;
|
||||||
free(series->labels);
|
free(series->labels);
|
||||||
series->labels = NULL;
|
series->labels = NULL;
|
||||||
|
free(series);
|
||||||
|
series = NULL;
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user