generated from freudenreichan/info2Praktikum-NeuronalesNetz
ImageImput passt hoffentlich wieder
This commit is contained in:
1 parent
4d2f03eb4a
commit
488f69b46f
1 file changed
+81
-5
+81
-5
@@ -4,16 +4,17 @@
|
|||||||
|
|
||||||
#include "imageInput.h"
|
#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__"
|
#define FILE_HEADER_STRING "__info2_image_file_format__"
|
||||||
|
|
||||||
// Hilfsfunktionen
|
// Hilfsfunktionen
|
||||||
static FILE *openImageFile(const char *path); // Datei öffnen
|
static FILE *openImageFile(const char *path); // Datei öffnen
|
||||||
static int checkFileHeader(FILE *file); // Header prüfen
|
static int checkFileHeader(FILE *file); // Header prüfen
|
||||||
static int readData(FILE *file, unsigned short *count, unsigned short *width, unsigned short *height); // Anzahl Bilder, Breite, Höhe einlesen
|
static int readData(FILE *file, unsigned short *count, unsigned short *width, unsigned short *height); // Anzahl Bilder, Breite, Höhe einlesen
|
||||||
static GrayScaleImageSeries *createSeries(unsigned short count); // Speicher reservieren
|
static GrayScaleImageSeries *createSeries(unsigned short count); // Speicher reservieren
|
||||||
static int readImagesFromFile(FILE *file, GrayScaleImageSeries *series, unsigned short width, unsigned short height); // Bilder lesen
|
static int readImagesFromFile(FILE *file, GrayScaleImageSeries *series, unsigned short width, unsigned short height); // Bilder lesen
|
||||||
|
|
||||||
|
|
||||||
GrayScaleImageSeries *readImages(const char *path)
|
GrayScaleImageSeries *readImages(const char *path)
|
||||||
{
|
{
|
||||||
GrayScaleImageSeries *series = NULL;
|
GrayScaleImageSeries *series = NULL;
|
||||||
@@ -67,6 +68,7 @@ GrayScaleImageSeries *readImages(const char *path)
|
|||||||
return series;
|
return series;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void clearSeries(GrayScaleImageSeries *series)
|
void clearSeries(GrayScaleImageSeries *series)
|
||||||
{
|
{
|
||||||
if (series == NULL)
|
if (series == NULL)
|
||||||
@@ -84,6 +86,7 @@ void clearSeries(GrayScaleImageSeries *series)
|
|||||||
free(series);
|
free(series);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static FILE *openImageFile(const char *path)
|
static FILE *openImageFile(const char *path)
|
||||||
{
|
{
|
||||||
FILE *file = fopen(path, "rb");
|
FILE *file = fopen(path, "rb");
|
||||||
@@ -97,6 +100,7 @@ static FILE *openImageFile(const char *path)
|
|||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int checkFileHeader(FILE *file)
|
static int checkFileHeader(FILE *file)
|
||||||
{
|
{
|
||||||
size_t len = strlen(FILE_HEADER_STRING);
|
size_t len = strlen(FILE_HEADER_STRING);
|
||||||
@@ -120,6 +124,7 @@ static int checkFileHeader(FILE *file)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static GrayScaleImageSeries *createSeries(unsigned short count)
|
static GrayScaleImageSeries *createSeries(unsigned short count)
|
||||||
{
|
{
|
||||||
GrayScaleImageSeries *series =
|
GrayScaleImageSeries *series =
|
||||||
@@ -129,3 +134,74 @@ static GrayScaleImageSeries *createSeries(unsigned short count)
|
|||||||
{
|
{
|
||||||
return NULL;
|
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