shithub: pokecrystal

Download patch

ref: 98d8914b62750ea7575a5c16ce954cc0bbedb825
parent: d1172e1fb67fe8bd9c33cc4ab0d45905167d4c53
author: yenatch <[email protected]>
date: Sun Feb 10 22:42:33 EST 2013

better dimension handling in 2bpp->png

--- a/extras/gfx.py
+++ b/extras/gfx.py
@@ -1216,23 +1216,54 @@
 	
 	image = open(filein, 'rb').read()
 	
-	if len(image) == 0: return 'empty image!'
-
-	# unless the pic is square, at least one dimension should be given...
+	num_pixels = len(image) * 4
 	
-	if height   == None and width == None:
-		height = int(sqrt(len(image)*4))
-		width = height
+	if num_pixels == 0: return 'empty image!'
 	
-	elif height == None: height = len(image)*4 / width
+	
+	# unless the pic is square, at least one dimension should be given
+	
+	if width == None and height == None:
+		width  = int(sqrt(num_pixels))
+		height = width
+	
+	elif height == None:
+		height = num_pixels / width
 
-	elif width  == None: width = len(image)*4 / height
+	elif width  == None:
+		width  = num_pixels / height
 	
-	# ...or it will become 1 tile wide
 	
-	if height * width != len(image)*4:
-		height = len(image)*4 / 8
-		width  = 8
+	# but try to see if it can be made rectangular
+	
+	if width * height != num_pixels:
+		
+		# look for possible combos of width/height that would form a rectangle
+		matches = []
+		
+		# this is pretty inefficient, and there is probably a simpler way
+		for width in range(8,256+1,8): # we only want dimensions that fit in tiles
+			height = num_pixels / width
+			if height % 8 == 0:
+				matches.append((width, height))
+		
+		# go for the most square image
+		width, height = sorted(matches, key=lambda (x,y): x+y)[0] # favors height
+	
+	
+	# if it can't, the only option is a width of 1 tile
+	
+	if width * height != num_pixels:
+		width = 8
+		height = num_pixels / width
+	
+	
+	# if this still isn't rectangular, then the image isn't made of tiles
+	
+	# for now we'll just spit out a warning
+	if width * height != num_pixels:
+		print 'Warning! ' + fileout + ' is ' + width + 'x' + height + '(' + width*height + ' pixels),\n' +\
+		       'but ' + filein + ' is ' + num_pixels + ' pixels!'
 	
 	
 	# map it out