from OpenGL.GL import * from OpenGL.GLU import * from OpenGL.GLUT import * import sys, string try: import Image # try importing from PIL except: from wxPython.wx import * # else try importing wxPython class myImg: def __init__(self, fname): ending = string.split(fname, '.')[1] if ending == 'jpg': try: img = Image.open(fname) # opens the image file using PIL if img.mode != 'RGB': img = img.convert('RGB') self.data = img.tostring('raw', 'RGB', 0, -1) self.w = img.size[0] self.h = img.size[1] except: wxImage_AddHandler(wxJPEGHandler()) wximg = wxImage(fname, wxBITMAP_TYPE_JPEG) # opens the image file using wxPython self.w = wximg.GetWidth() self.h = wximg.GetHeight() self.data = wximg.GetData() self.flip() elif ending == 'ppm': # read ppm files f = open(fname, 'rb') f.readline(); f.readline() self.w, self.h = tuple(map(int, string.split(f.readline()))) f.readline() self.data = f.read() self.flip() def flip(self): """Assume we have loaded in an RGB image, flip it vertically.""" w = 3*self.w tmp = [] for i in range(self.h): tmp.insert(0, self.data[i*w: (i+1)*w]) self.data = string.join(tmp, '') def init(): glClearColor(0,0,0,0) glColor3(1,0,0) # draw in red glPixelStorei(GL_UNPACK_ALIGNMENT, 1) glShadeModel(GL_FLAT) global texName texName = glGenTextures(1) glBindTexture(GL_TEXTURE_2D, texName) gluBuild2DMipmaps(GL_TEXTURE_2D, 3, img.w, img.h, GL_RGB, GL_UNSIGNED_BYTE, img.data) glTexImage2D(GL_TEXTURE_2D, 0, 3, img.w, img.h, 0, GL_RGB, GL_UNSIGNED_BYTE, img.data) glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) glEnableClientState( GL_VERTEX_ARRAY ) glEnableClientState( GL_TEXTURE_COORD_ARRAY ) def reshape(w,h): global winsize winsize = (w,h) glViewport(0,0,w,h) glMatrixMode(GL_PROJECTION) glLoadIdentity() glOrtho(0,w,0,h,-1,1) glMatrixMode(GL_MODELVIEW) def glvt(u,v,x,y,z=0): glTexCoord2f(u,v) glVertex3f(x,y,z) def glError(): e = glGetError() if e: print gluErrorString(e) else: print e def display(): glClear(GL_COLOR_BUFFER_BIT) # draw the raw pixels glRasterPos2(0,0) glDrawPixels(img.w, img.h, GL_RGB, GL_UNSIGNED_BYTE, img.data) glDisableClientState( GL_TEXTURE_COORD_ARRAY ) # draw the texture subset in red lines tc = [(.1,.1),(.9,.1),(.3,.2),(.2,.9)] v = [] for t in tc: v.append((128*t[0], 128*t[1], .5)) glVertexPointerf( v ) glDrawArrays( GL_LINE_LOOP, 0, 4 ) glEnable(GL_TEXTURE_2D) end = 128 # draw the full image as a texture end = end + winsize[1] """ v = [[128,0],[end,0],[end,winsize[1]],[128,winsize[1]]] t = Numeric.array([[0.,0.],[1.,0.],[1.,1.],[0.,1.]]) #t = [[0.,0.],[1.,0.],[1.,1.],[0.,1.]] glVertexPointerf( v ) #glTexCoordPointerf( [[0.,0.],[1.,0.],[1.,1.],[0.,1.]] ) glBindTexture(GL_TEXTURE_2D, texName) print texName glEnableClientState( GL_TEXTURE_COORD_ARRAY ) glTexCoordPointerf( t ) #glTexCoordPointer( 2, GL_FLOAT, 0, t.tostring() ) glDrawArrays( GL_QUADS, 0, 4 ) """ glBegin(GL_QUADS) glvt(0,0,128,0) glvt(1,0,end,0) glvt(1,1,end,winsize[1]) glvt(0,1,128,winsize[1]) glEnd() # draw the subset start = end + 1 vc = [(.1,.1), (1,.2), (.4,.3), (.25, .7)] glBegin(GL_TRIANGLES) for i in (0,1,2,2,0,3): glvt(tc[i][0], tc[i][1], start+winsize[1]*vc[i][0], winsize[1]*vc[i][1]) glEnd() glDisable(GL_TEXTURE_2D) glutSwapBuffers() def main(prg, fname): glutInit([]) glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB) glutInitWindowSize(600,200) glutInitWindowPosition(300,100) glutCreateWindow(prg) glutReshapeFunc (reshape) glutDisplayFunc (display) global img img = myImg(fname) init() glutMainLoop() if __name__ == '__main__': try: fname = sys.argv[1] except: sys.stderr.write('Usage: ' + sys.argv[0] + ' imgfile\n') sys.exit(1) main(sys.argv[0], fname)