浏览代码

use hl png support

Nicolas Cannasse 8 年之前
父节点
当前提交
4f5306491d
共有 1 个文件被更改,包括 24 次插入2 次删除
  1. 24 2
      hxd/res/Image.hx

+ 24 - 2
hxd/res/Image.hx

@@ -108,6 +108,10 @@ class Image extends Resource {
 			// native PNG loader is faster
 			var i = lime.graphics.Image.fromBytes( bytes );
 			pixels = new Pixels(inf.width, inf.height, i.data.toBytes(), RGBA );
+			#elseif hl
+			if( fmt == null ) fmt = BGRA;
+			pixels = decodePNG(bytes, inf.width, inf.height, fmt, flipY);
+			if( pixels == null ) throw "Failed to decode JPG " + entry.path;
 			#else
 			var png = new format.png.Reader(new haxe.io.BytesInput(bytes));
 			png.checkCRC = false;
@@ -145,7 +149,7 @@ class Image extends Resource {
 		if( flipY != null ) pixels.setFlip(flipY);
 		return pixels;
 	}
-	
+
 	#if hl
 	static function decodeJPG( src : haxe.io.Bytes, width : Int, height : Int, fmt : hxd.PixelFormat, flipY : Bool ) {
 		var ifmt : hl.Format.PixelFormat = switch( fmt ) {
@@ -163,7 +167,25 @@ class Image extends Resource {
 		}
 		var pix = new hxd.Pixels(width, height, dst, fmt);
 		if( flipY ) pix.flags.set(FlipY);
-		pix.convert(fmt);
+		return pix;
+	}
+
+	static function decodePNG( src : haxe.io.Bytes, width : Int, height : Int, fmt : hxd.PixelFormat, flipY : Bool ) {
+		var ifmt : hl.Format.PixelFormat = switch( fmt ) {
+		case RGBA: RGBA;
+		case BGRA: BGRA;
+		case ARGB: ARGB;
+		default:
+			fmt = BGRA;
+			BGRA;
+		};
+		var dst = hxd.impl.Tmp.getBytes(width * height * 4);
+		if( !hl.Format.decodePNG(src.getData(), src.length, dst.getData(), width, height, width * 4, ifmt, (flipY?1:0)) ) {
+			hxd.impl.Tmp.saveBytes(dst);
+			return null;
+		}
+		var pix = new hxd.Pixels(width, height, dst, fmt);
+		if( flipY ) pix.flags.set(FlipY);
 		return pix;
 	}
 	#end