<commit_before>package com.umnhoj.gol;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageTypeSpecifier;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.metadata.IIOInvalidTreeException;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.metadata.IIOMetadataNode;
import javax.imageio.stream.FileImageOutputStream;
import javax.imageio.stream.ImageOutputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.NodeList;

import com.umnhoj.gol.rle.RleFile;
import com.umnhoj.gol.types.Cell;
import com.umnhoj.gol.types.CellSet;

public class GameOfLife implements Runnable {
	private static final String APPLICATION_EXTENSION = "ApplicationExtension";

	private static final String GRAPHIC_CONTROL_EXTENSION = "GraphicControlExtension";

	private static final Logger log = LoggerFactory.getLogger(GameOfLife.class);

	public static final int ZOOM = 2;

	protected int numGenerations;
	protected Path inputGrid;
	protected Path outputGif;

	public static void main(final String[] args) {
		if (args.length != 3) {
			log.error("Incorrect number of arguments, please see ../README.md for details");
			System.exit(1);
		}

		new GameOfLife(Integer.parseInt(args[0]), Paths.get(args[1]), Paths.get(args[2])).run();
	}

	public GameOfLife() {

	}

	public GameOfLife(final int numGenerations, final Path inputGrid, final Path outputGif) {
		this.numGenerations = numGenerations;
		this.inputGrid = inputGrid;
		this.outputGif = outputGif;
	}

	@Override
	public void run() {
		// Parse the RLE file
		final RleFile rleFile = GameOfLife.parseRle(this.inputGrid);

		// Run iterations
		long startTime = System.nanoTime();
		final List<CellSet> generations = this.runIterations(rleFile);
		long endTime = System.nanoTime();

		// Print output GIF
		{
			// Determine bounds
			final Rectangle bounds = computeBounds(generations);

			// Create images
			final List<BufferedImage> images = GameOfLife.createImages(generations, bounds, ZOOM);

			// Create a GIF with the buffered images
			// https://en.wikipedia.org/wiki/GIF
			// http://giflib.sourceforge.net/whatsinagif/bits_and_bytes.html
			{
				final ImageWriter iw = ImageIO.getImageWritersByFormatName("gif").next();
				final ImageWriteParam param = iw.getDefaultWriteParam();
				final ImageTypeSpecifier typeSpecifier = ImageTypeSpecifier.createFromRenderedImage(images.get(0));
				final IIOMetadata metadata = iw.getDefaultImageMetadata(typeSpecifier, param);

				final String nativeMetadataFormatName = metadata.getNativeMetadataFormatName();

				final IIOMetadataNode node = (IIOMetadataNode) metadata.getAsTree(nativeMetadataFormatName);
				{
					final IIOMetadataNode child = getOrCreateNode(node, GRAPHIC_CONTROL_EXTENSION);

					// In hundredth of a second -_-"
					child.setAttribute("delayTime", Integer.toString(2));
				}
				// {
				// final IIOMetadataNode child = getOrCreateNode(node,
				// APPLICATION_EXTENSION);
				// final IIOMetadataNode extension = new
				// IIOMetadataNode("ApplicationExtension");
				// extension.setAttribute("applicationID", "NETSCAPE");
				// extension.setAttribute("authenticationCode", "2.0");
				// extension.setUserObject(new byte[] { 0x1, (byte) (0xFF),
				// (byte) (0xFF) });
				// child.appendChild(extension);
				// }
				try {
					metadata.setFromTree(nativeMetadataFormatName, node);
				} catch (IIOInvalidTreeException e) {
					throw new RuntimeException("Invalid GIF metadata", e);
				}

				final ImageOutputStream output;
				try {
					output = new FileImageOutputStream(this.outputGif.toFile());
				} catch (FileNotFoundException e) {
					throw new RuntimeException(e);
				} catch (IOException e) {
					throw new RuntimeException(e);
				}

				iw.setOutput(output);
				try {
					iw.prepareWriteSequence(null);
				} catch (IOException e) {
					throw new RuntimeException(e);
				}

				for (final BufferedImage image : images) {
					try {
						iw.writeToSequence(new IIOImage(image, null, metadata), param);
					} catch (IOException e) {
						throw new RuntimeException(e);
					}
				}<commit_msg>clean up a bit<commit_after>package com.umnhoj.gol;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageTypeSpecifier;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.metadata.IIOInvalidTreeException;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.metadata.IIOMetadataNode;
import javax.imageio.stream.FileImageOutputStream;
import javax.imageio.stream.ImageOutputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.NodeList;

import com.umnhoj.gol.rle.RleFile;
import com.umnhoj.gol.types.Cell;
import com.umnhoj.gol.types.CellSet;

public class GameOfLife implements Runnable {

	private static final String GRAPHIC_CONTROL_EXTENSION = "GraphicControlExtension";

	private static final Logger log = LoggerFactory.getLogger(GameOfLife.class);

	public static final int ZOOM = 2;

	protected int numGenerations;
	protected Path inputGrid;
	protected Path outputGif;

	public static void main(final String[] args) {
		if (args.length != 3) {
			log.error("Incorrect number of arguments, please see ../README.md for details");
			System.exit(1);
		}

		new GameOfLife(Integer.parseInt(args[0]), Paths.get(args[1]), Paths.get(args[2])).run();
	}

	public GameOfLife() {

	}

	public GameOfLife(final int numGenerations, final Path inputGrid, final Path outputGif) {
		this.numGenerations = numGenerations;
		this.inputGrid = inputGrid;
		this.outputGif = outputGif;
	}

	@Override
	public void run() {
		// Parse the RLE file
		final RleFile rleFile = GameOfLife.parseRle(this.inputGrid);

		// Run iterations
		long startTime = System.nanoTime();
		final List<CellSet> generations = this.runIterations(rleFile);
		long endTime = System.nanoTime();

		// Print output GIF
		{
			// Determine bounds
			final Rectangle bounds = computeBounds(generations);

			// Create images
			final List<BufferedImage> images = GameOfLife.createImages(generations, bounds, ZOOM);

			// Create a GIF with the buffered images
			// https://en.wikipedia.org/wiki/GIF
			// http://giflib.sourceforge.net/whatsinagif/bits_and_bytes.html
			{
				final ImageWriter iw = ImageIO.getImageWritersByFormatName("gif").next();
				final ImageWriteParam param = iw.getDefaultWriteParam();
				final ImageTypeSpecifier typeSpecifier = ImageTypeSpecifier.createFromRenderedImage(images.get(0));
				final IIOMetadata metadata = iw.getDefaultImageMetadata(typeSpecifier, param);

				final String nativeMetadataFormatName = metadata.getNativeMetadataFormatName();

				final IIOMetadataNode node = (IIOMetadataNode) metadata.getAsTree(nativeMetadataFormatName);
				{
					final IIOMetadataNode child = getOrCreateNode(node, GRAPHIC_CONTROL_EXTENSION);

					// In hundredth of a second -_-"
					child.setAttribute("delayTime", Integer.toString(2));
				}
				try {
					metadata.setFromTree(nativeMetadataFormatName, node);
				} catch (IIOInvalidTreeException e) {
					throw new RuntimeException("Invalid GIF metadata", e);
				}

				final ImageOutputStream output;
				try {
					output = new FileImageOutputStream(this.outputGif.toFile());
				} catch (FileNotFoundException e) {
					throw new RuntimeException(e);
				} catch (IOException e) {
					throw new RuntimeException(e);
				}

				iw.setOutput(output);
				try {
					iw.prepareWriteSequence(null);
				} catch (IOException e) {
					throw new RuntimeException(e);
				}

				for (final BufferedImage image : images) {
					try {
						iw.writeToSequence(new IIOImage(image, null, metadata), param);
					} catch (IOException e) {
						throw new RuntimeException(e);
					}
				}
{'fraction_non_alphanumeric': 0.1056411444457315, 'fraction_numerical': 0.0034750376462411676, 'mean_word_length': 4.283965728274174, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, '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': '1558524', 'n_tokens_mistral': 3057, 'n_tokens_neox': 2634, 'n_words': 591}