fix Speicherfreigabe im Fehlerfall
This commit is contained in:
parent
e60586a42b
commit
2f6a3a4391
@ -5,30 +5,30 @@
|
||||
|
||||
#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)
|
||||
static int readHeader(FILE *file, unsigned short *count, unsigned short *width, unsigned short *height)
|
||||
{
|
||||
char buffer[HEADER_STRING_LEN + 1];
|
||||
if (fread(buffer, 1, HEADER_STRING_LEN, file) != HEADER_STRING_LEN)
|
||||
unsigned short headerlength = strlen(FILE_HEADER_STRING);
|
||||
char buffer[headerlength + 1];
|
||||
if (fread(buffer, 1, headerlength, file) != headerlength)
|
||||
return 0;
|
||||
|
||||
buffer[HEADER_STRING_LEN] = '\0';
|
||||
buffer[headerlength] = '\0';
|
||||
|
||||
if (strcmp(buffer, FILE_HEADER_STRING) != 0)
|
||||
return 0;
|
||||
|
||||
if (fread(count, sizeof(unsigned int), 1, file) != 1)
|
||||
if (fread(count, sizeof(unsigned short), 1, file) != 1)
|
||||
return 0;
|
||||
|
||||
if (fread(width, sizeof(unsigned int), 1, file) != 1)
|
||||
if (fread(width, sizeof(unsigned short), 1, file) != 1)
|
||||
return 0;
|
||||
|
||||
if (fread(height, sizeof(unsigned int), 1, file) != 1)
|
||||
if (fread(height, sizeof(unsigned short), 1, file) != 1)
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
@ -52,26 +52,42 @@ GrayScaleImageSeries *readImages(const char *path)
|
||||
if (!file)
|
||||
return NULL;
|
||||
|
||||
unsigned int count, w, h;
|
||||
unsigned short count, width, height;
|
||||
|
||||
if (!readHeader(file, &count, &w, &h)) {
|
||||
if (!readHeader(file, &count, &width, &height)) {
|
||||
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));
|
||||
|
||||
if (!series || !series->images || !series->labels)
|
||||
{
|
||||
free(series->images);
|
||||
free(series->labels);
|
||||
free(series);
|
||||
fclose(file);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
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));
|
||||
series->images[i].width = width;
|
||||
series->images[i].height = height;
|
||||
series->images[i].buffer = malloc(width * height * sizeof(GrayScalePixelType));
|
||||
|
||||
//malloc prüfen
|
||||
if (!series->images[i].buffer)
|
||||
{
|
||||
clearSeries(series);
|
||||
fclose(file);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
//Image einlesen + prüfen
|
||||
if (!readSingleImage(file, &series->images[i])) {
|
||||
fclose(file);
|
||||
clearSeries(series);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user