--- ImageParser.py.orig	Wed Nov 21 15:06:24 2001
+++ ImageParser.py	Thu Nov 22 22:36:59 2001
@@ -207,141 +207,69 @@
 ##
 ##
 
-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
+	ImageParser.__init__(self, url, type, data, config, attribs)
+        self._infile = tempfile.mktemp()
+        f = open(self._infile, "wb")
+        f.write(self._bits)
+        f.close()
 
-        tmpfile = tempfile.mktemp()
-
-        try:
-            try:
-                size_data = self.convert_to_pnm(data, tmpfile)
-                self._doc.set_data(self.convert_to_Tbmp(tmpfile))
-		self._doc.set_size_data(size_data)
-		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 " + self._infile
+        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)))
 
 
-    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:
-            print "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:
-            #print "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')
+            print width, height
+            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
-            if self._verbose > 1:
-                print "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 "
+                format = " palm1:"
+            elif bpp == 2:
+                ncolors = " -colors 4 -dither "
+                format = " palm2:"
+            elif bpp == 4:
+                ncolors = " -colors 16 -dither "
+                format = " palm4:"
             else:
-                ncolors = "16"
+                ncolors = " -colors 256 -dither "
+                format = " palm8:"
 
-            if self._bpp == 1:
-                convert = convert + " -monochrome"
-
-            command = convert + " -colors " + ncolors + " -dither -geometry " + size + " " + infile +" ppm:" + tmpfile
+            command = convert + ncolors + size + self._infile + format + 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)))
-
+	    f = open(tmpfile, 'r'+binary_flag)
+	    newbits = f.read()
+	    f.close()
+	    return newbits
         finally:
-            try: os.unlink(infile) 
+            try: os.unlink(tmpfile)
             except: pass
 
 
