/* */ package com.sun.j3d.utils.image;
/* */
/* */ import java.awt.Component;
/* */ import java.awt.Container;
/* */ import java.awt.Graphics;
/* */ import java.awt.Graphics2D;
/* */ import java.awt.Image;
/* */ import java.awt.Toolkit;
/* */ import java.awt.color.ColorSpace;
/* */ import java.awt.geom.AffineTransform;
/* */ import java.awt.image.BufferedImage;
/* */ import java.awt.image.ComponentColorModel;
/* */ import java.awt.image.Raster;
/* */ import java.awt.image.WritableRaster;
/* */ import java.io.PrintStream;
/* */ import java.lang.reflect.Method;
/* */ import java.net.URL;
/* */ import java.security.AccessController;
/* */ import java.security.PrivilegedAction;
/* */ import javax.media.j3d.ImageComponent2D;
/* */ import javax.media.j3d.Texture;
/* */ import javax.media.j3d.Texture2D;
/* */
/* */ public class TextureLoader
/* */ {
/* */ public static final int GENERATE_MIPMAP = 1;
/* */ public static final int BY_REFERENCE = 2;
/* */ public static final int Y_UP = 4;
/* 98 */ private static ColorSpace cs = ColorSpace.getInstance(1000);
/* 99 */ private static int[] nBits = { 8, 8, 8, 8 };
/* 100 */ private static int[] bandOffset = { 0, 1, 2, 3 };
/* 101 */ private static ComponentColorModel colorModel = new ComponentColorModel(cs, nBits, true, false, 3, 0);
/* */
/* 103 */ private Texture2D tex = null;
/* 104 */ private BufferedImage bufferedImage = null;
/* 105 */ private ImageComponent2D imageComponent = null;
/* 106 */ private int textureFormat = 6;
/* 107 */ private int imageComponentFormat = 2;
/* */ private int flags;
/* */ private boolean byRef;
/* */ private boolean yUp;
/* 112 */ private boolean useJAI = false;
/* */ private Method jaiGetWidth;
/* */ private Method jaiGetHeight;
/* */ private Method jaiGetAsBufferedImage;
/* */ private Method jaiGetRendering;
/* */ private Method jaiCreate;
/* 122 */ private boolean doneJAICheck = false;
/* 123 */ private boolean jaiInstalled = false;
/* */
/* */ public TextureLoader(BufferedImage bImage)
/* */ {
/* 131 */ this(bImage, new String("RGBA"), 0);
/* */ }
/* */
/* */ public TextureLoader(BufferedImage bImage, String format)
/* */ {
/* 141 */ this(bImage, format, 0);
/* */ }
/* */
/* */ public TextureLoader(BufferedImage bImage, int flags)
/* */ {
/* 151 */ this(bImage, new String("RGBA"), flags);
/* */ }
/* */
/* */ public TextureLoader(BufferedImage bImage, String format, int flags)
/* */ {
/* 162 */ parseFormat(format);
/* 163 */ this.flags = flags;
/* 164 */ this.bufferedImage = bImage;
/* */
/* 166 */ if ((flags & 0x2) != 0) {
/* 167 */ this.byRef = true;
/* */ }
/* 169 */ if ((flags & 0x4) != 0)
/* 170 */ this.yUp = true;
/* */ }
/* */
/* */ public TextureLoader(Image image, Component observer)
/* */ {
/* 182 */ this(image, new String("RGBA"), 0, observer);
/* */ }
/* */
/* */ public TextureLoader(Image image, String format, Component observer)
/* */ {
/* 193 */ this(image, format, 0, observer);
/* */ }
/* */
/* */ public TextureLoader(Image image, int flags, Component observer)
/* */ {
/* 204 */ this(image, new String("RGBA"), flags, observer);
/* */ }
/* */
/* */ public TextureLoader(Image image, String format, int flags, Component observer)
/* */ {
/* 218 */ if (observer == null) {
/* 219 */ observer = new Container();
/* */ }
/* */
/* 222 */ parseFormat(format);
/* 223 */ this.flags = flags;
/* 224 */ this.bufferedImage = createBufferedImage(image, observer);
/* */
/* 226 */ if ((flags & 0x2) != 0) {
/* 227 */ this.byRef = true;
/* */ }
/* 229 */ if ((flags & 0x4) != 0)
/* 230 */ this.yUp = true;
/* */ }
/* */
/* */ public TextureLoader(String fname, Component observer)
/* */ {
/* 242 */ this(fname, new String("RGBA"), 0, observer);
/* */ }
/* */
/* */ public TextureLoader(String fname, String format, Component observer)
/* */ {
/* 253 */ this(fname, format, 0, observer);
/* */ }
/* */
/* */ public TextureLoader(String fname, int flags, Component observer)
/* */ {
/* 264 */ this(fname, new String("RGBA"), flags, observer);
/* */ }
/* */
/* */ public TextureLoader(String fname, String format, int flags, Component observer)
/* */ {
/* 278 */ if (observer == null) {
/* 279 */ observer = new Container();
/* */ }
/* */
/* 282 */ Toolkit toolkit = Toolkit.getDefaultToolkit();
/* 283 */ Image[] image = new Image[1];
/* 284 */ String fn = fname;
/* */
/* 286 */ AccessController.doPrivileged(new PrivilegedAction() { private final Image[] val$image;
/* */ private final Toolkit val$toolkit;
/* */ private final String val$fn;
/* */
/* 289 */ public Object run() { this.val$image[0] = this.val$toolkit.createImage(this.val$fn);
/* 290 */ return null;
/* */ }
/* */ });
/* 294 */ parseFormat(format);
/* 295 */ this.flags = flags;
/* 296 */ this.bufferedImage = createBufferedImage(image[0], observer);
/* */
/* 298 */ if ((this.bufferedImage == null) && (JAIInstalled())) {
/* 299 */ parseFormat(format);
/* 300 */ this.bufferedImage = JAIgetImage(fname, observer);
/* */ }
/* */
/* 303 */ if (this.bufferedImage == null) {
/* 304 */ System.err.println("Error loading Image " + fname);
/* */ }
/* 306 */ if ((flags & 0x2) != 0) {
/* 307 */ this.byRef = true;
/* */ }
/* 309 */ if ((flags & 0x4) != 0)
/* 310 */ this.yUp = true;
/* */ }
/* */
/* */ private BufferedImage JAIgetImage(String filename, Component observer)
/* */ {
/* 318 */ String fn = filename;
/* 319 */ BufferedImage bImage = null;
/* */ try
/* */ {
/* 322 */ Object source = AccessController.doPrivileged(new PrivilegedAction() {
/* */ private final String val$fn;
/* */
/* */ public Object run() {
/* */ try {
/* 327 */ Object renderedOp = TextureLoader.this.jaiCreate.invoke(null, new Object[] { "fileload", this.val$fn });
/* */
/* 330 */ TextureLoader.this.jaiGetRendering.invoke(renderedOp, new Object[0]);
/* */
/* 332 */ return renderedOp;
/* */ } catch (Exception e) {
/* 334 */ e.printStackTrace();
/* 335 */ }return null;
/* */ }
/* */ });
/* 341 */ int width = ((Integer)this.jaiGetWidth.invoke(source, new Object[0])).intValue();
/* 342 */ int height = ((Integer)this.jaiGetHeight.invoke(source, new Object[0])).intValue();
/* */
/* 345 */ WritableRaster wr = Raster.createInterleavedRaster(0, width, height, width * 4, 4, bandOffset, null);
/* 346 */ bImage = new BufferedImage(colorModel, wr, false, null);
/* */
/* 348 */ Graphics g = bImage.getGraphics();
/* 349 */ BufferedImage im = (BufferedImage)this.jaiGetAsBufferedImage.invoke(source, new Object[0]);
/* 350 */ g.drawImage(im, 0, 0, observer);
/* */ } catch (Exception e) {
/* 352 */ return null;
/* */ }
/* */
/* 357 */ return bImage;
/* */ }
/* */
/* */ private BufferedImage JAIgetImage(URL url, Component observer)
/* */ {
/* 364 */ URL local_url = url;
/* 365 */ BufferedImage bImage = null;
/* */ try {
/* 367 */ Object source = AccessController.doPrivileged(new PrivilegedAction() {
/* */ private final URL val$local_url;
/* */
/* */ public Object run() {
/* */ try {
/* 372 */ Object renderedOp = TextureLoader.this.jaiCreate.invoke(null, new Object[] { "URL", this.val$local_url });
/* */
/* 375 */ TextureLoader.this.jaiGetRendering.invoke(renderedOp, new Object[0]);
/* */
/* 377 */ return renderedOp;
/* */ } catch (Exception e) {
/* 379 */ e.printStackTrace();
/* 380 */ }return null;
/* */ }
/* */ });
/* 387 */ int width = ((Integer)this.jaiGetWidth.invoke(source, new Object[0])).intValue();
/* 388 */ int height = ((Integer)this.jaiGetHeight.invoke(source, new Object[0])).intValue();
/* */
/* 391 */ WritableRaster wr = Raster.createInterleavedRaster(0, width, height, width * 4, 4, bandOffset, null);
/* 392 */ bImage = new BufferedImage(colorModel, wr, false, null);
/* */
/* 394 */ Graphics g = bImage.getGraphics();
/* 395 */ BufferedImage im = (BufferedImage)this.jaiGetAsBufferedImage.invoke(source, new Object[0]);
/* 396 */ g.drawImage(im, 0, 0, observer);
/* */ } catch (Exception e) {
/* 398 */ return null;
/* */ }
/* */
/* 402 */ return bImage;
/* */ }
/* */
/* */ private boolean JAIInstalled()
/* */ {
/* 411 */ if (!this.doneJAICheck) {
/* */ try {
/* 413 */ Class cl = Class.forName("javax.media.jai.PlanarImage");
/* 414 */ this.jaiGetWidth = cl.getDeclaredMethod("getWidth", new Class[0]);
/* 415 */ this.jaiGetHeight = cl.getDeclaredMethod("getHeight", new Class[0]);
/* 416 */ this.jaiGetAsBufferedImage = cl.getDeclaredMethod("getAsBufferedImage", new Class[0]);
/* 417 */ Class cl2 = Class.forName("javax.media.jai.JAI");
/* 418 */ this.jaiCreate = cl2.getDeclaredMethod("create", new Class[] { String.class, Object.class });
/* 419 */ Class cl3 = Class.forName("javax.media.jai.RenderedOp");
/* 420 */ this.jaiGetRendering = cl3.getDeclaredMethod("getRendering", new Class[0]);
/* */
/* 422 */ this.jaiInstalled = true;
/* */ } catch (ClassNotFoundException e) {
/* 424 */ this.jaiInstalled = false;
/* */ } catch (NoSuchMethodException ex) {
/* 426 */ ex.printStackTrace();
/* 427 */ this.jaiInstalled = false;
/* */ }
/* */
/* 430 */ this.doneJAICheck = true;
/* */ }
/* */
/* 433 */ return this.jaiInstalled;
/* */ }
/* */
/* */ public TextureLoader(URL url, Component observer)
/* */ {
/* 444 */ this(url, new String("RGBA"), 0, observer);
/* */ }
/* */
/* */ public TextureLoader(URL url, String format, Component observer)
/* */ {
/* 455 */ this(url, format, 0, observer);
/* */ }
/* */
/* */ public TextureLoader(URL url, int flags, Component observer)
/* */ {
/* 466 */ this(url, new String("RGBA"), flags, observer);
/* */ }
/* */
/* */ public TextureLoader(URL url, String format, int flags, Component observer)
/* */ {
/* 479 */ if (observer == null) {
/* 480 */ observer = new Container();
/* */ }
/* */
/* 483 */ Toolkit toolkit = Toolkit.getDefaultToolkit();
/* 484 */ Image[] image = new Image[1];
/* 485 */ URL Url = url;
/* */
/* 487 */ AccessController.doPrivileged(new PrivilegedAction() { private final Image[] val$image;
/* */ private final Toolkit val$toolkit;
/* */ private final URL val$Url;
/* */
/* 490 */ public Object run() { this.val$image[0] = this.val$toolkit.getImage(this.val$Url);
/* 491 */ return null;
/* */ }
/* */ });
/* 495 */ parseFormat(format);
/* 496 */ this.flags = flags;
/* 497 */ this.bufferedImage = createBufferedImage(image[0], observer);
/* */
/* 499 */ if ((this.bufferedImage == null) && (JAIInstalled())) {
/* 500 */ this.bufferedImage = JAIgetImage(url, observer);
/* */ }
/* */
/* 503 */ if (this.bufferedImage == null) {
/* 504 */ System.err.println("Error loading Image " + url.toString());
/* */ }
/* 506 */ if ((flags & 0x2) != 0) {
/* 507 */ this.byRef = true;
/* */ }
/* 509 */ if ((flags & 0x4) != 0)
/* 510 */ this.yUp = true;
/* */ }
/* */
/* */ public ImageComponent2D getImage()
/* */ {
/* 522 */ if (this.imageComponent == null) {
/* 523 */ this.imageComponent = new ImageComponent2D(this.imageComponentFormat, this.bufferedImage, this.byRef, this.yUp);
/* */ }
/* 525 */ return this.imageComponent;
/* */ }
/* */
/* */ public ImageComponent2D getScaledImage(float xScale, float yScale)
/* */ {
/* 538 */ if ((xScale == 1.0F) && (yScale == 1.0F)) {
/* 539 */ return getImage();
/* */ }
/* 541 */ return new ImageComponent2D(this.imageComponentFormat, getScaledImage(this.bufferedImage, xScale, yScale), this.byRef, this.yUp);
/* */ }
/* */
/* */ public ImageComponent2D getScaledImage(int width, int height)
/* */ {
/* 557 */ if ((this.bufferedImage.getWidth() == width) && (this.bufferedImage.getHeight() == height))
/* */ {
/* 559 */ return getImage();
/* */ }
/* 561 */ return new ImageComponent2D(this.imageComponentFormat, getScaledImage(this.bufferedImage, width, height), this.byRef, this.yUp);
/* */ }
/* */
/* */ public Texture getTexture()
/* */ {
/* 575 */ ImageComponent2D[] scaledImageComponents = null;
/* 576 */ BufferedImage[] scaledBufferedImages = null;
/* 577 */ if (this.tex == null) {
/* 578 */ if (this.bufferedImage == null) return null;
/* */
/* 580 */ int width = getClosestPowerOf2(this.bufferedImage.getWidth());
/* 581 */ int height = getClosestPowerOf2(this.bufferedImage.getHeight());
/* */
/* 583 */ if ((this.flags & 0x1) != 0)
/* */ {
/* 585 */ BufferedImage origImage = this.bufferedImage;
/* 586 */ int newW = width;
/* 587 */ int newH = height;
/* 588 */ int level = Math.max(computeLog(width), computeLog(height)) + 1;
/* 589 */ scaledImageComponents = new ImageComponent2D[level];
/* 590 */ scaledBufferedImages = new BufferedImage[level];
/* 591 */ this.tex = new Texture2D(2, this.textureFormat, width, height);
/* */
/* 594 */ for (int i = 0; i < level; i++) {
/* 595 */ scaledBufferedImages[i] = getScaledImage(origImage, newW, newH);
/* 596 */ scaledImageComponents[i] = new ImageComponent2D(this.imageComponentFormat, scaledBufferedImages[i], this.byRef, this.yUp);
/* */
/* 600 */ this.tex.setImage(i, scaledImageComponents[i]);
/* 601 */ if (newW > 1) newW >>= 1;
/* 602 */ if (newH > 1) newH >>= 1;
/* 603 */ origImage = scaledBufferedImages[i];
/* */ }
/* */ }
/* */ else {
/* 607 */ scaledImageComponents = new ImageComponent2D[1];
/* 608 */ scaledBufferedImages = new BufferedImage[1];
/* */
/* 611 */ scaledBufferedImages[0] = getScaledImage(this.bufferedImage, width, height);
/* */
/* 613 */ scaledImageComponents[0] = new ImageComponent2D(this.imageComponentFormat, scaledBufferedImages[0], this.byRef, this.yUp);
/* */
/* 617 */ this.tex = new Texture2D(1, this.textureFormat, width, height);
/* */
/* 619 */ this.tex.setImage(0, scaledImageComponents[0]);
/* */ }
/* 621 */ this.tex.setMinFilter(3);
/* 622 */ this.tex.setMagFilter(3);
/* */ }
/* */
/* 625 */ return this.tex;
/* */ }
/* */
/* */ private BufferedImage createBufferedImage(Image image, Component observer)
/* */ {
/* 633 */ observer.prepareImage(image, null);
/* */ while (true) {
/* 635 */ int status = observer.checkImage(image, null);
/* 636 */ if ((status & 0x40) != 0)
/* 637 */ return null;
/* 638 */ if ((status & 0x20) != 0)
/* */ break;
/* */ try
/* */ {
/* 642 */ Thread.sleep(100L);
/* */ } catch (InterruptedException e) {
/* */ }
/* */ }
/* 646 */ int width = image.getWidth(observer);
/* 647 */ int height = image.getHeight(observer);
/* */
/* 649 */ WritableRaster wr = Raster.createInterleavedRaster(0, width, height, width * 4, 4, bandOffset, null);
/* 650 */ BufferedImage bImage = new BufferedImage(colorModel, wr, false, null);
/* */
/* 652 */ Graphics g = bImage.getGraphics();
/* 653 */ g.drawImage(image, 0, 0, observer);
/* */
/* 655 */ return bImage;
/* */ }
/* */
/* */ private void parseFormat(String format)
/* */ {
/* 660 */ if (format.equals("RGBA")) {
/* 661 */ this.imageComponentFormat = 2;
/* 662 */ this.textureFormat = 6;
/* */ }
/* 664 */ else if (format.equals("RGBA4")) {
/* 665 */ this.imageComponentFormat = 6;
/* 666 */ this.textureFormat = 6;
/* */ }
/* 668 */ else if (format.equals("RGB5_A1")) {
/* 669 */ this.imageComponentFormat = 4;
/* 670 */ this.textureFormat = 6;
/* */ }
/* 672 */ else if (format.equals("RGB")) {
/* 673 */ this.imageComponentFormat = 1;
/* 674 */ this.textureFormat = 5;
/* */ }
/* 676 */ else if (format.equals("RGB4")) {
/* 677 */ this.imageComponentFormat = 5;
/* 678 */ this.textureFormat = 5;
/* */ }
/* 680 */ else if (format.equals("RGB5")) {
/* 681 */ this.imageComponentFormat = 3;
/* 682 */ this.textureFormat = 5;
/* */ }
/* 684 */ else if (format.equals("R3_G3_B2")) {
/* 685 */ this.imageComponentFormat = 9;
/* 686 */ this.textureFormat = 5;
/* */ }
/* 688 */ else if (format.equals("LUM8_ALPHA8")) {
/* 689 */ this.imageComponentFormat = 8;
/* 690 */ this.textureFormat = 4;
/* */ }
/* 692 */ else if (format.equals("LUM4_ALPHA4")) {
/* 693 */ this.imageComponentFormat = 7;
/* 694 */ this.textureFormat = 4;
/* */ }
/* 696 */ else if (format.equals("LUMINANCE")) {
/* 697 */ this.imageComponentFormat = 10;
/* 698 */ this.textureFormat = 2;
/* */ }
/* 700 */ else if (format.equals("ALPHA")) {
/* 701 */ this.imageComponentFormat = 10;
/* 702 */ this.textureFormat = 3;
/* */ }
/* */ }
/* */
/* */ private BufferedImage getScaledImage(BufferedImage origImage, int width, int height)
/* */ {
/* 710 */ int origW = origImage.getWidth();
/* 711 */ int origH = origImage.getHeight();
/* 712 */ float xScale = width / origW;
/* 713 */ float yScale = height / origH;
/* */
/* 715 */ return getScaledImage(origImage, xScale, yScale);
/* */ }
/* */
/* */ private BufferedImage getScaledImage(BufferedImage origImage, float xScale, float yScale)
/* */ {
/* 723 */ if ((xScale == 1.0F) && (yScale == 1.0F)) {
/* 724 */ return origImage;
/* */ }
/* */
/* 727 */ int scaleW = (int)(origImage.getWidth() * xScale + 0.5D);
/* 728 */ int scaleH = (int)(origImage.getHeight() * yScale + 0.5D);
/* 729 */ WritableRaster wr = Raster.createInterleavedRaster(0, scaleW, scaleH, scaleW * 4, 4, bandOffset, null);
/* 730 */ BufferedImage scaledImage = new BufferedImage(colorModel, wr, false, null);
/* */
/* 732 */ Graphics2D g2 = scaledImage.createGraphics();
/* 733 */ AffineTransform at = AffineTransform.getScaleInstance(xScale, yScale);
/* */
/* 735 */ g2.transform(at);
/* 736 */ g2.drawImage(origImage, 0, 0, null);
/* */
/* 738 */ return scaledImage;
/* */ }
/* */
/* */ private int computeLog(int value)
/* */ {
/* 744 */ int i = 0;
/* */
/* 746 */ if (value == 0) return -1; while (true)
/* */ {
/* 748 */ if (value == 1)
/* 749 */ return i;
/* 750 */ value >>= 1;
/* 751 */ i++;
/* */ }
/* */ }
/* */
/* */ private int getClosestPowerOf2(int value)
/* */ {
/* 757 */ if (value < 1) {
/* 758 */ return value;
/* */ }
/* 760 */ int powerValue = 1;
/* */ do
/* 762 */ powerValue *= 2;
/* 763 */ while (value >= powerValue);
/* */
/* 765 */ int minBound = powerValue / 2;
/* 766 */ if (powerValue - value > value - minBound)
/* */ {
/* 768 */ return minBound;
/* */ }
/* 770 */ return powerValue;
/* */ }
/* */ }
/* Location: Z:\System\Library\Java\Extensions\j3dutils.jar
* Qualified Name: com.sun.j3d.utils.image.TextureLoader
* JD-Core Version: 0.6.2
*/