| 12
 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
 
 |  
#include <stdio.h>
#include <stdlib.h>
 
typedef struct{
    short type;
    int size;
    short reserved1;
    short reserved2;
    int offset;
} BMPHeader;
 
typedef struct{
    int size;
    int width;
    int height;
    short planes;
    short bitsPerPixel;
    unsigned compression;
    unsigned imageSize;
    int xPelsPerMeter;
    int yPelsPerMeter;
    int clrUsed;
    int clrImportant;
} BMPInfoHeader;
 
 
void LoadBMPFile(unsigned char **dst, int *width, int *height, const char *name){
    BMPHeader hdr;
    BMPInfoHeader infoHdr;
    int x, y;
 
    FILE *fd;
 
 
    printf("Chargement de %s...\n", name);
 
 
    if( !(fd = fopen(name,"rb")) ){
        printf("***BMP load error: file access denied***\n");
        exit(0);
    }
 
    fread(&hdr, sizeof(hdr), 1, fd);
    if(hdr.type != 0x4D42){
        printf("***BMP load error: bad file format***\n");
        exit(0);
    }
    fread(&infoHdr, sizeof(infoHdr), 1, fd);
 
    if(infoHdr.bitsPerPixel != 24){
        printf("***BMP load error: invalid color depth***\n");
        exit(0);
    }
 
    if(infoHdr.compression){
        printf("***BMP load error: compressed image***\n");
        exit(0);
    }
 
    *width =  infoHdr.width;
    *height = infoHdr.height;
    *dst    = (unsigned char *)malloc(*width * *height * sizeof(unsigned char));
 
    printf("Largeur : %u\n", infoHdr.width);
    printf("Hauteur : %u\n", infoHdr.height);
 
    fseek(fd, hdr.offset - sizeof(hdr) - sizeof(infoHdr), SEEK_CUR);
	int r,g,b;
    for(y = 0; y < *height; y++){
	for(x = 0; x < infoHdr.width; x++){
		(*dst)[(y * *width + x)].z      =fgetc(fd); // composante bleue
		(*dst)[(y * *width + x)].y=fgetc(fd); // composante verte
		(*dst)[(y * *width + x)].x=fgetc(fd); // composante rouge
 
            }
            /* si la largeur de l'image n'est pas multiple de 4, des octets sont ajoutés à la fin de chaque ligne, on ne les ignore */
	for(x = 0; x < (4 - (3 * infoHdr.width) % 4) % 4; x++)
                 fgetc(fd);
 
    }
 
 
    if(ferror(fd)){
        printf("***Unknown BMP load error.***\n");
        free(*dst);
        exit(0);
    }
     else
         printf("BMP file loaded successfully!\n");
 
    fclose(fd);
} | 
Partager