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 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
|
package ij.process;
import java.util.*;
import java.awt.*;
import java.awt.image.*;
import ij.gui.*;
/** This is an 32-bit floating-point image and methods that operate on that image. */
public class FloatProcessor extends ImageProcessor {
private float min, max, snapshotMin, snapshotMax;
private float[] pixels;
protected byte[] pixels8;
private float[] snapshotPixels = null;
private float fillColor = Float.MAX_VALUE;
private boolean fixedScale = false;
/** Creates a new FloatProcessor using the specified pixel array and ColorModel.
Set 'cm' to null to use the default grayscale LUT. */
public FloatProcessor(int width, int height, float[] pixels, ColorModel cm) {
if (pixels!=null && width*height!=pixels.length)
throw new IllegalArgumentException(WRONG_LENGTH);
this.width = width;
this.height = height;
this.pixels = pixels;
this.cm = cm;
resetRoi();
}
/** Creates a blank FloatProcessor using the default grayscale LUT that
displays zero as black. Call invertLut() to display zero as white. */
public FloatProcessor(int width, int height) {
this(width, height, new float[width*height], null);
}
/** Creates a FloatProcessor from an int array using the default grayscale LUT. */
public FloatProcessor(int width, int height, int[] pixels) {
this(width, height);
for (int i=0; i<pixels.length; i++)
this.pixels[i] = (float)(pixels[i]);
}
/** Creates a FloatProcessor from a double array using the default grayscale LUT. */
public FloatProcessor(int width, int height, double[] pixels) {
this(width, height);
for (int i=0; i<pixels.length; i++)
this.pixels[i] = (float)pixels[i];
}
/** Creates a FloatProcessor from a 2D float array using the default LUT. */
public FloatProcessor(float[][] array) {
width = array.length;
height = array[0].length;
pixels = new float[width*height];
int i=0;
for (int y=0; y<height; y++) {
for (int x=0; x<width; x++) {
pixels[i++] = array[x][y];
}
}
resetRoi();
}
/** Creates a FloatProcessor from a 2D int array. */
public FloatProcessor(int[][] array) {
width = array.length;
height = array[0].length;
pixels = new float[width*height];
int i=0;
for (int y=0; y<height; y++) {
for (int x=0; x<width; x++) {
pixels[i++] = (float)array[x][y];
}
}
}
/**
Calculates the minimum and maximum pixel value for the entire image.
Returns without doing anything if fixedScale has been set true as a result
of calling setMinAndMax(). In this case, getMin() and getMax() return the
fixed min and max defined by setMinAndMax(), rather than the calculated min
and max.
@see #getMin()
@see #getMin()
*/
public void findMinAndMax() {
//ij.IJ.log("findMinAndMax: "+fixedScale);
if (fixedScale)
return;
min = Float.MAX_VALUE;
max = -Float.MAX_VALUE;
for (int i=0; i < width*height; i++) {
float value = pixels[i];
if (!Float.isInfinite(value)) {
if (value<min)
min = value;
if (value>max)
max = value;
}
}
minMaxSet = true;
showProgress(1.0);
}
/**
Sets the min and max variables that control how real
pixel values are mapped to 0-255 screen values. Use
resetMinAndMax() to enable auto-scaling;
@see ij.plugin.frame.ContrastAdjuster
*/
public void setMinAndMax(double minimum, double maximum) {
if (minimum==0.0 && maximum==0.0)
{resetMinAndMax(); return;}
min = (float)minimum;
max = (float)maximum;
fixedScale = true;
minMaxSet = true;
resetThreshold();
}
/** Recalculates the min and max values used to scale pixel
values to 0-255 for display. This ensures that this
FloatProcessor is set up to correctly display the image. */
public void resetMinAndMax() {
fixedScale = false;
findMinAndMax();
resetThreshold();
}
/** Returns the smallest displayed pixel value. */
public double getMin() {
if (!minMaxSet) findMinAndMax();
return min;
}
/** Returns the largest displayed pixel value. */
public double getMax() {
if (!minMaxSet) findMinAndMax();
return max;
}
public Image createImage() {
boolean firstTime = pixels8==null;
if (firstTime || !lutAnimation)
create8BitImage();
if (cm==null)
makeDefaultColorModel();
if (source==null) {
source = new MemoryImageSource(width, height, cm, pixels8, 0, width);
source.setAnimated(true);
source.setFullBufferUpdates(true);
img = Toolkit.getDefaultToolkit().createImage(source);
} else if (newPixels) {
source.newPixels(pixels8, cm, 0, width);
newPixels = false;
} else
source.newPixels();
lutAnimation = false;
return img;
}
protected byte[] create8BitImage() {
// scale from float to 8-bits
int size = width*height;
if (pixels8==null)
pixels8 = new byte[size];
float value;
int ivalue;
float min2=(float)getMin(), max2=(float)getMax();
float scale = 255f/(max2-min2);
for (int i=0; i<size; i++) {
value = pixels[i]-min2;
if (value<0f) value = 0f;
ivalue = (int)((value*scale)+0.5f);
if (ivalue>255) ivalue = 255;
pixels8[i] = (byte)ivalue;
}
return pixels8;
}
/** Returns this image as an 8-bit BufferedImage. */
public BufferedImage getBufferedImage() {
return convertToByte(true).getBufferedImage();
}
/** Returns a new, blank FloatProcessor with the specified width and height. */
public ImageProcessor createProcessor(int width, int height) {
ImageProcessor ip2 = new FloatProcessor(width, height, new float[width*height], getColorModel());
ip2.setMinAndMax(getMin(), getMax());
ip2.setInterpolationMethod(interpolationMethod);
return ip2;
}
public void snapshot() {
snapshotWidth=width;
snapshotHeight=height;
snapshotMin=(float)getMin();
snapshotMax=(float)getMax();
if (snapshotPixels==null || (snapshotPixels!=null && snapshotPixels.length!=pixels.length))
snapshotPixels = new float[width * height];
System.arraycopy(pixels, 0, snapshotPixels, 0, width*height);
}
public void reset() {
if (snapshotPixels==null)
return;
min=snapshotMin;
max=snapshotMax;
minMaxSet = true;
System.arraycopy(snapshotPixels,0,pixels,0,width*height);
}
public void reset(ImageProcessor mask) {
if (mask==null || snapshotPixels==null)
return;
if (mask.getWidth()!=roiWidth||mask.getHeight()!=roiHeight)
throw new IllegalArgumentException(maskSizeError(mask));
byte[] mpixels = (byte[])mask.getPixels();
for (int y=roiY, my=0; y<(roiY+roiHeight); y++, my++) {
int i = y * width + roiX;
int mi = my * roiWidth;
for (int x=roiX; x<(roiX+roiWidth); x++) {
if (mpixels[mi++]==0)
pixels[i] = snapshotPixels[i];
i++;
}
}
}
/** Swaps the pixel and snapshot (undo) arrays. */
public void swapPixelArrays() {
if (snapshotPixels==null) return;
float pixel;
for (int i=0; i<pixels.length; i++) {
pixel = pixels[i];
pixels[i] = snapshotPixels[i];
snapshotPixels[i] = pixel;
}
}
public void setSnapshotPixels(Object pixels) {
snapshotPixels = (float[])pixels;
snapshotWidth=width;
snapshotHeight=height;
}
public Object getSnapshotPixels() {
return snapshotPixels;
}
/** Returns a pixel value that must be converted using
Float.intBitsToFloat(). */
public int getPixel(int x, int y) {
if (x>=0 && x<width && y>=0 && y<height)
return Float.floatToIntBits(pixels[y*width+x]);
else
return 0;
}
public final int get(int x, int y) {
return Float.floatToIntBits(pixels[y*width+x]);
}
public final void set(int x, int y, int value) {
pixels[y*width + x] = Float.intBitsToFloat(value);
}
public final int get(int index) {
return Float.floatToIntBits(pixels[index]);
}
public final void set(int index, int value) {
pixels[index] = Float.intBitsToFloat(value);
}
public final float getf(int x, int y) {
return pixels[y*width+x];
}
public final void setf(int x, int y, float value) {
pixels[y*width + x] = value;
}
public final float getf(int index) {
return pixels[index];
}
public final void setf(int index, float value) {
pixels[index] = value;
}
/** Returns the value of the pixel at (x,y) in a
one element int array. iArray is an optiona
preallocated array. */
public int[] getPixel(int x, int y, int[] iArray) {
if (iArray==null) iArray = new int[1];
iArray[0] = (int)getPixelValue(x, y);
return iArray;
}
/** Sets a pixel in the image using a one element int array. */
public final void putPixel(int x, int y, int[] iArray) {
putPixelValue(x, y, iArray[0]);
}
/** Uses the current interpolation method (BILINEAR or BICUBIC)
to calculate the pixel value at real coordinates (x,y). */
public double getInterpolatedPixel(double x, double y) {
if (interpolationMethod==BICUBIC)
return getBicubicInterpolatedPixel(x, y, this);
else {
if (x<0.0) x = 0.0;
if (x>=width-1.0) x = width-1.001;
if (y<0.0) y = 0.0;
if (y>=height-1.0) y = height-1.001;
return getInterpolatedPixel(x, y, pixels);
}
}
final public int getPixelInterpolated(double x, double y) {
if (interpolationMethod==BILINEAR) {
if (x<0.0 || y<0.0 || x>=width-1 || y>=height-1)
return 0;
else
return Float.floatToIntBits((float)getInterpolatedPixel(x, y, pixels));
} else if (interpolationMethod==BICUBIC)
return Float.floatToIntBits((float)getBicubicInterpolatedPixel(x, y, this));
else
return getPixel((int)(x+0.5), (int)(y+0.5));
}
/** Stores the specified value at (x,y). The value is expected to be a
float that has been converted to an int using Float.floatToIntBits(). */
public final void putPixel(int x, int y, int value) {
if (x>=0 && x<width && y>=0 && y<height)
pixels[y*width + x] = Float.intBitsToFloat(value);
}
/** Stores the specified real value at (x,y). */
public void putPixelValue(int x, int y, double value) {
if (x>=0 && x<width && y>=0 && y<height)
pixels[y*width + x] = (float)value;
}
/** Returns the value of the pixel at (x,y) as a float. */
public float getPixelValue(int x, int y) {
if (x>=0 && x<width && y>=0 && y<height)
return pixels[y*width + x];
else
return 0f;
}
/** Draws a pixel in the current foreground color. */
public void drawPixel(int x, int y) {
if (x>=clipXMin && x<=clipXMax && y>=clipYMin && y<=clipYMax)
putPixel(x, y, Float.floatToIntBits(fillColor));
}
/** Returns a reference to the float array containing
this image's pixel data. */
public Object getPixels() {
return (Object)pixels;
}
/** Returns a copy of the pixel data. Or returns a reference to the
snapshot buffer if it is not null and 'snapshotCopyMode' is true.
@see ImageProcessor#snapshot
@see ImageProcessor#setSnapshotCopyMode
*/
public Object getPixelsCopy() {
if (snapshotCopyMode && snapshotPixels!=null) {
snapshotCopyMode = false;
return snapshotPixels;
} else {
float[] pixels2 = new float[width*height];
System.arraycopy(pixels, 0, pixels2, 0, width*height);
return pixels2;
}
}
public void setPixels(Object pixels) {
this.pixels = (float[])pixels;
resetPixels(pixels);
if (pixels==null) snapshotPixels = null;
if (pixels==null) pixels8 = null;
}
/** Copies the image contained in 'ip' to (xloc, yloc) using one of
the transfer modes defined in the Blitter interface. */
public void copyBits(ImageProcessor ip, int xloc, int yloc, int mode) {
ip = ip.convertToFloat();
new FloatBlitter(this).copyBits(ip, xloc, yloc, mode);
}
public void applyTable(int[] lut) {}
private void process(int op, double value) {
float c, v1, v2;
//boolean resetMinMax = roiWidth==width && roiHeight==height && !(op==FILL);
c = (float)value;
float min2=0f, max2=0f;
if (op==INVERT)
{min2=(float)getMin(); max2=(float)getMax();}
for (int y=roiY; y<(roiY+roiHeight); y++) {
int i = y * width + roiX;
for (int x=roiX; x<(roiX+roiWidth); x++) {
v1 = pixels[i];
switch(op) {
case INVERT:
v2 = max2 - (v1 - min2);
break;
case FILL:
v2 = fillColor;
break;
case ADD:
v2 = v1 + c;
break;
case MULT:
v2 = v1 * c;
break;
case GAMMA:
if (v1<=0f)
v2 = 0f;
else
v2 = (float)Math.exp(c*Math.log(v1));
break;
case LOG:
if (v1<=0f)
v2 = 0f;
else
v2 = (float)Math.log(v1);
break;
case EXP:
v2 = (float)Math.exp(v1);
break;
case SQR:
v2 = v1*v1;
break;
case SQRT:
if (v1<=0f)
v2 = 0f;
else
v2 = (float)Math.sqrt(v1);
break;
case ABS:
v2 = (float)Math.abs(v1);
break;
case MINIMUM:
if (v1<value)
v2 = (float)value;
else
v2 = v1;
break;
case MAXIMUM:
if (v1>value)
v2 = (float)value;
else
v2 = v1;
break;
default:
v2 = v1;
}
pixels[i++] = v2;
}
}
}
public void invert() {process(INVERT, 0.0);}
public void add(int value) {process(ADD, value);}
public void add(double value) {process(ADD, value);}
public void multiply(double value) {process(MULT, value);}
public void and(int value) {}
public void or(int value) {}
public void xor(int value) {}
public void gamma(double value) {process(GAMMA, value);}
public void log() {process(LOG, 0.0);}
public void exp() {process(EXP, 0.0);}
public void sqr() {process(SQR, 0.0);}
public void sqrt() {process(SQRT, 0.0);}
public void abs() {process(ABS, 0.0);}
public void min(double value) {process(MINIMUM, value);}
public void max(double value) {process(MAXIMUM, value);}
/** Fills the current rectangular ROI. */
public void fill() {process(FILL, 0.0);}
/** Fills pixels that are within roi and part of the mask.
Does nothing if the mask is not the same as the the ROI. */
public void fill(ImageProcessor mask) {
if (mask==null)
{fill(); return;}
int roiWidth=this.roiWidth, roiHeight=this.roiHeight;
int roiX=this.roiX, roiY=this.roiY;
if (mask.getWidth()!=roiWidth||mask.getHeight()!=roiHeight)
return;
byte[] mpixels = (byte[])mask.getPixels();
for (int y=roiY, my=0; y<(roiY+roiHeight); y++, my++) {
int i = y * width + roiX;
int mi = my * roiWidth;
for (int x=roiX; x<(roiX+roiWidth); x++) {
if (mpixels[mi++]!=0)
pixels[i] = fillColor;
i++;
}
}
}
/** Does 3x3 convolution. */
public void convolve3x3(int[] kernel) {
filter3x3(CONVOLVE, kernel);
}
/** Filters using a 3x3 neighborhood. */
public void filter(int type) {
filter3x3(type, null);
}
/** 3x3 filter operations, code partly based on 3x3 convolution code
* contributed by Glynne Casteel. */
void filter3x3(int type, int[] kernel) {
float v1, v2, v3; //input pixel values around the current pixel
float v4, v5, v6;
float v7, v8, v9;
float k1=0f, k2=0f, k3=0f; //kernel values (used for CONVOLVE only)
float k4=0f, k5=0f, k6=0f;
float k7=0f, k8=0f, k9=0f;
float scale = 0f;
if (type==CONVOLVE) {
k1=kernel[0]; k2=kernel[1]; k3=kernel[2];
k4=kernel[3]; k5=kernel[4]; k6=kernel[5];
k7=kernel[6]; k8=kernel[7]; k9=kernel[8];
for (int i=0; i<kernel.length; i++)
scale += kernel[i];
if (scale==0) scale = 1f;
scale = 1f/scale; //multiplication factor (multiply is faster than divide)
}
int inc = roiHeight/25;
if (inc<1) inc = 1;
float[] pixels2 = (float[])getPixelsCopy();
//float[] pixels2 = (float[])getPixelsCopy();
int xEnd = roiX + roiWidth;
int yEnd = roiY + roiHeight;
for (int y=roiY; y<yEnd; y++) {
int p = roiX + y*width; //points to current pixel
int p6 = p - (roiX>0 ? 1 : 0); //will point to v6, currently lower
int p3 = p6 - (y>0 ? width : 0); //will point to v3, currently lower
int p9 = p6 + (y<height-1 ? width : 0); // ... to v9, currently lower
v2 = pixels2[p3];
v5 = pixels2[p6];
v8 = pixels2[p9];
if (roiX>0) { p3++; p6++; p9++; }
v3 = pixels2[p3];
v6 = pixels2[p6];
v9 = pixels2[p9];
switch (type) {
case BLUR_MORE:
for (int x=roiX; x<xEnd; x++,p++) {
if (x<width-1) { p3++; p6++; p9++; }
v1 = v2; v2 = v3;
v3 = pixels2[p3];
v4 = v5; v5 = v6;
v6 = pixels2[p6];
v7 = v8; v8 = v9;
v9 = pixels2[p9];
pixels[p] = (v1+v2+v3+v4+v5+v6+v7+v8+v9)*0.11111111f; //0.111... = 1/9
}
break;
case FIND_EDGES:
for (int x=roiX; x<xEnd; x++,p++) {
if (x<width-1) { p3++; p6++; p9++; }
v1 = v2; v2 = v3;
v3 = pixels2[p3];
v4 = v5; v5 = v6;
v6 = pixels2[p6];
v7 = v8; v8 = v9;
v9 = pixels2[p9];
float sum1 = v1 + 2*v2 + v3 - v7 - 2*v8 - v9;
float sum2 = v1 + 2*v4 + v7 - v3 - 2*v6 - v9;
pixels[p] = (float)Math.sqrt(sum1*sum1 + sum2*sum2);
}
break;
case CONVOLVE:
for (int x=roiX; x<xEnd; x++,p++) {
if (x<width-1) { p3++; p6++; p9++; }
v1 = v2; v2 = v3;
v3 = pixels2[p3];
v4 = v5; v5 = v6;
v6 = pixels2[p6];
v7 = v8; v8 = v9;
v9 = pixels2[p9];
float sum = k1*v1 + k2*v2 + k3*v3
+ k4*v4 + k5*v5 + k6*v6
+ k7*v7 + k8*v8 + k9*v9;
sum *= scale;
pixels[p] = sum;
}
break;
}
if (y%inc==0)
showProgress((double)(y-roiY)/roiHeight);
}
showProgress(1.0);
}
/** Rotates the image or ROI 'angle' degrees clockwise.
@see ImageProcessor#setInterpolate
*/
public void rotate(double angle) {
float[] pixels2 = (float[])getPixelsCopy();
ImageProcessor ip2 = null;
if (interpolationMethod==BICUBIC)
ip2 = new FloatProcessor(getWidth(), getHeight(), pixels2, null);
double centerX = roiX + (roiWidth-1)/2.0;
double centerY = roiY + (roiHeight-1)/2.0;
int xMax = roiX + this.roiWidth - 1;
double angleRadians = -angle/(180.0/Math.PI);
double ca = Math.cos(angleRadians);
double sa = Math.sin(angleRadians);
double tmp1 = centerY*sa-centerX*ca;
double tmp2 = -centerX*sa-centerY*ca;
double tmp3, tmp4, xs, ys;
int index, ixs, iys;
if (interpolationMethod==BICUBIC) {
for (int y=roiY; y<(roiY + roiHeight); y++) {
index = y*width + roiX;
tmp3 = tmp1 - y*sa + centerX;
tmp4 = tmp2 + y*ca + centerY;
for (int x=roiX; x<=xMax; x++) {
xs = x*ca + tmp3;
ys = x*sa + tmp4;
pixels[index++] = (float)getBicubicInterpolatedPixel(xs, ys, ip2);
}
if (y%30==0) showProgress((double)(y-roiY)/roiHeight);
}
} else {
double dwidth=width,dheight=height;
double xlimit = width-1.0, xlimit2 = width-1.001;
double ylimit = height-1.0, ylimit2 = height-1.001;
for (int y=roiY; y<(roiY + roiHeight); y++) {
index = y*width + roiX;
tmp3 = tmp1 - y*sa + centerX;
tmp4 = tmp2 + y*ca + centerY;
for (int x=roiX; x<=xMax; x++) {
xs = x*ca + tmp3;
ys = x*sa + tmp4;
if ((xs>=-0.01) && (xs<dwidth) && (ys>=-0.01) && (ys<dheight)) {
if (interpolationMethod==BILINEAR) {
if (xs<0.0) xs = 0.0;
if (xs>=xlimit) xs = xlimit2;
if (ys<0.0) ys = 0.0;
if (ys>=ylimit) ys = ylimit2;
pixels[index++] = (float)getInterpolatedPixel(xs, ys, pixels2);
} else {
ixs = (int)(xs+0.5);
iys = (int)(ys+0.5);
if (ixs>=width) ixs = width - 1;
if (iys>=height) iys = height -1;
pixels[index++] = pixels2[width*iys+ixs];
}
} else
pixels[index++] = 0;
}
if (y%30==0)
showProgress((double)(y-roiY)/roiHeight);
}
}
showProgress(1.0);
}
public void flipVertical() {
int index1,index2;
float tmp;
for (int y=0; y<roiHeight/2; y++) {
index1 = (roiY+y)*width+roiX;
index2 = (roiY+roiHeight-1-y)*width+roiX;
for (int i=0; i<roiWidth; i++) {
tmp = pixels[index1];
pixels[index1++] = pixels[index2];
pixels[index2++] = tmp;
}
}
}
public void noise(double range) {
Random rnd=new Random();
for (int y=roiY; y<(roiY+roiHeight); y++) {
int i = y * width + roiX;
for (int x=roiX; x<(roiX+roiWidth); x++) {
float RandomBrightness = (float)(rnd.nextGaussian()*range);
pixels[i] = pixels[i] + RandomBrightness;
i++;
}
}
resetMinAndMax();
}
public ImageProcessor crop() {
ImageProcessor ip2 = createProcessor(roiWidth, roiHeight);
float[] pixels2 = (float[])ip2.getPixels();
for (int ys=roiY; ys<roiY+roiHeight; ys++) {
int offset1 = (ys-roiY)*roiWidth;
int offset2 = ys*width+roiX;
for (int xs=0; xs<roiWidth; xs++)
pixels2[offset1++] = pixels[offset2++];
}
return ip2;
}
/** Returns a duplicate of this image. */
public synchronized ImageProcessor duplicate() {
ImageProcessor ip2 = createProcessor(width, height);
float[] pixels2 = (float[])ip2.getPixels();
System.arraycopy(pixels, 0, pixels2, 0, width*height);
return ip2;
}
/** Scales the image or selection using the specified scale factors.
@see ImageProcessor#setInterpolate
*/
public void scale(double xScale, double yScale) {
double xCenter = roiX + roiWidth/2.0;
double yCenter = roiY + roiHeight/2.0;
int xmin, xmax, ymin, ymax;
if ((xScale>1.0) && (yScale>1.0)) {
//expand roi
xmin = (int)(xCenter-(xCenter-roiX)*xScale);
if (xmin<0) xmin = 0;
xmax = xmin + (int)(roiWidth*xScale) - 1;
if (xmax>=width) xmax = width - 1;
ymin = (int)(yCenter-(yCenter-roiY)*yScale);
if (ymin<0) ymin = 0;
ymax = ymin + (int)(roiHeight*yScale) - 1;
if (ymax>=height) ymax = height - 1;
} else {
xmin = roiX;
xmax = roiX + roiWidth - 1;
ymin = roiY;
ymax = roiY + roiHeight - 1;
}
float[] pixels2 = (float[])getPixelsCopy();
ImageProcessor ip2 = null;
if (interpolationMethod==BICUBIC)
ip2 = new FloatProcessor(getWidth(), getHeight(), pixels2, null);
boolean checkCoordinates = (xScale < 1.0) || (yScale < 1.0);
int index1, index2, xsi, ysi;
double ys, xs;
if (interpolationMethod==BICUBIC) {
for (int y=ymin; y<=ymax; y++) {
ys = (y-yCenter)/yScale + yCenter;
index1 = y*width + xmin;
for (int x=xmin; x<=xmax; x++) {
xs = (x-xCenter)/xScale + xCenter;
pixels[index1++] = (float)getBicubicInterpolatedPixel(xs, ys, ip2);
}
if (y%30==0) showProgress((double)(y-ymin)/height);
}
} else {
double xlimit = width-1.0, xlimit2 = width-1.001;
double ylimit = height-1.0, ylimit2 = height-1.001;
for (int y=ymin; y<=ymax; y++) {
ys = (y-yCenter)/yScale + yCenter;
ysi = (int)ys;
if (ys<0.0) ys = 0.0;
if (ys>=ylimit) ys = ylimit2;
index1 = y*width + xmin;
index2 = width*(int)ys;
for (int x=xmin; x<=xmax; x++) {
xs = (x-xCenter)/xScale + xCenter;
xsi = (int)xs;
if (checkCoordinates && ((xsi<xmin) || (xsi>xmax) || (ysi<ymin) || (ysi>ymax)))
pixels[index1++] = (float)getMin();
else {
if (interpolationMethod==BILINEAR) {
if (xs<0.0) xs = 0.0;
if (xs>=xlimit) xs = xlimit2;
pixels[index1++] = (float)getInterpolatedPixel(xs, ys, pixels2);
} else
pixels[index1++] = pixels2[index2+xsi];
}
}
if (y%30==0) showProgress((double)(y-ymin)/height);
}
}
showProgress(1.0);
}
/** Uses bilinear interpolation to find the pixel value at real coordinates (x,y). */
private final double getInterpolatedPixel(double x, double y, float[] pixels) {
int xbase = (int)x;
int ybase = (int)y;
double xFraction = x - xbase;
double yFraction = y - ybase;
int offset = ybase * width + xbase;
double lowerLeft = pixels[offset];
double lowerRight = pixels[offset + 1];
double upperRight = pixels[offset + width + 1];
double upperLeft = pixels[offset + width];
double upperAverage = upperLeft + xFraction * (upperRight - upperLeft);
double lowerAverage = lowerLeft + xFraction * (lowerRight - lowerLeft);
return lowerAverage + yFraction * (upperAverage - lowerAverage);
}
/** Creates a new FloatProcessor containing a scaled copy of this image or selection. */
public ImageProcessor resize(int dstWidth, int dstHeight) {
double srcCenterX = roiX + roiWidth/2.0;
double srcCenterY = roiY + roiHeight/2.0;
double dstCenterX = dstWidth/2.0;
double dstCenterY = dstHeight/2.0;
double xScale = (double)dstWidth/roiWidth;
double yScale = (double)dstHeight/roiHeight;
if (interpolationMethod!=NONE) {
dstCenterX += xScale/2.0;
dstCenterY += yScale/2.0;
}
ImageProcessor ip2 = createProcessor(dstWidth, dstHeight);
float[] pixels2 = (float[])ip2.getPixels();
double xs, ys;
if (interpolationMethod==BICUBIC) {
for (int y=0; y<=dstHeight-1; y++) {
ys = (y-dstCenterY)/yScale + srcCenterY;
int index = y*dstWidth;
for (int x=0; x<=dstWidth-1; x++) {
xs = (x-dstCenterX)/xScale + srcCenterX;
pixels2[index++] = (float)getBicubicInterpolatedPixel(xs, ys, this);
}
if (y%30==0) showProgress((double)y/dstHeight);
}
} else {
double xlimit = width-1.0, xlimit2 = width-1.001;
double ylimit = height-1.0, ylimit2 = height-1.001;
int index1, index2;
for (int y=0; y<=dstHeight-1; y++) {
ys = (y-dstCenterY)/yScale + srcCenterY;
if (interpolationMethod==BILINEAR) {
if (ys<0.0) ys = 0.0;
if (ys>=ylimit) ys = ylimit2;
}
index1 = width*(int)ys;
index2 = y*dstWidth;
for (int x=0; x<=dstWidth-1; x++) {
xs = (x-dstCenterX)/xScale + srcCenterX;
if (interpolationMethod==BILINEAR) {
if (xs<0.0) xs = 0.0;
if (xs>=xlimit) xs = xlimit2;
pixels2[index2++] = (float)getInterpolatedPixel(xs, ys, pixels);
} else
pixels2[index2++] = pixels[index1+(int)xs];
}
if (y%30==0) showProgress((double)y/dstHeight);
}
}
showProgress(1.0);
return ip2;
}
FloatProcessor downsize(int dstWidth, int dstHeight) {
FloatProcessor ip2 = this;
if (dstWidth<roiWidth) { //downsizing in x
ip2 = ip2.downsize1D(dstWidth, roiHeight, true);
ip2.setRoi(0, 0, dstWidth, roiHeight); //prepare roi for resizing in y
}
if (dstHeight<roiHeight) //downsizing in y
ip2 = ip2.downsize1D(ip2.getRoi().width, dstHeight, false);
if (ip2.getWidth()!=dstWidth || ip2.getHeight()!=dstHeight)
ip2 = (FloatProcessor)ip2.resize(dstWidth, dstHeight); //do any upsizing if required
return ip2;
}
// Downsizing in one direction.
private FloatProcessor downsize1D(int dstWidth, int dstHeight, boolean xDirection) {
int srcPointInc = xDirection ? 1 : width; //increment of array index for next point along direction
int srcLineInc = xDirection ? width : 1; //increment of array index for next line to be downscaled
int dstPointInc = xDirection ? 1 : dstWidth;
int dstLineInc = xDirection ? dstWidth : 1;
int srcLine0 = xDirection ? roiY : roiX;
int dstLines = xDirection ? dstHeight : dstWidth;
DownsizeTable dt = xDirection ?
new DownsizeTable(getWidth(), roiX, roiWidth, dstWidth, interpolationMethod) :
new DownsizeTable(getHeight(), roiY, roiHeight, dstHeight, interpolationMethod);
FloatProcessor ip2 = (FloatProcessor)createProcessor(dstWidth, dstHeight);
float[] pixels = (float[])getPixels();
float[] pixels2 = (float[])ip2.getPixels();
for (int srcLine=srcLine0, dstLine=0; dstLine<dstLines; srcLine++,dstLine++) {
int dstLineOffset = dstLine * dstLineInc;
int tablePointer = 0;
for (int srcPoint=dt.srcStart, p=srcPoint*srcPointInc+srcLine*srcLineInc;
srcPoint<=dt.srcEnd; srcPoint++, p+=srcPointInc) {
float v = pixels[p];
for (int i=0; i<dt.kernelSize; i++, tablePointer++)
pixels2[dstLineOffset+dt.indices[tablePointer]*dstPointInc] += v * dt.weights[tablePointer];
}
}
return ip2;
}
/** This method is from Chapter 16 of "Digital Image Processing:
An Algorithmic Introduction Using Java" by Burger and Burge
(http://www.imagingbook.com/). */
public double getBicubicInterpolatedPixel(double x0, double y0, ImageProcessor ip2) {
int u0 = (int) Math.floor(x0); //use floor to handle negative coordinates too
int v0 = (int) Math.floor(y0);
if (u0<=0 || u0>=width-2 || v0<=0 || v0>=height-2)
return ip2.getBilinearInterpolatedPixel(x0, y0);
double q = 0;
for (int j = 0; j <= 3; j++) {
int v = v0 - 1 + j;
double p = 0;
for (int i = 0; i <= 3; i++) {
int u = u0 - 1 + i;
p = p + ip2.getf(u,v) * cubic(x0 - u);
}
q = q + p * cubic(y0 - v);
}
return q;
}
/** Sets the foreground fill/draw color. */
public void setColor(Color color) {
drawingColor = color;
int bestIndex = getBestIndex(color);
if (bestIndex>0 && getMin()==0.0 && getMax()==0.0) {
fillColor = bestIndex;
setMinAndMax(0.0,255.0);
} else if (bestIndex==0 && getMin()>0.0 && (color.getRGB()&0xffffff)==0)
fillColor = 0f;
else
fillColor = (float)(getMin() + (getMax()-getMin())*(bestIndex/255.0));
}
/** Sets the default fill/draw value. */
public void setValue(double value) {
fillColor = (float)value;
}
/** Does nothing. The rotate() and scale() methods always zero fill. */
public void setBackgroundValue(double value) {
}
/** Always returns 0. */
public double getBackgroundValue() {
return 0.0;
}
public void setThreshold(double minThreshold, double maxThreshold, int lutUpdate) {
if (minThreshold==NO_THRESHOLD)
{resetThreshold(); return;}
if (getMax()>getMin()) {
double minT = Math.round(((minThreshold-getMin())/(getMax()-getMin()))*255.0);
double maxT = Math.round(((maxThreshold-getMin())/(getMax()-getMin()))*255.0);
super.setThreshold(minT, maxT, lutUpdate); // update LUT
} else
super.resetThreshold();
this.minThreshold = minThreshold;
this.maxThreshold = maxThreshold;
}
/** Performs a convolution operation using the specified kernel. */
public void convolve(float[] kernel, int kernelWidth, int kernelHeight) {
snapshot();
new ij.plugin.filter.Convolver().convolve(this, kernel, kernelWidth, kernelHeight);
}
/** Not implemented. */
public void threshold(int level) {}
/** Not implemented. */
public void autoThreshold() {}
/** Not implemented. */
public void medianFilter() {}
/** Not implemented. */
public int[] getHistogram() {return null;}
/** Not implemented. */
public void erode() {}
/** Not implemented. */
public void dilate() {}
/** Returns this FloatProcessor.
* @param channelNumber Ignored (needed for compatibility with ColorProcessor.toFloat)
* @param fp Ignored (needed for compatibility with the other ImageProcessor types).
* @return This FloatProcessor
*/
public FloatProcessor toFloat(int channelNumber, FloatProcessor fp) {
return this;
}
/** Sets the pixels, and min&max values from a FloatProcessor.
* Also the values are taken from the FloatProcessor.
* @param channelNumber Ignored (needed for compatibility with ColorProcessor.toFloat)
* @param fp The FloatProcessor where the image data are read from.
*/
public void setPixels(int channelNumber, FloatProcessor fp) {
if (fp.getPixels() != getPixels())
setPixels(fp.getPixels());
setMinAndMax(fp.getMin(), fp.getMax());
}
/** Returns the smallest possible positive nonzero pixel value. */
public double minValue() {
return Float.MIN_VALUE;
}
/** Returns the largest possible positive finite pixel value. */
public double maxValue() {
return Float.MAX_VALUE;
}
}
|