generated from freudenreichan/info2Praktikum-NeuronalesNetz
ImageImput passt hoffentlich wieder
This commit is contained in:
1 parent
4d2f03eb4a
commit
488f69b46f
1 file changed
+77
-1
+77
-1
@@ -4,7 +4,7 @@
|
||||
|
||||
#include "imageInput.h"
|
||||
|
||||
// #define BUFFER_SIZE 100 // wird nicht verwendet!
|
||||
//#define BUFFER_SIZE 100 // wird nicht verwendet!
|
||||
#define FILE_HEADER_STRING "__info2_image_file_format__"
|
||||
|
||||
// Hilfsfunktionen
|
||||
@@ -14,6 +14,7 @@ static int readData(FILE *file, unsigned short *count, unsigned short *width, un
|
||||
static GrayScaleImageSeries *createSeries(unsigned short count); // Speicher reservieren
|
||||
static int readImagesFromFile(FILE *file, GrayScaleImageSeries *series, unsigned short width, unsigned short height); // Bilder lesen
|
||||
|
||||
|
||||
GrayScaleImageSeries *readImages(const char *path)
|
||||
{
|
||||
GrayScaleImageSeries *series = NULL;
|
||||
@@ -67,6 +68,7 @@ GrayScaleImageSeries *readImages(const char *path)
|
||||
return series;
|
||||
}
|
||||
|
||||
|
||||
void clearSeries(GrayScaleImageSeries *series)
|
||||
{
|
||||
if (series == NULL)
|
||||
@@ -84,6 +86,7 @@ void clearSeries(GrayScaleImageSeries *series)
|
||||
free(series);
|
||||
}
|
||||
|
||||
|
||||
static FILE *openImageFile(const char *path)
|
||||
{
|
||||
FILE *file = fopen(path, "rb");
|
||||
@@ -97,6 +100,7 @@ static FILE *openImageFile(const char *path)
|
||||
return file;
|
||||
}
|
||||
|
||||
|
||||
static int checkFileHeader(FILE *file)
|
||||
{
|
||||
size_t len = strlen(FILE_HEADER_STRING);
|
||||
@@ -120,6 +124,7 @@ static int checkFileHeader(FILE *file)
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static GrayScaleImageSeries *createSeries(unsigned short count)
|
||||
{
|
||||
GrayScaleImageSeries *series =
|
||||
@@ -129,3 +134,74 @@ static GrayScaleImageSeries *createSeries(unsigned short count)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
series->count = count;
|
||||
|
||||
series->labels =
|
||||
malloc(count * sizeof(unsigned char));
|
||||
|
||||
series->images =
|
||||
calloc(count, sizeof(GrayScaleImage));
|
||||
|
||||
if (series->labels == NULL || series->images == NULL)
|
||||
{
|
||||
free(series->labels);
|
||||
free(series->images);
|
||||
free(series);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return series;
|
||||
}
|
||||
|
||||
|
||||
static int readData(FILE *file, unsigned short *count, unsigned short *width, unsigned short *height)
|
||||
{
|
||||
if (fread(count, sizeof(unsigned short), 1, file) != 1)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (fread(width, sizeof(unsigned short), 1, file) != 1)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (fread(height, sizeof(unsigned short), 1, file) != 1)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int readImagesFromFile(FILE *file, GrayScaleImageSeries *series, unsigned short width, unsigned short height)
|
||||
{
|
||||
for (unsigned int i = 0; i < series->count; i++)
|
||||
{
|
||||
series->images[i].width = width;
|
||||
series->images[i].height = height;
|
||||
|
||||
series->images[i].buffer =
|
||||
malloc(width * height * sizeof(GrayScalePixelType));
|
||||
|
||||
if (series->images[i].buffer == NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (fread(series->images[i].buffer, sizeof(GrayScalePixelType), width * height, file) != width * height)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (fread(&series->labels[i], sizeof(unsigned char), 1, file) != 1)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
Reference in new issue
Block a user