fix: 修复斜线刷子连线的空白问题

This commit is contained in:
2026-02-28 02:08:36 +08:00
parent 6f6e6f326b
commit ae095946f0
3 changed files with 29 additions and 26 deletions

2
.gitignore vendored
View File

@@ -16,3 +16,5 @@ target/
# and can be added to the global gitignore or merged into this file. For a more nuclear # and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder. # option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/ #.idea/
mspaint.png

View File

@@ -88,8 +88,8 @@ impl MSCanvas {
return self.pixels.clone(); return self.pixels.clone();
} }
let dst_width = self.width * scale; let dst_width = self.width * scale;
let dst_height = self.width * scale; let dst_height = self.height * scale;
let mut dst = vec![0; (self.width * self.width * 4) as usize]; // RGBA let mut dst = vec![0; (dst_width * dst_height * 4) as usize]; // RGBA
for y in 0..self.height { for y in 0..self.height {
for x in 0..self.width { for x in 0..self.width {
@@ -262,21 +262,28 @@ impl MSCanvas {
pub fn brush_slash(&mut self, center: Point) { pub fn brush_slash(&mut self, center: Point) {
let r = self.line_width / 2; let r = self.line_width / 2;
let l = self.line_width - r; let l = self.line_width - r;
// for dx in -1..1 { // 左右扩展,填补线条的空白
// for d in -l..r { for dx in 0..2 {
// self.draw_pixel_at(Point::new(center.x - (d as f32) + (dx as f32), center.y - d as f32)); for d in -l..r {
// } self.draw_pixel_at(Point::new(
// } center.x - (d as f32) + (dx as f32),
for d in -l..r { center.y - d as f32,
self.draw_pixel_at(Point::new(center.x - (d as f32), center.y - d as f32)); ));
}
} }
} }
pub fn brush_backslash(&mut self, center: Point) { pub fn brush_backslash(&mut self, center: Point) {
let r = self.line_width / 2; let r = self.line_width / 2;
let l = self.line_width - r; let l = self.line_width - r;
for d in -l..r { // 左右扩展,填补线条的空白
self.draw_pixel_at(Point::new(center.x + d as f32, center.y - d as f32)); for dx in 0..2 {
for d in -l..r {
self.draw_pixel_at(Point::new(
center.x + d as f32 + (dx as f32),
center.y - d as f32,
));
}
} }
} }

View File

@@ -1,4 +1,4 @@
use ::image::RgbaImage; use ::image::{ImageBuffer, ImageError, Rgba};
use iced::Theme; use iced::Theme;
use iced::padding; use iced::padding;
use iced::widget::container; use iced::widget::container;
@@ -360,13 +360,14 @@ impl PaintApp {
let scale = 4; let scale = 4;
let (width, height) = self.canvas.size(); let (width, height) = self.canvas.size();
let pixels = self.canvas.get_pixels_scale(4); let pixels = self.canvas.get_pixels_scale(4);
save_rgba_as_png( save_rgba_to_png(
pixels, pixels,
(width * scale) as u32, (width * scale) as u32,
(height * scale) as u32, (height * scale) as u32,
"mspaint.png", "mspaint.png",
) )
.unwrap(); .unwrap();
println!("save png");
} }
Message::RefreshImage => { Message::RefreshImage => {
if self.dirty { if self.dirty {
@@ -582,23 +583,16 @@ pub fn main() -> iced::Result {
.run() .run()
} }
fn save_rgba_as_png( fn save_rgba_to_png(
pixels: Vec<u8>, rgba_data: Vec<u8>,
width: u32, width: u32,
height: u32, height: u32,
path: &str, path: &str,
) -> Result<(), Box<dyn std::error::Error>> { ) -> Result<(), ImageError> {
// 验证像素数量是否匹配 // Each pixel is 4 bytes (R, G, B, A)
if pixels.len() != (width * height * 4) as usize { let img: ImageBuffer<Rgba<u8>, Vec<u8>> = ImageBuffer::from_raw(width, height, rgba_data)
return Err("Pixel buffer size does not match width × height × 4".into()); .expect("Failed to create ImageBuffer: data size mismatch");
}
// 创建 RgbaImage
let img: RgbaImage = RgbaImage::from_vec(width, height, pixels)
.ok_or("Failed to create image from pixel data")?;
// 保存为 PNG保留 Alpha 通道)
img.save(path)?; img.save(path)?;
Ok(()) Ok(())
} }