Free space optimization: discard PNG

    Imagine a quite ordinary, routine task: our application should load external images, they should have a transparent background, and then we will already perform some actions on them.

    Often we say without any hesitation that it should be only png, after which the designers prepare thousands of content files for your application that dramatically fill the free space on the servers. But what if they set the task to optimize the use of disk space?

    It turns out that the technology quite successfully used by many applications is to solve this problem without the png format.

    All we need to do is generate 2 gif files, one of which will store the image, and the second a mask of a transparent background.

    The size of two gif files is significantly smaller than the size of one full png.

    So in the example below, the first gif file with the image takes up only 9 kb, and the mask for it is 3 kb.

    The image itself
    image

    The mask itself


    While the png - file with a transparent background for the same picture will occupy about 35 kilobytes. Save 65%.

    You can get the third with two opaque pictures with a transparent background using only one of the built-in BitmapData.threshold methods: replace the pixel of the image where the corresponding pixel of the mask is less than white, with a transparent pixel

    gifImageBitmapData.threshold(gifMask.bitmapData, gifMask.getRect(gifMask),new Point(0,0),'<',0xffffffff,0x00FFFFFF,0xffffffff);

    Bottom line: if you do not need to transmit a semi-transparent gradient, but only need to trim the background from the pictures, and in addition, the quality of the picture does not play a paramount role, then you can use the above-described technology. As you can see, the amount of our code for image processing will not increase significantly.

    The technology, of course, is not universal and has its drawbacks - 2 small files will probably load longer than 1 large file, but it does its job: the content for your application will take much less.

    Also popular now: