PDA

View Full Version : 4-Bit RLE BMP Images



xg0blin
03-04-2004, 10:20 AM
I have no trouble with 8-bit RLE, but 4-bit RLE won't render correctly (as I'm not loading it correctly). The code is pretty short, and should be simple (it's something obvious I'm missing that I'm just not seeing). Any help appreciated in advanced.




int LoadBMPRLE4Data(FILE *Fin, ulong Width, slong Height, ushort Depth, ubyte *Data)
{
ulong PixelsRead = 0, TotalPixels = Width * Height;
ubyte Garbage[3], First, Second;

while(PixelsRead < TotalPixels)
{
if(fread(&amp;First, sizeof(ubyte), 1, Fin) != 1)
{
perror("Failed to read in RLE encoded data");
return -1;
}
if(fread(&amp;Second, sizeof(ubyte), 1, Fin) != 1)
{
perror("Failed to read in RLE encoded data");
return -1;
}

if(First == 0)
{
if(Second == 0)
{
if(PixelsRead % Width != 0)
PixelsRead += (PixelsRead % Width);
}
else if(Second == 1)
break;
else if(Second == 2)
{
sbyte HOffSet, VOffSet;
if(fread(&amp;HOffSet, sizeof(sbyte), 1, Fin) != 1)
{
perror("Failed to read in RLE encoded data");
return -1;
}
if(fread(&amp;VOffSet, sizeof(sbyte), 1, Fin) != 1)
{
perror("Failed to read in RLE encoded data");
return -1;
}
PixelsRead += (VOffSet * Width) + HOffSet;
}
else
{
ubyte Color;
for(int j = 0; j < Second; )
{
if(fread(&amp;Color, sizeof(sbyte), 1, Fin) != 1)
{
perror("Failed to read in RLE encoded data");
return -1;
}
Data[PixelsRead++] = (Color & 0xF0) >> 4;
j++;
if(j < Second)
{
Data[PixelsRead++] = (Color & 0x0F);
j++;
}
}
if((Second % 8 != 0) &amp;&amp; (((Second + 1) % 8) != 0))
{
int Remains = ((Second + 1)/2) % 4;
int Fat = 4 - Remains;
if(fread(Garbage, sizeof(ubyte) * Fat, 1, Fin) != 1)
{
perror("Failed to read in RLE encoded data");
return -1;
}
}
}
}
else
{
for(int j = 0; j < First; )
{
Data[PixelsRead++] = (Second & 0xF0) >> 4;
j++;
if(j < First)
{
Data[PixelsRead++] = (Second & 0x0F);
j++;
}
}
}
}
return 0;
}