PageRenderTime 36ms CodeModel.GetById 19ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/protocols/smpp/src/main/java/org/mobicents/protocols/smpp/util/ImageUtils.java

http://mobicents.googlecode.com/
Java | 79 lines | 36 code | 6 blank | 37 comment | 8 complexity | 8cc50ac4a4618d2acc20b99fca21e012 MD5 | raw file
 1/*
 2 * JBoss, Home of Professional Open Source
 3 * Copyright 2011, Red Hat, Inc. and individual contributors
 4 * by the @authors tag. See the copyright.txt in the distribution for a
 5 * full listing of individual contributors.
 6 *
 7 * This is free software; you can redistribute it and/or modify it
 8 * under the terms of the GNU Lesser General Public License as
 9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */
22
23package org.mobicents.protocols.smpp.util;
24
25import java.awt.image.BufferedImage;
26import java.nio.ByteBuffer;
27
28import org.mobicents.protocols.smpp.SMPPRuntimeException;
29
30/**
31 * Utility methods for working with images.
32 * @version $Id: ImageUtils.java 484 2010-02-08 16:08:50Z orank $
33 */
34public final class ImageUtils {
35
36    private ImageUtils() {
37    }
38    
39    /**
40     * Use a {@link BufferedImage} to create black and white bitmap data.
41     * Bytes are output to the supplied <tt>buffer</tt> where each bit
42     * represents a single pixel. 0 represents a white pixel, 1 represents
43     * a black pixel. The leftmost pixel is in the most significant bit
44     * of the byte..so bit 8 of byte 0 represents the pixel at (0, 0).
45     * Bit 7 of byte 1 represents the pixel at (9, 0).
46     * @param image The image to create bitmap data for.
47     * @param buffer The buffer to output bitmap data to.
48     * @throws SMPPRuntimeException If there is insufficient space in
49     * the buffer to contain the bitmap data. 
50     */
51    public static void imageToBwBitmap(BufferedImage image, ByteBuffer buffer) {
52        int width = image.getWidth();
53        int height = image.getHeight();
54        int bytesWidth = (width / 8) + 1;
55        if (width % 8 == 0) {
56            bytesWidth--;
57        }
58        if (buffer.remaining() < (bytesWidth * height)) {
59            throw new SMPPRuntimeException(
60                    "Insufficient space to for bitmap data");
61        }
62        int bit = 7;
63        int currentByte = 0;
64        for (int y = 0; y < height; y++) {
65            for (int x = 0; x < width; x++) {
66                int colour = image.getRGB(x, y);
67                if ((colour & 0xffffff) != 0xffffff) {
68                    currentByte |= 1 << bit;
69                }
70                bit--;
71                if (bit < 0) {
72                    buffer.put((byte) currentByte);
73                    bit = 7;
74                    currentByte = 0;
75                }
76            }
77        }
78    }
79}