From 34b742403f64f27c0fcaddfd7cf2694d3b6c7d6e Mon Sep 17 00:00:00 2001 From: chaoskagami Date: Wed, 20 Aug 2014 17:54:30 -0400 Subject: [PATCH] One reference to tools may very well have killed conversion of bgs. I'm going another run to test. --- UI/src/nl/weeaboo/krkr/XP3Extractor.java | 104 ++++++------- .../krkr/fate/FateResourceConverter.java | 70 ++++----- .../krkr/fate/RealtaNuaSoundExtractor.java | 38 ++--- UI/src/nl/weeaboo/vnds/VNSoundUtil.java | 36 ++--- .../nl/weeaboo/vnds/tools/ImageConverter.java | 146 +++++++++--------- .../nl/weeaboo/vnds/tools/SoundConverter.java | 78 +++++----- 6 files changed, 236 insertions(+), 236 deletions(-) diff --git a/UI/src/nl/weeaboo/krkr/XP3Extractor.java b/UI/src/nl/weeaboo/krkr/XP3Extractor.java index 2fe1701..4d33036 100644 --- a/UI/src/nl/weeaboo/krkr/XP3Extractor.java +++ b/UI/src/nl/weeaboo/krkr/XP3Extractor.java @@ -22,10 +22,10 @@ public class XP3Extractor { //Temporary buffers for unzip private static final byte infBuffer[] = new byte[64 * 1024]; private static final byte readBuffer[] = new byte[64 * 1024]; - - public XP3Extractor() { + + public XP3Extractor() { } - + //Functions public static final int read_s32(InputStream in) throws IOException { return (int)readLE(in, 4); @@ -40,20 +40,20 @@ public class XP3Extractor { } return result; } - + /** * Warning: dst should use ascii-only in its pathname */ public void extract(String archive, String dst, ProgressListener pl) throws IOException { Log.v("Extracting " + archive); - + FileInputStream fin = new FileInputStream(archive); FileChannel fc = fin.getChannel(); - + int origSize; File uncompressedFile = new File(dst+"/__temp__.dat"); uncompressedFile.getParentFile().mkdirs(); - + { byte signature[] = new byte[] {(byte)'X', (byte)'P', (byte)'3', (byte)0x0D, (byte)0x0A, (byte) ' ', (byte)0x0A, @@ -65,18 +65,18 @@ public class XP3Extractor { throw new IOException("FileFormat error"); } } - + int indexOffset = (int)read_s64(fin); if (indexOffset > fc.size()) throw new IOException("FileFormat error"); fc.position(indexOffset); - + boolean compression = readLE(fin, 1) != 0; if (compression) { int compSize = (int)read_s64(fin); origSize = (int)read_s64(fin); - + if (indexOffset+compSize+17 != fc.size()) throw new IOException("FileFormat error"); - + int uncompressedL = -1; BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(uncompressedFile)); try { @@ -84,7 +84,7 @@ public class XP3Extractor { } finally { bout.close(); } - + if (uncompressedL != origSize) throw new IOException("FileFormat error"); } else { origSize = (int)read_s64(fin); @@ -102,7 +102,7 @@ public class XP3Extractor { FileInputStream uncompressedIn = new FileInputStream(uncompressedFile); FileChannel uncompressedC = uncompressedIn.getChannel(); - + byte out[] = new byte[1024 * 1024]; int outL = 0; @@ -110,20 +110,20 @@ public class XP3Extractor { int read = 0; while (uncompressedC.position() < origSize) { Entry entry = readEntry(uncompressedIn); - + File outFile = new File(String.format("%s/%s", dst, entry.file)); outFile.getParentFile().mkdirs(); outL = 0; t++; - - if (pl != null && (t & 0xFF) == 0) { + + if (pl != null && (t & 0xFF) == 0) { pl.onProgress(read, (int)fc.size(), ""); } - + //Log.verbose("[write] " + outFile.getAbsolutePath()); //Benchmark.tick(); - + //Write segments to seperate files int totalSize = 0; for (Segment segment : entry.segments) { @@ -132,7 +132,7 @@ public class XP3Extractor { if (out.length < totalSize) { out = new byte[totalSize]; } - + for (Segment segment : entry.segments) { fc.position(segment.offset); @@ -144,12 +144,12 @@ public class XP3Extractor { read += segment.compSize; } - - //Decrypt + + //Decrypt if (entry.encrypted) { decrypt(outFile.getName(), out, outL); } - + try { if (outFile.getName().endsWith(".tlg")) { if (out.length >= 2 && out[0] == 'B' && out[1] == 'M') { @@ -164,15 +164,15 @@ public class XP3Extractor { Process p = ProcessUtil.execInDir( String.format("tlg2bmp \"%s\" \"%s\"", tlgTemp, bmpTemp), - "tools/"); + ""); ProcessUtil.waitFor(p); ProcessUtil.kill(p); - + outFile.delete(); new File(tlgTemp).delete(); new File(bmpTemp).renameTo(new File(StringUtil.stripExtension(outFile.getAbsolutePath())+".bmp")); */ - + FileUtil.writeBytes(outFile, out, 0, outL); } } else { @@ -184,10 +184,10 @@ public class XP3Extractor { Log.w(ioe.toString()); } } - + //Benchmark.tock(outFile.getName() + " %s"); } - + uncompressedC.close(); uncompressedIn.close(); @@ -195,13 +195,13 @@ public class XP3Extractor { fin.close(); uncompressedFile.delete(); - + if (pl != null) pl.onFinished(archive + " fully extracted"); } - + static synchronized int unzip(InputStream in, byte out[], int inL) throws IOException { Inflater inf = new Inflater(); - + int read = 0; int inflated = 0; try { @@ -220,7 +220,7 @@ public class XP3Extractor { if (r == -1) { throw new EOFException("Unexpected end of ZLIB input stream"); } - read += r; + read += r; inf.setInput(readBuffer, 0, r); } } @@ -228,10 +228,10 @@ public class XP3Extractor { throw new IOException(e); } } - + static synchronized int unzip(InputStream in, OutputStream out, int inL) throws IOException { Inflater inf = new Inflater(); - + int read = 0; int inflated = 0; try { @@ -251,7 +251,7 @@ public class XP3Extractor { if (r == -1) { throw new EOFException("Unexpected end of ZIP input stream"); } - read += r; + read += r; inf.setInput(readBuffer, 0, r); } } @@ -259,35 +259,35 @@ public class XP3Extractor { throw new IOException(e); } } - + @SuppressWarnings("unused") protected Entry readEntry(InputStream in) throws IOException { Entry entry = new Entry(); - + byte temp[] = new byte[4]; - + in.read(temp); if (!new String(temp).equals("File")) throw new IOException("FileFormat error :: " + new String(temp)); int entryLength = (int)read_s64(in); - + in.read(temp); if (!new String(temp).equals("info")) throw new IOException("FileFormat error"); int infoLength = (int)read_s64(in); - + entry.encrypted = read_s32(in) != 0; int origSize = (int)read_s64(in); int compSize = (int)read_s64(in); - + int filenameL = (int)readLE(in, 2); - //System.err.println(origSize + " " + compSize + " " + new String(temp) + " " + filenameL); + //System.err.println(origSize + " " + compSize + " " + new String(temp) + " " + filenameL); if (infoLength != filenameL*2+22) throw new IOException("FileFormat error"); - + char filename[] = new char[filenameL]; for (int n = 0; n < filenameL; n++) { filename[n] = (char)readLE(in, 2); } - entry.file = new String(filename); - + entry.file = new String(filename); + in.read(temp); if (!new String(temp).equals("segm")) throw new IOException("FileFormat error"); int numSegments = ((int)read_s64(in)) / 28; @@ -298,7 +298,7 @@ public class XP3Extractor { s.offset = (int)read_s64(in); s.origSize = (int)read_s64(in); s.compSize = (int)read_s64(in); - + entry.segments[n] = s; } @@ -309,12 +309,12 @@ public class XP3Extractor { return entry; } - + public void decrypt(String filename, byte data[], int dataL) { if (dataL > 0x13) { data[0x13] ^= 1; } - if (dataL > 0x2ea29) { + if (dataL > 0x2ea29) { data[0x2ea29] ^= 3; } @@ -322,11 +322,11 @@ public class XP3Extractor { data[n] ^= 0x36; } } - + //Getters - + //Setters - + //Inner Classes private static class Entry { public String file; @@ -337,7 +337,7 @@ public class XP3Extractor { public boolean compressed; public int offset; public int origSize; - public int compSize; + public int compSize; } - + } diff --git a/UI/src/nl/weeaboo/krkr/fate/FateResourceConverter.java b/UI/src/nl/weeaboo/krkr/fate/FateResourceConverter.java index 978b111..fe99435 100644 --- a/UI/src/nl/weeaboo/krkr/fate/FateResourceConverter.java +++ b/UI/src/nl/weeaboo/krkr/fate/FateResourceConverter.java @@ -18,22 +18,22 @@ import nl.weeaboo.vnds.tools.ImageConverter.ScalingType; import nl.weeaboo.vnds.tools.SoundConverter; public class FateResourceConverter extends AbstractResourceConverter { - + public FateResourceConverter() { } - + //Functions public static void main(String args[]) { System.setProperty("line.separator", "\n"); - + FateResourceConverter e = new FateResourceConverter(); try { e.parseCommandLine(args, 2); } catch (IOException ioe) { printUsage(e.getClass()); return; - } - + } + try { e.extract(args[0], args[1]); } catch (IOException ioe) { @@ -51,30 +51,30 @@ public class FateResourceConverter extends AbstractResourceConverter { //Extract game data FateExtractor.main(new String[] {src, originalF.getAbsolutePath()}); - + //Clean up _generated folder initOutputFolder(generatedF); - + //Convert convertBackground(dstF); convertForeground(dstF); convertSound(dstF); convertMusic(dstF); - + //Template File templateF = new File("template/fate"); copyTemplate(templateF, generatedF); - + //Done Log.v("Done"); } - - public void convertBackground(final File root) { + + public void convertBackground(final File root) { Log.v("Converting backgrounds..."); - + final ImageConverter ic = createBackgroundConverter(); - + Map files = new HashMap(); FileUtil.collectFiles(files, new File(root, "/_original/bgimage"), false, false, new FileCollectFilter() { public boolean accept(String relpath, File file) { @@ -82,7 +82,7 @@ public class FateResourceConverter extends AbstractResourceConverter { return relpath.endsWith("tlg") || relpath.endsWith("bmp"); } }); - + BatchProcess bp = createBatch(); try { bp.run(files, new FileOp() { @@ -98,13 +98,13 @@ public class FateResourceConverter extends AbstractResourceConverter { Log.w("Batch Process Interrupted"); } } - + public void convertForeground(final File root) { Log.v("Converting sprites..."); - + final ImageConverter ic = createForegroundConverter(); ic.setScalingType(ScalingType.SPRITE); - + Map files = new HashMap(); FileUtil.collectFiles(files, new File(root, "/_original/fgimage"), false, false, new FileCollectFilter() { public boolean accept(String relpath, File file) { @@ -112,7 +112,7 @@ public class FateResourceConverter extends AbstractResourceConverter { return relpath.endsWith("tlg") || relpath.endsWith("bmp"); } }); - + BatchProcess bp = createBatch(); try { bp.run(files, new FileOp() { @@ -131,36 +131,36 @@ public class FateResourceConverter extends AbstractResourceConverter { protected static File convertTLG(File tlgF) throws IOException { String hash = "__" + Long.toHexString(Thread.currentThread().getId()) + "__"; - + File tempTLG = new File(tlgF.getParentFile(), hash + ".tlg"); File tempBMP = new File(tlgF.getParentFile(), hash + ".bmp"); - + File bmpF = new File(StringUtil.replaceExt(tlgF.getAbsolutePath(), "bmp")); bmpF.delete(); - + tlgF.renameTo(tempTLG); - + try { Process p = ProcessUtil.execInDir( String.format("tlg2bmp \"%s\" \"%s\"", tempTLG.getAbsolutePath(), tempBMP.getAbsolutePath()), - "tools/"); + ""); ProcessUtil.waitFor(p); } finally { tempTLG.delete(); - tempBMP.renameTo(bmpF); + tempBMP.renameTo(bmpF); } - + return bmpF; } - + public void convertSound(File root) { final File targetFolder = new File(root, "_generated/sound"); - + try { Log.v("Converting SFX..."); final SoundConverter sc = createSFXEncoder(); - + Map files = new HashMap(); FileUtil.collectFiles(files, new File(root, "/_original/sound"), false, false, new FileCollectFilter() { public boolean accept(String relpath, File file) { @@ -179,11 +179,11 @@ public class FateResourceConverter extends AbstractResourceConverter { } catch (InterruptedException ie) { Log.w("Batch Process Interrupted"); } - - if (convertVoice) { + + if (convertVoice) { Log.v("Converting Voice..."); final SoundConverter sc = createVoiceEncoder(); - + Map files = new HashMap(); FileUtil.collectFiles(files, new File(root, "/_original/patch6"), false, false, new FileCollectFilter() { public boolean accept(String relpath, File file) { @@ -212,8 +212,8 @@ public class FateResourceConverter extends AbstractResourceConverter { Log.v("Converting music..."); final SoundConverter sc = createMusicEncoder(); - - //Convert music + + //Convert music Map files = new HashMap(); FileUtil.collectFiles(files, new File(root, "/_original/bgm"), false, false, new FileCollectFilter() { public boolean accept(String relpath, File file) { @@ -237,7 +237,7 @@ public class FateResourceConverter extends AbstractResourceConverter { } //Getters - + //Setters - + } diff --git a/UI/src/nl/weeaboo/krkr/fate/RealtaNuaSoundExtractor.java b/UI/src/nl/weeaboo/krkr/fate/RealtaNuaSoundExtractor.java index c68e937..ea7df27 100644 --- a/UI/src/nl/weeaboo/krkr/fate/RealtaNuaSoundExtractor.java +++ b/UI/src/nl/weeaboo/krkr/fate/RealtaNuaSoundExtractor.java @@ -14,9 +14,9 @@ import nl.weeaboo.vnds.Log; public class RealtaNuaSoundExtractor { - public RealtaNuaSoundExtractor() { + public RealtaNuaSoundExtractor() { } - + //Functions public static void main(String args[]) { RealtaNuaSoundExtractor se = new RealtaNuaSoundExtractor(); @@ -28,7 +28,7 @@ public class RealtaNuaSoundExtractor { e.printStackTrace(); } } - + public static final int read_s32(InputStream in) throws IOException { return (int)readLE(in, 4); } @@ -46,14 +46,14 @@ public class RealtaNuaSoundExtractor { public void extract(String discRoot, String outFolder) throws IOException { File outFolderFile = new File(outFolder); outFolderFile.mkdirs(); - - File newExe = new File(outFolder+"/ahx2wav.exe"); - FileUtil.copyFile(new File("tools/ahx2wav_v014/ahx2wav.exe"), newExe); + + File newExe = new File("bin/ahx2wav"); + FileUtil.copyFile(new File("bin/ahx2wav"), newExe); FileInputStream fin = new FileInputStream(discRoot+"/data0.bin"); FileChannel fc = fin.getChannel(); - + Log.v("Extracting AHX sound files..."); - + try { byte sig[] = new byte[] {(byte)'A', (byte)'F', (byte)'S', (byte)'\0'}; byte arcsig[] = new byte[4]; @@ -61,7 +61,7 @@ public class RealtaNuaSoundExtractor { for (int n = 0; n < 4; n++) { if (arcsig[n] != sig[n]) throw new IOException("FileFormat Error"); } - + //0x808 -- file offset/size table offset //0x466C3000 -- filename table offset //0x00159660 -- filename table length @@ -77,7 +77,7 @@ public class RealtaNuaSoundExtractor { files[n].offset = 0x800 + read_s32(fin); files[n].length = read_s32(fin); } - + byte nameBuffer[] = new byte[32]; for (int n = 0; n < filesL; n++) { fc.position(0x466C3000 + 0x30 * n); @@ -86,12 +86,12 @@ public class RealtaNuaSoundExtractor { while (l < nameBuffer.length && nameBuffer[l] != '\0') l++; files[n].filename = new String(nameBuffer, 0, l); } - + for (int n = 0; n < filesL; n++) { if (n % 256 == 0) { Log.v(String.format("(%d/%d) %s...", n+1, filesL, files[n].filename)); } - + File outFile = new File(outFolder+'/'+files[n].filename); FileOutputStream fout = new FileOutputStream(outFile); int r = 0; @@ -99,7 +99,7 @@ public class RealtaNuaSoundExtractor { r += fc.transferTo(files[n].offset+r, files[n].length-r, fout.getChannel()); } fout.flush(); - fout.close(); + fout.close(); //Convert to wav Process p = ProcessUtil.execInDir(String.format( @@ -111,12 +111,12 @@ public class RealtaNuaSoundExtractor { throw new IOException("Error converting file: " + outFile.getAbsolutePath() + "\nAborting sound extraction."); } ProcessUtil.kill(p); - + //Delete original - outFile.delete(); + outFile.delete(); } - - //Rename a.b.wav to a.wav + + //Rename a.b.wav to a.wav File converted[] = outFolderFile.listFiles(); for (File f : converted) { String filename = f.getName(); @@ -133,9 +133,9 @@ public class RealtaNuaSoundExtractor { newExe.delete(); } } - + //Getters - + //Setters //Inner Classes diff --git a/UI/src/nl/weeaboo/vnds/VNSoundUtil.java b/UI/src/nl/weeaboo/vnds/VNSoundUtil.java index ce99617..d374261 100644 --- a/UI/src/nl/weeaboo/vnds/VNSoundUtil.java +++ b/UI/src/nl/weeaboo/vnds/VNSoundUtil.java @@ -17,13 +17,13 @@ public final class VNSoundUtil { public static String ffmpeg = "ffmpeg"; - private VNSoundUtil() { + private VNSoundUtil() { } - + public static File padAudioFile(File src, File dst) throws IOException { File tempF = new File(dst.getParent(), stripExtension(dst.getName())+".temp"); tempF.getParentFile().mkdirs(); - + try { //Decode to raw PCM @@ -41,19 +41,19 @@ public final class VNSoundUtil { } ProcessUtil.waitFor(p); ProcessUtil.kill(p); - + { byte[] bytes = FileUtil.readBytes(tempF); boolean isShort = bytes.length <= 10 * (44100*2*2); float fadeSeconds = (isShort ? 1 : 3); float padSeconds = (isShort ? 3 : 0); - + ShortBuffer[] samples = deinterleave(bytes, 2); fadeInOut(samples, Math.round(fadeSeconds * 44100), Math.round(fadeSeconds * 44100)); bytes = interleave(samples); - - //Pad raw PCM with data + + //Pad raw PCM with data FileOutputStream fout = new FileOutputStream(tempF); try { fout.write(bytes); @@ -63,21 +63,21 @@ public final class VNSoundUtil { fout.close(); } } - + //Encode to target format p = ProcessUtil.execInDir(String.format( "ffmpeg -y -f s16le -acodec pcm_s16le -ac 2 -ar 44100 -i \"%s\" \"%s\"", tempF.getAbsolutePath(), dst.getAbsolutePath()), - "tools"); + ""); ProcessUtil.waitFor(p); ProcessUtil.kill(p); } finally { tempF.delete(); } - + return dst; } - + public static void fadeInOut(ShortBuffer[] channels, int inDuration, int outDuration) { for (ShortBuffer ch : channels) { inDuration = Math.min(ch.limit()/2, inDuration); @@ -103,17 +103,17 @@ public final class VNSoundUtil { } } } - + protected static ShortBuffer[] deinterleave(byte[] srcArray, int channels) { ByteBuffer src = ByteBuffer.wrap(srcArray); src.order(ByteOrder.LITTLE_ENDIAN); ShortBuffer ssrc = src.asShortBuffer(); - + ShortBuffer[] dst = new ShortBuffer[channels]; for (int n = 0; n < channels; n++) { dst[n] = ShortBuffer.allocate(ssrc.limit() / channels); } - + while (ssrc.remaining() > 0) { for (ShortBuffer buf : dst) { buf.put(ssrc.get()); @@ -123,7 +123,7 @@ public final class VNSoundUtil { for (ShortBuffer buf : dst) { buf.rewind(); } - + return dst; } @@ -134,11 +134,11 @@ public final class VNSoundUtil { oldpos[n] = src[n].position(); len += src[n].limit(); } - + ByteBuffer dst = ByteBuffer.allocate(2 * len); dst.order(ByteOrder.LITTLE_ENDIAN); ShortBuffer sdst = dst.asShortBuffer(); - + try { while (sdst.remaining() > 0) { for (ShortBuffer buf : src) { @@ -151,7 +151,7 @@ public final class VNSoundUtil { } } dst.rewind(); - + return dst.array(); } diff --git a/UI/src/nl/weeaboo/vnds/tools/ImageConverter.java b/UI/src/nl/weeaboo/vnds/tools/ImageConverter.java index 46b0d1b..d031e70 100644 --- a/UI/src/nl/weeaboo/vnds/tools/ImageConverter.java +++ b/UI/src/nl/weeaboo/vnds/tools/ImageConverter.java @@ -29,7 +29,7 @@ import nl.weeaboo.vnds.ProgressListener; import nl.weeaboo.vnds.VNImageUtil; public class ImageConverter { - + public enum ConvertType { TYPE_RAW_RGBA("RAW RGBA", "dta"), TYPE_RAW_RGB256("RAW RGB256", "dta"), @@ -37,43 +37,43 @@ public class ImageConverter { TYPE_PNG("PNG", "png"), TYPE_PNG_256_NEUQUANT("PNG 256 (neuquant)", "png"), TYPE_PNG_256_MEDIAN("PNG 256 (median)", "png"); - + private String label; private String fileExt; - + private ConvertType(String label, String fileExt) { this.label = label; this.fileExt = fileExt; } - + public String getFileExt() { return fileExt; } public String toString() { return label; } } - + public enum ScalingType { NONE("None"), BACKGROUND("Background"), SPRITE("Sprite"), STRETCH("Stretch"); String label; - + private ScalingType(String label) { this.label = label; } - + public String toString() { return label; } } - + public enum DitheringType { NONE("None"), RANDOM("Random"), FLOYD_STEINBERG("Floyd-Steinberg"); - + String label; - + private DitheringType(String label) { this.label = label; } - + public String toString() { return label; } } - + private int maxThreads = 8; private ConvertType mode = ConvertType.TYPE_RAW_RGBA; private ScalingType scaling = ScalingType.NONE; @@ -82,10 +82,10 @@ public class ImageConverter { private int quality = 98; //JPG Only private DitheringType dithering = DitheringType.NONE; private boolean log; - + //Temporary vars private Map processLogs; - + //Functions protected static void printUsage() { System.err.printf("Usage: java -jar ImageConverter.jar \nflags:" @@ -94,7 +94,7 @@ public class ImageConverter { + "\n\t-png " + "\n\t-jpg " // + "\n\t-size " - + "\n"); + + "\n"); } public static void main(String args[]) throws IOException { @@ -122,12 +122,12 @@ public class ImageConverter { } } else if (args[n].startsWith("-threads")) { ic.maxThreads = Integer.parseInt(args[++n]); -/* +/* } else if (args[n].startsWith("-size")) { int w = Integer.parseInt(args[++n]); int h = Integer.parseInt(args[++n]); ic.setBackgroundSize(w, h); -*/ +*/ } else if (filename == null) { filename = args[n]; } else { @@ -138,12 +138,12 @@ public class ImageConverter { printUsage(); return; } - + if (filename == null) { printUsage(); return; } - + ic.convertFolder(filename, new ProgressListener() { public void onFinished(String message) { System.out.printf("%s\n", message); @@ -153,7 +153,7 @@ public class ImageConverter { } }); } - + public void convertFolder(String folder, final ProgressListener pl) throws IOException { convertFolder(folder, pl, 1); } @@ -169,7 +169,7 @@ public class ImageConverter { } else { files.put(folderF.getName(), folderF); } - + BatchProcess bp = new BatchProcess(); bp.setTaskSize(32); bp.setThreads(maxThreads); @@ -186,20 +186,20 @@ public class ImageConverter { e.printStackTrace(); } } - + public File convertFile(File file) { return convertFile(file, null); } - public File convertFile(File file, File targetFolder) { - String filenameNoExt = file.getName(); + public File convertFile(File file, File targetFolder) { + String filenameNoExt = file.getName(); if (filenameNoExt.lastIndexOf('.') > 0) { filenameNoExt = filenameNoExt.substring(0, filenameNoExt.lastIndexOf('.')); } StringBuilder log = new StringBuilder(); if (isLogging()) processLogs.put(file, log); - - try { + + try { BufferedImage result = null; try { if (file.getName().endsWith(".bmp")) { @@ -216,11 +216,11 @@ public class ImageConverter { Log.e("Unreadable image: " + file.getAbsolutePath()); return null; } - + int w = result.getWidth(); int h = result.getHeight(); - //Scaling + //Scaling if (scaling == ScalingType.STRETCH) { w = targetScreenSize.width; h = targetScreenSize.height; @@ -234,9 +234,9 @@ public class ImageConverter { scaledW = Math.max(1, Math.round(w * scale)); scaledH = Math.max(1, Math.round(h * scale)); } - + Image scaled = ImageUtil.getScaledImage(result, scaledW, scaledH, Image.SCALE_AREA_AVERAGING); - + if (scaling == ScalingType.BACKGROUND) { w = targetScreenSize.width; h = targetScreenSize.height; @@ -244,12 +244,12 @@ public class ImageConverter { w = scaledW; h = scaledH; } - + result = ImageUtil.createCompatibleImage(w, h, scaled); if (mode == ConvertType.TYPE_JPG) { result = ImageUtil.asOpaqueImage(result); } - + Graphics2D g = (Graphics2D)result.getGraphics(); if (scaling == ScalingType.BACKGROUND) { int sw = Math.round(scale*srcScreenSize.width); @@ -259,39 +259,39 @@ public class ImageConverter { g.drawImage(scaled, (w-scaledW)/2, (h-scaledH)/2, null); g.dispose(); } - + //Dithering if (dithering == DitheringType.RANDOM) { int[] rgb = result.getRGB(0, 0, w, h, null, 0, w); - + Random rnd = new Random(0x13371337); int t = 0; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { int c = rgb[t]; - + double r = ((c>>16)&0xFF) * 31.0 / 255.0; double g = ((c>>8 )&0xFF) * 31.0 / 255.0; double b = ((c )&0xFF) * 31.0 / 255.0; - + boolean ceil = rnd.nextFloat() < (r-Math.floor(r) + g-Math.floor(g) + b-Math.floor(b)) / 3.0; - + int ri = (int)(ceil ? Math.ceil(r) : Math.floor(r)); int gi = (int)(ceil ? Math.ceil(g) : Math.floor(g)); int bi = (int)(ceil ? Math.ceil(b) : Math.floor(b)); - + rgb[t] = (c&0xFF000000) | (((ri<<3)&0xFF)<<16) | (((gi<<3)&0xFF)<<8) | ((bi<<3)&0xFF); t++; } } - + result.setRGB(0, 0, w, h, rgb, 0, w); } else if (dithering == DitheringType.FLOYD_STEINBERG) { int[] rgb = result.getRGB(0, 0, w, h, null, 0, w); floydSteinberg(rgb, w, h); result.setRGB(0, 0, w, h, rgb, 0, w); } - + if (targetFolder == null) { targetFolder = file.getParentFile(); file.delete(); @@ -299,36 +299,36 @@ public class ImageConverter { //Create unique hash (multiple threads are writing temp files in the same folder) String threadHash = String.valueOf(hashCode() ^ file.hashCode() ^ Thread.currentThread().hashCode()); - + file = new File(String.format("%s/%s.%s", targetFolder, filenameNoExt, mode.getFileExt().toLowerCase())); if (file.getParentFile() != null) { file.getParentFile().mkdirs(); } - + if (mode.getFileExt().equalsIgnoreCase("jpg")) { String bmpFile = String.format("%s/__%s.bmp", targetFolder, threadHash); String tmpFile = String.format("%s/__%s.jpg", targetFolder, threadHash); ImageIO.write(result, "bmp", new File(bmpFile)); - + Process p = ProcessUtil.execInDir(String.format( "cjpeg -quality %d -optimize -dct fast \"%s\" \"%s\"", quality, bmpFile, tmpFile), - "tools/"); + ""); ProcessUtil.waitFor(p); ProcessUtil.kill(p); file.delete(); new File(bmpFile).delete(); new File(tmpFile).renameTo(file); - } else if (mode.getFileExt().equalsIgnoreCase("png")) { + } else if (mode.getFileExt().equalsIgnoreCase("png")) { String tmpFile = String.format("%s/__%s.png", targetFolder, threadHash); ImageIO.write(result, "png", new File(tmpFile)); - + if (mode == ConvertType.TYPE_PNG_256_MEDIAN) { Process p = ProcessUtil.execInDir(String.format( "pngquant 256 \"%s\"", tmpFile), - "tools/pngquant-0.95/"); + ""); ProcessUtil.waitFor(p); ProcessUtil.kill(p); file.delete(); @@ -336,7 +336,7 @@ public class ImageConverter { } else if (mode == ConvertType.TYPE_PNG_256_NEUQUANT) { Process p = ProcessUtil.execInDir(String.format( "pngnq \"%s\"", tmpFile), - "tools/pngnq-0.5-i386/"); + ""); ProcessUtil.waitFor(p); ProcessUtil.kill(p); file.delete(); @@ -345,18 +345,18 @@ public class ImageConverter { String crushedName = StringUtil.stripExtension(tmpFile)+".crushed.png"; Process p = ProcessUtil.execInDir(String.format( "pngcrush -fix \"%s\" \"%s\"", tmpFile, crushedName), - "tools/pngcrush-1.6.10/"); + ""); ProcessUtil.waitFor(p); ProcessUtil.kill(p); file.delete(); new File(crushedName).renameTo(file); } - + new File(tmpFile).delete(); } else if (mode.getFileExt().equalsIgnoreCase("dta")) { if (mode == ConvertType.TYPE_RAW_RGBA) { int[] rgb = result.getRGB(0, 0, w, h, null, 0, w); - + BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(file)); for (int c : rgb) { int a = ((c>>>24) >= 127 ? (1<<15) : 0); @@ -364,7 +364,7 @@ public class ImageConverter { int g = (c>>11) & 31; int b = (c>>3) & 31; c = a | (b<<10) | (g<<5) | (r); - + bout.write(c&0xFF); bout.write((c>>8)&0xFF); } @@ -376,7 +376,7 @@ public class ImageConverter { Process p = ProcessUtil.execInDir(String.format( "pngnq \"%s\"", tmpFile.getAbsolutePath()), - "tools/pngnq-0.5-i386/"); + ""); ProcessUtil.waitFor(p); ProcessUtil.kill(p); tmpFile.delete(); @@ -386,7 +386,7 @@ public class ImageConverter { IndexColorModel icm = (IndexColorModel)result.getColorModel(); BufferedOutputStream bout; - + bout = new BufferedOutputStream(new FileOutputStream( StringUtil.stripExtension(file.getAbsolutePath())+".pal")); for (int n = 0; n < icm.getMapSize(); n++) { @@ -408,11 +408,11 @@ public class ImageConverter { bout.write(dta[n]); } bout.close(); - + tmpFile.delete(); } - } else { - throw new IllegalArgumentException("Invalid file-ext: " + mode.getFileExt()); + } else { + throw new IllegalArgumentException("Invalid file-ext: " + mode.getFileExt()); } return file; } catch (Exception e) { @@ -422,40 +422,40 @@ public class ImageConverter { return null; } - + public File dumpLog(String filename) throws IOException { File file = new File(filename); PrintWriter out = new PrintWriter(new FileWriter(file)); - + out.println("----------------------------------------"); out.println("----------------------------------------"); for (Entry entry : getLogs().entrySet()) { out.println(); out.println("Log for file:" + entry.getKey().getAbsolutePath()); out.println(); - + out.println(entry.getValue().toString()); - + out.println("----------------------------------------"); out.println("----------------------------------------"); } - + out.close(); - + return file; } private static int round(float f) { return (int)f; } - + private static float saturate255(float f) { if (f < 0f) return 0f; if (f > 255f) return 255f; return f; } - + /** Does dithering for 5-bit colors (equivalent to 8-bit / 8.0) */ private static void floydSteinberg(int rgb[], int w, int h) { int L = w * h; @@ -474,12 +474,12 @@ public class ImageConverter { final int DOWN = w; final int DOWN_LEFT = DOWN-1; final int DOWN_RIGHT = DOWN+1; - + for (int c = 0; c < 3; c++) { float[] p = rgba[c]; int t = 0; for (int y = 0; y < h; y++) { - for (int x = 0; x < w; x++) { + for (int x = 0; x < w; x++) { float oldc = p[t]; float newc = p[t] = round(oldc * DIV_8) << 3; float error = oldc - newc; @@ -500,12 +500,12 @@ public class ImageConverter { p[t+DOWN_RIGHT] = saturate255(p[t+DOWN_RIGHT]); } } - + t++; - } + } } } - + int t = 0; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { @@ -518,7 +518,7 @@ public class ImageConverter { } } } - + // Getters public Map getLogs() { return processLogs; } public boolean isLogging() { return log; } @@ -529,7 +529,7 @@ public class ImageConverter { public int getQuality() { return quality; } public DitheringType getDitheringType() { return dithering; } public int getMaxThreads() { return maxThreads; } - + // Setters public void setMode(ConvertType mode) { this.mode = mode; } public void setScalingType(ScalingType s) { this.scaling = s; } @@ -539,5 +539,5 @@ public class ImageConverter { public void setQuality(int quality) { this.quality = quality; } public void setDitheringType(DitheringType dithering) { this.dithering = dithering; } public void setMaxThreads(int mt) { this.maxThreads = mt; } - + } diff --git a/UI/src/nl/weeaboo/vnds/tools/SoundConverter.java b/UI/src/nl/weeaboo/vnds/tools/SoundConverter.java index 2197b64..aeace10 100644 --- a/UI/src/nl/weeaboo/vnds/tools/SoundConverter.java +++ b/UI/src/nl/weeaboo/vnds/tools/SoundConverter.java @@ -18,15 +18,15 @@ public class SoundConverter { public enum ConvertType { TYPE_AAC("AAC"), TYPE_ADPCM("ADPCM"), TYPE_MP3("MP3"), TYPE_OGG("OGG"); - + private String label; - + private ConvertType(String label) { this.label = label; } - + public String toString() { return label; } - + public static ConvertType fromExt(String string) { string = string.toLowerCase(); if (string.equals("aac")) { @@ -38,54 +38,54 @@ public class SoundConverter { } else if (string.equals("ogg")) { return TYPE_OGG; } - + throw new IllegalArgumentException("Unsupported conversion type: " + string); } } - + public static final int AAC_Q_LOW = 35; public static final int AAC_Q_MED = 50; public static final int AAC_Q_HIGH = 70; - + public static final int VORBIS_Q_LOW = 0; public static final int VORBIS_Q_MED = 2; public static final int VORBIS_Q_HIGH = 4; - - private String workingDir = "tools/"; + + private String workingDir = ""; private int maxThreads = 8; private ConvertType mode = ConvertType.TYPE_AAC; private int aac_quality = AAC_Q_HIGH; private int mp3_minb = 8, mp3_maxb = 128, mp3_avgb = 96; private int vorbis_quality = VORBIS_Q_MED; - private int volume = 100; + private int volume = 100; private boolean nameToUpperCase; private boolean log; - + public SoundConverter() { } - + // Functions public static void main(String args[]) throws IOException { SoundConverter ve = new SoundConverter(); - + //Fate/Stay Night //ve.convertFolder("foldername/"); - + //Narcissu //ve.setVolume(800); //ve.setConvertNameToUpperCase(true); //ve.convertFolder("foldername/", null); - + ve.setMode(ConvertType.TYPE_OGG); ve.convertFile(new File("z:/temp.mp3")); } - + public void convertFolder(String folder, final ProgressListener pl) { Map files = new HashMap(); for (File file : new File(folder).listFiles()) { if (!file.isDirectory()) files.put(file.getName(), file); } - + BatchProcess bp = new BatchProcess(); bp.setTaskSize(32); bp.setThreads(maxThreads); @@ -113,12 +113,12 @@ public class SoundConverter { files.add(file); } } - + protected static File[] createTempFiles(File file, File tempFolder, String... exts) throws IOException { File[] result = new File[exts.length + 1]; - + String path = file.getAbsolutePath(); //Check if entire path consists of ASCII characters @@ -129,10 +129,10 @@ public class SoundConverter { break; } } - + if (ascii) { result[0] = file; - } else { + } else { File dst = null; String pre = String.format("temp-" + Thread.currentThread().hashCode()); String post = StringUtil.getExtension(file.getName()).toLowerCase(); @@ -142,7 +142,7 @@ public class SoundConverter { FileUtil.copyFile(file, dst); result[0] = dst; } - + for (int n = 0; n < exts.length; n++) { String p = StringUtil.replaceExt(result[n].getAbsolutePath(), exts[n]); File f = new File(p); @@ -150,14 +150,14 @@ public class SoundConverter { int t = 1; while (f.exists()) { t++; - f = new File(StringUtil.stripExtension(p) + "-" + t + "." + StringUtil.getExtension(p)); + f = new File(StringUtil.stripExtension(p) + "-" + t + "." + StringUtil.getExtension(p)); } result[n+1] = f; } - + return result; } - + public void convertFile(File file) throws IOException { convertFile(file, mode, null); } @@ -167,20 +167,20 @@ public class SoundConverter { public File convertFile(File srcF, ConvertType mode, File dstFolder) throws IOException { if (dstFolder == null) { dstFolder = srcF.getParentFile(); - } + } dstFolder.mkdirs(); String filters = ""; if (volume != 100) { filters += " -vol " + volume; - } - + } + File[] temp; String[] cmds; switch (mode) { case TYPE_AAC: temp = createTempFiles(srcF, dstFolder, "wav", "aac"); - + cmds = new String[] { String.format("ffmpeg -y -i \"%s\" -vn -ac 1 -ar 22050 %s \"%s\"", temp[0], filters, temp[1]), @@ -190,10 +190,10 @@ public class SoundConverter { break; case TYPE_MP3: temp = createTempFiles(srcF, dstFolder, "wav", "mp3"); - + String bitrateFlags = String.format("--abr %d -b %d -B %d", mp3_avgb, mp3_minb, mp3_maxb); - + cmds = new String[] { String.format("ffmpeg -y -i \"%s\" -vn -ac 2 -ar 32000 %s \"%s\"", temp[0], filters, temp[1]), @@ -213,7 +213,7 @@ public class SoundConverter { break; case TYPE_OGG: temp = createTempFiles(srcF, dstFolder, "ogg"); - + cmds = new String[] { String.format("ffmpeg -y -i \"%s\" -vn -acodec libvorbis -aq %d %s \"%s\"", temp[0], vorbis_quality, filters, temp[1]) @@ -222,7 +222,7 @@ public class SoundConverter { default: throw new IllegalArgumentException("Illegal mode: " + mode); } - + File dstF = new File(dstFolder, StringUtil.replaceExt(srcF.getName(), StringUtil.getExtension(temp[temp.length-1].getName()).toLowerCase())); try { @@ -230,13 +230,13 @@ public class SoundConverter { Process p = ProcessUtil.execInDir(cmd, workingDir); //System.out.println(ProcessUtil.read(p)); ProcessUtil.waitFor(p); - } + } } finally { //Rename final result to dst if (temp[temp.length-1].exists()) { temp[temp.length-1].renameTo(dstF); } - + //Delete temp files for (File file : temp) { if (!file.equals(srcF) && !file.equals(dstF)) { @@ -244,10 +244,10 @@ public class SoundConverter { } } } - + return dstF; } - + // Getters public boolean isLogging() { return log; } public ConvertType getMode() { return mode; } @@ -260,7 +260,7 @@ public class SoundConverter { public boolean getConvertNameToUpperCase() { return nameToUpperCase; } public int getMaxThreads() { return maxThreads; } public String getWorkingDir() { return workingDir; } - + // Setters public void setMode(ConvertType mode) { this.mode = mode; } public void setVorbisQuality(int quality) { this.vorbis_quality = quality; } @@ -274,5 +274,5 @@ public class SoundConverter { public void setConvertNameToUpperCase(boolean nameToUpperCase) { this.nameToUpperCase = nameToUpperCase; } public void setMaxThreads(int mt) { this.maxThreads = mt; } public void setWorkingDir(String dir) { this.workingDir = dir; } - + } -- 2.39.5