使用 JAI,如何向调色板添加新颜色?

发布于 2024-12-14 06:11:54 字数 868 浏览 1 评论 0原文

我正在使用 JAI 为图像添加“边框”。例如,

ParameterBlock pb = new ParameterBlock();
pb.addSource(r);
pb.add(leftPad);
pb.add(rightPad);
pb.add(topPad);
pb.add(bottomPad);

pb.add(new BorderExtenderConstant(consts));

r = JAI.create("border", pb);

上面“consts”的值取决于 ColorModel。使用 ComponentColorModel 时,每个像素都有自己的颜色,因此我不必弄乱调色板。当图像有调色板(GIF、PNG 等)时,ColorModel 为 IndexColorModel。

当使用 IndexColorModel 时,“consts”是一个 double[] 数组,大小为 1。数组中的值是调色板中的索引。

我已经找到了如何通过创建新的 IndexColorModel 来修改调色板,但我必须编写的逻辑将是疯狂的! (例如,调色板中可以有多少种颜色取决于许多因素。此外,如果我需要从调色板中删除一种颜色以添加新颜色,我需要逻辑来确定哪种颜色最好删除并然后修改之前引用该颜色的所有像素——哇,这是很多代码!)

所以,我的问题是,如何向调色板添加一种颜色?有现成的图书馆吗?或者我应该使用 ioimage 中的东西? (说实话,我有点困惑 jai 在哪里“结束”,而 ioimage 在哪里“开始”。)

另外,附带问题,由于某种原因,我的测试图像在调色板中只有大约 10 种颜色,被读取为具有 256 种颜色。然后,当我使用 jai 保存图像时,所有 256 种颜色都会被保存(11 到 255 都是黑色)。知道为什么要这样做吗?

谢谢! 大卫

I'm using JAI to add a "border" to an image. Eg

ParameterBlock pb = new ParameterBlock();
pb.addSource(r);
pb.add(leftPad);
pb.add(rightPad);
pb.add(topPad);
pb.add(bottomPad);

pb.add(new BorderExtenderConstant(consts));

r = JAI.create("border", pb);

The value of 'consts' above depends on the ColorModel. When using ComponentColorModel each pixel has its own color so I don't have to mess with a palette. When an image has a palette (GIFs, PNGs, ...) the ColorModel is IndexColorModel.

When IndexColorModel is being used then 'consts' is a double[] array, with the size of one. The value in the array is the index in the color palette.

I have found how to modify the palette by creating a new IndexColorModel but the logic I would have to code would be insane! (Eg. How many colors I can have in the palette depends on many factors. Additionally if I need to remove a color from the palette in order to add the new one, I would need logic that determines which color would be best to remove and then modify all pixels previously referencing that color -- wow, that's a lot of code!)

So, my question is, how does one add a color to the palette? Is there an existing library? Or should I be using something from ioimage? (To be honest I'm a little confused where jai "ends" and ioimage "starts".)

Also, side question, for some reason my test images that only have about 10 colors in the palette are read in as having 256 colors. When I then save the image with jai all the 256 colors are saved (11 through 255 are all black). Any idea why it's doing this?

Thanks!
David

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

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

发布评论

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

评论(1

女中豪杰 2024-12-21 06:11:54

我针对上述问题提出的最佳解决方案是将图像从 IndexColorModel 转换为 ComponentColorModel。 (ComponentColorModel 的每个像素都指定其自己的颜色,因此您无需使用调色板 - 您可以轻松地使用您想要的任何颜色。)

在阅读 http://java.sun.com/products/java-media/jai/forDevelopers/jaifaq.html#palette

这是我在阅读图像后所做的事情:

if(image.getColorModel() instanceof IndexColorModel) {
    IndexColorModel icm = (IndexColorModel)image.getColorModel();
    byte[][] data = new byte[4][icm.getMapSize()];

    icm.getReds(data[0]);
    icm.getGreens(data[1]);
    icm.getBlues(data[2]);
    icm.getAlphas(data[3]);

    LookupTableJAI lut = new LookupTableJAI(data);

    image = JAI.create("lookup", image, lut);
}

一旦执行然后您可以将图像隐藏回来。我还没有花时间去弄清楚这一点。如果其他人想弄清楚,您可能需要阅读以下内容: http://www.java.net/节点/675577

The best solution I've been able to come up with to my question above is to convert the image from IndexColorModel to ComponentColorModel. (Each pixel of a ComponentColorModel specifies its own color so there is no pallet you have to work with -- you can easily use any color you want.)

I cam up with this simple solution after reading http://java.sun.com/products/java-media/jai/forDevelopers/jaifaq.html#palette

This is what I am doing after reading an image in:

if(image.getColorModel() instanceof IndexColorModel) {
    IndexColorModel icm = (IndexColorModel)image.getColorModel();
    byte[][] data = new byte[4][icm.getMapSize()];

    icm.getReds(data[0]);
    icm.getGreens(data[1]);
    icm.getBlues(data[2]);
    icm.getAlphas(data[3]);

    LookupTableJAI lut = new LookupTableJAI(data);

    image = JAI.create("lookup", image, lut);
}

Once doing the manipulation you can then covert the image back. I haven't spent the time to figure that out. If someone else wants to figure it out you might want to read this: http://www.java.net/node/675577

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