forked from freudenreichan/info2Praktikum-NeuronalesNetz
fixed error sources in imageInput.c
This commit is contained in:
1 parent
19d6219998
commit
1f08837b74
1 file changed
+48
-14
+48
-14
@@ -13,23 +13,28 @@ static int openAndCheck(FILE **fd, const char *path)
|
|||||||
int status = 0;
|
int status = 0;
|
||||||
const size_t headerLen = sizeof(FILE_HEADER_STRING);
|
const size_t headerLen = sizeof(FILE_HEADER_STRING);
|
||||||
char *headerRead = calloc(1, headerLen);
|
char *headerRead = calloc(1, headerLen);
|
||||||
|
if (headerRead == NULL) {
|
||||||
|
|
||||||
|
fprintf(stderr, "Error: could not allocate Memory for Header check\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
if (!(*fd = fopen(path, "rb")))
|
if (!(*fd = fopen(path, "rb")))
|
||||||
{
|
{
|
||||||
// fprintf(stderr, "Error: could not open file %s\n", path);
|
fprintf(stderr, "Error: could not open file %s\n", path);
|
||||||
status = 2;
|
status = 2;
|
||||||
goto ending_openAndCheck;
|
goto ending_openAndCheck;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!fread(headerRead, headerLen-1, 1, *fd))
|
if (!fread(headerRead, headerLen-1, 1, *fd))
|
||||||
{
|
{
|
||||||
// fprintf(stderr, "Error: could not read file %s\n", path);
|
fprintf(stderr, "Error: could not read file %s\n", path);
|
||||||
status = 1;
|
status = 1;
|
||||||
goto ending_openAndCheck;
|
goto ending_openAndCheck;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strcmp(headerRead, FILE_HEADER_STRING)) // 0 = Keine Abweichung!
|
if (strcmp(headerRead, FILE_HEADER_STRING)) // 0 = Keine Abweichung!
|
||||||
{
|
{
|
||||||
// fprintf(stderr, "Error: %s header mismatch\n", path);
|
fprintf(stderr, "Error: %s header mismatch\n", path);
|
||||||
status = 1;
|
status = 1;
|
||||||
goto ending_openAndCheck;
|
goto ending_openAndCheck;
|
||||||
}
|
}
|
||||||
@@ -103,18 +108,31 @@ static GrayScaleImageSeries *getImageReihe(const char *path, int *status)
|
|||||||
|
|
||||||
// fd is at head of data, read calculated size:
|
// fd is at head of data, read calculated size:
|
||||||
// fprintf(stderr, "Anzahl: %d, Width: %d, Height: %d\n", bilderAnzahl, width, height);
|
// fprintf(stderr, "Anzahl: %d, Width: %d, Height: %d\n", bilderAnzahl, width, height);
|
||||||
|
|
||||||
GrayScaleImageSeries *series = (GrayScaleImageSeries *)calloc(bilderAnzahl, sizeof(GrayScaleImageSeries));
|
GrayScaleImageSeries *series = (GrayScaleImageSeries *)calloc(bilderAnzahl, sizeof(GrayScaleImageSeries));
|
||||||
|
if (series == NULL) {
|
||||||
|
fprintf(stderr, "Error allocating Memory for Image Series\n");
|
||||||
|
fclose(fd);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
GrayScaleImage *images = (GrayScaleImage *)calloc(bilderAnzahl, sizeof(GrayScaleImage));
|
GrayScaleImage *images = (GrayScaleImage *)calloc(bilderAnzahl, sizeof(GrayScaleImage));
|
||||||
|
if (images == NULL) {
|
||||||
|
fprintf(stderr, "Error allocating Memory for images\n");
|
||||||
|
free(series);
|
||||||
|
fclose(fd);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
unsigned char *imageLabels = (unsigned char *)calloc(bilderAnzahl, sizeof(unsigned char));
|
unsigned char *imageLabels = (unsigned char *)calloc(bilderAnzahl, sizeof(unsigned char));
|
||||||
// unsigned char buf;
|
if (imageLabels == NULL) {
|
||||||
// int count = 0;
|
fprintf(stderr, "Error allocating Memory for labels\n");
|
||||||
|
free(images);
|
||||||
|
free(series);
|
||||||
|
fclose(fd);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
// 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
|
// read each image - pixelWerte dann Klasse
|
||||||
for (unsigned short i = 0; i < bilderAnzahl; ++i)
|
for (unsigned short i = 0; i < bilderAnzahl; ++i)
|
||||||
@@ -123,10 +141,23 @@ static GrayScaleImageSeries *getImageReihe(const char *path, int *status)
|
|||||||
images[i].width = width;
|
images[i].width = width;
|
||||||
size_t bufSize = sizeof(GrayScalePixelType) * (width * height);
|
size_t bufSize = sizeof(GrayScalePixelType) * (width * height);
|
||||||
images[i].buffer = (GrayScalePixelType *)calloc(1, bufSize);
|
images[i].buffer = (GrayScalePixelType *)calloc(1, bufSize);
|
||||||
|
if (images[i].buffer == NULL) {
|
||||||
|
*status = 1;
|
||||||
|
free(imageLabels);
|
||||||
|
free(images);
|
||||||
|
free(series);
|
||||||
|
fclose(fd);
|
||||||
|
fprintf(stderr, "Error allocating pixel buffer\n");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
// fprintf(stderr, "Reading %lu Bytes\n", bufSize);
|
// fprintf(stderr, "Reading %lu Bytes\n", bufSize);
|
||||||
if (getPixelData(&fd, bufSize, images[i].buffer))
|
if (getPixelData(&fd, bufSize, images[i].buffer))
|
||||||
{
|
{
|
||||||
*status = 1;
|
*status = 1;
|
||||||
|
free(images[i].buffer);
|
||||||
|
free(imageLabels);
|
||||||
|
free(images);
|
||||||
|
free(series);
|
||||||
fclose(fd);
|
fclose(fd);
|
||||||
fprintf(stderr, "Error reading pixel data\n");
|
fprintf(stderr, "Error reading pixel data\n");
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -135,6 +166,10 @@ static GrayScaleImageSeries *getImageReihe(const char *path, int *status)
|
|||||||
if (getUChar(&fd, &imageLabels[i]))
|
if (getUChar(&fd, &imageLabels[i]))
|
||||||
{
|
{
|
||||||
*status = 1;
|
*status = 1;
|
||||||
|
free(images[i].buffer);
|
||||||
|
free(imageLabels);
|
||||||
|
free(images);
|
||||||
|
free(series);
|
||||||
fclose(fd);
|
fclose(fd);
|
||||||
fprintf(stderr, "Error reading image label\n");
|
fprintf(stderr, "Error reading image label\n");
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -160,16 +195,15 @@ GrayScaleImageSeries *readImages(const char *path)
|
|||||||
|
|
||||||
void clearSeries(GrayScaleImageSeries *series)
|
void clearSeries(GrayScaleImageSeries *series)
|
||||||
{
|
{
|
||||||
// GrayScalePixelType* pixelData = (GrayScalePixelType*)calloc(bilderAnzahl, sizeof(GrayScalePixelType) * width * height);
|
if (series == NULL) return;
|
||||||
|
|
||||||
for (unsigned short i = 0; i < series->count; ++i)
|
for (unsigned short i = 0; i < series->count; ++i)
|
||||||
{
|
{
|
||||||
free(series->images[i].buffer);
|
free(series->images[i].buffer);
|
||||||
}
|
}
|
||||||
free(series->images);
|
free(series->images);
|
||||||
// unsigned char* imageLabels = (unsigned char*)calloc(bilderAnzahl, sizeof(unsigned char));
|
|
||||||
free(series->labels);
|
free(series->labels);
|
||||||
|
|
||||||
// free(series->images[0].buffer);
|
|
||||||
// GrayScaleImageSeries* series = (GrayScaleImageSeries*)calloc(bilderAnzahl, sizeof(GrayScaleImageSeries));
|
|
||||||
free(series);
|
free(series);
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user