操纵8位色索引图像中的像素 [英] Manipulating pixels in 8bit-Colour-Indexed Images

查看:104
本文介绍了操纵8位色索引图像中的像素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我使用ASP.net/C#

我的英语不太好,但是我会尽力解释我的需求.

有两个问题:


问题1)我希望能够从GIF格式文件中了解像素的颜色索引.

如您所知,我的意思是说,gif图像文件包含一个浅色,它是一个长度为256的数组.从0到255的每个数组项都包含一种颜色,并且在GIF文件中,颜色可以在不同的位置重复.例如,我可以有一个灰度调色板,看起来像这样[#000000,#010101,#020202,...,#FEFEFE,#FFFFFF],但是在我的情况下,所有颜色都相同,让说红色:
[#FF0000,#FF0000,#FF000,...,#FF0000].这在GIF文件中是完全正确的,因为每个像素均不与颜色相关,而是与颜色表中的索引相关.因此,即使整个图像是红色的(看起来是红色的),实际的位图数据也可能是这样的:

(此代码是概念性的,而不是实际的C#代码)
palete =红色//[#ff0000,...,#ff0000]所有红色
pixelData = [[1,2,3,4,5,6,7,8,9,10 ... 256],
[1,2,3,4,5,6,7,8,9,10 ... 256],
...
[1,2,3,4,5,6,7,8,9,0 ... 256]]; //假设像素数据是颜色索引的矩阵.


在此示例中,我的图像具有调色板中所有可能的颜色,但是由于调色板全为红色,因此图像将显示为红色.

好的,没关系,但是现在我的问题是...

如何打开该外部GIF文件并遍历像素,以在调色板表中检索其关联的颜色索引而不是实际颜色?根据前面的示例:

int colourIndex = miImage.getPixelColourIndex(0,0);

我不想检索值#ff0000,相反,我想获取值1.

如果我说:

int colourIndex = miImage.getPixelColourIndex(1,0); //colourIndex应该为2,因为[[1, 2 ,3 ....]

¿有人知道我需要什么吗?我需要知道与一个像素关联的颜色索引,而不是该像素的实际颜色(因为多个颜色索引可以指向相同的颜色值,在这种情况下为红色).



问题2)问题2恰好相反.我想根据颜色索引值而不是颜色写索引图像(然后将其保存到GIF).

那就是说,我想做:

myImage.setPixel(0,0,1); //是setPixel(x,y,colourIndex);

我想要这样,而不是说实际的颜色,它看起来像是:

myImage.setPixel(0,0,#ff0000); //我不想要这个,我想使用索引值而不是颜色,因为我的调色板将包含所有红色值.



请!!!我将对此表示衷心的感谢,我想没有一个本机函数可以执行此操作,但是可能有一种解决方法,不是吗?

感谢您的阅读和事先的帮助


据我所知,没有内置的方法可以做到这一点:您将不得不手动读取和写入文件.这将是有问题的,因为文件不仅是二进制和复杂的,而且还压缩了.

要开始使用,请: Wikipedia [ ^ ]可以提供帮助-它还包含对正式规范的引用(并非所有软件都严格遵守该规范:MS Paint是一个示例).

我怀疑您想用它来隐藏图像中的信息:请注意,任何转换都可能会放松或破坏信息,包括缩放...

祝您好运:您的项目比您想像中的要大!


不要打扰!
.NET已经具有功能强大的图像库,可以很好地处理GIF.
使用它!
如果您还不够,请告诉我们确切缺少的内容.
您不需要了解GIF文件格式的内部.
.NET知道如何管理它.只是学习一点.

P.S.我知道GIF文件(而且我讨厌它们)
以前,当我在Pascal/MS-DOS中对图像查看器进行编程时,我曾经很习惯地做自己的事情!
这很有趣,但是如今,它实际上只是在浪费时间.
抱歉.


Hi Im using ASP.net / C#

My english is not very good, but i will try my best to explain what i need.

There are 2 questions:


Question 1) I want to be able to know the colour index of a pixel from a GIF format file.

With that I mean, as you know, a gif image file contains a colour palete, which is an array of length 256. Each array item from 0 to 255 contains a colour, and in GIF files, colours can be repeated in different positions. For example, I can have a grayscale palette, which will look something like this [#000000, #010101, #020202, ..., #FEFEFE, #FFFFFF], but in my case, all the colours are the same, lets say red:
[#FF0000,#FF0000,#FF000, ..., #FF0000]. That is perfectly valid in a GIF File, since each pixel is not tied to a colour, instead, it is tied to an index in the colour table. So even if the whole image is red (looks red), the actual bitmap data could be something like this:

(this code is conceptual, not actual C# code)
palete = redPalete; // [#ff0000,..., #ff0000] all red colours
pixelData = [[1,2,3,4,5,6,7,8,9,10 ... 256],
[1,2,3,4,5,6,7,8,9,10 ... 256],
...
[1,2,3,4,5,6,7,8,9,0 ... 256]]; //supose the pixel data is a matrix of colour indexes.


In this example, my image has all possible colours from the palette, but since the palette is all red, the image will look red.

Ok, that is ok, but now my problem, is...

How can I open that external GIF File, and iterate through pixels, retrieving its associated colour index in the palette table instead of the actual colour?? According to the previous example:

int colourIndex = miImage.getPixelColourIndex(0,0);

and i dont want to retrieve the value #ff0000, instead, i want to obtain the value 1.

and if i say:

int colourIndex = miImage.getPixelColourIndex(1,0); //colourIndex should be 2, since [[1,2,3 ....]

¿anyone got the idea of what i need? I need to know the colour index associated with a pixel, instead of the actual colour of that pixel (since several colour indexes can point to the same colour value, in this case red).



Question 2) Question 2 is exactly the opposite case. I want to write an indexed image (and then save it to GIF), according to the colour indexes values, instead of the colour.

That means, i want to do:

myImage.setPixel(0,0,1); //being setPixel(x, y, colourIndex);

i want that instead of saying the actual colour, which could look like:

myImage.setPixel(0,0,#ff0000); // I dont want this, i want to use the index value, not the colour, since my palette will contain all red values.



Please!!! I will sincerely appreciate help with this, I guess there is not a native function to do that, but probably there is a way around it, isnt it??

Thank you for reading and for your help in advance


Alvaro

解决方案

As far as I know, there is no built in way to do this: you will have to read and write the file manually. That''s going to be problematic, as the file is not only binary and complex, but compressed as well.

To get you started: Wikipedia[^] can help - it also contains references to the formal specification (not that all software adheres strictly to that: MS Paint is one example).

I suspect that you want to use this to hide information in an image: be aware that any transformation may well loose or corrupt the information, including scaling...

Good luck: you have a bigger project than you thought on your hands!


Don''t bother!
.NET already has a powerful image library which handles GIFs just fine.
Use it!
If it is not enough for you, tell us what''s missing exactly.
You don''t need to know the internals of the GIF file format.
.NET knows how to manage it. Just learn a bit.

P.S. I know GIF files (and I hate them)
I used to do your way a looooooong time ago, when I programmed an image viewer in Pascal / MS-DOS!
It was fun, but nowadays it''s really just a waste of time.
Sorry.


这篇关于操纵8位色索引图像中的像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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