MCPcopy Create free account
hub / github.com/AstroImageJ/astroimagej / saveAsJpeg

Method saveAsJpeg

ij/src/main/java/ij/plugin/JpegWriter.java:49–143  ·  view source on GitHub ↗
(ImagePlus imp, String path, int quality)

Source from the content-addressed store, hash-verified

47 }
48
49 String saveAsJpeg(ImagePlus imp, String path, int quality) {
50 int width = imp.getWidth();
51 int height = imp.getHeight();
52 int biType = BufferedImage.TYPE_INT_RGB;
53 boolean overlay = imp.getOverlay()!=null && !imp.getHideOverlay();
54 ImageProcessor ip = imp.getProcessor();
55 if (ip.isDefaultLut() && !imp.isComposite() && !overlay && !ip.isThreshold())
56 biType = BufferedImage.TYPE_BYTE_GRAY;
57 BufferedImage bi = new BufferedImage(width, height, biType);
58 String error = null;
59 try {
60 Graphics g = bi.createGraphics();
61 Image img = imp.getImage();
62 if (overlay && !imp.tempOverlay())
63 img = imp.flatten().getImage();
64 g.drawImage(img, 0, 0, null);
65 g.dispose();
66 Iterator iter = ImageIO.getImageWritersByFormatName("jpeg");
67 ImageWriter writer = (ImageWriter)iter.next();
68 File f = new File(path);
69 String originalPath = null;
70 boolean replacing = f.exists();
71 if (replacing) {
72 originalPath = path;
73 path += ".temp";
74 f = new File(path);
75 }
76 ImageOutputStream ios = ImageIO.createImageOutputStream(f);
77 writer.setOutput(ios);
78 ImageWriteParam param = writer.getDefaultWriteParam();
79 param.setCompressionMode(param.MODE_EXPLICIT);
80 param.setCompressionQuality(quality/100f);
81 if (quality == 100)
82 param.setSourceSubsampling(1, 1, 0, 0);
83 IIOImage iioImage = null;
84 boolean disableSubsampling = quality>=90;
85 if (chromaSubsamplingSet)
86 disableSubsampling = disableChromaSubsampling;
87 if (!disableSubsampling) // Use chroma subsampling YUV420
88 iioImage = new IIOImage(bi, null, null);
89 else {
90 // Disable JPEG chroma subsampling
91 // http://svn.apache.org/repos/asf/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/image/BaseOptimizer.java
92 // http://svn.apache.org/repos/asf/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/image/JpegImageUtils.java
93 // Peter Haub, Okt. 2019
94 IIOMetadata metadata = writer.getDefaultImageMetadata(new ImageTypeSpecifier(bi.getColorModel(), bi.getSampleModel()), param);
95 Node rootNode = metadata!=null ? metadata.getAsTree("javax_imageio_jpeg_image_1.0") : null;
96 boolean metadataUpdated = false;
97 // The top level root node has two children, out of which the second one will
98 // contain all the information related to image markers.
99 if (rootNode!=null && rootNode.getLastChild() != null) {
100 Node markerNode = rootNode.getLastChild();
101 NodeList markers = markerNode.getChildNodes();
102 // Search for 'SOF' marker where subsampling information is stored.
103 for (int i = 0; i < markers.getLength(); i++) {
104 Node node = markers.item(i);
105 // 'SOF' marker can have
106 // 1 child node if the color representation is greyscale,

Callers 3

runMethod · 0.95
saveMethod · 0.45
runMethod · 0.45

Calls 15

isDefaultLutMethod · 0.95
isThresholdMethod · 0.95
writeMethod · 0.95
errorMethod · 0.95
getHideOverlayMethod · 0.80
isCompositeMethod · 0.80
tempOverlayMethod · 0.80
setOutputMethod · 0.80
getImageMethod · 0.65
nextMethod · 0.65
closeMethod · 0.65
getWidthMethod · 0.45

Tested by

no test coverage detected