Created
December 18, 2014 23:24
-
-
Save hogjonny/44ac2987d2ee211237a5 to your computer and use it in GitHub Desktop.
Simple DDS file loader ... load a DDS file as a QImage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#coding:utf-8 | |
__author__ = '[email protected]' | |
## reference: https://gist.github.com/bjorn/4635382 | |
# -- This line is 75 characters ------------------------------------------- | |
# example code | |
#QImage readDDSFile(const QString &filename) | |
#{ | |
#QGLWidget glWidget; | |
#glWidget.makeCurrent(); | |
#GLuint texture = glWidget.bindTexture(filename); | |
#if (!texture) | |
#return QImage(); | |
#// Determine the size of the DDS image | |
#GLint width, height; | |
#glBindTexture(GL_TEXTURE_2D, texture); | |
#glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width); | |
#glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height); | |
#if (width == 0 || height == 0) | |
#return QImage(); | |
#QGLPixelBuffer pbuffer(QSize(width, height), glWidget.format(), &glWidget); | |
#if (!pbuffer.makeCurrent()) | |
#return QImage(); | |
#pbuffer.drawTexture(QRectF(-1, -1, 2, 2), texture); | |
#return pbuffer.toImage(); | |
#} | |
#from PySide import QtGui, QtCore | |
import os, sys | |
from OpenGL.GL import * | |
#import numpy as np | |
from PySide.QtOpenGL import * | |
from PySide.QtGui import * | |
from PySide.QtCore import * | |
# global debug config set up | |
global gDebug | |
from bp_config.debugConfig import gDebug, setDebug | |
# my path convenience class, should be easy to remove/replace | |
from bp_files.bp_path import Path | |
########################################################################### | |
## DDS file reader | |
class fileDDS(object): | |
"""This class represnts a generic DDS file as a QImage""" | |
global gDebug | |
# --BASE-METHODS------------------------------------------------------- | |
# --constructor- | |
def __init__(self, filePath=None): | |
self._filePath = None | |
self._width = int(0) | |
self._height = int(0) | |
self._qImage = None | |
if filePath != None: | |
self._filePath = Path(filePath) | |
self._qImage = self.readDDSFile(self._filePath) | |
# --method------------------------------------------------------------- | |
def readDDSFile(self, filePath=None): | |
glWidget = QGLWidget() | |
glWidget.makeCurrent() | |
texture = glWidget.bindTexture(filePath) | |
if not texture: | |
return QImage() | |
# Determine the size of the DDS image | |
glBindTexture(GL_TEXTURE_2D, texture) | |
self._width = glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH) | |
self._height = glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT) | |
if gDebug: | |
print 'Image W/H:: {0}, {1}'.format(self._width, self._height) | |
if self._width == 0 and self._height == 0: | |
return QImage() | |
pbuffer = QGLPixelBuffer(QSize(self._width, self._height), glWidget.format()) | |
if not pbuffer.makeCurrent(): | |
return QImage() | |
pbuffer.drawTexture(QRectF(-1, -1, 2, 2), texture) | |
return pbuffer.toImage() | |
########################################################################### | |
# --call block------------------------------------------------------------- | |
if __name__ == "__main__": | |
'''Allows the script to be run as main()''' | |
global gDebug | |
gDebug = setDebug(True) | |
from bp_files.bp_path import Path # path convenience class | |
global myApp | |
myApp = QApplication(sys.argv) | |
myApp.setStyle('Plastique') | |
testPath = Path(r'testDDS.dds') | |
image = fileDDS(testPath)._qImage | |
# Should get back a QImage | |
print image | |
sys.exit(myApp.exec_()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment