forked from freudenreichan/info2Praktikum-NeuronalesNetz
input working
This commit is contained in:
1 parent
229c4c8b65
commit
b755e9caf0
1 file changed
+158
-5
+158
-5
@@ -6,17 +6,170 @@
|
||||
#define BUFFER_SIZE 100
|
||||
#define FILE_HEADER_STRING "__info2_image_file_format__"
|
||||
|
||||
// TODO Implementieren Sie geeignete Hilfsfunktionen für das Lesen der Bildserie aus einer Datei
|
||||
// TODO: Fehlerfälle prüfen, prüfen auf 0-werten etc.
|
||||
|
||||
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
|
||||
GrayScaleImageSeries *readImages(const char *path)
|
||||
static int openAndCheck(FILE **fd, const char *path)
|
||||
{
|
||||
int status = 0;
|
||||
const size_t headerLen = sizeof(FILE_HEADER_STRING);
|
||||
char *headerRead = calloc(1, headerLen);
|
||||
if (!(*fd = fopen(path, "rb")))
|
||||
{
|
||||
// fprintf(stderr, "Error: could not open file %s\n", path);
|
||||
status = 2;
|
||||
goto ending_openAndCheck;
|
||||
}
|
||||
|
||||
if (!fread(headerRead, headerLen-1, 1, *fd))
|
||||
{
|
||||
// fprintf(stderr, "Error: could not read file %s\n", path);
|
||||
status = 1;
|
||||
goto ending_openAndCheck;
|
||||
}
|
||||
|
||||
if (strcmp(headerRead, FILE_HEADER_STRING)) // 0 = Keine Abweichung!
|
||||
{
|
||||
// fprintf(stderr, "Error: %s header mismatch\n", path);
|
||||
status = 1;
|
||||
goto ending_openAndCheck;
|
||||
}
|
||||
ending_openAndCheck:
|
||||
free(headerRead);
|
||||
return status;
|
||||
}
|
||||
|
||||
static int getUShort(FILE **fd, unsigned short *bilderAnzahl)
|
||||
{
|
||||
if (!fread(bilderAnzahl, (size_t)sizeof(unsigned short), 1, *fd))
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int getUChar(FILE **fd, unsigned char *dest)
|
||||
{
|
||||
// unsigned short lab = '0';
|
||||
if (!fread(dest, sizeof(*dest), 1, *fd))
|
||||
return 1;
|
||||
// printf("Read Label: %d\n", *dest);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int getPixelData(FILE **fd, const size_t bufSize, GrayScalePixelType *pixelData)
|
||||
{
|
||||
if (!fread((char *)pixelData, bufSize, 1, *fd))
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static GrayScaleImageSeries *getImageReihe(const char *path, int *status)
|
||||
{
|
||||
FILE *fd = NULL;
|
||||
|
||||
// printf("Opening %s\n", path);
|
||||
|
||||
*status = openAndCheck(&fd, path);
|
||||
if (*status)
|
||||
{
|
||||
if (*status != 2)
|
||||
fclose(fd);
|
||||
;
|
||||
return NULL;
|
||||
}
|
||||
// fd is at head of file, read dimensions:
|
||||
unsigned short bilderAnzahl;
|
||||
unsigned short width;
|
||||
unsigned short height;
|
||||
|
||||
if (getUShort(&fd, &bilderAnzahl))
|
||||
{
|
||||
*status = 1;
|
||||
fclose(fd);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (getUShort(&fd, &width))
|
||||
{
|
||||
*status = 1;
|
||||
fclose(fd);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (getUShort(&fd, &height))
|
||||
{
|
||||
*status = 1;
|
||||
fclose(fd);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// fd is at head of data, read calculated size:
|
||||
// fprintf(stderr, "Anzahl: %d, Width: %d, Height: %d\n", bilderAnzahl, width, height);
|
||||
GrayScaleImageSeries *series = (GrayScaleImageSeries *)calloc(bilderAnzahl, sizeof(GrayScaleImageSeries));
|
||||
GrayScaleImage *images = (GrayScaleImage *)calloc(bilderAnzahl, sizeof(GrayScaleImage));
|
||||
unsigned char *imageLabels = (unsigned char *)calloc(bilderAnzahl, sizeof(unsigned char));
|
||||
// unsigned char buf;
|
||||
// int count = 0;
|
||||
|
||||
// while(!getUChar(&fd, &buf) && buf != 15) {
|
||||
// fprintf(stderr, "Label: %d; Count: %d\n", buf, count);
|
||||
// count++;
|
||||
// }
|
||||
|
||||
// fprintf(stderr, "Label: %d; Count: %d\n", buf, count);
|
||||
|
||||
// read each image - pixelWerte dann Klasse
|
||||
for (unsigned short i = 0; i < bilderAnzahl; ++i)
|
||||
{
|
||||
images[i].height = height;
|
||||
images[i].width = width;
|
||||
size_t bufSize = sizeof(GrayScalePixelType) * (width * height);
|
||||
images[i].buffer = (GrayScalePixelType *)calloc(1, bufSize);
|
||||
// fprintf(stderr, "Reading %lu Bytes\n", bufSize);
|
||||
if (getPixelData(&fd, bufSize, images[i].buffer))
|
||||
{
|
||||
*status = 1;
|
||||
fclose(fd);
|
||||
fprintf(stderr, "Error reading pixel data\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (getUChar(&fd, &imageLabels[i]))
|
||||
{
|
||||
*status = 1;
|
||||
fclose(fd);
|
||||
fprintf(stderr, "Error reading image label\n");
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
series->images = images;
|
||||
series->count = bilderAnzahl;
|
||||
series->labels = imageLabels;
|
||||
|
||||
fclose(fd);
|
||||
return series;
|
||||
}
|
||||
|
||||
GrayScaleImageSeries *readImages(const char *path)
|
||||
{
|
||||
GrayScaleImageSeries *series = NULL;
|
||||
|
||||
int status = 0;
|
||||
series = getImageReihe(path, &status);
|
||||
|
||||
return series;
|
||||
}
|
||||
|
||||
// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
|
||||
void clearSeries(GrayScaleImageSeries *series)
|
||||
{
|
||||
// GrayScalePixelType* pixelData = (GrayScalePixelType*)calloc(bilderAnzahl, sizeof(GrayScalePixelType) * width * height);
|
||||
for (unsigned short i = 0; i < series->count; ++i)
|
||||
{
|
||||
free(series->images[i].buffer);
|
||||
}
|
||||
free(series->images);
|
||||
// unsigned char* imageLabels = (unsigned char*)calloc(bilderAnzahl, sizeof(unsigned char));
|
||||
free(series->labels);
|
||||
|
||||
// free(series->images[0].buffer);
|
||||
// GrayScaleImageSeries* series = (GrayScaleImageSeries*)calloc(bilderAnzahl, sizeof(GrayScaleImageSeries));
|
||||
free(series);
|
||||
}
|
||||
Reference in new issue
Block a user