1
1
use crate :: common:: identify_transparency:: identify_transparency;
2
2
use crate :: common:: image:: convert_bgra_to_rgba;
3
- use crate :: { ImageOnHeap , Margin , PlatformApi , Result , WindowId , WindowList } ;
3
+ use crate :: { Margin , PlatformApi , Result , WindowId , WindowList } ;
4
4
5
+ use crate :: common:: Frame ;
5
6
use anyhow:: Context ;
6
- use image:: flat:: SampleLayout ;
7
- use image:: { ColorType , FlatSamples } ;
8
7
use log:: debug;
9
8
use x11rb:: connection:: Connection ;
10
9
use x11rb:: protocol:: xproto:: * ;
@@ -117,11 +116,11 @@ impl X11Api {
117
116
self . get_all_sub_windows ( & ( screen. root as WindowId ) )
118
117
}
119
118
120
- pub fn get_window_geometry ( & self , window : & WindowId ) -> Result < ( i16 , i16 , u16 , u16 ) > {
119
+ pub fn get_window_geometry ( & self , window : & WindowId ) -> Result < ( i32 , i32 , u32 , u32 ) > {
121
120
let conn = & self . conn ;
122
121
let window = * window as Window ;
123
122
let geom = conn. get_geometry ( window) ?. reply ( ) ?;
124
- Ok ( ( geom. x , geom. y , geom. width , geom. height ) )
123
+ Ok ( ( geom. x as _ , geom. y as _ , geom. width as _ , geom. height as _ ) )
125
124
}
126
125
}
127
126
@@ -131,7 +130,7 @@ impl PlatformApi for X11Api {
131
130
/// to cut them away in further screenshots
132
131
fn calibrate ( & mut self , window_id : WindowId ) -> Result < ( ) > {
133
132
let image = self . capture_window_screenshot ( window_id) ?;
134
- self . margin = identify_transparency ( * image) ?;
133
+ self . margin = identify_transparency ( & image) ?;
135
134
136
135
Ok ( ( ) )
137
136
}
@@ -153,15 +152,15 @@ impl PlatformApi for X11Api {
153
152
Ok ( wins)
154
153
}
155
154
156
- fn capture_window_screenshot ( & self , window_id : WindowId ) -> Result < ImageOnHeap > {
155
+ fn capture_window_screenshot ( & self , window_id : WindowId ) -> Result < Frame > {
157
156
let ( _, _, mut width, mut height) = self . get_window_geometry ( & window_id) ?;
158
- let ( mut x, mut y) = ( 0_i16 , 0_i16 ) ;
157
+ let ( mut x, mut y) = ( 0_i32 , 0_i32 ) ;
159
158
if let Some ( margin) = self . margin . as_ref ( ) {
160
159
if !margin. is_zero ( ) {
161
- width -= margin. left + margin. right ;
162
- height -= margin. top + margin. bottom ;
163
- x = margin. left as i16 ;
164
- y = margin. top as i16 ;
160
+ width -= ( margin. left + margin. right ) as u32 ;
161
+ height -= ( margin. top + margin. bottom ) as u32 ;
162
+ x = margin. left as _ ;
163
+ y = margin. top as _ ;
165
164
}
166
165
}
167
166
let image = self
@@ -170,10 +169,10 @@ impl PlatformApi for X11Api {
170
169
. get_image (
171
170
ImageFormat :: Z_PIXMAP ,
172
171
window_id as Drawable ,
173
- x,
174
- y,
175
- width,
176
- height,
172
+ x as _ ,
173
+ y as _ ,
174
+ width as _ ,
175
+ height as _ ,
177
176
!0 ,
178
177
) ?
179
178
. reply ( )
@@ -182,24 +181,17 @@ impl PlatformApi for X11Api {
182
181
window_id
183
182
) ) ?;
184
183
185
- let mut raw_data = image. data ;
186
- convert_bgra_to_rgba ( & mut raw_data) ;
187
-
188
- let color = ColorType :: Rgba8 ;
189
- let channels = 4 ;
190
- let mut buffer = FlatSamples {
191
- samples : raw_data,
192
- layout : SampleLayout :: row_major_packed ( channels, width as u32 , height as u32 ) ,
193
- color_hint : Some ( color) ,
194
- } ;
184
+ let mut samples = image. data ;
185
+ convert_bgra_to_rgba ( & mut samples) ;
195
186
196
187
if image. depth == 24 {
188
+ let stride = 3 ;
197
189
// NOTE: in this case the alpha channel is 0, but should be set to 0xff
198
190
// the index into the alpha channel
199
- let mut i = 3 ;
200
- let len = buffer . samples . len ( ) ;
191
+ let mut i = stride ;
192
+ let len = samples. len ( ) ;
201
193
while i < len {
202
- let alpha = buffer . samples . get_mut ( i) . unwrap ( ) ;
194
+ let alpha = samples. get_mut ( i) . unwrap ( ) ;
203
195
if alpha == & 0 {
204
196
* alpha = 0xff ;
205
197
} else {
@@ -208,27 +200,36 @@ impl PlatformApi for X11Api {
208
200
}
209
201
210
202
// going one pixel further, still pointing to the alpha channel
211
- i += buffer . layout . width_stride ;
203
+ i += stride ;
212
204
}
213
205
}
214
206
if self . margin . is_some ( ) {
207
+ let stride = 3 ;
215
208
// once first image is captured, we make sure that transparency is removed
216
209
// even in cases where `margin.is_zero()`
217
210
let mut i = 3 ;
218
- let len = buffer . samples . len ( ) ;
211
+ let len = samples. len ( ) ;
219
212
while i < len {
220
- let alpha = buffer . samples . get_mut ( i) . unwrap ( ) ;
213
+ let alpha = samples. get_mut ( i) . unwrap ( ) ;
221
214
if alpha != & 0xff {
222
215
* alpha = 0xff ;
223
216
}
224
217
225
218
// going one pixel further, still pointing to the alpha channel
226
- i += buffer . layout . width_stride ;
219
+ i += stride ;
227
220
}
228
221
}
229
222
debug ! ( "Image dimensions: {}x{}" , width, height) ;
230
223
231
- Ok ( ImageOnHeap :: new ( buffer) )
224
+ let channels = 4 ;
225
+ Ok ( Frame :: from_bgra ( samples, channels, width, height) )
226
+ // let color = ColorType::Rgba8;
227
+ // let mut buffer = FlatSamples {
228
+ // samples,
229
+ // layout: SampleLayout::row_major_packed(channels, width as u32, height as u32),
230
+ // color_hint: Some(color),
231
+ // };
232
+ // Ok(ImageOnHeap::new(buffer))
232
233
}
233
234
234
235
fn get_active_window ( & self ) -> Result < WindowId > {
0 commit comments