<commit_before>/*
 * Copyright (c) 2011-2015, <NAME>. All Rights Reserved.
 *
 * This file is part of BoofCV (http://boofcv.org).
 *
 * Licensed 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 boofcv.gui.image;

import georegression.struct.point.Point2D_F64;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;


/**
 * Simple JPanel for displaying buffered images allows images to be zoomed in and out of
 *
 * @author <NAME>
 */
public class ImageZoomPanel extends JScrollPane {
	// the image being displayed
	protected BufferedImage img;
	protected ImagePanel panel = new ImagePanel();

	protected double scale=1;

	public ImageZoomPanel(final BufferedImage img ) {
		this.img = img;
		setScale(1);
	}

	public ImageZoomPanel() {
		getViewport().setView(panel);
	}

	public void setScale( double scale ) {

		Rectangle r = panel.getVisibleRect();
		double centerX = (r.x + r.width/2.0)/this.scale;
		double centerY = (r.y + r.height/2.0)/this.scale;

		this.scale = scale;
		int w=0,h=0;
		if( img != null ) {
			w = (int)Math.ceil(img.getWidth()*scale);
			h = (int)Math.ceil(img.getHeight()*scale);
		}
		panel.setPreferredSize(new Dimension(w, h));
		getViewport().setView(panel);

		centerView(centerX, centerY);
	}

	public void centerView( double cx , double cy ) {
		Rectangle r = panel.getVisibleRect();
		int x = (int)(cx*scale-r.width/2);
		int y = (int)(cy*scale-r.height/2);

		getHorizontalScrollBar().setValue(x);
		getVerticalScrollBar().setValue(y);
	}


	/**
	 * Change the image being displayed.
	 *
	 * @param image The new image which will be displayed.
	 */
	public void setBufferedImage(BufferedImage image) {
		this.img = image;

		Dimension prev = getPreferredSize();

		int w=0,h=0;
		if( img != null ) {
			w = (int)Math.ceil(img.getWidth()*scale);
			h = (int)Math.ceil(img.getHeight()*scale);
		}

		if( prev.getWidth() != w || prev.getHeight() != h ) {
			panel.setPreferredSize(new Dimension(w,h));
			getViewport().setView(panel);
		}
	}

	public BufferedImage getImage() {
		return img;
	}

	public Point2D_F64 pixelToPoint(int x, int y) {
		Point2D_F64 ret = new Point2D_F64();
		ret.x = x/scale;
		ret.y = y/scale;

		return ret;
	}

	/**
	 * Paint inside the image panel. Useful for overlays
	 */
	protected void paintInPanel(AffineTransform tran,Graphics2D g2 ) {
		// intentionally empty
	}

	protected class ImagePanel extends JPanel
	{
		@Override
		public void paintComponent(Graphics g) {
			super.paintComponent(g);

			if( img == null ) return;

			Graphics2D g2 = (Graphics2D)g;

			AffineTransform tran = AffineTransform.getScaleInstance(scale, scale);
			g2.drawImage(img,tran,null);

			paintInPanel(tran, g2);
		}
	}
}<commit_msg>- added synchronize to fix threading issues<commit_after>/*
 * Copyright (c) 2011-2015, <NAME>. All Rights Reserved.
 *
 * This file is part of BoofCV (http://boofcv.org).
 *
 * Licensed 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 boofcv.gui.image;

import georegression.struct.point.Point2D_F64;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;


/**
 * Simple JPanel for displaying buffered images allows images to be zoomed in and out of
 *
 * @author <NAME>
 */
public class ImageZoomPanel extends JScrollPane {
	// the image being displayed
	protected BufferedImage img;
	protected ImagePanel panel = new ImagePanel();

	protected double scale=1;

	public ImageZoomPanel(final BufferedImage img ) {
		this.img = img;
		setScale(1);
	}

	public ImageZoomPanel() {
		getViewport().setView(panel);
	}

	public synchronized void setScale( double scale ) {

		Rectangle r = panel.getVisibleRect();
		double centerX = (r.x + r.width/2.0)/this.scale;
		double centerY = (r.y + r.height/2.0)/this.scale;

		this.scale = scale;
		int w=0,h=0;
		if( img != null ) {
			w = (int)Math.ceil(img.getWidth()*scale);
			h = (int)Math.ceil(img.getHeight()*scale);
		}
		panel.setPreferredSize(new Dimension(w, h));
		getViewport().setView(panel);

		centerView(centerX, centerY);
	}

	public synchronized void centerView( double cx , double cy ) {
		Rectangle r = panel.getVisibleRect();
		int x = (int)(cx*scale-r.width/2);
		int y = (int)(cy*scale-r.height/2);

		getHorizontalScrollBar().setValue(x);
		getVerticalScrollBar().setValue(y);
	}


	/**
	 * Change the image being displayed.
	 *
	 * @param image The new image which will be displayed.
	 */
	public synchronized void setBufferedImage(BufferedImage image) {
		this.img = image;

		Dimension prev = getPreferredSize();

		int w=0,h=0;
		if( img != null ) {
			w = (int)Math.ceil(img.getWidth()*scale);
			h = (int)Math.ceil(img.getHeight()*scale);
		}

		if( prev.getWidth() != w || prev.getHeight() != h ) {
			panel.setPreferredSize(new Dimension(w,h));
			getViewport().setView(panel);
		}
	}

	public BufferedImage getImage() {
		return img;
	}

	public Point2D_F64 pixelToPoint(int x, int y) {
		Point2D_F64 ret = new Point2D_F64();
		ret.x = x/scale;
		ret.y = y/scale;

		return ret;
	}

	/**
	 * Paint inside the image panel. Useful for overlays
	 */
	protected void paintInPanel(AffineTransform tran,Graphics2D g2 ) {
		// intentionally empty
	}

	protected class ImagePanel extends JPanel
	{
		@Override
		public void paintComponent(Graphics g) {
			super.paintComponent(g);

			if( img == null ) return;

			Graphics2D g2 = (Graphics2D)g;

			synchronized ( ImageZoomPanel.this ) {
				AffineTransform tran = AffineTransform.getScaleInstance(scale, scale);
				g2.drawImage(img, tran, null);

				paintInPanel(tran, g2);
			}
		}
	}
}
{'fraction_non_alphanumeric': 0.12377358490566037, 'fraction_numerical': 0.012981132075471698, 'mean_word_length': 3.729478943611706, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6885286', 'n_tokens_mistral': 2389, 'n_tokens_neox': 2170, 'n_words': 621}