PageRenderTime 31ms CodeModel.GetById 19ms RepoModel.GetById 0ms 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
Possible License(s): LGPL-3.0, GPL-3.0, LGPL-2.1, GPL-2.0, CC-BY-SA-3.0, CC0-1.0, Apache-2.0, BSD-3-Clause
  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. package org.mobicents.protocols.smpp.util;
  23. import java.awt.image.BufferedImage;
  24. import java.nio.ByteBuffer;
  25. import org.mobicents.protocols.smpp.SMPPRuntimeException;
  26. /**
  27. * Utility methods for working with images.
  28. * @version $Id: ImageUtils.java 484 2010-02-08 16:08:50Z orank $
  29. */
  30. public final class ImageUtils {
  31. private ImageUtils() {
  32. }
  33. /**
  34. * Use a {@link BufferedImage} to create black and white bitmap data.
  35. * Bytes are output to the supplied <tt>buffer</tt> where each bit
  36. * represents a single pixel. 0 represents a white pixel, 1 represents
  37. * a black pixel. The leftmost pixel is in the most significant bit
  38. * of the byte..so bit 8 of byte 0 represents the pixel at (0, 0).
  39. * Bit 7 of byte 1 represents the pixel at (9, 0).
  40. * @param image The image to create bitmap data for.
  41. * @param buffer The buffer to output bitmap data to.
  42. * @throws SMPPRuntimeException If there is insufficient space in
  43. * the buffer to contain the bitmap data.
  44. */
  45. public static void imageToBwBitmap(BufferedImage image, ByteBuffer buffer) {
  46. int width = image.getWidth();
  47. int height = image.getHeight();
  48. int bytesWidth = (width / 8) + 1;
  49. if (width % 8 == 0) {
  50. bytesWidth--;
  51. }
  52. if (buffer.remaining() < (bytesWidth * height)) {
  53. throw new SMPPRuntimeException(
  54. "Insufficient space to for bitmap data");
  55. }
  56. int bit = 7;
  57. int currentByte = 0;
  58. for (int y = 0; y < height; y++) {
  59. for (int x = 0; x < width; x++) {
  60. int colour = image.getRGB(x, y);
  61. if ((colour & 0xffffff) != 0xffffff) {
  62. currentByte |= 1 << bit;
  63. }
  64. bit--;
  65. if (bit < 0) {
  66. buffer.put((byte) currentByte);
  67. bit = 7;
  68. currentByte = 0;
  69. }
  70. }
  71. }
  72. }
  73. }