1 7 8 package java.awt.image; 9 10 import java.awt.Transparency ; 11 import java.awt.color.ColorSpace ; 12 13 69 70 public abstract class PackedColorModel extends ColorModel { 71 int[] maskArray; 72 int[] maskOffsets; 73 float[] scaleFactors; 74 75 108 public PackedColorModel (ColorSpace space, int bits, 109 int[] colorMaskArray, int alphaMask, 110 boolean isAlphaPremultiplied, 111 int trans, int transferType) { 112 super(bits, PackedColorModel.createBitsArray(colorMaskArray, 113 alphaMask), 114 space, (alphaMask == 0 ? false : true), 115 isAlphaPremultiplied, trans, transferType); 116 if (bits < 1 || bits > 32) { 117 throw new IllegalArgumentException ("Number of bits must be between" 118 +" 1 and 32."); 119 } 120 maskArray = new int[numComponents]; 121 maskOffsets = new int[numComponents]; 122 scaleFactors = new float[numComponents]; 123 124 for (int i=0; i < numColorComponents; i++) { 125 DecomposeMask(colorMaskArray[i], i, space.getName(i)); 127 } 128 if (alphaMask != 0) { 129 DecomposeMask(alphaMask, numColorComponents, "alpha"); 130 if (nBits[numComponents-1] == 1) { 131 transparency = Transparency.BITMASK; 132 } 133 } 134 } 135 136 176 public PackedColorModel(ColorSpace space, int bits, int rmask, int gmask, 177 int bmask, int amask, 178 boolean isAlphaPremultiplied, 179 int trans, int transferType) { 180 super (bits, PackedColorModel.createBitsArray(rmask, gmask, bmask, 181 amask), 182 space, (amask == 0 ? false : true), 183 isAlphaPremultiplied, trans, transferType); 184 185 if (space.getType() != ColorSpace.TYPE_RGB) { 186 throw new IllegalArgumentException ("ColorSpace must be TYPE_RGB."); 187 } 188 maskArray = new int[numComponents]; 189 maskOffsets = new int[numComponents]; 190 scaleFactors = new float[numComponents]; 191 192 DecomposeMask(rmask, 0, "red"); 193 194 DecomposeMask(gmask, 1, "green"); 195 196 DecomposeMask(bmask, 2, "blue"); 197 198 if (amask != 0) { 199 DecomposeMask(amask, 3, "alpha"); 200 if (nBits[3] == 1) { 201 transparency = Transparency.BITMASK; 202 } 203 } 204 } 205 206 226 final public int getMask(int index) { 227 return maskArray[index]; 228 } 229 230 237 final public int[] getMasks() { 238 return (int[]) maskArray.clone(); 239 } 240 241 246 private void DecomposeMask(int mask, int idx, String componentName) { 247 int off = 0; 248 int count = nBits[idx]; 249 250 maskArray[idx] = mask; 252 253 if (mask != 0) { 255 while ((mask & 1) == 0) { 256 mask >>>= 1; 257 off++; 258 } 259 } 260 261 if (off + count > pixel_bits) { 262 throw new IllegalArgumentException (componentName + " mask "+ 263 Integer.toHexString(maskArray[idx])+ 264 " overflows pixel (expecting "+ 265 pixel_bits+" bits"); 266 } 267 268 maskOffsets[idx] = off; 269 if (count == 0) { 270 scaleFactors[idx] = 256.0f; 273 } else { 274 scaleFactors[idx] = 255.0f / ((1 << count) - 1); 275 } 276 277 } 278 279 292 public SampleModel createCompatibleSampleModel(int w, int h) { 293 return new SinglePixelPackedSampleModel (transferType, w, h, 294 maskArray); 295 } 296 297 308 public boolean isCompatibleSampleModel(SampleModel sm) { 309 if (! (sm instanceof SinglePixelPackedSampleModel )) { 310 return false; 311 } 312 313 if (numComponents != sm.getNumBands()) { 315 return false; 316 } 317 318 if (sm.getTransferType() != transferType) { 320 return false; 321 } 322 323 SinglePixelPackedSampleModel sppsm = (SinglePixelPackedSampleModel ) sm; 324 int[] bitMasks = sppsm.getBitMasks(); 326 if (bitMasks.length != maskArray.length) { 327 return false; 328 } 329 for (int i=0; i < bitMasks.length; i++) { 330 if (bitMasks[i] != maskArray[i]) { 331 return false; 332 } 333 } 334 335 return true; 336 } 337 338 351 public WritableRaster getAlphaRaster(WritableRaster raster) { 352 if (hasAlpha() == false) { 353 return null; 354 } 355 356 int x = raster.getMinX(); 357 int y = raster.getMinY(); 358 int[] band = new int[1]; 359 band[0] = raster.getNumBands() - 1; 360 return raster.createWritableChild(x, y, raster.getWidth(), 361 raster.getHeight(), x, y, 362 band); 363 } 364 365 374 public boolean equals(Object obj) { 375 if (!(obj instanceof PackedColorModel )) { 376 return false; 377 } 378 379 if (!super.equals(obj)) { 380 return false; 381 } 382 383 PackedColorModel cm = (PackedColorModel ) obj; 384 int numC = cm.getNumComponents(); 385 if (numC != numComponents) { 386 return false; 387 } 388 for(int i=0; i < numC; i++) { 389 if (maskArray[i] != cm.getMask(i)) { 390 return false; 391 } 392 } 393 return true; 394 } 395 396 private final static int[] createBitsArray(int[]colorMaskArray, 397 int alphaMask) { 398 int numColors = colorMaskArray.length; 399 int numAlpha = (alphaMask == 0 ? 0 : 1); 400 int[] arr = new int[numColors+numAlpha]; 401 for (int i=0; i < numColors; i++) { 402 arr[i] = countBits(colorMaskArray[i]); 403 if (arr[i] < 0) { 404 throw new IllegalArgumentException ("Noncontiguous color mask (" 405 + Integer.toHexString(colorMaskArray[i])+ 406 "at index "+i); 407 } 408 } 409 if (alphaMask != 0) { 410 arr[numColors] = countBits(alphaMask); 411 if (arr[numColors] < 0) { 412 throw new IllegalArgumentException ("Noncontiguous alpha mask (" 413 + Integer.toHexString(alphaMask)); 414 } 415 } 416 return arr; 417 } 418 419 private final static int[] createBitsArray(int rmask, int gmask, int bmask, 420 int amask) { 421 int[] arr = new int[3 + (amask == 0 ? 0 : 1)]; 422 arr[0] = countBits(rmask); 423 arr[1] = countBits(gmask); 424 arr[2] = countBits(bmask); 425 if (arr[0] < 0) { 426 throw new IllegalArgumentException ("Noncontiguous red mask (" 427 + Integer.toHexString(rmask)); 428 } 429 else if (arr[1] < 0) { 430 throw new IllegalArgumentException ("Noncontiguous green mask (" 431 + Integer.toHexString(gmask)); 432 } 433 else if (arr[2] < 0) { 434 throw new IllegalArgumentException ("Noncontiguous blue mask (" 435 + Integer.toHexString(bmask)); 436 } 437 if (amask != 0) { 438 arr[3] = countBits(amask); 439 if (arr[3] < 0) { 440 throw new IllegalArgumentException ("Noncontiguous alpha mask (" 441 + Integer.toHexString(amask)); 442 } 443 } 444 return arr; 445 } 446 447 private final static int countBits(int mask) { 448 int count = 0; 449 if (mask != 0) { 450 while ((mask & 1) == 0) { 451 mask >>>= 1; 452 } 453 while ((mask & 1) == 1) { 454 mask >>>= 1; 455 count++; 456 } 457 } 458 if (mask != 0) { 459 return -1; 460 } 461 return count; 462 } 463 464 } 465 | Popular Tags |