/** * react-native-pixel-data * * TypeScript type definitions for the pixel data extraction SDK. * These types are mirrored in Swift and Kotlin for native implementations. * * @packageDocumentation * @module react-native-pixel-data/types */ /** * Load image from a remote HTTP/HTTPS URL. * * @remarks * Supports JPEG, PNG, WebP, and GIF formats. * The URL must be accessible from the device. * HTTPS is recommended for security. * * @example * ```typescript * const source: UrlImageSource = { * type: 'url', * value: 'https://example.com/image.jpg' * }; * ``` */ export interface UrlImageSource { /** Discriminator for URL source type */ type: 'url'; /** The HTTP/HTTPS URL of the image */ value: string; } /** * Load image from a local file path. * * @remarks * Supports absolute paths and file:// URIs. * Ensure the app has read permissions for the file. * * @example * ```typescript * const source: FileImageSource = { * type: 'file', * value: '/path/to/image.png' * }; * // Or with file:// prefix * const source2: FileImageSource = { * type: 'file', * value: 'file:///path/to/image.png' * }; * ``` */ export interface FileImageSource { /** Discriminator for file source type */ type: 'file'; /** Local file path (absolute path or file:// URI) */ value: string; } /** * Load image from a Base64-encoded string. * * @remarks * Accepts both raw Base64 strings and data URI format. * Useful for images received from APIs or stored in databases. * * @example * ```typescript * // With data URI prefix * const source: Base64ImageSource = { * type: 'base64', * value: 'data:image/png;base64,iVBORw0KGgo...' * }; * // Raw Base64 (no prefix) * const source2: Base64ImageSource = { * type: 'base64', * value: 'iVBORw0KGgo...' * }; * ``` */ export interface Base64ImageSource { /** Discriminator for Base64 source type */ type: 'base64'; /** Base64-encoded image data (with or without data URI prefix) */ value: string; } /** * Load image from bundled app assets. * * @remarks * - iOS: Images in the app bundle or asset catalog * - Android: Images in res/drawable or assets folder * * @example * ```typescript * const source: AssetImageSource = { * type: 'asset', * value: 'images/sample.png' * }; * ``` */ export interface AssetImageSource { /** Discriminator for asset source type */ type: 'asset'; /** Asset name or path within the app bundle */ value: string; } /** * Load image from device photo library. * * @remarks * Requires photo library permissions. * - iOS: Use 'ph://' prefix with PHAsset local identifier * - Android: Use content:// URI from MediaStore * * @example * ```typescript * // iOS * const iosSource: PhotoLibraryImageSource = { * type: 'photoLibrary', * value: 'ph://CC95F08C-88C3-4012-9D6D-64A413D254B3' * }; * // Android * const androidSource: PhotoLibraryImageSource = { * type: 'photoLibrary', * value: 'content://media/external/images/media/123' * }; * ``` */ export interface PhotoLibraryImageSource { /** Discriminator for photo library source type */ type: 'photoLibrary'; /** Photo library identifier (ph:// on iOS, content:// on Android) */ value: string; } /** * Union type for all supported image sources. * * @remarks * Use this type when accepting any image source in your code. * The `type` field acts as a discriminator for type narrowing. * * @example * ```typescript * function processImage(source: ImageSource) { * switch (source.type) { * case 'url': * console.log('Loading from URL:', source.value); * break; * case 'file': * console.log('Loading from file:', source.value); * break; * // ... handle other types * } * } * ``` */ export type ImageSource = UrlImageSource | FileImageSource | Base64ImageSource | AssetImageSource | PhotoLibraryImageSource; /** * Supported color formats for pixel data extraction. * * @remarks * Choose the format that matches your ML model's input requirements: * - `RGB` - Standard 3-channel format (most common) * - `RGBA` - 4-channel with alpha (transparency) * - `BGR` - Blue-Green-Red order (OpenCV compatible) * - `BGRA` - BGR with alpha channel * - `GRAYSCALE` - Single channel luminance * * @example * ```typescript * // Most ML models use RGB * const format: ColorFormat = 'RGB'; * * // OpenCV models often use BGR * const opencvFormat: ColorFormat = 'BGR'; * ``` */ export type ColorFormat = 'RGB' | 'RGBA' | 'BGR' | 'BGRA' | 'GRAYSCALE'; /** * Get the number of channels for a given color format. * * @param format - The color format to check * @returns The number of channels (1, 3, or 4) * * @example * ```typescript * getChannelCount('RGB'); // Returns 3 * getChannelCount('RGBA'); // Returns 4 * getChannelCount('GRAYSCALE'); // Returns 1 * ``` */ export declare function getChannelCount(format: ColorFormat): number; /** * Normalization presets for ML model compatibility. * * @remarks * - `imagenet` - ImageNet normalization: `(pixel/255 - mean) / std` * - mean: [0.485, 0.456, 0.406] * - std: [0.229, 0.224, 0.225] * - `tensorflow` - TensorFlow style: `(pixel - 127.5) / 127.5` (range: -1 to 1) * - `scale` - Simple scaling: `pixel / 255` (range: 0 to 1) * - `raw` - No normalization (range: 0 to 255) * - `custom` - User-defined mean and std values */ export type NormalizationPreset = 'imagenet' | 'tensorflow' | 'scale' | 'raw' | 'custom'; /** * Normalization configuration for pixel values. * * @remarks * Normalization is applied per-channel using the formula: * `normalized = (pixel / 255 - mean) / std` * * For `raw` preset, no transformation is applied. * For `scale` preset, values are simply divided by 255. * * @example * ```typescript * // Use ImageNet normalization (PyTorch pretrained models) * const imagenet: Normalization = { preset: 'imagenet' }; * * // Use TensorFlow normalization * const tf: Normalization = { preset: 'tensorflow' }; * * // Custom normalization * const custom: Normalization = { * preset: 'custom', * mean: [0.5, 0.5, 0.5], * std: [0.5, 0.5, 0.5] * }; * ``` */ export interface Normalization { /** The normalization preset to use */ preset: NormalizationPreset; /** * Per-channel mean values for custom normalization. * Required when preset is 'custom'. * Array length must match the number of color channels. */ mean?: number[]; /** * Per-channel standard deviation values for custom normalization. * Required when preset is 'custom'. * Array length must match the number of color channels. */ std?: number[]; } /** * Resize strategies for image preprocessing. * * @remarks * - `cover` - Fill the target dimensions, cropping excess (maintains aspect ratio) * - `contain` - Fit within dimensions, letterbox with black padding (maintains aspect ratio) * - `stretch` - Stretch to exact dimensions (may distort aspect ratio) * * @example * ```typescript * // Cover: good for classification models * const strategy: ResizeStrategy = 'cover'; * * // Contain: preserves full image, adds padding * const strategy2: ResizeStrategy = 'contain'; * ``` */ export type ResizeStrategy = 'cover' | 'contain' | 'stretch'; /** * Options for image resizing. * * @remarks * Resize is applied after ROI cropping (if specified). * For ML models, common sizes are 224x224, 256x256, or 299x299. * * @example * ```typescript * // Standard ImageNet size * const resize: ResizeOptions = { * width: 224, * height: 224, * strategy: 'cover' * }; * * // EfficientNet size * const resize2: ResizeOptions = { * width: 380, * height: 380, * strategy: 'cover' * }; * ``` */ export interface ResizeOptions { /** Target width in pixels (must be positive) */ width: number; /** Target height in pixels (must be positive) */ height: number; /** * Resize strategy to use. * @defaultValue 'cover' */ strategy?: ResizeStrategy; } /** * Data layout formats for ML frameworks. * * @remarks * Different ML frameworks expect different tensor layouts: * - `HWC` - Height × Width × Channels (general purpose, TensorFlow.js) * - `CHW` - Channels × Height × Width (PyTorch, ONNX) * - `NHWC` - Batch × Height × Width × Channels (TensorFlow) * - `NCHW` - Batch × Channels × Height × Width (PyTorch batched) * * The N dimension (batch) is always 1 for single image processing. * * @example * ```typescript * // For PyTorch models * const layout: DataLayout = 'CHW'; * * // For TensorFlow models * const layout2: DataLayout = 'NHWC'; * ``` */ export type DataLayout = 'HWC' | 'CHW' | 'NHWC' | 'NCHW'; /** * Region of Interest for cropping before processing. * * @remarks * ROI is applied before resize. Coordinates are in pixels * relative to the original image dimensions. * * Use ROI to: * - Extract specific regions from large images * - Focus on detected objects (e.g., from a bounding box detector) * - Crop out irrelevant areas * * @example * ```typescript * // Extract a 200x200 region starting at (100, 50) * const roi: Roi = { * x: 100, * y: 50, * width: 200, * height: 200 * }; * ``` */ export interface Roi { /** X coordinate of top-left corner (pixels from left edge) */ x: number; /** Y coordinate of top-left corner (pixels from top edge) */ y: number; /** Width of the region in pixels */ width: number; /** Height of the region in pixels */ height: number; } /** * Output data type for pixel values. * * @remarks * - `array` - Standard JavaScript number array (most compatible) * - `float32Array` - TypedArray for better performance with large images * - `uint8Array` - TypedArray with raw 0-255 values (only with 'raw' normalization) * * @example * ```typescript * // Standard array (default) * const format: OutputFormat = 'array'; * * // TypedArray for ML inference * const format2: OutputFormat = 'float32Array'; * ``` */ export type OutputFormat = 'array' | 'float32Array' | 'uint8Array'; /** * Options for the getPixelData function. * * @remarks * This is the main configuration object for pixel data extraction. * Only `source` is required; all other options have sensible defaults. * * @example * ```typescript * // Minimal configuration * const options: GetPixelDataOptions = { * source: { type: 'url', value: 'https://example.com/image.jpg' } * }; * * // Full configuration for PyTorch model * const fullOptions: GetPixelDataOptions = { * source: { type: 'url', value: 'https://example.com/image.jpg' }, * colorFormat: 'RGB', * resize: { width: 224, height: 224, strategy: 'cover' }, * normalization: { preset: 'imagenet' }, * dataLayout: 'CHW', * outputFormat: 'array' * }; * ``` */ export interface GetPixelDataOptions { /** * Image source configuration. * This is the only required option. */ source: ImageSource; /** * Output color format. * @defaultValue 'RGB' */ colorFormat?: ColorFormat; /** * Normalization preset or custom configuration. * @defaultValue `{ preset: 'scale' }` */ normalization?: Normalization; /** * Resize options. * If not provided, original image size is preserved. */ resize?: ResizeOptions; /** * Region of Interest to crop before processing. * Applied before resize. Coordinates are in original image pixels. */ roi?: Roi; /** * Output data layout for ML framework compatibility. * @defaultValue 'HWC' */ dataLayout?: DataLayout; /** * Output data format. * @defaultValue 'array' */ outputFormat?: OutputFormat; } /** * Result from getPixelData containing the extracted pixel data and metadata. * * @remarks * The `data` array contains normalized pixel values in the specified layout. * Total array length = width × height × channels (for HWC/CHW) * or = 1 × channels × height × width (for NCHW/NHWC with batch dimension). * * @example * ```typescript * const result = await getPixelData(options); * * console.log(`Image: ${result.width}x${result.height}`); * console.log(`Channels: ${result.channels}`); * console.log(`Processing time: ${result.processingTimeMs.toFixed(2)}ms`); * console.log(`Total pixels: ${result.data.length}`); * * // Access first pixel (RGB values for HWC layout) * const r = result.data[0]; * const g = result.data[1]; * const b = result.data[2]; * ``` */ export interface PixelDataResult { /** * The extracted pixel data. * Values are normalized according to the normalization preset. * Array is flattened according to the dataLayout. * * The type depends on the `outputFormat` option: * - `'array'` (default): `number[]` * - `'float32Array'`: `Float32Array` * - `'uint8Array'`: `Uint8Array` */ data: number[] | Float32Array | Uint8Array; /** Width of the output image in pixels */ width: number; /** Height of the output image in pixels */ height: number; /** Number of channels in the output (1 for grayscale, 3 for RGB/BGR, 4 for RGBA/BGRA) */ channels: number; /** The data layout used for the output */ dataLayout: DataLayout; /** Processing time in milliseconds (includes loading, resizing, and normalization) */ processingTimeMs: number; /** * Shape of the output tensor as an array of dimensions. * @example [224, 224, 3] for HWC, [3, 224, 224] for CHW, [1, 3, 224, 224] for NCHW */ shape?: number[]; } /** * Options for batch processing multiple images. * * @remarks * Batch processing is more efficient than sequential calls * when processing multiple images. * * @example * ```typescript * const batchOptions: BatchOptions = { * concurrency: 4 // Process 4 images in parallel * }; * ``` */ export interface BatchOptions { /** * Maximum number of concurrent image processing operations. * Higher values use more memory but process faster. * @defaultValue 4 */ concurrency?: number; } /** * Error result for a single image in batch processing. * * @remarks * When batch processing fails for an individual image, * an error object is returned instead of PixelDataResult. * Use `isPixelDataError()` to check for errors. * * @example * ```typescript * results.results.forEach((result, index) => { * if (isPixelDataError(result)) { * console.error(`Image ${result.index} failed: ${result.message}`); * } * }); * ``` */ export interface PixelDataError { /** Error flag - always true for error results */ error: true; /** Human-readable error message */ message: string; /** Error code for programmatic handling */ code: string; /** Index of the failed image in the batch (0-based) */ index: number; } /** * Result from batch processing multiple images. * * @remarks * The results array maintains the same order as the input options array. * Each element is either a successful PixelDataResult or a PixelDataError. * * @example * ```typescript * const batchResult = await batchGetPixelData(optionsArray); * * console.log(`Processed ${batchResult.results.length} images`); * console.log(`Total time: ${batchResult.totalTimeMs.toFixed(2)}ms`); * * const successful = batchResult.results.filter(r => !isPixelDataError(r)); * const failed = batchResult.results.filter(isPixelDataError); * ``` */ export interface BatchResult { /** * Results for each image in the same order as input. * Each element is either PixelDataResult (success) or PixelDataError (failure). */ results: (PixelDataResult | PixelDataError)[]; /** Total processing time in milliseconds for the entire batch */ totalTimeMs: number; } /** * Type guard to check if a batch result item is an error. * * @param result - The result item to check * @returns True if the result is an error, false otherwise * * @example * ```typescript * batchResult.results.forEach((result) => { * if (isPixelDataError(result)) { * // TypeScript knows result is PixelDataError here * console.error(result.code, result.message); * } else { * // TypeScript knows result is PixelDataResult here * console.log(result.width, result.height); * } * }); * ``` */ export declare function isPixelDataError(result: PixelDataResult | PixelDataError): result is PixelDataError; /** * Custom error class for pixel data operations. * * @remarks * All errors from the SDK are wrapped in PixelDataException * with a specific error code for programmatic handling. * * Error codes: * - `INVALID_SOURCE` - Invalid image source configuration * - `INVALID_RESIZE` - Invalid resize dimensions * - `INVALID_ROI` - Invalid region of interest * - `INVALID_NORMALIZATION` - Invalid normalization configuration * - `LOAD_ERROR` - Failed to load image * - `FILE_NOT_FOUND` - File does not exist * - `PERMISSION_DENIED` - Missing required permissions * - `PROCESSING_ERROR` - Error during pixel processing * - `UNKNOWN` - Unexpected error * * @example * ```typescript * try { * await getPixelData(options); * } catch (error) { * if (error instanceof PixelDataException) { * switch (error.code) { * case 'FILE_NOT_FOUND': * console.error('Image file not found'); * break; * case 'LOAD_ERROR': * console.error('Failed to load image:', error.message); * break; * default: * console.error(`Error [${error.code}]:`, error.message); * } * } * } * ``` */ export declare class PixelDataException extends Error { /** Error code for programmatic handling */ code: string; /** * Creates a new PixelDataException. * * @param code - Error code identifier * @param message - Human-readable error message */ constructor(code: string, message: string); } //# sourceMappingURL=types.d.ts.map