fix Speicherfreigabe im Fehlerfall
This commit is contained in:
parent
e60586a42b
commit
2f6a3a4391
@ -5,30 +5,30 @@
|
|||||||
|
|
||||||
#define BUFFER_SIZE 100
|
#define BUFFER_SIZE 100
|
||||||
#define FILE_HEADER_STRING "__info2_image_file_format__"
|
#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
|
// TODO Implementieren Sie geeignete Hilfsfunktionen für das Lesen der Bildserie aus einer Datei
|
||||||
|
|
||||||
/* ---------------- Hilfsfunktionen ---------------- */
|
/* ---------------- 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];
|
unsigned short headerlength = strlen(FILE_HEADER_STRING);
|
||||||
if (fread(buffer, 1, HEADER_STRING_LEN, file) != HEADER_STRING_LEN)
|
char buffer[headerlength + 1];
|
||||||
|
if (fread(buffer, 1, headerlength, file) != headerlength)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
buffer[HEADER_STRING_LEN] = '\0';
|
buffer[headerlength] = '\0';
|
||||||
|
|
||||||
if (strcmp(buffer, FILE_HEADER_STRING) != 0)
|
if (strcmp(buffer, FILE_HEADER_STRING) != 0)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
if (fread(count, sizeof(unsigned int), 1, file) != 1)
|
if (fread(count, sizeof(unsigned short), 1, file) != 1)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
if (fread(width, sizeof(unsigned int), 1, file) != 1)
|
if (fread(width, sizeof(unsigned short), 1, file) != 1)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
if (fread(height, sizeof(unsigned int), 1, file) != 1)
|
if (fread(height, sizeof(unsigned short), 1, file) != 1)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
@ -52,26 +52,42 @@ GrayScaleImageSeries *readImages(const char *path)
|
|||||||
if (!file)
|
if (!file)
|
||||||
return NULL;
|
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);
|
fclose(file);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
GrayScaleImageSeries *series = malloc(sizeof(GrayScaleImageSeries));
|
GrayScaleImageSeries *series = malloc(sizeof(GrayScaleImageSeries));
|
||||||
series->count = count;
|
series->count = count;
|
||||||
|
|
||||||
series->images = malloc(count * sizeof(GrayScaleImage));
|
series->images = malloc(count * sizeof(GrayScaleImage));
|
||||||
|
|
||||||
series->labels = malloc(count * sizeof(unsigned char));
|
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++)
|
for (unsigned int i = 0; i < count; i++)
|
||||||
{
|
{
|
||||||
series->images[i].width = w;
|
series->images[i].width = width;
|
||||||
series->images[i].height = h;
|
series->images[i].height = height;
|
||||||
series->images[i].buffer = malloc(w * h * sizeof(GrayScalePixelType));
|
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])) {
|
if (!readSingleImage(file, &series->images[i])) {
|
||||||
fclose(file);
|
fclose(file);
|
||||||
clearSeries(series);
|
clearSeries(series);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user