I have 23 images in this format, my code loads 22 of them perfectly. One has a glitch.
It seems to be an initial value problem. When you start a new line of the graphic, some modes seem to expect the first pixel to be set to zero, some seem to expect it to be set to the same value as the pixel above.
It makes no sense at all
for (int y = 0; y < height; y++)
{
if (mode[y] < 4)
{
r = g = b = 0;
}
for (int x = 0; x < width; x++)
{
#region Decode
switch (mode[y])
{
// none
case 0:
r = br.ReadByte();
g = br.ReadByte();
b = br.ReadByte();
a = 255;
break;
// sub
case 1:
r += br.ReadByte();
g += br.ReadByte();
b += br.ReadByte();
a = 255;
break;
// up
case 2:
b = data[pos - prev];
g = data[pos - prev + 1];
r = data[pos - prev + 2];
r += br.ReadByte();
g += br.ReadByte();
b += br.ReadByte();
a = 255;
break;
// average
case 3:
r = Average(r, data[pos - prev + 2]);
g = Average(g, data[pos - prev + 1]);
b = Average(b, data[pos - prev]);
r += br.ReadByte();
g += br.ReadByte();
b += br.ReadByte();
a = 255;
break;
// paeth
case 4:
if (pos < prev + 4)
{
r = (byte)paethPredictor(r, data[pos - prev + 2], 0);
g = (byte)paethPredictor(g, data[pos - prev + 1], 0);
b = (byte)paethPredictor(b, data[pos - prev], 0);
}
else
{
if (x > 0)
{
r = (byte)paethPredictor(r, data[pos - prev + 2], data[pos - prev - 2]);
g = (byte)paethPredictor(g, data[pos - prev + 1], data[pos - prev - 3]);
b = (byte)paethPredictor(b, data[pos - prev], data[pos - prev - 4]);
}
else
{
r = (byte)paethPredictor(data[pos - prev + 2], data[pos - prev + 2], data[pos - prev - 2]);
g = (byte)paethPredictor(data[pos - prev + 1], data[pos - prev + 1], data[pos - prev - 3]);
b = (byte)paethPredictor(data[pos - prev], data[pos - prev], data[pos - prev - 4]);
}
}
r += br.ReadByte();
g += br.ReadByte();
b += br.ReadByte();
a = 255;
break;
}
#endregion
data[pos++] = b;
data[pos++] = g;
data[pos++] = r;
data[pos++] = a;
}
}
Two images of my test app, the left window is the RGB texture, middle the alpha mask, right the combination.
And here is the one graphic with a glitch