//package com.vistech.internet;
import javax.servlet.*;
import javax.servlet.http.*;

import java.io.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.image.renderable.*;
import java.util.*;
import javax.media.jai.*;
import com.sun.media.jai.codec.*;

public class ImageManipServlet extends HttpServlet{
   public void doGet(HttpServletRequest req, HttpServletResponse res)
                  throws ServletException{
      String dir = null;
      String fileList[] = null;
      PlanarImage image;
      try {
        res.setContentType("image/jpeg");
        ServletContext context = getServletContext();
        String par = req.getParameter("images");
        if (par == null) par = "lena.gif";
        double magFactor = 0.0, rotAngle = 0.0;

        String rot = req.getParameter("rotAngle");
        String mag = req.getParameter("magFactor");
        try {
           rotAngle = Math.toRadians((Double.valueOf(rot)).doubleValue());
        } catch(Exception e) {}
        try{
           magFactor = (Integer.valueOf(mag)).intValue()/100.0;
        } catch(Exception e) {}
        String imagepath = context.getRealPath(par);
        System.out.println("imagepath = "+imagepath);
        image = readAsPlanarImage(imagepath);
        PlanarImage outimage = manip(image, rotAngle, magFactor);
        ServletOutputStream out = res.getOutputStream();
        saveAsJPEG(outimage,out);
     } catch(Exception e) {System.out.println(e);}
   }

   public void doPost(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException {
        doGet(request, response);
   }

   public static PlanarImage readAsPlanarImage(String filename) {
      return JAI.create("fileload", filename);
   }

   public static void saveAsJPEG(RenderedImage image, OutputStream out)
           throws java.io.IOException {
      JPEGEncodeParam param = new JPEGEncodeParam();
      ImageEncoder encoder = ImageCodec.createImageEncoder("JPEG", out, param);
      encoder.encode(image);
      out.close();
   }

   protected PlanarImage manip(PlanarImage image, double rot, double mag) {
      if(( rot <= 0.001) && (mag <=0.0)) return image;
      RenderedOp op =null, op1=null, op2 =null;
      if(rot > 0.001){
         op1 = rotate(image, rot);
         if(mag <=0.0) return op1.createInstance();
         else {
           op2 = scale(op1, mag,mag);
           return op2.createInstance();
         }
      }else {
         op2 = scale(image, mag,mag);
         return op2.createInstance();
      }
   }

   public RenderedOp scale(PlanarImage image, double magx, double magy) {
      ParameterBlock pb = new ParameterBlock();
      pb.addSource(image);
      pb.add((float)magx);
      pb.add((float)magy);
      pb.add(0f);
      pb.add(0f);
      pb.add(Interpolation.getInstance(Interpolation.INTERP_NEAREST));
      return JAI.create("scale", pb);
   }

    public RenderedOp rotate(PlanarImage image, double ang) {
      ParameterBlock pb = new ParameterBlock();
      pb.addSource(image);
      pb.add((float)(image.getWidth())/2);
      pb.add((float)(image.getHeight())/2);
      pb.add((float)ang);
      pb.add(Interpolation.getInstance(Interpolation.INTERP_NEAREST));
      return JAI.create("rotate", pb);
   }

   public static void saveAsJPEG(RenderedImage image, String file)
           throws java.io.IOException {
      String filename = file;
      if(!filename.endsWith(".jpg"))filename = new String(file+".jpg");
      OutputStream out = new FileOutputStream(filename);
      JPEGEncodeParam param = new JPEGEncodeParam();
      ImageEncoder encoder = ImageCodec.createImageEncoder("JPEG", out, param);
      encoder.encode(image);
      out.close();
   }
}
