IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C Discussion :

Lecture d'une image bmp


Sujet :

C

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Inscrit en
    Mars 2010
    Messages
    33
    Détails du profil
    Informations forums :
    Inscription : Mars 2010
    Messages : 33
    Par défaut Lecture d'une image bmp
    Salut à tous,
    je suis entrain de tester un exemple en C qui permet d'afficher les propriétés d'une
    image bmp(largeur,hauteur,bit par pixel ,ect...),voici mon code:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    #include <stdio.h>
    #include <stdlib.h>
     
    typedef struct bmpheader
    {
    unsigned short type;
    unsigned long file_size;
    unsigned long reserved;
    unsigned long data_offset;
    unsigned long header_size;
    signed long width;
    signed long height;
    unsigned short planes;
    unsigned short bits_per_pixel;
    unsigned int compression;
    unsigned long data_size;
    signed horizontal_resolution;
    signed vertical_resolution;
    unsigned long colors;
    unsigned long important_colors;
    }bmpheader;
     
    void readbmpheader(char *bitmapfilename)
    {
    FILE *bitmapfile;
    bmpheader header;
    bitmapfile=fopen(bitmapfilename,"rb");
    if(bitmapfile==NULL)
    {
    printf("Error when reading...!\n");
    exit(-1);
    }
    fread(&header,sizeof(bmpheader),1,bitmapfile);
     
    printf("Bitmap type:\n");
    printf("%d\n",header.type);
    printf("Bitmap size:\n");
    printf("%d\n",header.file_size);
    printf("Bitmap reserved value:\n");
    printf("%d\n",header.reserved);
    printf("Bitmap data offset:");
    printf("%d\n",header.data_offset);
    printf("Bitmap header size:\n");
    printf("%d\n",header.header_size);
    printf("Bitmap width:\n");
    printf("%d\n",header.width);
    printf("Bitmap height:\n");
    printf("%d\n",header.height);
    printf("Bitmap planes number:\n");
    printf("%d\n",header.planes);
    printf("Bitmap bits per pixel:\n");
    printf("%d\n",header.bits_per_pixel);
    printf("Bitmap compression mode:\n");
    printf("%d\n",header.compression);
    printf("Bitmap data size:\n");
    printf("%d\n",header.data_size);
    printf("Bitmap horisontal resolution:\n");
    printf("%d\n",header.horizontal_resolution);
    printf("Bitmap vertical resolution:\n");
    printf("%d\n",header.vertical_resolution);
    printf("Bitmap Colors:\n");
    printf("%d\n",header.colors);
    printf("Bitmap Important colors:\n");
    printf("%d\n",header.important_colors);
     
    }
     
    int main()
    {
    readbmpheader("\\Image.bmp");
    getchar();
    return 0;
    }
    mon image est de taille 2000x1339 et 7,66 mo d'espace sur le disque,et la console affiche des résultats faux comme bit par pixel égale à 0,taille et largeur différent à celle de mon image,taille sur le disque 122... il parait que tous les champs sont faux...,donc j'ai commit une faute quelques parts...
    quelqu'un peut m'aider SVP!
    merci en avance !

  2. #2
    Membre chevronné
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Mai 2006
    Messages
    507
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Pas de Calais (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Communication - Médias

    Informations forums :
    Inscription : Mai 2006
    Messages : 507
    Par défaut
    Bonjour,

    Je connais pas l'entête des bmp mais la façon dont tu t'y prends est assez dangeureuse...
    Es-tu sûr des tailles de tes types short/long/int ? D'une plateforme 32bits à une 64, leurs tailles peuvent varier...

  3. #3
    Invité(e)
    Invité(e)
    Par défaut
    Bonjour,

    On peut trouver les spécifications des entêtes BMP sur MSDN : ici, et .

    Et cette page qui explique le principe.

  4. #4
    Membre averti
    Inscrit en
    Mars 2010
    Messages
    33
    Détails du profil
    Informations forums :
    Inscription : Mars 2010
    Messages : 33
    Par défaut
    Merci pour pour les liens utiles !
    maintenant c'est bon,et ça marche avec des résultats correctes 100%:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    #include <stdio.h>
    #include <stdlib.h>
     
    #pragma pack(push, 1)
    typedef struct{
    unsigned short type;
    unsigned long size;
    unsigned short reserved1;
    unsigned short reserved2;
    unsigned long offsetbits;
    }bmpheader;
    #pragma pack(pop)
    typedef struct{
    unsigned long headersize;
    unsigned long width;
    unsigned long height;
    unsigned short planes;
    unsigned short bitcount;
    unsigned long compression;
    unsigned long sizeimage;
    unsigned long xpelspermeter;
    unsigned long ypelspermeter;
    unsigned long colorsused;
    unsigned long colorsimportant;
    unsigned char *palette;
    unsigned char *pixels;
    }bmpinfo;
     
    void readbmpheader(char *bitmapfilename)
    {
    FILE *bitmapfile;
    bmpheader header;
    bmpinfo info;
    bitmapfile=fopen(bitmapfilename,"rb");
    if(bitmapfile==NULL)
    {
    printf("Error when reading...!\n");
    exit(-1);
    }
     
    fread(&header,sizeof(header),1,bitmapfile);
    printf("Type de bitmap\n");
    printf("%x\n",header.type);
    printf("Taille de bitmap on octets:\n");
    printf("%d\n",header.size);
    printf("reserved1 :\n");
    printf("%d\n",header.reserved1);
    printf("reserved2 :\n");
    printf("%d\n",header.reserved2);
    printf("Data offset de la bitmap\n");
    printf("%d\n",header.offsetbits);
     
    fread(&info,sizeof(bmpinfo),1,bitmapfile);
    printf("Taille de l'entete:\n");
    printf("%d\n",info.headersize);
    printf("Largeur de l'image:\n");
    printf("%d\n",info.width);
    printf("Hauteur de l'image:\n");
    printf("%d\n",info.height);
    printf("Nombre de planes:\n");
    printf("%d\n",info.planes);
    printf("Bits par pixels:\n");
    printf("%d\n",info.bitcount);
    printf("Mode de compression:\n");
    printf("%d\n",info.compression);
    printf("Taille de l'image:\n");
    printf("%d\n",info.sizeimage);
    printf("Xpelspermeter:\n");
    printf("%d\n",info.xpelspermeter);
    printf("Ypelspermeter:\n");
    printf("%d\n",info.ypelspermeter);
    printf("Couleur utilise:\n");
    printf("%d\n",info.colorsused);
    printf("Coleur importants:\n");
    printf("%d\n",info.colorsimportant);
     
    fclose(bitmapfile);
     
    }
     
     
     
    int main()
    {
    readbmpheader("\\Image.bmp");
    getchar();
    return 0;
    }
    mais je bloque à comment récupérer la palette et les pixels...
    quelqu'un a des idées?

  5. #5
    Responsable 2D/3D/Jeux


    Avatar de LittleWhite
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Mai 2008
    Messages
    27 119
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Mai 2008
    Messages : 27 119
    Billets dans le blog
    148
    Par défaut
    Récemment, le même problème à été soulevé ici: http://www.developpez.net/forums/d90...atrice-image-c
    Vous souhaitez participer à la rubrique 2D/3D/Jeux ? Contactez-moi

    Ma page sur DVP
    Mon Portfolio

    Qui connaît l'erreur, connaît la solution.

  6. #6
    Membre averti
    Inscrit en
    Mars 2010
    Messages
    33
    Détails du profil
    Informations forums :
    Inscription : Mars 2010
    Messages : 33
    Par défaut
    OK merci,ça m'aider un peu...
    pour quelques parties,c'était obscure,car ce que je veut est afficher toutes les
    composantes R,V,B des pixels,ou de récupérer le raster...
    voici ce que j'ai fait:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    #include <stdio.h>
    #include <stdlib.h>
     
    #pragma pack(push, 1)
    typedef struct{
    unsigned short type;
    unsigned long size;
    unsigned short reserved1;
    unsigned short reserved2;
    unsigned long offsetbits;
    }bmpheader;
    #pragma pack(pop)
    typedef struct{
    unsigned long headersize;
    unsigned long width;
    unsigned long height;
    unsigned short planes;
    unsigned short bitcount;
    unsigned long compression;
    unsigned long sizeimage;
    unsigned long xpelspermeter;
    unsigned long ypelspermeter;
    unsigned long colorsused;
    unsigned long colorsimportant;
    }bmpinfo;
     
    void readbmpheader(char *bitmapfilename)
    {
    FILE *bitmapfile;
    bmpheader header;
    bmpinfo info;
    bitmapfile=fopen(bitmapfilename,"rb");
    if(bitmapfile==NULL)
    {
    printf("Error when reading...!\n");
    exit(-1);
    }
     
    fread(&header,sizeof(bmpheader),1,bitmapfile);
    printf("Type de bitmap\n");
    printf("%x\n",header.type);
    printf("Taille de bitmap on octets:\n");
    printf("%d\n",header.size);
    printf("reserved1 :\n");
    printf("%d\n",header.reserved1);
    printf("reserved2 :\n");
    printf("%d\n",header.reserved2);
    printf("Data offset de la bitmap\n");
    printf("%d\n",header.offsetbits);
     
    fread(&info,sizeof(bmpinfo),1,bitmapfile);
    printf("Taille de l'entete:\n");
    printf("%d\n",info.headersize);
    printf("Largeur de l'image:\n");
    printf("%d\n",info.width);
    printf("Hauteur de l'image:\n");
    printf("%d\n",info.height);
    printf("Nombre de planes:\n");
    printf("%d\n",info.planes);
    printf("Bits par pixels:\n");
    printf("%d\n",info.bitcount);
    printf("Mode de compression:\n");
    printf("%d\n",info.compression);
    printf("Taille de l'image:\n");
    printf("%d\n",info.sizeimage);
    printf("Xpelspermeter:\n");
    printf("%d\n",info.xpelspermeter);
    printf("Ypelspermeter:\n");
    printf("%d\n",info.ypelspermeter);
    printf("Couleur utilise:\n");
    printf("%d\n",info.colorsused);
    printf("Coleur importants:\n");
    printf("%d\n",info.colorsimportant);
     
    /*afficher tous les compsantes RGB des pixels*/
    fseek(bitmapfile,header.offsetbits,SEEK_SET);
    unsigned char red,green,blue;
    for(int height=0;height<=info.height;height++)
    {
    for(int width=0;width<=info.width;width++)
    {
    fread(&red,sizeof(unsigned char),1,bitmapfile);
    fread(&green,sizeof(unsigned char),1,bitmapfile);
    fread(&blue,sizeof(unsigned char),1,bitmapfile);
    printf("Pixel %s%d%s%d%s\n","[",height,",",width,"]");
    printf("Rouge %d\n",red);
    printf("Vert %d\n",green);
    printf("Bleu %d\n",blue);
    printf("\n");
    }
    }
     
    /*et ça pour récupérer tous les pixels*/
    fseek(bitmapfile,header.offsetbits,SEEK_SET);
    unsigned char *pixels;
    /* la taille du raster est la taille de la bitmap en octet moins
    l'offset bits(qui est toujours 54 pour les bitmaps windows)
    donc ça donne taille de raster=largeur*hauteur */
    pixels=(unsigned char*)malloc(header.size-header.offsetbits);
    fread(pixels,sizeof(unsigned char),header.size-header.offsetbits,bitmapfile);
     
    free(pixels);
     
     
     
     
    fclose(bitmapfile);
     
    }
     
     
     
    int main()
    {
    readbmpheader("\\Image.bmp");
    getchar();
    return 0;
    }
    et ça marcher...
    enfaite,le bitmap ce que j'ai utiliser à la méthode de compression 0(pas de compression),donc c'était facile,mais si l'image est compressé,ça va être plus difficile...

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. lecture entête d'une image.bmp
    Par josh51 dans le forum C
    Réponses: 2
    Dernier message: 15/10/2009, 17h17
  2. linux: lecture d'une image bmp
    Par asoka13 dans le forum OpenGL
    Réponses: 2
    Dernier message: 12/03/2008, 10h30
  3. inserer une image BMP dans un fichier rtf
    Par Alice9 dans le forum MFC
    Réponses: 17
    Dernier message: 06/07/2004, 10h31
  4. [BPW] Impression d'une image BMP
    Par Alcatîz dans le forum Turbo Pascal
    Réponses: 13
    Dernier message: 21/08/2003, 14h34
  5. Lecture d'une image bitmap
    Par Geronimo dans le forum x86 32-bits / 64-bits
    Réponses: 18
    Dernier message: 28/06/2002, 12h01

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo