From 5e82cbe6362e6de49539c3b010e0772b80cb82a2 Mon Sep 17 00:00:00 2001 From: Ben Fry Date: Tue, 14 Jul 2015 11:51:48 -0400 Subject: [PATCH] support additional image types from ImageIO.read(), check alpha on unknown images (fixes #3442), other notes --- core/src/processing/awt/PGraphicsJava2D.java | 3 ++ core/src/processing/core/PApplet.java | 3 +- core/src/processing/core/PImage.java | 39 +++++++++++++++----- core/src/processing/core/ThinkDifferent.java | 1 + core/todo.txt | 18 +++++++-- todo.txt | 7 ++++ 6 files changed, 57 insertions(+), 14 deletions(-) diff --git a/core/src/processing/awt/PGraphicsJava2D.java b/core/src/processing/awt/PGraphicsJava2D.java index 9a4288bca..d64b31edd 100644 --- a/core/src/processing/awt/PGraphicsJava2D.java +++ b/core/src/processing/awt/PGraphicsJava2D.java @@ -2903,6 +2903,9 @@ public class PGraphicsJava2D extends PGraphics { if ((sourceX == 0) && (sourceY == 0) && (sourceWidth == sourceImage.width) && (sourceHeight == sourceImage.height)) { +// System.out.format("%d %d %dx%d %d%n", targetX, targetY, +// sourceImage.width, sourceImage.height, +// sourceImage.pixels.length); raster.setDataElements(targetX, targetY, sourceImage.width, sourceImage.height, sourceImage.pixels); diff --git a/core/src/processing/core/PApplet.java b/core/src/processing/core/PApplet.java index f332978ca..4569ef34f 100644 --- a/core/src/processing/core/PApplet.java +++ b/core/src/processing/core/PApplet.java @@ -5294,7 +5294,8 @@ public class PApplet implements PConstants { } // if it's a .gif image, test to see if it has transparency - if (extension.equals("gif") || extension.equals("png")) { + if (extension.equals("gif") || extension.equals("png") || + extension.equals("unknown")) { image.checkAlpha(); } diff --git a/core/src/processing/core/PImage.java b/core/src/processing/core/PImage.java index c8142540d..18755711c 100644 --- a/core/src/processing/core/PImage.java +++ b/core/src/processing/core/PImage.java @@ -309,18 +309,31 @@ public class PImage implements PConstants, Cloneable { BufferedImage bi = (BufferedImage) img; width = bi.getWidth(); height = bi.getHeight(); - pixels = new int[width * height]; - pixels = ((DataBufferInt) bi.getRaster().getDataBuffer()).getData(); int type = bi.getType(); - if (type == BufferedImage.TYPE_INT_ARGB) { - format = ARGB; - } else if (type == BufferedImage.TYPE_INT_RGB) { - for (int i = 0; i < pixels.length; i++) { - pixels[i] = 0xFF000000 | pixels[i]; + if (type == BufferedImage.TYPE_3BYTE_BGR || + type == BufferedImage.TYPE_4BYTE_ABGR) { + pixels = new int[width * height]; + bi.getRGB(0, 0, width, height, pixels, 0, width); + if (type == BufferedImage.TYPE_4BYTE_ABGR) { + format = ARGB; + } else { + opaque(); + } + } else { + DataBuffer db = bi.getRaster().getDataBuffer(); + if (db instanceof DataBufferInt) { + pixels = ((DataBufferInt) db).getData(); + if (type == BufferedImage.TYPE_INT_ARGB) { + format = ARGB; + } else if (type == BufferedImage.TYPE_INT_RGB) { + opaque(); + } } } - - } else { // go the old school java 1.0 route + } + // Implements fall-through if not DataBufferInt above, or not a + // known type, or not DataBufferInt for the data itself. + if (pixels == null) { // go the old school Java 1.0 route width = img.getWidth(null); height = img.getHeight(null); pixels = new int[width * height]; @@ -1263,6 +1276,14 @@ public class PImage implements PConstants, Cloneable { } + /** Set the high bits of all pixels to opaque. */ + protected void opaque() { + for (int i = 0; i < pixels.length; i++) { + pixels[i] = 0xFF000000 | pixels[i]; + } + } + + /** * Optimized code for building the blur kernel. * further optimized blur code (approx. 15% for radius=20) diff --git a/core/src/processing/core/ThinkDifferent.java b/core/src/processing/core/ThinkDifferent.java index ca299be6a..ca2903f91 100644 --- a/core/src/processing/core/ThinkDifferent.java +++ b/core/src/processing/core/ThinkDifferent.java @@ -71,6 +71,7 @@ public class ThinkDifferent { } + // Called via reflection from PSurfaceAWT and others static public void setIconImage(Image image) { // When already set, is a sun.awt.image.MultiResolutionCachedImage on OS X // Image current = application.getDockIconImage(); diff --git a/core/todo.txt b/core/todo.txt index 5b745d5ab..fde528788 100644 --- a/core/todo.txt +++ b/core/todo.txt @@ -46,6 +46,8 @@ X https://github.com/processing/processing/issues/3413 X SVG briefly broken for Java2D X https://github.com/processing/processing/issues/3417 X change how font metrics are pulled to fix text width issues +X check alpha when image extension is "unknown" +X https://github.com/processing/processing/issues/3442 threading headaches X sketch not always showing with empty draw() @@ -93,6 +95,10 @@ X errors with loading SVGs in P3D/P2D X https://github.com/processing/processing/issues/3379 X sketch window briefly appears on top left corner when using OpenGL X https://github.com/processing/processing/issues/3308 +X add attrib() method +X https://github.com/processing/processing/issues/2963 +X andres needs input on how the api works +o assign this to DXF as well? contribs X Fix NullPointerException in DepthSorter @@ -127,9 +133,17 @@ _ on Export to Application, this has an impact _ update wiki/docs to say "don't override sketchXxxx() methods" +_ change "PythonMode contains an incompatible Mode" message +_ to "PythonMode is not compatible with Processing 3" +_ another "incompatible tool" case +java.lang.AbstractMethodError + at processing.app.contrib.ToolContribution.init(ToolContribution.java:134) + beta X move Java2D and JavaFX classes to their own packages _ make notes about this in the README +_ move AWT image loading into PImageAWT +_ look into how GL and FX will handle from there _ run only the necessary pieces on the EDT _ in part because FX doesn't even use the EDT _ re-check the Linux frame visibility stuff @@ -175,10 +189,6 @@ _ https://www.linkedin.com/pulse/oracle-just-removed-javafx-support-arm-jan-sn opengl _ hard crash at 1920x1080, mirrored, Casey's GT 650M 1GB -_ add attrib() method -_ https://github.com/processing/processing/issues/2963 -_ andres needs input on how the api works -_ assign this to DXF as well? _ window loses focus after maximizing _ https://github.com/processing/processing/issues/3339 diff --git a/todo.txt b/todo.txt index cfd9dbfa2..c6e3b0b5c 100644 --- a/todo.txt +++ b/todo.txt @@ -42,6 +42,8 @@ X https://github.com/processing/processing/issues/3111 X Re-enable export to application with command line X https://github.com/processing/processing/pull/3451 X https://github.com/processing/processing/issues/2760 +X change undefined constructor error message for clarity +X https://github.com/processing/processing/issues/3434 gsoc X Mode problems window wasn't doing line breaks @@ -65,6 +67,10 @@ X https://github.com/processing/processing/issues/3429 X https://github.com/processing/processing/pull/3438 X CM with updated statusPanel and error popup X https://github.com/processing/processing/pull/3452 +X allow modes to import other modes +X https://github.com/processing/processing/pull/3444 +X CM list converted into table +X https://github.com/processing/processing/pull/3454 cleaning o libraries in java tabs (separate .java files) are reported missing @@ -134,6 +140,7 @@ _ recent menu gets huge with other p5 versions on osx pde/build +_ Cannot find "processing.core" library. Line 12 in tab sketch_150704a _ dim out the Run button if there are compile errors detected _ the AST checker has better error message handling for those cases _ and hitting Run replaces the useful error with something weird