[Image-SIG] Extracting hotspots from xbm files

Bernhard Herzog bh@intevation.de
24 Apr 2001 16:51:11 +0200


Hi all,

here's a patch to the XbmImagePlugin so that it also extracts the
hot-spots (important for X-cursors). The hotspot co-ordinates are put
into the image's info dictionary under the key "hot_spot" as a 2-tuple.
It also makes the plugin use re instead of regex.

  Bernhard


*** PIL/XbmImagePlugin.py.orig	Tue Apr 24 16:27:59 2001
--- PIL/XbmImagePlugin.py	Tue Apr 24 16:42:27 2001
***************
*** 18,30 ****
  
  __version__ = "0.3"
  
! import regex, string
  import Image, ImageFile
  
  # XBM header
! xbm_head = regex.compile(
!     "#define[ \t]+[^_]*_width[ \t]+\([0-9]*\)[\r\n]+"
!     "#define[ \t]+[^_]*_height[ \t]+\([0-9]*\)[\r\n]+"
      "[\000-\377]*_bits\[\]"
  )
  
--- 18,34 ----
  
  __version__ = "0.3"
  
! import re, string
  import Image, ImageFile
  
  # XBM header
! xbm_head = re.compile(
!     "#define[ \t]+[^_]*_width[ \t]+(?P<width>[0-9]*)[\r\n]+"
!     "#define[ \t]+[^_]*_height[ \t]+(?P<height>[0-9]*)[\r\n]+"
!     "(?P<hot_spot>"
!     "#define[ \t]+[^_]*_x_hot[ \t]+(?P<xhot>[0-9]*)[\r\n]+"
!     "#define[ \t]+[^_]*_y_hot[ \t]+(?P<yhot>[0-9]*)[\r\n]+"
!     ")?"
      "[\000-\377]*_bits\[\]"
  )
  
***************
*** 40,56 ****
  
      def _open(self):
  
! 	s = xbm_head.match(self.fp.read(512))
  
! 	if s > 0:
! 
! 	    xsize = string.atoi(xbm_head.group(1))
! 	    ysize = string.atoi(xbm_head.group(2))
  
  	    self.mode = "1"
  	    self.size = xsize, ysize
  
! 	    self.tile = [("xbm", (0, 0)+self.size, s, None)]
  
  
  def _save(im, fp, filename):
--- 44,62 ----
  
      def _open(self):
  
! 	match = xbm_head.match(self.fp.read(512))
  
! 	if match is not None:
  
+ 	    xsize = string.atoi(match.group("width"))
+ 	    ysize = string.atoi(match.group("height"))
+ 	    if match.group("hot_spot") is not None:
+ 		self.info["hot_spot"] = (string.atoi(match.group("xhot")),
+ 					 string.atoi(match.group("yhot")))
  	    self.mode = "1"
  	    self.size = xsize, ysize
  
! 	    self.tile = [("xbm", (0, 0)+self.size, match.end(), None)]
  
  
  def _save(im, fp, filename):


-- 
Intevation GmbH                                 http://intevation.de/
Sketch                                 http://sketch.sourceforge.net/
MapIt!                                               http://mapit.de/