98 lines
2.5 KiB
C
98 lines
2.5 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__"
|
|
#define HEADER_STRING_LEN strlen(FILE_HEADER_STRING)
|
|
|
|
// TODO Implementieren Sie geeignete Hilfsfunktionen für das Lesen der Bildserie aus einer Datei
|
|
|
|
/* ---------------- Hilfsfunktionen ---------------- */
|
|
|
|
static int readHeader(FILE *file, unsigned int *count, unsigned int *width, unsigned int *height)
|
|
{
|
|
char buffer[HEADER_STRING_LEN + 1];
|
|
if (fread(buffer, 1, HEADER_STRING_LEN, file) != HEADER_STRING_LEN)
|
|
return 0;
|
|
|
|
buffer[HEADER_STRING_LEN] = '\0';
|
|
|
|
if (strcmp(buffer, FILE_HEADER_STRING) != 0)
|
|
return 0;
|
|
|
|
if (fread(count, sizeof(unsigned int), 1, file) != 1)
|
|
return 0;
|
|
|
|
if (fread(width, sizeof(unsigned int), 1, file) != 1)
|
|
return 0;
|
|
|
|
if (fread(height, sizeof(unsigned int), 1, file) != 1)
|
|
return 0;
|
|
|
|
return 1;
|
|
}
|
|
|
|
static int readSingleImage(FILE *file, GrayScaleImage *image)
|
|
{
|
|
unsigned int pixelCount = image->width * image->height;
|
|
|
|
if (fread(image->buffer, sizeof(GrayScalePixelType), pixelCount, file) != pixelCount)
|
|
return 0;
|
|
|
|
return 1;
|
|
}
|
|
|
|
|
|
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
|
|
GrayScaleImageSeries *readImages(const char *path)
|
|
{
|
|
GrayScaleImageSeries *series = NULL;
|
|
|
|
FILE *file = fopen(path, "rb");
|
|
if (!file)
|
|
return NULL;
|
|
|
|
unsigned int count, w, h;
|
|
|
|
if (!readHeader(file, &count, &w, &h)) {
|
|
fclose(file);
|
|
return NULL;
|
|
}
|
|
|
|
GrayScaleImageSeries *series = malloc(sizeof(GrayScaleImageSeries));
|
|
series->count = count;
|
|
|
|
series->images = malloc(count * sizeof(GrayScaleImage));
|
|
|
|
series->labels = malloc(count * sizeof(unsigned char));
|
|
|
|
for (unsigned int i = 0; i < count; i++)
|
|
{
|
|
series->images[i].width = w;
|
|
series->images[i].height = h;
|
|
series->images[i].buffer = malloc(w * h * sizeof(GrayScalePixelType));
|
|
|
|
if (!readSingleImage(file, &series->images[i])) {
|
|
fclose(file);
|
|
clearSeries(series);
|
|
return NULL;
|
|
}
|
|
|
|
// label einlesen
|
|
if (fread(&series->labels[i], sizeof(unsigned char), 1, file) != 1) {
|
|
fclose(file);
|
|
clearSeries(series);
|
|
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)
|
|
{
|
|
} |