fioxed errors that failed the first 4 tests - imageInput now done
This commit is contained in:
parent
44d76d7524
commit
7a5b1847f5
98
imageInput.c
98
imageInput.c
@ -3,49 +3,71 @@
|
||||
#include <string.h>
|
||||
#include "imageInput.h"
|
||||
|
||||
#define BUFFER_SIZE 100
|
||||
#define FILE_HEADER_STRING "__info2_image_file_format__"
|
||||
#define FILE_HEADER_LENGTH (sizeof(FILE_HEADER_STRING) - 1U)
|
||||
|
||||
// TODO Implementieren Sie geeignete Hilfsfunktionen für das Lesen der Bildserie aus einer Datei
|
||||
|
||||
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
|
||||
// Reads an entire image series file and returns a heap-allocated descriptor.
|
||||
GrayScaleImageSeries *readImages(const char *path)
|
||||
{
|
||||
GrayScaleImageSeries *series = NULL;
|
||||
|
||||
FILE *file = fopen(path, "rb");
|
||||
|
||||
if(file != NULL)
|
||||
if(file == NULL)
|
||||
{
|
||||
if(checkFileHeader(file))
|
||||
{
|
||||
unsigned int numberOfImages = readDimension(file);
|
||||
unsigned short int width = readDimension(file);
|
||||
unsigned short int height = readDimension(file);
|
||||
series = (GrayScaleImageSeries *)malloc(sizeof(GrayScaleImageSeries));
|
||||
series->images = (GrayScaleImage *)malloc(numberOfImages * sizeof(GrayScaleImage));
|
||||
series->labels = (unsigned char *)malloc(numberOfImages * sizeof(unsigned char));
|
||||
series->count = numberOfImages;
|
||||
for(unsigned int i = 0; i < numberOfImages; i++)
|
||||
{
|
||||
series->images[i] = readImage(file);
|
||||
series->labels[i] = readLabel(file);
|
||||
}
|
||||
fclose(file);
|
||||
return series;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(!checkFileHeader(file))
|
||||
{
|
||||
fclose(file);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
unsigned int numberOfImages = readDimension(file);
|
||||
unsigned short width = readDimension(file);
|
||||
unsigned short height = readDimension(file);
|
||||
|
||||
GrayScaleImageSeries *series = (GrayScaleImageSeries *)malloc(sizeof(GrayScaleImageSeries));
|
||||
if(series == NULL)
|
||||
{
|
||||
fclose(file);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
series->images = (GrayScaleImage *)calloc(numberOfImages, sizeof(GrayScaleImage));
|
||||
series->labels = (unsigned char *)calloc(numberOfImages, sizeof(unsigned char));
|
||||
series->count = numberOfImages;
|
||||
|
||||
if(series->images == NULL || series->labels == NULL)
|
||||
{
|
||||
clearSeries(series);
|
||||
fclose(file);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for(unsigned int i = 0; i < numberOfImages; i++)
|
||||
{
|
||||
series->images[i] = readImage(file, width, height);
|
||||
series->labels[i] = readLabel(file);
|
||||
}
|
||||
|
||||
fclose(file);
|
||||
return series;
|
||||
}
|
||||
|
||||
|
||||
// Validates and consumes the file header tag that identifies the custom format.
|
||||
bool checkFileHeader(FILE *file)
|
||||
{
|
||||
char buffer[BUFFER_SIZE] = {0};
|
||||
fread(buffer, sizeof(char), BUFFER_SIZE, file);
|
||||
return strcmp(buffer, FILE_HEADER_STRING) == 0;
|
||||
char buffer[FILE_HEADER_LENGTH + 1] = {0};
|
||||
size_t bytesRead = fread(buffer, sizeof(char), FILE_HEADER_LENGTH, file);
|
||||
if(bytesRead != FILE_HEADER_LENGTH)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return strncmp(buffer, FILE_HEADER_STRING, FILE_HEADER_LENGTH) == 0;
|
||||
}
|
||||
|
||||
// Reads a single 16-bit dimension (width, height or count of Images) from the file.
|
||||
unsigned short int readDimension(FILE *file)
|
||||
{
|
||||
unsigned short int dimension = 0;
|
||||
@ -53,14 +75,24 @@ unsigned short int readDimension(FILE *file)
|
||||
return dimension;
|
||||
}
|
||||
|
||||
GrayScaleImage readImage(FILE *file)
|
||||
// Reads a single grayscale image of the provided dimensions from the file.
|
||||
GrayScaleImage readImage(FILE *file, unsigned short width, unsigned short height)
|
||||
{
|
||||
GrayScaleImage image;
|
||||
image.buffer = (GrayScalePixelType *)malloc(image.width * image.height * sizeof(GrayScalePixelType));
|
||||
fread(image.buffer, sizeof(GrayScalePixelType), image.width * image.height, file);
|
||||
GrayScaleImage image = {0};
|
||||
image.width = width;
|
||||
image.height = height;
|
||||
|
||||
unsigned int pixelCount = (unsigned int)width * (unsigned int)height;
|
||||
image.buffer = (GrayScalePixelType *)malloc(pixelCount * sizeof(GrayScalePixelType));
|
||||
if(image.buffer != NULL)
|
||||
{
|
||||
fread(image.buffer, sizeof(GrayScalePixelType), pixelCount, file);
|
||||
}
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
// Reads one byte label that belongs to the preceding image.
|
||||
unsigned char readLabel(FILE *file)
|
||||
{
|
||||
unsigned char label = 0;
|
||||
@ -68,7 +100,7 @@ unsigned char readLabel(FILE *file)
|
||||
return label;
|
||||
}
|
||||
|
||||
// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
|
||||
// Releases all heap allocations associated with a GrayScaleImageSeries.
|
||||
void clearSeries(GrayScaleImageSeries *series)
|
||||
{
|
||||
if (series != NULL)
|
||||
|
||||
@ -1,6 +1,9 @@
|
||||
#ifndef IMAGEINPUT_H
|
||||
#define IMAGEINPUT_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
typedef unsigned char GrayScalePixelType;
|
||||
|
||||
typedef struct
|
||||
@ -22,7 +25,7 @@ GrayScaleImageSeries *readImages(const char *path);
|
||||
void clearSeries(GrayScaleImageSeries *series);
|
||||
bool checkFileHeader(FILE *file);
|
||||
unsigned short int readDimension(FILE *file);
|
||||
GrayScaleImage readImage(FILE *file);
|
||||
GrayScaleImage readImage(FILE *file, unsigned short width, unsigned short height);
|
||||
unsigned char readLabel(FILE *file);
|
||||
|
||||
#endif
|
||||
|
||||
@ -1 +0,0 @@
|
||||
some_tag
|
||||
Loading…
x
Reference in New Issue
Block a user