FileDocCategorySizeDatePackage
DeColorize.javaAPI DocExample1978Tue Jan 25 10:45:14 GMT 2000None

DeColorize

public class DeColorize extends HttpServlet

Fields Summary
Hashtable
gifs
Constructors Summary
Methods Summary
public voiddoGet(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)


       
                                  
    res.setContentType("image/gif");
    ServletOutputStream out = res.getOutputStream();

    // Get the image location from the path info
    String source = req.getPathTranslated();
    if (source == null) {
      throw new ServletException("Extra path information " +
                                 "must point to an image");
    }

    // Short circuit if it's been done before
    if (gifs.containsKey(source)) {
      ByteArrayOutputStream baos = (ByteArrayOutputStream) gifs.get(source);
      baos.writeTo(out);
      return;
    }

    // Construct an unshown frame
    // No addNotify() since its peer isn't needed
    Frame frame = new Frame();

    // Load the image
    Image image = Toolkit.getDefaultToolkit().getImage(source);
    MediaTracker mt = new MediaTracker(frame);
    mt.addImage(image, 0);
    try {
      mt.waitForAll();
    }
    catch (InterruptedException e) {
      getServletContext().log(e, "Interrupted while loading image");
      throw new ServletException(e.getMessage());
    }

    // Get the size of the image
    int width = image.getWidth(frame);
    int height = image.getHeight(frame);

    // Create an image to match, run through a filter
    Image filtered = frame.createImage(
      new FilteredImageSource(image.getSource(),
                              new GrayscaleImageFilter()));

    // Encode, store, and return the filtered image
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GifEncoder encoder = new GifEncoder(filtered, baos);
    encoder.encode();
    gifs.put(source, baos);
    baos.writeTo(out);