1 7 8 package java.awt.image; 9 10 import java.util.Hashtable ; 11 import java.awt.image.ImageProducer ; 12 import java.awt.image.ImageConsumer ; 13 import java.awt.image.ColorModel ; 14 import java.awt.Image ; 15 16 57 public class PixelGrabber implements ImageConsumer { 58 ImageProducer producer; 59 60 int dstX; 61 int dstY; 62 int dstW; 63 int dstH; 64 65 ColorModel imageModel; 66 byte[] bytePixels; 67 int[] intPixels; 68 int dstOff; 69 int dstScan; 70 71 private boolean grabbing; 72 private int flags; 73 74 private static final int GRABBEDBITS = (ImageObserver.FRAMEBITS 75 | ImageObserver.ALLBITS); 76 private static final int DONEBITS = (GRABBEDBITS 77 | ImageObserver.ERROR); 78 79 101 public PixelGrabber(Image img, int x, int y, int w, int h, 102 int[] pix, int off, int scansize) { 103 this(img.getSource(), x, y, w, h, pix, off, scansize); 104 } 105 106 130 public PixelGrabber(ImageProducer ip, int x, int y, int w, int h, 131 int[] pix, int off, int scansize) { 132 producer = ip; 133 dstX = x; 134 dstY = y; 135 dstW = w; 136 dstH = h; 137 dstOff = off; 138 dstScan = scansize; 139 intPixels = pix; 140 imageModel = ColorModel.getRGBdefault(); 141 } 142 143 165 public PixelGrabber(Image img, int x, int y, int w, int h, 166 boolean forceRGB) 167 { 168 producer = img.getSource(); 169 dstX = x; 170 dstY = y; 171 dstW = w; 172 dstH = h; 173 if (forceRGB) { 174 imageModel = ColorModel.getRGBdefault(); 175 } 176 } 177 178 181 public synchronized void startGrabbing() { 182 if ((flags & DONEBITS) != 0) { 183 return; 184 } 185 if (!grabbing) { 186 grabbing = true; 187 flags &= ~(ImageObserver.ABORT); 188 producer.startProduction(this); 189 } 190 } 191 192 195 public synchronized void abortGrabbing() { 196 imageComplete(IMAGEABORTED); 197 } 198 199 208 public boolean grabPixels() throws InterruptedException { 209 return grabPixels(0); 210 } 211 212 232 public synchronized boolean grabPixels(long ms) 233 throws InterruptedException 234 { 235 if ((flags & DONEBITS) != 0) { 236 return (flags & GRABBEDBITS) != 0; 237 } 238 long end = ms + System.currentTimeMillis(); 239 if (!grabbing) { 240 grabbing = true; 241 flags &= ~(ImageObserver.ABORT); 242 producer.startProduction(this); 243 } 244 while (grabbing) { 245 long timeout; 246 if (ms == 0) { 247 timeout = 0; 248 } else { 249 timeout = end - System.currentTimeMillis(); 250 if (timeout <= 0) { 251 break; 252 } 253 } 254 wait(timeout); 255 } 256 return (flags & GRABBEDBITS) != 0; 257 } 258 259 265 public synchronized int getStatus() { 266 return flags; 267 } 268 269 278 public synchronized int getWidth() { 279 return (dstW < 0) ? -1 : dstW; 280 } 281 282 291 public synchronized int getHeight() { 292 return (dstH < 0) ? -1 : dstH; 293 } 294 295 310 public synchronized Object getPixels() { 311 return (bytePixels == null) 312 ? ((Object ) intPixels) 313 : ((Object ) bytePixels); 314 } 315 316 333 public synchronized ColorModel getColorModel() { 334 return imageModel; 335 } 336 337 349 public void setDimensions(int width, int height) { 350 if (dstW < 0) { 351 dstW = width - dstX; 352 } 353 if (dstH < 0) { 354 dstH = height - dstY; 355 } 356 if (dstW <= 0 || dstH <= 0) { 357 imageComplete(STATICIMAGEDONE); 358 } else if (intPixels == null && 359 imageModel == ColorModel.getRGBdefault()) { 360 intPixels = new int[dstW * dstH]; 361 dstScan = dstW; 362 dstOff = 0; 363 } 364 flags |= (ImageObserver.WIDTH | ImageObserver.HEIGHT); 365 } 366 367 378 public void setHints(int hints) { 379 return; 380 } 381 382 393 public void setProperties(Hashtable <?,?> props) { 394 return; 395 } 396 397 409 public void setColorModel(ColorModel model) { 410 return; 411 } 412 413 private void convertToRGB() { 414 int size = dstW * dstH; 415 int newpixels[] = new int[size]; 416 if (bytePixels != null) { 417 for (int i = 0; i < size; i++) { 418 newpixels[i] = imageModel.getRGB(bytePixels[i] & 0xff); 419 } 420 } else if (intPixels != null) { 421 for (int i = 0; i < size; i++) { 422 newpixels[i] = imageModel.getRGB(intPixels[i]); 423 } 424 } 425 bytePixels = null; 426 intPixels = newpixels; 427 dstScan = dstW; 428 dstOff = 0; 429 imageModel = ColorModel.getRGBdefault(); 430 } 431 432 452 public void setPixels(int srcX, int srcY, int srcW, int srcH, 453 ColorModel model, 454 byte pixels[], int srcOff, int srcScan) { 455 if (srcY < dstY) { 456 int diff = dstY - srcY; 457 if (diff >= srcH) { 458 return; 459 } 460 srcOff += srcScan * diff; 461 srcY += diff; 462 srcH -= diff; 463 } 464 if (srcY + srcH > dstY + dstH) { 465 srcH = (dstY + dstH) - srcY; 466 if (srcH <= 0) { 467 return; 468 } 469 } 470 if (srcX < dstX) { 471 int diff = dstX - srcX; 472 if (diff >= srcW) { 473 return; 474 } 475 srcOff += diff; 476 srcX += diff; 477 srcW -= diff; 478 } 479 if (srcX + srcW > dstX + dstW) { 480 srcW = (dstX + dstW) - srcX; 481 if (srcW <= 0) { 482 return; 483 } 484 } 485 int dstPtr = dstOff + (srcY - dstY) * dstScan + (srcX - dstX); 486 if (intPixels == null) { 487 if (bytePixels == null) { 488 bytePixels = new byte[dstW * dstH]; 489 dstScan = dstW; 490 dstOff = 0; 491 imageModel = model; 492 } else if (imageModel != model) { 493 convertToRGB(); 494 } 495 if (bytePixels != null) { 496 for (int h = srcH; h > 0; h--) { 497 System.arraycopy(pixels, srcOff, bytePixels, dstPtr, srcW); 498 srcOff += srcScan; 499 dstPtr += dstScan; 500 } 501 } 502 } 503 if (intPixels != null) { 504 int dstRem = dstScan - srcW; 505 int srcRem = srcScan - srcW; 506 for (int h = srcH; h > 0; h--) { 507 for (int w = srcW; w > 0; w--) { 508 intPixels[dstPtr++] = model.getRGB(pixels[srcOff++]&0xff); 509 } 510 srcOff += srcRem; 511 dstPtr += dstRem; 512 } 513 } 514 flags |= ImageObserver.SOMEBITS; 515 } 516 517 537 public void setPixels(int srcX, int srcY, int srcW, int srcH, 538 ColorModel model, 539 int pixels[], int srcOff, int srcScan) { 540 if (srcY < dstY) { 541 int diff = dstY - srcY; 542 if (diff >= srcH) { 543 return; 544 } 545 srcOff += srcScan * diff; 546 srcY += diff; 547 srcH -= diff; 548 } 549 if (srcY + srcH > dstY + dstH) { 550 srcH = (dstY + dstH) - srcY; 551 if (srcH <= 0) { 552 return; 553 } 554 } 555 if (srcX < dstX) { 556 int diff = dstX - srcX; 557 if (diff >= srcW) { 558 return; 559 } 560 srcOff += diff; 561 srcX += diff; 562 srcW -= diff; 563 } 564 if (srcX + srcW > dstX + dstW) { 565 srcW = (dstX + dstW) - srcX; 566 if (srcW <= 0) { 567 return; 568 } 569 } 570 if (intPixels == null) { 571 if (bytePixels == null) { 572 intPixels = new int[dstW * dstH]; 573 dstScan = dstW; 574 dstOff = 0; 575 imageModel = model; 576 } else { 577 convertToRGB(); 578 } 579 } 580 int dstPtr = dstOff + (srcY - dstY) * dstScan + (srcX - dstX); 581 if (imageModel == model) { 582 for (int h = srcH; h > 0; h--) { 583 System.arraycopy(pixels, srcOff, intPixels, dstPtr, srcW); 584 srcOff += srcScan; 585 dstPtr += dstScan; 586 } 587 } else { 588 if (imageModel != ColorModel.getRGBdefault()) { 589 convertToRGB(); 590 } 591 int dstRem = dstScan - srcW; 592 int srcRem = srcScan - srcW; 593 for (int h = srcH; h > 0; h--) { 594 for (int w = srcW; w > 0; w--) { 595 intPixels[dstPtr++] = model.getRGB(pixels[srcOff++]); 596 } 597 srcOff += srcRem; 598 dstPtr += dstRem; 599 } 600 } 601 flags |= ImageObserver.SOMEBITS; 602 } 603 604 615 public synchronized void imageComplete(int status) { 616 grabbing = false; 617 switch (status) { 618 default: 619 case IMAGEERROR: 620 flags |= ImageObserver.ERROR | ImageObserver.ABORT; 621 break; 622 case IMAGEABORTED: 623 flags |= ImageObserver.ABORT; 624 break; 625 case STATICIMAGEDONE: 626 flags |= ImageObserver.ALLBITS; 627 break; 628 case SINGLEFRAMEDONE: 629 flags |= ImageObserver.FRAMEBITS; 630 break; 631 } 632 producer.removeConsumer(this); 633 notifyAll(); 634 } 635 636 648 public synchronized int status() { 649 return flags; 650 } 651 } 652 | Popular Tags |