Advertisement
Guest User

PyQt TGA QImage

a guest
Jun 11th, 2010
408
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.58 KB | None | 0 0
  1. import sys
  2. from PyQt4 import QtGui
  3.  
  4. def main(*args, **kwargs):
  5.     return readTga(*args, **kwargs)
  6.  
  7. def readTga(file, asPixmap=True):
  8.     'Given the path to a TGA file, return either a QImage or QPixmap if asPixmap is True'
  9.     with open(file, 'rb') as f:
  10.         header = f.read(12)
  11.         imageSpec = f.read(6)
  12.        
  13.         header = [ord(c) for c in header]
  14.         imageSpec = [ord(c) for c in imageSpec]
  15.        
  16.         width = imageSpec[1]*256+imageSpec[0]
  17.         height = imageSpec[3]*256+imageSpec[2]
  18.         bitDepth = imageSpec[4]
  19.         bytesPerPixel = bitDepth/8
  20.         descriptor =imageSpec[5]
  21.        
  22.         bits = []
  23.         for i in xrange(8):
  24.             bits.append((descriptor >> i) & 1)
  25.        
  26.         bottomUp = bool(not bits[5])
  27.         rightToLeft = bool(bits[4])
  28.        
  29.         #print 'Read tga - Image info:'
  30.         #print '- (width, height, bitDepth, bytesPerPixel)', width, height, bitDepth, bytesPerPixel
  31.         image = QtGui.QImage(width, height, QtGui.QImage.Format_ARGB32)
  32.        
  33.         if header[2] == 2:
  34.             #print '- True color, uncompressed'
  35.             for y in range(height):
  36.                 for x in range(width):
  37.                     color = getPixel(f, bytesPerPixel)
  38.                    
  39.                     pX = x
  40.                     pY = y
  41.                     if rightToLeft:
  42.                         pX = (width-1)-x
  43.                    
  44.                     if bottomUp:
  45.                         pY = (height-1)-y
  46.                     image.setPixel(pX, pY, color)
  47.        
  48.         if header[2] == 10:
  49.             #print '- True color, rle compressed'
  50.             y=0
  51.             x=0
  52.             while y<height:
  53.                 while x<width:
  54.                     #rle chunk
  55.                     pack = ord(f.read(1))
  56.                     #if pack is less than 127, indicates how many normal pixels until the next rle pack value
  57.                     #if pack is more than 127, the distance from 127 is how many times to repeat the same pixel
  58.                     if pack>127:
  59.                         length = pack-127
  60.                         color = getPixel(f, bytesPerPixel)
  61.                     else:
  62.                         length = pack + 1
  63.                    
  64.                     for i in range(length): #set the chunk pixels
  65.                         if pack<=127:
  66.                             color = getPixel(f, bytesPerPixel)
  67.                         pX = x
  68.                         pY = y
  69.                         if rightToLeft:
  70.                             pX = (width-1)-x
  71.                        
  72.                         if bottomUp:
  73.                             pY = (height-1)-y
  74.                        
  75.                         image.setPixel(pX, pY, color)
  76.                         x+=1
  77.                 x=0
  78.                 y+=1
  79.         else:
  80.             raise Exception, 'Header value %s is not supported by loadTga.'%header[2]
  81.        
  82.         if asPixmap:
  83.             image = QtGui.QPixmap().fromImage(image)
  84.         return image
  85.  
  86. def getPixel(f, bytesPerPixel):
  87.     'Given the file object f, and number of bytes per pixel, read in the next pixel and return a qRgba uint'
  88.     pixel = []
  89.     for i in range(bytesPerPixel):
  90.         pixel.append(ord(f.read(1)))
  91.    
  92.     if bytesPerPixel==4:
  93.         pixel = [pixel[2], pixel[1], pixel[0], pixel[3]]
  94.         color = QtGui.qRgba(*pixel)
  95.     else:
  96.         pixel = [pixel[2], pixel[1], pixel[0]]
  97.         color = QtGui.qRgb(*pixel)
  98.     return color
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement