什么 C 库允许缩放巨大的图像?

发布于 2024-09-10 21:14:36 字数 275 浏览 6 评论 0原文

考虑以下文件:

-rw-r--r-- 1 user user 470886479 2009-12-15 08:26 the_known_universe.png

如何使用不超过 4GB 的 RAM 将图像缩小到合理的分辨率?

例如:

$ convert -scale 7666x3833 the_known_universe.png

什么 C 库可以处理它?

谢谢你!

Consider the following file:

-rw-r--r-- 1 user user 470886479 2009-12-15 08:26 the_known_universe.png

How would you scale the image down to a reasonable resolution, using no more than 4GB of RAM?

For example:

$ convert -scale 7666x3833 the_known_universe.png

What C library would handle it?

Thank you!

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

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

发布评论

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

评论(7

鹿港小镇 2024-09-17 21:14:37

我相信 libpng 有一个流接口。我认为这可以用来一次读取图像的一部分;根据图像文件,您也许能够按顺序排列行。然后,您可以收缩每一行(例如,收缩 50%,水平收缩行并丢弃每隔一行)并写入输出文件。

在 C 中使用 libpng 可能需要相当多的代码,但文档可以很好地指导您完成它。

http://www.libpng.org/ pub/png/libpng-1.2.5-manual.html#section-3.8

I believe libpng has a stream interface. I think this can be used to read parts of the image at a time; depending on the image file you might be able to get the lines in order. You could then shrink each line (e.g. for 50% shrinking, shrink the line horizontally and discard every second line) and write to an output file.

Using libpng in C can take a fair amount of code, but the documentation guides you through it pretty well.

http://www.libpng.org/pub/png/libpng-1.2.5-manual.html#section-3.8

临风闻羌笛 2024-09-17 21:14:37

您可以尝试制作 64 位版本的 ImageMagick 或看看是否有。我的同事写了一篇博客,其中包含一个超级简单的 png 解码器(假设您有 zlib 或同等版本),这样您就可以看到自己推出所需的代码。

http://www.atalasoft.com/cs/blogs/stevehawley/archive/2010/02/23/libpng-you-re-doing-it-wrong.aspx

您需要按照您的方式进行重新采样正在读进去。

You could try making a 64 bit build of ImageMagick or seeing if there is one. My colleague wrote a blog with a super-simple png decoder (assumes you have zlib or equivalent) so you can kind of see the code you'd need to roll your own.

http://www.atalasoft.com/cs/blogs/stevehawley/archive/2010/02/23/libpng-you-re-doing-it-wrong.aspx

You would need to do the resample as you're reading it in.

纸伞微斜 2024-09-17 21:14:37

几年前我使用过cximage。我认为最新版本位于
http://www.xdp.it/cximage.htm
离开 CodeProject 后。

编辑:抱歉,这是C++而不是C。

I used cximage a few years ago. I think the latest version is at
http://www.xdp.it/cximage.htm
after moving off of CodeProject.

Edit: sorry, it's C++ not C.

柠檬色的秋千 2024-09-17 21:14:37

您可以使用旨在对大(和小)图像执行复杂操作的图像处理库。一个例子是 IM 成像工具包。它与 C 链接良好(但至少部分是用 C++ 实现的)并且与 Lua 具有良好的绑定。从 Lua 绑定来看,应该很容易进行实验。

You could use an image processing library that is intended to do complex operations on large (and small) images. One example is the IM imaging toolkit. It links well with C (but is implemented at least partly in C++) and has a good binding to Lua. From the Lua binding it should be easy to experiment.

抹茶夏天i‖ 2024-09-17 21:14:37

libvips 适合处理巨大的图像。它是一个流式图像处理库,因此它可以同时并行地从源读取、处理并写入目标。它通常比 imagemagick 快 3 到 5 倍,并且需要很少的内存。

例如,对于我笔记本电脑上最大的 PNG (1.8GB),我可以通过以下方式缩小 10 倍:

$ vipsheader huge.png
huge.png: 72000x72000 uchar, 3 bands, srgb, pngload
$ ls -l huge.png 
-rw-r--r-- 1 john john 1785845477 Feb 19 09:39 huge.png
$ time vips resize huge.png x.png 0.1
real    1m35.279s
user    1m49.178s
sys 0m1.208s
peak RES 230mb

速度不快,但也不会太差。 PNG 是一种相当慢的格式,使用 TIFF 会快得多。

libvips 可由大多数软件包管理器安装(例如 macOS 上的 homebrew,Debian 上的 apt),有一个 Windows 二进制文件< /a>,而且它是免费的(LGPL)。除了命令行之外,还有 C、C++、Python、Ruby、Lua、node、PHP 等的绑定。

libvips is comfortable with huge images. It's a streaming image processing library, so it can read from the source, process, and write to the destination simultaneously and in parallel. It's typically 3x to 5x faster than imagemagick and needs very little memory.

For example, with the largest PNG I have on my laptop (1.8gb), I can downsize 10x with:

$ vipsheader huge.png
huge.png: 72000x72000 uchar, 3 bands, srgb, pngload
$ ls -l huge.png 
-rw-r--r-- 1 john john 1785845477 Feb 19 09:39 huge.png
$ time vips resize huge.png x.png 0.1
real    1m35.279s
user    1m49.178s
sys 0m1.208s
peak RES 230mb

Not fast, but not too shabby either. PNG is rather a slow format, it would be much quicker with TIFF.

libvips is installable by most package managers (eg. homebrew on macOS, apt on Debian), there's a Windows binary, and it's free (LGPL). As well as the command-line, there are bindings for C, C++, Python, Ruby, Lua, node, PHP, and others.

清旖 2024-09-17 21:14:37

您是否考虑过探索基于金字塔的图像?想象一个金字塔,其中图像被分为多个层,每个层具有不同的分辨率。每层都分为图块。
通过这种方式,您可以显示图像的缩小版本以及图像的局部放大视图,而无需重新缩放。

请参阅维基百科条目

FlashPix 是原始格式之一,我为其编写了一个渲染器。
我还创建了一种新格式的金字塔转换器和渲染器,用于医疗应用程序。实际的扫描仪可以生成 90GB 以上的器官切片扫描结果,用于癌症研究。
转换器的算法实际上非常棘手,要高效地生成金字塔图像。不管你相信与否,它实际上是基于 Java 的,而且它的性能比你想象的要好得多。它使用了多线程。基准测试表明 C 版本不太可能做得更好。这是六年前的事了。我十多年前做的原始渲染器。
这些天你再也不会听到任何关于基于金字塔的图像了。但这确实是按需生成缩放图像而无需生成缓存缩放版本的唯一有效方法。

Jpeg2000 可能也可能没有可选的金字塔功能。

我记得 ImageMagick 的支持格式和转换可能包括 FlashPix。
谷歌搜索“图像金字塔”揭示了一些有趣的结果。带回一些回忆;-)

Have you considered exploring pyramid based images? Imagine a pyramid where the image is divided up in multiple layers, each layer with a different resolution. Each layer is split up into tiles.
This way you can display a zoomed out version of the image, and also a zoomed in partial view of the image, without having to re-scale.

See the Wikipedia entry.

One of the original formats was FlashPix, which I wrote a renderer for.
I've also created a new format of a pyramid converter and renderer, which was used for a medical application. An actual scanner would produce 90GB+ scans of a slice of an organ for cancer research.
The algorithm of the converter was actually pretty tricky to get efficient, to produce the pyramid images efficienty. Believe it or not, it was actually Java based, and it performed much better than you'd think. It used multithreading. Benchmarking showed it was unlikely that a C version would do a whole lot better. This was 6ish years ago. The original renderer I did over 10 years ago.
You don't hear anything about pyramid based images anymore these days. But it's really the only efficient way to produce scaled images on demand without having to generate cached scaled versions.

Jpeg2000 may or may not have an optional pyramid feature as well.

I recall that ImageMagick's supporter formats and conversions perhaps, include FlashPix.
Googling for "image pyramid" reveals some interesting results. Bring back some memories ;-)

独自唱情﹋歌 2024-09-17 21:14:37

如果您可以将其移动到 64 位操作系统,您可以将其作为内存映射文件或等效文件打开,并使用几乎任何您想要的库。它不会很快,并且可能需要增加页面/交换文件(取决于操作系统以及您想用它做什么),但作为回报,您不会仅限于流媒体库,因此您将能够在进行分辨率降低或切片之前执行更多操作。

If you can move it to a 64-bit OS you can open it as a memory mapped file or equivalent and use pretty much any library you want. It won't be fast, and may need the increase of the page/swap file (depending on the OS and what else you want to do with it) but in return you won't be limited to streaming libraries so you'll be able to do more operation before going into resolution reduction or slicing.

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