[Manipulation fichier] Probleme
Bonsoir a tous,
J'essayais de recoder plus ou moins la source
que j'ai vu dans la rubrique "Source" de la section "C".
( )
...un programme qui permet de transformer une image
au format bmp en son negatif.
(Je suis sur windows avec Code::Block)
Mais j'avais un probleme lorsque j'ecrivais dans le fichier.
(dans la fonction "negatif"). Puis apres quelques modifications
plus ou moins hasardeuses le programme s'est mis a fonctionner.
Mon probleme c'est que je ne comprends pas pourquoi il fonctionne.
Donc si quelqu'un pouvait m'expliquer pourquoi mon premier code
ne marche pas et pourquoi le second fonctionne.
Voila le code auquel j'etait arrive mais qui ne marchait pas:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
void negatif(t_infos *infos)
{
int odepth;
unsigned char temp[4];
int nbpix;
FILE *fp;
int i;
fp = infos->fp;
odepth = infos->depth / 8; // Nombre d'octets par pixel.
nbpix = infos->width * infos->height;
for (i = 0; i < nbpix; i++)
{
fread(temp, sizeof(temp[0]), odepth, fp); // je lis odepth octet(s).
temp[0] = OMAX - temp[0]; // j'effectue l'operation pour passer
temp[1] = OMAX - temp[1]; // en negatif. (si je suis sur une image
temp[2] = OMAX - temp[2]; // en 8 bits alors temp[1-2-3] ne me servent
temp[3] = OMAX - temp[3]; // a rien. (Maximum 32 bits).
fseek(fp, -odepth, SEEK_CUR); // je me repositionne.
fwrite(temp, sizeof(temp[0]), odepth, fp); // j'ecris mon/mes octet(s) (odepth).
}
} |
Fonction negatif qui fonctionne (sans que je comprenne):
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
void negatif(t_infos *infos)
{
int odepth;
unsigned char temp[4];
int nbpix;
FILE *fp;
int i;
fp = infos->fp;
odepth = infos->depth / 8; // Nombre d'octets par pixel.
nbpix = infos->width * infos->height;
/* Je met ce fseek a cause du premier fseek de la boucle suivante. */
fseek(fp, odepth, SEEK_CUR);
for (i = 0; i < nbpix; i++)
{
fseek(fp, -odepth, SEEK_CUR); // ------------------------------ <<
fread(temp, sizeof(temp[0]), odepth, fp);
temp[0] = OMAX - temp[0];
temp[1] = OMAX - temp[1];
temp[2] = OMAX - temp[2];
temp[3] = OMAX - temp[3];
fseek(fp, -odepth, SEEK_CUR);
fwrite(temp, sizeof(temp[0]), odepth, fp);
fseek(fp, odepth, SEEK_CUR);
}
} |
Reste du code:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
#include <stdlib.h>
#include <stdio.h>
#define HEADER_FILE_SIZE 14
#define OMAX 255
typedef struct infos
{
FILE *fp;
char sign[2];
int filesize;
int offset;
int headsize;
int width;
int height;
int depth;
int compress;
char buff[3];
} t_infos;
int main()
{
t_infos infos;
infos.fp = fopen("test.bmp", "r+b");
get_header(&infos);
printf("\nTraitement en cours...\n");
negatif(&infos);
printf("Traitement termine.\n");
fclose(infos.fp);
getchar();
return (0);
}
void get_header(t_infos *infos)
{
unsigned char temp[HEADER_FILE_SIZE];
unsigned char *tmp;
/* Recuperation du header du ficher. */
fread(temp, sizeof(temp[0]), HEADER_FILE_SIZE, infos->fp);
infos->sign[0] = temp[0];
infos->sign[1] = temp[1];
infos->filesize = temp[2] + (temp[3] * 256) + (temp[4] * 256 * 256);
infos->offset = temp[10] + (temp[11] * 256);
/* Recuperation du header de l'image. */
tmp = malloc(sizeof(*tmp) * infos->offset);
fread(tmp, sizeof(tmp[0]), (infos->offset - HEADER_FILE_SIZE), infos->fp);
infos->width = tmp[4] + (tmp[5] * 256);
infos->height = tmp[8] + (tmp[9] * 256);
infos->depth = tmp[14];
infos->compress = tmp[16];
} |
J'espere n'avoir rien oublier.
Merci encore.