imageInput: Kommntare verbessert
This commit is contained in:
parent
eca1c1e4cb
commit
8605e292fd
68
imageInput.c
68
imageInput.c
@ -7,43 +7,46 @@
|
||||
#define FILE_HEADER_STRING "__info2_image_file_format__"
|
||||
|
||||
|
||||
/*
|
||||
* Diese Hilfsfunktionen kümmern sich um:
|
||||
* - Das Öffnen der Datei und Überprüfen des Dateinamens
|
||||
* - Das Überprüfen des Headers, um sicherzustellen, dass es sich um die richtige Datei handelt
|
||||
* - Das Einlesen der Metadaten (Anzahl der Bilder, Bildbreite und -höhe)
|
||||
* -> Die Hilfsfunktionen garantieren, dass die Datei gültig ist, bevor mit dem Einlesen der Bilddaten fortgefahren wird.
|
||||
|
||||
//HilfsFunktionen:
|
||||
|
||||
|
||||
/* Opens the file in binary mode.
|
||||
* path: file path to open (must not be NULL).
|
||||
* returns FILE* or NULL on failure.
|
||||
*/
|
||||
|
||||
|
||||
static FILE *openImageFile(const char *path) // Checks if the given filename pointer is valid (not NULL).
|
||||
static FILE *openImageFile(const char *path)
|
||||
{
|
||||
if (path == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return fopen(path, "rb"); //opening document in binear
|
||||
return fopen(path, "rb");
|
||||
}
|
||||
|
||||
static int readAndCheckHeader(FILE *file) // gets the length of the header text
|
||||
/* Reads and checks the file header.
|
||||
* file: opened file to read the header from.
|
||||
* returns 1 if header is valid, otherwise 0.
|
||||
*/
|
||||
static int readAndCheckHeader(FILE *file)
|
||||
{
|
||||
size_t headerLength = strlen(FILE_HEADER_STRING);
|
||||
char buffer[BUFFER_SIZE];
|
||||
|
||||
if (headerLength + 1 > BUFFER_SIZE) //checks if buffer is big enough for header size
|
||||
if (headerLength + 1 > BUFFER_SIZE)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (fread(buffer, 1, headerLength, file) != headerLength) // Checks if reading the expected number of header bytes from the file succeeded
|
||||
if (fread(buffer, 1, headerLength, file) != headerLength)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
buffer[headerLength] = '\0'; // add string terminator so the header becomes a valid C-string
|
||||
buffer[headerLength] = '\0';
|
||||
|
||||
if (strcmp(buffer, FILE_HEADER_STRING) != 0) // checks if the expected header matches the header read from the file
|
||||
if (strcmp(buffer, FILE_HEADER_STRING) != 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
@ -52,25 +55,29 @@ static int readAndCheckHeader(FILE *file) // gets the length of the header text
|
||||
}
|
||||
|
||||
|
||||
static int readImageMetaData(FILE *file, // reads the metadata (count, width, height) from the file and stores them in the provided pointers
|
||||
/* Reads metadata: image count, width, and height.
|
||||
* file: file to read from; count/width/height are outputs.
|
||||
* returns 1 on success or 0 on invalid/zero metadata.
|
||||
*/
|
||||
static int readImageMetaData(FILE *file,
|
||||
unsigned short *count,
|
||||
unsigned short *width,
|
||||
unsigned short *height)
|
||||
{
|
||||
if (fread(count, sizeof(unsigned short), 1, file) != 1) // read the number of images from the file
|
||||
if (fread(count, sizeof(unsigned short), 1, file) != 1)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if (fread(width, sizeof(unsigned short), 1, file) != 1) // reads the image width
|
||||
if (fread(width, sizeof(unsigned short), 1, file) != 1)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if (fread(height, sizeof(unsigned short), 1, file) != 1) //reads the image height
|
||||
if (fread(height, sizeof(unsigned short), 1, file) != 1)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (*count == 0 || *width == 0 || *height == 0) // check for invalid metadata (count, width or height cannot be zero)
|
||||
if (*count == 0 || *width == 0 || *height == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
@ -79,9 +86,13 @@ static int readImageMetaData(FILE *file, // reads the metadata (count, width, he
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
Hauptfunktion
|
||||
*/
|
||||
//Hauptfunktion:
|
||||
|
||||
|
||||
/* Loads all images + labels from a .info2 file.
|
||||
* path: file path to load.
|
||||
* returns a new GrayScaleImageSeries or NULL on error.
|
||||
*/
|
||||
GrayScaleImageSeries *readImages(const char *path)
|
||||
{
|
||||
// 1. Open the file
|
||||
@ -110,7 +121,7 @@ GrayScaleImageSeries *readImages(const char *path)
|
||||
}
|
||||
|
||||
// 4. Allocate memory for image series
|
||||
GrayScaleImageSeries *series = (GrayScaleImageSeries *)malloc(sizeof(GrayScaleImageSeries)); // Allocate memory for the image series, images, and labels. Return NULL if allocation fails.
|
||||
GrayScaleImageSeries *series = (GrayScaleImageSeries *)malloc(sizeof(GrayScaleImageSeries));
|
||||
if (series == NULL)
|
||||
{
|
||||
fclose(file);
|
||||
@ -154,7 +165,7 @@ GrayScaleImageSeries *readImages(const char *path)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (fread(image->buffer, sizeof(GrayScalePixelType), numPixels, file) != numPixels) // Check if the correct number of pixel values (width * height) were read for the image
|
||||
if (fread(image->buffer, sizeof(GrayScalePixelType), numPixels, file) != numPixels)
|
||||
{
|
||||
for (unsigned int j = 0; j <= i; j++)
|
||||
{
|
||||
@ -167,7 +178,7 @@ GrayScaleImageSeries *readImages(const char *path)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (fread(&series->labels[i], sizeof(unsigned char), 1, file) != 1) // Check if the label for the image was successfully read (1 byte)
|
||||
if (fread(&series->labels[i], sizeof(unsigned char), 1, file) != 1)
|
||||
{
|
||||
for (unsigned int j = 0; j <= i; j++)
|
||||
{
|
||||
@ -185,7 +196,10 @@ GrayScaleImageSeries *readImages(const char *path)
|
||||
return series;
|
||||
}
|
||||
|
||||
// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
|
||||
/* Frees the entire image series.
|
||||
* series: image series to free (NULL-safe).
|
||||
* returns nothing (void).
|
||||
*/
|
||||
void clearSeries(GrayScaleImageSeries *series)
|
||||
{
|
||||
if (series == NULL)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user