Home > Tutorials > Image Processing – Decreasing Color Depth

Image Processing – Decreasing Color Depth


Decreasing color depth involves converting to standard values.

For example: if I want to offset 32, then each image color channel will apply the range: 0, 31, 63 …

From the original image:

Original Image

Original Image

Some bit offset taken for this image:

32-bit Offset

32-bit Offset

64-bit Offset

64-bit Offset

128-bit Offset

128-bit Offset

Here the implementation:

	public static Bitmap decreaseColorDepth(Bitmap src, int bitOffset) {
		// get image size
		int width = src.getWidth();
		int height = src.getHeight();
		// create output bitmap
		Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
		// color information
		int A, R, G, B;
		int pixel;

		// scan through all pixels
		for(int x = 0; x < width; ++x) {
			for(int y = 0; y < height; ++y) {
				// get pixel color
				pixel = src.getPixel(x, y);
				A = Color.alpha(pixel);
				R = Color.red(pixel);
				G = Color.green(pixel);
				B = Color.blue(pixel);

				// round-off color offset
				R = ((R + (bitOffset / 2)) - ((R + (bitOffset / 2)) % bitOffset) - 1);
				if(R < 0) { R = 0; }
				G = ((G + (bitOffset / 2)) - ((G + (bitOffset / 2)) % bitOffset) - 1);
				if(G < 0) { G = 0; }
				B = ((B + (bitOffset / 2)) - ((B + (bitOffset / 2)) % bitOffset) - 1);
				if(B < 0) { B = 0; }

				// set pixel color to output bitmap
				bmOut.setPixel(x, y, Color.argb(A, R, G, B));
			}
		}

		// return final image
		return bmOut;
	}

Hope you like it!

Cheers,
Pete Houston

Advertisements
  1. tipa.bot
    December 25, 2012 at 12:12 am

    can you help
    I want to get a 4 – bit image
    I changed the parameter bitOffset to a larger but did not achieve a positive result

  2. Ma
    August 21, 2012 at 10:39 pm

    ThanX

  3. July 3, 2012 at 3:41 pm

    Great work…

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: