Tale of the old icons. Riddle of 18 colors. Transparent and inverse colors
Few people now do not care about the extra kilobytes or two. But there are such people, and just for such a
zadrota This note is written by a man. )
In those few cases when I needed to write down the expressive icon (.ico) of the application and at the same time should have saved bytes, I used the following hack: I recorded the image in 16-color mode - but! - not in the usual fixed palette, but in the adaptive palette.
What does it give? Icon 48x4? 1-bit transparency, 256 colors = 3774 bytes, it in 16 colors = 1662 bytes. Winning - 2 kilobytes, with a slight drop in image quality.
Example. Left to right:
fixed standard 16-color palette (and here a couple of minutes picked up the shading, so that the image had at least some decent appearance)
adaptive 16-color palette + dithering.
How it works? Strangely enough, a 16-color icon always carries a palette. That is, 99.9% of the old icons carry an absolutely identical 64-byte color table (4 bytes per color). And, yes - it turns out, it can be reprogrammed.
Unfortunately, I'm not familiar with the program, that I can fully work with these kinds of icons. So here's the recipe for how to "inferiorly" concoct such an icon: take IrfanView, load into it a picture: either prepared in advance 16-color, or reduce the number of colors right in it: menu - image - reduce the depth of color - 16 colors. Now: menu - save as - select the .ico format. All? No, not everything: Irfan View does not know how to work with transparency, it will have to be restored manually in another program. Microangelo Studio can load and edit such icons (alas, create a library from them, just like creating such icons "from scratch" it does not allow). Download the icon in the Microangelo and perhaps, we get a window with a warning about the "incorrect palette" - what this means in practice, will be told later. With the tools "pouring" and "pencil" we mark transparent zones; write down; ready!
This was what I did before, about ten years ago. Now, looking closely at the Microangelo palette, I have questions. What is this strange palette, 18 colors? How can you store 18 colors in a file at all? The number is not round, gentlemen programmers do not do this; it's just not convenient for them. )) It is seen that in the palette there are continuous 16 colors and two service ones: "transparent" and "inverse". What is "inverted"? - if such a color occurs, it displays the negativity of what lies beneath it; This is most often used in cursors; and the cursor - it and in fact and the structure of the file - also an icon. But how are all these colors stored? If the usual image points are packed in 4 bits (16 colors), then the transparency can be stored only as a separate bitmask (1 bit per point of the image). Okay, where is the inversion stored? Another mask, or what? Not economical, but possible Let's calculate.
Icon 48x4? 16 colors, 1 image. The file size is 1662 bytes. What's in it? I have a poor understanding of the subtleties, but let's try to figure out. The main part, the image itself: 48 * 48/2 = 1152 bytes. Palette: 16 * 4 = 64 bytes. Mask transparency 48 * 48/8 = 288 bytes. Header .ico, officially = 6 bytes. Description of the frame (1 pc.) From the documentation = 16 bytes. Total: 1152 + 64 + 288 + 6 + 16 = 1526. Perhaps there is something else, because the actual file size is larger - but it does not matter; Another thing is important, the remainder of the "dissimilar" is small: 136 bytes, and it obviously does not allow placing another bit mask with a size of 288 bytes. So the inverse color is somehow embedded in the picture itself. Maybe now you can guess yourself, how did they do it?
And they did it, apparently, like this: in the original Windows (it seems that starting with Win 95OSR2 is full, and in Win XP and newer ones it's already partially), in icons and cursors, strictly speaking - there was no real transparent color. If the alpha mask showed: the current cursor /icon point is "transparent" - the color was taken under the cursor /icon and XOR-wasted ("exclusive" or "", and maybe there was just subtraction) - attention! - with the current color of the point taken from the icon palette.
What happens? If it was the first color (black in a fixed palette), XOR did with the background "nothing" - the color became just transparent. If it was the last color (in the fixed palette - white) - the color changed completely, turned into a negative. This is the whole secret, how the "18 colors" were stored Ie, once again: "transparent" = black (+) alpha mask, "inverse" = white (+) alpha mask. And now - let's talk about "glitches".
The above mentioned Microangelo warning says: "You do not have full white and black colors in the palette, transparency will not work correctly!" Indeed, our adaptive palette can do without black or white colors. How, then, will transparency work? Oh, just as we expect: some color from the palette (for example, green), "XOR" with the background will be taken, and we will get a beautiful violet color instead of transparency. )) Or another example - the place of the last color in the palette accidentally hit black: and the "inversion" turned into the usual full-fledged transparency.
Example: on the left is the standard palette, on the right is reprogrammed. On the left, the transparency (P) and the inversion (I) work as it should; on the right - obviously not.
However, all these charms of icons we can see only in the original Windows. Modern OSes, such as Win 1? ignore the colors of the palette, and the bit mask "transparency" always works exactly as transparency in pure form: XOR-a no, and there is accordingly no inversion, either. However, in the mechanism of displaying the cursor, there is still compatibility - there is a full-fledged old good economic scheme; "Warm, lamp", and very few people understand and necessary.
Example: different display of the same icons in Windows XP and in Windows 7.
It may be interesting
I am overwhelmed by your post with such a nice topic. Usually I visit your blogs and get updated through the information you include but today’s blog would be the most appreciable. Well done!
Took me time to understand all of the comments, but I seriously enjoyed the write-up. It proved being really helpful to me and Im positive to all of the commenters right here! Its constantly nice when you can not only be informed, but also entertained! I am certain you had enjoyable writing this write-up.