Merge pull request #57 from jedediah/master
Use pure Python PNG loader instead of pygame.load
This commit is contained in:
commit
6b11355182
54
mceutils.py
54
mceutils.py
@ -29,7 +29,7 @@ import zipfile
|
|||||||
from cStringIO import StringIO
|
from cStringIO import StringIO
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
from pygame import image, display
|
from pygame import image, display, Surface
|
||||||
import png
|
import png
|
||||||
import config
|
import config
|
||||||
import release
|
import release
|
||||||
@ -382,46 +382,18 @@ def loadAlphaTerrainTexture():
|
|||||||
|
|
||||||
|
|
||||||
def loadPNGData(filename_or_data):
|
def loadPNGData(filename_or_data):
|
||||||
# if filename[1:4] != "PNG":
|
reader = png.Reader(filename_or_data)
|
||||||
if isinstance(filename_or_data, basestring):
|
(w, h, data, metadata) = reader.read_flat()
|
||||||
filename_or_data = os.path.join(mcplatform.dataDir, filename_or_data)
|
data = numpy.array(data, dtype='uint8')
|
||||||
filename_or_data = filename_or_data.encode(sys.getfilesystemencoding())
|
data.shape = (h, w, metadata['planes'])
|
||||||
else:
|
if data.shape[2] == 1:
|
||||||
# path = numpy.fromstring(filename, 'uint8')
|
# indexed color. remarkably straightforward.
|
||||||
pass
|
data.shape = data.shape[:2]
|
||||||
try:
|
data = numpy.array(reader.palette(), dtype='uint8')[data]
|
||||||
img = image.load(filename_or_data)
|
|
||||||
|
if data.shape[2] < 4:
|
||||||
img = img.convert_alpha()
|
data = numpy.insert(data, 3, 255, 2)
|
||||||
|
|
||||||
data = numpy.fromstring(img.get_buffer().raw, 'uint8')
|
|
||||||
w, h = img.get_size()
|
|
||||||
data.shape = (h, w, 4) # xxx 32-bit images
|
|
||||||
|
|
||||||
format = GL.GL_BGRA
|
|
||||||
|
|
||||||
except Exception, e:
|
|
||||||
print repr(e), "while using pygame to load an image"
|
|
||||||
|
|
||||||
reader = png.Reader(filename_or_data)
|
|
||||||
(w, h, data, metadata) = reader.read_flat()
|
|
||||||
data = numpy.array(data, dtype='uint8')
|
|
||||||
data.shape = (h, w, metadata['planes'])
|
|
||||||
if data.shape[2] == 1:
|
|
||||||
# indexed color. remarkably straightforward.
|
|
||||||
data.shape = data.shape[:2]
|
|
||||||
data = numpy.array(reader.palette(), dtype='uint8')[data]
|
|
||||||
|
|
||||||
if data.shape[2] < 4:
|
|
||||||
data = numpy.insert(data, 3, 255, 2)
|
|
||||||
|
|
||||||
format = GL.GL_RGBA
|
|
||||||
|
|
||||||
if format == GL.GL_BGRA:
|
|
||||||
temp = numpy.array(data[:, :, 0])
|
|
||||||
data[:, :, 0] = data[:, :, 2]
|
|
||||||
data[:, :, 2] = temp
|
|
||||||
|
|
||||||
return w, h, data
|
return w, h, data
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user