All files / dicomImageLoader/src/shared getPixelDataTypeFromMinMax.ts

66.66% Statements 10/15
72.72% Branches 16/22
50% Functions 1/2
66.66% Lines 10/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63                                                    1640x 1632x 194x 60x 134x 134x         1438x   1438x 1438x         1640x                                    
import type { Types } from '@cornerstonejs/core';
 
/**
 * Determines the appropriate TypedArray constructor based on the min and max pixel values
 * @param min - The minimum pixel value in the data
 * @param max - The maximum pixel value in the data
 * @returns The appropriate TypedArray constructor (Uint8Array, Uint16Array, Int8Array, Int16Array, or Float32Array)
 * @remarks
 * This function examines the min/max values and returns the most memory efficient
 * TypedArray that can represent the data without loss of precision:
 * - For integer values:
 *   - If all values are positive (min >= 0):
 *     - Returns Uint8Array if max <= 255
 *     - Returns Uint16Array if max <= 65535
 *     - Returns Uint32Array if max <= 4294967295
 *   - If values include negatives:
 *     - Returns Int8Array if values are within [-128, 127]
 *     - Returns Int16Array if values are within [-32768, 32767]
 * - For non-integer values or values outside above ranges:
 *   - Returns Float32Array as the fallback
 */
export default function getPixelDataTypeFromMinMax(
  min: number,
  max: number
): Types.PixelDataTypedArray {
  let pixelDataType;
  if (Number.isInteger(min) && Number.isInteger(max)) {
    if (min >= 0) {
      if (max <= 255) {
        pixelDataType = Uint8Array;
      } else if (max <= 65535) {
        pixelDataType = Uint16Array;
      } else Eif (max <= 4294967295) {
        pixelDataType = Uint32Array;
      }
    } else {
      Iif (min >= -128 && max <= 127) {
        pixelDataType = Int8Array;
      } else Eif (min >= -32768 && max <= 32767) {
        pixelDataType = Int16Array;
      }
    }
  }
 
  return pixelDataType || Float32Array;
}
 
/**
 * Validates if a given TypedArray type is appropriate for the min/max pixel value range
 * @param min - The minimum pixel value in the data
 * @param max - The maximum pixel value in the data
 * @param type - The TypedArray constructor to validate
 * @returns True if the type can represent the min/max range without data loss, false otherwise
 */
export function validatePixelDataType(
  min,
  max,
  type: Types.PixelDataTypedArray
) {
  const pixelDataType = getPixelDataTypeFromMinMax(min, max);
  return pixelDataType === type;
}