Hi to all.
Currently I am trying to decode ICO file with my own loader, but I am getting weird results.
Here is screen from debugger.
I have icon 170*170 px, with 32bit color, but in header info I see that height is twice bigger. It will be ok if image size also correspond to the size, but It does not.
170*170*4 = 115 600
170*340*4 = 231 200
But image size is 119 680.
After I read 115 600, there are 4080 bytes array, which is mostly filled with zeroes.
If I understood correctly, this is transparency buffer. I applied it, but in any case colors does not change.
Here is how I read colors data:
int realBitsCount = (int)iconImageInfo.Header.bitCount;
bool hasAndMask = /*(realBitsCount < 32) &&*/ (height != iconImageInfo.Header.height);
dataPtr = IntPtr.Add(dataPtr, Marshal.SizeOf<IconImageInfo>());
var buffer = new byte[width * height * 4];
var stream = new UnmanagedMemoryStream((byte*)dataPtr, iconInfo.BytesInRes);
var sizeinBytes = realBitsCount / 8;
int bufferOffset = 0;
if (realBitsCount == 32)
{
for (int i = 0; i < buffer.Length; i += sizeinBytes)
{
stream.Read(buffer, bufferOffset, sizeinBytes);
Utilities.Swap(ref buffer[bufferOffset], ref buffer[bufferOffset + 2]);
bufferOffset += sizeinBytes;
}
}
and here is how I read and apply transparency:
int shift;
int shift2;
byte bit;
int mask;
int boundary = width * realBitsCount; //!!! 32 bit boundary (http://www.daubnet.com/en/file-format-ico)
while (boundary % 32 != 0) boundary++;
boundary = width;
while (boundary % 32 != 0) boundary++;
var bufSize = iconInfo.BytesInRes - (width * height * (realBitsCount / 8));
var transparencyBuffer = new byte[bufSize];
stream.Read(transparencyBuffer, 0, (int)bufSize);
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
shift = 4 * (x + y * width) + 3;
bit = (byte)(7 - (x % 8));
shift2 = (x + (height - y - 1) * boundary) / 8;
var b = transparencyBuffer[shift2];
mask = (0x01 & (b >> bit));
var before = buffer[shift];
buffer[shift] *= (byte)(1 - mask);
}
}
And here is what I receive and what I should receive (from left to right)
Does anyone know what I am doing wrong here? Please, help me.