Here you can find the source of encodeRGBAsGrayScale(final byte[] raw, final int width, final int height, final int bitsPerPixel, final OutputStream out)
Parameter | Description |
---|---|
raw | the buffer containing the RGB image data |
width | the width of the image in pixels |
height | the height of the image in pixels |
bitsPerPixel | the number of bits to use per pixel |
out | the OutputStream to write the pixels to |
Parameter | Description |
---|---|
IOException | if an I/O error occurs |
public static void encodeRGBAsGrayScale(final byte[] raw, final int width, final int height, final int bitsPerPixel, final OutputStream out) throws IOException
//package com.java2s; /*//from w w w . ja va2 s. c om * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.io.IOException; import java.io.OutputStream; public class Main { /** * Converts a byte array containing 24 bit RGB image data to a grayscale * image. * * @param raw * the buffer containing the RGB image data * @param width * the width of the image in pixels * @param height * the height of the image in pixels * @param bitsPerPixel * the number of bits to use per pixel * @param out * the OutputStream to write the pixels to * * @throws IOException * if an I/O error occurs */ public static void encodeRGBAsGrayScale(final byte[] raw, final int width, final int height, final int bitsPerPixel, final OutputStream out) throws IOException { final int pixelsPerByte = 8 / bitsPerPixel; int bytewidth = width / pixelsPerByte; if (width % pixelsPerByte != 0) { bytewidth++; } // TODO Rewrite to encode directly from a RenderedImage to avoid // buffering the whole RGB // image in memory final byte[] linedata = new byte[bytewidth]; byte ib; for (int y = 0; y < height; y++) { ib = 0; int i = 3 * y * width; for (int x = 0; x < width; x++, i += 3) { // see http://www.jguru.com/faq/view.jsp?EID=221919 double greyVal = 0.212671d * (raw[i] & 0xff) + 0.715160d * (raw[i + 1] & 0xff) + 0.072169d * (raw[i + 2] & 0xff); switch (bitsPerPixel) { case 1: if (greyVal < 128) { ib |= (byte) (1 << 7 - x % 8); } break; case 4: greyVal /= 16; ib |= (byte) ((byte) greyVal << (1 - x % 2) * 4); break; case 8: ib = (byte) greyVal; break; default: throw new UnsupportedOperationException("Unsupported bits per pixel: " + bitsPerPixel); } if (x % pixelsPerByte == pixelsPerByte - 1 || x + 1 == width) { linedata[x / pixelsPerByte] = ib; ib = 0; } } out.write(linedata); } } }