From 8d369579560210434d08b5774c720cbb53077b76 Mon Sep 17 00:00:00 2001 From: tTh Date: Wed, 17 Jul 2024 00:47:19 +0200 Subject: [PATCH] cleanup --- Lib/bmp.c | 68 +++++++++++++++++++++++++------------------------------ 1 file changed, 31 insertions(+), 37 deletions(-) diff --git a/Lib/bmp.c b/Lib/bmp.c index d83f273..27464f2 100644 --- a/Lib/bmp.c +++ b/Lib/bmp.c @@ -19,15 +19,13 @@ #include "bmp.h" /* maybe I can hardcoded bmp.h here ? */ /*::------------------------------------------------------------------::*/ -static void -fatal_error(char *txt) +static void fatal_error(char *txt) { fprintf(stderr, "BMP: Fatal error: %s\n", txt); exit(10); } /*::------------------------------------------------------------------::*/ -int -Image_BMP_infos(char *nom, int *pw, int *ph, int *pt, int verb) +int Image_BMP_infos(char *nom, int *pw, int *ph, int *pt, int verb) { FILE *fp; BMPHEAD head; @@ -35,17 +33,20 @@ int foo; if (verb) printf("BMP_Infos : '%s'\n", nom); -if ((fp=fopen(nom, "r")) == NULL) - { +if ((fp=fopen(nom, "r")) == NULL) { fprintf(stderr, "BMP_Infos: can't open %s\n", nom); return FILE_NOT_FOUND; } foo = fread(&head, 1, sizeof(head), fp); +if (sizeof(head) != foo) { + fprintf(stderr, "%s: err read header of %s\n", __func__, nom); + fclose(fp); + return UNKNOW_ERROR; + } fclose(fp); -if (verb) - { +if (verb) { printf("signature %c%c filesize %8ld\n", head.id[0], head.id[1], head.filesize); printf("headerSize %8ld infoSize %8ld\n", @@ -54,7 +55,7 @@ if (verb) head.width, head.height, head.bits); printf("colors: used %ld important %ld\n", head.clrused, head.clrimportant); - printf("taille structure header %d\n", sizeof(BMPHEAD)); + printf("taille structure header %ld\n", sizeof(BMPHEAD)); } /* * now, return some usefull informations. @@ -70,28 +71,32 @@ return 0; */ #define PIX2BYTE(n) ((n+7)/8) -Image_Desc * -Image_BMP_alloc_load(char *nom, int reserved) +Image_Desc * Image_BMP_alloc_load(char *nom, int reserved) { +(void)reserved; /* WARNING KILLER */ FILE *fp; BMPHEAD head; int ligne, foo, larg, col, ligne2; Image_Desc *image; uint8_t *buffer; -if ((fp=fopen(nom, "r")) == NULL) - { +if ((fp=fopen(nom, "r")) == NULL) { fprintf(stderr, "can't open %s\n", nom); return NULL; } foo = fread(&head, 1, sizeof(head), fp); +if (sizeof(head) != foo) { + fprintf(stderr, "%s: err read header of %s\n", __func__, nom); + fclose(fp); + return NULL; + } + #if DEBUG_LEVEL fprintf(stderr, "BMP: on a lu %d octets pour le header.\n", foo); #endif -if ( head.id[0] != 'B' || head.id[1] != 'M' ) - { +if ( head.id[0] != 'B' || head.id[1] != 'M' ) { fprintf(stderr, "BMP_Alloc_Load: BAD MAGIC %s\n", nom); return NULL; } @@ -103,15 +108,13 @@ fprintf(stderr, "BMP_Alloc_Load: image depth = %d\n", head.bits); /* * first step: allocating the memory. */ -switch (head.bits) - { +switch (head.bits) { case 1: case 4: case 8: fprintf(stderr, "bit depth %d not supported\n", head.bits); return NULL; break; case 24: - if ( (image=Image_alloc(head.width, head.height, 3))==NULL) fatal_error("no memory for picture in 'alloc_load'\n"); @@ -119,7 +122,6 @@ switch (head.bits) fatal_error("no memory for buffer in 'alloc_load'\n"); larg = head.width * 3; - break; default: @@ -132,8 +134,7 @@ switch (head.bits) /* * round up to an even dword boundary (?) */ -if (larg & 0x00000003) - { +if (larg & 0x00000003) { larg |= 0x00000003; larg++; } @@ -143,17 +144,14 @@ if (larg & 0x00000003) * * (no default case, filtered in first step) */ -switch (head.bits) - { +switch (head.bits) { case 24: - for (ligne=0; ligneBpix[ligne2])[col] = buffer[ col*3 ]; (image->Gpix[ligne2])[col] = buffer[ (col*3) + 1 ]; (image->Rpix[ligne2])[col] = buffer[ (col*3) + 2 ]; @@ -172,9 +170,9 @@ return image; * * et cette fois-ci, on va faire gaffe au boutisme :) */ -int -Image_BMP_save_24(char *filename, Image_Desc *img, int flag) +int Image_BMP_save_24(char *filename, Image_Desc *img, int flag) { +(void)flag; /* WARNING KILLER */ FILE *fp; long grand; short court; @@ -187,8 +185,7 @@ fprintf(stderr, "%s : writing %p to %s, flag=%d\n", __func__, img, filename, flag); #endif -if ((fp=fopen(filename, "w")) == NULL) - { +if ((fp=fopen(filename, "w")) == NULL) { fprintf(stderr, "can't open %s for writing\n", filename); return FILE_CREATE_ERR; } @@ -206,8 +203,7 @@ if (bytes & 0x3) bytes++; } */ -switch (bytes & 0x3) - { +switch (bytes & 0x3) { case 0: /* OK */ break; case 1: bytes+=3; break; case 2: bytes+=2; break; @@ -261,14 +257,12 @@ fflush(fp); if ((buffer=(uint8_t *)malloc(sizeof(uint8_t)*4*img->width)) == NULL) fatal_error("no memory buffer for BMP24 save operation"); -for (line=img->height-1; line>=0; line--) - { +for (line=img->height-1; line>=0; line--) { ptr = buffer; Rptr = img->Rpix[line]; Gptr = img->Gpix[line]; Bptr = img->Bpix[line]; - for (foo=0; foowidth; foo++) - { + for (foo=0; foowidth; foo++) { *ptr++ = Bptr[foo]; *ptr++ = Gptr[foo]; *ptr++ = Rptr[foo];