Finish imageInput implementation (header check, metadata, image buffers) — all tests passing
This commit is contained in:
parent
0f1802559e
commit
eca1c1e4cb
129
imageInput.c
129
imageInput.c
@ -6,6 +6,7 @@
|
||||
#define BUFFER_SIZE 100
|
||||
#define FILE_HEADER_STRING "__info2_image_file_format__"
|
||||
|
||||
|
||||
/*
|
||||
* Diese Hilfsfunktionen kümmern sich um:
|
||||
* - Das Öffnen der Datei und Überprüfen des Dateinamens
|
||||
@ -13,7 +14,8 @@
|
||||
* - 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).
|
||||
{
|
||||
if (path == NULL)
|
||||
@ -21,7 +23,7 @@ static FILE *openImageFile(const char *path) // Checks if the given filename poi
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return fopen(path, "rb"); // opening document in binary mode
|
||||
return fopen(path, "rb"); //opening document in binear
|
||||
}
|
||||
|
||||
static int readAndCheckHeader(FILE *file) // gets the length of the header text
|
||||
@ -29,7 +31,7 @@ static int readAndCheckHeader(FILE *file) // gets the length of the header text
|
||||
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) //checks if buffer is big enough for header size
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
@ -49,6 +51,7 @@ static int readAndCheckHeader(FILE *file) // gets the length of the header text
|
||||
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
|
||||
unsigned short *count,
|
||||
unsigned short *width,
|
||||
@ -62,12 +65,12 @@ static int readImageMetaData(FILE *file, // reads the metadata (count, width, he
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if (fread(height, sizeof(unsigned short), 1, file) != 1) // reads the image height
|
||||
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)
|
||||
if (*count == 0 || *width == 0 || *height == 0) // check for invalid metadata (count, width or height cannot be zero)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
@ -75,36 +78,55 @@ static int readImageMetaData(FILE *file, // reads the metadata (count, width, he
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
Hauptfunktion
|
||||
*/
|
||||
GrayScaleImageSeries *readImages(const char *path)
|
||||
{
|
||||
// 1. Open the file
|
||||
FILE *file = openImageFile(path);
|
||||
if (file == NULL) {
|
||||
if (file == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// 2. Check the header
|
||||
if (!readAndCheckHeader(file)) {
|
||||
if (!readAndCheckHeader(file))
|
||||
{
|
||||
fclose(file);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// 3. Read image metadata
|
||||
unsigned short count, width, height;
|
||||
if (!readImageMetaData(file, &count, &width, &height)) {
|
||||
unsigned short count = 0;
|
||||
unsigned short width = 0;
|
||||
unsigned short height = 0;
|
||||
|
||||
if (!readImageMetaData(file, &count, &width, &height))
|
||||
{
|
||||
fclose(file);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// 4. Allocate memory for image series
|
||||
GrayScaleImageSeries *series = malloc(sizeof(GrayScaleImageSeries)); // Allocate memory for the image series, images, and labels. Return NULL if allocation fails.
|
||||
|
||||
if (series == NULL) {
|
||||
GrayScaleImageSeries *series = (GrayScaleImageSeries *)malloc(sizeof(GrayScaleImageSeries)); // Allocate memory for the image series, images, and labels. Return NULL if allocation fails.
|
||||
if (series == NULL)
|
||||
{
|
||||
fclose(file);
|
||||
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) {
|
||||
|
||||
series->count = count;
|
||||
series->images = (GrayScaleImage *)calloc(count, sizeof(GrayScaleImage));
|
||||
series->labels = (unsigned char *)malloc(count * sizeof(unsigned char));
|
||||
|
||||
if (series->images == NULL || series->labels == NULL)
|
||||
{
|
||||
free(series->images);
|
||||
free(series->labels);
|
||||
free(series);
|
||||
fclose(file);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@ -112,18 +134,81 @@ GrayScaleImageSeries *readImages(const char *path)
|
||||
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
|
||||
image->width = (unsigned int)width;
|
||||
image->height = (unsigned int)height;
|
||||
|
||||
size_t numPixels = (size_t)width * (size_t)height;
|
||||
|
||||
image->buffer = (GrayScalePixelType *)malloc(numPixels * sizeof(GrayScalePixelType));
|
||||
if (image->buffer == NULL)
|
||||
{
|
||||
return 0;
|
||||
for (unsigned int j = 0; j < i; j++)
|
||||
{
|
||||
free(series->images[j].buffer);
|
||||
}
|
||||
free(series->images);
|
||||
free(series->labels);
|
||||
free(series);
|
||||
fclose(file);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (fread(label, sizeof(unsigned char), 1, file) != 1) // Check if the label for the image was successfully read (1 byte)
|
||||
if (fread(image->buffer, sizeof(GrayScalePixelType), numPixels, file) != numPixels) // Check if the correct number of pixel values (width * height) were read for the image
|
||||
{
|
||||
return 0;
|
||||
for (unsigned int j = 0; j <= i; j++)
|
||||
{
|
||||
free(series->images[j].buffer);
|
||||
}
|
||||
free(series->images);
|
||||
free(series->labels);
|
||||
free(series);
|
||||
fclose(file);
|
||||
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)
|
||||
{
|
||||
for (unsigned int j = 0; j <= i; j++)
|
||||
{
|
||||
free(series->images[j].buffer);
|
||||
}
|
||||
free(series->images);
|
||||
free(series->labels);
|
||||
free(series);
|
||||
fclose(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)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (series->images != NULL)
|
||||
{
|
||||
for (unsigned int i = 0; i < series->count; i++)
|
||||
{
|
||||
free(series->images[i].buffer);
|
||||
series->images[i].buffer = NULL;
|
||||
}
|
||||
free(series->images);
|
||||
series->images = NULL;
|
||||
}
|
||||
|
||||
if (series->labels != NULL)
|
||||
{
|
||||
free(series->labels);
|
||||
series->labels = NULL;
|
||||
}
|
||||
|
||||
free(series);
|
||||
}
|
||||
|
||||
BIN
runImageInputTests
Executable file
BIN
runImageInputTests
Executable file
Binary file not shown.
20
runImageInputTests.dSYM/Contents/Info.plist
Normal file
20
runImageInputTests.dSYM/Contents/Info.plist
Normal file
@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>CFBundleDevelopmentRegion</key>
|
||||
<string>English</string>
|
||||
<key>CFBundleIdentifier</key>
|
||||
<string>com.apple.xcode.dsym.runImageInputTests</string>
|
||||
<key>CFBundleInfoDictionaryVersion</key>
|
||||
<string>6.0</string>
|
||||
<key>CFBundlePackageType</key>
|
||||
<string>dSYM</string>
|
||||
<key>CFBundleSignature</key>
|
||||
<string>????</string>
|
||||
<key>CFBundleShortVersionString</key>
|
||||
<string>1.0</string>
|
||||
<key>CFBundleVersion</key>
|
||||
<string>1</string>
|
||||
</dict>
|
||||
</plist>
|
||||
Binary file not shown.
@ -0,0 +1,5 @@
|
||||
---
|
||||
triple: 'arm64-apple-darwin'
|
||||
binary-path: runImageInputTests
|
||||
relocations: []
|
||||
...
|
||||
Loading…
x
Reference in New Issue
Block a user