generated from freudenreichan/info2Praktikum-NeuronalesNetz
153 lines
4.1 KiB
C
153 lines
4.1 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__"
|
|
|
|
|
|
/// @brief Reads a value in little-endian format from file
|
|
/// @param openedFile stream FILE, from which to read
|
|
/// @param bytes how many bytes to read
|
|
static unsigned int readLittleEndian(FILE* openedFile, int bytes) {
|
|
unsigned int value = 0;
|
|
|
|
if (openedFile == NULL) return 0;
|
|
|
|
for (int i = 0; i < bytes; i++) {
|
|
int tmp = fgetc(openedFile);
|
|
if (tmp == EOF) return 0;
|
|
|
|
value |= ((unsigned int)tmp) << (i * 8);
|
|
}
|
|
|
|
return value;
|
|
}
|
|
|
|
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
|
|
GrayScaleImageSeries *readImages(const char *path)
|
|
{
|
|
FILE* openFile = fopen(path, "rb");
|
|
|
|
// file Could not be opened/does not exist
|
|
if (openFile == NULL) {
|
|
return NULL;
|
|
}
|
|
|
|
char actualFileTag[strlen(FILE_HEADER_STRING) + 1];
|
|
|
|
size_t tagLength = strlen(FILE_HEADER_STRING);
|
|
if (fread(actualFileTag, 1, tagLength, openFile) != tagLength) {
|
|
fclose(openFile);
|
|
return NULL;
|
|
}
|
|
actualFileTag[tagLength] = '\0';
|
|
|
|
// checks if the files are equal
|
|
if (strcmp(actualFileTag, FILE_HEADER_STRING) != 0) {
|
|
fclose(openFile);
|
|
return NULL;
|
|
}
|
|
|
|
unsigned int numberOfImages = readLittleEndian(openFile, 2);
|
|
|
|
// no Images in series -> No image-series
|
|
if (numberOfImages == 0) {
|
|
fclose(openFile);
|
|
return NULL;
|
|
}
|
|
|
|
unsigned int width = readLittleEndian(openFile, 2);
|
|
unsigned int height = readLittleEndian(openFile, 2);
|
|
|
|
// no height/width --> impossible file
|
|
if(height == 0 || width == 0) {
|
|
fclose(openFile);
|
|
return NULL;
|
|
}
|
|
|
|
//all the starting parameters are set --> the images can be read and stored
|
|
GrayScaleImageSeries* series = malloc(sizeof(GrayScaleImageSeries));
|
|
if (series == NULL) {
|
|
fclose(openFile);
|
|
return NULL;
|
|
}
|
|
|
|
GrayScaleImage* images = malloc(numberOfImages * sizeof(GrayScaleImage));
|
|
unsigned char* labels = malloc(numberOfImages * sizeof(unsigned char));
|
|
|
|
if (images == NULL || labels == NULL) {
|
|
free(images);
|
|
free(labels);
|
|
free(series);
|
|
fclose(openFile);
|
|
return NULL;
|
|
}
|
|
|
|
series->count = 0;
|
|
series->images = images;
|
|
series->labels = labels;
|
|
|
|
for (unsigned int i = 0; i < numberOfImages; i++) {
|
|
// allocating the actual matrix image for each image
|
|
images[i].buffer = malloc(width * height);
|
|
|
|
if (images[i].buffer == NULL) {
|
|
for (unsigned int k = 0; k < i; k++) {
|
|
free(images[k].buffer);
|
|
}
|
|
free(images);
|
|
free(labels);
|
|
free(series);
|
|
fclose(openFile);
|
|
return NULL;
|
|
}
|
|
|
|
images[i].height = height;
|
|
images[i].width = width;
|
|
|
|
if (fread(images[i].buffer, 1, width * height, openFile) != width * height) {
|
|
for (unsigned int k = 0; k <= i; k++) {
|
|
free(images[k].buffer);
|
|
}
|
|
free(images);
|
|
free(labels);
|
|
free(series);
|
|
fclose(openFile);
|
|
return NULL;
|
|
}
|
|
|
|
//rest of the values that only affect the image itself
|
|
int label = fgetc(openFile);
|
|
if (label == EOF) {
|
|
for (unsigned int k = 0; k <= i; k++) {
|
|
free(images[k].buffer);
|
|
}
|
|
free(images);
|
|
free(labels);
|
|
free(series);
|
|
fclose(openFile);
|
|
return NULL;
|
|
}
|
|
series->labels[i] = (unsigned char)label;
|
|
series->count++;
|
|
}
|
|
|
|
fclose(openFile);
|
|
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 (int i = 0; i < series->count; i++) {
|
|
free(series->images[i].buffer);
|
|
}
|
|
|
|
free(series->images);
|
|
free(series->labels);
|
|
free(series);
|
|
} |