1 /******************************************************************************* 2 * Copyright 2011 See AUTHORS file. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 ******************************************************************************/ 16 17 package com.badlogic.gdx.tools.etc1; 18 19 import java.io.File; 20 import java.util.ArrayList; 21 22 import com.badlogic.gdx.files.FileHandle; 23 import com.badlogic.gdx.graphics.Pixmap; 24 import com.badlogic.gdx.graphics.Pixmap.Format; 25 import com.badlogic.gdx.graphics.glutils.ETC1; 26 import com.badlogic.gdx.tools.FileProcessor; 27 import com.badlogic.gdx.utils.GdxNativesLoader; 28 29 public class ETC1Compressor { 30 static class ETC1FileProcessor extends FileProcessor { 31 ETC1FileProcessor () { 32 addInputSuffix(".png"); 33 addInputSuffix(".jpg"); 34 addInputSuffix(".jpeg"); 35 addInputSuffix(".bmp"); 36 setOutputSuffix(".etc1"); 37 } 38 39 @Override 40 protected void processFile (Entry entry) throws Exception { 41 System.out.println("Processing " + entry.inputFile); 42 Pixmap pixmap = new Pixmap(new FileHandle(entry.inputFile)); 43 if (pixmap.getFormat() != Format.RGB888 && pixmap.getFormat() != Format.RGB565) { 44 System.out.println("Converting from " + pixmap.getFormat() + " to RGB888!"); 45 Pixmap tmp = new Pixmap(pixmap.getWidth(), pixmap.getHeight(), Format.RGB888); 46 tmp.drawPixmap(pixmap, 0, 0, 0, 0, pixmap.getWidth(), pixmap.getHeight()); 47 pixmap.dispose(); 48 pixmap = tmp; 49 } 50 ETC1.encodeImagePKM(pixmap).write(new FileHandle(entry.outputFile)); 51 pixmap.dispose(); 52 } 53 54 @Override 55 protected void processDir (Entry entryDir, ArrayList<Entry> value) throws Exception { 56 if (!entryDir.outputDir.exists()) { 57 if (!entryDir.outputDir.mkdirs()) 58 throw new Exception("Couldn't create output directory '" + entryDir.outputDir + "'"); 59 } 60 } 61 } 62 63 public static void process (String inputDirectory, String outputDirectory, boolean recursive, boolean flatten) 64 throws Exception { 65 GdxNativesLoader.load(); 66 ETC1FileProcessor processor = new ETC1FileProcessor(); 67 processor.setRecursive(recursive); 68 processor.setFlattenOutput(flatten); 69 processor.process(new File(inputDirectory), new File(outputDirectory)); 70 } 71 72 public static void main (String[] args) throws Exception { 73 if (args.length != 2) { 74 System.out.println("ETC1Compressor <input-dir> <output-dir>"); 75 System.exit(-1); 76 } 77 ETC1Compressor.process(args[0], args[1], true, false); 78 } 79 } 80