Fehlerbehandlung Aktualisiert

This commit is contained in:
Thomas Rauh Desktop 2025-11-16 19:43:30 +01:00
parent e98bc885d9
commit 92744ae015
3 changed files with 66 additions and 16 deletions

View File

@ -12,9 +12,11 @@ static void einzelBild(unsigned char *px_ls, FILE *file, GrayScaleImageSeries *s
bild.height = hoehe; bild.height = hoehe;
bild.width = breite; bild.width = breite;
bild.buffer = malloc(sizeof(GrayScalePixelType)*hoehe*breite); bild.buffer = malloc(sizeof(GrayScalePixelType)*hoehe*breite);
if(bild.buffer == NULL){
return;
}
int anz = fread(px_ls,sizeof(unsigned char), hoehe*breite +1, file); int anz = fread(px_ls,sizeof(unsigned char), hoehe*breite +1, file);
//printf("Test\n%d\n%d\n",anz, (breite*hoehe + 1));
if(anz == (breite*hoehe + 1)){ if(anz == (breite*hoehe + 1)){
series->labels[bildnummer] = px_ls[breite*hoehe]; series->labels[bildnummer] = px_ls[breite*hoehe];
@ -42,16 +44,29 @@ GrayScaleImageSeries *readImages(const char *path)
ausgelesen = malloc(sizeof(char)*BUFFER_SIZE); ausgelesen = malloc(sizeof(char)*BUFFER_SIZE);
if(anzahl_ls == NULL || breite_ls == NULL ||hoehe_ls == NULL || ausgelesen == NULL){
return series;
}
file = fopen(path,"rb"); file = fopen(path,"rb");
if(file==NULL){ //Fehlerbehandlung if(file==NULL){ //Fehlerbehandlung
printf("Fehler bei File-Suche\n"); printf("Fehler bei File-Suche\n");
free(anzahl_ls);
free(breite_ls);
free(hoehe_ls);
free(ausgelesen);
fclose(file); fclose(file);
return series; return series;
} }
fread(ausgelesen,sizeof(char), strlen(FILE_HEADER_STRING), file); fread(ausgelesen,sizeof(unsigned char), strlen(FILE_HEADER_STRING), file);
if(strcmp(ausgelesen,FILE_HEADER_STRING)!=0){ if(memcmp(ausgelesen,FILE_HEADER_STRING,strlen(FILE_HEADER_STRING))!=0){
printf("Kein richtiger File-Header vorhanden\n"); printf("Kein richtiger File-Header vorhanden\n");
free(anzahl_ls);
free(breite_ls);
free(hoehe_ls);
free(ausgelesen);
fclose(file); fclose(file);
return series; return series;
} }
@ -64,16 +79,35 @@ GrayScaleImageSeries *readImages(const char *path)
breite = breite_ls[0]; breite = breite_ls[0];
hoehe = hoehe_ls[0]; hoehe = hoehe_ls[0];
series = malloc(sizeof(GrayScaleImageSeries)*(1+anzahl_ls[0]+anzahl_ls[0]*breite*hoehe)); series = malloc(sizeof(GrayScaleImageSeries)*(1+anzahl_ls[0]+anzahl_ls[0]*breite*hoehe));
if(series == NULL){
return series;
}
series->count = anzahl_ls[0]; series->count = anzahl_ls[0];
//printf("%d\n%d\n%d\n",series->count, breite, hoehe);
series->images = malloc(sizeof(GrayScaleImage)*series->count); series->images = malloc(sizeof(GrayScaleImage)*series->count);
series->labels = malloc(sizeof(unsigned char)*series->count); series->labels = malloc(sizeof(unsigned char)*series->count);
if (series->images == NULL || series->labels == NULL){
free(anzahl_ls);
free(breite_ls);
free(hoehe_ls);
free(ausgelesen);
fclose(file);
return series;
}
} }
} }
} }
px_ls = malloc(sizeof(GrayScalePixelType)*hoehe*breite +1); //Speicheranpassung für Einlese-Buffer px_ls = malloc(sizeof(GrayScalePixelType)*hoehe*breite +1); //Speicheranpassung für Einlese-Buffer
if(px_ls == NULL){
free(anzahl_ls);
free(breite_ls);
free(hoehe_ls);
free(px_ls);
free(ausgelesen);
fclose(file);
return series;
}
for(int bildnummer=0; bildnummer <series->count;bildnummer++){ for(int bildnummer=0; bildnummer <series->count;bildnummer++){
einzelBild(px_ls, file, series, breite, hoehe, bildnummer); einzelBild(px_ls, file, series, breite, hoehe, bildnummer);
@ -100,7 +134,6 @@ void clearSeries(GrayScaleImageSeries *series)
free(series->images[i].buffer); free(series->images[i].buffer);
} }
} }
printf("Test\n");
free(series->labels); free(series->labels);
free(series->images); free(series->images);
free(series); free(series);

View File

@ -13,11 +13,11 @@ static void prepareImageFile(const char *path, unsigned short int width, unsigne
if(file != NULL) if(file != NULL)
{ {
const char *fileTag = "__info2_image_file_format__"; const char *fileTag = "__info2_image_file_format__";
GrayScalePixelType *zeroBuffer = (GrayScalePixelType *)calloc(numberOfImages * width * height, sizeof(GrayScalePixelType)); GrayScalePixelType *zeroBuffer = calloc(width * height, sizeof(GrayScalePixelType));
if(zeroBuffer != NULL) if(zeroBuffer != NULL)
{ {
fwrite(fileTag, sizeof(fileTag[0]), strlen(fileTag), file); fwrite(fileTag, sizeof(const char), strlen(fileTag), file);
fwrite(&numberOfImages, sizeof(numberOfImages), 1, file); fwrite(&numberOfImages, sizeof(numberOfImages), 1, file);
fwrite(&width, sizeof(width), 1, file); fwrite(&width, sizeof(width), 1, file);
fwrite(&height, sizeof(height), 1, file); fwrite(&height, sizeof(height), 1, file);

View File

@ -18,8 +18,9 @@ Matrix createMatrix(unsigned int rows, unsigned int cols) {
} }
matrix.rows = rows; matrix.rows = rows;
matrix.cols = cols; matrix.cols = cols;
memset(matrix.buffer, UNDEFINED_MATRIX_VALUE, rows*cols); for (unsigned int i=0;i<rows*cols;i++){
matrix.buffer[i] = UNDEFINED_MATRIX_VALUE;
}
} }
return matrix; return matrix;
@ -27,17 +28,21 @@ Matrix createMatrix(unsigned int rows, unsigned int cols) {
void clearMatrix(Matrix *matrix) void clearMatrix(Matrix *matrix)
{ {
free(matrix->buffer); if(matrix != NULL){
matrix->rows = 0; if(matrix->buffer != NULL)
matrix->cols = 0; free(matrix->buffer);
matrix->buffer = NULL; matrix->rows = 0;
free(matrix->buffer); matrix->cols = 0;
matrix->buffer = NULL;
}
} }
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{ {
if(rowIdx<(matrix.rows) && colIdx<(matrix.cols)){ if(matrix.buffer != NULL){
matrix.buffer[matrix.cols*rowIdx + colIdx]=value; if(rowIdx<(matrix.rows) && colIdx<(matrix.cols)){
matrix.buffer[matrix.cols*rowIdx + colIdx]=value;
}
} }
} }
@ -59,6 +64,9 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
output.rows = matrix1.rows; output.rows = matrix1.rows;
output.cols = matrix1.cols; output.cols = matrix1.cols;
output.buffer = malloc(sizeof(MatrixType)*output.rows*output.cols); output.buffer = malloc(sizeof(MatrixType)*output.rows*output.cols);
if(output.buffer == NULL){
return output;
}
for (int i=0;i<matrix1.rows*matrix1.cols;i++){ for (int i=0;i<matrix1.rows*matrix1.cols;i++){
output.buffer[i]= matrix1.buffer[i]+matrix2.buffer[i]; output.buffer[i]= matrix1.buffer[i]+matrix2.buffer[i];
} }
@ -67,6 +75,9 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
output.rows = matrix1.rows; output.rows = matrix1.rows;
output.cols = matrix1.cols; output.cols = matrix1.cols;
output.buffer = malloc(sizeof(MatrixType)*output.rows*output.cols); output.buffer = malloc(sizeof(MatrixType)*output.rows*output.cols);
if(output.buffer == NULL){
return output;
}
for (int i=0;i<matrix2.rows;i++){ for (int i=0;i<matrix2.rows;i++){
for (int ii=0;ii<matrix1.cols;ii++){ for (int ii=0;ii<matrix1.cols;ii++){
output.buffer[(i*matrix1.cols)+ii]= matrix1.buffer[(i*matrix1.cols)+ii]+matrix2.buffer[i]; output.buffer[(i*matrix1.cols)+ii]= matrix1.buffer[(i*matrix1.cols)+ii]+matrix2.buffer[i];
@ -77,6 +88,9 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
output.rows = matrix2.rows; output.rows = matrix2.rows;
output.cols = matrix2.cols; output.cols = matrix2.cols;
output.buffer = malloc(sizeof(MatrixType)*output.rows*output.cols); output.buffer = malloc(sizeof(MatrixType)*output.rows*output.cols);
if(output.buffer == NULL){
return output;
}
for (int i=0;i<matrix1.rows;i++){ for (int i=0;i<matrix1.rows;i++){
for (int ii=0;ii<matrix2.cols;ii++){ for (int ii=0;ii<matrix2.cols;ii++){
output.buffer[(i*matrix2.cols)+ii]= matrix2.buffer[(i*matrix2.cols)+ii]+matrix1.buffer[i]; output.buffer[(i*matrix2.cols)+ii]= matrix2.buffer[(i*matrix2.cols)+ii]+matrix1.buffer[i];
@ -93,6 +107,9 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
output.rows = matrix1.rows; output.rows = matrix1.rows;
output.cols = matrix2.cols; output.cols = matrix2.cols;
output.buffer = malloc(sizeof(MatrixType)*output.rows*output.cols); output.buffer = malloc(sizeof(MatrixType)*output.rows*output.cols);
if(output.buffer == NULL){
return output;
}
for (int spalte2=0;spalte2<output.cols;spalte2++){ for (int spalte2=0;spalte2<output.cols;spalte2++){
for (int zeile1=0;zeile1<matrix1.rows;zeile1++){ for (int zeile1=0;zeile1<matrix1.rows;zeile1++){
for (int zeile2=0;zeile2<matrix2.rows;zeile2++){ for (int zeile2=0;zeile2<matrix2.rows;zeile2++){