mirror of
https://github.com/processing/processing4.git
synced 2026-06-16 04:26:26 +02:00
fix com.sun classes (issue #1424) and background() with blendMode()
This commit is contained in:
@@ -57,6 +57,8 @@ public class PGraphicsJava2D extends PGraphics /*PGraphics2D*/ {
|
||||
public Graphics2D g2;
|
||||
protected BufferedImage offscreen;
|
||||
|
||||
Composite defaultComposite;
|
||||
|
||||
GeneralPath gpath;
|
||||
|
||||
/// break the shape at the next vertex (next vertex() call is a moveto())
|
||||
@@ -159,6 +161,7 @@ public class PGraphicsJava2D extends PGraphics /*PGraphics2D*/ {
|
||||
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
|
||||
g2 = (Graphics2D) image.getGraphics();
|
||||
}
|
||||
defaultComposite = g2.getComposite();
|
||||
|
||||
// can't un-set this because this may be only a resize
|
||||
// http://dev.processing.org/bugs/show_bug.cgi?id=463
|
||||
@@ -515,15 +518,20 @@ public class PGraphicsJava2D extends PGraphics /*PGraphics2D*/ {
|
||||
*/
|
||||
@Override
|
||||
public void blendMode(final int mode) {
|
||||
g2.setComposite(new Composite() {
|
||||
if (mode == BLEND) {
|
||||
g2.setComposite(defaultComposite);
|
||||
|
||||
@Override
|
||||
public CompositeContext createContext(ColorModel srcColorModel,
|
||||
ColorModel dstColorModel,
|
||||
RenderingHints hints) {
|
||||
return new BlendingContext(mode);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
g2.setComposite(new Composite() {
|
||||
|
||||
@Override
|
||||
public CompositeContext createContext(ColorModel srcColorModel,
|
||||
ColorModel dstColorModel,
|
||||
RenderingHints hints) {
|
||||
return new BlendingContext(mode);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1979,11 +1987,16 @@ public class PGraphicsJava2D extends PGraphics /*PGraphics2D*/ {
|
||||
//new Exception().printStackTrace(System.out);
|
||||
// in case people do transformations before background(),
|
||||
// need to handle this with a push/reset/pop
|
||||
Composite oldComposite = g2.getComposite();
|
||||
g2.setComposite(defaultComposite);
|
||||
|
||||
pushMatrix();
|
||||
resetMatrix();
|
||||
g2.setColor(bgColor); //, backgroundAlpha));
|
||||
g2.fillRect(0, 0, width, height);
|
||||
popMatrix();
|
||||
|
||||
g2.setComposite(oldComposite);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -28,8 +28,9 @@ import java.awt.*;
|
||||
import java.awt.image.*;
|
||||
import java.io.*;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Iterator;
|
||||
|
||||
import com.sun.image.codec.jpeg.*;
|
||||
import javax.imageio.*;
|
||||
|
||||
|
||||
/**
|
||||
@@ -3087,29 +3088,32 @@ public class PImage implements PConstants, Cloneable {
|
||||
File file = new File(path);
|
||||
|
||||
if (extension.equals("jpg") || extension.equals("jpeg")) {
|
||||
// ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
// JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
|
||||
BufferedOutputStream output =
|
||||
new BufferedOutputStream(new FileOutputStream(file));
|
||||
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(output);
|
||||
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);
|
||||
// Set JPEG quality to 90% with baseline optimization. Setting this
|
||||
// to 1 was a huge jump (about triple the size), so this seems good.
|
||||
// Oddly, a smaller file size than Photoshop at 90%, but it's a
|
||||
// completely different algorithm, I suppose.
|
||||
param.setQuality(0.9f, true);
|
||||
// p.setQuality(1, true);
|
||||
encoder.setJPEGEncodeParam(param);
|
||||
encoder.encode(bimage);
|
||||
output.flush();
|
||||
output.close();
|
||||
// FileOutputStream fo = new FileOutputStream(new File(path));
|
||||
// out.writeTo(fo);
|
||||
return true;
|
||||
ImageWriter writer = null;
|
||||
Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpeg");
|
||||
if (iter.hasNext()) {
|
||||
writer = iter.next();
|
||||
|
||||
} else {
|
||||
return javax.imageio.ImageIO.write(bimage, extension, file);
|
||||
// Set JPEG quality to 90% with baseline optimization. Setting this
|
||||
// to 1 was a huge jump (about triple the size), so this seems good.
|
||||
// Oddly, a smaller file size than Photoshop at 90%, but it's a
|
||||
// completely different algorithm, I suppose.
|
||||
ImageWriteParam param = writer.getDefaultWriteParam();
|
||||
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
|
||||
param.setCompressionQuality(0.9f);
|
||||
|
||||
BufferedOutputStream output =
|
||||
new BufferedOutputStream(new FileOutputStream(file));
|
||||
writer.setOutput(ImageIO.createImageOutputStream(output));
|
||||
writer.write(null, new IIOImage(bimage, null, null), param);
|
||||
writer.dispose();
|
||||
|
||||
output.flush();
|
||||
output.close();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
// If iter.hasNext() somehow fails up top, it falls through to here
|
||||
return javax.imageio.ImageIO.write(bimage, extension, file);
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
|
||||
@@ -84,9 +84,15 @@ X set quality level higher when exporting JPEG images
|
||||
o add ability to control jpeg compression level with save() and saveFrame()
|
||||
o or just write a better example for this one?
|
||||
X http://code.google.com/p/processing/issues/detail?id=58
|
||||
X using com.sun.image.codec.jpeg.* in PImage causes problems with OpenJDK
|
||||
X http://code.google.com/p/processing/issues/detail?id=1424
|
||||
X fix problem with background() and the default composite (for Casey)
|
||||
X also store the default composite instance and use that on blendMode(BLEND)
|
||||
|
||||
_ implement mousePressed(Event) etc
|
||||
_ better to do this instead of bringing back the magic event
|
||||
_ add mouse wheel support to 2.0 event system
|
||||
_ http://code.google.com/p/processing/issues/detail?id=1423
|
||||
|
||||
andres (cleanup)
|
||||
A when turning smoothing on, internal lines of shapes are visible
|
||||
|
||||
Reference in New Issue
Block a user