/ResizeApplet/tags/000_first-publish/build.xml |
---|
0,0 → 1,52 |
<project name="ResizeApplet" default="deploy" basedir="."> |
<property name="build.compiler" value="jikes"/> |
<property name="src" location="src"/> |
<property name="classes" location="classes"/> |
<property name="classpath" value=""/> |
<property name="target" location="target"/> |
<property name="html" location="html"/> |
<property name="lib" location="lib"/> |
<target name="compile"> |
<mkdir dir="${classes}" /> |
<javac |
srcdir="${src}" |
destdir="${classes}" |
classpath="${classpath}:${classes}" |
debug="on" |
extdirs="${lib}" |
/> |
</target> |
<target name="deploy" depends="compile"> |
<!-- temp solution, should be used as jar directly --> |
<unjar src="${lib}/jiu.jar" dest="classes"> |
<patternset> |
<include name="net/**" /> |
</patternset> |
</unjar> |
<jar destfile="${target}/ResizeApplet.jar"> |
<fileset dir="${classes}" /> |
</jar> |
<signjar jar="${target}/ResizeApplet.jar" alias="dev" storepass="password" /> |
<copy todir="${target}"> |
<fileset dir="${html}"/> |
</copy> |
</target> |
<target name="clean"> |
<delete dir="${classes}"/> |
<delete includeemptydirs="true"> |
<fileset dir="${target}" includes="**/*"/> |
</delete> |
</target> |
<target name="all" depends="clean,deploy" /> |
</project> |
/ResizeApplet/tags/000_first-publish/html/index.html |
---|
0,0 → 1,0 |
<applet archive=ResizeApplet.jar code=ak/resizeapplet/ResizeApplet.class width="400" height="200"></applet> |
/ResizeApplet/tags/000_first-publish/lib/jiu.jar |
---|
Cannot display: file marked as a binary type. |
svn:mime-type = application/octet-stream |
Property changes: |
Added: svn:mime-type |
+application/octet-stream |
\ No newline at end of property |
/ResizeApplet/tags/000_first-publish/src/ak/resizeapplet/ExtFileFilter.java |
---|
0,0 → 1,48 |
package ak.resizeapplet; |
import java.util.Set; |
import java.util.HashSet; |
import java.io.File; |
import javax.swing.filechooser.FileFilter; |
public class ExtFileFilter extends FileFilter |
{ |
private String description; |
private Set extensions = new HashSet(); |
public ExtFileFilter(String description) |
{ |
this.description = description; |
} |
public boolean accept(File f) |
{ |
if(f == null) return false; |
if(f.isHidden()) return false; |
if(f.isDirectory()) return true; |
String name = f.getName(); |
int pos = name.lastIndexOf('.'); |
String ext; |
if(pos <= 0 || pos >= name.length()-1) { |
ext = ""; |
} |
else { |
ext = name.substring(pos+1).toUpperCase(); |
} |
return extensions.contains(ext); |
} |
public void addExtension(String ext) |
{ |
extensions.add(ext.toUpperCase()); |
} |
public String getDescription() |
{ |
return description; |
} |
} |
/ResizeApplet/tags/000_first-publish/src/ak/resizeapplet/ImageResizer.java |
---|
0,0 → 1,10 |
package ak.resizeapplet; |
import java.awt.Image; |
import java.awt.image.BufferedImage; |
public interface ImageResizer |
{ |
public BufferedImage resize(Image origin, int newWidth, int newHeight); |
} |
/ResizeApplet/tags/000_first-publish/src/ak/resizeapplet/ResizeApplet.java |
---|
0,0 → 1,144 |
package ak.resizeapplet; |
import javax.swing.*; |
import java.awt.*; |
import java.awt.event.*; |
import java.awt.image.*; |
import java.util.*; |
import java.io.*; |
public class ResizeApplet extends JApplet |
{ |
private final static int thumbnailWidth = 200; |
private final static int thumbnailHeight = 150; |
private JLabel fileLabel; |
private JTextField fileField; |
private JButton browseButton; |
private JButton loadButton; |
private JLabel imagePanel; |
private ImageResizer resizer = new JiuResizer(); |
private ImageIcon thumbnail = new ImageIcon(); |
private void createGUI() |
{ |
setLayout(new GridBagLayout()); |
GridBagConstraints c = new GridBagConstraints(); |
fileLabel = new JLabel("File"); |
fileLabel.setDisplayedMnemonic('f'); |
c.gridx = 0; c.gridy = 0; c.weightx = 0.0; c.weighty = 0.0; c.fill = GridBagConstraints.NONE; |
add(fileLabel, c); |
fileField = new JTextField(); |
c.gridx = 1; c.gridy = 0; c.weightx = 0.7; c.weighty = 0.0; c.fill = GridBagConstraints.HORIZONTAL; |
add(fileField, c); |
fileLabel.setLabelFor(fileField); |
browseButton = new JButton("Browse"); |
browseButton.setMnemonic('b'); |
browseButton.addActionListener(new ActionListener() { |
public void actionPerformed(ActionEvent e) { |
browseButtonClicked(); |
} |
} ); |
c.gridx = 2; c.gridy = 0; c.weightx = 0.1; c.weighty = 0.0; c.fill = GridBagConstraints.HORIZONTAL; |
add(browseButton, c); |
loadButton = new JButton("Load"); |
loadButton.setMnemonic('l'); |
loadButton.addActionListener(new ActionListener() { |
public void actionPerformed(ActionEvent e) { |
loadButtonClicked(); |
} |
} ); |
c.gridx = 3; c.gridy = 0; c.weightx = 0.1; c.weighty = 0.0; c.fill = GridBagConstraints.HORIZONTAL; |
add(loadButton, c); |
imagePanel = new JLabel(); |
imagePanel.setBorder(BorderFactory.createLineBorder(Color.black)); |
imagePanel.setMinimumSize(new Dimension(thumbnailWidth, thumbnailHeight)); |
imagePanel.setPreferredSize(new Dimension(thumbnailWidth, thumbnailHeight)); |
c.gridx = 0; c.gridy = 1; c.weightx = 0.0; c.weighty = 0.9; c.fill = GridBagConstraints.NONE; |
c.gridwidth = 4; |
add(imagePanel, c); |
} |
public void init() |
{ |
try { |
createGUI(); |
} |
catch(Exception ex) { |
System.err.println("Cannot create GUI: " + ex.getMessage()); |
} |
} |
private void browseButtonClicked() |
{ |
try { |
JFileChooser chooser = new JFileChooser(); |
ExtFileFilter filter = new ExtFileFilter("Images"); |
filter.addExtension("jpg"); |
filter.addExtension("jpeg"); |
filter.addExtension("gif"); |
chooser.setFileFilter(filter); |
chooser.setSelectedFile(new File(fileField.getText())); |
if(chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { |
fileField.setText(chooser.getSelectedFile().getAbsolutePath()); |
loadButtonClicked(); |
} |
} |
catch(Exception ex) { |
JOptionPane.showMessageDialog(this, "Cannot browse files: " + ex.getMessage(), |
"ResizeApplet", JOptionPane.ERROR_MESSAGE); |
} |
} |
private void loadButtonClicked() |
{ |
try { |
thumbnail.setImage( |
resizer.resize(loadImage(fileField.getText()), thumbnailWidth, thumbnailHeight)); |
imagePanel.setIcon(thumbnail); |
} |
catch(Exception ex) { |
JOptionPane.showMessageDialog(this, "Cannot browse files: " + ex.getMessage(), |
"ResizeApplet", JOptionPane.ERROR_MESSAGE); |
} |
} |
private void sendToServer() |
{ |
//javax.imageio.ImageIO.write(thumbnail.getImage(), "jpg", outputStream); |
} |
protected Image loadImage(String fileName) |
throws Exception |
{ |
Toolkit toolkit = Toolkit.getDefaultToolkit(); |
Image image = toolkit.createImage(fileName); |
toolkit.prepareImage(image, -1, -1, null); |
while(true) { |
int status = toolkit.checkImage(image, -1, -1, null); |
if((status & ImageObserver.ALLBITS) != 0) break; |
if((status & ImageObserver.ERROR) != 0) throw new Exception("Cannot read the image"); |
try { |
Thread.sleep(100); |
} |
catch(Exception ex) { |
throw new Exception("Cannot read the image"); |
} |
} |
return image; |
} |
} |
/ResizeApplet/tags/000_first-publish/src/ak/resizeapplet/JiuResizer.java |
---|
0,0 → 1,196 |
package ak.resizeapplet; |
import java.awt.Image; |
import java.awt.image.BufferedImage; |
import java.awt.image.PixelGrabber; |
import java.awt.image.ImageObserver; |
import net.sourceforge.jiu.data.BilevelImage; |
import net.sourceforge.jiu.data.Gray8Image; |
import net.sourceforge.jiu.data.MemoryRGB24Image; |
import net.sourceforge.jiu.data.Paletted8Image; |
import net.sourceforge.jiu.data.PixelImage; |
import net.sourceforge.jiu.data.RGB24Image; |
import net.sourceforge.jiu.data.RGBIndex; |
import net.sourceforge.jiu.geometry.Resample; |
public class JiuResizer |
implements ImageResizer |
{ |
protected static final int DEFAULT_ALPHA = 0xff000000; |
protected static final int FILTER_TYPE = Resample.FILTER_TYPE_HERMITE; |
/* |
Possible type small, ms medium, ms quality |
----------------------------------------------------------- |
FILTER_TYPE_BOX 2.4 3.5 very bad |
FILTER_TYPE_TRIANGLE 2.9 5.2 |
FILTER_TYPE_B_SPLINE 3.6 7.2 |
FILTER_TYPE_BELL 3.5 6.1 |
FILTER_TYPE_HERMITE 2.9 5.3 |
FILTER_TYPE_LANCZOS3 4.7 9.7 the best |
FILTER_TYPE_MITCHELL 3.6 7.1 |
*/ |
public JiuResizer() |
{ |
} |
public BufferedImage resize(Image origin, int newWidth, int newHeight) |
{ |
try { |
RGB24Image image = convertImageToRGB24Image(origin); |
Resample resample; |
if(image == null) return null; |
if(image.getWidth() == newWidth && image.getHeight() == newHeight) |
return convertToAwtImage(image, DEFAULT_ALPHA); |
resample = new Resample(); |
resample.setInputImage(image); |
resample.setSize(newWidth, newHeight); |
resample.setFilter(FILTER_TYPE); |
resample.process(); |
return convertToAwtImage(resample.getOutputImage(), DEFAULT_ALPHA); |
} |
catch(Exception ex) { |
throw new RuntimeException(ex.getMessage()); |
} |
} |
protected static BufferedImage convertToAwtImage(PixelImage image, int alpha) |
{ |
if (image == null) |
{ |
return null; |
} |
if (image instanceof RGB24Image) |
{ |
return convertToAwtImage((RGB24Image)image, alpha); |
} |
else |
if (image instanceof Gray8Image) |
{ |
return null; //convertToAwtImage((Gray8Image)image, alpha); |
} |
else |
if (image instanceof Paletted8Image) |
{ |
return null; //convertToAwtImage((Paletted8Image)image, alpha); |
} |
else |
if (image instanceof BilevelImage) |
{ |
return null; //convertToAwtImage((BilevelImage)image, alpha); |
} |
else |
{ |
return null; |
} |
} |
protected static BufferedImage convertToAwtImage(RGB24Image image, int alpha) |
{ |
if (image == null) |
{ |
return null; |
} |
int width = image.getWidth(); |
int height = image.getHeight(); |
if (width < 1 || height < 1) |
{ |
return null; |
} |
int[] pixels = new int[width]; |
byte[] red = new byte[width]; |
byte[] green = new byte[width]; |
byte[] blue = new byte[width]; |
BufferedImage newImage = new BufferedImage(width, height, |
BufferedImage.TYPE_INT_RGB); |
for (int y = 0; y < height; y++) |
{ |
image.getByteSamples(RGBIndex.INDEX_RED, 0, y, width, 1, red, 0); |
image.getByteSamples(RGBIndex.INDEX_GREEN, 0, y, width, 1, green, 0); |
image.getByteSamples(RGBIndex.INDEX_BLUE, 0, y, width, 1, blue, 0); |
convertFromRGB24(red, 0, green, 0, blue, 0, alpha, pixels, 0, width); |
for(int x = 0; x < width; x++) |
newImage.setRGB(x, y, pixels[x]); |
} |
return newImage; |
} |
protected static void convertFromRGB24( |
byte[] srcRed, int srcRedOffset, |
byte[] srcGreen, int srcGreenOffset, |
byte[] srcBlue, int srcBlueOffset, |
int alpha, |
int[] dest, int destOffset, |
int num) |
{ |
while (num-- > 0) |
{ |
dest[destOffset++] = |
alpha | |
(srcBlue[srcBlueOffset++] & 0xff) | |
((srcGreen[srcGreenOffset++] & 0xff) << 8) | |
((srcRed[srcRedOffset++] & 0xff) << 16); |
} |
} |
/** |
* Creates an {@link RGB24Image} from the argument AWT image instance. |
* @param image AWT image object to be converted to a {@link RGB24Image} |
* @return a {@link RGB24Image} object holding the |
* image data from the argument image |
*/ |
protected static RGB24Image convertImageToRGB24Image(Image image) |
{ |
if (image == null) |
{ |
return null; |
} |
int width = image.getWidth(null); |
int height = image.getHeight(null); |
if (width < 1 || height < 1) |
{ |
return null; |
} |
int[] pixels = new int[width * height]; |
PixelGrabber pg = new PixelGrabber( |
image, 0, 0, width, height, pixels, 0, width); |
try |
{ |
pg.grabPixels(); |
} |
catch (InterruptedException e) |
{ |
return null; |
} |
if ((pg.getStatus() & ImageObserver.ABORT) != 0) |
{ |
//System.err.println("image fetch aborted or errored"); |
return null; |
} |
RGB24Image result = new MemoryRGB24Image(width, height); |
int offset = 0; |
for (int y = 0; y < height; y++) |
{ |
for (int x = 0; x < width; x++) |
{ |
int pixel = pixels[offset++] & 0xffffff; |
result.putSample(RGBIndex.INDEX_RED, x, y, pixel >> 16); |
result.putSample(RGBIndex.INDEX_GREEN, x, y, (pixel >> 8) & 0xff); |
result.putSample(RGBIndex.INDEX_BLUE, x, y, pixel & 0xff); |
} |
} |
return result; |
} |
} |
/ResizeApplet/tags/000_first-publish |
---|
Property changes: |
Added: svn:ignore |
+target |
+classes |
+ |