[Image-SIG] TGA trouble

Joerg Baumann Joerg.Baumann@stud.informatik.uni-erlangen.de
Mon, 7 Aug 2000 21:58:33 +0200 (MET DST)


On Sat, 5 Aug 2000, rhyde99 wrote:

> I would like to be able to load images rendered by the Lightflow rendering 
> tools (www.lightflowtech.com), but unfortunately it renders only to a tga
> format not readable by PIL.  It actually seems to be trying to load it as
> a pcx image.  I can provide a Lightflow-rendered image upon request, so
> that anyone interested can check this out.
> 
> I know little about the inner workings of this (or any) image file format, 
> so I can rule out writing my own decoder.  does anyone have the 
> knowledge and willingness to help me with this?
> 
Hi,

There were 2 little bugs in PIL:
* TgaImagePlugin.py could only read TGAs with an Image ID field of 
  length zero.
* TGAs with an Image ID field of length 0x0a were classified as PCX

There is a diff for TgaImagePlugin.py and PcxImagePlugin.py below.
File format information was obtained from www.wotsit.org (It's worth a
to remember).

If you have any problems with this diff, feel free to contact me, but I
want to make certain that everyone understands that there is _no_ warranty
for this free software.

have fun,
  joerg
   
  Joerg Baumann       joerg.baumann@stud.informatik.uni-erlangen.de
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+  
  Beware of bugs in the above code; I have only proved it correct, 
  not tried it.
                  Don Knuth 


*** PcxImagePlugin.py	Wed Jun  7 15:17:04 2000
--- PcxImagePlugin.py	Mon Aug  7 21:46:21 2000
***************
*** 31,35 ****
  
  def _accept(prefix):
!     return ord(prefix[0]) == 10 and ord(prefix[1]) in [0, 2, 3, 5]
  
  class PcxImageFile(ImageFile.ImageFile):
--- 31,35 ----
  
  def _accept(prefix):
!     return ord(prefix[0]) == 10 and ord(prefix[1]) in [0, 2, 3, 5] and
ord(prefix[2]) == 1 and ord(prefix[3]) in [255, 2, 4,8]
  
  class PcxImageFile(ImageFile.ImageFile):
*** TgaImagePlugin.py	Wed Jun  7 15:17:04 2000
--- TgaImagePlugin.py	Mon Aug  7 21:39:52 2000
***************
*** 42,47 ****
  
  def _accept(prefix):
!     return prefix[0] == "\0"
! 
  
  class TgaImageFile(ImageFile.ImageFile):
--- 42,52 ----
  
  def _accept(prefix):
!     colormaptype = ord(prefix[1])
!     imagetype = ord(prefix[2])   
!     if colormaptype==0:
! 	return imagetype in (2,3,10,11)
!     elif colormaptype==1:
! 	return imagetype in (1,9)
!     else: return []
  
  class TgaImageFile(ImageFile.ImageFile):
***************
*** 57,61 ****
  	s = self.fp.read(18)
  
! 	id = ord(s[0])
  
  	colormaptype = ord(s[1])
--- 62,68 ----
  	s = self.fp.read(18)
  
! 	id_length = ord(s[0])
!         #read id_length bytes of image id descriptor
!         id=self.fp.read(id_length)
  
  	colormaptype = ord(s[1])
***************
*** 69,73 ****
  
  	# validate header fields
! 	if id != 0 or colormaptype not in (0, 1) or\
  	   self.size[0] <= 0 or self.size[1] <= 0 or\
  	   depth not in (8, 16, 24, 32):
--- 76,80 ----
  
  	# validate header fields
! 	if colormaptype not in (0, 1) or\
  	   self.size[0] <= 0 or self.size[1] <= 0 or\
  	   depth not in (8, 16, 24, 32):