On dispose des fichiers pixmap_io.c et pixmap_io.h permettant de lire et d'écrire des images PGM/PPM. Cela se fait respectivement par les deux appels :
unsigned char *load_pixmap(char *filename, int *width, int *height); void store_pixmap(char *filename, unsigned char *data, int width, int height);ou :
int load_RGB_pixmap(char *filename, int *width, int *height, unsigned char**R_data, unsigned char**G_data, unsigned char**B_data);
void store_RGB_pixmap(char *filename, unsigned char *R_data, unsigned char *G_data, unsigned char *B_data, int width, int height);
La fonction load_pixmap prend en entrée un nom de fichier
à lire et retourne un pointeur sur les octets de l'image (ou
NULL en cas de problème).
Les dimensions sont rangées dans les paramètres width
et height.
La fonction store_pixmap prend en entrée un nom de
fichier à écrire, le pointeur sur les octets de l'image et
les dimensions de l'image.
La fonction load_RGB_pixmap affecte les 3 pointeurs (un pour
chaque composante RVB) sur les octets de l'image et retourne TRUE
(ou FALSE en cas de problème).
#include <stdio.h>
#include <stdlib.h>
#include "pixmap_io.h"
int main(int argc, char **argv)
{
unsigned char *data;
int width, height;
if( (argc > 1) && ((data = load_pixmap(argv[1], &width, &height)) != NULL) )
store_pixmap("clone.pgm", data, width, height);
return 0;
}
Voici un autre exemple de programme qui réalise une permutation des trois composantes d'une image :
#include <stdio.h>
#include <stdlib.h>
#include "pixmap_io.h"
int main(int argc, char **argv)
{
unsigned char *data_R, *data_G, *data_B;
int width, height;
if( (argc > 1) && load_RGB_pixmap(argv[1], &width, &height,
&data_R, &data_G, &data_B) )
store_RGB_pixmap("GBR.pnm", data_G, data_B, data_R, width, height);
return 0;
}
Cette expression convient également pour chaque composante d'une image PPM.
Pour toutes suggestions, commentaires ou remarques, email : Philippe.Chassignet@polytechnique.fr