瀏覽代碼

added tga file support

ncannasse 7 年之前
父節點
當前提交
3dc830777a
共有 3 個文件被更改,包括 39 次插入4 次删除
  1. 37 3
      hxd/fs/Convert.hx
  2. 1 0
      hxd/fs/LocalFileSystem.hx
  3. 1 1
      hxd/res/Config.hx

+ 37 - 3
hxd/fs/Convert.hx

@@ -66,11 +66,11 @@ class Command extends Convert {
 		this.cmd = cmd;
 		this.args = args;
 	}
-	
+
 	override function convert() {
 		command(cmd,[for( a in args ) if( a == "%SRC" ) srcPath else if( a == "%DST" ) dstPath else a]);
 	}
-	
+
 }
 
 
@@ -100,4 +100,38 @@ class ConvertWAV2OGG extends Convert {
 		command(cmd, ["--resample", "44100", "-Q", srcPath, "-o", dstPath]);
 	}
 
-}
+}
+
+class ConvertTGA2PNG extends Convert {
+
+	public function new() {
+		super("tga", "png");
+	}
+
+	override function convert() {
+		var input = new haxe.io.BytesInput(sys.io.File.getBytes(srcPath));
+		var r = new format.tga.Reader(input).read();
+		if( r.header.imageType != UncompressedTrueColor || r.header.bitsPerPixel != 32 )
+			throw "Not supported "+r.header.imageType+"/"+r.header.bitsPerPixel;
+		var w = r.header.width;
+		var h = r.header.height;
+		var pix = hxd.Pixels.alloc(w, h, ARGB);
+		var access : hxd.Pixels.PixelsARGB = pix;
+		var p = 0;
+		for( y in 0...h )
+			for( x in 0...w ) {
+				var c = r.imageData[x + y * w];
+				access.setPixel(x, y, c);
+			}
+		switch( r.header.imageOrigin ) {
+		case BottomLeft:
+			pix.flags.set(FlipY);
+		case TopLeft:
+		default:
+			throw "Not supported "+r.header.imageOrigin;
+		}
+		sys.io.File.saveBytes(dstPath, pix.toPNG());
+	}
+
+}
+

+ 1 - 0
hxd/fs/LocalFileSystem.hx

@@ -299,6 +299,7 @@ class LocalFileSystem implements FileSystem {
 		baseDir = dir;
 		converts = new Map();
 		addConvert(new Convert.ConvertFBX2HMD());
+		addConvert(new Convert.ConvertTGA2PNG());
 		#if flash
 		var froot = new flash.filesystem.File(flash.filesystem.File.applicationDirectory.nativePath + "/" + baseDir);
 		if( !froot.exists ) throw "Could not find dir " + dir;

+ 1 - 1
hxd/res/Config.hx

@@ -18,7 +18,7 @@ class Config {
 		Maps the extension to a given resource class. Example ["wav,mp3,ogg" => "hxd.res.Sound"]
 	**/
 	public static var extensions = [
-		"jpg,png,jpeg,gif" => "hxd.res.Image",
+		"jpg,png,jpeg,gif,tga" => "hxd.res.Image",
 		"fbx,hmd" => "hxd.res.Model",
 		"ttf" => "hxd.res.Font",
 		"fnt" => "hxd.res.BitmapFont",