001    package com.github.sarxos.webcam.util;
002    
003    import java.awt.Graphics2D;
004    import java.awt.image.BufferedImage;
005    import java.awt.image.DataBuffer;
006    import java.awt.image.DataBufferByte;
007    import java.io.ByteArrayOutputStream;
008    import java.io.IOException;
009    
010    import javax.imageio.ImageIO;
011    
012    import com.github.sarxos.webcam.WebcamException;
013    
014    
015    public class ImageUtils {
016    
017            /**
018             * Graphics Interchange Format.
019             */
020            public static final String FORMAT_GIF = "GIF";
021    
022            /**
023             * Portable Network Graphic format.
024             */
025            public static final String FORMAT_PNG = "PNG";
026    
027            /**
028             * Joint Photographic Experts Group format.
029             */
030            public static final String FORMAT_JPG = "JPG";
031    
032            /**
033             * Bitmap image format.
034             */
035            public static final String FORMAT_BMP = "BMP";
036    
037            /**
038             * Wireless Application Protocol Bitmap image format.
039             */
040            public static final String FORMAT_WBMP = "WBMP";
041    
042            public static BufferedImage premultiple(BufferedImage src) {
043                    BufferedImage pre = new BufferedImage(512, 512, BufferedImage.TYPE_INT_ARGB_PRE);
044                    Graphics2D g2 = pre.createGraphics();
045                    g2.drawImage(src, 0, 0, null);
046                    g2.dispose();
047                    pre.flush();
048                    return pre;
049            }
050    
051            public static BufferedImage unpremultiple(BufferedImage pre) {
052                    BufferedImage src = new BufferedImage(512, 512, BufferedImage.TYPE_INT_ARGB);
053                    Graphics2D g2 = pre.createGraphics();
054                    g2.drawImage(src, 0, 0, null);
055                    g2.dispose();
056                    src.flush();
057                    return src;
058            }
059    
060            /**
061             * Convert {@link BufferedImage} to byte array.
062             * 
063             * @param image the image to be converted
064             * @param format the output image format
065             * @return New array of bytes
066             */
067            public static byte[] toByteArray(BufferedImage image, String format) {
068    
069                    byte[] bytes = null;
070                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
071    
072                    try {
073                            ImageIO.write(image, format, baos);
074                            bytes = baos.toByteArray();
075                    } catch (IOException e) {
076                            throw new WebcamException(e);
077                    } finally {
078                            try {
079                                    baos.close();
080                            } catch (IOException e) {
081                                    throw new WebcamException(e);
082                            }
083                    }
084    
085                    return bytes;
086            }
087    
088            public static byte[] toRawByteArray(BufferedImage image) {
089    
090                    DataBuffer dbuf = image.getRaster().getDataBuffer();
091    
092                    if (dbuf instanceof DataBufferByte) {
093    
094                            return ((DataBufferByte) dbuf).getData();
095    
096                    } else {
097    
098                            int w = image.getWidth();
099                            int h = image.getHeight();
100                            int n = w * h;
101    
102                            byte[] bytes = new byte[n * 3];
103    
104                            int i, x, y, rgb;
105    
106                            for (i = 0; i < n; i++) {
107    
108                                    x = i % w;
109                                    y = i / h;
110    
111                                    rgb = image.getRGB(x, y);
112    
113                                    bytes[i * 3 + 0] = (byte) ((rgb >> 16) & 0xff);
114                                    bytes[i * 3 + 1] = (byte) ((rgb >> 8) & 0xff);
115                                    bytes[i * 3 + 2] = (byte) (rgb & 0xff);
116                            }
117    
118                            return bytes;
119                    }
120            }
121    }