<commit_before>/*
 * #%L
 * Various plugins for ImageJ.
 * %%
 * Copyright (C) 2010 - 2013 Board of Regents of the University of
 * Wisconsin-Madison.
 * %%
 * 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.
 * #L%
 */

package loci.plugins.stitching;

import ij.IJ;
import ij.ImageJ;
import ij.ImagePlus;
import ij.gui.Roi;
import ij.io.OpenDialog;
import ij.plugin.PlugIn;
import ij.plugin.frame.RoiManager;
import ij.process.ByteProcessor;

import java.awt.Dimension;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import loci.formats.FormatException;
import loci.formats.ImageReader;
import loci.formats.MetadataTools;
import loci.formats.meta.IMetadata;

/**
 * A plugin to create ROIs matching the tile layout of a multi-tile dataset.
 * 
 * @author <NAME>
 */
public class TilesToROIs implements PlugIn {

	// -- PlugIn methods --

	@Override
	public void run(final String arg) {
		final File file = chooseFile();
		if (file == null) return;

		try {
			final ImagePlus imp = vizTiles(file);
			imp.show();
		}
		catch (final Exception e) {
			IJ.handleException(e);
		}
	}

	// -- Main method --

	public static void main(final String... args) {
		new ImageJ();
		IJ.runPlugIn(TilesToROIs.class.getName(), "");
	}

	// -- Helper methods --

	/** Prompts the user to choose a file. */
	private File chooseFile() {
		final OpenDialog od = new OpenDialog("Choose a data file", "");
		final String name = od.getFileName();
		final String dir = od.getDirectory();
		if (name == null || dir == null) {
			// no file selected
			return null;
		}
		final File file = new File(dir, name);
		if (!file.exists()) {
			IJ.error("No such file: " + file);
			return null;
		}
		return file;
	}

	private ImagePlus vizTiles(final File file) throws IOException,
		FormatException
	{
		IJ.showStatus("Reading tile coordinates");
		final List<Pt> coords = readCoords(file);

		IJ.showStatus("Calculating");
		final Pt min = getMinPoint(coords), max = getMaxPoint(coords);
		final Dimension mosaicDim = getMosaicDimensions(min, max);
		if (mosaicDim == null) {
			IJ.showStatus("");
			return null;
		}

		IJ.showStatus("Creating image");
		final ImagePlus imp = createImage(file.getName(), mosaicDim);

		IJ.showStatus("Populating tile ROIs");
		buildTileROIs(coords, min);

		IJ.showStatus("");

		return imp;
	}

	private List<Pt> readCoords(final File file) throws FormatException,
		IOException
	{
		final ImageReader in = new ImageReader();
		final IMetadata omeMeta = MetadataTools.createOMEXMLMetadata();
		in.setMetadataStore(omeMeta);
		in.setId(file.getAbsolutePath());

		final ArrayList<Pt> coords = new ArrayList<Pt>();
		final int imageCount = omeMeta.getImageCount();
		for (int i = 0; i < imageCount; i++) {
			in.setSeries(i);
			final int sizeX = in.getSizeX();
			final int sizeY = in.getSizeY();
			final int planeCount = omeMeta.getPlaneCount(i);
			for (int p = 0; p < planeCount; p++) {
				final Integer c = omeMeta.getPlaneTheC(i, p).getValue();
				final Integer z = omeMeta.getPlaneTheZ(i, p).getValue();
				final Integer t = omeMeta.getPlaneTheT(i, p).getValue();
				final Double posX = omeMeta.getPlanePositionX(i, p);
				final Double posY = omeMeta.getPlanePositionY(i, p);
				final Double posZ = omeMeta.getPlanePositionZ(i, p);
				coords.add(new Pt(i, p, c, z, t, sizeX, sizeY, posX, posY, posZ, t));
			}
			try {
				final Double labelX = omeMeta.getStageLabelX(i);
				final Double labelY = omeMeta.getStageLabelY(i);
				final Double labelZ = omeMeta.getStageLabelZ(i);
				coords.add(new Pt(i, sizeX, sizeY, labelX, labelY, labelZ));
			}
			catch (final NullPointerException exc) {
				// HACK: Workaround for bug in loci:ome-xml:4.4.8.
			}
		}
		in.close();

		return coords;
	}

	private Pt getMinPoint(final List<Pt> coords) {
		final Pt min = new Pt();
		min.x = Double.POSITIVE_INFINITY;
		min.y = Double.POSITIVE_INFINITY;<commit_msg>TilesToROIs: use TileUtils helper methods<commit_after>/*
 * #%L
 * Various plugins for ImageJ.
 * %%
 * Copyright (C) 2010 - 2013 Board of Regents of the University of
 * Wisconsin-Madison.
 * %%
 * 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.
 * #L%
 */

package loci.plugins.stitching;

import ij.IJ;
import ij.ImageJ;
import ij.ImagePlus;
import ij.gui.Roi;
import ij.plugin.PlugIn;
import ij.plugin.frame.RoiManager;
import ij.process.ByteProcessor;

import java.awt.Dimension;
import java.io.File;
import java.io.IOException;
import java.util.List;

import loci.formats.FormatException;
import loci.formats.IFormatReader;
import loci.formats.meta.IMetadata;

/**
 * A plugin to create ROIs matching the tile layout of a multi-tile dataset.
 * 
 * @author <NAME>
 */
public class TilesToROIs implements PlugIn {

	// -- PlugIn methods --

	@Override
	public void run(final String arg) {
		final File file = TileUtils.chooseFile();
		if (file == null) return;

		try {
			final ImagePlus imp = vizTiles(file);
			imp.show();
		}
		catch (final Exception e) {
			IJ.handleException(e);
		}
	}

	// -- Main method --

	public static void main(final String... args) {
		new ImageJ().exitWhenQuitting(true);
		IJ.runPlugIn(TilesToROIs.class.getName(), "");
	}

	// -- Helper methods --

	private ImagePlus vizTiles(final File file) throws IOException,
		FormatException
	{
		IJ.showStatus("Initializing dataset");
		final IFormatReader in = TileUtils.initializeReader(file);
		final IMetadata meta = (IMetadata) in.getMetadataStore();
		in.close();

		IJ.showStatus("Reading tile coordinates");
		final List<Pt> coords = TileUtils.readCoords(meta);

		IJ.showStatus("Calculating");
		final Pt min = getMinPoint(coords), max = getMaxPoint(coords);
		final Dimension mosaicDim = getMosaicDimensions(min, max);
		if (mosaicDim == null) {
			IJ.showStatus("");
			return null;
		}

		IJ.showStatus("Creating image");
		final ImagePlus imp = createImage(file.getName(), mosaicDim);

		IJ.showStatus("Populating tile ROIs");
		buildTileROIs(coords, min);

		IJ.showStatus("");

		return imp;
	}

	private Pt getMinPoint(final List<Pt> coords) {
		final Pt min = new Pt();
		min.x = Double.POSITIVE_INFINITY;
		min.y = Double.POSITIVE_INFINITY;
{'fraction_non_alphanumeric': 0.12057922584238373, 'fraction_numerical': 0.00403787245892509, 'mean_word_length': 3.763262599469496, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 1}, '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': '3222440', 'n_tokens_mistral': 2602, 'n_tokens_neox': 2317, 'n_words': 701}