Implement ImageInput

This commit is contained in:
dotterweichlu103910 committed 2026-11-11 21:16:28 +01:00
1 parent c26c5247e3
commit 682415e76e
1 file changed
+43 -31
+43 -31
View File
@@ -19,63 +19,76 @@ static int controlFile(FILE *file) {
static unsigned int readNumber(FILE *file) { static unsigned int readNumber(FILE *file) {
unsigned int number = 0; unsigned int number = 0;
fread(&number, sizeof(unsigned short), 1, file); unsigned int readcount = fread(&number, sizeof(unsigned short), 1, file);
if (readcount != 1) {
return 0;
}
return number; return number;
} }
// fillBuffer und readLabel muss immer in einer Schleife zusammen stehen // fillBuffer und readLabel muss immer in einer Schleife zusammen stehen
static int fillBuffer(unsigned char buffer[], unsigned int bufferSize, FILE *file) { static int fillBuffer(unsigned char buffer[], unsigned int bufferSize, FILE *file) {
int readcount = fread(buffer, sizeof(GrayScalePixelType), bufferSize, file); unsigned int readcount = fread(buffer, sizeof(GrayScalePixelType), bufferSize, file);
if (readcount == 0) if (readcount == 0) {
return 0; return 0;
}
return 1; return 1;
} }
static void readLabels(FILE *file, unsigned char label[], unsigned int count) { static int readLabels(FILE *file, unsigned char label[], unsigned int count) {
fread(label + count, sizeof(char), 1, file); unsigned int readlabels = fread(label + count, sizeof(char), 1, file);
if (readlabels == 0) {
return 0;
}
return 1;
} }
GrayScaleImageSeries *readImages(const char *path) { GrayScaleImageSeries *readImages(const char *path) {
FILE *file = fopen(path, "rb"); FILE *file = fopen(path, "rb");
if (!controlFile(file)) { if (!controlFile(file)) {
return NULL; return NULL;
} }
GrayScaleImageSeries *firstSeries = malloc(sizeof(GrayScaleImageSeries)); GrayScaleImageSeries *series = malloc(sizeof(GrayScaleImageSeries));
firstSeries ->count = readNumber(file); series->count = readNumber(file);
unsigned int width = readNumber(file); unsigned int width = readNumber(file);
unsigned int height = readNumber(file); unsigned int height = readNumber(file);
if (width == 0 || height == 0 || series->count == 0 || series == NULL) {
firstSeries ->labels = malloc(firstSeries ->count*sizeof(unsigned char));
firstSeries ->images = malloc(firstSeries ->count*sizeof(GrayScaleImage));
firstSeries ->addrSourceTxt = file;
if (firstSeries ->labels == NULL || firstSeries ->images == NULL) {
return NULL; return NULL;
} }
int fault = 1; // Füllen des structs series
unsigned int const bufferSize = width * height; series->labels = malloc(series->count * sizeof(unsigned char));
for (unsigned int i = 0; i < firstSeries ->count && fault; i++) series->images = malloc(series->count * sizeof(GrayScaleImage));
{ series->addrSourceTxt = file;
GrayScaleImage *grayScaleImage = &firstSeries ->images[i]; if (series->labels == NULL || series->images == NULL) {
grayScaleImage ->buffer = malloc(width * height*sizeof(GrayScalePixelType)); return NULL;
grayScaleImage ->width = width;
grayScaleImage -> height = height;
if (width == 0 || height == 0 || firstSeries ->count == 0) { // prüfen ob eingabe richtig ist
return NULL;
}
fault = fillBuffer(grayScaleImage ->buffer, bufferSize, file);
readLabels(file, firstSeries ->labels, i);
} }
return firstSeries; // struct grayScaleImage füllen als Teil eines Arrays innerhalb series
int readBufferFault = 1;
int readLabelFault = 1;
unsigned int const bufferSize = width * height;
for (unsigned int i = 0; i < series
->count && readBufferFault && readLabelFault; i++) {
GrayScaleImage *grayScaleImage = &series->images[i];
grayScaleImage->buffer = calloc(bufferSize, width * height * sizeof(GrayScalePixelType));
grayScaleImage->width = width;
grayScaleImage->height = height;
readBufferFault = fillBuffer(grayScaleImage->buffer, bufferSize, file);// frage an tobi kann ich readbufferfault auch schöner initialisieren
readLabelFault = readLabels(file, series->labels, i);
if (grayScaleImage->buffer == NULL) {
return NULL;
}
}
return series;
} }
void clearSeries(GrayScaleImageSeries *series) { void clearSeries(GrayScaleImageSeries *series) {
if (series == NULL) {return;} if (series == NULL) { return; }
if (series->images != NULL) { if (series->images != NULL) {
for (unsigned int i = 0; i < series->count; i++) { for (unsigned int i = 0; i < series->count; i++) {
const GrayScaleImage image = series->images[i]; const GrayScaleImage image = series->images[i];
@@ -91,4 +104,3 @@ void clearSeries(GrayScaleImageSeries *series) {
fclose(series->addrSourceTxt); fclose(series->addrSourceTxt);
free(series); free(series);
} }