82 ColorModel imageModel;
83 byte[] bytePixels;
84 int[] intPixels;
85 int dstOff;
86 int dstScan;
87
88 private boolean grabbing;
89 private int flags;
90
91 private static final int GRABBEDBITS = (ImageObserver.FRAMEBITS
92 | ImageObserver.ALLBITS);
93 private static final int DONEBITS = (GRABBEDBITS
94 | ImageObserver.ERROR);
95
96 /**
97 * Create a PixelGrabber object to grab the (x, y, w, h) rectangular
98 * section of pixels from the specified image into the given array.
99 * The pixels are stored into the array in the default RGB ColorModel.
100 * The RGB data for pixel (i, j) where (i, j) is inside the rectangle
101 * (x, y, w, h) is stored in the array at
102 * <tt>pix[(j - y) * scansize + (i - x) + off]</tt>.
103 * @see ColorModel#getRGBdefault
104 * @param img the image to retrieve pixels from
105 * @param x the x coordinate of the upper left corner of the rectangle
106 * of pixels to retrieve from the image, relative to the default
107 * (unscaled) size of the image
108 * @param y the y coordinate of the upper left corner of the rectangle
109 * of pixels to retrieve from the image
110 * @param w the width of the rectangle of pixels to retrieve
111 * @param h the height of the rectangle of pixels to retrieve
112 * @param pix the array of integers which are to be used to hold the
113 * RGB pixels retrieved from the image
114 * @param off the offset into the array of where to store the first pixel
115 * @param scansize the distance from one row of pixels to the next in
116 * the array
117 */
118 public PixelGrabber(Image img, int x, int y, int w, int h,
119 int[] pix, int off, int scansize) {
120 this(img.getSource(), x, y, w, h, pix, off, scansize);
121 }
122
123 /**
124 * Create a PixelGrabber object to grab the (x, y, w, h) rectangular
125 * section of pixels from the image produced by the specified
126 * ImageProducer into the given array.
127 * The pixels are stored into the array in the default RGB ColorModel.
128 * The RGB data for pixel (i, j) where (i, j) is inside the rectangle
129 * (x, y, w, h) is stored in the array at
130 * <tt>pix[(j - y) * scansize + (i - x) + off]</tt>.
131 * @param ip the <code>ImageProducer</code> that produces the
132 * image from which to retrieve pixels
133 * @param x the x coordinate of the upper left corner of the rectangle
134 * of pixels to retrieve from the image, relative to the default
135 * (unscaled) size of the image
136 * @param y the y coordinate of the upper left corner of the rectangle
137 * of pixels to retrieve from the image
138 * @param w the width of the rectangle of pixels to retrieve
139 * @param h the height of the rectangle of pixels to retrieve
140 * @param pix the array of integers which are to be used to hold the
141 * RGB pixels retrieved from the image
142 * @param off the offset into the array of where to store the first pixel
143 * @param scansize the distance from one row of pixels to the next in
144 * the array
145 * @see ColorModel#getRGBdefault
146 */
147 public PixelGrabber(ImageProducer ip, int x, int y, int w, int h,
148 int[] pix, int off, int scansize) {
149 producer = ip;
150 dstX = x;
151 dstY = y;
214 }
215
216 /**
217 * Request the Image or ImageProducer to start delivering pixels and
218 * wait for all of the pixels in the rectangle of interest to be
219 * delivered.
220 * @return true if the pixels were successfully grabbed, false on
221 * abort, error or timeout
222 * @exception InterruptedException
223 * Another thread has interrupted this thread.
224 */
225 public boolean grabPixels() throws InterruptedException {
226 return grabPixels(0);
227 }
228
229 /**
230 * Request the Image or ImageProducer to start delivering pixels and
231 * wait for all of the pixels in the rectangle of interest to be
232 * delivered or until the specified timeout has elapsed. This method
233 * behaves in the following ways, depending on the value of
234 * <code>ms</code>:
235 * <ul>
236 * <li> If {@code ms == 0}, waits until all pixels are delivered
237 * <li> If {@code ms > 0}, waits until all pixels are delivered
238 * as timeout expires.
239 * <li> If {@code ms < 0}, returns <code>true</code> if all pixels
240 * are grabbed, <code>false</code> otherwise and does not wait.
241 * </ul>
242 * @param ms the number of milliseconds to wait for the image pixels
243 * to arrive before timing out
244 * @return true if the pixels were successfully grabbed, false on
245 * abort, error or timeout
246 * @exception InterruptedException
247 * Another thread has interrupted this thread.
248 */
249 public synchronized boolean grabPixels(long ms)
250 throws InterruptedException
251 {
252 if ((flags & DONEBITS) != 0) {
253 return (flags & GRABBEDBITS) != 0;
254 }
255 long end = ms + System.currentTimeMillis();
256 if (!grabbing) {
257 grabbing = true;
258 flags &= ~(ImageObserver.ABORT);
259 producer.startProduction(this);
260 }
403 * Note: This method is intended to be called by the ImageProducer
404 * of the Image whose pixels are being grabbed. Developers using
405 * this class to retrieve pixels from an image should avoid calling
406 * this method directly since that operation could result in problems
407 * with retrieving the requested pixels.
408 * @param props the list of properties
409 */
410 public void setProperties(Hashtable<?,?> props) {
411 return;
412 }
413
414 /**
415 * The setColorModel method is part of the ImageConsumer API which
416 * this class must implement to retrieve the pixels.
417 * <p>
418 * Note: This method is intended to be called by the ImageProducer
419 * of the Image whose pixels are being grabbed. Developers using
420 * this class to retrieve pixels from an image should avoid calling
421 * this method directly since that operation could result in problems
422 * with retrieving the requested pixels.
423 * @param model the specified <code>ColorModel</code>
424 * @see #getColorModel
425 */
426 public void setColorModel(ColorModel model) {
427 return;
428 }
429
430 private void convertToRGB() {
431 int size = dstW * dstH;
432 int newpixels[] = new int[size];
433 if (bytePixels != null) {
434 for (int i = 0; i < size; i++) {
435 newpixels[i] = imageModel.getRGB(bytePixels[i] & 0xff);
436 }
437 } else if (intPixels != null) {
438 for (int i = 0; i < size; i++) {
439 newpixels[i] = imageModel.getRGB(intPixels[i]);
440 }
441 }
442 bytePixels = null;
443 intPixels = newpixels;
444 dstScan = dstW;
445 dstOff = 0;
446 imageModel = ColorModel.getRGBdefault();
447 }
448
449 /**
450 * The setPixels method is part of the ImageConsumer API which
451 * this class must implement to retrieve the pixels.
452 * <p>
453 * Note: This method is intended to be called by the ImageProducer
454 * of the Image whose pixels are being grabbed. Developers using
455 * this class to retrieve pixels from an image should avoid calling
456 * this method directly since that operation could result in problems
457 * with retrieving the requested pixels.
458 * @param srcX the X coordinate of the upper-left corner
459 * of the area of pixels to be set
460 * @param srcY the Y coordinate of the upper-left corner
461 * of the area of pixels to be set
462 * @param srcW the width of the area of pixels
463 * @param srcH the height of the area of pixels
464 * @param model the specified <code>ColorModel</code>
465 * @param pixels the array of pixels
466 * @param srcOff the offset into the pixels array
467 * @param srcScan the distance from one row of pixels to the next
468 * in the pixels array
469 * @see #getPixels
470 */
471 public void setPixels(int srcX, int srcY, int srcW, int srcH,
472 ColorModel model,
473 byte pixels[], int srcOff, int srcScan) {
474 if (srcY < dstY) {
475 int diff = dstY - srcY;
476 if (diff >= srcH) {
477 return;
478 }
479 srcOff += srcScan * diff;
480 srcY += diff;
481 srcH -= diff;
482 }
483 if (srcY + srcH > dstY + dstH) {
484 srcH = (dstY + dstH) - srcY;
531 }
532 }
533 flags |= ImageObserver.SOMEBITS;
534 }
535
536 /**
537 * The setPixels method is part of the ImageConsumer API which
538 * this class must implement to retrieve the pixels.
539 * <p>
540 * Note: This method is intended to be called by the ImageProducer
541 * of the Image whose pixels are being grabbed. Developers using
542 * this class to retrieve pixels from an image should avoid calling
543 * this method directly since that operation could result in problems
544 * with retrieving the requested pixels.
545 * @param srcX the X coordinate of the upper-left corner
546 * of the area of pixels to be set
547 * @param srcY the Y coordinate of the upper-left corner
548 * of the area of pixels to be set
549 * @param srcW the width of the area of pixels
550 * @param srcH the height of the area of pixels
551 * @param model the specified <code>ColorModel</code>
552 * @param pixels the array of pixels
553 * @param srcOff the offset into the pixels array
554 * @param srcScan the distance from one row of pixels to the next
555 * in the pixels array
556 * @see #getPixels
557 */
558 public void setPixels(int srcX, int srcY, int srcW, int srcH,
559 ColorModel model,
560 int pixels[], int srcOff, int srcScan) {
561 if (srcY < dstY) {
562 int diff = dstY - srcY;
563 if (diff >= srcH) {
564 return;
565 }
566 srcOff += srcScan * diff;
567 srcY += diff;
568 srcH -= diff;
569 }
570 if (srcY + srcH > dstY + dstH) {
571 srcH = (dstY + dstH) - srcY;
641 flags |= ImageObserver.ERROR | ImageObserver.ABORT;
642 break;
643 case IMAGEABORTED:
644 flags |= ImageObserver.ABORT;
645 break;
646 case STATICIMAGEDONE:
647 flags |= ImageObserver.ALLBITS;
648 break;
649 case SINGLEFRAMEDONE:
650 flags |= ImageObserver.FRAMEBITS;
651 break;
652 }
653 producer.removeConsumer(this);
654 notifyAll();
655 }
656
657 /**
658 * Returns the status of the pixels. The ImageObserver flags
659 * representing the available pixel information are returned.
660 * This method and {@link #getStatus() getStatus} have the
661 * same implementation, but <code>getStatus</code> is the
662 * preferred method because it conforms to the convention of
663 * naming information-retrieval methods with the form
664 * "getXXX".
665 * @return the bitwise OR of all relevant ImageObserver flags
666 * @see ImageObserver
667 * @see #getStatus()
668 */
669 public synchronized int status() {
670 return flags;
671 }
672 }
|
82 ColorModel imageModel;
83 byte[] bytePixels;
84 int[] intPixels;
85 int dstOff;
86 int dstScan;
87
88 private boolean grabbing;
89 private int flags;
90
91 private static final int GRABBEDBITS = (ImageObserver.FRAMEBITS
92 | ImageObserver.ALLBITS);
93 private static final int DONEBITS = (GRABBEDBITS
94 | ImageObserver.ERROR);
95
96 /**
97 * Create a PixelGrabber object to grab the (x, y, w, h) rectangular
98 * section of pixels from the specified image into the given array.
99 * The pixels are stored into the array in the default RGB ColorModel.
100 * The RGB data for pixel (i, j) where (i, j) is inside the rectangle
101 * (x, y, w, h) is stored in the array at
102 * {@code pix[(j - y) * scansize + (i - x) + off]}.
103 * @see ColorModel#getRGBdefault
104 * @param img the image to retrieve pixels from
105 * @param x the x coordinate of the upper left corner of the rectangle
106 * of pixels to retrieve from the image, relative to the default
107 * (unscaled) size of the image
108 * @param y the y coordinate of the upper left corner of the rectangle
109 * of pixels to retrieve from the image
110 * @param w the width of the rectangle of pixels to retrieve
111 * @param h the height of the rectangle of pixels to retrieve
112 * @param pix the array of integers which are to be used to hold the
113 * RGB pixels retrieved from the image
114 * @param off the offset into the array of where to store the first pixel
115 * @param scansize the distance from one row of pixels to the next in
116 * the array
117 */
118 public PixelGrabber(Image img, int x, int y, int w, int h,
119 int[] pix, int off, int scansize) {
120 this(img.getSource(), x, y, w, h, pix, off, scansize);
121 }
122
123 /**
124 * Create a PixelGrabber object to grab the (x, y, w, h) rectangular
125 * section of pixels from the image produced by the specified
126 * ImageProducer into the given array.
127 * The pixels are stored into the array in the default RGB ColorModel.
128 * The RGB data for pixel (i, j) where (i, j) is inside the rectangle
129 * (x, y, w, h) is stored in the array at
130 * {@code pix[(j - y) * scansize + (i - x) + off]}.
131 * @param ip the {@code ImageProducer} that produces the
132 * image from which to retrieve pixels
133 * @param x the x coordinate of the upper left corner of the rectangle
134 * of pixels to retrieve from the image, relative to the default
135 * (unscaled) size of the image
136 * @param y the y coordinate of the upper left corner of the rectangle
137 * of pixels to retrieve from the image
138 * @param w the width of the rectangle of pixels to retrieve
139 * @param h the height of the rectangle of pixels to retrieve
140 * @param pix the array of integers which are to be used to hold the
141 * RGB pixels retrieved from the image
142 * @param off the offset into the array of where to store the first pixel
143 * @param scansize the distance from one row of pixels to the next in
144 * the array
145 * @see ColorModel#getRGBdefault
146 */
147 public PixelGrabber(ImageProducer ip, int x, int y, int w, int h,
148 int[] pix, int off, int scansize) {
149 producer = ip;
150 dstX = x;
151 dstY = y;
214 }
215
216 /**
217 * Request the Image or ImageProducer to start delivering pixels and
218 * wait for all of the pixels in the rectangle of interest to be
219 * delivered.
220 * @return true if the pixels were successfully grabbed, false on
221 * abort, error or timeout
222 * @exception InterruptedException
223 * Another thread has interrupted this thread.
224 */
225 public boolean grabPixels() throws InterruptedException {
226 return grabPixels(0);
227 }
228
229 /**
230 * Request the Image or ImageProducer to start delivering pixels and
231 * wait for all of the pixels in the rectangle of interest to be
232 * delivered or until the specified timeout has elapsed. This method
233 * behaves in the following ways, depending on the value of
234 * {@code ms}:
235 * <ul>
236 * <li> If {@code ms == 0}, waits until all pixels are delivered
237 * <li> If {@code ms > 0}, waits until all pixels are delivered
238 * as timeout expires.
239 * <li> If {@code ms < 0}, returns {@code true} if all pixels
240 * are grabbed, {@code false} otherwise and does not wait.
241 * </ul>
242 * @param ms the number of milliseconds to wait for the image pixels
243 * to arrive before timing out
244 * @return true if the pixels were successfully grabbed, false on
245 * abort, error or timeout
246 * @exception InterruptedException
247 * Another thread has interrupted this thread.
248 */
249 public synchronized boolean grabPixels(long ms)
250 throws InterruptedException
251 {
252 if ((flags & DONEBITS) != 0) {
253 return (flags & GRABBEDBITS) != 0;
254 }
255 long end = ms + System.currentTimeMillis();
256 if (!grabbing) {
257 grabbing = true;
258 flags &= ~(ImageObserver.ABORT);
259 producer.startProduction(this);
260 }
403 * Note: This method is intended to be called by the ImageProducer
404 * of the Image whose pixels are being grabbed. Developers using
405 * this class to retrieve pixels from an image should avoid calling
406 * this method directly since that operation could result in problems
407 * with retrieving the requested pixels.
408 * @param props the list of properties
409 */
410 public void setProperties(Hashtable<?,?> props) {
411 return;
412 }
413
414 /**
415 * The setColorModel method is part of the ImageConsumer API which
416 * this class must implement to retrieve the pixels.
417 * <p>
418 * Note: This method is intended to be called by the ImageProducer
419 * of the Image whose pixels are being grabbed. Developers using
420 * this class to retrieve pixels from an image should avoid calling
421 * this method directly since that operation could result in problems
422 * with retrieving the requested pixels.
423 * @param model the specified {@code ColorModel}
424 * @see #getColorModel
425 */
426 public void setColorModel(ColorModel model) {
427 return;
428 }
429
430 private void convertToRGB() {
431 int size = dstW * dstH;
432 int newpixels[] = new int[size];
433 if (bytePixels != null) {
434 for (int i = 0; i < size; i++) {
435 newpixels[i] = imageModel.getRGB(bytePixels[i] & 0xff);
436 }
437 } else if (intPixels != null) {
438 for (int i = 0; i < size; i++) {
439 newpixels[i] = imageModel.getRGB(intPixels[i]);
440 }
441 }
442 bytePixels = null;
443 intPixels = newpixels;
444 dstScan = dstW;
445 dstOff = 0;
446 imageModel = ColorModel.getRGBdefault();
447 }
448
449 /**
450 * The setPixels method is part of the ImageConsumer API which
451 * this class must implement to retrieve the pixels.
452 * <p>
453 * Note: This method is intended to be called by the ImageProducer
454 * of the Image whose pixels are being grabbed. Developers using
455 * this class to retrieve pixels from an image should avoid calling
456 * this method directly since that operation could result in problems
457 * with retrieving the requested pixels.
458 * @param srcX the X coordinate of the upper-left corner
459 * of the area of pixels to be set
460 * @param srcY the Y coordinate of the upper-left corner
461 * of the area of pixels to be set
462 * @param srcW the width of the area of pixels
463 * @param srcH the height of the area of pixels
464 * @param model the specified {@code ColorModel}
465 * @param pixels the array of pixels
466 * @param srcOff the offset into the pixels array
467 * @param srcScan the distance from one row of pixels to the next
468 * in the pixels array
469 * @see #getPixels
470 */
471 public void setPixels(int srcX, int srcY, int srcW, int srcH,
472 ColorModel model,
473 byte pixels[], int srcOff, int srcScan) {
474 if (srcY < dstY) {
475 int diff = dstY - srcY;
476 if (diff >= srcH) {
477 return;
478 }
479 srcOff += srcScan * diff;
480 srcY += diff;
481 srcH -= diff;
482 }
483 if (srcY + srcH > dstY + dstH) {
484 srcH = (dstY + dstH) - srcY;
531 }
532 }
533 flags |= ImageObserver.SOMEBITS;
534 }
535
536 /**
537 * The setPixels method is part of the ImageConsumer API which
538 * this class must implement to retrieve the pixels.
539 * <p>
540 * Note: This method is intended to be called by the ImageProducer
541 * of the Image whose pixels are being grabbed. Developers using
542 * this class to retrieve pixels from an image should avoid calling
543 * this method directly since that operation could result in problems
544 * with retrieving the requested pixels.
545 * @param srcX the X coordinate of the upper-left corner
546 * of the area of pixels to be set
547 * @param srcY the Y coordinate of the upper-left corner
548 * of the area of pixels to be set
549 * @param srcW the width of the area of pixels
550 * @param srcH the height of the area of pixels
551 * @param model the specified {@code ColorModel}
552 * @param pixels the array of pixels
553 * @param srcOff the offset into the pixels array
554 * @param srcScan the distance from one row of pixels to the next
555 * in the pixels array
556 * @see #getPixels
557 */
558 public void setPixels(int srcX, int srcY, int srcW, int srcH,
559 ColorModel model,
560 int pixels[], int srcOff, int srcScan) {
561 if (srcY < dstY) {
562 int diff = dstY - srcY;
563 if (diff >= srcH) {
564 return;
565 }
566 srcOff += srcScan * diff;
567 srcY += diff;
568 srcH -= diff;
569 }
570 if (srcY + srcH > dstY + dstH) {
571 srcH = (dstY + dstH) - srcY;
641 flags |= ImageObserver.ERROR | ImageObserver.ABORT;
642 break;
643 case IMAGEABORTED:
644 flags |= ImageObserver.ABORT;
645 break;
646 case STATICIMAGEDONE:
647 flags |= ImageObserver.ALLBITS;
648 break;
649 case SINGLEFRAMEDONE:
650 flags |= ImageObserver.FRAMEBITS;
651 break;
652 }
653 producer.removeConsumer(this);
654 notifyAll();
655 }
656
657 /**
658 * Returns the status of the pixels. The ImageObserver flags
659 * representing the available pixel information are returned.
660 * This method and {@link #getStatus() getStatus} have the
661 * same implementation, but {@code getStatus} is the
662 * preferred method because it conforms to the convention of
663 * naming information-retrieval methods with the form
664 * "getXXX".
665 * @return the bitwise OR of all relevant ImageObserver flags
666 * @see ImageObserver
667 * @see #getStatus()
668 */
669 public synchronized int status() {
670 return flags;
671 }
672 }
|