Matrix Header korrigiert und imageInput implementiert
This commit is contained in:
1 parent
2088cbecce
commit
fe70850d8b
3 files changed
+123
-6
No files matched your search
+118
-5
@@ -8,15 +8,128 @@
|
||||
|
||||
// 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)
|
||||
static int readHeader(FILE *file)
|
||||
{
|
||||
GrayScaleImageSeries *series = NULL;
|
||||
char header[sizeof(FILE_HEADER_STRING) - 1];
|
||||
|
||||
if (fread(header, 1, sizeof(header), file) != sizeof(header)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return memcmp(header, FILE_HEADER_STRING, sizeof(header)) == 0;
|
||||
}
|
||||
|
||||
static int readDimensions(FILE *file, unsigned int *count, unsigned int *width, unsigned int *height)
|
||||
{
|
||||
unsigned short dimensions[3];
|
||||
|
||||
if (fread(dimensions, sizeof(unsigned short), 3, file) != 3) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
*count = dimensions[0];
|
||||
*width = dimensions[1];
|
||||
*height = dimensions[2];
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int readImage(FILE *file, GrayScaleImage *image)
|
||||
{
|
||||
size_t pixelCount = (size_t)image->width * image->height;
|
||||
|
||||
image->buffer = malloc(pixelCount * sizeof(GrayScalePixelType));
|
||||
|
||||
if (image->buffer == NULL){
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (fread(image->buffer, sizeof(GrayScalePixelType), pixelCount, file) != pixelCount) {
|
||||
free(image->buffer);
|
||||
image->buffer = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int readLabel(FILE *file, unsigned char* label) {
|
||||
|
||||
if (fread(label, sizeof(unsigned char), 1, file) != 1) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
|
||||
GrayScaleImageSeries *readImages(const char *path) {
|
||||
FILE *file = fopen(path, "rb");
|
||||
|
||||
if (file == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Header prüfen
|
||||
if (!readHeader(file)) {
|
||||
fclose(file);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Dimensionen einlesen
|
||||
unsigned int count, width, height;
|
||||
|
||||
if (!readDimensions(file, &count, &width, &height)) {
|
||||
fclose(file);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Bilderserie erstellen
|
||||
GrayScaleImageSeries *series = malloc(sizeof(GrayScaleImageSeries));
|
||||
|
||||
if (series == NULL) {
|
||||
fclose(file);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
series->count = count;
|
||||
series->images = calloc(count, sizeof(GrayScaleImage));
|
||||
series->labels = malloc(count * sizeof(unsigned char));
|
||||
|
||||
if (series->images == NULL || series->labels == NULL) {
|
||||
free(series->images);
|
||||
free(series->labels);
|
||||
free(series);
|
||||
fclose(file);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Alle Bilder einlesen
|
||||
for (unsigned int i = 0; i < count; i++) {
|
||||
series->images[i].width = width;
|
||||
series->images[i].height = height;
|
||||
|
||||
if (!readImage(file, &series->images[i]) || !readLabel(file, &series->labels[i])) {
|
||||
clearSeries(series);
|
||||
fclose(file);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
fclose(file);
|
||||
|
||||
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 (unsigned int i = 0; i < series->count; i++) {
|
||||
free(series->images[i].buffer);
|
||||
}
|
||||
|
||||
free(series->images);
|
||||
free(series->labels);
|
||||
free(series);
|
||||
}
|
||||
Reference in new issue
Block a user