<commit_before>package to.etc.sjit;

import java.awt.*;
import java.awt.image.*;
import java.io.*;

import javax.imageio.*;

import com.sun.image.codec.jpeg.*;

import to.etc.util.*;

/**
 * This static utility class has a lot of image-related helper functions.
 *
 * <p>Title: Mumble Global Libraries - Non-database tools</p>
 * <p>Description: Small tools for Java programs</p>
 * <p>Copyright: Copyright (c) 2002 <NAME>; released under the LGPL licence.</p>
 * <p>Website <a href="http://www.mumble.to/">Mumble</a></p>
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version 1.0
 */
public class ImaTool {
	private ImaTool() {
	}

	/*--------------------------------------------------------------*/
	/*	CODING:	Small helper functions..							*/
	/*--------------------------------------------------------------*/
	/**
	 *	Does a resize of an object with size (sw, sh) to fit within a rectangle
	 *  of (dw, dh) while keeping the same aspect ratio
	 */
	static public Dimension resizeWithAspect(int dw, int dh, int sw, int sh) {
		//-- Exit if already at/below the ar


		//-- Calculate Idx / Odx as fx
		double fx = (double) sw / (double) dw;
		double fy = (double) sh / (double) dh;

		double f = fx / fy;
		if(f >= 1.0) {
			//-- Adjust Y
			dh = (int) (sh / fx + 0.5);
		} else {
			dw = (int) (sw / fy + 0.5);
		}
		return new Dimension(dw, dh);
	}

	static public Dimension resizeWithAspect(Dimension dest, Dimension src) {
		//-- Calculate Idx / Odx as fx
		double fx = (double) src.width / (double) dest.width;
		double fy = (double) src.height / (double) dest.height;

		double f = fx / fy;
		if(f >= 1.0) {
			//-- Adjust Y
			return new Dimension(dest.width, (int) (src.height / fx + 0.5));
			//			dh	= (int)(sh / fx);
		} else {
			return new Dimension((int) (src.width / fy + 0.5), dest.height);
			//			dw	= (int)(sw / fy);
		}
	}


	/*--------------------------------------------------------------*/
	/*	CODING:	Conversion...										*/
	/*--------------------------------------------------------------*/
	/**
	 *	Converts an Image into a same-size buffered image if it's not already a
	 *  BufferedImage.
	 */
	static public BufferedImage makeBuffered(Image i, int bit) {
		if(i instanceof BufferedImage)
			return (BufferedImage) i;

		//-- Generate a buffered thing.
		int iw = i.getWidth(null);
		int ih = i.getHeight(null);
		BufferedImage bi = new BufferedImage(iw, ih, bit);
		Graphics g = bi.getGraphics();
		g.drawImage(i, 0, 0, null);
		g.dispose();
		return bi;
	}


	/**
	 *	Converts an Image into a same-size buffered image if it's not already a
	 *  BufferedImage.
	 */
	static public BufferedImage makeBuffered(Image i) {
		return makeBuffered(i, BufferedImage.TYPE_INT_RGB);
	}

	/*--------------------------------------------------------------*/
	/*	CODING:	PNG loader (temp).									*/
	/*--------------------------------------------------------------*/
	static public BufferedImage loadPNG(InputStream is) throws IOException {
		return ImageIO.read(is);
		//
		//		PngImage	p = new PngImage();
		//		return p.read(is, false);
	}

	/*--------------------------------------------------------------*/
	/*	CODING:	JPEG save and load stuff...							*/
	/*--------------------------------------------------------------*/
	/**
	 *	Loads a JPEG image from a stream. The image is returned as a BufferedImage.
	 */
	static public BufferedImage loadJPEG(InputStream is) throws IOException {
		JPEGImageDecoder jd = JPEGCodec.createJPEGDecoder(is);
		//		JPEGDecodeParam		jdp	= jd.getJPEGDecodeParam();
		BufferedImage bi = jd.decodeAsBufferedImage();
		return bi;
	}


	/**
	 *	Loads a JPEG image from a file. The image is returned as a BufferedImage.
	 */
	static public BufferedImage loadJPEG(File f) throws IOException {
		FileInputStream fis = new FileInputStream(f);
		try {
			return loadJPEG(fis);
		} finally {
			try {
				if(fis != null)
					fis.close();
			} catch(Exception x) {}
		}
	}


	/**
	 *	Saves an image as a JPEG to the stream specified, with the quality
	 *  spec'd. If q is zero the default quality will be used.
	 */
	static public void saveJPEG(BufferedImage bi, OutputStream os, double qf) throws IOException {
		//-- Write a JPEG
		JPEGImageEncoder je = JPEGCodec.createJPEGEncoder(os);
		if(qf != 0.0) {
			JPEGEncodeParam ep = je.getDefaultJPEGEncodeParam(bi);
			ep.setQuality((float) qf, false);
			je.encode(bi, ep);
		} else
			je.encode(bi);
	}


	/**
	 *	Saves an image as a JPEG to the stream specified, with the quality
	 *  spec'd. If q is zero the default quality will be used.
	 */
	static public void saveJPEG(BufferedImage bi, File f, double qf) throws IOException {
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(f);
			saveJPEG(bi, fos, qf);

		} finally {
			try {
				if(fos != null)
					fos.close();<commit_msg>Bugfix: JDK6 has deprecated sun.* code to handle JPEGs. Replace with ImageIO code<commit_after>package to.etc.sjit;

import java.awt.*;
import java.awt.image.*;
import java.io.*;

import javax.imageio.*;

import to.etc.util.*;

/**
 * This static utility class has a lot of image-related helper functions.
 *
 * <p>Title: Mumble Global Libraries - Non-database tools</p>
 * <p>Description: Small tools for Java programs</p>
 * <p>Copyright: Copyright (c) 2002 <NAME>; released under the LGPL licence.</p>
 * <p>Website <a href="http://www.mumble.to/">Mumble</a></p>
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version 1.0
 */
public class ImaTool {
	private ImaTool() {
	}

	/*--------------------------------------------------------------*/
	/*	CODING:	Small helper functions..							*/
	/*--------------------------------------------------------------*/
	/**
	 *	Does a resize of an object with size (sw, sh) to fit within a rectangle
	 *  of (dw, dh) while keeping the same aspect ratio
	 */
	static public Dimension resizeWithAspect(int dw, int dh, int sw, int sh) {
		//-- Exit if already at/below the ar


		//-- Calculate Idx / Odx as fx
		double fx = (double) sw / (double) dw;
		double fy = (double) sh / (double) dh;

		double f = fx / fy;
		if(f >= 1.0) {
			//-- Adjust Y
			dh = (int) (sh / fx + 0.5);
		} else {
			dw = (int) (sw / fy + 0.5);
		}
		return new Dimension(dw, dh);
	}

	static public Dimension resizeWithAspect(Dimension dest, Dimension src) {
		//-- Calculate Idx / Odx as fx
		double fx = (double) src.width / (double) dest.width;
		double fy = (double) src.height / (double) dest.height;

		double f = fx / fy;
		if(f >= 1.0) {
			//-- Adjust Y
			return new Dimension(dest.width, (int) (src.height / fx + 0.5));
			//			dh	= (int)(sh / fx);
		} else {
			return new Dimension((int) (src.width / fy + 0.5), dest.height);
			//			dw	= (int)(sw / fy);
		}
	}


	/*--------------------------------------------------------------*/
	/*	CODING:	Conversion...										*/
	/*--------------------------------------------------------------*/
	/**
	 *	Converts an Image into a same-size buffered image if it's not already a
	 *  BufferedImage.
	 */
	static public BufferedImage makeBuffered(Image i, int bit) {
		if(i instanceof BufferedImage)
			return (BufferedImage) i;

		//-- Generate a buffered thing.
		int iw = i.getWidth(null);
		int ih = i.getHeight(null);
		BufferedImage bi = new BufferedImage(iw, ih, bit);
		Graphics g = bi.getGraphics();
		g.drawImage(i, 0, 0, null);
		g.dispose();
		return bi;
	}


	/**
	 *	Converts an Image into a same-size buffered image if it's not already a
	 *  BufferedImage.
	 */
	static public BufferedImage makeBuffered(Image i) {
		return makeBuffered(i, BufferedImage.TYPE_INT_RGB);
	}

	/*--------------------------------------------------------------*/
	/*	CODING:	PNG loader (temp).									*/
	/*--------------------------------------------------------------*/
	static public BufferedImage loadPNG(InputStream is) throws IOException {
		return ImageIO.read(is);
		//
		//		PngImage	p = new PngImage();
		//		return p.read(is, false);
	}

	/*--------------------------------------------------------------*/
	/*	CODING:	JPEG save and load stuff...							*/
	/*--------------------------------------------------------------*/
	/**
	 *	Loads a JPEG image from a stream. The image is returned as a BufferedImage.
	 */
	static public BufferedImage loadJPEG(InputStream is) throws IOException {
		return ImageIO.read(is);
		//		
		//		JPEGImageDecoder jd = JPEGCodec.createJPEGDecoder(is);
		//		//		JPEGDecodeParam		jdp	= jd.getJPEGDecodeParam();
		//		BufferedImage bi = jd.decodeAsBufferedImage();
		//		return bi;
	}


	/**
	 *	Loads a JPEG image from a file. The image is returned as a BufferedImage.
	 */
	static public BufferedImage loadJPEG(File f) throws IOException {
		FileInputStream fis = new FileInputStream(f);
		try {
			return loadJPEG(fis);
		} finally {
			try {
				if(fis != null)
					fis.close();
			} catch(Exception x) {}
		}
	}


	/**
	 *	Saves an image as a JPEG to the stream specified, with the quality
	 *  spec'd. If q is zero the default quality will be used.
	 */
	static public void saveJPEG(BufferedImage bi, OutputStream os, double qf) throws IOException {
		//-- Write a JPEG
		ImageIO.write(bi, "JPG", os);
		//
		//		ImageIO.
		//		JPEGImageEncoder je = JPEGCodec.createJPEGEncoder(os);
		//		if(qf != 0.0) {
		//			JPEGEncodeParam ep = je.getDefaultJPEGEncodeParam(bi);
		//			ep.setQuality((float) qf, false);
		//			je.encode(bi, ep);
		//		} else
		//			je.encode(bi);
	}


	/**
	 *	Saves an image as a JPEG to the stream specified, with the quality
	 *  spec'd. If q is zero the default quality will be used.
	 */
	static public void saveJPEG(BufferedImage bi, File f, double qf) throws IOException {
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(f);
			saveJPEG(bi, fos, qf);

		} finally {
			try {
				if(fos != null)
					fos.close();
{'fraction_non_alphanumeric': 0.2296220633299285, 'fraction_numerical': 0.004596527068437181, 'mean_word_length': 1.94377630787733, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 37, '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': '7473404', 'n_tokens_mistral': 3565, 'n_tokens_neox': 3159, 'n_words': 957}