Crossfire JXClient, Trunk  R20561
RawScale8d.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.util.HashMap;
25 import java.util.Map;
26 import java.util.Map.Entry;
27 import org.jetbrains.annotations.NotNull;
28 
33 public class RawScale8d {
34 
38  @NotNull
39  private final int[] srcImage;
40 
44  @NotNull
45  private final int[] dstImage;
46 
50  private final int width;
51 
55  private final int height;
56 
60  @NotNull
61  private final Map<Integer, Integer> pixels = new HashMap<>();
62 
69  public RawScale8d(@NotNull final int[] imageData, final int dataWidth, final int dataHeight) {
70  width = dataWidth;
71  height = dataHeight;
72  //noinspection AssignmentToCollectionOrArrayFieldFromParameter
73  srcImage = imageData;
74  dstImage = new int[imageData.length*4];
75  }
76 
83  private void setDestPixel(final int x, final int y, final int p) {
84  dstImage[x+y*width/8] = p;
85  }
86 
93  private int getSourcePixel(final int x, final int y) {
94  return srcImage[x+y*width];
95  }
96 
102  private void process(final int x, final int y) {
103  pixels.clear();
104  for (int dx = 0; dx < 8; dx++) {
105  for (int dy = 0; dy < 8; dy++) {
106  final int value = getSourcePixel(8*x+dx, 8*y+dy);
107  if (value != 0) {
108  final Integer count = pixels.get(value);
109  pixels.put(value, count == null ? 1 : count+1);
110  }
111  }
112  }
113  int maxCount = 0;
114  int maxPixel = 0;
115  for (final Entry<Integer, Integer> e : pixels.entrySet()) {
116  final int thisCount = e.getValue();
117  if (thisCount > maxCount) {
118  maxCount = thisCount;
119  maxPixel = e.getKey();
120  }
121  }
122  pixels.clear();
123 
124  setDestPixel(x, y, maxPixel);
125  }
126 
133  @NotNull
134  public int[] getScaledData() {
135  for (int x = 0; x < width/8; x++) {
136  for (int y = 0; y < height/8; y++) {
137  process(x, y);
138  }
139  }
140 
141  //noinspection ReturnOfCollectionOrArrayField
142  return dstImage;
143  }
144 
145 }
int getSourcePixel(final int x, final int y)
Gets a pixel from the source image.
Definition: RawScale8d.java:93
int [] getScaledData()
Returns the scale image data.
final int [] dstImage
The destination image data.
Definition: RawScale8d.java:45
final int [] srcImage
The source image data.
Definition: RawScale8d.java:39
final int height
The height of the source image.
Definition: RawScale8d.java:55
void setDestPixel(final int x, final int y, final int p)
Sets a pixel in the destination image data.
Definition: RawScale8d.java:83
final Map< Integer, Integer > pixels
Maps pixel value to number of pixels.
Definition: RawScale8d.java:61
RawScale8d(@NotNull final int[] imageData, final int dataWidth, final int dataHeight)
Creates a new instance.
Definition: RawScale8d.java:69
Scales down a raw image to an eighth in both dimensions.
Definition: RawScale8d.java:33
final int width
The width of the source image.
Definition: RawScale8d.java:50
void process(final int x, final int y)
Processes a specific destination pixel.