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

SDL Discussion :

Image PPM avec SDL


Sujet :

SDL

  1. #1
    Membre confirmé
    Profil pro
    Inscrit en
    Janvier 2010
    Messages
    157
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2010
    Messages : 157
    Par défaut Image PPM avec SDL
    Bonjour,

    J' utilise la bibliothéque SDL sous C pour ètudier une image,le probléme c'est que l'image est en PPM (extension .ppm) et que SDL ne gére pas ce type d'image.
    Je pourrais savoir s'il vous plait s'il y a un moyen pour rendre ces images utilisables avec SDL, ou m'indiquer une autre bibliothéque qui admet ce type.

    Merci.

  2. #2
    Expert confirmé
    Avatar de Kannagi
    Homme Profil pro
    cyber-paléontologue
    Inscrit en
    Mai 2010
    Messages
    3 226
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : cyber-paléontologue

    Informations forums :
    Inscription : Mai 2010
    Messages : 3 226
    Par défaut
    Jamais entendu ce fichier , bah soit tu ouvre photoshop/gimp qui lit pas mal de format et tu enregistre ton image dans un autre format(png,jpg).

    Soit avec opengl tu crée ton propre loader avec cette doc qui ont plus m'a l'air facile a lire , et pas compliquer a programmer.

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


    Avatar de LittleWhite
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Mai 2008
    Messages
    27 160
    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 160
    Billets dans le blog
    152
    Par défaut
    Bonjour,

    Pour rappel, voici le format PPM.
    Donc au final, c'est un fichier texte très simpliste, mais qui décrit une image.

    Pour le lire avec la SDL, ce n'est pas non plus une chose ardue. Il faut simplement lire le fichier en mode texte (comme on lirai un autre fichier en C), puis pour chaque pixel, on remplira la SDL_Surface de notre image destination.

    Donc la documentation à lire:
    Celle du format PPM
    Celle de SDL_Surface et les autres autour (ça peut servir :p)
    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.

  4. #4
    Membre confirmé
    Profil pro
    Inscrit en
    Janvier 2010
    Messages
    157
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2010
    Messages : 157
    Par défaut
    Merci de votre aide,

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

    Voici une bibliothèque que j'avais réalisée l'an dernier sur le sujet. Elle lit et écrit les trois types de PNM (PBM, PGM, PPM) en ASCII et binaire.

    Elle est normalement totalement fonctionnel. De mes tests à l'époque (conversion d'images chargées par SDL_image et écriture avec ma lib dans un des trois formats) cela fonctionné après ouverture avec des logiciels comme Gimp.
    J'ai également réussi à afficher des images de ces formats venant de GIMP.

    Il peut bien évidemment y avoir des bugs et erreurs. Merci de me les retourner le cas échéant.


    SDL_pnm.h
    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
     
    /*
        SDL_pnm :  A portable anymap format library for SDL (PPM : portable pixmap format (PPM), portable graymap format (PGM) and portable bitmap format (PBM))
        Copyright (C) 2010 Loïc Bartoletti.
        All rights reserved.
     
        Redistribution and use in source and binary forms, with or without
        modification, are permitted provided that the following conditions
        are met:
        1. Redistributions of source code must retain the above copyright
            notice, this list of conditions and the following disclaimer.
        2. Redistributions in binary form must reproduce the above copyright
            notice, this list of conditions and the following disclaimer in the
        documentation and/or other materials provided with the distribution.
        THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
        ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
        IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
        ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
        FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
        DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
        OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
        HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
        LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
        OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
        SUCH DAMAGE.
    */
     
     
     
    #ifndef SDL_pnm_H_INCLUDED
    #define SDL_pnm_H_INCLUDED
     
    #include <SDL/SDL.h>
    #include <SDL/begin_code.h>
    /*
        SDL_pnm :  A portable anymap format library for SDL (PPM : portable pixmap format (PPM), portable graymap format (PGM) and portable bitmap format (PBM))
        Copyright (C) 2010 Loïc Bartoletti.
        All rights reserved.
     
        Redistribution and use in source and binary forms, with or without
        modification, are permitted provided that the following conditions
        are met:
        1. Redistributions of source code must retain the above copyright
            notice, this list of conditions and the following disclaimer.
        2. Redistributions in binary form must reproduce the above copyright
            notice, this list of conditions and the following disclaimer in the
        documentation and/or other materials provided with the distribution.
        THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
        ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
        IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
        ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
        FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
        DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
        OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
        HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
        LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
        OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
        SUCH DAMAGE.
    */
     
    /* Set up for C function definitions, even when using C++ */
    #ifdef __cplusplus
    extern "C" {
    #endif
     
     
        extern DECLSPEC void Write_PBM_ASCII(SDL_Surface *input, char *output);
        extern DECLSPEC void Write_PGM_ASCII(SDL_Surface *input, char *output);
        extern DECLSPEC void Write_PPM_ASCII(SDL_Surface *input, char *output);
     
        extern DECLSPEC void Write_PBM_RAW(SDL_Surface *input, char *output);
        extern DECLSPEC void Write_PGM_RAW(SDL_Surface *input, char *output);
        extern DECLSPEC void Write_PPM_RAW(SDL_Surface *input, char *output);
     
        extern DECLSPEC SDL_Surface *Read_PBM_ASCII(const char* input);
        extern DECLSPEC SDL_Surface *Read_PGM_ASCII(const char* input);
        extern DECLSPEC SDL_Surface *Read_PPM_ASCII(const char* input);
     
        extern DECLSPEC SDL_Surface *Read_PBM_RAW(const char* input);
        extern DECLSPEC SDL_Surface *Read_PGM_RAW(const char* input);
        extern DECLSPEC SDL_Surface *Read_PPM_RAW(const char* input);
     
    #ifdef __cplusplus
    }
    #endif
    #include <SDL/close_code.h>
     
    #endif /* SDL_pnm_H_INCLUDED */
    SDL_pnm.c
    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
    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
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506
    507
    508
    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
    520
    521
    522
    523
    524
    525
    526
    527
    528
    529
    530
    531
    532
    533
    534
    535
    536
    537
    538
    539
    540
    541
    542
    543
    544
    545
    546
    547
    548
    549
    550
    551
    552
    553
    554
    555
    556
    557
    558
    559
    560
    561
    562
    563
    564
    565
    566
    567
    568
    569
    570
    571
    572
    573
    574
    575
    576
    577
    578
    579
    580
    581
    582
    583
    584
    585
    586
    587
    588
    589
    590
    591
    592
    593
    594
    595
    596
    597
    598
    599
    600
    601
    602
    603
    604
    605
    606
    607
    608
    609
    610
    611
    612
    613
    614
    615
    616
    617
    618
    619
    620
    621
    622
    623
    624
    625
    626
    627
    628
    629
    630
    631
    632
    633
    634
    635
    636
    637
    638
    639
    640
    641
    642
    643
    644
    645
    646
    647
    648
    649
    650
    651
    652
    653
    654
    655
    656
    657
    658
    659
    660
    661
    662
    663
    664
    665
    666
    667
    668
    669
    670
    671
    672
    673
    674
    675
    676
    677
    678
    679
    680
    681
    682
    683
    684
    685
    686
    687
    688
    689
    690
    691
    692
    693
    694
    695
    696
    697
    698
    699
    700
    701
    702
    703
    704
    705
    706
    707
    708
    709
    710
    711
    712
    713
    714
    715
    716
    717
    718
    719
    720
    721
    722
    723
    724
    725
    726
    727
    728
    729
    730
    731
    732
    733
    734
    735
    736
    737
    738
    739
    740
    741
    742
    743
    744
    745
    746
    747
    748
    749
    750
    751
    752
    753
    754
    755
    756
    757
    758
    759
    760
    761
    762
    763
    764
    765
    766
    767
    768
    769
    770
    771
    772
    773
    774
    775
    776
    777
    778
    779
    780
    781
    782
    783
    784
    785
    786
    787
    788
    789
    790
    791
    792
    793
    794
    795
    796
    797
    798
    799
    800
    801
    802
    803
    804
    805
    806
    807
    808
    809
    810
    811
    812
    813
    814
    815
    816
    817
    818
    819
    820
    821
    822
    823
    824
    825
    826
    827
    828
    829
    830
    831
    832
    833
    834
    835
    836
    837
    838
    839
    840
    841
    842
    843
    844
    845
    846
    847
    848
    849
    850
    851
    852
    853
    854
    855
    856
     
    /*
        SDL_pnm :  A portable anymap format library for SDL (PPM : portable pixmap format (PPM), portable graymap format (PGM) and portable bitmap format (PBM))
        Copyright (C) 2010 Loïc Bartoletti.
        All rights reserved.
     
        Redistribution and use in source and binary forms, with or without
        modification, are permitted provided that the following conditions
        are met:
        1. Redistributions of source code must retain the above copyright
            notice, this list of conditions and the following disclaimer.
        2. Redistributions in binary form must reproduce the above copyright
            notice, this list of conditions and the following disclaimer in the
        documentation and/or other materials provided with the distribution.
        THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
        ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
        IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
        ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
        FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
        DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
        OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
        HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
        LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
        OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
        SUCH DAMAGE.
    */
     
    #include <SDL/SDL.h>
    #include "SDL_pnm.h"
    #include <stdio.h>
    #include <stdlib.h>
     
    /* SDL manual ; http://www.libsdl.org/docs/html/guidevideo.html#AEN90 */
    static Uint32 getpixel(SDL_Surface *surface, int x, int y)
    {
        int bpp = surface->format->BytesPerPixel;
        /* Here p is the address to the pixel we want to retrieve */
        Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
     
        switch (bpp)
        {
        case 1:
            return *p;
     
        case 2:
            return *(Uint16 *)p;
     
        case 3:
            if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
                return p[0] << 16 | p[1] << 8 | p[2];
            else
                return p[0] | p[1] << 8 | p[2] << 16;
     
        case 4:
            return *(Uint32 *)p;
     
        default:
            return 0;       /* shouldn't happen, but avoids warnings */
        }
    }
     
     
    /* SDL manual : http://www.libsdl.org/docs/html/guidevideo.html#AEN93 */
    static void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
    {
        int bpp = surface->format->BytesPerPixel;
        /* Here p is the address to the pixel we want to set */
        Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
     
        switch (bpp)
        {
        case 1:
            *p = pixel;
            break;
     
        case 2:
            *(Uint16 *)p = pixel;
            break;
     
        case 3:
            if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
            {
                p[0] = (pixel >> 16) & 0xff;
                p[1] = (pixel >> 8) & 0xff;
                p[2] = pixel & 0xff;
            }
            else
            {
                p[0] = pixel & 0xff;
                p[1] = (pixel >> 8) & 0xff;
                p[2] = (pixel >> 16) & 0xff;
            }
            break;
     
        case 4:
            *(Uint32 *)p = pixel;
            break;
        }
    }
     
    void Write_PBM_ASCII(SDL_Surface *input, char *output)
    {
        FILE *f_pxm = NULL;
        char *fichier = malloc((strlen(output)+ strlen(".pbm")) * sizeof(char*));
        sprintf(fichier, "%s.pbm", output);
        if ((f_pxm = fopen(fichier, "w+")) == NULL)
        {
            printf("Impossible de créer %s\n", fichier);
            exit(EXIT_FAILURE);
        }
        free(fichier);
        /* on écrit l'en-tête */
        fprintf(f_pxm, "P1\n# %s make with SDL_pnm\n%d %d\n", output, input->w, input->h);
     
        int x, y;
        Uint32 pix;
        int nb_appel = 1;
     
     
        for (y = 0; y < input->h ; y++)
        {
            for (x = 0; x < input->w; x++, nb_appel++)
            {
                pix = getpixel(input, x, y);
                Uint8 r, g, b;
                SDL_GetRGB(pix, input->format, &r, &g, &b);
                /* on considère que la couleur du pixel à sortir est la moyenne des couleurs RGB */
                /* on considère qu'au maximum on peut ecrire 1 espace + 1 caractères */
                /* Une ligne ne doit pas dépasser 70 caractères */
                /* donc tout les 17 appels on met un retour à la ligne */
                if (nb_appel == 35)
                {
                    fprintf(f_pxm, "\n");
                    nb_appel = 0;
                }
                if ( ((r + g + b)/3)>125 )
                    fprintf(f_pxm, "0 ");
                else
                    fprintf(f_pxm, "1 ");
            }
            fprintf(f_pxm, "\n");
        }
        fclose(f_pxm);
     
    }
     
    void Write_PGM_ASCII(SDL_Surface *input, char *output)
    {
        FILE *f_pxm = NULL;
        char *fichier = malloc((strlen(output)+strlen(".pgm")) * sizeof(char*));
        sprintf(fichier, "%s.pgm", output);
        if ((f_pxm = fopen(fichier, "w+")) == NULL)
        {
            printf("Impossible de créer %s\n", fichier);
            exit(EXIT_FAILURE);
        }
        free(fichier);
        /* on écrit l'en-tête */
        fprintf(f_pxm, "P2\n# %s make with SDL_pnm\n%d %d\n%d\n", output, input->w, input->h, 255);
     
        int x, y;
        Uint32 pix;
        int nb_appel = 1;
        for (y = 0; y < input->h ; y++)
        {
            for (x = 0; x < input->w; x++, nb_appel++)
            {
                pix = getpixel(input, x, y);
                Uint8 r, g, b;
                SDL_GetRGB(pix, input->format, &r, &g, &b);
                /* on considère que la couleur du pixel à sortir est la moyenne des couleurs RGB */
                /* on considère qu'au maximum on peut ecrire 1 espace + 3 caractères */
                /* Une ligne ne doit pas dépasser 70 caractères */
                /* donc tout les 17 appels on met un retour à la ligne */
                if (nb_appel == 17)
                {
                    fprintf(f_pxm, "\n");
                    nb_appel = 0;
                }
                fprintf(f_pxm, "%d ", (r + g + b)/3);
            }
            fprintf(f_pxm, "\n");
        }
        fclose(f_pxm);
     
    }
     
    void Write_PPM_ASCII(SDL_Surface *input, char *output)
    {
        FILE *f_pxm = NULL;
        char *fichier = malloc((strlen(output)+ strlen(".ppm")) * sizeof(char*));
        sprintf(fichier, "%s.ppm", output);
        if ((f_pxm = fopen(fichier, "w+")) == NULL)
        {
            printf("Impossible de créer %s\n", fichier);
            exit(EXIT_FAILURE);
        }
        free(fichier);
        /* on écrit l'en-tête */
        fprintf(f_pxm, "P3\n# %s make with SDL_pnm\n%d %d\n%d\n", output, input->w, input->h, 255);
     
        int x, y;
        Uint32 pix;
        int nb_appel = 1;
        for (y = 0; y < input->h ; y++)
        {
            for (x = 0; x < input->w; x++, nb_appel++)
            {
                pix = getpixel(input, x, y);
                Uint8 r, g, b;
                SDL_GetRGB(pix, input->format, &r, &g, &b);
                /* on considère qu'au maximum on peut ecrire 3 espace + 3*3 caractères */
                /* Une ligne ne doit pas dépasser 70 caractères */
                /* donc tout les 5 appels on met un retour à la ligne */
                if (nb_appel == 5)
                {
                    fprintf(f_pxm, "\n");
                    nb_appel = 0;
                }
                fprintf(f_pxm, "%d %d %d ", r, g, b);
            }
            fprintf(f_pxm, "\n");
        }
        fclose(f_pxm);
     
    }
     
     
    void Write_PBM_RAW(SDL_Surface *input, char *output)
    {
        FILE *f_pxm = NULL;
        char *fichier = malloc((strlen(output)+ strlen(".pbm")) * sizeof(char*));
        sprintf(fichier, "%s.pbm", output);
        if ((f_pxm = fopen(fichier, "wb")) == NULL)
        {
            printf("Impossible de créer %s\n", fichier);
            exit(EXIT_FAILURE);
        }
        free(fichier);
        /* on écrit l'en-tête */
        fprintf(f_pxm, "P4\n# %s make with SDL_pnm\n%d %d\n", output, input->w, input->h);
     
        int x, y, i = 0;
        Uint32 pix;
     
        unsigned char* data;
     
     
        /* Allocation de la 1er dimension */
        data = malloc(input->h*input->w*sizeof(unsigned char));
        for (y = 0; y < input->h ; y++)
        {
            for (x = 0; x < input->w; x++, i++)
            {
                pix = getpixel(input, x, y);
                Uint8 r, g, b;
                SDL_GetRGB(pix, input->format, &r, &g, &b);
     
                if ( ((r + g + b)/3)>125 )
                    data[i]= 1;
                else
                    data[i]= 0;
     
            }
     
        }
     
        int           bit;
        unsigned char c;
        unsigned char          *indexb;
     
        int           j;
        int           k;
     
        indexb = data;
        c = 0;
     
        for ( j = 0; j < input->h; j++ )
        {
            for ( i = 0; i < input->w; i++ )
            {
     
                k = 7 - i%8;
                bit = *indexb;
                c = c | ( bit << k );
     
                *indexb++;
     
                if ( (i+1)%8 == 0 || i == ( input->w - 1 ) )
                {
                    fputc ( c, f_pxm );
                    c = 0;
                }
     
            }
        }
     
     
        fclose(f_pxm);
     
     
    }
     
    void Write_PGM_RAW(SDL_Surface *input, char *output)
    {
        FILE *f_pxm = NULL;
        char *fichier = malloc((strlen(output)+ strlen(".pgm")) * sizeof(char*));
        sprintf(fichier, "%s.pgm", output);
        if ((f_pxm = fopen(fichier, "wb")) == NULL)
        {
            printf("Impossible de créer %s\n", fichier);
            exit(EXIT_FAILURE);
        }
        free(fichier);
        /* on écrit l'en-tête */
        fprintf(f_pxm, "P5\n# %s make with SDL_pnm\n%d %d\n%d\n", output, input->w, input->h, 255);
     
        int x, y;
        Uint32 pix;
        for (y = 0; y < input->h ; y++)
        {
            for (x = 0; x < input->w; x++)
            {
                pix = getpixel(input, x, y);
                Uint8 r, g, b;
                SDL_GetRGB(pix, input->format, &r, &g, &b);
                /* on considère que la couleur du pixel à sortir est la moyenne des couleurs RGB */
     
                fputc((r + g + b)/3, f_pxm);
            }
        }
        fclose(f_pxm);
     
    }
     
    void Write_PPM_RAW(SDL_Surface *input, char *output)
    {
        FILE *f_pxm = NULL;
        char *fichier = malloc((strlen(output)+ strlen(".ppm")) * sizeof(char*));
        sprintf(fichier, "%s.ppm", output);
        if ((f_pxm = fopen(fichier, "wb")) == NULL)
        {
            printf("Impossible de créer %s\n", fichier);
            exit(EXIT_FAILURE);
        }
        free(fichier);
        /* on écrit l'en-tête */
        fprintf(f_pxm, "P6\n# %s make with SDL_pnm\n%d %d\n%d\n", output, input->w, input->h, 255);
     
        int x, y;
        Uint32 pix;
        for (y = 0; y < input->h ; y++)
        {
            for (x = 0; x < input->w; x++)
            {
                pix = getpixel(input, x, y);
                Uint8 r, g, b;
                SDL_GetRGB(pix, input->format, &r, &g, &b);
     
                fputc((int)r, f_pxm);
                fputc((int)g, f_pxm);
                fputc((int)b, f_pxm);
            }
     
        }
        fclose(f_pxm);
     
    }
     
     
    SDL_Surface *Read_PBM_ASCII(const char* input)
    {
        FILE *f_pxm = NULL;
        if ((f_pxm = fopen(input, "r")) == NULL)
        {
            printf("Cannot open %s\n", input);
            exit(EXIT_FAILURE);
        }
        /* on écrit l'en-tête */
     
        int magic_number = 0;
        int largeur, hauteur;
     
        char *ligne = malloc(70 * sizeof(char*));
        if(ligne == NULL)
        {
            printf("Memory allocation failure.\n");
            exit(EXIT_FAILURE);
        }
        fgets(ligne, 70, f_pxm);
        sscanf(ligne, "P%d", &magic_number);
     
     
        fgets(ligne, 70, f_pxm);
        if (ligne[0] != '#')
        {
            sscanf(ligne, "%d %d", &largeur, &hauteur);
            fgets(ligne, 70, f_pxm);
     
        }
        else
        {
            fgets(ligne, 70, f_pxm);
            sscanf(ligne, "%d %d\n", &largeur, &hauteur);
     
        }
     
        SDL_Surface *img = NULL;
        img = SDL_CreateRGBSurface(SDL_HWSURFACE, largeur, hauteur, 32, 0, 0, 0, 0);
     
        int i;
        int **data;
        /* Allocation de la 1er dimension */
        data = malloc ( sizeof(*data)  *  hauteur);
        if(data == NULL)
        {
            printf("Memory allocation failure.\n");
            exit(EXIT_FAILURE);
        }
        /* Allocation des tableaux */
        for (i=0; i<hauteur; i++)
        {
            data[i] = malloc ( sizeof(**data) * largeur);
        }
     
     
        int x, y;
     
        for (y = 0; y < hauteur; y++)
        {
            for (x = 0; x < largeur; x++)
            {
                fscanf(f_pxm, "%d ", &data[y][x]);
                if(data[y][x] == 1)
                {
                    data[y][x] = 0;
                }
                else
                    data[y][x] = 255;
     
                putpixel(img, x, y, SDL_MapRGB(img->format, data[y][x], data[y][x], data[y][x]));
            }
        }
     
        fclose(f_pxm);
        free(ligne);
        free(data);
        return img;
    }
     
     
    SDL_Surface *Read_PGM_ASCII(const char* input)
    {
     
        FILE *f_pxm = NULL;
        if ((f_pxm = fopen(input, "r")) == NULL)
        {
            printf("Cannot open %s\n", input);
            exit(EXIT_FAILURE);
        }
        /* on écrit l'en-tête */
     
        int magic_number = 0;
        int largeur, hauteur, palette;
     
        char *ligne = malloc(70 * sizeof(char*));
        if(ligne == NULL)
        {
            printf("Memory allocation failure.\n");
            exit(EXIT_FAILURE);
        }
        fgets(ligne, 70, f_pxm);
        sscanf(ligne, "P%d", &magic_number);
     
        fgets(ligne, 70, f_pxm);
        if (ligne[0] != '#')
        {
     
     
     
            sscanf(ligne, "%d %d", &largeur, &hauteur);
            fgets(ligne, 70, f_pxm);
            sscanf(ligne, "%d", &palette);
        }
        else
        {
            fgets(ligne, 70, f_pxm);
            sscanf(ligne, "%d %d\n", &largeur, &hauteur);
            fgets(ligne, 70, f_pxm);
            sscanf(ligne, "%d\n", &palette);
        }
     
        SDL_Surface *img = NULL;
        img = SDL_CreateRGBSurface(SDL_HWSURFACE, largeur, hauteur, 32, 0, 0, 0, 0);
     
        int i;
        int **data;
        /* Allocation de la 1er dimension */
        data = malloc ( sizeof(*data)  *  hauteur);
        if(data == NULL)
        {
            printf("Memory allocation failure.\n");
            exit(EXIT_FAILURE);
        }
        /* Allocation des tableaux */
        for (i=0; i<hauteur; i++)
        {
            data[i] = malloc ( sizeof(**data) * largeur);
        }
     
     
        int x, y;
     
        for (y = 0; y < hauteur; y++)
        {
            for (x = 0; x < largeur; x++)
            {
                fscanf(f_pxm, "%d ", &data[y][x]);
                putpixel(img, x, y, SDL_MapRGB(img->format, data[y][x], data[y][x], data[y][x]));
            }
        }
     
        fclose(f_pxm);
        free(ligne);
        free(data);
        return img;
    }
     
     
    SDL_Surface *Read_PPM_ASCII(const char* input)
    {
     
        FILE *f_pxm = NULL;
        if ((f_pxm = fopen(input, "r")) == NULL)
        {
            printf("Cannot open %s\n", input);
            exit(EXIT_FAILURE);
        }
     
     
        /* on lit l'en-tête */
     
        int magic_number = 0;
        int largeur, hauteur, palette;
     
        char *ligne = malloc(70 * sizeof(char*));
        if(ligne == NULL)
        {
            printf("Memory allocation failure.\n");
            exit(EXIT_FAILURE);
        }
        fgets(ligne, 70, f_pxm);
        sscanf(ligne, "P%d", &magic_number);
     
        fgets(ligne, 70, f_pxm);
        if (ligne[0] != '#')
        {
     
     
            sscanf(ligne, "%d %d", &largeur, &hauteur);
            fgets(ligne, 70, f_pxm);
            sscanf(ligne, "%d", &palette);
        }
        else
        {
            fgets(ligne, 70, f_pxm);
            sscanf(ligne, "%d %d\n", &largeur, &hauteur);
            fgets(ligne, 70, f_pxm);
            sscanf(ligne, "%d\n", &palette);
        }
     
        SDL_Surface *img = NULL;
        img = SDL_CreateRGBSurface(SDL_HWSURFACE, largeur, hauteur, 32, 0, 0, 0, 0);
     
        int **data;
        /* Allocation de la 1er dimension */
        data = malloc ( sizeof(*data)  *  hauteur);
        if(data == NULL)
        {
            printf("Memory allocation failure.\n");
            exit(EXIT_FAILURE);
        }
        int i=0;
        /* Allocation des tableaux */
        for (i=0; i<hauteur; i++)
        {
            data[i] = malloc ( sizeof(**data) * largeur);
        }
     
        int x, y;
        int r,g,b;
        for (y = 0; y < hauteur; y++)
        {
            for (x = 0; x < largeur; x++)
            {
                fscanf(f_pxm, "%d ", &r);
                fscanf(f_pxm, "%d ", &g);
                fscanf(f_pxm, "%d ", &b);
                putpixel(img, x, y, SDL_MapRGB(img->format, r,g,b));
     
            }
        }
        free(ligne);
        free(data);
        fclose(f_pxm);
     
        return img;
    }
     
     
     
    SDL_Surface *Read_PBM_RAW(const char* input)
    {
        FILE *f_pxm = NULL;
        if ((f_pxm = fopen(input, "r")) == NULL)
        {
            printf("Cannot open %s\n", input);
            exit(EXIT_FAILURE);
        }
        /* on écrit l'en-tête */
     
        int magic_number = 0;
        int largeur, hauteur;
     
        char *ligne = malloc(70 * sizeof(char*));
        if(ligne == NULL)
        {
            printf("Memory allocation failure.\n");
            exit(EXIT_FAILURE);
        }
        fgets(ligne, 70, f_pxm);
        sscanf(ligne, "P%d", &magic_number);
     
        fgets(ligne, 70, f_pxm);
        if (ligne[0] != '#')
        {
     
     
     
            sscanf(ligne, "%d %d", &largeur, &hauteur);
        }
        else
        {
     
            fgets(ligne, 70, f_pxm);
            sscanf(ligne, "%d %d", &largeur, &hauteur);
        }
     
        SDL_Surface *img = NULL;
        img = SDL_CreateRGBSurface(SDL_HWSURFACE, largeur, hauteur, 32, 0, 0, 0, 0);
     
     
        unsigned char* data = malloc(hauteur*largeur*sizeof(*data));
        if(data == NULL)
        {
            printf("Memory allocation failure.\n");
            exit(EXIT_FAILURE);
        }
     
        int           bit;
        int           c;
        unsigned char c2=0;
        int           i;
        int           j;
        int           k;
        int           numbyte;
     
        numbyte = 0;
     
        for ( j = 0; j < hauteur; j++ )
        {
            for ( i = 0; i < largeur; i++ )
            {
     
                if ( i%8 == 0 )
                {
     
                    c = fgetc ( f_pxm );
     
                    if ( c == EOF )
                    {
                        printf ( "\n" );
                        printf ( "PBMB_READ_DATA: Failed reading data byte %d.\n", numbyte );
     
                    }
                    c2 = ( unsigned char ) c;
                    numbyte = numbyte + 1;
                }
     
                k = 7 - i%8;
                bit = ( c2 >> k )%2;
                if(bit ==1)
                    bit=255;
                else bit = 0;
                putpixel(img, i, j, SDL_MapRGB(img->format, (Uint8)bit, (Uint8)bit, (Uint8)bit));
     
                *data = bit;
                data = data + 1;
            }
     
        }
        fclose(f_pxm);
        free(ligne);
        free(data);
        return img;
    }
     
    SDL_Surface *Read_PGM_RAW(const char* input)
    {
     
        FILE *f_pxm = NULL;
        if ((f_pxm = fopen(input, "r")) == NULL)
        {
            printf("Cannot open %s\n", input);
            exit(EXIT_FAILURE);
        }
        /* on écrit l'en-tête */
     
        int magic_number = 0;
        int largeur, hauteur, palette;
     
        char *ligne = malloc(70 * sizeof(char*));
        if(ligne == NULL)
        {
            printf("Memory allocation failure.\n");
            exit(EXIT_FAILURE);
        }
        fgets(ligne, 70, f_pxm);
        sscanf(ligne, "P%d", &magic_number);
     
        fgets(ligne, 70, f_pxm);
        if (ligne[0] != '#')
        {
     
     
     
            sscanf(ligne, "%d %d", &largeur, &hauteur);
            fgets(ligne, 70, f_pxm);
            sscanf(ligne, "%d", &palette);
        }
        else
        {
            fgets(ligne, 70, f_pxm);
            sscanf(ligne, "%d %d\n", &largeur, &hauteur);
            fgets(ligne, 70, f_pxm);
            sscanf(ligne, "%d\n", &palette);
        }
     
        SDL_Surface *img = NULL;
        img = SDL_CreateRGBSurface(SDL_HWSURFACE, largeur, hauteur, 32, 0, 0, 0, 0);
     
     
        unsigned char* data;
     
        int nread;
        data = malloc(largeur*hauteur*sizeof(unsigned char));
        if(data == NULL)
        {
            printf("Memory allocation failure.\n");
            exit(EXIT_FAILURE);
        }
        nread = fread((void*)data, sizeof(unsigned char), largeur*hauteur, f_pxm);
        int x, y;
        int i;
        for (y = 0, i = 0; y < hauteur; y++)
        {
            for (x = 0; x < largeur; x++, i++)
            {
                putpixel(img, x, y, SDL_MapRGB(img->format, (Uint8)data[i],(Uint8)data[i],(Uint8)data[i]));
            }
        }
        free(ligne);
        free(data);
        fclose(f_pxm);
     
        return img;
    }
     
     
    SDL_Surface *Read_PPM_RAW(const char* input)
    {
     
        FILE *f_pxm = NULL;
        if ((f_pxm = fopen(input, "r")) == NULL)
        {
            printf("Cannot open %s\n", input);
            exit(EXIT_FAILURE);
        }
        /* on écrit l'en-tête */
     
        int magic_number = 0;
        int largeur, hauteur, palette;
     
        char *ligne = malloc(70 * sizeof(char*));
        if(ligne == NULL)
        {
            printf("Memory allocation failure.\n");
            ligne=NULL;
            free(ligne);
            return NULL;
        }
        fgets(ligne, 70, f_pxm);
        sscanf(ligne, "P%d", &magic_number);
     
        fgets(ligne, 70, f_pxm);
        if (ligne[0] != '#')
        {
     
     
     
            sscanf(ligne, "%d %d", &largeur, &hauteur);
            fgets(ligne, 70, f_pxm);
            sscanf(ligne, "%d", &palette);
        }
        else
        {
            fgets(ligne, 70, f_pxm);
            sscanf(ligne, "%d %d\n", &largeur, &hauteur);
            fgets(ligne, 70, f_pxm);
            sscanf(ligne, "%d\n", &palette);
        }
     
        SDL_Surface *img = NULL;
        img = SDL_CreateRGBSurface(SDL_HWSURFACE, largeur, hauteur, 32, 0, 0, 0, 0);
     
        unsigned char* data;
     
        int nread;
        data = malloc((largeur*2)*hauteur*3*sizeof(unsigned char));
        if(data == NULL)
        {
            printf("Memory allocation failure.\n");
            free(ligne);
            free(data);
            fclose(f_pxm);
            return NULL;
        }
        nread = fread((void*)data, sizeof(unsigned char), (largeur*2)*hauteur*3, f_pxm);
     
        int x, y;
        int i=0;
        for (y = 0; y < hauteur; y++)
        {
            for (x = 0; x < largeur; x++)
            {
                putpixel(img, x, y, SDL_MapRGB(img->format, data[i],data[i+1],data[i+2]));
                i+=3;
     
            }
        }
        free(ligne);
        free(data);
        fclose(f_pxm);
     
        return img;
    }

Discussions similaires

  1. Besoin de conseils sur la gestion d'images avec SDL
    Par mazertys17 dans le forum SDL
    Réponses: 12
    Dernier message: 01/11/2014, 18h40
  2. Coller une image avec sdl
    Par nourine1987 dans le forum SDL
    Réponses: 2
    Dernier message: 27/05/2010, 14h02
  3. image ppm vers pgm avec visual c++
    Par nawal1000 dans le forum Windows
    Réponses: 2
    Dernier message: 09/12/2007, 13h42
  4. Réponses: 6
    Dernier message: 15/11/2007, 12h31
  5. Probleme Avec Des Images Ppm
    Par nounou213 dans le forum C
    Réponses: 6
    Dernier message: 29/04/2007, 11h42

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