NTFT.py was speed up with numpy, and some cleanup here and there

This commit is contained in:
pbsds committed 2015-02-05 23:32:08 +01:00
1 parent f81e878be5
commit b1019b957f
5 files changed
+73 -850

No files matched your search

+30 -19
View File
@@ -8,7 +8,7 @@
# -The guys behind TiledGGD. This sped up my work a lit.
# -Jsafive for supplying .ugo files
#
import sys, os
import sys, os, numpy as np
try:
import Image
hasPIL = True
@@ -87,7 +87,8 @@ class NTFT:
return False
#JUST DO IT!
self.Image = [[None for _ in xrange(h)] for _ in xrange(w)]
#self.Image = [[None for _ in xrange(h)] for _ in xrange(w)]
self.Image = np.zeros((w, h), dtype=">u4")
for y in xrange(h):
for x in xrange(w):
pos = (x + y*pw)*2
@@ -99,7 +100,8 @@ class NTFT:
g = (byte >> 5 & 0x1F) * 0xFF / 0x1F
r = (byte & 0x1F) * 0xFF / 0x1F
self.Image[x][y] = (r<<24) | (g<<16) | (b<<8) | a#RGBA8
#self.Image[x][y] = (r<<24) | (g<<16) | (b<<8) | a#RGBA8
self.Image[x, y] = (r<<24) | (g<<16) | (b<<8) | a#RGBA8
self.Loaded = True
return self
@@ -115,8 +117,9 @@ class NTFT:
if not self.Loaded:
return False
h = len(self.Image[0])
w = len(self.Image)
#h = len(self.Image[0])
#w = len(self.Image)
w, h = self.Image.shape
#the actual stored data is a image with the sizes padded to the nearest power of 2
psize = []
@@ -130,7 +133,8 @@ class NTFT:
for y in xrange(psize[1]):
for x in xrange(psize[0]):
#read
c = self.Image[clamp(x, 0, w-1)][clamp(y, 0, h-1)]
#c = self.Image[clamp(x, 0, w-1)][clamp(y, 0, h-1)]
c = self.Image[clamp(x, 0, w-1), clamp(y, 0, h-1)]
r = c >> 24
g = (c >> 16) & 0xFF
b = (c >> 8 ) & 0xFF
@@ -153,19 +157,24 @@ class NTFT:
#Function WriteImage:
#
# Writes a 2D list of uint32 RGBA values as a image files.
# Writes a 2D array of uint32 RGBA values as a image file.
# Designed to work with NTFT.Image
#
# This function requires the PIl imaging module
def WriteImage(image, outputPath):
if not hasPIL or not image: return False
if not hasPIL:
print "Error: PIL not found!"
return False
#if not image: return False
out = []
for y in xrange(len(image[0])):
for x in xrange(len(image)):
out.append(DecAsc(image[x][y], 4))
out = image.tostring("F")
out = Image.fromstring("RGBA", (len(image), len(image[0])), "".join(out))
# out = []
# for y in xrange(len(image[0])):
# for x in xrange(len(image)):
# out.append(DecAsc(image[x][y], 4))
out = Image.fromstring("RGBA", (len(image), len(image[0])), out)
filetype = outputPath[outputPath.rfind(".")+1:]
out.save(outputPath, filetype)
@@ -178,7 +187,7 @@ def WriteImage(image, outputPath):
# This can be passed into NTFT().SetImage()
#
# This function requires the PIl imaging module
def ReadImage(path):
def ReadImage(path):#TODO: make it support numpy
if not hasPIL: return False
image = Image.open(path)
@@ -192,12 +201,14 @@ def ReadImage(path):
def Combine((r, g, b, a)):
return (r << 24) | (g << 16) | (b << 8) | a
ret = []
#ret = []
ret = np.zeros((w, h), dtype=">u4")
for x in xrange(w):
line = []
#line = []
for y in xrange(h):
line.append(Combine(pixeldata[y*w + x]))
ret.append(line)
ret[x, y] = Combine(pixeldata[y*w + x])#maybe make a more numpy efficient way?
#line.append(Combine(pixeldata[y*w + x]))
#ret.append(line)
return ret
@@ -221,7 +232,7 @@ def ReadImage(path):
if __name__ == "__main__":
print " == NTFT.py =="
print " == by pbsds =="
print " == v0.80 =="
print " == v0.95 =="
print
if not hasPIL: