You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sis.apache.org by de...@apache.org on 2021/05/17 23:17:59 UTC
[sis] branch visual-test updated: Add a simple image viewer.
This is an automated email from the ASF dual-hosted git repository.
desruisseaux pushed a commit to branch visual-test
in repository https://gitbox.apache.org/repos/asf/sis.git
The following commit(s) were added to refs/heads/visual-test by this push:
new 85bc03e Add a simple image viewer.
85bc03e is described below
commit 85bc03ea1f038aade6138ad7fd7109bc1305e3d2
Author: Martin Desruisseaux <ma...@geomatys.com>
AuthorDate: Tue May 18 00:33:30 2021 +0200
Add a simple image viewer.
---
src/main/java/org/apache/sis/swing/ImagePane.java | 108 ++++++++++++++++++++++
1 file changed, 108 insertions(+)
diff --git a/src/main/java/org/apache/sis/swing/ImagePane.java b/src/main/java/org/apache/sis/swing/ImagePane.java
new file mode 100644
index 0000000..4d7c0f9
--- /dev/null
+++ b/src/main/java/org/apache/sis/swing/ImagePane.java
@@ -0,0 +1,108 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sis.swing;
+
+import java.awt.Graphics2D;
+import java.awt.image.RenderedImage;
+import java.awt.geom.AffineTransform;
+import java.awt.geom.Rectangle2D;
+import javax.swing.JFrame;
+import org.opengis.referencing.datum.PixelInCell;
+import org.apache.sis.image.PlanarImage;
+import org.apache.sis.coverage.grid.GridCoverage;
+import org.apache.sis.coverage.grid.GridGeometry;
+import org.apache.sis.referencing.operation.matrix.AffineTransforms2D;
+
+
+/**
+ * Shows a {@link RenderedImage}.
+ *
+ * @author Martin Desruisseaux (Geomatys)
+ * @version 1.1
+ * @since 1.1
+ * @module
+ */
+@SuppressWarnings("serial")
+public class ImagePane extends ZoomPane {
+ /**
+ * The data to show.
+ */
+ private final RenderedImage image;
+
+ /**
+ * The transform from pixel coordinates to "real world" coordinates.
+ */
+ private final AffineTransform gridToCRS;
+
+ /**
+ * Show the given coverage in a panel. This method assumes that the coverage is two-dimensional
+ * and uses an affine "grid to CRS" transform. This is not well verified because this method is
+ * only for quick testing purpose.
+ *
+ * @param coverage the coverage to show.
+ */
+ public static void show(final GridCoverage coverage) {
+ final RenderedImage image = coverage.render(null);
+ final GridGeometry gg = (GridGeometry) image.getProperty(PlanarImage.GRID_GEOMETRY_KEY);
+ show(image, (AffineTransform) gg.getGridToCRS(PixelInCell.CELL_CORNER));
+ }
+
+ /**
+ * Show the given image in a panel.
+ *
+ * @param image the image to show.
+ * @param gridToCRS the transform from pixel coordinates to "real world" coordinates.
+ */
+ public static void show(final RenderedImage image, final AffineTransform gridToCRS) {
+ final JFrame frame = new JFrame("RenderedImage");
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+ frame.add(new ImagePane(image, gridToCRS).createScrollPane());
+ frame.pack();
+ frame.setVisible(true);
+ }
+
+ /**
+ * Creates a new viewer for the given image.
+ *
+ * @param image the image to show.
+ * @param gridToCRS the transform from pixel coordinates to "real world" coordinates.
+ */
+ public ImagePane(final RenderedImage image, final AffineTransform gridToCRS) {
+ this.image = image;
+ this.gridToCRS = gridToCRS;
+ }
+
+ /**
+ * Requests a window of the size of the image to show.
+ */
+ @Override
+ public Rectangle2D getArea() {
+ final Rectangle2D.Double bounds = new Rectangle2D.Double();
+ bounds.width = image.getWidth();
+ bounds.height = image.getHeight();
+ return AffineTransforms2D.transform(gridToCRS, bounds, bounds);
+ }
+
+ /**
+ * Paints the image.
+ */
+ @Override
+ protected void paintComponent(final Graphics2D graphics) {
+ graphics.transform(zoom);
+ graphics.drawRenderedImage(image, gridToCRS);
+ }
+}