<commit_before>package com.ra4king.gameutils;

import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;

/**
 * A class that stores Images.
 * @author <NAME>
 */
public class Art extends Assets<Image> {
	Art() {}
	
	public Image add(Image i, String name) {
		return super.add(name,createCompatibleImage(i));
	}
	
	public Image extract(URL url) throws IOException {
		return ImageIO.read(url);
	}
	
	/**
	 * Splits the image with the specified width and height of each quadrant and adds the pieces to the map.
	 * @param image The image to be split.
	 * @param width The width of each quadrant inside the image.
	 * @param height the height of each quadrant inside the image.
	 * @return The images split from the original image.
	 */
	public Image[][] splitAndAdd(Image image, int width, int height) {
		Image im[][] = split(image,width,height);
		for(Image i[] : im)
			for(Image i2 : i)
				add(i2,"Image"+size());
		return im;
	}
	
	/**
	 * Splits the image with the specified width and height of each quadrant.
	 * @param image The image to be split.
	 * @param width The width of each quadrant inside the image.
	 * @param height The height of each quadrant inside the image.
	 * @return The images split from the original image.
	 */
	public static BufferedImage[][] split(Image image, int width, int height) {
		double r = image.getWidth(null)/(double)width;
		double c = image.getHeight(null)/(double)height;
		
		if(r-(int)r != 0.0 || c-(int)c != 0.0)
			throw new IllegalArgumentException("Image is not evenly divisible.");
		
		BufferedImage newImages[][] = new BufferedImage[image.getHeight(null)/height][image.getWidth(null)/width];
		for(int a = 0; a < newImages.length; a++) {
			for(int b = 0; b < newImages[a].length; b++) {
				newImages[a][b] = createCompatibleImage(width,height);
				Graphics2D g = newImages[a][b].createGraphics();
				g.drawImage(image, 0, 0, width, height, b*width, a*height, b*width+width, a*height+height, null);
				g.dispose();
			}
		}
		return newImages;
	}
	
	/**
	 * Returns a BufferedImage that is compatible with the current display settings. The transparency used is TRANSLUCENT.
	 * @param width The width of the image.
	 * @param height The height of the image.
	 * @return The compatible BufferedImage.
	 */
	public static BufferedImage createCompatibleImage(int width, int height) {
		return createCompatibleImage(width,height,Transparency.TRANSLUCENT);
	}
	
	/**
	 * Returns a BufferedImage that is compatible with the current display settings.
	 * @param width The width of the image.
	 * @param height The height of the image.
	 * @param translucency the translucency of the image. It can be any integer from the java.awt.Transparency class.
	 * @return The compatible BufferedImage.
	 */
	public static BufferedImage createCompatibleImage(int width, int height, int translucency) {
		return GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().createCompatibleImage(width,height,translucency);
	}
	<commit_msg>Using BufferedImage.getSubimage for less memory usage.<commit_after>package com.ra4king.gameutils;

import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;

/**
 * A class that stores Images.
 * @author <NAME>
 */
public class Art extends Assets<Image> {
	Art() {}
	
	public Image add(Image i, String name) {
		return super.add(name,createCompatibleImage(i));
	}
	
	public Image extract(URL url) throws IOException {
		return ImageIO.read(url);
	}
	
	/**
	 * Splits the image with the specified width and height of each quadrant and adds the pieces to the map.
	 * @param image The image to be split.
	 * @param width The width of each quadrant inside the image.
	 * @param height the height of each quadrant inside the image.
	 * @return The images split from the original image.
	 */
	public BufferedImage[][] splitAndAdd(Image image, int width, int height) {
		BufferedImage im[][] = split(image,width,height);
		for(Image i[] : im)
			for(Image i2 : i)
				add(i2,"Image"+size());
		return im;
	}
	
	/**
	 * Splits the image with the specified width and height of each quadrant.
	 * @param image The image to be split.
	 * @param width The width of each quadrant inside the image.
	 * @param height The height of each quadrant inside the image.
	 * @return The images split from the original image.
	 */
	public static BufferedImage[][] split(Image image, int width, int height) {
		double r = image.getWidth(null)/(double)width;
		double c = image.getHeight(null)/(double)height;
		
		if(r-(int)r != 0.0 || c-(int)c != 0.0)
			throw new IllegalArgumentException("Image is not evenly divisible.");
		
		boolean isBI = image instanceof BufferedImage;
		
		if(isBI)
			image = createCompatibleImage(image);
		
		BufferedImage newImages[][] = new BufferedImage[image.getHeight(null)/height][image.getWidth(null)/width];
		for(int a = 0; a < newImages.length; a++) {
			for(int b = 0; b < newImages[a].length; b++) {
				if(isBI) {
					newImages[a][b] = ((BufferedImage)image).getSubimage(b*width, a*height, b*width+width, a*height+height);
				}
				else {
					newImages[a][b] = createCompatibleImage(width,height);
					Graphics2D g = newImages[a][b].createGraphics();
					g.drawImage(image, 0, 0, width, height, b*width, a*height, b*width+width, a*height+height, null);
					g.dispose();
				}
			}
		}
		
		return newImages;
	}
	
	/**
	 * Returns a BufferedImage that is compatible with the current display settings. The transparency used is TRANSLUCENT.
	 * @param width The width of the image.
	 * @param height The height of the image.
	 * @return The compatible BufferedImage.
	 */
	public static BufferedImage createCompatibleImage(int width, int height) {
		return createCompatibleImage(width,height,Transparency.TRANSLUCENT);
	}
	
	/**
	 * Returns a BufferedImage that is compatible with the current display settings.
	 * @param width The width of the image.
	 * @param height The height of the image.
	 * @param translucency the translucency of the image. It can be any integer from the java.awt.Transparency class.
	 * @return The compatible BufferedImage.
	 */
	public static BufferedImage createCompatibleImage(int width, int height, int translucency) {
		return GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().createCompatibleImage(width,height,translucency);
	}
	
{'fraction_non_alphanumeric': 0.10937267173297571, 'fraction_numerical': 0.003874236328416033, 'mean_word_length': 4.139356814701379, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, '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': '2536701', 'n_tokens_mistral': 2174, 'n_tokens_neox': 1947, 'n_words': 674}