3

I have one image which was saved into two files: png and jpeg. Then I load them using default colour depth( Bitmap.Config.ARGB_8888)

Allocation tracker shows that both images consumed 1904016 bytes. Ok, looks fine. But then I added Bitmap.Config.RGB_565 and jpg image consumed 952016 bytes but png image still use 1904015. Why so?

public class MainActivity extends Activity {

    private BitmapDrawable png;
    private BitmapDrawable jpg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.btnDecode8888).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                decode(null);
            }
        });

        findViewById(R.id.btnDecode565).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inPreferredConfig = Bitmap.Config.RGB_565;
                decode(options);
            }
        });
    }

    private void decode(BitmapFactory.Options options) {
        png = decodeFile("/mnt/sdcard/img.png", options);
        jpg = decodeFile("/mnt/sdcard/img.jpg", options);
    }

    private BitmapDrawable decodeFile(String path, BitmapFactory.Options options){
        BitmapDrawable result = null;
        try {
            FileInputStream file = new FileInputStream(path);
            Bitmap bitmap = BitmapFactory.decodeStream(file, null, options);
            result = new BitmapDrawable(getResources(), bitmap);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        return result;
    }
}
eleven
  • 6,779
  • 2
  • 32
  • 52

2 Answers2

1

What you are describing has nothing to do with the relative merits of PNG and JPEG. It is an underlying implementation issue.

A plain vanilla JPEG (JFIF) has 24 bits per pixel. When decompressed (ignoring YCBCR and sampling), The R, G, and B components end up with 8 bits each.

PNG also does has 8-bit (ignoring higher) color depths and multiple representation method for those colors. You are trying to down sample, converting 8-r, 8-g, 8-b (24 bits per pixel) into 5-r, 8-g, 5-b (16bits).

This conversion can be done just as easily from a PNG image as from a JPEG image. If you see a difference on your system, it is your system software. It sounds like it is not converting 24bpp (Actually 32 because of the alpha channel) to 16bpp for png, even though this is entirely possible.

user3344003
  • 20,574
  • 3
  • 26
  • 62
-1

The best way to explain this is with an existing answer to a similar question:

PNG vs. GIF vs. JPEG vs. SVG - When best to use?

He explains it much better than I could ;)

Community
  • 1
  • 1
apmartin1991
  • 3,064
  • 1
  • 23
  • 44
  • what does it explain? – Blackbelt Mar 17 '14 at 15:36
  • It explains that generally speaking, png (loss-less, compressed) is heavier that jpeg (lossy, compressed). Still, I wonder why they have the same weight with ARGB_8888. So, while it seems relevent information to me, it does not fully answer the question. – Tonio Mar 17 '14 at 15:44