1 package de.desy.acop.video.displayer;
2
3 import java.awt.FontMetrics;
4 import java.awt.Graphics;
5 import java.awt.Insets;
6 import java.awt.Rectangle;
7 import java.awt.image.BufferedImage;
8 import java.awt.image.DataBufferInt;
9 import java.awt.image.IndexColorModel;
10
11 import javax.swing.JComponent;
12
13
14
15
16
17
18
19 public class ColorLookupPanel extends JComponent {
20
21 private static final long serialVersionUID = 31031976L;
22
23
24
25
26 private ImageCLUT clut;
27
28
29
30
31
32
33 public ColorLookupPanel(ImageCLUT clut) {
34 this.clut = clut;
35 }
36
37
38
39
40 public ImageCLUT getCLUT() {
41 return clut;
42 }
43
44
45
46
47
48 public void setCLUT(ImageCLUT clut) {
49 this.clut = clut;
50 }
51
52 @Override
53 public void paintComponent(Graphics g) {
54
55 FontMetrics fm = g.getFontMetrics();
56 Insets insets = new Insets(fm.getHeight() + 2, 8, 4, 8);
57
58 int w = getWidth() - insets.left - insets.right;
59 int h = getHeight() - insets.top - insets.bottom;
60
61 Rectangle rect = new Rectangle(insets.left - 1, insets.top - 1, w + 1, h + 1);
62 g.drawRect(rect.x, rect.y, rect.width, rect.height);
63
64 if (clut != null) {
65 ColorMap cm = clut.getColorMap();
66 if (!cm.equals(ColorMap.NONE)) {
67 int pixelSize = clut.getPixelSize();
68 IndexColorModel icm = cm.getLUT(pixelSize);
69 BufferedImage image = new BufferedImage(icm.getMapSize(), 1, BufferedImage.TYPE_INT_RGB);
70 icm.getRGBs(((DataBufferInt) image.getRaster().getDataBuffer()).getData());
71 g.drawImage(image, insets.left, insets.top, w, h, this);
72
73 int x0, x1 = insets.left - 1;
74 float offset = w / (float) pixelSize;
75 for (int index = 0; index < pixelSize; index++) {
76 x0 = x1;
77 x1 += Math.round(offset);
78 g.drawLine(x0, insets.top - 2, x0, insets.top + h);
79 String s = String.valueOf(index);
80 g.drawString(s, x0 + (x1 - x0 - fm.stringWidth(s)) / 2, 12);
81 }
82 return;
83 }
84 }
85 String s = "no color lookup table";
86 g.drawString(s, (int) (rect.getCenterX() - fm.stringWidth(s) / 2), 12);
87 }
88
89 }