Crossfire JXClient, Trunk  R20561
ImageScale2x.java
Go to the documentation of this file.
1 /*
2  * This file is part of JXClient, the Fullscreen Java Crossfire Client.
3  *
4  * JXClient is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * JXClient is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with JXClient; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Copyright (C) 2005-2008 Yann Chachkoff.
19  * Copyright (C) 2006-2011 Andreas Kirschbaum.
20  */
21 
22 package com.realtime.crossfire.jxclient.faces;
23 
24 import java.awt.image.BufferedImage;
25 import java.io.File;
26 import javax.swing.Icon;
27 import javax.swing.ImageIcon;
28 import org.jetbrains.annotations.NotNull;
29 
34 public class ImageScale2x {
35 
39  @NotNull
40  private final int[] srcData;
41 
45  private final int width;
46 
50  private final int height;
51 
56  public ImageScale2x(@NotNull final Icon srcImageIcon) {
57  width = srcImageIcon.getIconWidth();
58  height = srcImageIcon.getIconHeight();
59 
60  srcData = new int[width*height];
61  final BufferedImage srcBufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
62  srcImageIcon.paintIcon(null, srcBufferedImage.getGraphics(), 0, 0);
63  srcBufferedImage.getRGB(0, 0, width, height, srcData, 0, width);
64  }
65 
71  @NotNull
72  public ImageIcon getScaledImage() {
73  final RawScale2x scaler = new RawScale2x(srcData, width, height);
74 
75  final BufferedImage image = new BufferedImage(width*2, height*2, BufferedImage.TYPE_INT_ARGB);
76  image.setRGB(0, 0, width*2, height*2, scaler.getScaledData(), 0, width*2);
77 
78  return new ImageIcon(image);
79  }
80 
85  public static void main(@NotNull final String[] args) {
86  final String srcFile = "random_orig.png";
87  System.out.println("Reading: "+srcFile);
88  final ImageIcon src = new ImageIcon(srcFile);
89  final ImageScale2x scaler = new ImageScale2x(src);
90  final Icon out = scaler.getScaledImage();
91 
92  final String outFile = srcFile.substring(0, srcFile.length()-4)+"2x.png";
93  System.out.println("Writing: "+outFile);
94  final FileCache fileCache = new FileCache(new File("cache"));
95  fileCache.save(outFile, 0, out);
96  }
97 
98 }
void save(@NotNull final Face face, @NotNull final ImageIcon imageIcon)
Stores an ImageIcon into the cache.the face to save the image icon to store
Definition: FileCache.java:98
A simple implementation of the Scale2x algorithm for scaling raw image data.
Definition: RawScale2x.java:30
final int width
The width of the image.
final int height
The height of the image.
A disk based cache for image files.
Definition: FileCache.java:38
ImageScale2x(@NotNull final Icon srcImageIcon)
Creates a new scaler that will scale the passed image.
final int [] srcData
The src data from the image.
int [] getScaledData()
Gets the scale image data.
ImageIcon getScaledImage()
Retrieves the scaled image.
static void main(@NotNull final String[] args)
An entry point and a bit of test code.
A utility to perform the scale2x algorithm on a Java Image.