forked from freudenreichan/info2Praktikum-NeuronalesNetz
fixed error sources in imageInput.c
This commit is contained in:
1 parent
19d6219998
commit
1f08837b74
1 file changed
+49
-15
+49
-15
@@ -13,23 +13,28 @@ 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 (headerRead == NULL) {
|
||||
|
||||
fprintf(stderr, "Error: could not allocate Memory for Header check\n");
|
||||
return 1;
|
||||
}
|
||||
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;
|
||||
goto ending_openAndCheck;
|
||||
}
|
||||
|
||||
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;
|
||||
goto ending_openAndCheck;
|
||||
}
|
||||
|
||||
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;
|
||||
goto ending_openAndCheck;
|
||||
}
|
||||
@@ -103,18 +108,31 @@ static GrayScaleImageSeries *getImageReihe(const char *path, int *status)
|
||||
|
||||
// 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));
|
||||
if (series == NULL) {
|
||||
fprintf(stderr, "Error allocating Memory for Image Series\n");
|
||||
fclose(fd);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
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 buf;
|
||||
// int count = 0;
|
||||
if (imageLabels == NULL) {
|
||||
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
|
||||
for (unsigned short i = 0; i < bilderAnzahl; ++i)
|
||||
@@ -123,10 +141,23 @@ static GrayScaleImageSeries *getImageReihe(const char *path, int *status)
|
||||
images[i].width = width;
|
||||
size_t bufSize = sizeof(GrayScalePixelType) * (width * height);
|
||||
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);
|
||||
if (getPixelData(&fd, bufSize, images[i].buffer))
|
||||
{
|
||||
*status = 1;
|
||||
free(images[i].buffer);
|
||||
free(imageLabels);
|
||||
free(images);
|
||||
free(series);
|
||||
fclose(fd);
|
||||
fprintf(stderr, "Error reading pixel data\n");
|
||||
return NULL;
|
||||
@@ -135,6 +166,10 @@ static GrayScaleImageSeries *getImageReihe(const char *path, int *status)
|
||||
if (getUChar(&fd, &imageLabels[i]))
|
||||
{
|
||||
*status = 1;
|
||||
free(images[i].buffer);
|
||||
free(imageLabels);
|
||||
free(images);
|
||||
free(series);
|
||||
fclose(fd);
|
||||
fprintf(stderr, "Error reading image label\n");
|
||||
return NULL;
|
||||
@@ -160,16 +195,15 @@ GrayScaleImageSeries *readImages(const char *path)
|
||||
|
||||
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)
|
||||
{
|
||||
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