MCPcopy Index your code
hub / github.com/benfry/processing4 / saveImageIO

Method saveImageIO

core/src/processing/awt/ShimAWT.java:553–631  ·  view source on GitHub ↗

Use ImageIO functions from Java 1.4 and later to handle image save. Various formats are supported, typically jpeg, png, bmp, and wbmp. To get a list of the supported formats for writing, use: println(javax.imageio.ImageIO.getReaderFormatNames())

(PImage image, String path, String... args)

Source from the content-addressed store, hash-verified

551 * <TT>println(javax.imageio.ImageIO.getReaderFormatNames())</TT>
552 */
553 static protected boolean saveImageIO(PImage image, String path, String... args) throws IOException {
554 try {
555 int outputFormat = (image.format == ARGB) ?
556 BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB;
557
558 Map<String, Number> params = new HashMap<>();
559 params.put("quality", 0.9f); // default JPEG quality
560 params.put("dpi", 100.0); // default DPI for PNG
561
562 if (args != null) {
563 for (String arg : args) {
564 if (arg.startsWith("quality=")) {
565 params.put("quality", Float.parseFloat(arg.substring(8)));
566 } else if (arg.startsWith("dpi=")) {
567 params.put("dpi", Double.parseDouble(arg.substring(4)));
568 }
569 }
570 }
571
572 String extension =
573 path.substring(path.lastIndexOf('.') + 1).toLowerCase();
574
575 // JPEG and BMP images that have an alpha channel set get pretty unhappy.
576 // BMP just doesn't write, and JPEG writes it as a CMYK image.
577 // https://github.com/processing/processing/issues/454
578 if (extension.equals("bmp") || extension.equals("jpg") || extension.equals("jpeg")) {
579 outputFormat = BufferedImage.TYPE_INT_RGB;
580 }
581
582 BufferedImage bimage = new BufferedImage(image.pixelWidth, image.pixelHeight, outputFormat);
583 bimage.setRGB(0, 0, image.pixelWidth, image.pixelHeight, image.pixels, 0, image.pixelWidth);
584
585 File file = new File(path);
586
587 ImageWriter writer = null;
588 ImageWriteParam param = null;
589 IIOMetadata metadata = null;
590
591 if (extension.equals("jpg") || extension.equals("jpeg")) {
592 if ((writer = imageioWriter("jpeg")) != null) {
593 // Set JPEG quality to 90% with baseline optimization. Setting this
594 // to 1 was a huge jump (about triple the size), so this seems good.
595 // Oddly, a smaller file size than Photoshop at 90%, but I suppose
596 // it's a completely different algorithm.
597 param = writer.getDefaultWriteParam();
598 param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
599 //param.setCompressionQuality(0.9f);
600 param.setCompressionQuality((Float) params.get("quality"));
601 }
602 }
603
604 if (extension.equals("png")) {
605 if ((writer = imageioWriter("png")) != null) {
606 param = writer.getDefaultWriteParam();
607 metadata = imageioDPI(writer, param, (Double) params.get("dpi"));
608 }
609 }
610

Callers 1

saveImageMethod · 0.95

Calls 13

imageioWriterMethod · 0.95
imageioDPIMethod · 0.95
createOutputMethod · 0.95
parseFloatMethod · 0.80
getMethod · 0.65
writeMethod · 0.65
putMethod · 0.45
equalsMethod · 0.45
setOutputMethod · 0.45
disposeMethod · 0.45
flushMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected