Skip to content

Commit da270dc

Browse files
authored
chore(deps): bump image from 0.23.14 to 0.24.0 (#108)
1 parent 57caa0a commit da270dc

File tree

8 files changed

+402
-51
lines changed

8 files changed

+402
-51
lines changed

Cargo.lock

Lines changed: 360 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ version = "3.0.14"
3232
features = ["cargo"]
3333

3434
[dependencies.image]
35-
version = "0.23.14"
35+
version = "0.24.0"
3636
features = ["tga"]
3737

3838
[target.'cfg(target_os = "macos")'.dependencies]

src/capture.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use anyhow::{Context, Result};
22
use image::save_buffer;
3+
use image::ColorType::Rgba8;
34
use std::borrow::Borrow;
45
use std::ops::{Add, Sub};
56
use std::sync::mpsc::Receiver;
@@ -80,7 +81,7 @@ pub fn save_frame(
8081
&image.samples,
8182
image.layout.width,
8283
image.layout.height,
83-
image.color_hint.unwrap(),
84+
image.color_hint.unwrap_or(Rgba8),
8485
)
8586
.context("Cannot save frame")
8687
}

src/common/identify_transparency.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use crate::{Image, Margin, Result};
22
use image::flat::View;
3-
use image::{Bgra, GenericImageView};
3+
use image::{GenericImageView, Rgba};
44

55
///
66
/// this helps to identify outer transparent regions
77
/// since some backends provides transparency either from a compositor effect like drop shadow on ubuntu / GNOME
88
/// or some strange right side strip on MacOS
99
pub fn identify_transparency(image: Image) -> Result<Option<Margin>> {
10-
let image: View<_, Bgra<u8>> = image.as_view()?;
10+
let image: View<_, Rgba<u8>> = image.as_view()?;
1111
let (width, height) = image.dimensions();
1212
let half_width = width / 2;
1313
let half_height = height / 2;
@@ -17,7 +17,7 @@ pub fn identify_transparency(image: Image) -> Result<Option<Margin>> {
1717
let mut margin = Margin::zero();
1818
// identify top margin
1919
for y in 0..half_height {
20-
let Bgra([_, _, _, a]) = image.get_pixel(half_width, y);
20+
let Rgba([_, _, _, a]) = image.get_pixel(half_width, y);
2121
if a > transparency_end {
2222
// the end of the transparent area
2323
margin.top = y as u16;
@@ -26,7 +26,7 @@ pub fn identify_transparency(image: Image) -> Result<Option<Margin>> {
2626
}
2727
// identify bottom margin
2828
for y in (half_height..height).rev() {
29-
let Bgra([_, _, _, a]) = image.get_pixel(half_width, y);
29+
let Rgba([_, _, _, a]) = image.get_pixel(half_width, y);
3030
if a > transparency_end {
3131
// the end of the transparent area
3232
margin.bottom = (height - y - 1) as u16;
@@ -35,7 +35,7 @@ pub fn identify_transparency(image: Image) -> Result<Option<Margin>> {
3535
}
3636
// identify left margin
3737
for x in 0..half_width {
38-
let Bgra([_, _, _, a]) = image.get_pixel(x, half_height);
38+
let Rgba([_, _, _, a]) = image.get_pixel(x, half_height);
3939
if a > transparency_end {
4040
// the end of the transparent area
4141
margin.left = x as u16;
@@ -44,7 +44,7 @@ pub fn identify_transparency(image: Image) -> Result<Option<Margin>> {
4444
}
4545
// identify right margin
4646
for x in (half_width..width).rev() {
47-
let Bgra([_, _, _, a]) = image.get_pixel(x, half_height);
47+
let Rgba([_, _, _, a]) = image.get_pixel(x, half_height);
4848
if a > transparency_end {
4949
// the end of the transparent area
5050
margin.right = (width - x - 1) as u16;
@@ -63,9 +63,9 @@ mod test {
6363
fn should_identify_macos_right_side_issue() -> Result<()> {
6464
// given an screen capture with transparency on the right side
6565
let image_org = image::open("tests/frames/frame-macos-right-side-issue.tga")?;
66-
let image = image_org.into_bgra8();
66+
let image = image_org.into_rgba8();
6767
let (width, height) = image.dimensions();
68-
let Bgra([blue, green, red, alpha]) = image.get_pixel(width - 1, height / 2);
68+
let Rgba([blue, green, red, alpha]) = image.get_pixel(width - 1, height / 2);
6969
assert_eq!(alpha, &0, "the test image was not transparent");
7070
assert_eq!(red, &0, "the test image is not as expected");
7171
assert_eq!(green, &0, "the test image is not as expected");

src/common/image.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use crate::common::Margin;
22
use crate::{Image, ImageOnHeap, Result};
33
use image::flat::View;
4-
use image::{imageops, Bgra, GenericImageView, ImageBuffer};
4+
use image::{imageops, GenericImageView, ImageBuffer, Rgba};
55

66
///
77
/// specialized version of crop for [`ImageOnHeap`] and [`Margin`]
88
///
99
#[cfg_attr(not(macos), allow(dead_code))]
1010
pub fn crop(image: Image, margin: &Margin) -> Result<ImageOnHeap> {
11-
let mut img2: View<_, Bgra<u8>> = image.as_view()?;
11+
let mut img2: View<_, Rgba<u8>> = image.as_view()?;
1212
let (width, height) = (
1313
img2.width() - (margin.left + margin.right) as u32,
1414
img2.height() - (margin.top + margin.bottom) as u32,
@@ -31,6 +31,13 @@ pub fn crop(image: Image, margin: &Margin) -> Result<ImageOnHeap> {
3131
Ok(Box::new(buf.into_flat_samples()))
3232
}
3333

34+
///
35+
pub fn convert_bgra_to_rgba(buffer: &mut [u8]) {
36+
for chunk in buffer.chunks_exact_mut(4) {
37+
chunk.swap(0, 2);
38+
}
39+
}
40+
3441
#[cfg(test)]
3542
mod tests {
3643
use super::*;
@@ -40,7 +47,7 @@ mod tests {
4047
fn should_crop() -> Result<()> {
4148
// given
4249
let image_org = open("tests/frames/frame-macos-right-side-issue.tga")?;
43-
let image = image_org.into_bgra8();
50+
let image = image_org.into_rgba8();
4451
let image_raw = ImageOnHeap::new(image.into_flat_samples());
4552
let (width, height) = (image_raw.layout.width, image_raw.layout.height);
4653

src/linux/x11_api.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::common::identify_transparency::identify_transparency;
2+
use crate::common::image::convert_bgra_to_rgba;
23
use crate::{ImageOnHeap, Margin, PlatformApi, Result, WindowId, WindowList};
4+
35
use anyhow::Context;
46
use image::flat::SampleLayout;
57
use image::{ColorType, FlatSamples};
@@ -180,8 +182,10 @@ impl PlatformApi for X11Api {
180182
window_id
181183
))?;
182184

183-
let raw_data = image.data;
184-
let color = ColorType::Bgra8;
185+
let mut raw_data = image.data;
186+
convert_bgra_to_rgba(&mut raw_data);
187+
188+
let color = ColorType::Rgba8;
185189
let channels = 4;
186190
let mut buffer = FlatSamples {
187191
samples: raw_data,
@@ -258,23 +262,23 @@ impl PlatformApi for X11Api {
258262
mod test {
259263
use super::*;
260264
use image::flat::View;
261-
use image::{save_buffer, Bgra, GenericImageView};
265+
use image::{save_buffer, GenericImageView, Rgba};
262266

263267
#[test]
264268
fn calibrate() -> Result<()> {
265269
let mut api = X11Api::new()?;
266270
let win = api.get_active_window()?;
267271
let image_raw = api.capture_window_screenshot(win)?;
268-
let image: View<_, Bgra<u8>> = image_raw.as_view().unwrap();
272+
let image: View<_, Rgba<u8>> = image_raw.as_view().unwrap();
269273
let (width, height) = image.dimensions();
270274

271275
api.calibrate(win)?;
272276
let image_calibrated_raw = api.capture_window_screenshot(win)?;
273-
let image_calibrated: View<_, Bgra<u8>> = image_calibrated_raw.as_view().unwrap();
277+
let image_calibrated: View<_, Rgba<u8>> = image_calibrated_raw.as_view().unwrap();
274278
let (width_new, height_new) = image_calibrated.dimensions();
275279
dbg!(width, width_new, height, height_new);
276280

277-
let Bgra([_, _, _, alpha]) = image.get_pixel(width / 2, 0);
281+
let Rgba([_, _, _, alpha]) = image.get_pixel(width / 2, 0);
278282
dbg!(alpha);
279283
if alpha == 0 {
280284
// if that pixel was full transparent, for example on ubuntu / GNOME, caused by the drop shadow
@@ -331,10 +335,10 @@ mod test {
331335
let api = X11Api::new()?;
332336
let win = api.get_active_window()?;
333337
let image_raw = api.capture_window_screenshot(win)?;
334-
let image: View<_, Bgra<u8>> = image_raw.as_view().unwrap();
338+
let image: View<_, Rgba<u8>> = image_raw.as_view().unwrap();
335339
let (width, height) = image.dimensions();
336340

337-
let Bgra([blue, green, red, alpha]) = image.get_pixel(width / 2, height / 2);
341+
let Rgba([red, green, blue, alpha]) = image.get_pixel(width / 2, height / 2);
338342
assert_ne!(blue, 0);
339343
assert_ne!(green, 0);
340344
assert_ne!(red, 0);

src/macos/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl PlatformApi for QuartzApi {
6464
mod test {
6565
use super::*;
6666
use image::flat::View;
67-
use image::{save_buffer, Bgra, GenericImageView};
67+
use image::{save_buffer, GenericImageView, Rgba};
6868

6969
///
7070
/// for terminals with odd dimensions, like: 93x17
@@ -73,16 +73,16 @@ mod test {
7373
let mut api = setup()?;
7474
let win = api.get_active_window()?;
7575
let image_raw = api.capture_window_screenshot(win)?;
76-
let image: View<_, Bgra<u8>> = image_raw.as_view().unwrap();
76+
let image: View<_, Rgba<u8>> = image_raw.as_view().unwrap();
7777
let (width, height) = image.dimensions();
7878

7979
api.calibrate(win)?;
8080
let image_calibrated_raw = api.capture_window_screenshot(win)?;
81-
let image_calibrated: View<_, Bgra<u8>> = image_calibrated_raw.as_view().unwrap();
81+
let image_calibrated: View<_, Rgba<u8>> = image_calibrated_raw.as_view().unwrap();
8282
let (width_new, height_new) = image_calibrated.dimensions();
8383
dbg!(width, width_new, height, height_new);
8484

85-
let Bgra([_, _, _, alpha]) = image.get_pixel(width / 2, 0);
85+
let Rgba([_, _, _, alpha]) = image.get_pixel(width / 2, 0);
8686
dbg!(alpha);
8787
if alpha == 0 {
8888
// if that pixel was full transparent, for example on ubuntu / GNOME, caused by the drop shadow

src/macos/screenshot.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::common::image::convert_bgra_to_rgba;
12
use crate::ImageOnHeap;
23

34
use anyhow::{ensure, Context, Result};
@@ -7,7 +8,7 @@ use image::flat::SampleLayout;
78
use image::{ColorType, FlatSamples};
89

910
pub fn capture_window_screenshot(win_id: u64) -> Result<ImageOnHeap> {
10-
let (w, h, channels, raw_data) = {
11+
let (w, h, channels, mut raw_data) = {
1112
let image = unsafe {
1213
CGDisplay::screenshot(
1314
CGRectNull,
@@ -43,7 +44,9 @@ pub fn capture_window_screenshot(win_id: u64) -> Result<ImageOnHeap> {
4344
(w, h, byte_per_pixel, raw_data)
4445
};
4546

46-
let color = ColorType::Bgra8;
47+
convert_bgra_to_rgba(&mut raw_data);
48+
49+
let color = ColorType::Rgba8;
4750
let buffer = FlatSamples {
4851
samples: raw_data,
4952
layout: SampleLayout::row_major_packed(channels, w, h),

0 commit comments

Comments
 (0)