The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.
TypeScript agentai-chatai-studioall-in-onecoding-agentdesktop-appfluximage-generationlinuxllmlocal-ailocal-llmopen-sourceprivacyself-hostedstable-diffusionuncensoreduncensored-aivideo-generationwindows
feat(create): a Qwen-Image 2.1 edit on a Mac can come out in a shape of its own (C75)
purple4bef15c1d09f
diff --git a/e2e/mac-edit-shape-c75.spec.ts b/e2e/mac-edit-shape-c75.spec.tsnew file mode 100644index 00000000..7c880720--- /dev/null+++ b/e2e/mac-edit-shape-c75.spec.ts@@ -0,0 +1,86 @@+import { test, expect, type Page } from '@playwright/test'+import { tauriMockInit, DEFAULT_ASSISTANT_REPLY, DEFAULT_MODEL_NAME, type TauriMockOptions } from './support/tauri-mock'+import { routeCloud, seedOnboardingDone, cloudSwitch } from './support/cloud-mock'+import { mlxCalls } from './support/recorded'+import { gradientPng } from './support/png'++/**+ * C75 on a Mac (Qwen-Image 2.1 on the second engine): "Custom size" cuts the+ * picture to the shape of the size fields before it is staged, centred and at+ * its own pixels, because the engine's edit follows its first image. The+ * default stays: the picture goes in as it is.+ *+ * LU_SHOTS=<folder> writes the screenshot there.+ */++const QWEN_MAC: TauriMockOptions = {+ assistantReply: DEFAULT_ASSISTANT_REPLY,+ modelName: DEFAULT_MODEL_NAME,+ platform: 'mac',+ mlx: { installedImages: ['sd-turbo', 'qwen-image-2.1-8bit'], mfluxEngineInstalled: true },+}++async function boot(page: Page) {+ await page.addInitScript(tauriMockInit, QWEN_MAC)+ await seedOnboardingDone(page)+ await routeCloud(page, { license: 'active', access: true, mediaLive: true })+ await page.goto('/')+ await expect(cloudSwitch(page)).toBeVisible({ timeout: 20_000 })+ await page.getByRole('button', { name: /^Create$/ }).click()+ await page.getByRole('radio', { name: 'Edit / Image to Image', exact: true }).click()+ await page.locator('input[type="file"]').first().setInputFiles({ name: 'wide.png', mimeType: 'image/png', buffer: gradientPng(768, 512) })+ await expect(page.getByRole('img', { name: 'source' })).toBeVisible({ timeout: 15_000 })+}++/** Width and height of the first picture staged for the engine. */+const stagedSize = (page: Page, b64: string) => page.evaluate(async (data) => {+ const img = new Image()+ img.src = `data:image/png;base64,${data}`+ await img.decode()+ return [img.naturalWidth, img.naturalHeight]+}, b64)++async function editOnce(page: Page) {+ await page.locator('textarea').first().fill('make it a night scene')+ await page.getByRole('button', { name: /^Create$/ }).last().click()+ await expect.poll(async () => (await mlxCalls(page)).filter((c) => c.cmd === 'mlx_generate').length, { timeout: 30_000 }).toBe(1)+ return mlxCalls(page)+}++test('C75 Mac: "Custom size" in 9:16 cuts a 3:2 picture to 9:16 before the engine reads it', async ({ page }) => {+ await boot(page)+ await page.getByRole('button', { name: 'Advanced settings' }).click()+ const drawer = page.locator('aside').filter({ hasText: 'Advanced settings' })+ const shape = drawer.getByTestId('qwen-edit-shape')+ await expect(shape).toBeVisible({ timeout: 15_000 })+ await shape.getByRole('radio', { name: 'Custom size' }).click()+ await drawer.getByRole('button', { name: '9:16', exact: true }).click()+ await expect(shape).toContainText('cropped from its centre')+ await shape.scrollIntoViewIfNeeded()+ if (process.env.LU_SHOTS) {+ await page.waitForTimeout(600)+ await page.screenshot({ path: `${process.env.LU_SHOTS}/c75-mac-edit-seitenverhaeltnis.png` })+ }+ await page.keyboard.press('Escape')++ const calls = await editOnce(page)+ // The upload staged the picture as it is; the edit stages the cut one.+ const staged = calls.filter((c) => c.cmd === 'video_stage_start_image')+ expect(staged).toHaveLength(2)+ expect(await stagedSize(page, String(staged[0].raw.data))).toEqual([768, 512])+ const [w, h] = await stagedSize(page, String(staged[1].raw.data))+ // The full height of the picture, the width cut to 9:16, nothing scaled up.+ expect(h).toBe(512)+ expect(Math.abs(w / h - 9 / 16)).toBeLessThan(0.01)+ const gen = calls.find((c) => c.cmd === 'mlx_generate')!+ expect(gen.raw.images).toEqual(['/tmp/lu-e2e/videos/start-images/e2e-start.png'])+})++test('COUNTER-CHECK C75 Mac: by default the picture goes in as it is', async ({ page }) => {+ await boot(page)+ const calls = await editOnce(page)+ // Only the upload: the edit reads the staged picture as it is.+ const staged = calls.filter((c) => c.cmd === 'video_stage_start_image')+ expect(staged).toHaveLength(1)+ expect(await stagedSize(page, String(staged[0].raw.data))).toEqual([768, 512])+})diff --git a/src/components/create/experimental/ParamGroups.tsx b/src/components/create/experimental/ParamGroups.tsxindex efab3ae7..c4f0ea2a 100644--- a/src/components/create/experimental/ParamGroups.tsx+++ b/src/components/create/experimental/ParamGroups.tsx@@ -193,19 +193,25 @@ export function ParamGroups() {const showHiresFix = !isCloud && !isVideo && hiresLane && (!isMlxLocal || macHires)// In Edit the picture comes out in the shape of the source, not of the// sliders (lib/edit-hires). Without a source there is nothing to name yet.+ // C75: a Qwen-Image 2.1 edit picks its shape, on ComfyUI and on the Mac.+ const qwenEditShapeShown = isEdit && !isCloud && laneType === 'qwenimage' && showSize+ const macShapeChosen = qwenEditShapeShown && isMlxLocal && s.qwenEditShape === 'canvas'const hiresBase = isEdit && !macHires? editHiresBase({ source: s.source, width: s.width, height: s.height, modelType: laneType, modelName: laneModel, editShape: s.qwenEditShape }): isEdit && macHires// The Mac's edit comes back in the shape of the source, on the canvas// the engine runs (1056 where the sliders say 1024, lib/image-engines).- ? (s.source ? editCanvas(macEditSide(qwenEditResolution(s.width, s.height)), s.source) : null)+ // C75: in the shape of the fields when the picture is cut to them.+ ? (s.source ? editCanvas(macEditSide(qwenEditResolution(s.width, s.height)), macShapeChosen ? { width: s.width, height: s.height } : s.source) : null): { width: s.width, height: s.height }- // C75: a local Qwen-Image 2.1 edit on ComfyUI picks its shape. The Mac's- // engine edits in the shape of the source and has no such switch.- const qwenEditShapeShown = isEdit && !isCloud && !isMlxLocal && laneType === 'qwenimage' && showSize- const qwenEditOut = qwenEditShapeShown- ? editHiresBase({ source: s.source, width: s.width, height: s.height, modelType: 'qwenimage', editShape: s.qwenEditShape })- : null+ // What the edit comes out at: the Mac's engine runs its own side (1056+ // where the fields say 1024), ComfyUI the node's budget.+ const qwenEditOut = !qwenEditShapeShown ? null+ : isMlxLocal+ ? (s.qwenEditShape === 'canvas' || s.source+ ? editCanvas(macEditSide(qwenEditResolution(s.width, s.height)), s.qwenEditShape === 'canvas' ? { width: s.width, height: s.height } : s.source!)+ : null)+ : editHiresBase({ source: s.source, width: s.width, height: s.height, modelType: 'qwenimage', editShape: s.qwenEditShape })// The grid a typed width or height is rounded to: the local model's latent// cell. The cloud keeps the 64 its providers were checked against.const sizeGrid = isCloud ? 64 : latentDownscale(laneType)diff --git a/src/hooks/useCreate.ts b/src/hooks/useCreate.tsindex 3e9f5bb3..1e85e8aa 100644--- a/src/hooks/useCreate.ts+++ b/src/hooks/useCreate.ts@@ -1,5 +1,6 @@import { useState, useCallback, useRef, useEffect } from 'react'import { blobFromUrl } from '../lib/render/data-url'+import { coverCropBlob } from '../lib/cover-crop'import { videoRunGivenUp, videoRunProgress, videoRunStatus } from '../lib/mlx-video-progress'import { featherMaskBlob } from '../lib/mask-feather'import { macHiresPass } from '../lib/mac-hires'@@ -1115,7 +1116,12 @@ export function useCreate() {// the canvas apart from the budget, and the canvas below stays at the// full size of the source.const editSide = macEditSide(referenceSide(editBudget, editRefs.length))- const editSize = mlxEdit && source && editRefs.length > 1 ? macEditCanvas(editCanvas(editBudget, source)) : null+ // C75: a Qwen-Image 2.1 edit in the shape of the size fields. The+ // picture (and its mask) is cut to that shape before it is staged+ // (lib/cover-crop), so the engine's edit follows it.+ const macShape = mlxEdit && !!source && listed?.type === 'qwenimage' && state.qwenEditShape === 'canvas'+ const shapeOf = macShape ? { width, height } : source+ const editSize = mlxEdit && source && editRefs.length > 1 ? macEditCanvas(editCanvas(editBudget, shapeOf!)) : null// "Transparent background": the same prompt wrapper the Windows graph// uses. The model writes the alpha channel and the engine keeps it.const transparent = wantsTransparent({@@ -1154,11 +1160,16 @@ export function useCreate() {// An image picked in cloud mode has no file yet. It is written to// disk here, the way a start image is before a local video.const editImages: string[] = []- for (const ref of editRefs) {- editImages.push(ref.filename || await stageStartImage(await blobFromUrl(ref.url)))+ for (const [i, ref] of editRefs.entries()) {+ editImages.push(i === 0 && macShape+ ? await stageStartImage(await coverCropBlob(await blobFromUrl(ref.url), width, height))+ : ref.filename || await stageStartImage(await blobFromUrl(ref.url)))}const editMask = mlxEdit && mask?.url- ? await stageStartImage(await featherMaskBlob(await blobFromUrl(mask.url), growMaskBy))+ ? await stageStartImage(await featherMaskBlob(+ macShape ? await coverCropBlob(await blobFromUrl(mask.url), width, height) : await blobFromUrl(mask.url),+ growMaskBy,+ )): undefined// "Improve my prompt" with the Qwen enhancer (GH #148): it rewrites// now, with the chat model out of memory, and its process has endeddiff --git a/src/lib/__tests__/cover-crop-c75.test.ts b/src/lib/__tests__/cover-crop-c75.test.tsnew file mode 100644index 00000000..dfa242cb--- /dev/null+++ b/src/lib/__tests__/cover-crop-c75.test.ts@@ -0,0 +1,26 @@+/**+ * C75 on a Mac: the picture of a Qwen-Image 2.1 edit is cut to the asked+ * shape, centred, at its own pixels, before the engine reads it.+ *+ * Run: npx vitest run src/lib/__tests__/cover-crop-c75.test.ts+ */+import { describe, expect, it } from 'vitest'+import { coverCropRect } from '../cover-crop'++describe('the centred piece in the asked shape', () => {+ it('3:2 to 16:9 keeps the width and cuts top and bottom', () => {+ expect(coverCropRect(768, 512, 1664, 928)).toEqual({ sx: 0, sy: 42, sw: 768, sh: 428 })+ })+ it('3:2 to 9:16 keeps the height and cuts the sides', () => {+ expect(coverCropRect(768, 512, 768, 1360)).toEqual({ sx: 239, sy: 0, sw: 289, sh: 512 })+ })+ it('the same shape is the whole picture', () => {+ expect(coverCropRect(1000, 500, 2000, 1000)).toEqual({ sx: 0, sy: 0, sw: 1000, sh: 500 })+ })+ it('never more than the picture, never nothing', () => {+ const r = coverCropRect(10, 3000, 4096, 64)+ expect(r.sw).toBe(10)+ expect(r.sh).toBeGreaterThanOrEqual(1)+ expect(coverCropRect(0, 0, 1, 1)).toEqual({ sx: 0, sy: 0, sw: 1, sh: 1 })+ })+})diff --git a/src/lib/cover-crop.ts b/src/lib/cover-crop.tsnew file mode 100644index 00000000..da1ce5f3--- /dev/null+++ b/src/lib/cover-crop.ts@@ -0,0 +1,33 @@+// C75 on a Mac: a Qwen-Image 2.1 edit in a shape of its own. The engine's+// edit follows the shape of its first image, as the ComfyUI encode node does,+// so the picture is cut to the asked shape before it is staged: the largest+// centred piece of it in that aspect ratio, at its own pixels (never scaled+// up). The mask is cut by the same rectangle, so it still lies on the picture.++/** The centred piece of a `srcW` x `srcH` picture in the aspect ratio of+ * `w` x `h`, in whole source pixels. */+export function coverCropRect(srcW: number, srcH: number, w: number, h: number): { sx: number; sy: number; sw: number; sh: number } {+ if (!(srcW > 0 && srcH > 0 && w > 0 && h > 0)) return { sx: 0, sy: 0, sw: Math.max(1, srcW), sh: Math.max(1, srcH) }+ const want = w / h+ let sw = srcW+ let sh = Math.round(srcW / want)+ if (sh > srcH) {+ sh = srcH+ sw = Math.round(srcH * want)+ }+ sw = Math.max(1, Math.min(srcW, sw))+ sh = Math.max(1, Math.min(srcH, sh))+ return { sx: Math.floor((srcW - sw) / 2), sy: Math.floor((srcH - sh) / 2), sw, sh }+}++/** `image` cut to the centred piece in the aspect ratio of `w` x `h`, as a PNG. */+export async function coverCropBlob(image: Blob, w: number, h: number): Promise<Blob> {+ const bitmap = await createImageBitmap(image)+ const { sx, sy, sw, sh } = coverCropRect(bitmap.width, bitmap.height, w, h)+ const canvas = document.createElement('canvas')+ canvas.width = sw+ canvas.height = sh+ canvas.getContext('2d')!.drawImage(bitmap, sx, sy, sw, sh, 0, 0, sw, sh)+ bitmap.close()+ return new Promise((resolve, reject) => canvas.toBlob((b) => (b ? resolve(b) : reject(new Error('The picture could not be cut to its shape.'))), 'image/png'))+}