Android Open Source - pixel-art Bitmap Encoder






From Project

Back to project page pixel-art.

License

The source code is released under:

Apache License

If you think the Android project pixel-art listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.jaween.pixelart.util;
/*from  w  w  w  . j a  v  a  2  s .com*/
import android.graphics.Bitmap;
import android.util.Log;

import java.util.ArrayList;

/**
 * Compresses bitmaps
 */
public class BitmapEncoder {

    private static final String LOG_TAG = BitmapEncoder.class.getSimpleName();

    private ArrayList<Integer> encodedBitmapList = new ArrayList<Integer>();
    private int[] pixelArray;

    private int width = 0;
    private int height = 0;

    /**
     * Allocates memory for temporary data structures. Must be set prior to calling an encode or
     * decode function.
     * @param width The width of Bitmaps that will be used
     * @param height The height of Bitmaps that will be used
     */
    public void setBitmapDimensions(int width, int height) {
        // Creates a new pixel array if the incoming dimensions are new
        if (this.width != width || this.height != height) {
            pixelArray = new int[width * height];
            this.width = width;
            this.height = height;
        }
    }

    /**
     * Uses run-length encoding to compress a Bitmap.
     * @param source The Bitmap to compress
     * @return An array in pairs of 'run colour' followed by 'run count'
     */
    public Integer[] encodeRunLength(Bitmap source) {
        // Loads the pixels from the bitmap into pixelArray
        source.getPixels(pixelArray, 0, source.getWidth(), 0, 0, source.getWidth(), source.getHeight());
        return encodeRunLength(pixelArray);
    }

    /**
     * Uses run-length encoding to compress an array.
     * @param pixels The array of pixels to compress
     * @return An array in pairs of 'run colour' followed by 'run count'
     */
    public Integer[] encodeRunLength(int[] pixels) {

        encodedBitmapList.clear();

        // Initial run
        int currentRunColour = pixels[0];
        int currentRunCount = 0;

        // Iterates over all the pixels in the bitmap
        int y = 0;
        while (y < height) {
            int x = 0;
            while (x < width) {
                int currentPixelColour = pixels[x + y * width];
                if (currentPixelColour == currentRunColour) {
                    // Continues current run
                    currentRunCount += 1;
                } else {
                    // New run, adds previous run to list
                    encodedBitmapList.add(currentRunColour);
                    encodedBitmapList.add(currentRunCount);

                    // Begins new run
                    currentRunColour = currentPixelColour;
                    currentRunCount = 1;
                }
                x++;
            }
            y++;
        }

        // Adds the final run (was not added because the image ended)
        encodedBitmapList.add(currentRunColour);
        encodedBitmapList.add(currentRunCount);

        // Converts the ArrayList to an array
        Integer[] encodedBitmapArray = new Integer[encodedBitmapList.size()];
        encodedBitmapList.toArray(encodedBitmapArray);

        return encodedBitmapArray;
    }

    /**
     * Decodes a run-length encoded Bitmap.
     * @param encodedBitmap The array to decompress, in pairs of 'run colour' followed by 'run count'
     * @param destination The Bitmap in which to store the decompressed data
     */
    public void decodeRunLength(Integer[] encodedBitmap, Bitmap destination) {
        decodeRunLength(encodedBitmap, pixelArray);
        destination.setPixels(pixelArray, 0, width, 0, 0, width, height);
    }

    /**
     * Decodes a run-length encoded Bitmap.
     * @param encodedBitmap The array to decompress, in pairs of 'run colour' followed by 'run count'
     * @param destination The array in which to store the decompressed data
     */
    public void decodeRunLength(Integer[] encodedBitmap, int[] destination) {
        if (encodedBitmap == null || destination == null) {
            if (encodedBitmap == null) {
                Log.e(LOG_TAG, "Decoding error: Encoded bitmap was null");
            }
            if (destination == null) {
                Log.e(LOG_TAG, "Decoding error: Encoded bitmap was null");
            }
            return;
        }

        if (encodedBitmap.length <= 0) {
            Log.e(LOG_TAG, "Decoding error: Encoded bitmap length was " + encodedBitmap.length);
            return;
        }

        int x = 0;
        int y = 0;

        // Iterates over pairs of run colours followed by run counts
        for (int i = 0; i < encodedBitmap.length; i += 2) {
            int currentRunColour = encodedBitmap[i];
            int currentRunCount = encodedBitmap[i + 1];

            while (currentRunCount > 0) {
                destination[x + y * height] = currentRunColour;
                currentRunCount--;

                // Wraps x when it reaches the edge of the image
                x++;
                if (x >= width) {
                    x = 0;
                    y++;
                }
            }
        }
    }
}




Java Source Code List

com.jaween.pixelart.ContainerActivity.java
com.jaween.pixelart.ContainerFragment.java
com.jaween.pixelart.PanelManagerFragment.java
com.jaween.pixelart.io.AnimationFile.java
com.jaween.pixelart.io.FileAdapter.java
com.jaween.pixelart.io.ImportExport.java
com.jaween.pixelart.io.LoadFileDialog.java
com.jaween.pixelart.tools.Command.java
com.jaween.pixelart.tools.Dropper.java
com.jaween.pixelart.tools.Eraser.java
com.jaween.pixelart.tools.FloodFill.java
com.jaween.pixelart.tools.FreeSelect.java
com.jaween.pixelart.tools.MagicWand.java
com.jaween.pixelart.tools.Oval.java
com.jaween.pixelart.tools.Pen.java
com.jaween.pixelart.tools.RectSelect.java
com.jaween.pixelart.tools.Rect.java
com.jaween.pixelart.tools.Selection.java
com.jaween.pixelart.tools.ToolReport.java
com.jaween.pixelart.tools.Tool.java
com.jaween.pixelart.tools.attributes.EraserToolAttributes.java
com.jaween.pixelart.tools.attributes.MagicWandToolAttributes.java
com.jaween.pixelart.tools.attributes.OvalToolAttributes.java
com.jaween.pixelart.tools.attributes.PenToolAttributes.java
com.jaween.pixelart.tools.attributes.RectToolAttributes.java
com.jaween.pixelart.tools.attributes.ToolAttributes.java
com.jaween.pixelart.tools.options.EraserOptionsView.java
com.jaween.pixelart.tools.options.MagicWandOptionsView.java
com.jaween.pixelart.tools.options.OvalOptionsView.java
com.jaween.pixelart.tools.options.PenOptionsView.java
com.jaween.pixelart.tools.options.RectOptionsView.java
com.jaween.pixelart.tools.options.ToolOptionsView.java
com.jaween.pixelart.ui.ColourButton.java
com.jaween.pixelart.ui.ColourSelector.java
com.jaween.pixelart.ui.DrawingFragment.java
com.jaween.pixelart.ui.DrawingSurface.java
com.jaween.pixelart.ui.PaletteFragment.java
com.jaween.pixelart.ui.PanelFragment.java
com.jaween.pixelart.ui.PixelGrid.java
com.jaween.pixelart.ui.Thumbnail.java
com.jaween.pixelart.ui.ToolButton.java
com.jaween.pixelart.ui.ToolboxFragment.java
com.jaween.pixelart.ui.TransparencyCheckerboard.java
com.jaween.pixelart.ui.animation.AnimationFragment.java
com.jaween.pixelart.ui.animation.FrameAdapter.java
com.jaween.pixelart.ui.animation.Frame.java
com.jaween.pixelart.ui.colourpicker.ColourPickerDialog.java
com.jaween.pixelart.ui.colourpicker.ColourPickerFragment.java
com.jaween.pixelart.ui.colourpicker.ColourPickerView.java
com.jaween.pixelart.ui.layer.LayerAdapter.java
com.jaween.pixelart.ui.layer.LayerFragment.java
com.jaween.pixelart.ui.layer.Layer.java
com.jaween.pixelart.ui.undo.DrawOpManager.java
com.jaween.pixelart.ui.undo.DrawOpUndoData.java
com.jaween.pixelart.ui.undo.FrameUndoData.java
com.jaween.pixelart.ui.undo.LayerUndoData.java
com.jaween.pixelart.ui.undo.UndoItem.java
com.jaween.pixelart.ui.undo.UndoManager.java
com.jaween.pixelart.util.AbsVerticalSeekBar.java
com.jaween.pixelart.util.AnimatedGifEncoder.java
com.jaween.pixelart.util.AutoSaver.java
com.jaween.pixelart.util.BitmapEncoder.java
com.jaween.pixelart.util.Color.java
com.jaween.pixelart.util.ConfigChangeFragment.java
com.jaween.pixelart.util.Debug.java
com.jaween.pixelart.util.MarchingAnts.java
com.jaween.pixelart.util.PreferenceManager.java
com.jaween.pixelart.util.ScaleListener.java
com.jaween.pixelart.util.SlideAnimator.java
com.jaween.pixelart.util.SlidingLinearLayout.java
com.jaween.pixelart.util.VerticalProgressBar.java
com.tokaracamara.android.verticalslidevar.VerticalSeekBar.java