Bonjour,
je cherche à copier une partiel d'une image dans une autre.
Par exemple, pour une image binaire, remplacer les pixels noirs par ceux d'une autre image.
Mon programme fonctionne, mais bizarrement, lors de la copie, mon image copié laisse des "traces". Je pense que cela doit venir lors du test, mais bon...
main.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
 
 
    #include <stdlib.h>
    #include <gtk/gtk.h>
    #include "fonction.h"
 
 
 
    int main(int argc, char **argv)
    {
        GtkWidget *pWindow;
        GtkWidget *pVBox;
//        GtkWidget *pImage;
//        GtkWidget *pImage_texture;
        GtkWidget * pbutton;
        Image_struc* image_st;image_st = g_malloc(sizeof(Image_struc));
 
        /*initialisation de gtk*/
        gtk_init(&argc, &argv);
 
        pWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
 
        /*La fenetre pourra etre redimmensionnable*/
        gtk_window_set_resizable(GTK_WINDOW(pWindow),TRUE);
 
        /// Definition de la position
        gtk_window_set_position(GTK_WINDOW(pWindow), GTK_WIN_POS_CENTER);
 
        gtk_window_set_default_size(GTK_WINDOW(pWindow), 1024, 768);
        gtk_window_set_title(GTK_WINDOW(pWindow), "Ma fenetre");
        g_signal_connect(G_OBJECT(pWindow), "destroy", G_CALLBACK(gtk_main_quit), NULL);
 
        pVBox = gtk_hbox_new(FALSE, 0);
        gtk_container_add(GTK_CONTAINER(pWindow), pVBox);
 
        ///* Chargement des images a partir d'un fichier */
        image_st->wid_PixBuf1=gdk_pixbuf_new_from_file("./image_a_modifier.jpg",NULL);
       image_st-> GtkWid_image_loaded= gtk_image_new_from_pixbuf(image_st->wid_PixBuf1);
         gtk_box_pack_start(GTK_BOX(pVBox), image_st-> GtkWid_image_loaded, FALSE, FALSE, 5);
 
         image_st->wid_PixBuf2=gdk_pixbuf_new_from_file("./image_a_copier.jpg",NULL);
     image_st-> GtkWid_texture= gtk_image_new_from_pixbuf(image_st->wid_PixBuf2);
            gtk_box_pack_start(GTK_BOX(pVBox),  image_st-> GtkWid_texture, FALSE, FALSE, 5);
 
 
       pbutton=gtk_button_new_with_label("action");
       gtk_box_pack_start(GTK_BOX(pVBox), pbutton, FALSE, FALSE, 5);
 
 
 
    /// callback
    g_signal_connect (G_OBJECT(pbutton), "clicked", G_CALLBACK (remplir_image), (gpointer*)image_st);
 
 
        /*affichage de tout*/
        gtk_widget_show_all(pWindow);
 
        /*fermeture de gtk*/
        gtk_main();
 
        return EXIT_SUCCESS;
    }
fonction.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
 
#include <stdlib.h>
#include <gtk/gtk.h>
#include "fonction.h"
#include <glib/gprintf.h>
 
 void put_pixel (GdkPixbuf *pixbuf, int x, int y, guchar red, guchar green, guchar blue)
{
  int width, height, rowstride, n_channels;
  guchar *pixels, *p;
 
  n_channels = gdk_pixbuf_get_n_channels (pixbuf);
 
  g_assert (gdk_pixbuf_get_colorspace (pixbuf) == GDK_COLORSPACE_RGB);
  g_assert (gdk_pixbuf_get_bits_per_sample (pixbuf) == 8);
  //g_assert (gdk_pixbuf_get_has_alpha (pixbuf));
  g_assert (n_channels == 3);
 
  width = gdk_pixbuf_get_width (pixbuf);
  height = gdk_pixbuf_get_height (pixbuf);
 
  g_assert (x >= 0 && x < width);
  g_assert (y >= 0 && y < height);
 
  rowstride = gdk_pixbuf_get_rowstride (pixbuf);
  pixels = gdk_pixbuf_get_pixels (pixbuf);
 
  p = pixels + y * rowstride + x * n_channels;
  p[0] = red;
  p[1] = green;
  p[2] = blue;
  //p[3] = alpha;
}
 
 
gboolean gdkpixbuf_get_colors_by_coordinates(GdkPixbuf *pixbuf, gint x, gint y, guchar *red, guchar *green, guchar *blue)
{
  guchar *pixel=NULL;
  gint channel=0;
  gint width=0;
 
  if (!pixbuf) return FALSE;
  if (x<0 || y<0) return FALSE;
  if (x>gdk_pixbuf_get_width(pixbuf)) return FALSE;
  if (y>gdk_pixbuf_get_height(pixbuf)) return FALSE;
 
  pixel=gdk_pixbuf_get_pixels(pixbuf);
  channel=gdk_pixbuf_get_n_channels(pixbuf);
  width=gdk_pixbuf_get_width(pixbuf);
 
  *red   = pixel[(x*channel)+(y*width*channel)];
  *green = pixel[(x*channel)+(y*width*channel)+1];
  *blue  = pixel[(x*channel)+(y*width*channel)+2];
 
  return TRUE;
}
    gdkpixbuf_get_colors_by_coordinates(image_texture_buf,x%gdk_pixbuf_get_width(image_
void remplace_texture(GdkPixbuf* image_charge, GdkPixbuf*image_texture_buf,gint x_coor,gint y_coor)
{ guchar rouge, vert, bleu;
 
 
gdkpixbuf_get_colors_by_coordinates(image_texture_buf,x_coor%gdk_pixbuf_get_width(image_texture_buf),y_coor%gdk_pixbuf_get_height(image_texture_buf),&rouge, &vert, &bleu);
put_pixel(image_charge, x_coor,y_coor,rouge,vert,bleu);
 
}
void remplir_image(GtkWidget *pButton, gpointer data){
    Image_struc* image_st;
    image_st=(Image_struc*) data;
    GdkPixbuf* image_texture_buf=image_st->wid_PixBuf2;
    GdkPixbuf *image_charge=image_st->wid_PixBuf1;
    guchar rouge,vert,bleu;
 
 
 
    int x,y;
  for (x=0; x<gdk_pixbuf_get_width(image_charge);x++){
      for (y=0; y<gdk_pixbuf_get_height(image_charge);y++){
 
    gdkpixbuf_get_colors_by_coordinates(image_charge, x, y, &rouge, &vert, &bleu);
   g_printf("\n rouge : %d \n vert : %d \n bleu : %d \n x: %d y : %d" ,rouge, vert, bleu, x,y);
    if( rouge==255 && vert==255 && bleu==255)
    remplace_texture(image_charge, image_texture_buf,x,y);
 
    }    }
 
    gtk_image_set_from_pixbuf( GTK_IMAGE(image_st-> GtkWid_image_loaded),image_charge);
 
 
 
}
fonction.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
 
 
 void put_pixel (GdkPixbuf *pixbuf, int x, int y, guchar red, guchar green, guchar blue);
gboolean gdkpixbuf_get_colors_by_coordinates(GdkPixbuf *pixbuf, gint x, gint y, guchar *red, guchar *green, guchar *blue);
//void remplir_image(GdkPixbuf* image_texture_buf,GdkPixbuf *image_charge);
 
void remplir_image(GtkWidget *pButton, gpointer data);
 
typedef struct
{
 
   GdkPixbuf *wid_PixBuf1;
   GdkPixbuf *wid_PixBuf2;
   GtkWidget *GtkWid_image_loaded;
   GtkWidget *GtkWid_texture;
}
Image_struc;
 
void remplace_texture(GdkPixbuf* image_charge, GdkPixbuf*image_texture_buf,gint x_coor,gint y_coor);
Merci d'avance