Reorganize Load_BMP()

This commit is contained in:
Thomas Bernard 2019-01-07 12:55:17 +01:00
parent 71d1089442
commit 55dadae79d
No known key found for this signature in database
GPG Key ID: 0FF11B67A5C0863C

View File

@ -3161,7 +3161,7 @@ static void Load_BMP_Pixels(T_IO_Context * context, FILE * file, unsigned int co
short y_pos;
byte * buffer;
byte value;
byte a,b,c=0;
byte a,b;
int bits[4];
int shift[4];
int i;
@ -3324,54 +3324,60 @@ static void Load_BMP_Pixels(T_IO_Context * context, FILE * file, unsigned int co
x_pos = 0;
y_pos = context->Height-1;
if(Read_byte(file, &a)!=1 || Read_byte(file, &b) != 1)
while (!File_error)
{
if(!(Read_byte(file, &a) && Read_byte(file, &b)))
{
File_error = 2;
while ( (!File_error) && ((a)||(b!=1)) )
{
if (a) // Encoded mode (A fois les 1/2 pixels de B)
for (index=1; index<=a; index++)
{
if (index & 1)
Set_pixel(context, x_pos,y_pos,b>>4);
else
Set_pixel(context, x_pos,y_pos,b&0xF);
x_pos++;
break;
}
else // Absolute mode
if (a > 0) // Encoded mode : pixel count = a
{
GFX2_Log(GFX2_DEBUG, "BI_RLE4: %d &%02X\n", a, b);
for (index = 0; index < a; index++)
Set_pixel(context, x_pos++, y_pos, ((index & 1) ? b : (b >> 4)) & 0x0f);
}
else
{
// a == 0 : Escape code
byte c = 0;
GFX2_Log(GFX2_DEBUG, "BI_RLE4: %d %d\n", a, b);
if (b == 1) // end of bitmap
break;
switch (b)
{
case 0 : //End of line
x_pos = 0;
y_pos--;
break;
case 1 : // End of bitmap
break;
case 2 : // Delta
if(Read_byte(file, &a)!=1 || Read_byte(file, &b)!=1)
File_error=2;
x_pos += a;
y_pos -= b;
break;
default: // Nouvelle série (B 1/2 pixels bruts)
for (index=1; ((index<=b) && (!File_error)); index++,x_pos++)
default: // Absolute mode : pixel count = b
for (index = 0; index < b && !File_error; index++, x_pos++)
{
if (index&1)
Set_pixel(context, x_pos,y_pos,c&0xF);
else
{
if(Read_byte(file, &c)!=1) File_error=2;
if (!Read_byte(file, &c))
File_error=2;
else
Set_pixel(context, x_pos,y_pos,c>>4);
}
else
Set_pixel(context, x_pos,y_pos,c&0xF);
}
// On lit l'octet rendant le nombre d'octets pair, si
// nécessaire. Encore un truc de crétin "made in MS".
if ( ((b&3)==1) || ((b&3)==2) )
if ((b + 1) & 2)
{
byte dummy;
if(Read_byte(file, &dummy)!=1) File_error=2;
// read a pad byte to enforce word alignment
if (!Read_byte(file, &c))
File_error = 2;
}
}
}
if(Read_byte(file, &a)!=1 || Read_byte(file, &b)!=1) File_error=2;
}
break;
default:
@ -3390,12 +3396,18 @@ void Load_BMP(T_IO_Context * context)
byte true_color = 0;
dword mask[4]; // R G B A
file = Open_file_read(context);
if (file == NULL)
{
File_error = 1;
return;
}
File_error = 0;
if ((file=Open_file_read(context)))
{
file_size = File_length_file(file);
/* Read header */
if (!(Read_bytes(file,header.Signature,2)
&& Read_dword_le(file,&(header.Size_1))
&& Read_word_le(file,&(header.Reserved_1))
@ -3422,7 +3434,8 @@ void Load_BMP(T_IO_Context * context)
&& Read_dword_le(file,&(header.YPM))
&& Read_dword_le(file,&(header.Nb_Clr))
&& Read_dword_le(file,&(header.Clr_Imprt))
)) File_error = 1;
))
File_error = 1;
else
GFX2_Log(GFX2_DEBUG, "Windows BMP %ux%d planes=%u bpp=%u compression=%u\n",
header.Width, header.Height, header.Planes, header.Nb_bits, header.Compression);
@ -3451,11 +3464,13 @@ void Load_BMP(T_IO_Context * context)
else
{
Warning("Unknown BMP type");
File_error = 2;
File_error = 1;
}
}
if (File_error == 0)
{
/* header was read */
switch (header.Nb_bits)
{
case 1 :
@ -3473,7 +3488,7 @@ void Load_BMP(T_IO_Context * context)
true_color = 1;
break;
default:
Warning("Unsupported bit per pixel");
GFX2_Log(GFX2_WARNING, "BMP: Unsupported bit per pixel %u\n", header.Nb_bits);
File_error = 1;
}
@ -3515,7 +3530,6 @@ void Load_BMP(T_IO_Context * context)
if (header.Size_2 >= 108)
{
Read_dword_le(file,&mask[3]); // Alpha mask
if (header.Size_2 == 124)
fseek(file, header.Size_2 - 40 - 16, SEEK_CUR); // skip extended v4/v5 header fields
}
GFX2_Log(GFX2_DEBUG, "BMP masks : R=%08x G=%08x B=%08x A=%08x\n", mask[0], mask[1], mask[2], mask[3]);
@ -3533,15 +3547,8 @@ void Load_BMP(T_IO_Context * context)
}
}
}
else
{
File_error=1;
}
fclose(file);
}
else
File_error=1;
}
/// Save BMP file