forked from freudenreichan/info2Praktikum-NeuronalesNetz
153 lines
4.7 KiB
C
153 lines
4.7 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
|
|
//Hilfsfunktion zum Einlesen eines einzigen Bildes
|
|
static int read_single_image(FILE *file, unsigned short width, unsigned short height, GrayScaleImage *img, unsigned char *out_label)
|
|
{
|
|
size_t pixels_to_read = (size_t)width * height;
|
|
|
|
img->width = width; //save width and height read from file in struct
|
|
img->height = height;
|
|
|
|
img->buffer = malloc(pixels_to_read * sizeof(GrayScalePixelType)); //allocating needed memory for pixel-buffer
|
|
if(img->buffer == NULL) //Error case
|
|
{
|
|
printf("%s", "Fehler bei der Speicherreservierung!\n");
|
|
return 0;
|
|
}
|
|
|
|
|
|
if(fread(img->buffer, sizeof(GrayScalePixelType), pixels_to_read, file) != pixels_to_read) //read all pixels one after another
|
|
{
|
|
free(img->buffer); //Error case: free allocated memory
|
|
return 0;
|
|
}
|
|
|
|
|
|
if(fread(out_label, sizeof(unsigned char), 1, file) != 1) //read label
|
|
{
|
|
free(img->buffer); //Error case: free allocated memory
|
|
return 0;
|
|
}
|
|
|
|
return 1;
|
|
}
|
|
|
|
|
|
|
|
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
|
|
GrayScaleImageSeries *readImages(const char *path)
|
|
{
|
|
char buffer[BUFFER_SIZE];
|
|
unsigned short num_images;
|
|
unsigned short width;
|
|
unsigned short height;
|
|
|
|
|
|
|
|
FILE *file = fopen(path, "rb");
|
|
|
|
if(file == NULL) //file can not be opened
|
|
{
|
|
return NULL;
|
|
}
|
|
|
|
/*********************************************************************************
|
|
* VERIFY IF HEADER FILE IS INCLUDED
|
|
********************************************************************************/
|
|
fread(buffer, sizeof(buffer[0]), strlen(FILE_HEADER_STRING), file);
|
|
buffer[strlen(FILE_HEADER_STRING)] = '\0';
|
|
|
|
if(strcmp(buffer, FILE_HEADER_STRING) != 0) //file does not include file header
|
|
{
|
|
fclose(file);
|
|
return NULL;
|
|
}
|
|
|
|
/*********************************************************************************
|
|
* READ AMOUNT, WIDTH, HEIGHT
|
|
********************************************************************************/
|
|
fread(&num_images, sizeof(num_images), 1, file);
|
|
fread(&width, sizeof(width), 1, file);
|
|
fread(&height, sizeof(height), 1, file);
|
|
|
|
|
|
/*********************************************************************************
|
|
* MEMORY ALLOCATION FOR SERIES
|
|
********************************************************************************/
|
|
GrayScaleImageSeries *series = malloc(sizeof(GrayScaleImageSeries));
|
|
if(series == NULL) //Error: memory cannot be allocated; close file
|
|
{
|
|
fclose(file);
|
|
return NULL;
|
|
}
|
|
|
|
series->count = num_images; //save number of images in struct
|
|
series->images = malloc(num_images * sizeof(GrayScaleImage)); //allocate memory for all images
|
|
series->labels = malloc(num_images * sizeof(unsigned char)); //allocate memory for all labels
|
|
|
|
if(series->images == NULL || series->labels == NULL) //Error: memory cannot be allocated: free every memory
|
|
{
|
|
free(series->images);
|
|
free(series->labels);
|
|
free(series);
|
|
|
|
fclose(file);
|
|
return NULL;
|
|
}
|
|
|
|
|
|
/*********************************************************************************
|
|
* READ IMAGES
|
|
********************************************************************************/
|
|
|
|
for(int i = 0; i < num_images; i++)
|
|
{
|
|
if(read_single_image(file, width, height, &series->images[i], &series->labels[i]) != 1)
|
|
{
|
|
series->count = i;
|
|
|
|
clearSeries(series); //free all allocated memory
|
|
|
|
fclose(file); //Error case: close 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) //pointer points to nothing; nothing to clear
|
|
{
|
|
return;
|
|
}
|
|
|
|
if(series->images != NULL) //check if image pointer is valid
|
|
{
|
|
for(int i = 0; i < series->count; i++) //free memory allocated for all images until error occured
|
|
{
|
|
free(series->images[i].buffer);
|
|
}
|
|
free(series->images); //free memory allocated for image array
|
|
}
|
|
|
|
if(series->labels != NULL) //check if label pointer is valid
|
|
{
|
|
free(series->labels); //free memory allocated for label array
|
|
}
|
|
|
|
free(series); //free memory allocated for series
|
|
} |