gpt4 book ai didi

rust - 如何使用 Piston 图像库更改图像颜色类型?

转载 作者:行者123 更新时间:2023-11-29 08:08:39 28 4
gpt4 key购买 nike

我有一个应用程序旨在使用 RGB 像素格式处理图像数据。我需要让它拍摄所有编码的图像。我的第一个想法是在处理之前将图像转换为 RGB 格式:

extern crate image;

use std::ops;
use image::ConvertBuffer;

pub fn process_generic<P,C>(im: image::ImageBuffer<P,C>)
where P: image::Pixel + 'static,
P::Subpixel: 'static,
C: ops::Deref<Target = [P::Subpixel]>
{
let result: image::ImageBuffer<image::Rgb<u8>, Vec<u8>> = im.convert();
}

fn main() {}

编译这个给我一个错误

error[E0277]: the trait bound `image::Rgb<u8>: image::color::FromColor<P>` is not satisfied
--> src/main.rs:12:66
|
12 | let result: image::ImageBuffer<image::Rgb<u8>, Vec<u8>> = im.convert();
| ^^^^^^^ the trait `image::color::FromColor<P>` is not implemented for `image::Rgb<u8>`
|
= help: consider adding a `where image::Rgb<u8>: image::color::FromColor<P>` bound
= note: required because of the requirements on the impl of `image::ConvertBuffer<image::ImageBuffer<image::Rgb<u8>, std::vec::Vec<u8>>>` for `image::ImageBuffer<P, C>`

我无法添加边界,因为模块 image::color 是私有(private)的。我错过了什么吗?我还尝试手动检查并转换每个像素,但这似乎是一种更糟糕的方法。

最佳答案

您可以使用 image::ConvertBuffer 提供的特征绑定(bind)直接:

extern crate image;

use image::ConvertBuffer;

pub fn process_generic<P, C>(im: image::ImageBuffer<P, C>)
where
image::ImageBuffer<P, C>: image::ConvertBuffer<image::ImageBuffer<image::Rgb<u8>, Vec<u8>>>,
P: image::Pixel + 'static,
{
let _result = im.convert();
}

fn main() {}

尽管我建议为 image::ImageBuffer<image::Rgb<u8>, Vec<u8>> 引入类型别名和/或 image::ImageBuffer<P, C> .

关于rust - 如何使用 Piston 图像库更改图像颜色类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45002722/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com