瀏覽代碼

optimized tga decoding in RGBA (less converts)

Nicolas Cannasse 4 年之前
父節點
當前提交
9c9e2ce1ae
共有 1 個文件被更改,包括 24 次插入7 次删除
  1. 24 7
      hxd/res/Image.hx

+ 24 - 7
hxd/res/Image.hx

@@ -295,14 +295,31 @@ class Image extends Resource {
 				throw "Not supported TGA "+r.header.imageType+"/"+r.header.bitsPerPixel;
 				throw "Not supported TGA "+r.header.imageType+"/"+r.header.bitsPerPixel;
 			var w = r.header.width;
 			var w = r.header.width;
 			var h = r.header.height;
 			var h = r.header.height;
-			pixels = hxd.Pixels.alloc(w, h, ARGB);
-			var access : hxd.Pixels.PixelsARGB = pixels;
-			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);
+			if( fmt == RGBA ) {
+				pixels = hxd.Pixels.alloc(w, h, RGBA);
+				var bytes = pixels.bytes;
+				#if hl
+				var bytes : hl.Bytes = bytes;
+				inline function set(i,c) bytes.setI32(i<<2,c);
+				#else
+				inline function set(i,c) bytes.setInt32(i<<2,c);
+				#end
+				for( i in 0...w*h ) {
+					var c = r.imageData[i];
+					c = (c >>> 24) | (c << 8);
+					set(i,c);
 				}
 				}
+			} else {
+				pixels = hxd.Pixels.alloc(w, h, ARGB);
+				var access : hxd.Pixels.PixelsARGB = pixels;
+				var p = 0;
+				for( y in 0...h ) {
+					for( x in 0...w ) {
+						var c = r.imageData[p++];
+						access.setPixel(x, y, c);
+					}
+				}
+			}
 			switch( r.header.imageOrigin ) {
 			switch( r.header.imageOrigin ) {
 			case BottomLeft: pixels.flags.set(FlipY);
 			case BottomLeft: pixels.flags.set(FlipY);
 			case TopLeft: // nothing
 			case TopLeft: // nothing