1 7 8 17 18 package java.awt.image; 19 20 46 47 public class MultiPixelPackedSampleModel extends SampleModel 48 { 49 50 int pixelBitStride; 51 52 53 int bitMask; 54 55 59 int pixelsPerDataElement; 60 61 62 int dataElementSize; 63 64 66 int dataBitOffset; 67 68 69 int scanlineStride; 70 71 85 public MultiPixelPackedSampleModel(int dataType, 86 int w, 87 int h, 88 int numberOfBits) { 89 this(dataType,w,h, 90 numberOfBits, 91 (w*numberOfBits+DataBuffer.getDataTypeSize(dataType)-1)/ 92 DataBuffer.getDataTypeSize(dataType), 93 0); 94 if (dataType != DataBuffer.TYPE_BYTE && 95 dataType != DataBuffer.TYPE_USHORT && 96 dataType != DataBuffer.TYPE_INT) { 97 throw new IllegalArgumentException ("Unsupported data type "+ 98 dataType); 99 } 100 } 101 102 125 public MultiPixelPackedSampleModel(int dataType, int w, int h, 126 int numberOfBits, 127 int scanlineStride, 128 int dataBitOffset) { 129 super(dataType, w, h, 1); 130 if (dataType != DataBuffer.TYPE_BYTE && 131 dataType != DataBuffer.TYPE_USHORT && 132 dataType != DataBuffer.TYPE_INT) { 133 throw new IllegalArgumentException ("Unsupported data type "+ 134 dataType); 135 } 136 this.dataType = dataType; 137 this.pixelBitStride = numberOfBits; 138 this.scanlineStride = scanlineStride; 139 this.dataBitOffset = dataBitOffset; 140 this.dataElementSize = DataBuffer.getDataTypeSize(dataType); 141 this.pixelsPerDataElement = dataElementSize/numberOfBits; 142 if (pixelsPerDataElement*numberOfBits != dataElementSize) { 143 throw new RasterFormatException ("MultiPixelPackedSampleModel " + 144 "does not allow pixels to " + 145 "span data element boundaries"); 146 } 147 this.bitMask = (1 << numberOfBits) - 1; 148 } 149 150 151 165 public SampleModel createCompatibleSampleModel(int w, int h) { 166 SampleModel sampleModel = 167 new MultiPixelPackedSampleModel (dataType, w, h, pixelBitStride); 168 return sampleModel; 169 } 170 171 180 public DataBuffer createDataBuffer() { 181 DataBuffer dataBuffer = null; 182 183 int size = (int)scanlineStride*height; 184 switch (dataType) { 185 case DataBuffer.TYPE_BYTE: 186 dataBuffer = new DataBufferByte (size+(dataBitOffset+7)/8); 187 break; 188 case DataBuffer.TYPE_USHORT: 189 dataBuffer = new DataBufferUShort (size+(dataBitOffset+15)/16); 190 break; 191 case DataBuffer.TYPE_INT: 192 dataBuffer = new DataBufferInt (size+(dataBitOffset+31)/32); 193 break; 194 } 195 return dataBuffer; 196 } 197 198 205 public int getNumDataElements() { 206 return 1; 207 } 208 209 213 public int[] getSampleSize() { 214 int sampleSize[] = {pixelBitStride}; 215 return sampleSize; 216 } 217 218 223 public int getSampleSize(int band) { 224 return pixelBitStride; 225 } 226 227 232 public int getOffset(int x, int y) { 233 int offset = y * scanlineStride; 234 offset += (x*pixelBitStride+dataBitOffset)/dataElementSize; 235 return offset; 236 } 237 238 245 public int getBitOffset(int x){ 246 return (x*pixelBitStride+dataBitOffset)%dataElementSize; 247 } 248 249 254 public int getScanlineStride() { 255 return scanlineStride; 256 } 257 258 264 public int getPixelBitStride() { 265 return pixelBitStride; 266 } 267 268 273 public int getDataBitOffset() { 274 return dataBitOffset; 275 } 276 277 286 public int getTransferType() { 287 if (pixelBitStride > 16) 288 return DataBuffer.TYPE_INT; 289 else if (pixelBitStride > 8) 290 return DataBuffer.TYPE_USHORT; 291 else 292 return DataBuffer.TYPE_BYTE; 293 } 294 295 310 public SampleModel createSubsetSampleModel(int bands[]) { 311 if (bands != null) { 312 if (bands.length != 1) 313 throw new RasterFormatException ("MultiPixelPackedSampleModel has " 314 + "only one band."); 315 } 316 SampleModel sm = createCompatibleSampleModel(width, height); 317 return sm; 318 } 319 320 335 public int getSample(int x, int y, int b, DataBuffer data) { 336 if ((x < 0) || (y < 0) || (x >= width) || (y >= height) || 338 (b != 0)) { 339 throw new ArrayIndexOutOfBoundsException 340 ("Coordinate out of bounds!"); 341 } 342 int bitnum = dataBitOffset + x*pixelBitStride; 343 int element = data.getElem(y*scanlineStride + bitnum/dataElementSize); 344 int shift = dataElementSize - (bitnum & (dataElementSize-1)) 345 - pixelBitStride; 346 return (element >> shift) & bitMask; 347 } 348 349 363 public void setSample(int x, int y, int b, int s, 364 DataBuffer data) { 365 if ((x < 0) || (y < 0) || (x >= width) || (y >= height) || 367 (b != 0)) { 368 throw new ArrayIndexOutOfBoundsException 369 ("Coordinate out of bounds!"); 370 } 371 int bitnum = dataBitOffset + x * pixelBitStride; 372 int index = y * scanlineStride + (bitnum / dataElementSize); 373 int shift = dataElementSize - (bitnum & (dataElementSize-1)) 374 - pixelBitStride; 375 int element = data.getElem(index); 376 element &= ~(bitMask << shift); 377 element |= (s & bitMask) << shift; 378 data.setElem(index,element); 379 } 380 381 430 public Object getDataElements(int x, int y, Object obj, DataBuffer data) { 431 if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) { 432 throw new ArrayIndexOutOfBoundsException 433 ("Coordinate out of bounds!"); 434 } 435 436 int type = getTransferType(); 437 int bitnum = dataBitOffset + x*pixelBitStride; 438 int shift = dataElementSize - (bitnum & (dataElementSize-1)) 439 - pixelBitStride; 440 int element = 0; 441 442 switch(type) { 443 444 case DataBuffer.TYPE_BYTE: 445 446 byte[] bdata; 447 448 if (obj == null) 449 bdata = new byte[1]; 450 else 451 bdata = (byte[])obj; 452 453 element = data.getElem(y*scanlineStride + 454 bitnum/dataElementSize); 455 bdata[0] = (byte)((element >> shift) & bitMask); 456 457 obj = (Object )bdata; 458 break; 459 460 case DataBuffer.TYPE_USHORT: 461 462 short[] sdata; 463 464 if (obj == null) 465 sdata = new short[1]; 466 else 467 sdata = (short[])obj; 468 469 element = data.getElem(y*scanlineStride + 470 bitnum/dataElementSize); 471 sdata[0] = (short)((element >> shift) & bitMask); 472 473 obj = (Object )sdata; 474 break; 475 476 case DataBuffer.TYPE_INT: 477 478 int[] idata; 479 480 if (obj == null) 481 idata = new int[1]; 482 else 483 idata = (int[])obj; 484 485 element = data.getElem(y*scanlineStride + 486 bitnum/dataElementSize); 487 idata[0] = (element >> shift) & bitMask; 488 489 obj = (Object )idata; 490 break; 491 } 492 493 return obj; 494 } 495 496 510 public int[] getPixel(int x, int y, int iArray[], DataBuffer data) { 511 if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) { 512 throw new ArrayIndexOutOfBoundsException 513 ("Coordinate out of bounds!"); 514 } 515 int pixels[]; 516 if (iArray != null) { 517 pixels = iArray; 518 } else { 519 pixels = new int [numBands]; 520 } 521 int bitnum = dataBitOffset + x*pixelBitStride; 522 int element = data.getElem(y*scanlineStride + bitnum/dataElementSize); 523 int shift = dataElementSize - (bitnum & (dataElementSize-1)) 524 - pixelBitStride; 525 pixels[0] = (element >> shift) & bitMask; 526 return pixels; 527 } 528 529 568 public void setDataElements(int x, int y, Object obj, DataBuffer data) { 569 if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) { 570 throw new ArrayIndexOutOfBoundsException 571 ("Coordinate out of bounds!"); 572 } 573 574 int type = getTransferType(); 575 int bitnum = dataBitOffset + x * pixelBitStride; 576 int index = y * scanlineStride + (bitnum / dataElementSize); 577 int shift = dataElementSize - (bitnum & (dataElementSize-1)) 578 - pixelBitStride; 579 int element = data.getElem(index); 580 element &= ~(bitMask << shift); 581 582 switch(type) { 583 584 case DataBuffer.TYPE_BYTE: 585 586 byte[] barray = (byte[])obj; 587 element |= ( ((int)(barray[0])&0xff) & bitMask) << shift; 588 data.setElem(index, element); 589 break; 590 591 case DataBuffer.TYPE_USHORT: 592 593 short[] sarray = (short[])obj; 594 element |= ( ((int)(sarray[0])&0xffff) & bitMask) << shift; 595 data.setElem(index, element); 596 break; 597 598 case DataBuffer.TYPE_INT: 599 600 int[] iarray = (int[])obj; 601 element |= (iarray[0] & bitMask) << shift; 602 data.setElem(index, element); 603 break; 604 } 605 } 606 607 617 public void setPixel(int x, int y, int[] iArray, DataBuffer data) { 618 if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) { 619 throw new ArrayIndexOutOfBoundsException 620 ("Coordinate out of bounds!"); 621 } 622 int bitnum = dataBitOffset + x * pixelBitStride; 623 int index = y * scanlineStride + (bitnum / dataElementSize); 624 int shift = dataElementSize - (bitnum & (dataElementSize-1)) 625 - pixelBitStride; 626 int element = data.getElem(index); 627 element &= ~(bitMask << shift); 628 element |= (iArray[0] & bitMask) << shift; 629 data.setElem(index,element); 630 } 631 632 public boolean equals(Object o) { 633 if ((o == null) || !(o instanceof MultiPixelPackedSampleModel )) { 634 return false; 635 } 636 637 MultiPixelPackedSampleModel that = (MultiPixelPackedSampleModel )o; 638 return this.width == that.width && 639 this.height == that.height && 640 this.numBands == that.numBands && 641 this.dataType == that.dataType && 642 this.pixelBitStride == that.pixelBitStride && 643 this.bitMask == that.bitMask && 644 this.pixelsPerDataElement == that.pixelsPerDataElement && 645 this.dataElementSize == that.dataElementSize && 646 this.dataBitOffset == that.dataBitOffset && 647 this.scanlineStride == that.scanlineStride; 648 } 649 650 public int hashCode() { 652 int hash = 0; 653 hash = width; 654 hash <<= 8; 655 hash ^= height; 656 hash <<= 8; 657 hash ^= numBands; 658 hash <<= 8; 659 hash ^= dataType; 660 hash <<= 8; 661 hash ^= pixelBitStride; 662 hash <<= 8; 663 hash ^= bitMask; 664 hash <<= 8; 665 hash ^= pixelsPerDataElement; 666 hash <<= 8; 667 hash ^= dataElementSize; 668 hash <<= 8; 669 hash ^= dataBitOffset; 670 hash <<= 8; 671 hash ^= scanlineStride; 672 return hash; 673 } 674 } 675 | Popular Tags |