forked from freudenreichan/info2Praktikum-NeuronalesNetz
Implement ImageInput
This commit is contained in:
1 parent
8babce0896
commit
c26c5247e3
2 files changed
+85
-11
No files matched your search
+83
-11
@@ -6,17 +6,89 @@
|
||||
#define BUFFER_SIZE 100
|
||||
#define FILE_HEADER_STRING "__info2_image_file_format__"
|
||||
|
||||
// TODO Implementieren Sie geeignete Hilfsfunktionen für das Lesen der Bildserie aus einer Datei
|
||||
|
||||
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
|
||||
GrayScaleImageSeries *readImages(const char *path)
|
||||
{
|
||||
GrayScaleImageSeries *series = NULL;
|
||||
|
||||
return series;
|
||||
static int controlFile(FILE *file) {
|
||||
if (file == NULL) {
|
||||
return 0;
|
||||
}
|
||||
char buffer[BUFFER_SIZE];
|
||||
fread(buffer, sizeof(char), strlen(FILE_HEADER_STRING), file);
|
||||
buffer[strlen(FILE_HEADER_STRING)] = '\0';
|
||||
return !strcmp(FILE_HEADER_STRING, buffer);
|
||||
}
|
||||
|
||||
static unsigned int readNumber(FILE *file) {
|
||||
unsigned int number = 0;
|
||||
fread(&number, sizeof(unsigned short), 1, file);
|
||||
return number;
|
||||
}
|
||||
|
||||
|
||||
// fillBuffer und readLabel muss immer in einer Schleife zusammen stehen
|
||||
static int fillBuffer(unsigned char buffer[], unsigned int bufferSize, FILE *file) {
|
||||
int readcount = fread(buffer, sizeof(GrayScalePixelType), bufferSize, file);
|
||||
if (readcount == 0)
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void readLabels(FILE *file, unsigned char label[], unsigned int count) {
|
||||
fread(label + count, sizeof(char), 1, file);
|
||||
}
|
||||
|
||||
|
||||
GrayScaleImageSeries *readImages(const char *path) {
|
||||
FILE *file = fopen(path, "rb");
|
||||
|
||||
if (!controlFile(file)) {
|
||||
return NULL;
|
||||
}
|
||||
GrayScaleImageSeries *firstSeries = malloc(sizeof(GrayScaleImageSeries));
|
||||
firstSeries ->count = readNumber(file);
|
||||
unsigned int width = readNumber(file);
|
||||
unsigned int height = readNumber(file);
|
||||
|
||||
firstSeries ->labels = malloc(firstSeries ->count*sizeof(unsigned char));
|
||||
firstSeries ->images = malloc(firstSeries ->count*sizeof(GrayScaleImage));
|
||||
firstSeries ->addrSourceTxt = file;
|
||||
if (firstSeries ->labels == NULL || firstSeries ->images == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
int fault = 1;
|
||||
unsigned int const bufferSize = width * height;
|
||||
for (unsigned int i = 0; i < firstSeries ->count && fault; i++)
|
||||
{
|
||||
GrayScaleImage *grayScaleImage = &firstSeries ->images[i];
|
||||
grayScaleImage ->buffer = malloc(width * height*sizeof(GrayScalePixelType));
|
||||
grayScaleImage ->width = width;
|
||||
grayScaleImage -> height = height;
|
||||
if (width == 0 || height == 0 || firstSeries ->count == 0) { // prüfen ob eingabe richtig ist
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fault = fillBuffer(grayScaleImage ->buffer, bufferSize, file);
|
||||
readLabels(file, firstSeries ->labels, i);
|
||||
}
|
||||
|
||||
return firstSeries;
|
||||
}
|
||||
|
||||
|
||||
void clearSeries(GrayScaleImageSeries *series) {
|
||||
if (series == NULL) {return;}
|
||||
if (series->images != NULL) {
|
||||
for (unsigned int i = 0; i < series->count; i++) {
|
||||
const GrayScaleImage image = series->images[i];
|
||||
if (image.buffer != NULL) {
|
||||
free(image.buffer);
|
||||
}
|
||||
}
|
||||
free(series->images);
|
||||
}
|
||||
if (series->labels != NULL) {
|
||||
free(series->labels);
|
||||
}
|
||||
fclose(series->addrSourceTxt);
|
||||
free(series);
|
||||
}
|
||||
|
||||
// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
|
||||
void clearSeries(GrayScaleImageSeries *series)
|
||||
{
|
||||
}
|
||||
Reference in new issue
Block a user