1 7 8 17 18 package java.awt.image; 19 20 62 63 public abstract class SampleModel 64 { 65 66 69 protected int width; 70 71 74 protected int height; 75 76 77 protected int numBands; 78 79 82 protected int dataType; 83 84 static private native void initIDs(); 85 static { 86 ColorModel.loadLibraries(); 87 initIDs(); 88 } 89 90 104 public SampleModel(int dataType, int w, int h, int numBands) 105 { 106 float size = (float)w*h; 107 if (w <= 0 || h <= 0) { 108 throw new IllegalArgumentException ("Width ("+w+") and height ("+ 109 h+") must be > 0"); 110 } 111 if (size >= Integer.MAX_VALUE) { 112 throw new IllegalArgumentException ("Dimensions (width="+w+ 113 " height="+h+") are too large"); 114 } 115 116 if (dataType < DataBuffer.TYPE_BYTE || 117 (dataType > DataBuffer.TYPE_DOUBLE && 118 dataType != DataBuffer.TYPE_UNDEFINED)) 119 { 120 throw new IllegalArgumentException ("Unsupported dataType: "+ 121 dataType); 122 } 123 124 if (numBands <= 0) { 125 throw new IllegalArgumentException ("Number of bands must be > 0"); 126 } 127 128 this.dataType = dataType; 129 this.width = w; 130 this.height = h; 131 this.numBands = numBands; 132 } 133 134 138 final public int getWidth() { 139 return width; 140 } 141 142 146 final public int getHeight() { 147 return height; 148 } 149 150 154 final public int getNumBands() { 155 return numBands; 156 } 157 158 173 public abstract int getNumDataElements(); 174 175 178 final public int getDataType() { 179 return dataType; 180 } 181 182 199 public int getTransferType() { 200 return dataType; 201 } 202 203 218 public int[] getPixel(int x, int y, int iArray[], DataBuffer data) { 219 220 int pixels[]; 221 222 if (iArray != null) 223 pixels = iArray; 224 else 225 pixels = new int[numBands]; 226 227 for (int i=0; i<numBands; i++) { 228 pixels[i] = getSample(x, y, i, data); 229 } 230 231 return pixels; 232 } 233 234 280 public abstract Object getDataElements(int x, int y, 281 Object obj, DataBuffer data); 282 283 334 public Object getDataElements(int x, int y, int w, int h, 335 Object obj, DataBuffer data) { 336 337 int type = getTransferType(); 338 int numDataElems = getNumDataElements(); 339 int cnt = 0; 340 Object o = null; 341 342 switch(type) { 343 344 case DataBuffer.TYPE_BYTE: 345 346 byte[] btemp; 347 byte[] bdata; 348 349 if (obj == null) 350 bdata = new byte[numDataElems*w*h]; 351 else 352 bdata = (byte[])obj; 353 354 for (int i=y; i<y+h; i++) { 355 for (int j=x; j<x+w; j++) { 356 o = getDataElements(j, i, o, data); 357 btemp = (byte[])o; 358 for (int k=0; k<numDataElems; k++) { 359 bdata[cnt++] = btemp[k]; 360 } 361 } 362 } 363 obj = (Object )bdata; 364 break; 365 366 case DataBuffer.TYPE_USHORT: 367 case DataBuffer.TYPE_SHORT: 368 369 short[] sdata; 370 short[] stemp; 371 372 if (obj == null) 373 sdata = new short[numDataElems*w*h]; 374 else 375 sdata = (short[])obj; 376 377 for (int i=y; i<y+h; i++) { 378 for (int j=x; j<x+w; j++) { 379 o = getDataElements(j, i, o, data); 380 stemp = (short[])o; 381 for (int k=0; k<numDataElems; k++) { 382 sdata[cnt++] = stemp[k]; 383 } 384 } 385 } 386 387 obj = (Object )sdata; 388 break; 389 390 case DataBuffer.TYPE_INT: 391 392 int[] idata; 393 int[] itemp; 394 395 if (obj == null) 396 idata = new int[numDataElems*w*h]; 397 else 398 idata = (int[])obj; 399 400 for (int i=y; i<y+h; i++) { 401 for (int j=x; j<x+w; j++) { 402 o = getDataElements(j, i, o, data); 403 itemp = (int[])o; 404 for (int k=0; k<numDataElems; k++) { 405 idata[cnt++] = itemp[k]; 406 } 407 } 408 } 409 410 obj = (Object )idata; 411 break; 412 413 case DataBuffer.TYPE_FLOAT: 414 415 float[] fdata; 416 float[] ftemp; 417 418 if (obj == null) 419 fdata = new float[numDataElems*w*h]; 420 else 421 fdata = (float[])obj; 422 423 for (int i=y; i<y+h; i++) { 424 for (int j=x; j<x+w; j++) { 425 o = getDataElements(j, i, o, data); 426 ftemp = (float[])o; 427 for (int k=0; k<numDataElems; k++) { 428 fdata[cnt++] = ftemp[k]; 429 } 430 } 431 } 432 433 obj = (Object )fdata; 434 break; 435 436 case DataBuffer.TYPE_DOUBLE: 437 438 double[] ddata; 439 double[] dtemp; 440 441 if (obj == null) 442 ddata = new double[numDataElems*w*h]; 443 else 444 ddata = (double[])obj; 445 446 for (int i=y; i<y+h; i++) { 447 for (int j=x; j<x+w; j++) { 448 o = getDataElements(j, i, o, data); 449 dtemp = (double[])o; 450 for (int k=0; k<numDataElems; k++) { 451 ddata[cnt++] = dtemp[k]; 452 } 453 } 454 } 455 456 obj = (Object )ddata; 457 break; 458 } 459 460 return obj; 461 } 462 463 506 public abstract void setDataElements(int x, int y, 507 Object obj, DataBuffer data); 508 509 555 public void setDataElements(int x, int y, int w, int h, 556 Object obj, DataBuffer data) { 557 558 int cnt = 0; 559 Object o = null; 560 int type = getTransferType(); 561 int numDataElems = getNumDataElements(); 562 563 switch(type) { 564 565 case DataBuffer.TYPE_BYTE: 566 567 byte[] barray = (byte[])obj; 568 byte[] btemp = new byte[numDataElems]; 569 570 for (int i=y; i<y+h; i++) { 571 for (int j=x; j<x+w; j++) { 572 for (int k=0; k<numDataElems; k++) { 573 btemp[k] = barray[cnt++]; 574 } 575 576 setDataElements(j, i, btemp, data); 577 } 578 } 579 break; 580 581 case DataBuffer.TYPE_USHORT: 582 case DataBuffer.TYPE_SHORT: 583 584 short[] sarray = (short[])obj; 585 short[] stemp = new short[numDataElems]; 586 587 for (int i=y; i<y+h; i++) { 588 for (int j=x; j<x+w; j++) { 589 for (int k=0; k<numDataElems; k++) { 590 stemp[k] = sarray[cnt++]; 591 } 592 593 setDataElements(j, i, stemp, data); 594 } 595 } 596 break; 597 598 case DataBuffer.TYPE_INT: 599 600 int[] iArray = (int[])obj; 601 int[] itemp = new int[numDataElems]; 602 603 for (int i=y; i<y+h; i++) { 604 for (int j=x; j<x+w; j++) { 605 for (int k=0; k<numDataElems; k++) { 606 itemp[k] = iArray[cnt++]; 607 } 608 609 setDataElements(j, i, itemp, data); 610 } 611 } 612 break; 613 614 case DataBuffer.TYPE_FLOAT: 615 616 float[] fArray = (float[])obj; 617 float[] ftemp = new float[numDataElems]; 618 619 for (int i=y; i<y+h; i++) { 620 for (int j=x; j<x+w; j++) { 621 for (int k=0; k<numDataElems; k++) { 622 ftemp[k] = fArray[cnt++]; 623 } 624 625 setDataElements(j, i, ftemp, data); 626 } 627 } 628 break; 629 630 case DataBuffer.TYPE_DOUBLE: 631 632 double[] dArray = (double[])obj; 633 double[] dtemp = new double[numDataElems]; 634 635 for (int i=y; i<y+h; i++) { 636 for (int j=x; j<x+w; j++) { 637 for (int k=0; k<numDataElems; k++) { 638 dtemp[k] = dArray[cnt++]; 639 } 640 641 setDataElements(j, i, dtemp, data); 642 } 643 } 644 break; 645 } 646 647 } 648 649 664 public float[] getPixel(int x, int y, float fArray[], 665 DataBuffer data) { 666 667 float pixels[]; 668 669 if (fArray != null) 670 pixels = fArray; 671 else 672 pixels = new float[numBands]; 673 674 for (int i=0; i<numBands; i++) 675 pixels[i] = getSampleFloat(x, y, i, data); 676 677 return pixels; 678 } 679 680 695 public double[] getPixel(int x, int y, double dArray[], 696 DataBuffer data) { 697 698 double pixels[]; 699 700 if(dArray != null) 701 pixels = dArray; 702 else 703 pixels = new double[numBands]; 704 705 for (int i=0; i<numBands; i++) 706 pixels[i] = getSampleDouble(x, y, i, data); 707 708 return pixels; 709 } 710 711 729 public int[] getPixels(int x, int y, int w, int h, 730 int iArray[], DataBuffer data) { 731 732 int pixels[]; 733 int Offset=0; 734 735 if (iArray != null) 736 pixels = iArray; 737 else 738 pixels = new int[numBands * w * h]; 739 740 for (int i=y; i<(h+y); i++) { 741 for (int j=x; j<(w+x); j++) { 742 for(int k=0; k<numBands; k++) { 743 pixels[Offset++] = getSample(j, i, k, data); 744 } 745 } 746 } 747 748 return pixels; 749 } 750 751 769 public float[] getPixels(int x, int y, int w, int h, 770 float fArray[], DataBuffer data) { 771 772 float pixels[]; 773 int Offset = 0; 774 775 if (fArray != null) 776 pixels = fArray; 777 else 778 pixels = new float[numBands * w * h]; 779 780 for (int i=y; i<(h+y); i++) { 781 for(int j=x; j<(w+x); j++) { 782 for(int k=0; k<numBands; k++) { 783 pixels[Offset++] = getSampleFloat(j, i, k, data); 784 } 785 } 786 } 787 788 return pixels; 789 } 790 791 809 public double[] getPixels(int x, int y, int w, int h, 810 double dArray[], DataBuffer data) { 811 double pixels[]; 812 int Offset = 0; 813 814 if (dArray != null) 815 pixels = dArray; 816 else 817 pixels = new double[numBands * w * h]; 818 819 for (int i=y; i<(h+y); i++) { 821 for (int j=x; j<(w+x); j++) { 822 for (int k=0; k<numBands; k++) { 823 pixels[Offset++] = getSampleDouble(j, i, k, data); 824 } 825 } 826 } 827 828 return pixels; 829 } 830 831 832 848 public abstract int getSample(int x, int y, int b, DataBuffer data); 849 850 851 866 public float getSampleFloat(int x, int y, int b, DataBuffer data) { 867 868 float sample; 869 sample = (float) getSample(x, y, b, data); 870 return sample; 871 } 872 873 888 public double getSampleDouble(int x, int y, int b, DataBuffer data) { 889 890 double sample; 891 892 sample = (double) getSample(x, y, b, data); 893 return sample; 894 } 895 896 917 public int[] getSamples(int x, int y, int w, int h, int b, 918 int iArray[], DataBuffer data) { 919 int pixels[]; 920 int Offset=0; 921 922 if (iArray != null) 923 pixels = iArray; 924 else 925 pixels = new int[w * h]; 926 927 for(int i=y; i<(h+y); i++) { 928 for (int j=x; j<(w+x); j++) { 929 pixels[Offset++] = getSample(j, i, b, data); 930 } 931 } 932 933 return pixels; 934 } 935 936 957 public float[] getSamples(int x, int y, int w, int h, 958 int b, float fArray[], 959 DataBuffer data) { 960 float pixels[]; 961 int Offset=0; 962 963 if (fArray != null) 964 pixels = fArray; 965 else 966 pixels = new float[w * h]; 967 968 for (int i=y; i<(h+y); i++) { 969 for (int j=x; j<(w+x); j++) { 970 pixels[Offset++] = getSampleFloat(j, i, b, data); 971 } 972 } 973 974 return pixels; 975 } 976 977 998 public double[] getSamples(int x, int y, int w, int h, 999 int b, double dArray[], 1000 DataBuffer data) { 1001 double pixels[]; 1002 int Offset=0; 1003 1004 if (dArray != null) 1005 pixels = dArray; 1006 else 1007 pixels = new double[w * h]; 1008 1009 for (int i=y; i<(y+h); i++) { 1010 for (int j=x; j<(x+w); j++) { 1011 pixels[Offset++] = getSampleDouble(j, i, b, data); 1012 } 1013 } 1014 1015 return pixels; 1016 } 1017 1018 1032 public void setPixel(int x, int y, int iArray[], DataBuffer data) { 1033 1034 for (int i=0; i<numBands; i++) 1035 setSample(x, y, i, iArray[i], data); 1036 } 1037 1038 1052 public void setPixel(int x, int y, float fArray[], DataBuffer data) { 1053 1054 for (int i=0; i<numBands; i++) 1055 setSample(x, y, i, fArray[i], data); 1056 } 1057 1058 1071 public void setPixel(int x, int y, double dArray[], DataBuffer data) { 1072 1073 for (int i=0; i<numBands; i++) 1074 setSample(x, y, i, dArray[i], data); 1075 } 1076 1077 1094 public void setPixels(int x, int y, int w, int h, 1095 int iArray[], DataBuffer data) { 1096 int Offset=0; 1097 1098 for (int i=y; i<(y+h); i++) { 1099 for (int j=x; j<(x+w); j++) { 1100 for (int k=0; k<numBands; k++) { 1101 setSample(j, i, k, iArray[Offset++], data); 1102 } 1103 } 1104 } 1105 } 1106 1107 1124 public void setPixels(int x, int y, int w, int h, 1125 float fArray[], DataBuffer data) { 1126 int Offset=0; 1127 1128 for (int i=y; i<(y+h); i++) { 1129 for (int j=x; j<(x+w); j++) { 1130 for(int k=0; k<numBands; k++) { 1131 setSample(j, i, k, fArray[Offset++], data); 1132 } 1133 } 1134 } 1135 } 1136 1137 1154 public void setPixels(int x, int y, int w, int h, 1155 double dArray[], DataBuffer data) { 1156 int Offset=0; 1157 1158 for (int i=y; i<(y+h); i++) { 1159 for (int j=x; j<(x+w); j++) { 1160 for (int k=0; k<numBands; k++) { 1161 setSample(j, i, k, dArray[Offset++], data); 1162 } 1163 } 1164 } 1165 } 1166 1167 1183 public abstract void setSample(int x, int y, int b, 1184 int s, 1185 DataBuffer data); 1186 1187 1203 public void setSample(int x, int y, int b, 1204 float s , 1205 DataBuffer data) { 1206 int sample = (int)s; 1207 1208 setSample(x, y, b, sample, data); 1209 } 1210 1211 1227 public void setSample(int x, int y, int b, 1228 double s, 1229 DataBuffer data) { 1230 int sample = (int)s; 1231 1232 setSample(x, y, b, sample, data); 1233 } 1234 1235 1254 public void setSamples(int x, int y, int w, int h, int b, 1255 int iArray[], DataBuffer data) { 1256 1257 int Offset=0; 1258 1259 for (int i=y; i<(y+h); i++) { 1260 for (int j=x; j<(x+w); j++) { 1261 setSample(j, i, b, iArray[Offset++], data); 1262 } 1263 } 1264 } 1265 1266 1285 public void setSamples(int x, int y, int w, int h, int b, 1286 float fArray[], DataBuffer data) { 1287 int Offset=0; 1288 1289 for (int i=y; i<(y+h); i++) { 1290 for (int j=x; j<(x+w); j++) { 1291 setSample(j, i, b, fArray[Offset++], data); 1292 } 1293 } 1294 } 1295 1296 1315 public void setSamples(int x, int y, int w, int h, int b, 1316 double dArray[], DataBuffer data) { 1317 int Offset=0; 1318 1319 for (int i=y; i<(y+h); i++) { 1320 for (int j=x; j<(x+w); j++) { 1321 setSample(j, i, b, dArray[Offset++], data); 1322 } 1323 } 1324 } 1325 1326 1335 public abstract SampleModel createCompatibleSampleModel(int w, int h); 1336 1337 1345 public abstract SampleModel createSubsetSampleModel(int bands[]); 1346 1347 1353 public abstract DataBuffer createDataBuffer(); 1354 1355 1358 public abstract int[] getSampleSize(); 1359 1360 1364 public abstract int getSampleSize(int band); 1365 1366} 1367 | Popular Tags |