--- ImageParser.py.orig	Tue Jan 22 21:01:13 2002
+++ ImageParser.py	Wed Jan 23 14:49:23 2002
@@ -239,143 +239,73 @@
 #####################################################################
 ##
 ## This is the Image Magick parser from Chris.  It depends on os.popen
-## and the availability of ImageMagick (convert) plus the Tbmp-tools
+## and the availability of ImageMagick (convert)
+## Tbmp-tools eliminated 12 Dec 2001 CRH
 ##
 ##
 
-class ImageMagickImageParser:
+class ImageMagickImageParser(ImageParser):
     "Convert an image to the PalmBitmap. Uses 'convert' from ImageMagick"
 
-    def __init__(self, url, type, data, config, attribs, compress=1):
-        self._url = url
-        self._verbose = config.get_int ('verbosity', 1)
-        self._config = config
-        self._maxwidth = attribs.get('maxwidth')
-        self._maxheight = attribs.get('maxheight')
-        self._bpp = attribs.get('bpp')
-        self._doc = PluckerDocs.PluckerImageDocument (str(url), config)
-        self._scaled = 0
-
+    def __init__(self, url, type, data, config, attribs, compress = 1):
+        ImageParser.__init__(self, url, type, data, config, attribs)
         tmpfile = tempfile.mktemp()
+        f = open(tmpfile, "w" + binary_flag)
+        f.write(self._bits)
+        f.close()
 
-        try:
-            try:
-                size_data = self.convert_to_pnm(data, tmpfile)
-                self._doc.set_data(self.convert_to_Tbmp(tmpfile))
-		if self._verbose > 1:
-		    sys.stderr.write("input image was " + str(size_data[0]) + ", maxwidth/height used was " + str(size_data[1]) + ", output image size is " + str(size_data[2]) + "\n")
-            finally:
-                try: os.unlink(tmpfile)
-                except: pass
-        except RuntimeError:
-            # This we pass through...
-            raise
-        except:
-            raise RuntimeError, "Error while converting image (%s)." % self._url
+        command = "identify -ping " + tmpfile
+        if self._verbose > 1:
+            print "Running: ", command
+        pipe = os.popen(command)
+        info = pipe.read()
+        pipe.close()
+        match = re.search(r"\s(\d+)x(\d+)\D", info)
+        if not match:
+            raise RuntimeError, "Can't determine image size from output of ImageMagick 'identify' program:  " + info
+        else:
+            self._size = (int(match.group(1)), int(match.group(2)))
+        try: os.unlink(tmpfile)
+        except: pass
 
 
-    def get_plucker_doc(self):
-        return self._doc
+    def size(self):
+        return self._size
 
 
     def scaled(self):
         return self._scaled
 
 
-    def convert_to_Tbmp(self, tmpfile):
-
-        ppmtoTbmp = self._config.get_string ('ppmtoTbmp_program', 'ppmtoTbmp')
-        if self._bpp == 2:
-            ppmtoTbmp = ppmtoTbmp + " -2bit"
-        elif self._bpp == 4:
-            ppmtoTbmp = ppmtoTbmp + " -4bit"
-
-        if self._verbose <= 1:
-            ppmtoTbmp = ppmtoTbmp + " -quiet"
-
-	outfile = tempfile.mktemp()
-
-        command = ppmtoTbmp + " " + tmpfile + " > " + outfile
-
-        if self._verbose > 1:
-            message(2, "Running: ", command)
-
-        try:
-            if os.system (command):
-                raise RuntimeError, "Error while executing command '%s'" % command
-            f = open (outfile, "rb")
-            data = f.read ()
-            f.close ()
-
-            if len (data) == 0:
-                # Oops, nothing fetched?!?
-                raise RuntimeError, "No data from parsing image! (%s)" % self._url
-	    elif len(data) > SimpleImageMaxSize:
-		raise RuntimeError, "Image data too large (%d bytes)!  Scale it down." % len(data)
-        finally:
-            try: os.unlink(outfile) 
-            except: pass
-        return data
-
-
-    def convert_to_pnm(self, data, tmpfile):        
-
-	infile = tempfile.mktemp()
-
+    def convert(self, width, height, bpp, section = None):
+        tmpfile = tempfile.mktemp()
         try:
-            #message(0, "Open infile " + infile)
-            f = open(infile, "wb")
-            f.write(data)
-            f.close()
-
             convert = self._config.get_string ('convert_program', 'convert')
-            maxwidth = self._config.get_string ('maxwidth', '150')
-            maxheight = self._config.get_string ('maxheight', '250')
+            size = " -geometry \"" + str(width) + "x" + str(height) + ">\" "
 
-            if self._maxheight != None:
-                maxheight = self._maxheight
-            if self._maxwidth != None:
-                maxwidth = self._maxwidth
-        
-	    command = "identify -ping " + infile
-	    message("Running: " + command)
-            pipe = os.popen(command)
-            info = pipe.read()
-            pipe.close()
-	    match = re.search(r"\s([0-9]+)x([0-9]+)[+\s]", info)
-	    if not match:
-		raise RuntimeError, "Can't determine image size from output of ImageMagick 'identify' program:  " + info
-	    else:
-		mywidth = int(match.group(1))
-		myheight = int(match.group(2))
-            if mywidth > int(maxwidth) \
-                    or myheight > int(maxheight):
-                self._scaled = 1
-		if self._verbose > 2: print '...image (natively %dx%d) must be scaled' % (mywidth, myheight)
-
-            size = "\"" + maxwidth + "x" + maxheight + ">\""
-
-            if self._bpp == 1:
-                ncolors = "2"
-            elif self._bpp == 2:
-                ncolors = "4"
+            if bpp == 1:
+                ncolors = " -monochrome -dither "
+            elif bpp == 2:
+                ncolors = " -colors 4 -colorspace GRAY -dither "
+            elif bpp == 4:
+                ncolors = " -colors 16 -colorspace GRAY -dither "
             else:
-                ncolors = "16"
-
-            if self._bpp == 1:
-                convert = convert + " -monochrome"
+                ncolors = " -colors 256 -dither "
 
-            command = convert + " -colors " + ncolors + " -dither -geometry " + size + " " + infile +" ppm:" + tmpfile
+            command = convert + ncolors + size + " - " + " palm:" + tmpfile
 
             if self._verbose > 1:
                 print "Running: ", command
-            if os.system (command):
-                raise RuntimeError, "Error while executing command '%s'" % command
-
-	    return ((mywidth, myheight), (int(maxwidth), int(maxheight)), (int(maxwidth), int(maxheight)))
-
+            pipe = os.popen(command, "w" + binary_flag)
+            pipe.write(self._bits)
+            pipe.close()
+            f = open(tmpfile, 'r' + binary_flag)
+            newbits = f.read()
+            f.close()
+            os.unlink(tmpfile)
+            return newbits
         finally:
-            try: os.unlink(infile) 
+            try: os.unlink(tmpfile)
             except: pass
 
 
