<commit_before>package imj2.tools;

import static java.lang.Math.abs;
import static java.lang.Math.max;
import static java.lang.Math.min;
import static net.sourceforge.aprog.tools.Tools.debugError;
import static net.sourceforge.aprog.tools.Tools.debugPrint;

import imj2.core.Image.Channels;
import imj2.core.Image.Monochannel;
import imj2.core.Image2D;
import imj2.core.LinearPackedGrayImage;
import imj2.core.TiledImage2D;
import imj2.tools.IMJTools.TileProcessor;

import java.io.Serializable;
import java.util.Date;
import java.util.concurrent.atomic.AtomicLong;

import net.sourceforge.aprog.tools.IllegalInstantiationException;
import net.sourceforge.aprog.tools.TicToc;

/**
 * @author codistmonk (creation 2014-05-03)
 */
public final class InteractiveClassifier {
	
	private InteractiveClassifier() {
		throw new IllegalInstantiationException();
	}
	
	/**
	 * @param commandLineArguments
	 * <br>Unused
	 */
	public static final void main(final String[] commandLineArguments) {
		{
			final TicToc timer = new TicToc();
			
			debugPrint("Loading image started...", new Date(timer.tic()));
			
//			final TiledImage2D image = new LociBackedImage("../Libraries/images/svs/SYS08_A10_7414-005.svs");
			final TiledImage2D image = (TiledImage2D) new MultifileImage(
					"../Libraries/images/jpg/SYS08_A10_7414-005", "SYS08_A10_7414-005_lod0", null).getLODImage(6);
			
			debugPrint("Loading image done in", timer.toc(), "ms");
			debugPrint(image.getWidth(), image.getHeight(), image.getPixelCount());
			
			debugPrint("Analyzing image started...", new Date(timer.tic()));
			
			final MarkersBuilder markersBuilder = new MarkersBuilder(image, 32);
			final LinearPackedGrayImage markers = markersBuilder.newEmptyMarkers();
			
			IMJTools.forEachTileIn(image, new TileProcessor() {
				
				@Override
				public final void pixel(final Info info) {
					debugPrint("tile:", info.getTileX(), info.getTileY(), info.getActualTileWidth(), info.getActualTileHeight());
					
					final int xStart = info.getTileX() + info.getPixelXInTile();
					final int yStart = info.getTileY() + info.getPixelYInTile();
					final int xEnd = info.getTileX() + info.getActualTileWidth();
					final int yEnd = info.getTileY() + info.getActualTileHeight();
					
					markersBuilder.setMarkers(markers, xStart, yStart, xEnd, yEnd);
				}
				
				@Override
				public final void endOfTile() {
					// NOP
				}
				
				/**
				 * {@value}.
				 */
				private static final long serialVersionUID = 8075249791734409997L;
				
			});
			
			debugPrint("Analyzing image done in", timer.toc(), "ms");
			
			markersBuilder.check(markers);
		}
	}
	
	/**
	 * @author codistmonk (creation 2014-05-03)
	 */
	public static final class MarkersBuilder implements Serializable {
		
		private final Image2D image;

		private final int w;

		private final int h;
		
		private final int s;

		private final int markersPerRow;

		private final int rows;

		private final long horizontalMarkers;

		private final int markersPerColumn;

		private final int columns;

		private final long verticalMarkers;

		private final long allMarkers;
		
		private final AtomicLong markersDone;
		
		public MarkersBuilder(final Image2D image, final int s) {
			this.image = image;
			
			this.w = image.getWidth();
			this.h = image.getHeight();
			this.s = s;
			
			/*
			 * <-------------------w------------------->
			 * <---s/2--><-----s-----><-----s----->
			 * *---------0---*--------0------*-----0---*
			 * 
			 * <-----------------------w----------------------->
			 * <---s/2--><-----s-----><-----s-----><-----s----->
			 * *---------0---*--------0------*-----0-----------*
			 * 
			 * numberOfMarkers = 1+ceiling((w-s/2)/s)
			 */
			
			this.markersPerRow = 1 + ceiling(this.w - s / 2, s);
			this.rows = 2 + ceiling(this.h - s / 2, s);
			this.horizontalMarkers = (long) this.markersPerRow * this.rows;
			this.markersPerColumn = 1 + ceiling(this.h - s / 2, s);
			this.columns = 2 + ceiling(this.w - s / 2, s);
			this.verticalMarkers = (long) this.markersPerColumn * this.columns;
			this.allMarkers = this.horizontalMarkers + this.verticalMarkers;
			final LinearPackedGrayImage markers = this.newEmptyMarkers();
			this.markersDone = new AtomicLong();
			
			debugPrint("markersPerRow:", this.markersPerRow, "rows:", this.rows
					, "markersPerColumn:", this.markersPerColumn, "columns:", this.columns
					, "markers:", markers.getPixelCount());
		}
		
		public final LinearPackedGrayImage newEmptyMarkers() {
			return new LinearPackedGrayImage(""
					, this.allMarkers, new Monochannel(Long.SIZE - Long.numberOfLeadingZeros(this.s - 1L)));
		}
		
		public final void setMarkers(final LinearPackedGrayImage markers, final int xStart, final int yStart, final int xEnd, final int yEnd) {
			/*
			 * y0 <= s/2 + k s
			 * <- y0 - s/2 <= k s
			 * <- (y0 - s/2)/s <= k
			 * <- k = ceil(y0-s/2,s)
			 */
			
			for (int y = this.s / 2 + this.s * ceiling(yStart - this.s / 2, this.s); y < yEnd; y += this.s) {
				final int row = 1 + ceiling(y - this.s / 2, this.s);
				
				for (int x = this.s / 2 + this.s * ceiling(xStart - this.s / 2, this.s); x < xEnd; x += this.s) {
					final int column = 1 + ceiling(x - this.s / 2, this.s);
					
//					debugPrint("segment:", x, y, row, column);
					
					{<commit_msg>[IMJ][imj2] Refactoring.<commit_after>package imj2.tools;

import static java.lang.Math.abs;
import static java.lang.Math.max;
import static java.lang.Math.min;
import static net.sourceforge.aprog.tools.Tools.debugError;
import static net.sourceforge.aprog.tools.Tools.debugPrint;

import imj2.core.Image.Channels;
import imj2.core.Image.Monochannel;
import imj2.core.Image2D;
import imj2.core.LinearPackedGrayImage;
import imj2.core.TiledImage2D;
import imj2.tools.IMJTools.TileProcessor;

import java.io.Serializable;
import java.util.Date;
import java.util.concurrent.atomic.AtomicLong;

import net.sourceforge.aprog.tools.IllegalInstantiationException;
import net.sourceforge.aprog.tools.TicToc;

/**
 * @author codistmonk (creation 2014-05-03)
 */
public final class InteractiveClassifier {
	
	private InteractiveClassifier() {
		throw new IllegalInstantiationException();
	}
	
	/**
	 * @param commandLineArguments
	 * <br>Unused
	 */
	public static final void main(final String[] commandLineArguments) {
		{
			final TicToc timer = new TicToc();
			
			debugPrint("Loading image started...", new Date(timer.tic()));
			
//			final TiledImage2D image = new LociBackedImage("../Libraries/images/svs/SYS08_A10_7414-005.svs");
			final TiledImage2D image = (TiledImage2D) new MultifileImage(
					"../Libraries/images/jpg/SYS08_A10_7414-005", "SYS08_A10_7414-005_lod0", null).getLODImage(6);
			
			debugPrint("Loading image done in", timer.toc(), "ms");
			debugPrint(image.getWidth(), image.getHeight(), image.getPixelCount());
			
			debugPrint("Analyzing image started...", new Date(timer.tic()));
			
			final MarkersBuilder markersBuilder = new MarkersBuilder(image, 32);
			
			markersBuilder.buildMarkersFor(image);
			
			debugPrint("Analizing image done in", timer.toc(), "ms");
		}
	}
	
	/**
	 * @author codistmonk (creation 2014-05-03)
	 */
	public static final class MarkersBuilder implements Serializable {
		
		private final Image2D image;

		private final int w;

		private final int h;
		
		private final int s;

		private final int markersPerRow;

		private final int rows;

		private final long horizontalMarkers;

		private final int markersPerColumn;

		private final int columns;

		private final long verticalMarkers;

		private final long allMarkers;
		
		private final AtomicLong markersDone;
		
		public MarkersBuilder(final Image2D image, final int s) {
			this.image = image;
			
			this.w = image.getWidth();
			this.h = image.getHeight();
			this.s = s;
			
			/*
			 * <-------------------w------------------->
			 * <---s/2--><-----s-----><-----s----->
			 * *---------0---*--------0------*-----0---*
			 * 
			 * <-----------------------w----------------------->
			 * <---s/2--><-----s-----><-----s-----><-----s----->
			 * *---------0---*--------0------*-----0-----------*
			 * 
			 * numberOfMarkers = 1+ceiling((w-s/2)/s)
			 */
			
			this.markersPerRow = 1 + ceiling(this.w - s / 2, s);
			this.rows = 2 + ceiling(this.h - s / 2, s);
			this.horizontalMarkers = (long) this.markersPerRow * this.rows;
			this.markersPerColumn = 1 + ceiling(this.h - s / 2, s);
			this.columns = 2 + ceiling(this.w - s / 2, s);
			this.verticalMarkers = (long) this.markersPerColumn * this.columns;
			this.allMarkers = this.horizontalMarkers + this.verticalMarkers;
			final LinearPackedGrayImage markers = this.newEmptyMarkers();
			this.markersDone = new AtomicLong();
			
			debugPrint("markersPerRow:", this.markersPerRow, "rows:", this.rows
					, "markersPerColumn:", this.markersPerColumn, "columns:", this.columns
					, "markers:", markers.getPixelCount());
		}
		
		public final LinearPackedGrayImage buildMarkersFor(final TiledImage2D image) {
			final LinearPackedGrayImage result = this.newEmptyMarkers();
			
			IMJTools.forEachTileIn(image, new TileProcessor() {
				
				@Override
				public final void pixel(final Info info) {
					debugPrint("tile:", info.getTileX(), info.getTileY(), info.getActualTileWidth(), info.getActualTileHeight());
					
					final int xStart = info.getTileX() + info.getPixelXInTile();
					final int yStart = info.getTileY() + info.getPixelYInTile();
					final int xEnd = info.getTileX() + info.getActualTileWidth();
					final int yEnd = info.getTileY() + info.getActualTileHeight();
					
					MarkersBuilder.this.setMarkers(result, xStart, yStart, xEnd, yEnd);
				}
				
				@Override
				public final void endOfTile() {
					// NOP
				}
				
				/**
				 * {@value}.
				 */
				private static final long serialVersionUID = 8075249791734409997L;
				
			});
			
			this.check(result);
			
			return result;
		}
		
		public final LinearPackedGrayImage newEmptyMarkers() {
			return new LinearPackedGrayImage(""
					, this.allMarkers, new Monochannel(Long.SIZE - Long.numberOfLeadingZeros(this.s - 1L)));
		}
		
		public final void setMarkers(final LinearPackedGrayImage markers, final int xStart, final int yStart, final int xEnd, final int yEnd) {
			/*
			 * y0 <= s/2 + k s
			 * <- y0 - s/2 <= k s
			 * <- (y0 - s/2)/s <= k
			 * <- k = ceil(y0-s/2,s)
			 */
			
			for (int y = this.s / 2 + this.s * ceiling(yStart - this.s / 2, this.s); y < yEnd; y += this.s) {
				final int row = 1 + ceiling(y - this.s / 2, this.s);
				
				for (int x = this.s / 2 + this.s * ceiling(xStart - this.s / 2, this.s); x < xEnd; x += this.s) {
					final int column = 1 + ceiling(x - this.s / 2, this.s);
					
//					debugPrint("segment:", x, y, row, column);
					
					{
{'fraction_non_alphanumeric': 0.16783151616811107, 'fraction_numerical': 0.02273786226819495, 'mean_word_length': 2.7122103078519544, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4033942', 'n_tokens_mistral': 4363, 'n_tokens_neox': 3468, 'n_words': 802}