imageInput: Zwischenstand
This commit is contained in:
parent
96b3d72848
commit
0f1802559e
89
imageInput.c
89
imageInput.c
@ -1,4 +1,4 @@
|
|||||||
##include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "imageInput.h"
|
#include "imageInput.h"
|
||||||
@ -6,20 +6,22 @@
|
|||||||
#define BUFFER_SIZE 100
|
#define BUFFER_SIZE 100
|
||||||
#define FILE_HEADER_STRING "__info2_image_file_format__"
|
#define FILE_HEADER_STRING "__info2_image_file_format__"
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Hilfsfunktionen
|
* 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.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
static FILE *openImageFile(const char *path) // Checks if the given filename pointer is valid (not NULL).
|
static FILE *openImageFile(const char *path) // Checks if the given filename pointer is valid (not NULL).
|
||||||
{
|
{
|
||||||
if (path == NULL)
|
if (path == NULL)
|
||||||
{
|
{
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
return fopen(path, "rb"); //opening document in binear
|
return fopen(path, "rb"); // opening document in binary mode
|
||||||
}
|
}
|
||||||
|
|
||||||
static int readAndCheckHeader(FILE *file) // gets the length of the header text
|
static int readAndCheckHeader(FILE *file) // gets the length of the header text
|
||||||
@ -27,20 +29,19 @@ static int readAndCheckHeader(FILE *file) // gets the length of the header text
|
|||||||
size_t headerLength = strlen(FILE_HEADER_STRING);
|
size_t headerLength = strlen(FILE_HEADER_STRING);
|
||||||
char buffer[BUFFER_SIZE];
|
char buffer[BUFFER_SIZE];
|
||||||
|
|
||||||
if (headerLength + 1 > BUFFER_SIZE) //checks if buffer is big enough for header size
|
if (headerLength + 1 > BUFFER_SIZE) // checks if buffer is big enough for header size
|
||||||
{
|
{
|
||||||
return 0;
|
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) // Checks if reading the expected number of header bytes from the file succeeded
|
||||||
|
{
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
buffer[headerLength] = '\0'; // add string terminator so the header becomes a valid C-string
|
buffer[headerLength] = '\0'; // add string terminator so the header becomes a valid C-string
|
||||||
|
|
||||||
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) // checks if the expected header matches the header read from the file
|
||||||
|
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -48,25 +49,81 @@ static int readAndCheckHeader(FILE *file) // gets the length of the header text
|
|||||||
return 1; /* Header ok */
|
return 1; /* Header ok */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static int readImageMetaData(FILE *file, // reads the metadata (count, width, height) from the file and stores them in the provided pointers
|
static int readImageMetaData(FILE *file, // reads the metadata (count, width, height) from the file and stores them in the provided pointers
|
||||||
unsigned short *count,
|
unsigned short *count,
|
||||||
unsigned short *width,
|
unsigned short *width,
|
||||||
unsigned short *height)
|
unsigned short *height)
|
||||||
{
|
{
|
||||||
|
if (fread(count, sizeof(unsigned short), 1, file) != 1) // read the number of images from the file
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (fread(width, sizeof(unsigned short), 1, file) != 1) // reads the image width
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (fread(height, sizeof(unsigned short), 1, file) != 1) // reads the image height
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (*count == 0 || *width == 0 || *height == 0) // check for invalid metadata (count, width or height cannot be zero)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
GrayScaleImageSeries *readImages(const char *path)
|
||||||
|
{
|
||||||
|
// 1. Open the file
|
||||||
|
FILE *file = openImageFile(path);
|
||||||
|
if (file == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Check the header
|
||||||
|
if (!readAndCheckHeader(file)) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Read image metadata
|
||||||
|
unsigned short count, width, height;
|
||||||
|
if (!readImageMetaData(file, &count, &width, &height)) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
// 4. Allocate memory for image series
|
||||||
Hauptfunktion
|
GrayScaleImageSeries *series = malloc(sizeof(GrayScaleImageSeries)); // Allocate memory for the image series, images, and labels. Return NULL if allocation fails.
|
||||||
*/
|
|
||||||
|
|
||||||
|
if (series == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
series->count = count;
|
||||||
|
series->images = calloc(count, sizeof(GrayScaleImage));
|
||||||
|
series->labels = malloc(count * sizeof(unsigned char));
|
||||||
|
if (series->images == NULL || series->labels == NULL) {
|
||||||
|
free(series);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 5. Read the images and labels
|
||||||
|
for (unsigned int i = 0; i < count; i++)
|
||||||
|
{
|
||||||
|
GrayScaleImage *image = &series->images[i];
|
||||||
|
unsigned char *label = &series->labels[i];
|
||||||
|
|
||||||
|
if (fread(image->buffer, sizeof(GrayScalePixelType), width * height, file) != width * height) // Check if the correct number of pixel values (width * height) were read for the image
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fread(label, sizeof(unsigned char), 1, file) != 1) // Check if the label for the image was successfully read (1 byte)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return series;
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user