有一种简单的方法可以减少IndexedColorModel中的颜色数量吗? [英] Is there an easy way to reduce the number of colors in an IndexedColorModel?

查看:259
本文介绍了有一种简单的方法可以减少IndexedColorModel中的颜色数量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大的8位PNG图像。我使用Java将图像切割成较小的32x32图像。我使用Java的 ImageIO 将PNG加载到 BufferedImage 中,然后调用它的 getSubimage(x ,y,32,32)。然后我使用 ImageIO 将每个图块写成PNG。

I have a large 8-bit PNG image. I am using Java to slice the image into smaller 32x32 images. I use Java's ImageIO to load the PNG into a BufferedImage and then call it's getSubimage(x, y, 32, 32). I then use ImageIO to write each tile out as a PNG.

问题是生成的图像具有相同的 IndexColorModel 作为原始图像。例如,一个32x32磁贴只有8种颜色,但它包含一个颜色模型,其中包含原始图像中的所有100种颜色。

The problem is that the resulting image has the same IndexColorModel as the original image. For example, one 32x32 tile has only 8 total colors but it includes a color model with all 100-odd colors from the original image.

我想删除未使用的颜色在我写出PNG之前,从32x32 tile的 IndexColorModel 开始。没有任何意义,包括图像中未使用的颜色的颜色数据,我希望图像尽可能小。

I would like to remove unused colors from the 32x32 tile's IndexColorModel before I write out the PNG. There is no sense including color data for colors not used in the image and I'd like the images to be as small as possible.

是否有内置机制要做到这一点,还是有人能指点我(简单)修改/减少 ColorModel

Is there a built-in mechanism to do this or can someone point me to an (easy) way to modify/reduce the ColorModel?

谢谢!

推荐答案

在java.awt.image中查看 ColorConvertOp

Take a look at ColorConvertOp in java.awt.image.

基本上,您创建了所需深度的新 IndexColorModel 。如果你真的想要最小的,你可以走过 Raster 并计算颜色。否则,只需选择每像素4或5位。然后使用 TYPE_BYTE_BINARY IndexColorMap 创建 BufferedImage 。最后,使用 ColorConvertOp filter()方法将原始数据复制到新的 BufferedImage

Basically, you create a new IndexColorModel of the desired depth. If you really want the smallest, you can walk through the Raster and count the colors. Otherwise, just choose 4 or 5 bits per pixel. Then create a BufferedImage with TYPE_BYTE_BINARY and the IndexColorMap. Finally, use the ColorConvertOp's filter() method to copy the original data to the new BufferedImage.

这篇关于有一种简单的方法可以减少IndexedColorModel中的颜色数量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆