Есть ли способ создать обобщенную функцию c, используя ящик для изображений, который создает белое изображение? - PullRequest
2 голосов
/ 04 мая 2020

Я пытаюсь создать универсальную c функцию для создания белого изображения в ржавчине.

До сих пор я пробовал это:

use imageproc::drawing::draw_filled_rect_mut;
use image::buffer::ConvertBuffer;
use image::{GenericImage, ImageBuffer, Pixel, RgbaImage, Rgba};
use imageproc::rect::Rect;

pub(crate) fn create_square_image<I, P>(source_img: &I, size: u32) -> ImageBuffer<P, Vec<u8>>
where
    I: GenericImage<Pixel = P>,
    I: ConvertBuffer<ImageBuffer<P, Vec<u8>>>,
    P: Pixel<Subpixel = u8>
{
    let (width, height) = source_img.dimensions();

    let mut block_image = create_white_image(size, size);

    let delta_x = (size - width) / 2;
    let delta_y = (size - height) / 2;

    for y in 0..height {
        let target_y = y + delta_y;
        if target_y >= size {
            continue;
        }
        for x in 0..width {
            let target_x = x + delta_x;
            if target_x >= size {
                continue;
            }
            let pixel = source_img.get_pixel(x, y);
            block_image.put_pixel(x, y, pixel)
        }
    }

    block_image
}

pub(crate) fn create_white_image<P>(width: u32, height: u32) -> ImageBuffer<P, Vec<u8>>
where
    P: Pixel<Subpixel = u8> + 'static,
    RgbaImage: ConvertBuffer<ImageBuffer<P, Vec<u8>>>
{
    let mut image = ImageBuffer::new(width, height);
    draw_filled_rect_mut(&mut image, Rect::at(0, 0).of_size(width, height), Rgba([255, 255, 255, 255]));

    image.convert()
}

Это дает мне такую ​​ошибку при попытке построить:

error[E0277]: the trait bound `P: image::color::FromColor<image::color::Rgba<u8>>` is not satisfied
   --> src\util.rs:217:27
    |
217 |     let mut block_image = create_white_image(size, size);
    |                           ^^^^^^^^^^^^^^^^^^ the trait `image::color::FromColor<image::color::Rgba<u8>>` is not implemented for `P`

и я думаю, я был бы в порядке, если бы я также установил ограничение на тип FromColor, но по какой-то причине он не был опубликован c клеткой.

Что-то мне не хватает?

Активный набор инструментов:

stable-x86_64-pc-windows-gnu (default)
rustc 1.43.0 (4fb7144ed 2020-04-20)

автомобиль go .toml зависимости:

[dependencies]
image = "0.23.4"
imageproc = "0.20.0"
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...