Resizing GIF images: please help!

Discussion in 'Java' started by Clyde Ellul, Oct 16, 2003.

  1. Clyde Ellul

    Clyde Ellul Guest

    Hi there.

    I need to write a simple program that reads a GIF image from an input
    stream, resizes it, then writes it back to an output stream in the
    same format (GIF). (JPEG input/output is good too, but GIF is
    preferrable). JAI can do this sort of but can't write to GIF format.
    Also, it looks too complicated for what I need; I'm a bit in a
    hurry...

    Is there a simple way of doing what I want? Is there a free,
    open-source library that does just that? I think I may know how to
    read/decode and scale the image, but I don't know how to encode it to
    a new, resized GIF file and I don't have time to write my own encoder.
    (In fact I can't afford more than an hour on this!)

    Any help will be highly appreciated.

    Regards,
    Clyde
     
    Clyde Ellul, Oct 16, 2003
    #1
    1. Advertising

  2. comp.lang.java removed (not a newsgroup anymore)
    fup2 comp.lang.java.programmer

    Please set a followup-to if you must cross-post!

    Clyde Ellul:

    [...]

    >Is there a simple way of doing what I want? Is there a free,
    >open-source library that does just that? I think I may know how to
    >read/decode and scale the image, but I don't know how to encode it to
    >a new, resized GIF file and I don't have time to write my own encoder.
    >(In fact I can't afford more than an hour on this!)


    There is a free implementation with source code, but depending on
    where the code is used (or written, or whatever, I'm not a lawyer) you
    still must pay licensing fees for LZW to Unisys.

    Here are some GIF encoders:
    <http://www.geocities.com/marcoschmidt.geo/java-image-coding.html>.

    Regards,
    Marco
    --
    Please reply in the newsgroup, not by email!
    Java programming tips: http://jiu.sourceforge.net/javatips.html
    Other Java pages: http://www.geocities.com/marcoschmidt.geo/java.html
     
    Marco Schmidt, Oct 16, 2003
    #2
    1. Advertising

  3. Clyde Ellul

    Roedy Green Guest

    On 15 Oct 2003 16:45:31 -0700, (Clyde Ellul) wrote or
    quoted :

    >Is there a simple way of doing what I want? Is there a free,
    >open-source library that does just that?


    There are utilities like photoshop and Paint Shop Pro that likely can
    be automated to process a batch of images.

    In Java you can convert images to what amount to a matrix of ints with
    r+b+g+alpha 8bit components. You can then write those back in
    formats.

    See http://minpprod.com/jgloss/jimi.html and follow links for tools to
    do that.

    That still leaves you to manipulate the image bit by bit. Java of
    course can scale images for you happily with drawImage.


    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Oct 16, 2003
    #3
  4. Clyde Ellul

    Roedy Green Guest

    On Thu, 16 Oct 2003 02:00:21 +0200, Marco Schmidt
    <> wrote or quoted :

    >There is a free implementation with source code, but depending on
    >where the code is used (or written, or whatever, I'm not a lawyer) you
    >still must pay licensing fees for LZW to Unisys.


    Has that patent not yet expired?

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Oct 16, 2003
    #4
  5. Roedy Green:

    >>There is a free implementation with source code, but depending on
    >>where the code is used (or written, or whatever, I'm not a lawyer) you
    >>still must pay licensing fees for LZW to Unisys.

    >
    >Has that patent not yet expired?


    In some places it has not expired yet. According to
    <http://burnallgifs.org/>:

    20 June 2003: The LZW patent expires today in the United States.
    However, patents on LZW are still in force in other countries. In mail
    dated Monday, 23 June 2003, Unisys attorney Mark Starr asserts that
    LZW patents are still in force in Canada, France, Italy, Germany, the
    United Kingdom, and Japan.

    Obviously, this is biased, but even if it's true, I don't know what it
    means exactly. Can someone write a GIF encoder in Germany (patent
    still exists), and someone in the USA (patent has expired) can use it
    freely? What about writing it in Germany and offering it on a server
    in the USA? I'm not sure, and I'm not eager to find out in a court of
    law.

    Regards,
    Marco
    --
    Please reply in the newsgroup, not by email!
    Java programming tips: http://jiu.sourceforge.net/javatips.html
    Other Java pages: http://www.geocities.com/marcoschmidt.geo/java.html
     
    Marco Schmidt, Oct 16, 2003
    #5
  6. Marco Schmidt:

    >Here are some GIF encoders:
    ><http://www.geocities.com/marcoschmidt.geo/java-image-coding.html>.


    I overlooked the question for resizing. Here's some code for that
    which uses only standard parts of the Java runtime library (at least
    for scaling):
    <http://www.geocities.com/marcoschmidt.geo/java-save-jpeg-thumbnail.html>.
    It's the drawImage approach Roedy has mentioned already.

    Regards,
    Marco
    --
    Please reply in the newsgroup, not by email!
    Java programming tips: http://jiu.sourceforge.net/javatips.html
    Other Java pages: http://www.geocities.com/marcoschmidt.geo/java.html
     
    Marco Schmidt, Oct 16, 2003
    #6
  7. Clyde,

    I've written a quick image resizing class, ImageResizer, using the
    javax.JavaIO class (requires JDK1.4) as you mentioned. The encoding/decoding
    is not that complicated, but as you already know it does not support writing
    GIFs (since the GIF format is owned by AOL?). It does however support
    reading GIFs, but from the little I have tested the code the results of
    encoding a read GIF is not that satisfactory. The best would be to use PNG
    or JPG with this approach.

    You could however have a look at this reference page, "List of Java
    libraries to read and write image files":
    http://www.geocities.com/marcoschmidt.geo/java-image-coding.html ...for
    other alternatives.

    The ImageResizer class can be started with the following command:
    java ImageResizer <source image file> <destination image file> <x scaling
    factor> <y scaling factor>

    The javax.JavaIO supports the following formats:
    jpg (read/write), png (read/write), gif (read only)


    The code:
    // Millian Brave [], 2003/10/16

    import java.io.File;
    import java.awt.image.BufferedImage;
    import java.awt.image.Raster;
    import java.awt.image.WritableRaster;
    import javax.imageio.ImageIO;


    public class ImageResizer
    {

    /**
    * Constructor.
    * @param srcImgName the source image name
    * @param dstImgName the destination image name
    */
    public ImageResizer(String srcImgName, String dstImgName,
    float scaleX, float scaleY, String imgOutputFormat)
    {
    BufferedImage srcImg = null, dstImg = null;

    try {
    srcImg = ImageIO.read(new File(srcImgName));
    dstImg = resizeImage(srcImg, scaleX, scaleY);
    ImageIO.write(dstImg, imgOutputFormat, new File(dstImgName));
    }
    catch (Exception ex) {
    ex.printStackTrace();
    }
    }


    /**
    * Resizes the image according to the given scaling factors.
    * @param srcImg the image to be resized
    * @return resized image
    */
    public BufferedImage resizeImage(BufferedImage srcImg,
    float scaleX, float scaleY)
    {
    // get image dimensions
    int srcW = srcImg.getWidth();
    int srcH = srcImg.getHeight();
    int dstW = (int) (srcW * scaleX);
    int dstH = (int) (srcH * scaleY);

    // Get data structures
    BufferedImage dstImg = new BufferedImage(dstW, dstH, srcImg.getType());
    Raster srcRaster = srcImg.getRaster();
    WritableRaster dstRaster = dstImg.getRaster();
    double[] tmpPix = {0, 0, 0};

    // resize image
    for (int y=0; y<dstH; y++) {
    for (int x=0; x<dstW; x++) {
    int xPos = (int) (x * (1/scaleX)); // (find corresponding src x pos)
    int yPos = (int) (y * (1/scaleY)); // (find corresponding src y pos)
    tmpPix = srcRaster.getPixel(xPos, yPos, tmpPix);
    dstRaster.setPixel(x, y, tmpPix);
    }
    }

    return dstImg;
    }


    /**
    * Application starting point.
    * @param argv <p>argv[0] --> the source image name</p>
    * <p>argv[1] --> the destination image name</p>
    * <p>argv[2] --> x scaling factor</p>
    * <p>argv[3] --> y scaling factor</p>
    */
    public static void main(String[] argv)
    {
    if (argv.length == 5) {
    float scaleX = Float.parseFloat(argv[2]);
    float scaleY = Float.parseFloat(argv[3]);
    ImageResizer imageResizer = new ImageResizer(argv[0], argv[1],
    scaleX, scaleY,
    argv[4]);
    }
    else {
    System.err.println("usage: java ImageResizer " +
    "<srcImg> <dstImg> <scaleX> <scaleY> <output
    format>");
    System.exit(1);
    }

    }

    }


    ---
    Regards,
    Millian Brave <>



    "Clyde Ellul" <> skrev i melding
    news:...
    > Hi there.
    >
    > I need to write a simple program that reads a GIF image from an input
    > stream, resizes it, then writes it back to an output stream in the
    > same format (GIF). (JPEG input/output is good too, but GIF is
    > preferrable). JAI can do this sort of but can't write to GIF format.
    > Also, it looks too complicated for what I need; I'm a bit in a
    > hurry...
    >
    > Is there a simple way of doing what I want? Is there a free,
    > open-source library that does just that? I think I may know how to
    > read/decode and scale the image, but I don't know how to encode it to
    > a new, resized GIF file and I don't have time to write my own encoder.
    > (In fact I can't afford more than an hour on this!)
    >
    > Any help will be highly appreciated.
    >
    > Regards,
    > Clyde
     
    Millian Brave, Oct 16, 2003
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hb
    Replies:
    6
    Views:
    9,129
  2. Bernhard Gsöllpointner

    handling / resizing images (jgp, gif...) in vcpp 6.0

    Bernhard Gsöllpointner, Jul 17, 2003, in forum: C++
    Replies:
    2
    Views:
    381
    David White
    Jul 17, 2003
  3. HugeBob

    Help Resizing PNG's and GIF's

    HugeBob, Nov 20, 2007, in forum: Java
    Replies:
    1
    Views:
    492
    Knute Johnson
    Nov 20, 2007
  4. Beauregard T. Shagnasty

    Re: filename.gif or filename.gif.jpg?

    Beauregard T. Shagnasty, May 30, 2008, in forum: HTML
    Replies:
    1
    Views:
    755
    Jonathan N. Little
    May 30, 2008
  5. Joey

    resizing GIF images

    Joey, Oct 14, 2004, in forum: ASP General
    Replies:
    1
    Views:
    105
    Just1Coder
    Oct 14, 2004
Loading...

Share This Page