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