ImageMagick C++ API输出16位灰度png?

发布于 2024-12-09 09:53:54 字数 829 浏览 0 评论 0原文

我通过 Magick++ API 链接到 ImageMagick。我正在尝试获取 uint16 数据并将其输出为 1024x768 1 通道 16 位灰度 PNG。我从下面得到的输出是 RGB8 PNG。除了格式之外,图像内容是正确的。

gray16_view_t u16view = ...;
uint16_t* data = interleaved_view_get_raw_data(u16view);
size_t length = sizeof(u16) * u16view.width() * u16view.height();
Magick::Blob u16Blob(data, length);
Magick::Geometry size(u16view.width(), u16view.height());
Magick::Image u16MagickImg(u16Blob, size, 16, "GRAY");
u16MagickImg.write("test-16bit.png");

有没有办法指定更多有关输出格式的信息?

关于 imagemagick 的 PNG 处理的一些讨论如下: http://www.imagemagick.org/Usage/formats /#png_格式 他们将 PNG8、PNG24 和 PNG32 列为可用格式,但以下部分暗示

-define png:bit-depth 16 
-define png:color-type=0 

在命令行上将具有所需的输出

I'm linking to ImageMagick via the Magick++ API. I'm attempting to take uint16 data and output it as a 1024x768 1-channel 16-bit grayscale PNG. The output I get from the following is an RGB8 PNG. The image contents are correct besides the formatting.

gray16_view_t u16view = ...;
uint16_t* data = interleaved_view_get_raw_data(u16view);
size_t length = sizeof(u16) * u16view.width() * u16view.height();
Magick::Blob u16Blob(data, length);
Magick::Geometry size(u16view.width(), u16view.height());
Magick::Image u16MagickImg(u16Blob, size, 16, "GRAY");
u16MagickImg.write("test-16bit.png");

Is there any way to specify more about the output format?

Some discussion of imagemagick's PNG handling is here: http://www.imagemagick.org/Usage/formats/#png_formats
They list PNG8, PNG24, and PNG32 as available formats, but the following section implies that

-define png:bit-depth 16 
-define png:color-type=0 

on the commandline would have the desired output

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月寒剑心 2024-12-16 09:53:54
    u16MagickImg.quality(00);
    u16MagickImg.defineSet("png:color-type", "0");
    u16MagickImg.defineSet("png:bit-depth", "16");
    u16MagickImg.quality(00);
    u16MagickImg.defineSet("png:color-type", "0");
    u16MagickImg.defineSet("png:bit-depth", "16");
撩心不撩汉 2024-12-16 09:53:54

我尝试了defineSet,它对我不起作用,但接下来起作用了:

image.defineValue("png", "format", "png24");

我的情况有点不同,所以我使用不同的png格式说明符和值,在你的情况下应该是:

u16MagickImg.defineValue("png", "color-type", "0");
u16MagickImg.defineValue("png", "bit-depth", "16");

请参阅此处的格式说明符列表:
http://www.imagemagick.org/script/command-line-options .php#define

参见 Image 类的defineValue 和defineSet 方法的含义信息:
http://www.imagemagick.org/Magick++/Image.html

引用:

DefineValue:“设置或获取在对指定格式进行编码或解码时要应用的定义字符串。定义的含义是特定于格式的。格式由 magick_ 参数指定,格式特定的键由 key_ 指定,关联的值由以下方式指定value_。如果必须完全删除该键,请参阅defineSet()方法。
DefineSet:“设置或获取在编码或解码指定格式时应用的定义标志。与defineValue()方法类似,不同之处在于传递flag_值'true'会使用该格式和键创建无值定义。 flag_ value 'false' 删除任何现有的匹配定义,如果存在匹配的键,则该方法返回 'true',如果不存在匹配的键,则返回 'false'。”

png.c 源文件中还有一些重要信息:
如果无法按照请求的位深度和颜色类型无损写入图像,则不会写入 PNG 文件,将发出警告,并且编码器将返回 MagickFalse。

我不是专家,不确定它是否适用于您的特定情况,但上面的代码实际上在 OS X ImageMagick 10.6.9 上对我有用。

希望这有帮助。

I tried defineSet and it didn't work for me, but next worked:

image.defineValue("png", "format", "png24");

My situation is a bit different so I use different png format specifier and value, in your case here should be:

u16MagickImg.defineValue("png", "color-type", "0");
u16MagickImg.defineValue("png", "bit-depth", "16");

See the format specifiers list here:
http://www.imagemagick.org/script/command-line-options.php#define

See information of the meaning of defineValue and defineSet methods of the Image class here:
http://www.imagemagick.org/Magick++/Image.html

Quotes from there:

defineValue: "Set or obtain a definition string to applied when encoding or decoding the specified format. The meanings of the definitions are format specific. The format is designated by the magick_ argument, the format-specific key is designated by key_, and the associated value is specified by value_. See the defineSet() method if the key must be removed entirely."
defineSet: "Set or obtain a definition flag to applied when encoding or decoding the specified format.. Similar to the defineValue() method except that passing the flag_ value 'true' creates a value-less define with that format and key. Passing the flag_ value 'false' removes any existing matching definition. The method returns 'true' if a matching key exists, and 'false' if no matching key exists."

Also some important information from the png.c source file:
If the image cannot be written without loss with the requested bit-depth and color-type, a PNG file will not be written, a warning will be issued, and the encoder will return MagickFalse.

I'm not an expert and not sure it will work in your particular case, but the code above is something that actually worked for me on OS X ImageMagick 10.6.9.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文