Skip to content

Commit 1e46b19

Browse files
JPEG: Around ~15x faster to decode
1 parent c8f121c commit 1e46b19

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

MCGalaxy/util/Imaging/JpegDecoder.cs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public override SimpleBitmap Decode(byte[] src) {
4343
SetBuffer(src);
4444
SimpleBitmap bmp = new SimpleBitmap();
4545

46+
ComputeIDCTFactors();
4647
ReadMarkers(src, bmp);
4748
return bmp;
4849
}
@@ -398,31 +399,43 @@ void DecodeBlock(JpegComponent comp, byte[] src, int[] block, float[] output) {
398399
IDCT(block, output);
399400
}
400401

402+
float[] idct_factors;
403+
void ComputeIDCTFactors() {
404+
float[] factors = new float[128];
405+
406+
for (int xy = 0; xy < 8; xy++)
407+
{
408+
for (int uv = 0; uv < 8; uv++)
409+
{
410+
float cuv = uv == 0 ? 0.70710678f : 1.0f;
411+
float cosuv = (float)Math.Cos((2 * xy + 1) * uv * Math.PI / 16.0);
412+
413+
factors[(2 * xy + 1) * uv] = cuv * cosuv;
414+
}
415+
}
416+
idct_factors = factors;
417+
}
418+
401419
void IDCT(int[] block, float[] output) {
420+
float[] factors = idct_factors;
421+
402422
for (int y = 0; y < 8; y++)
403423
for (int x = 0; x < 8; x++)
404424
{
405425
float sum = 0.0f;
406426
for (int v = 0; v < 8; v++)
407427
for (int u = 0; u < 8; u++)
408428
{
409-
float cu = u == 0 ? 0.70710678f : 1.0f;
410-
float cv = v == 0 ? 0.70710678f : 1.0f;
411-
412429
float suv = block[v*8+u];
413-
float cosu = (float)Math.Cos((2 * x + 1) * u * Math.PI / 16.0);
414-
float cosv = (float)Math.Cos((2 * y + 1) * v * Math.PI / 16.0);
430+
float cu_cosu = factors[(2 * x + 1) * u];
431+
float cv_cosv = factors[(2 * y + 1) * v];
415432

416-
sum += cu * cv * suv * cosu * cosv;
433+
sum += cu_cosu * cv_cosv * suv;
417434
}
418435
output[y*8+x] = (sum / 4.0f) + 128.0f; // undo level shift at end
419436
}
420437
}
421438

422-
void OutputImage(SimpleBitmap bmp) {
423-
424-
}
425-
426439
uint bit_buf;
427440
int bit_cnt;
428441
bool end;

0 commit comments

Comments
 (0)