66import java .io .*;
77import java .net .URI ;
88import java .net .URISyntaxException ;
9+ import java .util .HashMap ;
910import java .util .Iterator ;
1011import java .awt .geom .AffineTransform ;
12+ import java .util .Map ;
1113
1214import javax .imageio .IIOImage ;
1315import javax .imageio .ImageIO ;
@@ -495,15 +497,15 @@ static protected PImage loadImageIO(PApplet sketch, String filename) {
495497 // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
496498
497499
498- static public boolean saveImage (PImage image , String path ) {
500+ static public boolean saveImage (PImage image , String path , String ... args ) {
499501 if (saveImageExtensions == null ) {
500502 saveImageExtensions = javax .imageio .ImageIO .getWriterFormatNames ();
501503 }
502504 try {
503505 if (saveImageExtensions != null ) {
504506 for (String saveImageFormat : saveImageExtensions ) {
505507 if (path .endsWith ("." + saveImageFormat )) {
506- if (!saveImageIO (image , path )) {
508+ if (!saveImageIO (image , path , args )) {
507509 System .err .println ("Error while saving image." );
508510 return false ;
509511 }
@@ -530,11 +532,24 @@ static public boolean saveImage(PImage image, String path) {
530532 * To get a list of the supported formats for writing, use: <BR>
531533 * <TT>println(javax.imageio.ImageIO.getReaderFormatNames())</TT>
532534 */
533- static protected boolean saveImageIO (PImage image , String path ) throws IOException {
535+ static protected boolean saveImageIO (PImage image , String path , String ... args ) throws IOException {
534536 try {
535537 int outputFormat = (image .format == ARGB ) ?
536538 BufferedImage .TYPE_INT_ARGB : BufferedImage .TYPE_INT_RGB ;
537539
540+ Map <String , Number > params = new HashMap <>();
541+ params .put ("quality" , 0.9f ); // default JPEG quality
542+ params .put ("dpi" , 100.0 ); // default DPI for PNG
543+
544+ if (args != null ) {
545+ for (String arg : args ) {
546+ if (arg .startsWith ("quality=" )) {
547+ params .put ("quality" , Float .parseFloat (arg .substring (8 )));
548+ } else if (arg .startsWith ("dpi=" )) {
549+ params .put ("dpi" , Double .parseDouble (arg .substring (4 )));
550+ }
551+ }
552+ }
538553
539554 String extension =
540555 path .substring (path .lastIndexOf ('.' ) + 1 ).toLowerCase ();
@@ -563,16 +578,15 @@ static protected boolean saveImageIO(PImage image, String path) throws IOExcepti
563578 // it's a completely different algorithm.
564579 param = writer .getDefaultWriteParam ();
565580 param .setCompressionMode (ImageWriteParam .MODE_EXPLICIT );
566- param .setCompressionQuality (0.9f );
581+ //param.setCompressionQuality(0.9f);
582+ param .setCompressionQuality ((Float ) params .get ("quality" ));
567583 }
568584 }
569585
570586 if (extension .equals ("png" )) {
571587 if ((writer = imageioWriter ("png" )) != null ) {
572588 param = writer .getDefaultWriteParam ();
573- if (false ) {
574- metadata = imageioDPI (writer , param , 100 );
575- }
589+ metadata = imageioDPI (writer , param , (Double ) params .get ("dpi" ));
576590 }
577591 }
578592
0 commit comments