Mobile Development 13 min read

Android Image Compression Analysis: Quality Compression and Huffman Algorithm

This article examines Android's quality image compression, detailing Bitmap.compress() behavior, the Skia engine's role, Huffman coding optimization, libjpeg settings, and differences from iOS, while recommending newer libraries and techniques for improved compression efficiency.

Tencent Music Tech Team
Tencent Music Tech Team
Tencent Music Tech Team
Android Image Compression Analysis: Quality Compression and Huffman Algorithm

This article provides a comprehensive analysis of Android image compression techniques, focusing on quality compression and the underlying Huffman algorithm. The content is divided into nine sections covering various aspects of image compression in Android.

The article begins by introducing two main compression methods in Android: quality compression (which changes storage volume without altering dimensions) and downsampling compression (which reduces image dimensions). It explains that quality compression is the focus of this discussion.

The technical implementation of quality compression is explored through code examples showing how Bitmap.compress() works with different formats (JPEG, PNG, and WEBP). The article explains that PNG is lossless and ignores quality parameters, while WEBP offers approximately 30% better compression than JPEG.

The compression process involves multiple layers: Java layer → Native function → Skia engine → Third-party library (like libjpeg). The Skia engine acts as a glue layer connecting various encoding/decoding libraries. The article traces the compression call chain from the Java compress() function through native code to the Skia engine's encodeStream() function.

Practical demonstrations show that quality compression doesn't change bitmap memory size but affects visual quality and file storage size. Code examples demonstrate how setting quality to 50 reduces file size from 106k to 8k while maintaining the same bitmap dimensions in memory.

The article provides an in-depth explanation of the Skia image engine, describing it as Google's C++ image processing engine used across various products. It explains how Skia interfaces with third-party libraries like libpng and libjpeg for encoding/decoding functionality.

A detailed explanation of Huffman coding is provided, comparing it with fixed-length encoding. The article illustrates how Huffman coding assigns shorter codes to frequently occurring elements and longer codes to rare elements, using examples with five values (a, b, c, d, e) to demonstrate the algorithm's efficiency.

The discussion covers libjpeg's optimize_coding parameter, explaining that when set to TRUE, it computes optimal Huffman coding tables through an extra data pass, significantly improving compression at the cost of processing time. The article references community discussions and official Skia team responses about this parameter's impact.

The article compares Android's JPEG compression with iOS, identifying two key factors that affect Android's image quality: (1) color space conversion using a custom function instead of libjpeg's original, sacrificing pixel precision for speed, and (2) using JDCT_IFAST for discrete cosine transform, which is the fastest but least precise method.

Finally, the article summarizes that starting from Android 7.0, optimize_coding is set to TRUE by default, and suggests using libraries like mozilla/mozjpeg or libjpeg-turbo for better compression rates. It also mentions arithmetic coding as an alternative to Huffman coding, potentially offering 5-15% better compression.

The content concludes with references to various sources and discussions that informed the analysis, providing readers with additional resources for deeper understanding.

Mobile developmentAndroidimage compressionHuffman codingJPEGlibjpegOptimize CodingQuality CompressionSkia Engine
Tencent Music Tech Team
Written by

Tencent Music Tech Team

Public account of Tencent Music's development team, focusing on technology sharing and communication.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.