#include #include #include #include "imageInput.h" #define BUFFER_SIZE 100 #define FILE_HEADER_STRING "__info2_image_file_format__" // TODO: Fehlerfälle prüfen, prüfen auf 0-werten etc. 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); status = 2; goto ending_openAndCheck; } // size_t buf = 0; 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) { size_t bytes = 0; if (!(bytes = fread(bilderAnzahl, (size_t)sizeof(unsigned short), 1, *fd)) || bytes != 1) return 1; return 0; } static int getUChar(FILE **fd, unsigned char *dest) { size_t bytes = 0; if (!(bytes = fread(dest, sizeof(*dest), 1, *fd)) || bytes != 1) return 1; // printf("Read Label: %d\n", *dest); return 0; } static int getPixelData(FILE **fd, const size_t bufSize, GrayScalePixelType *pixelData) { size_t readLen = 0; if (!(readLen = fread((char *)pixelData, bufSize, 1, *fd)) || readLen != 1) 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)); 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)); if (imageLabels == NULL) { fprintf(stderr, "Error allocating Memory for labels\n"); free(images); free(series); fclose(fd); return NULL; } // 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); 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; } 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; } } 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; } void clearSeries(GrayScaleImageSeries *series) { if (series == NULL) return; for (unsigned short i = 0; i < series->count; ++i) { free(series->images[i].buffer); } free(series->images); free(series->labels); free(series); }