Skip to content

A lightweight Clip Studio Paint alternative with real-time multiplayer collaborative drawing. Built with Tauri and React.

2.7 KB
Raw Blame
import { BlendMode, LoadedDocument } from '../types/editor';
import { extractWorkTimeMsFromPsdBuffer } from './artworkTimeTracker';
import {
  fromNativeLayerNodeMeta,
  openNativeDocumentBytes,
} from './nativeEngineBridge';

/**
 * Maps Photoshop blend mode string to Ink Studio BlendMode.
 */
export function mapPsdBlendMode(psdBlendMode?: string): BlendMode {
  if (!psdBlendMode) return 'normal';
  const mode = psdBlendMode.toLowerCase();

  switch (mode) {
    case 'multiply':
      return 'multiply';
    case 'screen':
      return 'screen';
    case 'overlay':
      return 'overlay';
    case 'darken':
      return 'darken';
    case 'darker color':
      return 'darker-color';
    case 'lighten':
      return 'lighten';
    case 'lighter color':
      return 'lighter-color';
    case 'color dodge':
      return 'color-dodge';
    case 'linear dodge':
      return 'add-glow';
    case 'color burn':
      return 'color-burn';
    case 'linear burn':
      return 'linear-burn';
    case 'hard light':
      return 'hard-light';
    case 'vivid light':
      return 'vivid-light';
    case 'linear light':
      return 'linear-light';
    case 'pin light':
      return 'pin-light';
    case 'hard mix':
      return 'hard-mix';
    case 'soft light':
      return 'soft-light';
    case 'difference':
      return 'difference';
    case 'subtract':
    case 'subtraction':
      return 'subtract';
    case 'exclusion':
      return 'exclusion';
    case 'divide':
      return 'divide';
    case 'hue':
      return 'hue';
    case 'saturation':
      return 'saturation';
    case 'color':
      return 'color';
    case 'luminosity':
      return 'luminosity';
    case 'pass through':
      return 'pass-through';
    case 'normal':
    default:
      return 'normal';
  }
}

/**
 * Parses a Photoshop `.psd` or `.psb` file directly into Rust `crates/ink-engine`
 * with zero full-resolution DOM `<canvas>` allocations.
 */
export async function parsePsdFile(
  buffer: ArrayBuffer,
  fileName: string,
  docId?: string
): Promise<LoadedDocument> {
  const targetDocId = docId || `doc-${Date.now()}-${Math.random().toString(36).slice(2, 6)}`;
  const nativeRes = await openNativeDocumentBytes(targetDocId, fileName, new Uint8Array(buffer));
  const nativeWorkTimeMs = extractWorkTimeMsFromPsdBuffer(buffer) ?? nativeRes.doc.workTimeMs;
  return {
    name: nativeRes.doc.name || fileName.replace(/\.[^/.]+$/, ''),
    dimensions: nativeRes.doc.dimensions,
    colorMode: nativeRes.doc.colorMode,
    layers: nativeRes.doc.layers.map(fromNativeLayerNodeMeta),
    previewDataUrl: nativeRes.previewDataUrl ?? nativeRes.doc.previewDataUrl ?? undefined,
    ...(nativeWorkTimeMs !== undefined ? { workTimeMs: nativeWorkTimeMs } : {}),
  };
}