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
  
/* -*- C -*- */ 
class GDK2.Image; 
inherit G.Object; 
 
//! A gdk (low level) image. 
//! Mainly used for W(Image) objects. 
//! 
//! NOIMG 
 
void create(int|void fast_mode, Image.Image|void image) 
//! Create a new GDK2.Image object. The first argument is either 0, which 
//! indicates that you want a 'slow' image. If you use '1', you 
//! indicate that you want a 'fast' image. Fast images are stored in 
//! shared memory, and thus are not sent over any network. But please 
//! limit your usage of fast images, they use up a possibly limited 
//! system resource set. See the man page for shmget(2) for more 
//! information on the limits on shared segments on your system. 
//!<p> 
//! A 'fast' image will automatically revert back to 'slow' mode if no 
//! shared memory is available. 
//! </p><p> 
//! If the second argument is specified, it is the actual image data.</p> 
{ 
  pgtk2_verify_setup(); 
  pgtk2_verify_not_inited(); 
  THIS->extra_int=GDK_IMAGE_NORMAL; 
  if (args && Pike_sp[-args].u.integer) 
    THIS->extra_int=GDK_IMAGE_FASTEST; 
  if (args==2) { 
    stack_swap(); 
    pop_stack(); 
    pgdk2_image_set(1); 
    args = 1; 
  } 
  pgtk2_pop_n_elems(args); 
/* Do not add one extra ref for the internal gdk object..*/ 
/*  add_ref(Pike_fp->current_object); */ 
  pgtk2__init_this_object(); 
} 
 
%{ 
#define HEADER "P6\n%d %d\n255\n" 
#define HEADER_SIZE 20 
%} 
string get_pnm() 
//! Returns the data in the image as a pnm object. 
//! Currently, this is always a P6 (true color raw) image. 
//! This could change in the future. To get a pike image object 
//! do 'Image.PNM.decode( gdkimage->get_pnm() )' 
{ 
  GdkImage *i=GDK_IMAGE(THIS->obj); 
  char *p; 
  struct pike_string *res; 
  char header[100]; 
  int x,y,len; 
 
  pgtk2_pop_n_elems(args); 
  sprintf(header,HEADER,i->width,i->height); 
  len=strlen(header)+i->width*i->height*3; 
  res=begin_shared_string(len); 
  p=res->str; 
  strcpy(p,header); 
  p+=strlen(header); 
 
  for (y=0; y<i->height; y++) 
    for(x=0; x<i->width; x++) { 
      unsigned int pi=gdk_image_get_pixel(i,x,y); 
      struct my_pixel pix=pgtk2_pixel_from_xpixel(pi,i); 
      (*p++)=pix.r; 
      (*p++)=pix.g; 
      (*p++)=pix.b; 
    } 
  push_string(end_shared_string(res)); 
} 
 
void grab(GTK2.Widget widget, int xoffset, int yoffset, int width, int height) 
//! Call this function to grab a portion of a widget (argument 1) to the image. 
//! Grabbing non-toplevel widgets may produce unexpected results. 
//! To get the size of a widget use -&gt;xsize() and -&gt;ysize(). 
//! To get the offset of the upper left corner of the widget, relative to it's 
//! X-window (this is what you want for the offset arguments), use 
//! -&gt;xoffset() and -&gt;yoffset(). 
{ 
  GdkWindow *win=NULL; 
  struct object *o; 
  INT_TYPE x,y,w,h; 
 
  get_all_args(NULL,args,"%o%i%i%i%i",&o,&x,&y,&w,&h); 
 
  if (get_pg2object(o,pgtk2_widget_program)) 
    win=GTK_WIDGET(get_pg2object(o,pgtk2_widget_program))->window; 
 
  if (!win) 
    Pike_error("This is not a window, or this widget doesn't have a window\n"); 
 
  if (THIS->obj) 
    g_object_unref(THIS->obj); 
 
  THIS->obj=G_OBJECT(gdk_drawable_get_image(win,x,y,w,h)); 
   
  RETURN_THIS(); 
} 
 
void set(Image.Image|int image_or_xsize,int|void ysize) 
//! Call this to set this image to either the contents of a pike image 
//! or a blank image of a specified size. 
{ 
  struct object *img; 
 
  if (args==1) { 
    get_all_args(NULL,args,"%o",&img); 
    THIS->obj=G_OBJECT(gdkimage_from_pikeimage(img,THIS->extra_int, 
                                               &THIS->obj)); 
  } else { 
    INT_TYPE x,y; 
    GdkImage *gi; 
    get_all_args(NULL,args,"%i%i",&x,&y); 
    if (THIS->obj) 
      g_object_unref(THIS->obj); 
    gi=gdk_image_new(THIS->extra_int,gdk_visual_get_system(),x,y); 
    THIS->obj=G_OBJECT(gi); 
    if (!THIS->obj) 
      Pike_error("Failed to create GDK2.Image from size.\n"); 
  } 
  RETURN_THIS(); 
} 
 
int get_pixel(int x, int y) 
//! Get the pixel value of a pixel as a X-pixel value. 
//! It is usualy not very easy to convert this value to a 
//! rgb triple. See get_pnm. 
{ 
  INT_TYPE x,y; 
  get_all_args(NULL,args,"%i%i",&x,&y); 
  if (!THIS->obj) 
    Pike_error("No image.\n"); 
  pgtk2_pop_n_elems(args); 
  push_int(gdk_image_get_pixel(GDK_IMAGE(THIS->obj),x,y)); 
} 
 
void set_pixel(int x, int y, int pixel) 
//! Set the pixel value of a pixel. Please note that the pixel argument 
//! is a X-pixel value, which is not easily gotten from a RGB color.  
//! See get_pixel and set. 
{ 
  INT_TYPE x,y,to; 
  get_all_args(NULL,args,"%i%i%i",&x,&y,&to); 
  if (!THIS->obj) 
    Pike_error("No image.\n"); 
  gdk_image_put_pixel(GDK_IMAGE(THIS->obj),x,y,to); 
  RETURN_THIS(); 
}