Resizing GIF images: please help!

C

Clyde Ellul

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
 
M

Marco Schmidt

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
 
R

Roedy Green

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.
 
R

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?
 
M

Marco Schmidt

Roedy Green:
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
 
M

Millian Brave

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 [[email protected]], 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);
}

}

}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top