00001 package commandline.ImageTools;
00002
00003
00004
00005 import java.awt.Dimension;
00006 import java.awt.Graphics;
00007 import java.awt.Graphics2D;
00008 import java.awt.Insets;
00009 import java.awt.Point;
00010 import java.awt.Rectangle;
00011 import java.awt.event.MouseEvent;
00012 import java.awt.event.MouseListener;
00013 import java.awt.event.MouseMotionListener;
00014 import java.awt.geom.AffineTransform;
00015 import java.awt.image.AffineTransformOp;
00016 import java.awt.image.BufferedImage;
00017 import java.awt.image.BufferedImageOp;
00018 import java.awt.image.ByteLookupTable;
00019 import java.awt.image.ColorModel;
00020 import java.awt.image.DataBuffer;
00021 import java.awt.image.LookupOp;
00022 import java.awt.image.Raster;
00023 import java.awt.image.SampleModel;
00024 import java.awt.image.WritableRaster;
00025
00026 import javax.media.jai.PlanarImage;
00027 import javax.media.jai.RasterFactory;
00028 import javax.swing.JComponent;
00029 import javax.swing.JLabel;
00030 import javax.swing.SwingConstants;
00031
00046 public class ImageDisplay extends JComponent
00047 implements MouseListener, MouseMotionListener {
00048
00050 protected PlanarImage source;
00052 protected SampleModel sampleModel;
00054 protected ColorModel colorModel;
00055
00057 protected int minTileX;
00059 protected int maxTileX;
00061 protected int minTileY;
00063 protected int maxTileY;
00065 protected int tileWidth;
00067 protected int tileHeight;
00069 protected int tileGridXOffset;
00071 protected int tileGridYOffset;
00072
00073 protected int originX = 0;
00074 protected int originY = 0;
00075
00076 protected int shift_x = 0;
00077 protected int shift_y = 0;
00078
00079 protected JLabel odometer = null;
00080 protected int componentWidth;
00081 protected int componentHeight;
00082
00084 protected BufferedImageOp biop = null;
00085 protected boolean brightnessEnabled = false;
00086 protected int brightness = 0;
00087 protected byte[] lutData;
00088
00090 private synchronized void initialize() {
00091 if ( source == null ) return;
00092
00093 componentWidth = source.getWidth();
00094 componentHeight = source.getHeight();
00095
00096 setPreferredSize(new Dimension(componentWidth, componentHeight));
00097
00098 this.sampleModel = source.getSampleModel();
00099
00100
00101 this.colorModel = source.getColorModel();
00102 if (this.colorModel == null) {
00103
00104 this.colorModel = PlanarImage.createColorModel(this.sampleModel);
00105 if (this.colorModel == null) {
00106 throw new IllegalArgumentException("no color model");
00107 }
00108 }
00109
00110 minTileX = source.getMinTileX();
00111 maxTileX = source.getMinTileX() + source.getNumXTiles() - 1;
00112 minTileY = source.getMinTileY();
00113 maxTileY = source.getMinTileY() + source.getNumYTiles() - 1;
00114 tileWidth = source.getTileWidth();
00115 tileHeight = source.getTileHeight();
00116 tileGridXOffset = source.getTileGridXOffset();
00117 tileGridYOffset = source.getTileGridYOffset();
00118 }
00119
00123 public ImageDisplay() {
00124 super();
00125 source = null;
00126
00127 lutData = new byte[256];
00128
00129 for ( int i = 0; i < 256; i++ ) {
00130 lutData[i] = (byte)i;
00131 }
00132
00133 componentWidth = 64;
00134 componentHeight = 64;
00135 setPreferredSize(new Dimension(componentWidth, componentHeight));
00136 setOrigin(0, 0);
00137 }
00138
00144 public ImageDisplay(PlanarImage im) {
00145 super();
00146 source = im;
00147 initialize();
00148
00149 lutData = new byte[256];
00150
00151 for ( int i = 0; i < 256; i++ ) {
00152 lutData[i] = (byte)i;
00153 }
00154
00155 setOrigin(0, 0);
00156 }
00157
00164 public ImageDisplay(int width, int height) {
00165 super();
00166 source = null;
00167
00168 lutData = new byte[256];
00169
00170 for ( int i = 0; i < 256; i++ ) {
00171 lutData[i] = (byte)i;
00172 }
00173
00174 componentWidth = width;
00175 componentHeight = height;
00176 setPreferredSize(new Dimension(componentWidth, componentHeight));
00177 setOrigin(0, 0);
00178 }
00179
00181 public void set(PlanarImage im) {
00182 source = im;
00183 initialize();
00184
00185 repaint();
00186 }
00187
00188 public void set(PlanarImage im, int x, int y) {
00189 source = im;
00190 initialize();
00191 setOrigin(x, y);
00192 }
00193
00194 public PlanarImage getImage() {
00195 return source;
00196 }
00197
00198 public final JLabel getOdometer() {
00199 if ( odometer == null ) {
00200 odometer = new JLabel();
00201 odometer.setVerticalAlignment(SwingConstants.CENTER);
00202 odometer.setHorizontalAlignment(SwingConstants.LEFT);
00203 odometer.setText(" ");
00204 addMouseListener(this);
00205 addMouseMotionListener(this);
00206 }
00207
00208 return odometer;
00209 }
00210
00212 public final void setOrigin(int x, int y) {
00213
00214 originX = -x;
00215 originY = -y;
00216 repaint();
00217 }
00218
00219 public int getXOrigin() {
00220 return originX;
00221 }
00222
00223 public int getYOrigin() {
00224 return originY;
00225 }
00226
00228 public void setBounds(int x, int y, int width, int height) {
00229 Insets insets = getInsets();
00230 int w;
00231 int h;
00232
00233 if ( source == null ) {
00234 w = width;
00235 h = height;
00236 } else {
00237 w = source.getWidth();
00238 h = source.getHeight();
00239
00240 if ( width < w ) {
00241 w = width;
00242 }
00243
00244 if ( height < h ) {
00245 h = height;
00246 }
00247 }
00248
00249 componentWidth = w + insets.left + insets.right;
00250 componentHeight = h + insets.top + insets.bottom;
00251
00252 super.setBounds(x+shift_x, y+shift_y, componentWidth, componentHeight);
00253 }
00254
00255 public void setLocation(int x, int y) {
00256 shift_x = x;
00257 shift_y = y;
00258 super.setLocation(x, y);
00259 }
00260
00261 private final int XtoTileX(int x) {
00262 return (int) Math.floor((double) (x - tileGridXOffset)/tileWidth);
00263 }
00264
00265 private final int YtoTileY(int y) {
00266 return (int) Math.floor((double) (y - tileGridYOffset)/tileHeight);
00267 }
00268
00269 private final int TileXtoX(int tx) {
00270 return tx*tileWidth + tileGridXOffset;
00271 }
00272
00273 private final int TileYtoY(int ty) {
00274 return ty*tileHeight + tileGridYOffset;
00275 }
00276
00277
00278
00279 private final byte clampByte(int v) {
00280 if ( v > 255 ) {
00281 return (byte)255;
00282 } else if ( v < 0 ) {
00283 return (byte)0;
00284 } else {
00285 return (byte)v;
00286 }
00287 }
00288
00289 public final void setBrightnessEnabled(boolean v) {
00290 brightnessEnabled = v;
00291
00292 if ( brightnessEnabled == true ) {
00293 biop = new AffineTransformOp(new AffineTransform(),
00294 AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
00295 } else {
00296 biop = null;
00297 }
00298 }
00299
00300 public final boolean getBrightnessEnabled() {
00301 return brightnessEnabled;
00302 }
00303
00304 public final void setBrightness(int b) {
00305 if ( b != brightness && brightnessEnabled == true ) {
00306 for ( int i = 0; i < 256; i++ ) {
00307 lutData[i] = clampByte(i+b);
00308 }
00309
00310 repaint();
00311 }
00312 }
00313
00320 public synchronized void paintComponent(Graphics g) {
00321
00322 Graphics2D g2D = null;
00323 if (g instanceof Graphics2D) {
00324 g2D = (Graphics2D)g;
00325 } else {
00326 return;
00327 }
00328
00329
00330 if ( source == null ) {
00331 g2D.setColor(getBackground());
00332 g2D.fillRect(0, 0, componentWidth, componentHeight);
00333 return;
00334 }
00335
00336 int transX = -originX;
00337 int transY = -originY;
00338
00339
00340 Rectangle clipBounds = g.getClipBounds();
00341
00342 if (clipBounds == null) {
00343 clipBounds = new Rectangle(0, 0, componentWidth, componentHeight);
00344 }
00345
00346
00347 if ( transX > 0 ||
00348 transY > 0 ||
00349 transX < (componentWidth-source.getWidth()) ||
00350 transY < (componentHeight-source.getHeight())) {
00351 g2D.setColor(getBackground());
00352 g2D.fillRect(0, 0, componentWidth, componentHeight);
00353 }
00354
00355 clipBounds.translate(-transX, -transY);
00356
00357
00358 int txmin, txmax, tymin, tymax;
00359 int ti, tj;
00360
00361 txmin = XtoTileX(clipBounds.x);
00362 txmin = Math.max(txmin, minTileX);
00363 txmin = Math.min(txmin, maxTileX);
00364
00365 txmax = XtoTileX(clipBounds.x + clipBounds.width - 1);
00366 txmax = Math.max(txmax, minTileX);
00367 txmax = Math.min(txmax, maxTileX);
00368
00369 tymin = YtoTileY(clipBounds.y);
00370 tymin = Math.max(tymin, minTileY);
00371 tymin = Math.min(tymin, maxTileY);
00372
00373 tymax = YtoTileY(clipBounds.y + clipBounds.height - 1);
00374 tymax = Math.max(tymax, minTileY);
00375 tymax = Math.min(tymax, maxTileY);
00376 Insets insets = getInsets();
00377
00378
00379 for (tj = tymin; tj <= tymax; tj++) {
00380 for (ti = txmin; ti <= txmax; ti++) {
00381 int tx = TileXtoX(ti);
00382 int ty = TileYtoY(tj);
00383
00384 Raster tile = source.getTile(ti, tj);
00385 if ( tile != null ) {
00386 DataBuffer dataBuffer = tile.getDataBuffer();
00387
00388 WritableRaster wr = tile.createWritableRaster(sampleModel,
00389 dataBuffer,
00390 null);
00391
00392 BufferedImage bi = new BufferedImage(colorModel,
00393 wr,
00394 colorModel.isAlphaPremultiplied(),
00395 null);
00396
00397
00398 if ( brightnessEnabled == true ) {
00399 SampleModel sm = sampleModel.createCompatibleSampleModel(tile.getWidth(),
00400 tile.getHeight());
00401
00402 WritableRaster raster = RasterFactory.createWritableRaster(sm, null);
00403
00404 BufferedImage bimg = new BufferedImage(colorModel,
00405 raster,
00406 colorModel.isAlphaPremultiplied(),
00407 null);
00408
00409
00410 ByteLookupTable lutTable = new ByteLookupTable(0, lutData);
00411 LookupOp lookup = new LookupOp(lutTable, null);
00412 lookup.filter(bi, bimg);
00413
00414 g2D.drawImage(bimg, biop, tx+transX+insets.left, ty+transY+insets.top);
00415 } else {
00416 AffineTransform transform;
00417
00418 transform = AffineTransform.getTranslateInstance(tx + transX + insets.left,
00419 ty + transY + insets.top);
00420
00421 g2D.drawRenderedImage(bi, transform);
00422 }
00423 }
00424 }
00425 }
00426 }
00427
00428
00429 public final void mouseEntered(MouseEvent e) {
00430 }
00431
00432 public final void mouseExited(MouseEvent e) {
00433 }
00434
00435 public void mousePressed(MouseEvent e) {
00436 Point p = e.getPoint();
00437 if ( odometer != null ) {
00438 String output = " (" + p.x + ", " + p.y + ")";
00439 odometer.setText(output);
00440 }
00441 }
00442
00443 public final void mouseReleased(MouseEvent e) {
00444 Point p = e.getPoint();
00445
00446 if ( odometer != null ) {
00447 String output = " (" + p.x + ", " + p.y + ")";
00448 odometer.setText(output);
00449 }
00450 }
00451
00452 public final void mouseClicked(MouseEvent e) {
00453 }
00454
00455 public final void mouseMoved(MouseEvent e) {
00456 Point p = e.getPoint();
00457
00458 if ( odometer != null ) {
00459 String output = " (" + p.x + ", " + p.y + ")";
00460 odometer.setText(output);
00461 }
00462 }
00463
00464 public final void mouseDragged(MouseEvent e) {
00465 mousePressed(e);
00466 }
00467 }