<commit_before>/*******************************************************************************
 * Copyright 2011 See AUTHORS file.
 * 
 * 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.
 ******************************************************************************/

package com.badlogic.gdx.tools.skins;

import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.Writer;

import javax.imageio.ImageIO;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.NinePatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData;
import com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Page;
import com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Region;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.tools.FileProcessor;
import com.badlogic.gdx.tools.imagepacker.TexturePacker;
import com.badlogic.gdx.tools.imagepacker.TexturePacker.Settings;
import com.badlogic.gdx.utils.GdxRuntimeException;
import com.badlogic.gdx.utils.Json;
import com.badlogic.gdx.utils.ObjectMap;

public class SkinPacker {
	static public void process (Settings settings, final File inputDir, final File skinFile, final File imageFile)
		throws Exception {
		Texture.setEnforcePotImages(false);

		final File packedDir = new File("temp-packed");
		new FileHandle(packedDir).deleteDirectory();

		imageFile.delete();

		final ObjectMap<String, int[]> nameToSplits = new ObjectMap();

		final TexturePacker texturePacker = new TexturePacker(settings);

		FileProcessor regionProcessor = new FileProcessor() {
			protected void processFile (InputFile inputFile) throws Exception {
				texturePacker.addImage(ImageIO.read(inputFile.inputFile), inputFile.outputFile.getName());
			}
		};
		regionProcessor.setRecursive(false);
		regionProcessor.setInputFilter(new FilenameFilter() {
			public boolean accept (File dir, String name) {
				return name.endsWith(".png") && !name.endsWith(".9.png");
			}
		});
		regionProcessor.setOutputSuffix("");
		regionProcessor.process(inputDir, inputDir);

		FileProcessor ninePatchProcessor = new FileProcessor() {
			protected void processFile (InputFile inputFile) throws Exception {
				BufferedImage image = ImageIO.read(inputFile.inputFile);
				String name = inputFile.outputFile.getName();
				name = name.substring(0, name.length() - 2);
				image = getSplits(image, name);
				texturePacker.addImage(image, name);
			}

			private BufferedImage getSplits (BufferedImage image, String name) {
				WritableRaster raster = image.getRaster();
				int[] rgba = new int[4];

				int startX = 1;
				for (int x = 1; x < raster.getWidth() - 1; x++) {
					raster.getPixel(x, 0, rgba);
					if (rgba[3] == 0) continue;
					if (rgba[0] != 0 || rgba[1] != 0 || rgba[2] != 0)
						throw new RuntimeException("Unknown pixel:" + x + ",0: " + name);
					startX = x;
					break;
				}
				int endX;
				for (endX = startX; endX < raster.getWidth() - 1; endX++) {
					raster.getPixel(endX, 0, rgba);
					if (rgba[3] == 0) break;
					if (rgba[0] != 0 || rgba[1] != 0 || rgba[2] != 0)
						throw new RuntimeException("Unknown pixel " + endX + ",0: " + name);
				}
				for (int x = endX + 1; x < raster.getWidth() - 1; x++) {
					raster.getPixel(x, 0, rgba);
					if (rgba[3] != 0) throw new RuntimeException("Unknown pixel " + x + ",0: " + name);
				}

				int startY = 1;
				for (int y = 1; y < raster.getHeight() - 1; y++) {
					raster.getPixel(0, y, rgba);
					if (rgba[3] == 0) continue;
					if (rgba[0] != 0 || rgba[1] != 0 || rgba[2] != 0)
						throw new RuntimeException("Unknown pixel: 0," + y + ": " + name);
					startY = y;
					break;
				}
				int endY;
				for (endY = startY; endY < raster.getHeight() - 1; endY++) {
					raster.getPixel(0, endY, rgba);
					if (rgba[3] == 0) break;
					if (rgba[0] != 0 || rgba[1] != 0 || rgba[2] != 0)
						throw new RuntimeException("Unknown pixel 0," + endY + ": " + name);
				}
				for (int y = endY + 1; y < raster.getHeight() - 1; y++) {
					raster.getPixel(0, y, rgba);
					if (rgba[3] != 0) throw new RuntimeException("Unknown pixel 0," + y + ": " + name);
				}

				int[] splits = new int[4];
				splits[0] = startX - 1;
				splits[1] = endX - 1;
				splits[2] = startY - 1;
				splits[3] = endY - 1;<commit_msg>[updated] SKin packer to handle ninpatches with single region.<commit_after>/*******************************************************************************
 * Copyright 2011 See AUTHORS file.
 * 
 * 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.
 ******************************************************************************/

package com.badlogic.gdx.tools.skins;

import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.Writer;

import javax.imageio.ImageIO;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.NinePatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData;
import com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Page;
import com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Region;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.tools.FileProcessor;
import com.badlogic.gdx.tools.imagepacker.TexturePacker;
import com.badlogic.gdx.tools.imagepacker.TexturePacker.Settings;
import com.badlogic.gdx.utils.GdxRuntimeException;
import com.badlogic.gdx.utils.Json;
import com.badlogic.gdx.utils.ObjectMap;

public class SkinPacker {
	static public void process (Settings settings, final File inputDir, final File skinFile, final File imageFile)
		throws Exception {
		Texture.setEnforcePotImages(false);

		final File packedDir = new File("temp-packed");
		new FileHandle(packedDir).deleteDirectory();

		imageFile.delete();

		final ObjectMap<String, int[]> nameToSplits = new ObjectMap();

		final TexturePacker texturePacker = new TexturePacker(settings);

		FileProcessor regionProcessor = new FileProcessor() {
			protected void processFile (InputFile inputFile) throws Exception {
				texturePacker.addImage(ImageIO.read(inputFile.inputFile), inputFile.outputFile.getName());
			}
		};
		regionProcessor.setRecursive(false);
		regionProcessor.setInputFilter(new FilenameFilter() {
			public boolean accept (File dir, String name) {
				return name.endsWith(".png") && !name.endsWith(".9.png");
			}
		});
		regionProcessor.setOutputSuffix("");
		regionProcessor.process(inputDir, inputDir);

		FileProcessor ninePatchProcessor = new FileProcessor() {
			protected void processFile (InputFile inputFile) throws Exception {
				BufferedImage image = ImageIO.read(inputFile.inputFile);
				String name = inputFile.outputFile.getName();
				name = name.substring(0, name.length() - 2);
				image = getSplits(image, name);
				texturePacker.addImage(image, name);
			}

			private BufferedImage getSplits (BufferedImage image, String name) {
				WritableRaster raster = image.getRaster();
				int[] rgba = new int[4];

				int startX = 1;
				for (int x = 1; x < raster.getWidth() - 1; x++) {
					raster.getPixel(x, 0, rgba);
					if (rgba[3] == 0) continue;
					if (rgba[0] != 0 || rgba[1] != 0 || rgba[2] != 0)
						throw new RuntimeException("Unknown pixel:" + x + ",0: " + name);
					startX = x;
					break;
				}
				int endX;
				for (endX = startX; endX < raster.getWidth() - 1; endX++) {
					raster.getPixel(endX, 0, rgba);
					if (rgba[3] == 0) break;
					if (rgba[0] != 0 || rgba[1] != 0 || rgba[2] != 0)
						throw new RuntimeException("Unknown pixel " + endX + ",0: " + name);
				}
				for (int x = endX + 1; x < raster.getWidth() - 1; x++) {
					raster.getPixel(x, 0, rgba);
					if (rgba[3] != 0) throw new RuntimeException("Unknown pixel " + x + ",0: " + name);
				}

				int startY = 1;
				for (int y = 1; y < raster.getHeight() - 1; y++) {
					raster.getPixel(0, y, rgba);
					if (rgba[3] == 0) continue;
					if (rgba[0] != 0 || rgba[1] != 0 || rgba[2] != 0)
						throw new RuntimeException("Unknown pixel: 0," + y + ": " + name);
					startY = y;
					break;
				}
				int endY;
				for (endY = startY; endY < raster.getHeight() - 1; endY++) {
					raster.getPixel(0, endY, rgba);
					if (rgba[3] == 0) break;
					if (rgba[0] != 0 || rgba[1] != 0 || rgba[2] != 0)
						throw new RuntimeException("Unknown pixel 0," + endY + ": " + name);
				}
				for (int y = endY + 1; y < raster.getHeight() - 1; y++) {
					raster.getPixel(0, y, rgba);
					if (rgba[3] != 0) throw new RuntimeException("Unknown pixel 0," + y + ": " + name);
				}

				// No splits, or all splits.
				if (startY == 1 && endY == raster.getHeight()) return image;

				int[] splits = new int[4];
				splits[0] = startX - 1;
				splits[1] = endX - 1;
				splits[2] = startY - 1;
				splits[3] = endY - 1;
{'fraction_non_alphanumeric': 0.16119709118030953, 'fraction_numerical': 0.016315495058735783, 'mean_word_length': 3.8341595313204144, 'pattern_counts': {'":': 6, '<': 17, '<?xml version=': 0, '>': 5, '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': '982765', 'n_tokens_mistral': 4166, 'n_tokens_neox': 3468, 'n_words': 897}