Skip to main content

Servlet to transform GIF (both transparent and non-transparent) to JPEG

1 reply [Last post]
johnp
Offline
Joined: 2005-05-02

Hello,

Using JAI I'm writing a servlet to decode a transparent GIF and write
it to the output stream as an encoded JPEG. I can't quite get it to
work. I either get a broken image or a solid color. Below is my source
code. Any help would be greatly appriciated.

Thanks

John P.

package leads;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.sql.*;
import oracle.sql.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.color.*;
import java.awt.image.renderable.*;
import com.sun.media.jai.codec.*;
import javax.media.jai.*;
import javax.media.jai.operator.*;

public class GetImageConveter extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
System.out.println("YOU GOT HERE - VERY BEGINNING");
Statement stmt = null;
ResultSet result = null;
String id = req.getParameter("id");
System.out.println("YOU GOT HERE - ID: " + id);
if (id == null || id.trim().length() == 0)
return;

String query = "select img_blob from lds_image where id = " + id;
Connection conn = LeadsUtil.getConnection();
InputStream is = null;

try
{
stmt = conn.createStatement();
result = stmt.executeQuery(query);
if (!result.next())
throw new Exception("Debug: cannot not fetch image from db");

resp.setContentType("image/jpe

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Brian Burkhalter

You could use

http://java.sun.com/j2se/1.5.0/docs/api/java/awt/image/IndexColorModel.html#convertToIntDiscrete(java.awt.image.Raster,%20boolean)

to convert the GIF RenderedImage data to a BufferedImage of type TYPE_INT_RGB
or TYPE_INT_ARGB. If the latter then you would need to sub-band the data to
get an RGB image. Once you have an RGB image you should be able to save that
as a JPEG.

Brian

On Mon, 2 May 2005 jai-interest@javadesktop.org wrote:

> Hello,
>
>
> Using JAI I'm writing a servlet to decode a transparent GIF and write
> it to the output stream as an encoded JPEG. I can't quite get it to
> work. I either get a broken image or a solid color. Below is my source
> code. Any help would be greatly appriciated.
>
>
> Thanks
>
>
> John P.
>
>
> package leads;
>
>
> import java.io.*;
> import javax.servlet.*;
> import javax.servlet.http.*;
> import java.util.*;
> import java.sql.*;
> import oracle.sql.*;
> import java.awt.*;
> import java.awt.image.*;
> import java.awt.color.*;
> import java.awt.image.renderable.*;
> import com.sun.media.jai.codec.*;
> import javax.media.jai.*;
> import javax.media.jai.operator.*;
>
>
> public class GetImageConveter extends HttpServlet
> {
> public void doGet(HttpServletRequest req, HttpServletResponse resp)
> throws ServletException, IOException
> {
> System.out.println("YOU GOT HERE - VERY BEGINNING");
> Statement stmt = null;
> ResultSet result = null;
> String id = req.getParameter("id");
> System.out.println("YOU GOT HERE - ID: " + id);
> if (id == null || id.trim().length() == 0)
> return;
>
>
> String query = "select img_blob from lds_image where id = " + id;
> Connection conn = LeadsUtil.getConnection();
> InputStream is = null;
>
>
> try
> {
> stmt = conn.createStatement();
> result = stmt.executeQuery(query);
> if (!result.next())
> throw new Exception("Debug: cannot not fetch image from db");
>
>
> resp.setContentType("image/jpe­g");
> is = result.getBinaryStream(1);
> System.out.println("YOU GOT HERE 1");
> ForwardSeekableStream Fstream = new ForwardSeekableStream(is);
> String[] names = ImageCodec.getDecoderNames(Fst­ream);
>
>
> for(int i = 0; i < names.length; i++){
> System.out.println("STREAM TYPE: " + names[i]);
> }
>
>
> ImageEncoder imgEnc = null;
> ImageDecoder decoder =
> ImageCodec.createImageDecoder(­"GIF",is,null);
> RenderedImage img = decoder.decodeAsRenderedImage(­);
> imgEnc = ImageCodec.createImageEncoder(­"JPEG",
> resp.getOutputStream(), null);
>
>
> if(img.getColorModel().hasAlph­a() == true){
> Raster img_raster = decoder.decodeAsRaster();
> int height = img_raster.getHeight();
> int width = img_raster.getWidth();
> Rectangle img_rect = new Rectangle(0, 0, width, height);
> WritableRaster writable_raster =
> img_raster.createCompatibleWri­tableRaster();
> BufferedImage bi = new BufferedImage(img.getColorMode­l(),
> writable_raster, false, null);
> int counter = 0;
> for(int a = 0; a < width; a++){
> for(int b = 0; b < height; b++){
> int rgba = bi.getRGB(a, b);
> int red = (rgba >> 16) & 0xff;
> int green = (rgba >> 8) & 0xff;
> int blue = rgba & 0xff;
> int alpha = (rgba >> 24) & 0xff;
> bi.setRGB(a,b,((red << 16) | (green << 8) | blue));
> counter++;
> }
> }
> System.out.println("Pixel Counter: " +
> Integer.toString(counter));
> System.out.println("Second Branch");
> width = bi.getWidth();
> height = bi.getHeight();
> // BufferedImage rgbImage = new BufferedImage(width,
> height, BufferedImage.TYPE_INT_RGB);
> BufferedImage rgbImage = new BufferedImage(width, height,
> BufferedImage.TYPE_INT_RGB);
> Graphics2D g = rgbImage.createGraphics();
> g.drawImage(bi, 0, 0, width, height, Color.white, null);
> // g.drawImage(bi, null, 0, 0);
> g.dispose();
> bi = rgbImage;
> RenderedOp op = FormatDescriptor.create(bi, new
> Integer(DataBuffer.TYPE_BYTE), null);
> imgEnc.encode(op);
> System.out.println("YOU GOT HERE 2");
> }
> else{
> System.out.println("Second Branch");
> imgEnc.encode(img);
> }
>
>
> }
> catch (Exception e)
> {
> System.out.println("Debug: cannot get image id = " + id + " " +
> e.toString());
> }
> finally{
> try{
> if(result != null)
> result.close();
> if(stmt != null)
> stmt.close();
> }
> catch(SQLException e)
> {
> System.out.println("Debug: cannot close result set/
> statement: " + e.toString());
> }
> LeadsUtil.freeConnection(conn)­;
> }
> }
> ---
> [Message sent by forum member 'johnp' (John Pallozzi)]
>
> http://www.javadesktop.org/forums/thread.jspa?messageID=76549&#76549
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
> For additional commands, e-mail: interest-help@jai.dev.java.net
>
>

----------------
Brian Burkhalter
Advanced Development, Graphics and Media
Software Chief Technology Office
Sun Microsystems, Inc.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This email message is for the sole use of the intended recipient(s)
and may contain confidential and privileged information. Any
unauthorized review, use, disclosure or distribution is prohibited.
If you are not the intended recipient, please contact the sender by
reply email and destroy all copies of the original message.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
For additional commands, e-mail: interest-help@jai.dev.java.net