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’un bmp


Sujet :

C

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé
    Homme Profil pro
    Inscrit en
    Octobre 2007
    Messages
    487
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2007
    Messages : 487
    Par défaut Lecture d’un bmp

    Ou puis je trouver un code qui lit une image de type bmp?

  2. #2
    Expert confirmé
    Avatar de raptor70
    Inscrit en
    Septembre 2005
    Messages
    3 173
    Détails du profil
    Informations personnelles :
    Âge : 40

    Informations forums :
    Inscription : Septembre 2005
    Messages : 3 173
    Par défaut

  3. #3
    Rédacteur
    Avatar de Franck.H
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Janvier 2004
    Messages
    6 951
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : France, Haut Rhin (Alsace)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : Service public

    Informations forums :
    Inscription : Janvier 2004
    Messages : 6 951
    Par défaut
    Bin tu peux passer par une bibliothèque tierce pour lire des images, par exemple la SDL ou sinon tu connais le format et tu créé le code mais là c'est une autre paire de manche, tu peux voir ici éventuellement pour le format: http://www.wotsit.org/
    Mon Site
    Ma bibliothèque de gestion des chaînes de caractères en C

    L'imagination est plus importante que le savoir. A. Einstein

    Je ne répond à aucune question technique par MP, merci d'avance !

  4. #4
    Membre confirmé Avatar de Topeur
    Profil pro
    Inscrit en
    Février 2008
    Messages
    91
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2008
    Messages : 91
    Par défaut
    Il existe aussi openCV comme bibliotheque

    Mais si tu veux créer ta propre bibliothèque et fonction de lecture, alors bon courage, parce que c'est loin d'être évident

  5. #5
    Membre averti
    Profil pro
    Étudiant
    Inscrit en
    Novembre 2006
    Messages
    19
    Détails du profil
    Informations personnelles :
    Âge : 41
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Novembre 2006
    Messages : 19
    Par défaut
    Citation Envoyé par dot-_-net Voir le message

    Ou puis je trouver un code qui lit une image de type bmp?
    Voilà un code très simple (valable pour les images bitmaps 24 bits, sinon fais les modifications necessaires) :

    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
     
    #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);
    }
    Par contre, si tu as un code pour écrire un fichier bmp, je suis preneur !

  6. #6
    Expert éminent
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 395
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 395
    Par défaut
    Bouffa: Ton code ne garantit pas l'alignement pour BMPHeader.
    SVP, pas de questions techniques par MP. Surtout si je ne vous ai jamais parlé avant.

    "Aw, come on, who would be so stupid as to insert a cast to make an error go away without actually fixing the error?"
    Apparently everyone.
    -- Raymond Chen.
    Traduction obligatoire: "Oh, voyons, qui serait assez stupide pour mettre un cast pour faire disparaitre un message d'erreur sans vraiment corriger l'erreur?" - Apparemment, tout le monde. -- Raymond Chen.

  7. #7
    Membre éclairé
    Homme Profil pro
    Inscrit en
    Octobre 2007
    Messages
    487
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2007
    Messages : 487
    Par défaut
    Citation Envoyé par bouffa Voir le message
    Voilà un code très simple (valable pour les images bitmaps 24 bits, sinon fais les modifications necessaires) :

    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
     
    #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);
    }
    Par contre, si tu as un code pour écrire un fichier bmp, je suis preneur !
    Ce code je l’ai testé ça ne lit pas un fichier bmp merci en tout cas

Discussions similaires

  1. Lecture dun fichier xml avec python
    Par merlinerick dans le forum Interfaçage autre langage
    Réponses: 6
    Dernier message: 29/07/2009, 13h49
  2. Probleme dans la lecture d’un bmp
    Par dot-_-net dans le forum C++
    Réponses: 3
    Dernier message: 08/04/2008, 18h25
  3. Problème de lecture d’un caractère
    Par TheBlue dans le forum C
    Réponses: 5
    Dernier message: 17/01/2008, 21h31
  4. [débutant] lecture fichier bmp en C++
    Par bmw13fr dans le forum Bibliothèques
    Réponses: 3
    Dernier message: 09/03/2006, 22h33
  5. Lecture d´un fichier texte .
    Par pilouface dans le forum C
    Réponses: 5
    Dernier message: 20/01/2006, 23h48

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