Skip to content

Commit b9f0dd4

Browse files
committed
chore: version 1
1 parent a87331e commit b9f0dd4

File tree

4 files changed

+48
-27
lines changed

4 files changed

+48
-27
lines changed

libharuhishot/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ mod state;
66
mod utils;
77

88
pub use screenshot::{
9-
AreaSelectCallback, CaptureOption, ClipImageViewInfo, ImageInfo, ImageViewInfo,
9+
AreaSelectCallback, CaptureOption, ClipImageViewInfo, ClipImageViewInfoArea, ImageInfo,
10+
ImageViewInfo,
1011
};
1112
pub use state::*;
1213
pub use utils::*;

libharuhishot/src/screenshot.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,12 @@ pub struct ImageViewInfo {
180180

181181
#[derive(Debug, Clone)]
182182
pub struct ClipImageViewInfo {
183+
pub region: Region,
184+
pub areas: Vec<ClipImageViewInfoArea>,
185+
}
186+
187+
#[derive(Debug, Clone)]
188+
pub struct ClipImageViewInfoArea {
183189
pub info: ImageInfo,
184190
pub region: ClipRegion,
185191
}
@@ -519,7 +525,7 @@ impl HaruhiShotState {
519525
&mut self,
520526
option: CaptureOption,
521527
callback: F,
522-
) -> Result<Vec<ClipImageViewInfo>, HaruhiError>
528+
) -> Result<ClipImageViewInfo, HaruhiError>
523529
where
524530
F: AreaSelectCallback,
525531
{
@@ -611,7 +617,7 @@ impl HaruhiShotState {
611617
let converter = crate::convert::create_converter(shotdata.data.frame_format).unwrap();
612618
let color_type = converter.convert_inplace(&mut frame_mmap);
613619

614-
areas.push(ClipImageViewInfo {
620+
areas.push(ClipImageViewInfoArea {
615621
info: ImageInfo {
616622
data: frame_mmap.deref().into(),
617623
width: shotdata.data.width,
@@ -622,7 +628,10 @@ impl HaruhiShotState {
622628
region: area,
623629
})
624630
}
625-
Ok(areas)
631+
Ok(ClipImageViewInfo {
632+
region,
633+
areas,
634+
})
626635
}
627636
}
628637

libharuhishot/src/utils.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ pub struct ClipRegion {
7373
}
7474

7575
impl ClipRegion {
76+
pub fn display_position_real(&self) -> Position {
77+
self.display_region.position
78+
}
79+
pub fn display_logical_size(&self) -> Size {
80+
self.display_region.size
81+
}
7682
/// the the real absolute position
7783
/// NOTE: no wayland version, because the screen position is real
7884
pub fn absolute_position_real(&self) -> Position {

src/main.rs

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use image::{GenericImageView, ImageEncoder, ImageError, Rgba};
88
pub use libharuhishot::HaruhiShotState;
99
use libharuhishot::reexport::Transform;
1010
use libharuhishot::{
11-
CaptureOption, ClipImageViewInfo, ClipRegion, ImageInfo, Position, Region, Size,
11+
CaptureOption, ClipImageViewInfoArea, ClipRegion, ImageInfo, Position, Region, Size,
1212
};
1313

1414
use std::io::{BufWriter, Write, stdout};
@@ -149,9 +149,9 @@ fn capture_area(
149149
let mut min_y = i32::MAX;
150150
let mut max_x = i32::MIN;
151151
let mut max_y = i32::MIN;
152-
for view in &views {
153-
let Position { x, y } = view.region.absolute_position_real();
154-
let Size { width, height } = view.region.clip_size_real();
152+
for view in &views.areas {
153+
let Position { x, y } = view.region.display_position_real();
154+
let Size { width, height } = view.region.display_logical_size();
155155

156156
min_x = min_x.min(x);
157157
min_y = min_y.min(y);
@@ -162,7 +162,7 @@ fn capture_area(
162162
let total_height = (max_y - min_y) as u32;
163163

164164
let mut combined_image = image::RgbaImage::new(total_width, total_height);
165-
for ClipImageViewInfo {
165+
for ClipImageViewInfoArea {
166166
info:
167167
ImageInfo {
168168
data,
@@ -172,7 +172,7 @@ fn capture_area(
172172
..
173173
},
174174
region,
175-
} in views
175+
} in views.areas
176176
{
177177
// Load the captured image
178178
let img = image::ImageBuffer::from_raw(img_width, img_height, data).ok_or(
@@ -200,44 +200,49 @@ fn capture_area(
200200
}
201201
_ => unreachable!(),
202202
};
203-
204-
// we use the relative position to make image
205-
let Position { x, y } = region.relative_position_wl();
206-
207-
let Size { width, height } = region.clip_size_real();
208-
209-
let subimage = img
210-
.view(x as u32, y as u32, width as u32, height as u32)
211-
.to_image();
212-
let rgba_img: image::RgbaImage = subimage;
213-
203+
let Size { width, height } = region.display_logical_size();
204+
let img = image::imageops::resize(
205+
&img,
206+
width as u32,
207+
height as u32,
208+
image::imageops::FilterType::Gaussian,
209+
);
214210
// we use the real position to calculate the position
215-
let Position { x, y } = region.absolute_position_real();
211+
let Position { x, y } = region.display_position_real();
216212
// Calculate the position in he combined image
217213
let offset_x = (x - min_x) as u32;
218214
let offset_y = (y - min_y) as u32;
219215

220216
// Copy the output image to the combined image
221-
for (x, y, pixel) in rgba_img.enumerate_pixels() {
217+
for (x, y, pixel) in img.enumerate_pixels() {
222218
let target_x = offset_x + x;
223219
let target_y = offset_y + y;
224220
if target_x < total_width && target_y < total_height {
225221
combined_image.put_pixel(target_x, target_y, *pixel);
226222
}
227223
}
228224
}
225+
let clip_region = views.region;
226+
let image = combined_image
227+
.view(
228+
clip_region.position.x as u32,
229+
clip_region.position.y as u32,
230+
clip_region.size.width as u32,
231+
clip_region.size.height as u32,
232+
)
233+
.to_image();
229234

230235
if use_stdout {
231236
let mut buff = std::io::Cursor::new(Vec::new());
232-
combined_image.write_to(&mut buff, image::ImageFormat::Png)?;
237+
image.write_to(&mut buff, image::ImageFormat::Png)?;
233238
let content = buff.get_ref();
234239
let stdout = stdout();
235240
let mut writer = BufWriter::new(stdout.lock());
236241
writer.write_all(content)?;
237242
Ok(HaruhiShotResult::StdoutSucceeded)
238243
} else {
239244
let file = random_file_path();
240-
combined_image.save(&file)?;
245+
image.save(&file)?;
241246
Ok(HaruhiShotResult::SaveToFile(file))
242247
}
243248
}
@@ -253,7 +258,7 @@ fn get_color(state: &mut HaruhiShotState) -> Result<HaruhiShotResult, HaruhiImag
253258
))?;
254259
waysip_to_region(info.size(), info.left_top_point())
255260
})?;
256-
let ClipImageViewInfo {
261+
let ClipImageViewInfoArea {
257262
info:
258263
ImageInfo {
259264
data,
@@ -271,7 +276,7 @@ fn get_color(state: &mut HaruhiShotState) -> Result<HaruhiShotResult, HaruhiImag
271276
},
272277
..
273278
},
274-
} = views.remove(0);
279+
} = views.areas.remove(0);
275280
let image: image::ImageBuffer<Rgba<u8>, Vec<u8>> =
276281
image::ImageBuffer::from_raw(img_width, img_height, data).unwrap();
277282
let img = match transform {

0 commit comments

Comments
 (0)