Browse Source

cleanup legacy hxd.Pixels flipY

Nicolas Cannasse 2 years ago
parent
commit
9fc8f6da15
7 changed files with 21 additions and 45 deletions
  1. 0 1
      h3d/impl/DX12Driver.hx
  2. 0 1
      h3d/impl/DirectXDriver.hx
  3. 0 1
      h3d/impl/GlDriver.hx
  4. 0 1
      hxd/BitmapData.hx
  5. 8 24
      hxd/Pixels.hx
  6. 3 3
      hxd/fs/Convert.hx
  7. 10 14
      hxd/res/Image.hx

+ 0 - 1
h3d/impl/DX12Driver.hx

@@ -1265,7 +1265,6 @@ class DX12Driver extends h3d.impl.Driver {
 
 	override function uploadTexturePixels(t:h3d.mat.Texture, pixels:hxd.Pixels, mipLevel:Int, side:Int) {
 		pixels.convert(t.format);
-		pixels.setFlip(false);
 		if( mipLevel >= t.mipLevels ) throw "Mip level outside texture range : " + mipLevel + " (max = " + (t.mipLevels - 1) + ")";
 
 		tmp.heap.type = UPLOAD;

+ 0 - 1
h3d/impl/DirectXDriver.hx

@@ -659,7 +659,6 @@ class DirectXDriver extends h3d.impl.Driver {
 
 	override function uploadTexturePixels(t:h3d.mat.Texture, pixels:hxd.Pixels, mipLevel:Int, side:Int) {
 		pixels.convert(t.format);
-		pixels.setFlip(false);
 		if( hasDeviceError ) return;
 		if( mipLevel >= t.t.mips ) throw "Mip level outside texture range : " + mipLevel + " (max = " + (t.t.mips - 1) + ")";
 		var stride = @:privateAccess pixels.stride;

+ 0 - 1
h3d/impl/GlDriver.hx

@@ -1165,7 +1165,6 @@ class GlDriver extends Driver {
 		var bind = getBindType(t);
 		gl.bindTexture(bind, t.t.t);
 		pixels.convert(t.format);
-		pixels.setFlip(false);
 		var dataLen = pixels.dataSize;
 		#if hl
 		var stream = streamData(pixels.bytes.getData(),pixels.offset,dataLen);

+ 0 - 1
hxd/BitmapData.hx

@@ -623,7 +623,6 @@ class BitmapData {
 	public function setPixels( pixels : Pixels ) {
 		if( pixels.width != width || pixels.height != height )
 			throw "Invalid pixels size";
-		pixels.setFlip(false);
 		#if flash
 		var bytes = pixels.bytes.getData();
 		bytes.position = 0;

+ 8 - 24
hxd/Pixels.hx

@@ -3,7 +3,6 @@ package hxd;
 enum Flags {
 	ReadOnly;
 	AlphaPremultiplied;
-	FlipY;
 }
 
 @:forward(bytes, format, width, height, offset, flags, clear, dispose, toPNG, clone, sub, blit)
@@ -20,7 +19,6 @@ abstract PixelsARGB(Pixels) to Pixels {
 
 	@:from public static function fromPixels(p:Pixels) : PixelsARGB {
 		p.convert(ARGB);
-		p.setFlip(false);
 		return cast p;
 	}
 }
@@ -43,7 +41,6 @@ abstract PixelsFloat(Pixels) to Pixels {
 	}
 
 	@:from public static function fromPixels(p:Pixels) : PixelsFloat {
-		p.setFlip(false);
 		p.convert(R32F);
 		return cast p;
 	}
@@ -74,7 +71,6 @@ abstract PixelsFloatRGBA(Pixels) to Pixels {
 	}
 
 	@:from public static function fromPixels(p:Pixels) : PixelsFloatRGBA {
-		p.setFlip(false);
 		p.convert(RGBA32F);
 		return cast p;
 	}
@@ -142,17 +138,13 @@ class Pixels {
 		var stride = calcStride(width, format);
 		var outP = 0;
 		for( dy in 0...height ) {
-			var p = (x + yflip(y + dy) * this.width) * bytesPerPixel + offset;
+			var p = (x + (y + dy) * this.width) * bytesPerPixel + offset;
 			out.blit(outP, this.bytes, p, stride);
 			outP += stride;
 		}
 		return new hxd.Pixels(width, height, out, format);
 	}
 
-	inline function yflip(y:Int) {
-		return if( flags.has(FlipY) ) this.height - 1 - y else y;
-	}
-
 	public function blit( x : Int, y : Int, src : hxd.Pixels, srcX : Int, srcY : Int, width : Int, height : Int ) {
 		if( x < 0 || y < 0 || x + width > this.width || y + height > this.height )
 			throw "Pixels.blit() outside bounds";
@@ -165,8 +157,8 @@ class Pixels {
 			throw "assert";
 		var stride = calcStride(width, format);
 		for( dy in 0...height ) {
-			var srcP = (srcX + src.yflip(dy + srcY) * src.width) * bpp + src.offset;
-			var dstP = (x + yflip(dy + y) * this.width) * bpp + offset;
+			var srcP = (srcX + (dy + srcY) * src.width) * bpp + src.offset;
+			var dstP = (x + (dy + y) * this.width) * bpp + offset;
 			bytes.blit(dstP, src.bytes, srcP, stride);
 		}
 	}
@@ -224,10 +216,6 @@ class Pixels {
 		var idx = 0;
 		var p = offset;
 		var dl = 0;
-		if( flags.has(FlipY) ) {
-			p += ((height - 1) * width) * bytesPerPixel;
-			dl = -width * 2 * bytesPerPixel;
-		}
 		switch(format) {
 		case BGRA:
 			for( y in 0...height ) {
@@ -303,11 +291,8 @@ class Pixels {
 		if( flags.has(ReadOnly) ) copyInner();
 	}
 
-	public function setFlip( b : Bool ) {
-		#if js if( b == null ) b = false; #end
-		if( flags.has(FlipY) == b ) return;
+	public function flipY() {
 		willChange();
-		if( b ) flags.set(FlipY) else flags.unset(FlipY);
 		if( stride%4 != 0 ) invalidFormat();
 		for( y in 0...height >> 1 ) {
 			var p1 = y * stride + offset;
@@ -429,7 +414,7 @@ class Pixels {
 	}
 
 	public function getPixel(x, y) : Int {
-		var p = ((x + yflip(y) * width) * bytesPerPixel) + offset;
+		var p = ((x + y * width) * bytesPerPixel) + offset;
 		switch(format) {
 		case BGRA:
 			return bytes.getInt32(p);
@@ -449,7 +434,7 @@ class Pixels {
 	}
 
 	public function setPixel(x, y, color) : Void {
-		var p = ((x + yflip(y) * width) * bytesPerPixel) + offset;
+		var p = ((x + y * width) * bytesPerPixel) + offset;
 		willChange();
 		switch(format) {
 		case R8:
@@ -470,7 +455,7 @@ class Pixels {
 	public function getPixelF(x, y, ?v:h3d.Vector) {
 		if( v == null )
 			v = new h3d.Vector();
-		var p = ((x + yflip(y) * width) * bytesPerPixel) + offset;
+		var p = ((x + y * width) * bytesPerPixel) + offset;
 		switch( format ) {
 		case R32F:
 			v.set(bytes.getFloat(p),0,0,0);
@@ -489,7 +474,7 @@ class Pixels {
 
 	public function setPixelF(x, y, v:h3d.Vector) {
 		willChange();
-		var p = ((x + yflip(y) * width) * bytesPerPixel) + offset;
+		var p = ((x + y * width) * bytesPerPixel) + offset;
 		switch( format ) {
 		case R32F:
 			bytes.setFloat(p, v.x);
@@ -513,7 +498,6 @@ class Pixels {
 
 	public function toPNG( ?level = 9 ) {
 		var png;
-		setFlip(false);
 		if( offset != 0 ) {
 			bytes = bytes.sub(offset, calcDataSize(width,height, format));
 			offset = 0;

+ 3 - 3
hxd/fs/Convert.hx

@@ -194,7 +194,7 @@ class ConvertTGA2PNG extends Convert {
 			}
 		switch( r.header.imageOrigin ) {
 		case BottomLeft:
-			pix.flags.set(FlipY);
+			pix.flipY();
 		case TopLeft:
 		default:
 			throw "Not supported "+r.header.imageOrigin;
@@ -317,7 +317,7 @@ class CompressIMG extends Convert {
 				for ( layer in 0...info.layerCount ) {
 					var layerPixels = [];
 					for( mip in 0...info.mipLevels ) {
-						var pixels = image.getPixels(null, null, layer * info.mipLevels + mip);
+						var pixels = image.getPixels(null, layer * info.mipLevels + mip);
 						layerPixels.push(pixels);
 					}
 					var layerBytes = hxd.Pixels.toDDSLayers(layerPixels);
@@ -350,7 +350,7 @@ class CompressIMG extends Convert {
 					if ( image == null )
 						throw "Unsupported format";
 					for ( mip in 0... info.mipLevels) {
-						var pixels = image.getPixels(null, null, mip);
+						var pixels = image.getPixels(null, mip);
 						convertPixels.push(pixels);
 					}
 					sys.FileSystem.deleteFile(layerPath);

+ 10 - 14
hxd/res/Image.hx

@@ -284,7 +284,7 @@ class Image extends Resource {
 		return inf;
 	}
 
-	public function getPixels( ?fmt : PixelFormat, ?flipY : Bool, ?index : Int ) {
+	public function getPixels( ?fmt : PixelFormat, ?index : Int ) {
 		var pixels : hxd.Pixels;
 		if( index == null )
 			index = 0;
@@ -293,7 +293,7 @@ class Image extends Resource {
 			var bytes = entry.getBytes(); // using getTmpBytes cause bug in E2
 			#if hl
 			if( fmt == null ) fmt = inf.pixelFormat;
-			pixels = decodePNG(bytes, inf.width, inf.height, fmt, flipY);
+			pixels = decodePNG(bytes, inf.width, inf.height, fmt);
 			if( pixels == null ) throw "Failed to decode PNG " + entry.path;
 			#else
 			if( inf.pixelFormat != BGRA )
@@ -303,8 +303,7 @@ class Image extends Resource {
 			// we only support BGRA decoding here
 			pixels = Pixels.alloc(inf.width, inf.height, BGRA);
 			var pdata = png.read();
-			format.png.Tools.extract32(pdata, pixels.bytes, flipY);
-			if( flipY ) pixels.flags.set(FlipY);
+			format.png.Tools.extract32(pdata, pixels.bytes, false);
 			#end
 		case Gif:
 			var bytes = entry.getBytes();
@@ -317,7 +316,7 @@ class Image extends Resource {
 			var bytes = entry.getBytes();
 			#if hl
 			if( fmt == null ) fmt = inf.pixelFormat;
-			pixels = decodeJPG(bytes, inf.width, inf.height, fmt, flipY);
+			pixels = decodeJPG(bytes, inf.width, inf.height, fmt);
 			if( pixels == null ) throw "Failed to decode JPG " + entry.path;
 			#else
 			if( inf.pixelFormat != BGRA )
@@ -358,7 +357,7 @@ class Image extends Resource {
 				}
 			}
 			switch( r.header.imageOrigin ) {
-			case BottomLeft: pixels.flags.set(FlipY);
+			case BottomLeft: pixels.flipY();
 			case TopLeft: // nothing
 			default: throw "Not supported "+r.header.imageOrigin;
 			}
@@ -406,13 +405,12 @@ class Image extends Resource {
 			pixels = new hxd.Pixels(data.width, data.height, data.bytes, inf.pixelFormat);
 		}
 		if( fmt != null ) pixels.convert(fmt);
-		if( flipY != null ) pixels.setFlip(flipY);
 		return pixels;
 	}
 
 	#if hl
 
-	static function decodeJPG( src : haxe.io.Bytes, width : Int, height : Int, requestedFmt : hxd.PixelFormat, flipY : Bool ) {
+	static function decodeJPG( src : haxe.io.Bytes, width : Int, height : Int, requestedFmt : hxd.PixelFormat ) {
 		var outFmt = requestedFmt;
 		var ifmt : hl.Format.PixelFormat = switch( requestedFmt ) {
 		case RGBA: RGBA;
@@ -423,14 +421,13 @@ class Image extends Resource {
 			BGRA;
 		};
 		var dst = haxe.io.Bytes.alloc(width * height * 4);
-		if( !hl.Format.decodeJPG(src.getData(), src.length, dst.getData(), width, height, width * 4, ifmt, (flipY?1:0)) )
+		if( !hl.Format.decodeJPG(src.getData(), src.length, dst.getData(), width, height, width * 4, ifmt, 0) )
 			return null;
 		var pix = new hxd.Pixels(width, height, dst, outFmt);
-		if( flipY ) pix.flags.set(FlipY);
 		return pix;
 	}
 
-	static function decodePNG( src : haxe.io.Bytes, width : Int, height : Int, requestedFmt : hxd.PixelFormat, flipY : Bool ) {
+	static function decodePNG( src : haxe.io.Bytes, width : Int, height : Int, requestedFmt : hxd.PixelFormat ) {
 		var outFmt = requestedFmt;
 		var ifmt : hl.Format.PixelFormat = switch( requestedFmt ) {
 		case RGBA: RGBA;
@@ -458,10 +455,9 @@ class Image extends Resource {
 		default:
 		}
 		var dst = haxe.io.Bytes.alloc(width * height * pxsize);
-		if( !hl.Format.decodePNG(src.getData(), src.length, dst.getData(), width, height, width * stride, ifmt, (flipY?1:0)) )
+		if( !hl.Format.decodePNG(src.getData(), src.length, dst.getData(), width, height, width * stride, ifmt, 0) )
 			return null;
 		var pix = new hxd.Pixels(width, height, dst, outFmt);
-		if( flipY ) pix.flags.set(FlipY);
 		return pix;
 	}
 
@@ -570,7 +566,7 @@ class Image extends Resource {
 			default:
 				for( layer in 0...tex.layerCount ) {
 					for( mip in 0...inf.mipLevels ) {
-						var pixels = getPixels(tex.format,null,layer * inf.mipLevels + mip);
+						var pixels = getPixels(tex.format,layer * inf.mipLevels + mip);
 						tex.uploadPixels(pixels,mip,layer);
 						pixels.dispose();
 					}