diff --git a/core/PApplet.java b/core/PApplet.java index c1d369f20..962b0b797 100644 --- a/core/PApplet.java +++ b/core/PApplet.java @@ -41,11 +41,11 @@ public class PApplet extends Applet implements PConstants, Runnable, MouseListener, MouseMotionListener, KeyListener, FocusListener { - // jdkVersionStr = "1.3" or "1.1" or whatever - public static final String jdkVersionStr = + // JDK_VERSION_STR = "1.3" or "1.1" or whatever + public static final String JDK_VERSION_STRING = System.getProperty("java.version").substring(0,3); - public static final double jdkVersion = - new Double(jdkVersionStr).doubleValue(); + public static final double JDK_VERSION = + new Double(JDK_VERSION_STRING).doubleValue(); //toFloat(System.getProperty("java.version").substring(0,3)); public PGraphics g; @@ -210,7 +210,7 @@ public class PApplet extends Applet // send tab keys through to the PApplet try { - if (jdkVersion >= 1.4) { + if (JDK_VERSION >= 1.4) { //setFocusTraversalKeysEnabled(false); // 1.4-only function Method defocus = Component.class.getMethod("setFocusTraversalKeysEnabled", @@ -262,7 +262,7 @@ public class PApplet extends Applet public void createGraphics() { Dimension size = getSize(); - if (PApplet.jdkVersion >= 1.3) { + if (PApplet.JDK_VERSION >= 1.3) { g = new PGraphics2(INITIAL_WIDTH, INITIAL_HEIGHT); //g = new PGraphics2(size.width, size.height); //DEFAULT_WIDTH, DEFAULT_HEIGHT); @@ -1390,11 +1390,11 @@ public class PApplet extends Applet */ public void cursor(PImage image, int hotspotX, int hotspotY) { //if (!isOneTwoOrBetter()) { - if (jdkVersion < 1.2) { + if (JDK_VERSION < 1.2) { System.err.println("cursor() error: Java 1.2 or higher is " + "required to set cursors"); System.err.println(" (You're using version " + - jdkVersionStr + ")"); + JDK_VERSION_STRING + ")"); return; } @@ -1423,7 +1423,7 @@ public class PApplet extends Applet } catch (NoSuchMethodError e) { System.out.println("cursor() is not available on " + - nf((float)jdkVersion, 1, 1)); + nf((float)JDK_VERSION, 1, 1)); } catch (IndexOutOfBoundsException e) { System.err.println("cursor() error: the hotspot " + hotspot + " is out of bounds for the given image."); @@ -1974,7 +1974,7 @@ public class PApplet extends Applet public PSound loadSound(String filename) { - if (PApplet.jdkVersion >= 1.3) { + if (PApplet.JDK_VERSION >= 1.3) { return new PSound2(this, openStream(filename)); } return new PSound(this, openStream(filename)); diff --git a/core/PFont.java b/core/PFont.java index 946d2ae3a..8bf43f5b5 100644 --- a/core/PFont.java +++ b/core/PFont.java @@ -473,6 +473,7 @@ public class PFont implements PConstants { float savedTintG = parent.tintG; float savedTintB = parent.tintB; float savedTintA = parent.tintA; + boolean savedTintAlpha = parent.tintAlpha; parent.tint = true; parent.tintColor = parent.fillColor; @@ -480,6 +481,7 @@ public class PFont implements PConstants { parent.tintG = parent.fillG; parent.tintB = parent.fillB; parent.tintA = parent.fillA; + parent.tintAlpha = parent.fillAlpha; parent.imageImpl(images[glyph], x1, y1, x2, y2, //x2-x1, y2-y1, @@ -491,6 +493,7 @@ public class PFont implements PConstants { parent.tintG = savedTintG; parent.tintB = savedTintB; parent.tintA = savedTintA; + parent.tintAlpha = savedTintAlpha; /* // this code was moved here (instead of using parent.image) diff --git a/core/PGraphics.java b/core/PGraphics.java index 199be1c8c..02a4bede5 100644 --- a/core/PGraphics.java +++ b/core/PGraphics.java @@ -1362,7 +1362,7 @@ public class PGraphics extends PImage implements PConstants { public void image(PImage image, float x, float y) { imageImpl(image, - x, y, image.width, image.height, + x, y, x+image.width, y+image.height, 0, 0, image.width, image.height); } @@ -1382,12 +1382,12 @@ public class PGraphics extends PImage implements PConstants { int u1, int v1, int u2, int v2) { if (imageMode == CORNER) { imageImpl(image, - a, b, c, d, + a, b, a + c, b + d, u1, v1, u2, v2); } else if (imageMode == CORNERS) { imageImpl(image, - a, b, c - a, d - b, + a, b, c, d, u1, v1, u2, v2); /* diff --git a/core/PGraphics2.java b/core/PGraphics2.java index 493f6b212..ebd31731a 100644 --- a/core/PGraphics2.java +++ b/core/PGraphics2.java @@ -546,12 +546,31 @@ public class PGraphics2 extends PGraphics { //float x, float y, float w, float h, int u1, int v1, int u2, int v2) { if (who.cache == null) { - who.cache = new BufferedImage(who.width, who.height, - BufferedImage.TYPE_INT_ARGB); + who.cache = new ImageCache(who); + + //who.cache = new BufferedImage(who.width, who.height, + // BufferedImage.TYPE_INT_ARGB); who.updatePixels(); // mark the whole thing for update } + ImageCache cash = (ImageCache) who.cache; + // if image previously was tinted, or the color changed + // or the image was tinted, and tint is now disabled + if ((tint && (!cash.tinted || (cash.tintedColor != tintColor))) || + (!tint && cash.tinted)) { + // for tint change, mark all pixels as needing update + who.updatePixels(); + } + if (who.modified) { + cash.update(); + who.modified = false; + } + + /* + if (who.modified) { + ((ImageCache) who.cache).update(); + // update the sub-portion of the image as necessary BufferedImage bi = (BufferedImage) who.cache; @@ -565,17 +584,126 @@ public class PGraphics2 extends PGraphics { //who.pixelsUpdated(); who.modified = false; } + */ //int x2 = (int) (x + w); //int y2 = (int) (y + h); - graphics.drawImage((Image) who.cache, + graphics.drawImage(((ImageCache) who.cache).image, //(int) x, (int) y, x2, y2, (int) x1, (int) y1, (int) x2, (int) y2, u1, v1, u2, v2, null); } + class ImageCache { + PImage source; + boolean tinted; + int tintedColor; + int tintedPixels[]; + BufferedImage image; + + public ImageCache(PImage source) { + this.source = source; + // if RGB, set the image type to RGB, + // otherwise it's ALPHA or ARGB, and use an ARGB bimage + int type = (source.format == RGB) ? + BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB; + image = new BufferedImage(source.width, source.height, type); + } + + public void update() { //boolean t, int argb) { + if ((source.format == ARGB) || (source.format == RGB)) { + if (tint) { + // create tintedPixels[] if necessary + if (tintedPixels == null) { + tintedPixels = new int[source.width * source.height]; + } + + int argb2 = tintColor; + int a2 = (tintColor >> 24) & 0xff; + int r2 = (tintColor >> 16) & 0xff; + int g2 = (tintColor >> 8) & 0xff; + int b2 = (tintColor) & 0xff; + + // multiply each of the color components into tintedPixels + for (int i = 0; i < tintedPixels.length; i++) { + int argb1 = source.pixels[i]; + int a1 = (argb1 >> 24) & 0xff; + int r1 = (argb1 >> 16) & 0xff; + int g1 = (argb1 >> 8) & 0xff; + int b1 = (argb1) & 0xff; + + tintedPixels[i] = + (((a2 * a1) & 0xff00) << 16) | + (((r2 * r1) & 0xff00) << 8) | + ((g2 * g1) & 0xff00) | + (((b2 * b1) & 0xff00) >> 8); + } + tinted = true; + tintedColor = tintColor; + + // finally, do a setRGB based on tintedPixels + image.setRGB(0, 0, source.width, source.height, + tintedPixels, 0, source.width); + + } else { // no tint + // just do a setRGB like before + image.setRGB(0, 0, source.width, source.height, + source.pixels, 0, source.width); + } + + } else if (source.format == ALPHA) { + if (tintedPixels == null) { + tintedPixels = new int[source.width * source.height]; + } + + int lowbits = tintColor & 0x00ffffff; + if (((tintColor >> 24) & 0xff) >= 254) { + // no actual alpha to the tint, set the image's alpha + // as the high 8 bits, and use the color as the low 24 bits + for (int i = 0; i < tintedPixels.length; i++) { + // don't bother with the math if value is zero + tintedPixels[i] = (source.pixels[i] == 0) ? + 0 : (source.pixels[i] << 24) | lowbits; + } + + } else { + // multiply each image alpha by the tint alpha + int alphabits = (tintColor >> 24) & 0xff; + for (int i = 0; i < tintedPixels.length; i++) { + tintedPixels[i] = (source.pixels[i] == 0) ? + 0 : (((alphabits * source.pixels[i]) & 0xFF00) << 16) | lowbits; + } + } + + // mark the pixels for next time + tinted = true; + tintedColor = tintColor; + + // finally, do a setRGB based on tintedPixels + image.setRGB(0, 0, source.width, source.height, + tintedPixels, 0, source.width); + /* + int argb2 = tint ? tintColor : 0xFFFFFFFF; + int a2 = (tintColor >> 24) & 0xff; + int r2 = (tintColor >> 16) & 0xff; + int g2 = (tintColor >> 8) & 0xff; + int b2 = (tintColor) & 0xff; + + // multiply each of the color components into tintedPixels + for (int i = 0; i < tintedPixels.length; i++) { + int argb1 = source.pixels[i]; + int a1 = (argb1 >> 24) & 0xff; + int r1 = (argb1 >> 16) & 0xff; + int g1 = (argb1 >> 8) & 0xff; + int b1 = (argb1) & 0xff; + */ + } + } + } + + ////////////////////////////////////////////////////////////// diff --git a/core/todo.txt b/core/todo.txt index 6dd02f220..80b61a10c 100644 --- a/core/todo.txt +++ b/core/todo.txt @@ -111,14 +111,21 @@ X set initial size using --size= X color channels seem to be swapped on windows in image example X check on the mac to see what it looks like -_ get text working again -_ partially the problem is with ALPHA images -_ how should they be stored internally -_ also colored text, requires tint() to work properly +_ call filter() to convert RGB/RGBA/ALPHA/GRAY +_ cuz the cache is gonna be bad going rgb to rgba -_ move textMode and textSpace back out of PFont -_ use die() to fail in font situations -_ can't, just use a RuntimeException +X fix font coloring in PGraphics2 +_ fix tint() for PGraphics2 +_ fix tint() for PGraphics3 (what could be wrong?) +_ maybe not setting fill color when drawing images? + +_ get text working again +X partially the problem is with ALPHA images +X how should they be stored internally +X also colored text, requires tint() to work properly +X move textMode and textSpace back out of PFont +X use die() to fail in font situations +X can't, just use a RuntimeException _ default to single byte input from serial port _ and add serial.setBuffer() for message length as alternative @@ -127,6 +134,12 @@ _ or serial.setDelimiter() to fire message on delim // +_ implement PGraphics2.curveVertex() + +_ make Graphics2 et al load dynamically using reflection +_ can this be done with g2 and if exception just falls back to g1? +_ this way people can remove g1 by hand + api todos _ sphere x,y,z,r or box w,h,d.. need to make them consistent _ look at curve functions more closely @@ -136,12 +149,8 @@ _ how to force PGraphics() instead of PGraphics2() _ also have a simple way to hook in triangle leeches to PGraphics3 -_ implement PGraphics2.curveVertex() - _ get PGraphics.java engine working again -// - lighting _ lighting totally sucks (both PGraphics3 and PGraphicsGL) _ bring in materials for opengl as well?