LCOV - code coverage report
Current view: top level - src/utils - imageUtil.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 77.8 % 36 28
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { appendCloudResParam } from '@/platform/distribution/cloudPreviewUtil'
       2              : import { parseAnnotatedPath } from '@/utils/createAnnotatedPath'
       3              : 
       4         2356 : const GRID_THUMBNAIL_PREVIEW_FORMAT = 'webp;75'
       5              : 
       6              : /**
       7              :  * Converts a full-resolution `/api/view` image URL into a lightweight
       8              :  * thumbnail URL for small on-node grid cells: server-resized on cloud (`res`)
       9              :  * and re-encoded to a compact format on every backend (`preview`). Non-view
      10              :  * URLs, SVGs (the server cannot rasterize them into a preview), and URLs that
      11              :  * already request a preview are returned unchanged.
      12              :  */
      13          108 : export function getGridThumbnailUrl(url: string): string {
      14          108 :   if (!url) return url
      15          108 :   let parsed: URL
      16          108 :   try {
      17          108 :     parsed = new URL(url, window.location.origin)
      18              :   } catch {
      19            0 :     return url
      20              :   }
      21          108 :   if (!parsed.pathname.endsWith('/view')) return url
      22              : 
      23          108 :   const { searchParams } = parsed
      24          108 :   const filename = searchParams.get('filename') ?? undefined
      25          108 :   const isSvg = !!filename && filename.toLowerCase().endsWith('.svg')
      26          108 :   appendCloudResParam(searchParams, filename)
      27          108 :   if (!isSvg && !searchParams.has('preview'))
      28          108 :     searchParams.set('preview', GRID_THUMBNAIL_PREVIEW_FORMAT)
      29              : 
      30          108 :   return url.startsWith('http')
      31              :     ? parsed.toString()
      32              :     : `${parsed.pathname}${parsed.search}`
      33              : }
      34              : 
      35           42 : export function extractWidgetStringValue(value: unknown): string | undefined {
      36           42 :   if (typeof value === 'string') return value
      37           42 :   if (
      38              :     value &&
      39              :     typeof value === 'object' &&
      40              :     'filename' in value &&
      41              :     typeof value.filename === 'string'
      42              :   )
      43            0 :     return value.filename
      44              :   return undefined
      45              : }
      46              : 
      47          302 : export function parseImageWidgetValue(raw: string): {
      48              :   filename: string
      49              :   subfolder: string
      50              :   type: string
      51              : } {
      52          302 :   const { filepath, rootFolder: type } = parseAnnotatedPath(raw)
      53          302 :   const path = filepath.replaceAll('\\', '/')
      54          302 :   const slashIndex = path.lastIndexOf('/')
      55          302 :   return {
      56              :     filename: path.slice(slashIndex + 1),
      57              :     subfolder: path.slice(0, Math.max(slashIndex, 0)),
      58              :     type
      59              :   }
      60              : }
      61              : 
      62         2356 : export const is_all_same_aspect_ratio = (imgs: HTMLImageElement[]): boolean => {
      63            0 :   if (!imgs.length || imgs.length === 1) return true
      64              : 
      65            0 :   const ratio = imgs[0].naturalWidth / imgs[0].naturalHeight
      66              : 
      67            0 :   for (let i = 1; i < imgs.length; i++) {
      68              :     const this_ratio = imgs[i].naturalWidth / imgs[i].naturalHeight
      69            0 :     if (ratio != this_ratio) return false
      70              :   }
      71              : 
      72            0 :   return true
      73              : }
      74              : 
      75         2356 : export const fitDimensionsToNodeWidth = (
      76              :   width: number,
      77              :   height: number,
      78              :   nodeWidth: number,
      79              :   minHeight: number = 64
      80              : ): { minHeight: number; minWidth: number } => {
      81           23 :   const intrinsicAspectRatio = width / height
      82           23 :   if (!intrinsicAspectRatio || isNaN(intrinsicAspectRatio))
      83            0 :     return { minHeight: 0, minWidth: 0 }
      84              : 
      85              :   // Set min. height s.t. image spans node's x-axis while maintaining aspect ratio
      86           23 :   const minWidth = nodeWidth
      87              :   const calculatedHeight = Math.max(minWidth / intrinsicAspectRatio, minHeight)
      88              : 
      89           23 :   return { minHeight: calculatedHeight, minWidth }
      90              : }
        

Generated by: LCOV version 2.0-1