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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
|
#include <stdio.h>
#include <stdlib.h>
/* 0=normal 1=debug */
#define DBG 1
#define HEADER_SIZE 54
struct taille
{
unsigned long filesize; /* [2] 32-bit */
unsigned long offset; /* [10] 32-bit */
unsigned long headersize; /* [14] 32-bit */
unsigned long largeur; /* [18] 32-bit */
unsigned long hauteur; /* [22] 32-bit */
unsigned short planes; /* [26] 16-bit */
unsigned short bit_per_pix; /* [28] 16-bit */
unsigned long compression; /* [30] 32-bit */
unsigned long sizeimage; /* [34] 32-bit */
unsigned long resolution_hor; /* [38] 32-bit */
unsigned long resolution_ver; /* [42] 32-bit */
unsigned long coul_uti; /* [46] 32-bit */
unsigned long coul_imp; /* [50] 32-bit */
};
struct image
{
unsigned char bleu;
unsigned char vert;
unsigned char rouge;
size_t x;
size_t y;
};
static struct image *lecture_image (struct taille *p_taille
, char const *file)
{
struct image *image = NULL;
if (p_taille->sizeimage != 0)
{
unsigned char *temp = malloc (p_taille->sizeimage * sizeof * temp);
if (temp != NULL)
{
//Ouverture du Fichier
FILE *fichier = fopen (file, "rb");
if (fichier != NULL)
{
//Ignorer de l'entete de l'image
{
unsigned char entete[HEADER_SIZE];
fread (entete, 1, sizeof entete, fichier);
}
//Copie de l'image
fread (temp, 1, p_taille->sizeimage, fichier);
fclose (fichier), fichier = NULL;
{
image = malloc (p_taille->sizeimage * sizeof * image);
if (image != NULL)
{
//Traitement de l'image
{
size_t i;
for (i = 0; i < p_taille->largeur * p_taille->hauteur; i += 3)
{
image[i].bleu = temp[i];
image[i].vert = temp[i + 1];
image[i].rouge = temp[i + 2];
image[i].x = i % p_taille->largeur;
image[i].y = (i - (i % p_taille->largeur)) / p_taille->largeur;
}
}
}
else
{
fprintf (stderr, "Allocation impossible\n");
}
}
}
else
{
fprintf (stderr, "Erreur d'ouverture du fichier\n");
}
assert (fichier == NULL);
free (temp), temp = NULL;
}
else
{
fprintf (stderr, "Allocation impossible\n");
}
assert (temp == NULL);
}
else
{
fprintf (stderr, "Taille nulle\n");
}
return image;
}
static unsigned long bytes2ul (unsigned char const *p)
{
unsigned long ul = 0;
/* LSB en tete : */
ul |= p[0] << (8 * 0);
ul |= p[1] << (8 * 1);
ul |= p[2] << (8 * 2);
ul |= p[3] << (8 * 3);
return ul;
}
static unsigned short bytes2us (unsigned char const *p)
{
unsigned short us = 0;
/* LSB en tete : */
us |= p[0] << (8 * 0);
us |= p[1] << (8 * 1);
return us;
}
static int taille_fichier (struct taille *p_taille, char const *fname)
{
int err = 0;
if (p_taille != NULL && fname != NULL && *fname != 0)
{
//Ouverture du Fichier
FILE *fichier = fopen (fname, "rb");
printf ("chargement de '%s'\n", fname);
if (fichier != NULL)
{
unsigned char entete[HEADER_SIZE];
fread (entete, 1, sizeof entete, fichier);
if (strncmp (entete, "BM", 2) == 0)
{
#if DBG
#define PRT(field, ofs, cmt)\
,printf ("[%2d] %-14s = %lu %s\n", ofs, #field, (unsigned long) p_taille->field, cmt)
#else
#define PRT(field, ofs, cmt)
#endif
#define UPD_UL(field, ofs, cmt)\
p_taille->field = bytes2ul (entete + ofs)\
PRT (field, ofs, cmt)
#define UPD_US(field, ofs, cmt)\
p_taille->field = bytes2us (entete + ofs)\
PRT (field, ofs, cmt)
UPD_UL (filesize, 2, "bytes");
UPD_UL (offset, 10, "bytes");
UPD_UL (headersize, 14, "bytes");
UPD_UL (largeur, 18, "pix");
UPD_UL (hauteur, 22, "pix");
UPD_US (planes, 26, "");
UPD_US (bit_per_pix, 28, "");
UPD_UL (compression, 30
, "(0=pas de comp. 1=RLE 8bits 2=RLE 4bits 3=bitfield enc.)");
UPD_UL (sizeimage, 34, "bytes");
UPD_UL (resolution_hor, 38, "pix/metre");
UPD_UL (resolution_ver, 42, "pix/metre");
UPD_UL (coul_uti, 46, "");
UPD_UL (coul_imp, 50, "");
#undef UPD_US
#undef UPD_UL
#undef PRT
}
else
{
fprintf (stderr, "not a bit map\n");
err = 1;
}
}
else
{
err = 1;
}
}
else
{
err = 1;
}
return err;
}
int main (int argc, char **argv)
{
if (argc > 1)
{
char const *fname = argv[1];
struct taille taille =
{
0
};
int err = taille_fichier (&taille, fname);
if (!err)
{
struct image *image = lecture_image (&taille, argv[1]);
if (image != NULL)
{
printf ("x : %d\n"
"y : %d\n"
"bleu : %d\n"
"vert : %d\n"
"rouge : %d\n"
, (int) image[60].x
, (int) image[60].y
, (int) image[60].bleu
, (int) image[60].vert
, (int) image[60].rouge);
free (image), image = NULL;
}
else
{
fprintf (stderr, "erreur image\n");
}
}
else
{
fprintf (stderr, "erreur fichier\n");
}
}
else
{
fprintf (stderr, "erreur parametres\n");
}
return 0;
} |