135 lines
3.2 KiB
C
135 lines
3.2 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include "imageInput.h"
|
|
|
|
#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
|
|
|
|
static int readHeader(FILE *file)
|
|
{
|
|
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) {
|
|
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);
|
|
} |