tons of changes
[org.ibex.xt.git] / src / org / ibex / xt / ImageConverter.java
1 // Decompiled by Jad v1.5.8c. Copyright 2001 Pavel Kouznetsov.
2 // Jad home page: http://www.geocities.com/kpdus/jad.html
3 // Decompiler options: packimports(3) 
4 // Source File Name:   ImageConverter.java
5
6 package org.ibex.xt;
7
8 import java.awt.*;
9 import java.awt.image.*;
10 import org.ibex.util.*;
11 import java.io.*;
12 import javax.servlet.*;
13 import javax.servlet.http.*;
14
15 public class ImageConverter extends HttpServlet {
16
17     public ImageConverter() { }
18
19     public void init(ServletConfig servletconfig) { cx = servletconfig.getServletContext(); }
20     public void doPost(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
21         throws IOException, ServletException { doGet(httpservletrequest, httpservletresponse); }
22
23     public void doGet(HttpServletRequest req, HttpServletResponse resp)
24         throws IOException, ServletException {
25         try {
26             if (req.getServletPath().indexOf("..") != -1) throw new IOException(".. not allowed in image paths");
27             String s = cx.getRealPath(req.getServletPath());
28             String s1 = s.indexOf('.') != -1 ? s.substring(0, s.lastIndexOf('.')) : s;
29             File file = new File((new StringBuilder()).append(s1).append(".png").toString());
30             if (!file.exists()) file = new File((new StringBuilder()).append(s1).append(".psd").toString());
31             if (!file.exists()) file = new File((new StringBuilder()).append(s1).append(".gif").toString());
32             if (!file.exists()) file = new File((new StringBuilder()).append(s1).append(".jpeg").toString());
33             if (file.getAbsolutePath().endsWith(".gif")) {
34                 resp.setContentType("image/gif");
35                 // FIXME slow
36                 resp.getOutputStream().write(InputStreamToByteArray.convert(new FileInputStream(file)));
37             } else if (file.getAbsolutePath().endsWith(".jpeg")) {
38                 resp.setContentType("image/jpeg");
39                 // FIXME slow
40                 resp.getOutputStream().write(InputStreamToByteArray.convert(new FileInputStream(file)));
41             } else if (file.getAbsolutePath().endsWith(".png")) {
42                 resp.setContentType("image/png");
43                 // FIXME slow
44                 resp.getOutputStream().write(InputStreamToByteArray.convert(new FileInputStream(file)));
45             } else if (file.getAbsolutePath().endsWith(".psd")) {
46                 PSDReader psdr = new PSDReader();
47                 psdr.read(new FileInputStream(file));
48                 Image img = psdr.getImage();
49                 if (req.getParameter("height") != null) {
50                     int height = Integer.parseInt(req.getParameter("height"));
51                     int width = (img.getWidth(null) * height) / img.getHeight(null);
52                     img = ((BufferedImage)img).getScaledInstance(width, height, Image.SCALE_SMOOTH);
53                 }
54                 System.out.println("bufferedimage is " + img);
55                 if (!(img instanceof BufferedImage)) {
56                     BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
57                     bi.getGraphics().drawImage(img, 0, 0, null);
58                     img = bi;
59                 }
60                 PngEncoderB pngenc = new PngEncoderB((BufferedImage)img, true);
61                 resp.setContentType("image/png");
62                 // FIXME slow
63                 resp.getOutputStream().write(pngenc.pngEncode(true));
64             }
65         } catch(Exception exception) {
66             exception.printStackTrace();
67             throw new ServletException(exception);
68         }
69     }
70
71     private ServletContext cx;
72 }