generated from freudenreichan/info2Praktikum-NeuronalesNetz
207 lines
4.2 KiB
C
207 lines
4.2 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "imageInput.h"
|
|
|
|
//#define BUFFER_SIZE 100 // wird nicht verwendet!
|
|
#define FILE_HEADER_STRING "__info2_image_file_format__"
|
|
|
|
// Hilfsfunktionen
|
|
static FILE *openImageFile(const char *path); // Datei öffnen
|
|
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 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;
|
|
|
|
// Datei öffnen
|
|
FILE *file = openImageFile(path);
|
|
|
|
if (file == NULL)
|
|
{
|
|
return NULL;
|
|
}
|
|
|
|
// Header prüfen
|
|
if (!checkFileHeader(file))
|
|
{
|
|
printf("Keine geeignete Datei\n");
|
|
fclose(file);
|
|
return NULL;
|
|
}
|
|
|
|
unsigned short count;
|
|
unsigned short width;
|
|
unsigned short height;
|
|
|
|
// Anzahl Bilder, Breite und Höhe lesen
|
|
if (!readData(file, &count, &width, &height))
|
|
{
|
|
fclose(file);
|
|
return NULL;
|
|
}
|
|
|
|
// Speicher für Bildserie reservieren
|
|
series = createSeries(count);
|
|
|
|
if (series == NULL)
|
|
{
|
|
fclose(file);
|
|
return NULL;
|
|
}
|
|
|
|
// Bilder und Labels einlesen
|
|
if (!readImagesFromFile(file, series, width, height))
|
|
{
|
|
clearSeries(series);
|
|
fclose(file);
|
|
return NULL;
|
|
}
|
|
|
|
fclose(file);
|
|
|
|
return series;
|
|
}
|
|
|
|
|
|
void clearSeries(GrayScaleImageSeries *series)
|
|
{
|
|
if (series == NULL)
|
|
{
|
|
return;
|
|
}
|
|
|
|
for (unsigned int i = 0; i < series->count; i++)
|
|
{
|
|
free(series->images[i].buffer);
|
|
}
|
|
|
|
free(series->images);
|
|
free(series->labels);
|
|
free(series);
|
|
}
|
|
|
|
|
|
static FILE *openImageFile(const char *path)
|
|
{
|
|
FILE *file = fopen(path, "rb");
|
|
|
|
if (file == NULL)
|
|
{
|
|
printf("Fehler: Datei %s konnte nicht geoeffnet werden\n", path);
|
|
return NULL;
|
|
}
|
|
|
|
return file;
|
|
}
|
|
|
|
|
|
static int checkFileHeader(FILE *file)
|
|
{
|
|
size_t len = strlen(FILE_HEADER_STRING);
|
|
|
|
char buffer[sizeof(FILE_HEADER_STRING)];
|
|
|
|
size_t reading = fread(buffer, sizeof(char), len, file);
|
|
|
|
if (reading != len)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
buffer[len] = '\0';
|
|
|
|
if (strcmp(buffer, FILE_HEADER_STRING) == 0)
|
|
{
|
|
return 1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
static GrayScaleImageSeries *createSeries(unsigned short count)
|
|
{
|
|
GrayScaleImageSeries *series =
|
|
malloc(sizeof(GrayScaleImageSeries));
|
|
|
|
if (series == 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;
|
|
} |