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 :

pixeliser une image


Sujet :

SDL

  1. #1
    Membre habitué
    Homme Profil pro
    sans emploi
    Inscrit en
    Février 2014
    Messages
    365
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 49
    Localisation : France, Maine et Loire (Pays de la Loire)

    Informations professionnelles :
    Activité : sans emploi
    Secteur : Conseil

    Informations forums :
    Inscription : Février 2014
    Messages : 365
    Points : 131
    Points
    131
    Par défaut pixeliser une image
    Bonjour

    Sur le site SDL l'exemple d'utilisation de la fonction SDL_SetWindowIcon est:

    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
    #include "SDL.h"
     
    int main(int argc, char* argv[]){
     
      SDL_Event e;
     
      SDL_Init(SDL_INIT_VIDEO);   // Initialize SDL2
     
      SDL_Window *window = SDL_CreateWindow(    // Open a new window
        "SDL2 window icon demo", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_RESIZABLE
      );
     
      SDL_Surface *surface;     // Declare an SDL_Surface to be filled in with pixel data from an image file
      Uint16 pixels[16*16] = {  // ...or with raw pixel data:
        0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff,
        0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff,
        0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff,
        0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff,
        0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff,
        0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff,
        0x0fff, 0x0aab, 0x0789, 0x0bcc, 0x0eee, 0x09aa, 0x099a, 0x0ddd,
        0x0fff, 0x0eee, 0x0899, 0x0fff, 0x0fff, 0x1fff, 0x0dde, 0x0dee,
        0x0fff, 0xabbc, 0xf779, 0x8cdd, 0x3fff, 0x9bbc, 0xaaab, 0x6fff,
        0x0fff, 0x3fff, 0xbaab, 0x0fff, 0x0fff, 0x6689, 0x6fff, 0x0dee,
        0xe678, 0xf134, 0x8abb, 0xf235, 0xf678, 0xf013, 0xf568, 0xf001,
        0xd889, 0x7abc, 0xf001, 0x0fff, 0x0fff, 0x0bcc, 0x9124, 0x5fff,
        0xf124, 0xf356, 0x3eee, 0x0fff, 0x7bbc, 0xf124, 0x0789, 0x2fff,
        0xf002, 0xd789, 0xf024, 0x0fff, 0x0fff, 0x0002, 0x0134, 0xd79a,
        0x1fff, 0xf023, 0xf000, 0xf124, 0xc99a, 0xf024, 0x0567, 0x0fff,
        0xf002, 0xe678, 0xf013, 0x0fff, 0x0ddd, 0x0fff, 0x0fff, 0xb689,
        0x8abb, 0x0fff, 0x0fff, 0xf001, 0xf235, 0xf013, 0x0fff, 0xd789,
        0xf002, 0x9899, 0xf001, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0xe789,
        0xf023, 0xf000, 0xf001, 0xe456, 0x8bcc, 0xf013, 0xf002, 0xf012,
        0x1767, 0x5aaa, 0xf013, 0xf001, 0xf000, 0x0fff, 0x7fff, 0xf124,
        0x0fff, 0x089a, 0x0578, 0x0fff, 0x089a, 0x0013, 0x0245, 0x0eff,
        0x0223, 0x0dde, 0x0135, 0x0789, 0x0ddd, 0xbbbc, 0xf346, 0x0467,
        0x0fff, 0x4eee, 0x3ddd, 0x0edd, 0x0dee, 0x0fff, 0x0fff, 0x0dee,
        0x0def, 0x08ab, 0x0fff, 0x7fff, 0xfabc, 0xf356, 0x0457, 0x0467,
        0x0fff, 0x0bcd, 0x4bde, 0x9bcc, 0x8dee, 0x8eff, 0x8fff, 0x9fff,
        0xadee, 0xeccd, 0xf689, 0xc357, 0x2356, 0x0356, 0x0467, 0x0467,
        0x0fff, 0x0ccd, 0x0bdd, 0x0cdd, 0x0aaa, 0x2234, 0x4135, 0x4346,
        0x5356, 0x2246, 0x0346, 0x0356, 0x0467, 0x0356, 0x0467, 0x0467,
        0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff,
        0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff,
        0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff,
        0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff
      };
      surface = SDL_CreateRGBSurfaceFrom(pixels,16,16,16,16*2,0x0f00,0x00f0,0x000f,0xf000);
     
      // The icon is attached to the window pointer
      SDL_SetWindowIcon(window, surface);
     
      // ...and the surface containing the icon pixel data is no longer required.
      SDL_FreeSurface(surface);
     
      // Loop until the user closes the window or presses any key.
      for( ; e.type != SDL_QUIT && e.type != SDL_KEYDOWN; SDL_PollEvent(&e));
     
      SDL_DestroyWindow(window);   // Close and destroy the window.
      SDL_Quit();                  // Clean up and exit.
      return 0;
     
    }
    L'icone est créer avec des pixels Uint16 pixels[16*16] = {0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, ....

    J'ai crée un icone avec Inkscape. Existe t'il un moyen de récupérer le code de pixel de cette image?

    Cordialement

  2. #2
    Expert éminent sénior
    Avatar de Mat.M
    Profil pro
    Développeur informatique
    Inscrit en
    Novembre 2006
    Messages
    8 361
    Détails du profil
    Informations personnelles :
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Novembre 2006
    Messages : 8 361
    Points : 20 379
    Points
    20 379
    Par défaut
    InkScape permet de sauvegarder des bitmpas au format PNG
    Convertir l'image au format BMP avec Paint Par exemple sous Windows en 16bits.
    Après créer un petit programme qui appelle SDL_LoadBMP et l'image chargée la mettre dans une SDL_Surface
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    const char *image_path = "myimage.bmp";
    SDL_Surface *image = SDL_LoadBMP(image_path);
     
    /* Let the user know if the file failed to load */
    if (!image) { printf("Failed to load image at %s: %s\n", image_path, SDL_GetError());   return;
    }
    Ensuite chaque pixel lu il faut l'écrire dans un fichier texte
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     
    	FILE *fFichier=fopen("icone.tx","w+t");
    	SDL_LockSurface(surface);
            int longueur_fichier=surface->h * surface->w;
            for(int i=0;i<longueur_fichier;i++)
           {
    	Uint16 *pixel=surface->pixels;
    	char valeur_pixel[20];
    	sprintf(valeur,"%i",pixel);///// VERIFIER format %i
    	fprintf(fFichier,valeur) ;
           }
            SDL_UnlockSurface(surface);
    	fclose(fFichier);
            SDL_FreeSurface(image);

    J'ai tapé ça rapidement après faut vérifier si tout fonctionne bien
    Prendre en compte surface->pitch au besoin

  3. #3
    Membre habitué
    Homme Profil pro
    sans emploi
    Inscrit en
    Février 2014
    Messages
    365
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 49
    Localisation : France, Maine et Loire (Pays de la Loire)

    Informations professionnelles :
    Activité : sans emploi
    Secteur : Conseil

    Informations forums :
    Inscription : Février 2014
    Messages : 365
    Points : 131
    Points
    131
    Par défaut
    avec
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Uint16 *pixel = image->pixels;
    le message d'erreur
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    src/pixeliser_image3.cpp:72:36: error: invalid conversion from 'void*' to 'Uint16*' {aka 'short unsigned int*'} [-fpermissive]
       72 |             Uint16 *pixel = image->pixels;
    apparait

    avec
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    void *pixel = image->pixels;
    ça fonctionne:
    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
    // instruction préprocesseur: recopier le code qui se trouve dans le ficher
    //========================================================================
    #include <SDL.h>
    #include <SDL_image.h>		// import image de format different de bmp
    #include <SDL_ttf.h>
    #include <stdio.h>
    #include <iostream>
     
    // declaration, initialisation de variables
    //=========================================
     
    // dimension ecran
    const int SCREEN_WIDTH = 640;
    const int SCREEN_HEIGHT = 440;
     
    // The window we'll be rendering to
    SDL_Window* gWindow = NULL;
    // The window renderer
    SDL_Renderer* gRenderer = NULL;
    // Globally used font
    TTF_Font *font = NULL;
     
    // couleur
    SDL_Color couleur_vert = {130, 250, 0, 1};
    SDL_Color couleur_texte = {5, 50, 200};
     
    // fonction principale
    //====================
    int main( int argc, char* args[] )
    {
     
    	//Initialize SDL
        SDL_Init( SDL_INIT_VIDEO );
    	//Set texture filtering to linear
    	SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" );
    	//Create window
    	gWindow = SDL_CreateWindow( "Table de multiplication", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
     
    	//Create vsynced renderer for window
    	gRenderer = SDL_CreateRenderer( gWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC );
    	//Initialize renderer color
    	//SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
    	SDL_SetRenderDrawColor( gRenderer, couleur_vert.r, couleur_vert.g, couleur_vert.b, couleur_vert.a);
    	//Initialize PNG loading
    	int imgFlags = IMG_INIT_PNG;
    	IMG_Init( imgFlags );
    	//Initialize SDL_ttf
    	TTF_Init();
    	//Load media
        //Open the font
    	font = TTF_OpenFont( "arial.ttf", 25 );
     
        // charger image au format BMP
        const char *image_path = "logo_bpm_25_pixels.bmp";
        // mettre image dans une variable de type SDL_Surface
        SDL_Surface *image = SDL_LoadBMP(image_path);
        // verification
        if (!image) 
        { 
            printf("Failed to load image at %s: %s\n", image_path, SDL_GetError());
        }
     
        // écrire chaque pixel dans un fichier texte
    	FILE *fFichier=fopen("icone.tx","w+t");
     
        // SDL_LockSurface: Set up a surface for directly accessing the pixels.
    	SDL_LockSurface(image);  
        int longueur_fichier=image->h * image->w;   // <=> nombre de pixels
        std::cout << "longueur_fichier: " << longueur_fichier << std::endl;
        for(int i=0; i<longueur_fichier; i++)
           {
                void *pixel = image->pixels;
                char valeur_pixel[20];
                std::cout << "image->pixels: " << image->pixels << std::endl;
                sprintf(valeur_pixel,"%i",pixel);
                fprintf(fFichier,valeur_pixel) ;
           }
     
        // SDL_UnlockSurface: Release a surface after directly accessing the pixels.
        SDL_UnlockSurface(image);
    	fclose(fFichier);
        SDL_FreeSurface(image);
     
    	//Main loop flag
    	bool quit = false;
     
    	//Event handler
    	SDL_Event e;
     
    	//While application is running
    	while( !quit )
    	{
    		//The rerender text flag
    		bool renderText = false;
            bool bouton_egal_clique = false;
     
    		//Handle events on queue
    		while( SDL_PollEvent( &e ) != 0 )
    		{
    			//User requests quit
    			if( e.type == SDL_QUIT )
    			{
    				quit = true;
    			}
            }
    	}
     
    	//Free global font
    	TTF_CloseFont( font );
    	font = NULL;
     
    	//Destroy window	
    	SDL_DestroyRenderer( gRenderer );
    	SDL_DestroyWindow( gWindow );
    	gWindow = NULL;
    	gRenderer = NULL;
     
    	//Quit SDL subsystems
    	TTF_Quit();
    	IMG_Quit();
    	SDL_Quit();
     
    	return 0;
    }
    j'obtiens:
    - dans la console:
    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
    longueur_fichier: 25        
    image->pixels: 0x21b6d3bc520
    image->pixels: 0x21b6d3bc520
    image->pixels: 0x21b6d3bc520
    image->pixels: 0x21b6d3bc520
    image->pixels: 0x21b6d3bc520
    image->pixels: 0x21b6d3bc520
    image->pixels: 0x21b6d3bc520
    image->pixels: 0x21b6d3bc520
    image->pixels: 0x21b6d3bc520
    image->pixels: 0x21b6d3bc520
    image->pixels: 0x21b6d3bc520
    image->pixels: 0x21b6d3bc520
    image->pixels: 0x21b6d3bc520
    image->pixels: 0x21b6d3bc520
    image->pixels: 0x21b6d3bc520
    image->pixels: 0x21b6d3bc520
    image->pixels: 0x21b6d3bc520
    image->pixels: 0x21b6d3bc520
    image->pixels: 0x21b6d3bc520
    image->pixels: 0x21b6d3bc520
    image->pixels: 0x21b6d3bc520
    image->pixels: 0x21b6d3bc520
    image->pixels: 0x21b6d3bc520
    image->pixels: 0x21b6d3bc520
    image->pixels: 0x21b6d3bc520
    - dans le fichier texte: 1832633632183263363218326336321832633632183263363218326336321832633632183263363218326336321832633632183263363218326336321832633632183263363218326336321832633632183263363218326336321832633632183263363218326336321832633632183263363218326336321832633632

    est ce que c'est bon?

  4. #4
    Expert éminent sénior
    Avatar de Mat.M
    Profil pro
    Développeur informatique
    Inscrit en
    Novembre 2006
    Messages
    8 361
    Détails du profil
    Informations personnelles :
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Novembre 2006
    Messages : 8 361
    Points : 20 379
    Points
    20 379
    Par défaut
    le code que j'ai fait je l'ai tapé rapidement donc les données écrites dans le fichier texte il faut voir comment séparer les valeurs avec une virgule.
    Ensuite faire un copier-coller dans le fichier source.

  5. #5
    Membre habitué
    Homme Profil pro
    sans emploi
    Inscrit en
    Février 2014
    Messages
    365
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 49
    Localisation : France, Maine et Loire (Pays de la Loire)

    Informations professionnelles :
    Activité : sans emploi
    Secteur : Conseil

    Informations forums :
    Inscription : Février 2014
    Messages : 365
    Points : 131
    Points
    131
    Par défaut
    le séparateur est " ,"
    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
    // instruction préprocesseur: recopier le code qui se trouve dans le ficher
    //========================================================================
    #include <SDL.h>
    #include <SDL_image.h>		// import image de format different de bmp
    #include <SDL_ttf.h>
    #include <stdio.h>
    #include <iostream>
     
    // declaration, initialisation de variables
    //=========================================
     
    // dimension ecran
    const int SCREEN_WIDTH = 640;
    const int SCREEN_HEIGHT = 440;
     
    // The window we'll be rendering to
    SDL_Window* gWindow = NULL;
    // The window renderer
    SDL_Renderer* gRenderer = NULL;
    // Globally used font
    TTF_Font *font = NULL;
     
    // couleur
    SDL_Color couleur_vert = {130, 250, 0, 1};
    SDL_Color couleur_texte = {5, 50, 200};
     
    // fonction principale
    //====================
    int main( int argc, char* args[] )
    {
     
    	//Initialize SDL
        SDL_Init( SDL_INIT_VIDEO );
    	//Set texture filtering to linear
    	SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" );
    	//Create window
    	gWindow = SDL_CreateWindow( "Table de multiplication", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
     
    	//Create vsynced renderer for window
    	gRenderer = SDL_CreateRenderer( gWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC );
    	//Initialize renderer color
    	//SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
    	SDL_SetRenderDrawColor( gRenderer, couleur_vert.r, couleur_vert.g, couleur_vert.b, couleur_vert.a);
    	//Initialize PNG loading
    	int imgFlags = IMG_INIT_PNG;
    	IMG_Init( imgFlags );
    	//Initialize SDL_ttf
    	TTF_Init();
    	//Load media
        //Open the font
    	font = TTF_OpenFont( "arial.ttf", 25 );
     
        // charger image au format BMP
        const char *image_path = "logo_bpm_25_pixels.bmp";
        // mettre image dans une variable de type SDL_Surface
        SDL_Surface *image = SDL_LoadBMP(image_path);
        // verification
        if (!image) 
        { 
            printf("Failed to load image at %s: %s\n", image_path, SDL_GetError());
        }
     
        // écrire chaque pixel dans un fichier texte
    	FILE *fFichier=fopen("icone.tx","w+t");
     
        // SDL_LockSurface: Set up a surface for directly accessing the pixels.
    	SDL_LockSurface(image);  
        int longueur_fichier=image->h * image->w;   // <=> nombre de pixels
        std::cout << "longueur_fichier: " << longueur_fichier << std::endl;
        for(int i=0; i<longueur_fichier; i++)
           {
                void *pixel = image->pixels;
                char valeur_pixel[25];
                std::cout << "image->pixels: " << image->pixels << std::endl;
                sprintf(valeur_pixel,"%i, ",pixel);
                fprintf(fFichier,valeur_pixel) ;
           }
     
        // SDL_UnlockSurface: Release a surface after directly accessing the pixels.
        SDL_UnlockSurface(image);
    	fclose(fFichier);
        SDL_FreeSurface(image);
     
    	//Main loop flag
    	bool quit = false;
     
    	//Event handler
    	SDL_Event e;
     
    	//While application is running
    	while( !quit )
    	{
    		//The rerender text flag
    		bool renderText = false;
            bool bouton_egal_clique = false;
     
    		//Handle events on queue
    		while( SDL_PollEvent( &e ) != 0 )
    		{
    			//User requests quit
    			if( e.type == SDL_QUIT )
    			{
    				quit = true;
    			}
            }
    	}
     
    	//Free global font
    	TTF_CloseFont( font );
    	font = NULL;
     
    	//Destroy window	
    	SDL_DestroyRenderer( gRenderer );
    	SDL_DestroyWindow( gWindow );
    	gWindow = NULL;
    	gRenderer = NULL;
     
    	//Quit SDL subsystems
    	TTF_Quit();
    	IMG_Quit();
    	SDL_Quit();
     
    	return 0;
    }
    puis je colle les valeur du fichier texte dans le programme d'affichage des pixels

    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
    #include "SDL.h"
     
    int main(int argc, char* argv[]){
    Uint32   rmask = 0x000000ff;
    Uint32   gmask = 0x0000ff00;
    Uint32   bmask = 0x00ff0000;
    Uint32   amask = (req_format == STBI_rgb) ? 0 : 0xff000000;
     
      SDL_Event e;
     
      SDL_Init(SDL_INIT_VIDEO);   // Initialize SDL2
     
      SDL_Window *window = SDL_CreateWindow(    // Open a new window
        "SDL2 window icon demo", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_RESIZABLE
      );
     
      SDL_Surface *surface;     // Declare an SDL_Surface to be filled in with pixel data from an image file
      Uint16 pixels[16*16] = {  // ...or with raw pixel data:
    280937760, 280937760, 280937760, 280937760, 280937760, 
    280937760, 280937760, 280937760, 280937760, 280937760, 
    280937760, 280937760, 280937760, 280937760, 280937760, 
    280937760, 280937760, 280937760, 280937760, 280937760, 
    280937760, 280937760, 280937760, 280937760, 280937760
      };
      //surface = SDL_CreateRGBSurfaceFrom(pixels,16,16,16,16*2,0x0f00,0x00f0,0x000f,0xf000);
      surface = SDL_CreateRGBSurfaceFrom(pixels,16,16,16,16*2,0x0f00,0x00f0,0x000f,0xf000);
     
      // The icon is attached to the window pointer
      SDL_SetWindowIcon(window, surface);
     
      // ...and the surface containing the icon pixel data is no longer required.
      SDL_FreeSurface(surface);
     
      // Loop until the user closes the window or presses any key.
      for( ; e.type != SDL_QUIT && e.type != SDL_KEYDOWN; SDL_PollEvent(&e));
     
      SDL_DestroyWindow(window);   // Close and destroy the window.
      SDL_Quit();                  // Clean up and exit.
      return 0;
     
    }
    j'obtiens le message
    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
    src/test_creer_icone.cpp:29:3: error: narrowing conversion of '280937760' from 'int' to 'Uint16' {aka 'short unsigned int'} [-Wnarrowing]
       29 |   };
          |   ^
    src/test_creer_icone.cpp:29:3: error: narrowing conversion of '280937760' from 'int' to 'Uint16' {aka 'short unsigned int'} [-Wnarrowing]
    src/test_creer_icone.cpp:29:3: error: narrowing conversion of '280937760' from 'int' to 'Uint16' {aka 'short unsigned int'} [-Wnarrowing]
    src/test_creer_icone.cpp:29:3: error: narrowing conversion of '280937760' from 'int' to 'Uint16' {aka 'short unsigned int'} [-Wnarrowing]
    src/test_creer_icone.cpp:29:3: error: narrowing conversion of '280937760' from 'int' to 'Uint16' {aka 'short unsigned int'} [-Wnarrowing]
    src/test_creer_icone.cpp:29:3: error: narrowing conversion of '280937760' from 'int' to 'Uint16' {aka 'short unsigned int'} [-Wnarrowing]
    src/test_creer_icone.cpp:29:3: error: narrowing conversion of '280937760' from 'int' to 'Uint16' {aka 'short unsigned int'} [-Wnarrowing]
    src/test_creer_icone.cpp:29:3: error: narrowing conversion of '280937760' from 'int' to 'Uint16' {aka 'short unsigned int'} [-Wnarrowing]
    src/test_creer_icone.cpp:29:3: error: narrowing conversion of '280937760' from 'int' to 'Uint16' {aka 'short unsigned int'} [-Wnarrowing]
    src/test_creer_icone.cpp:29:3: error: narrowing conversion of '280937760' from 'int' to 'Uint16' {aka 'short unsigned int'} [-Wnarrowing]
    src/test_creer_icone.cpp:29:3: error: narrowing conversion of '280937760' from 'int' to 'Uint16' {aka 'short unsigned int'} [-Wnarrowing]
    src/test_creer_icone.cpp:29:3: error: narrowing conversion of '280937760' from 'int' to 'Uint16' {aka 'short unsigned int'} [-Wnarrowing]
    src/test_creer_icone.cpp:29:3: error: narrowing conversion of '280937760' from 'int' to 'Uint16' {aka 'short unsigned int'} [-Wnarrowing]
    src/test_creer_icone.cpp:29:3: error: narrowing conversion of '280937760' from 'int' to 'Uint16' {aka 'short unsigned int'} [-Wnarrowing]
    src/test_creer_icone.cpp:29:3: error: narrowing conversion of '280937760' from 'int' to 'Uint16' {aka 'short unsigned int'} [-Wnarrowing]
    src/test_creer_icone.cpp:29:3: error: narrowing conversion of '280937760' from 'int' to 'Uint16' {aka 'short unsigned int'} [-Wnarrowing]
    src/test_creer_icone.cpp:29:3: error: narrowing conversion of '280937760' from 'int' to 'Uint16' {aka 'short unsigned int'} [-Wnarrowing]
    src/test_creer_icone.cpp:29:3: error: narrowing conversion of '280937760' from 'int' to 'Uint16' {aka 'short unsigned int'} [-Wnarrowing]
    src/test_creer_icone.cpp:29:3: error: narrowing conversion of '280937760' from 'int' to 'Uint16' {aka 'short unsigned int'} [-Wnarrowing]
    src/test_creer_icone.cpp:29:3: error: narrowing conversion of '280937760' from 'int' to 'Uint16' {aka 'short unsigned int'} [-Wnarrowing]
    src/test_creer_icone.cpp:29:3: error: narrowing conversion of '280937760' from 'int' to 'Uint16' {aka 'short unsigned int'} [-Wnarrowing]
    src/test_creer_icone.cpp:29:3: error: narrowing conversion of '280937760' from 'int' to 'Uint16' {aka 'short unsigned int'} [-Wnarrowing]
    src/test_creer_icone.cpp:29:3: error: narrowing conversion of '280937760' from 'int' to 'Uint16' {aka 'short unsigned int'} [-Wnarrowing]
    src/test_creer_icone.cpp:29:3: error: narrowing conversion of '280937760' from 'int' to 'Uint16' {aka 'short unsigned int'} [-Wnarrowing]
    src/test_creer_icone.cpp:29:3: error: narrowing conversion of '280937760' from 'int' to 'Uint16' {aka 'short unsigned int'} [-Wnarrowing]

  6. #6
    Expert éminent sénior
    Avatar de Mat.M
    Profil pro
    Développeur informatique
    Inscrit en
    Novembre 2006
    Messages
    8 361
    Détails du profil
    Informations personnelles :
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Novembre 2006
    Messages : 8 361
    Points : 20 379
    Points
    20 379
    Par défaut
    si vous utilisez cout<< bref les flux stream du C++ ,il n'est pas nécessaire d'utiliser fprintf ou sprintf ; regarder dans les tutos des alternatives pour avoir du code homogène.
    Ensuite attention un entier int signé c'est de -32,767 à +32,767 donc les valeurs récupérées dans le ficher texte sont trop grandes.
    Mais au moins on avance ça écrit des valeurs dans un fichier texte

  7. #7
    Membre habitué
    Homme Profil pro
    sans emploi
    Inscrit en
    Février 2014
    Messages
    365
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 49
    Localisation : France, Maine et Loire (Pays de la Loire)

    Informations professionnelles :
    Activité : sans emploi
    Secteur : Conseil

    Informations forums :
    Inscription : Février 2014
    Messages : 365
    Points : 131
    Points
    131
    Par défaut
    bonjour

    j'ai
    - crée l'image Nom : logo_64_pixels_png.png
Affichages : 110
Taille : 259 octets (png. on peut pas importer de bmp)
    - pixelisé l'image
    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
    // instruction préprocesseur: recopier le code qui se trouve dans le ficher
    //========================================================================
    #include <SDL.h>
    #include <SDL_image.h>		// import image de format different de bmp
    #include <SDL_ttf.h>
    #include <stdio.h>
    #include <iostream>
     
    // declaration, initialisation de variables
    //=========================================
     
    // dimension ecran
    const int SCREEN_WIDTH = 640;
    const int SCREEN_HEIGHT = 440;
     
    // The window we'll be rendering to
    SDL_Window* gWindow = NULL;
    // The window renderer
    SDL_Renderer* gRenderer = NULL;
    // Globally used font
    TTF_Font *font = NULL;
     
    // couleur
    SDL_Color couleur_vert = {130, 250, 0, 1};
    SDL_Color couleur_texte = {5, 50, 200};
     
    // fonction principale
    //====================
    int main( int argc, char* args[] )
    {
    	//Initialize SDL
        SDL_Init( SDL_INIT_VIDEO );
    	//Set texture filtering to linear
    	SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" );
    	//Create window
    	gWindow = SDL_CreateWindow( "Table de multiplication", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
     
    	//Create vsynced renderer for window
    	gRenderer = SDL_CreateRenderer( gWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC );
    	//Initialize renderer color
    	//SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
    	SDL_SetRenderDrawColor( gRenderer, couleur_vert.r, couleur_vert.g, couleur_vert.b, couleur_vert.a);
    	//Initialize PNG loading
    	int imgFlags = IMG_INIT_PNG;
    	IMG_Init( imgFlags );
    	//Initialize SDL_ttf
    	TTF_Init();
    	//Load media
        //Open the font
    	font = TTF_OpenFont( "arial.ttf", 25 );
     
        // charger image au format BMP
        const char *image_path = "logo_64_pixels_bmp.bmp";
        // mettre image dans une variable de type SDL_Surface
        SDL_Surface *image = SDL_LoadBMP(image_path);
        // verification
        if (!image) 
        { 
            printf("Failed to load image at %s: %s\n", image_path, SDL_GetError());
        }
     
    	// nbOctetsParPixel: nombre d'octets utilisés pour stocker un pixel.
        // profondeur de couleur de l'image : 8, 16, 24 ou 32 bits = nbOctetsParPixel * 8 (un octet = 8 bits)
    	int nbOctetsParPixel = image->format->BytesPerPixel;
    	std::cout << "BytesPerPixel: " << nbOctetsParPixel << std::endl;
    	// BytesPerPixel: 3
     
    	// profondeur: depth
    	int profondeur = nbOctetsParPixel * 8;
    	std::cout << "profondeur: depth: " << profondeur << std::endl;
    	// profondeur: 24
     
        // écrire chaque pixel dans un fichier texte
    	FILE *fFichier=fopen("icone_64_pixels.tx","w+t");
     
        // SDL_LockSurface: Set up a surface for directly accessing the pixels.
    	SDL_LockSurface(image);  
        int longueur_fichier=image->h * image->w;   // <=> nombre de pixels
        std::cout << "longueur_fichier: " << longueur_fichier << std::endl;
        for(int i=0; i<longueur_fichier; i++)
           {
                void *pixel = image->pixels;
                char valeur_pixel[25];
                std::cout << "image->pixels: " << image->pixels << std::endl;
                sprintf(valeur_pixel,"%i, ",pixel);
                fprintf(fFichier,valeur_pixel);
           }
     
        // SDL_UnlockSurface: Release a surface after directly accessing the pixels.
        SDL_UnlockSurface(image);
    	fclose(fFichier);
        SDL_FreeSurface(image);
     
    	//Main loop flag
    	bool quit = false;
     
    	//Event handler
    	SDL_Event e;
     
    	//While application is running
    	while( !quit )
    	{
    		//The rerender text flag
    		bool renderText = false;
            bool bouton_egal_clique = false;
     
    		//Handle events on queue
    		while( SDL_PollEvent( &e ) != 0 )
    		{
    			//User requests quit
    			if( e.type == SDL_QUIT )
    			{
    				quit = true;
    			}
            }
    	}
     
    	//Free global font
    	TTF_CloseFont( font );
    	font = NULL;
     
    	//Destroy window	
    	SDL_DestroyRenderer( gRenderer );
    	SDL_DestroyWindow( gWindow );
    	gWindow = NULL;
    	gRenderer = NULL;
     
    	//Quit SDL subsystems
    	TTF_Quit();
    	IMG_Quit();
    	SDL_Quit();
     
    	return 0;
    }
    - entré les codes pour créer l'icone (en remplaçant Uint16 par unsigned long int)
    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
    #include "SDL.h"
     
    int main(int argc, char* argv[]){
     
      SDL_Event e;
     
      SDL_Init(SDL_INIT_VIDEO);   // Initialize SDL2
     
      SDL_Window *window = SDL_CreateWindow(    // Open a new window
        "SDL2 window icon demo", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_RESIZABLE
      );
     
      SDL_Surface *surface;     // Declare an SDL_Surface to be filled in with pixel data from an image file
      long long int pixels[8*8] = {  // ...or with raw pixel data: 8 pixels par ligne * 8 lignes
      // 4 octet rgba par pixel => 4 octets * 8 pixels = 32 octets par lignes
     
        0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  
        0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  
        0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  
        0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  
        0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  
        0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  
        0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  
        0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520
     
      };
      //SDL_Surface* SDL_CreateRGBSurfaceFrom(void *pixels,     a pointer to existing pixel data
      //                                  int width,            the width of the surface
      //                                  int height,           the height of the surface
      //                                  int depth,            the depth of the surface in bits
      //                                  int pitch,            the pitch of the surface in bytes: Pitch is the distance, in bytes, 
      //                                                        between two memory addresses that represent the beginning of one bitmap line
      //                                                        La longueur d'une ligne de pixels en octets (lecture seule)
      //                                                        1 octet = 8 bits. 1 pixel = 3 octets. => 1 pixel = 3*8 = 24 octets
      //                                                        8 pixels par ligne = 8*24 = 192 octets
      //                                                        and the beginning of the next bitmap line
      //                                  Uint32 Rmask,         the red mask for the pixels
      //                                  Uint32 Gmask,         the green mask for the pixels
      //                                  Uint32 Bmask,         the blue mask for the pixels
      //                                  Uint32 Amask);        the alpha mask for the pixels
      surface = SDL_CreateRGBSurfaceFrom(pixels,8,8,24,192,0x0f00,0x00f0,0x000f,0xf000);
     
      // The icon is attached to the window pointer
      SDL_SetWindowIcon(window, surface);
     
      // ...and the surface containing the icon pixel data is no longer required.
      SDL_FreeSurface(surface);
     
      // Loop until the user closes the window or presses any key.
      for( ; e.type != SDL_QUIT && e.type != SDL_KEYDOWN; SDL_PollEvent(&e));
     
      SDL_DestroyWindow(window);   // Close and destroy the window.
      SDL_Quit();                  // Clean up and exit.
      return 0;
    }
    et l'icone n'apparait pas

    ca ne marche pas non plus avec
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
      long long int pixels[8*8] = {  // ...or with raw pixel data: 8 pixels par ligne * 8 lignes
     
    0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  
    0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  
    0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  
    0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  
    0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  
    0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  
    0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  
    0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520,  0x1967b42c520
    pourquoi j'ai le même code "499696928" ou "0x1967b42c520"?

Discussions similaires

  1. [XL-2010] Pixeliser une image
    Par Cruch dans le forum Excel
    Réponses: 10
    Dernier message: 04/02/2018, 23h09
  2. Pixeliser une image avec la library PIL
    Par takeamic dans le forum Général Python
    Réponses: 6
    Dernier message: 24/01/2016, 14h28
  3. Pixelisation d'image lors d'une rotation
    Par univscien dans le forum 2D
    Réponses: 19
    Dernier message: 08/11/2009, 16h08
  4. Pixeliser une image
    Par Core8 dans le forum C
    Réponses: 7
    Dernier message: 23/03/2007, 15h49
  5. lire une image au format RAW
    Par Anonymous dans le forum OpenGL
    Réponses: 5
    Dernier message: 20/05/2002, 00h11

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