Skip to content

Commit 1f653cd

Browse files
committed
refactor indexed color decoding
reduced decode palette to 8 bits to improve compatibility with libpng optimizations moved 8 -> 16 bit upscaling to the decode loop, gamma correction is now a separate pass
1 parent b1afa4f commit 1f653cd

1 file changed

Lines changed: 13 additions & 24 deletions

File tree

spng/spng.c

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,6 @@ struct spng_subimage
116116
size_t scanline_width;
117117
};
118118

119-
struct spng_plte_entry16
120-
{
121-
uint16_t red;
122-
uint16_t green;
123-
uint16_t blue;
124-
uint16_t alpha;
125-
};
126-
127119
struct spng_text2
128120
{
129121
int type;
@@ -294,7 +286,7 @@ struct spng_ctx
294286
uint16_t *gamma_lut16;
295287
uint16_t gamma_lut8[256];
296288
unsigned char trns_px[8];
297-
struct spng_plte_entry16 decode_plte[256];
289+
struct spng_plte_entry decode_plte[256];
298290
struct spng_sbit decode_sb;
299291
struct decode_flags decode_flags;
300292
struct spng_row_info row_info;
@@ -1302,7 +1294,7 @@ static inline void scale_row(unsigned char *row, uint32_t pixels, int fmt, unsig
13021294
}
13031295

13041296
/* Expand to *row using 8-bit palette indices from *scanline */
1305-
void expand_row(unsigned char *row, const unsigned char *scanline, const struct spng_plte_entry16 *plte, uint32_t width, int fmt)
1297+
void expand_row(unsigned char *row, const unsigned char *scanline, const struct spng_plte_entry *plte, uint32_t width, int fmt)
13061298
{
13071299
uint32_t i;
13081300
unsigned char *px;
@@ -2635,7 +2627,7 @@ int spng_decode_scanline(spng_ctx *ctx, void *out, size_t len)
26352627
const uint16_t *gamma_lut = ctx->gamma_lut;
26362628
unsigned char *trns_px = ctx->trns_px;
26372629
const struct spng_sbit *sb = &ctx->decode_sb;
2638-
const struct spng_plte_entry16 *plte = ctx->decode_plte;
2630+
const struct spng_plte_entry *plte = ctx->decode_plte;
26392631
struct spng__iter iter = (ihdr->bit_depth < 16) ? spng__iter_init(ihdr->bit_depth, ctx->scanline) : (struct spng__iter){0};
26402632

26412633
const unsigned char *scanline;
@@ -2746,6 +2738,11 @@ int spng_decode_scanline(spng_ctx *ctx, void *out, size_t len)
27462738
b_16 = plte[entry].blue;
27472739
a_16 = plte[entry].alpha;
27482740

2741+
r_16 = (r_16 << 8) | r_16;
2742+
g_16 = (g_16 << 8) | g_16;
2743+
b_16 = (b_16 << 8) | b_16;
2744+
a_16 = (a_16 << 8) | a_16;
2745+
27492746
memcpy(pixel, &r_16, 2);
27502747
memcpy(pixel + 2, &g_16, 2);
27512748
memcpy(pixel + 4, &b_16, 2);
@@ -3185,7 +3182,7 @@ int spng_decode_image(spng_ctx *ctx, void *out, size_t len, int fmt, int flags)
31853182
sb->alpha_bits == processing_depth &&
31863183
processing_depth == depth_target) f.do_scaling = 0;
31873184

3188-
struct spng_plte_entry16 *plte = ctx->decode_plte;
3185+
struct spng_plte_entry *plte = ctx->decode_plte;
31893186

31903187
/* Pre-process palette entries */
31913188
if(f.indexed)
@@ -3198,21 +3195,13 @@ int spng_decode_image(spng_ctx *ctx, void *out, size_t len, int fmt, int flags)
31983195
else
31993196
ctx->plte.entries[i].alpha = 255;
32003197

3201-
plte[i].red = sample_to_target(ctx->plte.entries[i].red, 8, sb->red_bits, depth_target);
3202-
plte[i].green = sample_to_target(ctx->plte.entries[i].green, 8, sb->green_bits, depth_target);
3203-
plte[i].blue = sample_to_target(ctx->plte.entries[i].blue, 8, sb->blue_bits, depth_target);
3204-
plte[i].alpha = sample_to_target(ctx->plte.entries[i].alpha, 8, sb->alpha_bits, depth_target);
3205-
3206-
if(f.apply_gamma)
3207-
{
3208-
plte[i].red = gamma_lut[plte[i].red];
3209-
plte[i].green = gamma_lut[plte[i].green];
3210-
plte[i].blue = gamma_lut[plte[i].blue];
3211-
}
3198+
plte[i].red = sample_to_target(ctx->plte.entries[i].red, 8, sb->red_bits, 8);
3199+
plte[i].green = sample_to_target(ctx->plte.entries[i].green, 8, sb->green_bits, 8);
3200+
plte[i].blue = sample_to_target(ctx->plte.entries[i].blue, 8, sb->blue_bits, 8);
3201+
plte[i].alpha = sample_to_target(ctx->plte.entries[i].alpha, 8, sb->alpha_bits, 8);
32123202
}
32133203

32143204
f.apply_trns = 0;
3215-
f.apply_gamma = 0;
32163205
}
32173206

32183207
unsigned char *trns_px = ctx->trns_px;

0 commit comments

Comments
 (0)