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
| #include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <SDL/SDL.h>
#define NBPIXELLARG 400
#define NBPIXELHAUT 200
/******************************************************************************/
/* Change la couleur du pixel de coord (x,y) sur la surface */
/******************************************************************************/
void setPixel(SDL_Surface *surface,int x,int y,int rr,int gg,int bb) {
int bpp = surface->format->BytesPerPixel;
Uint32 pixel=SDL_MapRGB(surface->format,rr,gg,bb);
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;
}
}
/******************************************************************************/
/* affiche l'image */
/******************************************************************************/
int afficherImage(unsigned char matRGB[3][NBPIXELHAUT][NBPIXELLARG]) {
SDL_Surface *ecran=NULL;
SDL_Event event;
int continuer=1, i, j;
// Demarrage de la SDL
if ( SDL_Init(SDL_INIT_VIDEO) == -1 ) {
fprintf(stderr,"erreur d'initialisation de la SDL : %s\n",SDL_GetError());
return 1;
}
// Ouverture d'une fenetre
SDL_WM_SetCaption("image generee pixel par pixel",NULL);
ecran=SDL_SetVideoMode(NBPIXELLARG,NBPIXELHAUT,32,SDL_SWSURFACE | SDL_DOUBLEBUF);
if ( ecran == NULL ) {
fprintf(stderr,"impossible de charger le mode video : %s\n",SDL_GetError());
SDL_Quit();
return 1;
}
// On vide l'image (elle devient noire) : pas obligatoire
SDL_FillRect(ecran,NULL,SDL_MapRGB(ecran->format,0,0,0));
// On fixe les pixels un par un
SDL_LockSurface(ecran);
for (i=0;i<NBPIXELHAUT;i++) {
for (j=0;j<NBPIXELLARG;j++) {
setPixel(ecran,j,i,(int)matRGB[0][i][j],(int)matRGB[1][i][j],(int)matRGB[2][i][j]);
}
}
SDL_UnlockSurface(ecran);
SDL_Flip(ecran);
// Boucle d'evenements
while (continuer) {
SDL_WaitEvent(&event);
switch(event.type) {
case SDL_QUIT:
continuer=0;
break;
}
}
// Arret de la SDL
SDL_Quit();
return 0;
}
/******************************************************************************/
/* genere un entier aleatoire entre m et M */
/******************************************************************************/
int genererEntier(int m,int M) {
return (m + (int)((double)rand() / ((double)RAND_MAX + 1) * (M-m+1)) );
}
/******************************************************************************/
/* main */
/******************************************************************************/
int main(int argc, char *argv[]) {
int i, j;
unsigned char matRGB[3][NBPIXELHAUT][NBPIXELLARG];
srand((unsigned) time(NULL));
// matrice de rouge
for (i=0;i<NBPIXELHAUT;i++) {
for (j=0;j<NBPIXELLARG;j++) {
matRGB[0][i][j] = (unsigned char)genererEntier(0,255);
}
}
// matrice de vert
for (i=0;i<NBPIXELHAUT;i++) {
for (j=0;j<NBPIXELLARG;j++) {
matRGB[1][i][j] = (unsigned char)genererEntier(0,255);
}
}
// matrice de bleu
for (i=0;i<NBPIXELHAUT;i++) {
for (j=0;j<NBPIXELLARG;j++) {
matRGB[2][i][j] = (unsigned char)genererEntier(0,255);
}
}
if ( afficherImage(matRGB) ) {
fprintf(stderr,"erreur lors de l'affichage de l'image\n");
exit(EXIT_FAILURE);
}
return EXIT_SUCCESS;
} |