Compare commits

..

4 Commits

Author SHA1 Message Date
tTh
8d36957956 cleanup 2024-07-17 00:47:19 +02:00
tTh
b367f42b9e cosmetic 2024-07-16 19:07:37 +02:00
tTh
b264cffb02 hide trace msg 2024-07-16 19:01:12 +02:00
tTh
449705c0ee check error on header read 2024-07-16 18:58:11 +02:00
4 changed files with 66 additions and 64 deletions

View File

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

View File

@ -16,7 +16,9 @@ int Image_dither_Bayer_0(Image_Desc *s, Image_Desc *d, int uh)
{ {
int dx, dy, x, y, r, g, b; int dx, dy, x, y, r, g, b;
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %p %p %d )\n", __func__, s, d, uh); fprintf(stderr, ">>> %s ( %p %p %d )\n", __func__, s, d, uh);
#endif
/* directement copie de la page 389 de "Bitmapped graphics" de /* directement copie de la page 389 de "Bitmapped graphics" de
Steve Rimmer. */ Steve Rimmer. */
@ -63,7 +65,9 @@ int Image_dither_crude(Image_Desc *s, Image_Desc *d, int uh)
int x, y, r, g, b; int x, y, r, g, b;
int som; int som;
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %p %p %d )\n", __func__, s, d, uh); fprintf(stderr, ">>> %s ( %p %p %d )\n", __func__, s, d, uh);
#endif
if (s != d) Image_clear(d, 0, 0, 0); if (s != d) Image_clear(d, 0, 0, 0);
@ -105,14 +109,16 @@ return OLL_KORRECT;
} }
/*::------------------------------------------------------------------::*/ /*::------------------------------------------------------------------::*/
/* /*
* ça ne marche pas très fort, ce truc... * Il ne marche pas tres fort, ce truc...
*/ */
int Image_dither_2x2(Image_Desc *s, Image_Desc *d, int uh) int Image_dither_2x2(Image_Desc *s, Image_Desc *d, int uh)
{ {
int x, y, xm, ym; int x, y, xm, ym;
int r, g, b, v; int r, g, b, v;
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %p %p %d )\n", __func__, s, d, uh); fprintf(stderr, ">>> %s ( %p %p %d )\n", __func__, s, d, uh);
#endif
x = s->width; xm = (x&1) ? (x-1) : x; x = s->width; xm = (x&1) ? (x-1) : x;
y = s->height; ym = (y&1) ? (y-1) : y; y = s->height; ym = (y&1) ? (y-1) : y;
@ -153,7 +159,9 @@ int Image_dither_seuil_random(Image_Desc *s, Image_Desc *d, int uh)
int x, y, r, g, b; int x, y, r, g, b;
int foo; int foo;
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %p %p %d )\n", __func__, s, d, uh); fprintf(stderr, ">>> %s ( %p %p %d )\n", __func__, s, d, uh);
#endif
if ( (foo=Image_compare_desc(s, d)) ) { if ( (foo=Image_compare_desc(s, d)) ) {
fprintf(stderr, "%s: images are differents %d\n", __func__, foo); fprintf(stderr, "%s: images are differents %d\n", __func__, foo);
@ -192,7 +200,9 @@ int x, y, xa, xb, inc, errR, errG, errB;
int r, g, b, dr, dg, db; int r, g, b, dr, dg, db;
int foo; int foo;
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %p %p %d )\n", __func__, s, d, uh); fprintf(stderr, ">>> %s ( %p %p %d )\n", __func__, s, d, uh);
#endif
if ( (foo=Image_compare_desc(s, d)) ) { if ( (foo=Image_compare_desc(s, d)) ) {
fprintf(stderr, "%s: images are differents %d\n", __func__, foo); fprintf(stderr, "%s: images are differents %d\n", __func__, foo);

View File

@ -18,8 +18,7 @@
* Les colors maps sont censees etre compatible avec celles * Les colors maps sont censees etre compatible avec celles
* de FRACTINT, mais il faudrait verifier. * de FRACTINT, mais il faudrait verifier.
*/ */
int int Image_save_color_Map(char *file, char *name, RGB_map *map)
Image_save_color_Map(char *file, char *name, RGB_map *map)
{ {
int foo; int foo;
FILE *fp; FILE *fp;
@ -77,15 +76,16 @@ return OLL_KORRECT;
* What is the 'right thing' to do when we get more than * What is the 'right thing' to do when we get more than
* 256 lines of data ? return an error ? * 256 lines of data ? return an error ?
*/ */
int int Image_load_color_Map(char *file, char *name, RGB_map *where)
Image_load_color_Map(char *file, char *name, RGB_map *where)
{ {
FILE *fp; FILE *fp;
int nbre, r, g, b, foo, errcode; int nbre, r, g, b, foo, errcode;
char buffer[256]; char buffer[256];
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( '%s' '%s' %p )\n", __func__, fprintf(stderr, ">>> %s ( '%s' '%s' %p )\n", __func__,
file, name, where); file, name, where);
#endif
if ( name != NULL ) { if ( name != NULL ) {
if (strlen(name)>IMG_OBJNAME_LEN) return STRING_TOO_LONG; if (strlen(name)>IMG_OBJNAME_LEN) return STRING_TOO_LONG;
@ -94,8 +94,8 @@ if ( name != NULL ) {
else strcpy(where->name, "<noname>"); else strcpy(where->name, "<noname>");
/* /*
* patch du 11 Décembre 2001: on utilise une fonction qui recherche le * patch du 11 Decembre 2001: on utilise une fonction qui recherche le
* fichier dans differents endroits. Cf 'mustopen.c' pour + de détails. * fichier dans differents endroits. Cf 'mustopen.c' pour + de details.
*/ */
if ((fp=Image_must_fopen(file, "r", 0)) == NULL) { if ((fp=Image_must_fopen(file, "r", 0)) == NULL) {
/* safety poke */ /* safety poke */
@ -134,13 +134,12 @@ return errcode;
* *
* XXX voir aussi indexcol.x XXX * XXX voir aussi indexcol.x XXX
*/ */
int int Image_attach_Map(Image_Desc *im, char *nom_map, int flags)
Image_attach_Map(Image_Desc *im, char *nom_map, int flags)
{ {
RGB_map map; RGB_map map;
int foo; int foo;
fprintf(stderr, "Image attach Map: cette fonction n'est pas finie\n"); fprintf(stderr, "%s: cette fonction n'est pas finie\n", __func__);
foo = Image_load_color_Map(nom_map, "", &map); foo = Image_load_color_Map(nom_map, "", &map);
if (foo == 0) { if (foo == 0) {
fprintf(stderr, "Attach Map: foo is zero ?\n"); fprintf(stderr, "Attach Map: foo is zero ?\n");
@ -151,8 +150,7 @@ return FUNC_NOT_FINISH;
/*::------------------------------------------------------------------::*/ /*::------------------------------------------------------------------::*/
/* new 31 Juillet 2000 */ /* new 31 Juillet 2000 */
int int Image_make_random_Map(char *nom, RGB_map *map, int nbre)
Image_make_random_Map(char *nom, RGB_map *map, int nbre)
{ {
int foo; int foo;
@ -176,8 +174,7 @@ return OLL_KORRECT;
/* 21 Sept 2000 /* 21 Sept 2000
Make a 2x2x2 color palette. Make a 2x2x2 color palette.
*/ */
int int Image_make_222_Map(char *nom, RGB_map *map, int noise)
Image_make_222_Map(char *nom, RGB_map *map, int noise)
{ {
int foo, r, g, b; int foo, r, g, b;
@ -206,13 +203,12 @@ return OLL_KORRECT;
/*::------------------------------------------------------------------::*/ /*::------------------------------------------------------------------::*/
/* 15 Mai 2001 /* 15 Mai 2001
* A big classic: the three-sinus color map. * A big classic: the three-sinus color map.
* paramètres: * parametres:
* 0,1,2 R,G,B period * 0,1,2 R,G,B period
* 3,4,5 R,G,B phase * 3,4,5 R,G,B phase
* 6,7 reserved * 6,7 reserved
*/ */
int int Image_palette_3sinus(char *nom, RGB_map *ou, double pars[8])
Image_palette_3sinus(char *nom, RGB_map *ou, double pars[8])
{ {
int foo; int foo;
double dfoo, dra, dga, dba; double dfoo, dra, dga, dba;
@ -241,8 +237,7 @@ return FUNC_IS_BETA;
/*::------------------------------------------------------------------::*/ /*::------------------------------------------------------------------::*/
/* new 18 Dec 2001 /* new 18 Dec 2001
*/ */
int int Image_mix_palettes(RGB_map *p1, RGB_map *p2, RGB_map *d, char *txt, int k)
Image_mix_palettes(RGB_map *p1, RGB_map *p2, RGB_map *d, char *txt, int k)
{ {
int idx, k2; int idx, k2;
int r1, g1, b1, r2, g2, b2; int r1, g1, b1, r2, g2, b2;
@ -254,8 +249,7 @@ fprintf(stderr, "Image mix palette: work in progress...\n");
k2 = 10000 - k; k2 = 10000 - k;
for (idx=0; idx<256; idx++) { for (idx=0; idx<256; idx++) {
if (idx < p1->nbre) if (idx < p1->nbre) {
{
r1 = p1->red[idx]; r1 = p1->red[idx];
g1 = p1->green[idx]; g1 = p1->green[idx];
b1 = p1->blue[idx]; b1 = p1->blue[idx];
@ -290,7 +284,7 @@ return FUNC_IS_BETA;
/*::------------------------------------------------------------------::*/ /*::------------------------------------------------------------------::*/
/* new 01 aout 2008 */ /* new 01 aout 2008 */
/* XXX HACK XXX */ /* XXX HACK XXX */ /* please explain ! */
double round(double); double round(double);
/* XXX HACK XXX */ /* XXX HACK XXX */
@ -400,6 +394,6 @@ for (foo=0; foo<p->nbre; foo++) {
fclose(fp); fclose(fp);
return FULL_NUCKED; return FUNC_IS_BETA;
} }
/*::------------------------------------------------------------------::*/ /*::------------------------------------------------------------------::*/

View File

@ -497,8 +497,7 @@ return OLL_KORRECT;
* que les images RGB. il manque le GRIS !!! * que les images RGB. il manque le GRIS !!!
* ! il manque aussi la decompression ! * ! il manque aussi la decompression !
*/ */
Image_Desc * Image_Desc * Image_TGA_alloc_load(char *nom)
Image_TGA_alloc_load(char *nom)
{ {
Tga_file_header head; Tga_file_header head;
FILE *fp; FILE *fp;
@ -516,6 +515,11 @@ if ( (fp=fopen(nom, "rb")) == NULL )
memset(&head, 0, sizeof(Tga_file_header)); memset(&head, 0, sizeof(Tga_file_header));
foo = Image_TGA_read_header(fp, &head); foo = Image_TGA_read_header(fp, &head);
if (foo) {
fprintf(stderr, "%s: read error %d on header\n", __func__, foo);
fclose(fp);
return NULL;
}
#if DEBUG_LEVEL > 1 #if DEBUG_LEVEL > 1
printf("TGA ALLOC LOAD: read header -> %d\n", foo); printf("TGA ALLOC LOAD: read header -> %d\n", foo);