1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
|
/*
* MemoryBilevelImage
*
* Copyright (c) 2002 Marco Schmidt.
* All rights reserved.
*/
package net.sourceforge.jiu.data;
import net.sourceforge.jiu.data.BilevelImage;
import net.sourceforge.jiu.util.ArrayConverter;
/**
* An implementation of the {@link BilevelImage} interface that stores image
* data in a <code>byte</code> array in memory.
* An image of <code>width</code> times <code>height</code> pixels will require
* <code>(width + 7) / 8 * height</code> bytes of memory.
* @author Marco Schmidt
*/
public class MemoryBilevelImage implements BilevelImage
{
private final int BYTES_PER_ROW;
private final byte[] data;
private final int HEIGHT;
private final int WIDTH;
/**
* Create a new MemoryBilevelImage object with the specified resolution.
* @param width the horizontal resolution of the new image, must be larger than zero
* @param height the vertical resolution of the new image, must be larger than zero
* @throws IllegalArgumentException if any of the two parameters is smaller than one
*/
public MemoryBilevelImage(int width, int height)
{
if (width < 1)
{
throw new IllegalArgumentException("Width must be larger than zero; got " + width);
}
if (height < 1)
{
throw new IllegalArgumentException("Height must be larger than zero; got " + height);
}
BYTES_PER_ROW = (width + 7) / 8;
WIDTH = width;
HEIGHT = height;
data = new byte[BYTES_PER_ROW * HEIGHT];
}
private void checkBitOffset(int bitOffset)
{
if (bitOffset < 0 || bitOffset > 7)
{
throw new IllegalArgumentException("A bit offset value must be from the interval 0..7.");
}
}
private void checkPositionAndNumber(int x, int y, int w, int h)
{
if (w < 0)
{
throw new IllegalArgumentException("Negative number of samples " +
"to be copied: " + w);
}
if (h < 0)
{
throw new IllegalArgumentException("Negative number of rows " +
"to be copied: " + h);
}
if (x < 0 || x >= getWidth())
{
throw new IllegalArgumentException("The value for x is invalid: " + x + ".");
}
if (y < 0 || y >= getHeight())
{
throw new IllegalArgumentException("The value for y is invalid: " + y + ".");
}
if (x + w > getWidth())
{
throw new IllegalArgumentException("Cannot copy " + w + " values starting at " +
"offset " + x + " (width is only " + getWidth() + ").");
}
if (y + h > getHeight())
{
throw new IllegalArgumentException("Cannot copy " + h + " rows starting at " +
y + " (height is only " + getHeight() + ").");
}
}
private void checkValue(int value)
{
if (value != WHITE && value != BLACK)
{
throw new IllegalArgumentException("Sample value must be either BilevelImage.BLACK or BilevelImage.WHITE.");
}
}
public void clear(int newValue)
{
clear(0, newValue);
}
public void clear(int channelIndex, int newValue)
{
if (channelIndex != 0)
{
throw new IllegalArgumentException("Invalid channel index; " +
"bilevel images have only one channel, so 0 is the only " +
"valid argument; got " + channelIndex);
}
checkValue(newValue);
byte value;
if (newValue == BLACK)
{
value = 0;
}
else
{
value = (byte)0xff;
}
for (int i = 0; i < data.length; i++)
{
data[i] = value;
}
}
public PixelImage createCompatibleImage(int width, int height)
{
return new MemoryBilevelImage(width, height);
}
public PixelImage createCopy()
{
PixelImage copy = createCompatibleImage(getWidth(), getHeight());
MemoryBilevelImage result = (MemoryBilevelImage)copy;
System.arraycopy(data, 0, result.data, 0, data.length);
return result;
}
public long getAllocatedMemory()
{
return data.length;
}
public int getBitsPerPixel()
{
return 1;
}
public int getHeight()
{
return HEIGHT;
}
public Class getImageType()
{
return BilevelImage.class;
}
public int getMaxSample(int channelIndex)
{
return 1;
}
public int getNumChannels()
{
return 1;
}
public void getPackedBytes(int x, int y, int numSamples, byte[] dest, int destOffset, int destBitOffset)
{
checkPositionAndNumber(x, y, numSamples, 1);
checkBitOffset(destBitOffset);
int srcOffset = y * BYTES_PER_ROW + (x >> 3);
int srcBitOffset = x & 7;
ArrayConverter.copyPackedBytes(data, srcOffset, srcBitOffset, dest, destOffset, destBitOffset, numSamples);
}
public int getSample(int x, int y)
{
if (isBlack(x, y))
{
return BLACK;
}
else
{
return WHITE;
}
}
public int getSample(int channelIndex, int x, int y)
{
if (channelIndex == 0)
{
if (isBlack(x, y))
{
return BLACK;
}
else
{
return WHITE;
}
}
else
{
throw new IllegalArgumentException("The channelIndex argument must be 0 for bilevel images; got " + channelIndex);
}
}
public void getSamples(int channelIndex, int x, int y, int w, int h, int[] dest, int destOffset)
{
final int INITIAL_MASK = 1 << (7 - (x % 8));
int offset = y * BYTES_PER_ROW + x / 8;
while (h-- > 0)
{
int mask = INITIAL_MASK;
int srcOffset = offset;
int remainingColumns = w;
int srcValue = data[srcOffset++] & 0xff;
while (remainingColumns-- != 0)
{
if ((srcValue & mask) == 0)
{
dest[destOffset++] = BLACK;
}
else
{
dest[destOffset++] = WHITE;
}
if (mask == 1)
{
mask = 128;
srcValue = data[srcOffset++] & 0xff;
}
else
{
mask >>= 1;
}
}
offset += BYTES_PER_ROW;
}
}
public int getWidth()
{
return WIDTH;
}
public boolean isBlack(int x, int y)
{
try
{
int offset = y * BYTES_PER_ROW + (x >> 3);
return (data[offset] & (byte)(1 << (7 - (x & 7)))) == 0;
}
catch (ArrayIndexOutOfBoundsException aioobe)
{
checkPositionAndNumber(x, y, 1, 1);
}
return true;
}
public boolean isWhite(int x, int y)
{
try
{
int offset = y * BYTES_PER_ROW + (x >> 3);
return (data[offset] & (byte)(1 << (7 - (x & 7)))) != 0;
}
catch (ArrayIndexOutOfBoundsException aioobe)
{
checkPositionAndNumber(x, y, 1, 1);
}
return true;
}
public void putBlack(int x, int y)
{
try
{
int offset = y * BYTES_PER_ROW + (x >> 3);
data[offset] &= (byte)(255 - (1 << (7 - (x & 7))));
}
catch (ArrayIndexOutOfBoundsException aioobe)
{
checkPositionAndNumber(x, y, 1, 1);
}
}
public void putPackedBytes(int x, int y, int numSamples, byte[] src, int srcOffset, int srcBitOffset)
{
checkPositionAndNumber(x, y, numSamples, 1);
checkBitOffset(srcBitOffset);
int destOffset = y * BYTES_PER_ROW + (x >> 3);
int destBitOffset = x & 7;
ArrayConverter.copyPackedBytes(src, srcOffset, srcBitOffset, data, destOffset, destBitOffset, numSamples);
}
public void putSample(int x, int y, int newValue)
{
putSample(0, x, y, newValue);
}
public void putSample(int channelIndex, int x, int y, int newValue)
{
checkValue(newValue);
try
{
int offset = y * BYTES_PER_ROW + (x >> 3);
if (newValue == BLACK)
{
data[offset] &= (byte)(255 - (1 << (7 - (x & 7))));
}
else
{
data[offset] |= (byte)(1 << (7 - (x & 7)));
}
}
catch (ArrayIndexOutOfBoundsException aioobe)
{
checkPositionAndNumber(x, y, 1, 1);
}
}
public void putSamples(int channelIndex, int x, int y, int w, int h, int[] src, int srcOffset)
{
checkPositionAndNumber(x, y, w, h);
int INITIAL_ROW_MASK = 1 << (7 - (x & 7));
int initialDestOffset = y * BYTES_PER_ROW + (x >> 3);
while (h-- > 0)
{
int mask = INITIAL_ROW_MASK;
int destOffset = initialDestOffset;
int pixelsLeft = w;
while (pixelsLeft-- > 0)
{
if (src[srcOffset++] == BLACK)
{
data[destOffset] &= (byte)(255 - mask);
}
else
{
data[destOffset] |= (byte)mask;
}
if (mask == 1)
{
mask = 128;
destOffset++;
}
}
initialDestOffset += BYTES_PER_ROW;
}
}
public void putWhite(int x, int y)
{
try
{
int offset = y * BYTES_PER_ROW + (x >> 3);
data[offset] |= (byte)(1 << (7 - (x & 7)));
}
catch (ArrayIndexOutOfBoundsException aioobe)
{
checkPositionAndNumber(x, y, 1, 1);
}
}
}
|