--- loncom/homework/imageresponse.pm	2003/07/31 20:38:12	1.32
+++ loncom/homework/imageresponse.pm	2003/09/22 20:49:01	1.35
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # image click response style
 #
-# $Id: imageresponse.pm,v 1.32 2003/07/31 20:38:12 ng Exp $
+# $Id: imageresponse.pm,v 1.35 2003/09/22 20:49:01 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -25,13 +25,16 @@
 #
 # http://www.lon-capa.org/
 #
-
+# July,August 2003 H. K. Ng
+#
 #FIXME LATER assumes multiple possible submissions but only one is possible 
 #currently
 
 package Apache::imageresponse;
+use Apache::randomlylabel;
 use strict;
 use Image::Magick;
+use GD;
 
 BEGIN {
   &Apache::lonxml::register('Apache::imageresponse',('imageresponse'));
@@ -110,7 +113,7 @@ sub displayfoils {
     if ($target eq 'tex') {$result.="\\vskip 0 mm \n";} else {$result.="<br />\n";}
     my $image=$Apache::response::foilgroup{"$name.image"};
     &Apache::lonxml::debug("image is $image");
-    if ($Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"} =~ /^correct/ ) {
+    if ( &Apache::response::show_answer() ) {
       if ($target eq 'tex') {
 	$result.=$Apache::response::foilgroup{"$name.image"}."\\vskip 0 mm \n";
       } else {
@@ -325,25 +328,27 @@ sub end_image {
     my $y=$ENV{"form.HWVAL_$id:$temp.y"};
     if (defined ($x) && defined ($y)) {
 	&Apache::lonxml::debug("x and y defined as $x,$y");
-	my $magickImage = Image::Magick->new;
-	my $currentImage = $magickImage->Read('/home/httpd/html'.$image);
+	my $currentImage = &Apache::randomlylabel::get_image('/home/httpd/html'.$image,1);
+	if (! defined($currentImage)) {
+	    &Apache::lonnet::logthis('Unable to create image object for '.$image);
+	    return '';
+	}
+	my $red;
+	if (!($red = $currentImage->colorResolve(255,0,0))) {
+	    $red = $currentImage->colorClosestHWB(255,0,0);
+	}
 	my $length = 6;
-	$currentImage = $magickImage->Draw(primitive=>'line',
-					   stroke=>'red',
-					   points=>($x-$length).','.($y-$length).' '.
-					   ($x+$length).','.($y+$length));
-	$currentImage = $magickImage->Draw(primitive=>'line',
-					   stroke=>'red',
-					   points=>($x-$length).','.($y+$length).' '.
-					   ($x+$length).','.($y-$length));
-	binmode STDOUT;
-	my ($graphExt) = ($image =~ /.*\.(.*)$/);
-	&Apache::lonxml::debug("graph mime type $graphExt");
-	my $webImageName = "/prtspool/$ENV{'user.name'}_$ENV{'user.domain'}_".(time).'.'.$graphExt; #needs to be more random
+	$currentImage->line($x-$length,$y-$length,$x+$length,$y+$length,$red);
+	$currentImage->line($x-$length,$y+$length,$x+$length,$y-$length,$red);
+
+	my ($nameWOext) = ($image =~ /^.*\/(.*)\..*$/);
+	&Apache::lonxml::debug("graph name $nameWOext");
+	my $webImageName = "/prtspool/$ENV{'user.name'}_$ENV{'user.domain'}_".
+	    $nameWOext.'.png'; #needs to be more random or specific
 	my $newImageName = '/home/httpd'.$webImageName;
 
-	$currentImage = $magickImage->Write($graphExt.':'.$newImageName);
-	undef $magickImage;
+	my $imgfh = Apache::File->new('>'.$newImageName); 
+	print $imgfh $currentImage->png;
 	$image = $webImageName;
     }
     &Apache::lonxml::debug("out image is $image");