(RBMP) More optimizations

This commit is contained in:
twinaphex 2019-09-18 20:25:15 +02:00
parent b582f1c7c6
commit bd39d72acd

View File

@ -84,10 +84,7 @@ static int rbmp_get16le(rbmp_context *s)
return rbmp_get8(s) + (rbmp_get8(s) << 8); return rbmp_get8(s) + (rbmp_get8(s) << 8);
} }
static uint32_t rbmp_get32le(rbmp_context *s) #define RBMP_GET32LE(s) (rbmp_get16le(s) + (rbmp_get16le(s) << 16))
{
return rbmp_get16le(s) + (rbmp_get16le(s) << 16);
}
static unsigned char *rbmp_convert_format( static unsigned char *rbmp_convert_format(
unsigned char *data, unsigned char *data,
@ -266,11 +263,15 @@ static unsigned char *rbmp_bmp_load(rbmp_context *s, unsigned *x, unsigned *y,
if (rbmp_get8(s) != 'B' || rbmp_get8(s) != 'M') if (rbmp_get8(s) != 'B' || rbmp_get8(s) != 'M')
return 0; return 0;
rbmp_get32le(s); /* discard filesize */ /* discard filesize */
rbmp_get16le(s); /* discard reserved */ rbmp_get16le(s);
rbmp_get16le(s); /* discard reserved */ rbmp_get16le(s);
offset = rbmp_get32le(s); /* discard reserved */
hsz = rbmp_get32le(s); rbmp_get16le(s);
rbmp_get16le(s);
offset = (uint32_t)RBMP_GET32LE(s);
hsz = (uint32_t)RBMP_GET32LE(s);
/* BMP type not supported? */ /* BMP type not supported? */
if (hsz != 12 && hsz != 40 && hsz != 56 && hsz != 108 && hsz != 124) if (hsz != 12 && hsz != 40 && hsz != 56 && hsz != 108 && hsz != 124)
@ -283,8 +284,8 @@ static unsigned char *rbmp_bmp_load(rbmp_context *s, unsigned *x, unsigned *y,
} }
else else
{ {
s->img_x = rbmp_get32le(s); s->img_x = (uint32_t)RBMP_GET32LE(s);
s->img_y = rbmp_get32le(s); s->img_y = (uint32_t)RBMP_GET32LE(s);
} }
/* Bad BMP? */ /* Bad BMP? */
@ -307,26 +308,40 @@ static unsigned char *rbmp_bmp_load(rbmp_context *s, unsigned *x, unsigned *y,
} }
else else
{ {
int compress = rbmp_get32le(s); int compress = (uint32_t)RBMP_GET32LE(s);
/* BMP RLE type not supported? */ /* BMP RLE type not supported? */
if (compress == 1 || compress == 2) if (compress == 1 || compress == 2)
return 0; return 0;
rbmp_get32le(s); /* discard sizeof */ /* discord sizeof */
rbmp_get32le(s); /* discard hres */ rbmp_get16le(s);
rbmp_get32le(s); /* discard vres */ rbmp_get16le(s);
rbmp_get32le(s); /* discard colors used */ /* discard hres */
rbmp_get32le(s); /* discard max important */ rbmp_get16le(s);
rbmp_get16le(s);
/* discard vres */
rbmp_get16le(s);
rbmp_get16le(s);
/* discard colors used */
rbmp_get16le(s);
rbmp_get16le(s);
/* discard max important */
rbmp_get16le(s);
rbmp_get16le(s);
if (hsz == 40 || hsz == 56) if (hsz == 40 || hsz == 56)
{ {
if (hsz == 56) if (hsz == 56)
{ {
rbmp_get32le(s); rbmp_get16le(s);
rbmp_get32le(s); rbmp_get16le(s);
rbmp_get32le(s); rbmp_get16le(s);
rbmp_get32le(s); rbmp_get16le(s);
rbmp_get16le(s);
rbmp_get16le(s);
rbmp_get16le(s);
rbmp_get16le(s);
} }
if (bpp == 16 || bpp == 32) if (bpp == 16 || bpp == 32)
{ {
@ -350,9 +365,9 @@ static unsigned char *rbmp_bmp_load(rbmp_context *s, unsigned *x, unsigned *y,
#endif #endif
break; break;
case 3: case 3:
mr = rbmp_get32le(s); mr = (uint32_t)RBMP_GET32LE(s);
mg = rbmp_get32le(s); mg = (uint32_t)RBMP_GET32LE(s);
mb = rbmp_get32le(s); mb = (uint32_t)RBMP_GET32LE(s);
/* not documented, but generated by /* not documented, but generated by
* Photoshop and handled by MS Paint */ * Photoshop and handled by MS Paint */
/* Bad BMP ?*/ /* Bad BMP ?*/
@ -372,19 +387,33 @@ static unsigned char *rbmp_bmp_load(rbmp_context *s, unsigned *x, unsigned *y,
} }
else else
{ {
mr = rbmp_get32le(s); mr = (uint32_t)RBMP_GET32LE(s);
mg = rbmp_get32le(s); mg = (uint32_t)RBMP_GET32LE(s);
mb = rbmp_get32le(s); mb = (uint32_t)RBMP_GET32LE(s);
ma = rbmp_get32le(s); ma = (uint32_t)RBMP_GET32LE(s);
rbmp_get32le(s); /* Discard color space */ /* Discard color space */
rbmp_get16le(s);
rbmp_get16le(s);
for (i = 0; i < 12; ++i) for (i = 0; i < 12; ++i)
rbmp_get32le(s); /* Discard color space parameters */ {
/* Discard color space parameters */
rbmp_get16le(s);
rbmp_get16le(s);
}
if (hsz == 124) if (hsz == 124)
{ {
rbmp_get32le(s); /* Discard rendering intent */ /* Discard rendering intent */
rbmp_get32le(s); /* Discard offset of profile data */ rbmp_get16le(s);
rbmp_get32le(s); /* Discard size of profile data */ rbmp_get16le(s);
rbmp_get32le(s); /* Discard reserved */ /* Discard offset of profile data */
rbmp_get16le(s);
rbmp_get16le(s);
/* Discard size of profile data */
rbmp_get16le(s);
rbmp_get16le(s);
/* Discard reserved */
rbmp_get16le(s);
rbmp_get16le(s);
} }
} }
if (bpp < 16) if (bpp < 16)
@ -571,10 +600,12 @@ static unsigned char *rbmp_bmp_load(rbmp_context *s, unsigned *x, unsigned *y,
{ {
/* Need to apply alpha channel as well */ /* Need to apply alpha channel as well */
if (ma) if (ma)
{
if (bpp == 16)
{ {
for (i = 0; i < (int) s->img_x; ++i) for (i = 0; i < (int) s->img_x; ++i)
{ {
uint32_t v = (bpp == 16 ? (uint32_t) rbmp_get16le(s) : rbmp_get32le(s)); uint32_t v = (uint32_t)rbmp_get16le(s);
out[z++] = RBMP_BYTECAST(rbmp_shiftsigned(v & mr, rshift, rcount)); out[z++] = RBMP_BYTECAST(rbmp_shiftsigned(v & mr, rshift, rcount));
out[z++] = RBMP_BYTECAST(rbmp_shiftsigned(v & mg, gshift, gcount)); out[z++] = RBMP_BYTECAST(rbmp_shiftsigned(v & mg, gshift, gcount));
out[z++] = RBMP_BYTECAST(rbmp_shiftsigned(v & mb, bshift, bcount)); out[z++] = RBMP_BYTECAST(rbmp_shiftsigned(v & mb, bshift, bcount));
@ -585,13 +616,39 @@ static unsigned char *rbmp_bmp_load(rbmp_context *s, unsigned *x, unsigned *y,
{ {
for (i = 0; i < (int) s->img_x; ++i) for (i = 0; i < (int) s->img_x; ++i)
{ {
uint32_t v = (bpp == 16 ? (uint32_t) rbmp_get16le(s) : rbmp_get32le(s)); uint32_t v = (uint32_t)RBMP_GET32LE(s);
out[z++] = RBMP_BYTECAST(rbmp_shiftsigned(v & mr, rshift, rcount));
out[z++] = RBMP_BYTECAST(rbmp_shiftsigned(v & mg, gshift, gcount));
out[z++] = RBMP_BYTECAST(rbmp_shiftsigned(v & mb, bshift, bcount));
out[z++] = RBMP_BYTECAST(rbmp_shiftsigned(v & ma, ashift, acount));
}
}
}
else
{
if (bpp == 16)
{
for (i = 0; i < (int) s->img_x; ++i)
{
uint32_t v = (uint32_t)rbmp_get16le(s);
out[z++] = RBMP_BYTECAST(rbmp_shiftsigned(v & mr, rshift, rcount)); out[z++] = RBMP_BYTECAST(rbmp_shiftsigned(v & mr, rshift, rcount));
out[z++] = RBMP_BYTECAST(rbmp_shiftsigned(v & mg, gshift, gcount)); out[z++] = RBMP_BYTECAST(rbmp_shiftsigned(v & mg, gshift, gcount));
out[z++] = RBMP_BYTECAST(rbmp_shiftsigned(v & mb, bshift, bcount)); out[z++] = RBMP_BYTECAST(rbmp_shiftsigned(v & mb, bshift, bcount));
out[z++] = RBMP_BYTECAST(255); out[z++] = RBMP_BYTECAST(255);
} }
} }
else
{
for (i = 0; i < (int) s->img_x; ++i)
{
uint32_t v = (uint32_t)RBMP_GET32LE(s);
out[z++] = RBMP_BYTECAST(rbmp_shiftsigned(v & mr, rshift, rcount));
out[z++] = RBMP_BYTECAST(rbmp_shiftsigned(v & mg, gshift, gcount));
out[z++] = RBMP_BYTECAST(rbmp_shiftsigned(v & mb, bshift, bcount));
out[z++] = RBMP_BYTECAST(255);
}
}
}
} }
else else
{ {
@ -609,7 +666,7 @@ static unsigned char *rbmp_bmp_load(rbmp_context *s, unsigned *x, unsigned *y,
{ {
for (i = 0; i < (int) s->img_x; ++i) for (i = 0; i < (int) s->img_x; ++i)
{ {
uint32_t v = rbmp_get32le(s); uint32_t v = (uint32_t)RBMP_GET32LE(s);
out[z++] = RBMP_BYTECAST(rbmp_shiftsigned(v & mr, rshift, rcount)); out[z++] = RBMP_BYTECAST(rbmp_shiftsigned(v & mr, rshift, rcount));
out[z++] = RBMP_BYTECAST(rbmp_shiftsigned(v & mg, gshift, gcount)); out[z++] = RBMP_BYTECAST(rbmp_shiftsigned(v & mg, gshift, gcount));
out[z++] = RBMP_BYTECAST(rbmp_shiftsigned(v & mb, bshift, bcount)); out[z++] = RBMP_BYTECAST(rbmp_shiftsigned(v & mb, bshift, bcount));