Crossfire JXClient, Trunk  R20561
RawScale2x.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 org.jetbrains.annotations.NotNull;
25 
30 public class RawScale2x {
31 
35  @NotNull
36  private final int[] srcImage;
37 
41  @NotNull
42  private final int[] dstImage;
43 
47  private final int width;
48 
52  private final int height;
53 
61  public RawScale2x(@NotNull final int[] imageData, final int dataWidth, final int dataHeight) {
62  width = dataWidth;
63  height = dataHeight;
64  //noinspection AssignmentToCollectionOrArrayFieldFromParameter
65  srcImage = imageData;
66  dstImage = new int[imageData.length*4];
67  }
68 
76  private static boolean different(final int a, final int b) {
77  return a != b;
78  }
79 
86  private void setDestPixel(final int x, final int y, final int p) {
87  dstImage[x+y*width*2] = p;
88  }
89 
97  private int getSourcePixel(final int x, final int y) {
98  final int xx = Math.min(width-1, Math.max(0, x));
99  final int yy = Math.min(height-1, Math.max(0, y));
100  return srcImage[xx+yy*width];
101  }
102 
109  private void process(final int x, final int y) {
110  //final int a = getSourcePixel(x-1, y-1);
111  final int b = getSourcePixel(x, y-1);
112  //final int c = getSourcePixel(x+1, y-1);
113  final int d = getSourcePixel(x-1, y);
114  final int e = getSourcePixel(x, y);
115  final int f = getSourcePixel(x+1, y);
116  //final int g = getSourcePixel(x-1, y+1);
117  final int h = getSourcePixel(x, y+1);
118  //final int i = getSourcePixel(x+1, y+1);
119  final int e0;
120  final int e1;
121  final int e2;
122  final int e3;
123  if (different(b, h) && different(d, f)) {
124  e0 = different(d, b) ? e : d;
125  e1 = different(b, f) ? e : f;
126  e2 = different(d, h) ? e : d;
127  e3 = different(h, f) ? e : f;
128  } else {
129  e0 = e;
130  e1 = e;
131  e2 = e;
132  e3 = e;
133  }
134 
135  setDestPixel(x*2, y*2, e0);
136  setDestPixel(x*2+1, y*2, e1);
137  setDestPixel(x*2, y*2+1, e2);
138  setDestPixel(x*2+1, y*2+1, e3);
139  }
140 
147  @NotNull
148  public int[] getScaledData() {
149  for (int x = 0; x < width; x++) {
150  for (int y = 0; y < height; y++) {
151  process(x, y);
152  }
153  }
154 
155  //noinspection ReturnOfCollectionOrArrayField
156  return dstImage;
157  }
158 
159 }
final int width
The width of the source image.
Definition: RawScale2x.java:47
RawScale2x(@NotNull final int[] imageData, final int dataWidth, final int dataHeight)
Creates a new scaler based on some raw data.
Definition: RawScale2x.java:61
A simple implementation of the Scale2x algorithm for scaling raw image data.
Definition: RawScale2x.java:30
int getSourcePixel(final int x, final int y)
Gets a pixel from the source image.
Definition: RawScale2x.java:97
void process(final int x, final int y)
Processes a specific pixel.
int [] getScaledData()
Gets the scale image data.
final int height
The height of the source image.
Definition: RawScale2x.java:52
void setDestPixel(final int x, final int y, final int p)
Sets a pixel in the destination image data.
Definition: RawScale2x.java:86
final int [] dstImage
The destination image data.
Definition: RawScale2x.java:42
static boolean different(final int a, final int b)
Checks if two pixels are different.
Definition: RawScale2x.java:76
final int [] srcImage
The source image data.
Definition: RawScale2x.java:36