1. /*
  2. * @(#)PixelInterleavedSampleModel.java 1.12 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.awt.image;
  8. /**
  9. * This class represents image data which is stored in a pixel interleaved
  10. * fashion and for
  11. * which each sample of a pixel occupies one data element of the DataBuffer.
  12. * It subclasses ComponentSampleModel but provides a more efficent
  13. * implementation for accessing pixel interleaved image data than is provided
  14. * by ComponentSampleModel. This class
  15. * stores sample data for all bands in a single bank of the
  16. * DataBuffer. Accessor methods are provided so that image data can be
  17. * manipulated directly. Pixel stride is the number of
  18. * data array elements between two samples for the same band on the same
  19. * scanline. Scanline stride is the number of data array elements between
  20. * a given sample and the corresponding sample in the same column of the next
  21. * scanline. Band offsets denote the number
  22. * of data array elements from the first data array element of the bank
  23. * of the DataBuffer holding each band to the first sample of the band.
  24. * The bands are numbered from 0 to N-1.
  25. * Bank indices denote the correspondence between a bank of the data buffer
  26. * and a band of image data.
  27. */
  28. public class PixelInterleavedSampleModel extends ComponentSampleModel
  29. {
  30. /**
  31. * Constructs a PixelInterleavedSampleModel with the specified parameters.
  32. * The number of bands will be given by the length of the bandOffsets
  33. * array.
  34. * @param dataType The data type for storing samples.
  35. * @param w The width (in pixels) of the region of
  36. * image data described.
  37. * @param h The height (in pixels) of the region of
  38. * image data described.
  39. * @param pixelStride The pixel stride of the image data.
  40. * @param scanlineStride The line stride of the image data.
  41. * @param bandOffsets The offsets of all bands.
  42. * @throws IllegalArgumentException if any offset between bands is
  43. * greater than the scanline stride
  44. * @throws IllegalArgumentException if the product of
  45. * <code>pixelStride</code> and <code>w</code> is greater
  46. * than <code>scanlineStride</code>
  47. * @throws IllegalArgumentException if <code>pixelStride</code> is
  48. * less than any offset between bands
  49. */
  50. public PixelInterleavedSampleModel(int dataType,
  51. int w, int h,
  52. int pixelStride,
  53. int scanlineStride,
  54. int bandOffsets[]) {
  55. super(dataType, w, h, pixelStride, scanlineStride, bandOffsets);
  56. int minBandOff=bandOffsets[0];
  57. int maxBandOff=bandOffsets[0];
  58. for (int i=1; i<bandOffsets.length; i++) {
  59. minBandOff = Math.min(minBandOff,bandOffsets[i]);
  60. maxBandOff = Math.max(maxBandOff,bandOffsets[i]);
  61. }
  62. maxBandOff -= minBandOff;
  63. if (maxBandOff > scanlineStride) {
  64. throw new IllegalArgumentException("Offsets between bands must be"+
  65. " less than the scanline "+
  66. " stride");
  67. }
  68. if (pixelStride*w > scanlineStride) {
  69. throw new IllegalArgumentException("Pixel stride times width "+
  70. "must be less "+
  71. "than the scanline stride");
  72. }
  73. if (pixelStride < maxBandOff) {
  74. throw new IllegalArgumentException("Pixel stride must be greater"+
  75. " than or equal to the offsets"+
  76. " between bands");
  77. }
  78. }
  79. /**
  80. * Creates a new PixelInterleavedSampleModel with the specified
  81. * width and height. The new PixelInterleavedSampleModel will have the
  82. * same number of bands, storage data type, and pixel stride
  83. * as this PixelInterleavedSampleModel. The band offsets may be
  84. * compressed such that the minimum of all of the band offsets is zero.
  85. */
  86. public SampleModel createCompatibleSampleModel(int w, int h) {
  87. int minBandoff=bandOffsets[0];
  88. int numBands = bandOffsets.length;
  89. for (int i=1; i < numBands; i++) {
  90. if (bandOffsets[i] < minBandoff) {
  91. minBandoff = bandOffsets[i];
  92. }
  93. }
  94. int[] bandOff;
  95. if (minBandoff > 0) {
  96. bandOff = new int[numBands];
  97. for (int i=0; i < numBands; i++) {
  98. bandOff[i] = bandOffsets[i] - minBandoff;
  99. }
  100. }
  101. else {
  102. bandOff = bandOffsets;
  103. }
  104. return new PixelInterleavedSampleModel(dataType, w, h, pixelStride,
  105. pixelStride*w, bandOff);
  106. }
  107. /**
  108. * Creates a new PixelInterleavedSampleModel with a subset of the
  109. * bands of this PixelInterleavedSampleModel. The new
  110. * PixelInterleavedSampleModel can be used with any DataBuffer that the
  111. * existing PixelInterleavedSampleModel can be used with. The new
  112. * PixelInterleavedSampleModel/DataBuffer combination will represent
  113. * an image with a subset of the bands of the original
  114. * PixelInterleavedSampleModel/DataBuffer combination.
  115. */
  116. public SampleModel createSubsetSampleModel(int bands[]) {
  117. int newBandOffsets[] = new int[bands.length];
  118. for (int i=0; i<bands.length; i++) {
  119. newBandOffsets[i] = bandOffsets[bands[i]];
  120. }
  121. return new PixelInterleavedSampleModel(this.dataType, width, height,
  122. this.pixelStride,
  123. scanlineStride, newBandOffsets);
  124. }
  125. }