Skip to content

Commit

Permalink
chore: solve code warning (#72)
Browse files Browse the repository at this point in the history
* chore: solve code warning

* chore: 修改CI
  • Loading branch information
nashaofu authored Aug 24, 2023
1 parent d57267a commit f90aa9e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
11 changes: 9 additions & 2 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@ concurrency:

jobs:
lint:
name: Lint
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
host:
- macos-latest
- windows-latest
- ubuntu-latest
name: Lint on ${{ matrix.host }}
runs-on: ${{ matrix.host }}
steps:
- uses: actions/checkout@v3

Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "screenshots"
version = "0.8.1"
version = "0.8.2"
edition = "2021"
description = "A cross-platform screen capturer library"
license = "Apache-2.0"
Expand Down
10 changes: 5 additions & 5 deletions src/image_utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::{anyhow, Result};
use image::{open, RgbaImage};
use image::RgbaImage;

pub fn vec_to_rgba_image(width: u32, height: u32, buf: Vec<u8>) -> Result<RgbaImage> {
RgbaImage::from_vec(width, height, buf).ok_or(anyhow!("buffer not big enough"))
Expand All @@ -11,8 +11,7 @@ pub fn bgra_to_rgba_image(width: u32, height: u32, buf: Vec<u8>) -> Result<RgbaI
let rgba_buf = buf
.chunks_exact(4)
.take((width * height) as usize)
.map(|bgra| [bgra[2], bgra[1], bgra[0], bgra[3]])
.flatten()
.flat_map(|bgra| [bgra[2], bgra[1], bgra[0], bgra[3]])
.collect();

vec_to_rgba_image(width, height, rgba_buf)
Expand All @@ -26,8 +25,7 @@ pub fn bgra_to_rgba_image(width: u32, height: u32, buf: Vec<u8>) -> Result<RgbaI
#[cfg(any(target_os = "macos", test))]
pub fn remove_extra_data(width: usize, bytes_per_row: usize, buf: Vec<u8>) -> Vec<u8> {
buf.chunks_exact(bytes_per_row)
.map(|row| row.split_at(width * 4).0.to_owned())
.flatten()
.flat_map(|row| row.split_at(width * 4).0.to_owned())
.collect()
}

Expand All @@ -39,6 +37,8 @@ pub fn png_to_rgba_image(
width: i32,
height: i32,
) -> Result<RgbaImage> {
use image::open;

let mut dynamic_image = open(filename)?;
dynamic_image = dynamic_image.crop(x as u32, y as u32, width as u32, height as u32);
Ok(dynamic_image.to_rgba8())
Expand Down
10 changes: 5 additions & 5 deletions src/linux/xorg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use image::RgbaImage;
use xcb::x::{Drawable, GetImage, ImageFormat, ImageOrder};

fn get_pixel8_rgba(
bytes: &Vec<u8>,
bytes: &[u8],
x: u32,
y: u32,
width: u32,
Expand All @@ -28,7 +28,7 @@ fn get_pixel8_rgba(
}

fn get_pixel16_rgba(
bytes: &Vec<u8>,
bytes: &[u8],
x: u32,
y: u32,
width: u32,
Expand All @@ -51,7 +51,7 @@ fn get_pixel16_rgba(
}

fn get_pixel24_32_rgba(
bytes: &Vec<u8>,
bytes: &[u8],
x: u32,
y: u32,
width: u32,
Expand Down Expand Up @@ -87,7 +87,7 @@ fn capture(x: i32, y: i32, width: u32, height: u32) -> Result<RgbaImage> {
});

let get_image_reply = conn.wait_for_reply(get_image_cookie)?;
let bytes = Vec::from(get_image_reply.data());
let bytes = get_image_reply.data();
let depth = get_image_reply.depth();

let mut rgba = vec![0u8; (width * height * 4) as usize];
Expand All @@ -111,7 +111,7 @@ fn capture(x: i32, y: i32, width: u32, height: u32) -> Result<RgbaImage> {
for y in 0..height {
for x in 0..width {
let index = ((y * width + x) * 4) as usize;
let (r, g, b, a) = get_pixel_rgba(&bytes, x, y, width, bits_per_pixel, bit_order);
let (r, g, b, a) = get_pixel_rgba(bytes, x, y, width, bits_per_pixel, bit_order);

rgba[index] = r;
rgba[index + 1] = g;
Expand Down

0 comments on commit f90aa9e

Please sign in to comment.