Click here to Skip to main content
16,017,351 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
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
Posted

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!
 
Share this answer
 
Comments
Quintec 24-Mar-11 16:34pm    
I started thinking in the same way, that there is no way of doing so in .NET, but it seems hard to believe that such a powerful technology doesnt allow to obtain the actual pixel value (the palette index) of an indexed image.

Thanks for you response, but I still hope someone can write an alternative to do it :-). And yes, it is for hidding text, but not for malicius purposes :)
OriginalGriff 25-Mar-11 4:37am    
It does allow you to access the pixel value. What it doesn't allow you to do is access the palette directly, because that is specific to GIF images, and not general to all. It abstracts the data at a higher level, by converting GIF to internal (probably bitmap) format, and then reversing the conversion when writing.
Quintec 25-Mar-11 7:40am    
In this case, it doesnt allow you to do many things you can do in GIF files, such as repeating colours in palette (could be really useful in many cases) or recolorize an image in a smart way. For example, it was common to create for example a car image carefully choosing its palette, in such a way, that chainging a section of the palete the car can change its colour without affecting anything else and with a realistic output.

Yesterday I leart the basics of PHP, and I achieved to do what Im asking in less than one hour. ¿how could it be that a powerfull language doesnt allow it?


PHP Function:

bool imagesetpixel ( resource $image , int $x , int $y , int $IndexColor )
imagesetpixel() draws a pixel at the specified coordinate with its associated color in the palette


There should be an alternative way to achieve it in .NET... Thanks anyways for your coment :-)
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.
 
Share this answer
 
Comments
Quintec 24-Mar-11 16:35pm    
I know that .NET comes with an image library, but it is missing (or at least, i cant find it) the functionality i am asking about, write/retrieve pixel values according to its index value in the colour table, instead of doing so in RGB.

Anyone? :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900