Debian Codegraph @

src/main/java/org/freehep/util/images/ImageUtilities.java

raw
1 // Copyright 2001-2007, FreeHEP.
2 package org.freehep.util.images;
3
4 import java.awt.Color;
5 import java.awt.Graphics;
6 import java.awt.Graphics2D;
7 import java.awt.Image;
8 import java.awt.geom.AffineTransform;
9 import java.awt.image.BufferedImage;
10 import java.awt.image.ColorModel;
11 import java.awt.image.ImageObserver;
12 import java.awt.image.Raster;
13 import java.awt.image.RenderedImage;
14
15 /**
16 * @author Mark Donszelmann
17 * @version $Id: ImageUtilities.java 10366 2007-01-22 19:16:34Z duns $
18 */
19 public class ImageUtilities {
20
21 private ImageUtilities() {
22 }
23
24 public static RenderedImage createRenderedImage(Image image, ImageObserver observer, Color bkg) {
25 if ((bkg == null) && (image instanceof RenderedImage)) return (RenderedImage)image;
26
27 BufferedImage bufferedImage = new BufferedImage(
28 image.getWidth(observer),
29 image.getHeight(observer),
30 (bkg == null) ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB);
31 Graphics g = bufferedImage.getGraphics();
32 if (bkg == null) {
33 g.drawImage(image, 0, 0, observer);
34 } else {
35 g.drawImage(image, 0, 0, bkg, observer);
36 }
37 return bufferedImage;
38 }
39
40 public static BufferedImage createBufferedImage(RenderedImage image, ImageObserver observer, Color bkg) {
41 if (image instanceof BufferedImage) return (BufferedImage)image;
42 throw new IllegalArgumentException("not supperted " + image.getClass());
43 }
44
45 public static BufferedImage createBufferedImage(Image image, ImageObserver observer, Color bkg) {
46 if ((bkg == null) && (image instanceof BufferedImage)) return (BufferedImage)image;
47 return (BufferedImage) createRenderedImage(image, observer, bkg);
48 }
49
50 public static RenderedImage createRenderedImage(RenderedImage image, Color bkg) {
51 if (bkg == null) return image;
52
53 BufferedImage bufferedImage = new BufferedImage(
54 image.getWidth(),
55 image.getHeight(),
56 BufferedImage.TYPE_INT_RGB);
57 Graphics2D g = (Graphics2D)bufferedImage.getGraphics();
58 g.setBackground(bkg);
59 g.clearRect(0, 0, image.getWidth(), image.getHeight());
60 g.drawRenderedImage(image, new AffineTransform());
61 return bufferedImage;
62 }
63
64 public static byte[] getBytes(Image image, Color bkg, String code, int pad, ImageObserver observer) {
65 return getBytes(createRenderedImage(image, observer, bkg), bkg, code, pad);
66 }
67
68 /**
69 * Returns the bytes of an image.
70 *
71 * @param image to be converted to bytes
72 * @param bkg the color to be used for alpha-multiplication
73 * @param code ARGB, A, or BGR, ... you may also use *ARGB to pre-multiply with alpha
74 * @param pad number of bytes to pad the scanline with (1=byte, 2=short, 4=int, ...)
75 */
76 public static byte[] getBytes(RenderedImage image, Color bkg, String code, int pad) {
77 if (pad < 1) pad = 1;
78
79 Raster raster = image.getData();
80
81 int width = image.getWidth();
82 int height = image.getHeight();
83
84 boolean preMultiply = (code.charAt(0) == '*');
85 if (preMultiply) code = code.substring(1);
86
87 int pixelSize = code.length();
88
89 int size = width*height*pixelSize;
90 size += (width % pad)*height;
91 int index = 0;
92 byte[] bytes = new byte[size];
93
94 ColorModel colorModel = image.getColorModel();
95
96 for (int y=0; y<height; y++) {
97 for (int x=0; x<width; x++) {
98
99 int argb = colorModel.getRGB(raster.getDataElements(x, y, (Object)null));
100 int a = ((argb >> 24) & 0xFF);
101 int r = ((argb >> 16) & 0xFF);
102 int g = ((argb >> 8) & 0xFF);
103 int b = ((argb >> 0) & 0xFF);
104
105 // Check the transparancy. If transparent substitute
106 // the background color.
107 if (preMultiply && (a < 0xFF)) {
108 if (bkg == null) bkg = Color.BLACK;
109 double alpha = a/255.0;
110 r = (int)(alpha*r+(1-alpha)*bkg.getRed());
111 g = (int)(alpha*g+(1-alpha)*bkg.getGreen());
112 b = (int)(alpha*b+(1-alpha)*bkg.getBlue());
113 }
114
115 for (int i=0; i<code.length(); i++) {
116 switch (code.charAt(i)) {
117 case 'a':
118 case 'A':
119 bytes[index] = (byte)a;
120 break;
121
122 case 'r':
123 case 'R':
124 bytes[index] = (byte)r;
125 break;
126
127 case 'g':
128 case 'G':
129 bytes[index] = (byte)g;
130 break;
131
132 case 'b':
133 case 'B':
134 bytes[index] = (byte)b;
135 break;
136
137 default:
138 System.err.println(ImageUtilities.class.getClass()+": Invalid code in '"+code+"'");
139 break;
140 }
141 index++;
142 }
143 }
144 for (int i=0; i<(width % pad); i++) {
145 bytes[index] = 0;
146 index++;
147 }
148 }
149
150 return bytes;
151 }
152 }