# Enum Class DataType

Object
Enum<DataType>
DataType

All Implemented Interfaces:
  `Serializable`, `Comparable<DataType>`, `Constable`

public enum DataType extends Enum<DataType>
Identification of the primitive type used for storing sample values in an image. This is a type-safe version of the `TYPE_*` constants defined in `DataBuffer`, except that this enumeration distinguishes signed and unsigned 32 bits integers.

Since:
  1.1

- ## Nested Class Summary

  ### Nested classes/interfaces inherited from class Enum

  `Enum​.EnumDesc<E extends Enum<E>>`

- ## Enum Constant Summary

  Enum Constants
  Enum Constant
  Description
  `BYTE`
  Unsigned 8-bits data.
  `DOUBLE`
  Double precision (64-bits) floating point data.
  `FLOAT`
  Single precision (32-bits) floating point data.
  `INT`
  Signed 32-bits data.
  `SHORT`
  Signed 16-bits data.
  `UINT`
  Unsigned 32-bits data.
  `USHORT`
  Unsigned 16-bits data.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `final int`
  `bytes()`
  Returns the size in bytes of this data type.
  `final Number`
  `fillValue()`
  Returns the default fill value, which is 0 for integer types and NaN for floating point types.
  `static DataType`
  `forBands(RenderedImage image)`
  Returns the data type of the bands in the given image.
  `static DataType`
  `forBands(SampleModel sm)`
  Returns the data type of the bands managed by the given the sample model.
  `static DataType`
  `forDataBufferType(int type)`
  Returns the enumeration value for the given `DataBuffer` constant.
  `static DataType`
  `forNumberOfBits(int size, boolean real, boolean signed)`
  Returns the enumeration value for the given number of bits.
  `static DataType`
  `forPrimitiveType(Class<?> type, boolean unsigned)`
  Returns the data type for the given primitive type.
  `static DataType`
  `forRange(NumberRange<?> range, boolean forceInteger)`
  Returns the smallest data type capable to store the given range of values.
  `final boolean`
  `isInteger()`
  Returns whether this type is an integer type, signed or not.
  `static boolean`
  `isInteger(SampleModel sm)`
  Returns `true` if the given sample model uses an integer type.
  `final boolean`
  `isUnsigned()`
  Returns whether this type is an unsigned integer type.
  `static boolean`
  `isUnsigned(SampleModel sm)`
  Returns `true` if the type of sample values is an unsigned integer type.
  `final int`
  `size()`
  Returns the size in bits of this data type.
  `final int`
  `toDataBufferType()`
  Returns the `DataBuffer` constant for this enumeration value.
  `final DataType`
  `toFloat()`
  Returns the smallest floating point type capable to store all values of this type without precision lost.
  `final DataType`
  `toPrimitive()`
  Returns the primitive (signed) variant of this data type.
  `static DataType`
  `valueOf(String name)`
  Returns the enum constant of this class with the specified name.
  `static DataType[]`
  `values()`
  Returns an array containing the constants of this enum class, in the order they are declared.

  ### Methods inherited from class Enum

  `clone, compareTo, describeConstable, equals, finalize, getDeclaringClass, hashCode, name, ordinal, toString, valueOf`

  ### Methods inherited from class Object

  `getClass, notify, notifyAll, wait, wait, wait`

- ## Enum Constant Details

  - ### BYTE

    public static final DataType BYTE
    Unsigned 8-bits data. Mapped to `DataBuffer​.TYPE_BYTE` in Java2D API.

  - ### USHORT

    public static final DataType USHORT
    Unsigned 16-bits data. Mapped to `DataBuffer​.TYPE_USHORT` in Java2D API.

  - ### SHORT

    public static final DataType SHORT
    Signed 16-bits data. Mapped to `DataBuffer​.TYPE_SHORT` in Java2D API.

  - ### INT

    public static final DataType INT
    Signed 32-bits data. Mapped to `DataBuffer​.TYPE_INT` in Java2D API. Note that the sign of the latter Java2D type is ambiguous. See `forDataBufferType(int)` for more information.

    This type is selected by default for all `DataBuffer​.TYPE_INT` cases that cannot be resolved as an `UINT` case.

  - ### UINT

    public static final DataType UINT
    Unsigned 32-bits data. Mapped to `DataBuffer​.TYPE_INT` in Java2D API. Note that the sign of the latter Java2D type is ambiguous. See `forDataBufferType(int)` for more information.

    This case is selected when the data are used with any packed sample model: `SinglePixelPackedSampleModel` or `MultiPixelPackedSampleModel`.

    Since:
      1.5

  - ### FLOAT

    public static final DataType FLOAT
    Single precision (32-bits) floating point data. Mapped to `DataBuffer​.TYPE_FLOAT` in Java2D API.

  - ### DOUBLE

    public static final DataType DOUBLE
    Double precision (64-bits) floating point data. Mapped to `DataBuffer​.TYPE_DOUBLE` in Java2D API.

- ## Method Details

  - ### values

    public static DataType[] values()
    Returns an array containing the constants of this enum class, in the order they are declared.
    Returns:
      an array containing the constants of this enum class, in the order they are declared

  - ### valueOf

    public static DataType valueOf(String name)
    Returns the enum constant of this class with the specified name. The string must match exactly an identifier used to declare an enum constant in this class. (Extraneous whitespace characters are not permitted.)
    Parameters:
      `name` - the name of the enum constant to be returned.
    Returns:
      the enum constant with the specified name
    Throws:
      `IllegalArgumentException` - if this enum class has no constant with the specified name
      `NullPointerException` - if the argument is null

  - ### forBands

    public static DataType forBands(RenderedImage image)
    Returns the data type of the bands in the given image. This is often the storage data type, but not necessarily. See `forBands(SampleModel)` for more information.
    Parameters:
      `image` - the image for which to get the band data type.
    Returns:
      type of sample values in the given image (never `null`).
    Throws:
      `RasterFormatException` - if the image does not use a recognized data type.

  - ### forBands

    public static DataType forBands(SampleModel sm)
    Returns the data type of the bands managed by the given the sample model. This is often the storage data type, but not necessarily. For example, if an ARGB image uses a storage mode where the sample values in the four bands are packed in a single 32-bits integer, then this method returns `BYTE` (the type of a single band) rather than `INT` (the type of a whole pixel).
    Parameters:
      `sm` - the sample model for which to get the band data type.
    Returns:
      type of sample values in the bands managed by the given sample model (never `null`).
    Throws:
      `RasterFormatException` - if the sample model does not use a recognized data type.
    Since:
      1.5

  - ### forRange

    public static DataType forRange(NumberRange<?> range, boolean forceInteger)
    Returns the smallest data type capable to store the given range of values. If the given range uses a floating point type, there there is a choice:
    - If `asInteger` is `false`, then this method returns `FLOAT` or `DOUBLE` depending on the range type.
    - Otherwise, this method treats the floating point values as if they were integers, with minimum value rounded toward negative infinity and maximum value rounded toward positive infinity.
    Parameters:
      `range` - the range of values.
      `forceInteger` - whether to handle floating point values as integers.
    Returns:
      smallest data type for the given range of values.

  - ### forPrimitiveType

    public static DataType forPrimitiveType(Class<?> type, boolean unsigned)
    Returns the data type for the given primitive type. The given `type` should be a primitive type such as `Short​.TYPE`, but wrappers class such as `Short​.class` are also accepted.
    Parameters:
      `type` - the primitive type or its wrapper class.
      `unsigned` - whether the type should be considered unsigned.
    Returns:
      the data type (never `null`) for the given primitive type.
    Throws:
      `RasterFormatException` - if the given type is not a recognized.

  - ### forDataBufferType

    public static DataType forDataBufferType(int type)
    Returns the enumeration value for the given `DataBuffer` constant. This method is the converse of `toDataBufferType()`. It is provided for interoperability with Java2D.

    #### 32 bit integers

    The case of `DataBuffer​.TYPE_INT` is ambiguous. Whether this type is considered as signed or unsigned depends on the context:
    - The sign is indistinguishable when using Java2D API working on integer values such as the `Raster​.getSample(…)` method.
    - When converted to floating point values by Java2D API such as `Raster​.getSampleFloat(…)`, sample values are:
      - Unsigned when the raster uses `SinglePixelPackedSampleModel` or `MultiPixelPackedSampleModel`.
      - Signed when the raster uses any other type of sample model.
    - The type is unsigned when converted to a color by `ComponentColorModel`.
    For resolving above ambiguities, consider using `forBands(SampleModel)` instead of this method.
    Parameters:
      `type` - one of `DataBuffer​.TYPE_*` constants.
    Returns:
      the data type (never `null`) for the given data buffer type.
    Throws:
      `RasterFormatException` - if the given type is not a recognized `DataBuffer​.TYPE_*` constant.

  - ### forNumberOfBits

    public static DataType forNumberOfBits(int size, boolean real, boolean signed)
    Returns the enumeration value for the given number of bits. Only the following combinations of argument values are accepted:

    Valid combinations of argument values
    size | real | signed
    --- | --- | ---
    1 | `false` | `false`
    2 | `false` | `false`
    4 | `false` | `false`
    8 | `false` | `false`
    16 | `false` | `false` or `true`
    32 | `false` | `false` or `true`
    32 | `true` | ignored
    64 | `true` | ignored

    Parameters:
      `size` - number of bits as a power of 2 between 1 and 64 inclusive.
      `real` - `true` for floating point numbers or `false` for integers.
      `signed` - `true` for signed numbers of `false` for unsigned numbers.
    Returns:
      the data type (never `null`) for the given number of bits.
    Throws:
      `RasterFormatException` - if the combination of argument values is invalid.
    Since:
      1.2

  - ### size

    public final int size()
    Returns the size in bits of this data type.
    Returns:
      size in bits of this data type.

  - ### bytes

    public final int bytes()
    Returns the size in bytes of this data type.
    Returns:
      size in bytes of this data type, from 1 to 4 inclusive.
    Since:
      1.3

  - ### isInteger

    public final boolean isInteger()
    Returns whether this type is an integer type, signed or not. Integer types are `BYTE`, `USHORT`, `SHORT`, `INT` and `UINT`.
    Returns:
      `true` if this type is an integer type.

  - ### isInteger

    public static boolean isInteger(SampleModel sm)
    Returns `true` if the given sample model uses an integer type. Returns `false` if the type is a floating point type or in case of doubt (e.g. for `DataBuffer​.TYPE_UNDEFINED`).
    Parameters:
      `sm` - the sample model, or `null`.
    Returns:
      whether the given sample model uses an integer type.
    Since:
      1.5

  - ### isUnsigned

    public final boolean isUnsigned()
    Returns whether this type is an unsigned integer type. Unsigned types are `BYTE`, `USHORT` and `UINT`.
    Returns:
      `true` if this type is an unsigned integer type.

  - ### isUnsigned

    public static boolean isUnsigned(SampleModel sm)
    Returns `true` if the type of sample values is an unsigned integer type. Returns `false` if the type is a floating point type or in case of doubt (e.g. for `DataBuffer​.TYPE_UNDEFINED`).
    Parameters:
      `sm` - the sample model, or `null`.
    Returns:
      whether the given sample model provides unsigned sample values.
    Since:
      1.5

  - ### toPrimitive

    public final DataType toPrimitive()
    Returns the primitive (signed) variant of this data type. This method returns the value that most closely maps to a Java primitive type of the same number of bits. Since all Java primitive types are signed, this method returns the signed variant of this type except for the special case of `BYTE` (because `DataType` does not define signed variant of that type).

    More specifically, this methods replaces `UINT` by `INT`, replaces `USHORT` by `SHORT`, and returns all other types unchanged. The purpose of this method is to simplify the `switch` statements with cases restricted to Java primitive types, such as mapping to specific NIO `Buffer` subclasses.

    Returns:
      the data type that most closely corresponds to a Java primitive type.
    Since:
      1.5

  - ### toFloat

    public final DataType toFloat()
    Returns the smallest floating point type capable to store all values of this type without precision lost. This method returns:
    - `DOUBLE` if this data type is `DOUBLE`, `INT` or `UINT`.
    - `FLOAT` for all other types.
    The promotion of integer values to floating point values is sometimes necessary when the image may contain `Float​.NaN` values.
    Returns:
      the smallest of `FLOAT` or `DOUBLE` types which can store all values of this type without any lost.

  - ### toDataBufferType

    public final int toDataBufferType()
    Returns the `DataBuffer` constant for this enumeration value. This method is the converse of `forDataBufferType(int)`. It is provided for interoperability with Java2D.
    Returns:
      one of the `DataBuffer` constants.

  - ### fillValue

    public final Number fillValue()
    Returns the default fill value, which is 0 for integer types and NaN for floating point types. The class of this number is the wrapper class corresponding to the type of sample values, ignoring whether the type is signed or unsigned. For example, for `USHORT`, the returned fill value is an instance of the `Short` class.
    Returns:
      0 of NaN in an instance of the wrapper class of the sample values.
    Since:
      1.5