diff --git a/README.md b/README.md index 4cbcc4c1..4872b566 100644 --- a/README.md +++ b/README.md @@ -91,7 +91,7 @@ The MiniMax Code Agent communicates through the host MCP client. Business reques | [本机大盘 · Local Market Board](plugins/jzlikewei/kurumi-monitor/) | Watch your own machine as a stock terminal: CPU, GPU, temperature, memory, disk, network and every running process become listed stocks with live candlesticks, moving averages, a watchlist, a leaderboard and a trade tape, on macOS and Windows. Requires minimax Code 3.1.2 or later | [jzlikewei](https://github.com/jzlikewei) | | [Quota Bottle](plugins/7723qqq/quota-bottle/) | An octopus-shaped glass bottle that floats on your desktop and shows the remaining Token Plan quota from a login you configure yourself, with the liquid level and opacity following the remaining share, and a device-code login plus an opt-in local token-usage estimate as fallbacks | [7723qqq](https://github.com/7723qqq) | | [Site QA](plugins/lonelyguy-se1/site-qa/) | Check local HTML/robots/sitemap for SEO and accessibility gaps, then ask the agent to fix them (Mini App + skill/CLI). Requires MiniMax Code 3.1.6 or later | [LonelyGuySE1](https://github.com/LonelyGuy-SE1) | -| [H3-Image-Executor](plugins/liddalidd/h3-image-executor/) | Turn one sentence into verified 4K images: it plans the shot list, searches for reference images, generates, reviews each image by looking at it, corrects colour cast, runs a bounded fix loop and delivers the result with an acceptance report; multi-image runs generate a character sheet first so the same subject stays the same subject. Needs an H3-Image endpoint and key you configure yourself. Requires MiniMax Code 3.1.6 or later | [liddalidd](https://github.com/liddalidd) | +| [H3-Image-Executor](plugins/liddalidd/h3-image-executor/) | Turn one sentence into verified 4K images: it plans the shot list, searches for reference images, generates, reviews each image by looking at it, corrects colour cast, runs a bounded fix loop and delivers the result with an acceptance report; multi-image runs generate a character sheet first so the same subject stays the same subject, QR codes in the picture are replaced with real ones that are scanned before delivery, long or calligraphic text is typeset character for character onto a surface the model leaves blank, data charts are drawn from the numbers onto a blank panel and measured back before delivery, and animated GIFs are assembled from keyframes generated off one reference image, cut from a delivered grid of panels, or, for loading rings and spinning icons, turned around their centre by the program. Needs an H3-Image endpoint and key you configure yourself. Requires MiniMax Code 3.1.6 or later | [liddalidd](https://github.com/liddalidd) | | [Express Media](plugins/andre-1998/express-media/) | Compress a video to the size each platform accepts and report whether it fits; the bundled limits table shows where every number comes from | [Andre-1998](https://github.com/Andre-1998) |
diff --git a/README.zh-CN.md b/README.zh-CN.md index 1f6223d7..19c6bf71 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -91,7 +91,7 @@ MiniMax Code Agent 通过宿主 MCP 客户端与 MiniApp 协作。业务请求 | [本机大盘](plugins/jzlikewei/kurumi-monitor/README.zh-CN.md) | 把本机当成行情终端来看:CPU、GPU、温度、内存、磁盘、网络与每个运行中的进程都是一只个股,实时 K 线、均线、自选行情、主力动向与逐笔成交,支持 macOS 与 Windows。需要 minimax Code 3.1.2 或更高版本 | [jzlikewei](https://github.com/jzlikewei) | | [额度瓶](plugins/7723qqq/quota-bottle/README.zh-CN.md) | 一只悬浮在桌面上的章鱼形玻璃瓶,用你自己配置的登录读取 Token Plan 剩余额度;液面与透明度按剩余占比走,拿不到实时额度时回退到设备码登录或手填总额与可选的本机本月用量 | [7723qqq](https://github.com/7723qqq) | | [Site QA](plugins/lonelyguy-se1/site-qa/) | 检查本地 HTML/robots/sitemap 的 SEO / 无障碍问题,再让 Agent 修复(Mini App + skill/CLI)。需要 MiniMax Code 3.1.6 或更高版本(说明文档为英文) | [LonelyGuySE1](https://github.com/LonelyGuy-SE1) | -| [H3-Image-Executor](plugins/liddalidd/h3-image-executor/README.zh-CN.md) | 把一句话需求跑成经过校验的 4K 图片:规划分镜、检索参考图、生成、看图评审、色偏校正、有限次修正,最后交付图片和验收报告;多图任务会先生成角色设定图锁定主体外观。需要自备 H3-Image 接口地址和 Key。需要 MiniMax Code 3.1.6 或更高版本 | [liddalidd](https://github.com/liddalidd) | +| [H3-Image-Executor](plugins/liddalidd/h3-image-executor/README.zh-CN.md) | 把一句话需求跑成经过校验的 4K 图片:规划分镜、检索参考图、生成、看图评审、色偏校正、有限次修正,最后交付图片和验收报告;多图任务会先生成角色设定图锁定主体外观,画面里的二维码会换成真码并在交付前扫码验证,大段文字和书法由程序按原文排进模型留空的载体上,数据图表由程序按数据画进留空的面板并在交付前量回来核对,还能用一张参考图改出几个关键帧、拼成 GIF 动图,或者把交付的分格组图切开拼成 GIF,加载圈、转圈的图标由程序按圆心转出来。需要自备 H3-Image 接口地址和 Key。需要 MiniMax Code 3.1.6 或更高版本 | [liddalidd](https://github.com/liddalidd) | | [急件 · 视频投递](plugins/andre-1998/express-media/README.zh-CN.md) | 把视频压到各平台能收的体积,并如实告诉你发不发得出去;内置上限表标明每个数字的来源 | [Andre-1998](https://github.com/Andre-1998) |
diff --git a/plugins/liddalidd/h3-image-executor/.minimax-plugin/plugin.json b/plugins/liddalidd/h3-image-executor/.minimax-plugin/plugin.json index a930e308..0e507e41 100644 --- a/plugins/liddalidd/h3-image-executor/.minimax-plugin/plugin.json +++ b/plugins/liddalidd/h3-image-executor/.minimax-plugin/plugin.json @@ -2,20 +2,28 @@ "schemaVersion": 1, "name": "h3-image-executor", "displayName": "H3-Image-Executor", - "version": "1.8.1", - "description": "把一句话需求跑成经过校验的 4K 图片:自动检索参考图、规划分镜、生成、看图评审、色偏校正与修正循环,最后交付验证过的图和验收报告。多图任务会先生成角色设定图来锁定主体外观,全部出完后再做一次跨图一致性对比。在聊天里直接说「画一张…」就会把需求送进面板自动开跑,不需要自己调生图工具。", + "version": "1.9.0", + "description": "把一句话需求跑成经过校验的 4K 图片:自动检索参考图、规划分镜、生成、看图评审、色偏校正与修正循环,最后交付验证过的图和验收报告。多图任务会先生成角色设定图来锁定主体外观,全部出完后再做一次跨图一致性对比。画面里要放二维码时,会把模型画的假码换成能扫的真码,并从交付文件扫码验证;要写大段文字、竖排或毛笔书法时,先让模型画一张载体留空的底图,再由程序按原文逐字排上去;要放柱状图、折线图、饼图这类数据图表时,同样让模型留出空白面板,坐标轴、刻度、数值和比例由程序按数据画上去,再从交付文件量回来核对。还能用一张参考图做 GIF 动图:从参考图出发一帧接一帧改出关键帧,动作跨度大的地方补过渡帧,再由程序拼成循环播放的 GIF。加载圈、转圈的图标这类绕中心转的动画只画一张图,由程序按圆心转出各帧;分格的组图(六格表情、四格漫画)交付后可以切格拼成 GIF,组图不重画,相邻两格之间一起补一格过渡、从候选里挑;随包的 h3-gif 工具也能把别处的组图、几张单独的图或程序画的一组帧拼成 GIF、让图转起来,并读回核对。在聊天里直接说「画一张…」就会把需求送进面板自动开跑,不需要自己调生图工具。", "author": "liddalidd", "icon": "icon.png", "category": "Studio", "exampleQueries": [ "画一张橘猫趴在窗台上晒太阳的图,午后暖光,胶片质感。", "用 H3-Image-Executor 画一组四格漫画,注意角色前后要长得一样。", - "接着刚才那张图,把背景换成夜景。" + "接着刚才那张图,把背景换成夜景。", + "设计一张 MiniMax 主题的电子名片,二维码链接至 https://www.minimax.cn/", + "写一幅竖排毛笔书法条幅,米色宣纸,内容:床前明月光,疑是地上霜。举头望明月,低头思故乡。", + "做一个柴犬吉祥物的 GIF 表情包:先眨眨眼,再抬起爪子挥手打招呼。", + "做一个十二个点的加载圈 GIF,亮点顺时针转,512×512。", + "画一组六格的 Q 版小人表情变化,从微笑到大哭,再拼成 GIF。", + "做一张咖啡店年度经营海报,右边白板上用柱状图画出 2021 到 2025 年营业额:86、120、158、203、245 万元。" ], "apps": [], "mcpServers": [], "skills": [ "skills/h3-image-executor/SKILL.md", - "skills/h3-image-review/SKILL.md" + "skills/h3-image-review/SKILL.md", + "skills/h3-image-qrcode/SKILL.md", + "skills/h3-image-gif/SKILL.md" ] } diff --git a/plugins/liddalidd/h3-image-executor/README.md b/plugins/liddalidd/h3-image-executor/README.md index ce6844e3..fd6aa086 100644 --- a/plugins/liddalidd/h3-image-executor/README.md +++ b/plugins/liddalidd/h3-image-executor/README.md @@ -19,17 +19,193 @@ before the app can generate anything. See [Configure the endpoint](#configure-th - **Plans before it draws.** A planner turns the sentence into a shot list: one spec per image, each with its own aspect ratio, hard constraints and soft preferences. -- **Finds reference images by itself.** When a named subject has to look a specific way, it - searches for a reference, downloads it, and judges whether the candidate is usable. +- **Finds reference images by itself.** Whether to search, how many items and how much effort to + spend is up to the planner for each request; it does not search every time. When the request + contains a proper noun or a specific name (a person, a character, a brand or product, a logo, a + car model, a landmark, a named work, or an unfamiliar term for something whose look is uncertain), + the planner searches first. For generic things and common styles or page types it searches only + when a real example would clearly make the picture more accurate. The planner also sets a search + budget: one search-tool call and one batch judged each count as one, up to 16; without a budget + it gets two per item and at least eight. The budget is spent in the order the items are listed, + and every item searched keeps one judging step in reserve that other items' re-searches and extra + batches cannot use; a batch where no candidate downloaded makes no judging call and costs nothing. + When the budget runs out the search + stops, and items that were not searched or not fully judged are reported in the delivery notes as + over budget, not as not found. Each reference records what it is for (the subject's look, a + detail, the style, the layout or the setting) and what to take from it, and the generation + request says so for each image, so the model does not copy one of them as the base picture. A + layout reference is full of text, so it is not sent when the base picture has to stay blank for + typeset text or a chart. The judge first checks that a candidate shows the named thing itself; for + a character or mascot, official illustrations and photos of statues or figures all count; a + style, layout or setting reference only has to be a real example of its kind. When a whole batch + is rejected, it searches again under another name (for example the subject's name in its original + language) instead of paging through the same results. When the first search errors at the search + service or returns nothing, the planner's other wording is tried once as well (not when the tool + is denied). When the request is themed on a real film, + series, animation or game, it searches for the official poster or stills and uses them for the + lead's signature costume, props, palette and title lettering; faces are drawn as original + characters unless you name the actor to resemble. - **Locks character appearance across a set.** For multi-image requests, it first generates a character sheet and binds it to every image as a reference, so the same subject does not turn into two different creatures halfway through the set. - **Reviews by looking.** Each candidate is checked against its constraints by a vision model that never sees the prompt that produced it, so the review cannot be talked into agreeing. +- **Gives each step as many tool calls as the task needs.** The number of tool calls the planning, + review, correction, selection and follow-up edit steps may make is no longer fixed. Before each + call it is worked out from what that step has to open (each candidate's review sheet, the + reference sheet and so on) plus one spare call, plus a margin for the image's difficulty: the + planner rates each image easy, normal or hard, adding one, two or four calls; an image it did not + rate is rated from its spec, so eight or more pieces of text, two or more references to match or + an animation of eight frames or more count as hard. A step that already ran out of calls on an + image gets two more the next time, and each step has a ceiling. The limit written in the prompt + is the one enforced at run time, and the step's details in the timeline show how it was reached. - **Measures colour.** Red cast is detected programmatically rather than by eye, and a cast image - is redrawn instead of shipped. + is redrawn instead of shipped. When the red is in the subject itself (an orange cat, a red scarf, + a warm-lit product) and the near-neutral parts of the picture are balanced, the image is not + counted as cast. When the request itself asks for yellow, red or orange as the main colour (a + bright yellow poster) and the picture has almost no neutral areas, white balance cannot be + measured, so the image is not counted as cast and the review judges it; the prompt for such an + image no longer asks the model to avoid warm or yellow tones. When the request says white paper + or a white background, the white areas are measured on their own: if red, green and blue differ + by 4 levels or more (purplish, pinkish, yellowish or bluish white), the image fails and is + redrawn. The average over the whole picture does not show this kind of cast. +- **Does not drift over repeated edits.** Each image keeps the first candidate whose colour passed + as its colour reference. Every later edit of another candidate, in a correction round or a + follow-up, sends it along as the second image, described as a colour reference only (tone, white + balance, how white the paper is). A follow-up that asks for different colours or lighting is sent + without it, and a new reference is picked afterwards. +- **Puts in QR codes that scan.** Image models cannot draw a working QR code; they draw black and + white squares that only look like one. When your request asks for a code, the planner copies + what goes in it word for word from your request, and after each candidate is generated the app + finds the code the model drew, replaces it with a real one that follows the original perspective + and shading, and scans the final file. A code that does not scan fails the image like any other + hard constraint and sends it back for redrawing. Ask for a code without saying what goes in it + and you get a decorative one, marked as such in the delivery; add the content in the same session + and the app swaps a real code into the delivered image without generating it again. +- **Typesets long text itself.** Image models cannot write a long passage, vertical text or brush + calligraphy reliably: in a real run asking for the full 324-character *Lantingji Xu* as a + handscroll, the first round's candidates had invented characters, the delivered image left out 36 + characters including a whole sentence, and four more rounds after a follow-up still failed the + checks; the review could not proofread that much text. For text like that the app asks the model + for the picture with the writing surface left blank (the paper, the signboard, the poster panel), + then sets the text onto it with a font, character for character, following the light and texture + of the surface. A passage of 24 or more characters (or 40 in total), 10 or more characters set + vertically and 12 or more in brush lettering go this way from the first round; a short title is + drawn by the model first and switches over when the review finds the lettering wrong on every + candidate, if the plan says where the title sits. If the area is not actually blank, the image is + redrawn. Changing the text afterwards (the words, font, size, colour or punctuation) is set again + on the saved blank image in seconds, with no generation call. Pages laid out like a textbook or a + book follow typesetting conventions: text with the same role (several body paragraphs, several + labels) gets one size, and footnotes and page numbers are never larger than the body; body + paragraphs start with a two-character indent and are justified; footnote marks (①②…) in the body + and titles are set as small superscripts, while the numbers that start each footnote stay full + size; a short rule is drawn above the footnotes. Up to 16 pieces of text are set per image, and + any beyond that are listed in the delivery notes. Each piece looks for blank space only around its + own planned position: when a line or block the model drew covers that position, the piece moves + up or down to the nearest blank spot (at most two box heights). Other pieces' planned positions + and text already set count as taken, so two pieces never land on each other, and a final check + over every character keeps an image with overlapping text from being delivered. With many pieces, + the blank-space request to the model describes whole areas instead of listing coordinates (listed + percentages got drawn as a page of numbers). When the review finds text in the wrong place + (over a line, off the surface, outside the box it belongs in) and the picture itself is fine, + the text is set again on the same blank image at the corrected positions, with no generation + call and without using up a redraw round, at most twice per image. When one character needs two + colours (a literacy chart with the 氵 of 清 in orange and the 青 in dark grey), it is still set as + one whole character: the app finds the cut with the least ink between the radical and the rest of + the glyph and colours the radical's side, which can be left, right, top or bottom. If the font + joins the parts so the cut is not clean (brush fonts often do), that piece switches to the Song or + Hei font; if no font splits it, the character keeps one colour and the delivery notes say so. + Short text such as titles, labels and example words stays on one line unless one line would make + it much smaller. After planning, the app reads the pieces in layout order, top to bottom and left + to right, and compares them with the quoted text in your request; when a quote cannot be read + (for example 清 split into 氵 and 青), the planner gets the exact problem back and revises once, + and if it still does not match, the review checks those places on the image and the delivery + notes mention them. The planning prompt states which font each style really uses on this + machine: where no Kai font is installed (the macOS default), Kai uses the brush font. +- **Makes animated GIFs from one reference image.** Image models produce one still at a time, so an + animation is planned as 4 to 16 keyframes, as many as the motion needs: four to six for a blink + or raised eyebrows, ten to sixteen for turning the head away or hands coming into the frame, so + that neighbouring frames differ by one small step. The first frame is made from the reference; + each later frame is made from the frame before it, one small step on, with the reference sent + alongside to hold the identity, clothing, background and colours. Made from the reference one by + one, the frames came out inconsistent: the model drew the same "hands up to the chest" at a + different height each time. The app then assembles them into a looping GIF. The model redraws the fine texture of the whole picture on every edit and + can shift it by a pixel or two, so before assembling, the app aligns each frame to the reference, + matches its tone, keeps only the parts that really move and takes everything else from the + reference, which keeps the background still. Two fade frames are added between neighbouring + frames by default, and a last frame that is the same image as the first is merged into it, so the + loop does not stall at the seam. If you attach a picture, the frames are made from it directly. If + not, the reference is drawn first and delivered for you to confirm or change, and the frames come + only after you press 生成 GIF, or after a minute with no reply, which counts as confirming. One review turn looks at all the frames next to the reference, and + only the frames it rejects are redrawn, once. Frames that return to an earlier pose reuse that + frame's image. After the frames are drawn, the app locks each one to the reference and measures + how much changes between neighbours; where one step changes more than 9% of the picture, it adds + one in-between frame, made from the frame before with the frame after also sent, asking for the + state halfway between; it is reviewed like the rest and dropped if it fails, and a back-and-forth + motion draws it once. Every frame is shown for the same time, and the whole loop lasts as long as + the planned keyframes, so added frames do not make the pace uneven. A frame that still fails the + review after its redraw is left out of the GIF (the delivery notes say so) so the motion does not + jerk backwards; the gap it leaves gets an in-between frame from its neighbours however small the + measured change, with its original plan in the prompt, and one that succeeds takes its place. + When that fails too, its share of time goes to the frame before, so the motion keeps the planned + speed instead of moving one step in one frame and three in the next. The review also checks that + text, symbols and props from the reference are in every frame. Fades are added only between + frames that differ little: blending two very different frames gives two half-transparent figures, + which reads as flicker rather than motion. The GIF is read back from the written file to check + the frame count, size, timing and looping. +- **Turns a grid of panels into a GIF.** A picture laid out in panels (six expressions, a four-panel + comic, a nine-grid) is drawn by the model in one go, so every panel has the same character and + style, far more consistent than frames drawn one by one. After it is delivered, say "把这组图变成 + GIF": the program finds the lines or gutters between the panels, cuts each panel out without the + separators, makes them one size, aligns them by their content (the model places the subject a + little differently in each panel, which makes it jump when played as is), and joins them top to + bottom, left to right into a looping GIF. The grid itself is not redrawn. Between neighbouring panels + it adds one in-between panel each, so the motion moves step by step instead of switching at once. + The in-betweens are generated together: an Agent turn looks at the grid and writes what each panel + should become (where the hands are, how far the mouth opens), the grid is edited once into a grid + of in-betweens, two candidates at a time, and for each step the program first drops candidates that + copy a neighbour, then a review picks one from the rest; steps with no pick get a second round (two + at most). Asked only to "draw the state halfway between", the model copies the next panel almost + every time. Each in-between is aligned to the figure in its own panel (shift and scale) and locked to + that panel's background, keeping only the parts that move, so the background does not flicker. It + costs one generation call, about a minute, plus two Agent turns. By default each panel shows for + 0.7 s, the first for 1.05 s, the last for 1.4 s, and each in-between for 0.18 s (taken from the panel + before it). Panels that change scene (a four-panel story comic) get no in-betweens, nor do grids + where you say not to add frames. Every frame shares one palette, and the GIF is read back from the + written file to check it. When the request asks for a grid and a GIF from the start, the plan sets + that up and both are delivered, in-betweens included, once the grid passes review. +- **Spins things that turn around a centre in the program.** Loading rings, spinning icons, gears, + windmills and records cannot be drawn reliably frame by frame: in a twelve-dot loading ring, half + of the twelve frames put the highlight on the wrong dot. For these the plan has the model draw one + picture, and once it passes review the program turns the disc around the centre to make the + frames: a ring of evenly spaced dots turns one step per frame, so the dim dots land back on + themselves and look still while only the highlight moves, with one blended frame between steps; + a continuous spin turns a little per frame. The centre starts from the plan and is refined to the + position where the picture best matches itself after one step; nothing outside the disc moves. One + more step after the last frame gives the first, so the loop closes by construction. It takes one + generation call, and the GIF is built in under 2 s. +- **Draws data charts from the data.** Image models draw bar, line and pie charts that only look like + charts: bar heights do not match the values, axis ticks are wrong and numbers are miswritten. When a + picture needs a data chart, the app has the model draw the picture with a blank panel at that spot + (a whiteboard, a card, a screen, a page), and the program draws the axes, ticks, grid, bars, lines, + wedges, value labels and legend from the data. It then measures them back from the final file: it + locates the tick marks to fit value-to-pixel, then checks every bar end, every data point and every + wedge boundary, and does not deliver the image if anything is off by more than about 1.5 px. Bar + axes always start at 0, ticks use round steps, pie percentages are allocated by largest remainder so + they add up to exactly 100%, and value labels only ever gain trailing zeros, never get rounded. The + plan copies only the numbers you gave; ratios and totals are computed by the program, and any number + that cannot be found in your words is listed in the delivery for you to check. Panels the model draws + are often off the planned spot or lit with a gradient, so the chart follows the whole panel; a panel + with something drawn on it is sent back for a redraw. Changing numbers, adding or removing + categories, switching the chart type or the colours later redraws the chart on the saved base in a + second or two, without calling the generation endpoint. - **Fixes in a bounded loop.** At most 5 rounds; two rounds without improvement switches approach; three ends early. Content-safety rejections stop immediately instead of retrying. +- **Rides out short outages.** Gateway errors (HTTP 429 and 5xx) and dropped connections on + generation, upload and download are retried twice with a growing wait, and a review turn that + fails is retried on the same images instead of throwing them away. If a failure persists, or a + configuration error such as HTTP 401 appears mid-run, the run stops and delivers the best image + it already has, saying under the image that it stopped early. - **Compares across images.** After every image is done, it builds one side-by-side sheet and asks whether the same subject is still the same subject. It reports; it does not redraw. - **Ships an acceptance report.** Every delivery carries a `report.json` with per-image verdicts, @@ -93,7 +269,65 @@ delivers images plus a report. Useful things to know: the delivery area with the raw message, not as a silent "0 images". - **Follow up in the same session.** Sending another message edits the delivered images rather than starting over. -- **History is in the sidebar.** Each row can be rerun or deleted; deleting moves the session to +- **Say when a reference is wrong.** A follow-up such as "参考图找的不对,重新找" runs a new search + for the subjects that had no reference, or that you say are wrong, and redraws with what it + finds. If the new search finds nothing, the panel says so and does not redraw. If you have the + right picture yourself, attaching it is still the most reliable way. +- **QR codes.** Say what the code should contain, for example "二维码链接至 https://www.minimax.cn/". + The content has to appear in your own words: if the plan's content cannot be found there letter + for letter, the app does not put a code in and asks you instead. Each delivered image is marked + 二维码可扫 (replaced and scanned), 装饰码 (no content given) or 二维码没换成 (with the reason). + A follow-up that only gives or changes the content, such as "二维码换成 https://…", is applied to + the delivered image in a few seconds, with no generation call and no new review. +- **Typeset text.** An image whose text the app set is marked 文字由程序排入, with the font and the + layout under it; 文字没排上 means the area left for the text was not blank, and says why. Ask for + changes in the same session, such as "字换成宋体", "字大一点" or "加上标点". Calligraphy (classical + prose, poems, couplets, handscrolls) is set without punctuation by default, the way it was + written; say you want punctuation and it is set small beside the characters. The text is a font, + not handwriting: a character looks the same every time it appears, on an even grid. In a session + from an earlier version, asking to fix a long passage the model wrote has the model erase its + writing first, then the text is set. +- **GIFs.** Ask for a GIF, a 动图 or an animated sticker, and attach a picture if you have one. A + reference the app drew first is marked GIF 参考图 · 待确认, with the planned frames under it; press + **生成 GIF** to make the frames, or say what to change and the reference is redrawn first. The card + counts down: with no reply of any kind for a minute, the reference counts as confirmed and the frames + start by themselves, and that turn is labelled 按默认确认自动开始 in the conversation; press **先别生成** + to stop the countdown. If the app restarts while waiting, the minute starts over; a session reopened + long after its countdown ran out does not start on its own. The + delivered GIF is marked GIF · N 帧, with a numbered strip of the frames under it; both the GIF and + the reference can be downloaded. Follow-ups such as "慢一点", "第 3 帧重画", "去掉最后一帧", + "在第 2 帧后面加一帧眨眼", "只播一遍" or "别要渐变" work in the same session: changing the speed, + order, size, looping or whether frames fade or cut, or removing a frame, rebuilds the GIF in + seconds with no generation call, and redrawing or adding a frame generates only that frame. Asking + to change the subject itself (a colour, the clothes) redraws the reference and waits for you to + confirm again. The GIF is 768 px on its long edge by default (up to 1024), with one 255-colour + palette shared by every frame. +- **Spinning GIFs.** Ask for a loading ring or a spinning icon, for example "做一个十二个点的加载圈 GIF, + 512×512". The card shows the GIF, marked GIF · N 帧, with the frames of one turn under it; both the + GIF and the picture can be downloaded. Follow-ups such as "转快一点", "反过来转", "一圈 8 格" or "连续转" + rebuild it in seconds with no generation call; changing the look (colours, dot shapes) edits the + picture as usual and spins the new one. "让它转起来" works on a picture already delivered. +- **Grid GIFs.** On a delivered grid, follow up with "把这组图变成 GIF". A minute or two later the card + shows the GIF, marked GIF · N 格, with a strip of the panels numbered top to bottom, left to right + (in-betweens marked with the panel number and a plus); both the GIF and the grid can be downloaded. + To skip the wait and the in-betweens, say "直接拼,不要补帧". Follow-ups such as "每格停久一点", + "最后一格多停一会儿", "来回播", "去掉第 3 格", "换个顺序", "加渐变" or "切换时弹一下" rebuild it in seconds with + no generation call (a step that appears after removing a panel gets its own in-between); when some + steps got no in-between the card says so, and "更顺一点" tries again; "第 3 格改成生气" edits the grid as + usual, then redoes the in-betweens and rebuilds the GIF from the new grid. A picture with no + panels is reported as such and not redrawn; for a sheet with nothing between the panels (stickers + on white), say "按 2 行 3 列切" and it is cut into equal panels. +- **Data charts.** When you want a bar, horizontal bar, line, area, pie or donut chart in the picture, + put the numbers in the request ("revenue for 2021 to 2025 was 86, 120, 158, 203 and 245 万元"); if + the chart is the point of the picture and you gave no numbers, the app asks for them first. The + delivered image is marked 图表核对通过, or 图表 · 请核对数据 when some number cannot be found in your + words; the delivery details list all the data the chart uses. Follow-ups such as "2024 年改成 280", + "换成折线图", "加一年 2026 年 300", "颜色换成蓝色" or "去掉图表" work in the same session, and changing only the + chart never regenerates the picture. One picture holds up to 3 charts, each with up to 24 + categories and 6 series. +- **History is in the sidebar.** Each row can be rerun or deleted. A rerun opens a new session with + only the original request and the images you uploaded; searched references, the character sheets + and GIF reference the app drew, and the previous results are left out. Deleting moves the session to `sessions/.trash/` inside `dataDir` instead of erasing it. The app never empties that folder; delete its contents yourself to free disk space. - **Stopping a run.** The **停止** button aborts the session's Agent turns and ends the run at the @@ -122,21 +356,217 @@ submitted, and the composer says so. ## Tested environment -- MiniMax Code 3.1.12-preview.5165 on macOS 15.0.1 (Apple Silicon) - -Verified in that environment with this build (1.8.1): publishing a public link, including the -`website_deploy` approval in the page, and a drawing request in chat being handed to the panel. -Verified there with earlier builds: single-image and multi-image runs end to end, reference search -and download, character-sheet generation and cross-image comparison, the bounded fix loop, colour -measurement, session restore after a runtime restart, deletion and rerun, share page generation, -and failure reporting. - -Checked only with automated tests against a simulated Host (the Node service and the page in -headless Chrome), not in MiniMax Code: end-to-end runs with this build, the 2048 px copies in the -share page, the **停止** button, the messages after switching `session.run` or `tool:call` off, -questionnaires, and view restore after the Host reloads the page. Not verified: Windows, MiniMax -Code releases other than the one above, and any H3-Image endpoint other than the one used during -development. +- MiniMax Code 3.1.12-preview.5165 on macOS 15.0.1 (Apple Silicon), with 1.8.1 and earlier builds +- MiniMax Code 3.1.16 preview builds (earlier previews for the first runs) on macOS 15.0.1 (Apple + Silicon), with development builds of 1.9.0 + +Verified on 3.1.12-preview.5165 with 1.8.1: publishing a public link, including the `website_deploy` +approval in the page, and a drawing request in chat being handed to the panel. Verified there with +earlier builds: single-image and multi-image runs end to end, reference search and download, +character-sheet generation and cross-image comparison, the bounded fix loop, colour measurement, +session restore after a runtime restart, deletion and rerun, share page generation, and failure +reporting. + +Development builds of 1.9.0 were run in MiniMax Code, with the real planning and review turns, +from 11 to 20 November 2026: typeset text on textbook pages, literacy charts, a film poster and an +anatomy diagram; keyframe GIFs (a rain story, a running cat, a meme face, a pixel-art loop, a +loading ring); a grid of panels turned into a GIF; a QR-code business card; and two data charts. +The paragraphs below say what those runs showed and what was changed because of them. Program +spins, in-between panels and the `h3-gif` tool have not been run in MiniMax Code. + +1.9.0 adds the QR-code work. Its one run in MiniMax Code, a business card, replaced the drawn code +with a real one that scanned on three of four candidates (on the fourth the pasted code did not +scan, so the code was left as drawn), but delivered nothing: a review turn failed and a reference +upload then got HTTP 502, which led to the change to temporary failures below. Otherwise it was +checked only with automated tests: the QR encoder against the jsQR decoder (every mask, versions 1 to 40, +capacity limits), the code replacement on the two candidates of a real run whose codes did not +scan, and on synthetic codes with rotation, perspective, uneven shading, low contrast, no white +border and a tiny size; and, against a simulated Host and a simulated H3-Image endpoint, a run that +replaces the code, a decorative code followed by a follow-up that adds the content, changing the +content after a runtime restart, content the planner changed, and a code the model never drew. +**Scanning with a phone camera has not been verified**: the app's check is a software decoder. + +1.9.0 also changes the reference-image search, again checked only with automated tests. The JPEG +decoder now reads progressive files: its output matches the baseline originals byte for byte for +15 lossless `jpegtran` conversions (progressive, successive approximation, restart markers, +non-interleaved scans, all common chroma subsamplings), matches `djpeg` at 53 dB PSNR or better +on 26 test files with 4:2:0, 4:2:2, 4:4:4, 4:4:0 and grayscale subsampling, and decodes the seven +progressive candidates a real run had silently skipped. 4:1:1 files decode but differ from +`djpeg` more (34 dB, as before 1.9.0). Against a simulated search tool, Host and endpoint: +candidate numbering when a candidate cannot be decoded, the second search under another name, +addresses the model invented, a follow-up that searches again (including replacing a reference +and finding nothing), and session restore of the new fields. + +1.9.0 also changes how temporary failures are handled, checked only with automated tests against +a simulated endpoint (HTTP 502 on generation and upload, HTTP 401, a refused connection) and +simulated Agent turns that fail: the retry counts and waits, delivery of the images already made, +and the notice under the delivered image, which was also looked at in headless Chrome. + +1.9.0 also adds typesetting. It was first tried against the real H3-Image endpoint, with the +planning and review turns replaced by fixed answers: the request of a real run (the full +*Lantingji Xu* as a handscroll) gave two blank-paper images in one round, and all 324 characters +were set on both in vertical columns. A copy of that run's saved session was then asked, against +the same endpoint, for the 36 missing characters: the model erased its own writing, the blank check +rejected three of the four erased images (faint ghosts of the old strokes remained), and all 324 +characters were set on the fourth, about a minute in all. The blank check rejects all twelve images +of the earlier run, which the model had written on, and accepts every blank image that endpoint +returned. Automated tests against a simulated Host and endpoint cover starting with typesetting, +redrawing when the area is not blank, blank banners down to 5% of the image height, going back to +drawn text when the fonts lack a character, switching after the review finds the lettering wrong, a +plan that leaves out the layout, changing the font and the punctuation after a runtime restart +without a generation call, and a follow-up on a session saved by an earlier version. The marks on +the delivered image were looked at only in headless Chrome. **Not verified:** whether the real +planning turn writes the layout as asked, how the real review judges typeset images, and the +Windows fonts. + +1.9.0 also adds GIFs. They were first tested against the real H3-Image endpoint, with the +planning and review turns replaced by fixed answers: a shiba mascot was drawn first as the reference +(two candidates in 16 s), and after the confirmation eight frames were made from it (four generated +in parallel in about 17 s; two frames were the reference itself and two returned to an earlier +pose), giving a 768 × 768 GIF of 392 KB, about 40 s in all. The shiba, and an orange cat from an +earlier probe, are flat illustrations, and their frames lined up with the reference pixel for pixel +(the cat's backgrounds differed from it by 1 to 3 levels out of 255 on average). A watercolour rain +scene (6 frames) was then run in MiniMax Code, with the real planning and review turns, and behaved +differently: the model redrew the fine texture of the whole picture on every edit, so even the frame +that only turned the cat's head had 16% of its pixels off by more than 14 levels, and the two +umbrella frames were shifted by 1 to 3 pixels and enlarged by about 0.5%. The delivered GIF's +background flickered on every frame and the loop stalled for 1.2 s at the seam; the plan wrote only +each frame's new motion, so the umbrella and the cat snapped back in later frames; and the second +review ran out of tool calls before it saw any image and rejected the redrawn frame. Rebuilt from +those frames the current way, the share of pixels changing between neighbouring frames drops from +32–61% to 3–33% (only the parts that move), and the GIF shrinks from 1654 KB to 659 KB without +fades, or 1234 KB with them; the shiba set with fades is 418 KB. These GIFs decode the same in +Pillow as in the app's own decoder. Automated tests against a simulated Host and endpoint cover +frames from an uploaded reference, a reference drawn first and confirmed (also after a runtime +restart), the reference starting by itself after a minute with no reply (cancelled by a follow-up, +stopped by 先别生成 also across a restart, restarted in full after a restart mid-countdown, and not +started for a session reopened long after its countdown ran out), the review rejecting a frame, a review that saw no image, a frame that came back +unchanged, speed, order, frame removal and switching to cuts with no generation call, a first and +last frame that are the same image, redrawing and adding single frames, and changing the subject +after the GIF was made. The GIF card, the confirmation button and the countdown were looked at only in headless +Chrome. **Not verified:** whether the real planning turn, with the revised prompt, now carries +earlier changes into every later frame, and whether the real review then catches frames that snap +back. + +The colour check change in 1.9.0 was checked on images from real runs: the eight warm-lit product +photos of one run that were all judged red before now pass, as does the orange-cat reference above, +and the same reference with a red cast added (red raised by 6 to 25 percent) is still judged red. + +The data charts in 1.9.0 were first tried against the real H3-Image endpoint, with the planning and +review turns replaced by fixed answers: a coffee-shop revenue poster was drawn (a barista on the left, a whiteboard on the right with a five-year bar chart). In the first +round both candidates' whiteboards were off the planned spot and were sent back; in the second round +both left the board blank, the program drew the bar chart and measured 11 points back from the final +file with a largest error of 0.08 px, about 30 s and 4 generated images in all. The follow-up "2025 +年改成 260,换成折线图" made no generation call and gave v2 in about a second, 12 points measured, largest +error 0.08 px. Following the panel was added afterwards: of the two first-round images that were sent +back, the narrower whiteboard is now drawn on directly (0.13 px), and the one with a clear window +reflection is still sent back. Automated tests against a simulated Host and endpoint cover bar, +grouped, stacked and negative bar charts, horizontal bars, two-series lines, area, a line starting +above 0, pie, donut, a dark panel, lit and shifted whiteboards, a panel with lines on it (redrawn), a +panel too small, numbers not given by the user (flagged in the delivery), changing only numbers, +switching the type, removing the chart and redrawing the chart during a text-only change (all with no +generation call), adding a chart to a picture that had none (with the model clearing a panel first +when the spot was not blank), and further changes after two runtime restarts. Two runs in MiniMax +Code followed. A line chart of five yearly growth rates given in the request was measured back at 11 +points with a largest error of 0.06 px, and the follow-up 变成柱状图 redrew it as bars on the same +base with no generation call (0.1 px). A bar chart asked for with an uploaded picture was measured +at 12 points (0.12 px); its numbers were not in the request, and the delivery flagged them for +checking. **Not verified** beyond those two runs: whether the real planning turn copies only your +numbers and places the panel well, and how the real review judges a program-drawn chart. + +A later round of 1.9.0 changes (typesetting layout, colour, reference search, tool-call limits, GIF +smoothness, rerun) came from the typesetting and GIF runs above. The runs in MiniMax Code on 19 +November used builds that included it, but its parts were not checked there one by one. Checked on +images from real runs: a +textbook page, re-set on that run's blank base by the new rules, has both body paragraphs at one +size, two-character indents, superscript footnote marks, a rule above the footnotes and the page +number; the twelve bases of a linear-algebra page run, re-set from the original plan, now take the +text on five instead of two, none with overlapping text; 清, 情 and 请 are set whole with their +radicals coloured on that literacy chart run's base; 21 left-right and top-bottom characters split +at the right place in the Hei and Song fonts; the white-paper check rejects only the delivered image +with the purplish paper among that textbook run's four candidates; the four candidates of the +bright-yellow poster run are no longer judged red; and the tool-call records of 24 real sessions set +the limits for each step. Against a simulated Host, endpoint and search tool: one size per role, +indents, superscripts and rules, no overlapping text and moving to nearby blank space, re-setting +text only, two colours in a character and switching fonts, the check and revision after planning; +the white-paper check, warm main colours and the colour reference (also after a restart); the search +budget (no search, too small, three or more items, both finishing orders, a second wording, batches +with no judging call not counted) and reference roles; the tool-call limits of each step; GIF +frames made from the previous one, in-between frames, even timing, and frames that failed review +left out; and a rerun carrying only the request and uploaded images. **Not verified:** whether the +real planning turn writes roles, `part`, difficulty and the search budget, whether the real review +uses re-setting text only, whether a second wording finds images on the real search service, and +whether a GIF made over a dozen chained steps keeps the figure and gets accurate in-between frames. + +Grid GIFs have been run once in MiniMax Code, on a six-panel grid of a girl in a red dress: for the +follow-up "变成GIF" the edit turn wrote `from_panels` with 3 columns and 2 rows, no generation call +was made, and building the GIF took about 2 s. After alignment the head and the background did not +move by a single pixel between panels; but the default at the time added one frame 3% larger at +each switch, which pushed the whole picture outward and the head up by 9 px before snapping back +0.08 s later, so every switch looked like a jump. That bounce is now off unless asked for (the same +grid is 909 KB without it and 2119 KB with it). After that change, "重新变成gif" in the same session +had the edit turn copy every current setting back, the bounce included, which rebuilt the jumping +version again: the bounce is now added only when your words that turn mention 弹, settings copied +back unchanged do not count as changes, and settings saved by the first version have the bounce +turned off when restored. Replaying that sentence on a snapshot of the session against a simulated +Host gives a 6-frame GIF with no bounce. That grid in fact had no separator lines, its panels +drawn edge to edge, and the program had put the cuts on the screen frames and chart axes inside panels +2 and 3, 70 to 120 px off; it now cuts at the equal positions, which keeps 10% more of each panel. +With alignment now matching the upper middle of each panel and refined to a fraction of a pixel, the +head in that grid does not move between panels, and in panel 4 of the second version of another run's +six-panel grid (both hands on the head), where the head used to sit 12 px lower than in the other +panels, it is now within 1 px. The `h3-gif` tool has not been run in MiniMax Code. + +In-between panels were tested against the real H3-Image endpoint with fixed answers for the planning +and review turns (those only run in MiniMax Code). Three ways of asking were tried first: only "make +each panel the state halfway to the next", the same plus a grid with every panel replaced by the next +one, and a written description of each in-between. The first two copied the next panel in 4–6 and 1–3 +cells; with descriptions both candidates drew all 6 cells as in-betweens, so that is what is used, with +only the original grid sent. Replaying "重新变成gif" on a snapshot of the red-dress session: one +generation (2K, two candidates, 35–40 s, once 193 s while the service queued), 6 steps picked, a GIF of +12 frames and 1.7 MB, down from 2.4 MB once the in-betweens were locked to their panel's background, +which also stopped the background flickering. Another run's chibi six-panel grid got all 6 steps too, +at 831 KB. Against a simulated Host and endpoint: copied candidates rejected by the program, the review +picking letters, program picks when the review saw nothing, a second round that edits only the panel +without a pick, the card note after two failed rounds, "更顺一点" retrying, "不要补帧", speed changes with +no generation, only the new step after removing a panel, redoing in-betweens after a grid edit, no +in-betweens for a grid that changes scene, a failed planning turn, and in-betweens kept across a +restart. **Not verified:** how well the real planning turn writes the in-betweens and how well the real +review picks them. + +Program spins and the handling of dropped keyframes were tested against the real H3-Image endpoint +with fixed answers for the planning and review turns (those only run in MiniMax Code): a twelve-dot +loading ring drawn from the new planning rules (one generation, 18 s) was spun into a 24-frame, +154 KB GIF with one turn in 0.96 s, and with its twelve steps stacked the dim dots stay put while the +highlight moves one dot per step; a gradient arc spinner turns continuously over 30 frames. The +reference from the real run that dropped six of twelve frames, spun directly, keeps its dim dots +still too. For keyframes, after the "paw above the head" frame of a shiba wave was failed, an +in-between was drawn from its neighbours and its original plan, giving a 6-frame GIF at 220 ms per +frame; the same run showed the model drawing "waving outward above the head" as a paw stretched +downward, which only the real frame review can catch. + +Checked on an image from another real run: a six-panel expression grid (5472 × 3072, thin grey +lines, panels not exactly equal) is cut into 3 columns and 2 rows with no separator left in any +panel, aligned, and joined into a 768 × 650 GIF of 6 frames and 388 KB that passes the read-back +check, about 2 s from reading the image to writing the file (1.2 s of it decoding the 4K image). On +synthetic grids: thin lines, white gutters with an outer margin, framed panels, stickers on white +(with rows and columns given), a title bar on top, two panels over three, an ordinary picture (not +cut), a vertical strip, an empty last panel, and panels with large blank areas. Against a simulated +Host and endpoint: making the GIF in a follow-up with no generation call, the same when the edit turn +writes the older `start` or copies the current settings back, changing the speed and playing back and forth, dropping a panel, no rebuild +when nothing changed, rebuilding from the new grid after one panel is edited, the report when the +picture has no panels, a grid and a GIF planned from the start, further changes after a restart, and +asking for the bounce and then removing it. All six `h3-gif` commands and their exit codes on errors +were tested too. **Not verified:** whether the real planning turn writes `panel_gif` when a grid +fits, and how well grids drawn in other ways (comic layouts, irregular panels) are cut. + +Checked with 1.8.1 only with automated tests against a simulated Host (the Node service and the +page in headless Chrome), not in MiniMax Code: end-to-end runs, the 2048 px copies in the share +page, the **停止** button, the messages after switching `session.run` or `tool:call` off, +questionnaires, and view restore after the Host reloads the page. The QR marks on delivered images +and the in-between and spin notes on the GIF card were looked at only in headless Chrome against a +simulated Host and endpoint. Not verified: Windows, MiniMax Code releases other than those above, +and any H3-Image endpoint other than the one used during development. ## Data & access @@ -147,6 +577,14 @@ development. configured them in the page. This relies on shell configuration layout that is not part of any published contract. - Images you pick as references through the file picker. +- Images and GIFs an Agent passes to the bundled `h3-review`, `h3-qr` and `h3-gif` command-line + tools, and, for `h3-qr fix --content-file`, the text file holding the code's content. +- Font files, when text is typeset or a chart is drawn: read in place, never copied, changed or uploaded. On macOS + `/System/Library/Fonts/STHeiti Medium.ttc`, `/System/Library/Fonts/STHeiti Light.ttc` and + `/System/Library/Fonts/Supplemental/Songti.ttc`; on Windows `simkai.ttf`, `simsun.ttc`, + `msyh.ttc` and `simhei.ttf` in `%WINDIR%\Fonts`. The heiti and songti files are read whenever + text is typeset, brush lettering included, to fill in characters the chosen font lacks. A file + that is not there is skipped. **Files written** @@ -154,17 +592,48 @@ development. `0600`), `sessions//` (plans, images, previews, references, review sheets, deliveries, reports, the share page `share/index.html` with the larger copies it embeds in `share/large/`, `session.json`), `refcache/` (downloaded reference images, plus a one-day record of downloads - that failed), and `sessions/.trash/` (deleted sessions). + that failed), and `sessions/.trash/` (deleted sessions). When a QR code is replaced, the repaired + copy is written next to the generated image as `sessions//images/-qr.jpg` (`.png` + when the endpoint returned PNG); the generated image itself is kept unchanged. Typeset text works + the same way: the image with the text is `sessions//images/-text.jpg`, and the blank + image the model generated stays next to it, because later text changes start from it. Program-drawn + charts likewise: the image with the chart is `sessions//images/-chart.jpg`, and the + base with the blank panel stays next to it for later changes. GIF frames + are written as `sessions//images/F-r-g0.jpg` with 512 px previews in + `previews/`, a reference the app drew first is copied into `sessions//refs/` under the next + `R` number, and each delivery folder gets the GIF (`.gif`), the reference as the still + (`.jpg`) and the frame strip (`-frames.png`). A grid GIF is written as + `sessions//images/-panels.gif`, with its strip `-panels-strip.png` next to + it; the delivery folder gets the GIF, the grid itself and the strip. Candidate grids of in-betweens + are written as `sessions//images/T-r-g.jpg`, and the sheets for the planning and + review turns in `sheets/`. A spun GIF is written as + `sessions//images/-spin.gif` with its strip `-spin-strip.png`; the + delivery folder gets the GIF, the picture and the strip. - The two JSON state files — `settings.json` and `sessions//session.json` — are written atomically: a temporary file in the same directory, then `rename`. Image and HTML files are written directly. -- Outside `dataDir`, only two things write files: +- Outside `dataDir`, only four things write files: - The publish turn (see **Tools called**) copies the share page to `h3-share-publish/index.html` in this Mini App's own Agent working directory, because `website_deploy` only accepts paths inside that directory. Each publish overwrites it. - The `h3-review` command-line tool bundled for the `h3-image-review` Skill writes PNG review sheets when an Agent runs it: next to the input image for `sheet`, in the current working directory for `refs` and `grid`, or wherever `-o` points. The app itself never runs it. + - The `h3-qr` command-line tool bundled for the `h3-image-qrcode` Skill writes, when an Agent + runs it: for `fix`, the repaired image next to the input as `-qr.` (with `-2`, `-3`… + rather than overwriting an existing file) or wherever `-o` points, and never over the input + image itself; for `make`, a PNG named `qr.png` (same numbering) in the current working + directory, or wherever `-o` points. `scan` and `locate` write nothing. When `fix` cannot make + the code scan, it writes no file at all. The app itself never runs it. + - The `h3-gif` command-line tool bundled for the `h3-image-gif` Skill writes, when an Agent runs + it: for `grid`, the GIF next to the grid as `.gif`; for `slides`, next to the first image as + `-slides.gif`; for `spin`, next to the image as `-spin.gif`; for `frames`, next to the + frame folder as `.gif` (with `-2`, `-3`… + rather than overwriting), or wherever `-o` points; next to the GIF a contact sheet + `-frames.png` (or wherever `--sheet` points). `split` writes the panels as + `-panels/01.png`… (choosing another folder if that one exists), and `check` writes a sheet + only when given `--sheet`. It never overwrites an input file, and writes nothing when the GIF + fails its read-back check. The app itself never runs it. - The page keeps view state in its own browser storage: whether the sidebar is collapsed, and, saved each time the page is hidden, the open session ID, the unsent text in the composer (up to 4,000 characters) and how many unsent reference images you had picked, so a reload by the Host @@ -172,12 +641,18 @@ development. **Network** -- The H3-Image endpoint you configure. It receives your key as a bearer token, your prompts, and - any reference images you supply. There is no default: it is whatever you typed. +- The H3-Image endpoint you configure. It receives your key as a bearer token, your prompts, the + reference images you supply or the search found, and generated images sent back for edits: the + image being changed, the one used as the colour reference, a GIF's previous frame, the two + neighbouring frames when an in-between frame is added, and the whole grid when in-between panels + are made. There is no default: it is whatever you + typed. - Image URLs returned by that endpoint, which the app downloads from. -- Image URLs found by the reference-image search (see **Tools called**) or written into the plan - by the planning turn: any third-party site, over `https` or `http`. The app only downloads these - images; the search queries themselves go through the tool, not from this app. +- Image URLs found by the reference-image search (see **Tools called**), and image URLs you give in + your request or a follow-up: any third-party site, over `https` or `http`. The app downloads + these images itself and uploads them to your endpoint; it never asks the endpoint to fetch an + address. An address that the planning or follow-up turn writes but that does not appear in your + own words is ignored. The search queries themselves go through the tool, not from this app. The app refuses to download images from `169.254.0.0/16` — the link-local range where cloud instance metadata services live — and from wildcard addresses such as `0.0.0.0`, and it checks @@ -185,8 +660,8 @@ every redirect again before following it. For image URLs that come from the sear refuses localhost and private ranges (`10.*`, `172.16-31.*`, `192.168.*`, `100.64-127.*`, `fc00::/7`), including host names that resolve to them, because a third-party search result should never make the app read from your own network. Private ranges and localhost stay allowed for your -endpoint's image URLs and for image URLs written into the plan from your request, because your -endpoint may itself sit on an internal network. Each download is capped: 25 MB for a reference +endpoint's image URLs and for image URLs you give yourself, because your endpoint may itself sit on +an internal network. Each download is capped: 25 MB for a reference image, 100 MB for a generated image. **Processes spawned** @@ -218,7 +693,10 @@ Nothing is spawned at load, on a timer, or on save, and no other child process i The app declares `agentAccess: ["session.run"]` and runs Agent turns to plan, review and correct. Those turns run on the model service your MiniMax Code session is configured to use, so your prompt text, your reference images and the generated images themselves are sent there for -planning, review, correction and selection. Those turns can read and write files, run shell +planning, review, correction and selection; when screening reference candidates, so are the +titles and site names the search tool returned for them; the planning and follow-up turns also see +the names of the fonts used for typesetting on this machine (such as 宋体-简 or Ma Shan Zheng). Those +turns can read and write files, run shell commands and call MCP tools, subject to your permission rules. Every tool-permission request is shown in the page for you to answer, including the `website_deploy` request of the publish turn described under **Tools called**; the app never allows one on your behalf. It only denies, on its @@ -230,8 +708,11 @@ on. It does not declare `session.read` and cannot read your other conversations. The app declares `toolAccess: ["call"]` and calls one tool: the first installed tool whose name contains `search_images`. On the tested setup that is `connector__matrix__search_images`, the -`search_images` tool of the `matrix` Connector. It receives the subject names from the plan with a -short note on what kind of picture to find, and returns image URLs. Each call goes through your +`search_images` tool of the `matrix` Connector. It receives the search terms the plan wrote (subject +names, or a style or page type; when one finds nothing, the plan's other wording) with a short note +on what kind of picture to find, and returns image URLs. How many searches one plan or follow-up +makes is bounded by its search budget (at most 16, searches and candidate judging combined; +automatic retries after a service error are not counted). Each call goes through your permission rules. When your rules say "ask", the app shows the Host's prompt to always allow this one tool before the first search. If you switch tool calls (`tool:call`) off in the plugin details, the run continues without searched references and the page says why. @@ -243,8 +724,9 @@ The page contains your request and follow-ups, the reference images you attached online, thumbnails of the other search results, the plan, downscaled copies of every candidate and delivered image, a larger copy (long edge 2048 px) of each delivered image that opens when you click it, the review sheets, the full prompts the app sent to the model and the model's replies, -and the run log with `request_id`s and costs. The larger copies are capped at 4 MB in total; -delivered images past that cap open at preview size. Nothing is published until you press that button and then +and the run log with `request_id`s and costs, and each delivered GIF as it is. The larger copies are +capped at 4 MB in total, and the GIFs at 3 MB; delivered images past that cap open at preview size, +and a GIF past it is shown as its reference. Nothing is published until you press that button and then click **允许一次** (allow once) on the `website_deploy` request that appears in the page. A permanent line under the button says that anyone with the link can open the page and what it contains. Before the page is written, local paths are replaced with placeholders: the session @@ -261,6 +743,22 @@ from the publish turn is denied, and a questionnaire request aborts the turn. on unspecified layout. - `h3-image-review` tells the Agent how to run the bundled `h3-review` tool (see **Files written**) when you ask it to look at generated images itself. +- `h3-image-qrcode` tells the Agent how to run the bundled `h3-qr` tool (see **Files written**) + when you ask it to make the QR code in an image scan, to check whether a code scans, or to make + a standalone code: copy the content from your words, ask when you have not said it, and report a + code as working only when the tool's own scan says so. It points requests about images made in + this panel back to the panel session instead. +- `h3-image-gif` tells the Agent how to run the bundled `h3-gif` tool (see **Files written**) when + you ask it to make a GIF from a grid of panels, from several separate images or from frames it + draws with its own code, or to check a GIF: a GIF counts as done only when the tool's read-back + check passes. It points grids made in this panel back to the panel session instead. + +QR codes are made and checked entirely inside the app's Node process and those tools: no network +request, no process, no Agent turn and no tool call is involved. What goes into a code is part of +the plan, so the planning turn sees it like the rest of your request. Typesetting is the same: the +fonts are read and the text is drawn inside the Node process, and the review turn sees the typeset +image like any other candidate. Cutting grids and making GIFs also happen entirely in the Node process +and `h3-gif`, with no network request and no generation call. **Device use** @@ -277,8 +775,92 @@ above run in the Node service. The app only ever sees its own panel. - Content-safety rejections are deterministic, so the app stops at the first one instead of retrying. Change the request itself and send it again. - The reference-image search depends on the search tool and inherits its availability. When the - tool rejects or fails a query, that subject gets no searched reference; attach one yourself. + tool rejects or fails a query, that subject gets no searched reference; attach one yourself. When + the search budget runs out, later items are not searched or not fully judged, and the delivery + notes say so. +- Reference candidates in WebP, or in arithmetic-coded, lossless or CMYK JPEG, cannot be decoded. + They are skipped, and the timeline says why instead of counting them as rejected. +- QR codes: the app finds a code by its three corner finder squares, so a code the model drew + obscured, strongly tilted, stylised or without finder squares is not found. The image is then + redrawn with a stricter instruction, at most twice without review; after that it goes through + the normal review and fix loop, and if no candidate's code could be replaced, the best image is + delivered marked 二维码没换成 with the reason. Only dark-on-light codes are made and checked; + most phone cameras cannot read inverted ones. Each image holds at most three codes. When a code + is replaced, the whole image is re-encoded once as a quality-93 JPEG (PNG stays PNG). The check + is a software decoder (jsQR) reading the final file from several crops and at about 2 pixels per + module; it is a strong signal, not a guarantee that every phone reads the printed result. +- Typeset text: the blank check looks for an evenly coloured area, so a strongly textured surface + (wood grain, brick, patterned cloth) can be judged not blank; after two redraws without review the + image goes through the normal review and fix loop and is delivered marked 文字没排上. When the + model erases its own writing, it often leaves ghosts of the old strokes, so expect extra generation + calls before one comes back clean. Text is set flat and upright inside a rectangle, so on a tilted + or curved sign it looks pasted on. The two brush + fonts are bundled; 宋体 and 黑体 come from the system on macOS and Windows, 楷体 from the system on + Windows only (the macOS 楷体 is a download on demand, so the bundled brush font is used instead), + and on Linux every style falls back to the bundled brush font. Fonts with CFF outlines (most + `.otf` files) cannot be read. When no available font has a character, the app goes back to letting + the model draw the text. The image is re-encoded once as a quality-93 JPEG (PNG stays PNG). + Two colours in one character need one cut on the left, right, top or bottom: characters whose + radical wraps around the rest (这, 国, 同) cannot be split and keep one colour. The check after + planning compares only the text inside quotation marks in your request. +- GIFs are keyframe animations, not video. Each frame is made from the one before it, so frames are + drawn one at a time, about two minutes for eight; over a dozen steps the figure can slowly drift, + held back by sending the reference every time and locking the background and tone when + assembling, and small details in the moving parts (the folds of a scarf, fur texture, the face) + still change a little from frame to frame. After one frame in the middle is redrawn on its own, + the frames after it still continue from the old one, and the join relies on the in-between frame + the app adds when it measures a big step. Fades are added only between frames that differ little; + bigger changes cut directly. Camera moves or a changing background are out of reach: a frame that changes everywhere is not + locked to the reference. A WebP reference cannot be used, because the app cannot decode it. GIF + has at most 256 colours: photographic frames with soft gradients show some banding (they get an + ordered dither; flat illustrations do not). +- A program spin only turns in the picture plane: a coin flipping or a globe turning on its axis is + out of reach. The whole disc around the centre turns, background included, so the background there + must be plain or a gradient out from the centre (the plan asks for that); a pattern inside the disc + shows as turning. A stepped spin needs evenly spaced dots or spokes: drawn unevenly, the dim dots + wobble by a pixel or two each frame. A continuous spin does not refine the centre and uses the plan's. +- A grid GIF switches from panel to panel; it is not smooth motion. For a blink or a wave, use the + GIF made from a reference above. Panels are found by their lines or gutters, so a sheet with + nothing between the panels (stickers on white) or with slanted or overlapping panels (comic layouts + that break the frame) needs you to say how many rows and columns, and is then cut into equal parts; + panels drawn edge to edge with no separator are cut into equal parts too, so an unequal layout with + no separators is not cut correctly. Panels are aligned by their upper middle (the subject's head + and face) and only shifted, by up to 4% of their size, so a subject drawn at different sizes or + with its head elsewhere stays misaligned, and the background, drawn anew in each panel, can still + be off by a pixel or two. In-between panels are drawn by the model and can be wrong (hands in the + wrong place, a copy of a neighbour); when the program and the review find nothing usable for a step, + that step cuts straight through. The drawing never matches the panels exactly, and an in-between + keeps its own panel's background, so the background (a chart on a screen, say) still switches at once + at the next panel. With all in-betweens the GIF has twice the frames and roughly twice the size. + Without them the expression and the hands switch at once; with fades, two very different panels + blend into two half-transparent pairs of hands. The bounce on + each switch is added only when you ask for it: it costs one full frame per panel, roughly doubles + the file, and makes the whole picture grow and settle back at every switch. +- Data charts are flat charts drawn by the program: the lines and text are clean print-style, not hand + drawn, and they do not follow the panel's perspective, so the panel has to face the camera (the plan + asks for that). Bar, horizontal bar, line, area, pie, donut and stacked bar charts are supported; + scatter, radar, map and table charts are not. Value labels in one chart all keep the same number of + decimals (2185.8 next to 3191.43 is written 2185.80). A number that cannot be found in your words is + still drawn and only flagged in the delivery; when the data came from a table in an image, the + program cannot check it against the source. + +## Third-party code + +`miniapp/node/vendor/jsqr/jsQR.cjs` is the `dist/jsQR.js` file of [jsQR](https://github.com/cozmo/jsQR) +1.4.0 by Cosmo Wolfe and Jefff Nelson, copied unchanged except for the `.cjs` extension, under the +Apache License 2.0; the full licence text is in `miniapp/node/vendor/jsqr/LICENSE`. The QR encoder +(`miniapp/node/lib/qrcode.mjs`) is written for this package. + +`miniapp/node/vendor/fonts/MaShanZheng-Regular.ttf` (Ma Shan Zheng, copyright 2018 The Ma Shan Zheng +Project Authors) and `miniapp/node/vendor/fonts/ZhiMangXing-Regular.ttf` (Zhi Mang Xing, copyright +2018 The Zhi Mang Xing Project Authors) are the Google Fonts files, unchanged, under the SIL Open +Font License 1.1; the licence texts are `OFL-MaShanZheng.txt` and `OFL-ZhiMangXing.txt` next to +them. Together they add about 9.9 MB to the package. The font reader (`miniapp/node/lib/font.mjs`) +and the typesetter (`textlayout.mjs`, `textoverlay.mjs`) are written for this package, as are the GIF +encoder and decoder (`miniapp/node/lib/gif.mjs`), the grid cutter (`panels.mjs`), the spinner +(`motion.mjs`) and the GIF builder (`gifbuild.mjs`). ## License -MIT. See [LICENSE](LICENSE). +MIT. See [LICENSE](LICENSE). The bundled jsQR is Apache-2.0 and the bundled fonts are OFL-1.1; see +**Third-party code**. diff --git a/plugins/liddalidd/h3-image-executor/README.zh-CN.md b/plugins/liddalidd/h3-image-executor/README.zh-CN.md index 09d6bdc7..2372cd7d 100644 --- a/plugins/liddalidd/h3-image-executor/README.zh-CN.md +++ b/plugins/liddalidd/h3-image-executor/README.zh-CN.md @@ -13,14 +13,103 @@ ## 它做什么 - **先规划再动笔。** 规划节点把一句话拆成分镜:每张图一份规格,各自的画幅、硬约束和偏好。 -- **自己找参考图。** 当画面里的主体必须长成特定样子时,它会去检索参考图、下载,并判定候选能不能用。 +- **自己找参考图。** 要不要检索、搜几项、肯花多少次,由规划按这次需求自己判断,不是每次都搜。需求里出现专有名词或 + 特定称谓(真人、角色、品牌和产品、logo、车型、地标、具体的作品,或者不确定长什么样的冷门叫法)时,规划会先去检索; + 通用的东西、常见的画风和版面类型,有把握画对就不搜,觉得参考一张真实样例能明显更准时再搜。规划同时给一个检索预算 + (搜图一次、判定一批候选各算 1 次,最多 16 次,没写时按每项 2 次、至少 8 次),按检索项的顺序花,每搜一项都给它留一次判定, + 别的项换说法重搜、多判几批都不会占用这一次;候选一张都没下载下来那一批没调用判定,不算次数。花完就停,没搜或没判完的项在交付说明里照实写「超出检索预算」,不说成没找到。每张参考图都记着用途(主体外观、局部、画风、 + 版式、场景)和要取它什么,生图时逐张告诉模型,免得把某一张当底图照搬;底图要留白给程序排字、画图表的图, + 版式参考图(上面全是字)不送进生图。判定先看候选是不是这个东西本身;角色和吉祥物的官方插画、雕像或手办照片都算; + 画风、版式、场景参考只要是这一类的真实样例就能用。一整批都被否掉时, + 它会换一种说法(比如主体在原产地语言里的名字)重新搜,而不是在同一批结果里往下翻;第一次检索搜图服务就报错、 + 或者一张都没回的,也用规划给的另一种说法再搜一次(工具被拒的不再试)。 + 点名一部真实存在的作品(电影、剧集、动画、游戏)当主题时,会检索它的官方海报或剧照,参考主角的经典造型、 + 标志性道具、配色和片名字体;人物的脸照原创画,你点名要像某位演员时除外。 - **多图锁定角色外观。** 多图任务会先生成一张角色设定图,并把它绑到每一张图上作为参考, 避免同一个主体画到一半变成另一只。 - **看图评审。** 每张候选都由视觉模型按约束逐条核对,而且评审看不到送进模型的那段 prompt, 不会被 prompt 说服。 -- **量色偏。** 偏红由程序实测判定而不是靠眼睛看,判偏红的图会重画而不是直接交付。 +- **工具调用次数按难度给。** 规划、评审、修正、选优、追问编辑这些节点每次能调用几次工具,不再写死,而是每次调用前现算: + 这一步必须打开的图(每个候选的看图材料、参考图材料……)加 1 次余量,再按这张图的难度加(规划判简单、一般、难, + 分别加 1、2、4 次;没判就按规格推断,比如 8 段以上的文字、要对照两张以上参考图、8 帧以上的动图算难)。 + 某个节点在这张图上撞过上限,下一次再多给 2 次;每个节点另有封顶。提示词里写的上限和运行时执行的是同一个数, + 时间线的节点详情里写明这个数是怎么算出来的。 +- **量色偏。** 偏红由程序实测判定而不是靠眼睛看,判偏红的图会重画而不是直接交付。红的是主体本身(橘猫、红围巾、 + 暖光下的产品)、而画面里接近中性的地方颜色是正的,不算偏红。需求要的主色本来就是黄、红、橙(明黄底的海报), + 画面里又几乎没有中性色时,程序判不了白平衡,不算偏红,交给评审看;这类图的 prompt 也不再要求「不要偏暖发黄」。 + 需求写明白色纸张、白底时,程序单独量白色的地方:红绿蓝相差 4 级以上(发紫、发粉、发黄、发蓝)就判不通过、重画, + 整图均值看不出这种偏色。 +- **多轮编辑不越改越偏。** 每张图记下第一张颜色合格的候选当色彩基准。之后每次在别的候选上编辑(修正轮、追问), + 都把它当第 2 张图一起送进去,说明只用来对照色调、白平衡和纸面白度。追问里明确要改颜色、光线时这一次不带, + 改完以后重新选。 +- **放能扫的二维码。** 生图模型画不出能扫的二维码,它画的只是长得像码的黑白方块。需求里要码时, + 规划节点把码里放的内容从你的原话里逐字抄下;每张候选出图后,程序找到模型画的码,换成跟随原来透视和明暗的 + 真码,再从最终文件扫码验证。扫不出就和别的硬约束一样判不通过、打回重画。要码却没说放什么,会照常出图, + 交付时注明是装饰码;你在同一会话里补一句内容,程序直接在交付图上换成真码,不重新生图。 +- **大段文字由程序排版。** 生图模型写不好长段文字、竖排和毛笔书法:一次真实任务要的是 324 字的《兰亭集序》全文长卷, + 第一轮的候选有编出来的字,交付的那张漏了 36 个字(含一整句),追问补字后又跑了 4 轮仍没过检查;评审也没法逐字核对 + 这么多字。遇到这类文字,应用让模型只画一张把载体留空的图(纸面、招牌、海报底板),再由程序用字体把文字逐字排上去, + 跟着载体原来的明暗和纹理走。一段 24 字以上(或合计 40 字以上)、竖排 10 字以上、毛笔字 12 字以上的文字第一轮就走 + 这条路;短标题先让模型直接画,评审判出每张候选的字都写错了、且规划写明了标题的位置时再切过来。预留的地方其实没留空, + 就打回重画。之后改字(内容、字体、大小、颜色、标点)直接在保存下来的无字底图上重排,几秒钟就好,不调用生图接口。 + 排课本、书刊这类版面时按排版规矩来:同一角色的文字(几段正文、几条标签)统一成同一个字号,脚注、页码不比正文大; + 正文首行空两格、两端对齐;正文和标题里的脚注符号(①②…)排成右上角的小字,脚注区每条开头的序号保持正常大小; + 脚注上方画一条短分隔线。一张图最多排 16 段文字,超出的段落如实列在交付说明里。 + 每段字只在自己的规划位置附近找空白:规划位置被模型画的线、色块压住时,就近上下挪开(最多两个框高); + 别的段的规划位置和已经排好的字都算占用,两段字不会排到一起,程序最后还会逐字查一遍,叠了就不交付。 + 段落多时,给模型的留白要求按整片区域描述,不逐段写坐标(逐段写的百分数会被模型画成一页数字)。 + 评审判出字的位置不对(压线、出界、该在色块里却在外面)而底图本身没问题时,只在同一张底图上按新位置重新排字, + 不调生图接口,也不占重画的轮次,每张图最多两次。 + 一个字里要两种颜色时(识字挂图:「清」的「氵」橙色、「青」深灰),字照样作为完整的一个字排,程序在字形上找偏旁和 + 其余部分之间墨最少的一条切线,切线靠偏旁那一侧上另一种颜色;偏旁在左、右、上、下都行。字体里部件连笔切不干净时 + (毛笔字常这样),这一层自动换宋体或黑体;都切不开就整字一种颜色,交付说明里照实写。标题、标签、示例词这类短文字 + 默认排一行,排一行字号小得太多才换行。规划完程序先把文字层按位置从上到下、从左到右读一遍,和你原话引号里的文字对照, + 读不出来(比如把「清」拆成了「氵」「青」两块)就把具体问题交回规划改一次,改过还是对不上,评审会按图核对,交付说明也会写明。 + 规划提示词里写明了本机各书体实际用的字体:没有楷体字体的机器上(macOS 默认没有),「楷体」用的是毛笔楷书。 +- **用一张参考图做 GIF 动图。** 生图模型一次只出一张静态图,动图按 4–16 个关键帧来规划,帧数按动作幅度定:眨眼、挑眉 + 这类小动作 4–6 帧,转头、手从画面外抬进来这类大动作 10–16 帧,相邻两帧只差一小步。第一帧从参考图改, + 后面每一帧在前一帧上接着改一小步,同时把参考图一起送进去,锁住身份、服装、背景和颜色:每一帧单独从参考图改的话, + 模型对同一句「双手抬到胸口」每次画得都不一样,手一会儿高一会儿低。最后由程序拼成循环播放的 GIF。 + 模型每改一帧都会把整幅画的细纹理重画一遍,画面还会错开一两个像素,所以合成时程序先把每一帧对齐参考图、校正色调, + 只留下真正在动的部位,其余一律用参考图的像素,背景不会闪;相邻两帧之间默认补两帧渐变,首尾是同一张图时并成一帧, + 循环接缝处不会卡一下。你给了图,就直接照这张图出帧;没给,先画一张参考图交给你确认或修改,点「生成 GIF」之后才出帧, + 1 分钟内没有回应就按默认确认、自动开始出帧。 + 一次评审对照参考图看全部帧,只重画判了不合格的帧,最多一次。回到前面某个姿态的帧直接用那一帧的图。 + 帧画完之后,程序把每一帧锁回参考图、量相邻两帧变了多少:一步变了超过画面的 9% 的地方补 1 帧过渡,把前一帧和 + 后一帧都送进去,以前一帧为底画两者正中间的状态,评审照常核对,没过评审的不用,来回往复的动作只画一次。 + 每一帧停一样久,整组时长按规划的关键帧数算,补了帧也不会忽快忽慢。重画一次还没过评审的帧不放进 GIF(交付说明里写明), + 免得动作往反方向走一下;它留下的空当照样按前后两帧补一帧过渡(不管量出来变了多少),提示词里写上它原来的计划, + 补上的就顶替它的位置。补不上时它的那一份时间留给前一帧,动作照计划的速度走,不会一帧跳一步、一帧跳三步。 + 评审也会核对参考图里有的文字、符号、道具每一帧都在。渐变只补在差得不多的两帧之间: + 差得多的两帧叠在一起是两个半透明的人影,看起来是闪而不是动。GIF 写出去之后再读回来,核对帧数、尺寸、每帧时长和是否循环。 +- **分格组图切开拼成 GIF。** 一张图里排好的几格(六格表情、四格漫画、九宫格)是模型一次画出来的,同一个角色、同一套画风, + 比一帧一帧分开画一致得多。交付之后说「把这组图变成 GIF」,程序找出格子之间的分隔线或留白,把每一格切开、修掉分隔线, + 统一成同样大小,再按画面对齐(模型在每一格里画的主体位置差一点,原样播主体会一跳一跳的),从上到下、从左到右拼成循环的 GIF。 + 组图本身不重画。相邻两格之间会各补一格中间状态(过渡格),动作一步一步地走,不是一下子换掉:过渡格一起生成, + 先由 Agent 看着组图写每一格要改成什么样(手抬到哪、嘴张多大),再在原组图上改一次,一次出 2 张候选;每一步先由程序筛掉照抄了 + 前后格的,再由评审从剩下的候选里挑一格,没挑到的步子再出一轮(最多两轮)。只说「画两格中间的样子」的话,模型几乎都是照抄下一格。 + 过渡格对齐到它所在那一格的人物上(平移和缩放),背景用那一格的像素,只留下真正在动的部位,背景不会闪。 + 要多一次生图,约 1 分钟,加上两次 Agent 轮次。默认每格停 0.7 秒,第一格 1.05 秒、最后一格 1.4 秒,过渡格 0.18 秒(从前一格的时间里扣)。 + 格与格之间换了场景(讲故事的四格漫画)不补;你说「不要补帧」也不补。所有帧共用一套调色板,写完从文件读回来核对。 + 需求里一开始就说要组图再做成 GIF,规划会直接这么安排,组图评审通过后补好过渡格一起交付。 +- **绕中心转的动画由程序转出来。** 加载圈、转圈的图标、齿轮、风车、唱片这类动作,让模型一帧一帧改画不准:十二个点的加载圈, + 十二帧里有一半亮点落错了格子。这类需求规划只让模型画一张图,评审通过后由程序按圆心把那一块圆盘转出各帧:一格一格跳的 + (圆环上等距的点)每帧转一格,暗的点正好落回原来的位置、看起来不动,只有亮点在跑,两格之间叠一帧渐变;连续转的每帧转一点。 + 圆心先按规划给的位置,再找「转一格之后和自己最重合」的位置;圆盘外面一个像素都不动。最后一帧再转一格就是第一帧,首尾天然接得上。 + 只调一次生图接口,转出 GIF 不到 2 秒。 +- **数据图表按数据画准。** 生图模型画柱状图、折线图、饼图时只会画「像图表的东西」:柱子高矮和数值对不上,刻度乱标, + 数字写错。图里要放数据图表时,应用让模型只画一张在那个位置留出空白面板的图(白板、卡片、屏幕、纸面),坐标轴、刻度、 + 网格、柱子、折线、扇形、数值标注和图例都由程序按数据画上去,再从最终文件里量回来核对:先量出刻度线拟合「数值→像素」, + 再检查每根柱子的端点、每个数据点、每个扇形的边界,偏差超过约 1.5 像素就不交付。柱状图和条形图的数值轴一律从 0 起, + 刻度取整齐的间隔,饼图的百分比按最大余数法分配、加起来正好 100%,数值标注只补零不四舍五入。规划只抄你给的数, + 比例、合计由程序算;图里有数在你的原话里找不到,交付时会列出来请你核对。模型画的面板常常和规划的位置对不齐、 + 或者带着窗光的明暗渐变,图表跟着整块面板画;面板上画了别的东西就打回重画。之后改数、加减类目、换图表类型、 + 换颜色,直接在保存下来的底图上重画,一两秒就好,不调用生图接口。 - **有限次修正。** 最多 5 轮;连续 2 轮没改善就换改法;连续 3 轮提前收手; 内容安全审核拒绝会立即停止,不再重试。 +- **扛得住短暂的服务故障。** 生图、上传、下载遇到网关错误(HTTP 429、5xx)或连接断开时,会等一会儿再试两次, + 等待逐次拉长;评审回合失败时等一会儿重评同一批图,不把它们扔掉重画。故障一直不恢复,或者中途出现 HTTP 401 + 这类配置错误时,就停下来交付手上已有的最好的图,并在图下面写明是中途停下的。 - **跨图对比。** 所有图出完后拼一张并排对比图,判断「同一个主体是不是同一个东西」。只报告,不自动重画。 - **交付验收报告。** 每次交付都带一份 `report.json`:逐图判定、每次生图的 `request_id`、成本, 以及它没能验证的部分。 @@ -75,7 +164,40 @@ export MINIMAX_BASE_URL="https://..." - **失败的图会说清原因。** 接口报错(包括内容安全审核拒绝)会带着原始报文出现在交付区, 而不是一句没有下文的「0 张」。 - **可以在同一会话里追问。** 再发一条消息是在已交付的图上继续改,而不是从头再来。 -- **历史在侧边栏。** 每条可以重跑或删除;删除是把会话移进 `dataDir` 下的 `sessions/.trash/`,不是抹掉。 +- **参考图不对就直说。** 追问「参考图找的不对,重新找」会给还没有参考图、或者你说不对的主体重新检索, + 用找到的图重画;重新检索什么都没找到时,面板会如实说明,不会白画一遍。你手上有对的图时,直接上传仍然最可靠。 +- **二维码。** 直接说码里放什么,例如「二维码链接至 https://www.minimax.cn/」。内容必须出现在你自己的话里: + 规划给的内容在原话里逐字找不到时,不贴码,改为在交付里问你。每张交付图会标「二维码可扫」(已换成真码并扫码通过)、 + 「装饰码」(没给内容)或「二维码没换成」(附原因)。只补或只换内容的追问(如「二维码换成 https://…」) + 几秒钟就在交付图上换好,不调用生图接口,也不重新评审。 +- **程序排的字。** 文字由程序排上去的图会标「文字由程序排入」,下面写着字体和排法;「文字没排上」表示预留给文字的 + 地方没留空,并写明原因。要改就在同一会话里说,例如「字换成宋体」「字大一点」「加上标点」。书法作品(古文、诗词、 + 对联、手卷)默认不写标点,和古人的写法一样;你说要标点,就缩小写在字旁。排上去的是字体不是手写:同一个字每次 + 出现都长得一样,排在整齐的格子里。旧版本留下的会话里,要改模型写的长段文字时,会先让模型把字擦掉,再由程序排字。 +- **GIF 动图。** 说要 GIF、动图或会动的表情包,有图就附上。应用先画出来的参考图标「GIF 参考图 · 待确认」,下面写着计划的 + 各帧;点「生成 GIF」开始出帧,或者直接说要改哪里,先改参考图。卡片上有倒计时:1 分钟内没有任何回应,就按默认确认、 + 自动开始出帧,这一轮在对话里标着「按默认确认自动开始」;不想自动开始就点「先别生成」。等待期间应用重启,倒计时重新 + 计满 1 分钟;应用关了很久再打开,不会替你开始。交付的 GIF 标「GIF · N 帧」,下面是带编号的帧条, + GIF 和参考图都能下载。同一会话里可以接着说「慢一点」「第 3 帧重画」「去掉最后一帧」「在第 2 帧后面加一帧眨眼」「只播一遍」 + 「别要渐变」:调快慢、顺序、尺寸、是否循环、帧和帧之间渐变还是直接切换,或者删帧,几秒钟重新合成,不调生图;重画或加帧 + 只画那一帧。要改主体本身(换颜色、换衣服),会先改参考图,再等你确认一次。GIF 默认长边 768 像素(最大 1024), + 所有帧共用一套 255 色的调色板。 +- **转圈的 GIF。** 说要加载圈、转圈的图标就行,例如「做一个十二个点的加载圈 GIF,512×512」。交付卡片上放的是 GIF,标「GIF · N 帧」, + 下面是一圈里的各帧,GIF 和原图都能下载。接着可以说「转快一点」「反过来转」「一圈 8 格」「连续转」:几秒钟重新转,不调生图; + 要改样子(换颜色、换点的形状)照常改图,改完按新图重新转。一张已经画好的图,说「让它转起来」也行。 +- **组图拼 GIF。** 交付的组图上接着说「把这组图变成 GIF」,一两分钟后交付卡片上放的是 GIF,标「GIF · N 格」,下面是带格子编号的帧条 + (从上到下、从左到右数,补的过渡格标「格子号+」),GIF 和组图都能下载。不想等、也不要过渡格,就说「直接拼,不要补帧」。 + 接着可以说「每格停久一点」「最后一格多停一会儿」「来回播」「去掉第 3 格」「换个顺序」「加渐变」「切换时弹一下」,几秒钟重新拼, + 不调生图(去掉一格后多出来的那一步会补一格过渡);有几步没补上过渡格时卡片上会写,说「更顺一点」再补一次; + 说「第 3 格改成生气」会照常改组图,改完按新的组图重新补过渡格、重新拼。 + 图里没有分格时会照实说切不出格子,不会重画;白底贴纸这种格子之间什么都没有的图,说「按 2 行 3 列切」就按等分切。 +- **数据图表。** 要图里有柱状图、条形图、折线图、面积图、饼图或环形图时,把数直接写在需求里(「2021 到 2025 年营业额 + 分别是 86、120、158、203、245 万元」),没给数而图表又是重点时,应用会先问你要。交付图标「图表核对通过」, + 有数在你的原话里找不到时标「图表 · 请核对数据」;「交付详情」里列出图表用的全部数据。同一会话里可以接着说 + 「2024 年改成 280」「换成折线图」「加一年 2026 年 300」「颜色换成蓝色」「去掉图表」:只改图表时不重新生图。 + 一张图最多 3 张图表,每张最多 24 个类目、6 个系列。 +- **历史在侧边栏。** 每条可以重跑或删除。重跑开一个新会话,只带原来的需求和你当时手动上传的图;检索来的参考图、 + 程序画的角色基准图和 GIF 参考图、上一次的结果都不带。删除是把会话移进 `dataDir` 下的 `sessions/.trash/`,不是抹掉。 应用从不清空这个目录,要释放磁盘空间请自己删除里面的内容。 - **停止任务。** 「停止」按钮会中止这个会话的 Agent 轮次,并在下一步结束任务。已经发给接口的 生图请求仍会跑完,但之后不会再启动新的步骤。 @@ -99,17 +221,128 @@ export MINIMAX_BASE_URL="https://..." ## 已验证环境 -- macOS 15.0.1(Apple Silicon)上的 MiniMax Code 3.1.12-preview.5165 - -用当前版本(1.8.1)在该环境下验证过:发布公开链接(含页面上的 `website_deploy` 确认)、在聊天里说 -画图需求会交给面板。用更早的版本在该环境下验证过:单图与多图任务端到端跑通、参考图检索与下载、 -角色设定图生成与跨图对比、有限次修正循环、色偏实测、运行时重启后会话恢复、删除与重跑、分享页生成、 -失败信息透传。 - -只用模拟 Host 的自动化测试(Node 服务,加上无头 Chrome 里的页面)验证过、没有在 MiniMax Code 里实测的: -当前版本的端到端出图、分享页里 2048px 的大图、「停止」按钮、关闭 `session.run` 或 `tool:call` 后的提示、 -问卷、Host 重新加载页面后的视图恢复。未验证:Windows、除上述版本外的 MiniMax Code、以及开发时所用 -之外的任何 H3-Image 接口。 +- macOS 15.0.1(Apple Silicon)上的 MiniMax Code 3.1.12-preview.5165:1.8.1 和更早的版本 +- macOS 15.0.1(Apple Silicon)上的 MiniMax Code 3.1.16 预览版(最早几次是更早的预览版):1.9.0 的开发版 + +用 1.8.1 在 3.1.12-preview.5165 上验证过:发布公开链接(含页面上的 `website_deploy` 确认)、在聊天里说画图需求会交给面板。 +用更早的版本在该环境下验证过:单图与多图任务端到端跑通、参考图检索与下载、角色设定图生成与跨图对比、 +有限次修正循环、色偏实测、运行时重启后会话恢复、删除与重跑、分享页生成、失败信息透传。 + +1.9.0 的开发版在 2026 年 11 月 11 日到 20 日之间在 MiniMax Code 里实跑过,规划和评审都是真实轮次:课本内页、识字挂图、 +电影海报和人体结构原理图上的程序排字;关键帧 GIF(雨天的故事、奔跑的小猫、表情包、像素风循环动画、加载圈); +交付的组图拼成 GIF;二维码电子名片;两张数据图表。下面各段写了这些任务暴露了什么问题、为此改了什么。 +程序转圈、过渡格和 `h3-gif` 工具还没在 MiniMax Code 里跑过。 + +1.9.0 新增了二维码相关的改动。它在 MiniMax Code 里跑过一次(一张电子名片):4 张候选里有 3 张把模型画的码换成了真码、 +扫码通过,第 4 张贴上真码后扫不出来,保留了原样;但这次任务一张图也没交付:一轮评审失败,接着参考图上传遇到 HTTP 502, +下面对临时故障的改动就是因此做的。除此之外只用自动化测试验证过: +二维码编码器对 jsQR 解码器的往返(全部 8 种掩码、版本 1 到 40、容量边界);在一次真实任务里两张扫不出的候选上 +换码,以及在旋转、透视、明暗不均、低对比度、没有白边、尺寸过小的合成假码上换码;在模拟的 Host 和模拟的 H3-Image +接口上跑:换码出图、先出装饰码再追问补内容、运行时重启后换内容、规划改写了内容、模型根本没画码。 +**没有用手机摄像头实际扫过**:应用的验证用的是软件解码器。 + +1.9.0 还改了参考图检索,同样只用自动化测试验证过。JPEG 解码器现在能读渐进式文件:15 个用 `jpegtran` +无损转换的变体(渐进式、逐次逼近、restart 标记、非交织扫描、各种常见色度抽样)解出来和 baseline 原图逐字节一致; +26 个 4:2:0、4:2:2、4:4:4、4:4:0 和灰度测试文件和 `djpeg` 的输出相比,PSNR 都在 53 dB 以上;一次真实任务里被悄悄跳过的 +7 张渐进式候选全部解得开。4:1:1 的文件能解,但和 `djpeg` 差得多一些(34 dB,和 1.9.0 之前一样)。在模拟的搜图工具、 +Host 和接口上测过:有候选解不开时的编号、换说法重搜、模型编出来的地址、追问里重新检索(含替换参考图、什么都没找到), +以及新字段在重启后的恢复。 + +1.9.0 还改了临时故障的处理,同样只用自动化测试验证过:在模拟接口上制造生图和上传的 HTTP 502、HTTP 401、 +连接被拒,以及模拟失败的 Agent 回合,核对重试次数和等待、已有图的交付,以及交付图下面的提示(这一行也在无头 Chrome 里看过)。 + +1.9.0 还新增了程序排字。先在真实的 H3-Image 接口上、把规划和评审轮次换成固定答复, +跑了一次真实任务的需求(《兰亭集序》全文长卷):一轮出了两张空白宣纸的底图,两张都把 324 个字竖排进去了。又把那次任务 +保存的会话复制一份,在同一个接口上追问补齐漏掉的 36 个字:模型先擦掉自己写的字,擦完的 4 张里有 3 张还留着旧笔画的 +淡淡痕迹,被空白检测打回,第 4 张排进了全部 324 个字,前后约一分钟。空白检测把那次任务里模型写了字的 12 张图全部 +判成没留空,这个接口出的空白图都能排上。在模拟的 Host 和接口上测过:一开始就走程序排字、没留空时重画、只占画面高度 +5% 的空白横幅、字体缺字时退回让模型写、评审判出字写错后切过来、规划漏写排法、运行时重启后换字体和加标点(不调生图)、 +旧版本会话的追问。交付图上的标记只在无头 Chrome 里看过。**未验证:**真实的规划轮次会不会照要求写排法、 +真实评审怎么判排了字的图、Windows 上的字体。 + +1.9.0 还新增了 GIF 动图。先在真实的 H3-Image 接口上、把规划和评审轮次换成固定答复测过: +先画了一张柴犬吉祥物当参考图(两张候选,16 秒),确认之后从它出了 8 帧(并行生成 4 帧,约 17 秒;2 帧就是参考图本身, +2 帧回到前面的姿态),合成一张 768 × 768、392 KB 的 GIF,前后约 40 秒。柴犬和更早一次试验的橘猫都是平涂插画, +各帧和参考图逐像素对齐(橘猫实测背景只差 1–3 级,满级 255)。之后在 MiniMax Code 里实跑了一张水彩雨景(6 帧, +真实的规划和评审),情况不一样:模型每改一帧都重画了整幅细纹理,只改猫头的一帧也有 16% 的像素差超过 14 级, +改伞的两帧错开 1–3 像素、放大约 0.5%,交付的 GIF 背景每帧都在闪、接缝处卡 1.2 秒;规划只写了每一帧新加的动作, +伞和猫在后面的帧里弹回了原样;评审第二次因为工具次数用完没看到图,把重画的那一帧判成了不合格。用那次的帧按现在的做法 +重新合成:相邻两帧之间变化的像素从 32–61% 降到 3–33%(只剩真正在动的部位),不加渐变时 GIF 从 1654 KB 降到 659 KB, +加上渐变是 1234 KB;柴犬那组加上渐变是 418 KB。这些 GIF 用 Pillow 解出来和应用自己的解码器一致。在模拟的 Host 和接口上 +测过:用上传的参考图出帧、先画参考图再确认(含运行时重启之后)、参考图 1 分钟没有回应时自动开始(倒计时里追问会取消、 +「先别生成」会停住且重启后仍停住、倒计时里重启会重新计满、到点早已过去很久的会话重启后不开始)、评审判某一帧不合格、评审没看到图、模型原样返回参考图的帧、 +调快慢、调顺序和删帧、改成直接切换(不调生图)、首尾同一张图、单独重画和加帧、GIF 做好之后改主体。GIF 卡片、确认按钮和倒计时 +只在无头 Chrome 里看过。**未验证:**改过提示词之后,真实的规划轮次会不会把延续下来的状态写进每一帧、真实评审会不会 +据此判出弹回原样的帧。 + +1.9.0 对色偏判定的改动用真实任务的图核对过:一次任务里 8 张暖光产品照以前全被判偏红,现在都能过;上面那张橘猫参考图 +也能过;同一张参考图人为加上偏红(红色调高 6% 到 25%)仍判偏红。 + +1.9.0 的数据图表先在真实的 H3-Image 接口上、把规划和评审轮次换成固定答复测过,画了一张 +咖啡店营业额海报(左边咖啡师、右边墙上白板画 5 年营业额柱状图):第一轮两张候选的白板和规划位置没对齐,被打回重画; +第二轮两张都留好了白板,程序画上柱状图,从最终文件量回来 11 处,最大偏差 0.08 像素,前后约 30 秒、生图 4 张。 +追问「2025 年改成 260,换成折线图」没有调用生图,一秒左右出了 v2,量回来 12 处,最大偏差 0.08 像素。之后加上了 +「跟着面板走」:拿第一轮被打回的两张重试,偏窄的那块白板现在能直接画上(偏差 0.13 像素),带明显窗户倒影的那块 +仍然打回。在模拟的 Host 和接口上测过:柱状、分组、堆叠、带负数的柱状图,条形图,双折线,面积图,从非 0 起的折线, +饼图,环形图,深色面板,光照渐变和错位的白板,面板上画了线(打回重画),面板太小,数据不是用户给的(交付时请核对), +只改数、换类型、去掉图表、只改字时图表跟着重画(都不调生图),给原来没有图表的图加一张(面板不空时先让模型留出面板), +以及运行时两次重启之后接着改数。之后在 MiniMax Code 里跑过两次:按需求里给的五年增长率画的折线图, +从最终文件量回来 11 处,最大偏差 0.06 像素,追问「变成柱状图」在同一张底图上改画成柱状图,没调生图(0.1 像素); +另一次附了一张图要求转成柱状图,量回来 12 处(0.12 像素),图表里的数需求原话里没有,交付时标出来请用户核对。 +**除这两次之外未验证:**真实的规划轮次会不会按要求只抄用户给的数、把面板留在合适的位置,真实评审怎么看程序画的图表。 + +1.9.0 后来又改了一轮(排字版式、色彩、参考图检索、工具调用次数、GIF 流畅度、重跑),起因是上面那些排字和 GIF 任务。 +11 月 19 日在 MiniMax Code 里跑的任务用的版本已经带上这一轮,但没有在那里逐项核对过。 +用真实任务的图核对过的:课本内页(一次真实任务的无字底图)按新规则重排,两段正文字号一致、段首空两格、脚注符号成上标、 +脚注上方有分隔线、页码排上了;线代课本页那次任务的 12 张底图按原规划重排,排上的从 2 张变成 5 张,没有一张叠字; +识字挂图那次任务的底图上「清」「情」「请」整字分色排上;黑体、宋体里 21 个左右、上下结构的字都切对了偏旁; +白纸检测在那张课本页的 4 张候选上只拦下纸面偏紫的交付图;明黄海报那次任务的 4 张候选不再判偏红; +24 个真实会话的工具调用记录用来定了各节点的次数。在模拟的 Host、接口和搜图工具上测过:排字的统一字号、缩进、上标、分隔线、 +不叠字和就近挪开、只重新排字、一字两色和换字体、规划后核对和重写;白纸检测、暖色主色、色彩基准(含重启后); +检索预算(不搜、预算不够、3 项以上、两种先后顺序、换说法重搜、没调用判定不扣次数)和参考图用途;各节点的工具调用上限; +GIF 接着前一帧改、补过渡帧、统一节奏、没过评审的帧不放进去;重跑只带需求和上传的图。**未验证:**真实的规划轮次会不会写角色、 +`part`、难度和检索预算,真实评审会不会用「只重新排字」,真实搜图换说法后能不能搜到,GIF 连着改十几次人物会不会走样、 +过渡帧画得准不准。 + +组图拼 GIF 在 MiniMax Code 里跑过一次(六格的红裙少女表情组图):追问「变成GIF」时编辑节点写了 `from_panels` 和 3 列 2 行, +没调生图,拼 GIF 用了约 2 秒。对齐之后相邻两格的头和背景一像素都没挪;当时默认在每次切换时插一帧放大 3% 的「弹一下」, +这一帧让整幅画面往外扩、头往上跳 9 像素,0.08 秒后又弹回,看起来每次切换都跳一下,所以改成了默认不弹(同一组图不弹的 GIF +是 909 KB,弹的是 2119 KB)。改了默认之后,同一个会话里又说了一句「重新变成gif」,编辑节点把清单里的现有设定原样抄了回来, +连「切换时弹一下」也写上了,于是又拼出了跳的那一版:现在弹一下只在你这次的话里提到「弹」时才加,照抄回来的设定不算改动, +第一版存下来的设定恢复时也会关掉弹一下。用那个会话的快照在模拟的 Host 上重放这一句,拼出来的是 6 帧、不弹的 GIF。 +那张组图的格子之间其实没有分隔线、格子直接挨着画,程序当时把切点贴到了第 2、3 格屏幕的边框和 +图表坐标轴上,偏了 70–120 像素,现在按等分位置切,切下来的画面宽了 10%。对齐改成只比每格中上部、再细调到亚像素之后, +这张组图相邻两格的头一像素不挪;另一次任务的六格组图第二版第 4 格(双手抱头)以前头比别的格低 12 个像素,现在差 1 像素以内。 +`h3-gif` 工具还没在 MiniMax Code 里跑过。 + +过渡格用真实的 H3-Image 接口测过,规划、评审轮次用固定答复(只能在 MiniMax Code 里跑)。先试了三种问法:只说「每一格画成它和下一格 +中间的样子」、再加一张「每格换成下一格」的组图、逐格写清中间状态。前两种照抄下一格的格子分别有 4–6 格、1–3 格,逐格写清的两张候选 +6 格都画成了中间状态,所以用的是逐格写清、只送原组图。拿红裙少女那次会话的快照重放「重新变成gif」:一次生图(2K、两张候选,35–40 秒, +有一次接口排队用了 193 秒),程序挑出 6 步,GIF 12 帧、1.7 MB;过渡格锁到它所在那一格的背景以后,从 2.4 MB 降下来,背景不再闪。 +另一次任务的 Q 版六格组图同样 6 步都补上,GIF 831 KB。在模拟的 Host 和接口上测过:照抄的候选被程序判掉、评审挑字母、 +评审没看到图时按程序挑、一步没挑到时第二轮只改那一格、两轮都没挑到时卡片上说明、「更顺一点」重试、「不要补帧」、只调快慢不生图、 +去掉一格后只补新的一步、改组图后重补、换场景的组图不补、规划轮次失败、重启之后过渡格还在。**未验证:**真实的过渡格规划写得准不准、 +真实评审挑得对不对。 + +程序转圈和关键帧扔帧的处理,用真实的 H3-Image 接口、固定答复的规划和评审测过(规划、评审轮次只能在 MiniMax Code 里跑): +十二点加载圈按新的规划写法画了一张图(一次生图 18 秒),程序转出 24 帧、154 KB、一圈 0.96 秒的 GIF,十二格叠在一起看, +暗点不动、亮点每格走一步;渐变弧线的加载圈连续转 30 帧。真机那次任务(十二帧扔了六帧)的参考图直接转,暗点同样不动。 +关键帧这边,柴犬抬爪挥手判掉「举到头顶」那一帧以后,按前后两帧和它原来的计划补了一帧,GIF 6 帧、每帧 220 毫秒; +这次也看到模型自己会把「举在头顶上方向外挥」画成爪子往下伸,这种错只有真实的动图评审拦得住。 + +用真实任务的图核对过的:另一张六格表情组图(5472 × 3072, +格子之间是细灰线、不精确等分)切出 3 列 2 行,各格不带分隔线,对齐后拼成 768 × 650、6 帧、388 KB 的 GIF,读回核对通过, +从读图到写完约 2 秒(其中解码这张 4K 图 1.2 秒)。合成的组图测过:细线、白色留白带外边距、带边框的格子、白底贴纸(指定行列)、顶部标题栏、 +上 2 下 3、一张普通的图(切不出)、竖条、最后一格空着、格子里大片空白。在模拟的 Host 和接口上测过:追问拼 GIF 不调生图、 +编辑节点按老写法写 `start` 也一样、调快慢和来回播、去掉一格、规格没变时不重拼、改某一格后按新组图重拼、不是组图时照实说、 +规划时就要组图 + GIF、重启之后接着调、点名要弹一下和再去掉、编辑节点照抄现有设定。`h3-gif` 的六个子命令和出错时的退出码也都测过。**未验证:** +真实的规划会不会在该用组图时写 `panel_gif`,以及别的画法的组图(漫画分镜、不规则的格子)切得准不准。 + +用 1.8.1 只做过模拟 Host 的自动化测试(Node 服务,加上无头 Chrome 里的页面)、没有在 MiniMax Code 里实测的: +端到端出图、分享页里 2048px 的大图、「停止」按钮、关闭 `session.run` 或 `tool:call` 后的提示、问卷、 +Host 重新加载页面后的视图恢复。交付图上的二维码标记、GIF 卡片上关于过渡格和程序转圈的说明只在模拟的 Host 和接口下 +用无头 Chrome 看过。 +未验证:Windows、除上述版本外的 MiniMax Code、以及开发时所用之外的任何 H3-Image 接口。 ## 数据与访问 @@ -119,37 +352,63 @@ export MINIMAX_BASE_URL="https://..." 在页面里配置时,用于查找 `MINIMAX_API_KEY`、`MCODE_API_KEY` 和 `MINIMAX_BASE_URL`。这依赖 shell 配置的文件布局,不属于任何已公布的契约。 - 你通过文件选择器挑的参考图。 +- Agent 交给随包的 `h3-review`、`h3-qr`、`h3-gif` 命令行工具的图片和 GIF;以及 `h3-qr fix --content-file` 指定的、 + 存放二维码内容的文本文件。 +- 排字、画图表时读取的字体文件:原地只读,不复制、不修改、不上传。macOS 上是 `/System/Library/Fonts/STHeiti Medium.ttc`、 + `/System/Library/Fonts/STHeiti Light.ttc` 和 `/System/Library/Fonts/Supplemental/Songti.ttc`;Windows 上是 + `%WINDIR%\Fonts` 里的 `simkai.ttf`、`simsun.ttc`、`msyh.ttc` 和 `simhei.ttf`。每次排字都会读黑体和宋体的文件 + (排毛笔字也读),用来补所选字体里没有的字。文件不存在就跳过。 **写入的文件** - 应用保存的所有内容都写在 Host 提供的 `dataDir` 下:`settings.json`(权限 `0600`)、 `sessions//`(规划、图片、预览、参考图、评审拼图、交付物、报告、分享页 `share/index.html` 及其内嵌大图的缓存 `share/large/`、`session.json`)、`refcache/`(下载的参考图,以及保留一天的下载失败记录)、`sessions/.trash/` - (已删除的会话)。 + (已删除的会话)。换二维码时,贴好真码的那份写在生成图旁边:`sessions//images/<候选号>-qr.jpg` + (接口回 PNG 时是 `.png`),生成图本身原样保留。程序排字也一样:排好字的那份是 + `sessions//images/<候选号>-text.jpg`,模型画的无字底图留在旁边,之后改字都从它开始。程序画的图表同理: + 画好图表的那份是 `sessions//images/<候选号>-chart.jpg`,留白的底图留在旁边,之后改数都从它开始。GIF 的帧写在 + `sessions//images/<图号>F-r<轮号>-g0.jpg`,512 像素的预览在 `previews/`;应用先画出来的参考图会按下一个 R 编号 + 复制进 `sessions//refs/`;每一版交付目录里有 GIF(`<文件名>.gif`)、当作静态图的参考图(`<文件名>.jpg`) + 和帧条(`<文件名>-frames.png`)。组图拼的 GIF 写在 `sessions//images/<候选号>-panels.gif`,帧条是旁边的 + `<候选号>-panels-strip.png`;交付目录里是 GIF、组图本身和帧条。过渡格的候选组图写在 `sessions//images/<图号>T-r-g.jpg`, + 规划和评审用的看图材料写在 `sheets/`。程序转圈的 GIF 写在 `sessions//images/<候选号>-spin.gif`, + 帧条是 `<候选号>-spin-strip.png`;交付目录里是 GIF、原图和帧条。 - 两个 JSON 状态文件——`settings.json` 和 `sessions//session.json`——是原子写入:同目录临时 文件再 `rename`。图片和 HTML 文件直接写。 -- `dataDir` 之外只有两处会写文件: +- `dataDir` 之外只有四处会写文件: - 发布轮次(见「调用的工具」)会把分享页复制到本 Mini App 自己的 Agent 工作目录下的 `h3-share-publish/index.html`,因为 `website_deploy` 只接受这个目录里的路径。每次发布都会覆盖它。 - 随 `h3-image-review` Skill 发布的 `h3-review` 命令行工具,在 Agent 运行它时写 PNG 看图材料: `sheet` 写在输入图旁边,`refs` 和 `grid` 写在当前工作目录,或者写到 `-o` 指定的位置。 应用自己从不运行它。 + - 随 `h3-image-qrcode` Skill 发布的 `h3-qr` 命令行工具,在 Agent 运行它时写:`fix` 把换好码的图写在输入图旁边, + 名为 `<原名>-qr.<扩展名>`(重名时加 `-2`、`-3`……,不覆盖已有文件),或写到 `-o` 指定的位置,永远不覆盖输入图本身; + `make` 在当前工作目录写 `qr.png`(同样按序号避开重名),或写到 `-o` 指定的位置。`scan` 和 `locate` 不写文件。 + `fix` 没能让码扫得出时一个文件都不写。应用自己从不运行它。 + - 随 `h3-image-gif` Skill 发布的 `h3-gif` 命令行工具,在 Agent 运行它时写:`grid` 把 GIF 写在组图旁边(`<原名>.gif`), + `slides` 写在第一张图旁边(`<原名>-slides.gif`),`spin` 写在图旁边(`<原名>-spin.gif`),`frames` 写在帧目录旁边(`<目录名>.gif`),重名时加 `-2`、`-3`……, + 或写到 `-o` 指定的位置;GIF 旁边再写一张帧拼图 `-frames.png`(或 `--sheet` 指定的位置)。`split` 把各格写成 + `<原名>-panels/01.png`……(目录已存在就换一个),`check` 只在给了 `--sheet` 时写帧拼图。永远不覆盖输入文件; + 写出去的 GIF 读回来核对没通过时一个文件都不写。应用自己从不运行它。 - 页面在自己的浏览器存储里保存视图状态:侧边栏是否收起;以及每次页面被隐藏时记下的当前会话 ID、 输入框里还没发出去的文字(最多 4000 字)和已选但未发送的参考图张数,这样 Host 重新加载页面后能恢复。 **网络** -- 你配置的 H3-Image 接口。它会收到你的 Key(作为 bearer token)、你的 prompt,以及你提供的参考图。 +- 你配置的 H3-Image 接口。它会收到你的 Key(作为 bearer token)、你的 prompt、你提供的和检索来的参考图, + 以及编辑时送回去的生成图:要改的那张、当作色彩基准的那张、GIF 的前一帧和补过渡帧时的前后两帧、补过渡格时的整张组图。 没有默认值——就是你填的那个。 - 该接口返回的图片地址,应用会从那里下载图片。 -- 参考图检索(见「调用的工具」)找到的图片地址,以及规划轮次写进规划里的图片地址:可能是任何第三方 - 网站,`https` 或 `http` 都有。应用只下载这些图片;检索查询本身经由工具发出,不是本应用直接发出。 +- 参考图检索(见「调用的工具」)找到的图片地址,以及你在需求或追问里给出的图片地址:可能是任何第三方 + 网站,`https` 或 `http` 都有。这些图都由应用自己下载、再上传给你的接口,从不让接口去取某个地址。 + 规划轮次或追问轮次写出来、但你的原话里没有的地址一律不用。检索查询本身经由工具发出,不是本应用直接发出。 本应用拒绝从 `169.254.0.0/16`(云主机元数据服务所在的链路本地段)以及 `0.0.0.0` 这类通配地址 下载图片,跟随每一次重定向之前都会重新检查一遍地址。对检索工具给出的图片地址,还会拒绝 localhost 和私有段(`10.*`、`172.16-31.*`、`192.168.*`、`100.64-127.*`、`fc00::/7`),包括解析到这些地址的 -域名:第三方的检索结果不应该让应用去读你自己网络里的东西。对你的接口返回的图片地址、以及按你的需求 -写进规划里的图片地址,私有段和 localhost 仍然允许,因为你的接口本身可能就在内网。每次下载都有上限: +域名:第三方的检索结果不应该让应用去读你自己网络里的东西。对你的接口返回的图片地址、以及你自己给出的 +图片地址,私有段和 localhost 仍然允许,因为你的接口本身可能就在内网。每次下载都有上限: 参考图 25 MB,生成图 100 MB。 **启动的进程** @@ -176,7 +435,8 @@ export MINIMAX_BASE_URL="https://..." 本应用声明 `agentAccess: ["session.run"]`,会运行 Agent 轮次来做规划、评审和修正。这些轮次运行在 你 MiniMax Code 会话所配置的模型服务上,因此你的 prompt 原文、参考图以及生成出来的图本身都会送到 -该模型服务,用于规划、评审、修正和选优。这些轮次可以读写文件、执行 shell 命令、调用 MCP 工具, +该模型服务,用于规划、评审、修正和选优;筛选参考图候选时,检索工具给出的候选标题和网站名也会一起送过去; +规划和追问轮次还会看到本机排字用的字体名(例如「宋体-简」「马善政毛笔楷书」)。这些轮次可以读写文件、执行 shell 命令、调用 MCP 工具, 均受你的权限规则约束。每一个工具权限请求都会展示在页面上由你决定,包括下面「调用的工具」里说的 发布轮次发起的 `website_deploy` 请求;应用从不替你放行。它自己只会拒绝发布轮次发起的其他工具请求。 如果你在插件详情里关闭了 Agent 权限(`session.run`),任务会停下,页面会说明去哪里重新打开。 @@ -186,7 +446,9 @@ export MINIMAX_BASE_URL="https://..." 本应用声明 `toolAccess: ["call"]`,只调用一个工具:已安装工具里第一个名字包含 `search_images` 的工具。在测试环境里它是 `connector__matrix__search_images`,即 `matrix` Connector 的 -`search_images` 工具。它收到的是规划里的主体名,以及一句说明要找什么样的图,返回图片地址。每次调用 +`search_images` 工具。它收到的是规划写的检索词(主体名,或者画风、版式这类说法;搜不到时还有规划给的另一种说法), +以及一句说明要找什么样的图,返回图片地址。一次规划或追问里搜几次受检索预算约束(最多 16 次,搜图和判定候选合计; +服务报错时的自动重试不计)。每次调用 都走你的权限规则;规则为「询问」时,应用会在第一次检索前弹出 Host 的确认框,请你始终允许这一个工具。 如果你在插件详情里关闭了工具调用(`tool:call`),任务会在没有检索参考图的情况下继续,页面会说明原因。 @@ -194,8 +456,8 @@ export MINIMAX_BASE_URL="https://..." Host 的 `website_deploy` 工具,把自包含分享页发布到一个公开的 `https://.space.mcode.cn` 地址,**任何拿到链接的人都能打开**。页面里包含:你的需求和追问、你上传的或应用在网上找到的参考图、 其余检索结果的缩略图、规划、所有候选图和交付图的缩小版、每张交付图一份长边 2048px 的大图(点开时显示)、 -评审拼图、应用发给模型的完整提示词和模型的回复,以及带 `request_id` 和成本的执行记录。大图总量上限 4 MB, -超出的交付图点开时仍是预览大小。你点了这个按钮、并且在页面上对出现的 `website_deploy` +评审拼图、应用发给模型的完整提示词和模型的回复,以及带 `request_id` 和成本的执行记录,还有每一张交付的 GIF 原文件。 +大图总量上限 4 MB,GIF 总量上限 3 MB;超出的交付图点开时仍是预览大小,超出的 GIF 显示成它的参考图。你点了这个按钮、并且在页面上对出现的 `website_deploy` 请求点了「允许一次」之后,才会真正发布。按钮下面常驻一行提示,说明任何拿到链接的人都能打开,以及页面包含 哪些内容。写盘前,本机路径会被替换成占位符:会话目录和插件的 `dataDir` 保留其后的相对部分,用户 主目录下的其他路径、以及 `/Users/` 或 `X:\Users\` 下的路径整段替换。发布轮次的其他工具请求一律 @@ -208,6 +470,16 @@ Host 的 `website_deploy` 工具,把自包含分享页发布到一个公开的 `sessions//session.json`。Skill 并不知道 MiniMax Code 把这个目录放在哪里,所以找到它依赖未公开的 目录布局。 - `h3-image-review` 告诉 Agent 在你要它亲自看生成图时,怎么运行随包的 `h3-review` 工具(见「写入的文件」)。 +- `h3-image-qrcode` 告诉 Agent 在你要它把图里的码换成能扫的、检查一个码能不能扫、或单独生成一张二维码时, + 怎么运行随包的 `h3-qr` 工具(见「写入的文件」):内容从你的原话里逐字照抄,你没说就先问;只有工具自己扫码 + 通过才能说码能用。面板里出的图,它会让 Agent 回到面板会话里改。 +- `h3-image-gif` 告诉 Agent 在你要它把一张分格组图、几张单独的图,或者它自己写程序画的一组帧做成 GIF,以及检查一个 GIF 时, + 怎么运行随包的 `h3-gif` 工具(见「写入的文件」):只有工具读回来核对通过才能说做好了。面板里出的组图,它会让 Agent 回到面板会话里做。 + +二维码的生成和验证全部在应用的 Node 进程和上面这两个工具里完成:不发网络请求、不启动进程、不跑 Agent 轮次、 +不调用工具。码里放的内容是规划的一部分,所以规划轮次和你需求的其余部分一样能看到它。程序排字也一样:读字体、 +排字都在 Node 进程里完成,评审轮次看到的排好字的图和别的候选没有区别。组图切格、拼 GIF 也全在 Node 进程和 `h3-gif` 里完成, +不发网络请求、不调生图接口。 **设备使用** @@ -221,8 +493,57 @@ Host 的 `website_deploy` 工具,把自包含分享页发布到一个公开的 - 角色设定图能改善一致性,但不保证。设定图与故事图之间姿势或画幅差异大时仍会漂移。 - 内容安全审核拒绝是确定性的,所以应用在第一次被拒时就停下,不再重试。请修改需求本身后重新发送。 - 参考图检索依赖检索工具,可用性随它而定。工具拒绝或检索失败时,这个主体就没有检索到的参考图, - 请自己上传一张。 + 请自己上传一张。检索预算用完时,排在后面的项不搜、或者判不完,交付说明里会写明。 +- WebP 格式、以及算术编码、无损、CMYK 的 JPEG 参考图候选解不开。它们会被跳过,时间线会写明原因,不算作「不符合要求」。 +- 二维码:程序靠三个角的回字形定位方块找码,模型画的码被遮挡、倾斜过大、做成艺术化图案或没有定位方块时找不到。 + 这时会带着更严格的要求重画,不经评审最多两次;之后走正常的评审修正循环,所有候选的码都没换成时,交付最好的那张, + 标「二维码没换成」并附原因。只生成和验证正色码(深色码印在浅底上),反色码多数手机扫不了。每张图最多 3 枚码。 + 换码时整张图会按质量 93 重新编码一次 JPEG(PNG 仍是 PNG)。验证用的是软件解码器(jsQR),从多种裁切范围、 + 以及每个模块约 2 像素的缩小图读最终文件;这是很强的信号,但不保证每一部手机都能扫出印出来的码。 +- 程序排字:空白检测找的是颜色均匀的区域,纹理很强的载体(木纹、砖墙、花纹布料)可能被判成没留空;不经评审重画两次之后 + 走正常的评审修正循环,交付时标「文字没排上」。让模型擦掉它自己写的字时,常会留下旧笔画的痕迹,要多调几次生图才能 + 拿到干净的底图。字是平着、正着排在一个矩形里的,放在倾斜或弯曲的招牌上会像贴上去的。 + 两款毛笔字体随包发布;宋体和黑体用 macOS 和 Windows 的系统字体,楷体只在 Windows 上用系统字体(macOS 的楷体要按需下载, + 所以改用随包的毛笔楷书),Linux 上所有书体都退回随包的毛笔字体。CFF 轮廓的字体(多数 `.otf`)读不了。 + 所有可用字体里都没有的字,会退回让模型直接写。排字时整张图会按质量 93 重新编码一次 JPEG(PNG 仍是 PNG)。 + 一字两色只能按左、右、上、下切一刀:半包围、全包围的字(这、国、同)切不出偏旁,整字用一种颜色。 + 规划后的文字核对只对照你原话引号里的内容,没加引号的文字不核对。 +- GIF 是关键帧动画,不是视频。每一帧在前一帧上接着改,帧要一帧一帧地画,8 帧约 2 分钟;连着改十几次,人物可能一点点走样, + 靠每次都附上参考图、合成时锁回背景和色调压着,在动的部位里小细节(围巾的褶皱、毛发纹理、五官)仍会帧与帧略有不同。 + 单独重画中间某一帧之后,后面的帧还是接着原来那一帧画的,衔接靠程序量出跨度后补的过渡帧。渐变只补在差得不多的两帧之间, + 差得多的地方直接切换。镜头运动、背景变化做不了: + 整幅都在变的帧不会锁背景。WebP 的参考图用不了,应用解不开这种格式。GIF 最多 256 色:带柔和渐变的照片类 + 帧会有一点色带(这类帧会加有序抖动,平涂的插画不加)。 +- 程序转圈只能绕垂直于画面的轴转(平面上转),硬币翻面、地球自转这种立体的转动做不了。转的是圆心周围一整块圆盘, + 里面的背景跟着一起转,所以背景要是纯色或者从圆心往外的渐变(规划会这样要求);圆盘里有别的图案,转起来看得出来。 + 一格一格跳的要求点或辐条等距:模型画得不匀时,暗的点每帧会抖一两个像素。连续转的不找圆心,用规划给的位置。 +- 组图拼 GIF 是一格一格地切换,不是连贯的动作:要眨眼、挥手这类顺滑的动作,用上面那种按参考图出帧的 GIF。 + 程序靠分隔线或留白找格子;格子直接挨着画、没画分隔线时按等分切。格子之间什么都没有(白底贴纸)、格子歪斜或 + 互相压着(漫画的出格分镜)时找不准,要你说几行几列,按等分切;不等分又没有分隔线的排法切不准。 + 各格按中上部(主体的头和脸)做平移对齐,最多挪格子宽高的 4%:每格里主体大小不一样、或者头不在中上部时对不齐, + 模型每格画的背景位置也会差一两个像素。 + 过渡格是模型画的,会画错(手的位置不对、照抄了前后格),程序和评审挑不出能用的那一步就直接切换;人物画法和关键格总会差一点, + 过渡格的背景用它所在那一格的像素,所以到下一格时背景(比如屏幕上的 K 线)还是一下子换过去。补满过渡格的 GIF 帧数翻倍, + 文件大约大一倍。不补过渡格时,表情、手一下子换成下一格的样子;加渐变的话,两格差得多时叠出来是两只半透明的手。 + 说了要「切换时弹一下」才会加:每格多一整帧,GIF 大约大一倍,整幅画面每次切换会先放大再落回。 +- 数据图表是程序画的平面图表:线条和字是规整的印刷风格,不是手绘,也不会跟着面板的透视变形,所以面板要正对镜头 + (规划会这样要求)。支持柱状、条形、折线、面积、饼图、环形图和堆叠柱状,不支持散点、雷达、地图和表格。 + 同一张图表里的数值标注统一保留同样的小数位(2185.8 和 3191.43 在一起时写成 2185.80)。数从你的原话里找不到时照样画, + 只在交付时提醒你核对;你给的是图片里的表格时,程序没法核对原文。 + +## 第三方代码 + +`miniapp/node/vendor/jsqr/jsQR.cjs` 是 [jsQR](https://github.com/cozmo/jsQR) 1.4.0(作者 Cosmo Wolfe、 +Jefff Nelson)的 `dist/jsQR.js`,除扩展名改为 `.cjs` 外原样拷贝,许可证为 Apache License 2.0,全文见 +`miniapp/node/vendor/jsqr/LICENSE`。二维码编码器(`miniapp/node/lib/qrcode.mjs`)是本包自己写的。 + +`miniapp/node/vendor/fonts/MaShanZheng-Regular.ttf`(马善政毛笔楷书 Ma Shan Zheng,版权 2018 The Ma Shan Zheng +Project Authors)和 `miniapp/node/vendor/fonts/ZhiMangXing-Regular.ttf`(钟齐志莽行书 Zhi Mang Xing,版权 2018 +The Zhi Mang Xing Project Authors)是 Google Fonts 上的原文件,未作修改,许可证为 SIL Open Font License 1.1, +全文见同目录的 `OFL-MaShanZheng.txt` 和 `OFL-ZhiMangXing.txt`。两款字体让包体大了约 9.9 MB。字体解析 +(`miniapp/node/lib/font.mjs`)和排版(`textlayout.mjs`、`textoverlay.mjs`)是本包自己写的,GIF 的编码和解码 +(`miniapp/node/lib/gif.mjs`)、组图切格(`panels.mjs`)、程序转圈(`motion.mjs`)和拼 GIF(`gifbuild.mjs`)也是。 ## 许可证 -MIT,见 [LICENSE](LICENSE)。 +MIT,见 [LICENSE](LICENSE)。随包的 jsQR 为 Apache-2.0,随包的字体为 OFL-1.1,见「第三方代码」。 diff --git a/plugins/liddalidd/h3-image-executor/miniapp/client/app.js b/plugins/liddalidd/h3-image-executor/miniapp/client/app.js index f21435dd..3d7d9b78 100644 --- a/plugins/liddalidd/h3-image-executor/miniapp/client/app.js +++ b/plugins/liddalidd/h3-image-executor/miniapp/client/app.js @@ -442,7 +442,8 @@ function flash(text) { setTimeout(() => { box.remove(); }, 2400); } -// 重跑:服务端拿原需求和参考图开一个新会话,回来直接切过去,原会话原封不动留着。 +// 重跑:服务端只拿原需求和你当时上传的图开一个新会话(检索来的、程序画的图和上一次的结果都不带), +// 回来直接切过去,原会话原封不动留着。 async function rerunSession(id, button) { if (button) button.disabled = true; const result = await api('/api/session/rerun', { method: 'POST', body: JSON.stringify({ id }) }); @@ -718,14 +719,19 @@ async function poll() { state.clockOffset = Date.now() - (timing.now || Date.now()); render(snapshot); loadSessions(); - if (!snapshot.busy && snapshot.status !== 'running') { - clearInterval(state.timer); - state.timer = null; + const idle = !snapshot.busy && snapshot.status !== 'running'; + if (idle) { el('start').disabled = false; el('cancel').disabled = true; } else { el('cancel').disabled = false; } + // 参考图在倒计时:会话是空闲的,但到点后服务端会自己开始出帧,页面得接着轮询才看得到新的一轮。 + const counting = Boolean(snapshot.autoConfirm && !snapshot.autoConfirm.held); + if (idle && !counting) { + clearInterval(state.timer); + state.timer = null; + } } function startClock() { @@ -775,6 +781,8 @@ function watchSharePublish() { // 只有最后一轮会跑,计时器只管它;更早的轮次用时是定值,渲染时写一次就行。 function tickClock() { + // 参考图的自动确认倒计时:到点时间是服务端定的,按服务端时钟换算剩余秒数。 + for (const span of document.querySelectorAll('[data-deadline]')) span.textContent = countdownText(Number(span.dataset.deadline)); const item = lastTurnNode(); const view = state.turns[state.turns.length - 1]; if (!item || !view) return; @@ -1042,11 +1050,14 @@ function renderUserMessage(view, item, snapshot) { ? state.launchLenWarn : null; const signature = (view.instruction || '') + ':' + mine.length + ':' - + (warn ? warn.expected + '/' + warn.actual : '-'); + + (warn ? warn.expected + '/' + warn.actual : '-') + ':' + (view.auto ? 'auto' : ''); const key = 'u' + view.index; if (state.sigs[key] === signature) return; state.sigs[key] = signature; - item.userText.textContent = view.instruction || '(空需求)'; + // 到点自动开始的一轮不是用户发的:照实写明,用户回来时才不会以为这句话是自己说的。 + item.userText.textContent = view.auto + ? '(参考图交付后 ' + waitText(snapshot.autoConfirmMs) + '没有回应,按默认确认自动开始)' + (view.instruction || '') + : view.instruction || '(空需求)'; const box = item.userRefs; box.textContent = ''; mine.forEach((ref) => { @@ -1360,7 +1371,17 @@ function renderImages(snapshot) { image.rounds.forEach((round) => { const wrapRound = node('div', 'round'); - const actionText = round.action && round.action.type === 'edit' ? '局部编辑' : '生成'; + const type = round.action && round.action.type; + // qr = 只在原图上换二维码、text = 只在不带字的底图上重新排字,两种都没有调生图接口。 + const overlay = Boolean(round.action && round.action.overlay); + const actionText = + type === 'edit' + ? overlay ? '局部编辑(不带字的底图)' : '局部编辑' + : type === 'qr' + ? '只换二维码' + : type === 'text' + ? '只改文字' + : overlay ? '生成(不带字的底图,程序排字)' : '生成'; wrapRound.appendChild( node( 'div', @@ -1638,6 +1659,100 @@ function buildShareTools(snapshot) { } return tools; } +// 交付图上的二维码标记:全部换成真码并扫码通过才是「二维码可扫」; +// 没给内容的是装饰码;给了内容却没换成的单独标红,让用户一眼看出这张图的码不能用。 +function qrPill(entry) { + const items = (entry && entry.qr && entry.qr.items) || []; + if (!items.length) return null; + const real = items.filter((item) => item.status === 'real').length; + let cls = 'unpassed'; + let label = '装饰码'; + if (real === items.length) { + cls = 'pass'; + label = '二维码可扫'; + } else if (items.some((item) => item.status === 'failed')) { + cls = 'fail'; + label = '二维码没换成'; + } + const pill = node('span', 'pill ' + cls, label); + pill.title = items.map((item) => (item.content ? item.content + ':' : '') + item.note).join('\n'); + return pill; +} + +// 程序排字的图:文字是程序按原文排上去的,不是模型画的。排上了标绿,没排上标红。 +function textPill(entry) { + const info = entry && entry.text; + if (!info) return null; + const ok = info.status === 'overlay'; + const pill = node('span', 'pill ' + (ok ? 'pass' : 'fail'), ok ? '文字由程序排入' : '文字没排上'); + pill.title = info.note || ''; + return pill; +} + +// 程序画的图表:画上了、量回来核对通过标绿;有数在用户原话里找不到标黄,提醒核对;没画上标红。 +function chartPill(entry) { + const info = entry && entry.chart; + if (!info) return null; + const ok = info.status === 'overlay'; + const doubtful = (info.items || []).some((one) => (one.unverified || []).length); + const pill = node('span', 'pill ' + (!ok ? 'fail' : doubtful ? 'unpassed' : 'pass'), !ok ? '图表没画上' : doubtful ? '图表 · 请核对数据' : '图表核对通过'); + pill.title = [info.note].concat((info.items || []).map((one) => one.typeLabel + (one.title ? '「' + one.title + '」' : '') + ':' + one.note)).filter(Boolean).join('\n'); + return pill; +} + +function chartDataText(one) { + const series = (one.series || []).map((s) => (s.name ? s.name + ' ' : '') + (s.values || []).join('、')).join(';'); + return one.typeLabel + (one.title ? '「' + one.title + '」' : '') + ':' + (one.categories || []).join('、') + ' → ' + series + (one.unit ? '(' + one.unit + ')' : ''); +} + +// GIF:合成好的标「GIF · N 帧」(组图拼的标「N 格」);参考图还在等确认的标「GIF 参考图 · 待确认」;没做成的标红。 +function gifPill(entry) { + if (entry && entry.gif) { + const unit = entry.gif.source === 'panels' ? ' 格' : ' 帧'; + return node('span', 'pill ' + (entry.gif.verify && entry.gif.verify.ok ? 'pass' : 'unpassed'), 'GIF · ' + entry.gif.frames + unit); + } + const info = entry && entry.animation; + if (!info) return null; + if (info.state === 'awaiting_confirm') return node('span', 'pill unpassed', 'GIF 参考图 · 待确认'); + if (info.state === 'failed') return node('span', 'pill fail', 'GIF 没做成'); + return null; +} + +function fmtBytes(bytes) { + return bytes >= 1024 * 1024 ? (bytes / 1024 / 1024).toFixed(1) + ' MB' : Math.max(1, Math.round(bytes / 1024)) + ' KB'; +} + +// 毫秒写成秒:0.18 秒、1.4 秒。 +function seconds(ms) { + const value = Math.max(0, Number(ms) || 0) / 1000; + return (value < 1 ? Math.round(value * 100) / 100 : Math.round(value * 10) / 10) + ' 秒'; +} + +// 参考图卡片上的「生成 GIF」:发一句固定的话,后端认出来直接开始出帧。 +function confirmGif(snapshot, text) { + if (!text || snapshot.busy || state.sessionId !== snapshot.id) return; + el('instruction').value = text; + state.mode = 'followup'; + sendInstruction(); +} + +// 「先别生成」:停掉自动确认的倒计时,之后要用户自己点「生成 GIF」或者回复。 +async function holdGif(snapshot) { + if (state.sessionId !== snapshot.id) return; + await api('/api/session/hold', { method: 'POST', body: JSON.stringify({ id: snapshot.id }) }); + poll(); +} + +function countdownText(deadline) { + const left = Math.ceil((deadline - (Date.now() - state.clockOffset)) / 1000); + return left > 0 ? '还剩 ' + left + ' 秒' : '正在自动开始'; +} + +function waitText(ms) { + const value = Number(ms) || 60000; + return value >= 60000 && value % 60000 === 0 ? value / 60000 + ' 分钟' : Math.round(value / 1000) + ' 秒'; +} + // 交付是这一整轮的结论:图要大、信息要少,细节全部收进折叠里。 function renderDelivery(view, item, snapshot) { const card = item.deliver; @@ -1663,7 +1778,11 @@ function renderDelivery(view, item, snapshot) { // 否则用户会盯着一个永远不变的「发布中」。 (snapshot.shareState || 'idle') + ':' + (snapshot.shareError || '') + ':' + - (snapshot.shareClip || ''); + (snapshot.shareClip || '') + ':' + + // GIF 参考图的「生成 GIF」按钮只在会话空闲时出现:busy 变了要重画。 + (snapshot.busy ? 'busy' : '') + ':' + + // 自动确认开始倒计时、被「先别生成」停掉、到点开始,都要重画参考图卡片。 + (snapshot.autoConfirm ? (snapshot.autoConfirm.held ? 'held' : 'auto' + snapshot.autoConfirm.deadline) : ''); if (state.sigs[key] === signature) return; state.sigs[key] = signature; box.textContent = ''; @@ -1765,10 +1884,13 @@ function renderDelivery(view, item, snapshot) { } // 用后端给的真实文件名,不要自己拼 .png:接口回 JPEG 时交付文件就是 .jpg。 const src = fileUrl('deliver', String(entry.file).split('/').pop(), 'v' + delivery.version); + // GIF 交付时大图放动图本身,静态图(参考图)留给下载和之后的修改。 + const gifSrc = entry.gif ? fileUrl('deliver', String(entry.gif.file).split('/').pop(), 'v' + delivery.version) : ''; + const shown = gifSrc || src; const img = node('img'); - img.src = src; + img.src = shown; img.alt = entry.title; - img.onclick = () => openLightbox(src); + img.onclick = () => openLightbox(shown); cell.appendChild(img); shots.push(img); @@ -1778,17 +1900,177 @@ function renderDelivery(view, item, snapshot) { node('strong', null, entry.id + ' · ' + entry.title), node('span', 'pill ' + (entry.status === 'passed' ? 'pass' : 'unpassed'), entry.status === 'passed' ? '通过' : '选优交付'), ); + const qrBadge = qrPill(entry); + if (qrBadge) title.appendChild(qrBadge); + const textBadge = textPill(entry); + if (textBadge) title.appendChild(textBadge); + const chartBadge = chartPill(entry); + if (chartBadge) title.appendChild(chartBadge); + const gifBadge = gifPill(entry); + if (gifBadge) title.appendChild(gifBadge); cap.appendChild(title); const meta = node('div', 'meta', [entry.size || '-', entry.rounds + ' 轮', entry.verdict ? 'hard ' + entry.verdict.hardPassed + '/' + entry.verdict.hardTotal : ''].filter(Boolean).join(' · ')); cap.appendChild(meta); + if (entry.gif && entry.gif.source === 'motion') { + // 程序绕圆心转出来的 GIF:大图放 GIF,下面是一圈里的几帧;原图留给下载和之后的修改。 + const gif = entry.gif; + meta.textContent = [ + gif.width + '×' + gif.height, + gif.steps ? '一圈 ' + gif.steps + ' 格' : '连续转 ' + gif.frames + ' 帧', + (gif.steps ? '每格 ' : '每帧 ') + seconds(gif.keyMs || gif.frameMs), + gif.durationMs ? '一圈 ' + seconds(gif.durationMs) : '', + gif.direction === 'ccw' ? '逆时针' : '顺时针', + gif.transition === 'fade' ? '渐变过渡' : '直接切换', + fmtBytes(gif.bytes || 0), + ].filter(Boolean).join(' · '); + if (gif.strip) { + const strip = node('img', 'deliver-strip'); + strip.src = fileUrl('deliver', String(gif.strip).split('/').pop(), 'v' + delivery.version); + strip.alt = entry.title + ' 的各帧'; + strip.onclick = () => openLightbox(strip.src); + cap.appendChild(strip); + } + cap.appendChild(node('div', 'meta deliver-text', '这是程序按圆心把这张图转出来的 GIF,每一帧都是算出来的,不会丢帧。要调快慢、换方向,直接说,不重新生图;要改画面,说改哪里,改完会重新转。')); + } else if (entry.gif && entry.gif.source === 'panels') { + // 组图拼的 GIF:大图放 GIF,下面是切出来的各格;组图本身留给下载和之后的修改。 + const gif = entry.gif; + meta.textContent = [ + gif.width + '×' + gif.height, + gif.frames + ' 格', + gif.tweens ? '补了 ' + gif.tweens + ' 格过渡' : '', + '每格 ' + seconds(gif.keyMs || gif.frameMs), + gif.durationMs ? '共 ' + seconds(gif.durationMs) : '', + gif.loop ? (gif.pingpong ? '来回循环' : '循环') : '只播一遍', + // 补了过渡格的就不再说「直接切换」:格与格之间已经是一步一步的了。 + gif.transition === 'fade' ? '渐变过渡' : gif.bounce ? '切换时弹一下' : gif.tweens ? '' : '直接切换', + fmtBytes(gif.bytes || 0), + ].filter(Boolean).join(' · '); + if (gif.strip) { + const strip = node('img', 'deliver-strip'); + strip.src = fileUrl('deliver', String(gif.strip).split('/').pop(), 'v' + delivery.version); + strip.alt = entry.title + ' 的各格'; + strip.onclick = () => openLightbox(strip.src); + cap.appendChild(strip); + } + if (gif.tweenNote) cap.appendChild(node('div', 'fail-hint deliver-interrupted', gif.tweenNote + '。可以说「更顺一点」再补一次。')); + cap.appendChild( + node( + 'div', + 'meta deliver-text', + '这是组图切格拼成的 GIF' + (gif.tweens ? ',相邻两格之间补了过渡格(帧条里标「格子号+」的那几格)' : '') + + '。要调快慢、换顺序、去掉某一格,直接说,不重新生图;要改某一格的画面,说改哪一格,改完会重新拼。', + ), + ); + } else if (entry.gif) { + const gif = entry.gif; + meta.textContent = [ + gif.width + '×' + gif.height, + gif.frames + ' 帧', + // 实际每帧停多久:补了过渡帧、去掉了没过评审的帧之后,和规划写的不一样。 + '每帧 ' + seconds(gif.keyMs || gif.frameMs), + gif.durationMs ? '共 ' + seconds(gif.durationMs) : '', + gif.loop ? '循环' : '只播一遍', + gif.transition === 'cut' ? '直接切换' : gif.transition === 'fade' ? '渐变过渡' : '', + fmtBytes(gif.bytes || 0), + ].filter(Boolean).join(' · '); + if (gif.strip) { + const strip = node('img', 'deliver-strip'); + strip.src = fileUrl('deliver', String(gif.strip).split('/').pop(), 'v' + delivery.version); + strip.alt = entry.title + ' 的各帧'; + strip.onclick = () => openLightbox(strip.src); + cap.appendChild(strip); + } + const weak = (gif.items || []).filter((one) => !one.same && one.status !== 'passed'); + if (weak.length) { + cap.appendChild( + node( + 'div', + 'fail-hint deliver-interrupted', + weak.map((one) => '第 ' + one.index + ' 帧' + (one.status === 'failed' ? '没画出来' : one.status === 'unreviewed' ? '没评审' : '没过评审') + (one.skipped ? ',没放进 GIF' : '') + (one.problem ? '(' + one.problem + ')' : '')).join(';') + + '。可以说「第 ' + weak[0].index + ' 帧重画」。', + ), + ); + } + cap.appendChild(node('div', 'meta deliver-text', '要调快慢、去掉或重画某一帧、加一帧,直接说;只调快慢和顺序时不重新生图。')); + } else if (entry.animation && entry.animation.state === 'awaiting_confirm') { + const frames = entry.animation.frames || []; + cap.appendChild( + node( + 'div', + 'meta deliver-text', + '这是 GIF 的参考图。确认后从它改出 ' + frames.length + ' 帧:' + + frames.map((one) => '第 ' + one.index + ' 帧' + (one.same ? '用这张图' : '「' + one.prompt + '」')).join(';') + + '。要改参考图就直接说改哪里。', + ), + ); + const auto = snapshot.autoConfirm; + if (item.isLast && !snapshot.busy && auto && auto.imageId === entry.id) { + if (auto.held) { + cap.appendChild(node('div', 'meta deliver-text', '已暂停自动开始:确认之后点「生成 GIF」,或者直接说要改哪里。')); + } else { + const line = node('div', 'meta deliver-text deliver-countdown'); + const left = node('strong', null, countdownText(auto.deadline)); + left.dataset.deadline = String(auto.deadline); + line.append(left, document.createTextNode(':' + waitText(auto.ms) + '内没有回应,会按默认确认、自动开始出帧。')); + cap.appendChild(line); + } + } + } else if (entry.animation && entry.animation.state === 'failed') { + cap.appendChild(node('div', 'fail-hint deliver-interrupted', 'GIF 没做成:' + (entry.animation.error || '能用的帧不到 2 帧') + '。这是参考图,可以说「重新出帧」再试。')); + } + // 出图中途停下(接口故障、评审没跑成)时交付的是停下前的图:这句话常驻在图下面, + // 不能只写在默认收起的「需要你确认」里,否则用户会以为这是一张正常跑完的图。 + if (entry.interrupted) { + cap.appendChild(node('div', 'fail-hint deliver-interrupted', '出图中途停下:' + entry.interrupted + '。这是停下前已有的图。')); + } + // 程序画的图表同理:数据可以直接改,不用重画;量回来的偏差写在这里,用户知道这些数是核对过的。 + if (entry.chart && entry.chart.status === 'overlay') { + const checked = (entry.chart.items || []).map((one) => one.note).filter(Boolean).join(';'); + cap.appendChild( + node( + 'div', + 'meta deliver-text', + '图表由程序按数据画入,坐标轴、数值和比例从成品里量回来核对过(' + checked + ')。要改数、换图表类型或颜色,直接说,不用重画。', + ), + ); + } + // 程序排的字可以直接改(换字、换字体、调大小),不用重画:这一句常驻,用户才知道有这条路。 + if (entry.text && entry.text.status === 'overlay') { + const layouts = (entry.text.items || []).map((one) => one.layout).filter(Boolean).join(';'); + cap.appendChild( + node( + 'div', + 'meta deliver-text', + '文字由程序按原文排入(' + (entry.text.font || '') + (layouts ? ' · ' + layouts : '') + ')。要改字、换字体或调大小,直接说,不用重画。', + ), + ); + } // 交付后「过程详情」会自动收起,请求号不能跟着一起消失: // 反馈问题时要报的就是它,必须在交付图旁边常驻。 if (entry.requestId) cap.appendChild(window.H3Blocks.reqChip(entry.requestId)); const actions = node('div', 'deliver-actions'); const open = node('button', 'btn tiny', '看大图'); - open.onclick = () => openLightbox(src); - const download = node('a', 'btn tiny', '下载 4K'); + open.onclick = () => openLightbox(shown); + if (entry.animation && entry.animation.state === 'awaiting_confirm' && item.isLast && !snapshot.busy) { + const go = node('button', 'btn tiny primary', '生成 GIF'); + go.onclick = () => confirmGif(snapshot, entry.animation.confirm); + actions.appendChild(go); + const auto = snapshot.autoConfirm; + if (auto && !auto.held && auto.imageId === entry.id) { + const hold = node('button', 'btn tiny', '先别生成'); + hold.onclick = () => holdGif(snapshot); + actions.appendChild(hold); + } + } + if (gifSrc) { + const gifDownload = node('a', 'btn tiny', '下载 GIF'); + gifDownload.href = gifSrc; + gifDownload.download = String(entry.gif.file).split('/').pop(); + actions.appendChild(gifDownload); + } + const longEdge = Math.max(...String(entry.size || '').split(/[x×*]/).map(Number).filter(Number.isFinite), 0); + const download = node('a', 'btn tiny', entry.gif ? (entry.gif.source === 'panels' ? '下载组图' : entry.gif.source === 'motion' ? '下载原图' : '下载参考图') : longEdge && longEdge < 3840 ? '下载原图' : '下载 4K'); download.href = src; download.download = String(entry.file).split('/').pop(); actions.append(open, download); @@ -1808,6 +2090,18 @@ function renderDelivery(view, item, snapshot) { items.forEach((entry) => { const rows = [ entry.program && entry.program.redCast ? { kind: 'kv', label: entry.id + ' 色彩判定', value: '偏红,已打回重画' } : null, + entry.qr && (entry.qr.items || []).length + ? { + kind: 'kv', + label: entry.id + ' 二维码', + value: entry.qr.items.map((item) => (item.content ? '「' + item.content + '」' : '') + item.note).join(';'), + } + : null, + ...((entry.chart && entry.chart.items) || []).map((one, index) => ({ + kind: 'kv', + label: entry.id + ' 图表 ' + (index + 1), + value: chartDataText(one) + ((one.unverified || []).length ? '。这些数在你的原话里找不到:' + one.unverified.join('、') : ''), + })), { kind: 'kv', label: entry.id + ' request_id', value: (entry.requestId || '').slice(0, 16) }, entry.reason ? { kind: 'text', label: entry.id + ' 说明', text: entry.reason } : null, ].filter(Boolean); diff --git a/plugins/liddalidd/h3-image-executor/miniapp/client/share.js b/plugins/liddalidd/h3-image-executor/miniapp/client/share.js index 32c33e52..4b2837b6 100644 --- a/plugins/liddalidd/h3-image-executor/miniapp/client/share.js +++ b/plugins/liddalidd/h3-image-executor/miniapp/client/share.js @@ -301,11 +301,29 @@ grid.appendChild(node); return; } - const picture = img('deliver/' + item.previewFile, item.title, null, 'large/' + item.file); + // GIF 放动图本身;动图超出分享页预算没内嵌的,放参考图的预览并注明。 + const gifKey = item.gif ? 'gif/' + item.gif.file : ''; + const picture = gifKey && images[gifKey] + ? img(gifKey, item.title) + : img('deliver/' + item.previewFile, item.title, null, 'large/' + item.file); if (picture) node.appendChild(picture); node.appendChild(el('h3', null, item.id + ' · ' + item.title)); node.appendChild(el('span', 'pill ' + (item.status === 'passed' ? 'pass' : 'unpassed'), item.status === 'passed' ? '通过' : '未通过(选优交付)')); + if (item.gif) { + // 组图拼的 GIF 按「格」说:每一帧就是组图里的一格。程序转出来的按「一圈」说。 + const panels = item.gif.source === 'panels'; + const motion = item.gif.source === 'motion'; + node.appendChild(kv('GIF', item.gif.width + '×' + item.gif.height + ' · ' + item.gif.frames + (panels ? ' 格 · 每格 ' : ' 帧 · 每帧 ') + + Math.round(item.gif.keyMs || item.gif.frameMs) / 1000 + ' 秒' + (item.gif.durationMs ? ' · 共 ' + Math.round(item.gif.durationMs / 100) / 10 + ' 秒' : '') + (item.gif.loop ? ' · 循环' : '') + + (item.gif.transition === 'cut' ? (panels && item.gif.bounce ? ' · 切换时弹一下' : panels && item.gif.tweens ? '' : ' · 直接切换') : item.gif.transition === 'fade' ? ' · 渐变过渡' : '') + + (panels ? ' · 组图切格拼成' + (item.gif.tweens ? ',补了 ' + item.gif.tweens + ' 格过渡' : '') : '') + (motion ? ' · 程序绕圆心转出来的' : '') + + (images[gifKey] ? '' : panels ? ' · 动图太大没放进分享页,这里是组图' : ' · 动图太大没放进分享页,这里是参考图'))); + } + for (const one of (item.chart && item.chart.items) || []) { + node.appendChild(kv('图表', one.typeLabel + (one.title ? '「' + one.title + '」' : '') + ' · ' + (one.ok ? '程序按数据画入,核对通过' : '没画上') + + ((one.unverified || []).length ? ' · 有 ' + one.unverified.length + ' 个数没在原话里找到' : ''))); + } node.appendChild(kv('尺寸', item.size || '-')); node.appendChild(kv('轮次', String(item.rounds))); if (item.verdict) node.appendChild(kv('hard', item.verdict.hardPassed + '/' + item.verdict.hardTotal)); diff --git a/plugins/liddalidd/h3-image-executor/miniapp/client/styles.css b/plugins/liddalidd/h3-image-executor/miniapp/client/styles.css index 25afbde1..39216273 100644 --- a/plugins/liddalidd/h3-image-executor/miniapp/client/styles.css +++ b/plugins/liddalidd/h3-image-executor/miniapp/client/styles.css @@ -1396,6 +1396,11 @@ details.group-item-detail[open] > .group-item-head .group-item-text { color: var line-height: 1.6; color: var(--mcode-text-muted); } +.app .fail-hint.deliver-interrupted { color: var(--mcode-warning); } +.app .meta.deliver-text { + font-size: 11px; + line-height: 1.6; +} /* ---------------------------------------------------------------- 分享页 */ @@ -2220,3 +2225,8 @@ body.app { color: var(--mcode-text-subtle); } +/* GIF 的帧条:放在说明里,比大图矮一截,和上下文字留出间距。 */ +.app .deliver-item img.deliver-strip { + margin: 8px 0 4px; + border: 1px solid var(--mcode-border); +} diff --git a/plugins/liddalidd/h3-image-executor/miniapp/node/lib/agent.mjs b/plugins/liddalidd/h3-image-executor/miniapp/node/lib/agent.mjs index 5bd26ca6..6fd35062 100644 --- a/plugins/liddalidd/h3-image-executor/miniapp/node/lib/agent.mjs +++ b/plugins/liddalidd/h3-image-executor/miniapp/node/lib/agent.mjs @@ -18,6 +18,14 @@ function notPermittedError() { return error; } +// 回合被判 error 后,第 1、2 次重试前各等多久。模型服务的 502 往往持续几十秒,立刻重发只会再撞一次。 +// 导出成可改的对象,测试里改小。 +export const RETRY_WAIT = { ms: [8000, 20000] }; + +function sleep(ms) { + return new Promise((resolve) => setTimeout(resolve, ms)); +} + export function createAgentRunner(context, host) { const pendingPermissions = new Map(); const running = new Set(); @@ -170,6 +178,9 @@ export function createAgentRunner(context, host) { // 跑一个需要 JSON 回复的节点。 // - 工具调用超过 maxTools 就在运行时中断,然后在同一会话里强制收尾(保留已经查到的信息)。 // - 回复不是合法 JSON 时在同一会话里要求重写。 + // - 回合被 Host 判成 error(多半是模型服务临时故障):等一会儿,换新会话按原样重跑,工具上限不变。 + // 以前重跑时上限会变成不限,而且是立刻重发:真机 20261112-23b706 / 463fcd 里模型服务正在报 502, + // 第二次 0.6 秒就失败了,第三次没有上限地跑了两分钟。 async function runJson(session, options) { const step = options.step; const maxTools = options.maxTools || 0; @@ -177,19 +188,25 @@ export function createAgentRunner(context, host) { let ms = 0; let toolCalls = 0; let text = options.instruction; + // 本次调用的工具上限。强制收尾时才降到 0(只要 JSON,不该再调工具)。 + let budget = maxTools; + let mode = 'start'; const allTools = []; + // 撞没撞过上限:调用方据此让同一个节点下次多给几次(见 budget.mjs)。 + let hitCap = false; for (let attempt = 0; attempt < 3; attempt += 1) { - host.onProgress(session, step, attempt === 0 ? '开始' : '收尾中', { + host.onProgress(session, step, mode === 'start' ? '开始' : mode === 'retry' ? '换新会话重试' : '收尾中', { detail: detail('节点输入 · ' + step, [ - kv('轮次', attempt === 0 ? '首次调用' : '第 ' + (attempt + 1) + ' 次(收尾)'), - kv('工具预算', (attempt === 0 ? maxTools : 0) + ' 次'), + kv('轮次', attempt === 0 ? '首次调用' : '第 ' + (attempt + 1) + ' 次(' + (mode === 'retry' ? '重试' : '收尾') + ')'), + kv('工具预算', budget ? budget + ' 次' : mode === 'finish' ? '0 次(只要最终 JSON)' : '不限'), + options.budgetNote && mode !== 'finish' ? kv('预算怎么定的', options.budgetNote) : null, textBlock('发给模型的完整提示词', text), ]), }); let turn; try { - turn = await runTurn(session, text, sessionId, step, attempt === 0 ? maxTools : 0); + turn = await runTurn(session, text, sessionId, step, budget); } catch (error) { // 中断后同一会话可能不可用,退回到全新会话强制收尾。 // 权限被关掉时换会话也没用,直接把带说明的错误交上去。 @@ -212,7 +229,10 @@ export function createAgentRunner(context, host) { session.cost.byStep[step] = bucket; if (turn.overBudget) { + hitCap = true; text = '工具调用预算已经用完,不要再调用任何工具。现在就用你已经掌握的信息,直接输出最终 JSON 对象。'; + budget = 0; + mode = 'finish'; continue; } if (turn.result.status !== 'finished') { @@ -222,12 +242,16 @@ export function createAgentRunner(context, host) { const suffix = info ? ':' + [info.code, info.message].filter(Boolean).join(' ') : ''; const failure = 'agent_' + turn.result.status + suffix; // Host 把这一轮判成 error(实测过一次:第 2 次 LLM 返回后卡 191 秒,整轮 205 秒后 error), - // 是上游的问题不是我们的问题。换个干净会话再试一次,别让一次抽风把整个节点报废。 + // 是上游的问题不是我们的问题。等一会儿换个干净会话再试,别让一次抽风把整个节点报废。 // aborted 是我们自己中断的(工具预算耗尽 / 用户点停止),重试没有意义。 if (turn.result.status === 'error' && attempt < 2 && session.status !== 'cancelled') { - host.onProgress(session, step, '本轮被判失败,换新会话重试:' + failure, { level: 'warn' }); + host.onProgress(session, step, '本轮被判失败,' + Math.round(RETRY_WAIT.ms[attempt] / 1000) + ' 秒后换新会话重试:' + failure, { level: 'warn' }); + await sleep(RETRY_WAIT.ms[attempt]); + if (session.status === 'cancelled') throw new Error(failure); sessionId = null; text = options.instruction; + budget = maxTools; + mode = 'retry'; continue; } // 不是工具预算(上面已经 continue)也不是用户点了停止,那多半是用户在插件详情里 @@ -235,7 +259,10 @@ export function createAgentRunner(context, host) { if (turn.result.status === 'aborted' && session.status !== 'cancelled') { throw new Error(failure + ':Agent 回合被中止。如果你刚关闭了 Agent 权限,' + AGENT_OFF_HINT); } - throw new Error(failure); + // 重试用完还是 error:多半是模型服务这段时间不可用,标成临时故障,上层据此先交付手上已有的图。 + const error = new Error(failure); + if (turn.result.status === 'error') error.transient = true; + throw error; } const lastText = turn.result.text || ''; const data = extractJson(lastText); @@ -250,9 +277,11 @@ export function createAgentRunner(context, host) { textBlock('模型最终回复', lastText), ]), }); - return { data, text: lastText, sessionId }; + return { data, text: lastText, sessionId, toolCalls, overBudget: hitCap }; } text = '上一次回复无法解析成 JSON。请只输出一个合法 JSON 对象,不要 markdown 代码块、不要任何解释文字。'; + budget = 0; + mode = 'finish'; } throw new Error('agent_json_parse_failed'); } diff --git a/plugins/liddalidd/h3-image-executor/miniapp/node/lib/animation.mjs b/plugins/liddalidd/h3-image-executor/miniapp/node/lib/animation.mjs new file mode 100644 index 00000000..8d272f98 --- /dev/null +++ b/plugins/liddalidd/h3-image-executor/miniapp/node/lib/animation.mjs @@ -0,0 +1,1216 @@ +// GIF 动图:一张参考图 → 若干帧 → 程序合成的 GIF。 +// +// 参考图从哪来:用户给了图,规划写它的 R 编号,各帧直接照这张图改;用户没给,这张图先按正常流程画出来、交付, +// 等用户确认(或者先改一改)再出帧。帧全都照着参考图改,参考图不对,后面每一帧都白画。 +// 第一帧从参考图改,后面每一帧在前一帧上接着改一小步,参考图作为第 2 张图一起送进去,锁住身份、服装、背景和颜色 +// (见 framePrompt)。以前每一帧单独从参考图改,同一句「双手抬到胸口」每次画得都不一样,相邻两帧的手一会儿高一会儿低。 +// 每一帧的 prompt 照样写这一帧的完整状态:前面帧里做出的变化(伞斜过来了),后面的帧要保持就再写一遍。 +// 模型每次改图都会把整幅细纹理重画、画面轻微错位,合成时 gif.mjs 把每一帧锁回参考图,背景才不闪。 +// 一步跨得太大的地方补过渡帧,没过评审的帧不放进 GIF,每一帧停一样久(见 smoothSteps、playable、playOrder)。 +// 评审一次看全部帧:逐帧评审每次要几分钟,12 帧就是十几次;放在一起看还能判断整组连起来顺不顺。 +// 只重画判了不合格的帧,最多一次。帧画完由 gif.mjs 合成,再从写出去的文件读回来核对帧数、尺寸和时长。 + +import { readFile, writeFile, copyFile, access } from 'node:fs/promises'; +import { basename, extname, join } from 'node:path'; +import * as h3 from './h3.mjs'; +import { decodeImage, extensionFor, sniffFormat } from './image.mjs'; +import { downsample, encodePng } from './png.mjs'; +import { buildGridSheet } from './review.mjs'; +import { decodeGif, fitFrame, gifSize, makeGif, motionSteps } from './gif.mjs'; +import { detail, kv, list, text } from './detail.mjs'; +import { difficultyOf, hitsOf, noteToolHit, toolBudget } from './budget.mjs'; + +export const ANIMATION = { + // 帧数按动作幅度定:大动作要拆成十几小步才顺,以前的 12 帧上限不够再补过渡帧(见 smoothSteps)。 + maxFrames: 16, + frameMs: 300, + minFrameMs: 60, + maxFrameMs: 3000, + edge: 768, + minEdge: 240, + maxEdge: 1024, + // GIF 最大 1024 像素,帧用 2K 出图就够,比 4K 快。 + frameArea: '2K', + // 评审判不合格的帧最多重画几次。 + frameRedo: 1, + previewEdge: 512, + reviewWidth: 1536, + // transition 为 fade 时,相邻关键帧之间补几帧渐变。 + tween: 2, + // 渐变只补在变化不超过这么多(占整幅)的两帧之间:差得多的两帧叠在一起是两个半透明的人影。 + // 比补中间帧的门槛(SMOOTH.stepLimit)略高:补过中间帧、变化压到门槛以下的每一步都照常补渐变。 + tweenMaxChange: 0.1, +}; + +export const TRANSITIONS = ['fade', 'cut']; + +// 待确认的参考图卡片上那个按钮发的话。原样收到这句就直接开始出帧,不经过编辑节点。 +export const ANIMATE_CONFIRM = '用这张参考图生成 GIF'; + +export function confirmText(imageId) { + return ANIMATE_CONFIRM + '(' + imageId + ')'; +} + +export function confirmTarget(instruction) { + const match = /^用这张参考图生成 GIF(([A-Za-z0-9]{1,8}))$/.exec(String(instruction || '').trim()); + return match ? match[1].toUpperCase() : ''; +} + +// 规划写的 animation → 规格。frames 少于 2 帧、或者全是「参考图本身」,就不是动图。 +export function normalizeAnimation(raw) { + if (!raw || typeof raw !== 'object') return null; + const frames = (Array.isArray(raw.frames) ? raw.frames : []).slice(0, ANIMATION.maxFrames).map(normalizeFrame); + if (frames.length < 2 || frames.every((frame) => frame.same)) return null; + const reference = typeof raw.reference === 'string' && /^R\d+$/i.test(raw.reference.trim()) ? raw.reference.trim().toUpperCase() : 'self'; + return { + reference, + frames, + frameMs: clampMs(raw.frame_ms ?? raw.frameMs) || ANIMATION.frameMs, + loop: raw.loop !== false, + edge: clampEdge(raw.edge), + transition: normalizeTransition(raw.transition) || 'fade', + }; +} + +function normalizeTransition(value) { + const text = String(value || '').trim().toLowerCase(); + return TRANSITIONS.includes(text) ? text : ''; +} + +// 恢复出来的旧会话没有 transition,按默认的渐变处理。 +export function transitionOf(spec) { + return normalizeTransition(spec && spec.transition) || 'fade'; +} + +function normalizeFrame(item) { + const value = typeof item === 'string' ? { prompt: item } : item && typeof item === 'object' ? item : {}; + const prompt = String(value.prompt || '').trim().slice(0, 600); + return { prompt, same: value.same === true || !prompt, holdMs: clampMs(value.hold_ms ?? value.holdMs) }; +} + +function clampMs(value) { + const n = Number(value); + if (!Number.isFinite(n) || n <= 0) return 0; + return Math.round(Math.max(ANIMATION.minFrameMs, Math.min(ANIMATION.maxFrameMs, n))); +} + +function clampEdge(value) { + const n = Number(value); + if (!Number.isFinite(n) || n <= 0) return ANIMATION.edge; + return Math.round(Math.max(ANIMATION.minEdge, Math.min(ANIMATION.maxEdge, n))); +} + +export function isAnimation(image) { + return Boolean(image && image.spec && image.spec.animation); +} + +// 规划直接用用户给的图当参考图(不用先画)。R 编号在会话里找不到的,退回先画一张。 +export function usesGivenReference(session, image) { + const spec = image.spec && image.spec.animation; + return Boolean(spec && spec.reference !== 'self' && (session.refs || []).some((ref) => ref.id === spec.reference)); +} + +// 运行状态:state 是 awaiting_confirm(参考图交付了,等用户确认)/ frames(出帧中)/ done / failed。 +// 每帧有一个不复用的 uid,候选文件名用它(A F-r-g0),删帧、加帧、调顺序都不会让文件互相覆盖。 +export function ensureState(image) { + if (!image.animation) image.animation = { state: '', refId: '', refFile: '', width: 0, height: 0, nextUid: 1, frames: [], reviews: [], error: '' }; + const anim = image.animation; + if (!anim.frames.length) anim.frames = image.spec.animation.frames.map((frame) => newFrame(anim, frame)); + return anim; +} + +function newFrame(anim, frame) { + const uid = anim.nextUid; + anim.nextUid += 1; + return { + uid, + prompt: frame.prompt, + same: Boolean(frame.same), + holdMs: frame.holdMs || 0, + status: frame.same ? 'passed' : 'pending', + problem: '', + retryPrompt: '', + note: '', + own: Boolean(frame.own), + rounds: 0, + // inbetween:程序补的过渡帧;between:它夹在哪两帧中间(两帧的 uid,画的时候把这两帧的图都送进去)。 + inbetween: Boolean(frame.inbetween), + between: Array.isArray(frame.between) ? frame.between.slice(0, 2) : null, + candidates: [], + }; +} + +// 参考图交付之后改它(换颜色、换衣服):旧的帧作废,等用户确认新的参考图再重新出帧。 +// 帧的安排(追问里加过、删过的帧)保留,uid 接着往上编,旧帧的文件不会被覆盖。 +export function resetForNewReference(image) { + const old = image.animation && image.animation.frames.length ? image.animation.frames : image.spec.animation.frames; + const nextUid = image.animation ? image.animation.nextUid : 1; + image.spec.animation.reference = 'self'; + image.animation = { state: 'awaiting_confirm', refId: '', refFile: '', width: 0, height: 0, nextUid, frames: [], reviews: [], error: '' }; + image.animation.frames = old.map((frame) => newFrame(image.animation, frame)); + return image.animation; +} + +export function latestCandidate(frame) { + return frame && frame.candidates.length ? frame.candidates[frame.candidates.length - 1] : null; +} + +// 来回往复的动作(手在脸旁 → 举过头顶 → 回到脸旁)里,同一个姿态会出现好几次。各画各的,同一个姿态每次都略有 +// 不同,播到这里画面会抖;所以 prompt 一模一样的帧只画一次,后面的直接用最早那一帧的图。 +// 追问里单独重画其中一帧(不给新说法)时打上 own,它就自己画。 +export function mirrorSource(anim, frame) { + if (!frame || frame.same || frame.own) return null; + const key = promptKey(frame.prompt); + for (const other of anim.frames) { + if (other === frame) return null; + if (!other.same && promptKey(other.prompt) === key) return other; + } + return null; +} + +function promptKey(value) { + return String(value || '').replace(/[\s。;;,,、!!]+/g, ''); +} + +// 这一帧实际用哪张图:参考图本身返回 null,同姿态的帧用最早那一帧的候选。 +export function frameCandidate(anim, frame) { + if (frame.same) return null; + return latestCandidate(mirrorSource(anim, frame) || frame); +} + +const RATIOS = ['1:1', '4:3', '3:4', '3:2', '2:3', '16:9', '9:16']; + +function aspectFor(width, height) { + const ratio = width / height; + let best = 'auto'; + let bestError = 0.03; + for (const item of RATIOS) { + const [a, b] = item.split(':').map(Number); + const error = Math.abs(a / b - ratio) / ratio; + if (error < bestError) { + bestError = error; + best = item; + } + } + return best; +} + +async function exists(path) { + try { + await access(path); + return true; + } catch { + return false; + } +} + +function nextRefId(session) { + let max = 0; + for (const ref of session.refs || []) { + const match = /^R(\d+)$/.exec(String(ref.id || '')); + if (match) max = Math.max(max, Number(match[1])); + } + return 'R' + (max + 1); +} + +// 参考图:已经定过的接着用;规划点名的 R 编号;否则拿这张图当前交付的那一版,登记成新的 R 编号 +// (之后评审、编辑都认它),文件拷进 refs/。上传一次,各帧共用同一个 mm_file://。 +async function prepareContext(session, image, deps, hooks) { + const anim = image.animation; + const spec = image.spec.animation; + let ref = anim.refId ? (session.refs || []).find((item) => item.id === anim.refId) : null; + if (ref && !(await exists(ref.path))) ref = null; + if (!ref && spec.reference !== 'self') ref = (session.refs || []).find((item) => item.id === spec.reference) || null; + if (!ref) { + const source = image.chosen || hooks.lastDelivered(session, image, deps); + if (!source || !source.path) throw new Error('这张 GIF 还没有参考图'); + const buffer = await readFile(source.path); + const id = nextRefId(session); + const file = id + extensionFor(buffer); + const path = join(deps.dirs.refs, file); + await writeFile(path, buffer); + ref = { id, file, path, note: 'GIF 参考图(' + image.id + ')', origin: 'generated' }; + session.refs.push(ref); + // 评审用的参考图拼图是缓存的,新登记了参考图就要重拼。 + session.refSheet = null; + } + const buffer = await readFile(ref.path); + const format = sniffFormat(buffer); + if (format !== 'png' && format !== 'jpeg') throw new Error('参考图是 ' + format + ' 格式,程序解不开,GIF 需要 PNG 或 JPEG 的参考图'); + const decoded = decodeImage(buffer); + anim.refId = ref.id; + anim.refFile = ref.file; + anim.width = decoded.width; + anim.height = decoded.height; + const preview = downsample(decoded, ANIMATION.previewEdge); + const previewPath = join(deps.dirs.previews, ref.id + '-gif.png'); + await writeFile(previewPath, encodePng(preview)); + const mm = await h3.resolveReference(deps.config, ref.path); + return { ref, mm, aspect: aspectFor(decoded.width, decoded.height), width: decoded.width, height: decoded.height, preview, previewPath, stopped: '' }; +} + +// 每一帧怎么画(mode): +// ref:从参考图改(第 1 帧,或者前面一帧都没画成时); +// chain:在上一帧上接着改、只动一小步,参考图作为第 2 张图锁住身份、服装、背景和颜色; +// between:补的过渡帧,第 1 张是前一帧、第 3 张是后一帧,画两者正中间的状态。 +// 以前每一帧都单独从参考图改:模型对「双手抬到胸口」这类说法每次理解得都不一样,相邻两帧的手一会儿高一会儿低, +// 锁回参考图之后相邻两帧还差 10–20%(真机 20261117-cd2dc5)。接着上一帧改,两帧之间只差这一小步。 +// 一帧比一帧偏的老问题由参考图(第 2 张)、合成时锁回参考图的背景和色调兜住。 +export function framePrompt(frame, position, total, mode) { + const change = String(frame.retryPrompt || frame.prompt).replace(/[。;;,,\s]+$/, ''); + const problem = frame.problem ? '(上一版这一帧的问题:' + frame.problem + '。这一次必须改正。)' : ''; + const keep = + '主体的身份和五官、服装、画风、背景、光线和颜色都以参考图为准;构图、主体在画面里的位置和大小不变,' + + '不要移动镜头、不要缩放、不要裁切;参考图里有的文字和符号一个都不要少,也不要添加新的物体、文字或水印。'; + if (mode === 'chain') { + return ( + '这是一组 GIF 动图里的第 ' + position + ' 帧(共 ' + total + ' 帧)。第 1 张图是上一帧,在它上面接着改,只动一小步,改到这个样子:' + + change + '。第 2 张图是这组动图的参考图:' + keep + problem + ); + } + if (mode === 'between') { + return ( + '这是一组 GIF 动图里补的过渡帧(第 ' + position + ' 帧,共 ' + total + ' 帧)。第 1 张图是前一帧,第 3 张图是后一帧,' + + '以第 1 张为底图来改,画两者正中间的状态:每个在动的部位(头的角度和朝向、手和手臂的位置、肩膀、眉毛、眼睛、嘴型)都走到一半,' + + '不要画成第 1 张或第 3 张的样子。第 2 张图是这组动图的参考图:' + keep + '(两帧的说明:' + change + ')' + problem + ); + } + return ( + '这是一组 GIF 动图里的第 ' + position + ' 帧(共 ' + total + ' 帧),在输入的这张参考图上改。只做这些改动:' + + change + + '。除此之外的一切——主体的外形、配色和身份,画风,背景,光线,构图,主体在画面里的位置和大小——都和参考图保持完全一致;' + + '不要移动镜头、不要缩放、不要裁切,不要添加文字、水印或新的物体。' + + problem + ); +} + +// 出帧 + 评审 + 重画判了不合格的帧。只处理还没画好的帧(pending / failed),已经通过的帧不动。 +export async function runFrames(session, image, deps, hooks) { + const anim = ensureState(image); + anim.state = 'frames'; + anim.error = ''; + // 先画出来的参考图本身是好的:帧没做成时,这张图照样按它自己的结论交付。 + const before = image.status; + image.status = 'running'; + const started = Date.now(); + let ctx; + try { + ctx = await prepareContext(session, image, deps, hooks); + } catch (error) { + image.status = image.chosen ? before : 'failed'; + throw error; + } + const todo = anim.frames.filter((frame) => !frame.same && !mirrorSource(anim, frame) && (frame.status === 'pending' || frame.status === 'failed')); + const mirrored = anim.frames.filter((frame) => mirrorSource(anim, frame)).length; + deps.emit(session, 'paint', image.id + ' 开始出 GIF 的 ' + todo.length + ' 帧:每一帧都从参考图 ' + ctx.ref.id + ' 改出来' + (mirrored ? '(' + mirrored + ' 帧和前面的姿态相同,直接复用)' : ''), { + detail: detail('GIF · ' + image.id, [ + kv('参考图', ctx.ref.id + '(' + ctx.width + '×' + ctx.height + ')'), + kv('帧数', anim.frames.length + ' 帧,要画 ' + todo.length + ' 帧,每帧 ' + image.spec.animation.frameMs + ' 毫秒' + (image.spec.animation.loop ? ',循环' : ',只播一遍')), + list('每一帧', anim.frames.map((frame, index) => ({ text: '第 ' + (index + 1) + ' 帧:' + describeFrame(anim, frame) }))), + ]), + }); + await generateFrames(session, image, ctx, deps, hooks, todo); + await reviewFrames(session, image, ctx, deps, hooks, todo); + await redoRejected(session, image, ctx, deps, hooks, todo); + // 重画过还没过评审的帧不放进 GIF(见 playable):先定下来,再量相邻两帧、补过渡帧,过渡帧才补在真正播放的两帧之间。 + // 以前这一步放在补过渡帧后面:过渡帧照着之后被扔掉的帧画(真机 20261118-d490f3 第 9 帧夹在被扔掉的第 8 帧和第 10 帧中间), + // 扔掉的帧留下的空当反而没人补(真机 20261119-e645c0 十二帧扔了六帧,亮点一下跳一格、一下跳三格)。 + for (const frame of todo) if (frame.status === 'rejected') frame.status = 'unpassed'; + // 一步跨得太大的地方(包括扔掉帧留下的空当)补中间帧(补的帧照样评审、不合格的重画一次,还不合格就不用)。 + if (!ctx.stopped && !hooks.isCancelled(session)) await smoothSteps(session, image, ctx, deps, hooks); + for (const frame of todo) { + if (frame.status === 'rejected') frame.status = 'unpassed'; + if (frame.status === 'running' || frame.status === 'pending') frame.status = latestCandidate(frame) ? 'unreviewed' : 'failed'; + } + syncMirrors(anim); + const usable = anim.frames.filter((frame) => frame.same || frameCandidate(anim, frame)); + anim.state = usable.length >= 2 ? 'done' : 'failed'; + if (anim.state === 'failed') anim.error = ctx.stopped || '能用的帧不到 2 帧'; + const clean = anim.frames.every((frame) => frame.same || frame.status === 'passed'); + image.status = anim.state === 'done' ? (clean ? 'passed' : 'unpassed') : image.chosen ? before : 'failed'; + deps.emit(session, 'paint', image.id + ' GIF 的帧画完了:' + summarizeFrames(anim), { durMs: Date.now() - started, level: clean ? 'info' : 'warn' }); +} + +async function redoRejected(session, image, ctx, deps, hooks, todo) { + const anim = image.animation; + for (let attempt = 0; attempt < ANIMATION.frameRedo && !ctx.stopped && !hooks.isCancelled(session); attempt += 1) { + const redo = todo.filter((frame) => frame.status === 'rejected' || frame.status === 'failed'); + if (!redo.length) break; + deps.emit(session, 'refine', image.id + ' 重画 ' + redo.length + ' 帧:' + redo.map((frame) => '第 ' + (anim.frames.indexOf(frame) + 1) + ' 帧').join('、')); + await generateFrames(session, image, ctx, deps, hooks, redo); + await reviewFrames(session, image, ctx, deps, hooks, redo); + } +} + +// ------------------------------------------------------------------ 补中间帧 +// +// 帧画完之后量一遍相邻两帧变了多少(锁回参考图之后,和合成时一样),一步跨得太大的地方补一帧过渡: +// 第 1 张送前一帧、第 3 张送后一帧,让模型以前一帧为底画两者正中间的状态,评审照常核对,没过评审的不用。 +// 只靠文字说「走到三分之一」、从参考图重画,模型画不准:真机 20261117-cd2dc5 补的 4 帧,补完相邻两帧还差 13–20%。 +// 来回往复的动作里 A→B 和 B→A 补的是同一帧,只画一次。时长在合成时统一分配(见 playOrder),补帧不会让节奏忽快忽慢。 +const SMOOTH = { stepLimit: 0.09, maxNew: 6 }; + +async function smoothSteps(session, image, ctx, deps, hooks) { + const anim = image.animation; + const spec = image.spec.animation; + const usable = playable(anim); + if (usable.length < 2) return; + const decoded = new Map(); + const images = []; + try { + for (const frame of usable) { + if (frame.same) { + images.push(ctx.preview); + continue; + } + const candidate = frameCandidate(anim, frame); + const path = candidate.preview || candidate.path; + if (!decoded.has(path)) decoded.set(path, decodeImage(await readFile(path))); + images.push(decoded.get(path)); + } + } catch { + return; + } + const steps = motionSteps(images, ctx.preview, { loop: spec.loop, edge: 384 }); + // 只播一遍的:最后一帧到第一帧不算一步(motionSteps 已经不算);循环播放的也算上接缝那一步。 + const keyOf = (frame) => (frame.same ? '0' : 'c' + frameCandidate(anim, frame).id); + const plans = []; + const prompts = new Set(); + // 按真正播放的帧数封顶:扔掉的帧不播,不占名额。 + let total = usable.length; + // 扔掉了帧的空当先补,而且不管量出来变了多少都补:那一帧是规划里本来就要有的一步。真机试过:柴犬抬爪扔掉「举到头顶」那一帧, + // 爪子从脸旁直接跳到头顶上方挥手,只变了画面的一小块,量出来不到补帧的门槛,结果就没补。 + const dropped = (step) => droppedBetween(anim, usable[step.from], usable[step.to]).length; + const ranked = steps.slice().sort((x, y) => (dropped(y) > 0) - (dropped(x) > 0) || y.change - x.change); + for (const step of ranked) { + if ((step.change <= SMOOTH.stepLimit && !dropped(step)) || total >= ANIMATION.maxFrames) continue; + const a = usable[step.from]; + const b = usable[step.to]; + // 方向统一成两帧的先后:A→B 和 B→A 写出来是同一句话、同一对底图,第二次直接复用。 + const [first, second] = keyOf(a) <= keyOf(b) ? [a, b] : [b, a]; + // 两帧中间正好扔了一帧:过渡帧就是替它补上,把它原来的计划写进去,模型知道中间该是什么样。 + const gap = droppedBetween(anim, a, b); + const prompt = '姿态 A(第 1 张):' + describePose(first) + ';姿态 B(第 3 张):' + describePose(second) + + (gap.length === 1 && gap[0].prompt ? ';这一帧原来的计划:' + describePose(gap[0]) : ''); + if (!prompts.has(prompt) && prompts.size >= SMOOTH.maxNew) continue; + prompts.add(prompt); + plans.push({ a, b, step, prompt, between: [first.uid, second.uid] }); + total += 1; + } + if (!plans.length) return; + const added = []; + for (const plan of plans) { + const frame = newFrame(anim, { prompt: plan.prompt, same: false, holdMs: 0, inbetween: true }); + frame.between = plan.between; + anim.frames.splice(anim.frames.indexOf(plan.a) + 1, 0, frame); + added.push(frame); + } + if (plans.some((plan) => droppedBetween(anim, plan.a, plan.b).length)) { + deps.emit(session, 'paint', image.id + ' 没过评审、扔掉的帧留下的空当,也按前后两帧补过渡帧'); + } + const todo = added.filter((frame) => !mirrorSource(anim, frame)); + deps.emit(session, 'paint', image.id + ' 有 ' + plans.length + ' 步动作跨得太大,每步补 1 帧过渡(要画 ' + todo.length + ' 帧)', { + detail: detail('补过渡帧 · ' + image.id, [ + list('相邻两帧变了多少(锁回参考图之后,占整幅)', steps.map((step) => ({ + tag: step.change > SMOOTH.stepLimit ? 'warn' : 'pass', + text: '第 ' + (anim.frames.indexOf(usable[step.from]) + 1) + ' → ' + (step.to < step.from ? '第 1' : '下一') + ' 帧:' + Math.round(step.change * 100) + '%', + }))), + ]), + }); + await generateFrames(session, image, ctx, deps, hooks, todo); + await reviewFrames(session, image, ctx, deps, hooks, todo); + await redoRejected(session, image, ctx, deps, hooks, todo); + syncMirrors(anim); + // 补的帧是锦上添花:没画出来、没过评审的不用。 + const bad = added.filter((frame) => frame.status !== 'passed'); + if (bad.length) { + anim.frames = anim.frames.filter((frame) => !bad.includes(frame)); + deps.emit(session, 'paint', image.id + ' 补的过渡帧有 ' + bad.length + ' 帧没过评审,不用', { level: 'warn' }); + } +} + +function describePose(frame) { + return frame.same ? '和参考图一模一样' : String(frame.prompt).replace(/[。;;,,\s]+$/, ''); +} + +// 按播放顺序从 a 走到 b(循环时可以绕回开头),中间扔掉的规划帧(没过评审、没画出来)。 +function droppedBetween(anim, a, b) { + const frames = anim.frames; + const out = []; + const start = frames.indexOf(a); + const end = frames.indexOf(b); + if (start < 0 || end < 0) return out; + for (let i = (start + 1) % frames.length; i !== end; i = (i + 1) % frames.length) { + const frame = frames[i]; + if (!frame.inbetween && !frame.same && (frame.status === 'unpassed' || frame.status === 'failed' || !frameCandidate(anim, frame))) out.push(frame); + if (out.length > frames.length) break; + } + return out; +} + +// 放进 GIF 的帧:参考图本身和画好的帧。没过评审的帧不放:动作往反方向走、少了东西的一帧放进去, +// 播起来正好是那一下卡顿(真机 20261117-cd2dc5 第 2 帧手先往下掉再往上抬)。去掉之后少于 2 帧就照旧全放。 +function playable(anim) { + const all = anim.frames.filter((frame) => frame.same || frameCandidate(anim, frame)); + const good = all.filter((frame) => frame.same || frame.status !== 'unpassed'); + return good.length >= 2 ? good : all; +} + +function summarizeFrames(anim) { + const count = (status) => anim.frames.filter((frame) => !frame.same && frame.status === status).length; + const parts = []; + if (count('passed')) parts.push(count('passed') + ' 帧通过'); + if (count('unreviewed')) parts.push(count('unreviewed') + ' 帧没评审'); + if (count('unpassed')) parts.push(count('unpassed') + ' 帧没过评审'); + if (count('failed')) parts.push(count('failed') + ' 帧没画出来'); + const same = anim.frames.filter((frame) => frame.same).length; + if (same) parts.push(same + ' 帧直接用参考图'); + return parts.join(','); +} + +// 复用别的帧的帧,状态跟着被复用的那一帧走。 +function syncMirrors(anim) { + for (const frame of anim.frames) { + const source = mirrorSource(anim, frame); + if (!source) continue; + frame.status = source.status; + frame.problem = source.problem; + } +} + +function describeFrame(anim, frame) { + if (frame.same) return '参考图本身'; + const source = mirrorSource(anim, frame); + if (source) return '和第 ' + (anim.frames.indexOf(source) + 1) + ' 帧同一个姿态(直接用那一帧的图)'; + return (frame.inbetween ? '(程序补的中间帧)' : '') + frame.prompt; +} + +// 按播放顺序一帧一帧画:每一帧要在上一帧上接着改,没法并行(见 framePrompt)。 +async function generateFrames(session, image, ctx, deps, hooks, frames) { + const order = image.animation.frames; + const queue = frames.slice().sort((a, b) => order.indexOf(a) - order.indexOf(b)); + for (const frame of queue) { + if (ctx.stopped || hooks.isCancelled(session)) break; + await generateFrame(session, image, frame, ctx, deps, hooks); + } +} + +// 这一帧画的时候送进去哪几张图、用哪种说法。 +async function frameInputs(anim, frame, ctx, deps) { + if (frame.inbetween && Array.isArray(frame.between)) { + const [a, b] = frame.between.map((uid) => anim.frames.find((item) => item.uid === uid)); + const pa = a ? framePath(anim, a, ctx) : ''; + const pb = b ? framePath(anim, b, ctx) : ''; + if (pa && pb) return { mode: 'between', refs: [await uploadFrame(ctx, deps, pa), ctx.mm, await uploadFrame(ctx, deps, pb)] }; + } + const previous = previousImage(anim, frame); + if (previous) return { mode: 'chain', refs: [await uploadFrame(ctx, deps, previous), ctx.mm] }; + return { mode: 'ref', refs: [ctx.mm] }; +} + +function framePath(anim, frame, ctx) { + if (frame.same) return ctx.ref.path; + const candidate = frameCandidate(anim, frame); + return candidate && !['failed', 'rejected', 'unpassed'].includes(frame.status) ? candidate.path : ''; +} + +// 往前找最近一张能用的帧;前一帧就是参考图本身(或者前面一张都没画成)时返回空:从参考图改。 +function previousImage(anim, frame) { + for (let i = anim.frames.indexOf(frame) - 1; i >= 0; i -= 1) { + const other = anim.frames[i]; + if (other.same) return ''; + const path = framePath(anim, other, { ref: {} }); + if (path) return path; + } + return ''; +} + +// 帧的图是本地文件,送进接口前要上传换成 mm_file://;同一张只传一次。 +async function uploadFrame(ctx, deps, path) { + if (path === ctx.ref.path) return ctx.mm; + if (!ctx.uploads) ctx.uploads = new Map(); + if (!ctx.uploads.has(path)) ctx.uploads.set(path, await h3.resolveReference(deps.config, path)); + return ctx.uploads.get(path); +} + +async function generateFrame(session, image, frame, ctx, deps, hooks) { + const anim = image.animation; + const position = anim.frames.indexOf(frame) + 1; + // executeAction 认的是「图」:给它一个只有这一帧的替身,候选按替身的 id 和轮次编号。 + const proxy = { + id: image.id + 'F' + frame.uid, + spec: { aspectRatio: ctx.aspect, area: ANIMATION.frameArea, size: '', refs: [ctx.mm], characterRefs: [], candidates: 1 }, + rounds: [{ index: 0 }], + roundBase: frame.rounds, + }; + frame.rounds += 1; + frame.status = 'running'; + try { + const inputs = await frameInputs(anim, frame, ctx, deps); + frame.mode = inputs.mode; + const action = { type: 'edit', prompt: framePrompt(frame, position, anim.frames.length, inputs.mode), refs: inputs.refs, parentId: null }; + const candidates = await hooks.executeAction(session, proxy, action, deps); + if (!candidates.length) throw new Error('生图接口没有返回这一帧'); + const checked = await checkFrame(candidates[0], ctx, deps, action.prompt); + frame.candidates.push(checked); + frame.status = checked.problem ? 'rejected' : 'generated'; + frame.problem = checked.problem; + frame.note = ''; + } catch (error) { + frame.status = 'failed'; + frame.note = hooks.describeError(error); + deps.emit(session, 'paint', image.id + ' 第 ' + position + ' 帧没画出来:' + frame.note, { level: 'error' }); + if (hooks.isFatal(error) || hooks.isContentPolicy(error)) { + ctx.stopped = frame.note; + if (hooks.isFatal(error)) session.fatal = frame.note; + } + } +} + +// 程序校验:能不能解码、画幅对不对,顺手出一张 512 的预览(评审拼图和页面上的帧条都用它)。 +// 和参考图几乎一模一样的帧,多半是模型没照指令改,直接判不合格。 +async function checkFrame(candidate, ctx, deps, prompt) { + const base = { + id: candidate.id, + file: candidate.file, + path: candidate.path, + previewFile: '', + preview: '', + width: 0, + height: 0, + requestId: candidate.requestId || '', + prompt, + refDiff: 0, + problem: '', + verdict: null, + createdAt: Date.now(), + }; + let decoded; + try { + decoded = decodeImage(candidate.buffer || (await readFile(candidate.path))); + } catch { + return { ...base, problem: '程序解不开这一帧的图片' }; + } + const preview = downsample(decoded, ANIMATION.previewEdge); + const previewFile = candidate.id + '.png'; + await writeFile(join(deps.dirs.previews, previewFile), encodePng(preview)); + const refAspect = ctx.width / ctx.height; + const aspect = decoded.width / decoded.height; + const refDiff = Number(meanGrayDiff(preview, ctx.preview).toFixed(2)); + let problem = ''; + if (Math.abs(aspect - refAspect) / refAspect > 0.03) problem = '画幅和参考图不一样(' + decoded.width + '×' + decoded.height + ')'; + else if (refDiff < 0.3) problem = '和参考图几乎一模一样,没画出这一帧要的变化'; + return { ...base, previewFile, preview: join(deps.dirs.previews, previewFile), width: decoded.width, height: decoded.height, refDiff, problem }; +} + +// 两张图缩成 64×64 灰度后的平均差(0–255)。 +function meanGrayDiff(a, b) { + const S = 64; + const gray = (image, x, y) => { + const px = Math.min(image.width - 1, Math.floor(((x + 0.5) * image.width) / S)); + const py = Math.min(image.height - 1, Math.floor(((y + 0.5) * image.height) / S)); + const o = (py * image.width + px) * image.channels; + return image.channels >= 3 ? 0.299 * image.data[o] + 0.587 * image.data[o + 1] + 0.114 * image.data[o + 2] : image.data[o]; + }; + let sum = 0; + for (let y = 0; y < S; y += 1) for (let x = 0; x < S; x += 1) sum += Math.abs(gray(a, x, y) - gray(b, x, y)); + return sum / (S * S); +} + +async function reviewFrames(session, image, ctx, deps, hooks, frames) { + const anim = image.animation; + const shown = frames + .filter((frame) => frame.status === 'generated') + .map((frame) => ({ frame, position: anim.frames.indexOf(frame) + 1, candidate: latestCandidate(frame) })); + if (!shown.length || hooks.isCancelled(session)) return; + const sheetPath = join(deps.dirs.sheets, image.id + '-frames-' + Date.now() + '.png'); + const items = [{ path: ctx.previewPath, label: '0' }].concat(shown.map((item) => ({ path: item.candidate.preview, label: String(item.position) }))); + const grid = await buildGridSheet(items, sheetPath, { perRow: 4, width: ANIMATION.reviewWidth }); + // 拼图打开一次;看不清时还要打开单帧原图,按帧数给(每 4 帧 1 次)。 + // 固定 3 次不够:评审常先用一次 run_code 看文件,再 read 拼图和原图,第 4 次就被掐断, + // 强制收尾时手上一张图都没有,只能判 fail(真机 20261115-d7e30e 的第 4 帧复核)。 + const budget = toolBudget('frames', { + needs: [ + { count: 1, what: '拼图' }, + { count: Math.ceil(shown.length / 4), what: '单帧原图(每 4 帧 1 次)' }, + ], + difficulty: difficultyOf(image).level, + hits: hitsOf(image, 'frames'), + }); + const instruction = [ + '你是H3-Image-Executor 的动图评审节点。用户要一张 GIF 动图:系统先定了一张参考图,再让模型一帧接着一帧改出来:', + '第一帧从参考图改,后面每一帧在前一帧上只动一小步(参考图一直作为对照,锁住身份、服装、背景和颜色)。', + '', + '用户原话:', + session.instruction, + '', + '看图材料:' + (grid.sheet || ctx.previewPath) + '(先用 read 打开它,多数时候看它就够了)。格子左上角的数字:0 是参考图,其余是帧号。', + '拼图上看不清某一帧的细节,再 read 它的原图:', + ...shown.map((item) => '第 ' + item.position + ' 帧 -> ' + item.candidate.path), + '参考图原图 -> ' + ctx.ref.path, + '只用 read 看图,不要用 run_code 或别的工具。', + '', + '整组按播放顺序是这样的(标「参考图本身」的帧不用评):', + ...anim.frames.map((frame, index) => '第 ' + (index + 1) + ' 帧:' + describeFrame(anim, frame)), + '', + '这次要评的帧:' + shown.map((item) => item.position).join('、') + '。逐帧核对四件事:', + '1. 和参考图是同一个主体:外形、配色、五官、服装、画风一眼看出是同一个,没有变成另一个角色或换了画风。', + '2. 背景、光线、构图,主体在画面里的位置和大小都和参考图一致:没有平移、缩放、裁切,背景没有换。', + '3. 画出了这一帧要的变化,而且变化合理:没有多出或缺少肢体、手指,没有新加的物体、文字或水印。', + '4. 参考图里有的东西这一帧都还在:文字、符号(比如头顶的问号)、道具一个都没少,也没有变成别的样子;少了就是 fail,', + ' 播起来这一帧会闪一下。', + '再按帧号连起来看一遍:动作的先后顺序说得通,相邻两帧只差一小步,没有哪一帧突兀地跳出来(往反方向走、一下跳到很远)。', + '前面帧里已经做出、按计划后面还要保持的变化(伞斜过来了、猫挪近了),看后面的帧有没有保持住;', + '某一帧把前面的变化突然撤回(伞弹回原样、猫跳回原位),把那一帧判 fail。整组的问题出在哪一帧,就把那一帧判 fail。', + '', + '只输出一个 JSON 对象,不要解释、不要 markdown 代码块:', + '{"seen":true,"frames":[{"index":2,"result":"pass","problem":"","prompt":""}],"sequence":{"ok":true,"problem":""},"summary":"一句话总结"}', + '- seen:打开看到了看图材料写 true;没能打开、一张图都没看到写 false,frames 留空,不要凭空判 fail。', + '- frames 里每一个要评的帧写一项,index 是帧号,result 只写 pass 或 fail。', + '- fail 时 problem 写清哪里不对;prompt 写重画这一帧时给模型的新说法:写全这一帧和参考图相比要变的所有地方,', + ' 包括前面帧延续下来、这一帧要保持的状态(重画时模型会看到前一帧和参考图,但不知道计划里这一帧该是什么样),没变的部分不用写。', + '工具调用上限 ' + budget.limit + ' 次。', + ].join('\n'); + let data = null; + const started = Date.now(); + try { + const reviewed = await deps.agent.runJson(session, { step: 'frames', instruction, maxTools: budget.limit, budgetNote: budget.note }); + noteToolHit(image, 'frames', reviewed); + ({ data } = reviewed); + } catch (error) { + deps.emit(session, 'judge', image.id + ' 动图评审没跑成:' + hooks.describeError(error) + ',这几帧按程序校验的结果交付', { level: 'warn' }); + for (const item of shown) item.frame.status = 'unreviewed'; + return; + } + // 没看到图给出的 fail 不是结论:照它重画只会白画一次,还会把画对了的帧标成没过评审。 + if (data && data.seen === false) { + deps.emit(session, 'judge', image.id + ' 动图评审没看到图,这几帧按程序校验的结果交付', { level: 'warn', durMs: Date.now() - started }); + for (const item of shown) item.frame.status = 'unreviewed'; + return; + } + const results = Array.isArray(data && data.frames) ? data.frames : []; + const lines = []; + for (const item of shown) { + const verdict = results.find((one) => one && Number(one.index) === item.position); + if (!verdict) { + item.frame.status = 'unreviewed'; + lines.push({ tag: 'warn', text: '第 ' + item.position + ' 帧:评审没给结论' }); + continue; + } + const pass = String(verdict.result || '').toLowerCase() === 'pass'; + const problem = String(verdict.problem || '').trim().slice(0, 300); + item.candidate.verdict = { result: pass ? 'pass' : 'fail', problem }; + item.frame.status = pass ? 'passed' : 'rejected'; + item.frame.problem = pass ? '' : problem || '评审判这一帧不合格'; + item.frame.retryPrompt = pass ? '' : String(verdict.prompt || '').trim().slice(0, 600); + lines.push({ tag: pass ? 'pass' : 'fail', text: '第 ' + item.position + ' 帧:' + (pass ? '通过' : item.frame.problem) }); + } + const sequence = data && data.sequence && typeof data.sequence === 'object' + ? { ok: data.sequence.ok !== false, problem: String(data.sequence.problem || '').trim().slice(0, 300) } + : null; + anim.reviews.push({ at: Date.now(), frames: shown.map((item) => item.position), summary: String((data && data.summary) || '').slice(0, 400), sequence, sheetFile: grid.sheet ? basename(grid.sheet) : '' }); + const failed = lines.filter((line) => line.tag === 'fail').length; + deps.emit(session, 'judge', image.id + ' 动图评审:' + (shown.length - failed) + '/' + shown.length + ' 帧通过' + (sequence && !sequence.ok ? ';整组:' + sequence.problem : ''), { + durMs: Date.now() - started, + level: failed ? 'warn' : 'info', + detail: detail('动图评审 · ' + image.id, [list('逐帧', lines), sequence ? kv('整组', sequence.ok ? '连起来说得通' : sequence.problem) : null, data && data.summary ? text('总结', data.summary) : null]), + }); +} + +// ------------------------------------------------------------------ 追问:调动图本身 + +// 编辑节点写的 animation → 操作。帧号都是追问时页面上列出来的那套编号(从 1 开始)。 +// from_panels 及以下几项是组图拼 GIF 用的(见 panelgif.mjs):rows / cols / count 切格提示,first_ms / last_ms 第一格、 +// 最后一格停多久,pingpong 来回播,bounce 切换时弹不弹。 +export function normalizeAnimationOps(raw) { + if (!raw || typeof raw !== 'object') return null; + const ints = (list) => (Array.isArray(list) ? list.map((value) => Math.round(Number(value))).filter((value) => value >= 1) : []); + const grid = (value) => { + const n = Math.round(Number(value)); + return n >= 1 && n <= 8 ? n : 0; + }; + const ops = { + start: raw.start === true, + fromPanels: raw.from_panels === true || raw.fromPanels === true, + rows: grid(raw.rows), + cols: grid(raw.cols), + count: Math.round(Number(raw.count)) >= 2 && Math.round(Number(raw.count)) <= 64 ? Math.round(Number(raw.count)) : 0, + firstMs: clampMs(raw.first_ms ?? raw.firstMs), + lastMs: clampMs(raw.last_ms ?? raw.lastMs), + pingpong: typeof raw.pingpong === 'boolean' ? raw.pingpong : undefined, + bounce: typeof raw.bounce === 'boolean' ? raw.bounce : undefined, + // 组图拼 GIF 补不补过渡格(paneltween.mjs):true 补(之前没补成的再试一次),false 不补。 + smooth: typeof raw.smooth === 'boolean' ? raw.smooth : undefined, + // 程序转圈(motiongif.mjs):{ type:'spin', center, radius, steps, frames, direction, ... },只写了的项才改。 + motion: raw.motion && typeof raw.motion === 'object' && !Array.isArray(raw.motion) ? raw.motion : null, + frameMs: clampMs(raw.frame_ms ?? raw.frameMs), + loop: typeof raw.loop === 'boolean' ? raw.loop : undefined, + edge: raw.edge ? clampEdge(raw.edge) : 0, + transition: normalizeTransition(raw.transition), + hold: (Array.isArray(raw.hold) ? raw.hold : []).map((item) => ({ index: Math.round(Number(item && item.index)), ms: clampMs(item && item.ms) })).filter((item) => item.index >= 1 && item.ms), + drop: ints(raw.drop), + order: ints(raw.order), + redo: (Array.isArray(raw.redo) ? raw.redo : []).map((item) => ({ index: Math.round(Number(item && item.index)), prompt: String((item && item.prompt) || '').trim().slice(0, 600) })).filter((item) => item.index >= 1), + add: (Array.isArray(raw.add) ? raw.add : []).map((item) => ({ after: Math.max(0, Math.round(Number(item && item.after) || 0)), prompt: String((item && item.prompt) || '').trim().slice(0, 600) })).filter((item) => item.prompt), + }; + const any = ops.start || ops.fromPanels || ops.motion || hasAdjustments(ops) || ops.firstMs || ops.lastMs || ops.rows || ops.cols || ops.count || + ops.pingpong !== undefined || ops.bounce !== undefined || ops.smooth !== undefined; + return any ? ops : null; +} + +// 调动图本身的操作(不含 start)。 +export function hasAdjustments(ops) { + return Boolean(ops && (ops.frameMs || ops.loop !== undefined || ops.edge || ops.transition || ops.hold.length || ops.drop.length || ops.order.length || ops.redo.length || ops.add.length)); +} + +// 把操作并进规格和帧列表。返回改了什么(给事件流看)和要不要调生图(重画、加帧才要)。 +export function applyAnimationOps(image, ops) { + const spec = image.spec.animation; + const anim = ensureState(image); + const changes = []; + if (ops.frameMs) { + spec.frameMs = ops.frameMs; + changes.push('每帧 ' + ops.frameMs + ' 毫秒'); + } + if (ops.loop !== undefined) { + spec.loop = ops.loop; + changes.push(ops.loop ? '循环播放' : '只播一遍'); + } + if (ops.edge) { + spec.edge = ops.edge; + changes.push('长边 ' + ops.edge + ' 像素'); + } + if (ops.transition) { + spec.transition = ops.transition; + changes.push(ops.transition === 'cut' ? '帧和帧之间直接切换' : '帧和帧之间加渐变'); + } + const original = anim.frames.slice(); + const at = (index) => original[index - 1] || null; + for (const hold of ops.hold) { + const frame = at(hold.index); + if (!frame) continue; + frame.holdMs = hold.ms; + changes.push('第 ' + hold.index + ' 帧停 ' + hold.ms + ' 毫秒'); + } + for (const redo of ops.redo) { + const frame = at(redo.index); + if (!frame) continue; + if (redo.prompt) frame.prompt = redo.prompt; + if (frame.same && !redo.prompt) continue; + if (!redo.prompt && mirrorSource(anim, frame)) frame.own = true; + frame.same = false; + frame.status = 'pending'; + frame.problem = ''; + frame.retryPrompt = ''; + changes.push('重画第 ' + redo.index + ' 帧'); + } + let order = original.slice(); + if (ops.order.length) { + const picked = dedupeNumbers(ops.order).map(at).filter(Boolean); + if (picked.length >= 2) { + order = picked; + changes.push('播放顺序改成 ' + dedupeNumbers(ops.order).filter((index) => at(index)).join('、')); + } + } + if (ops.drop.length) { + const dropped = new Set(ops.drop.map(at).filter(Boolean)); + const kept = order.filter((frame) => !dropped.has(frame)); + if (kept.length >= 2) { + order = kept; + changes.push('去掉第 ' + ops.drop.filter((index) => at(index)).join('、') + ' 帧'); + } + } + for (const add of ops.add.slice().sort((a, b) => b.after - a.after)) { + const anchor = add.after ? at(add.after) : null; + const position = anchor && order.includes(anchor) ? order.indexOf(anchor) + 1 : add.after === 0 ? 0 : order.length; + if (order.length >= ANIMATION.maxFrames) break; + order.splice(position, 0, newFrame(anim, { prompt: add.prompt, same: false, holdMs: 0 })); + changes.push('第 ' + add.after + ' 帧后面加一帧'); + } + if (order.some((frame) => !frame.same)) anim.frames = order; + const regenerate = anim.frames.some((frame) => !frame.same && (frame.status === 'pending' || frame.status === 'failed')); + return { changes, regenerate }; +} + +function dedupeNumbers(list) { + return list.filter((value, index) => list.indexOf(value) === index); +} + +// 编辑节点 inventory 里这张图的一段说明。 +export function animationInventory(image) { + const spec = image.spec.animation; + const anim = image.animation; + const frames = anim && anim.frames.length ? anim.frames : spec.frames; + const describe = frames + .map((frame, index) => { + const source = anim && mirrorSource(anim, frame); + if (frame.same) return '第 ' + (index + 1) + ' 帧(参考图本身)'; + if (source) return '第 ' + (index + 1) + ' 帧(和第 ' + (anim.frames.indexOf(source) + 1) + ' 帧同一个姿态,用同一张图)'; + return '第 ' + (index + 1) + ' 帧「' + clip(frame.prompt, 40) + '」' + (frame.status && frame.status !== 'passed' ? '(' + FRAME_STATUS[frame.status] + ')' : ''); + }) + .join(';'); + const timing = '每帧 ' + spec.frameMs + ' 毫秒,' + (spec.loop ? '循环' : '只播一遍') + ',' + (transitionOf(spec) === 'cut' ? '帧间直接切换' : '帧间渐变') + ',长边 ' + spec.edge + ' 像素'; + if (!anim || anim.state === 'awaiting_confirm' || !anim.state) { + return ';这是 GIF 的参考图,还没出帧(计划 ' + frames.length + ' 帧,' + timing + '):' + describe; + } + return ';这是 GIF(' + frames.length + ' 帧,' + timing + '):' + describe; +} + +const FRAME_STATUS = { + pending: '还没画', + running: '正在画', + generated: '还没评审', + rejected: '没过评审', + unpassed: '没过评审', + unreviewed: '没评审', + failed: '没画出来', +}; + +function clip(value, limit) { + const chars = Array.from(String(value || '')); + return chars.length > limit ? chars.slice(0, limit).join('') + '…' : chars.join(''); +} + +// ------------------------------------------------------------------ 交付 + +// 实际写进 GIF 的关键帧和时长。循环播放时最后一帧之后直接接回第 1 帧:两帧是同一张图(规划常在末尾 +// 再放一帧参考图当「回到起手」),原样写进去接缝处会连停两帧。20261115-d7e30e 首尾各停 600 毫秒, +// 每一轮都在参考图上卡 1.2 秒。并成一帧,停两者里长的那个。 +// 时长按「份」分,整组时长等于规划的帧数 × frame_ms(写了 hold_ms 的帧照它停): +// - 播放的每一帧一份。补的过渡帧也是一份:补了帧的那一步变慢、变顺,别的帧不会只剩一半时间 +// (以前过渡帧和原来那一帧分掉一步的时间,有的帧 180 毫秒、有的 60 毫秒,播起来忽快忽慢,真机 20261117-cd2dc5); +// - 扔掉的帧(没过评审、没画出来)的那一份留给它前面那一帧:亮点照计划的速度走,不会一帧跳一格、一帧跳三格 +// (以前扔掉的帧不占时间,真机 20261119-e645c0 扔了一半的帧,后面的转动快了一倍多)。空当里补了过渡帧的, +// 过渡帧顶替扔掉的帧那一份,不再多占。 +function playOrder(usable, anim, spec) { + const shown = new Set(usable.map((item) => item.frame)); + // 按播放顺序把帧分成一段一段:每段从一帧要播的规划帧开始,带着它后面的过渡帧和扔掉的规划帧,到下一帧要播的规划帧为止。 + const gaps = []; + let current = null; + // 第一帧要播的规划帧之前的帧(一般没有:第 1 帧多是参考图本身)算进最后一段,循环时绕回来。 + const before = { extras: [], dropped: 0 }; + for (const frame of anim.frames) { + const kept = shown.has(frame); + const into = current || before; + if (!frame.inbetween && kept) { + current = { lead: frame, extras: [], dropped: 0 }; + gaps.push(current); + } else if (!frame.inbetween) { + into.dropped += 1; + } else if (kept) { + into.extras.push(frame); + } + } + if (gaps.length) { + gaps[gaps.length - 1].dropped += before.dropped; + gaps[gaps.length - 1].extras.push(...before.extras); + } + const shares = new Map(); + let units = 0; + for (const gap of gaps) { + const frames = [gap.lead, ...gap.extras]; + const count = Math.max(1 + gap.dropped, frames.length); + const each = count / frames.length; + for (const frame of frames) { + if (frame.holdMs) continue; + shares.set(frame, each); + units += each; + } + } + const planned = anim.frames.filter((frame) => !frame.inbetween && !frame.holdMs).length; + const unit = units ? (spec.frameMs * Math.max(1, planned)) / units : spec.frameMs; + const order = usable.map((item) => ({ + item, + delay: item.frame.holdMs || Math.max(ANIMATION.minFrameMs, Math.round(unit * (shares.get(item.frame) || 1))), + })); + if (spec.loop && order.length > 2 && sameImage(order[0].item, order[order.length - 1].item)) { + const last = order.pop(); + order[0].delay = Math.max(order[0].delay, last.delay); + } + return order; +} + +function medianDelay(order) { + const free = order.filter((entry) => !entry.item.frame.holdMs).map((entry) => entry.delay).sort((x, y) => x - y); + if (!free.length) return order[0].delay; + return free[free.length >> 1]; +} + +function sameImage(a, b) { + if (a.frame.same || b.frame.same) return a.frame.same && b.frame.same; + return Boolean(a.candidate && b.candidate && a.candidate.id === b.candidate.id); +} + +function describeComposition(gif, order, usable) { + const parts = []; + const locked = (gif.locked || []).filter((info) => info && info.locked).length; + if (locked) parts.push(locked + ' 帧锁回参考图的背景'); + const inbetween = usable.filter((item) => item.frame.inbetween).length; + if (inbetween) parts.push('含 ' + inbetween + ' 帧程序补的过渡帧'); + if (gif.tweens) parts.push('补了 ' + gif.tweens + ' 帧渐变'); + if (gif.cuts) parts.push(gif.cuts + ' 处两帧差得多、没补渐变(叠出来是虚影)'); + if (order.length < usable.length) parts.push('首尾同一张图并成一帧'); + parts.push(gif.frameCount + ' 帧 ' + Math.round(gif.buffer.length / 1024) + ' KB'); + return parts.join(','); +} + +// 合成 GIF 并写进这一版交付目录:.gif、静态图 .(参考图本身,之后改画面从它开始)、 +// 预览 -preview.png、帧条 -frames.png。合成完从写出去的 GIF 读回来核对帧数、尺寸、时长和循环。 +export async function deliverAnimation(session, image, deps, hooks, out) { + const anim = image.animation; + const spec = image.spec.animation; + const notes = []; + const ref = (session.refs || []).find((item) => item.id === anim.refId); + const used = new Set(playable(anim)); + const usable = anim.frames + .map((frame, index) => ({ frame, position: index + 1, candidate: frameCandidate(anim, frame) })) + .filter((item) => used.has(item.frame)); + if (anim.state !== 'done' || !ref || usable.length < 2) { + return { + item: { id: image.id, title: image.title, status: 'failed', blocked: false, reason: 'GIF 没做成:' + (anim.error || '能用的帧不到 2 帧'), contentPolicy: false, rounds: anim.reviews.length }, + notes, + }; + } + const size = gifSize(anim.width, anim.height, spec.edge); + const order = playOrder(usable, anim, spec); + const transition = transitionOf(spec); + const gif = await hooks.queuePixelWork(async () => { + const base = fitFrame(decodeImage(await readFile(ref.path)), size.width, size.height); + const decoded = new Map(); + const frames = []; + for (const entry of order) { + if (entry.item.frame.same) { + frames.push(base); + continue; + } + const path = entry.item.candidate.path; + if (!decoded.has(path)) decoded.set(path, fitFrame(decodeImage(await readFile(path)), size.width, size.height)); + frames.push(decoded.get(path)); + } + return makeGif(frames, { + width: size.width, + height: size.height, + frameMs: spec.frameMs, + delays: order.map((entry) => entry.delay), + loop: spec.loop, + base, + tween: transition === 'fade' ? ANIMATION.tween : 0, + tweenMaxChange: ANIMATION.tweenMaxChange, + }); + }); + deps.emit(session, 'deliver', image.id + ' 合成 GIF:' + describeComposition(gif, order, usable), { + detail: detail('GIF 合成 · ' + image.id, [ + kv('关键帧', order.map((entry) => '第 ' + entry.item.position + ' 帧 ' + entry.delay + ' 毫秒').join(',')), + kv('写出', gif.frameCount + ' 帧(含 ' + gif.tweens + ' 帧渐变),' + Math.round(gif.buffer.length / 1024) + ' KB'), + list( + '锁背景', + order + .map((entry, index) => ({ entry, info: gif.locked && gif.locked[index] })) + .filter((one) => one.info) + .map((one) => ({ + text: + '第 ' + one.entry.item.position + ' 帧:' + (one.info.locked ? '变化区域占 ' + Math.round(one.info.area * 100) + '%' : '整幅都在变(' + Math.round(one.info.area * 100) + '%),没锁') + + (one.info.dx || one.info.dy || one.info.scale ? ',对齐挪了 (' + one.info.dx + ', ' + one.info.dy + ') 像素、缩放 ' + one.info.scale + '%' : ''), + })), + ), + ]), + }); + const gifName = out.base + '.gif'; + await writeFile(join(out.dir, gifName), gif.buffer); + const back = decodeGif(await readFile(join(out.dir, gifName))); + const durationMs = back.frames.reduce((sum, frame) => sum + frame.delayMs, 0); + const verify = { + ok: back.width === gif.width && back.height === gif.height && back.frames.length === gif.frameCount && (spec.loop ? back.loop === 0 : back.loop === null), + frames: back.frames.length, + width: back.width, + height: back.height, + durationMs, + loop: back.loop === 0, + }; + const stillName = out.base + (extname(ref.file) || '.png'); + await copyFile(ref.path, join(out.dir, stillName)); + const previewName = out.base + '-preview.png'; + const refPreview = join(deps.dirs.previews, ref.id + '-gif.png'); + let previewFile = null; + if (await exists(refPreview)) { + await copyFile(refPreview, join(out.dir, previewName)); + previewFile = out.version + '/' + previewName; + } + const stripName = out.base + '-frames.png'; + const strip = await buildGridSheet( + usable.map((item) => ({ path: item.frame.same ? (previewFile ? refPreview : ref.path) : item.candidate.preview || item.candidate.path, label: String(item.position) })), + join(out.dir, stripName), + // 每行最多 6 格,行与行尽量一样长:8 帧排成 4+4,不排成 6+2。 + { perRow: Math.ceil(usable.length / Math.ceil(usable.length / 6)), width: 1200, fill: 246 }, + ); + const missing = anim.frames.filter((frame) => !frame.same && !frameCandidate(anim, frame)); + const unpassed = anim.frames.filter((frame) => !frame.same && !mirrorSource(anim, frame) && frameCandidate(anim, frame) && frame.status !== 'passed'); + const number = (frame) => anim.frames.indexOf(frame) + 1; + if (missing.length) notes.push(image.id + ' 的 GIF 少了第 ' + missing.map(number).join('、') + ' 帧(没画出来),可以说「第 ' + number(missing[0]) + ' 帧重画」再试'); + if (unpassed.length) { + const skipped = unpassed.filter((frame) => !used.has(frame)); + notes.push( + image.id + ' 的 GIF 里第 ' + unpassed.map(number).join('、') + ' 帧' + (unpassed.some((frame) => frame.status === 'unpassed') ? '没过评审' : '没评审') + + (unpassed[0].problem ? '(' + unpassed[0].problem + ')' : '') + + (skipped.length ? ',第 ' + skipped.map(number).join('、') + ' 帧没放进 GIF' : '') + + ',可以说「第 ' + number(unpassed[0]) + ' 帧重画」', + ); + } + if (!verify.ok) notes.push(image.id + ' 的 GIF 读回来核对没通过(' + verify.frames + ' 帧,' + verify.width + '×' + verify.height + ')'); + const clean = !missing.length && !unpassed.length && verify.ok; + const item = { + id: image.id, + title: image.title, + status: clean ? 'passed' : 'unpassed', + candidateId: null, + file: out.version + '/' + stillName, + previewFile, + size: anim.width + 'x' + anim.height, + requestId: '', + reason: clean ? '每一帧都过了动图评审,GIF 读回来核对通过' : '', + rounds: anim.reviews.length, + verdict: null, + program: null, + prompt: '', + revisedPrompt: '', + qr: null, + text: null, + baseFile: null, + interrupted: image.interrupted || '', + gif: { + file: out.version + '/' + gifName, + strip: strip.sheet ? out.version + '/' + stripName : null, + width: gif.width, + height: gif.height, + bytes: gif.buffer.length, + frames: usable.length, + frameMs: spec.frameMs, + loop: spec.loop, + transition, + written: gif.frameCount, + durationMs, + // 一帧实际停多久:补了过渡帧、去掉了没过评审的帧之后,和规划写的 frame_ms 不一样(见 playOrder)。 + // 扔掉了帧的那几帧停得更久(见 playOrder),报中位数。 + keyMs: medianDelay(order), + // 程序补的中间姿态有几帧;两帧差得多、没补渐变的有几处。 + inbetween: usable.filter((item) => item.frame.inbetween).length, + cuts: gif.cuts || 0, + dither: gif.dither, + reference: ref.id, + verify, + items: anim.frames.map((frame, index) => ({ + index: index + 1, + prompt: frame.same ? '' : frame.prompt, + same: frame.same, + status: frame.same ? 'reference' : frameCandidate(anim, frame) ? frame.status : 'failed', + problem: frame.problem || frame.note || '', + holdMs: frame.holdMs || 0, + inbetween: Boolean(frame.inbetween), + mode: frame.mode || '', + // 没过评审、没放进 GIF 的帧 + skipped: !frame.same && frame.status === 'unpassed' && !used.has(frame), + mirrorOf: mirrorSource(anim, frame) ? anim.frames.indexOf(mirrorSource(anim, frame)) + 1 : 0, + candidateId: (frameCandidate(anim, frame) || {}).id || null, + })), + }, + animation: { state: 'done' }, + }; + return { item, notes }; +} + +// 参考图交付时挂在卡片上的那一段:计划出几帧、每帧什么样。 +export function awaitingInfo(image) { + const spec = image.spec.animation; + const anim = ensureState(image); + return { + state: 'awaiting_confirm', + confirm: confirmText(image.id), + frameMs: spec.frameMs, + loop: spec.loop, + edge: spec.edge, + frames: anim.frames.map((frame, index) => ({ index: index + 1, same: frame.same, prompt: frame.same ? '' : frame.prompt })), + }; +} + +// 落盘:帧候选只存文件名,路径在恢复时按会话目录重建。 +export function snapshotAnimation(anim) { + if (!anim) return null; + return { + state: anim.state, + refId: anim.refId, + refFile: anim.refFile, + width: anim.width, + height: anim.height, + nextUid: anim.nextUid, + error: anim.error || '', + reviews: anim.reviews || [], + frames: anim.frames.map((frame) => ({ + uid: frame.uid, + prompt: frame.prompt, + same: frame.same, + holdMs: frame.holdMs, + status: frame.status, + problem: frame.problem, + retryPrompt: frame.retryPrompt, + note: frame.note, + own: Boolean(frame.own), + rounds: frame.rounds, + inbetween: Boolean(frame.inbetween), + between: frame.between || null, + mode: frame.mode || '', + candidates: frame.candidates.map((candidate) => ({ + id: candidate.id, + file: candidate.file, + previewFile: candidate.previewFile, + width: candidate.width, + height: candidate.height, + requestId: candidate.requestId, + prompt: candidate.prompt, + refDiff: candidate.refDiff, + problem: candidate.problem, + verdict: candidate.verdict, + createdAt: candidate.createdAt, + })), + })), + }; +} + +const SAFE = /^(?!\.{1,2}$)[A-Za-z0-9._-]+$/; + +export function restoreAnimation(raw, dirs) { + if (!raw || typeof raw !== 'object' || !Array.isArray(raw.frames)) return null; + // 出帧做到一半进程被停了:没画完的帧回到待画,下次追问「继续」或重画时接着画。 + const state = raw.state === 'frames' ? 'failed' : String(raw.state || ''); + return { + state, + refId: String(raw.refId || ''), + refFile: SAFE.test(String(raw.refFile || '')) ? raw.refFile : '', + width: Number(raw.width) || 0, + height: Number(raw.height) || 0, + nextUid: Number(raw.nextUid) || raw.frames.length + 1, + error: raw.state === 'frames' ? '出帧时应用被关掉了' : String(raw.error || ''), + reviews: Array.isArray(raw.reviews) ? raw.reviews : [], + frames: raw.frames.map((frame) => ({ + uid: Number(frame.uid) || 0, + prompt: String(frame.prompt || ''), + same: Boolean(frame.same), + holdMs: Number(frame.holdMs) || 0, + status: frame.status === 'running' ? 'pending' : String(frame.status || 'pending'), + problem: String(frame.problem || ''), + retryPrompt: String(frame.retryPrompt || ''), + note: String(frame.note || ''), + own: Boolean(frame.own), + rounds: Number(frame.rounds) || 0, + inbetween: Boolean(frame.inbetween), + between: Array.isArray(frame.between) ? frame.between.map(Number).filter((uid) => uid > 0).slice(0, 2) : null, + mode: ['ref', 'chain', 'between'].includes(frame.mode) ? frame.mode : '', + candidates: (Array.isArray(frame.candidates) ? frame.candidates : []) + .filter((candidate) => candidate && SAFE.test(String(candidate.file || ''))) + .map((candidate) => ({ + ...candidate, + path: join(dirs.images, candidate.file), + preview: SAFE.test(String(candidate.previewFile || '')) ? join(dirs.previews, candidate.previewFile) : '', + })), + })), + }; +} diff --git a/plugins/liddalidd/h3-image-executor/miniapp/node/lib/budget.mjs b/plugins/liddalidd/h3-image-executor/miniapp/node/lib/budget.mjs new file mode 100644 index 00000000..4993e642 --- /dev/null +++ b/plugins/liddalidd/h3-image-executor/miniapp/node/lib/budget.mjs @@ -0,0 +1,102 @@ +// 各模型节点(规划、评审、修正、选优、追问编辑、跨图评审、动图评审、参考图判定)调用工具的上限。 +// +// 以前是写死的(规划 10、评审 6、修正 4、追问编辑 4……),简单的图和难的图给一样多。 +// 24 个真机会话的实测:追问编辑 5 次里 4 次撞到上限 4 被打断,修正 3 次全撞到上限 4, +// 评审 64 次里 6 次撞到上限 6(都是 324 字的长卷、15 段字的课本页这类难的图); +// 规划反过来,24 次里 18 次一次工具都没用。 +// +// 现在每次调用前现算: +// 上限 = 必须打开的图(每个候选的看图材料、参考图材料……) +// + 1 次(几乎每个节点都会先跑一次 run_code 看看文件,实测 64 次评审里有 88 次 run_code) +// + 按难度给的余量(规划给每张图判 easy / normal / hard;没判就按规格推断,见 inferDifficulty) +// + 这个节点在这张图上撞过几次上限,就再多给几个 2 次。 +// 余量按评审的实测定:用掉的次数减去必须打开的图,中位 1、p75 2、p90 3、最多 5(被上限截断,实际要的更多)。 +// 加上那 1 次,easy 给 1 正好到 p75,normal 给 2 到 p90,hard 给 4 到实测最大。 +// 每个节点另有封顶,免得一个节点在难图上无限翻文件。超了照旧在运行时中断、强制收尾(agent.mjs)。 + +export const DIFFICULTIES = new Set(['easy', 'normal', 'hard']); +export const DIFFICULTY_LABELS = { easy: '简单', normal: '一般', hard: '难' }; +const SLACK = { easy: 1, normal: 2, hard: 4 }; +const ORDER = { easy: 0, normal: 1, hard: 2 }; +const OVERHEAD = 1; +const RAISE = 2; +export const TOOL_CAPS = { plan: 12, think: 3, judge: 12, refine: 10, select: 6, edit: 12, cross: 8, frames: 10, tweenplan: 6, tweens: 8 }; + +export function normalizeDifficulty(value) { + const level = String(value || '').trim().toLowerCase(); + return DIFFICULTIES.has(level) ? level : ''; +} + +function texts(list) { + return (list || []).map((item) => (typeof item === 'string' ? item : (item && item.text) || '')).join(''); +} + +// 规划没判难度时按规格推断。只看规格里写得出来的东西:字多不多、要不要对照参考图、动图帧数、图表数、硬性条目数。 +export function inferDifficulty(spec) { + const s = spec || {}; + const layers = (s.textOverlay && s.textOverlay.layers) || []; + const chars = texts(s.textLayers && s.textLayers.length ? s.textLayers : layers).replace(/\s/g, '').length; + const refs = (s.refs || []).length + (s.characterRefs || []).length; + const frames = s.animation && Array.isArray(s.animation.frames) ? s.animation.frames.length : 0; + const charts = (s.charts || []).length; + const hardItems = (s.checklist || []).filter((item) => item && item.kind === 'hard' && item.verify !== 'program').length; + const qr = (s.qrCodes || []).length; + const hard = []; + if (layers.length >= 8) hard.push(layers.length + ' 段文字'); + else if (chars >= 120) hard.push(chars + ' 个字'); + if (refs >= 2) hard.push('要对照 ' + refs + ' 张参考图'); + if (frames >= 8) hard.push('动图 ' + frames + ' 帧'); + if (charts >= 2) hard.push(charts + ' 张图表'); + if (hardItems >= 6) hard.push(hardItems + ' 条硬性要求'); + if (hard.length) return { level: 'hard', reason: hard.join('、') }; + if (!refs && !chars && !frames && !charts && !qr && hardItems <= 3) return { level: 'easy', reason: '没有参考图、文字、图表,要求不多' }; + return { level: 'normal', reason: '' }; +} + +// 这张图的难度:规划判的优先,没判就推断。 +export function difficultyOf(image) { + const spec = (image && image.spec) || {}; + const own = normalizeDifficulty(spec.difficulty); + if (own) return { level: own, reason: spec.difficultyReason || '', source: 'plan' }; + return { ...inferDifficulty(spec), source: 'inferred' }; +} + +// 一次照顾好几张图的节点(追问编辑、跨图评审)按其中最难的那张算。 +export function hardestOf(images) { + let best = { level: 'easy', reason: '', source: 'inferred' }; + for (const image of images || []) { + const one = difficultyOf(image); + if (ORDER[one.level] > ORDER[best.level]) best = one; + } + return best; +} + +// needs:[{ count, what }],这个节点必须打开的东西。fixed:只给必须的那份,不按难度加(参考图判定只看一张拼图)。 +// 返回 { limit, note }:limit 直接给 runJson 的 maxTools,提示词里写同一个数;note 写进时间线,说明这个数怎么来的。 +export function toolBudget(step, options) { + const o = options || {}; + const needs = (o.needs || []).filter((item) => item && item.count > 0); + const need = needs.reduce((sum, item) => sum + item.count, 0); + const level = DIFFICULTIES.has(o.difficulty) ? o.difficulty : 'normal'; + const slack = o.fixed ? 0 : SLACK[level]; + const hits = o.fixed ? 0 : Math.max(0, Number(o.hits) || 0); + const cap = TOOL_CAPS[step] || 10; + const raw = need + OVERHEAD + slack + hits * RAISE; + const limit = Math.max(1, Math.min(cap, raw)); + const parts = needs.map((item) => item.what + ' ' + item.count); + parts.push('余量 ' + OVERHEAD); + if (slack) parts.push('难度' + DIFFICULTY_LABELS[level] + ' ' + slack); + if (hits) parts.push('之前撞过 ' + hits + ' 次上限 ' + hits * RAISE); + return { limit, note: parts.join(' + ') + ' = ' + raw + (raw > cap ? ',封顶 ' + cap : '') + ' 次' }; +} + +// 节点跑完撞没撞上限,记在图(或会话)上:同一个节点下次多给。 +export function noteToolHit(target, step, result) { + if (!target || !result || !result.overBudget) return; + target.toolHits = { ...(target.toolHits || {}) }; + target.toolHits[step] = (target.toolHits[step] || 0) + 1; +} + +export function hitsOf(target, step) { + return (target && target.toolHits && target.toolHits[step]) || 0; +} diff --git a/plugins/liddalidd/h3-image-executor/miniapp/node/lib/chart.mjs b/plugins/liddalidd/h3-image-executor/miniapp/node/lib/chart.mjs new file mode 100644 index 00000000..cd56ec23 --- /dev/null +++ b/plugins/liddalidd/h3-image-executor/miniapp/node/lib/chart.mjs @@ -0,0 +1,1518 @@ +// 程序画图表。生图模型画柱状图、折线图、饼图时只会画「像图表的东西」:柱子高矮和数值对不上、刻度乱标、 +// 数字写错。图里要放数据图表时,让模型只画一张留出空白面板的底图,坐标轴、刻度、网格、柱子、折线、扇形、 +// 数字和图例都由程序按数据画上去,画完再从最终文件里量回来核对。 +// +// 每张图表的步骤: +// 1. 找空白:和程序排字同一套办法(textoverlay.findBlankRegion),以规划的位置为准,和底图上实际的空白求交。 +// 2. 定刻度:柱状图、条形图的数值轴一律从 0 起,不从 0 起柱子的高矮比例就是错的;刻度取 1、2、2.5、5 × 10ⁿ 的整齐间隔。 +// 3. 排版:标题、图例、单位、刻度、类目先量好尺寸,剩下的是绘图区。放不下先整体缩字号,类目还放不下就斜着放。 +// 4. 绘制:形状按面积精确抗锯齿(借用字形光栅化),文字用排字的字体,颜色跟着面板原来的明暗走。 +// 5. 核对:从编码后的最终文件解码,先量出刻度线的位置、拟合出「数值 → 像素」,再按它检查每根柱子的端点、 +// 每个数据点、每个扇形的边界是不是落在数值对应的位置。量出来对不上,这张图就不交付。 +import { resolveFonts, normalizeFontKind } from './fonts.mjs'; +import { drawGlyphs, placeMatrix, rasterizeGlyph } from './textlayout.mjs'; +import { analysisImage, boxToPixels, denseMask, findBlankRegion, maxRectangle, normalizedBox } from './textoverlay.mjs'; +import { encodePng } from './png.mjs'; +import { encodeJpeg } from './jpegenc.mjs'; +import { decodeImage } from './image.mjs'; + +const JPEG_QUALITY = 93; +// 图表里最小的字(刻度、类目)至少这么大,和程序排字的下限一致。 +const MIN_SIZE_RATIO = 0.008; +const MIN_SIZE_PX = 14; +export const MAX_CHARTS = 3; +const MAX_CATEGORIES = 24; +const MAX_SERIES = 6; +const DEFAULT_BOX = [0.08, 0.12, 0.92, 0.92]; +// 排不下时整体缩字号的几档。 +const SIZE_STEPS = [1, 0.88, 0.77, 0.68, 0.6]; + +export const CHART_TYPES = { bar: '柱状图', hbar: '条形图', line: '折线图', area: '面积图', pie: '饼图', donut: '环形图' }; + +const TYPE_PATTERNS = [ + ['hbar', /^(hbar|barh|horizontal|条形|横向|横柱)/i], + ['bar', /^(bar|column|col|柱)/i], + ['area', /^(area|面积)/i], + ['line', /^(line|折线|曲线|趋势|走势)/i], + ['donut', /^(donut|doughnut|ring|环)/i], + ['pie', /^(pie|饼|扇)/i], +]; + +export const CHART_REASONS = { + not_blank: '底图上预留给图表的地方没有留空', + marks: '底图上预留给图表的地方已经画了东西', + too_small: '预留的空白太小,图表的字会小到看不清', + no_font: '找不到可用的字体', + missing_glyphs: '字体里缺字', + bad_data: '图表的数据不完整', + verify: '从最终文件量回来和数据对不上', + error: '画图表出错', +}; + +const PALETTE = ['#4E79A7', '#F28E2B', '#59A14F', '#E15759', '#76B7B2', '#EDC948', '#B07AA1', '#FF9DA7', '#9C755F', '#BAB0AC']; + +const tick = () => new Promise((resolve) => setImmediate(resolve)); + +// ---------------------------------------------------------------- 规格 + +// 规划写的 charts → 规格。写错的图表丢掉,problems 里说明原因(给事件流看)。 +export function normalizeCharts(raw) { + const list = Array.isArray(raw) ? raw : raw && typeof raw === 'object' ? [raw] : []; + const charts = []; + const problems = []; + list.slice(0, MAX_CHARTS).forEach((item, index) => { + const { chart, problem } = normalizeChart(item); + if (chart) charts.push(chart); + else problems.push('图表 ' + (index + 1) + ':' + problem); + }); + return { charts, problems }; +} + +export function chartType(value) { + const text = String(value || '').trim(); + for (const [type, pattern] of TYPE_PATTERNS) if (pattern.test(text)) return type; + return ''; +} + +// 规划写的(下划线字段)和落盘、追问合并后的(驼峰字段)都认。 +export function normalizeChart(raw) { + if (!raw || typeof raw !== 'object') return { chart: null, problem: '没有写图表' }; + const type = chartType(raw.type); + if (!type) return { chart: null, problem: '图表类型「' + String(raw.type || '') + '」不认识' }; + const circular = type === 'pie' || type === 'donut'; + const categories = (Array.isArray(raw.categories) ? raw.categories : []).slice(0, MAX_CATEGORIES).map((item) => cleanText(item, 24)); + let rawSeries = Array.isArray(raw.series) ? raw.series : Array.isArray(raw.values) ? [{ name: raw.name, values: raw.values, color: raw.color }] : []; + if (circular) rawSeries = rawSeries.slice(0, 1); + const series = rawSeries.slice(0, MAX_SERIES).map((item) => { + const parsed = (Array.isArray(item && item.values) ? item.values : []).slice(0, MAX_CATEGORIES).map(parseValue); + return { + name: cleanText(item && item.name, 24), + values: parsed.map((one) => one.value), + decimals: parsed.reduce((max, one) => Math.max(max, one.decimals), 0), + suffix: parsed.map((one) => one.suffix).find(Boolean) || '', + color: hexColor(item && item.color), + }; + }); + if (!categories.length) return { chart: null, problem: '没有类目' }; + if (categories.some((item) => !item)) return { chart: null, problem: '有的类目是空的' }; + if (!series.length) return { chart: null, problem: '没有数据' }; + for (const one of series) { + const name = one.name || '数据'; + if (one.values.length !== categories.length) return { chart: null, problem: '「' + name + '」有 ' + one.values.length + ' 个数,类目有 ' + categories.length + ' 个' }; + if (one.values.some((value) => !Number.isFinite(value))) return { chart: null, problem: '「' + name + '」里有不是数字的值' }; + } + if (circular) { + const values = series[0].values; + if (values.some((value) => value < 0)) return { chart: null, problem: '饼图、环形图的数不能是负的' }; + if (!(values.reduce((sum, value) => sum + value, 0) > 0)) return { chart: null, problem: '饼图、环形图的数加起来是 0' }; + } + const lineLike = type === 'line' || type === 'area'; + const bound = (value) => (lineLike && value !== undefined && value !== null && value !== '' && Number.isFinite(Number(value)) ? Number(value) : null); + const askedDecimals = Number(raw.decimals); + const decimals = Number.isInteger(askedDecimals) && askedDecimals >= 0 && askedDecimals <= 4 ? askedDecimals : 0; + return { + chart: { + type, + title: cleanText(raw.title, 40), + unit: cleanText(raw.unit, 12) || series.map((one) => one.suffix).find(Boolean) || '', + categories, + series: series.map((one) => ({ name: one.name, values: one.values, color: one.color })), + // 只能比数据本身的小数位多(补 0),不能少:少了就是把 12.5 标成 13。 + decimals: Math.max(decimals, Math.min(4, Math.max(...series.map((one) => one.decimals)))), + stacked: raw.stacked === true && (type === 'bar' || type === 'hbar') && series.length > 1, + labels: ['value', 'percent', 'both', 'none'].includes(raw.labels) ? raw.labels : circular ? 'percent' : 'value', + yMin: bound(raw.y_min ?? raw.yMin), + yMax: bound(raw.y_max ?? raw.yMax), + box: boxValue(raw.box), + surface: cleanText(raw.surface, 40), + font: normalizeFontKind(raw.font || 'hei'), + colors: (Array.isArray(raw.colors) ? raw.colors : []).map(hexColor).filter(Boolean).slice(0, MAX_CATEGORIES), + }, + problem: '', + }; +} + +function cleanText(value, limit) { + const chars = Array.from(String(value === undefined || value === null ? '' : value).replace(/\s+/g, ' ').trim()); + return chars.slice(0, limit).join(''); +} + +function boxValue(raw) { + if (!Array.isArray(raw) || raw.length !== 4) return null; + const box = raw.map(Number); + return box.every(Number.isFinite) ? box : null; +} + +function hexColor(value) { + const match = /^#?([0-9a-f]{6}|[0-9a-f]{3})$/i.exec(String(value || '').trim()); + if (!match) return ''; + const hex = match[1].length === 3 ? match[1].split('').map((ch) => ch + ch).join('') : match[1]; + return '#' + hex.toUpperCase(); +} + +function halfWidth(text) { + return text + .replace(/[\uFF10-\uFF19]/g, (ch) => String.fromCharCode(ch.charCodeAt(0) - 0xfee0)) + .replace(/./g, '.') + .replace(/%/g, '%') + .replace(/[-−]/g, '-') + .replace(/+/g, '+'); +} + +function parseValue(raw) { + if (typeof raw === 'number') return { value: raw, decimals: decimalsOf(raw), suffix: '' }; + const text = halfWidth(String(raw === undefined || raw === null ? '' : raw)).replace(/\s+/g, ''); + const match = /^([-+]?)((?:\d{1,3}(?:,\d{3})+|\d+)(?:\.\d+)?|\.\d+)(.*)$/.exec(text); + if (!match) return { value: NaN, decimals: 0, suffix: '' }; + const digits = match[2].replace(/,/g, ''); + const rest = match[3]; + return { value: Number(match[1] + digits), decimals: Math.min(4, (digits.split('.')[1] || '').length), suffix: rest && rest.length <= 4 && !/\d/.test(rest) ? rest : '' }; +} + +function decimalsOf(value) { + if (!Number.isFinite(value)) return 0; + const text = String(value); + if (/e/i.test(text)) return Math.min(4, Math.max(0, 1 - Math.floor(Math.log10(Math.abs(value) || 1)))); + return Math.min(4, (text.split('.')[1] || '').length); +} + +// 用户原话里出现过的数。「120,150,180」既可能是千分位也可能是三个数,两种读法都收。 +export function numbersIn(text) { + const out = []; + for (const match of halfWidth(String(text || '')).matchAll(/\d[\d,]*(?:\.\d+)?/g)) { + const whole = Number(match[0].replace(/,/g, '')); + if (Number.isFinite(whole)) out.push(whole); + if (match[0].includes(',')) { + for (const piece of match[0].split(',')) { + const value = Number(piece); + if (piece && Number.isFinite(value)) out.push(value); + } + } + } + return out; +} + +// 换算单位(1.2 亿 = 12000 万、25% = 0.25)和正负号(「下降 5%」写成 -5)都算同一个数。 +function sameNumber(value, known) { + const v = Math.abs(value); + if (v === 0) return known === 0; + for (let k = -8; k <= 8; k += 1) { + const scaled = known * 10 ** k; + if (Math.abs(v - scaled) <= scaled * 1e-9) return true; + } + return false; +} + +// 规划写进图表的数是不是都能在用户的原话里找到。比例、合计由程序算,不在这里查。 +export function chartDataCheck(chart, sourceText) { + const known = numbersIn(sourceText); + const missing = []; + for (const one of chart.series) { + for (const value of one.values) if (!known.some((n) => sameNumber(value, n)) && !missing.includes(value)) missing.push(value); + } + return { ok: missing.length === 0, missing: missing.slice(0, 8) }; +} + +// 一句话描述一张图表(编辑节点的清单、交付说明里用)。 +export function describeChart(chart) { + const type = CHART_TYPES[chart.type] || chart.type; + const series = chart.series + .map((one) => (one.name ? one.name + ':' : '') + one.values.map((v) => formatNumber(v, chart.decimals, chart.unit === '%')).join('、')) + .join(';'); + return type + (chart.title ? '「' + chart.title + '」' : '') + ',类目 ' + chart.categories.join('、') + ';' + series + (chart.unit && chart.unit !== '%' ? '(单位:' + chart.unit + ')' : ''); +} + +// ---------------------------------------------------------------- 刻度与数字 + +// 整齐的刻度:间隔取 1、2、2.5、5 × 10ⁿ,首尾刻度把数据包住。 +export function niceScale(lo, hi, target) { + let min = Math.min(lo, hi); + let max = Math.max(lo, hi); + if (max === min) { + if (min > 0) min = 0; + else if (max < 0) max = 0; + else max = 1; + } + const rough = (max - min) / Math.max(2, target || 5); + const mag = 10 ** Math.floor(Math.log10(rough)); + const norm = rough / mag; + const unit = norm <= 1 ? 1 : norm <= 2 ? 2 : norm <= 2.5 ? 2.5 : norm <= 5 ? 5 : 10; + const step = unit * mag; + const decimals = Math.max(0, -Math.floor(Math.log10(step) + 1e-9) + (unit === 2.5 ? 1 : 0)); + const start = Math.floor(min / step + 1e-9); + const end = Math.ceil(max / step - 1e-9); + const ticks = []; + for (let i = start; i <= end; i += 1) ticks.push(Number((i * step).toFixed(decimals + 2))); + return { min: ticks[0], max: ticks[ticks.length - 1], step, ticks, decimals }; +} + +// 数值轴要包住的范围。柱状图、条形图一律包住 0;折线图默认也从 0 起,规划写了 y_min 才从别处起(刻度上照实标出来)。 +function valueRange(chart) { + if (chart.stacked) { + let lo = 0; + let hi = 0; + chart.categories.forEach((_, i) => { + let pos = 0; + let neg = 0; + for (const one of chart.series) { + if (one.values[i] >= 0) pos += one.values[i]; + else neg += one.values[i]; + } + hi = Math.max(hi, pos); + lo = Math.min(lo, neg); + }); + return [lo, hi]; + } + const all = chart.series.flatMap((one) => one.values); + const lo = Math.min(...all); + const hi = Math.max(...all); + if (chart.type === 'line' || chart.type === 'area') { + const from = chart.yMin !== null && chart.yMin <= lo ? chart.yMin : Math.min(0, lo); + const to = chart.yMax !== null && chart.yMax >= hi ? chart.yMax : hi; + return [from, to]; + } + return [Math.min(0, lo), Math.max(0, hi)]; +} + +// 千分位只给五位以上的数加(中文里 2024、1200 不习惯加逗号)。 +export function formatNumber(value, decimals, percent) { + const fixed = Math.abs(value).toFixed(decimals); + const [int, frac] = fixed.split('.'); + const grouped = int.length > 4 ? int.replace(/\B(?=(\d{3})+(?!\d))/g, ',') : int; + const negative = value < 0 && Number(fixed) !== 0; + return (negative ? '-' : '') + grouped + (frac ? '.' + frac : '') + (percent ? '%' : ''); +} + +// 百分比按最大余数法分配:各自四舍五入加起来常常是 99.9% 或 100.1%,读的人会以为算错了。 +export function largestRemainder(values, decimals) { + const total = values.reduce((sum, value) => sum + value, 0); + const unit = 10 ** decimals; + const raw = values.map((value) => (value / total) * 100 * unit); + const floor = raw.map((value) => Math.floor(value + 1e-9)); + let left = Math.round(100 * unit - floor.reduce((sum, value) => sum + value, 0)); + const order = raw.map((value, i) => ({ i, frac: value - floor[i] })).sort((a, b) => b.frac - a.frac || a.i - b.i); + for (const { i } of order) { + if (left <= 0) break; + floor[i] += 1; + left -= 1; + } + return floor.map((value) => value / unit); +} + +function percentDecimals(values) { + const total = values.reduce((sum, value) => sum + value, 0); + return values.every((value) => Math.abs(((value / total) * 100) % 1) < 1e-9) ? 0 : 1; +} + +// ---------------------------------------------------------------- 对外入口 + +// 在已解码的图上画图表,返回最终文件的字节。charts:normalizeChart 的结果。 +// 返回 { ok, buffer, format, image, results };有一张没画成或者核对没通过就整体不通过,buffer 为 null。 +export async function overlayCharts(image, format, charts, options) { + const opts = options || {}; + const startedAt = Date.now(); + const list = (Array.isArray(charts) ? charts : []).filter((chart) => chart && chart.type); + if (!list.length) return { ok: false, buffer: null, format, image, results: [], ms: 0 }; + const out = { ...image, data: Buffer.from(image.data) }; + const work = analysisImage(image); + const drawn = []; + for (const chart of list) { + let item; + try { + item = await renderChart(out, work, image, chart, opts); + } catch (error) { + item = { result: failure(chart, 'error', String((error && error.message) || error)), geometry: null }; + } + drawn.push(item); + await tick(); + } + const results = drawn.map((item) => item.result); + if (!results.every((result) => result.ok)) return { ok: false, buffer: null, format, image, results, ms: Date.now() - startedAt }; + const outFormat = format === 'png' ? 'png' : 'jpeg'; + const buffer = outFormat === 'png' ? encodePng(out, { level: 6 }) : encodeJpeg(out, { quality: JPEG_QUALITY }); + const final = decodeImage(buffer); + for (const item of drawn) { + const verify = verifyChart(final, item.geometry); + item.result.verify = verify; + if (!verify.ok) Object.assign(item.result, { ok: false, reason: 'verify', reasonText: CHART_REASONS.verify, detail: verify.note }); + } + const ok = results.every((result) => result.ok); + return { ok, buffer: ok ? buffer : null, format: outFormat, image: out, results, ms: Date.now() - startedAt }; +} + +function failure(chart, reason, detail) { + return { + type: chart.type, + title: chart.title || '', + ok: false, + reason, + reasonText: CHART_REASONS[reason] || reason, + detail: detail || '', + }; +} + +async function renderChart(out, work, original, chart, opts) { + const planned = boxToPixels(chart.box || DEFAULT_BOX, original.width, original.height); + let region = findBlankRegion(work, planned, original); + // 按排字的规矩,空白要和规划位置重合过半、颜色离样本不远才算。模型画的面板常常对不齐(真机:白板整体偏左上、 + // 或者比规划的窄),窗光照着的白板左右还能差 70 级、一边冷一边暖,这几张都会被打回重画。图表不像文字非得在某个位置, + // 换一种认法:沿着平缓的渐变找出整块面板,面板的中心落在规划位置里、面积够大,就画在这块面板上。 + let moved = false; + if (!region.ok && region.reason === 'not_blank') { + const panel = findSmoothPanel(work, planned, original); + if (panel.ok) { + region = panel; + moved = true; + } + } + const base = { + type: chart.type, + title: chart.title || '', + planned: normalizedBox(planned, original), + blank: region.blank ? normalizedBox(region.blank, original) : null, + coverage: round(region.coverage, 3), + marks: round(region.marks, 4), + moved, + }; + if (!region.ok) return { result: { ...failure(chart, region.reason, region.detail), ...base }, geometry: null }; + const fontInfo = await (opts.resolveFonts || resolveFonts)(chart.font || 'hei', chartChars(chart)); + if (!fontInfo.fonts.length) return { result: { ...failure(chart, 'no_font'), ...base }, geometry: null }; + if (fontInfo.missing.length) return { result: { ...failure(chart, 'missing_glyphs', fontInfo.missing.slice(0, 12).join('')), ...base }, geometry: null }; + const box = region.box; + const minSize = Math.max(MIN_SIZE_PX, Math.min(original.width, original.height) * MIN_SIZE_RATIO); + const surface = [region.seed.r, region.seed.g, region.seed.b]; + const ctx = makeContext(out, original, fontInfo.fonts, surface, minSize); + const side = Math.min(box.w, box.h); + const start = Math.min(Math.max(side * 0.045, minSize), side * 0.08); + const circular = chart.type === 'pie' || chart.type === 'donut'; + let layout = null; + let labelsDropped = false; + // 数值标注放不下时,先整体缩字号;缩到最小还放不下才去掉数值标注(刻度照样标,数还是能从坐标轴读出来)。 + for (const dropLabels of [false, true]) { + if (dropLabels && (chart.labels === 'none' || circular)) break; + const spec = dropLabels ? { ...chart, labels: 'none' } : chart; + for (const k of SIZE_STEPS) { + const s = start * k; + if (s < minSize) break; + layout = circular ? layoutPie(ctx, spec, box, s) : layoutCartesian(ctx, spec, box, s); + if (layout) break; + } + if (layout) { + labelsDropped = dropLabels; + break; + } + } + if (!layout) return { result: { ...failure(chart, 'too_small', '预留的区域 ' + Math.round(box.w) + '×' + Math.round(box.h) + ' 像素放不下这张图表'), ...base, box: normalizedBox(box, original) }, geometry: null }; + if (circular) paintPie(ctx, layout); + else paintCartesian(ctx, layout); + const geometry = { ...layout, surface, ink: ctx.ink }; + return { + result: { + ...base, + ok: true, + reason: '', + reasonText: '', + detail: '', + box: normalizedBox(box, original), + font: fontInfo.label, + size: Math.round(layout.s), + labelsDropped, + scale: layout.scale ? { min: layout.scale.min, max: layout.scale.max, step: layout.scale.step } : null, + labels: layout.valueTexts || [], + warning: layout.warning || '', + }, + geometry, + }; +} + +// ---------------------------------------------------------------- 找面板(光照渐变也认) + +// 从规划位置里最平的一块出发,沿着「相邻两点颜色差得很少、本身也不是边缘」往外长出整块面板,再在里面找最大的空白矩形。 +// 边框、笔画、物体的边缘梯度大,长不过去;光照渐变每一步只差零点几级,挡不住。 +// 生长和取矩形都限制在规划位置往外扩 15% 的范围里:面板再大,图表也不会跑到规划给别的东西(标题、主体)的地方去。 +// 返回和 findBlankRegion 一样的字段:{ ok, reason, detail, box, blank, seed, coverage, marks }。 +export function findSmoothPanel(work, planned, image) { + const { w: W, h: H, factor: f, r, g, b, grad } = work; + const fail = (detail) => ({ ok: false, reason: 'not_blank', detail, coverage: 0, marks: 0 }); + const p = { + x0: clampInt(planned.x / f, 0, W), + y0: clampInt(planned.y / f, 0, H), + x1: clampInt((planned.x + planned.w) / f, 0, W), + y1: clampInt((planned.y + planned.h) / f, 0, H), + }; + const pw = p.x1 - p.x0; + const ph = p.y1 - p.y0; + if (pw < 8 || ph < 8) return fail('规划的位置太小'); + let seed = null; + for (let gy = 0; gy < 3; gy += 1) { + for (let gx = 0; gx < 3; gx += 1) { + const bx0 = Math.floor(p.x0 + pw * (0.12 + (0.76 * gx) / 3)); + const bx1 = Math.floor(p.x0 + pw * (0.12 + (0.76 * (gx + 1)) / 3)); + const by0 = Math.floor(p.y0 + ph * (0.12 + (0.76 * gy) / 3)); + const by1 = Math.floor(p.y0 + ph * (0.12 + (0.76 * (gy + 1)) / 3)); + const grads = []; + let sr = 0; + let sg = 0; + let sb = 0; + for (let y = by0; y < by1; y += 1) { + for (let x = bx0; x < bx1; x += 1) { + const i = y * W + x; + grads.push(grad[i]); + sr += r[i]; + sg += g[i]; + sb += b[i]; + } + } + if (!grads.length) continue; + const mean = grads.reduce((sum, v) => sum + v, 0) / grads.length; + if (!seed || mean < seed.grad) { + grads.sort((x, y) => x - y); + seed = { grad: mean, grad90: grads[Math.floor(grads.length * 0.9)], block: [bx0, by0, bx1, by1], r: sr / grads.length, g: sg / grads.length, b: sb / grads.length }; + } + } + } + if (!seed) return fail('规划的位置超出画面'); + // 门槛比排字宽:白板上窗户的镜面反光、屏幕上的微弱倒影是梯度 4–12 的浅线,图表画在上面没关系; + // 模型偷偷画的线条、伪文字、边框是梯度 20 以上的深线,照样挡得住(真机 A-r1-g0)。 + const tolGrad = clamp(seed.grad90 * 3 + 6, 12, 30); + const tolStep = clamp(seed.grad * 2 + 5, 7, 14); + const win = { + x0: clampInt(p.x0 - pw * 0.15, 0, W), + y0: clampInt(p.y0 - ph * 0.15, 0, H), + x1: clampInt(p.x1 + pw * 0.15, 0, W), + y1: clampInt(p.y1 + ph * 0.15, 0, H), + }; + const filled = new Uint8Array(W * H); + const queue = new Int32Array(W * H); + let head = 0; + let tail = 0; + const [sx0, sy0, sx1, sy1] = seed.block; + for (let y = sy0; y < sy1; y += 1) { + for (let x = sx0; x < sx1; x += 1) { + const i = y * W + x; + if (grad[i] <= tolGrad && !filled[i]) { + filled[i] = 1; + queue[tail++] = i; + } + } + } + const step = (i, j) => Math.max(Math.abs(r[i] - r[j]), Math.abs(g[i] - g[j]), Math.abs(b[i] - b[j])); + while (head < tail) { + const i = queue[head++]; + const x = i % W; + const y = (i / W) | 0; + const next = [x > win.x0 ? i - 1 : -1, x < win.x1 - 1 ? i + 1 : -1, y > win.y0 ? i - W : -1, y < win.y1 - 1 ? i + W : -1]; + for (const j of next) { + if (j < 0 || filled[j] || grad[j] > tolGrad || step(i, j) > tolStep) continue; + filled[j] = 1; + queue[tail++] = j; + } + } + const side = Math.min(pw, ph); + const k = Math.max(3, Math.min(Math.round(Math.max(W, H) * 0.012), Math.round(side * 0.2))); + const rect = maxRectangle(denseMask(filled, W, H, k, 0.92), W, win); + if (!rect.area) return fail('规划位置附近没有一块平整的面板'); + const blank = { x: rect.x0 * f, y: rect.y0 * f, w: (rect.x1 - rect.x0) * f, h: (rect.y1 - rect.y0) * f }; + const s = Math.min(blank.w, blank.h); + const pad = Math.round(Math.min(Math.max(Math.min(image.width, image.height) * 0.02, s * 0.035), s * 0.08)); + const box = { x: blank.x + pad, y: blank.y + pad, w: blank.w - 2 * pad, h: blank.h - 2 * pad }; + const coverage = (box.w * box.h) / Math.max(1, planned.w * planned.h); + const cx = box.x + box.w / 2; + const cy = box.y + box.h / 2; + const inside = cx >= planned.x && cx <= planned.x + planned.w && cy >= planned.y && cy <= planned.y + planned.h; + // 面板比规划的窄一些也用(真机 A-r1-g1:白板只有规划位置的 41%),中心得在规划位置里,不能是旁边别的平整地方。 + if (!inside || coverage < 0.35 || Math.min(box.w, box.h) < Math.min(planned.w, planned.h) * 0.45) { + return { ...fail('规划位置附近的平整面板只有规划位置的 ' + Math.round(coverage * 100) + '%'), coverage, blank }; + } + // 面板上零星的笔迹、伪文字长不进面板,会在矩形里留下洞:图表压在上面会露出来。 + let holes = 0; + let total = 0; + for (let y = Math.ceil(box.y / f); y < Math.floor((box.y + box.h) / f); y += 1) { + for (let x = Math.ceil(box.x / f); x < Math.floor((box.x + box.w) / f); x += 1) { + total += 1; + if (!filled[y * W + x]) holes += 1; + } + } + const marks = holes / Math.max(1, total); + if (marks > 0.01) { + return { ok: false, reason: 'marks', detail: '面板上有 ' + (marks * 100).toFixed(1) + '% 的地方画了东西', coverage, marks, blank }; + } + return { ok: true, reason: '', detail: '', box, blank, seed: { r: seed.r, g: seed.g, b: seed.b }, coverage, marks }; +} + +// 要画的所有字:字体按这些字预先载入字形。数字、标点和「单位:」都带上,刻度怎么定都不会缺字。 +function chartChars(chart) { + const parts = ['0123456789.,-%():单位系列', chart.title, chart.unit, ...chart.categories, ...chart.series.map((one) => one.name)]; + return Array.from(new Set(Array.from(parts.join('')))).join(''); +} + +// ---------------------------------------------------------------- 绘制工具 + +function makeContext(out, original, fonts, surface, minSize) { + const ink = lumOf(surface) >= 140 ? [51, 51, 51] : [240, 240, 240]; + return { out, original, surface, minSize, ink, shape: makeShaper(fonts) }; +} + +// 一行字的字形和尺寸。top/bottom 是字墨相对基线的上下沿(像素,向上为正),用来把字的视觉中心对准刻度。 +function makeShaper(fonts) { + const hit = (ch) => { + for (const font of fonts) { + const gid = font.glyphId(ch.codePointAt(0)); + if (gid) return { font, gid }; + } + return null; + }; + const cache = new Map(); + return (text, size) => { + const key = size.toFixed(3) + '|' + text; + if (cache.has(key)) return cache.get(key); + const items = []; + let pen = 0; + let top = -Infinity; + let bottom = Infinity; + for (const ch of Array.from(String(text))) { + if (/\s/.test(ch)) { + pen += 0.3 * size; + continue; + } + const found = hit(ch); + if (!found) continue; + const s = size / found.font.unitsPerEm; + const outline = found.font.outline(found.gid); + if (outline) { + top = Math.max(top, outline.yMax * s); + bottom = Math.min(bottom, outline.yMin * s); + } + items.push({ font: found.font, gid: found.gid, dx: pen, s }); + pen += found.font.advance(found.gid) * s; + } + if (!Number.isFinite(top)) { + top = size * 0.7; + bottom = 0; + } + const shaped = { width: pen, top, bottom, items }; + cache.set(key, shaped); + return shaped; + }; +} + +// 字的视觉中心线穿过 (ax, ay)。theta 是屏幕上顺时针旋转的弧度。 +function placeText(shaped, ax, ay, align, theta) { + const ox = align === 'center' ? -shaped.width / 2 : align === 'end' ? -shaped.width : 0; + const c = (shaped.top + shaped.bottom) / 2; + const cos = Math.cos(theta); + const sin = Math.sin(theta); + return shaped.items.map((item) => { + const along = ox + item.dx; + return { font: item.font, gid: item.gid, m: placeMatrix(item.s, theta, [0, 0], [ax + along * cos - c * sin, ay + along * sin + c * cos]) }; + }); +} + +function textTile(ctx, text, size, ax, ay, align, theta) { + const shaped = ctx.shape(text, size); + if (!shaped.items.length) return null; + const angle = theta || 0; + const glyphs = placeText(shaped, ax, ay, align, angle); + const ox = align === 'center' ? -shaped.width / 2 : align === 'end' ? -shaped.width : 0; + const c = (shaped.top + shaped.bottom) / 2; + const cos = Math.cos(angle); + const sin = Math.sin(angle); + const corners = []; + for (const along of [ox, ox + shaped.width]) { + for (const up of [shaped.bottom - c, shaped.top - c]) corners.push([ax + along * cos + up * sin, ay + along * sin - up * cos]); + } + const margin = Math.ceil(size * 0.3) + 3; + const x0 = Math.floor(Math.min(...corners.map((p) => p[0]))) - margin; + const y0 = Math.floor(Math.min(...corners.map((p) => p[1]))) - margin; + const x1 = Math.ceil(Math.max(...corners.map((p) => p[0]))) + margin; + const y1 = Math.ceil(Math.max(...corners.map((p) => p[1]))) + margin; + const mask = { x: x0, y: y0, w: x1 - x0, h: y1 - y0, data: new Float32Array((x1 - x0) * (y1 - y0)) }; + drawGlyphs(mask, glyphs); + return mask; +} + +// 轴对齐的矩形,边缘按覆盖面积算抗锯齿。 +function rectTile(x0, y0, x1, y1) { + if (!(x1 > x0) || !(y1 > y0)) return null; + const ix0 = Math.floor(x0); + const iy0 = Math.floor(y0); + const w = Math.ceil(x1) - ix0; + const h = Math.ceil(y1) - iy0; + const data = new Float32Array(w * h); + const cover = (a, b, i) => Math.max(0, Math.min(b, i + 1) - Math.max(a, i)); + const cx = new Float32Array(w); + for (let i = 0; i < w; i += 1) cx[i] = cover(x0, x1, ix0 + i); + for (let j = 0; j < h; j += 1) { + const cy = cover(y0, y1, iy0 + j); + for (let i = 0; i < w; i += 1) data[j * w + i] = cx[i] * cy; + } + return { x: ix0, y: iy0, w, h, data }; +} + +// 任意简单多边形(像素坐标,y 向下),借字形光栅化做精确面积的抗锯齿。 +function polygonTile(points) { + if (!points || points.length < 3) return null; + let xMin = Infinity; + let yMin = Infinity; + let xMax = -Infinity; + let yMax = -Infinity; + for (const [x, y] of points) { + if (x < xMin) xMin = x; + if (x > xMax) xMax = x; + if (y < yMin) yMin = y; + if (y > yMax) yMax = y; + } + return rasterizeGlyph({ contours: [points.map(([x, y]) => ({ x, y, on: true }))], xMin, yMin, xMax, yMax }, [1, 0, 0, 1, 0, 0]); +} + +function circlePoints(cx, cy, r) { + const n = Math.max(24, Math.min(720, Math.ceil((2 * Math.PI * r) / 1.5))); + return Array.from({ length: n }, (_, i) => { + const a = (2 * Math.PI * i) / n; + return [cx + r * Math.cos(a), cy + r * Math.sin(a)]; + }); +} + +function wedgePoints(cx, cy, r0, r1, a0, a1) { + const steps = Math.max(2, Math.ceil((a1 - a0) / Math.min(Math.PI / 360, 1.5 / r1))); + const outer = []; + for (let i = 0; i <= steps; i += 1) { + const a = a0 + ((a1 - a0) * i) / steps; + outer.push([cx + r1 * Math.cos(a), cy + r1 * Math.sin(a)]); + } + if (r0 <= 0) return [[cx, cy], ...outer]; + const inner = []; + for (let i = steps; i >= 0; i -= 1) { + const a = a0 + ((a1 - a0) * i) / steps; + inner.push([cx + r0 * Math.cos(a), cy + r0 * Math.sin(a)]); + } + return outer.concat(inner); +} + +function segmentPoints(x0, y0, x1, y1, width) { + const len = Math.hypot(x1 - x0, y1 - y0) || 1; + const nx = (-(y1 - y0) / len) * (width / 2); + const ny = ((x1 - x0) / len) * (width / 2); + return [[x0 + nx, y0 + ny], [x1 + nx, y1 + ny], [x1 - nx, y1 - ny], [x0 - nx, y0 - ny]]; +} + +// 几块蒙版取最大值合成一块:折线的每一段、拐点和数据点各画各的再合并,重叠的地方不会叠深。 +function unionTiles(tiles) { + const list = tiles.filter(Boolean); + if (!list.length) return null; + let x0 = Infinity; + let y0 = Infinity; + let x1 = -Infinity; + let y1 = -Infinity; + for (const t of list) { + x0 = Math.min(x0, t.x); + y0 = Math.min(y0, t.y); + x1 = Math.max(x1, t.x + t.w); + y1 = Math.max(y1, t.y + t.h); + } + const w = x1 - x0; + const h = y1 - y0; + const data = new Float32Array(w * h); + for (const t of list) { + for (let ty = 0; ty < t.h; ty += 1) { + const row = (t.y + ty - y0) * w + (t.x - x0); + for (let tx = 0; tx < t.w; tx += 1) { + const v = t.data[ty * t.w + tx]; + if (v > data[row + tx]) data[row + tx] = v; + } + } + } + return { x: x0, y: y0, w, h, data }; +} + +function strokeTile(points, width) { + const pieces = []; + for (let i = 1; i < points.length; i += 1) pieces.push(polygonTile(segmentPoints(points[i - 1][0], points[i - 1][1], points[i][0], points[i][1], width))); + for (let i = 1; i < points.length - 1; i += 1) pieces.push(polygonTile(circlePoints(points[i][0], points[i][1], width / 2))); + return unionTiles(pieces); +} + +// 按「印在面板上」的方式叠:颜色乘上这一点面板的明暗(相对面板平均亮度,限制在 ±8%),纸纹和光影还在。 +function paint(ctx, tile, rgb, opacity) { + if (!tile) return; + const { out, surface } = ctx; + const { data, channels, width, height } = out; + const op = opacity === undefined ? 1 : opacity; + const gray = (rgb[0] * 0.299 + rgb[1] * 0.587 + rgb[2] * 0.114); + const surfGray = lumOf(surface); + for (let ty = 0; ty < tile.h; ty += 1) { + const y = tile.y + ty; + if (y < 0 || y >= height) continue; + for (let tx = 0; tx < tile.w; tx += 1) { + const x = tile.x + tx; + if (x < 0 || x >= width) continue; + const a = tile.data[ty * tile.w + tx] * op; + if (a <= 0.002) continue; + const i = (y * width + x) * channels; + if (channels < 3) { + const light = clamp(data[i] / Math.max(1, surfGray), 0.92, 1.08); + data[i] = Math.round(data[i] * (1 - a) + clamp(gray * light, 0, 255) * a); + continue; + } + for (let c = 0; c < 3; c += 1) { + const base = data[i + c]; + const light = clamp(base / Math.max(1, surface[c]), 0.92, 1.08); + data[i + c] = Math.round(base * (1 - a) + clamp(rgb[c] * light, 0, 255) * a); + } + } + } +} + +// 把蒙版盖住的地方恢复成底图原样(饼图扇形之间的分隔线:露出面板本身)。 +function restore(ctx, tile) { + if (!tile) return; + const { out, original } = ctx; + const { data, channels, width, height } = out; + for (let ty = 0; ty < tile.h; ty += 1) { + const y = tile.y + ty; + if (y < 0 || y >= height) continue; + for (let tx = 0; tx < tile.w; tx += 1) { + const x = tile.x + tx; + if (x < 0 || x >= width) continue; + const a = tile.data[ty * tile.w + tx]; + if (a <= 0.002) continue; + const i = (y * width + x) * channels; + for (let c = 0; c < Math.min(3, channels); c += 1) data[i + c] = Math.round(data[i + c] * (1 - a) + original.data[i + c] * a); + } + } +} + +function fitSize(ctx, texts, size, maxWidth) { + const widest = Math.max(...texts.map((text) => ctx.shape(text, size).width)); + return widest > maxWidth ? (size * maxWidth) / widest : size; +} + +function contrastInk(rgb) { + return lumOf(rgb) >= 150 ? [40, 40, 40] : [255, 255, 255]; +} + +// 系列颜色:规划给的颜色和面板太接近(看不清、也量不出来)就换成调色板里的颜色。 +function pickColors(wanted, count, surface) { + const used = []; + const result = []; + for (let i = 0; i < count; i += 1) { + let rgb = parseHex(wanted[i]); + if (!rgb || colorDistance(rgb, surface) < 90 || used.some((other) => colorDistance(other, rgb) < 40)) { + rgb = PALETTE.map(parseHex).find((candidate) => colorDistance(candidate, surface) >= 90 && !used.some((other) => colorDistance(other, candidate) < 40)) || parseHex(PALETTE[i % PALETTE.length]); + } + used.push(rgb); + result.push(rgb); + } + return result; +} + +// 图例:色块 + 名字,排不下就换行,每行居中。 +function legendRows(ctx, items, left, right, top, size) { + const q = size * 0.8; + const gap = size * 1.2; + const widths = items.map((item) => q + size * 0.4 + ctx.shape(item.text, size).width); + if (Math.max(...widths) > right - left) return null; + const rows = []; + let row = []; + let used = 0; + items.forEach((item, i) => { + const need = (row.length ? gap : 0) + widths[i]; + if (row.length && used + need > right - left) { + rows.push({ row, used }); + row = []; + used = 0; + } + used += row.length ? gap + widths[i] : widths[i]; + row.push({ item, width: widths[i] }); + }); + if (row.length) rows.push({ row, used }); + const rowH = size * 1.6; + const placed = []; + rows.forEach(({ row: entries, used: width }, r) => { + let x = (left + right) / 2 - width / 2; + const cy = top + rowH * (r + 0.5); + for (const { item, width: w } of entries) { + placed.push({ swatch: [x, cy - q / 2, x + q, cy + q / 2], rgb: item.rgb, text: item.text, size, x: x + q + size * 0.4, y: cy, align: 'start' }); + x += w + gap; + } + }); + return { items: placed, height: rows.length * rowH }; +} + +// ---------------------------------------------------------------- 直角坐标(柱状、条形、折线、面积) + +function layoutCartesian(ctx, chart, box, s) { + const { minSize, ink } = ctx; + const horizontal = chart.type === 'hbar'; + const lineLike = chart.type === 'line' || chart.type === 'area'; + const n = chart.categories.length; + const m = chart.series.length; + const percent = chart.unit === '%'; + const showValues = chart.labels !== 'none'; + const colors = pickColors(chart.series.map((one, j) => one.color || chart.colors[j] || ''), m, ctx.surface); + const perCategory = !lineLike && m === 1 && chart.colors.length >= n; + const barColors = perCategory ? pickColors(chart.colors, n, ctx.surface) : null; + const pad = s * 0.5; + const area = { left: box.x + pad, right: box.x + box.w - pad, top: box.y + pad, bottom: box.y + box.h - pad }; + const L = { kind: lineLike ? 'lines' : 'bars', horizontal, s, texts: [], legend: [], grid: [], axes: [], tickMarks: [], ticks: [], bars: [], lines: [], points: [], valueTexts: [] }; + if (chart.title) { + const size = fitSize(ctx, [chart.title], s * 1.3, area.right - area.left); + if (size < minSize) return null; + L.texts.push({ text: chart.title, size, x: (area.left + area.right) / 2, y: area.top + size * 0.55, align: 'center', rgb: ink }); + area.top += size * 1.5; + } + if (m > 1) { + const legend = legendRows(ctx, chart.series.map((one, j) => ({ text: one.name || '系列 ' + (j + 1), rgb: barColors ? barColors[j] : colors[j] })), area.left, area.right, area.top, s * 0.9); + if (!legend) return null; + L.legend = legend.items; + area.top += legend.height + s * 0.2; + } + if (chart.unit && !percent) { + const size = s * 0.85; + L.texts.push({ text: '单位:' + chart.unit, size, x: horizontal ? area.right : area.left, y: area.top + size * 0.55, align: horizontal ? 'end' : 'start', rgb: ink }); + area.top += size * 1.4; + } + const tickSize = s * 0.9; + const valueSize = s * 0.85; + const axisW = Math.max(2, s * 0.06); + const gridW = Math.max(1.5, s * 0.04); + const tickW = Math.max(1.8, gridW * 1.2); + const tickLen = Math.max(axisW + 4, s * 0.35); + const gapV = Math.max(4, valueSize * 0.3); + const range = valueRange(chart); + const negatives = range[0] < 0; + const fmt = (v) => formatNumber(v, chart.decimals, percent); + Object.assign(L, { axisW, gridW, tickW, tickLen }); + + if (!horizontal) { + const catSize0 = s * 0.9; + const roughH = area.bottom - area.top - catSize0 * 2 - (showValues ? valueSize * 1.6 : s * 0.5); + const scale = niceScale(range[0], range[1], clampInt(Math.round(roughH / (s * 2.4)), 3, 8)); + const tickTexts = scale.ticks.map((t) => formatNumber(t, scale.decimals, percent)); + const labelW = Math.max(...tickTexts.map((t) => ctx.shape(t, tickSize).width)); + const x0 = area.left + labelW + s * 0.25 + tickLen; + const x1 = area.right - (lineLike ? s * 0.4 : 0); + if (x1 - x0 < s * 4) return null; + const slot = (x1 - x0) / n; + let catSize = catSize0; + let rotate = false; + const widest = (size) => Math.max(...chart.categories.map((c) => ctx.shape(c, size).width)); + let catW = widest(catSize); + if (catW > slot * 0.92) { + const fit = (catSize * slot * 0.92) / catW; + if (fit >= Math.max(minSize, catSize * 0.7)) catSize = fit; + else { + rotate = true; + catSize = Math.max(minSize, catSize * 0.85); + // 斜放的类目文字之间的垂直距离是 slot × sin45°,比字高还小就会叠在一起。 + if (slot * Math.SQRT1_2 < catSize * 1.05) return null; + } + catW = widest(catSize); + // 第一个类目斜着往左下伸,伸出预留的区域就会画到底图别的东西上。 + if (rotate && x0 + slot * 0.5 + catSize * 0.3 - catW * Math.SQRT1_2 < box.x) return null; + } + const negRoom = negatives && showValues && !lineLike ? valueSize * 1.3 : 0; + const catH = rotate ? catW * Math.SQRT1_2 + catSize * 1.1 : catSize * 1.7; + const y1 = area.bottom - catH - negRoom; + const y0 = area.top + (showValues ? valueSize * 1.6 : s * 0.5); + if (y1 - y0 < s * 4) return null; + const Y = (v) => y1 - ((v - scale.min) / (scale.max - scale.min)) * (y1 - y0); + const zero = scale.min <= 0 && scale.max >= 0 ? 0 : scale.min; + Object.assign(L, { plot: { x0, y0, x1, y1 }, scale, extent: y1 - y0, zeroPos: Y(zero) }); + scale.ticks.forEach((t, i) => { + const pos = Y(t); + L.ticks.push({ value: t, pos, text: tickTexts[i] }); + if (Math.abs(t - zero) > scale.step * 1e-6) L.grid.push([x0 + axisW / 2, pos - gridW / 2, x1, pos + gridW / 2]); + L.tickMarks.push([x0 - tickLen, pos - tickW / 2, x0 - axisW / 2, pos + tickW / 2]); + L.texts.push({ text: tickTexts[i], size: tickSize, x: x0 - tickLen - s * 0.25, y: pos, align: 'end', rgb: ink }); + }); + L.axes.push([x0 - axisW / 2, y0, x0 + axisW / 2, y1 + axisW / 2]); + L.axes.push([x0, Y(zero) - axisW / 2, x1, Y(zero) + axisW / 2]); + chart.categories.forEach((text, i) => { + const cx = x0 + slot * (i + 0.5); + const top = y1 + negRoom; + if (rotate) L.texts.push({ text, size: catSize, x: cx + catSize * 0.3, y: top + catSize * 0.55, align: 'end', theta: -Math.PI / 4, rgb: ink }); + else L.texts.push({ text, size: catSize, x: cx, y: top + catSize * 0.85, align: 'center', rgb: ink }); + }); + if (lineLike) { + const lw = Math.max(2.5, s * 0.1); + const r = Math.max(lw * 1.25, s * 0.17); + const baseValue = zero; + chart.series.forEach((one, j) => { + const pts = one.values.map((v, i) => [x0 + slot * (i + 0.5), Y(v)]); + L.lines.push({ rgb: colors[j], points: pts, width: lw, r, area: chart.type === 'area' ? [[pts[0][0], Y(baseValue)], ...pts, [pts[pts.length - 1][0], Y(baseValue)]] : null }); + pts.forEach(([x, y], i) => L.points.push({ x, y, r, width: lw, rgb: colors[j], series: j, value: one.values[i], label: fmt(one.values[i]) })); + }); + if (showValues && !placePointLabels(ctx, L, chart, slot, valueSize, y0, y1)) return null; + } else { + const placed = placeVerticalBars(ctx, L, chart, { x0, slot, Y, colors, barColors, valueSize, gapV, fmt, showValues }); + if (!placed) return null; + } + L.valueTexts = chart.series.map((one) => one.values.map(fmt)); + return L; + } + + // 条形图:类目在左边从上往下排,数值轴在下面。 + const catSize0 = s * 0.9; + const scale = niceScale(range[0], range[1], clampInt(Math.round(((area.right - area.left) * 0.6) / (s * 3.4)), 3, 7)); + const tickTexts = scale.ticks.map((t) => formatNumber(t, scale.decimals, percent)); + const y1 = area.bottom - tickLen - tickSize * 1.7; + const y0 = area.top + s * 0.2; + if (y1 - y0 < s * 3) return null; + const slot = (y1 - y0) / n; + let catSize = Math.min(catSize0, slot / 1.15); + if (catSize < minSize) return null; + let catW = Math.max(...chart.categories.map((c) => ctx.shape(c, catSize).width)); + const maxCatW = (area.right - area.left) * 0.36; + if (catW > maxCatW) { + catSize = (catSize * maxCatW) / catW; + if (catSize < minSize) return null; + catW = maxCatW; + } + const valueTexts = chart.series.flatMap((one) => one.values.map(fmt)); + const totalTexts = chart.stacked ? chart.categories.map((_, i) => formatNumber(chart.series.reduce((sum, one) => sum + Math.max(0, one.values[i]), 0), chart.decimals, percent)) : []; + const endTexts = chart.stacked ? totalTexts : valueTexts; + const valW = showValues ? Math.max(...endTexts.map((t) => ctx.shape(t, valueSize).width)) + s * 0.35 : 0; + const lastTickW = ctx.shape(tickTexts[tickTexts.length - 1], tickSize).width; + const firstTickW = ctx.shape(tickTexts[0], tickSize).width; + const x0 = area.left + catW + s * 0.45 + Math.max(negatives && showValues ? valW : 0, firstTickW / 2 - catW - s * 0.45, 0); + const x1 = area.right - Math.max(valW, lastTickW / 2); + if (x1 - x0 < s * 4) return null; + const X = (v) => x0 + ((v - scale.min) / (scale.max - scale.min)) * (x1 - x0); + const zero = scale.min <= 0 && scale.max >= 0 ? 0 : scale.min; + Object.assign(L, { plot: { x0, y0, x1, y1 }, scale, extent: x1 - x0, zeroPos: X(zero) }); + scale.ticks.forEach((t, i) => { + const pos = X(t); + L.ticks.push({ value: t, pos, text: tickTexts[i] }); + if (Math.abs(t - zero) > scale.step * 1e-6) L.grid.push([pos - gridW / 2, y0, pos + gridW / 2, y1 - axisW / 2]); + L.tickMarks.push([pos - tickW / 2, y1 + axisW / 2, pos + tickW / 2, y1 + tickLen]); + L.texts.push({ text: tickTexts[i], size: tickSize, x: pos, y: y1 + tickLen + tickSize * 0.85, align: 'center', rgb: ink }); + }); + L.axes.push([x0 - axisW / 2, y1 - axisW / 2, x1, y1 + axisW / 2]); + L.axes.push([X(zero) - axisW / 2, y0, X(zero) + axisW / 2, y1]); + chart.categories.forEach((text, i) => { + L.texts.push({ text, size: catSize, x: area.left + catW, y: y0 + slot * (i + 0.5), align: 'end', rgb: ink }); + }); + const band = m === 1 || chart.stacked ? slot * 0.6 : slot * 0.78; + const bh = chart.stacked ? band : band / m; + const gap = m > 1 && !chart.stacked ? bh * 0.1 : 0; + if (showValues && !chart.stacked && valueSize * 1.05 > bh + gap) return null; + chart.categories.forEach((_, i) => { + const cy = y0 + slot * (i + 0.5); + let pos = 0; + let neg = 0; + chart.series.forEach((one, j) => { + const v = one.values[i]; + let from = 0; + let to = v; + if (chart.stacked) { + if (v >= 0) { + from = pos; + pos += v; + to = pos; + } else { + from = neg; + neg += v; + to = neg; + } + } + const top = chart.stacked ? cy - band / 2 : cy - band / 2 + j * bh + gap / 2; + const bottom = chart.stacked ? cy + band / 2 : top + bh - gap; + const a = X(from); + const b = X(to); + const rgb = barColors ? barColors[i] : colors[j]; + const bar = { x0: Math.min(a, b), x1: Math.max(a, b), y0: top, y1: bottom, start: a, end: b, from, to, rgb, cross: [top, bottom], label: fmt(v) }; + L.bars.push(bar); + if (!showValues) return; + if (chart.stacked) { + const w = ctx.shape(bar.label, valueSize).width; + if (Math.abs(b - a) >= w + s * 0.4 && bottom - top >= valueSize * 1.15) L.texts.push({ text: bar.label, size: valueSize, x: (a + b) / 2, y: cy, align: 'center', rgb: contrastInk(rgb) }); + } else { + L.texts.push({ text: bar.label, size: valueSize, x: v >= 0 ? b + gapV : b - gapV, y: (top + bottom) / 2, align: v >= 0 ? 'start' : 'end', rgb: ink }); + } + }); + if (chart.stacked && showValues && pos > 0) { + const total = formatNumber(pos, chart.decimals, percent); + L.texts.push({ text: total, size: valueSize, x: X(pos) + gapV, y: cy, align: 'start', rgb: ink }); + } + }); + L.valueTexts = chart.series.map((one) => one.values.map(fmt)); + return L; +} + +function placeVerticalBars(ctx, L, chart, o) { + const { x0, slot, Y, colors, barColors, valueSize, gapV, fmt, showValues } = o; + const m = chart.series.length; + const group = m === 1 || chart.stacked ? slot * 0.56 : slot * 0.76; + const bw = chart.stacked ? group : group / m; + const gap = m > 1 && !chart.stacked ? bw * 0.1 : 0; + for (let i = 0; i < chart.categories.length; i += 1) { + const cx = x0 + slot * (i + 0.5); + let pos = 0; + let neg = 0; + for (let j = 0; j < m; j += 1) { + const v = chart.series[j].values[i]; + let from = 0; + let to = v; + if (chart.stacked) { + if (v >= 0) { + from = pos; + pos += v; + to = pos; + } else { + from = neg; + neg += v; + to = neg; + } + } + const left = chart.stacked ? cx - group / 2 : cx - group / 2 + j * bw + gap / 2; + const right = chart.stacked ? cx + group / 2 : left + bw - gap; + const a = Y(from); + const b = Y(to); + const rgb = barColors ? barColors[i] : colors[j]; + const bar = { x0: left, x1: right, y0: Math.min(a, b), y1: Math.max(a, b), start: a, end: b, from, to, rgb, cross: [left, right], label: fmt(v) }; + L.bars.push(bar); + if (!showValues) continue; + const width = ctx.shape(bar.label, valueSize).width; + if (chart.stacked) { + if (Math.abs(b - a) >= valueSize * 1.25 && width <= (right - left) * 0.94) L.texts.push({ text: bar.label, size: valueSize, x: cx, y: (a + b) / 2, align: 'center', rgb: contrastInk(rgb) }); + continue; + } + // 数值标注不能比柱子所在的那一格宽,否则会和旁边的标注叠在一起。 + if (width > (m === 1 ? slot * 0.98 : bw * 1.02)) return false; + const up = v >= 0; + L.texts.push({ text: bar.label, size: valueSize, x: (left + right) / 2, y: up ? b - gapV - valueSize * 0.38 : b + gapV + valueSize * 0.38, align: 'center', rgb: ctx.ink }); + } + if (chart.stacked && showValues && pos > 0) { + const total = formatNumber(pos, chart.decimals, chart.unit === '%'); + if (ctx.shape(total, valueSize).width > slot * 0.98) return false; + L.texts.push({ text: total, size: valueSize, x: cx, y: Y(pos) - gapV - valueSize * 0.38, align: 'center', rgb: ctx.ink }); + } + } + return true; +} + +// 折线的数值标注:每个类目里位置最高的点标在上方,其余的往下找不和别人重叠的位置。 +function placePointLabels(ctx, L, chart, slot, valueSize, y0, y1) { + const h = valueSize * 1.05; + const byCategory = new Map(); + for (const point of L.points) { + const list = byCategory.get(point.x) || []; + list.push(point); + byCategory.set(point.x, list); + } + for (const [x, points] of byCategory) { + const taken = []; + points.sort((a, b) => a.y - b.y); + for (const p of points) { + const width = ctx.shape(p.label, valueSize).width; + if (width > slot * 0.98) return false; + const options = [p.y - p.r - valueSize * 0.62, p.y + p.r + valueSize * 0.62]; + for (let k = 1; k <= 3; k += 1) options.push(p.y + p.r + valueSize * 0.62 + k * h); + const y = options.find((cy) => cy - h / 2 >= y0 - valueSize * 1.5 && cy + h / 2 <= y1 + valueSize * 0.2 && !taken.some((other) => Math.abs(other - cy) < h)); + if (y === undefined) return false; + taken.push(y); + L.texts.push({ text: p.label, size: valueSize, x, y, align: 'center', rgb: ctx.ink }); + } + } + return true; +} + +function paintCartesian(ctx, L) { + for (const rect of L.grid) paint(ctx, rectTile(...rect), ctx.ink, 0.16); + for (const line of L.lines) if (line.area) paint(ctx, polygonTile(line.area), line.rgb, 0.18); + for (const bar of L.bars) paint(ctx, rectTile(bar.x0, bar.y0, bar.x1, bar.y1), bar.rgb, 1); + for (const rect of L.axes) paint(ctx, rectTile(...rect), ctx.ink, 0.85); + for (const rect of L.tickMarks) paint(ctx, rectTile(...rect), ctx.ink, 0.85); + for (const line of L.lines) { + const marks = line.points.map(([x, y]) => polygonTile(circlePoints(x, y, line.r))); + line.tile = unionTiles([strokeTile(line.points, line.width), ...marks]); + paint(ctx, line.tile, line.rgb, 1); + } + for (const item of L.legend) paint(ctx, rectTile(...item.swatch), item.rgb, 1); + for (const item of L.legend) paint(ctx, textTile(ctx, item.text, item.size, item.x, item.y, item.align, 0), ctx.ink, 1); + for (const t of L.texts) paint(ctx, textTile(ctx, t.text, t.size, t.x, t.y, t.align, t.theta || 0), t.rgb, 1); +} + +// ---------------------------------------------------------------- 饼图、环形图 + +function layoutPie(ctx, chart, box, s) { + const { minSize, ink } = ctx; + const donut = chart.type === 'donut'; + const values = chart.series[0].values; + const total = values.reduce((sum, value) => sum + value, 0); + const pd = percentDecimals(values); + const percents = largestRemainder(values, pd); + const percentUnit = chart.unit === '%'; + const colors = pickColors(chart.categories.map((_, i) => chart.colors[i] || ''), values.length, ctx.surface); + const pad = s * 0.5; + const area = { left: box.x + pad, right: box.x + box.w - pad, top: box.y + pad, bottom: box.y + box.h - pad }; + const P = { kind: 'pie', donut, s, texts: [], legend: [], wedges: [], values, total, valueTexts: [] }; + if (chart.title) { + const size = fitSize(ctx, [chart.title], s * 1.3, area.right - area.left); + if (size < minSize) return null; + P.texts.push({ text: chart.title, size, x: (area.left + area.right) / 2, y: area.top + size * 0.55, align: 'center', rgb: ink }); + area.top += size * 1.5; + } + const valueText = (i) => formatNumber(values[i], chart.decimals, percentUnit) + (chart.unit && !percentUnit ? chart.unit : ''); + const pctText = (i) => formatNumber(percents[i], pd, true); + const entries = chart.categories.map((name, i) => { + if (chart.labels === 'none') return name; + if (percentUnit || chart.labels === 'value') return name + ' ' + valueText(i); + if (chart.labels === 'percent') return name + ' ' + pctText(i); + return name + ' ' + valueText(i) + '(' + pctText(i) + ')'; + }); + P.valueTexts = chart.categories.map((_, i) => (percentUnit ? valueText(i) : valueText(i) + ' / ' + pctText(i))); + if (percentUnit && Math.abs(total - 100) > 0.5) P.warning = '各项加起来是 ' + formatNumber(total, chart.decimals, true) + ',不是 100%,扇形按各项的相对大小画'; + let legendSize = s * 0.85; + const q = () => legendSize * 0.8; + const rowH = () => legendSize * 1.65; + const widest = () => Math.max(...entries.map((text) => ctx.shape(text, legendSize).width)) + q() + legendSize * 0.4; + let pieArea; + const side = box.w >= box.h * 1.15; + if (side) { + const maxW = (area.right - area.left) * 0.48; + if (widest() > maxW) legendSize = (legendSize * maxW) / widest(); + if (rowH() * entries.length > area.bottom - area.top) legendSize = (area.bottom - area.top) / entries.length / 1.65; + if (legendSize < minSize) return null; + const lw = widest(); + const lx = area.right - lw; + const ly = (area.top + area.bottom) / 2 - (rowH() * entries.length) / 2; + entries.forEach((text, i) => { + const cy = ly + rowH() * (i + 0.5); + P.legend.push({ swatch: [lx, cy - q() / 2, lx + q(), cy + q() / 2], rgb: colors[i], text, size: legendSize, x: lx + q() + legendSize * 0.4, y: cy, align: 'start' }); + }); + pieArea = { left: area.left, right: lx - s * 0.8, top: area.top, bottom: area.bottom }; + } else { + const legend = legendRows(ctx, entries.map((text, i) => ({ text, rgb: colors[i] })), area.left, area.right, area.top, legendSize); + if (!legend) return null; + const shift = area.bottom - legend.height; + for (const item of legend.items) { + item.swatch = [item.swatch[0], item.swatch[1] - area.top + shift, item.swatch[2], item.swatch[3] - area.top + shift]; + item.y += shift - area.top; + } + P.legend = legend.items; + pieArea = { left: area.left, right: area.right, top: area.top, bottom: shift - s * 0.4 }; + } + const R = (Math.min(pieArea.right - pieArea.left, pieArea.bottom - pieArea.top) / 2) * 0.96; + if (R < s * 2.5) return null; + const cx = (pieArea.left + pieArea.right) / 2; + const cy = (pieArea.top + pieArea.bottom) / 2; + const r0 = donut ? R * 0.56 : 0; + const sepW = Math.max(2, s * 0.06); + Object.assign(P, { center: [cx, cy], r0, r1: R, sepW, extent: R }); + let acc = 0; + const labelSize = s * 0.85; + values.forEach((value, i) => { + const a0 = -Math.PI / 2 + (2 * Math.PI * acc) / total; + acc += value; + const a1 = -Math.PI / 2 + (2 * Math.PI * acc) / total; + const wedge = { a0, a1, rgb: colors[i], value, label: null }; + P.wedges.push(wedge); + if (chart.labels === 'none' || value <= 0) return; + const text = percentUnit || chart.labels === 'value' ? valueText(i) : pctText(i); + const shaped = ctx.shape(text, labelSize); + const rho = donut ? (r0 + R) / 2 : R * 0.64; + const half = Math.asin(Math.min(1, Math.hypot(shaped.width, labelSize) / 2 / rho)); + const margin = (sepW + 4) / rho + Math.PI / 60; + // 扇形太窄放不下就不写在扇形里(图例里照样有),也给边界留出量回来核对的余地。 + if ((a1 - a0) / 2 < half + margin) return; + if (donut && R - r0 < labelSize * 1.4) return; + const mid = (a0 + a1) / 2; + wedge.label = { half: half + margin }; + P.texts.push({ text, size: labelSize, x: cx + rho * Math.cos(mid), y: cy + rho * Math.sin(mid), align: 'center', rgb: contrastInk(colors[i]) }); + }); + return P; +} + +function paintPie(ctx, P) { + const [cx, cy] = P.center; + for (const w of P.wedges) if (w.a1 - w.a0 > 1e-6) paint(ctx, polygonTile(wedgePoints(cx, cy, P.r0, P.r1, w.a0, w.a1)), w.rgb, 1); + if (P.wedges.filter((w) => w.value > 0).length > 1) { + for (const w of P.wedges) { + const inner = Math.max(0, P.r0 - 1); + const outer = P.r1 + 1.5; + restore(ctx, polygonTile(segmentPoints(cx + inner * Math.cos(w.a0), cy + inner * Math.sin(w.a0), cx + outer * Math.cos(w.a0), cy + outer * Math.sin(w.a0), P.sepW))); + } + } + for (const item of P.legend) paint(ctx, rectTile(...item.swatch), item.rgb, 1); + for (const item of P.legend) paint(ctx, textTile(ctx, item.text, item.size, item.x, item.y, item.align, 0), ctx.ink, 1); + for (const t of P.texts) paint(ctx, textTile(ctx, t.text, t.size, t.x, t.y, t.align, 0), t.rgb, 1); +} + +// ---------------------------------------------------------------- 读回核对 + +function verifyChart(final, geometry) { + if (!geometry) return { ok: false, note: '没有可核对的图形', checked: 0, maxErrorPx: null, tolerancePx: null, items: [] }; + const read = sampler(final); + return geometry.kind === 'pie' ? verifyPie(read, geometry) : verifyCartesian(read, geometry); +} + +function sampler(image) { + const { width, height, channels, data } = image; + const px = (x, y) => { + const i = (clampInt(y, 0, height - 1) * width + clampInt(x, 0, width - 1)) * channels; + return channels >= 3 ? [data[i], data[i + 1], data[i + 2]] : [data[i], data[i], data[i]]; + }; + // 连续坐标:像素 i 占 [i, i+1),中心在 i + 0.5。 + const at = (x, y) => { + const fx = x - 0.5; + const fy = y - 0.5; + const x0 = Math.floor(fx); + const y0 = Math.floor(fy); + const tx = fx - x0; + const ty = fy - y0; + const a = px(x0, y0); + const b = px(x0 + 1, y0); + const c = px(x0, y0 + 1); + const d = px(x0 + 1, y0 + 1); + return [0, 1, 2].map((k) => (a[k] * (1 - tx) + b[k] * tx) * (1 - ty) + (c[k] * (1 - tx) + d[k] * tx) * ty); + }; + return { at, width, height }; +} + +// c 在「参照色 → 目标色」这条线上走到了几成(0 是参照色,1 是目标色)。 +function membership(c, ref, target) { + const d = [target[0] - ref[0], target[1] - ref[1], target[2] - ref[2]]; + const denom = d[0] * d[0] + d[1] * d[1] + d[2] * d[2]; + if (denom < 1) return 0; + return clamp(((c[0] - ref[0]) * d[0] + (c[1] - ref[1]) * d[1] + (c[2] - ref[2]) * d[2]) / denom, 0, 1); +} + +function mean(colors) { + const sum = [0, 0, 0]; + for (const c of colors) for (let k = 0; k < 3; k += 1) sum[k] += c[k]; + return sum.map((v) => v / Math.max(1, colors.length)); +} + +function verifyCartesian(read, G) { + const tol = Math.max(1.5, G.extent * 0.002); + const items = []; + const horizontal = G.horizontal; + // 1. 刻度:沿着刻度短线的中线取样,量出每根刻度线的中心,拟合「数值 → 像素」。 + const fixed = horizontal ? G.plot.y1 + (G.axisW / 2 + G.tickLen) / 2 : G.plot.x0 - (G.axisW / 2 + G.tickLen) / 2; + const sample = (t) => (horizontal ? read.at(t, fixed) : read.at(fixed, t)); + const spacing = G.ticks.length > 1 ? Math.abs(G.ticks[1].pos - G.ticks[0].pos) : G.extent; + const win = Math.max(3, Math.min(spacing * 0.4, G.s * 0.6)); + const measured = []; + for (const tk of G.ticks) { + const ref = mean([sample(tk.pos - win), sample(tk.pos + win)]); + let sw = 0; + let sp = 0; + for (let t = Math.floor(tk.pos - win + 1); t <= Math.ceil(tk.pos + win - 1); t += 1) { + const w = Math.max(0, (membership(sample(t + 0.5), ref, G.ink) - 0.15) / 0.85); + sw += w; + sp += w * (t + 0.5); + } + if (sw < 0.5) { + items.push({ kind: 'tick', label: tk.text, ok: false, errorPx: null, note: '刻度线没量到' }); + continue; + } + measured.push({ value: tk.value, pos: sp / sw, text: tk.text }); + } + if (measured.length < 2) return summarize(items, tol, '刻度线量到的不到 2 根'); + const fit = linearFit(measured.map((one) => one.value), measured.map((one) => one.pos)); + let axisResidual = 0; + for (const one of measured) { + const err = Math.abs(fit.a * one.value + fit.b - one.pos); + axisResidual = Math.max(axisResidual, err); + items.push({ kind: 'tick', label: one.text, ok: err <= tol, errorPx: round(err, 2) }); + } + const predict = (value) => fit.a * value + fit.b; + // 2. 柱子:从柱子里面往外走,颜色从柱色变到外面的颜色走到一半的位置就是柱端(亚像素)。 + for (const bar of G.bars) { + const len = Math.abs(bar.end - bar.start); + if (len < 6) continue; + const dir = Math.sign(bar.end - bar.start); + const [c0, c1] = bar.cross; + const span = c1 - c0; + const lanes = [0.3, 0.4, 0.5, 0.6, 0.7].map((k) => c0 + span * k); + const colorAt = (t) => mean(lanes.map((c) => (horizontal ? read.at(t, c) : read.at(c, t)))); + const inside = mean([3, 4, 5].map((d) => colorAt(bar.end - dir * Math.min(d, len / 2)))); + const outside = mean([2.5, 3].map((d) => colorAt(bar.end + dir * d))); + let edge = null; + let prev = null; + const startT = bar.end - dir * Math.min(6, len / 2); + for (let k = 0; k <= 24; k += 0.5) { + const t = startT + dir * k; + const b = membership(colorAt(t), outside, inside); + if (prev && prev.b >= 0.5 && b < 0.5) { + edge = prev.t + (t - prev.t) * ((prev.b - 0.5) / (prev.b - b)); + break; + } + prev = { t, b }; + } + if (edge === null) { + items.push({ kind: 'bar', label: bar.label, ok: false, errorPx: null, note: '柱端没量到' }); + continue; + } + const err = Math.abs(edge - predict(bar.to)); + items.push({ kind: 'bar', label: bar.label, ok: err <= tol, errorPx: round(err, 2) }); + } + // 3. 数据点:数据点附近颜色的重心和程序画的形状的重心比,差多少就是画歪了多少,再和刻度换算的位置比。 + for (const point of G.points) { + const line = G.lines[point.series]; + if (!line || !line.tile) continue; + const r = Math.ceil(point.r + 1); + const target = mean(circlePoints(point.x, point.y, point.r * 0.35).map(([x, y]) => read.at(x, y)).concat([read.at(point.x, point.y)])); + const ring = circlePoints(point.x, point.y, point.r + point.width * 2 + 4).filter((_, i) => i % 3 === 0).map(([x, y]) => read.at(x, y)); + const ref = medianColor(ring); + let mw = 0; + let my = 0; + let ew = 0; + let ey = 0; + for (let y = Math.floor(point.y - r); y <= Math.ceil(point.y + r); y += 1) { + for (let x = Math.floor(point.x - r); x <= Math.ceil(point.x + r); x += 1) { + const w = Math.max(0, (membership(read.at(x + 0.5, y + 0.5), ref, target) - 0.3) / 0.7); + mw += w; + my += w * (y + 0.5); + const tx = x - line.tile.x; + const ty = y - line.tile.y; + const cov = tx >= 0 && ty >= 0 && tx < line.tile.w && ty < line.tile.h ? line.tile.data[ty * line.tile.w + tx] : 0; + const e = Math.max(0, (cov - 0.3) / 0.7); + ew += e; + ey += e * (y + 0.5); + } + } + if (mw < 1 || ew < 1) { + items.push({ kind: 'point', label: point.label, ok: false, errorPx: null, note: '数据点没量到' }); + continue; + } + const measuredY = point.y + (my / mw - ey / ew); + const err = Math.abs(measuredY - predict(point.value)); + items.push({ kind: 'point', label: point.label, ok: err <= tol, errorPx: round(err, 2) }); + } + return summarize(items, tol, '', { axisResidualPx: round(axisResidual, 2), ticks: measured.length }); +} + +function verifyPie(read, P) { + const [cx, cy] = P.center; + const rho = P.donut ? P.r0 + (P.r1 - P.r0) * 0.85 : P.r1 * 0.9; + const tol = Math.max(1.5, rho * 0.004); + const items = []; + const total = P.values.reduce((sum, value) => sum + value, 0); + const at = (angle) => read.at(cx + rho * Math.cos(angle), cy + rho * Math.sin(angle)); + const visible = P.wedges.map((w, i) => ({ ...w, i })).filter((w) => w.value > 0); + // 扇形的颜色:在每个扇形两端里面一点取样(离分隔线和扇形里的数字都远)。 + const inset = (P.sepW + 4) / rho; + for (const w of visible) { + if (w.a1 - w.a0 < inset * 2 + 0.01) continue; + for (const angle of [w.a0 + inset, w.a1 - inset]) { + const d = colorDistance(at(angle), w.rgb); + items.push({ kind: 'wedge', label: '第 ' + (w.i + 1) + ' 块', ok: d < 70, errorPx: null, note: d < 70 ? '' : '颜色不对' }); + } + } + // 扇形的边界:分隔线露出的是面板本身,分隔线的中心就是边界。期望的边界直接按数值算,不用画的时候的角度。 + if (visible.length > 1) { + let acc = 0; + P.values.forEach((value, i) => { + const expected = -Math.PI / 2 + (2 * Math.PI * acc) / total; + acc += value; + if (value <= 0) return; + const prevIndex = previousVisible(P.values, i); + const own = P.wedges[i]; + const other = P.wedges[prevIndex]; + const near = (2.5 * Math.PI) / 180 + 4 / rho; + let sw = 0; + let sa = 0; + const step = 0.25 / rho; + for (let a = expected - near; a <= expected + near; a += step) { + const c = at(a); + const toOwn = colorDistance(c, own.rgb); + const toOther = colorDistance(c, other.rgb); + const span = Math.min(colorDistance(P.surface, own.rgb), colorDistance(P.surface, other.rgb)); + const sep = clamp(1 - colorDistance(c, P.surface) / Math.max(1, span), 0, 1); + const w = toOwn > 30 && toOther > 30 ? Math.max(0, (sep - 0.25) / 0.75) : 0; + sw += w; + sa += w * a; + } + if (sw * step * rho < 0.5) { + items.push({ kind: 'boundary', label: '第 ' + (i + 1) + ' 块的起点', ok: false, errorPx: null, note: '分隔线没量到' }); + return; + } + const err = Math.abs(sa / sw - expected) * rho; + items.push({ kind: 'boundary', label: '第 ' + (i + 1) + ' 块的起点', ok: err <= tol, errorPx: round(err, 2) }); + }); + } + return summarize(items, tol, ''); +} + +function previousVisible(values, i) { + for (let k = 1; k <= values.length; k += 1) { + const j = (i - k + values.length) % values.length; + if (values[j] > 0) return j; + } + return i; +} + +function summarize(items, tol, failNote, extra) { + const bad = items.filter((item) => !item.ok); + const errors = items.map((item) => item.errorPx).filter((value) => typeof value === 'number'); + const maxErrorPx = errors.length ? round(Math.max(...errors), 2) : null; + const ok = !failNote && items.length > 0 && !bad.length; + let note = failNote; + if (!note) { + note = ok + ? '量回来 ' + items.length + ' 处,最大偏差 ' + (maxErrorPx === null ? 0 : maxErrorPx) + ' 像素(允许 ' + round(tol, 1) + ')' + : bad + .slice(0, 3) + .map((item) => (item.kind === 'tick' ? '刻度 ' : item.kind === 'bar' ? '柱子 ' : item.kind === 'point' ? '数据点 ' : '') + item.label + ':' + (item.note || '差 ' + item.errorPx + ' 像素')) + .join(';'); + } + return { ok, note, checked: items.length, maxErrorPx, tolerancePx: round(tol, 2), failed: bad.slice(0, 6), ...(extra || {}) }; +} + +function linearFit(xs, ys) { + const n = xs.length; + const mx = xs.reduce((a, b) => a + b, 0) / n; + const my = ys.reduce((a, b) => a + b, 0) / n; + let sxx = 0; + let sxy = 0; + for (let i = 0; i < n; i += 1) { + sxx += (xs[i] - mx) * (xs[i] - mx); + sxy += (xs[i] - mx) * (ys[i] - my); + } + const a = sxx > 0 ? sxy / sxx : 0; + return { a, b: my - a * mx }; +} + +function medianColor(colors) { + return [0, 1, 2].map((k) => { + const list = colors.map((c) => c[k]).sort((a, b) => a - b); + return list[list.length >> 1]; + }); +} + +// ---------------------------------------------------------------- 小工具 + +function parseHex(value) { + const hex = hexColor(value); + if (!hex) return null; + return [parseInt(hex.slice(1, 3), 16), parseInt(hex.slice(3, 5), 16), parseInt(hex.slice(5, 7), 16)]; +} + +export function toHex(rgb) { + return '#' + rgb.map((v) => Math.round(v).toString(16).padStart(2, '0')).join('').toUpperCase(); +} + +function lumOf(rgb) { + return 0.299 * rgb[0] + 0.587 * rgb[1] + 0.114 * rgb[2]; +} + +function colorDistance(a, b) { + return Math.hypot(a[0] - b[0], a[1] - b[1], a[2] - b[2]); +} + +function clamp(value, lo, hi) { + return value < lo ? lo : value > hi ? hi : value; +} + +function clampInt(value, lo, hi) { + return Math.max(lo, Math.min(hi, Math.round(value))); +} + +function round(value, digits) { + if (typeof value !== 'number' || !Number.isFinite(value)) return 0; + const k = 10 ** digits; + return Math.round(value * k) / k; +} diff --git a/plugins/liddalidd/h3-image-executor/miniapp/node/lib/font.mjs b/plugins/liddalidd/h3-image-executor/miniapp/node/lib/font.mjs new file mode 100644 index 00000000..60e51d6e --- /dev/null +++ b/plugins/liddalidd/h3-image-executor/miniapp/node/lib/font.mjs @@ -0,0 +1,526 @@ +// TrueType 字体解析:给程序排字用(textoverlay.mjs)。只认 glyf 轮廓(.ttf、.ttc 里的 TrueType 字面), +// 不认 CFF 轮廓(.otf、冬青黑体这类):listFaces 会把它们标成 cff,调用方跳过。 +// +// 只读排字要用的几张表:head / hhea / maxp / hmtx / loca / cmap / name,外加可选的 OS/2。 +// 字形轮廓按需读:系统黑体一个文件 56 MB,整份读进内存不划算,排一段字只用得到几百个字形。 +import { open, readFile } from 'node:fs/promises'; + +// ---------------------------------------------------------------- 数据源 + +function bufferSource(buffer) { + return { + size: buffer.length, + async read(offset, length) { + if (offset < 0 || offset + length > buffer.length) throw new Error('font_out_of_range'); + return buffer.subarray(offset, offset + length); + }, + async readMany(ranges) { + return ranges.map(([offset, length]) => buffer.subarray(offset, offset + length)); + }, + }; +} + +function fileSource(path, size) { + async function withHandle(fn) { + const handle = await open(path, 'r'); + try { + return await fn(handle); + } finally { + await handle.close(); + } + } + async function readAt(handle, offset, length) { + const out = Buffer.alloc(length); + let done = 0; + while (done < length) { + const { bytesRead } = await handle.read(out, done, length - done, offset + done); + if (!bytesRead) throw new Error('font_out_of_range'); + done += bytesRead; + } + return out; + } + return { + size, + read: (offset, length) => withHandle((handle) => readAt(handle, offset, length)), + readMany: (ranges) => + withHandle(async (handle) => { + const out = []; + for (const [offset, length] of ranges) out.push(await readAt(handle, offset, length)); + return out; + }), + }; +} + +// ---------------------------------------------------------------- 对外入口 + +// 列出一个字体文件里的全部字面(.ttc 有多个)。返回 [{ index, family, subfamily, outlines }]。 +export async function listFaces(path) { + const handle = await open(path, 'r'); + let size = 0; + try { + size = (await handle.stat()).size; + } finally { + await handle.close(); + } + const source = fileSource(path, size); + const offsets = await faceOffsets(source); + const out = []; + for (let index = 0; index < offsets.length; index += 1) { + try { + const tables = await readDirectory(source, offsets[index]); + const names = tables.name ? parseNames(await source.read(tables.name.offset, tables.name.length)) : {}; + out.push({ + index, + family: names.family || '', + familyEn: names.familyEn || '', + subfamily: names.subfamily || '', + outlines: tables.glyf && tables.loca ? 'glyf' : tables['CFF '] || tables.CFF2 ? 'cff' : 'unknown', + }); + } catch { + out.push({ index, family: '', familyEn: '', subfamily: '', outlines: 'unknown' }); + } + } + return out; +} + +// 打开一个字面。options.index:.ttc 里的第几个字面(默认 0)。 +export async function openFont(path, options) { + const handle = await open(path, 'r'); + let size = 0; + try { + size = (await handle.stat()).size; + } finally { + await handle.close(); + } + return loadFace(fileSource(path, size), options, path); +} + +// 整个文件读进来再解析:随包的字体只有几 MB,排一整篇长文时比按需读快。 +export async function openFontFully(path, options) { + return loadFace(bufferSource(await readFile(path)), options, path); +} + +// ---------------------------------------------------------------- 解析 + +async function faceOffsets(source) { + const head = await source.read(0, 12); + const tag = head.toString('latin1', 0, 4); + if (tag === 'ttcf') { + const count = head.readUInt32BE(8); + if (!count || count > 64) throw new Error('font_bad_ttc'); + const table = await source.read(12, count * 4); + return Array.from({ length: count }, (_, i) => table.readUInt32BE(i * 4)); + } + const version = head.readUInt32BE(0); + if (version === 0x00010000 || tag === 'true' || tag === 'OTTO') return [0]; + throw new Error('font_unknown_format'); +} + +async function readDirectory(source, offset) { + const head = await source.read(offset, 12); + const count = head.readUInt16BE(4); + if (!count || count > 128) throw new Error('font_bad_directory'); + const dir = await source.read(offset + 12, count * 16); + const tables = {}; + for (let i = 0; i < count; i += 1) { + const at = i * 16; + tables[dir.toString('latin1', at, at + 4)] = { offset: dir.readUInt32BE(at + 8), length: dir.readUInt32BE(at + 12) }; + } + return tables; +} + +async function loadFace(source, options, path) { + const index = Math.max(0, Number((options && options.index) || 0)); + const offsets = await faceOffsets(source); + if (index >= offsets.length) throw new Error('font_no_such_face'); + const tables = await readDirectory(source, offsets[index]); + for (const name of ['head', 'hhea', 'maxp', 'hmtx', 'cmap']) { + if (!tables[name]) throw new Error('font_missing_table:' + name); + } + if (!tables.glyf || !tables.loca) throw new Error('font_not_truetype'); + const want = ['head', 'hhea', 'maxp', 'hmtx', 'loca', 'cmap', 'name', 'OS/2'].filter((name) => tables[name]); + const blobs = await source.readMany(want.map((name) => [tables[name].offset, tables[name].length])); + const data = {}; + want.forEach((name, i) => { + data[name] = blobs[i]; + }); + + const head = data.head; + const unitsPerEm = head.readUInt16BE(18) || 1000; + const longLoca = head.readInt16BE(50) === 1; + const numGlyphs = data.maxp.readUInt16BE(4); + const hhea = data.hhea; + let ascender = hhea.readInt16BE(4); + let descender = hhea.readInt16BE(6); + const lineGap = hhea.readInt16BE(8); + const numberOfHMetrics = hhea.readUInt16BE(34); + if (data['OS/2'] && data['OS/2'].length >= 72) { + const typoAsc = data['OS/2'].readInt16BE(68); + const typoDesc = data['OS/2'].readInt16BE(70); + if (typoAsc > 0 && typoDesc < 0) { + ascender = typoAsc; + descender = typoDesc; + } + } + const hmtx = data.hmtx; + const loca = data.loca; + const locaAt = (gid) => (longLoca ? loca.readUInt32BE(gid * 4) : loca.readUInt16BE(gid * 2) * 2); + const cmap = parseCmap(data.cmap); + const names = data.name ? parseNames(data.name) : {}; + const glyfOffset = tables.glyf.offset; + const glyfLength = tables.glyf.length; + const outlines = new Map(); + + function glyphId(codePoint) { + return cmap(codePoint); + } + + function advance(gid) { + if (!numberOfHMetrics) return unitsPerEm; + const at = Math.min(gid, numberOfHMetrics - 1) * 4; + return hmtx.readUInt16BE(at); + } + + function glyphRange(gid) { + if (gid < 0 || gid >= numGlyphs) return null; + const start = locaAt(gid); + const end = locaAt(gid + 1); + if (end <= start || start >= glyfLength) return null; + return [glyfOffset + start, Math.min(end, glyfLength) - start]; + } + + // 把一批字形的轮廓读进来(含复合字形引用的部件,递归读)。 + async function loadGlyphs(gids) { + const raw = new Map(); + let pending = [...new Set(gids)].filter((gid) => gid > 0 && !outlines.has(gid)); + while (pending.length) { + const ranges = []; + const order = []; + for (const gid of pending) { + if (raw.has(gid)) continue; + const range = glyphRange(gid); + if (!range) { + raw.set(gid, null); + continue; + } + ranges.push(range); + order.push(gid); + } + const blobs = ranges.length ? await source.readMany(ranges) : []; + const next = []; + order.forEach((gid, i) => { + const blob = Buffer.from(blobs[i]); + raw.set(gid, blob); + if (blob.length >= 10 && blob.readInt16BE(0) < 0) { + for (const part of compositeParts(blob)) if (!raw.has(part) && !outlines.has(part)) next.push(part); + } + }); + pending = [...new Set(next)]; + } + const resolve = (gid, depth) => { + if (outlines.has(gid)) return outlines.get(gid); + const blob = raw.get(gid); + let outline = null; + if (blob && blob.length >= 10) { + outline = blob.readInt16BE(0) >= 0 ? parseSimple(blob) : depth < 8 ? parseComposite(blob, (part) => resolve(part, depth + 1)) : null; + } + outlines.set(gid, outline || EMPTY); + return outlines.get(gid); + }; + for (const gid of gids) if (gid > 0) resolve(gid, 0); + } + + // 排字前一次性把要用的字形读好。返回缺字(字体里没有的字符)。 + async function prepare(text) { + const gids = []; + const missing = []; + for (const ch of new Set(Array.from(String(text || '')))) { + const cp = ch.codePointAt(0); + if (cp <= 0x20 || cp === 0x3000) continue; + const gid = glyphId(cp); + if (gid) gids.push(gid); + else missing.push(ch); + } + await loadGlyphs(gids); + return missing; + } + + function outline(gid) { + const hit = outlines.get(gid); + return hit && hit !== EMPTY ? hit : null; + } + + return { + path, + index, + family: names.family || names.familyEn || '', + familyEn: names.familyEn || '', + subfamily: names.subfamily || '', + unitsPerEm, + ascender, + descender, + lineGap, + numGlyphs, + glyphId, + has: (ch) => Boolean(glyphId(String(ch).codePointAt(0))), + advance, + loadGlyphs, + prepare, + outline, + }; +} + +const EMPTY = Object.freeze({ contours: [], xMin: 0, yMin: 0, xMax: 0, yMax: 0, empty: true }); + +// cmap:优先 Unicode 全平面(格式 12),其次 BMP(格式 4)。返回 codePoint -> glyphId 的查找函数。 +function parseCmap(table) { + const count = table.readUInt16BE(2); + let best = null; + let bestRank = -1; + for (let i = 0; i < count; i += 1) { + const platform = table.readUInt16BE(4 + i * 8); + const encoding = table.readUInt16BE(6 + i * 8); + const offset = table.readUInt32BE(8 + i * 8); + if (offset + 2 > table.length) continue; + const format = table.readUInt16BE(offset); + const unicode = platform === 0 || (platform === 3 && (encoding === 1 || encoding === 10)); + if (!unicode) continue; + const rank = format === 12 ? 2 : format === 4 ? 1 : -1; + if (rank > bestRank) { + best = { offset, format }; + bestRank = rank; + } + } + if (!best) return () => 0; + const base = best.offset; + if (best.format === 12) { + const groups = table.readUInt32BE(base + 12); + const starts = new Uint32Array(groups); + const ends = new Uint32Array(groups); + const glyphs = new Uint32Array(groups); + for (let g = 0; g < groups; g += 1) { + const at = base + 16 + g * 12; + starts[g] = table.readUInt32BE(at); + ends[g] = table.readUInt32BE(at + 4); + glyphs[g] = table.readUInt32BE(at + 8); + } + return (cp) => { + let lo = 0; + let hi = groups - 1; + while (lo <= hi) { + const mid = (lo + hi) >> 1; + if (cp < starts[mid]) hi = mid - 1; + else if (cp > ends[mid]) lo = mid + 1; + else return glyphs[mid] + (cp - starts[mid]); + } + return 0; + }; + } + const segCount = table.readUInt16BE(base + 6) / 2; + const endAt = base + 14; + const startAt = endAt + segCount * 2 + 2; + const deltaAt = startAt + segCount * 2; + const rangeAt = deltaAt + segCount * 2; + return (cp) => { + if (cp > 0xffff) return 0; + let lo = 0; + let hi = segCount - 1; + while (lo <= hi) { + const mid = (lo + hi) >> 1; + const end = table.readUInt16BE(endAt + mid * 2); + const start = table.readUInt16BE(startAt + mid * 2); + if (cp > end) lo = mid + 1; + else if (cp < start) hi = mid - 1; + else { + const delta = table.readInt16BE(deltaAt + mid * 2); + const rangeOffset = table.readUInt16BE(rangeAt + mid * 2); + if (!rangeOffset) return (cp + delta) & 0xffff; + const at = rangeAt + mid * 2 + rangeOffset + (cp - start) * 2; + if (at + 2 > table.length) return 0; + const gid = table.readUInt16BE(at); + return gid ? (gid + delta) & 0xffff : 0; + } + } + return 0; + }; +} + +// name 表:字族名(优先简体中文,其次英文)和字重名。 +function parseNames(table) { + const count = table.readUInt16BE(2); + const storage = table.readUInt16BE(4); + const found = {}; + for (let i = 0; i < count; i += 1) { + const at = 6 + i * 12; + if (at + 12 > table.length) break; + const platform = table.readUInt16BE(at); + const language = table.readUInt16BE(at + 4); + const nameId = table.readUInt16BE(at + 6); + const length = table.readUInt16BE(at + 8); + const offset = table.readUInt16BE(at + 10); + if (platform !== 3 || (nameId !== 1 && nameId !== 2 && nameId !== 16)) continue; + const start = storage + offset; + if (start + length > table.length) continue; + const value = Buffer.from(table.subarray(start, start + length)).swap16().toString('utf16le'); + const key = nameId === 2 ? 'subfamily' : 'family'; + if (language === 0x0804) found[key + 'Zh'] = found[key + 'Zh'] || value; + else if (language === 0x0409) found[key + 'En'] = nameId === 16 ? value : found[key + 'En'] || value; + else found[key + 'Any'] = found[key + 'Any'] || value; + } + return { + family: found.familyZh || found.familyEn || found.familyAny || '', + familyEn: found.familyEn || found.familyAny || '', + subfamily: found.subfamilyEn || found.subfamilyZh || found.subfamilyAny || '', + }; +} + +// 简单字形:轮廓点(on = 是否在曲线上)。坐标是字体单位,y 轴朝上。 +function parseSimple(blob) { + const contoursCount = blob.readInt16BE(0); + const box = { xMin: blob.readInt16BE(2), yMin: blob.readInt16BE(4), xMax: blob.readInt16BE(6), yMax: blob.readInt16BE(8) }; + if (!contoursCount) return { contours: [], ...box }; + let at = 10; + const ends = []; + for (let i = 0; i < contoursCount; i += 1, at += 2) ends.push(blob.readUInt16BE(at)); + const points = ends[ends.length - 1] + 1; + const instructionLength = blob.readUInt16BE(at); + at += 2 + instructionLength; + const flags = new Uint8Array(points); + for (let i = 0; i < points; ) { + const flag = blob[at++]; + flags[i++] = flag; + if (flag & 8) { + let repeat = blob[at++]; + while (repeat-- > 0 && i < points) flags[i++] = flag; + } + } + const xs = new Int32Array(points); + const ys = new Int32Array(points); + let value = 0; + for (let i = 0; i < points; i += 1) { + const flag = flags[i]; + if (flag & 2) { + const delta = blob[at++]; + value += flag & 16 ? delta : -delta; + } else if (!(flag & 16)) { + value += blob.readInt16BE(at); + at += 2; + } + xs[i] = value; + } + value = 0; + for (let i = 0; i < points; i += 1) { + const flag = flags[i]; + if (flag & 4) { + const delta = blob[at++]; + value += flag & 32 ? delta : -delta; + } else if (!(flag & 32)) { + value += blob.readInt16BE(at); + at += 2; + } + ys[i] = value; + } + const contours = []; + let startPoint = 0; + for (const end of ends) { + const contour = []; + for (let i = startPoint; i <= end; i += 1) contour.push({ x: xs[i], y: ys[i], on: Boolean(flags[i] & 1) }); + if (contour.length) contours.push(contour); + startPoint = end + 1; + } + return { contours, ...box }; +} + +function compositeParts(blob) { + const parts = []; + let at = 10; + for (let guard = 0; guard < 64; guard += 1) { + if (at + 4 > blob.length) break; + const flags = blob.readUInt16BE(at); + parts.push(blob.readUInt16BE(at + 2)); + at += 4 + (flags & 1 ? 4 : 2); + if (flags & 8) at += 2; + else if (flags & 0x40) at += 4; + else if (flags & 0x80) at += 8; + if (!(flags & 0x20)) break; + } + return parts; +} + +const f2dot14 = (blob, at) => blob.readInt16BE(at) / 16384; + +// 复合字形:把部件的轮廓按各自的变换拼起来。 +function parseComposite(blob, resolve) { + const box = { xMin: blob.readInt16BE(2), yMin: blob.readInt16BE(4), xMax: blob.readInt16BE(6), yMax: blob.readInt16BE(8) }; + const contours = []; + const placed = []; + let at = 10; + for (let guard = 0; guard < 64; guard += 1) { + if (at + 4 > blob.length) break; + const flags = blob.readUInt16BE(at); + const gid = blob.readUInt16BE(at + 2); + at += 4; + let arg1; + let arg2; + if (flags & 1) { + arg1 = flags & 2 ? blob.readInt16BE(at) : blob.readUInt16BE(at); + arg2 = flags & 2 ? blob.readInt16BE(at + 2) : blob.readUInt16BE(at + 2); + at += 4; + } else { + arg1 = flags & 2 ? blob.readInt8(at) : blob[at]; + arg2 = flags & 2 ? blob.readInt8(at + 1) : blob[at + 1]; + at += 2; + } + let a = 1; + let b = 0; + let c = 0; + let d = 1; + if (flags & 8) { + a = d = f2dot14(blob, at); + at += 2; + } else if (flags & 0x40) { + a = f2dot14(blob, at); + d = f2dot14(blob, at + 2); + at += 4; + } else if (flags & 0x80) { + a = f2dot14(blob, at); + b = f2dot14(blob, at + 2); + c = f2dot14(blob, at + 4); + d = f2dot14(blob, at + 6); + at += 8; + } + const part = resolve(gid); + if (part && part.contours.length) { + const transformed = part.contours.map((contour) => + contour.map((p) => ({ x: a * p.x + c * p.y, y: b * p.x + d * p.y, on: p.on })), + ); + let dx = 0; + let dy = 0; + if (flags & 2) { + dx = arg1; + dy = arg2; + // 0x800(SCALED_COMPONENT_OFFSET):偏移量也要跟着缩放(苹果的约定)。 + if (flags & 0x800) { + dx = a * arg1 + c * arg2; + dy = b * arg1 + d * arg2; + } + } else { + // 点对齐:父字形的第 arg1 个点和这个部件的第 arg2 个点重合。 + const flat = transformed.flat(); + const parentPoint = placed[arg1]; + const childPoint = flat[arg2]; + if (parentPoint && childPoint) { + dx = parentPoint.x - childPoint.x; + dy = parentPoint.y - childPoint.y; + } + } + for (const contour of transformed) { + const moved = contour.map((p) => ({ x: p.x + dx, y: p.y + dy, on: p.on })); + contours.push(moved); + placed.push(...moved); + } + } + if (!(flags & 0x20)) break; + } + return { contours, ...box }; +} diff --git a/plugins/liddalidd/h3-image-executor/miniapp/node/lib/fonts.mjs b/plugins/liddalidd/h3-image-executor/miniapp/node/lib/fonts.mjs new file mode 100644 index 00000000..e0a56d00 --- /dev/null +++ b/plugins/liddalidd/h3-image-executor/miniapp/node/lib/fonts.mjs @@ -0,0 +1,175 @@ +// 程序排字用的字体:毛笔字随包带(vendor/fonts,SIL OFL 1.1),黑体、宋体、楷体用系统自带的。 +// 只读字体文件,不写、不复制、不上传。macOS 的楷体、行楷是按需下载的,默认只有几十个字形的子集, +// 所以「楷体」在 macOS 上退回随包的毛笔楷书。 +import { access } from 'node:fs/promises'; +import { join } from 'node:path'; +import { fileURLToPath } from 'node:url'; +import { listFaces, openFont } from './font.mjs'; +import { probeChars } from './textlayout.mjs'; + +const VENDOR = fileURLToPath(new URL('../vendor/fonts/', import.meta.url)); + +// 规划节点写的 font 取值。chain:这一类字体依次尝试的来源,前面的没有就用后面的。 +export const FONT_KINDS = { + brush: { label: '毛笔楷书', chain: ['brush'], handwriting: true }, + xing: { label: '毛笔行书', chain: ['xing', 'brush'], handwriting: true }, + kai: { label: '楷体', chain: ['kai', 'brush'], handwriting: false }, + song: { label: '宋体', chain: ['song', 'hei', 'brush'], handwriting: false }, + hei: { label: '黑体', chain: ['hei', 'song', 'brush'], handwriting: false }, +}; + +// 缺字时补字的顺序:主字体没有的字,按这个顺序找一个有的。 +const FALLBACK_ORDER = ['hei', 'song', 'brush']; + +function windowsFontDir() { + return join(process.env.WINDIR || process.env.SystemRoot || 'C:\\Windows', 'Fonts'); +} + +const SOURCES = { + brush: [{ bundled: 'MaShanZheng-Regular.ttf', name: '马善政毛笔楷书 Ma Shan Zheng' }], + xing: [{ bundled: 'ZhiMangXing-Regular.ttf', name: '钟齐志莽行书 Zhi Mang Xing' }], + kai: [{ win: 'simkai.ttf', name: '楷体' }], + song: [ + { mac: '/System/Library/Fonts/Supplemental/Songti.ttc', family: /宋体-简|Songti SC/, subfamily: /^Regular$/i, name: '宋体-简' }, + { win: 'simsun.ttc', index: 0, name: '宋体' }, + ], + hei: [ + { mac: '/System/Library/Fonts/STHeiti Medium.ttc', family: /黑体-简|Heiti SC/, name: '黑体-简' }, + { mac: '/System/Library/Fonts/STHeiti Light.ttc', family: /黑体-简|Heiti SC/, name: '黑体-简' }, + { win: 'msyh.ttc', index: 0, name: '微软雅黑' }, + { win: 'simhei.ttf', name: '黑体' }, + ], +}; + +function sourcePath(source) { + if (source.bundled) return join(VENDOR, source.bundled); + if (source.mac) return source.mac; + if (source.win) return join(windowsFontDir(), source.win); + return ''; +} + +const cache = new Map(); + +async function exists(path) { + try { + await access(path); + return true; + } catch { + return false; + } +} + +// 打开一个来源;文件不在、不是 TrueType 轮廓、找不到指定字面时返回 null。 +async function openSource(source) { + if (source.mac && process.platform !== 'darwin') return null; + if (source.win && process.platform !== 'win32') return null; + const path = sourcePath(source); + if (!path || !(await exists(path))) return null; + let index = source.index || 0; + if (source.family) { + const faces = await listFaces(path); + const face = faces.find( + (item) => + item.outlines === 'glyf' && + (source.family.test(item.family) || source.family.test(item.familyEn)) && + (!source.subfamily || source.subfamily.test(item.subfamily)), + ) || faces.find((item) => item.outlines === 'glyf' && (source.family.test(item.family) || source.family.test(item.familyEn))); + if (!face) return null; + index = face.index; + } + const font = await openFont(path, { index }); + font.label = source.name; + return font; +} + +async function openKind(kind) { + if (cache.has(kind)) return cache.get(kind); + const task = (async () => { + for (const source of SOURCES[kind] || []) { + try { + const font = await openSource(source); + if (font) return font; + } catch { + // 换下一个来源。 + } + } + return null; + })(); + cache.set(kind, task); + return task; +} + +// 本机每种书体实际会用哪个字体,写进规划提示词:没有楷体的机器上「楷体」落到毛笔楷书, +// 规划以为排的是识字教学用的标准楷体,交付的却是毛笔字(真机 20261117-f57a0f)。 +// 返回 [{ kind, label, used(实际用的书体), name(字体名)}],used 为空表示这一类一个字体都没有。 +export async function describeFontKinds() { + const out = []; + for (const [kind, spec] of Object.entries(FONT_KINDS)) { + let used = ''; + let name = ''; + for (const candidate of spec.chain) { + const font = await openKind(candidate); + if (font) { + used = candidate; + name = font.label || font.family || ''; + break; + } + } + out.push({ kind, label: spec.label, used, name }); + } + return out; +} + +export function normalizeFontKind(value) { + const key = String(value || '').trim().toLowerCase(); + if (FONT_KINDS[key]) return key; + // 行楷要的是清楚好认,和规划提示词一致归到毛笔楷书;行书、草书才用更连贯的行书字体。 + if (/行楷/.test(key)) return 'brush'; + if (/行书|草书|xing|running|cursive/.test(key)) return 'xing'; + if (/毛笔|书法|brush|calligraph/.test(key)) return 'brush'; + if (/楷|kai/.test(key)) return 'kai'; + if (/宋|明|song|serif|ming/.test(key)) return 'song'; + return 'hei'; +} + +// 给一段文字配字体:返回 { fonts, kind, label, handwriting, missing, substituted }。 +// fonts[0] 是主字体,后面是补字用的;missing 是哪个字体都没有的字。 +export async function resolveFonts(kindValue, text) { + const kind = normalizeFontKind(kindValue); + const spec = FONT_KINDS[kind]; + let main = null; + let mainKind = ''; + for (const candidate of spec.chain) { + main = await openKind(candidate); + if (main) { + mainKind = candidate; + break; + } + } + const fonts = main ? [main] : []; + for (const candidate of FALLBACK_ORDER) { + if (candidate === mainKind) continue; + const font = await openKind(candidate); + if (font && !fonts.includes(font)) fonts.push(font); + } + if (!fonts.length) return { fonts, kind, label: spec.label, handwriting: false, missing: [], substituted: [] }; + const wanted = String(text || '') + probeChars() + '﹁﹂﹃﹄「」『』'; + for (const font of fonts) await font.prepare(wanted); + const missing = []; + const substituted = []; + for (const ch of new Set(Array.from(String(text || '')))) { + if (/\s/.test(ch)) continue; + const cp = ch.codePointAt(0); + if (fonts[0].glyphId(cp)) continue; + if (fonts.some((font) => font.glyphId(cp))) substituted.push(ch); + else missing.push(ch); + } + return { + fonts, + kind: mainKind || kind, + label: fonts[0].label || fonts[0].family, + handwriting: Boolean(FONT_KINDS[mainKind] && FONT_KINDS[mainKind].handwriting), + missing, + substituted, + }; +} diff --git a/plugins/liddalidd/h3-image-executor/miniapp/node/lib/gif.mjs b/plugins/liddalidd/h3-image-executor/miniapp/node/lib/gif.mjs new file mode 100644 index 00000000..626ac0d7 --- /dev/null +++ b/plugins/liddalidd/h3-image-executor/miniapp/node/lib/gif.mjs @@ -0,0 +1,1059 @@ +// 程序合成 GIF 动图。生图模型一次只出一张静态图,动图由程序把几帧拼起来:统一缩到同一尺寸, +// 所有帧共用一套调色板,编码成 GIF89a。 +// +// 各帧都是从参考图(或前一帧)改出来的,没动的地方理应和参考图一样,但模型每次都会把整幅重画一遍: +// 细纹理(笔触、颗粒、雨丝)全变了,整体色调差几级,画面还会错开一两个像素、放大不到 1%。 +// 真机 20261115-d7e30e 的水彩雨景:只改猫头的一帧有 16% 的像素差超过 14 级,改伞的两帧错开 1–3 像素; +// 原样拼起来静止的背景每一帧都在闪。所以给了参考图(base)时,每一帧先对齐参考图、校正色调, +// 再找出真正变了的区域,区域外一律用参考图的像素:背景一动不动,只有动作的部位在变。 +// 编码时和当前画面差得很少的像素沿用上一帧(透明像素 + 不清除上一帧),只更新真正变了的地方。 +// 共用调色板保证同一种颜色在每一帧里都是同一个色号,颜色不会跳。 +// 关键帧之间可以补几帧渐变(tween):几百毫秒一个姿态的关键帧,硬切看起来是一下一下跳的。 +// +// decodeGif 用来校验交付文件:帧数、尺寸、每帧时长都从写出去的字节里读回来核对。 + +export const GIF_DEFAULTS = { + edge: 768, + frameMs: 300, + // 每个通道和当前画面相差不超过这个值的像素沿用上一帧。 + stabilize: 14, + // 色号 255 留给透明。 + colors: 255, + // 相邻两个关键帧之间补几帧渐变,0 是直接切换。 + tween: 0, +}; + +// 锁背景的参数按长边 768 定,别的尺寸按比例缩放。阈值用 20261115-d7e30e 的四帧调过: +// 只改猫头的一帧,变化区域只剩猫(约 4%);改伞的帧,变化区域是人、伞和伞挪开后露出的背景(25–35%)。 +const LOCK = { + // 对齐时每一块最多往各个方向找几个像素。 + search: 4, + // 先模糊再比较:笔触、颗粒、雨丝这类细纹理的差别正负相抵,真正的形状变化留下来。 + blur: 4, + threshold: 12, + // 先腐蚀再膨胀,去掉零星的噪点。 + open: 2, + // 变化区域往外扩,把动作的边缘、跟着动的阴影一起带上。 + grow: 6, + feather: 3, + // 变化区域超过整幅的这个比例,说明整幅画都在变(换光线、换场景),锁了反而丢掉变化。 + maxArea: 0.7, +}; + +const TRANSPARENT = 255; + +export function isGif(buffer) { + return Boolean(buffer && buffer.length > 6 && /^GIF8[79]a$/.test(Buffer.from(buffer.subarray(0, 6)).toString('ascii'))); +} + +// images:解码后的帧 {width,height,channels,data}。options.width/height 给了就按它定尺寸,否则按第一帧: +// 长边缩到 edge,其余帧居中裁到同一画幅再缩放。 +// options.delays:每帧毫秒数(数组),没给的用 frameMs。loop:true 无限循环,false 只播一遍。 +// options.base:各帧共同的参考图。给了就把每一帧锁到它上面(见文件开头),不给就只按第一帧校正色调。 +// options.tween:相邻关键帧之间补几帧渐变,渐变的时间从前一个关键帧里扣,整组时长不变。 +export function makeGif(images, options) { + const opts = { ...GIF_DEFAULTS, ...(options || {}) }; + if (!Array.isArray(images) || !images.length) throw new Error('gif_no_frames'); + const size = opts.width && opts.height ? { width: opts.width, height: opts.height } : gifSize(images[0].width, images[0].height, opts.edge); + const { width, height } = size; + let frames = images.map((image) => fitFrame(image, width, height)); + let locked = null; + if (opts.base) { + const ctx = lockContext(fitFrame(opts.base, width, height)); + locked = []; + frames = frames.map((frame) => { + const result = lockFrame(frame, ctx); + locked.push(result.info); + return result.frame; + }); + } else if (opts.matchTone !== false) { + for (let i = 1; i < frames.length; i += 1) frames[i] = matchTone(frames[i], frames[0]); + } + let delays = frames.map((_, index) => clampDelay((opts.delays && opts.delays[index]) || opts.frameMs)); + let tweens = 0; + let cuts = 0; + if (opts.tween > 0 && frames.length > 1) { + const result = addTweens(frames, delays, Math.min(4, Math.round(opts.tween)), opts.loop !== false, opts.tweenMaxChange || 0); + frames = result.frames; + delays = result.delays; + tweens = result.added; + cuts = result.cuts; + } + const { palette, flat } = buildPalette(frames, opts.colors); + const dither = opts.dither === undefined ? (flat ? 'none' : 'ordered') : opts.dither; + const mapper = makeMapper(palette); + + const chunks = []; + chunks.push(Buffer.from('GIF89a', 'ascii')); + chunks.push(u16le(width), u16le(height), Buffer.from([0xf7, 0, 0])); + const table = Buffer.alloc(256 * 3); + for (let i = 0; i < palette.length; i += 1) { + table[i * 3] = palette[i][0]; + table[i * 3 + 1] = palette[i][1]; + table[i * 3 + 2] = palette[i][2]; + } + chunks.push(table); + if (opts.loop !== false) { + chunks.push(Buffer.from([0x21, 0xff, 0x0b]), Buffer.from('NETSCAPE2.0', 'ascii'), Buffer.from([0x03, 0x01, 0x00, 0x00, 0x00])); + } + + const shown = new Uint8Array(width * height * 3); + const written = []; + const changed = []; + for (let index = 0; index < frames.length; index += 1) { + const src = frames[index].data; + let rect; + let indices; + if (index === 0) { + rect = { x: 0, y: 0, w: width, h: height }; + indices = new Uint8Array(width * height); + for (let p = 0; p < width * height; p += 1) indices[p] = mapper(src, p * 3, p % width, (p / width) | 0, dither); + shown.set(src); + changed.push(1); + } else { + const mask = changeMask(src, shown, width, height, opts.stabilize); + const box = maskBounds(mask, width, height); + if (!box) { + // 和上一帧一模一样:并到上一帧的时长里,不单独写一帧。 + written[written.length - 1].delay += delays[index]; + changed.push(0); + continue; + } + rect = box; + indices = new Uint8Array(box.w * box.h); + let count = 0; + for (let y = 0; y < box.h; y += 1) { + for (let x = 0; x < box.w; x += 1) { + const p = (box.y + y) * width + box.x + x; + if (mask[p]) { + indices[y * box.w + x] = mapper(src, p * 3, box.x + x, box.y + y, dither); + shown[p * 3] = src[p * 3]; + shown[p * 3 + 1] = src[p * 3 + 1]; + shown[p * 3 + 2] = src[p * 3 + 2]; + count += 1; + } else { + indices[y * box.w + x] = TRANSPARENT; + } + } + } + changed.push(count / (width * height)); + } + written.push({ rect, indices, delay: delays[index], transparent: index > 0 }); + } + + for (const frame of written) { + const cs = Math.max(2, Math.round(frame.delay / 10)); + chunks.push(Buffer.from([0x21, 0xf9, 0x04, (1 << 2) | (frame.transparent ? 1 : 0), cs & 0xff, (cs >> 8) & 0xff, frame.transparent ? TRANSPARENT : 0, 0])); + chunks.push(Buffer.from([0x2c]), u16le(frame.rect.x), u16le(frame.rect.y), u16le(frame.rect.w), u16le(frame.rect.h), Buffer.from([0])); + chunks.push(Buffer.from([8]), subBlocks(lzwEncode(frame.indices, 8))); + } + chunks.push(Buffer.from([0x3b])); + const buffer = Buffer.concat(chunks); + return { + buffer, + width, + height, + frameCount: written.length, + delays: written.map((frame) => frame.delay), + dither, + colors: palette.length, + changed, + locked, + tweens, + // 动作跨度太大、没有补渐变的地方(叠出来是两个半透明的人影,见 addTweens)。 + cuts, + }; +} + +function clampDelay(ms) { + return Math.max(20, Math.min(10000, Math.round(Number(ms) || GIF_DEFAULTS.frameMs))); +} + +// 长边缩到 edge(不放大)后的 GIF 尺寸。 +export function gifSize(width, height, edge) { + const scale = Math.min(1, (edge || GIF_DEFAULTS.edge) / Math.max(width, height)); + return { width: Math.max(1, Math.round(width * scale)), height: Math.max(1, Math.round(height * scale)) }; +} + +// 两帧之间变了的像素占比:缩到 160 像素再比,某个通道差超过 24 级算变了。 +export function changeShare(a, b) { + const S = 160; + const w = Math.max(1, Math.round(a.width >= a.height ? S : (S * a.width) / a.height)); + const h = Math.max(1, Math.round(a.width >= a.height ? (S * a.height) / a.width : S)); + const x = resizeArea(toRgb(a), w, h).data; + const y = resizeArea(toRgb(b), w, h).data; + let changed = 0; + for (let p = 0; p < w * h; p += 1) { + const o = p * 3; + if (Math.max(Math.abs(x[o] - y[o]), Math.abs(x[o + 1] - y[o + 1]), Math.abs(x[o + 2] - y[o + 2])) > 24) changed += 1; + } + return changed / (w * h); +} + +// 各帧锁回参考图之后(和合成 GIF 时一样),相邻两帧变了多少。循环时最后一帧接回第一帧也算一步。 +// 返回 [{ from, to, change }],change 是占整幅的比例。用来找一步跨得太大的地方补中间帧(见 animation.smoothSteps)。 +export function motionSteps(images, base, options) { + const opts = options || {}; + const size = gifSize(base.width, base.height, opts.edge || 384); + const ctx = lockContext(fitFrame(base, size.width, size.height)); + const frames = images.map((image) => lockFrame(fitFrame(image, size.width, size.height), ctx).frame); + const out = []; + for (let i = 0; i < frames.length; i += 1) { + const next = i + 1 < frames.length ? i + 1 : opts.loop ? 0 : -1; + if (next < 0 || next === i) continue; + out.push({ from: i, to: next, change: Math.round(changeShare(frames[i], frames[next]) * 1000) / 1000 }); + } + return out; +} + +// 把一帧变成 GIF 尺寸的 RGB:先居中裁到目标画幅,再面积平均缩放。尺寸已经对的原样返回。 +export function fitFrame(image, width, height) { + if (image.channels === 3 && image.width === width && image.height === height) return image; + return resizeArea(cropToAspect(toRgb(image), width / height), width, height); +} + +function u16le(value) { + return Buffer.from([value & 0xff, (value >> 8) & 0xff]); +} + +export function toRgb(image) { + const { width, height, channels, data } = image; + if (channels === 3) return { width, height, channels: 3, data }; + const out = Buffer.alloc(width * height * 3); + for (let p = 0; p < width * height; p += 1) { + const s = p * channels; + let r; + let g; + let b; + let a = 255; + if (channels >= 3) { + r = data[s]; + g = data[s + 1]; + b = data[s + 2]; + if (channels === 4) a = data[s + 3]; + } else { + r = g = b = data[s]; + if (channels === 2) a = data[s + 1]; + } + // 半透明的地方铺在白底上:GIF 只有全透明和不透明两种。 + out[p * 3] = Math.round((r * a + 255 * (255 - a)) / 255); + out[p * 3 + 1] = Math.round((g * a + 255 * (255 - a)) / 255); + out[p * 3 + 2] = Math.round((b * a + 255 * (255 - a)) / 255); + } + return { width, height, channels: 3, data: out }; +} + +// 画幅差得不多的帧(接口偶尔回一个略有不同的尺寸)居中裁成第一帧的画幅,不拉伸。 +function cropToAspect(image, aspect) { + const current = image.width / image.height; + if (Math.abs(current - aspect) / aspect < 0.005) return image; + let w = image.width; + let h = image.height; + if (current > aspect) w = Math.max(1, Math.round(h * aspect)); + else h = Math.max(1, Math.round(w / aspect)); + const x0 = Math.floor((image.width - w) / 2); + const y0 = Math.floor((image.height - h) / 2); + const out = Buffer.alloc(w * h * 3); + for (let y = 0; y < h; y += 1) { + const s = ((y0 + y) * image.width + x0) * 3; + out.set(image.data.subarray(s, s + w * 3), y * w * 3); + } + return { width: w, height: h, channels: 3, data: out }; +} + +// 接口改出来的帧整体色调常和参考图差一两级(实测一帧的背景整体暗了约 3 / 255)。沿用上一帧的像素和 +// 新更新的像素拼在一起时,这点差别会在动的部位周围拼出一块看得见的色块。用两帧里没动的像素(差得少的) +// 量出每个通道的平均偏差,整帧平移回去。 +function matchTone(frame, reference) { + const a = frame.data; + const b = reference.data; + const sums = [0, 0, 0]; + let n = 0; + for (let s = 0; s < a.length; s += 3 * 3) { + if (Math.abs(a[s] - b[s]) + Math.abs(a[s + 1] - b[s + 1]) + Math.abs(a[s + 2] - b[s + 2]) > 60) continue; + sums[0] += b[s] - a[s]; + sums[1] += b[s + 1] - a[s + 1]; + sums[2] += b[s + 2] - a[s + 2]; + n += 1; + } + if (n < a.length / 3 / 3 / 10) return frame; + const offsets = sums.map((sum) => Math.max(-20, Math.min(20, Math.round(sum / n)))); + if (!offsets[0] && !offsets[1] && !offsets[2]) return frame; + const out = Buffer.alloc(a.length); + for (let s = 0; s < a.length; s += 3) { + out[s] = Math.max(0, Math.min(255, a[s] + offsets[0])); + out[s + 1] = Math.max(0, Math.min(255, a[s + 1] + offsets[1])); + out[s + 2] = Math.max(0, Math.min(255, a[s + 2] + offsets[2])); + } + return { width: frame.width, height: frame.height, channels: 3, data: out }; +} + +// ------------------------------------------------------------------ 锁背景 + +// 一帧锁到另一帧上:对齐、校正色调,只留下真正变了的区域,其余用 base 的像素(见 lockFrame)。 +// 组图的过渡格用它:过渡格是另一张图里重画的,背景的细纹理、屏幕上的走势每个像素都差一点,原样放进去, +// 前一格 → 过渡格 → 后一格的背景要闪两下,GIF 也大一倍多(真机 20261119-250f55:909 KB 变成 2430 KB)。 +// maxArea:变化区域超过整幅的这么多就不锁(默认同 LOCK.maxArea)。过渡格的背景照规定和那一格一样,变化区域大是因为主体大、 +// 动得多(真机一格双手从头上放到肩膀,变化区域 73%),这时照样锁住剩下的背景。 +export function lockToBase(frame, base, maxArea) { + return lockFrame(fitFrame(frame, base.width, base.height), lockContext(base), maxArea); +} + +function lockContext(base) { + const { width, height } = base; + const unit = Math.max(width, height) / 768; + const radius = (value) => Math.max(1, Math.round(value * unit)); + const radii = { blur: radius(LOCK.blur), open: radius(LOCK.open), grow: radius(LOCK.grow), feather: radius(LOCK.feather) }; + return { base, width, height, radii, search: Math.max(2, Math.round(LOCK.search * unit)), luma: luma(base), blurred: boxBlur(base.data, width, height, 3, radii.blur) }; +} + +// 返回锁好的帧和一段说明(对齐挪了多少、变化区域占多大),和参考图一模一样的帧原样返回、说明为 null。 +function lockFrame(frame, ctx, maxArea) { + const { base, width, height } = ctx; + if (frame === base || Buffer.compare(frame.data, base.data) === 0) return { frame: base, info: null }; + const shift = estimateShift(frame, ctx); + const aligned = shift ? warp(frame, shift, base) : frame; + const toned = matchTone(aligned, base); + const { alpha, area } = changeArea(toned, ctx); + const info = { + dx: shift ? round2(shift.bx) : 0, + dy: shift ? round2(shift.by) : 0, + scale: shift ? round2(((shift.ax + shift.ay) / 2) * 100) : 0, + area: round2(area), + locked: area <= (maxArea || LOCK.maxArea), + }; + if (!info.locked) return { frame: toned, info }; + const a = toned.data; + const b = base.data; + const out = Buffer.alloc(width * height * 3); + for (let p = 0; p < width * height; p += 1) { + const k = alpha[p]; + const s = p * 3; + if (k <= 0) { + out[s] = b[s]; + out[s + 1] = b[s + 1]; + out[s + 2] = b[s + 2]; + } else { + out[s] = Math.round(b[s] + (a[s] - b[s]) * k); + out[s + 1] = Math.round(b[s + 1] + (a[s + 1] - b[s + 1]) * k); + out[s + 2] = Math.round(b[s + 2] + (a[s + 2] - b[s + 2]) * k); + } + } + return { frame: { width, height, channels: 3, data: out }, info }; +} + +function round2(value) { + return Math.round(value * 100) / 100; +} + +// 这一帧相对参考图挪了多少:画面切成 6×8 左右的块,每块在 ±search 像素里找最贴合的位置(再用抛物线细到亚像素)。 +// 人物在动的块对不上,纹理太平的块哪儿都差不多,两种都不要;剩下的块拟合「每个方向一个缩放 + 一个平移」, +// 偏差大的块剔掉再拟合。模型改图时画面会整体放大一点点,只算平移的话画面两边仍然对不上。 +function estimateShift(frame, ctx) { + const { width: w, height: h, search: S } = ctx; + const patch = Math.max(16, Math.round(Math.min(w, h) / 6)); + const bg = ctx.luma; + const fg = luma(frame); + const side = 2 * S + 1; + const costs = new Float64Array(side * side); + const samples = Math.ceil(patch / 2) * Math.ceil(patch / 2); + const points = []; + for (let y0 = S; y0 < h - patch - S; y0 += patch) { + for (let x0 = S; x0 < w - patch - S; x0 += patch) { + for (let v = -S; v <= S; v += 1) { + for (let u = -S; u <= S; u += 1) { + let sum = 0; + for (let y = y0; y < y0 + patch; y += 2) { + const rb = y * w; + const rf = (y + v) * w + u; + for (let x = x0; x < x0 + patch; x += 2) sum += Math.abs(bg[rb + x] - fg[rf + x]); + } + costs[(v + S) * side + u + S] = sum; + } + } + let bi = 0; + for (let i = 1; i < costs.length; i += 1) if (costs[i] < costs[bi]) bi = i; + const bu = bi % side; + const bv = (bi / side) | 0; + let far = Infinity; + for (let i = 0; i < costs.length; i += 1) { + if (Math.abs((i % side) - bu) <= 1 && Math.abs(((i / side) | 0) - bv) <= 1) continue; + if (costs[i] < far) far = costs[i]; + } + if (costs[bi] / samples > 14 || far < costs[bi] * 1.15) continue; + const refine = (before, at, after) => { + const den = before - 2 * at + after; + return den > 1e-9 ? Math.max(-0.5, Math.min(0.5, (0.5 * (before - after)) / den)) : 0; + }; + const du = bu > 0 && bu < side - 1 ? refine(costs[bi - 1], costs[bi], costs[bi + 1]) : 0; + const dv = bv > 0 && bv < side - 1 ? refine(costs[bi - side], costs[bi], costs[bi + side]) : 0; + points.push({ x: x0 + patch / 2, y: y0 + patch / 2, dx: bu - S + du, dy: bv - S + dv }); + } + } + if (points.length < 3) return null; + const fx = fitAxis(points.map((p) => p.x), points.map((p) => p.dx), w / 2, S); + const fy = fitAxis(points.map((p) => p.y), points.map((p) => p.dy), h / 2, S); + // 挪得最多的地方(画面四角)也不到 0.25 像素:不重采样,免得白白糊一点。 + if (Math.abs(fx.a) * (w / 2) + Math.abs(fx.b) < 0.25 && Math.abs(fy.a) * (h / 2) + Math.abs(fy.b) < 0.25) return null; + return { ax: fx.a, bx: fx.b, ay: fy.a, by: fy.b, points: points.length }; +} + +// 位移 = a ×(坐标 − 中心)+ b,最小二乘;和拟合差 0.75 像素以上的块剔掉再来,最多三遍。块不到 6 个只算平移。 +function fitAxis(coords, shifts, center, limit) { + let keep = coords.map(() => true); + let a = 0; + let b = 0; + for (let round = 0; round < 3; round += 1) { + const idx = []; + for (let i = 0; i < coords.length; i += 1) if (keep[i]) idx.push(i); + if (idx.length >= 6) { + let sx = 0; + let sy = 0; + let sxx = 0; + let sxy = 0; + for (const i of idx) { + const x = coords[i] - center; + sx += x; + sy += shifts[i]; + sxx += x * x; + sxy += x * shifts[i]; + } + const n = idx.length; + const den = n * sxx - sx * sx; + a = den > 1e-9 ? Math.max(-0.02, Math.min(0.02, (n * sxy - sx * sy) / den)) : 0; + b = (sy - a * sx) / n; + } else { + a = 0; + b = median(idx.map((i) => shifts[i])); + } + const next = coords.map((c, i) => Math.abs(a * (c - center) + b - shifts[i]) < 0.75); + if (next.filter(Boolean).length < 3) break; + keep = next; + } + return { a, b: Math.max(-limit, Math.min(limit, b)) }; +} + +function median(values) { + if (!values.length) return 0; + const sorted = values.slice().sort((x, y) => x - y); + const mid = sorted.length >> 1; + return sorted.length % 2 ? sorted[mid] : (sorted[mid - 1] + sorted[mid]) / 2; +} + +// 按拟合出来的位移双线性取样,把这一帧挪回参考图的位置。挪完落到画面外、这一帧里没有的那一两个像素, +// 用参考图补上;照边缘像素外延的话,边上会多出一条每帧都在变的细线。 +function warp(frame, shift, base) { + const { width: w, height: h, data } = frame; + const out = Buffer.alloc(w * h * 3); + const cx = w / 2; + const cy = h / 2; + for (let y = 0; y < h; y += 1) { + const ty = y + shift.ay * (y - cy) + shift.by; + const sy = Math.max(0, Math.min(h - 1.001, ty)); + const y0 = Math.floor(sy); + const fy = sy - y0; + const y1 = Math.min(h - 1, y0 + 1); + for (let x = 0; x < w; x += 1) { + const o = (y * w + x) * 3; + const tx = x + shift.ax * (x - cx) + shift.bx; + if (tx < -0.01 || tx > w - 0.99 || ty < -0.01 || ty > h - 0.99) { + out[o] = base.data[o]; + out[o + 1] = base.data[o + 1]; + out[o + 2] = base.data[o + 2]; + continue; + } + const sx = Math.max(0, Math.min(w - 1.001, tx)); + const x0 = Math.floor(sx); + const fx = sx - x0; + const x1 = Math.min(w - 1, x0 + 1); + const i00 = (y0 * w + x0) * 3; + const i01 = (y0 * w + x1) * 3; + const i10 = (y1 * w + x0) * 3; + const i11 = (y1 * w + x1) * 3; + for (let c = 0; c < 3; c += 1) { + const top = data[i00 + c] + (data[i01 + c] - data[i00 + c]) * fx; + const bottom = data[i10 + c] + (data[i11 + c] - data[i10 + c]) * fx; + out[o + c] = Math.round(top + (bottom - top) * fy); + } + } + } + return { width: w, height: h, channels: 3, data: out }; +} + +// 真正变了的区域:两张图都模糊之后任一通道差超过阈值,去掉零星噪点,往外扩一圈,边缘做成渐变。 +// alpha 是每个像素取这一帧的比例(0 全用参考图,1 全用这一帧);area 是扩完之后的区域占整幅的比例。 +function changeArea(frame, ctx) { + const { width: w, height: h, radii } = ctx; + const n = w * h; + const blurred = boxBlur(frame.data, w, h, 3, radii.blur); + const ref = ctx.blurred; + const core = new Float32Array(n); + for (let p = 0; p < n; p += 1) { + const s = p * 3; + const d = Math.max(Math.abs(blurred[s] - ref[s]), Math.abs(blurred[s + 1] - ref[s + 1]), Math.abs(blurred[s + 2] - ref[s + 2])); + if (d > LOCK.threshold) core[p] = 1; + } + const eroded = above(boxBlur(core, w, h, 1, radii.open), 0.999); + const opened = above(boxBlur(eroded, w, h, 1, radii.open), 0.001); + const grown = above(boxBlur(opened, w, h, 1, radii.grow), 0.001); + const alpha = boxBlur(grown, w, h, 1, radii.feather); + let area = 0; + for (let p = 0; p < n; p += 1) area += grown[p]; + return { alpha, area: area / n }; +} + +function above(values, threshold) { + const out = new Float32Array(values.length); + for (let i = 0; i < values.length; i += 1) if (values[i] > threshold) out[i] = 1; + return out; +} + +function luma(image) { + const n = image.width * image.height; + const out = new Float32Array(n); + const d = image.data; + for (let p = 0; p < n; p += 1) out[p] = 0.299 * d[p * 3] + 0.587 * d[p * 3 + 1] + 0.114 * d[p * 3 + 2]; + return out; +} + +// 方框模糊(边缘像素外延),先横后竖,每个方向用滑动窗口的累加和,和半径无关。 +function boxBlur(src, w, h, channels, r) { + const tmp = new Float32Array(w * h * channels); + const out = new Float32Array(w * h * channels); + const k = 2 * r + 1; + for (let y = 0; y < h; y += 1) { + const row = y * w; + for (let c = 0; c < channels; c += 1) { + let acc = 0; + for (let i = -r; i <= r; i += 1) acc += src[(row + Math.min(w - 1, Math.max(0, i))) * channels + c]; + for (let x = 0; x < w; x += 1) { + tmp[(row + x) * channels + c] = acc / k; + acc += src[(row + Math.min(w - 1, x + r + 1)) * channels + c] - src[(row + Math.max(0, x - r)) * channels + c]; + } + } + } + for (let x = 0; x < w; x += 1) { + for (let c = 0; c < channels; c += 1) { + let acc = 0; + for (let i = -r; i <= r; i += 1) acc += tmp[(Math.min(h - 1, Math.max(0, i)) * w + x) * channels + c]; + for (let y = 0; y < h; y += 1) { + out[(y * w + x) * channels + c] = acc / k; + acc += tmp[(Math.min(h - 1, y + r + 1) * w + x) * channels + c] - tmp[(Math.max(0, y - r) * w + x) * channels + c]; + } + } + } + return out; +} + +// ------------------------------------------------------------------ 过渡帧 + +// 每个关键帧和下一帧(循环时最后一帧接回第一帧)之间插 count 帧渐变。渐变每帧的时长取两帧里短的那个的 15% +// (40–100 毫秒),从前一个关键帧的时长里扣,关键帧自己至少留一半;关键帧短于 120 毫秒不插,快节奏的本来就看不出跳。 +// 渐变是两张图按比例叠在一起,不是中间的动作:两帧差得多(转头、手从画面外抬进来)时叠出来是两个半透明的人影 +// (真机 20261117-21581a:头周围一圈虚影、手半透明)。maxChange 给了就只在变化不超过它的两帧之间补渐变。 +function addTweens(frames, delays, count, loop, maxChange) { + const outFrames = []; + const outDelays = []; + let added = 0; + let cuts = 0; + for (let i = 0; i < frames.length; i += 1) { + const next = i + 1 < frames.length ? i + 1 : loop ? 0 : -1; + let hold = delays[i]; + const extra = []; + let step = 0; + if (next >= 0 && next !== i && Buffer.compare(frames[i].data, frames[next].data) !== 0) { + step = tweenDelay(delays[i], delays[next]); + if (step && maxChange > 0 && changeShare(frames[i], frames[next]) > maxChange) { + step = 0; + cuts += 1; + } + const k = step ? Math.min(count, Math.floor(hold / 2 / step)) : 0; + for (let j = 1; j <= k; j += 1) extra.push(blend(frames[i], frames[next], j / (k + 1))); + hold -= k * step; + } + outFrames.push(frames[i]); + outDelays.push(hold); + for (const frame of extra) { + outFrames.push(frame); + outDelays.push(step); + added += 1; + } + } + return { frames: outFrames, delays: outDelays, added, cuts }; +} + +function tweenDelay(a, b) { + const shorter = Math.min(a, b); + if (shorter < 120) return 0; + return Math.round(Math.max(40, Math.min(100, shorter * 0.15)) / 10) * 10; +} + +function blend(a, b, t) { + const out = Buffer.alloc(a.data.length); + for (let i = 0; i < out.length; i += 1) out[i] = Math.round(a.data[i] + (b.data[i] - a.data[i]) * t); + return { width: a.width, height: a.height, channels: 3, data: out }; +} + +// 面积平均缩放:每个目标像素取它覆盖的那块源像素的均值。缩小时不会像最近邻那样出锯齿和闪点。 +export function resizeArea(image, outW, outH) { + const src = image.channels === 3 ? image : toRgb(image); + if (src.width === outW && src.height === outH) return src; + const out = Buffer.alloc(outW * outH * 3); + const xs = new Int32Array(outW + 1); + for (let x = 0; x <= outW; x += 1) xs[x] = Math.min(src.width, Math.floor((x * src.width) / outW)); + for (let y = 0; y < outH; y += 1) { + const y0 = Math.min(src.height - 1, Math.floor((y * src.height) / outH)); + const y1 = Math.max(y0 + 1, Math.min(src.height, Math.floor(((y + 1) * src.height) / outH))); + for (let x = 0; x < outW; x += 1) { + const x0 = Math.min(src.width - 1, xs[x]); + const x1 = Math.max(x0 + 1, xs[x + 1]); + let r = 0; + let g = 0; + let b = 0; + for (let yy = y0; yy < y1; yy += 1) { + let s = (yy * src.width + x0) * 3; + for (let xx = x0; xx < x1; xx += 1) { + r += src.data[s]; + g += src.data[s + 1]; + b += src.data[s + 2]; + s += 3; + } + } + const n = (y1 - y0) * (x1 - x0); + const o = (y * outW + x) * 3; + out[o] = Math.round(r / n); + out[o + 1] = Math.round(g / n); + out[o + 2] = Math.round(b / n); + } + } + return { width: outW, height: outH, channels: 3, data: out }; +} + +// 中位切分:在所有帧的 5 位直方图上切盒子,每个盒子取盒内像素的平均色。 +// flat:前 colors 个最常见的颜色覆盖了九成像素(插画、表情包),这类图不加抖动,平涂的色块保持干净。 +function buildPalette(frames, colors) { + const count = new Uint32Array(32768); + const sums = new Float64Array(32768 * 3); + let total = 0; + for (const frame of frames) { + const pixels = frame.width * frame.height; + const step = Math.max(1, Math.floor(pixels / 160000)); + const data = frame.data; + for (let p = 0; p < pixels; p += step) { + const s = p * 3; + const bin = ((data[s] >> 3) << 10) | ((data[s + 1] >> 3) << 5) | (data[s + 2] >> 3); + count[bin] += 1; + sums[bin * 3] += data[s]; + sums[bin * 3 + 1] += data[s + 1]; + sums[bin * 3 + 2] += data[s + 2]; + total += 1; + } + } + const bins = []; + for (let bin = 0; bin < 32768; bin += 1) if (count[bin]) bins.push(bin); + const sorted = bins.map((bin) => count[bin]).sort((a, b) => b - a); + let top = 0; + for (let i = 0; i < Math.min(colors, sorted.length); i += 1) top += sorted[i]; + const flat = total > 0 && top / total >= 0.9; + + const channel = (bin, axis) => (axis === 0 ? bin >> 10 : axis === 1 ? (bin >> 5) & 31 : bin & 31); + const describe = (list) => { + const lo = [31, 31, 31]; + const hi = [0, 0, 0]; + let weight = 0; + for (const bin of list) { + for (let axis = 0; axis < 3; axis += 1) { + const v = channel(bin, axis); + if (v < lo[axis]) lo[axis] = v; + if (v > hi[axis]) hi[axis] = v; + } + weight += count[bin]; + } + const ranges = [hi[0] - lo[0], hi[1] - lo[1], hi[2] - lo[2]]; + const axis = ranges[1] >= ranges[0] && ranges[1] >= ranges[2] ? 1 : ranges[0] >= ranges[2] ? 0 : 2; + return { list, weight, axis, range: ranges[axis] }; + }; + const boxes = bins.length ? [describe(bins)] : []; + while (boxes.length < colors) { + let pick = -1; + let best = 0; + for (let i = 0; i < boxes.length; i += 1) { + const box = boxes[i]; + if (box.list.length < 2 || box.range === 0) continue; + const score = box.weight * (box.range + 1); + if (score > best) { + best = score; + pick = i; + } + } + if (pick < 0) break; + const box = boxes[pick]; + const list = box.list.slice().sort((a, b) => channel(a, box.axis) - channel(b, box.axis)); + let acc = 0; + let cut = 1; + for (let i = 0; i < list.length - 1; i += 1) { + acc += count[list[i]]; + cut = i + 1; + if (acc * 2 >= box.weight) break; + } + boxes.splice(pick, 1, describe(list.slice(0, cut)), describe(list.slice(cut))); + } + const palette = boxes.map((box) => { + let r = 0; + let g = 0; + let b = 0; + let n = 0; + for (const bin of box.list) { + r += sums[bin * 3]; + g += sums[bin * 3 + 1]; + b += sums[bin * 3 + 2]; + n += count[bin]; + } + return [Math.round(r / n), Math.round(g / n), Math.round(b / n)]; + }); + if (!palette.length) palette.push([0, 0, 0]); + return { palette, flat }; +} + +const BAYER4 = [0, 8, 2, 10, 12, 4, 14, 6, 3, 11, 1, 9, 15, 7, 13, 5]; +const DITHER_AMPLITUDE = 10; + +// 最近色查表:按 5 位量化后的颜色缓存结果,每个像素只查一次表。 +// 有序抖动按像素位置加固定的偏移,同一位置同一颜色每一帧都映射到同一个色号,不会在帧间闪烁。 +function makeMapper(palette) { + const cache = new Int16Array(32768).fill(-1); + const nearest = (r, g, b) => { + let best = 0; + let bestD = Infinity; + for (let i = 0; i < palette.length; i += 1) { + const c = palette[i]; + const dr = r - c[0]; + const dg = g - c[1]; + const db = b - c[2]; + const d = 2 * dr * dr + 4 * dg * dg + 3 * db * db; + if (d < bestD) { + bestD = d; + best = i; + } + } + return best; + }; + return (data, s, x, y, dither) => { + let r = data[s]; + let g = data[s + 1]; + let b = data[s + 2]; + if (dither === 'ordered') { + const t = ((BAYER4[(y & 3) * 4 + (x & 3)] + 0.5) / 16 - 0.5) * DITHER_AMPLITUDE; + r = Math.max(0, Math.min(255, r + t)); + g = Math.max(0, Math.min(255, g + t)); + b = Math.max(0, Math.min(255, b + t)); + } + const bin = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3); + let index = cache[bin]; + if (index < 0) { + index = nearest((r & 0xf8) + 4, (g & 0xf8) + 4, (b & 0xf8) + 4); + cache[bin] = index; + } + return index; + }; +} + +// 变了的像素:任一通道和当前画面差超过阈值。再向外扩一圈,把动的部位边缘的抗锯齿过渡带一起更新, +// 否则新旧两帧的边缘会叠出一圈毛边。 +function changeMask(src, shown, width, height, threshold) { + const raw = new Uint8Array(width * height); + for (let p = 0; p < width * height; p += 1) { + const s = p * 3; + if ( + Math.abs(src[s] - shown[s]) > threshold || + Math.abs(src[s + 1] - shown[s + 1]) > threshold || + Math.abs(src[s + 2] - shown[s + 2]) > threshold + ) { + raw[p] = 1; + } + } + const mask = new Uint8Array(width * height); + for (let y = 0; y < height; y += 1) { + for (let x = 0; x < width; x += 1) { + if (!raw[y * width + x]) continue; + for (let dy = -1; dy <= 1; dy += 1) { + const yy = y + dy; + if (yy < 0 || yy >= height) continue; + for (let dx = -1; dx <= 1; dx += 1) { + const xx = x + dx; + if (xx >= 0 && xx < width) mask[yy * width + xx] = 1; + } + } + } + } + return mask; +} + +function maskBounds(mask, width, height) { + let x0 = width; + let y0 = height; + let x1 = -1; + let y1 = -1; + for (let y = 0; y < height; y += 1) { + for (let x = 0; x < width; x += 1) { + if (!mask[y * width + x]) continue; + if (x < x0) x0 = x; + if (x > x1) x1 = x; + if (y < y0) y0 = y; + if (y > y1) y1 = y; + } + } + if (x1 < 0) return null; + return { x: x0, y: y0, w: x1 - x0 + 1, h: y1 - y0 + 1 }; +} + +// GIF 的 LZW:码长从 9 位涨到 12 位,码表满了发清除码重来。 +function lzwEncode(indices, minCodeSize) { + const clearCode = 1 << minCodeSize; + const eoiCode = clearCode + 1; + let nextCode = eoiCode + 1; + let codeSize = minCodeSize + 1; + let table = new Map(); + const out = []; + let cur = 0; + let shift = 0; + const emit = (code) => { + cur |= code << shift; + shift += codeSize; + while (shift >= 8) { + out.push(cur & 0xff); + cur >>>= 8; + shift -= 8; + } + }; + emit(clearCode); + let prefix = indices[0]; + for (let i = 1; i < indices.length; i += 1) { + const k = indices[i]; + const key = (prefix << 8) | k; + const code = table.get(key); + if (code !== undefined) { + prefix = code; + continue; + } + emit(prefix); + if (nextCode === 4096) { + emit(clearCode); + nextCode = eoiCode + 1; + codeSize = minCodeSize + 1; + table = new Map(); + } else { + if (nextCode >= 1 << codeSize) codeSize += 1; + table.set(key, nextCode); + nextCode += 1; + } + prefix = k; + } + emit(prefix); + emit(eoiCode); + if (shift > 0) out.push(cur & 0xff); + return Uint8Array.from(out); +} + +function subBlocks(bytes) { + const parts = []; + for (let i = 0; i < bytes.length; i += 255) { + const chunk = bytes.subarray(i, Math.min(bytes.length, i + 255)); + parts.push(Buffer.from([chunk.length]), Buffer.from(chunk)); + } + parts.push(Buffer.from([0])); + return Buffer.concat(parts); +} + +// 读回 GIF:按处置方式把每一帧合成到整张画布上,返回每帧的完整画面(RGB)和时长。 +export function decodeGif(buffer) { + if (!isGif(buffer)) throw new Error('not_gif'); + const bytes = buffer; + let pos = 6; + const width = bytes[pos] | (bytes[pos + 1] << 8); + const height = bytes[pos + 2] | (bytes[pos + 3] << 8); + const packed = bytes[pos + 4]; + const background = bytes[pos + 5]; + pos += 7; + let globalTable = null; + if (packed & 0x80) { + const size = 3 * (1 << ((packed & 7) + 1)); + globalTable = bytes.subarray(pos, pos + size); + pos += size; + } + const canvas = new Uint8Array(width * height * 4); + const frames = []; + let loop = null; + let gce = null; + while (pos < bytes.length) { + const block = bytes[pos]; + pos += 1; + if (block === 0x3b) break; + if (block === 0x21) { + const label = bytes[pos]; + pos += 1; + if (label === 0xf9) { + const size = bytes[pos]; + const flags = bytes[pos + 1]; + gce = { + disposal: (flags >> 2) & 7, + transparent: flags & 1 ? bytes[pos + 4] : -1, + delayMs: (bytes[pos + 2] | (bytes[pos + 3] << 8)) * 10, + }; + pos += 1 + size; + while (bytes[pos] !== 0) pos += 1 + bytes[pos]; + pos += 1; + } else if (label === 0xff) { + const size = bytes[pos]; + const id = Buffer.from(bytes.subarray(pos + 1, pos + 1 + size)).toString('ascii'); + pos += 1 + size; + while (bytes[pos] !== 0) { + if (id === 'NETSCAPE2.0' && bytes[pos] >= 3 && bytes[pos + 1] === 1) loop = bytes[pos + 2] | (bytes[pos + 3] << 8); + pos += 1 + bytes[pos]; + } + pos += 1; + } else { + while (bytes[pos] !== 0) pos += 1 + bytes[pos]; + pos += 1; + } + continue; + } + if (block !== 0x2c) throw new Error('gif_bad_block:' + block); + const x = bytes[pos] | (bytes[pos + 1] << 8); + const y = bytes[pos + 2] | (bytes[pos + 3] << 8); + const w = bytes[pos + 4] | (bytes[pos + 5] << 8); + const h = bytes[pos + 6] | (bytes[pos + 7] << 8); + const flags = bytes[pos + 8]; + pos += 9; + let table = globalTable; + if (flags & 0x80) { + const size = 3 * (1 << ((flags & 7) + 1)); + table = bytes.subarray(pos, pos + size); + pos += size; + } + if (!table) throw new Error('gif_no_color_table'); + const minCodeSize = bytes[pos]; + pos += 1; + const data = []; + while (bytes[pos] !== 0) { + data.push(bytes.subarray(pos + 1, pos + 1 + bytes[pos])); + pos += 1 + bytes[pos]; + } + pos += 1; + const indices = lzwDecode(Buffer.concat(data), minCodeSize, w * h); + const rows = flags & 0x40 ? interlacedRows(h) : null; + const control = gce || { disposal: 0, transparent: -1, delayMs: 0 }; + const before = control.disposal === 3 ? canvas.slice() : null; + for (let row = 0; row < h; row += 1) { + const cy = y + (rows ? rows[row] : row); + if (cy >= height) continue; + for (let col = 0; col < w; col += 1) { + const cx = x + col; + if (cx >= width) continue; + const index = indices[row * w + col]; + if (index === control.transparent) continue; + const o = (cy * width + cx) * 4; + canvas[o] = table[index * 3]; + canvas[o + 1] = table[index * 3 + 1]; + canvas[o + 2] = table[index * 3 + 2]; + canvas[o + 3] = 255; + } + } + const rgb = Buffer.alloc(width * height * 3); + for (let p = 0; p < width * height; p += 1) { + rgb[p * 3] = canvas[p * 4]; + rgb[p * 3 + 1] = canvas[p * 4 + 1]; + rgb[p * 3 + 2] = canvas[p * 4 + 2]; + } + frames.push({ delayMs: control.delayMs, disposal: control.disposal, rect: { x, y, w, h }, image: { width, height, channels: 3, data: rgb } }); + if (control.disposal === 2) { + for (let row = 0; row < h; row += 1) { + for (let col = 0; col < w; col += 1) { + const o = ((y + row) * width + x + col) * 4; + if (o + 3 < canvas.length) canvas.fill(0, o, o + 4); + } + } + } else if (before) { + canvas.set(before); + } + gce = null; + } + return { width, height, background, loop, frames }; +} + +function interlacedRows(h) { + const rows = []; + for (const [start, step] of [[0, 8], [4, 8], [2, 4], [1, 2]]) for (let r = start; r < h; r += step) rows.push(r); + return rows; +} + +function lzwDecode(bytes, minCodeSize, length) { + const out = new Uint8Array(length); + const clearCode = 1 << minCodeSize; + const eoiCode = clearCode + 1; + const prefix = new Uint16Array(4096); + const suffix = new Uint8Array(4096); + for (let i = 0; i < clearCode; i += 1) suffix[i] = i; + const stack = new Uint8Array(4097); + let codeSize = minCodeSize + 1; + let nextCode = eoiCode + 1; + let old = -1; + let first = 0; + let bits = 0; + let count = 0; + let pos = 0; + let o = 0; + while (o < length) { + while (count < codeSize) { + if (pos >= bytes.length) return out; + bits |= bytes[pos] << count; + pos += 1; + count += 8; + } + const code = bits & ((1 << codeSize) - 1); + bits >>>= codeSize; + count -= codeSize; + if (code === clearCode) { + codeSize = minCodeSize + 1; + nextCode = eoiCode + 1; + old = -1; + continue; + } + if (code === eoiCode) break; + if (old < 0) { + out[o] = suffix[code]; + o += 1; + old = code; + first = code; + continue; + } + let sp = 0; + let cur = code; + if (code >= nextCode) { + stack[sp] = first; + sp += 1; + cur = old; + } + while (cur >= clearCode) { + stack[sp] = suffix[cur]; + sp += 1; + cur = prefix[cur]; + } + stack[sp] = cur; + sp += 1; + first = cur; + while (sp > 0 && o < length) { + sp -= 1; + out[o] = stack[sp]; + o += 1; + } + if (nextCode < 4096) { + prefix[nextCode] = old; + suffix[nextCode] = first; + nextCode += 1; + if (nextCode === 1 << codeSize && codeSize < 12) codeSize += 1; + } + old = code; + } + return out; +} diff --git a/plugins/liddalidd/h3-image-executor/miniapp/node/lib/gifbuild.mjs b/plugins/liddalidd/h3-image-executor/miniapp/node/lib/gifbuild.mjs new file mode 100644 index 00000000..2c067508 --- /dev/null +++ b/plugins/liddalidd/h3-image-executor/miniapp/node/lib/gifbuild.mjs @@ -0,0 +1,479 @@ +// 拼 GIF、读回核对。两种来源: +// 组图切出来的各格(panels.mjs):按节奏排成一组帧:每格停多久、第一格和最后一格停久一点、可以来回播, +// 要的话切换时轻微弹一下(panelTimeline、renderTimeline、buildPanelGif); +// 一组现成的帧:Agent 写程序画的(tools/h3-gif.mjs frames)。 +// 编码交给 gif.mjs:所有帧共用一套调色板,同一种颜色每一帧都是同一个色号,不会一帧一跳;每帧时长可以不一样。 +// 写完从字节读回来逐帧核对帧数、时长、循环和画面(verifyGif),核对过了才算做成。 +// 做法参考 jungle-adventure 和 david-voxel-loop 两个例子:模型只出静态图,动起来全靠程序;全组一套调色板; +// 循环播放时首尾要接得上(最后一帧接回第一帧也是一步);写完读回来核对,再出一张帧拼图给人看一眼。 +import { changeShare, decodeGif, fitFrame, gifSize, lockToBase, makeGif, resizeArea, toRgb } from './gif.mjs'; +import { detectPanels, framePanels, renderPanel } from './panels.mjs'; +import { locateSpin, normalizeSpin, spinFrame } from './motion.mjs'; +import { renderGridSheet } from './review.mjs'; + +export const RHYTHM = { + frameMs: 700, + // 第一格停久一点看清起点,最后一格停得更久看清结果,再接回第一格。 + firstScale: 1.5, + lastScale: 2, + minMs: 60, + maxMs: 5000, + // 切换时轻微弹一下(spec.bounce 为 true 才加):新的一格先放大 3% 停 0.08 秒,再落回原大小。时间从这一格的停留里扣,整组时长不变。 + // 以前默认加,真机 20261119-250f55 看着是「每次切换都跳一下」:各格对齐以后头和背景本来一动不动,放大的那一帧让整幅画面 + // 先往外扩 11 像素、头往上跳 9 像素,0.08 秒后又弹回,一次切换动两下。所以默认不加,用户点名要才加。 + // 只放一帧:放大的帧每个像素都变,一帧就是几十 KB(db1dd9 六格拼成 768 像素:不弹 388 KB,弹一帧 756 KB,弹两帧约 1.1 MB)。 + pop: [{ zoom: 1.03, ms: 80 }], + // 渐变:两格之间叠两帧。两格差得多时叠出来是两个半透明的样子(真机 20261117-21581a 的虚影),所以默认直接切换。 + fade: { steps: 2, ms: 70 }, + edge: 768, + // 和当前画面差得很少的像素沿用上一帧:各格里同一片背景不会一格一格地闪。 + stabilize: 8, + // 过渡格(见 paneltween.mjs)停多久:每格停留时间的这么多,80–240 毫秒。 + tweenShare: 0.25, + tweenMinMs: 80, + tweenMaxMs: 240, +}; + +// 两格之间的过渡格用这个当名字:不分先后,往前播、往回播用的是同一格。 +export function pairKey(a, b) { + return Math.min(a, b) + '-' + Math.max(a, b); +} + +export function tweenMsFor(frameMs) { + return Math.max(RHYTHM.tweenMinMs, Math.min(RHYTHM.tweenMaxMs, Math.round((frameMs * RHYTHM.tweenShare) / 10) * 10)); +} + +// 读回核对的门槛:每一帧和要写的画面平均差不超过 meanDiff 级(调色板量化、沿用上一帧的像素都在这里面), +// 差 outlierDiff 级以上的像素不超过 outlierShare。 +export const VERIFY = { meanDiff: 6, outlierDiff: 48, outlierShare: 0.005 }; + +export function clampMs(value) { + const n = Number(value); + if (!Number.isFinite(n) || n <= 0) return 0; + return Math.round(Math.max(RHYTHM.minMs, Math.min(RHYTHM.maxMs, n))); +} + +// ------------------------------------------------------------------ 组图的节奏 + +// spec:{ order, drop, frameMs, firstMs, lastMs, hold:[{index,ms}], loop, pingpong, transition, bounce, tweens },order、drop 和 hold 的 index +// 都是格子编号(从 1 起,从上到下、从左到右数)。order 不写按编号顺序,drop 里的格子不放。bounce 写 true 才弹(见 RHYTHM.pop)。 +// tweens:有过渡格的两格(paneltween.pairKey 的写法,'2-3')。有过渡格的那一步,前一格停够之后先放过渡格(时间从前一格里扣), +// 再到后一格,不再弹一下、也不叠渐变。 +// 返回 { order, holds, turn(往前播的最后一格在 order 里的位置), steps:[{ kind:'key'|'pop'|'fade', panel, ms, zoom, to, t }], durationMs, transition, bounce, loop }。 +// GIF 的第一帧是第一格本身:很多地方拿第一帧当封面。循环播放时第一格进场的那一下弹,放在最后、接回开头之前。 +export function panelTimeline(count, spec) { + const s = spec || {}; + const loop = s.loop !== false; + const dropped = new Set((Array.isArray(s.drop) ? s.drop : []).map(Number)); + let order = (Array.isArray(s.order) && s.order.length ? s.order : range(1, count)) + .map(Number) + .filter((n) => Number.isInteger(n) && n >= 1 && n <= count && !dropped.has(n)); + order = order.filter((n, i) => i === 0 || n !== order[i - 1]); + if (loop && order.length > 2 && order[0] === order[order.length - 1]) order.pop(); + if (order.length < 2) throw new Error('panel_order_too_short'); + // 「最后一格」是往前播的最后一格:来回播时是掉头的那一格。 + const turn = order.length - 1; + if (s.pingpong && order.length > 2) order = order.concat(order.slice(1, -1).reverse()); + const frameMs = clampMs(s.frameMs) || RHYTHM.frameMs; + const custom = new Map((Array.isArray(s.hold) ? s.hold : []).map((item) => [Number(item.index), clampMs(item.ms)]).filter((pair) => pair[1])); + const holds = order.map((panel, i) => { + if (custom.has(panel)) return custom.get(panel); + if (i === 0) return clampMs(s.firstMs) || Math.round(frameMs * RHYTHM.firstScale); + if (i === turn) return clampMs(s.lastMs) || Math.round(frameMs * RHYTHM.lastScale); + return frameMs; + }); + const transition = s.transition === 'fade' ? 'fade' : 'cut'; + const bounce = transition === 'cut' && s.bounce === true; + const popMs = RHYTHM.pop.reduce((sum, item) => sum + item.ms, 0); + const fadeMs = RHYTHM.fade.steps * RHYTHM.fade.ms; + const tweenSet = new Set(Array.isArray(s.tweens) ? s.tweens : []); + const tweenMs = tweenMsFor(frameMs); + const steps = []; + let leadPops = []; + let tweens = 0; + order.forEach((panel, i) => { + let hold = holds[i]; + const next = i + 1 < order.length ? order[i + 1] : loop ? order[0] : 0; + // 停留太短的格子不弹、不渐变、不放过渡格:扣完时间只剩一闪,反而看不清。 + const tween = next && next !== panel && tweenSet.has(pairKey(panel, next)) && hold - tweenMs >= hold / 2; + // 后一格前面有过渡格的,切过去的那一下已经是一步一步的了:不再弹。 + const prev = i > 0 ? order[i - 1] : loop ? order[order.length - 1] : 0; + const arrivedByTween = prev && prev !== panel && tweenSet.has(pairKey(prev, panel)); + const pops = []; + if (bounce && !arrivedByTween && (i > 0 || loop) && hold - popMs >= hold / 2) { + for (const item of RHYTHM.pop) pops.push({ kind: 'pop', panel, zoom: item.zoom, ms: item.ms }); + hold -= popMs; + } + const after = []; + if (tween) { + after.push({ kind: 'tween', panel, to: next, key: pairKey(panel, next), ms: tweenMs, zoom: 1 }); + hold -= tweenMs; + tweens += 1; + } else if (transition === 'fade' && next && next !== panel && hold - fadeMs >= hold / 2) { + for (let k = 1; k <= RHYTHM.fade.steps; k += 1) after.push({ kind: 'fade', panel, to: next, t: k / (RHYTHM.fade.steps + 1), ms: RHYTHM.fade.ms, zoom: 1 }); + hold -= fadeMs; + } + if (i === 0) leadPops = pops; + else steps.push(...pops); + steps.push({ kind: 'key', panel, ms: hold, zoom: 1 }); + steps.push(...after); + }); + steps.push(...leadPops); + return { order, holds, turn, steps, durationMs: steps.reduce((sum, step) => sum + step.ms, 0), transition, bounce, loop, tweens, tweenMs }; +} + +// 按节奏出每一帧(GIF 尺寸的 RGB)。source(panel) 给出这一格取自哪张图的哪一块:{ image, frame, mode }。 +// tweens:Map(pairKey → { image, cell }),image 是已经对齐成和格子一样取景的过渡格,cell 是它画在哪一格上: +// 锁到那一格上,背景用那一格的像素,只留下真正在动的部位(见 gif.lockToBase)。同一格同一个缩放只出一次。 +export function renderTimeline(source, steps, size, tweens) { + const cache = new Map(); + const panelAt = (panel, zoom) => { + const key = panel + '@' + zoom; + if (!cache.has(key)) { + const from = source(panel); + cache.set(key, renderPanel(from.image, from.frame, size.width, size.height, { zoom, mode: from.mode })); + } + return cache.get(key); + }; + const tweenAt = (key) => { + const name = 'tween:' + key; + if (!cache.has(name)) { + const { image, cell } = tweens.get(key); + const drawn = renderPanel(image, { x: 0, y: 0, w: image.width, h: image.height }, size.width, size.height, {}); + cache.set(name, cell ? lockToBase(drawn, panelAt(cell, 1), 0.9).frame : drawn); + } + return cache.get(name); + }; + return steps.map((step) => { + if (step.kind === 'fade') return blend(panelAt(step.panel, 1), panelAt(step.to, 1), step.t); + if (step.kind === 'tween') return tweenAt(step.key); + return panelAt(step.panel, step.zoom || 1); + }); +} + +function blend(a, b, t) { + const out = Buffer.alloc(a.data.length); + for (let i = 0; i < out.length; i += 1) out[i] = Math.round(a.data[i] + (b.data[i] - a.data[i]) * t); + return { width: a.width, height: a.height, channels: 3, data: out }; +} + +function edgeOf(spec) { + return Math.max(64, Math.min(2048, Math.round(Number(spec && spec.edge) || RHYTHM.edge))); +} + +// 一张组图 → GIF。spec 见 panelTimeline,另有 rows / cols / count(切格的提示)、edge(GIF 长边)、align(是否对齐各格)。 +// extras.tweens:Map(pairKey → { image, cell }),见 paneltween.loadTweens。 +// 切不出两格以上时抛 not_grid。返回切格结果、取景、节奏、编码结果、核对结果和帧条。 +export function buildPanelGif(image, spec, extras) { + const s = spec || {}; + const rgb = toRgb(image); + const detection = detectPanels(rgb, { rows: s.rows, cols: s.cols, count: s.count }); + if (detection.count < 2) { + const error = new Error('not_grid'); + error.detection = detection; + throw error; + } + const framing = framePanels(rgb, detection.cells, { align: s.align !== false }); + const size = gifSize(framing.width, framing.height, edgeOf(s)); + const tweens = framing.mode === 'crop' && extras && extras.tweens instanceof Map ? extras.tweens : new Map(); + const built = assemble(detection.count, { ...s, tweens: [...tweens.keys()] }, size, (panel) => ({ image: rgb, frame: framing.frames[panel - 1], mode: framing.mode }), tweens); + return { detection, framing, ...built }; +} + +// 几张单独的图 → GIF(四张一组的漫画、一组表情):每张就是一格,按第一张的画幅统一大小。 +// 画幅差得不多的居中裁一点;差得多的整张缩进画幅、四周补这张图边上的底色。各张构图本来就不同,不对齐。 +export function buildSlidesGif(images, spec) { + const s = spec || {}; + if (!Array.isArray(images) || images.length < 2) throw new Error('slides_need_two'); + const rgbs = images.map((image) => toRgb(image)); + const aspect = rgbs[0].width / rgbs[0].height; + const mixed = rgbs.some((image) => Math.abs(image.width / image.height - aspect) / aspect > 0.12); + const frames = rgbs.map((image) => { + if (mixed) return { x: 0, y: 0, w: image.width, h: image.height }; + const current = image.width / image.height; + const w = current > aspect ? Math.round(image.height * aspect) : image.width; + const h = current > aspect ? image.height : Math.round(image.width / aspect); + return { x: Math.floor((image.width - w) / 2), y: Math.floor((image.height - h) / 2), w, h }; + }); + const size = gifSize(rgbs[0].width, rgbs[0].height, edgeOf(s)); + const built = assemble(rgbs.length, s, size, (panel) => ({ image: rgbs[panel - 1], frame: frames[panel - 1], mode: mixed ? 'fit' : 'crop' })); + return { framing: { mode: mixed ? 'fit' : 'crop', width: size.width, height: size.height, frames }, ...built }; +} + +// 排节奏、出帧、编码、核对,再把各格按第一次出场的顺序排成帧条(标格子编号;补的过渡格跟在前一格后面,标「格子号+」)。 +function assemble(count, spec, size, source, tweens) { + const timeline = panelTimeline(count, spec); + const frames = renderTimeline(source, timeline.steps, size, tweens || new Map()); + const { gif, verify } = encodeFrames(frames, { width: size.width, height: size.height, delays: timeline.steps.map((step) => step.ms), loop: timeline.loop }); + const shown = timeline.order.filter((panel, i) => timeline.order.indexOf(panel) === i); + const firstFrame = new Map(); + timeline.steps.forEach((step, i) => { + const name = step.kind === 'key' ? 'k' + step.panel : step.kind === 'tween' ? 't' + step.key : ''; + if (name && !firstFrame.has(name)) firstFrame.set(name, { frame: frames[i], step }); + }); + const entries = []; + for (const panel of shown) { + entries.push({ image: firstFrame.get('k' + panel).frame, label: String(panel) }); + const tween = [...firstFrame.values()].find((item) => item.step.kind === 'tween' && item.step.panel === panel); + if (tween) entries.push({ image: tween.frame, label: panel + '+' }); + } + const strip = contactSheet( + entries, + // 每行最多 6 格,行与行尽量一样长:8 格排成 4+4,不排成 6+2。 + { perRow: Math.ceil(entries.length / Math.ceil(entries.length / 6)), width: 1200, fill: 246, cellAspect: size.width / size.height }, + ); + return { timeline, size, gif, verify, strip, shown }; +} + +// ------------------------------------------------------------------ 程序转圈 + +// 一张图 → 绕圆心转的 GIF(见 motion.mjs)。spec:{ spin:{ center, radius, steps, frames, direction, refine }, frameMs, loop, transition, edge }。 +// 一格一格跑的(steps)默认每格 80 毫秒、两格之间叠一帧渐变(亮点从一个点淡到下一个点);连续转的默认每帧 50 毫秒、直接切换。 +// 每帧转的角度是 360°/帧数,最后一帧再转一格正好是第一帧:首尾天然接得上。 +export function buildSpinGif(image, spec) { + const s = spec || {}; + const spin = normalizeSpin(s.spin || {}); + const rgb = toRgb(image); + const where = locateSpin(rgb, spin); + const size = gifSize(rgb.width, rgb.height, edgeOf(s)); + // 在 GIF 两倍大的图上转,再缩回 GIF 尺寸:转的时候取样的锯齿,缩小以后看不出来。 + const scale = Math.min(1, (2 * Math.max(size.width, size.height)) / Math.max(rgb.width, rgb.height)); + const work = scale < 1 ? resizeArea(rgb, Math.max(1, Math.round(rgb.width * scale)), Math.max(1, Math.round(rgb.height * scale))) : rgb; + const k = work.width / rgb.width; + const count = spin.steps || spin.frames; + const sign = spin.direction === 'ccw' ? -1 : 1; + const keys = []; + for (let i = 0; i < count; i += 1) { + const turned = i ? spinFrame(work, where.cx * k, where.cy * k, where.radius * k, (sign * 2 * Math.PI * i) / count) : work; + keys.push(resizeArea(turned, size.width, size.height)); + } + const loop = s.loop !== false; + const frameMs = clampSpinMs(s.frameMs) || (spin.steps ? 80 : 50); + const fade = s.transition === 'fade' || (!s.transition && spin.steps > 0); + const frames = []; + const delays = []; + for (let i = 0; i < count; i += 1) { + const next = i + 1 < count ? keys[i + 1] : loop ? keys[0] : null; + // GIF 的时长以 10 毫秒为单位:渐变帧和定格帧都取 10 的倍数、至少 20 毫秒,一格的时长才和写的一样。 + const blends = frameMs >= 120 ? 2 : 1; + const part = Math.round(frameMs / (blends + 1) / 10) * 10; + if (!fade || !next || part < 20 || frameMs - blends * part < 20) { + frames.push(keys[i]); + delays.push(frameMs); + continue; + } + frames.push(keys[i]); + delays.push(frameMs - blends * part); + for (let b = 1; b <= blends; b += 1) { + frames.push(blend(keys[i], next, b / (blends + 1))); + delays.push(part); + } + } + const { gif, verify } = encodeFrames(frames, { width: size.width, height: size.height, delays, loop }); + // 帧条最多放 12 格:一格一格跑的放前 12 格,连续转的均匀取 12 帧。 + const picks = []; + const shown = Math.min(12, count); + for (let i = 0; i < shown; i += 1) picks.push(Math.round((i * count) / shown)); + const strip = contactSheet( + picks.map((index) => ({ image: keys[index], label: String(index + 1) })), + { perRow: Math.ceil(picks.length / Math.ceil(picks.length / 6)), width: 1200, fill: 246, cellAspect: size.width / size.height }, + ); + return { spin, where, size, gif, verify, strip, count, frameMs, fade, loop }; +} + +// 转圈的每帧可以很短:连续转每帧 40 毫秒(25 帧每秒)才顺。GIF 的时长以 10 毫秒为单位,短于 20 毫秒的帧浏览器会按 100 毫秒播。 +export function clampSpinMs(value) { + const n = Number(value); + if (!Number.isFinite(n) || n <= 0) return 0; + return Math.round(Math.max(20, Math.min(2000, n))); +} + +// ------------------------------------------------------------------ 编码与核对 + +// frames:同样大小的 RGB 帧;delays:每帧毫秒数。stabilize 给 0 时逐像素精确(程序画的帧),不给用 RHYTHM.stabilize。 +export function encodeFrames(input, options) { + const opts = options || {}; + const width = opts.width || input[0].width; + const height = opts.height || input[0].height; + // 尺寸不对的帧先缩好:核对要拿写进去的那一版比,不能拿缩之前的。 + const frames = input.map((frame) => fitFrame(frame, width, height)); + const gifOptions = { width, height, delays: opts.delays, loop: opts.loop !== false, matchTone: false, tween: 0, stabilize: Number.isFinite(opts.stabilize) ? opts.stabilize : RHYTHM.stabilize }; + if (opts.dither) gifOptions.dither = opts.dither; + if (Number.isFinite(opts.colors)) gifOptions.colors = Math.max(2, Math.min(255, Math.round(opts.colors))); + const gif = makeGif(frames, gifOptions); + const verify = verifyGif(gif.buffer, { frames, delays: opts.delays, loop: opts.loop !== false, changed: gif.changed, width, height }); + return { gif, verify, frames }; +} + +// 从写出去的字节读回来核对:尺寸、帧数、每帧时长、循环,以及每一帧的画面和要写的画面差多少。 +// 和上一帧一模一样的帧 gif.mjs 会并进上一帧的时长里(changed 为 0),核对时同样合并。 +export function verifyGif(buffer, expected) { + const problems = []; + let back; + try { + back = decodeGif(buffer); + } catch (error) { + return { ok: false, frames: 0, width: 0, height: 0, durationMs: 0, loop: false, maxMeanDiff: 0, outlierShare: 0, problems: ['读不回来:' + (error && error.message ? error.message : error)] }; + } + const want = []; + expected.frames.forEach((frame, i) => { + const ms = Math.max(20, Math.round((expected.delays && expected.delays[i]) || 0)); + if (i > 0 && expected.changed && expected.changed[i] === 0) { + want[want.length - 1].ms += ms; + return; + } + want.push({ frame, ms }); + }); + if (back.width !== expected.width || back.height !== expected.height) problems.push('尺寸 ' + back.width + '×' + back.height + ',应为 ' + expected.width + '×' + expected.height); + if (back.frames.length !== want.length) problems.push('读回 ' + back.frames.length + ' 帧,应为 ' + want.length + ' 帧'); + const loop = back.loop === 0; + if (expected.loop !== loop) problems.push(expected.loop ? '没写循环播放' : '写成了循环播放'); + let maxMeanDiff = 0; + let outlierShare = 0; + const badDelays = []; + const badFrames = []; + const n = Math.min(back.frames.length, want.length); + for (let i = 0; i < n; i += 1) { + const cs = Math.max(2, Math.round(want[i].ms / 10)); + if (back.frames[i].delayMs !== cs * 10) badDelays.push(i + 1); + const diff = frameDiff(back.frames[i].image, want[i].frame); + maxMeanDiff = Math.max(maxMeanDiff, diff.mean); + outlierShare = Math.max(outlierShare, diff.outliers); + if (diff.mean > VERIFY.meanDiff || diff.outliers > VERIFY.outlierShare) badFrames.push(i + 1); + } + if (badDelays.length) problems.push('第 ' + badDelays.join('、') + ' 帧的时长和要写的不一样'); + if (badFrames.length) problems.push('第 ' + badFrames.join('、') + ' 帧读回来的画面和要写的差太多'); + return { + ok: problems.length === 0, + frames: back.frames.length, + width: back.width, + height: back.height, + durationMs: back.frames.reduce((sum, frame) => sum + frame.delayMs, 0), + loop, + maxMeanDiff: Math.round(maxMeanDiff * 100) / 100, + outlierShare: Math.round(outlierShare * 10000) / 10000, + problems, + }; +} + +function frameDiff(a, b) { + const n = Math.min(a.width * a.height, b.width * b.height); + let sum = 0; + let outliers = 0; + for (let p = 0; p < n; p += 1) { + const s = p * 3; + const dr = Math.abs(a.data[s] - b.data[s]); + const dg = Math.abs(a.data[s + 1] - b.data[s + 1]); + const db = Math.abs(a.data[s + 2] - b.data[s + 2]); + sum += dr + dg + db; + if (Math.max(dr, dg, db) > VERIFY.outlierDiff) outliers += 1; + } + return { mean: n ? sum / (n * 3) : 0, outliers: n ? outliers / n : 0 }; +} + +// 看一个现成的 GIF:帧数、时长、循环,相邻两帧变了多少,以及循环时最后一帧接回第一帧那一步(接缝)。 +// 接缝比中间最大的一步还大不少,播到那里会跳一下;首尾两帧完全一样,接缝处等于多停了一帧。 +// 「步子多大」在缩成 64 像素宽、再模糊两档的灰度图上量平均差。只数变了多少像素不行:小物体挪一步和从画面一头 +// 跳回另一头,变的都是两个整圆;糊开以后两团叠在一起,挪得越远差得越多。 +export function inspectGif(buffer) { + const back = decodeGif(buffer); + const images = back.frames.map((frame) => frame.image); + const steps = []; + for (let i = 0; i + 1 < images.length; i += 1) steps.push(round3(changeShare(images[i], images[i + 1]))); + const looped = back.loop !== null && images.length > 1; + const last = images[images.length - 1]; + let seam = null; + if (looped) { + const base = images.map((image) => grayThumb(image, 64)); + const scales = [0.02, 0.15].map((sigma) => seamAt(base.map((thumb) => blurThumb(thumb, sigma)), sigma)); + seam = { + change: round3(changeShare(last, images[0])), + motion: round2(scales[1].seam), + widestStep: round2(scales[1].widest), + identicalEnds: Buffer.compare(last.data, images[0].data) === 0, + jump: scales.some((item) => item.seam > Math.max(0.3, item.widest * 1.3)), + }; + } + return { + width: back.width, + height: back.height, + frames: images.length, + delays: back.frames.map((frame) => frame.delayMs), + durationMs: back.frames.reduce((sum, frame) => sum + frame.delayMs, 0), + loop: back.loop, + steps, + seam, + images, + }; +} + +function seamAt(thumbs) { + let widest = 0; + for (let i = 0; i + 1 < thumbs.length; i += 1) widest = Math.max(widest, meanDiff(thumbs[i].data, thumbs[i + 1].data)); + return { seam: meanDiff(thumbs[thumbs.length - 1].data, thumbs[0].data), widest }; +} + +function grayThumb(image, width) { + const w = Math.max(1, Math.min(width, image.width)); + const h = Math.max(1, Math.round((w * image.height) / image.width)); + const small = resizeArea(image, w, h).data; + const data = new Float32Array(w * h); + for (let p = 0; p < data.length; p += 1) data[p] = 0.299 * small[p * 3] + 0.587 * small[p * 3 + 1] + 0.114 * small[p * 3 + 2]; + return { width: w, height: h, data }; +} + +// 三遍方框模糊近似高斯,sigma 按宽度的比例给。 +function blurThumb(thumb, sigma) { + const r = Math.max(1, Math.round((sigma * thumb.width) / 1.7)); + let data = thumb.data; + for (let pass = 0; pass < 3; pass += 1) data = boxPass(boxPass(data, thumb.width, thumb.height, r, true), thumb.width, thumb.height, r, false); + return { width: thumb.width, height: thumb.height, data }; +} + +function boxPass(src, w, h, r, horizontal) { + const out = new Float32Array(src.length); + const lines = horizontal ? h : w; + const length = horizontal ? w : h; + const at = (line, i) => (horizontal ? line * w + i : i * w + line); + for (let line = 0; line < lines; line += 1) { + for (let i = 0; i < length; i += 1) { + let sum = 0; + for (let k = -r; k <= r; k += 1) sum += src[at(line, Math.max(0, Math.min(length - 1, i + k)))]; + out[at(line, i)] = sum / (2 * r + 1); + } + } + return out; +} + +function meanDiff(a, b) { + let sum = 0; + for (let i = 0; i < a.length; i += 1) sum += Math.abs(a[i] - b[i]); + return a.length ? sum / a.length : 0; +} + +// 帧拼图:entries 是 [{ image, label }],按 perRow 一行排几张,每张左上角标编号。 +export function contactSheet(entries, options) { + const opts = options || {}; + const first = entries[0] && entries[0].image; + return renderGridSheet(entries, { perRow: opts.perRow || 4, width: opts.width || 1024, fill: Number.isFinite(opts.fill) ? opts.fill : 246, cellAspect: opts.cellAspect || (first ? first.width / first.height : 1) }); +} + +function range(from, to) { + const out = []; + for (let i = from; i <= to; i += 1) out.push(i); + return out; +} + +function median(values) { + const sorted = values.slice().sort((a, b) => a - b); + const mid = sorted.length >> 1; + return sorted.length % 2 ? sorted[mid] : (sorted[mid - 1] + sorted[mid]) / 2; +} + +function round2(value) { + return Math.round(value * 100) / 100; +} + +function round3(value) { + return Math.round(value * 1000) / 1000; +} diff --git a/plugins/liddalidd/h3-image-executor/miniapp/node/lib/h3.mjs b/plugins/liddalidd/h3-image-executor/miniapp/node/lib/h3.mjs index 02b35efc..08d55e30 100644 --- a/plugins/liddalidd/h3-image-executor/miniapp/node/lib/h3.mjs +++ b/plugins/liddalidd/h3-image-executor/miniapp/node/lib/h3.mjs @@ -20,6 +20,44 @@ const TIMEOUT_MS = 600000; // 单张生成图的字节上限。4K PNG 一般在几十 MB 以内,留足余量。 const MAX_IMAGE_BYTES = 100 * 1024 * 1024; +// 服务端的临时故障:网关 502/503/504、限流 429、500,以及连不上、连接被重置。这些等一等再试有意义; +// 400/401/403/404 是请求本身的问题,重试多少次都一样。 +// 生图、上传、下载各自在这里退避重试两次,间隔逐次拉长,给网关恢复的时间。重试完还失败, +// 抛出的错误带 transient = true,上层据此决定「先交付手上已有的图」,而不是把整个会话判死 +// (真机 20261112-23b706 / 463fcd:网关一次 502 就让两个会话各丢掉 8 张、4 张已经生成好的图)。 +// delays 导出成可改的对象,测试里改小,不用真等。 +export const RETRY = { delays: [4000, 12000] }; +const TRANSIENT_STATUS = new Set([429, 500, 502, 503, 504]); +// 上传接口 base_resp 里表示临时故障的状态码:1001 超时、1002 / 1039 限流、1013 服务内部错误。 +const TRANSIENT_BASE_CODES = new Set([1001, 1002, 1013, 1039]); + +function sleep(ms) { + return new Promise((resolve) => setTimeout(resolve, ms)); +} + +// 连不上、连接被重置、DNS 一时解析不了。等满 10 分钟的超时不在此列:再等一轮不划算。 +function isNetworkError(error) { + if (!error || error.name === 'TimeoutError' || error.name === 'AbortError') return false; + const cause = error.cause || {}; + const code = String(cause.code || error.code || ''); + return /^(ECONNRESET|ECONNREFUSED|ETIMEDOUT|EPIPE|ENOTFOUND|EAI_AGAIN|EHOSTUNREACH|ENETUNREACH|UND_ERR_SOCKET|UND_ERR_CONNECT_TIMEOUT|UND_ERR_CLOSED)$/.test(code) || + /fetch failed|socket hang up|other side closed/i.test(String(error.message || '')); +} + +function markTransient(error, transient) { + if (transient) error.transient = true; + return error; +} + +// 网关回的错误页常常是一整段 HTML(真机:502 Bad Gateway…), +// 原样塞进时间线既难看又看不出要点。取 title,没有就去掉标签。 +function briefBody(text) { + const raw = String(text || ''); + const title = /([^<]*)<\/title>/i.exec(raw); + const plain = title ? title[1] : raw.replace(/<[^>]*>/g, ' '); + return plain.replace(/\s+/g, ' ').trim().slice(0, 200); +} + let cached = null; // 页面里保存的配置。放在模块作用域,由 server 启动时从 dataDir 读进来、 // 保存时更新。Key 只在 Node 进程里流转,绝不回传给页面。 @@ -132,27 +170,45 @@ function applySize(payload, options) { async function post(config, path, payload, label) { if (!config.apiKey) throw new Error('missing_api_key'); if (!config.baseUrl) throw new Error('missing_base_url'); - const response = await fetch(config.baseUrl + path, { - method: 'POST', - headers: { ...authHeaders(config), 'content-type': 'application/json' }, - body: JSON.stringify(payload), - signal: AbortSignal.timeout(TIMEOUT_MS), - }); - const text = await response.text(); - let body = null; - try { - body = JSON.parse(text); - } catch { - body = null; - } - if (response.status !== 200) { - const message = (body && body.error && body.error.message) || text.slice(0, 300); - const error = new Error(label + '失败 HTTP ' + response.status + ':' + message); - error.status = response.status; - error.requestId = body && body.request_id; - throw error; + for (let attempt = 0; ; attempt += 1) { + const canRetry = attempt < RETRY.delays.length; + let response; + try { + response = await fetch(config.baseUrl + path, { + method: 'POST', + headers: { ...authHeaders(config), 'content-type': 'application/json' }, + body: JSON.stringify(payload), + signal: AbortSignal.timeout(TIMEOUT_MS), + }); + } catch (error) { + const transient = isNetworkError(error); + if (transient && canRetry) { + await sleep(RETRY.delays[attempt]); + continue; + } + throw markTransient(error, transient); + } + const text = await response.text(); + let body = null; + try { + body = JSON.parse(text); + } catch { + body = null; + } + if (response.status !== 200) { + const transient = TRANSIENT_STATUS.has(response.status); + if (transient && canRetry) { + await sleep(RETRY.delays[attempt]); + continue; + } + const message = (body && body.error && body.error.message) || briefBody(text); + const error = new Error(label + '失败 HTTP ' + response.status + ':' + message); + error.status = response.status; + error.requestId = body && body.request_id; + throw markTransient(error, transient); + } + return normalize(body); } - return normalize(body); } function normalize(body) { @@ -181,32 +237,64 @@ export async function uploadReference(config, filePath) { if (!config.apiKey) throw new Error('missing_api_key'); if (!config.baseUrl) throw new Error('missing_base_url'); const buffer = await readFile(filePath); - const form = new FormData(); - form.append('purpose', 'image_generation_input'); - form.append('file', new Blob([buffer]), basename(filePath)); - const response = await fetch(config.baseUrl + '/v1/files/upload', { - method: 'POST', - headers: authHeaders(config), - body: form, - signal: AbortSignal.timeout(TIMEOUT_MS), - }); - const body = await response.json().catch(() => null); - const baseResp = (body && body.base_resp) || {}; - if (response.status !== 200 || baseResp.status_code !== 0) { - throw new Error('参考图上传失败:' + (baseResp.status_msg || 'HTTP ' + response.status)); + for (let attempt = 0; ; attempt += 1) { + const canRetry = attempt < RETRY.delays.length; + const form = new FormData(); + form.append('purpose', 'image_generation_input'); + form.append('file', new Blob([buffer]), basename(filePath)); + let response; + try { + response = await fetch(config.baseUrl + '/v1/files/upload', { + method: 'POST', + headers: authHeaders(config), + body: form, + signal: AbortSignal.timeout(TIMEOUT_MS), + }); + } catch (error) { + const transient = isNetworkError(error); + if (transient && canRetry) { + await sleep(RETRY.delays[attempt]); + continue; + } + throw markTransient(new Error('参考图上传失败:' + String(error && error.message ? error.message : error), { cause: error }), transient); + } + const body = await response.json().catch(() => null); + const baseResp = (body && body.base_resp) || {}; + if (response.status === 200 && baseResp.status_code === 0 && body.file && body.file.file_id) { + return 'mm_file://' + body.file.file_id; + } + const transient = response.status === 200 + ? TRANSIENT_BASE_CODES.has(Number(baseResp.status_code)) + : TRANSIENT_STATUS.has(response.status); + if (transient && canRetry) { + await sleep(RETRY.delays[attempt]); + continue; + } + const error = new Error('参考图上传失败:' + (baseResp.status_msg || 'HTTP ' + response.status)); + error.status = response.status; + throw markTransient(error, transient); } - return 'mm_file://' + body.file.file_id; } export async function download(url, targetPath) { // 成图地址来自接口返回。接口本身可能是内网地址(放行),但云元数据段必须拦, // 而且每一跳重定向都要重新拦一遍——fetchImage 负责这两件事,并给响应体封顶。 + // 图已经生成好了(已经计费),下载失败不该让整轮重画:连接类错误和服务端 5xx 退避重试两次。 let buffer; - try { - buffer = await fetchImage(url, { maxBytes: MAX_IMAGE_BYTES, timeoutMs: 180000 }); - } catch (error) { - const message = String(error && error.message ? error.message : error); - throw new Error(error && error.blocked ? '图片' + message : '图片下载失败 ' + message); + for (let attempt = 0; ; attempt += 1) { + try { + buffer = await fetchImage(url, { maxBytes: MAX_IMAGE_BYTES, timeoutMs: 180000 }); + break; + } catch (error) { + const message = String(error && error.message ? error.message : error); + const status = /^HTTP (\d{3})/.exec(message); + const transient = !(error && error.blocked) && (isNetworkError(error) || Boolean(status && TRANSIENT_STATUS.has(Number(status[1])))); + if (transient && attempt < RETRY.delays.length) { + await sleep(RETRY.delays[attempt]); + continue; + } + throw markTransient(new Error(error && error.blocked ? '图片' + message : '图片下载失败 ' + message), transient); + } } // targetPath 可省略:调用方要先看字节才知道该用什么扩展名(接口回的不一定是 PNG)。 if (targetPath) await writeFile(targetPath, buffer); diff --git a/plugins/liddalidd/h3-image-executor/miniapp/node/lib/image.mjs b/plugins/liddalidd/h3-image-executor/miniapp/node/lib/image.mjs index f326c9de..19d989b9 100644 --- a/plugins/liddalidd/h3-image-executor/miniapp/node/lib/image.mjs +++ b/plugins/liddalidd/h3-image-executor/miniapp/node/lib/image.mjs @@ -1,11 +1,13 @@ // 图片格式分发:harness 只认"给我一个 buffer,还我 {width,height,channels,data}"。 -// PNG 走 png.mjs,JPEG 走 jpeg.mjs;WebP/HEIC 明确不支持,调用方按需跳过。 +// PNG 走 png.mjs,JPEG 走 jpeg.mjs,GIF 取第一帧(动图由 gif.mjs 合成);WebP/HEIC 明确不支持,调用方按需跳过。 import { decodePng, readPngHeader } from './png.mjs'; -import { decodeJpeg, isJpeg } from './jpeg.mjs'; +import { decodeJpeg, isJpeg, jpegUnsupportedReason } from './jpeg.mjs'; +import { decodeGif, isGif } from './gif.mjs'; export function sniffFormat(buffer) { if (buffer.length > 8 && buffer[0] === 0x89 && buffer.subarray(1, 4).toString('ascii') === 'PNG') return 'png'; if (isJpeg(buffer)) return 'jpeg'; + if (isGif(buffer)) return 'gif'; if (buffer.length > 12 && buffer.subarray(0, 4).toString('ascii') === 'RIFF' && buffer.subarray(8, 12).toString('ascii') === 'WEBP') return 'webp'; if (buffer.length > 12 && buffer.subarray(4, 8).toString('ascii') === 'ftyp') return 'heif'; return 'unknown'; @@ -18,6 +20,7 @@ export function sniffFormat(buffer) { export function extensionFor(buffer) { const format = sniffFormat(buffer); if (format === 'jpeg') return '.jpg'; + if (format === 'gif') return '.gif'; if (format === 'webp') return '.webp'; if (format === 'heif') return '.heic'; return '.png'; @@ -27,6 +30,7 @@ export function decodeImage(buffer) { const format = sniffFormat(buffer); if (format === 'png') return decodePng(buffer); if (format === 'jpeg') return decodeJpeg(buffer); + if (format === 'gif') return decodeGif(buffer).frames[0].image; throw new Error('unsupported_image_format:' + format); } @@ -48,7 +52,10 @@ export function undecodableReason(buffer) { if (![0, 2, 4, 6].includes(header.colorType)) return '颜色类型 ' + header.colorType + ' 不支持'; return ''; } - if (format === 'jpeg') return imageSize(buffer) ? '' : 'JPEG 头损坏'; + // JPEG 要看帧类型,不能只看有没有尺寸:以前只要读得到尺寸就放行,渐进式 JPEG 于是被当成 + // 「能用」缓存下来,到拼判定图时才解不开,被悄悄跳过(真机 20261112-8a442a)。 + // 现在渐进式能解了,剩下解不了的(算术编码、CMYK 等)在这里就如实拒掉。 + if (format === 'jpeg') return jpegUnsupportedReason(buffer); return '格式不支持(' + format + ')'; } diff --git a/plugins/liddalidd/h3-image-executor/miniapp/node/lib/jpeg.mjs b/plugins/liddalidd/h3-image-executor/miniapp/node/lib/jpeg.mjs index 83213717..3bfcd674 100644 --- a/plugins/liddalidd/h3-image-executor/miniapp/node/lib/jpeg.mjs +++ b/plugins/liddalidd/h3-image-executor/miniapp/node/lib/jpeg.mjs @@ -1,7 +1,15 @@ -// 纯 Node 实现的 baseline JPEG 解码器(不支持 progressive / arithmetic coding)。 +// 纯 Node 实现的 JPEG 解码器:Huffman 编码的 baseline / extended(SOF0、SOF1)和 progressive(SOF2)。 // 存在的理由:参考图(真人照片、车型图、用户上传图)绝大多数是 JPEG, // 没有它评审就看不到参考图,一致性判断只能靠猜。 -// 支持 8bit、1 或 3 分量、4:4:4 / 4:2:2 / 4:2:0 / 4:1:1 抽样、restart interval。 +// 支持 8bit、1 或 3 分量(YCbCr,或 Adobe / 分量 ID 标明的 RGB)、任意抽样比、restart interval、 +// 交织与非交织扫描。 +// 不支持:算术编码(SOF9–SOF15)、无损(SOF3)、分层(SOF5–SOF7)、12bit、CMYK / YCCK(4 分量)。 +// 这些在网上的图片里很少见;调用方应先用 jpegUnsupportedReason 把它们如实拒掉,而不是解到一半才失败。 +// +// 渐进式为什么必须支持:网上相当一部分照片是渐进式 JPEG(图库、新闻站、CDN 压缩过的图)。 +// 以前解不开它们,参考图检索拿回来的候选在拼判定图时被悄悄跳过,判定节点根本看不到, +// 时间线上却照样标「不符合要求」(真机 20261112-8a442a:吉祥物 9 张候选判定节点只看到 2 张, +// 白底官方全身插画就是渐进式 JPEG)。 const ZIGZAG = [ 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5, @@ -21,16 +29,72 @@ const COS = (() => { return table; })(); +// IDCT 的临时缓冲。单线程,复用一份即可,省掉每个块一次分配。 +const IDCT_TMP = new Float32Array(64); + export function isJpeg(buffer) { return buffer.length > 3 && buffer[0] === 0xff && buffer[1] === 0xd8 && buffer[2] === 0xff; } +function isSofMarker(marker) { + return marker >= 0xc0 && marker <= 0xcf && marker !== 0xc4 && marker !== 0xc8 && marker !== 0xcc; +} + +// 只读帧头(不解码):编码方式、位深、尺寸、分量数。读不到返回 null。 +export function readJpegFrame(buffer) { + if (!isJpeg(buffer)) return null; + let offset = 2; + while (offset < buffer.length - 9) { + if (buffer[offset] !== 0xff) { + offset += 1; + continue; + } + const marker = buffer[offset + 1]; + if (marker === 0xff) { + offset += 1; + continue; + } + if (marker === 0xd8 || marker === 0x01 || (marker >= 0xd0 && marker <= 0xd7)) { + offset += 2; + continue; + } + if (marker === 0xd9 || marker === 0xda) return null; + if (isSofMarker(marker)) { + return { + sof: marker - 0xc0, + precision: buffer[offset + 4], + height: buffer.readUInt16BE(offset + 5), + width: buffer.readUInt16BE(offset + 7), + components: buffer[offset + 9], + }; + } + offset += 2 + buffer.readUInt16BE(offset + 2); + } + return null; +} + +// 解不了的原因(中文,给时间线看);能解返回空字符串。只看帧头,不做实际解码。 +export function jpegUnsupportedReason(buffer) { + const frame = readJpegFrame(buffer); + if (!frame) return 'JPEG 头损坏'; + if (frame.sof === 3 || frame.sof === 7 || frame.sof === 11 || frame.sof === 15) return '无损 JPEG 不支持'; + if (frame.sof >= 9) return '算术编码 JPEG 不支持'; + if (frame.sof >= 5) return '分层 JPEG 不支持'; + if (frame.precision !== 8) return frame.precision + 'bit JPEG 不支持'; + if (frame.components === 4) return 'CMYK JPEG 不支持'; + if (frame.components !== 1 && frame.components !== 3) return frame.components + ' 分量 JPEG 不支持'; + if (!frame.width || !frame.height) return 'JPEG 尺寸缺失'; + return ''; +} + export function decodeJpeg(buffer) { const quantTables = []; const huffDC = []; const huffAC = []; let frame = null; let restartInterval = 0; + let adobeTransform = -1; + let scans = 0; let offset = 2; while (offset < buffer.length) { @@ -39,9 +103,15 @@ export function decodeJpeg(buffer) { continue; } const marker = buffer[offset + 1]; + // 0xFF 填充字节:只跳一个,下一个 0xFF 才是真正的标记前缀。 + if (marker === 0xff) { + offset += 1; + continue; + } offset += 2; if (marker === 0xd8 || marker === 0x01 || (marker >= 0xd0 && marker <= 0xd7)) continue; if (marker === 0xd9) break; + if (offset + 2 > buffer.length) break; const length = buffer.readUInt16BE(offset); const segment = buffer.subarray(offset + 2, offset + length); @@ -81,39 +151,123 @@ export function decodeJpeg(buffer) { } } else if (marker === 0xdd) { restartInterval = segment.readUInt16BE(0); - } else if (marker >= 0xc0 && marker <= 0xcf && marker !== 0xc4 && marker !== 0xc8 && marker !== 0xcc) { - if (marker !== 0xc0 && marker !== 0xc1) throw new Error('jpeg_progressive_unsupported'); - const height = segment.readUInt16BE(1); - const width = segment.readUInt16BE(3); - const count = segment[5]; - const components = []; - for (let i = 0; i < count; i += 1) { - const base = 6 + i * 3; - components.push({ - id: segment[base], - h: segment[base + 1] >> 4 || 1, - v: segment[base + 1] & 15 || 1, - quantId: segment[base + 2], - }); - } - frame = { width, height, components }; + } else if (marker === 0xee) { + // Adobe APP14:transform 0 表示 3 分量存的是 RGB 而不是 YCbCr。 + if (segment.length >= 12 && segment.subarray(0, 5).toString('latin1') === 'Adobe') adobeTransform = segment[11]; + } else if (isSofMarker(marker)) { + frame = readFrame(marker, segment); } else if (marker === 0xda) { if (!frame) throw new Error('jpeg_no_frame'); - const count = segment[0]; - for (let i = 0; i < count; i += 1) { - const id = segment[1 + i * 2]; - const tables = segment[2 + i * 2]; - const component = frame.components.find((item) => item.id === id); - if (component) { - component.dcId = tables >> 4; - component.acId = tables & 15; - } + const scan = readScan(segment, frame); + const dataStart = offset + length; + // 快速路径:顺序编码、第一段扫描就包含全部分量(绝大多数 baseline 图都是这样,也是唯一一段扫描)。 + // 直接解成像素,不留系数,内存和以前一样。单分量帧只有在抽样因子是 1x1 时才能走这条路: + // 单分量扫描按分量自己的块网格排列,不按 MCU。 + if (!frame.progressive && scans === 0 && scan.components.length === frame.components.length && + (scan.components.length > 1 || (frame.components[0].h === 1 && frame.components[0].v === 1))) { + latchQuant(frame, scan, quantTables); + attachTables(scan, huffDC, huffAC, false); + decodeInterleavedToPixels(buffer, dataStart, frame, scan, restartInterval); + return finishFrame(frame, rgbFrame(frame, adobeTransform)); } - return decodeScan(buffer, offset + length, frame, quantTables, huffDC, huffAC, restartInterval); + latchQuant(frame, scan, quantTables); + attachTables(scan, huffDC, huffAC, frame.progressive); + ensureCoefficients(frame); + const end = decodeScanToCoefficients(buffer, dataStart, frame, scan, restartInterval); + scans += 1; + offset = nextMarker(buffer, end); + continue; } offset += length; } - throw new Error('jpeg_no_scan'); + if (!frame) throw new Error('jpeg_no_frame'); + if (!scans) throw new Error('jpeg_no_scan'); + coefficientsToPixels(frame); + return finishFrame(frame, rgbFrame(frame, adobeTransform)); +} + +function readFrame(marker, segment) { + if (marker !== 0xc0 && marker !== 0xc1 && marker !== 0xc2) throw new Error('jpeg_unsupported_sof' + (marker - 0xc0)); + const precision = segment[0]; + if (precision !== 8) throw new Error('jpeg_unsupported_precision_' + precision); + const height = segment.readUInt16BE(1); + const width = segment.readUInt16BE(3); + const count = segment[5]; + if (count === 4) throw new Error('jpeg_cmyk_unsupported'); + if (count !== 1 && count !== 3) throw new Error('jpeg_unsupported_components_' + count); + if (!width || !height) throw new Error('jpeg_bad_size'); + const components = []; + for (let i = 0; i < count; i += 1) { + const base = 6 + i * 3; + components.push({ + id: segment[base], + h: segment[base + 1] >> 4 || 1, + v: segment[base + 1] & 15 || 1, + quantId: segment[base + 2], + }); + } + const hmax = Math.max(...components.map((c) => c.h)); + const vmax = Math.max(...components.map((c) => c.v)); + const mcusPerLine = Math.ceil(width / (8 * hmax)); + const mcusPerColumn = Math.ceil(height / (8 * vmax)); + for (const c of components) { + // 分量自己的采样尺寸和块网格(非交织扫描按它排列);存储按 MCU 补齐(交织扫描会覆盖补齐的块)。 + c.sampleWidth = Math.ceil((width * c.h) / hmax); + c.sampleHeight = Math.ceil((height * c.v) / vmax); + c.blocksPerLine = Math.ceil(c.sampleWidth / 8); + c.blocksPerColumn = Math.ceil(c.sampleHeight / 8); + c.strideBlocks = mcusPerLine * c.h; + c.rowBlocks = mcusPerColumn * c.v; + c.pred = 0; + c.quant = null; + } + return { width, height, components, hmax, vmax, mcusPerLine, mcusPerColumn, progressive: marker === 0xc2 }; +} + +function readScan(segment, frame) { + const count = segment[0]; + const components = []; + for (let i = 0; i < count; i += 1) { + const id = segment[1 + i * 2]; + const tables = segment[2 + i * 2]; + const component = frame.components.find((item) => item.id === id); + if (!component) throw new Error('jpeg_bad_scan_component'); + components.push({ component, dcId: tables >> 4, acId: tables & 15 }); + } + const p = 1 + count * 2; + return { + components, + ss: segment[p], + se: segment[p + 1], + ah: segment[p + 2] >> 4, + al: segment[p + 2] & 15, + }; +} + +// 量化表在分量第一次出现的扫描时锁定(libjpeg 同样处理),之后再出现的 DQT 不影响它。 +function latchQuant(frame, scan, quantTables) { + for (const { component } of scan.components) { + if (component.quant) continue; + const table = quantTables[component.quantId]; + component.quant = table ? Int32Array.from(table) : new Int32Array(64).fill(1); + } +} + +function attachTables(scan, huffDC, huffAC, progressive) { + for (const entry of scan.components) { + entry.dc = huffDC[entry.dcId]; + entry.ac = huffAC[entry.acId]; + // 渐进式:DC 扫描只用 DC 表,AC 扫描只用 AC 表,DC 细化扫描两张都不用。 + const needDc = !progressive || (scan.ss === 0 && scan.ah === 0); + const needAc = !progressive || scan.ss > 0; + if ((needDc && !entry.dc) || (needAc && !entry.ac)) throw new Error('jpeg_missing_huffman'); + } +} + +function ensureCoefficients(frame) { + for (const c of frame.components) { + if (!c.coefficients) c.coefficients = new Int16Array(c.strideBlocks * c.rowBlocks * 64); + } } function buildHuffman(counts, values) { @@ -134,54 +288,274 @@ function buildHuffman(counts, values) { return { mincode, maxcode, valptr, values }; } -function decodeScan(buffer, start, frame, quantTables, huffDC, huffAC, restartInterval) { - const { width, height, components } = frame; - const hmax = Math.max(...components.map((c) => c.h)); - const vmax = Math.max(...components.map((c) => c.v)); - const mcusPerLine = Math.ceil(width / (8 * hmax)); - const mcusPerColumn = Math.ceil(height / (8 * vmax)); +// ---------------------------------------------------------------- 快速路径:顺序编码、单段交织扫描 - for (const component of components) { - component.blocksPerLine = mcusPerLine * component.h; - component.blocksPerColumn = mcusPerColumn * component.v; - component.pixels = new Uint8Array(component.blocksPerLine * 8 * component.blocksPerColumn * 8); - component.pred = 0; - component.quant = quantTables[component.quantId] || new Int32Array(64).fill(1); - component.dc = huffDC[component.dcId]; - component.ac = huffAC[component.acId]; - if (!component.dc || !component.ac) throw new Error('jpeg_missing_huffman'); +function decodeInterleavedToPixels(buffer, start, frame, scan, restartInterval) { + const { mcusPerLine, mcusPerColumn } = frame; + for (const c of frame.components) { + c.pixels = new Uint8Array(c.strideBlocks * 8 * c.rowBlocks * 8); + c.pred = 0; } - const reader = new BitReader(buffer, start); const block = new Int32Array(64); const pixels = new Uint8Array(64); let mcu = 0; - for (let mcuRow = 0; mcuRow < mcusPerColumn; mcuRow += 1) { for (let mcuCol = 0; mcuCol < mcusPerLine; mcuCol += 1) { if (restartInterval && mcu && mcu % restartInterval === 0) { reader.restart(); - for (const component of components) component.pred = 0; + for (const c of frame.components) c.pred = 0; } mcu += 1; - for (const component of components) { - for (let v = 0; v < component.v; v += 1) { - for (let h = 0; h < component.h; h += 1) { - const row = mcuRow * component.v + v; - const col = mcuCol * component.h + h; - decodeBlock(reader, component, block); + for (const entry of scan.components) { + const c = entry.component; + for (let v = 0; v < c.v; v += 1) { + for (let h = 0; h < c.h; h += 1) { + const row = mcuRow * c.v + v; + const col = mcuCol * c.h + h; + decodeBlockDequantized(reader, entry, block); idct(block, pixels); - const stride = component.blocksPerLine * 8; + const stride = c.strideBlocks * 8; for (let y = 0; y < 8; y += 1) { const target = (row * 8 + y) * stride + col * 8; - for (let x = 0; x < 8; x += 1) component.pixels[target + x] = pixels[y * 8 + x]; + for (let x = 0; x < 8; x += 1) c.pixels[target + x] = pixels[y * 8 + x]; } } } } } } +} + +function decodeBlockDequantized(reader, entry, block) { + const c = entry.component; + block.fill(0); + const t = decodeHuffman(reader, entry.dc); + const diff = t ? extend(reader.receive(t), t) : 0; + c.pred += diff; + block[0] = c.pred * c.quant[0]; + let k = 1; + while (k < 64) { + const rs = decodeHuffman(reader, entry.ac); + const size = rs & 15; + const run = rs >> 4; + if (size === 0) { + if (run < 15) break; + k += 16; + continue; + } + k += run; + if (k > 63) break; + const z = ZIGZAG[k]; + block[z] = extend(reader.receive(size), size) * c.quant[z]; + k += 1; + } +} + +// ---------------------------------------------------------------- 系数路径:渐进式、非交织扫描 +// +// 每段扫描只贡献一部分系数(某几个频率、某几位精度),全部扫描读完才能做 IDCT。 +// 系数按块存成 Int16(未反量化,自然顺序),全部扫描结束后统一反量化 + IDCT。 +// 渐进式解码的四种扫描(DC 首次 / DC 细化 / AC 首次 / AC 细化)按 ITU-T T.81 附录 G 实现, +// AC 细化与 libjpeg 的 decode_mcu_AC_refine 逐步对应。 + +function decodeScanToCoefficients(buffer, start, frame, scan, restartInterval) { + const reader = new BitReader(buffer, start); + const state = { eobrun: 0 }; + const { ss, se, ah, al } = scan; + let decode; + if (!frame.progressive) decode = decodeSequential; + else if (ss === 0) decode = ah === 0 ? decodeDcFirst : decodeDcRefine; + else decode = ah === 0 ? decodeAcFirst : decodeAcRefine; + for (const entry of scan.components) entry.component.pred = 0; + + const single = scan.components.length === 1; + const units = single + ? scan.components[0].component.blocksPerLine * scan.components[0].component.blocksPerColumn + : frame.mcusPerLine * frame.mcusPerColumn; + for (let n = 0; n < units; n += 1) { + if (restartInterval && n && n % restartInterval === 0) { + reader.restart(); + for (const entry of scan.components) entry.component.pred = 0; + state.eobrun = 0; + } + if (single) { + const entry = scan.components[0]; + const c = entry.component; + const row = Math.floor(n / c.blocksPerLine); + const col = n % c.blocksPerLine; + decode(reader, entry, (row * c.strideBlocks + col) * 64, state, ss, se, al); + continue; + } + const mcuRow = Math.floor(n / frame.mcusPerLine); + const mcuCol = n % frame.mcusPerLine; + for (const entry of scan.components) { + const c = entry.component; + for (let v = 0; v < c.v; v += 1) { + for (let h = 0; h < c.h; h += 1) { + const row = mcuRow * c.v + v; + const col = mcuCol * c.h + h; + decode(reader, entry, (row * c.strideBlocks + col) * 64, state, ss, se, al); + } + } + } + } + return reader.pos; +} + +function decodeSequential(reader, entry, offset) { + const c = entry.component; + const coef = c.coefficients; + const t = decodeHuffman(reader, entry.dc); + const diff = t ? extend(reader.receive(t), t) : 0; + c.pred += diff; + coef[offset] = c.pred; + let k = 1; + while (k < 64) { + const rs = decodeHuffman(reader, entry.ac); + const size = rs & 15; + const run = rs >> 4; + if (size === 0) { + if (run < 15) break; + k += 16; + continue; + } + k += run; + if (k > 63) break; + coef[offset + ZIGZAG[k]] = extend(reader.receive(size), size); + k += 1; + } +} + +function decodeDcFirst(reader, entry, offset, state, ss, se, al) { + const c = entry.component; + const t = decodeHuffman(reader, entry.dc); + const diff = t ? extend(reader.receive(t), t) : 0; + c.pred += diff; + c.coefficients[offset] = c.pred * (1 << al); +} + +function decodeDcRefine(reader, entry, offset, state, ss, se, al) { + if (reader.bit()) entry.component.coefficients[offset] |= 1 << al; +} + +function decodeAcFirst(reader, entry, offset, state, ss, se, al) { + if (state.eobrun > 0) { + state.eobrun -= 1; + return; + } + const coef = entry.component.coefficients; + let k = ss; + while (k <= se) { + const rs = decodeHuffman(reader, entry.ac); + const size = rs & 15; + const run = rs >> 4; + if (size === 0) { + if (run < 15) { + state.eobrun = (1 << run) - 1; + if (run) state.eobrun += reader.receive(run); + break; + } + k += 16; + continue; + } + k += run; + if (k > 63) break; + coef[offset + ZIGZAG[k]] = extend(reader.receive(size), size) * (1 << al); + k += 1; + } +} + +function decodeAcRefine(reader, entry, offset, state, ss, se, al) { + const coef = entry.component.coefficients; + const p1 = 1 << al; + const m1 = -1 << al; + let k = ss; + if (state.eobrun <= 0) { + for (; k <= se; k += 1) { + const rs = decodeHuffman(reader, entry.ac); + let run = rs >> 4; + let value = rs & 15; + if (value) { + // 细化扫描里新出现的系数只可能是 ±1(乘上本段的位权)。 + value = reader.bit() ? p1 : m1; + } else if (run !== 15) { + state.eobrun = 1 << run; + if (run) state.eobrun += reader.receive(run); + break; + } + // 越过已经非零的系数(给它们补一位精度)和 run 个仍为零的系数。 + do { + const z = offset + ZIGZAG[k]; + if (coef[z] !== 0) { + if (reader.bit() && (coef[z] & p1) === 0) coef[z] += coef[z] >= 0 ? p1 : m1; + } else { + run -= 1; + if (run < 0) break; + } + k += 1; + } while (k <= se); + if (value && k <= se) coef[offset + ZIGZAG[k]] = value; + } + } + if (state.eobrun > 0) { + // EOB 区段里的块:只给已经非零的系数补位。 + for (; k <= se; k += 1) { + const z = offset + ZIGZAG[k]; + if (coef[z] !== 0 && reader.bit() && (coef[z] & p1) === 0) coef[z] += coef[z] >= 0 ? p1 : m1; + } + state.eobrun -= 1; + } +} + +function coefficientsToPixels(frame) { + const block = new Int32Array(64); + const pixels = new Uint8Array(64); + for (const c of frame.components) { + const coef = c.coefficients || new Int16Array(c.strideBlocks * c.rowBlocks * 64); + const quant = c.quant || new Int32Array(64).fill(1); + const stride = c.strideBlocks * 8; + c.pixels = new Uint8Array(stride * c.rowBlocks * 8); + for (let row = 0; row < c.rowBlocks; row += 1) { + for (let col = 0; col < c.strideBlocks; col += 1) { + const offset = (row * c.strideBlocks + col) * 64; + for (let i = 0; i < 64; i += 1) block[i] = coef[offset + i] * quant[i]; + idct(block, pixels); + for (let y = 0; y < 8; y += 1) { + const target = (row * 8 + y) * stride + col * 8; + for (let x = 0; x < 8; x += 1) c.pixels[target + x] = pixels[y * 8 + x]; + } + } + } + c.coefficients = null; + } +} +// 扫描数据结束后找下一个标记:跳过字节填充(FF 00)、RST 标记和填充用的 FF。 +function nextMarker(buffer, from) { + let p = Math.max(0, from); + while (p < buffer.length - 1) { + if (buffer[p] === 0xff) { + const next = buffer[p + 1]; + if (next !== 0x00 && next !== 0xff && !(next >= 0xd0 && next <= 0xd7)) return p; + } + p += 1; + } + return buffer.length; +} + +// ---------------------------------------------------------------- 输出 + +// 3 分量什么时候按 RGB 处理:Adobe APP14 写明 transform=0,或者分量 ID 就是 'R' 'G' 'B'。 +function rgbFrame(frame, adobeTransform) { + if (frame.components.length !== 3) return false; + if (adobeTransform === 0) return true; + const ids = frame.components.map((c) => c.id); + return ids[0] === 0x52 && ids[1] === 0x47 && ids[2] === 0x42; +} + +function finishFrame(frame, isRgb) { + const { width, height, components, hmax, vmax } = frame; const planes = components.map((component) => upsample(component, width, height, hmax, vmax)); const out = Buffer.alloc(width * height * 3); if (components.length === 1) { @@ -193,11 +567,19 @@ function decodeScan(buffer, start, frame, quantTables, huffDC, huffAC, restartIn } return { width, height, channels: 3, data: out }; } - const [Y, Cb, Cr] = planes; + const [A, B, C] = planes; + if (isRgb) { + for (let p = 0; p < width * height; p += 1) { + out[p * 3] = A[p]; + out[p * 3 + 1] = B[p]; + out[p * 3 + 2] = C[p]; + } + return { width, height, channels: 3, data: out }; + } for (let p = 0; p < width * height; p += 1) { - const y = Y[p]; - const cb = Cb[p] - 128; - const cr = Cr[p] - 128; + const y = A[p]; + const cb = B[p] - 128; + const cr = C[p] - 128; const di = p * 3; out[di] = clamp(y + 1.402 * cr); out[di + 1] = clamp(y - 0.344136 * cb - 0.714136 * cr); @@ -206,20 +588,21 @@ function decodeScan(buffer, start, frame, quantTables, huffDC, huffAC, restartIn return { width, height, channels: 3, data: out }; } -// 把某个分量的 MCU 块平面上采样到整图分辨率。 +// 把某个分量的块平面上采样到整图分辨率。 // 采样中心映射 src = (dst + 0.5) * (h / hmax) - 0.5,与 libjpeg 的 fancy(三角 3:1)上采样等价。 +// 取样只到分量的实际采样范围为止:按 MCU 补齐出来的块在非交织扫描里没有编码,不能拿来插值。 function upsample(component, width, height, hmax, vmax) { - const stride = component.blocksPerLine * 8; - const rows = component.blocksPerColumn * 8; + const stride = component.strideBlocks * 8; const src = component.pixels; const out = new Uint8Array(width * height); + const maxX = Math.min(stride, component.sampleWidth) - 1; + const maxY = Math.min(component.rowBlocks * 8, component.sampleHeight) - 1; const rx = component.h / hmax; const ry = component.v / vmax; - const identity = rx === 1 && ry === 1; - if (identity) { + if (rx === 1 && ry === 1) { for (let y = 0; y < height; y += 1) { - const sy = Math.min(rows - 1, y); - for (let x = 0; x < width; x += 1) out[y * width + x] = src[sy * stride + Math.min(stride - 1, x)]; + const row = Math.min(maxY, y) * stride; + for (let x = 0; x < width; x += 1) out[y * width + x] = src[row + Math.min(maxX, x)]; } return out; } @@ -227,14 +610,14 @@ function upsample(component, width, height, hmax, vmax) { const fy = (y + 0.5) * ry - 0.5; const y0 = Math.floor(fy); const wy = fy - y0; - const ya = Math.min(rows - 1, Math.max(0, y0)) * stride; - const yb = Math.min(rows - 1, Math.max(0, y0 + 1)) * stride; + const ya = Math.min(maxY, Math.max(0, y0)) * stride; + const yb = Math.min(maxY, Math.max(0, y0 + 1)) * stride; for (let x = 0; x < width; x += 1) { const fx = (x + 0.5) * rx - 0.5; const x0 = Math.floor(fx); const wx = fx - x0; - const xa = Math.min(stride - 1, Math.max(0, x0)); - const xb = Math.min(stride - 1, Math.max(0, x0 + 1)); + const xa = Math.min(maxX, Math.max(0, x0)); + const xb = Math.min(maxX, Math.max(0, x0 + 1)); const top = src[ya + xa] + (src[ya + xb] - src[ya + xa]) * wx; const bottom = src[yb + xa] + (src[yb + xb] - src[yb + xa]) * wx; out[y * width + x] = Math.round(top + (bottom - top) * wy); @@ -243,6 +626,8 @@ function upsample(component, width, height, hmax, vmax) { return out; } +// ---------------------------------------------------------------- 熵解码基础 + class BitReader { constructor(data, pos) { this.data = data; @@ -254,12 +639,13 @@ class BitReader { bit() { if (this.count === 0) { if (this.pos >= this.data.length) return 0; - let byte = this.data[this.pos]; + const byte = this.data[this.pos]; this.pos += 1; if (byte === 0xff) { const next = this.data[this.pos]; if (next === 0x00) this.pos += 1; else { + // 碰到标记:停在标记前,之后的读取都补 0。 this.pos -= 1; return 0; } @@ -302,36 +688,12 @@ function decodeHuffman(reader, table) { return 0; } -function decodeBlock(reader, component, block) { - block.fill(0); - const t = decodeHuffman(reader, component.dc); - const diff = t ? extend(reader.receive(t), t) : 0; - component.pred += diff; - block[0] = component.pred * component.quant[0]; - let k = 1; - while (k < 64) { - const rs = decodeHuffman(reader, component.ac); - const size = rs & 15; - const run = rs >> 4; - if (size === 0) { - if (run < 15) break; - k += 16; - continue; - } - k += run; - if (k > 63) break; - const z = ZIGZAG[k]; - block[z] = extend(reader.receive(size), size) * component.quant[z]; - k += 1; - } -} - function extend(value, length) { return value < 1 << (length - 1) ? value - (1 << length) + 1 : value; } function idct(block, out) { - const tmp = new Float32Array(64); + const tmp = IDCT_TMP; for (let u = 0; u < 8; u += 1) { for (let x = 0; x < 8; x += 1) { let sum = 0; diff --git a/plugins/liddalidd/h3-image-executor/miniapp/node/lib/motion.mjs b/plugins/liddalidd/h3-image-executor/miniapp/node/lib/motion.mjs new file mode 100644 index 00000000..624fd162 --- /dev/null +++ b/plugins/liddalidd/h3-image-executor/miniapp/node/lib/motion.mjs @@ -0,0 +1,214 @@ +// 程序动效:生图模型只画一张静态图,动起来的部分由程序按周期算出每一帧(jungle-adventure、david-voxel-loop 两个例子的做法)。 +// 现在有绕中心转(spin):加载圈、转动的图标、齿轮、风车、唱片。这类动作生图模型画不准:真机 20261119-e645c0 +// 的十二点加载圈,一帧一帧改出来的十二帧有一半亮点落错格子,扔掉以后亮点一下跳一格、一下跳三格。 +// 按圆心把画面转过去,每一帧都准,最后一帧再转一格正好接回第一帧。 +// +// 转的只是圆心周围那一块圆盘,圆盘外面不动;圆盘边缘留一圈渐变,边上是均匀的背景,转了也看不出接缝。 +// 一格一格跑的(steps:圆环上等距的 N 个点或 N 根辐条)每帧转 360/N 度:点和点正好重合,暗的点看起来不动,只有亮点在跑。 +// 这要求圆心找得准:先按给的圆心,在附近找「转一格之后边缘和自己最重合」的位置(refineCenter)。 +import { resizeArea, toRgb } from './gif.mjs'; + +export const SPIN = { + minSteps: 3, + maxSteps: 60, + minFrames: 8, + maxFrames: 60, + frames: 24, + // 转的圆盘:给的半径和程序量出来的内容半径取大的,再放大一点;最外面一圈渐变过渡,最大到画面短边的一半。 + margin: 1.06, + feather: 0.08, + maxRadius: 0.5, + // 找圆心:在长边 384 的图上,给定圆心附近 ±6% 里先粗后细地找。 + refineEdge: 384, + refineRange: 0.06, +}; + +// 规划、编辑节点写的 spin 参数 → 规格。center 是画面宽高的比例,radius 是画面短边的比例。 +export function normalizeSpin(raw) { + const value = raw && typeof raw === 'object' ? raw : {}; + const num = (v, lo, hi, fallback) => { + const n = Number(v); + return Number.isFinite(n) ? Math.max(lo, Math.min(hi, n)) : fallback; + }; + const center = Array.isArray(value.center) && value.center.length === 2 ? value.center.map((v) => num(v, 0.05, 0.95, 0.5)) : [0.5, 0.5]; + const stepsRaw = Math.round(Number(value.steps)); + const steps = Number.isFinite(stepsRaw) && stepsRaw >= SPIN.minSteps ? Math.min(SPIN.maxSteps, stepsRaw) : 0; + const framesRaw = Math.round(Number(value.frames)); + return { + center, + radius: num(value.radius, 0.05, SPIN.maxRadius, 0.35), + steps, + frames: steps ? steps : Number.isFinite(framesRaw) && framesRaw >= SPIN.minFrames ? Math.min(SPIN.maxFrames, framesRaw) : SPIN.frames, + direction: value.direction === 'ccw' ? 'ccw' : 'cw', + refine: value.refine !== false, + }; +} + +// 找圆心和要转的半径(都是 rgb 的像素)。steps ≥ 3 时按「转一格之后和自己最重合」细找圆心:比的是梯度(边缘), +// 均匀的背景怎么转都一样,比的就是点、辐条这些边缘;亮点挪了一格的那几处不管圆心在哪都对不上,是个常数,不影响找最小。 +// 连续转的(steps 为 0)没有这种对称,用给的圆心。 +export function locateSpin(image, spin) { + const rgb = toRgb(image); + const short = Math.min(rgb.width, rgb.height); + let cx = spin.center[0] * rgb.width; + let cy = spin.center[1] * rgb.height; + let refined = { moved: 0, score: 0, base: 0 }; + const scale = Math.min(1, SPIN.refineEdge / Math.max(rgb.width, rgb.height)); + const w = Math.max(32, Math.round(rgb.width * scale)); + const h = Math.max(32, Math.round(rgb.height * scale)); + const grad = gradient(resizeArea(rgb, w, h)); + const r0 = Math.max(4, spin.radius * Math.min(w, h)); + if (spin.refine && spin.steps >= SPIN.minSteps) { + const theta = (2 * Math.PI) / spin.steps; + const cost = (x, y) => rotationCost(grad, w, h, x, y, r0, theta); + let bx = cx * scale; + let by = cy * scale; + const base = cost(bx, by); + let best = base; + const range = SPIN.refineRange * Math.min(w, h); + for (const [span, step] of [[range, Math.max(1, range / 6)], [Math.max(1, range / 6), Math.max(0.25, range / 36)]]) { + const ox = bx; + const oy = by; + for (let dy = -span; dy <= span + 1e-9; dy += step) { + for (let dx = -span; dx <= span + 1e-9; dx += step) { + const c = cost(ox + dx, oy + dy); + if (c < best) { + best = c; + bx = ox + dx; + by = oy + dy; + } + } + } + } + refined = { moved: Math.hypot(bx / scale - cx, by / scale - cy), score: best, base }; + cx = bx / scale; + cy = by / scale; + } + // 内容半径:从圆心往外,梯度(边缘)的累计量到 98% 的半径。给的半径小了(切到点上),半截点不转、半截转,一眼就看得出。 + const contentR = contentRadius(grad, w, h, cx * scale, cy * scale, SPIN.maxRadius * Math.min(w, h)) / scale; + const radius = Math.min(SPIN.maxRadius * short, Math.max(spin.radius * short, contentR) * SPIN.margin); + return { cx, cy, radius, contentRadius: contentR, refined }; +} + +function rotationCost(grad, w, h, cx, cy, r, theta) { + const cos = Math.cos(theta); + const sin = Math.sin(theta); + const r2 = r * r; + let sum = 0; + let n = 0; + const x0 = Math.max(1, Math.floor(cx - r)); + const x1 = Math.min(w - 2, Math.ceil(cx + r)); + const y0 = Math.max(1, Math.floor(cy - r)); + const y1 = Math.min(h - 2, Math.ceil(cy + r)); + for (let y = y0; y <= y1; y += 1) { + for (let x = x0; x <= x1; x += 1) { + const dx = x - cx; + const dy = y - cy; + if (dx * dx + dy * dy > r2) continue; + // 这一点转一格之后落在哪 + const sx = cx + dx * cos - dy * sin; + const sy = cy + dx * sin + dy * cos; + if (sx < 0 || sy < 0 || sx > w - 1 || sy > h - 1) continue; + sum += Math.abs(grad[y * w + x] - bilinear1(grad, w, h, sx, sy)); + n += 1; + } + } + return n ? sum / n : Infinity; +} + +function contentRadius(grad, w, h, cx, cy, maxR) { + const bins = Math.max(8, Math.ceil(maxR)); + const energy = new Float64Array(bins + 1); + for (let y = 0; y < h; y += 1) { + for (let x = 0; x < w; x += 1) { + const r = Math.hypot(x - cx, y - cy); + if (r > maxR) continue; + energy[Math.min(bins, Math.floor(r))] += grad[y * w + x]; + } + } + // 均匀背景本身也有一点点梯度(渐变、噪点):按每一圈的中位强度扣掉一份底子再累计。 + const perRing = []; + for (let r = 0; r <= bins; r += 1) perRing.push(energy[r] / Math.max(1, 2 * Math.PI * Math.max(0.5, r))); + const floor = perRing.slice().sort((a, b) => a - b)[Math.floor(perRing.length * 0.5)]; + let total = 0; + const net = perRing.map((v, r) => Math.max(0, v - floor * 1.5) * 2 * Math.PI * Math.max(0.5, r)); + for (const v of net) total += v; + if (!total) return 0; + let acc = 0; + for (let r = 0; r <= bins; r += 1) { + acc += net[r]; + if (acc >= total * 0.98) return r + 1; + } + return bins; +} + +// 把圆盘转 angle(弧度,顺时针为正:图像坐标 y 朝下),圆盘外不动,边缘渐变。返回新的 RGB 图。 +export function spinFrame(rgb, cx, cy, radius, angle) { + const { width: w, height: h, data } = rgb; + const out = Buffer.from(data); + const feather = Math.max(1, radius * SPIN.feather); + const inner = radius - feather; + const cos = Math.cos(-angle); + const sin = Math.sin(-angle); + const x0 = Math.max(0, Math.floor(cx - radius)); + const x1 = Math.min(w - 1, Math.ceil(cx + radius)); + const y0 = Math.max(0, Math.floor(cy - radius)); + const y1 = Math.min(h - 1, Math.ceil(cy + radius)); + for (let y = y0; y <= y1; y += 1) { + for (let x = x0; x <= x1; x += 1) { + const dx = x + 0.5 - cx; + const dy = y + 0.5 - cy; + const r = Math.hypot(dx, dy); + if (r >= radius) continue; + const k = r <= inner ? 1 : 0.5 + 0.5 * Math.cos((Math.PI * (r - inner)) / feather); + // 反着找:这一点转之前在哪 + const sx = cx + dx * cos - dy * sin - 0.5; + const sy = cy + dx * sin + dy * cos - 0.5; + const o = (y * w + x) * 3; + for (let c = 0; c < 3; c += 1) { + const v = bilinear3(data, w, h, sx, sy, c); + out[o + c] = Math.round(data[o + c] + (v - data[o + c]) * k); + } + } + } + return { width: w, height: h, channels: 3, data: out }; +} + +function bilinear1(g, w, h, x, y) { + const x0 = Math.max(0, Math.min(w - 2, Math.floor(x))); + const y0 = Math.max(0, Math.min(h - 2, Math.floor(y))); + const fx = Math.max(0, Math.min(1, x - x0)); + const fy = Math.max(0, Math.min(1, y - y0)); + const p = y0 * w + x0; + const top = g[p] + (g[p + 1] - g[p]) * fx; + const bottom = g[p + w] + (g[p + w + 1] - g[p + w]) * fx; + return top + (bottom - top) * fy; +} + +function bilinear3(data, w, h, x, y, c) { + const xx = Math.max(0, Math.min(w - 1.001, x)); + const yy = Math.max(0, Math.min(h - 1.001, y)); + const x0 = Math.floor(xx); + const y0 = Math.floor(yy); + const fx = xx - x0; + const fy = yy - y0; + const p = (y0 * w + x0) * 3 + c; + const q = p + w * 3; + const top = data[p] + (data[p + 3] - data[p]) * fx; + const bottom = data[q] + (data[q + 3] - data[q]) * fx; + return top + (bottom - top) * fy; +} + +function gradient(img) { + const { width: w, height: h, data } = img; + const lum = new Float32Array(w * h); + for (let p = 0; p < w * h; p += 1) lum[p] = 0.299 * data[p * 3] + 0.587 * data[p * 3 + 1] + 0.114 * data[p * 3 + 2]; + const out = new Float32Array(w * h); + for (let y = 1; y < h - 1; y += 1) { + for (let x = 1; x < w - 1; x += 1) { + const p = y * w + x; + out[p] = Math.abs(lum[p + 1] - lum[p - 1]) + Math.abs(lum[p + w] - lum[p - w]); + } + } + return out; +} diff --git a/plugins/liddalidd/h3-image-executor/miniapp/node/lib/motiongif.mjs b/plugins/liddalidd/h3-image-executor/miniapp/node/lib/motiongif.mjs new file mode 100644 index 00000000..2f309755 --- /dev/null +++ b/plugins/liddalidd/h3-image-executor/miniapp/node/lib/motiongif.mjs @@ -0,0 +1,258 @@ +// 程序转圈的 GIF(面板里):一张画好、评审过的图,由程序绕圆心转出各帧拼成 GIF,不再一帧一帧调生图。 +// 加载圈、转动的图标、齿轮、风车这类「绕一个中心转」的动作,生图模型一帧一帧改画不准:真机 20261119-e645c0 的 +// 十二点加载圈,十二帧有一半亮点落错格子,扔掉之后亮点一下跳一格、一下跳三格。转法见 motion.mjs、gifbuild.buildSpinGif。 +// 两个入口:规划时就要(规划在这张图上写 motion_gif),图照常生成、评审,交付时转;追问「让它转起来」(编辑节点写 +// animation.motion)。之后调快慢、换方向只重新转;改画面照常改图,交付时按新图重新转。 +import { access, readFile, writeFile } from 'node:fs/promises'; +import { join } from 'node:path'; +import { decodeImage } from './image.mjs'; +import { encodePng } from './png.mjs'; +import { buildSpinGif, clampSpinMs } from './gifbuild.mjs'; +import { normalizeSpin } from './motion.mjs'; +import { detail, kv } from './detail.mjs'; + +// rev:设定的版本,转法改了就加 1,拼过的 GIF 缓存跟着失效(见 composeMotionGif)。 +export const MOTION_GIF = { rev: 1, minEdge: 240, maxEdge: 1024, edge: 512 }; + +export const MOTION_TYPES = ['spin']; + +// 规划写的 motion_gif、编辑节点写的 animation.motion、落盘的设定 → 规格。不是认得的动法返回 null。 +export function normalizeMotionGif(raw) { + if (!raw || typeof raw !== 'object') return null; + const type = String(raw.type || 'spin').trim().toLowerCase(); + if (!MOTION_TYPES.includes(type)) return null; + const transition = raw.transition === 'fade' ? 'fade' : raw.transition === 'cut' ? 'cut' : ''; + return { + rev: MOTION_GIF.rev, + type, + spin: normalizeSpin(raw.spin && typeof raw.spin === 'object' ? raw.spin : raw), + frameMs: clampSpinMs(raw.frame_ms ?? raw.frameMs), + loop: raw.loop !== false, + transition, + edge: clampEdge(raw.edge), + }; +} + +function clampEdge(value) { + const n = Number(value); + if (!Number.isFinite(n) || n <= 0) return MOTION_GIF.edge; + return Math.round(Math.max(MOTION_GIF.minEdge, Math.min(MOTION_GIF.maxEdge, n))); +} + +// 追问里的调整并进规格。ops 是 normalizeAnimationOps 的结果:frameMs、loop、edge、transition 在外层, +// 转法本身(方向、格数、圆心、半径)在 ops.motion 里,只写了的才改。和现在一样的不算改动。 +export function mergeMotionOps(current, ops) { + const spec = current ? { ...normalizeMotionGif({ ...current, spin: current.spin }) } : normalizeMotionGif(ops && ops.motion ? ops.motion : {}); + const changes = []; + if (!spec || !ops) return { spec, changes }; + const motion = ops.motion && typeof ops.motion === 'object' ? ops.motion : {}; + const spin = { ...spec.spin }; + if (current) { + if ((motion.direction === 'cw' || motion.direction === 'ccw') && motion.direction !== spin.direction) { + spin.direction = motion.direction; + changes.push(motion.direction === 'ccw' ? '改成逆时针转' : '改成顺时针转'); + } + if (motion.steps !== undefined) { + const next = normalizeSpin({ ...spin, steps: motion.steps, frames: motion.frames ?? spin.frames }); + if (next.steps !== spin.steps) { + spin.steps = next.steps; + spin.frames = next.frames; + changes.push(next.steps ? '一圈 ' + next.steps + ' 格,一格一格跳' : '连续转'); + } + } + if (motion.frames !== undefined && !spin.steps) { + const next = normalizeSpin({ ...spin, frames: motion.frames }); + if (next.frames !== spin.frames) { + spin.frames = next.frames; + changes.push('一圈 ' + next.frames + ' 帧'); + } + } + if (Array.isArray(motion.center) || motion.radius !== undefined) { + const next = normalizeSpin({ ...spin, center: motion.center || spin.center, radius: motion.radius ?? spin.radius }); + if (next.center.join(',') !== spin.center.join(',') || next.radius !== spin.radius) { + spin.center = next.center; + spin.radius = next.radius; + changes.push('圆心和转的范围按新的位置'); + } + } + } + spec.spin = spin; + // 外层的 frame_ms 经过 normalizeAnimationOps,最短只到 60 毫秒;转圈要更短的就写在 motion 里。 + const frameMs = clampSpinMs(motion.frame_ms ?? motion.frameMs) || ops.frameMs; + if (frameMs && frameMs !== spec.frameMs) { + spec.frameMs = frameMs; + changes.push('每格停 ' + seconds(frameMs)); + } + if (ops.loop !== undefined && ops.loop !== spec.loop) { + spec.loop = ops.loop; + changes.push(ops.loop ? '循环播放' : '只转一圈'); + } + const transition = ops.transition || (motion.transition === 'fade' || motion.transition === 'cut' ? motion.transition : ''); + if (transition && transition !== effectiveTransition(spec)) { + spec.transition = transition; + changes.push(transition === 'fade' ? '两格之间渐变' : '直接切换'); + } + const edge = ops.edge || (motion.edge ? clampEdge(motion.edge) : 0); + if (edge && edge !== spec.edge) { + spec.edge = edge; + changes.push('长边 ' + edge + ' 像素'); + } + return { spec, changes }; +} + +function effectiveTransition(spec) { + return spec.transition || (spec.spin.steps ? 'fade' : 'cut'); +} + +function seconds(ms) { + return Math.round(ms / 10) / 100 + ' 秒'; +} + +async function exists(path) { + try { + await access(path); + return true; + } catch { + return false; + } +} + +const SAFE = /^(?!\.{1,2}$)[A-Za-z0-9._-]+$/; + +// 转出 GIF,结果记在 image.motionGif 上,文件写在 images/<候选号>-spin.gif(帧条 -spin-strip.png)。 +// 同一张图、同样的设定转过就直接用。hooks:queuePixelWork、lastDelivered、describeError。 +export async function composeMotionGif(session, image, deps, hooks) { + const spec = image.spec && image.spec.motionGif; + const source = image.chosen || hooks.lastDelivered(session, image, deps); + if (!spec || !source || !source.path) return null; + const key = source.id + '|' + JSON.stringify(spec); + const prev = image.motionGif; + if (prev && prev.key === key) { + if (prev.state === 'failed') return prev; + if (prev.state === 'done' && (await exists(join(deps.dirs.images, prev.gifFile))) && (await exists(join(deps.dirs.images, prev.stripFile)))) return prev; + } + const started = Date.now(); + const stem = (SAFE.test(String(source.id)) ? source.id : image.id) + '-spin'; + try { + const built = await hooks.queuePixelWork(async () => { + const decoded = decodeImage(await readFile(source.path)); + return { decoded: { width: decoded.width, height: decoded.height }, ...buildSpinGif(decoded, spec) }; + }); + if (!built.verify.ok) throw new Error('GIF 读回来核对没通过:' + built.verify.problems.join(';')); + const gifFile = stem + '.gif'; + const stripFile = stem + '-strip.png'; + await writeFile(join(deps.dirs.images, gifFile), built.gif.buffer); + await writeFile(join(deps.dirs.images, stripFile), encodePng(built.strip, { level: 6 })); + const { width, height } = built.decoded; + const short = Math.min(width, height); + image.motionGif = { + key, + state: 'done', + sourceId: source.id, + error: '', + type: spec.type, + gifFile, + stripFile, + width: built.size.width, + height: built.size.height, + bytes: built.gif.buffer.length, + written: built.gif.frameCount, + frames: built.count, + durationMs: built.verify.durationMs, + keyMs: built.frameMs, + steps: built.spin.steps, + direction: built.spin.direction, + transition: built.fade ? 'fade' : 'cut', + loop: built.loop, + // 程序最后用的圆心(画面比例)、转的半径(短边比例),和规划给的圆心差多少(短边比例) + center: [round3(built.where.cx / width), round3(built.where.cy / height)], + radius: round3(built.where.radius / short), + moved: round3(built.where.refined.moved / short), + verify: built.verify, + at: Date.now(), + }; + deps.emit(session, 'deliver', image.id + ' 程序转出 GIF:' + describeState(image.motionGif), { + durMs: Date.now() - started, + detail: detail('程序转圈 · ' + image.id, [ + kv('转法', describeSpin(image.motionGif)), + kv('圆心', '(' + image.motionGif.center.join(', ') + '),转的范围半径 ' + image.motionGif.radius + '(画面短边的比例)' + + (image.motionGif.moved ? ',比规划给的圆心挪了 ' + image.motionGif.moved : '')), + kv('写出', image.motionGif.written + ' 帧,' + image.motionGif.width + '×' + image.motionGif.height + ',' + Math.round(image.motionGif.bytes / 1024) + ' KB,读回核对通过'), + ]), + }); + } catch (error) { + const reason = hooks.describeError(error); + image.motionGif = { key, state: 'failed', sourceId: source.id, error: reason, at: Date.now() }; + deps.emit(session, 'deliver', image.id + ' 没转成 GIF:' + reason, { level: 'warn', durMs: Date.now() - started }); + } + return image.motionGif; +} + +function round3(value) { + return Math.round(value * 1000) / 1000; +} + +function describeSpin(state) { + return [ + state.direction === 'ccw' ? '逆时针' : '顺时针', + state.steps ? '一圈 ' + state.steps + ' 格、一格一格跳,每格 ' + seconds(state.keyMs) : '连续转,一圈 ' + state.frames + ' 帧、每帧 ' + seconds(state.keyMs), + state.transition === 'fade' ? '两格之间渐变' : '直接切换', + state.loop ? '循环' : '只转一圈', + '一圈 ' + seconds(state.durationMs), + ].join(','); +} + +function describeState(state) { + return describeSpin(state) + ';' + state.written + ' 帧 ' + Math.round(state.bytes / 1024) + ' KB'; +} + +// 交付卡片上的 gif 一段(source 为 motion)。 +export function motionGifItem(state, files) { + return { + source: 'motion', + type: state.type, + file: files.gif, + strip: files.strip, + width: state.width, + height: state.height, + bytes: state.bytes, + frames: state.frames, + written: state.written, + frameMs: state.keyMs, + keyMs: state.keyMs, + durationMs: state.durationMs, + loop: state.loop, + transition: state.transition, + steps: state.steps, + direction: state.direction, + center: state.center, + radius: state.radius, + verify: state.verify, + }; +} + +// 编辑节点 inventory 里这张图的一段说明。 +export function motionGifInventory(image) { + const state = image.motionGif; + if (!image.spec || !image.spec.motionGif) return ''; + if (!state || state.state !== 'done') return ';这张图要由程序转成 GIF,还没转成' + (state && state.error ? '(' + state.error + ')' : ''); + return ';这是程序绕圆心转出来的 GIF:' + describeSpin(state) + ',圆心 (' + state.center.join(', ') + '),长边 ' + image.spec.motionGif.edge + ' 像素'; +} + +export function snapshotMotionGif(state) { + if (!state || typeof state !== 'object') return null; + return JSON.parse(JSON.stringify(state)); +} + +export function restoreMotionGif(raw) { + if (!raw || typeof raw !== 'object' || !['done', 'failed'].includes(raw.state)) return null; + if (raw.state === 'done' && !(SAFE.test(String(raw.gifFile || '')) && SAFE.test(String(raw.stripFile || '')))) return null; + const out = JSON.parse(JSON.stringify(raw)); + out.key = String(raw.key || ''); + out.error = String(raw.error || ''); + if (out.state === 'done') { + for (const field of ['width', 'height', 'bytes', 'written', 'frames', 'durationMs', 'keyMs', 'steps', 'radius', 'moved']) out[field] = Number(raw[field]) || 0; + out.center = Array.isArray(raw.center) && raw.center.length === 2 ? raw.center.map(Number) : [0.5, 0.5]; + } + return out; +} diff --git a/plugins/liddalidd/h3-image-executor/miniapp/node/lib/panelgif.mjs b/plugins/liddalidd/h3-image-executor/miniapp/node/lib/panelgif.mjs new file mode 100644 index 00000000..6fbac600 --- /dev/null +++ b/plugins/liddalidd/h3-image-executor/miniapp/node/lib/panelgif.mjs @@ -0,0 +1,352 @@ +// 组图拼 GIF(面板里):一张已经画好、评审过的分格组图,由程序切格、按节奏拼成 GIF,不再调生图。 +// 两个入口: +// 追问「把这组图变成 GIF」:编辑节点在 animation 里写 from_panels。以前它只能写 start(那是给 GIF 参考图用的), +// 这张图不是 GIF 参考图,系统就当成普通编辑,又照原 prompt 重画了一张静态组图(真机 20261118-db1dd9,多花两次生图、两分多钟)。 +// 规划时就要(「画一组六格表情,做成 GIF」):规划在这张图上写 panel_gif,组图照常生成、评审,交付时拼。 +// 之后调快慢、换顺序、去掉某一格只重新拼;改某一格的画面照常改组图,交付时按新的组图重新拼。 +// 切格、节奏、编码和读回核对都在 panels.mjs、gifbuild.mjs 里,这里管规格、缓存、交付和落盘。 +import { access, readFile, writeFile } from 'node:fs/promises'; +import { join } from 'node:path'; +import { decodeImage } from './image.mjs'; +import { encodePng } from './png.mjs'; +import { toRgb } from './gif.mjs'; +import { RHYTHM, buildPanelGif, clampMs } from './gifbuild.mjs'; +import { loadTweens, prepareGrid, tweenSignature } from './paneltween.mjs'; +import { detail, kv, list } from './detail.mjs'; + +// rev:设定的版本,存在 session.json 里。切格、对齐的做法改了就加 1:拼过的 GIF 按设定缓存(见 composePanelGif), +// 版本一变缓存就失效,下次交付按新的做法重新拼。 +// 3:补过渡格,过渡格锁到它画在的那一格上。 +export const PANEL_GIF = { minEdge: 240, maxEdge: 1024, rev: 3 }; + +// 规划写的 panel_gif、落盘的规格 → 规格。字段名两种写法都认(规划写 frame_ms,落盘是 frameMs)。 +export function normalizePanelGif(raw) { + if (!raw || typeof raw !== 'object') return null; + const grid = (value) => { + const n = Math.round(Number(value)); + return n >= 1 && n <= 8 ? n : 0; + }; + const panels = (value) => (Array.isArray(value) ? value.map((item) => Math.round(Number(item))).filter((item) => item >= 1 && item <= 64) : []); + const count = Math.round(Number(raw.count)); + return { + rev: PANEL_GIF.rev, + rows: grid(raw.rows), + cols: grid(raw.cols), + count: count >= 2 && count <= 64 ? count : 0, + order: panels(raw.order).slice(0, 64), + drop: panels(raw.drop).slice(0, 64), + frameMs: clampMs(raw.frame_ms ?? raw.frameMs), + firstMs: clampMs(raw.first_ms ?? raw.firstMs), + lastMs: clampMs(raw.last_ms ?? raw.lastMs), + hold: (Array.isArray(raw.hold) ? raw.hold : []) + .map((item) => ({ index: Math.round(Number(item && item.index)), ms: clampMs(item && item.ms) })) + .filter((item) => item.index >= 1 && item.ms) + .slice(0, 64), + loop: raw.loop !== false, + pingpong: raw.pingpong === true, + transition: raw.transition === 'fade' ? 'fade' : 'cut', + // 切换时弹一下默认不加:整幅画面会跟着放大再落回,对齐得再准,每次切换也会跳一下(见 gifbuild.RHYTHM.pop)。 + bounce: raw.bounce === true, + edge: clampEdge(raw.edge), + // 相邻两格之间补过渡格(paneltween.mjs):默认补,写 false 不补(漫画换场景、用户说直接切换就行)。 + smooth: raw.smooth !== false, + }; +} + +// 从 session.json 恢复设定。没有 rev 的是第一版存的:那时切换时弹一下默认开着,不是用户要的, +// 每次切换整幅画面都会跳一下(真机 20261119-250f55),恢复时按现在的默认关掉。那一版还会把编辑节点照抄回来的 +// 时长原样写进设定(每格 0.7 秒、第一格 1.05 秒、最后一格 1.4 秒),和默认一样的清回默认,之后调快慢时首尾跟着变。 +export function restorePanelGifSpec(raw) { + const spec = normalizePanelGif(raw); + if (!spec || Number(raw.rev) >= 2) return spec; + spec.bounce = false; + if (spec.frameMs === RHYTHM.frameMs) spec.frameMs = 0; + const frame = spec.frameMs || RHYTHM.frameMs; + if (spec.firstMs === Math.round(frame * RHYTHM.firstScale)) spec.firstMs = 0; + if (spec.lastMs === Math.round(frame * RHYTHM.lastScale)) spec.lastMs = 0; + return spec; +} + +function clampEdge(value) { + const n = Number(value); + if (!Number.isFinite(n) || n <= 0) return RHYTHM.edge; + return Math.round(Math.max(PANEL_GIF.minEdge, Math.min(PANEL_GIF.maxEdge, n))); +} + +// 把追问里的调整并进规格。返回新规格和改了什么(给事件流看)。格子编号都按从上到下、从左到右数。 +// state 是现在这张 GIF 拼出来的样子(image.panelGif),用来认出照抄回来的播放顺序。 +// 编辑节点常把上面列出的现有设定原样抄回来(真机 20261119-250f55 说「重新变成gif」,它把每格 0.7 秒、第一格 1.05 秒、 +// 切换时弹一下全写了一遍):和现在一样的项不算改动,也不写进设定。照抄的「第一格 1.05 秒」一旦写死,之后说「慢一点」, +// 第一格就不跟着变慢了。 +export function mergePanelOps(current, ops, state) { + const spec = { ...(normalizePanelGif(current || {}) || normalizePanelGif({})) }; + const changes = []; + if (!ops) return { spec, changes }; + const frame = spec.frameMs || RHYTHM.frameMs; + const done = state && state.state === 'done'; + // 现在的播放顺序有两种写法会被抄回来:没来回播时的顺序,和清单里写的实际播放顺序(来回播时是展开的 1…6…2)。 + const base = spec.order.length ? spec.order : done ? (state.panels || []).map((panel) => panel.index).filter((index) => !spec.drop.includes(index)) : []; + const before = { + frame, + first: spec.firstMs || Math.round(frame * RHYTHM.firstScale), + last: spec.lastMs || Math.round(frame * RHYTHM.lastScale), + orders: [base.join(','), done ? (state.order || []).join(',') : ''].filter(Boolean), + }; + if ((ops.rows && ops.rows !== spec.rows) || (ops.cols && ops.cols !== spec.cols)) { + if (ops.rows) spec.rows = Math.min(8, ops.rows); + if (ops.cols) spec.cols = Math.min(8, ops.cols); + changes.push('按 ' + (spec.rows || '?') + ' 行 ' + (spec.cols || '?') + ' 列切格'); + } + if (ops.count && ops.count !== spec.count) spec.count = ops.count; + if (ops.frameMs && ops.frameMs !== before.frame) { + spec.frameMs = ops.frameMs; + changes.push('每格停 ' + seconds(ops.frameMs)); + } + if (ops.firstMs && ops.firstMs !== before.first) { + spec.firstMs = ops.firstMs; + changes.push('第一格停 ' + seconds(ops.firstMs)); + } + if (ops.lastMs && ops.lastMs !== before.last) { + spec.lastMs = ops.lastMs; + changes.push('最后一格停 ' + seconds(ops.lastMs)); + } + if (ops.loop !== undefined && ops.loop !== spec.loop) { + spec.loop = ops.loop; + changes.push(ops.loop ? '循环播放' : '只播一遍'); + } + if (ops.pingpong !== undefined && ops.pingpong !== spec.pingpong) { + spec.pingpong = ops.pingpong; + changes.push(ops.pingpong ? '来回播' : '不来回播'); + } + if (ops.transition && (ops.transition === 'fade' ? 'fade' : 'cut') !== spec.transition) { + spec.transition = ops.transition === 'fade' ? 'fade' : 'cut'; + changes.push(spec.transition === 'fade' ? '两格之间渐变' : '两格之间直接切换'); + } + if (ops.bounce !== undefined && ops.bounce !== spec.bounce) { + spec.bounce = ops.bounce; + changes.push(ops.bounce ? '切换时轻微弹一下' : '切换时不弹'); + } + if (ops.smooth !== undefined && ops.smooth !== spec.smooth) { + spec.smooth = ops.smooth; + changes.push(ops.smooth ? '相邻两格之间补过渡格' : '不补过渡格,两格之间直接切换'); + } + if (ops.edge && clampEdge(ops.edge) !== spec.edge) { + spec.edge = clampEdge(ops.edge); + changes.push('长边 ' + spec.edge + ' 像素'); + } + for (const hold of ops.hold) { + if (spec.hold.some((item) => item.index === hold.index && item.ms === hold.ms)) continue; + spec.hold = spec.hold.filter((item) => item.index !== hold.index).concat([{ index: hold.index, ms: hold.ms }]); + changes.push('第 ' + hold.index + ' 格停 ' + seconds(hold.ms)); + } + // order 写的是要留下的全部格子:之前去掉的格子以它为准。 + if (ops.order.length >= 2 && !before.orders.includes(ops.order.join(','))) { + spec.order = ops.order.slice(0, 64); + spec.drop = []; + changes.push('播放顺序改成 ' + spec.order.join('、')); + } + const dropped = ops.drop.filter((index) => !spec.drop.includes(index)); + if (dropped.length) { + spec.drop = spec.drop.concat(dropped); + if (spec.order.length) spec.order = spec.order.filter((index) => !dropped.includes(index)); + changes.push('去掉第 ' + dropped.join('、') + ' 格'); + } + return { spec, changes }; +} + +// 切换时弹一下只在用户自己点名时加:编辑节点、规划会照着现有设定把它抄回来(见 mergePanelOps)。 +const BOUNCE_WORDS = /弹|蹦|放大|缩放|bounce|pop/i; + +export function bounceAsked(words) { + return BOUNCE_WORDS.test(String(words || '')); +} + +function seconds(ms) { + return Math.round(ms / 10) / 100 + ' 秒'; +} + +async function exists(path) { + try { + await access(path); + return true; + } catch { + return false; + } +} + +const SAFE = /^(?!\.{1,2}$)[A-Za-z0-9._-]+$/; + +// 拼 GIF,结果记在 image.panelGif 上,文件写在 images/<候选号>-panels.gif(帧条 -panels-strip.png)。 +// 同一张组图、同样的规格拼过就直接用:每次交付都会走到这里,多图任务里没改的图不用重拼。 +// hooks:queuePixelWork(像素活排队,见 workflow)、lastDelivered、describeError。 +export async function composePanelGif(session, image, deps, hooks) { + const spec = image.spec && image.spec.panelGif; + const source = image.chosen || hooks.lastDelivered(session, image, deps); + if (!spec || !source || !source.path) return null; + // 过渡格换了(补上了、重画了),GIF 也要重拼。 + const key = source.id + '|' + JSON.stringify(spec) + '|' + (spec.smooth === false ? '' : tweenSignature(image, source.id)); + const prev = image.panelGif; + if (prev && prev.key === key) { + if (prev.state === 'failed') return prev; + if (prev.state === 'done' && (await exists(join(deps.dirs.images, prev.gifFile))) && (await exists(join(deps.dirs.images, prev.stripFile)))) return prev; + } + const started = Date.now(); + const stem = (SAFE.test(String(source.id)) ? source.id : image.id) + '-panels'; + try { + const built = await hooks.queuePixelWork(async () => { + const rgb = toRgb(decodeImage(await readFile(source.path))); + const tweens = spec.smooth === false ? new Map() : await loadTweens(image, source, rgb, prepareGrid(rgb, spec), deps); + return buildPanelGif(rgb, spec, { tweens }); + }); + if (!built.verify.ok) throw new Error('GIF 读回来核对没通过:' + built.verify.problems.join(';')); + const gifFile = stem + '.gif'; + const stripFile = stem + '-strip.png'; + await writeFile(join(deps.dirs.images, gifFile), built.gif.buffer); + await writeFile(join(deps.dirs.images, stripFile), encodePng(built.strip, { level: 6 })); + const { detection, framing, timeline } = built; + image.panelGif = { + key, + state: 'done', + sourceId: source.id, + error: '', + gifFile, + stripFile, + width: built.size.width, + height: built.size.height, + bytes: built.gif.buffer.length, + written: built.gif.frameCount, + durationMs: built.verify.durationMs, + keyMs: clampMs(spec.frameMs) || RHYTHM.frameMs, + firstMs: timeline.holds[0], + lastMs: timeline.holds[timeline.turn], + loop: timeline.loop, + pingpong: Boolean(spec.pingpong), + transition: timeline.transition, + bounce: timeline.bounce, + // 用上了几格过渡格、每格停多久 + tweens: timeline.tweens || 0, + tweenMs: timeline.tweens ? timeline.tweenMs : 0, + tweenKeys: timeline.steps.filter((step) => step.kind === 'tween').map((step) => step.key).filter((key, i, all) => all.indexOf(key) === i), + grid: { rows: detection.rows, cols: detection.cols, count: detection.count, method: detection.method }, + panels: detection.cells.map((cell, i) => ({ index: cell.index, row: cell.row, col: cell.col, x: cell.x, y: cell.y, w: cell.w, h: cell.h, dx: framing.frames[i].dx, dy: framing.frames[i].dy })), + framing: { mode: framing.mode, width: framing.width, height: framing.height, aligned: framing.aligned || 0 }, + order: timeline.order, + shown: built.shown, + verify: built.verify, + at: Date.now(), + }; + deps.emit(session, 'deliver', image.id + ' 组图拼成 GIF:' + describeState(image.panelGif), { + durMs: Date.now() - started, + detail: detail('组图拼 GIF · ' + image.id, [ + kv('切格', describeGrid(image.panelGif)), + kv('节奏', describeRhythm(image.panelGif)), + kv('写出', image.panelGif.written + ' 帧,' + image.panelGif.width + '×' + image.panelGif.height + ',' + Math.round(image.panelGif.bytes / 1024) + ' KB,读回核对通过'), + list( + '各格', + image.panelGif.panels.map((panel) => ({ + text: '第 ' + panel.index + ' 格(第 ' + panel.row + ' 行第 ' + panel.col + ' 列)' + (built.shown.includes(panel.index) ? '' : ',没放进 GIF') + + (panel.dx || panel.dy ? ',对齐挪了 (' + panel.dx + ', ' + panel.dy + ') 像素' : ''), + })), + ), + ]), + }); + } catch (error) { + const reason = error && error.message === 'not_grid' + ? '没在图里找到格子之间的分隔线或留白,切不出两格以上' + : error && error.message === 'panel_order_too_short' + ? '按现在的顺序只剩不到 2 格' + : hooks.describeError(error); + image.panelGif = { key, state: 'failed', sourceId: source.id, error: reason, notGrid: Boolean(error && error.message === 'not_grid'), at: Date.now() }; + deps.emit(session, 'deliver', image.id + ' 组图没拼成 GIF:' + reason, { level: 'warn', durMs: Date.now() - started }); + } + return image.panelGif; +} + +function describeGrid(state) { + const how = { line: '按分隔线', gutter: '按留白', free: '按分隔线(格子大小不一)', even: '格子之间没有清楚的分隔,按等分' }[state.grid.method] || ''; + return state.grid.cols + ' 列 ' + state.grid.rows + ' 行共 ' + state.grid.count + ' 格(' + how + '切)' + (state.framing.mode === 'fit' ? ',格子大小不一,缩进同一画幅' : ''); +} + +function describeRhythm(state) { + return [ + '播放顺序 ' + state.order.join('、'), + '每格 ' + seconds(state.keyMs) + ',第一格 ' + seconds(state.firstMs) + ',最后一格 ' + seconds(state.lastMs), + state.pingpong ? '来回播' : '', + state.loop ? '循环' : '只播一遍', + state.tweens ? '补了 ' + (state.tweenKeys || []).length + ' 步过渡格(每格 ' + seconds(state.tweenMs) + ')' : '', + state.transition === 'fade' ? '两格之间渐变' : state.bounce ? '切换时轻微弹一下' : state.tweens ? '' : '直接切换', + '共 ' + seconds(state.durationMs), + ].filter(Boolean).join(','); +} + +function describeState(state) { + return describeGrid(state) + ';' + describeRhythm(state) + ';' + state.written + ' 帧 ' + Math.round(state.bytes / 1024) + ' KB'; +} + +// 交付卡片上的 gif 一段(和动图的 gif 同一套字段,source 为 panels)。files:这一版交付目录里的 GIF 和帧条。 +export function panelGifItem(state, files) { + return { + source: 'panels', + file: files.gif, + strip: files.strip, + width: state.width, + height: state.height, + bytes: state.bytes, + // 放进 GIF 的格子数(用户说「第几格」按 items 里的编号)。 + frames: state.shown.length, + written: state.written, + frameMs: state.keyMs, + keyMs: state.keyMs, + firstMs: state.firstMs, + lastMs: state.lastMs, + durationMs: state.durationMs, + loop: state.loop, + pingpong: state.pingpong, + transition: state.transition, + bounce: state.bounce, + // 补了几步过渡格(每一步一格)、每格停多久;note 是没补成时的原因 + tweens: (state.tweenKeys || []).length, + tweenMs: state.tweenMs || 0, + tweenNote: files.tweenNote || '', + grid: state.grid, + order: state.order, + verify: state.verify, + items: state.panels.map((panel) => ({ index: panel.index, row: panel.row, col: panel.col, inGif: state.shown.includes(panel.index) })), + }; +} + +// 编辑节点 inventory 里这张图的一段说明。 +export function panelGifInventory(image) { + const state = image.panelGif; + if (!image.spec || !image.spec.panelGif) return ''; + if (!state || state.state !== 'done') return ';这张组图要拼成 GIF,还没拼成' + (state && state.error ? '(' + state.error + ')' : ''); + const smoothNote = image.spec.panelGif.smooth === false + ? ',说过不补过渡格' + : state.tweens + ? '' + : ',格与格之间还没补过渡格(用户要更顺时写 {"smooth":true})'; + return ';这是组图拼成的 GIF:' + describeGrid(state) + ',格子编号从上到下、从左到右数;' + describeRhythm(state) + ',长边 ' + image.spec.panelGif.edge + ' 像素' + smoothNote; +} + +// 落盘:文件只存文件名,恢复时按会话目录找。 +export function snapshotPanelGif(state) { + if (!state || typeof state !== 'object') return null; + return JSON.parse(JSON.stringify(state)); +} + +export function restorePanelGif(raw) { + if (!raw || typeof raw !== 'object' || !['done', 'failed'].includes(raw.state)) return null; + if (raw.state === 'done' && !(SAFE.test(String(raw.gifFile || '')) && SAFE.test(String(raw.stripFile || '')))) return null; + const out = JSON.parse(JSON.stringify(raw)); + out.key = String(raw.key || ''); + out.error = String(raw.error || ''); + if (out.state === 'done') { + for (const field of ['width', 'height', 'bytes', 'written', 'durationMs', 'keyMs', 'firstMs', 'lastMs']) out[field] = Number(raw[field]) || 0; + out.order = Array.isArray(raw.order) ? raw.order.map(Number).filter((n) => n >= 1) : []; + out.shown = Array.isArray(raw.shown) ? raw.shown.map(Number).filter((n) => n >= 1) : []; + out.panels = Array.isArray(raw.panels) ? raw.panels : []; + if (!out.grid || typeof out.grid !== 'object') out.grid = { rows: 0, cols: 0, count: out.panels.length, method: '' }; + if (!out.framing || typeof out.framing !== 'object') out.framing = { mode: 'crop', width: 0, height: 0, aligned: 0 }; + } + return out; +} diff --git a/plugins/liddalidd/h3-image-executor/miniapp/node/lib/panels.mjs b/plugins/liddalidd/h3-image-executor/miniapp/node/lib/panels.mjs new file mode 100644 index 00000000..71d3e4b1 --- /dev/null +++ b/plugins/liddalidd/h3-image-executor/miniapp/node/lib/panels.mjs @@ -0,0 +1,779 @@ +// 组图切格:一张图里排好的几格(六格表情、四格漫画、九宫格),找出格子之间的分隔线或留白,切成同样大小的一格一格。 +// 用来把组图拼成 GIF(panelgif.mjs、tools/h3-gif.mjs)。模型一次画出来的几格是同一个角色、同一套画风, +// 比一帧一帧分开画一致得多,切开按顺序播就是一组表情变化(真机 20261118-db1dd9)。 +// +// 找格子在缩到长边 1024 的图上做。每一列(找竖的分隔)、每一行(找横的分隔)量两种证据: +// 细线:这一列和左右隔几个像素的列都差得多、而且朝同一个方向(都更暗或都更亮),贯穿大部分行; +// 留白:这一列几乎整列一个颜色,连着好几列都是,而且这一段两边都紧挨着一条贯穿整列的格子边。 +// 再逐个假设「等分成 n 格」,看每个该有分隔的位置附近有没有证据。模型画的组图基本等分但不精确 +// (db1dd9 三等分该在 341、683,实际在 343、685),所以切点贴到等分位置附近的分隔线上,不按精确的等分切; +// 附近没有像分隔的结构(格子直接挨着画,真机 20261119-250f55)就按等分位置切。 +// 先按行切,每一行再单独找列:上面两格、下面三格这种排法也切得开;只有一格的矮行(标题栏、底部说明)不算格子。 +import { resizeArea, toRgb } from './gif.mjs'; + +export const PANELS = { + analysisEdge: 1024, + // 每个方向最多几格。 + maxCells: 8, + // 细线:和两侧隔 lineSpans 个像素的列,某个通道同向差超过 lineDiff 级。 + lineDiff: 16, + lineSpans: [2, 4], + // 留白:和整列中位色差不超过 flatTol 级的像素占 flatShare 以上,连着 flatRun 列以上。 + flatTol: 10, + flatShare: 0.92, + flatRun: 2, + // 格子边:一整列里八成以上的像素和留白的颜色差这么多以上。 + edgeDiff: 15, + // 留白的证据比细线弱一档:一整列都是背景的地方不一定是格子边界。 + flatWeight: 0.9, + // 等分位置左右各找一格宽的这么多。 + window: 0.1, + // 每个分隔处的证据至少这么强,这个假设(几格)才成立:分隔被越过格子的内容挡住一截也算。 + minEvidence: 0.55, + // 切点只贴到像分隔的结构上:贯穿这一行(列)八成以上的线,或者两边都是格子边的留白带。画面里的竖线(屏幕边框、 + // 图表的坐标轴、门框)只占一部分高度:真机 20261119-250f55 的六格之间没画分隔线、格子直接挨着,程序贴到了 + // 第 2、3 格屏幕的边框和坐标轴上(线只贯穿 51–74%),切点偏了 70–120 像素。没有这样的结构就按等分位置切。 + separatorLine: 0.8, + // 不等分的排法:单独认一条分隔线要的证据、切出来的格子最窄占多少。 + freeEvidence: 0.75, + minCell: 0.12, + // 图四周的外框和留白最多占这么多,先去掉再找分隔。 + maxMargin: 0.15, + // 每条边最多往里修多少(占格子宽高的比例)。 + maxTrim: 0.06, + // 整格几乎一个颜色:空格子,不算一格。 + blankShare: 0.97, + // 格子宽高比差这么多以上,说明是大小不一的排法,各格缩进同一个画幅、四周补底色,不裁。 + aspectTolerance: 0.12, + // 对齐:在长边 320 的图上找,最多挪格子宽高的 4%;比不挪贴合 10% 以上才挪。再在长边 960 的图上、粗结果附近 + // ±3 像素细调(见 alignOffsets)。 + alignEdge: 320, + alignShift: 0.04, + alignGain: 0.9, + alignFine: 960, + alignRefine: 3, + // 对齐只比每格的中上部(左右各去掉 15%、下面去掉 30%):组图里每格不变的是主体的头和脸,在中上部;下面的手和身体 + // 每格动作不同,两边多是背景,模型每格画的背景位置也不一样。整格一起比时,db1dd9 第二版的第 4 格(双手抱头) + // 被举起的胳膊和背景里的绿线拉偏,头比别的格低 12 个 GIF 像素;只比中上部以后差 1 像素以内。 + alignRegion: [0.15, 0, 0.85, 0.7], + // 过渡格(另一张图)再找缩放:±6%,在长边 480 的图上找,比不缩贴合 3% 以上才缩。 + alignZoom: 0.06, + alignZoomEdge: 480, + alignZoomGain: 0.97, +}; + +// 找格子。hints:rows / cols(几行几列,已知时给)、count(一共几格)。 +// 返回 { width, height, rows, cols, count, method, strength, cells:[{index,row,col,x,y,w,h}] },坐标是原图像素; +// 切不出两格以上时 count 为 1、cells 只有整张图。method:line(按分隔线)/ gutter(按留白)/ free(不等分)/ +// even(等分:格子之间没有清楚的分隔,或者按指定的行列等分)。 +export function detectPanels(image, hints) { + const opts = hints || {}; + const rgb = toRgb(image); + const scale = Math.min(1, PANELS.analysisEdge / Math.max(rgb.width, rgb.height)); + const w = Math.max(32, Math.round(rgb.width * scale)); + const h = Math.max(32, Math.round(rgb.height * scale)); + const small = resizeArea(rgb, w, h); + const ctx = { small, flip: transpose(small), fx: rgb.width / w, fy: rgb.height / h }; + let result = locate(ctx, cleanHint(opts.rows), cleanHint(opts.cols)); + const want = cleanHint(opts.count); + // 说了一共几格、自动找的对不上:按这个格数的各种排法(6 格:3×2、2×3、6×1……)各试一次,取分隔证据最强的; + // 都没有证据(白底贴纸,格子之间什么都没有)就等分,取格子最接近方形的排法。 + if (want >= 2 && result.cells.length !== want && !opts.rows && !opts.cols) { + let best = null; + for (const [rows, cols] of factorPairs(want)) { + const trial = locate(ctx, rows, cols); + if (trial.cells.length !== want) continue; + trial.squareness = Math.abs(Math.log(rgb.width / cols / (rgb.height / rows))); + const stronger = best && trial.strength > best.strength + 0.05; + const tie = best && Math.abs(trial.strength - best.strength) <= 0.05 && trial.squareness < best.squareness; + if (!best || stronger || tie) best = trial; + } + if (best) result = best; + } + // 缩图的第 x 列取的是原图 [floor(x·fx), floor((x+1)·fx)) 这几列(resizeArea 的取法):换算回去也用 floor, + // 四舍五入会把分隔线的头一列带进格子里。 + const toFull = (value, factor, limit) => Math.max(0, Math.min(limit, Math.floor(value * factor))); + const cells = result.cells.map((cell, index) => { + const x = toFull(cell.x0, ctx.fx, rgb.width); + const y = toFull(cell.y0, ctx.fy, rgb.height); + return { index: index + 1, row: cell.row + 1, col: cell.col + 1, x, y, w: toFull(cell.x1, ctx.fx, rgb.width) - x, h: toFull(cell.y1, ctx.fy, rgb.height) - y }; + }); + if (cells.length < 2) { + return { width: rgb.width, height: rgb.height, rows: 1, cols: 1, count: 1, method: 'none', strength: 0, cells: [{ index: 1, row: 1, col: 1, x: 0, y: 0, w: rgb.width, h: rgb.height }] }; + } + return { + width: rgb.width, + height: rgb.height, + rows: result.rows, + cols: result.cols, + count: cells.length, + method: result.method, + strength: round2(result.strength), + cells, + }; +} + +function cleanHint(value) { + const n = Math.round(Number(value)); + return Number.isFinite(n) && n >= 1 && n <= PANELS.maxCells * PANELS.maxCells ? n : 0; +} + +function factorPairs(count) { + const out = []; + for (let rows = 1; rows <= PANELS.maxCells; rows += 1) { + if (count % rows) continue; + const cols = count / rows; + if (cols <= PANELS.maxCells) out.push([rows, cols]); + } + return out; +} + +// 先切行,再在每一行里切列,修掉格子边上的分隔线和外框,去掉空格子。 +function locate(ctx, rowHint, colHint) { + const { small, flip } = ctx; + const rowSplit = splitAxis(flip, Math.min(rowHint, PANELS.maxCells)); + const bands = spans(rowSplit.cuts, rowSplit.from, rowSplit.to); + let rows = bands.map(([y0, y1]) => { + const strip = cropRows(small, y0, y1); + const colSplit = splitAxis(strip, Math.min(colHint, PANELS.maxCells)); + return { y0, y1, split: colSplit, cells: spans(colSplit.cuts, colSplit.from, colSplit.to) }; + }); + // 标题栏、底部说明:别的行切出了好几格,这一行只有一格又明显矮。 + const multi = rows.filter((row) => row.cells.length >= 2); + if (multi.length) { + const typical = median(multi.map((row) => row.y1 - row.y0)); + rows = rows.filter((row) => row.cells.length >= 2 || row.y1 - row.y0 >= typical * 0.6); + } + let cells = []; + rows.forEach((row, r) => row.cells.forEach(([x0, x1], c) => cells.push({ row: r, col: c, x0, x1, y0: row.y0, y1: row.y1 }))); + cells = cells.filter((cell) => !isBlank(small, cell)); + trimCells(ctx, cells); + const splits = [rowSplit, ...rows.map((row) => row.split)].filter((split) => split.cuts.length); + const strength = splits.length ? Math.min(...splits.map((split) => split.strength)) : 0; + const methods = new Set(splits.map((split) => split.method)); + const method = methods.has('even') ? 'even' : methods.has('free') ? 'free' : methods.has('gutter') ? 'gutter' : 'line'; + return { rows: rows.length, cols: Math.max(1, ...rows.map((row) => row.cells.length)), method, strength, cells }; +} + +function spans(cuts, from, to) { + const edges = [from, ...cuts, to]; + const out = []; + for (let i = 0; i + 1 < edges.length; i += 1) if (edges[i + 1] - edges[i] >= 4) out.push([edges[i], edges[i + 1]]); + return out; +} + +// ------------------------------------------------------------------ 一个方向上找分隔 + +// img 的每一列量证据,返回这一方向上的切点(列号)。hint 是已知的格数(0 表示不知道)。 +function splitAxis(img, hint) { + const ev = columnEvidence(img, 0, img.height); + const { from, to } = contentSpan(img, ev); + const span = to - from; + const none = { cuts: [], from, to, method: 'none', strength: 0 }; + if (hint === 1 || span < 16) return none; + // 指定了几格:附近有分隔就贴上去,没有就等分(见 evenTrial)。 + if (hint >= 2) return { ...evenTrial(ev, from, span, hint), from, to }; + // 所有分隔都找得到的假设里取格子最多的:真是三格时,按六格切会有一半切点落在格子中间,找不到证据。 + let pick = null; + for (let n = 2; n <= PANELS.maxCells; n += 1) { + const trial = evenTrial(ev, from, span, n); + if (trial.strength >= PANELS.minEvidence) pick = trial; + } + if (pick) return { ...pick, from, to }; + const free = freeCuts(ev, from, to); + return free.length ? { cuts: free, from, to, method: 'free', strength: Math.min(...free.map((x) => ev.score[x])) } : none; +} + +// 假设等分成 n 格:每个等分位置附近(离等分位置越远扣得越多)找证据。 +// 这个假设成不成立(strength)看窗口里最强的证据;切点只贴到像分隔的结构上(见 PANELS.separatorLine),没有就按等分位置切。 +// 一段分隔里只能有一个切点:两个切点落进同一段,说明这段是一整片背景,不是两条边界。 +function evenTrial(ev, from, span, n) { + const cell = span / n; + const win = Math.max(2, Math.round(cell * PANELS.window)); + const cuts = []; + let strength = 1; + let snapped = 0; + let gutter = false; + let lastEnd = -1; + for (let k = 1; k < n; k += 1) { + const target = from + k * cell; + let best = Math.round(target); + let bestScore = -Infinity; + let sep = -1; + let sepScore = -Infinity; + const lo = Math.max(from + 2, Math.round(target - win)); + const hi = Math.min(from + span - 3, Math.round(target + win)); + for (let x = lo; x <= hi; x += 1) { + const score = ev.score[x] - (0.15 * Math.abs(x - target)) / win; + if (score > bestScore) { + bestScore = score; + best = x; + } + if ((ev.line[x] >= PANELS.separatorLine || ev.band[x]) && score > sepScore) { + sepScore = score; + sep = x; + } + } + strength = Math.min(strength, ev.score[best]); + if (sep < 0) { + cuts.push(Math.round(target)); + continue; + } + snapped += 1; + if (ev.line[sep] < ev.flatScore[sep]) gutter = true; + const [a, b] = structureExtent(ev, sep); + if (a <= lastEnd) strength = 0; + lastEnd = b; + // 切在整条分隔(线、留白、两边格子的边框连在一起)的中间;很宽的分不清边界在哪,按等分位置切。 + cuts.push(b - a > cell * 0.3 ? Math.max(a, Math.min(b, Math.round(target))) : Math.round((a + b) / 2)); + } + return { cuts, strength, method: !snapped ? 'even' : gutter ? 'gutter' : 'line', n }; +} + +// 分隔处的整段结构:线、两边都是格子边的留白带,中间允许隔一列缩图混出来的过渡色。 +// 带边框的格子之间是「边框 + 留白 + 边框」,只切在其中一条边框上,一边的格子会带着整条留白和对面的边框。 +function structureExtent(ev, x) { + const part = (i) => i >= 0 && i < ev.line.length && (ev.line[i] >= 0.5 || ev.band[i] === 1); + let a = x; + let b = x; + for (;;) { + if (part(a - 1)) a -= 1; + else if (part(a - 2)) a -= 2; + else break; + } + for (;;) { + if (part(b + 1)) b += 1; + else if (part(b + 2)) b += 2; + else break; + } + return [a, b]; +} + +function sameColor(colors, a, b) { + return Math.max(Math.abs(colors[a * 3] - colors[b * 3]), Math.abs(colors[a * 3 + 1] - colors[b * 3 + 1]), Math.abs(colors[a * 3 + 2] - colors[b * 3 + 2])) <= 12; +} + +// 不等分的排法:只认很强的细线和两边都是格子边的留白带,切出来的格子不能太窄。 +function freeCuts(ev, from, to) { + const span = to - from; + const minCell = Math.max(8, Math.round(span * PANELS.minCell)); + const found = []; + for (let x = from + minCell; x <= to - minCell; x += 1) { + const line = ev.line[x] >= PANELS.freeEvidence; + const gutter = !line && ev.band[x] && ev.flat[x] >= PANELS.blankShare; + if (!line && !gutter) continue; + const score = line ? ev.line[x] : ev.flatScore[x]; + const last = found[found.length - 1]; + if (last && x - last.x < minCell) { + if (score > last.score) found[found.length - 1] = { x, score, line }; + continue; + } + found.push({ x, score, line }); + } + return found.map((item) => { + const [a, b] = structureExtent(ev, item.x); + return Math.round((a + b) / 2); + }); +} + +// 四周的外框和留白:两头各往里数「结构」有多宽(见 borderWidth),找分隔时按去掉之后的范围等分。 +function contentSpan(img, ev) { + const width = img.width; + const limit = Math.round(width * PANELS.maxMargin); + const from = borderWidth(img, ev, 0, 1, limit, 0, img.height); + const to = width - borderWidth(img, ev, width - 1, -1, limit, 0, img.height); + if (to - from < width * 0.5) return { from: 0, to: width }; + return { from, to }; +} + +// 从 start 往 dir 方向数,边上有多宽是结构:分隔线、外框线,或者一片同色留白、紧接着一条贯穿整列的格子边(或者一条线)。 +// 格子自己的空白背景后面接的是头发、衣服这些零散的内容,不算:db1dd9 每格顶上那一截浅灰底以前被当成外边距修掉, +// 上面一行的格子比下面一行少一截,切出来人物上下差了 46 像素。 +function borderWidth(img, ev, start, dir, limit, y0, y1) { + const at = (t) => start + dir * t; + let t = 0; + for (let guard = 0; guard < 8 && t < limit; guard += 1) { + let lineEnd = -1; + for (let k = t; k < Math.min(limit, t + 4); k += 1) if (ev.line[at(k)] >= 0.5) lineEnd = k; + if (lineEnd >= 0) { + let k = lineEnd + 1; + while (k < limit && ev.line[at(k)] >= 0.5) k += 1; + // 线的另一边再多修一列:抗锯齿的过渡像素留在边上,播起来是一圈细边。 + t = Math.min(limit, k + 1); + continue; + } + let k = t; + while (k < limit && ev.flat[at(k)] >= PANELS.blankShare && sameColor(ev.color, at(k), at(t))) k += 1; + if (k === t || k >= limit) break; + // 留白和格子之间常有一列缩图时混出来的过渡色:它后面那一列才是格子边,过渡色那一列一起修掉。 + const edgeAt = (i) => ev.line[at(i)] >= 0.5 || differShare(img, at(i), y0, y1, ev.color, at(t)) >= 0.8; + if (edgeAt(k)) t = k; + else if (k + 1 < limit && edgeAt(k + 1)) t = k + 1; + else break; + } + return t; +} + +// 第 x 列在 [y0, y1) 行里和 colors 里第 ref 列的颜色差得明显的像素占多少。 +function differShare(img, x, y0, y1, colors, ref) { + const { width: w, data } = img; + const step = Math.max(1, Math.floor((y1 - y0) / 400)); + let differ = 0; + let count = 0; + for (let y = y0; y < y1; y += step) { + const o = (y * w + x) * 3; + count += 1; + if (Math.max(Math.abs(data[o] - colors[ref * 3]), Math.abs(data[o + 1] - colors[ref * 3 + 1]), Math.abs(data[o + 2] - colors[ref * 3 + 2])) > PANELS.edgeDiff) differ += 1; + } + return count ? differ / count : 0; +} + +// 每一列在 [y0, y1) 行里的证据:line 细线占比、flat 留白占比、band 是否在一条留白带里、score 综合证据、color 中位色。 +function columnEvidence(img, y0, y1) { + const { width: w, data } = img; + const rows = Math.max(1, y1 - y0); + const step = Math.max(1, Math.floor(rows / 400)); + const line = new Float32Array(w); + const flat = new Float32Array(w); + const color = new Uint8Array(w * 3); + const T = PANELS.lineDiff; + const hist = [new Uint32Array(256), new Uint32Array(256), new Uint32Array(256)]; + for (let x = 0; x < w; x += 1) { + let hits = 0; + let count = 0; + for (const bins of hist) bins.fill(0); + for (let y = y0; y < y1; y += step) { + const o = (y * w + x) * 3; + count += 1; + hist[0][data[o]] += 1; + hist[1][data[o + 1]] += 1; + hist[2][data[o + 2]] += 1; + let hit = false; + for (const d of PANELS.lineSpans) { + if (x - d < 0 || x + d >= w) continue; + const l = o - d * 3; + const r = o + d * 3; + for (let c = 0; c < 3; c += 1) { + const a = data[o + c] - data[l + c]; + const b = data[o + c] - data[r + c]; + if ((a > T && b > T) || (a < -T && b < -T)) { + hit = true; + break; + } + } + if (hit) break; + } + if (hit) hits += 1; + } + line[x] = count ? hits / count : 0; + for (let c = 0; c < 3; c += 1) color[x * 3 + c] = medianOf(hist[c], count); + let same = 0; + for (let y = y0; y < y1; y += step) { + const o = (y * w + x) * 3; + if (Math.max(Math.abs(data[o] - color[x * 3]), Math.abs(data[o + 1] - color[x * 3 + 1]), Math.abs(data[o + 2] - color[x * 3 + 2])) <= PANELS.flatTol) same += 1; + } + flat[x] = count ? same / count : 0; + } + // 留白带:连着 flatRun 列以上整列一个颜色、颜色也接得上,而且两边都紧挨着一条贯穿整列的格子边(或者图边)。 + // 格子自己的纯色底也是整列一个颜色,但它一边挨着的是人物这些零散的内容:不认它,否则一格会被底色切成好几格 + // (彩色底的两格,按六格切的每个切点都落在一片纯色底里)。 + const band = new Uint8Array(w); + const rawRun = (x) => flat[x] >= PANELS.flatShare; + let x = 0; + while (x < w) { + if (!rawRun(x)) { + x += 1; + continue; + } + let e = x + 1; + // 和这一段的第一列比颜色,不和前一列比:过渡色一列一列只差几级,接着比会把底色和留白连成一段。 + while (e < w && rawRun(e) && sameColor(color, e, x)) e += 1; + if (e - x >= PANELS.flatRun) { + const straight = (at, ref) => at < 0 || at >= w || differShare(img, at, y0, y1, color, ref) >= 0.8; + const leftEdge = straight(x - 1, x) || straight(x - 2, x); + const rightEdge = straight(e, e - 1) || straight(e + 1, e - 1); + if (leftEdge && rightEdge) band.fill(1, x, e); + } + x = e; + } + const flatScore = new Float32Array(w); + const score = new Float32Array(w); + for (let x = 0; x < w; x += 1) { + flatScore[x] = band[x] ? flat[x] * PANELS.flatWeight : 0; + score[x] = Math.max(line[x], flatScore[x]); + } + return { line, flat, band, flatScore, score, color }; +} + +function medianOf(bins, count) { + let acc = 0; + const half = count / 2; + for (let v = 0; v < 256; v += 1) { + acc += bins[v]; + if (acc >= half) return v; + } + return 255; +} + +// ------------------------------------------------------------------ 修边、空格 + +// 格子边上残留的分隔线、外框、半条留白修掉(只修结构,见 borderWidth)。每一格按自己的边修: +// 等分的组图里每格的结构一样宽,修完各格一样大;最外面的格子带着外边距,里面的格子没有,统一修同样多会让内容错位。 +function trimCells(ctx, cells) { + for (const cell of cells) { + const left = edgeTrim(ctx, cell, 'left'); + const right = edgeTrim(ctx, cell, 'right'); + const top = edgeTrim(ctx, cell, 'top'); + const bottom = edgeTrim(ctx, cell, 'bottom'); + cell.x0 += left; + cell.x1 -= right; + cell.y0 += top; + cell.y1 -= bottom; + } +} + +// 一格的一条边往里该修多少,最多修这一格宽(高)的 maxTrim。 +// 取边上那几列时两头各多带 4 列:判断一列是不是线要和它两侧隔几个像素的列比,切点正好落在线中间。 +function edgeTrim(ctx, cell, side) { + const vertical = side === 'left' || side === 'right'; + const img = vertical ? ctx.small : ctx.flip; + const lo = vertical ? cell.y0 : cell.x0; + const hi = vertical ? cell.y1 : cell.x1; + const a = vertical ? cell.x0 : cell.y0; + const b = vertical ? cell.x1 : cell.y1; + const limit = Math.max(1, Math.round((b - a) * PANELS.maxTrim)); + const pad = 4; + const near = side === 'left' || side === 'top'; + const slice = sliceCols(img, near ? a - pad : b - limit - pad, limit + 2 * pad); + const ev = columnEvidence(slice, lo, hi); + return borderWidth(slice, ev, near ? pad : limit + pad - 1, near ? 1 : -1, limit, lo, hi); +} + +// 取 [x, x+n) 这几列(越界的按边上的列补),给 columnEvidence 用:它要的是一张完整的小图。 +function sliceCols(img, x, n) { + const { width: w, height: h, data } = img; + const out = Buffer.alloc(n * h * 3); + for (let y = 0; y < h; y += 1) { + for (let i = 0; i < n; i += 1) { + const sx = Math.max(0, Math.min(w - 1, x + i)); + data.copy(out, (y * n + i) * 3, (y * w + sx) * 3, (y * w + sx) * 3 + 3); + } + } + return { width: n, height: h, channels: 3, data: out }; +} + +function isBlank(img, cell) { + const { width: w, data } = img; + const step = Math.max(1, Math.floor(Math.sqrt(((cell.x1 - cell.x0) * (cell.y1 - cell.y0)) / 4000))); + const samples = []; + for (let y = cell.y0; y < cell.y1; y += step) for (let x = cell.x0; x < cell.x1; x += step) samples.push((y * w + x) * 3); + if (!samples.length) return true; + const mid = [0, 1, 2].map((c) => median(samples.map((o) => data[o + c]))); + let same = 0; + for (const o of samples) { + if (Math.max(Math.abs(data[o] - mid[0]), Math.abs(data[o + 1] - mid[1]), Math.abs(data[o + 2] - mid[2])) <= PANELS.flatTol) same += 1; + } + return same / samples.length >= PANELS.blankShare; +} + +// ------------------------------------------------------------------ 统一大小、对齐 + +// 把各格变成同样大小的取景框。格子大小一致(差几个像素)时各格居中取同样大小;align 为 true 时再按画面 +// 对齐:模型在每一格里画的主体位置差一点,原样播放主体会一跳一跳的。大小不一的排法不裁,缩进同一个画幅。 +// 返回 { mode: 'crop' | 'fit', width, height, frames:[{x,y,w,h,dx,dy}] },crop 时每格取原图的 (x,y,w,h)。 +export function framePanels(image, cells, options) { + const opts = options || {}; + const rgb = toRgb(image); + const aspects = cells.map((cell) => cell.w / cell.h); + const typical = median(aspects); + const mixed = aspects.some((value) => Math.abs(value - typical) / typical > PANELS.aspectTolerance); + if (mixed) { + const width = Math.round(median(cells.map((cell) => cell.w))); + const height = Math.round(median(cells.map((cell) => cell.h))); + return { mode: 'fit', width, height, frames: cells.map((cell) => ({ x: cell.x, y: cell.y, w: cell.w, h: cell.h, dx: 0, dy: 0 })), aligned: 0 }; + } + const baseW = Math.min(...cells.map((cell) => cell.w)); + const baseH = Math.min(...cells.map((cell) => cell.h)); + const centered = cells.map((cell) => ({ cx: cell.x + cell.w / 2, cy: cell.y + cell.h / 2 })); + let offsets = cells.map(() => ({ dx: 0, dy: 0 })); + if (opts.align !== false && cells.length >= 2) offsets = alignOffsets(rgb, centered, baseW, baseH, opts.reference || 0); + const maxDx = Math.max(...offsets.map((item) => Math.abs(item.dx))); + const maxDy = Math.max(...offsets.map((item) => Math.abs(item.dy))); + const width = Math.max(8, Math.floor(baseW - 2 * maxDx)); + const height = Math.max(8, Math.floor(baseH - 2 * maxDy)); + const frames = centered.map((center, index) => { + const x = Math.round(center.cx + offsets[index].dx - width / 2); + const y = Math.round(center.cy + offsets[index].dy - height / 2); + return { + x: Math.max(0, Math.min(rgb.width - width, x)), + y: Math.max(0, Math.min(rgb.height - height, y)), + w: width, + h: height, + dx: Math.round(offsets[index].dx), + dy: Math.round(offsets[index].dy), + }; + }); + return { mode: 'crop', width, height, frames, aligned: offsets.filter((item) => item.dx || item.dy).length }; +} + +// 每一格相对参考格(默认第一格)挪多少(原图像素):先在长边 320 的梯度图上大范围找,再在长边 960 的图上、 +// 粗结果附近细找一次,都用抛物线细到亚像素。只粗找的话,320 的图上 1 像素是原图 5–6 像素,对齐完各格还差 1–2 像素 +// (真机 20261119-250f55 第二行三格的头比第一格低 0.6–0.7 个 GIF 像素,切换时线稿轮廓会抖一下)。 +function alignOffsets(rgb, centers, baseW, baseH, reference) { + const coarse = matchPanels(rgb, centers, baseW, baseH, reference, PANELS.alignEdge, null); + return matchPanels(rgb, centers, baseW, baseH, reference, PANELS.alignFine, coarse); +} + +// prior 没给是粗找:±alignShift 的范围,要比不挪明显更贴合(alignGain)、又没顶到搜索边界才算, +// 顶到边界多半是两格的内容本来就不一样(动作大),硬对反而错开。prior 给了是细调:每一格先按粗结果挪过去再取图, +// 在附近 ±alignRefine 像素里找,没顶到边界就算;粗找没对上的格子不细调。 +function matchPanels(rgb, centers, baseW, baseH, reference, edge, prior) { + const scale = Math.min(1, edge / Math.max(baseW, baseH)); + const w = Math.max(16, Math.round(baseW * scale)); + const h = Math.max(16, Math.round(baseH * scale)); + const origin = (center, shift) => ({ x: Math.round(center.cx + shift.dx - baseW / 2), y: Math.round(center.cy + shift.dy - baseH / 2) }); + const sample = (at) => gradient(resizeArea(cropImage(rgb, at.x, at.y, baseW, baseH), w, h)); + const refCenter = centers[reference] || centers[0]; + const refAt = origin(refCenter, { dx: 0, dy: 0 }); + const ref = sample(refAt); + const R = prior ? PANELS.alignRefine : Math.max(2, Math.round(Math.max(w, h) * PANELS.alignShift)); + return centers.map((center, index) => { + if (index === reference) return { dx: 0, dy: 0, matched: true }; + const start = prior ? prior[index] : { dx: 0, dy: 0, matched: true }; + if (!start.matched) return start; + const at = origin(center, start); + const found = bestShift(ref, sample(at), w, h, R, !prior); + if (!found) return prior ? start : { dx: 0, dy: 0, matched: false }; + // 两边取图的位置都取过整:按取整之后的位置算这一格相对参考格挪了多少,再加上在这张图上找到的平移。 + return { + dx: at.x - refAt.x - (center.cx - refCenter.cx) + (found.u * baseW) / w, + dy: at.y - refAt.y - (center.cy - refCenter.cy) + (found.v * baseH) / h, + matched: true, + }; + }); +} + +// 在 ±R 里找 b 相对 a 挪多少最贴合(只比 alignRegion 那一块,隔一个像素取样比梯度差),返回 { u, v }(亚像素)或 null。 +function bestShift(a, b, w, h, R, strict) { + const side = 2 * R + 1; + const costs = new Float64Array(side * side); + const [rx0, ry0, rx1, ry1] = PANELS.alignRegion; + const xa = Math.max(R, Math.round(rx0 * w)); + const xb = Math.min(w - R, Math.round(rx1 * w)); + const ya = Math.max(R, Math.round(ry0 * h)); + const yb = Math.min(h - R, Math.round(ry1 * h)); + for (let v = -R; v <= R; v += 1) { + for (let u = -R; u <= R; u += 1) { + let sum = 0; + let n = 0; + for (let y = ya; y < yb; y += 2) { + const ra = y * w; + const rb = (y + v) * w + u; + for (let x = xa; x < xb; x += 2) { + sum += Math.abs(a[ra + x] - b[rb + x]); + n += 1; + } + } + costs[(v + R) * side + u + R] = n ? sum / n : Infinity; + } + } + let bi = 0; + for (let i = 1; i < costs.length; i += 1) if (costs[i] < costs[bi]) bi = i; + const bu = bi % side; + const bv = (bi / side) | 0; + if (bu === 0 || bv === 0 || bu === side - 1 || bv === side - 1) return null; + if (strict && !(costs[bi] <= costs[R * side + R] * PANELS.alignGain)) return null; + const refine = (before, at, after) => { + const den = before - 2 * at + after; + return den > 1e-9 ? Math.max(-0.5, Math.min(0.5, (0.5 * (before - after)) / den)) : 0; + }; + return { u: bu - R + refine(costs[bi - 1], costs[bi], costs[bi + 1]), v: bv - R + refine(costs[bi - side], costs[bi], costs[bi + side]), cost: costs[bi] }; +} + +// 另一张图(rgb,比例和参考图一样、大小可以不同)里对应的那一块,对齐到参考图 refRgb 里的 refFrame。 +// 给过渡格用:过渡格是在原组图上改出来的另一张图,格子的位置和原图差不多,人物的头要和原图那一格对上。 +// guess 是这一块在 rgb 里的中心(rgb 的像素)。先在长边 320 的图上粗找(明显更贴合才挪),再在长边 960 的图上 +// 细调;粗找没对上就按 guess 细调。比的都是每格的中上部(见 alignRegion)。 +// 最后再找一次缩放:另一张图是重画的,人物常被画大或画小一两个百分点(真机 20261119-250f55 的一格过渡格大了约 3%), +// 只挪不缩的话,头的轮廓前后两帧对不上,播起来一胀一缩。 +// 返回 rgb 里对齐后的取景框 { x, y, w, h, dx, dy, scale, matched }:大小是 refFrame 按两张图的比例缩放、再除以 scale 的大小, +// 拿去缩到和别的格子一样大,人物就一样大了。 +// 横竖分别按两张图的比例缩放:接口回的图比例和原图不一定完全一样(5472×3072 改出来是 2720×1536,横竖差 0.6%), +// 只按宽度缩放的话,越往下的格子差得越多。 +export function alignAcross(refRgb, refFrame, rgb, guess) { + const w0 = Math.max(8, Math.round((refFrame.w * rgb.width) / refRgb.width)); + const h0 = Math.max(8, Math.round((refFrame.h * rgb.height) / refRgb.height)); + const coarse = matchAcross(refRgb, refFrame, rgb, guess, w0, h0, PANELS.alignEdge, null); + const fine = matchAcross(refRgb, refFrame, rgb, guess, w0, h0, PANELS.alignFine, coarse.matched ? coarse : { dx: 0, dy: 0, matched: false }); + const zoom = matchScale(refRgb, refFrame, rgb, { cx: guess.cx + fine.dx, cy: guess.cy + fine.dy }, w0, h0); + const w = Math.max(8, Math.min(rgb.width, Math.round(w0 / zoom.scale))); + const h = Math.max(8, Math.min(rgb.height, Math.round(h0 / zoom.scale))); + const x = Math.round(zoom.cx - w / 2); + const y = Math.round(zoom.cy - h / 2); + return { + x: Math.max(0, Math.min(rgb.width - w, x)), + y: Math.max(0, Math.min(rgb.height - h, y)), + w, + h, + dx: Math.round(zoom.cx - guess.cx), + dy: Math.round(zoom.cy - guess.cy), + scale: zoom.scale, + matched: coarse.matched, + }; +} + +// 在 ±alignZoom 的范围里一个百分点一个百分点地试缩放(每个缩放再细找 ±2 像素的平移),取最贴合的。 +// 比不缩明显好(alignGain)才缩,免得在纹理平的格子里乱缩。 +function matchScale(refRgb, refFrame, rgb, center, w0, h0) { + const edge = PANELS.alignZoomEdge; + const scale = Math.min(1, edge / Math.max(refFrame.w, refFrame.h)); + const w = Math.max(16, Math.round(refFrame.w * scale)); + const h = Math.max(16, Math.round(refFrame.h * scale)); + const ref = gradient(resizeArea(cropImage(refRgb, refFrame.x, refFrame.y, refFrame.w, refFrame.h), w, h)); + const tryScale = (s) => { + const cw = Math.max(8, Math.round(w0 / s)); + const ch = Math.max(8, Math.round(h0 / s)); + const got = gradient(resizeArea(cropImage(rgb, Math.round(center.cx - cw / 2), Math.round(center.cy - ch / 2), cw, ch), w, h)); + const found = bestShift(ref, got, w, h, 2, false); + return found ? { s, cost: found.cost, cx: center.cx + (found.u * cw) / w, cy: center.cy + (found.v * ch) / h } : null; + }; + const base = tryScale(1) || { s: 1, cost: Infinity, cx: center.cx, cy: center.cy }; + let best = base; + const steps = Math.round(PANELS.alignZoom / 0.01); + for (let k = -steps; k <= steps; k += 1) { + if (!k) continue; + const got = tryScale(1 + k * 0.01); + if (got && got.cost < best.cost) best = got; + } + if (best !== base && !(best.cost <= base.cost * PANELS.alignZoomGain)) best = base; + return { scale: best.s, cx: best.cx, cy: best.cy }; +} + +function matchAcross(refRgb, refFrame, rgb, guess, w0, h0, edge, prior) { + const scale = Math.min(1, edge / Math.max(refFrame.w, refFrame.h)); + const w = Math.max(16, Math.round(refFrame.w * scale)); + const h = Math.max(16, Math.round(refFrame.h * scale)); + const ref = gradient(resizeArea(cropImage(refRgb, refFrame.x, refFrame.y, refFrame.w, refFrame.h), w, h)); + const start = prior || { dx: 0, dy: 0 }; + const at = { x: Math.round(guess.cx + start.dx - w0 / 2), y: Math.round(guess.cy + start.dy - h0 / 2) }; + const got = gradient(resizeArea(cropImage(rgb, at.x, at.y, w0, h0), w, h)); + const R = prior ? PANELS.alignRefine : Math.max(2, Math.round(Math.max(w, h) * PANELS.alignShift)); + const found = bestShift(ref, got, w, h, R, !prior); + if (!found) return prior ? { ...prior } : { dx: 0, dy: 0, matched: false }; + return { + dx: at.x - (guess.cx - w0 / 2) + (found.u * w0) / w, + dy: at.y - (guess.cy - h0 / 2) + (found.v * h0) / h, + matched: prior ? Boolean(prior.matched) : true, + }; +} + +function gradient(img) { + const { width: w, height: h, data } = img; + const lum = new Float32Array(w * h); + for (let p = 0; p < w * h; p += 1) lum[p] = 0.299 * data[p * 3] + 0.587 * data[p * 3 + 1] + 0.114 * data[p * 3 + 2]; + const out = new Float32Array(w * h); + for (let y = 1; y < h - 1; y += 1) { + for (let x = 1; x < w - 1; x += 1) { + const p = y * w + x; + out[p] = Math.abs(lum[p + 1] - lum[p - 1]) + Math.abs(lum[p + w] - lum[p - w]); + } + } + return out; +} + +// ------------------------------------------------------------------ 出一格的画面 + +// 按取景框出一格、缩到 outW×outH。zoom 大于 1 时取中间 1/zoom 的一块放大(切换时轻微弹一下用)。 +// fit 模式(格子大小不一)把整格缩进画幅、四周补这一格边上的底色。 +export function renderPanel(image, frame, outW, outH, options) { + const opts = options || {}; + const zoom = Math.max(1, Number(opts.zoom) || 1); + const rgb = toRgb(image); + if (opts.mode === 'fit') { + const scale = Math.min(outW / frame.w, outH / frame.h) / zoom; + const fw = Math.max(1, Math.round(frame.w * scale * zoom)); + const fh = Math.max(1, Math.round(frame.h * scale * zoom)); + const canvas = Buffer.alloc(outW * outH * 3); + const fill = edgeColor(rgb, frame); + for (let p = 0; p < outW * outH; p += 1) canvas.set(fill, p * 3); + const cw = Math.min(fw, outW); + const ch = Math.min(fh, outH); + const sw = Math.round((cw / fw) * frame.w); + const sh = Math.round((ch / fh) * frame.h); + const part = resizeArea(cropImage(rgb, frame.x + Math.round((frame.w - sw) / 2), frame.y + Math.round((frame.h - sh) / 2), sw, sh), cw, ch); + const ox = Math.floor((outW - cw) / 2); + const oy = Math.floor((outH - ch) / 2); + for (let y = 0; y < ch; y += 1) part.data.copy(canvas, ((oy + y) * outW + ox) * 3, y * cw * 3, (y + 1) * cw * 3); + return { width: outW, height: outH, channels: 3, data: canvas }; + } + const w = Math.max(1, Math.round(frame.w / zoom)); + const h = Math.max(1, Math.round(frame.h / zoom)); + return resizeArea(cropImage(rgb, frame.x + Math.round((frame.w - w) / 2), frame.y + Math.round((frame.h - h) / 2), w, h), outW, outH); +} + +function edgeColor(rgb, frame) { + const values = [[], [], []]; + const push = (x, y) => { + const o = (Math.max(0, Math.min(rgb.height - 1, y)) * rgb.width + Math.max(0, Math.min(rgb.width - 1, x))) * 3; + for (let c = 0; c < 3; c += 1) values[c].push(rgb.data[o + c]); + }; + const inset = 2; + for (let x = frame.x; x < frame.x + frame.w; x += Math.max(1, Math.floor(frame.w / 64))) { + push(x, frame.y + inset); + push(x, frame.y + frame.h - 1 - inset); + } + for (let y = frame.y; y < frame.y + frame.h; y += Math.max(1, Math.floor(frame.h / 64))) { + push(frame.x + inset, y); + push(frame.x + frame.w - 1 - inset, y); + } + return Buffer.from(values.map((list) => median(list))); +} + +// ------------------------------------------------------------------ 小工具 + +export function cropImage(rgb, x, y, w, h) { + const out = Buffer.alloc(w * h * 3); + for (let row = 0; row < h; row += 1) { + const sy = Math.max(0, Math.min(rgb.height - 1, y + row)); + for (let col = 0; col < w; col += 1) { + const sx = Math.max(0, Math.min(rgb.width - 1, x + col)); + const s = (sy * rgb.width + sx) * 3; + const d = (row * w + col) * 3; + out[d] = rgb.data[s]; + out[d + 1] = rgb.data[s + 1]; + out[d + 2] = rgb.data[s + 2]; + } + } + return { width: w, height: h, channels: 3, data: out }; +} + +function cropRows(img, y0, y1) { + const { width: w, data } = img; + return { width: w, height: y1 - y0, channels: 3, data: Buffer.from(data.subarray(y0 * w * 3, y1 * w * 3)) }; +} + +function transpose(img) { + const { width: w, height: h, data } = img; + const out = Buffer.alloc(w * h * 3); + for (let y = 0; y < h; y += 1) { + for (let x = 0; x < w; x += 1) { + const s = (y * w + x) * 3; + const d = (x * h + y) * 3; + out[d] = data[s]; + out[d + 1] = data[s + 1]; + out[d + 2] = data[s + 2]; + } + } + return { width: h, height: w, channels: 3, data: out }; +} + +function median(values) { + if (!values.length) return 0; + const sorted = values.slice().sort((a, b) => a - b); + const mid = sorted.length >> 1; + return sorted.length % 2 ? sorted[mid] : (sorted[mid - 1] + sorted[mid]) / 2; +} + +function round2(value) { + return Math.round(value * 100) / 100; +} diff --git a/plugins/liddalidd/h3-image-executor/miniapp/node/lib/paneltween.mjs b/plugins/liddalidd/h3-image-executor/miniapp/node/lib/paneltween.mjs new file mode 100644 index 00000000..6295c2a4 --- /dev/null +++ b/plugins/liddalidd/h3-image-executor/miniapp/node/lib/paneltween.mjs @@ -0,0 +1,749 @@ +// 组图 GIF 的过渡格:相邻两格之间各补一格「中间状态」,一格一格的切换变成一步一步的动作。 +// +// 组图拼成的 GIF 是一格一格地切:表情、手一下子换掉(真机 20261119-250f55)。过渡格一起生成,能从候选里选: +// 在原组图上改一次,把每一格都改成「它和下一格中间」的样子,接口一次出 2 张候选;每一步先由程序筛掉照抄的, +// 再由过渡格评审从剩下的候选里挑一格,没挑到的步子再出一轮。一格一格单独画,每格只有一张,挑不了,画风也会一格一格地漂。 +// +// 每一格怎么改由过渡格规划节点看着组图写清楚(手抬到哪、嘴张多大)。只说「画两格中间的样子」,模型几乎都是照抄下一格: +// 真机试过,6 格里 4 到 6 格是照抄的;把「下一格」拼成第 2 张图送进去也一样。写清每一格的样子以后,两张候选 6 格都画成了中间状态。 +// +// 过渡格是另一张图:格子位置和原组图差不多但不完全一样(接口回的图比例和原图差 0.6%,越往右的格子差得越多), +// 每一格先对齐到原组图同一格的人物上(panels.alignAcross),再拿来比、拿来播。 +import { access, readFile, writeFile } from 'node:fs/promises'; +import { basename, join } from 'node:path'; +import * as h3 from './h3.mjs'; +import { decodeImage } from './image.mjs'; +import { changeShare, gifSize, resizeArea, toRgb } from './gif.mjs'; +import { pairKey } from './gifbuild.mjs'; +import { alignAcross, cropImage, detectPanels, framePanels, renderPanel } from './panels.mjs'; +import { encodePng } from './png.mjs'; +import { renderGridSheet } from './review.mjs'; +import { detail, kv, list, text } from './detail.mjs'; +import { difficultyOf, hitsOf, noteToolHit, toolBudget } from './budget.mjs'; + +export const TWEEN = { + candidates: 2, + // 一张组图最多出几轮过渡格(第二轮只补第一轮没挑到的步子)。 + rounds: 2, + // 两格之间锁好位置后整幅变了这么多以上(任一通道差 24 级以上的像素占比):不是同一个镜头,不补。 + sceneChange: 0.5, + // 两格变了的地方,过渡格有这么多跟另一头一样:照抄了另一头。真机照抄的 0.86–0.99,画了中间状态的 0.07–0.76。 + copyOther: 0.85, + // 这么少:这一格没改,原样又画了一遍。 + unchanged: 0.03, + // 离两头都远:到两头的距离加起来超过两头之间距离的这么多倍,画走了样(真机画对的最多 2.2)。 + maxExcess: 2.6, + sheetCell: 280, +}; + +// 要补哪几步:按播放顺序相邻的两格,循环播放(不来回)时再加最后一格接回第一格。来回播往回走用的是同一格。 +// 每一步画在其中一格的位置上(cell):先用前一格的位置,那一格已经被别的步子占了就用后一格(中间状态两头都一样)。 +// 还占不下的(播放顺序里同一格出现两次)留给下一轮。 +export function tweenPairs(count, spec) { + const s = spec || {}; + const dropped = new Set((Array.isArray(s.drop) ? s.drop : []).map(Number)); + let order = (Array.isArray(s.order) && s.order.length ? s.order : Array.from({ length: count }, (_, i) => i + 1)) + .map(Number) + .filter((n) => Number.isInteger(n) && n >= 1 && n <= count && !dropped.has(n)); + order = order.filter((n, i) => i === 0 || n !== order[i - 1]); + const loop = s.loop !== false; + if (loop && order.length > 2 && order[0] === order[order.length - 1]) order.pop(); + const steps = []; + for (let i = 0; i + 1 < order.length; i += 1) steps.push([order[i], order[i + 1]]); + if (loop && !s.pingpong && order.length > 2) steps.push([order[order.length - 1], order[0]]); + const seen = new Set(); + const out = []; + for (const [from, to] of steps) { + const key = pairKey(from, to); + if (seen.has(key)) continue; + seen.add(key); + out.push({ key, from, to }); + } + return out; +} + +function assignCells(pairs) { + const used = new Set(); + const assigned = new Map(); + for (const pair of pairs) { + const cell = !used.has(pair.from) ? pair.from : !used.has(pair.to) ? pair.to : 0; + if (!cell) continue; + used.add(cell); + assigned.set(cell, pair); + } + return assigned; +} + +// ------------------------------------------------------------------ 量过渡格 + +// 缩到 W×H 的灰度图,模糊 r 像素:比的是形状挪到了哪,不是纹理。三张图要缩到同一个 W×H 才能逐像素比 +// (过渡格按缩放对齐以后大小和格子差几个像素)。 +function thumb(img, W, H, r) { + const small = resizeArea(toRgb(img), W, H).data; + let g = new Float32Array(W * H); + for (let p = 0; p < g.length; p += 1) g[p] = 0.299 * small[p * 3] + 0.587 * small[p * 3 + 1] + 0.114 * small[p * 3 + 2]; + for (let pass = 0; pass < 2 && r > 0; pass += 1) { + for (const horizontal of [true, false]) { + const out = new Float32Array(g.length); + for (let y = 0; y < H; y += 1) { + for (let x = 0; x < W; x += 1) { + let sum = 0; + let n = 0; + for (let k = -r; k <= r; k += 1) { + const xx = horizontal ? x + k : x; + const yy = horizontal ? y : y + k; + if (xx < 0 || yy < 0 || xx >= W || yy >= H) continue; + sum += g[yy * W + xx]; + n += 1; + } + out[y * W + x] = sum / n; + } + } + g = out; + } + } + return g; +} + +function distance(a, b) { + let sum = 0; + for (let i = 0; i < a.length; i += 1) sum += Math.abs(a[i] - b[i]); + return a.length ? sum / a.length : 0; +} + +// own 是过渡格所在那一格(底图)、other 是另一头、tween 是过渡格,三张同样大小。 +// near:两头不一样的地方(按差多少加权),过渡格有多少更像另一头。照抄另一头接近 1,没改接近 0。 +// excess:到两头的距离之和 ÷ 两头之间的距离,正常的中间状态在 1 到 2 左右,画走了样会更大。 +export function tweenMetrics(own, other, tween) { + const W = 160; + const H = Math.max(1, Math.round((W * own.height) / own.width)); + const a = thumb(own, W, H, 1); + const b = thumb(other, W, H, 1); + const m = thumb(tween, W, H, 1); + let toOther = 0; + let total = 0; + for (let i = 0; i < a.length; i += 1) { + const w = Math.abs(b[i] - a[i]); + if (w < 6) continue; + total += w; + if (Math.abs(m[i] - b[i]) < Math.abs(m[i] - a[i])) toOther += w; + } + const H2 = Math.max(1, Math.round((128 * own.height) / own.width)); + const A = thumb(own, 128, H2, 3); + const B = thumb(other, 128, H2, 3); + const M = thumb(tween, 128, H2, 3); + const span = distance(A, B); + const near = total ? toOther / total : 0.5; + const excess = span > 0.01 ? (distance(A, M) + distance(M, B)) / span : 1; + let verdict = 'ok'; + if (near >= TWEEN.copyOther) verdict = 'copy'; + else if (near <= TWEEN.unchanged) verdict = 'unchanged'; + else if (excess > TWEEN.maxExcess) verdict = 'off'; + return { near: round2(near), excess: round2(excess), verdict }; +} + +const LETTERS = ['A', 'B', 'C', 'D']; + +const VERDICT_TEXT = { + copy: '照抄了另一格', + unchanged: '和原来那一格一样,没改', + off: '和前后两格都差得远,画走了样', +}; + +function round2(value) { + return Math.round(value * 100) / 100; +} + +// 从候选组图里切出画在第 cell 格的过渡格,对齐到原组图第 cell 格的人物上。fit 是别的格子量出来的整体偏移 +// (见 layoutFit),这一格自己没对上时从那里开始细调。 +function cutTween(origRgb, framing, candRgb, cell, fit) { + const ref = framing.frames[cell - 1]; + const sx = candRgb.width / origRgb.width; + const sy = candRgb.height / origRgb.height; + const cx = (ref.x + ref.w / 2) * sx; + const cy = (ref.y + ref.h / 2) * sy; + const guess = fit ? { cx: cx + fit.dx(cx), cy: cy + fit.dy(cy) } : { cx, cy }; + const frame = alignAcross(origRgb, ref, candRgb, guess); + return { frame, image: cropImage(candRgb, frame.x, frame.y, frame.w, frame.h) }; +} + +// 格子越往右、越往下,候选组图里的偏移越大(接口回的图比例不一样):用对上了的格子拟合「偏移 = a × 位置 + b」。 +function layoutFit(points) { + const line = (xs, ys) => { + if (!xs.length) return () => 0; + const distinct = new Set(xs.map((x) => Math.round(x))).size; + if (distinct < 2) { + const mean = ys.reduce((s, v) => s + v, 0) / ys.length; + return () => mean; + } + const n = xs.length; + const mx = xs.reduce((s, v) => s + v, 0) / n; + const my = ys.reduce((s, v) => s + v, 0) / n; + let num = 0; + let den = 0; + for (let i = 0; i < n; i += 1) { + num += (xs[i] - mx) * (ys[i] - my); + den += (xs[i] - mx) ** 2; + } + const slope = den > 1e-9 ? num / den : 0; + return (x) => my + slope * (x - mx); + }; + return { + dx: line(points.map((p) => p.cx), points.map((p) => p.dx)), + dy: line(points.map((p) => p.cy), points.map((p) => p.dy)), + }; +} + +// 一张候选组图里所有过渡格:先各自对齐,没对上的按对上了的格子拟合出来的偏移重新对一次。 +export function cutCandidate(origRgb, framing, keys, candRgb, assigned) { + const sx = candRgb.width / origRgb.width; + const sy = candRgb.height / origRgb.height; + const results = new Map(); + for (const [cell, pair] of assigned) results.set(cell, { pair, ...cutTween(origRgb, framing, candRgb, cell, null) }); + const points = [...results.entries()] + .filter(([, item]) => item.frame.matched) + .map(([cell, item]) => { + const ref = framing.frames[cell - 1]; + const cx = (ref.x + ref.w / 2) * sx; + const cy = (ref.y + ref.h / 2) * sy; + return { cx, cy, dx: item.frame.x + item.frame.w / 2 - cx, dy: item.frame.y + item.frame.h / 2 - cy }; + }); + if (points.length) { + const fit = layoutFit(points); + for (const [cell, item] of results) { + if (item.frame.matched) continue; + results.set(cell, { pair: item.pair, ...cutTween(origRgb, framing, candRgb, cell, fit) }); + } + } + for (const [cell, item] of results) { + const other = item.pair.from === cell ? item.pair.to : item.pair.from; + item.metrics = tweenMetrics(keys[cell - 1], keys[other - 1], item.image); + } + return results; +} + +// ------------------------------------------------------------------ 提示词 + +function cleanText(value) { + return String(value || '').replace(/[。;;,,\s]+$/, '').trim().slice(0, 300); +} + +export function tweenPrompt(grid, assigned, problems) { + const lines = []; + for (let cell = 1; cell <= grid.count; cell += 1) { + const pair = assigned.get(cell); + if (!pair) { + lines.push('第 ' + cell + ' 格:保持原样,不要改。'); + continue; + } + const problem = problems && problems.get(pair.key); + lines.push('第 ' + cell + ' 格:' + cleanText(pair.text) + '。' + (problem ? '(上一版这一格的问题:' + cleanText(problem) + '。这次改正。)' : '')); + } + return ( + '这是一张 ' + grid.rows + ' 行 ' + grid.cols + ' 列的 ' + grid.count + ' 格组图,按从左到右、从上到下的顺序,是同一个主体一步一步的变化。' + + '在这张图上改,画出每两格之间的过渡:每一格改成下面写的样子,只改主体在动的部分(表情、手和手臂、头的角度、身体的姿势);' + + '背景和背景里的东西、构图、主体在格子里的位置和大小、画风、线条和颜色都保持这一格原样;格子的排法和分隔线不变;' + + '不要加文字、序号、箭头和水印。\n' + lines.join('\n') + ); +} + +function stepLine(pair, index) { + return '第 ' + (index + 1) + ' 步:第 ' + pair.from + ' 格 → 第 ' + pair.to + ' 格(画在第 ' + pair.cell + ' 格的位置上)'; +} + +function planPrompt(session, image, grid, pairs, sheetPath, gridPath, budget) { + return [ + '你是H3-Image-Executor 的过渡格规划节点。用户要把一张分格组图拼成 GIF。格子之间一下一下地切换不够顺,', + '系统要在相邻两格之间各补一格「中间状态」:在原组图上改一次,把每一格改成它和下一格中间的样子。你来写每一格要改成什么样。', + '', + '用户原话:', + session.instruction, + '', + '这张图:' + (image.title || image.id) + ',' + grid.rows + ' 行 ' + grid.cols + ' 列共 ' + grid.count + ' 格,格子编号从上到下、从左到右数。', + '看图材料:' + sheetPath + '(先用 read 打开它:每一格单独放大,左上角是格子编号)。看不清再打开原图:' + gridPath, + '', + '要补的步子(按播放顺序):', + ...pairs.map((pair, index) => '- ' + stepLine(pair, index)), + '', + '每一步写一句:画在那一格位置上的中间状态长什么样。', + '- 写清每个在变的部位走到一半时的样子:表情(眉毛、眼睛、嘴型、眼泪)、手和手臂在哪、头的角度、身体的姿势。只写主体,', + ' 背景、构图和位置不用写,系统会要求保持那一格的原样。例:「双手握拳从胸前往上抬到下巴两侧,拳头刚要张开,还没贴到脸上;嘴张开大笑,眼睛开始发亮」。', + '- 写成这一格完整的样子,不要写「和第几格一样」「介于两格之间」这种话:生图模型不会去看别的格子。', + '- 两格不是同一个镜头里的连续动作(漫画换了场景、换了人物或机位),这一步写空字符串 "",不补。', + '- 不要让画面里出现文字、数字和序号。', + '', + '只输出一个 JSON 对象,不要解释、不要 markdown 代码块:', + '{"seen":true,"between":["…","…"],"summary":"一句话"}', + '- seen:打开看到了看图材料写 true;没看到写 false,between 留空。', + '- between 的条数和上面的步子一样多、顺序一样。', + '工具调用上限 ' + budget.limit + ' 次。', + ].join('\n'); +} + +function reviewPrompt(session, image, rows, sheetPath, candidates, gridPath, budget) { + return [ + '你是H3-Image-Executor 的过渡格评审节点。用户要把一张分格组图拼成 GIF,系统在相邻两格之间各补了一格中间状态', + '(在原组图上改出来的,一次出了 ' + candidates.length + ' 张候选),你来给每一步挑最好的一格。', + '', + '用户原话:', + session.instruction, + '', + '看图材料:' + sheetPath + '(先用 read 打开它,多数时候看它就够了)。每一行是一步:最左一张是前一格、最右一张是后一格(左上角是它们的格子编号),', + '中间是各候选画的过渡格(左上角是候选字母 ' + LETTERS.slice(0, candidates.length).join('、') + ')。拼图上看不清,再打开候选原图(同样排法的整张组图):', + ...candidates.map((candidate, index) => '候选 ' + LETTERS[index] + ' -> ' + candidate.path), + '原组图 -> ' + gridPath, + '', + '每一步要的中间状态:', + ...rows.map((row, index) => { + const notes = row.flags.map((flag, k) => (flag ? '候选 ' + LETTERS[k] + ' 程序判了' + flag + ',不要选' : '')).filter(Boolean); + return '- 第 ' + (index + 1) + ' 步(第 ' + row.pair.from + ' 格 → 第 ' + row.pair.to + ' 格,画在第 ' + row.pair.cell + ' 格的位置上):' + cleanText(row.pair.text) + (notes.length ? ';' + notes.join(';') : ''); + }), + '', + '逐步核对:', + '1. 是同一个主体:脸、发型、服装、配色、画风和前后两格一样,没有变成另一个样子,没有多出或少了肢体、手指。', + '2. 画的是两格中间的样子:在变的部位在前一格和后一格之间(有的部位已经变了、有的还没变也可以),不是照抄前一格或后一格,', + ' 没有往反方向走,也没有多出前后两格都没有的动作。', + '3. 构图、主体在格子里的位置和大小、背景和它所在那一格一样:播放时插在两格中间,背景不会跳。', + '4. 没有新加的文字、序号、箭头和水印。', + '每一步从候选里挑一张最好的;都不合格就不挑。', + '', + '只输出一个 JSON 对象,不要解释、不要 markdown 代码块:', + '{"seen":true,"steps":[{"step":1,"pick":"A","problem":""}],"summary":"一句话"}', + '- seen:打开看到了看图材料写 true;没看到写 false,steps 留空,不要凭空判。', + '- steps 里每一步写一项,step 是上面的步号,pick 是候选字母;都不合格写空字符串 "",problem 写哪里不对(下一轮会照着改)。', + '工具调用上限 ' + budget.limit + ' 次。', + ].join('\n'); +} + +// ------------------------------------------------------------------ 主流程 + +async function exists(path) { + try { + await access(path); + return true; + } catch { + return false; + } +} + +const SAFE = /^(?!\.{1,2}$)[A-Za-z0-9._-]+$/; +const RATIOS = ['1:1', '4:3', '3:4', '3:2', '2:3', '16:9', '9:16', '21:9']; + +function aspectFor(width, height) { + const ratio = width / height; + let best = 'auto'; + let bestError = 0.03; + for (const item of RATIOS) { + const [a, b] = item.split(':').map(Number); + const error = Math.abs(a / b - ratio) / ratio; + if (error < bestError) { + bestError = error; + best = item; + } + } + return best; +} + +// 过渡格只用在 GIF 里(最大 1024 像素):2K 出图切出来的一格够 GIF 用就出 2K,比 4K 快一半。 +function tweenArea(grid, framing, edge) { + const aspect = grid.width / grid.height; + const width2k = Math.sqrt(2048 * 2048 * aspect); + const panel2k = (framing.width * width2k) / grid.width; + const need = gifSize(framing.width, framing.height, edge).width; + return panel2k >= need ? '2K' : '4K'; +} + +// 准备组图:切格、对齐,切好的每一格(GIF 用的取景)。不是组图、格子大小不一的排法返回 null。 +export function prepareGrid(rgb, spec) { + const detection = detectPanels(rgb, { rows: spec.rows, cols: spec.cols, count: spec.count }); + if (detection.count < 2) return null; + const framing = framePanels(rgb, detection.cells, { align: spec.align !== false }); + if (framing.mode !== 'crop') return null; + const keys = framing.frames.map((frame) => cropImage(rgb, frame.x, frame.y, frame.w, frame.h)); + return { detection, framing, keys, grid: { rows: detection.rows, cols: detection.cols, count: detection.count, width: rgb.width, height: rgb.height } }; +} + +function freshState(prev, sourceId) { + return { sourceId, seq: (prev && prev.seq) || 0, rounds: 0, count: 0, state: '', pairs: [], reviews: [], note: '', planFailed: false, at: Date.now() }; +} + +// 这张组图需要补的过渡格都补上(已经补过、试过的不再补)。返回 image.panelTweens。 +// options.mode: +// make:在做 GIF(追问「变成 GIF」、规划时就要、组图改过了):缺的都补; +// adjust:只调了节奏(「慢一点」「去掉第 3 格」):这张组图补过过渡格的,才把调整后多出来的步子补上, +// 没补过的(以前做的 GIF、用户说过不要补)不在这时候突然生图; +// retry:用户说「更顺一点」:之前没补成的步子再试(还按每张组图最多两轮算)。 +// hooks:executeAction、describeError、isFatal、isContentPolicy、isCancelled、lastDelivered、queuePixelWork。 +export async function ensurePanelTweens(session, image, deps, hooks, options) { + const opts = options || {}; + const mode = opts.mode || 'make'; + const spec = image.spec && image.spec.panelGif; + const source = image.chosen || hooks.lastDelivered(session, image, deps); + if (!spec || spec.smooth === false || !source || !source.path) return image.panelTweens || null; + const own = image.panelTweens && image.panelTweens.sourceId === source.id ? image.panelTweens : null; + if (mode === 'adjust' && !(own && own.pairs.some((pair) => pair.status === 'passed' || pair.status === 'unreviewed'))) return image.panelTweens || null; + const state = own || freshState(image.panelTweens, source.id); + image.panelTweens = state; + if (mode === 'retry') { + state.pairs = state.pairs.filter((pair) => pair.status === 'passed' || pair.status === 'unreviewed'); + state.rounds = 0; + state.planFailed = false; + } + // 过渡格规划没跑成过:这张组图不再自动重试(每次追问都会走到这里),用户说「更顺一点」再试。 + if (state.planFailed) return state; + // 已经知道这张组图有几格、要补的步子都补过或试过了:不用再解码 4K 的组图。 + if (state.count && tweenPairs(state.count, spec).every((pair) => state.pairs.some((one) => one.key === pair.key))) return state; + let prepared; + let rgb; + try { + rgb = await hooks.queuePixelWork(async () => toRgb(decodeImage(await readFile(source.path)))); + prepared = prepareGrid(rgb, spec); + } catch (error) { + state.note = '读不了组图:' + hooks.describeError(error); + return state; + } + if (!prepared) return state; + const { framing, keys, grid } = prepared; + state.count = grid.count; + const known = new Set(state.pairs.map((pair) => pair.key)); + const wanted = tweenPairs(grid.count, spec).filter((pair) => !known.has(pair.key)); + if (!wanted.length) return state; + // 不是同一个镜头的两格不补:漫画换了场景,中间状态没有意义。 + const todo = []; + for (const pair of wanted) { + const change = changeShare(resizeArea(keys[pair.from - 1], 160, Math.max(1, Math.round((160 * framing.height) / framing.width))), resizeArea(keys[pair.to - 1], 160, Math.max(1, Math.round((160 * framing.height) / framing.width)))); + if (change > TWEEN.sceneChange) { + state.pairs.push({ ...pair, cell: 0, text: '', status: 'skipped', problem: '两格不是同一个镜头(整幅变了 ' + Math.round(change * 100) + '%),不补', candidateFile: '' }); + continue; + } + todo.push(pair); + } + if (!todo.length) return state; + if (state.rounds >= TWEEN.rounds) return state; + const started = Date.now(); + deps.emit(session, 'paint', image.id + ' 补过渡格:相邻两格之间各补一格中间状态(' + todo.length + ' 步),一次画出一整张组图,从候选里挑'); + // 1. 写每一步怎么改 + const cellsFor = assignCells(todo); + for (const [cell, pair] of cellsFor) pair.cell = cell; + const planned = todo.filter((pair) => pair.cell); + let texts; + try { + texts = await planTweens(session, image, deps, hooks, planned, prepared, source.path); + } catch (error) { + state.note = '没写出每一步怎么改(' + hooks.describeError(error) + '),这次不补'; + state.planFailed = true; + deps.emit(session, 'paint', image.id + ' 过渡格没补:' + state.note, { level: 'warn' }); + return state; + } + if (hooks.isCancelled(session)) return state; + if (!texts) { + state.note = '过渡格规划没给出每一步怎么改,这次不补'; + state.planFailed = true; + deps.emit(session, 'paint', image.id + ' 过渡格没补:' + state.note, { level: 'warn' }); + return state; + } + const pending = []; + planned.forEach((pair, index) => { + const value = cleanText(texts[index]); + if (!value) { + state.pairs.push({ ...pair, text: '', status: 'skipped', problem: '过渡格规划说这两格不是连续的动作,不补', candidateFile: '' }); + return; + } + pending.push({ ...pair, text: value, status: 'pending', problem: '', candidateFile: '', near: 0, excess: 0 }); + }); + // 2. 一轮一轮出图、挑 + const problems = new Map(); + while (pending.some((pair) => pair.status === 'pending') && state.rounds < TWEEN.rounds && !hooks.isCancelled(session)) { + const open = pending.filter((pair) => pair.status === 'pending'); + const assigned = new Map(open.map((pair) => [pair.cell, pair])); + state.rounds += 1; + const round = await tweenRound(session, image, deps, hooks, { state, prepared, rgb, source, spec, assigned, problems }); + if (!round) break; + for (const pair of open) { + const pick = round.picks.get(pair.key); + if (!pick) continue; + Object.assign(pair, pick); + } + if (round.stopped) break; + } + for (const pair of pending) { + if (pair.status === 'pending') { + pair.status = 'failed'; + if (!pair.problem) pair.problem = problems.get(pair.key) || '没挑到能用的过渡格'; + } + state.pairs.push(pair); + } + state.state = 'done'; + state.at = Date.now(); + const used = pending.filter((pair) => pair.status === 'passed' || pair.status === 'unreviewed'); + deps.emit( + session, + 'paint', + image.id + ' 过渡格补好了:' + used.length + '/' + pending.length + ' 步挑到了能用的' + (used.length < pending.length ? ',其余的两格之间直接切换' : ''), + { + durMs: Date.now() - started, + level: used.length < pending.length ? 'warn' : 'info', + detail: detail('过渡格 · ' + image.id, [ + list( + '每一步', + pending.map((pair) => ({ + tag: pair.status === 'passed' || pair.status === 'unreviewed' ? 'pass' : 'fail', + text: '第 ' + pair.from + ' 格 → 第 ' + pair.to + ' 格:' + (pair.status === 'failed' ? pair.problem : '用 ' + pair.candidateId + (pair.status === 'unreviewed' ? '(评审没跑成,按程序量的挑)' : '')), + note: pair.text, + })), + ), + ]), + }, + ); + return state; +} + +// 过渡格规划:看着组图写每一步怎么改。返回和 pairs 一样长的数组;没写出来返回 null。 +async function planTweens(session, image, deps, hooks, pairs, prepared, gridPath) { + const { framing, keys, grid } = prepared; + await deps.ensureDir(deps.dirs.sheets); + const sheetPath = join(deps.dirs.sheets, image.id + '-tweenplan-' + Date.now() + '.png'); + const sheet = renderGridSheet(keys.map((key, i) => ({ image: key, label: String(i + 1) })), { + perRow: grid.cols, + width: Math.min(1800, grid.cols * 480), + fill: 246, + cellAspect: framing.width / framing.height, + }); + await writeFile(sheetPath, encodePng(sheet, { level: 3 })); + const budget = toolBudget('tweenplan', { + needs: [{ count: 1, what: '看图材料' }, { count: 1, what: '原组图' }], + difficulty: difficultyOf(image).level, + hits: hitsOf(image, 'tweenplan'), + }); + const result = await deps.agent.runJson(session, { step: 'tweenplan', instruction: planPrompt(session, image, grid, pairs, sheetPath, gridPath, budget), maxTools: budget.limit, budgetNote: budget.note }); + noteToolHit(image, 'tweenplan', result); + const data = result && result.data; + // 多写了的不要,少写了的整个不用:对不上是哪一步写的哪一句。 + if (!data || data.seen === false || !Array.isArray(data.between) || data.between.length < pairs.length) return null; + return data.between.slice(0, pairs.length).map((value) => (typeof value === 'string' ? value : '')); +} + +// 出一轮:在原组图上改一次(2 张候选),切出每一格过渡格、程序筛、评审挑。返回 { picks: Map(key → 结果), stopped }。 +async function tweenRound(session, image, deps, hooks, ctx) { + const { state, prepared, rgb, source, spec, assigned, problems } = ctx; + const { framing, keys, grid } = prepared; + const proxy = { + id: image.id + 'T', + spec: { + aspectRatio: (image.spec.aspectRatio && image.spec.aspectRatio !== 'auto' ? image.spec.aspectRatio : '') || aspectFor(grid.width, grid.height), + area: tweenArea(grid, framing, spec.edge), + size: '', + refs: [], + characterRefs: [], + candidates: TWEEN.candidates, + }, + rounds: [{ index: 0 }], + roundBase: state.seq, + }; + state.seq += 1; + deps.emit(session, 'paint', image.id + ' 过渡格第 ' + state.rounds + ' 轮:在 ' + source.id + ' 上一次改出 ' + assigned.size + ' 格(' + TWEEN.candidates + ' 张候选、' + proxy.spec.area + ')'); + let candidates; + try { + const mm = await h3.resolveReference(deps.config, source.path); + const action = { type: 'edit', prompt: tweenPrompt(grid, assigned, problems), refs: [mm], parentId: null }; + candidates = await hooks.executeAction(session, proxy, action, deps); + } catch (error) { + const reason = hooks.describeError(error); + for (const pair of assigned.values()) problems.set(pair.key, '这一轮没画出来:' + reason); + deps.emit(session, 'paint', image.id + ' 过渡格没画出来:' + reason, { level: 'warn' }); + // 接口配置错误、内容审核拒绝:再出一轮也一样,停下。不记成会话失败:过渡格是附带的,GIF 照样交付,只是不补。 + if (hooks.isFatal(error) || hooks.isContentPolicy(error)) return { picks: new Map(), stopped: true }; + return { picks: new Map(), stopped: false }; + } + if (!candidates.length) return { picks: new Map(), stopped: false }; + // 切格、对齐、量 + const cut = []; + for (const candidate of candidates) { + try { + const candRgb = await hooks.queuePixelWork(async () => toRgb(decodeImage(candidate.buffer || (await readFile(candidate.path))))); + if (Math.abs(candRgb.width / candRgb.height - grid.width / grid.height) / (grid.width / grid.height) > 0.04) { + cut.push({ candidate, results: null, problem: '画幅和原组图不一样' }); + continue; + } + cut.push({ candidate, results: cutCandidate(rgb, framing, keys, candRgb, assigned) }); + } catch (error) { + cut.push({ candidate, results: null, problem: '解不开:' + hooks.describeError(error) }); + } + } + // 每一步的候选和程序判断 + const rows = []; + for (const [cell, pair] of assigned) { + const flags = cut.map((item) => { + if (!item.results) return item.problem || '解不开'; + const got = item.results.get(cell); + return got && got.metrics.verdict !== 'ok' ? VERDICT_TEXT[got.metrics.verdict] : ''; + }); + rows.push({ cell, pair, flags }); + } + const reviewable = rows.filter((row) => row.flags.some((flag) => !flag)); + const verdicts = reviewable.length ? await reviewTweens(session, image, deps, hooks, reviewable, cut, prepared, source.path) : null; + const picks = new Map(); + for (const row of rows) { + const usable = cut.map((item, k) => ({ item, k })).filter(({ k }) => !row.flags[k]); + if (!usable.length) { + problems.set(row.pair.key, row.flags.filter(Boolean)[0] || '没画出来'); + continue; + } + const verdict = verdicts && verdicts.get(row.pair.key); + let chosen = null; + let status = 'passed'; + if (verdict && verdict.pick > 0) { + chosen = usable.find(({ k }) => k === verdict.pick - 1) || null; + if (!chosen) { + problems.set(row.pair.key, '评审挑的候选 ' + LETTERS[verdict.pick - 1] + ' 程序判了不能用'); + continue; + } + } else if (verdict && verdict.pick === 0) { + problems.set(row.pair.key, verdict.problem || '评审判两张候选都不合格'); + continue; + } else { + // 评审没跑成:挑程序量出来最像中间状态的(near 最接近 0.5,走样最少)。 + status = 'unreviewed'; + chosen = usable + .map((entry) => ({ ...entry, metrics: entry.item.results.get(row.cell).metrics })) + .sort((x, y) => Math.abs(x.metrics.near - 0.5) - Math.abs(y.metrics.near - 0.5) || x.metrics.excess - y.metrics.excess)[0]; + } + const got = chosen.item.results.get(row.cell); + picks.set(row.pair.key, { + status, + problem: '', + candidateId: chosen.item.candidate.id, + candidateFile: chosen.item.candidate.file, + near: got.metrics.near, + excess: got.metrics.excess, + }); + } + return { picks, stopped: false }; +} + +// 过渡格评审:一张对比图,每一行一步:前一格、各候选、后一格。返回 Map(key → { pick, problem });没跑成返回 null。 +async function reviewTweens(session, image, deps, hooks, rows, cut, prepared, gridPath) { + const { framing, keys } = prepared; + const cellW = TWEEN.sheetCell; + const cellH = Math.max(1, Math.round((cellW * framing.height) / framing.width)); + const blank = { width: cellW, height: cellH, channels: 3, data: Buffer.alloc(cellW * cellH * 3, 200) }; + const entries = []; + for (const row of rows) { + entries.push({ image: renderPanel(keys[row.pair.from - 1], { x: 0, y: 0, w: framing.width, h: framing.height }, cellW, cellH, {}), label: String(row.pair.from) }); + cut.forEach((item, k) => { + const got = item.results && item.results.get(row.cell); + entries.push({ image: got ? renderPanel(got.image, { x: 0, y: 0, w: got.image.width, h: got.image.height }, cellW, cellH, {}) : blank, label: LETTERS[k] }); + }); + entries.push({ image: renderPanel(keys[row.pair.to - 1], { x: 0, y: 0, w: framing.width, h: framing.height }, cellW, cellH, {}), label: String(row.pair.to) }); + } + await deps.ensureDir(deps.dirs.sheets); + const sheetPath = join(deps.dirs.sheets, image.id + '-tweens-' + Date.now() + '.png'); + const perRow = cut.length + 2; + await writeFile(sheetPath, encodePng(renderGridSheet(entries, { perRow, width: perRow * cellW, fill: 246, cellAspect: cellW / cellH }), { level: 3 })); + const budget = toolBudget('tweens', { + needs: [{ count: 1, what: '对比图' }, { count: Math.ceil(cut.length / 2), what: '候选原图' }], + difficulty: difficultyOf(image).level, + hits: hitsOf(image, 'tweens'), + }); + const started = Date.now(); + let data; + try { + const reviewed = await deps.agent.runJson(session, { + step: 'tweens', + instruction: reviewPrompt(session, image, rows, sheetPath, cut.map((item) => item.candidate), gridPath, budget), + maxTools: budget.limit, + budgetNote: budget.note, + }); + noteToolHit(image, 'tweens', reviewed); + data = reviewed && reviewed.data; + } catch (error) { + deps.emit(session, 'judge', image.id + ' 过渡格评审没跑成:' + hooks.describeError(error) + ',按程序量的挑', { level: 'warn' }); + return null; + } + if (!data || data.seen === false || !Array.isArray(data.steps)) { + deps.emit(session, 'judge', image.id + ' 过渡格评审没看到图,按程序量的挑', { level: 'warn', durMs: Date.now() - started }); + return null; + } + const out = new Map(); + rows.forEach((row, index) => { + const verdict = data.steps.find((one) => one && Number(one.step) === index + 1); + if (!verdict) return; + // pick 写字母(也认数字:1 是 A);空的、0、认不出的都当没挑。 + const raw = String(verdict.pick == null ? '' : verdict.pick).trim().toUpperCase(); + const pick = LETTERS.indexOf(raw) >= 0 ? LETTERS.indexOf(raw) + 1 : /^\d+$/.test(raw) ? Number(raw) : 0; + out.set(row.pair.key, { pick: pick >= 0 && pick <= cut.length ? pick : 0, problem: String(verdict.problem || '').trim().slice(0, 200) }); + }); + const picked = [...out.values()].filter((one) => one.pick > 0).length; + deps.emit(session, 'judge', image.id + ' 过渡格评审:' + picked + '/' + rows.length + ' 步挑到了', { + durMs: Date.now() - started, + level: picked < rows.length ? 'warn' : 'info', + detail: detail('过渡格评审 · ' + image.id, [ + list('每一步', rows.map((row, index) => { + const one = out.get(row.pair.key); + return { tag: one && one.pick ? 'pass' : 'fail', text: '第 ' + (index + 1) + ' 步(' + row.pair.from + ' → ' + row.pair.to + '):' + (one ? (one.pick ? '用候选 ' + LETTERS[one.pick - 1] : one.problem || '都不合格') : '没给结论') }; + })), + data.summary ? text('总结', String(data.summary).slice(0, 300)) : null, + kv('对比图', basename(sheetPath)), + ]), + }); + return out; +} + +// ------------------------------------------------------------------ 合成用 + +// 拼 GIF 时要用的过渡格:Map(key → { image: 对齐好的那一格, cell: 画在哪一格上 })。从候选组图里重新切(切法改了也跟着变)。 +export async function loadTweens(image, source, rgb, prepared, deps) { + const state = image.panelTweens; + const out = new Map(); + if (!state || state.sourceId !== source.id || !prepared) return out; + const usable = state.pairs.filter((pair) => (pair.status === 'passed' || pair.status === 'unreviewed') && pair.cell && SAFE.test(String(pair.candidateFile || ''))); + const byFile = new Map(); + for (const pair of usable) byFile.set(pair.candidateFile, (byFile.get(pair.candidateFile) || []).concat([pair])); + for (const [file, pairs] of byFile) { + const path = join(deps.dirs.images, file); + if (!(await exists(path))) continue; + const candRgb = toRgb(decodeImage(await readFile(path))); + const assigned = new Map(pairs.map((pair) => [pair.cell, pair])); + const results = cutCandidate(rgb, prepared.framing, prepared.keys, candRgb, assigned); + for (const [cell, item] of results) out.set(item.pair.key, { image: item.image, cell }); + } + return out; +} + +export function tweenSignature(image, sourceId) { + const state = image.panelTweens; + if (!state || state.sourceId !== sourceId) return ''; + return state.pairs + .filter((pair) => pair.status === 'passed' || pair.status === 'unreviewed') + .map((pair) => pair.key + ':' + pair.candidateFile + '@' + pair.cell) + .sort() + .join(','); +} + +// 落盘:文件只存文件名。 +export function snapshotTweens(state) { + if (!state || typeof state !== 'object') return null; + return JSON.parse(JSON.stringify(state)); +} + +export function restoreTweens(raw) { + if (!raw || typeof raw !== 'object' || !Array.isArray(raw.pairs)) return null; + const out = JSON.parse(JSON.stringify(raw)); + out.seq = Number(raw.seq) || 0; + out.rounds = Number(raw.rounds) || 0; + out.count = Number(raw.count) || 0; + out.planFailed = Boolean(raw.planFailed); + out.pairs = raw.pairs + .filter((pair) => pair && typeof pair.key === 'string' && /^\d+-\d+$/.test(pair.key)) + .map((pair) => ({ + ...pair, + from: Number(pair.from) || 0, + to: Number(pair.to) || 0, + cell: Number(pair.cell) || 0, + candidateFile: SAFE.test(String(pair.candidateFile || '')) ? pair.candidateFile : '', + // 补到一半应用被关掉了:没挑完的步子下次再补。 + status: pair.status === 'pending' ? 'failed' : String(pair.status || 'failed'), + })); + return out; +} + diff --git a/plugins/liddalidd/h3-image-executor/miniapp/node/lib/png.mjs b/plugins/liddalidd/h3-image-executor/miniapp/node/lib/png.mjs index 9099d62a..127076ab 100644 --- a/plugins/liddalidd/h3-image-executor/miniapp/node/lib/png.mjs +++ b/plugins/liddalidd/h3-image-executor/miniapp/node/lib/png.mjs @@ -202,6 +202,11 @@ function crc32(buffer) { } // 色彩统计:通道均值、红/蓝相对增益、锐度代理(平均梯度能量)。 +// neutralRedGain:只在接近中性的像素(浅色背景、白墙、灰路)上量的红增益。整图均值分不清「整体偏红」和 +// 「主体本身就是红的」(橘猫、红裙子):后者整图 redGain 一样很高,中性的地方却是正的。 +// neutralShare 是这类像素占的比例,太少就量不准。 +// whiteRGB / whiteCast:只在接近白色的像素(白纸、白底)上量的平均色和通道差。白纸偏粉紫这种偏色, +// 整图均值看不出来:上面一大幅蓝色插图就把它盖过去了(真机 20261116-41535c,纸面 (245,242,248))。 export function analyze(image) { const { width, height, channels, data } = image; const step = Math.max(1, Math.floor((width * height) / 400000)); @@ -210,20 +215,42 @@ export function analyze(image) { let sumB = 0; let count = 0; let clipped = 0; + let neutralR = 0; + let neutralG = 0; + let neutralB = 0; + let neutral = 0; + let whiteR = 0; + let whiteG = 0; + let whiteB = 0; + let white = 0; for (let p = 0; p < width * height; p += step) { const i = p * channels; - if (channels === 1) { - sumR += data[i]; - sumG += data[i]; - sumB += data[i]; - } else if (channels === 2) { - sumR += data[i]; - sumG += data[i]; - sumB += data[i]; + let r; + let g; + let b; + if (channels < 3) { + r = g = b = data[i]; } else { - sumR += data[i]; - sumG += data[i + 1]; - sumB += data[i + 2]; + r = data[i]; + g = data[i + 1]; + b = data[i + 2]; + } + sumR += r; + sumG += g; + sumB += b; + const max = Math.max(r, g, b); + const min = Math.min(r, g, b); + if (max >= 40 && max - min <= max * 0.12) { + neutralR += r; + neutralG += g; + neutralB += b; + neutral += 1; + } + if (min >= 200 && max - min <= 24) { + whiteR += r; + whiteG += g; + whiteB += b; + white += 1; } if (data[i] > 250 || data[i + Math.min(1, channels - 1)] > 250) clipped += 1; count += 1; @@ -258,6 +285,11 @@ export function analyze(image) { clippedRatio: round(clipped / count, 4), sharpness: round(gcount ? gradient / gcount : 0, 3), balance: round(avg, 2), + neutralRedGain: neutral ? round(neutralR / Math.max(1, (neutralG + neutralB) / 2), 4) : 0, + neutralShare: round(neutral / count, 4), + whiteShare: round(white / count, 4), + whiteRGB: white ? [round(whiteR / white, 1), round(whiteG / white, 1), round(whiteB / white, 1)] : null, + whiteCast: white ? round(Math.max(whiteR, whiteG, whiteB) / white - Math.min(whiteR, whiteG, whiteB) / white, 2) : 0, }; } diff --git a/plugins/liddalidd/h3-image-executor/miniapp/node/lib/qrcode.mjs b/plugins/liddalidd/h3-image-executor/miniapp/node/lib/qrcode.mjs new file mode 100644 index 00000000..2dcbf9fc --- /dev/null +++ b/plugins/liddalidd/h3-image-executor/miniapp/node/lib/qrcode.mjs @@ -0,0 +1,449 @@ +// 二维码编码器(QR Code Model 2,ISO/IEC 18004)。纯 Node 实现,零第三方依赖。 +// +// 存在的理由:生图模型画不出能扫的二维码。它画的只是「长得像二维码」的黑白方块,解码器一个都扫不出来 +// (真机 20261111-606a7e:两张候选的码都扫不出内容,评审只核对了「像不像二维码」,照样放行)。 +// 所以图里要放二维码时,由程序生成真码,再贴回模型画的位置(见 qrfix.mjs)。 +// +// 支持版本 1–40、纠错等级 L/M/Q/H,以及数字、字母数字、字节(UTF-8)三种模式;整段内容只用一种模式。 +// 步骤按标准来:Reed-Solomon 纠错、分块交织,8 种掩码按罚分选最优。 + +const ECL = { + L: { ordinal: 0, formatBits: 1 }, + M: { ordinal: 1, formatBits: 0 }, + Q: { ordinal: 2, formatBits: 3 }, + H: { ordinal: 3, formatBits: 2 }, +}; +export const ECL_LEVELS = ['L', 'M', 'Q', 'H']; + +// 每块纠错码字数、纠错块数,按 [纠错等级][版本] 取;下标 0 不用。 +const ECC_CODEWORDS_PER_BLOCK = [ + [-1, 7, 10, 15, 20, 26, 18, 20, 24, 30, 18, 20, 24, 26, 30, 22, 24, 28, 30, 28, 28, 28, 28, 30, 30, 26, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30], + [-1, 10, 16, 26, 18, 24, 16, 18, 22, 22, 26, 30, 22, 22, 24, 24, 28, 28, 26, 26, 26, 26, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28], + [-1, 13, 22, 18, 26, 18, 24, 18, 22, 20, 24, 28, 26, 24, 20, 30, 24, 28, 28, 26, 30, 28, 30, 30, 30, 30, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30], + [-1, 17, 28, 22, 16, 22, 28, 26, 26, 24, 28, 24, 28, 22, 24, 24, 30, 28, 28, 26, 28, 30, 24, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30], +]; +const NUM_ERROR_CORRECTION_BLOCKS = [ + [-1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 4, 4, 6, 6, 6, 6, 7, 8, 8, 9, 9, 10, 12, 12, 12, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 24, 25], + [-1, 1, 1, 1, 2, 2, 4, 4, 4, 5, 5, 5, 8, 9, 9, 10, 10, 11, 13, 14, 16, 17, 17, 18, 20, 21, 23, 25, 26, 28, 29, 31, 33, 35, 37, 38, 40, 43, 45, 47, 49], + [-1, 1, 1, 2, 2, 4, 4, 6, 6, 8, 8, 8, 10, 12, 16, 12, 17, 16, 18, 21, 20, 23, 23, 25, 27, 29, 34, 34, 35, 38, 40, 43, 45, 48, 51, 53, 56, 59, 62, 65, 68], + [-1, 1, 1, 2, 4, 4, 4, 5, 6, 8, 8, 11, 11, 16, 16, 18, 16, 19, 21, 25, 25, 25, 34, 30, 32, 35, 37, 40, 42, 45, 48, 51, 54, 57, 60, 63, 66, 70, 74, 77, 81], +]; + +const MODES = { + numeric: { indicator: 0x1, countBits: [10, 12, 14] }, + alphanumeric: { indicator: 0x2, countBits: [9, 11, 13] }, + byte: { indicator: 0x4, countBits: [8, 16, 16] }, +}; +const ALPHANUMERIC = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:'; + +const PENALTY_N1 = 3; +const PENALTY_N2 = 3; +const PENALTY_N3 = 40; +const PENALTY_N4 = 10; + +// 生成一枚二维码。返回 { version, size, ecl, mask, mode, content, modules }, +// modules 是 size*size 的 Uint8Array,按行存,1 = 深色模块。 +// +// options.ecl:纠错等级,默认 M。boostEcl(默认开)会在不升版本的前提下把纠错等级往上提: +// 同样大小的码,纠错越高越耐脏、耐遮挡。 +export function encodeQr(text, options) { + const opts = options || {}; + const content = String(text === undefined || text === null ? '' : text); + if (!content) throw new Error('qr_empty_content'); + let ecl = ECL_LEVELS.includes(opts.ecl) ? opts.ecl : 'M'; + const minVersion = clampInt(opts.minVersion, 1, 40, 1); + const maxVersion = clampInt(opts.maxVersion, minVersion, 40, 40); + const segment = makeSegment(content); + + let version = minVersion; + let usedBits = 0; + for (;;) { + const countBits = charCountBits(segment.mode, version); + const capacity = getNumDataCodewords(version, ecl) * 8; + usedBits = segment.count < 2 ** countBits ? 4 + countBits + segment.bits.length : Infinity; + if (usedBits <= capacity) break; + if (version >= maxVersion) throw new Error('qr_content_too_long'); + version += 1; + } + if (opts.boostEcl !== false) { + for (const level of ['M', 'Q', 'H']) { + if (ECL[level].ordinal > ECL[ecl].ordinal && usedBits <= getNumDataCodewords(version, level) * 8) ecl = level; + } + } + + const bits = []; + appendBits(bits, segment.mode.indicator, 4); + appendBits(bits, segment.count, charCountBits(segment.mode, version)); + for (const bit of segment.bits) bits.push(bit); + const capacity = getNumDataCodewords(version, ecl) * 8; + appendBits(bits, 0, Math.min(4, capacity - bits.length)); + appendBits(bits, 0, (8 - (bits.length % 8)) % 8); + for (let pad = 0xec; bits.length < capacity; pad ^= 0xec ^ 0x11) appendBits(bits, pad, 8); + const data = new Array(bits.length / 8).fill(0); + bits.forEach((bit, index) => { + data[index >>> 3] |= bit << (7 - (index & 7)); + }); + + const size = version * 4 + 17; + const qr = { version, size, ecl, modules: new Uint8Array(size * size), isFunction: new Uint8Array(size * size) }; + drawFunctionPatterns(qr); + drawCodewords(qr, addEccAndInterleave(qr, data)); + + let mask = Number.isInteger(opts.mask) && opts.mask >= 0 && opts.mask <= 7 ? opts.mask : -1; + if (mask < 0) { + let best = Infinity; + for (let candidate = 0; candidate < 8; candidate += 1) { + applyMask(qr, candidate); + drawFormatBits(qr, candidate); + const penalty = getPenaltyScore(qr); + if (penalty < best) { + best = penalty; + mask = candidate; + } + applyMask(qr, candidate); + } + } + applyMask(qr, mask); + drawFormatBits(qr, mask); + return { version, size, ecl, mask, mode: segment.name, content, modules: qr.modules }; +} + +// 把码画成一张图(带静区),用于命令行的 make 子命令和测试。 +export function renderQr(qr, options) { + const opts = options || {}; + const modulePx = clampInt(opts.modulePx, 1, 64, 10); + const quiet = clampInt(opts.quiet, 0, 16, 4); + const dark = opts.dark || [0, 0, 0]; + const light = opts.light || [255, 255, 255]; + const side = (qr.size + quiet * 2) * modulePx; + const data = Buffer.alloc(side * side * 3); + for (let y = 0; y < side; y += 1) { + const my = Math.floor(y / modulePx) - quiet; + for (let x = 0; x < side; x += 1) { + const mx = Math.floor(x / modulePx) - quiet; + const isDark = mx >= 0 && my >= 0 && mx < qr.size && my < qr.size && qr.modules[my * qr.size + mx] === 1; + const color = isDark ? dark : light; + const o = (y * side + x) * 3; + data[o] = color[0]; + data[o + 1] = color[1]; + data[o + 2] = color[2]; + } + } + return { width: side, height: side, channels: 3, data }; +} + +// ---------------------------------------------------------------- 数据段 + +function makeSegment(text) { + const bits = []; + if (/^[0-9]+$/.test(text)) { + for (let i = 0; i < text.length; ) { + const n = Math.min(3, text.length - i); + appendBits(bits, Number(text.slice(i, i + n)), n * 3 + 1); + i += n; + } + return { mode: MODES.numeric, name: 'numeric', count: text.length, bits }; + } + if (/^[0-9A-Z $%*+\-./:]+$/.test(text)) { + let i = 0; + for (; i + 2 <= text.length; i += 2) { + appendBits(bits, ALPHANUMERIC.indexOf(text[i]) * 45 + ALPHANUMERIC.indexOf(text[i + 1]), 11); + } + if (i < text.length) appendBits(bits, ALPHANUMERIC.indexOf(text[i]), 6); + return { mode: MODES.alphanumeric, name: 'alphanumeric', count: text.length, bits }; + } + // 其余一律按 UTF-8 字节写。不加 ECI 声明:主流扫码器(微信、系统相机)都按 UTF-8 解, + // 反倒是带 ECI 的码有些老扫码器不认。 + const bytes = Buffer.from(text, 'utf8'); + for (const byte of bytes) appendBits(bits, byte, 8); + return { mode: MODES.byte, name: 'byte', count: bytes.length, bits }; +} + +function charCountBits(mode, version) { + return mode.countBits[Math.floor((version + 7) / 17)]; +} + +function appendBits(bits, value, length) { + if (length < 0 || length > 31 || value >>> length !== 0) throw new Error('qr_bits_out_of_range'); + for (let i = length - 1; i >= 0; i -= 1) bits.push((value >>> i) & 1); +} + +// ---------------------------------------------------------------- 容量 + +export function getNumRawDataModules(version) { + let result = (16 * version + 128) * version + 64; + if (version >= 2) { + const numAlign = Math.floor(version / 7) + 2; + result -= (25 * numAlign - 10) * numAlign - 55; + if (version >= 7) result -= 36; + } + return result; +} + +export function getNumDataCodewords(version, ecl) { + const o = ECL[ecl].ordinal; + return Math.floor(getNumRawDataModules(version) / 8) - ECC_CODEWORDS_PER_BLOCK[o][version] * NUM_ERROR_CORRECTION_BLOCKS[o][version]; +} + +// ---------------------------------------------------------------- 功能图形 + +function setFunctionModule(qr, x, y, dark) { + const index = y * qr.size + x; + qr.modules[index] = dark ? 1 : 0; + qr.isFunction[index] = 1; +} + +function drawFunctionPatterns(qr) { + const { size } = qr; + for (let i = 0; i < size; i += 1) { + setFunctionModule(qr, 6, i, i % 2 === 0); + setFunctionModule(qr, i, 6, i % 2 === 0); + } + drawFinderPattern(qr, 3, 3); + drawFinderPattern(qr, size - 4, 3); + drawFinderPattern(qr, 3, size - 4); + const positions = alignmentPatternPositions(qr.version); + const count = positions.length; + for (let i = 0; i < count; i += 1) { + for (let j = 0; j < count; j += 1) { + const onFinder = (i === 0 && j === 0) || (i === 0 && j === count - 1) || (i === count - 1 && j === 0); + if (!onFinder) drawAlignmentPattern(qr, positions[i], positions[j]); + } + } + drawFormatBits(qr, 0); + drawVersion(qr); +} + +function drawFinderPattern(qr, cx, cy) { + for (let dy = -4; dy <= 4; dy += 1) { + for (let dx = -4; dx <= 4; dx += 1) { + const x = cx + dx; + const y = cy + dy; + if (x < 0 || y < 0 || x >= qr.size || y >= qr.size) continue; + const dist = Math.max(Math.abs(dx), Math.abs(dy)); + setFunctionModule(qr, x, y, dist !== 2 && dist !== 4); + } + } +} + +function drawAlignmentPattern(qr, cx, cy) { + for (let dy = -2; dy <= 2; dy += 1) { + for (let dx = -2; dx <= 2; dx += 1) { + setFunctionModule(qr, cx + dx, cy + dy, Math.max(Math.abs(dx), Math.abs(dy)) !== 1); + } + } +} + +export function alignmentPatternPositions(version) { + if (version === 1) return []; + const count = Math.floor(version / 7) + 2; + const step = Math.floor((version * 8 + count * 3 + 5) / (count * 4 - 4)) * 2; + const result = [6]; + for (let pos = version * 4 + 17 - 7; result.length < count; pos -= step) result.splice(1, 0, pos); + return result; +} + +function drawFormatBits(qr, mask) { + const { size } = qr; + const data = (ECL[qr.ecl].formatBits << 3) | mask; + let rem = data; + for (let i = 0; i < 10; i += 1) rem = (rem << 1) ^ ((rem >>> 9) * 0x537); + const bits = ((data << 10) | rem) ^ 0x5412; + const bit = (i) => ((bits >>> i) & 1) === 1; + for (let i = 0; i <= 5; i += 1) setFunctionModule(qr, 8, i, bit(i)); + setFunctionModule(qr, 8, 7, bit(6)); + setFunctionModule(qr, 8, 8, bit(7)); + setFunctionModule(qr, 7, 8, bit(8)); + for (let i = 9; i < 15; i += 1) setFunctionModule(qr, 14 - i, 8, bit(i)); + for (let i = 0; i < 8; i += 1) setFunctionModule(qr, size - 1 - i, 8, bit(i)); + for (let i = 8; i < 15; i += 1) setFunctionModule(qr, 8, size - 15 + i, bit(i)); + setFunctionModule(qr, 8, size - 8, true); +} + +function drawVersion(qr) { + if (qr.version < 7) return; + let rem = qr.version; + for (let i = 0; i < 12; i += 1) rem = (rem << 1) ^ ((rem >>> 11) * 0x1f25); + const bits = (qr.version << 12) | rem; + for (let i = 0; i < 18; i += 1) { + const dark = ((bits >>> i) & 1) === 1; + const a = qr.size - 11 + (i % 3); + const b = Math.floor(i / 3); + setFunctionModule(qr, a, b, dark); + setFunctionModule(qr, b, a, dark); + } +} + +// ---------------------------------------------------------------- 纠错与码字排布 + +function addEccAndInterleave(qr, data) { + const o = ECL[qr.ecl].ordinal; + const numBlocks = NUM_ERROR_CORRECTION_BLOCKS[o][qr.version]; + const blockEccLen = ECC_CODEWORDS_PER_BLOCK[o][qr.version]; + const rawCodewords = Math.floor(getNumRawDataModules(qr.version) / 8); + const numShortBlocks = numBlocks - (rawCodewords % numBlocks); + const shortBlockLen = Math.floor(rawCodewords / numBlocks); + const divisor = reedSolomonDivisor(blockEccLen); + const blocks = []; + for (let i = 0, k = 0; i < numBlocks; i += 1) { + const block = data.slice(k, k + shortBlockLen - blockEccLen + (i < numShortBlocks ? 0 : 1)); + k += block.length; + const ecc = reedSolomonRemainder(block, divisor); + if (i < numShortBlocks) block.push(0); + blocks.push(block.concat(ecc)); + } + const result = []; + for (let i = 0; i < blocks[0].length; i += 1) { + blocks.forEach((block, j) => { + // 短块在数据段末尾补了一个占位字节,交织时跳过。 + if (i !== shortBlockLen - blockEccLen || j >= numShortBlocks) result.push(block[i]); + }); + } + return result; +} + +function reedSolomonDivisor(degree) { + const result = new Array(degree).fill(0); + result[degree - 1] = 1; + let root = 1; + for (let i = 0; i < degree; i += 1) { + for (let j = 0; j < result.length; j += 1) { + result[j] = gfMultiply(result[j], root); + if (j + 1 < result.length) result[j] ^= result[j + 1]; + } + root = gfMultiply(root, 0x02); + } + return result; +} + +function reedSolomonRemainder(data, divisor) { + const result = divisor.map(() => 0); + for (const byte of data) { + const factor = byte ^ result.shift(); + result.push(0); + divisor.forEach((coef, i) => { + result[i] ^= gfMultiply(coef, factor); + }); + } + return result; +} + +function gfMultiply(x, y) { + let z = 0; + for (let i = 7; i >= 0; i -= 1) { + z = (z << 1) ^ ((z >>> 7) * 0x11d); + z ^= ((y >>> i) & 1) * x; + } + return z; +} + +function drawCodewords(qr, data) { + const { size } = qr; + let i = 0; + for (let right = size - 1; right >= 1; right -= 2) { + if (right === 6) right = 5; + for (let vert = 0; vert < size; vert += 1) { + for (let j = 0; j < 2; j += 1) { + const x = right - j; + const upward = ((right + 1) & 2) === 0; + const y = upward ? size - 1 - vert : vert; + const index = y * size + x; + if (!qr.isFunction[index] && i < data.length * 8) { + qr.modules[index] = (data[i >>> 3] >>> (7 - (i & 7))) & 1; + i += 1; + } + } + } + } +} + +// ---------------------------------------------------------------- 掩码 + +function applyMask(qr, mask) { + const { size } = qr; + for (let y = 0; y < size; y += 1) { + for (let x = 0; x < size; x += 1) { + let invert; + switch (mask) { + case 0: invert = (x + y) % 2 === 0; break; + case 1: invert = y % 2 === 0; break; + case 2: invert = x % 3 === 0; break; + case 3: invert = (x + y) % 3 === 0; break; + case 4: invert = (Math.floor(x / 3) + Math.floor(y / 2)) % 2 === 0; break; + case 5: invert = ((x * y) % 2) + ((x * y) % 3) === 0; break; + case 6: invert = (((x * y) % 2) + ((x * y) % 3)) % 2 === 0; break; + case 7: invert = (((x + y) % 2) + ((x * y) % 3)) % 2 === 0; break; + default: throw new Error('qr_bad_mask'); + } + const index = y * size + x; + if (invert && !qr.isFunction[index]) qr.modules[index] ^= 1; + } + } +} + +function getPenaltyScore(qr) { + const { size, modules } = qr; + let result = 0; + for (let pass = 0; pass < 2; pass += 1) { + for (let a = 0; a < size; a += 1) { + let runColor = 0; + let run = 0; + const history = [0, 0, 0, 0, 0, 0, 0]; + for (let b = 0; b < size; b += 1) { + const color = pass === 0 ? modules[a * size + b] : modules[b * size + a]; + if (color === runColor) { + run += 1; + if (run === 5) result += PENALTY_N1; + else if (run > 5) result += 1; + } else { + addHistory(run, history, size); + if (!runColor) result += countFinderLike(history) * PENALTY_N3; + runColor = color; + run = 1; + } + } + result += terminateAndCount(runColor, run, history, size) * PENALTY_N3; + } + } + for (let y = 0; y < size - 1; y += 1) { + for (let x = 0; x < size - 1; x += 1) { + const color = modules[y * size + x]; + if (color === modules[y * size + x + 1] && color === modules[(y + 1) * size + x] && color === modules[(y + 1) * size + x + 1]) { + result += PENALTY_N2; + } + } + } + let dark = 0; + for (const value of modules) dark += value; + const total = size * size; + const k = Math.ceil(Math.abs(dark * 20 - total * 10) / total) - 1; + result += k * PENALTY_N4; + return result; +} + +function addHistory(run, history, size) { + history.pop(); + history.unshift(history[0] === 0 ? run + size : run); +} + +function countFinderLike(history) { + const n = history[1]; + const core = n > 0 && history[2] === n && history[3] === n * 3 && history[4] === n && history[5] === n; + return (core && history[0] >= n * 4 && history[6] >= n ? 1 : 0) + (core && history[6] >= n * 4 && history[0] >= n ? 1 : 0); +} + +function terminateAndCount(runColor, run, history, size) { + let length = run; + if (runColor) { + addHistory(length, history, size); + length = 0; + } + addHistory(length + size, history, size); + return countFinderLike(history); +} + +function clampInt(value, min, max, fallback) { + const n = Number(value); + if (!Number.isInteger(n)) return fallback; + return n < min ? min : n > max ? max : n; +} diff --git a/plugins/liddalidd/h3-image-executor/miniapp/node/lib/qrfix.mjs b/plugins/liddalidd/h3-image-executor/miniapp/node/lib/qrfix.mjs new file mode 100644 index 00000000..bbfab83f --- /dev/null +++ b/plugins/liddalidd/h3-image-executor/miniapp/node/lib/qrfix.mjs @@ -0,0 +1,1125 @@ +// 二维码修复:找到生图模型画的「假码」,生成真码贴回同一位置,再从最终像素里把它扫回来。 +// +// 为什么要有这一步:生图模型画不出能扫的二维码,它画的只是「长得像二维码」的黑白方块 +// (真机 20261111-606a7e:两张候选的码 jsQR 一个都扫不出,评审只核对了「像不像二维码」就放行)。 +// 所以图里要放二维码时,由程序生成真码(qrcode.mjs),贴回模型画的位置,再扫码验证。 +// +// 步骤: +// 1. 定位:先让 jsQR 扫一遍(图里已经有能扫的码就直接用它的四角);扫不出的, +// 自己找三个角的回字形定位方块(1:1:3:1:1 的黑白比例),挑出能拼成直角等腰三角形的三个。 +// 2. 求四角:沿三个定位方块的外边缘取点、拟合四条边线,边线相交得到透视四角; +// 拟合不出来时退回按三个定位方块推出的平行四边形。 +// 3. 取光照:在码区和四周留白里取样,按亮度分成浅色、深色两组,各拟合一张二次曲面, +// 贴上去的真码跟着原来的明暗走(上亮下暗、偏色、阴影都保留)。 +// 4. 贴码:把假码连同一圈边缘抹成浅色,按透视把真码的每个模块画进去,边缘抗锯齿、外圈羽化。 +// 5. 验证:只认扫码结果,而且只认最终文件:按原格式编码成 JPEG/PNG,再从这份字节重新解码, +// 在三种裁切范围下各扫「近看」(原分辨率)和「远看」(缩到每个模块约 2 像素)一次, +// 近看、远看都至少两次解出要放的内容才算通过,任何一次解出别的内容直接判不通过。 +// 多看几次是因为 jsQR 对裁切范围敏感:同一枚码换个边距就可能扫不出(实测过), +// 只扫一次会把好码误判成坏码;而要求「多数视角都扫得出」才算稳。 +import { encodeQr } from './qrcode.mjs'; +import { scanQr, scanAllQr } from './qrscan.mjs'; +import { decodeImage, sniffFormat } from './image.mjs'; +import { encodePng } from './png.mjs'; +import { encodeJpeg } from './jpegenc.mjs'; + +// 找定位方块时用的工作分辨率(长边)。4K 原图直接扫太慢,2048 下一个模块仍有 3 像素以上, +// 前提是码的边长不小于原图长边的约 1/25。 +const WORK_EDGE = 2048; +// 贴回去之后每个模块至少要占这么多像素,再小就算扫得出也是碰运气。 +export const MIN_MODULE_PX = 4; +// 真码四周要留的静区(模块数)。标准要求 4;原图白边不够时把码缩小一点、从码区里让出来, +// 3 是实测在深色底上也扫得稳的下限(2 模块时 jsQR 换几个边距就有扫不出的)。 +const QUIET_MODULES = 3; +// 编码最终 JPEG 的质量。原图本身就是 JPEG,再压一次用高质量,肉眼看不出二次压缩。 +const JPEG_QUALITY = 93; + +export const QR_REASONS = { + not_found: '没在图里找到二维码(找不到三个角的定位方块)', + too_small: '图里的二维码太小,装不下这段内容', + unscannable: '贴上真码后扫不出来', + mismatch: '贴上真码后扫出来的内容不对', + encode_failed: '内容太长,生成不了二维码', +}; + +// 让出事件循环:解码、编码 4K 图各要一两秒,连着跑会让面板的轮询请求排队,看起来像卡死。 +const tick = () => new Promise((resolve) => setImmediate(resolve)); + +// ---------------------------------------------------------------- 对外入口 + +// 在一张已解码的图上把码贴好(不验证,验证在 fixQrInImage 里对最终字节做)。contents:要放的内容, +// 一条时对应图里最大的那枚码,多条时按从上到下、从左到右对应。 +// 返回 { image, changed, results },results 每项对应 contents 里的一条。 +export function fixQrCodes(image, contents, options) { + const opts = options || {}; + const wanted = (Array.isArray(contents) ? contents : [contents]).map((item) => String(item || '')).filter(Boolean); + const results = []; + if (!wanted.length) return { image, changed: false, results, regions: [] }; + const regions = locateQrCodes(image, { max: Math.max(4, wanted.length + 2) }); + const assigned = assignRegions(regions, wanted.length); + let out = image; + let changed = false; + for (let index = 0; index < wanted.length; index += 1) { + const content = wanted[index]; + const region = assigned[index]; + if (!region) { + results.push(failure(content, 'not_found')); + continue; + } + if (region.decoded !== null && region.decoded === content) { + // 图里本来就是一枚能扫、内容也对的码(比如上一轮已经贴过),不再动它,交给 finishQr 照样验一遍。 + results.push({ + content, + ok: true, + changed: false, + reason: '', + reasonText: '', + version: region.version || 0, + ecl: '', + modules: region.version ? region.version * 4 + 17 : 0, + modulePx: round2(region.modulePx), + corners: roundCorners(region.corners), + eraseCorners: roundCorners(region.corners), + scanned: content, + source: region.source, + }); + continue; + } + if (out === image) out = { ...image, data: Buffer.from(image.data) }; + const pasted = pasteQr(out, region, content, opts); + if (!pasted.ok) { + results.push({ ...failure(content, pasted.reason), detail: pasted.detail || '', source: region.source }); + continue; + } + changed = true; + results.push({ + content, + ok: true, + changed: true, + reason: '', + reasonText: '', + version: pasted.qr.version, + ecl: pasted.qr.ecl, + modules: pasted.qr.size, + modulePx: round2(pasted.modulePx), + corners: roundCorners(pasted.codeCorners), + eraseCorners: roundCorners(pasted.eraseCorners), + scanned: null, + source: region.source, + }); + } + return { image: out, changed, results, regions }; +} + +// 已解码的图进、最终字节出:贴码 → 按原格式编码 → 从编码后的字节重新解码 → 逐枚扫码验证。 +// format 是原图格式('png' 时输出 PNG,其余一律 JPEG)。没有任何一枚码需要改时不重新编码, +// 但照样在原图上验一遍,返回的 buffer 为 null,表示「沿用原文件」。 +export async function fixQrInImage(image, format, contents, options) { + const fixed = fixQrCodes(image, contents, options); + await tick(); + let decoded = image; + let buffer = null; + let outFormat = format; + const pending = fixed.results.filter((item) => item.ok); + if (fixed.changed && pending.length) { + outFormat = format === 'png' ? 'png' : 'jpeg'; + buffer = outFormat === 'png' ? encodePng(fixed.image, { level: 6 }) : encodeJpeg(fixed.image, { quality: JPEG_QUALITY }); + await tick(); + decoded = decodeImage(buffer); + await tick(); + } + for (const result of pending) { + const check = verifyPasted(decoded, result, result.content); + result.ok = check.ok; + result.scanned = check.scanned; + result.scans = check.scans; + result.reason = check.ok ? '' : check.reason; + result.reasonText = check.ok ? '' : QR_REASONS[check.reason]; + result.verifiedOn = buffer ? outFormat : 'original'; + } + const ok = allOk(fixed.results); + // 有一枚没过就不交出新文件:贴了一半的图既不是原图也不是可用的图。 + if (!ok) return { buffer: null, format, decoded: image, changed: false, results: fixed.results, ok }; + return { buffer, format: outFormat, decoded, changed: Boolean(buffer), results: fixed.results, ok }; +} + +// 字节进、字节出,给命令行用。 +export async function fixQrInBuffer(buffer, contents, options) { + const format = sniffFormat(buffer); + const image = decodeImage(buffer); + await tick(); + const result = await fixQrInImage(image, format, contents, options); + return { ...result, buffer: result.buffer || buffer }; +} + +function allOk(results) { + return results.length > 0 && results.every((item) => item.ok); +} + +function failure(content, reason) { + return { + content, + ok: false, + changed: false, + reason, + reasonText: QR_REASONS[reason] || reason, + version: 0, + ecl: '', + modules: 0, + modulePx: 0, + corners: null, + scanned: null, + }; +} + +// ---------------------------------------------------------------- 定位 + +// 找图里所有二维码(能扫的、扫不出的都算)。返回 [{ corners: [左上, 右上, 右下, 左下], modulePx, +// modules, decoded, version, quiet, source }],按面积从大到小。 +export function locateQrCodes(image, options) { + const opts = options || {}; + const max = Math.max(1, Math.min(8, opts.max || 4)); + const regions = []; + + // 先扫能扫的码:jsQR 给的四角就是码的外边界,比自己拟合准。 + // 只认正色(深色码印在浅底上):反色码多数手机扫不了,验证也只按正色扫。 + const work = Math.min(1, 1600 / Math.max(image.width, image.height)); + for (const hit of scanAllQr(image, { max, maxEdge: work < 1 ? 1600 : undefined, invert: false })) { + const side = quadSide(hit.corners); + const modules = hit.version * 4 + 17; + regions.push({ + corners: hit.corners, + modulePx: side / modules, + modules, + decoded: hit.data, + version: hit.version, + source: 'decoded', + }); + } + + const gray = toGray(image, WORK_EDGE); + const binary = binarize(gray); + let triples = pickTriples(findFinderCandidates(binary, gray.width, gray.height, false), binary, gray.width, gray.height, max); + // 一组都没找到时再按「四周没有白边」放宽找一遍。放宽会多出不少误报,所以只在严格找不到时用。 + if (!triples.length && !regions.length) { + triples = pickTriples(findFinderCandidates(binary, gray.width, gray.height, true), binary, gray.width, gray.height, max); + } + for (const triple of triples) { + const region = refineRegion(image, triple, gray.scale); + if (!region) continue; + // 和已经扫出来的码重合的,以扫出来的为准。 + if (regions.some((other) => overlap(other.corners, region.corners) > 0.3)) continue; + regions.push({ ...region, decoded: null, version: 0, source: 'finder' }); + } + for (const region of regions) region.quiet = measureQuiet(image, region); + return regions.sort((a, b) => quadArea(b.corners) - quadArea(a.corners)).slice(0, max); +} + +// 一条内容时取最大的那枚码;多条时按从上到下、从左到右对应。 +function assignRegions(regions, count) { + if (!regions.length) return []; + if (count === 1) return [regions[0]]; + const picked = regions.slice(0, count); + const rowTolerance = Math.max(...picked.map((region) => quadSide(region.corners))) * 0.5; + return picked.sort((a, b) => { + const ca = center(a.corners); + const cb = center(b.corners); + if (Math.abs(ca.y - cb.y) > rowTolerance) return ca.y - cb.y; + return ca.x - cb.x; + }); +} + +// 缩成灰度工作图(盒式滤波,细小模块不会被漏掉)。 +function toGray(image, maxEdge) { + const scale = Math.min(1, maxEdge / Math.max(image.width, image.height)); + const width = Math.max(1, Math.round(image.width * scale)); + const height = Math.max(1, Math.round(image.height * scale)); + const { channels, data } = image; + const out = new Uint8Array(width * height); + const sx = image.width / width; + const sy = image.height / height; + for (let y = 0; y < height; y += 1) { + const y0 = Math.floor(y * sy); + const y1 = Math.max(y0 + 1, Math.min(image.height, Math.floor((y + 1) * sy))); + for (let x = 0; x < width; x += 1) { + const x0 = Math.floor(x * sx); + const x1 = Math.max(x0 + 1, Math.min(image.width, Math.floor((x + 1) * sx))); + let sum = 0; + let n = 0; + for (let yy = y0; yy < y1; yy += 1) { + let i = (yy * image.width + x0) * channels; + for (let xx = x0; xx < x1; xx += 1, i += channels) { + sum += channels >= 3 ? (data[i] * 77 + data[i + 1] * 150 + data[i + 2] * 29) >> 8 : data[i]; + n += 1; + } + } + out[y * width + x] = sum / n; + } + } + return { width, height, data: out, scale: width / image.width }; +} + +// Sauvola 局部阈值:平坦区域(均匀的深色背景、纯白底)不会被噪点切成碎块, +// 高对比区域阈值贴近局部均值。返回 1 = 深色。 +function binarize(gray) { + const { width, height, data } = gray; + const stride = width + 1; + const sum = new Float64Array(stride * (height + 1)); + const sq = new Float64Array(stride * (height + 1)); + for (let y = 0; y < height; y += 1) { + let rowSum = 0; + let rowSq = 0; + for (let x = 0; x < width; x += 1) { + const v = data[y * width + x]; + rowSum += v; + rowSq += v * v; + sum[(y + 1) * stride + x + 1] = sum[y * stride + x + 1] + rowSum; + sq[(y + 1) * stride + x + 1] = sq[y * stride + x + 1] + rowSq; + } + } + const radius = Math.max(8, Math.round(Math.min(width, height) / 16)); + const out = new Uint8Array(width * height); + for (let y = 0; y < height; y += 1) { + const y0 = Math.max(0, y - radius); + const y1 = Math.min(height, y + radius + 1); + for (let x = 0; x < width; x += 1) { + const x0 = Math.max(0, x - radius); + const x1 = Math.min(width, x + radius + 1); + const n = (x1 - x0) * (y1 - y0); + const s = sum[y1 * stride + x1] - sum[y0 * stride + x1] - sum[y1 * stride + x0] + sum[y0 * stride + x0]; + const q = sq[y1 * stride + x1] - sq[y0 * stride + x1] - sq[y1 * stride + x0] + sq[y0 * stride + x0]; + const mean = s / n; + const std = Math.sqrt(Math.max(0, q / n - mean * mean)); + const threshold = mean * (1 + 0.25 * (std / 128 - 1)); + out[y * width + x] = data[y * width + x] < threshold ? 1 : 0; + } + } + return out; +} + +// 1:1:3:1:1 的比例判定。模型画的定位方块比例不准,容差比标准解码器放宽一些。 +// relaxed:码直接画在深色底上、四周没有白边时,最外圈黑框会和背景连成一片, +// 这时只要求最外两段「至少有半个模块宽」,模块大小改由中间三段推算。 +function finderRatio(runs, relaxed) { + for (const run of runs) if (!run) return 0; + const module = relaxed ? (runs[1] + runs[2] + runs[3]) / 5 : runs.reduce((a, b) => a + b, 0) / 7; + if (module < 1) return 0; + const variance = module * 0.6; + const outer = relaxed + ? runs[0] > module * 0.5 && runs[4] > module * 0.5 + : Math.abs(runs[0] - module) < variance && Math.abs(runs[4] - module) < variance; + const ok = + outer && + Math.abs(runs[1] - module) < variance && + Math.abs(runs[2] - 3 * module) < 3 * variance && + Math.abs(runs[3] - module) < variance; + return ok ? module : 0; +} + +// 沿一条直线(行、列)从中心往两边数黑白段,返回 [外黑, 白, 中心黑, 白, 外黑] 与中心坐标。 +function crossCheck(binary, width, height, cx, cy, dx, dy, maxRun, relaxed) { + const at = (x, y) => (x < 0 || y < 0 || x >= width || y >= height ? -1 : binary[y * width + x]); + if (at(cx, cy) !== 1) return null; + const runs = [0, 0, 0, 0, 0]; + let x = cx; + let y = cy; + while (at(x, y) === 1 && runs[2] <= maxRun) { runs[2] += 1; x -= dx; y -= dy; } + while (at(x, y) === 0 && runs[1] <= maxRun) { runs[1] += 1; x -= dx; y -= dy; } + while (at(x, y) === 1 && runs[0] <= maxRun) { runs[0] += 1; x -= dx; y -= dy; } + const back = runs[2]; + x = cx + dx; + y = cy + dy; + while (at(x, y) === 1 && runs[2] <= maxRun * 2) { runs[2] += 1; x += dx; y += dy; } + while (at(x, y) === 0 && runs[3] <= maxRun) { runs[3] += 1; x += dx; y += dy; } + while (at(x, y) === 1 && runs[4] <= maxRun) { runs[4] += 1; x += dx; y += dy; } + const module = finderRatio(runs, relaxed); + if (!module) return null; + // 中心黑段的中点:往回走了 back-1 步,往前走了 runs[2]-back 步。 + const offset = (runs[2] - back - (back - 1)) / 2; + const total = relaxed ? module * 7 : runs.reduce((a, b) => a + b, 0); + return { module, total, cx: cx + dx * offset, cy: cy + dy * offset }; +} + +function findFinderCandidates(binary, width, height, relaxed) { + const found = []; + const runs = []; + for (let y = 0; y < height; y += 1) { + // 先把这一行切成黑白段。 + runs.length = 0; + let start = 0; + for (let x = 1; x <= width; x += 1) { + if (x === width || binary[y * width + x] !== binary[y * width + start]) { + runs.push({ color: binary[y * width + start], start, length: x - start }); + start = x; + } + } + for (let i = 0; i + 4 < runs.length; i += 1) { + if (runs[i].color !== 1) continue; + const lengths = [runs[i].length, runs[i + 1].length, runs[i + 2].length, runs[i + 3].length, runs[i + 4].length]; + const module = finderRatio(lengths, relaxed); + if (!module) continue; + const cx = Math.round(runs[i + 2].start + runs[i + 2].length / 2); + const total = module * 7; + const vertical = crossCheck(binary, width, height, cx, y, 0, 1, Math.ceil(total), relaxed); + if (!vertical) continue; + // 竖向和横向的模块大小要对得上(透视下允许差一些)。 + const ratio = vertical.module / module; + if (ratio < 0.55 || ratio > 1.8) continue; + const horizontal = crossCheck(binary, width, height, cx, Math.round(vertical.cy), 1, 0, Math.ceil(total), relaxed); + if (!horizontal) continue; + // 对角线再核一次:文字里的「回」「口」这类字横竖都像定位方块,斜着看就不像了。 + const diagonal = crossCheck(binary, width, height, Math.round(horizontal.cx), Math.round(vertical.cy), 1, 1, Math.ceil(total * 1.5), relaxed); + if (!diagonal) continue; + addCandidate(found, horizontal.cx, vertical.cy, (horizontal.module + vertical.module) / 2); + } + } + return found.filter((item) => item.count >= 2).sort((a, b) => b.count - a.count).slice(0, 40); +} + +function addCandidate(list, x, y, module) { + for (const item of list) { + if (Math.abs(item.x - x) < item.module * 2.5 && Math.abs(item.y - y) < item.module * 2.5) { + const ratio = module / item.module; + if (ratio < 0.5 || ratio > 2) continue; + const n = item.count; + item.x = (item.x * n + x) / (n + 1); + item.y = (item.y * n + y) / (n + 1); + item.module = (item.module * n + module) / (n + 1); + item.count = n + 1; + return; + } + } + list.push({ x, y, module, count: 1 }); +} + +// 从定位方块候选里挑出能组成二维码的三个:直角、两条腰差不多长、模块大小一致、 +// 中间区域黑白交错(二维码的数据区)。挑完一组就把用掉的候选去掉,接着找下一枚码。 +function pickTriples(candidates, binary, width, height, max) { + const pool = candidates.slice(0, 30); + const out = []; + const used = new Set(); + while (out.length < max) { + let best = null; + for (let i = 0; i < pool.length; i += 1) { + if (used.has(i)) continue; + for (let j = i + 1; j < pool.length; j += 1) { + if (used.has(j)) continue; + for (let k = j + 1; k < pool.length; k += 1) { + if (used.has(k)) continue; + const triple = orderTriple(pool[i], pool[j], pool[k]); + if (!triple) continue; + if (!best || triple.score < best.score) best = { ...triple, ids: [i, j, k] }; + } + } + } + if (!best) break; + for (const id of best.ids) used.add(id); + if (!looksLikeData(best, binary, width, height)) continue; + // 落在这枚码里面的其它候选(数据区里偶然像定位方块的图案)也一并去掉。 + const corners = affineCorners(best); + pool.forEach((item, index) => { + if (!used.has(index) && pointInQuad(item, corners)) used.add(index); + }); + out.push(best); + } + return out; +} + +function orderTriple(a, b, c) { + const modules = [a.module, b.module, c.module]; + const spread = Math.max(...modules) / Math.min(...modules); + if (spread > 1.7) return null; + const dab = dist(a, b); + const dac = dist(a, c); + const dbc = dist(b, c); + let corner; + let p; + let q; + if (dbc >= dab && dbc >= dac) [corner, p, q] = [a, b, c]; + else if (dac >= dab && dac >= dbc) [corner, p, q] = [b, a, c]; + else [corner, p, q] = [c, a, b]; + const legA = dist(corner, p); + const legB = dist(corner, q); + const legRatio = Math.min(legA, legB) / Math.max(legA, legB); + if (legRatio < 0.7) return null; + const cos = ((p.x - corner.x) * (q.x - corner.x) + (p.y - corner.y) * (q.y - corner.y)) / (legA * legB); + if (Math.abs(cos) > 0.35) return null; + const module = (a.module + b.module + c.module) / 3; + const span = (legA + legB) / 2 / module; + // 版本 1 的定位方块中心相距 14 个模块;模型画的码模块数不准,下限放宽到 8。 + if (span < 8 || span > 200) return null; + const cross = (p.x - corner.x) * (q.y - corner.y) - (p.y - corner.y) * (q.x - corner.x); + const [topRight, bottomLeft] = cross > 0 ? [p, q] : [q, p]; + const score = Math.abs(cos) * 2 + (1 - legRatio) * 2 + (spread - 1) - Math.log(a.count + b.count + c.count) * 0.05; + return { topLeft: corner, topRight, bottomLeft, module, span, score }; +} + +// 数据区抽样:深色占比要在 15%-85% 之间,相邻模块之间要有足够的黑白翻转。 +// 一块空白、一个实心方块、一段文字都过不了这一关。 +function looksLikeData(triple, binary, width, height) { + const { topLeft, topRight, bottomLeft, span } = triple; + const n = span + 7; + const ux = { x: (topRight.x - topLeft.x) / span, y: (topRight.y - topLeft.y) / span }; + const vy = { x: (bottomLeft.x - topLeft.x) / span, y: (bottomLeft.y - topLeft.y) / span }; + const origin = { x: topLeft.x - 3.5 * ux.x - 3.5 * vy.x, y: topLeft.y - 3.5 * ux.y - 3.5 * vy.y }; + let dark = 0; + let total = 0; + let flips = 0; + let pairs = 0; + const steps = Math.max(9, Math.min(60, Math.round(n))); + for (let row = 0; row < steps; row += 1) { + const v = 8 + ((n - 9) * (row + 0.5)) / steps; + let last = -1; + for (let col = 0; col < steps; col += 1) { + const u = 8 + ((n - 9) * (col + 0.5)) / steps; + // 跳过右下角以外的三个定位方块区域,只看数据区。 + const x = Math.round(origin.x + u * ux.x + v * vy.x); + const y = Math.round(origin.y + u * ux.y + v * vy.y); + if (x < 0 || y < 0 || x >= width || y >= height) continue; + const bit = binary[y * width + x]; + dark += bit; + total += 1; + if (last >= 0) { + pairs += 1; + if (bit !== last) flips += 1; + } + last = bit; + } + } + if (total < 20 || pairs < 10) return false; + const ratio = dark / total; + return ratio > 0.15 && ratio < 0.85 && flips / pairs > 0.15; +} + +function affineCorners(triple) { + const { topLeft, topRight, bottomLeft, span } = triple; + const ux = { x: (topRight.x - topLeft.x) / span, y: (topRight.y - topLeft.y) / span }; + const vy = { x: (bottomLeft.x - topLeft.x) / span, y: (bottomLeft.y - topLeft.y) / span }; + const at = (u, v) => ({ x: topLeft.x + (u - 3.5) * ux.x + (v - 3.5) * vy.x, y: topLeft.y + (u - 3.5) * ux.y + (v - 3.5) * vy.y }); + const n = span + 7; + return [at(0, 0), at(n, 0), at(n, n), at(0, n)]; +} + +// 在原图分辨率上沿定位方块外边缘取点、拟合四条边线,交出透视四角。 +function refineRegion(image, triple, scale) { + const back = (p) => ({ x: (p.x + 0.5) / scale - 0.5, y: (p.y + 0.5) / scale - 0.5 }); + const tl = back(triple.topLeft); + const tr = back(triple.topRight); + const bl = back(triple.bottomLeft); + const span = triple.span; + const module = triple.module / scale; + const u = norm({ x: tr.x - tl.x, y: tr.y - tl.y }); + const v = norm({ x: bl.x - tl.x, y: bl.y - tl.y }); + const gray = (x, y) => sampleGray(image, x, y); + + const edge = (c, along, out) => { + // 在定位方块的外黑框(中心外 2.5-3.5 模块)和外面的白边之间找黑白分界。 + const darkLevel = median([-1.5, 0, 1.5].map((s) => gray(c.x + along.x * s * module + out.x * 3 * module, c.y + along.y * s * module + out.y * 3 * module))); + const lightLevel = median([-1.5, 0, 1.5].map((s) => gray(c.x + along.x * s * module + out.x * 4.1 * module, c.y + along.y * s * module + out.y * 4.1 * module))); + if (lightLevel - darkLevel < 25) return []; + const threshold = (darkLevel + lightLevel) / 2; + const points = []; + for (let s = -2.75; s <= 2.76; s += 0.5) { + let prev = null; + for (let k = 2.6; k <= 4.4; k += 0.05) { + const x = c.x + along.x * s * module + out.x * k * module; + const y = c.y + along.y * s * module + out.y * k * module; + const g = gray(x, y); + if (prev && prev.g < threshold && g >= threshold) { + const t = (threshold - prev.g) / (g - prev.g || 1); + points.push({ x: prev.x + (x - prev.x) * t, y: prev.y + (y - prev.y) * t }); + break; + } + prev = { x, y, g }; + } + } + return points; + }; + + const minus = (p) => ({ x: -p.x, y: -p.y }); + const top = fitLine([...edge(tl, u, minus(v)), ...edge(tr, u, minus(v))], module); + const left = fitLine([...edge(tl, v, minus(u)), ...edge(bl, v, minus(u))], module); + const right = fitLine(edge(tr, v, u), module); + const bottom = fitLine(edge(bl, u, v), module); + + const affine = affineCorners({ topLeft: tl, topRight: tr, bottomLeft: bl, span }); + const tolerance = module * 1.5; + const pick = (a, b, fallback) => { + const hit = a && b ? intersect(a, b) : null; + return hit && dist(hit, fallback) < tolerance ? hit : fallback; + }; + const corners = [ + pick(top, left, affine[0]), + pick(top, right, affine[1]), + pick(right, bottom, affine[2]), + pick(left, bottom, affine[3]), + ]; + if (!isConvex(corners)) return { corners: affine, modulePx: module, modules: span + 7 }; + const side = quadSide(corners); + return { corners, modulePx: module, modules: side / module }; +} + +// 四条边外面有多宽的浅色静区(按原码的模块数算)。贴码时外圈羽化和真码自己的静区都不能超出它。 +function measureQuiet(image, region) { + const [p0, p1, p2, p3] = region.corners; + const h = squareToQuad(p0, p1, p2, p3); + const n = Math.max(1, region.modules); + // 先在码区里取一圈样本估计深浅分界。 + const samples = []; + for (let i = 0; i < 24; i += 1) { + for (let j = 0; j < 24; j += 1) { + const p = applyH(h, (i + 0.5) / 24, (j + 0.5) / 24); + samples.push(sampleGray(image, p.x, p.y)); + } + } + const threshold = otsu(samples); + const sides = [ + (t, d) => [t, -d], + (t, d) => [1 + d, t], + (t, d) => [t, 1 + d], + (t, d) => [-d, t], + ]; + const widths = sides.map((side) => { + let width = 0; + for (let k = 1; k <= 24; k += 1) { + const d = (k * 0.25) / n; + let light = 0; + let count = 0; + for (let i = 0; i < 20; i += 1) { + const [s, t] = side(0.05 + (0.9 * (i + 0.5)) / 20, d); + const p = applyH(h, s, t); + if (p.x < 0 || p.y < 0 || p.x >= image.width || p.y >= image.height) continue; + count += 1; + if (sampleGray(image, p.x, p.y) >= threshold) light += 1; + } + if (!count || light / count < 0.9) break; + width = k * 0.25; + } + return width; + }); + return { top: widths[0], right: widths[1], bottom: widths[2], left: widths[3], min: Math.min(...widths), threshold }; +} + +// ---------------------------------------------------------------- 贴码 + +export function pasteQr(image, region, content, options) { + const opts = options || {}; + const fakeModules = Math.max(10, region.modules || 25); + const sidePx = Math.min(...quadSides(region.corners)); + const quiet = region.quiet || measureQuiet(image, region); + // 可用静区(归一化到码边长 = 1),最多算 4 个原码模块。 + const quietNorm = Math.min(4, quiet.min) / fakeModules; + + // 版本:先算装下内容的最小版本;再尽量贴近原图里码的密度(看起来不突兀,多出来的容量 + // 会让纠错等级顺势往上提),但最多比最小版本高 2 级——模块越细,离远了越难扫。 + let base; + try { + base = encodeQr(content, { ecl: opts.ecl || 'M', boostEcl: false }); + } catch { + try { + base = encodeQr(content, { ecl: 'L', boostEcl: false }); + } catch { + return { ok: false, reason: 'encode_failed' }; + } + } + const fakeVersion = Math.max(1, Math.min(40, Math.round((fakeModules - 17) / 4))); + const target = Math.min(Math.max(base.version, fakeVersion), base.version + 2); + const options2 = [ + { ecl: base.ecl, minVersion: target }, + { ecl: base.ecl, minVersion: base.version }, + { ecl: 'L', minVersion: 1 }, + ]; + let qr = null; + let layout = null; + for (const option of options2) { + let candidate; + try { + candidate = encodeQr(content, option); + } catch { + continue; + } + const candidateLayout = placeCode(candidate.size, quietNorm); + if (!qr || candidateLayout.module > layout.module) { + qr = candidate; + layout = candidateLayout; + } + // 模块够大就用这一档,不再往下退。 + if (candidateLayout.module * sidePx >= MIN_MODULE_PX) { + qr = candidate; + layout = candidateLayout; + break; + } + } + if (!qr) return { ok: false, reason: 'encode_failed' }; + const modulePx = layout.module * sidePx; + if (modulePx < MIN_MODULE_PX) { + const need = Math.ceil(MIN_MODULE_PX / layout.module); + return { ok: false, reason: 'too_small', detail: '码边长约 ' + Math.round(sidePx) + 'px,至少要 ' + need + 'px' }; + } + + const [p0, p1, p2, p3] = region.corners; + const h = squareToQuad(p0, p1, p2, p3); + const inv = invert3(h); + // 抹除范围:原码外扩半个模块(边线拟合有误差,残留的假码边会干扰扫码),不超出原来的浅色静区。 + const erase = Math.min(0.5 / fakeModules, quietNorm * 0.6); + const feather = Math.min(erase, 0.3 / fakeModules); + const colors = sampleColors(image, h, quietNorm, fakeModules); + + const eraseCorners = [ + applyH(h, -erase, -erase), + applyH(h, 1 + erase, -erase), + applyH(h, 1 + erase, 1 + erase), + applyH(h, -erase, 1 + erase), + ]; + const minX = Math.max(0, Math.floor(Math.min(...eraseCorners.map((p) => p.x))) - 1); + const maxX = Math.min(image.width - 1, Math.ceil(Math.max(...eraseCorners.map((p) => p.x))) + 1); + const minY = Math.max(0, Math.floor(Math.min(...eraseCorners.map((p) => p.y))) - 1); + const maxY = Math.min(image.height - 1, Math.ceil(Math.max(...eraseCorners.map((p) => p.y))) + 1); + const { channels, data, width } = image; + const size = qr.size; + const modules = qr.modules; + const unit = layout.module; + const offset = layout.offset; + const subs = [-1 / 3, 0, 1 / 3]; + const light = [0, 0, 0]; + const dark = [0, 0, 0]; + + for (let y = minY; y <= maxY; y += 1) { + for (let x = minX; x <= maxX; x += 1) { + const c = applyH(inv, x + 0.5, y + 0.5); + const edgeDist = Math.min(c.x + erase, 1 + erase - c.x, c.y + erase, 1 + erase - c.y); + if (edgeDist < 0) continue; + const alpha = feather > 0 ? Math.min(1, edgeDist / feather) : 1; + // 3x3 超采样算模块覆盖率:用像素中心的雅可比近似子像素坐标,省掉 8 次透视反算。 + const cx = applyH(inv, x + 1.5, y + 0.5); + const cy = applyH(inv, x + 0.5, y + 1.5); + const dsx = cx.x - c.x; + const dtx = cx.y - c.y; + const dsy = cy.x - c.x; + const dty = cy.y - c.y; + let cover = 0; + for (const oy of subs) { + for (const ox of subs) { + const s = c.x + dsx * ox + dsy * oy; + const t = c.y + dtx * ox + dty * oy; + const i = Math.floor((s - offset) / unit); + const j = Math.floor((t - offset) / unit); + if (i >= 0 && j >= 0 && i < size && j < size && modules[j * size + i] === 1) cover += 1; + } + } + cover /= 9; + colors.light(c.x, c.y, light); + colors.dark(c.x, c.y, dark); + const noise = colors.noise ? hashNoise(x, y) * colors.noise : 0; + const o = (y * width + x) * channels; + for (let k = 0; k < Math.min(3, channels); k += 1) { + const value = light[k] * (1 - cover) + dark[k] * cover + noise; + data[o + k] = clamp255(data[o + k] * (1 - alpha) + value * alpha); + } + } + } + const codeCorners = [ + applyH(h, offset, offset), + applyH(h, offset + unit * size, offset), + applyH(h, offset + unit * size, offset + unit * size), + applyH(h, offset, offset + unit * size), + ]; + return { ok: true, qr, modulePx, codeCorners, eraseCorners, layout }; +} + +// 真码在原码范围里怎么摆:静区够就铺满原码,不够就整体缩小、居中,把静区让出来。 +function placeCode(size, quietNorm) { + let module = 1 / size; + if (QUIET_MODULES * module > quietNorm + 1e-9) module = (1 + 2 * quietNorm) / (size + 2 * QUIET_MODULES); + module = Math.min(module, 1 / size); + return { module, offset: (1 - size * module) / 2 }; +} + +// 浅色、深色各拟合一张二次曲面(每个通道 6 个系数),贴上去的码跟着原图的明暗与偏色走。 +function sampleColors(image, h, quietNorm, fakeModules) { + const grid = 40; + const ring = Math.min(quietNorm, 1.5 / fakeModules); + const points = []; + for (let j = 0; j < grid; j += 1) { + for (let i = 0; i < grid; i += 1) { + const s = (i + 0.5) / grid; + const t = (j + 0.5) / grid; + points.push({ s, t, inside: true }); + } + } + if (ring > 0) { + for (let i = 0; i < grid; i += 1) { + const a = (i + 0.5) / grid; + const d = ring * 0.6; + points.push({ s: a, t: -d }, { s: a, t: 1 + d }, { s: -d, t: a }, { s: 1 + d, t: a }); + } + } + const rgb = [0, 0, 0]; + for (const point of points) { + const p = applyH(h, point.s, point.t); + sampleRgb(image, p.x, p.y, rgb); + point.r = rgb[0]; + point.g = rgb[1]; + point.b = rgb[2]; + point.y = 0.299 * rgb[0] + 0.587 * rgb[1] + 0.114 * rgb[2]; + } + const inside = points.filter((point) => point.inside); + const threshold = otsu(inside.map((point) => point.y)); + const lightMean = mean(inside.filter((point) => point.y >= threshold).map((point) => point.y)); + const darkMean = mean(inside.filter((point) => point.y < threshold).map((point) => point.y)); + // 离分界太近的样本是模块边缘的过渡像素,不参与拟合。 + const lightCut = threshold + (lightMean - threshold) * 0.3; + const darkCut = threshold - (threshold - darkMean) * 0.3; + const lightSet = points.filter((point) => point.y >= lightCut && (point.inside || point.y >= threshold)); + const darkSet = inside.filter((point) => point.y <= darkCut); + const lightFit = fitSurface(lightSet); + let darkFit = fitSurface(darkSet); + + // 对比度兜底:模型画的码偶尔是灰对灰,扫码器需要深浅差足够大。 + const lc = evalSurface(lightFit, 0.5, 0.5); + const dc = evalSurface(darkFit, 0.5, 0.5); + const ly = 0.299 * lc[0] + 0.587 * lc[1] + 0.114 * lc[2]; + const dy = 0.299 * dc[0] + 0.587 * dc[1] + 0.114 * dc[2]; + if (dy > ly * 0.45) { + const k = (ly * 0.45) / Math.max(1, dy); + darkFit = darkFit.map((coef) => coef.map((value) => value * k)); + } + // 原图浅色区域的颗粒感:加一点同幅度的噪声,贴上去的白底不会比周围「干净」得扎眼。 + let residual = 0; + for (const point of lightSet) { + const fit = evalSurface(lightFit, point.s, point.t); + residual += (point.y - (0.299 * fit[0] + 0.587 * fit[1] + 0.114 * fit[2])) ** 2; + } + const noise = lightSet.length ? Math.min(4, Math.sqrt(residual / lightSet.length)) : 0; + return { + light: (s, t, out) => evalSurfaceInto(lightFit, s, t, out), + dark: (s, t, out) => evalSurfaceInto(darkFit, s, t, out), + noise, + lightLuma: ly, + darkLuma: Math.min(dy, ly * 0.45), + }; +} + +function fitSurface(points) { + const channels = ['r', 'g', 'b']; + if (points.length < 24) { + return channels.map((key) => [mean(points.map((point) => point[key])), 0, 0, 0, 0, 0]); + } + const ata = Array.from({ length: 6 }, () => new Array(6).fill(0)); + const atb = channels.map(() => new Array(6).fill(0)); + for (const point of points) { + const basis = [1, point.s, point.t, point.s * point.s, point.s * point.t, point.t * point.t]; + for (let a = 0; a < 6; a += 1) { + for (let b = 0; b < 6; b += 1) ata[a][b] += basis[a] * basis[b]; + channels.forEach((key, index) => { + atb[index][a] += basis[a] * point[key]; + }); + } + } + return channels.map((key, index) => { + const solved = solve6(ata, atb[index]); + return solved || [mean(points.map((point) => point[key])), 0, 0, 0, 0, 0]; + }); +} + +function evalSurface(fit, s, t) { + const out = [0, 0, 0]; + evalSurfaceInto(fit, s, t, out); + return out; +} + +function evalSurfaceInto(fit, s, t, out) { + // 外推到静区以外时把坐标收回来,二次曲面在边界外会发散。 + const cs = s < -0.2 ? -0.2 : s > 1.2 ? 1.2 : s; + const ct = t < -0.2 ? -0.2 : t > 1.2 ? 1.2 : t; + for (let k = 0; k < 3; k += 1) { + const c = fit[k]; + out[k] = c[0] + c[1] * cs + c[2] * ct + c[3] * cs * cs + c[4] * cs * ct + c[5] * ct * ct; + } + return out; +} + +// 高斯消元解 6x6 正规方程;接近奇异时返回 null,调用方退回常数。 +function solve6(matrix, vector) { + const n = 6; + const a = matrix.map((row, i) => [...row, vector[i]]); + for (let col = 0; col < n; col += 1) { + let pivot = col; + for (let row = col + 1; row < n; row += 1) if (Math.abs(a[row][col]) > Math.abs(a[pivot][col])) pivot = row; + if (Math.abs(a[pivot][col]) < 1e-9) return null; + [a[col], a[pivot]] = [a[pivot], a[col]]; + for (let row = 0; row < n; row += 1) { + if (row === col) continue; + const f = a[row][col] / a[col][col]; + for (let k = col; k <= n; k += 1) a[row][k] -= f * a[col][k]; + } + } + return a.map((row, i) => row[n] / row[i]); +} + +// ---------------------------------------------------------------- 验证 + +// 扫码验证。三种边距(码边长的 12% / 30% / 60%)各扫两次: +// 「近看」按原分辨率(裁出来的图长边超过 1600 时缩到 1600,模块仍远大于扫码需要的像素), +// 「远看」缩到每个模块约 2.2 像素。近看、远看各至少 2 次解出 content 才算通过; +// 任何一次解出别的内容,判 mismatch。target 至少要带 eraseCorners(或 corners)和 modulePx。 +export function verifyPasted(image, target, content) { + const quad = target.eraseCorners || target.corners; + const xs = quad.map((p) => p.x); + const ys = quad.map((p) => p.y); + const box = { x: Math.min(...xs), y: Math.min(...ys), w: Math.max(...xs) - Math.min(...xs), h: Math.max(...ys) - Math.min(...ys) }; + const side = Math.max(box.w, box.h); + const modulePx = Number(target.modulePx) || 0; + const scans = []; + for (const ratio of [0.12, 0.3, 0.6]) { + const pad = side * ratio; + const rect = { x: box.x - pad, y: box.y - pad, width: box.w + pad * 2, height: box.h + pad * 2 }; + const longEdge = Math.max(rect.width, rect.height); + const near = scanQr(image, { rect, maxEdge: longEdge > 1600 ? 1600 : undefined }); + scans.push({ view: 'near', pad: ratio, data: near ? near.data : null }); + if (modulePx > 2.6) { + const maxEdge = Math.max(64, Math.round((longEdge * 2.2) / modulePx)); + const far = scanQr(image, { rect, maxEdge }); + scans.push({ view: 'far', pad: ratio, data: far ? far.data : null }); + } + } + const wrong = scans.find((item) => item.data !== null && item.data !== content); + const count = (view) => scans.filter((item) => item.view === view && item.data === content).length; + const farViews = scans.filter((item) => item.view === 'far').length; + const ok = !wrong && count('near') >= 2 && (farViews === 0 || count('far') >= 2); + const scanned = wrong ? wrong.data : scans.some((item) => item.data === content) ? content : null; + return { ok, scanned, scans, reason: ok ? '' : wrong ? 'mismatch' : 'unscannable' }; +} + +// ---------------------------------------------------------------- 几何与采样工具 + +function sampleGray(image, x, y) { + const rgb = [0, 0, 0]; + sampleRgb(image, x, y, rgb); + return 0.299 * rgb[0] + 0.587 * rgb[1] + 0.114 * rgb[2]; +} + +// 双线性取色。坐标超出图片时贴边取。 +function sampleRgb(image, x, y, out) { + const { width, height, channels, data } = image; + const fx = Math.max(0, Math.min(width - 1.001, x - 0.5)); + const fy = Math.max(0, Math.min(height - 1.001, y - 0.5)); + const x0 = Math.floor(fx); + const y0 = Math.floor(fy); + const ax = fx - x0; + const ay = fy - y0; + const i00 = (y0 * width + x0) * channels; + const i10 = i00 + channels; + const i01 = i00 + width * channels; + const i11 = i01 + channels; + for (let k = 0; k < 3; k += 1) { + const c = channels >= 3 ? k : 0; + const top = data[i00 + c] * (1 - ax) + data[i10 + c] * ax; + const bottom = data[i01 + c] * (1 - ax) + data[i11 + c] * ax; + out[k] = top * (1 - ay) + bottom * ay; + } + return out; +} + +// 单位正方形 → 四边形的透视矩阵(Heckbert)。p0..p3 依次对应 (0,0) (1,0) (1,1) (0,1)。 +export function squareToQuad(p0, p1, p2, p3) { + const dx1 = p1.x - p2.x; + const dx2 = p3.x - p2.x; + const dx3 = p0.x - p1.x + p2.x - p3.x; + const dy1 = p1.y - p2.y; + const dy2 = p3.y - p2.y; + const dy3 = p0.y - p1.y + p2.y - p3.y; + if (Math.abs(dx3) < 1e-9 && Math.abs(dy3) < 1e-9) { + return [p1.x - p0.x, p3.x - p0.x, p0.x, p1.y - p0.y, p3.y - p0.y, p0.y, 0, 0, 1]; + } + const den = dx1 * dy2 - dx2 * dy1; + const g = (dx3 * dy2 - dx2 * dy3) / den; + const hh = (dx1 * dy3 - dx3 * dy1) / den; + return [ + p1.x - p0.x + g * p1.x, p3.x - p0.x + hh * p3.x, p0.x, + p1.y - p0.y + g * p1.y, p3.y - p0.y + hh * p3.y, p0.y, + g, hh, 1, + ]; +} + +export function applyH(m, s, t) { + const w = m[6] * s + m[7] * t + m[8]; + return { x: (m[0] * s + m[1] * t + m[2]) / w, y: (m[3] * s + m[4] * t + m[5]) / w }; +} + +function invert3(m) { + const [a, b, c, d, e, f, g, h, i] = m; + const A = e * i - f * h; + const B = -(d * i - f * g); + const C = d * h - e * g; + const det = a * A + b * B + c * C; + return [ + A / det, -(b * i - c * h) / det, (b * f - c * e) / det, + B / det, (a * i - c * g) / det, -(a * f - c * d) / det, + C / det, -(a * h - b * g) / det, (a * e - b * d) / det, + ]; +} + +// 总体最小二乘拟合直线,剔除离群点后再拟合一次。返回 { point, dir }。 +function fitLine(points, module) { + if (points.length < 3) return null; + let line = pcaLine(points); + for (let pass = 0; pass < 2 && line; pass += 1) { + const kept = points.filter((p) => Math.abs(cross2(line.dir, { x: p.x - line.point.x, y: p.y - line.point.y })) < module * 0.35); + if (kept.length < 3) return null; + line = pcaLine(kept); + } + return line; +} + +function pcaLine(points) { + const mx = mean(points.map((p) => p.x)); + const my = mean(points.map((p) => p.y)); + let sxx = 0; + let syy = 0; + let sxy = 0; + for (const p of points) { + sxx += (p.x - mx) ** 2; + syy += (p.y - my) ** 2; + sxy += (p.x - mx) * (p.y - my); + } + const angle = 0.5 * Math.atan2(2 * sxy, sxx - syy); + return { point: { x: mx, y: my }, dir: { x: Math.cos(angle), y: Math.sin(angle) } }; +} + +function intersect(a, b) { + const den = cross2(a.dir, b.dir); + if (Math.abs(den) < 0.2) return null; + const diff = { x: b.point.x - a.point.x, y: b.point.y - a.point.y }; + const t = cross2(diff, b.dir) / den; + return { x: a.point.x + a.dir.x * t, y: a.point.y + a.dir.y * t }; +} + +function cross2(a, b) { + return a.x * b.y - a.y * b.x; +} + +function norm(v) { + const length = Math.hypot(v.x, v.y) || 1; + return { x: v.x / length, y: v.y / length }; +} + +function dist(a, b) { + return Math.hypot(a.x - b.x, a.y - b.y); +} + +function center(corners) { + return { x: mean(corners.map((p) => p.x)), y: mean(corners.map((p) => p.y)) }; +} + +function quadSides(corners) { + return corners.map((p, i) => dist(p, corners[(i + 1) % 4])); +} + +function quadSide(corners) { + return mean(quadSides(corners)); +} + +function quadArea(corners) { + let area = 0; + for (let i = 0; i < corners.length; i += 1) area += cross2(corners[i], corners[(i + 1) % corners.length]); + return Math.abs(area) / 2; +} + +function isConvex(corners) { + let sign = 0; + for (let i = 0; i < 4; i += 1) { + const a = corners[i]; + const b = corners[(i + 1) % 4]; + const c = corners[(i + 2) % 4]; + const z = cross2({ x: b.x - a.x, y: b.y - a.y }, { x: c.x - b.x, y: c.y - b.y }); + if (Math.abs(z) < 1e-6) return false; + if (!sign) sign = Math.sign(z); + else if (Math.sign(z) !== sign) return false; + } + return true; +} + +function pointInQuad(p, corners) { + let sign = 0; + for (let i = 0; i < 4; i += 1) { + const a = corners[i]; + const b = corners[(i + 1) % 4]; + const z = cross2({ x: b.x - a.x, y: b.y - a.y }, { x: p.x - a.x, y: p.y - a.y }); + if (!sign) sign = Math.sign(z); + else if (Math.sign(z) !== sign) return false; + } + return true; +} + +// 两个四边形的重合度:用其中较小者的网格采样点落在另一个里的比例估计。 +function overlap(a, b) { + const [small, big] = quadArea(a) < quadArea(b) ? [a, b] : [b, a]; + const h = squareToQuad(small[0], small[1], small[2], small[3]); + let inside = 0; + for (let i = 0; i < 8; i += 1) { + for (let j = 0; j < 8; j += 1) { + if (pointInQuad(applyH(h, (i + 0.5) / 8, (j + 0.5) / 8), big)) inside += 1; + } + } + return inside / 64; +} + +function otsu(values) { + const hist = new Array(256).fill(0); + for (const value of values) hist[Math.max(0, Math.min(255, Math.round(value)))] += 1; + const total = values.length; + let sum = 0; + for (let i = 0; i < 256; i += 1) sum += i * hist[i]; + let sumB = 0; + let weightB = 0; + let best = 0; + let threshold = 128; + for (let i = 0; i < 256; i += 1) { + weightB += hist[i]; + if (!weightB) continue; + const weightF = total - weightB; + if (!weightF) break; + sumB += i * hist[i]; + const meanB = sumB / weightB; + const meanF = (sum - sumB) / weightF; + const between = weightB * weightF * (meanB - meanF) ** 2; + if (between > best) { + best = between; + threshold = i + 0.5; + } + } + return threshold; +} + +function median(values) { + const sorted = values.slice().sort((a, b) => a - b); + return sorted[Math.floor(sorted.length / 2)]; +} + +function mean(values) { + if (!values.length) return 0; + let total = 0; + for (const value of values) total += value; + return total / values.length; +} + +// 确定性的像素噪声(同一张图修两次结果一样),范围 [-1, 1]。 +function hashNoise(x, y) { + let n = (x * 374761393 + y * 668265263) | 0; + n = Math.imul(n ^ (n >>> 13), 1274126177); + n ^= n >>> 16; + return ((n & 0xffff) / 0xffff) * 2 - 1; +} + +function clamp255(value) { + return value < 0 ? 0 : value > 255 ? 255 : Math.round(value); +} + +function round2(value) { + return Math.round(Number(value) * 100) / 100; +} + +function roundCorners(corners) { + return corners ? corners.map((p) => ({ x: Math.round(p.x * 10) / 10, y: Math.round(p.y * 10) / 10 })) : null; +} diff --git a/plugins/liddalidd/h3-image-executor/miniapp/node/lib/qrscan.mjs b/plugins/liddalidd/h3-image-executor/miniapp/node/lib/qrscan.mjs new file mode 100644 index 00000000..d1c9e527 --- /dev/null +++ b/plugins/liddalidd/h3-image-executor/miniapp/node/lib/qrscan.mjs @@ -0,0 +1,140 @@ +// 扫码:从像素里把二维码重新认一遍。 +// +// 「贴回去的真码能不能扫」只认这一步的结果:编码器说自己编对了不算数,要像手机一样, +// 从最终图片的像素里找定位图形、读格式信息、纠错、解出内容,解出来的和要放的内容一致才算通过。 +// +// 解码器用 jsQR 1.4.0(https://github.com/cozmo/jsQR,Apache-2.0,作者 Cosmo Wolfe、Jefff Nelson)。 +// ../vendor/jsqr/jsQR.cjs 是它 npm 包里的 dist/jsQR.js 原样拷贝,只把扩展名改成 .cjs, +// 让它不管放在哪个 package.json 下面都按 CommonJS 加载;许可证全文在同目录的 LICENSE。 +import { createRequire } from 'node:module'; + +const require = createRequire(import.meta.url); +const jsQR = require('../vendor/jsqr/jsQR.cjs'); + +// image: { width, height, channels, data }。 +// options.rect:只扫这一块 { x, y, width, height }(原图像素坐标); +// options.maxEdge:先把这一块缩到长边不超过这么多像素再扫(盒式滤波,相当于手机离远了看); +// options.invert:true 时深浅两种都试(浅色码印在深底上也能认)。 +// 返回 { data, bytes, version, corners: [左上, 右上, 右下, 左下] },corners 换算回原图坐标;扫不出返回 null。 +export function scanQr(image, options) { + const opts = options || {}; + const rect = clampRect(opts.rect, image); + if (!rect) return null; + const scale = opts.maxEdge ? Math.min(1, opts.maxEdge / Math.max(rect.width, rect.height)) : 1; + const view = toRgba(image, rect, scale); + if (view.width < 21 || view.height < 21) return null; + let found; + try { + found = jsQR(view.data, view.width, view.height, { inversionAttempts: opts.invert ? 'attemptBoth' : 'dontInvert' }); + } catch { + found = null; + } + if (!found) return null; + const back = (point) => ({ + x: rect.x + point.x / view.scaleX, + y: rect.y + point.y / view.scaleY, + }); + const loc = found.location; + return { + data: String(found.data || ''), + bytes: Array.isArray(found.binaryData) ? found.binaryData.slice() : [], + version: found.version, + corners: [loc.topLeftCorner, loc.topRightCorner, loc.bottomRightCorner, loc.bottomLeftCorner].map(back), + }; +} + +// 把图里所有能扫出来的码都找出来:扫到一枚就把它涂白,再扫下一枚。jsQR 一次只认一枚。 +export function scanAllQr(image, options) { + const opts = options || {}; + const limit = Math.max(1, Math.min(8, opts.max || 4)); + const found = []; + let work = image; + for (let i = 0; i < limit; i += 1) { + const hit = scanQr(work, { ...opts, rect: undefined, invert: opts.invert !== false }); + if (!hit) break; + found.push(hit); + if (work === image) work = { ...image, data: Buffer.from(image.data) }; + fillPolygon(work, grow(hit.corners, 0.12), 255); + } + return found; +} + +// ---------------------------------------------------------------- 内部工具 + +function clampRect(rect, image) { + const r = rect || { x: 0, y: 0, width: image.width, height: image.height }; + const x = Math.max(0, Math.floor(r.x)); + const y = Math.max(0, Math.floor(r.y)); + const right = Math.min(image.width, Math.ceil(r.x + r.width)); + const bottom = Math.min(image.height, Math.ceil(r.y + r.height)); + if (right - x < 1 || bottom - y < 1) return null; + return { x, y, width: right - x, height: bottom - y }; +} + +// 取出一块并(按需)用盒式滤波缩小,转成 jsQR 要的 RGBA。 +// 盒式滤波而不是取最近点:最近点会把细小的模块直接漏掉,扫码结果会比真实情况更差。 +function toRgba(image, rect, scale) { + const { channels, data } = image; + const width = Math.max(1, Math.round(rect.width * scale)); + const height = Math.max(1, Math.round(rect.height * scale)); + const out = new Uint8ClampedArray(width * height * 4); + const scaleX = width / rect.width; + const scaleY = height / rect.height; + for (let oy = 0; oy < height; oy += 1) { + const sy0 = rect.y + Math.floor(oy / scaleY); + const sy1 = Math.max(sy0 + 1, Math.min(rect.y + rect.height, rect.y + Math.floor((oy + 1) / scaleY))); + for (let ox = 0; ox < width; ox += 1) { + const sx0 = rect.x + Math.floor(ox / scaleX); + const sx1 = Math.max(sx0 + 1, Math.min(rect.x + rect.width, rect.x + Math.floor((ox + 1) / scaleX))); + let r = 0; + let g = 0; + let b = 0; + let n = 0; + for (let sy = sy0; sy < sy1; sy += 1) { + let i = (sy * image.width + sx0) * channels; + for (let sx = sx0; sx < sx1; sx += 1, i += channels) { + if (channels >= 3) { + r += data[i]; + g += data[i + 1]; + b += data[i + 2]; + } else { + r += data[i]; + g += data[i]; + b += data[i]; + } + n += 1; + } + } + const o = (oy * width + ox) * 4; + out[o] = r / n; + out[o + 1] = g / n; + out[o + 2] = b / n; + out[o + 3] = 255; + } + } + return { width, height, data: out, scaleX, scaleY }; +} + +function grow(corners, ratio) { + const cx = corners.reduce((sum, p) => sum + p.x, 0) / corners.length; + const cy = corners.reduce((sum, p) => sum + p.y, 0) / corners.length; + return corners.map((p) => ({ x: cx + (p.x - cx) * (1 + ratio), y: cy + (p.y - cy) * (1 + ratio) })); +} + +// 扫描线填充凸多边形,只用于把已经认出来的码涂掉。 +function fillPolygon(image, points, value) { + const minY = Math.max(0, Math.floor(Math.min(...points.map((p) => p.y)))); + const maxY = Math.min(image.height - 1, Math.ceil(Math.max(...points.map((p) => p.y)))); + for (let y = minY; y <= maxY; y += 1) { + const xs = []; + for (let i = 0; i < points.length; i += 1) { + const a = points[i]; + const b = points[(i + 1) % points.length]; + if ((a.y <= y && b.y > y) || (b.y <= y && a.y > y)) xs.push(a.x + ((y - a.y) / (b.y - a.y)) * (b.x - a.x)); + } + if (xs.length < 2) continue; + const left = Math.max(0, Math.floor(Math.min(...xs))); + const right = Math.min(image.width - 1, Math.ceil(Math.max(...xs))); + image.data.fill(value, (y * image.width + left) * image.channels, (y * image.width + right + 1) * image.channels); + } +} diff --git a/plugins/liddalidd/h3-image-executor/miniapp/node/lib/refsearch.mjs b/plugins/liddalidd/h3-image-executor/miniapp/node/lib/refsearch.mjs index 15f92f33..3021d21d 100644 --- a/plugins/liddalidd/h3-image-executor/miniapp/node/lib/refsearch.mjs +++ b/plugins/liddalidd/h3-image-executor/miniapp/node/lib/refsearch.mjs @@ -7,6 +7,7 @@ const TOOL_MATCH = 'search_images'; const MAX_QUERIES = 6; +export const REF_USES = new Set(['subject', 'detail', 'style', 'layout', 'scene']); // 每个实体保留的候选池大小。池子只是"待选名单",不会一次全下: // 运行时按 3 张一批下载并判定,不合格才取下一批(见 workflow.mjs 的 refBatch)。 // 池子留大一点,是为了让"这一批不行"时有下一批可取,而不是立刻放弃整个实体。 @@ -201,7 +202,21 @@ export function normalizeRefQueries(raw) { if (!taskName) taskName = 'ref' + (out.length + 1); while (seen.has(taskName)) taskName += '1'; seen.add(taskName); - out.push({ task_name: taskName, query, prompt: asText(item.prompt).trim() }); + // alt_query:同一个实体换一种说法(原产地语言或英文的正式名称)。一批候选全被否时先用它重搜。 + const alt = normalizeQuery(asText(item.alt_query)).slice(0, 120); + const use = asText(item.use).trim().toLowerCase(); + out.push({ + task_name: taskName, + query, + alt_query: alt && alt !== normalizeQuery(query) ? alt : '', + prompt: asText(item.prompt).trim(), + // 这张参考图拿来做什么(主体外观 / 局部 / 画风 / 版式 / 场景),以及从它身上取什么。 + // 筛选按用途判,生图时逐张告诉模型:参考图一多,不说清用途模型就会照搬其中一张的构图和内容。 + use: REF_USES.has(use) ? use : 'subject', + take: asText(item.take).trim().slice(0, 80), + // 追问里重新检索时用:这一项找到的新图替换掉哪张旧参考图(R 编号)。 + replaces: /^R\d+$/i.test(asText(item.replaces).trim()) ? asText(item.replaces).trim().toUpperCase() : '', + }); if (out.length >= MAX_QUERIES) break; } return out; @@ -258,9 +273,10 @@ export async function searchRefImages(context, queries) { const list = (queries || []).filter((item) => item && asText(item.query)); if (!list.length) return { ok: false, reason: '没有检索意图', hits: [], notes: [] }; const resolved = await resolveTool(context); - if (resolved.error) return { ok: false, reason: resolved.error, hits: [], notes: [] }; + // stop:工具没有或被拒,换说法再搜也一样,调用方不要再试(见 workflow.runRefSearch)。 + if (resolved.error) return { ok: false, stop: true, reason: resolved.error, hits: [], notes: [] }; const blocked = await ensureAllowed(context, resolved); - if (blocked) return { ok: false, reason: blocked, hits: [], notes: [] }; + if (blocked) return { ok: false, stop: true, reason: blocked, hits: [], notes: [] }; const failures = []; const notes = []; @@ -268,7 +284,7 @@ export async function searchRefImages(context, queries) { // 权限被拒:整次检索停下,并丢掉「已放行」的缓存,下一次检索重新列工具、读到最新的权限。 const stopForPermission = (reason) => { nameCache.delete(context); - return { ok: false, reason: '搜图工具不可用:' + reason, hits: [], notes }; + return { ok: false, stop: true, reason: '搜图工具不可用:' + reason, hits: [], notes }; }; // 批量只试一次:这个 provider 对多条 queries 经常直接报错,单条才稳定。 diff --git a/plugins/liddalidd/h3-image-executor/miniapp/node/lib/review.mjs b/plugins/liddalidd/h3-image-executor/miniapp/node/lib/review.mjs index b8e7ebaf..8c1dc26e 100644 --- a/plugins/liddalidd/h3-image-executor/miniapp/node/lib/review.mjs +++ b/plugins/liddalidd/h3-image-executor/miniapp/node/lib/review.mjs @@ -96,33 +96,60 @@ export async function buildCandidateSheets(entries, outDir) { // ---------------------------------------------------------------- 参考图 / 并排对比 // 把多张图拼成一张带编号的对比图。labels 决定左上角标签文字。 +// +// 解不开的图不占格子,结果里的 skipped 写明是哪张、为什么。以前解不开的图会留一个空格子, +// 剩下的图仍按原下标编号:判定提示词说「共 1 张,编号 1」,图上却标着「2」, +// 判定节点照图上的数字回答就会被算成「都不符合」(真机 20261112-8a442a 的大屋根第 1 批)。 +// options.renumber:按实际画进去的顺序重新编号 1、2、3……,调用方用返回的 items[].index 对回原列表。 +// 每张解完先缩到不超过画布宽度再排版:排版要等知道最终张数才能定,缩小后留在内存里的只有几 MB。 export async function buildGridSheet(items, outPath, options) { const width = (options && options.width) || SHEET_WIDTH; - const perRow = Math.min(items.length, (options && options.perRow) || 4); - const rows = Math.ceil(items.length / perRow); - const cellW = Math.floor(width / perRow); - const cellH = cellW; - const canvas = createCanvas(width, cellH * rows, 3); - const used = []; + const loaded = []; + const skipped = []; for (let index = 0; index < items.length; index += 1) { - let image; try { - image = decodeImage(await readFile(items[index].path)); - } catch { - continue; + const image = decodeImage(await readFile(items[index].path)); + loaded.push({ index, source: items[index], width: image.width, height: image.height, image: downsample(image, width) }); + } catch (error) { + skipped.push({ index, path: items[index].path, label: items[index].label, error: String(error && error.message ? error.message : error) }); } - const box = fitBox(image.width, image.height, cellW, cellH); - const col = index % perRow; - const row = Math.floor(index / perRow); + } + if (!loaded.length) return { sheet: null, items: [], skipped }; + const { canvas, used } = layoutGridSheet(loaded, options); + await writeImage(outPath, canvas); + return { sheet: outPath, items: used, skipped }; +} + +// 已经解码好的图拼成同样版式的对比图(GIF 的帧条、h3-gif 的帧拼图用):entries 是 [{ image, label }]。 +export function renderGridSheet(entries, options) { + const width = (options && options.width) || SHEET_WIDTH; + const loaded = entries.map((entry, index) => ({ index, source: { label: entry.label, path: '' }, width: entry.image.width, height: entry.image.height, image: downsample(entry.image, width) })); + return layoutGridSheet(loaded, options).canvas; +} + +function layoutGridSheet(loaded, options) { + const width = (options && options.width) || SHEET_WIDTH; + const perRow = Math.max(1, Math.min(loaded.length, (options && options.perRow) || 4)); + const rows = Math.ceil(loaded.length / perRow); + const cellW = Math.floor(width / perRow); + // 帧条的格子按帧的画幅定高:横图的帧放进方格里上下各空一大截。 + const cellH = options && options.cellAspect ? Math.max(1, Math.round(cellW / options.cellAspect)) : cellW; + const canvas = createCanvas(width, cellH * rows, 3); + // 空格子默认是黑的;给了 fill 就铺成这个灰度(交付的帧条用浅色,空格不扎眼)。 + if (options && Number.isFinite(options.fill)) canvas.data.fill(options.fill); + const used = []; + loaded.forEach((entry, position) => { + const box = fitBox(entry.image.width, entry.image.height, cellW, cellH); + const col = position % perRow; + const row = Math.floor(position / perRow); const dx = col * cellW + Math.floor((cellW - box.w) / 2); const dy = row * cellH + Math.floor((cellH - box.h) / 2); - drawImage(canvas, image, dx, dy, box.w, box.h); - const label = items[index].label || String(index + 1); + drawImage(canvas, entry.image, dx, dy, box.w, box.h); + const label = options && options.renumber ? String(position + 1) : entry.source.label || String(position + 1); drawLabel(canvas, label, col * cellW, row * cellH); - used.push({ label, path: items[index].path, width: image.width, height: image.height }); - } - await writeImage(outPath, canvas); - return { sheet: outPath, items: used }; + used.push({ label, index: entry.index, path: entry.source.path, width: entry.width, height: entry.height }); + }); + return { canvas, used }; } export async function buildReferenceSheet(refs, outPath) { @@ -198,6 +225,13 @@ const DIGITS = { 8: ['111', '101', '111', '101', '111'], 9: ['111', '101', '111', '001', '111'], R: ['111', '101', '111', '110', '101'], + // 帧条里补的过渡格标「格子号+」:第 3 格之后补的那一格是 3+。 + '+': ['000', '010', '111', '010', '000'], + // 过渡格评审的候选标字母,和同一行里标数字的格子分开(以前同一行是「1 1 2 2」:前一格和候选 1 都标 1)。 + A: ['010', '101', '111', '101', '101'], + B: ['110', '101', '110', '101', '110'], + C: ['011', '100', '100', '100', '011'], + D: ['110', '101', '101', '101', '110'], }; // 在 (x, y) 处画一个黑底白字的编号标签,让材料图自解释,评审可以直接说"第 5 块"。 diff --git a/plugins/liddalidd/h3-image-executor/miniapp/node/lib/share.mjs b/plugins/liddalidd/h3-image-executor/miniapp/node/lib/share.mjs index 2ee9e4de..435078a5 100644 --- a/plugins/liddalidd/h3-image-executor/miniapp/node/lib/share.mjs +++ b/plugins/liddalidd/h3-image-executor/miniapp/node/lib/share.mjs @@ -24,6 +24,9 @@ const DEFAULTS = { // 大图的总预算(编码后的字节数,base64 之前;base64 之后约再大三分之一)。 // 按交付顺序加,超出预算的几张只带普通预览,免得交付图多的会话整页大到部署不动。 largeBudget: 4 * 1024 * 1024, + // GIF 原样内嵌(重新编码成 JPEG 会丢掉动画)。一张 768px、8 帧的 GIF 实测约 0.4 MB。 + // 按交付顺序加,超出预算的只带静态的参考图预览。 + gifBudget: 3 * 1024 * 1024, includeSheets: true, includeRefs: true, // 内嵌 JPEG 质量。86 是给「看图材料上有编号文字」留的余量:q82 在照片上已经看不出差别, @@ -180,6 +183,19 @@ async function collectImages(snapshot, dirs, options) { for (const [key, dataUrl] of entries) { if (dataUrl) images[key] = dataUrl; } + let gifBytes = 0; + for (const item of (snapshot.delivery && snapshot.delivery.items) || []) { + const parts = item.gif && item.gif.file ? String(item.gif.file).split('/') : []; + if (parts.length !== 2) continue; + try { + const buffer = await readFile(join(dirs.deliver, parts[0], parts[1])); + if (gifBytes + buffer.length > options.gifBudget) continue; + gifBytes += buffer.length; + images['gif/' + item.gif.file] = 'data:image/gif;base64,' + buffer.toString('base64'); + } catch { + /* 读不到就只放静态预览 */ + } + } return images; } diff --git a/plugins/liddalidd/h3-image-executor/miniapp/node/lib/textlayout.mjs b/plugins/liddalidd/h3-image-executor/miniapp/node/lib/textlayout.mjs new file mode 100644 index 00000000..77693956 --- /dev/null +++ b/plugins/liddalidd/h3-image-executor/miniapp/node/lib/textlayout.mjs @@ -0,0 +1,880 @@ +// 程序排字的排版与光栅化:把一段文字按字体排进一个矩形,画成覆盖率蒙版(0-1)。 +// 只管几何,不管颜色和合成(见 textoverlay.mjs)。 +// +// 光栅化用的是按面积累加的扫描算法(和 font-rs 同一思路):每条边把自己在每个像素里扫过的 +// 有向面积记下来,逐行求前缀和就是覆盖率,抗锯齿是精确的面积而不是超采样近似。 +// 同向重叠的轮廓(毛笔字体里很常见)取绝对值后截到 1,等价于非零环绕规则。 + +// ---------------------------------------------------------------- 光栅化 + +// 字体单位 -> 像素的仿射矩阵 [a, b, c, d, e, f]:px = a*x + c*y + e,py = b*x + d*y + f。 +// scale:每个字体单位多少像素;theta:屏幕上顺时针旋转的弧度;把字体里的点 pivot 放到屏幕上的 at。 +export function placeMatrix(scale, theta, pivot, at, stretchX) { + const cos = Math.cos(theta || 0); + const sin = Math.sin(theta || 0); + const sx = scale * (stretchX || 1); + const a = sx * cos; + const b = sx * sin; + const c = scale * sin; + const d = -scale * cos; + return [a, b, c, d, at[0] - (a * pivot[0] + c * pivot[1]), at[1] - (b * pivot[0] + d * pivot[1])]; +} + +function apply(m, x, y) { + return [m[0] * x + m[2] * y + m[4], m[1] * x + m[3] * y + m[5]]; +} + +// 每个字实际落墨的矩形(像素)。查两层字有没有叠在一起要用它:排字框四周留了白边,比墨迹大; +// 整段的外接矩形又太粗(段首缩进、末行半行都算进去了)。 +export function glyphRects(glyphs) { + const out = []; + for (const glyph of glyphs || []) { + const outline = glyph.font && glyph.font.outline(glyph.gid); + if (!outline || !outline.contours || !outline.contours.length) continue; + const corners = [ + apply(glyph.m, outline.xMin, outline.yMin), + apply(glyph.m, outline.xMax, outline.yMin), + apply(glyph.m, outline.xMin, outline.yMax), + apply(glyph.m, outline.xMax, outline.yMax), + ]; + const xs = corners.map((p) => p[0]); + const ys = corners.map((p) => p[1]); + out.push({ x0: Math.min(...xs), y0: Math.min(...ys), x1: Math.max(...xs), y1: Math.max(...ys), ch: glyph.ch }); + } + return out; +} + +// 一个字形画成一块小蒙版。返回 { x, y, w, h, data }(x、y 是这块蒙版在目标坐标里的左上角)。 +export function rasterizeGlyph(outline, m) { + if (!outline || !outline.contours || !outline.contours.length) return null; + const corners = [ + apply(m, outline.xMin, outline.yMin), + apply(m, outline.xMax, outline.yMin), + apply(m, outline.xMin, outline.yMax), + apply(m, outline.xMax, outline.yMax), + ]; + const x0 = Math.floor(Math.min(...corners.map((p) => p[0]))) - 2; + const y0 = Math.floor(Math.min(...corners.map((p) => p[1]))) - 2; + const x1 = Math.ceil(Math.max(...corners.map((p) => p[0]))) + 2; + const y1 = Math.ceil(Math.max(...corners.map((p) => p[1]))) + 2; + const w = x1 - x0; + const h = y1 - y0; + if (w <= 0 || h <= 0 || w * h > 64e6) return null; + const stride = w + 2; + const acc = new Float32Array(stride * h + 2); + const local = [m[0], m[1], m[2], m[3], m[4] - x0, m[5] - y0]; + for (const contour of outline.contours) walkContour(contour, local, acc, stride, h); + const data = new Float32Array(w * h); + for (let y = 0; y < h; y += 1) { + let sum = 0; + const row = y * stride; + for (let x = 0; x < w; x += 1) { + sum += acc[row + x]; + const v = sum < 0 ? -sum : sum; + data[y * w + x] = v > 1 ? 1 : v; + } + } + return { x: x0, y: y0, w, h, data }; +} + +// TrueType 轮廓:on 点之间是直线,off 点是二次贝塞尔的控制点,两个相邻 off 点中间隐含一个 on 点。 +function walkContour(points, m, acc, stride, h) { + const n = points.length; + if (n < 2) return; + let pts; + const first = points.findIndex((p) => p.on); + if (first < 0) { + const mid = { x: (points[n - 1].x + points[0].x) / 2, y: (points[n - 1].y + points[0].y) / 2, on: true }; + pts = [mid, ...points]; + } else { + pts = points.slice(first).concat(points.slice(0, first)); + } + const toPx = (p) => apply(m, p.x, p.y); + const start = toPx(pts[0]); + let cur = start; + let ctrl = null; + for (let i = 1; i <= pts.length; i += 1) { + const p = i === pts.length ? pts[0] : pts[i]; + const pp = toPx(p); + if (p.on) { + if (ctrl) { + quad(acc, stride, h, cur, ctrl, pp); + ctrl = null; + } else { + line(acc, stride, h, cur[0], cur[1], pp[0], pp[1]); + } + cur = pp; + } else { + if (ctrl) { + const mid = [(ctrl[0] + pp[0]) / 2, (ctrl[1] + pp[1]) / 2]; + quad(acc, stride, h, cur, ctrl, mid); + cur = mid; + } + ctrl = pp; + } + } +} + +function quad(acc, stride, h, p0, p1, p2) { + const devx = p0[0] - 2 * p1[0] + p2[0]; + const devy = p0[1] - 2 * p1[1] + p2[1]; + const devsq = devx * devx + devy * devy; + if (devsq < 0.333) { + line(acc, stride, h, p0[0], p0[1], p2[0], p2[1]); + return; + } + const n = 1 + Math.floor(Math.sqrt(Math.sqrt(3 * devsq))); + let px = p0[0]; + let py = p0[1]; + for (let i = 1; i <= n; i += 1) { + const t = i / n; + const mt = 1 - t; + const x = mt * mt * p0[0] + 2 * mt * t * p1[0] + t * t * p2[0]; + const y = mt * mt * p0[1] + 2 * mt * t * p1[1] + t * t * p2[1]; + line(acc, stride, h, px, py, x, y); + px = x; + py = y; + } +} + +function line(acc, stride, h, ax, ay, bx, by) { + if (Math.abs(ay - by) <= 1e-9) return; + let dir = 1; + let x0p = ax; + let y0p = ay; + let x1p = bx; + let y1p = by; + if (ay > by) { + dir = -1; + x0p = bx; + y0p = by; + x1p = ax; + y1p = ay; + } + const dxdy = (x1p - x0p) / (y1p - y0p); + let x = x0p; + let yStart = Math.floor(y0p); + if (y0p < 0) { + x -= y0p * dxdy; + yStart = 0; + } + const yEnd = Math.min(h, Math.ceil(y1p)); + const maxX = stride - 2; + for (let y = yStart; y < yEnd; y += 1) { + const rowStart = y * stride; + const dy = Math.min(y + 1, y1p) - Math.max(y, y0p); + const xnext = x + dxdy * dy; + const d = dy * dir; + let lo = x < xnext ? x : xnext; + let hi = x < xnext ? xnext : x; + lo = lo < 0 ? 0 : lo > maxX ? maxX : lo; + hi = hi < 0 ? 0 : hi > maxX ? maxX : hi; + const loFloor = Math.floor(lo); + const hiCeil = Math.ceil(hi); + if (hiCeil <= loFloor + 1) { + const xmf = 0.5 * (lo + hi) - loFloor; + const i = rowStart + loFloor; + acc[i] += d - d * xmf; + acc[i + 1] += d * xmf; + } else { + const s = 1 / (hi - lo); + const loFrac = lo - loFloor; + const a0 = 0.5 * s * (1 - loFrac) * (1 - loFrac); + const hiFrac = hi - hiCeil + 1; + const am = 0.5 * s * hiFrac * hiFrac; + const i = rowStart + loFloor; + acc[i] += d * a0; + if (hiCeil === loFloor + 2) { + acc[i + 1] += d * (1 - a0 - am); + } else { + const a1 = s * (1.5 - loFrac); + acc[i + 1] += d * (a1 - a0); + for (let xi = loFloor + 2; xi < hiCeil - 1; xi += 1) acc[rowStart + xi] += d * s; + const a2 = a1 + (hiCeil - loFloor - 3) * s; + acc[rowStart + hiCeil - 1] += d * (1 - a2 - am); + } + acc[rowStart + hiCeil] += d * am; + } + x = xnext; + } +} + +// 把一批已经定好位置的字形画进一块蒙版(取最大值合并)。mask:{ x, y, w, h, data },坐标与字形同一套。 +export function drawGlyphs(mask, glyphs) { + for (const glyph of glyphs) { + const outline = glyph.font.outline(glyph.gid); + const tile = rasterizeGlyph(outline, glyph.m); + if (!tile) continue; + const alpha = glyph.alpha === undefined ? 1 : glyph.alpha; + for (let ty = 0; ty < tile.h; ty += 1) { + const my = tile.y + ty - mask.y; + if (my < 0 || my >= mask.h) continue; + for (let tx = 0; tx < tile.w; tx += 1) { + const mx = tile.x + tx - mask.x; + if (mx < 0 || mx >= mask.w) continue; + const v = tile.data[ty * tile.w + tx] * alpha; + const at = my * mask.w + mx; + if (v > mask.data[at]) mask.data[at] = v; + } + } + } + return mask; +} + +// ---------------------------------------------------------------- 部件分色 +// +// 一个字里偏旁和其余部分用不同颜色(识字挂图:「清」的「氵」橙色、「青」深灰)。以前只能把字拆成 +// 「氵」「青」两层并排摆,或者在整字上再叠一个「氵」,拼出来都不是正确的字(真机 20261116-e7b1b1、20261117-f57a0f)。 +// 字体轮廓不一定按部件分:毛笔字常把几笔连成一个轮廓,「氵」的提也会伸进右边部件的范围。所以不按轮廓分, +// 而是在渲染好的字上找一条墨最少的切线(逐行最多挪 1 像素,可以绕开伸过来的笔画),切线靠偏旁那一侧算偏旁。 +// 几条一样好的切线里取最靠外侧的:「湖」里「古」和「月」之间也有一条不碰墨的缝,取靠里的会把「古」也算成偏旁。 +// 偏旁那一侧的墨太少的不算:「忄」的左点和竖之间也有一条缝,只切出左边那一点不是偏旁。 +const PART_MIN_SHARE = 0.1; +const PART_MAX_SHARE = 0.62; +const SEAM_MOVE_COST = 0.02; +// 能分色的字:统一汉字。标点、运算符、上标的脚注符号整个用一种颜色。 +const SPLIT_CHAR = /[\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF]|[\u{20000}-\u{3FFFF}]/u; + +export function splittableChar(ch) { + return SPLIT_CHAR.test(ch); +} + +// tile 是 rasterizeGlyph 的结果。返回 { ok, seam, vertical, near, share(偏旁分到的墨占比), cut(切断笔画的行占有墨的行的比例)}。 +export function splitTile(tile, side) { + const { w, h, data } = tile; + const vertical = side === 'left' || side === 'right'; + const near = side === 'left' || side === 'top'; + const R = vertical ? h : w; + const P = vertical ? w : h; + const ink = vertical ? (r, p) => data[r * w + p] : (r, p) => data[p * w + r]; + let p0 = P; + let p1 = -1; + let r0 = R; + let r1 = -1; + let total = 0; + for (let r = 0; r < R; r += 1) { + for (let p = 0; p < P; p += 1) { + const v = ink(r, p); + total += v; + if (v > 0.3) { + if (p < p0) p0 = p; + if (p > p1) p1 = p; + if (r < r0) r0 = r; + if (r > r1) r1 = r; + } + } + } + if (p1 - p0 < 4 || total <= 0) return { ok: false, reason: 'empty' }; + const span = p1 - p0; + const lo = Math.max(1, Math.round(p0 + span * (near ? 0.08 : 0.35))); + const hi = Math.min(P - 2, Math.round(p0 + span * (near ? 0.65 : 0.92))); + const L = hi - lo + 1; + if (L < 2) return { ok: false, reason: 'empty' }; + // 每行的前缀和:一条切线把多少墨分到偏旁那一侧,逐行查表就行。 + const prefix = new Float64Array(R * (P + 1)); + for (let r = 0; r < R; r += 1) { + let s = 0; + const row = r * (P + 1); + for (let p = 0; p < P; p += 1) { + s += ink(r, p); + prefix[row + p + 1] = s; + } + } + const cost = new Float64Array(R * L); + const from = new Int8Array(R * L); + for (let i = 0; i < L; i += 1) cost[i] = ink(0, lo + i); + for (let r = 1; r < R; r += 1) { + const prev = (r - 1) * L; + for (let i = 0; i < L; i += 1) { + let best = cost[prev + i]; + let step = 0; + if (i > 0 && cost[prev + i - 1] + SEAM_MOVE_COST < best) { + best = cost[prev + i - 1] + SEAM_MOVE_COST; + step = -1; + } + if (i < L - 1 && cost[prev + i + 1] + SEAM_MOVE_COST < best) { + best = cost[prev + i + 1] + SEAM_MOVE_COST; + step = 1; + } + cost[r * L + i] = best + ink(r, lo + i); + from[r * L + i] = step; + } + } + const inkRows = Math.max(1, r1 - r0 + 1); + const valid = []; + for (let end = 0; end < L; end += 1) { + const seam = new Int32Array(R); + let i = end; + for (let r = R - 1; r >= 0; r -= 1) { + seam[r] = lo + i; + if (r > 0) i += from[r * L + i]; + } + let part = 0; + let cut = 0; + let mean = 0; + for (let r = 0; r < R; r += 1) { + const s = seam[r]; + const row = r * (P + 1); + part += near ? prefix[row + s] : prefix[row + P] - prefix[row + s + 1]; + if (ink(r, s) > 0.5) cut += 1; + mean += s; + } + const share = part / total; + if (share < PART_MIN_SHARE || share > PART_MAX_SHARE) continue; + valid.push({ seam, cost: cost[(R - 1) * L + end], share, cut: cut / inkRows, mean: mean / R }); + } + if (!valid.length) return { ok: false, reason: 'no_split' }; + const minCost = Math.min(...valid.map((item) => item.cost)); + const best = valid.filter((item) => item.cost <= minCost + 0.5).sort((a, b) => (near ? a.mean - b.mean : b.mean - a.mean))[0]; + return { ok: true, seam: best.seam, vertical, near, share: best.share, cut: best.cut }; +} + +// tile 内坐标 (x, y) 是不是落在偏旁那一侧。切线本身那一列算其余部分。 +export function inPart(split, x, y) { + const r = split.vertical ? y : x; + const p = split.vertical ? x : y; + return split.near ? p < split.seam[r] : p > split.seam[r]; +} + +// 按 192 像素试切一个字:选字体用(见 textoverlay.choosePartFonts)。 +export function probeSplit(font, ch, side, size) { + const gid = font.glyphId(ch.codePointAt(0)); + if (!gid) return { ok: false, reason: 'missing' }; + const outline = font.outline(gid); + if (!outline || !outline.contours.length) return { ok: false, reason: 'empty' }; + const scale = (size || 192) / font.unitsPerEm; + const tile = rasterizeGlyph(outline, [scale, 0, 0, -scale, -outline.xMin * scale + 3, outline.yMax * scale + 3]); + const split = tile ? splitTile(tile, side) : null; + return split && split.ok ? { ok: true, cut: split.cut, share: split.share } : { ok: false, reason: (split && split.reason) || 'empty' }; +} + +// 和 drawGlyphs 一样画字,汉字按切线分进两张蒙版:偏旁进 partMask,其余进 mask。切不开的字整个进 mask。 +export function drawGlyphsSplit(mask, partMask, glyphs, side) { + const report = { split: 0, failed: [], maxCut: 0 }; + for (const glyph of glyphs) { + const outline = glyph.font.outline(glyph.gid); + const tile = rasterizeGlyph(outline, glyph.m); + if (!tile) continue; + let split = null; + if (splittableChar(glyph.ch)) { + split = splitTile(tile, side); + if (split.ok) { + report.split += 1; + report.maxCut = Math.max(report.maxCut, split.cut); + } else { + report.failed.push(glyph.ch); + split = null; + } + } + const alpha = glyph.alpha === undefined ? 1 : glyph.alpha; + for (let ty = 0; ty < tile.h; ty += 1) { + const my = tile.y + ty - mask.y; + if (my < 0 || my >= mask.h) continue; + for (let tx = 0; tx < tile.w; tx += 1) { + const mx = tile.x + tx - mask.x; + if (mx < 0 || mx >= mask.w) continue; + const v = tile.data[ty * tile.w + tx] * alpha; + if (!v) continue; + const target = split && inPart(split, tx, ty) ? partMask : mask; + const at = my * mask.w + mx; + if (v > target.data[at]) target.data[at] = v; + } + } + } + return report; +} + +// ---------------------------------------------------------------- 字符分类 + +const CJK = /[\u2E80-\u2FFF\u3000-\u303F\u3040-\u30FF\u3100-\u312F\u3190-\u31FF\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF\uFE10-\uFE1F\uFE30-\uFE4F\uFF00-\uFFEF]|[\u{20000}-\u{3FFFF}]/u; +const PUNCT = /[\u3000-\u303F\uFF01-\uFF0F\uFF1A-\uFF20\uFF3B-\uFF40\uFF5B-\uFF65\u2010-\u2027\u2030-\u205E\u00B7\uFE10-\uFE1F\uFE30-\uFE4F!-\/:-@\[-`{-~]/; +// 不能出现在行首(列首)的标点:放不下时挂到上一行行尾。 +const NO_START = new Set(Array.from(',。、;:?!)》」』】〕〉’”…—~·%‰,.;:?!)]}')); +// 不能出现在行尾(列尾)的标点:和后一个字绑在一起。 +const NO_END = new Set(Array.from('(《「『【〔〈‘“([{')); +// 竖排时放在字格右上角的标点。 +const CORNER = new Set(Array.from(',。、.,.')); +// 竖排时转 90° 的标点(括号、书名号、破折号、省略号)。 +const ROTATE = new Set(Array.from('()《》〈〉「」『』【】〔〕[]{}—…~()[]{}<>-–_')); +// 竖排引号换成直角引号的竖排形式,字体里没有就把横排的直角引号转 90°。 +const VERTICAL_QUOTE = { '“': ['﹁', '「'], '”': ['﹂', '」'], '‘': ['﹃', '『'], '’': ['﹄', '』'] }; +// 脚注序号:①–⑳、⑴–⒇、㉑–㊿。紧跟在字后面的是正文里的脚注符号,排成右上角的小字(上标); +// 在段首、句号分号冒号后面的是脚注区里每一条开头的序号,照正常字号排(课本、书刊都是这样)。 +const NOTE_MARK = /[\u2460-\u2487\u3251-\u325F\u32B1-\u32BF]/; +const NOTE_AFTER = new Set(Array.from('。!?;:…!?;:')); +const SUP_SCALE = 0.58; + +export function isPunct(ch) { + return PUNCT.test(ch) && !/[0-9A-Za-z]/.test(ch); +} + +export function isCjk(ch) { + return CJK.test(ch); +} + +// 可以排的字符数(不计空白)。 +export function glyphCount(text) { + return Array.from(String(text || '')).filter((ch) => !/\s/.test(ch)).length; +} + +// ---------------------------------------------------------------- 字体与度量 + +// fonts:主字体在前,后面是缺字时依次补字的字体。每个字符取第一个有这个字的字体。 +function pick(fonts, ch) { + const cp = ch.codePointAt(0); + for (const font of fonts) { + const gid = font.glyphId(cp); + if (gid) return { font, gid }; + } + return null; +} + +// 汉字字身框(em 框)的竖直中心,字体单位。不能只用 ascender/descender:不少毛笔字体的 +// 上下伸部写得很夸张,按它居中会让整列字偏上或偏下。改用几个方块字的轮廓框中心取平均。 +const CENTER_PROBES = Array.from('国永中日田口回'); +const centerCache = new WeakMap(); +export function emCenter(font) { + if (centerCache.has(font)) return centerCache.get(font); + const centers = []; + for (const ch of CENTER_PROBES) { + const gid = font.glyphId(ch.codePointAt(0)); + const outline = gid ? font.outline(gid) : null; + if (outline && outline.contours.length) centers.push((outline.yMin + outline.yMax) / 2); + } + centers.sort((a, b) => a - b); + const value = centers.length ? centers[Math.floor(centers.length / 2)] : (font.ascender + font.descender) / 2; + centerCache.set(font, value); + return value; +} + +export function probeChars() { + return CENTER_PROBES.join(''); +} + +// ---------------------------------------------------------------- 随机抖动(手写感) + +function seeded(seed) { + let a = seed >>> 0; + return () => { + a = (a + 0x6d2b79f5) >>> 0; + let t = a; + t = Math.imul(t ^ (t >>> 15), t | 1); + t ^= t + Math.imul(t ^ (t >>> 7), t | 61); + return ((t ^ (t >>> 14)) >>> 0) / 4294967296; + }; +} + +function hashText(text) { + let h = 2166136261; + for (const ch of String(text)) { + h ^= ch.codePointAt(0); + h = Math.imul(h, 16777619); + } + return h >>> 0; +} + +// ---------------------------------------------------------------- 排版入口 + +// 把一段文字排进 box({ x, y, w, h },像素)。opts: +// direction 'horizontal' | 'vertical'(竖排:从上到下、从右到左) +// align 'start' | 'center' | 'end'(横排是左中右;竖排是每列的上中下) +// punctuation(竖排)'inline' 占一格 | 'side' 小字写在字旁、不占格 | 'omit' 不写 +// handwriting true 时每个字加一点大小、角度、位置的随机变化(毛笔字体用) +// minSize 字号下限(像素),排不下就返回 fits: false +// maxSize 字号上限(像素) +// 返回 { fits, size, glyphs: [{ font, gid, m, ch }], rows, cols, lines, dropped, reason }。 +export function layoutText(fonts, text, box, opts) { + const options = opts || {}; + const direction = options.direction === 'vertical' ? 'vertical' : 'horizontal'; + return direction === 'vertical' ? layoutVertical(fonts, text, box, options) : layoutHorizontal(fonts, text, box, options); +} + +// ---------------------------------------------------------------- 竖排 + +function layoutVertical(fonts, text, box, options) { + const punctMode = ['inline', 'side', 'omit'].includes(options.punctuation) ? options.punctuation : 'inline'; + const charGap = options.charSpacing === undefined ? 0.1 : Number(options.charSpacing); + const colGap = options.columnSpacing === undefined ? (punctMode === 'side' ? 0.5 : 0.35) : Number(options.columnSpacing); + const rand = seeded(hashText(text) ^ 0x9e3779b9); + const main = fonts[0]; + + // 先切成字格:每个非标点字一格;标点按模式处理(占格 / 挂到相邻字上 / 丢掉)。 + const paragraphs = []; + let current = []; + const dropped = []; + const missing = []; + const pendingOpen = []; + for (const ch of Array.from(String(text || ''))) { + if (ch === '\n') { + paragraphs.push(current); + current = []; + continue; + } + if (/\s/.test(ch)) continue; + const punct = isPunct(ch); + if (punct && punctMode === 'omit') { + dropped.push(ch); + continue; + } + if (punct && punctMode === 'side') { + if (NO_END.has(ch)) pendingOpen.push(ch); + else if (current.length) current[current.length - 1].after.push(ch); + else if (paragraphs.length && paragraphs[paragraphs.length - 1].length) { + const last = paragraphs[paragraphs.length - 1]; + last[last.length - 1].after.push(ch); + } else pendingOpen.push(ch); + continue; + } + const cell = { ch, before: pendingOpen.splice(0), after: [], punct }; + current.push(cell); + } + if (current.length) paragraphs.push(current); + if (pendingOpen.length && paragraphs.length) { + const last = paragraphs[paragraphs.length - 1]; + if (last.length) last[last.length - 1].after.push(...pendingOpen.splice(0)); + } + const cells = paragraphs.flat(); + if (!cells.length) return { fits: false, reason: 'empty', size: 0, glyphs: [], rows: 0, cols: 0, dropped, missing }; + + // 选每列字数:让字号最大。 + const longest = Math.max(...paragraphs.map((p) => p.length)); + let best = null; + for (let rows = 1; rows <= longest; rows += 1) { + const columns = splitColumns(paragraphs, rows, punctMode); + const cols = columns.length; + const sizeByH = box.h / (rows * (1 + charGap) - charGap); + const sizeByW = box.w / (cols * (1 + colGap) - colGap); + const size = Math.min(sizeByH, sizeByW, options.maxSize || Infinity); + if (!best || size > best.size + 1e-6) best = { rows, cols, size, columns }; + } + const size = best.size; + const fits = size >= (options.minSize || 0); + const cols = best.columns.length; + // 列距:宽度有富余时把列铺满整个区域(书法长卷是满纸写的),但列距最多放到字宽的 1.2 倍, + // 再多就居中,免得几列字散在一大张纸上。 + const naturalPitch = size * (1 + colGap); + let pitchX = cols > 1 ? Math.min((box.w - size) / (cols - 1), size * 2.2) : 0; + if (pitchX < naturalPitch) pitchX = naturalPitch; + const blockW = (cols - 1) * pitchX + size; + const startRight = box.x + box.w - (box.w - blockW) / 2 - size / 2; + const pitchY = size * (1 + charGap); + const glyphs = []; + const handwriting = Boolean(options.handwriting); + best.columns.forEach((column, ci) => { + const colDrift = handwriting ? (rand() - 0.5) * 0.08 * size : 0; + const cx = startRight - ci * pitchX + colDrift; + const usedH = (column.length - 1) * pitchY + size; + let top = box.y; + if (options.align === 'center') top = box.y + (box.h - usedH) / 2; + else if (options.align === 'end') top = box.y + box.h - usedH; + column.forEach((cell, ri) => { + const cy = top + ri * pitchY + size / 2; + placeVerticalCell(glyphs, fonts, cell.ch, cx, cy, size, handwriting ? rand : null, missing, cell.punct); + cell.before.forEach((mark, k) => placeSideMark(glyphs, fonts, mark, cx, cy - size * (0.32 + k * 0.22), size, pitchX, missing)); + cell.after.forEach((mark, k) => placeSideMark(glyphs, fonts, mark, cx, cy + size * (0.3 + k * 0.24), size, pitchX, missing)); + }); + }); + return { + fits, + reason: fits ? '' : 'too_small', + size: round2(size), + glyphs, + rows: best.rows, + cols, + lines: cols, + cells: cells.length, + dropped, + missing: [...new Set(missing)], + font: main ? main.family : '', + }; +} + +// 按每列 rows 个字切列;占格的标点不能落在列首,落在列首时挂回上一列列尾(挤在最后一格的旁边)。 +function splitColumns(paragraphs, rows, punctMode) { + const columns = []; + for (const paragraph of paragraphs) { + let column = []; + for (const cell of paragraph) { + if (column.length >= rows) { + if (punctMode === 'inline' && NO_START.has(cell.ch) && column.length) { + column[column.length - 1] = { ...column[column.length - 1], after: [...column[column.length - 1].after, cell.ch] }; + continue; + } + columns.push(column); + column = []; + } + column.push(cell); + } + if (column.length) columns.push(column); + } + return columns; +} + +function placeVerticalCell(glyphs, fonts, ch, cx, cy, size, rand, missing, punct) { + let glyphCh = ch; + let rotate = 0; + if (VERTICAL_QUOTE[ch]) { + const [vertical, corner] = VERTICAL_QUOTE[ch]; + if (pick(fonts, vertical)) glyphCh = vertical; + else if (pick(fonts, corner)) { + glyphCh = corner; + rotate = Math.PI / 2; + } else rotate = Math.PI / 2; + } else if (ROTATE.has(ch)) rotate = Math.PI / 2; + const hit = pick(fonts, glyphCh); + if (!hit) { + missing.push(ch); + return; + } + const scale = size / hit.font.unitsPerEm; + const outline = hit.font.outline(hit.gid); + if (!outline) return; + if (punct) { + const pivot = [(outline.xMin + outline.xMax) / 2, (outline.yMin + outline.yMax) / 2]; + let at = [cx, cy]; + if (CORNER.has(ch)) at = [cx + size * 0.24, cy - size * 0.24]; + glyphs.push({ font: hit.font, gid: hit.gid, ch, m: placeMatrix(scale, rotate, pivot, at) }); + return; + } + const adv = hit.font.advance(hit.gid) || hit.font.unitsPerEm; + const pivot = [adv / 2, emCenter(hit.font)]; + let s = scale; + let theta = rotate; + let at = [cx, cy]; + if (rand) { + s *= 0.95 + rand() * 0.1; + theta += (rand() - 0.5) * 0.045; + at = [cx + (rand() - 0.5) * 0.05 * size, cy + (rand() - 0.5) * 0.05 * size]; + } + glyphs.push({ font: hit.font, gid: hit.gid, ch, m: placeMatrix(s, theta, pivot, at), alpha: rand ? 0.9 + rand() * 0.1 : 1 }); +} + +// 不占格的标点:缩小写在字的右侧(右下挂在前一个字上,开引号、开括号挂在后一个字的右上)。 +function placeSideMark(glyphs, fonts, ch, cx, cy, size, pitchX, missing) { + let glyphCh = ch; + let rotate = 0; + if (VERTICAL_QUOTE[ch]) { + const [vertical, corner] = VERTICAL_QUOTE[ch]; + if (pick(fonts, vertical)) glyphCh = vertical; + else if (pick(fonts, corner)) { + glyphCh = corner; + rotate = Math.PI / 2; + } else rotate = Math.PI / 2; + } else if (ROTATE.has(ch)) rotate = Math.PI / 2; + const hit = pick(fonts, glyphCh); + if (!hit) { + missing.push(ch); + return; + } + const outline = hit.font.outline(hit.gid); + if (!outline) return; + const markSize = size * 0.5; + const scale = markSize / hit.font.unitsPerEm; + const pivot = [(outline.xMin + outline.xMax) / 2, (outline.yMin + outline.yMax) / 2]; + // 紧贴本字的右缘:放在列缝正中会分不清是这一列的还是右边那一列的。 + const halfInk = ((rotate ? outline.yMax - outline.yMin : outline.xMax - outline.xMin) * scale) / 2; + const gap = Math.max(0, pitchX - size); + const x = cx + size / 2 + halfInk + Math.min(gap * 0.12, size * 0.06); + glyphs.push({ font: hit.font, gid: hit.gid, ch, m: placeMatrix(scale, rotate, pivot, [x, cy]) }); +} + +// ---------------------------------------------------------------- 横排 + +function layoutHorizontal(fonts, text, box, options) { + const lineGap = options.lineSpacing === undefined ? 0.35 : Number(options.lineSpacing); + const letter = options.letterSpacing === undefined ? 0 : Number(options.letterSpacing); + const missing = []; + const chars = Array.from(String(text || '').replace(/\r\n?/g, '\n')); + const items = []; + for (const ch of chars) { + if (ch === '\n') { + items.push({ ch, newline: true }); + continue; + } + // 全角空格占一个字宽:段首空两格就是两个全角空格。其余空白当半角空格。 + if (ch === '\u3000') { + items.push({ ch, space: true, full: true }); + continue; + } + if (/\s/.test(ch)) { + items.push({ ch: ' ', space: true }); + continue; + } + const hit = pick(fonts, ch); + if (!hit) { + missing.push(ch); + continue; + } + const prev = items[items.length - 1]; + const sup = options.superscript !== false && NOTE_MARK.test(ch) && prev && !prev.newline && !prev.space && !NOTE_AFTER.has(prev.ch); + items.push({ ch, font: hit.font, gid: hit.gid, adv: (hit.font.advance(hit.gid) / hit.font.unitsPerEm) * (sup ? SUP_SCALE : 1), sup }); + } + // 断行单位:汉字一个字一个单位;连续的西文字母数字是一个词,不从中间断; + // 行首禁则标点并进前一个单位,行尾禁则标点并进后一个单位。 + const units = []; + let word = null; + for (const item of items) { + if (item.newline) { + word = null; + units.push({ newline: true }); + continue; + } + if (item.space) { + word = null; + units.push({ space: true, full: Boolean(item.full), items: [{ ch: item.ch, adv: item.full ? 1 : 0.3 }] }); + continue; + } + const latin = /[0-9A-Za-z\u00C0-\u024F'@#&_\-./:%+]/.test(item.ch) && !isCjk(item.ch); + if (latin && word) { + word.items.push(item); + continue; + } + const unit = { items: [item] }; + word = latin ? unit : null; + const prev = units[units.length - 1]; + // 上标的脚注符号和它前面的字绑在一起:「闰土①」不能把①甩到下一行行首。 + if ((NO_START.has(item.ch) || item.sup) && prev && !prev.newline && !prev.space) { + prev.items.push(item); + word = null; + continue; + } + if (prev && prev.openTail) { + prev.items.push(item); + prev.openTail = NO_END.has(item.ch); + if (latin) word = prev; + continue; + } + unit.openTail = NO_END.has(item.ch); + units.push(unit); + } + const widthOf = (unit, size) => unit.items.reduce((sum, item) => sum + item.adv * size + letter * size, 0); + + function breakLines(size) { + const lines = []; + let current = []; + let width = 0; + // 段首(开头、换行之后)的空格是有意写的:全角空格是缩进,半角空格用来对齐(规划把矩阵逐行写成 + // 「A = [ 1 2 3 ]」「 [ 4 5 6 ]」)。只有自动折行到下一行的行首空白才去掉。 + let paragraphStart = true; + const push = () => { + while (current.length && current[current.length - 1].space) current.pop(); + lines.push(current); + current = []; + width = 0; + }; + for (const unit of units) { + if (unit.newline) { + current.paraEnd = true; + push(); + paragraphStart = true; + continue; + } + if (unit.space && !current.length && !paragraphStart) continue; + if (!unit.space) paragraphStart = false; + const w = widthOf(unit, size); + if (width + w <= box.w + 0.01 || !current.length) { + if (w > box.w + 0.01 && !unit.space) { + // 一个词比整行还宽:按字拆开。 + for (const item of unit.items) { + const iw = item.adv * size + letter * size; + if (width + iw > box.w + 0.01 && current.length) push(); + current.push({ items: [item] }); + width += iw; + } + continue; + } + current.push(unit); + width += w; + } else { + push(); + if (unit.space) continue; + current.push(unit); + width = w; + } + } + if (current.length) { + current.paraEnd = true; + push(); + } + return lines; + } + + function fitsAt(size) { + const lines = breakLines(size); + const height = lines.length * size * (1 + lineGap) - size * lineGap; + if (height > box.h + 0.01) return null; + for (const lineUnits of lines) { + const w = lineUnits.reduce((sum, unit) => sum + widthOf(unit, size), 0); + if (w > box.w + 0.5) return null; + } + if (options.maxLines && lines.length > options.maxLines) return null; + return lines; + } + + let lo = Math.max(1, options.minSize ? options.minSize * 0.5 : 4); + let hi = Math.min(box.h, options.maxSize || Infinity); + let lines = fitsAt(hi); + let size = hi; + if (!lines) { + for (let i = 0; i < 30; i += 1) { + const mid = (lo + hi) / 2; + if (fitsAt(mid)) lo = mid; + else hi = mid; + } + size = lo; + lines = fitsAt(size); + } + if (!lines) return { fits: false, reason: 'too_small', size: 0, glyphs: [], lines: 0, missing: [...new Set(missing)], dropped: [] }; + const fits = size >= (options.minSize || 0); + const lineHeight = size * (1 + lineGap); + const blockH = lines.length * lineHeight - size * lineGap; + let top = box.y + (box.h - blockH) / 2; + if (options.valign === 'start') top = box.y; + else if (options.valign === 'end') top = box.y + box.h - blockH; + const glyphs = []; + lines.forEach((lineUnits, li) => { + const items = lineUnits.flatMap((unit) => unit.items); + const w = items.reduce((sum, item) => sum + item.adv * size + letter * size, 0) - letter * size; + let x = box.x; + if (options.align === 'center') x = box.x + (box.w - w) / 2; + else if (options.align === 'end') x = box.x + box.w - w; + // 两端对齐(正文、脚注):除了每段最后一行,把这一行剩下的空当平均分进字距,右边缘才齐。 + // 差得太多(一行里只有一个长单词)就不硬撑,免得字被拉得很散。 + let spread = 0; + if (options.justify && !lineUnits.paraEnd && items.length > 1 && (!options.align || options.align === 'start')) { + spread = Math.max(0, box.w - w) / (items.length - 1); + if (spread > size * 0.35) spread = 0; + } + const emTop = top + li * lineHeight; + for (const item of items) { + if (item.font) { + // 上标:字身缩到 SUP_SCALE,顶边比这一行的字身顶边略高一点,落在前一个字的右上角。 + const glyphSize = item.sup ? size * SUP_SCALE : size; + const glyphTop = item.sup ? emTop - size * 0.06 : emTop; + const scale = glyphSize / item.font.unitsPerEm; + const baseline = glyphTop + glyphSize / 2 + emCenter(item.font) * scale; + glyphs.push({ font: item.font, gid: item.gid, ch: item.ch, m: [scale, 0, 0, -scale, x, baseline] }); + } + x += item.adv * size + letter * size + spread; + } + }); + return { + fits, + reason: fits ? '' : 'too_small', + size: round2(size), + glyphs, + lines: lines.length, + rows: lines.length, + cols: 0, + missing: [...new Set(missing)], + dropped: [], + superscripts: items.filter((item) => item.sup).length, + // 文字块实际占的上沿(像素):脚注上方的分隔线画在它上面。 + top, + font: fonts[0] ? fonts[0].family : '', + }; +} + +function round2(value) { + return Math.round(value * 100) / 100; +} diff --git a/plugins/liddalidd/h3-image-executor/miniapp/node/lib/textoverlay.mjs b/plugins/liddalidd/h3-image-executor/miniapp/node/lib/textoverlay.mjs new file mode 100644 index 00000000..158483cc --- /dev/null +++ b/plugins/liddalidd/h3-image-executor/miniapp/node/lib/textoverlay.mjs @@ -0,0 +1,1023 @@ +// 程序排字:生图模型写长段文字、竖排、书法时很容易写错、漏字、编字(真机 20261113-01b902: +// 324 字的《兰亭集序》长卷,第一轮的候选有臆造字,交付的那张漏了 36 个字,评审看不清只能判「拿不准」放行)。 +// 兜底做法是让模型只画一张不带字的底图(纸面、招牌、海报底板留空),再由程序用字体把原文逐字排进去。 +// +// 步骤(每个文字层各做一遍): +// 1. 找空白:在规划给的位置附近找一块颜色均匀、没有笔画和纹样的区域(底图上的空白纸面)。 +// 模型不一定照着位置留空,所以以规划的位置为准、和实际空白求交;交出来太小或者区域里有字迹,就判不通过重画。 +// 2. 配字体、排版:按规划的书体选字体(缺字时逐字用别的字体补),横排自动换行、竖排从右往左分列,取排得下的最大字号。 +// 3. 合成:深色字按「墨落在纸上」的方式叠(跟着纸面原来的明暗和纹理走),浅色字直接盖上; +// 毛笔字加一点墨色浓淡和边缘洇开,免得像贴上去的。 +// 4. 按原格式编码成最终文件(JPEG 用高质量 93,和换二维码一致)。 +import { layoutText, drawGlyphs, drawGlyphsSplit, glyphCount, glyphRects, isPunct, probeSplit, splittableChar } from './textlayout.mjs'; +import { resolveFonts, normalizeFontKind, FONT_KINDS } from './fonts.mjs'; +import { encodePng } from './png.mjs'; +import { encodeJpeg } from './jpegenc.mjs'; + +const JPEG_QUALITY = 93; +// 找空白用的工作分辨率(长边)。 +const WORK_EDGE = 768; +// 规划的位置和实际空白求交后,至少还要剩这么多(占规划面积的比例)。 +const MIN_COVERAGE = 0.55; +// 排进去的字至少这么大(占图片短边的比例),再小就不算「每个字都能认」。 +const MIN_SIZE_RATIO = 0.008; +const MIN_SIZE_PX = 14; + +export const TEXT_REASONS = { + not_blank: '底图上预留给文字的地方没有留空', + marks: '底图上预留给文字的地方已经有字迹或印章', + too_small: '预留的空白太小,排不下全部文字', + overlap: '两段文字排到了一起', + no_font: '找不到可用的字体', + missing_glyphs: '字体里缺字', + empty: '没有要排的文字', + error: '排字出错', +}; + +const tick = () => new Promise((resolve) => setImmediate(resolve)); + +// ---------------------------------------------------------------- 对外入口 + +// 在已解码的图上排字,返回最终文件的字节。layers:[{ text, box:[左,上,右,下], direction, font, color, align, punctuation, role }]。 +// 返回 { ok, buffer, format, image, results };有一层没排成就整体不通过,buffer 为 null(排了一半的图不交付)。 +// 先每层各自找空白、排版(别的层的规划位置和已经排好的字都算占用,一层字不会挪到别的层上去), +// 再把同一角色(正文、脚注、标题……)的字号统一成其中最小的,然后查一遍有没有两层字叠在一起,最后才画。 +// 一层一层各取「排得下的最大字号」的话,两段正文一个 118、一个 128 像素(真机 20261116-41535c 的课本页)。 +export async function overlayText(image, format, layers, options) { + const opts = options || {}; + const startedAt = Date.now(); + const list = (Array.isArray(layers) ? layers : []).filter((layer) => layer && String(layer.text || '').trim()); + if (!list.length) return { ok: false, buffer: null, format, image, results: [], ms: 0 }; + const out = { ...image, data: Buffer.from(image.data) }; + const work = analysisImage(image); + const plannedBoxes = list.map((layer) => boxToPixels(layer.box, image.width, image.height)); + const seeds = surfaceSeeds(work, list, plannedBoxes); + const prepared = []; + const placed = []; + for (let i = 0; i < list.length; i += 1) { + const blocked = [...plannedBoxes.filter((_, j) => j !== i).map((box) => ({ ...box, own: true })), ...placed]; + let item; + try { + item = await prepareLayer(work, image, list[i], opts, { planned: plannedBoxes[i], seed: seeds[i], blocked }); + } catch (error) { + item = { result: failure(list[i], 'error', String((error && error.message) || error)) }; + } + if (item.layout) { + const pad = item.layout.size * 0.08; + for (const rect of glyphRects(item.layout.glyphs)) placed.push({ x: rect.x0 - pad, y: rect.y0 - pad, w: rect.x1 - rect.x0 + 2 * pad, h: rect.y1 - rect.y0 + 2 * pad }); + } + prepared.push(item); + await tick(); + } + const caps = new Map(); + for (const item of prepared) { + if (!item.layout || !item.role) continue; + caps.set(item.role, Math.min(caps.has(item.role) ? caps.get(item.role) : Infinity, item.layout.size)); + } + // 层级:脚注、页码、图注不能比正文大。规划给的框太高时,按框取的最大字号会让页码比正文还大一号。 + const body = caps.get('body'); + if (body) for (const role of ['footnote', 'page', 'caption']) if (caps.has(role)) caps.set(role, Math.min(caps.get(role), body * 0.82)); + for (const item of prepared) { + if (!item.layout || !item.role) continue; + const cap = caps.get(item.role); + if (cap < item.layout.size - 0.5) { + const again = layoutText(item.fontInfo.fonts, item.text, item.box, { ...item.layoutOptions, maxSize: cap }); + if (again.fits) { + item.layout = again; + item.unified = true; + } + } + } + // 两层字叠在一起就不交付:以前每层各找各的空白,两段字可以排进同一块地方(真机 20261116-06a562, + // 页眉和脚注叠在一起又压在矩阵上,评审只给了质量分,照样交付)。 + const rects = prepared.map((item) => (item.layout ? glyphRects(item.layout.glyphs) : [])); + prepared.forEach((item, i) => { + if (!item.layout) return; + const hit = prepared.findIndex((other, j) => j < i && other.layout && rectsOverlap(rects[i], rects[j])); + if (hit < 0) return; + item.result = { ...failure(item.layer, 'overlap', '和「' + shortText(prepared[hit].text, 12) + '」叠在一起'), ...item.base, box: normalizedBox(item.box, image) }; + item.layout = null; + }); + prepared.forEach((item, i) => { + item.others = item.layout ? rects.filter((_, j) => j !== i && prepared[j].layout).flat() : []; + }); + const results = []; + for (const item of prepared) { + if (!item.layout) { + results.push(item.result); + continue; + } + try { + results.push(drawLayer(out, image, item)); + } catch (error) { + results.push({ ...failure(item.layer, 'error', String((error && error.message) || error)), ...item.base }); + } + await tick(); + } + const ok = results.every((item) => item.ok); + if (!ok) return { ok, buffer: null, format, image, results, ms: Date.now() - startedAt }; + const outFormat = format === 'png' ? 'png' : 'jpeg'; + const buffer = outFormat === 'png' ? encodePng(out, { level: 6 }) : encodeJpeg(out, { quality: JPEG_QUALITY }); + return { ok, buffer, format: outFormat, image: out, results, ms: Date.now() - startedAt }; +} + +// 两组字形矩形有没有交叠。各往里收一点:上标贴着前一个字、拼音贴着汉字这类紧挨着的不算。 +function rectsOverlap(a, b) { + for (const p of a) { + const sp = Math.min(p.x1 - p.x0, p.y1 - p.y0) * 0.12; + for (const q of b) { + const sq = Math.min(q.x1 - q.x0, q.y1 - q.y0) * 0.12; + if (p.x0 + sp < q.x1 - sq && q.x0 + sq < p.x1 - sp && p.y0 + sp < q.y1 - sq && q.y0 + sq < p.y1 - sp) return true; + } + } + return false; +} + +function shortText(value, n) { + const chars = Array.from(String(value || '').replace(/\s+/g, ' ').trim()); + return chars.length > n ? chars.slice(0, n).join('') + '…' : chars.join(''); +} + +// 同一块载体(规划写的 surface 相同)上的几层字,某一层的规划位置整个落在载体外面时 +// (页眉的框画在了桌面上:模型把整页画小了一圈,四周露出桌面),这一层找空白时改用同组大多数层的样本。 +// 不换的话它会把桌面当成空白,字排到页面外面去。只换亮度和大多数差得很远的(桌面对纸面), +// 纸面上的浅色色块和纸面差得不多,不换:规划写在色块里的字照旧排在色块里。 +function surfaceSeeds(work, layers, boxes) { + const own = boxes.map((box) => pickSeed(work, box)); + const out = new Array(layers.length).fill(null); + const groups = new Map(); + layers.forEach((layer, i) => { + const key = String(layer.surface || '').trim(); + if (!key || !own[i]) return; + if (!groups.has(key)) groups.set(key, []); + groups.get(key).push(i); + }); + for (const members of groups.values()) { + if (members.length < 3) continue; + let center = members[0]; + let best = -1; + for (const i of members) { + const count = members.filter((j) => Math.abs(own[j].lum - own[i].lum) <= 60).length; + if (count > best) { + best = count; + center = i; + } + } + if (best < Math.max(2, Math.ceil(members.length * 0.6))) continue; + for (const i of members) if (Math.abs(own[i].lum - own[center].lum) > 60) out[i] = own[center]; + } + return out; +} + +const ROLES = new Set(['title', 'subtitle', 'body', 'footnote', 'caption', 'label', 'page', 'pinyin']); + +// 统一字号按什么分组。规划写了 role 就按 role;没写的老会话,横排的长段落按「字体 + 颜色」分组: +// 黑色宋体的几段正文是一组,灰色的脚注是另一组,不会被拉成一样大。 +function roleOf(layer, text, direction) { + const role = String(layer.role || '').trim().toLowerCase(); + if (ROLES.has(role)) return role; + if (direction !== 'horizontal' || glyphCount(text) < 30) return ''; + return 'auto:' + normalizeFontKind(layer.font) + ':' + String(layer.color || '').toLowerCase(); +} + +// 首尾只去半角空白和换行:段首的全角空格是缩进,以前一个 trim() 就把它删了。 +function cleanText(value) { + return String(value || '') + .replace(/\r\n?/g, '\n') + .replace(/^[ \t\n\f\v\u00a0]+/, '') + .replace(/[\s\u3000]+$/, ''); +} + +// 正文段落首行空两格。规划写了「  」就照它的,没写才补;拉丁字母开头的段落(英文)不补。 +function indentParagraphs(text, count) { + if (!(count > 0)) return text; + return text + .split('\n') + .map((line) => (!line || line.startsWith('\u3000') || /^[A-Za-z0-9]/.test(line) ? line : '\u3000'.repeat(count) + line)) + .join('\n'); +} + +function failure(layer, reason, detail) { + return { + text: String(layer.text || ''), + ok: false, + reason, + reasonText: TEXT_REASONS[reason] || reason, + detail: detail || '', + }; +} + +// 部件分色:part = { side: left / right / top / bottom, color },这一层每个汉字靠 side 那一侧的部件用 part.color, +// 其余用这一层的 color。规划写的方向词中英文都认。 +const PART_SIDE_WORDS = { left: 'left', right: 'right', top: 'top', bottom: 'bottom', 左: 'left', 右: 'right', 上: 'top', 下: 'bottom', 左边: 'left', 右边: 'right', 上边: 'top', 下边: 'bottom', 上面: 'top', 下面: 'bottom' }; + +export function normalizePart(raw) { + if (!raw || typeof raw !== 'object') return null; + const side = PART_SIDE_WORDS[String(raw.side || '').trim().toLowerCase()] || ''; + const color = String(raw.color || '').trim(); + if (!side || !/^#?[0-9a-f]{3}([0-9a-f]{3})?$/i.test(color)) return null; + return { side, color: color.startsWith('#') ? color : '#' + color }; +} + +// 切线一行都没碰到笔画最好;碰到的不超过 2% 算干净,不超过 6% 还能用(笔画中间换色,近看看得出来)。 +const PART_CLEAN_CUT = 0.02; +const PART_MAX_CUT = 0.06; + +// 给要分色的一层挑字体:先试规划要的字体,切不干净再试宋体、黑体。毛笔字常把偏旁和右边连成一笔, +// 「楷体」在没有楷体字体的机器上也会落到毛笔字(见 fonts.mjs)。都切不开就整字一种颜色,照实写进结果。 +async function choosePartFonts(kind, text, side, current, opts) { + const chars = [...new Set(Array.from(text).filter((ch) => splittableChar(ch)))]; + if (!chars.length) return { fontInfo: current, ok: false, note: '这一层没有可以分色的汉字' }; + const resolve = opts.resolveFonts || resolveFonts; + const tried = []; + let best = null; + let firstBad = ''; + for (const candidate of [kind, 'song', 'hei']) { + const info = candidate === kind ? current : await resolve(candidate, text); + if (!info.fonts.length || info.missing.length) continue; + if (tried.some((item) => item.label === info.label)) continue; + let worst = 0; + let bad = ''; + for (const ch of chars) { + const font = info.fonts.find((item) => item.glyphId(ch.codePointAt(0))); + const probe = font ? probeSplit(font, ch, side) : { ok: false }; + if (!probe.ok) { + bad = ch; + break; + } + worst = Math.max(worst, probe.cut); + } + tried.push({ label: info.label, bad, worst }); + if (bad) { + firstBad = firstBad || bad; + continue; + } + if (worst <= PART_CLEAN_CUT) return { fontInfo: info, ok: true, worst, note: switchNote(tried, info, current) }; + if (!best || worst < best.worst) best = { info, worst }; + } + if (best && best.worst <= PART_MAX_CUT) return { fontInfo: best.info, ok: true, worst: best.worst, note: switchNote(tried, best.info, current) }; + return { + fontInfo: current, + ok: false, + note: '「' + (firstBad || chars.join('')) + '」在可用的字体里' + SIDE_LABELS[side] + '的部件和其余部分连在一起,分不开颜色,整字用一种颜色', + }; +} + +const SIDE_LABELS = { left: '左边', right: '右边', top: '上边', bottom: '下边' }; + +function switchNote(tried, chosen, current) { + if (chosen.label === current.label) return ''; + const first = tried.find((item) => item.label === current.label); + const why = !first ? '分不了色' : first.bad ? '「' + first.bad + '」分不开部件' : '分色要切断笔画'; + return current.label + '里' + why + ',这一层改用' + chosen.label; +} + +// 第一遍:配字体、找空白、按这一层自己的框排出能排下的最大字号。失败时返回 { result },成功时带上排版结果。 +// ctx:planned(规划位置,像素)、seed(同一块载体上别的层的样本,见 surfaceSeeds)、blocked(别的层占着的地方)。 +async function prepareLayer(work, original, layer, opts, ctx) { + const context = ctx || {}; + const direction = layer.direction === 'vertical' ? 'vertical' : 'horizontal'; + const raw = cleanText(layer.text); + if (!raw.trim()) return { result: failure(layer, 'empty') }; + const role = roleOf(layer, raw, direction); + const indent = direction === 'horizontal' && String(layer.role || '').toLowerCase() === 'body' ? clampInt(layer.indent === undefined || layer.indent === null ? 2 : Number(layer.indent), 0, 4) : 0; + const text = indentParagraphs(raw, indent); + const kind = normalizeFontKind(layer.font); + let fontInfo = await (opts.resolveFonts || resolveFonts)(kind, text); + if (!fontInfo.fonts.length) return { result: failure(layer, 'no_font') }; + if (fontInfo.missing.length) return { result: failure(layer, 'missing_glyphs', fontInfo.missing.slice(0, 12).join('')) }; + const wantPart = normalizePart(layer.part); + let part = null; + if (wantPart) { + const chosen = await choosePartFonts(kind, text, wantPart.side, fontInfo, opts); + fontInfo = chosen.fontInfo; + part = { ...wantPart, ok: chosen.ok, note: chosen.note || '', font: fontInfo.label }; + } + + const planned = context.planned || boxToPixels(layer.box, original.width, original.height); + const region = findTextRegion(work, planned, original, { seed: context.seed, blocked: context.blocked, nudge: true }); + const base = { + text: text.trim(), + kind: fontInfo.kind, + font: fontInfo.label, + planned: normalizedBox(planned, original), + blank: region.blank ? normalizedBox(region.blank, original) : null, + coverage: round(region.coverage, 3), + marks: round(region.marks, 4), + role, + // 规划位置不空、就近上下挪过的距离(占画面高度的比例,正数是往下)。 + moved: region.ok && region.moved ? round(region.moved / original.height, 4) : 0, + surfaceSeed: Boolean(context.seed), + }; + if (!region.ok) return { result: { ...failure(layer, region.reason, region.detail), ...base } }; + const box = region.box; + const minSize = Math.max(MIN_SIZE_PX, Math.min(original.width, original.height) * MIN_SIZE_RATIO); + const layoutOptions = { + direction, + align: layer.align, + valign: layer.valign, + punctuation: layer.punctuation || (direction === 'vertical' ? 'inline' : undefined), + handwriting: fontInfo.handwriting, + minSize, + maxSize: layer.maxSize || undefined, + lineSpacing: layer.lineSpacing, + letterSpacing: layer.letterSpacing, + // 正文、脚注(以及老会话里推断出来的长段落)两端对齐。 + justify: role === 'body' || role === 'footnote' || role.startsWith('auto:'), + }; + let layout = layoutText(fontInfo.fonts, text, box, layoutOptions); + // 标题、标签、示例词这类短文字默认排一行:程序取能放下的最大字号,框高一点就会把「清水」排成上下两行、 + // 把「+ 青 =」的「=」挤到下一行(真机 20261117-f57a0f)。只有排一行时字号比排几行小太多(不到六成)才换行。 + if (ONE_LINE_ROLES.has(role) && direction === 'horizontal' && layout.fits && layout.lines > 1 && !text.includes('\n')) { + const one = layoutText(fontInfo.fonts, text, box, { ...layoutOptions, maxLines: 1 }); + if (one.fits && one.size >= layout.size * 0.6) { + layout = one; + layoutOptions.maxLines = 1; + } + } + if (!layout.fits) { + return { + result: { + ...failure(layer, 'too_small', '能排下的最大字号 ' + Math.round(layout.size || 0) + 'px,低于 ' + Math.round(minSize) + 'px'), + ...base, + box: normalizedBox(box, original), + }, + }; + } + return { layer, text, role, indent, direction, fontInfo, region, box, layoutOptions, layout, base, unified: false, part }; +} + +const ONE_LINE_ROLES = new Set(['title', 'subtitle', 'label', 'caption', 'page', 'pinyin']); + +// 第二遍:定颜色、画字。脚注层先在字的上方画一条短分隔线(课本、书刊的脚注都和正文隔开), +// 空白的上沿离字太近、画不下线的,把字往下挪一点再排。 +function drawLayer(out, original, item) { + const { layer, region, fontInfo, direction } = item; + let { layout, box } = item; + const ink = resolveInk(layer.color, region.seed); + let rule = null; + if (item.role === 'footnote' && direction === 'horizontal' && layer.rule !== false) { + const thick = Math.max(2, Math.round(Math.min(original.width, original.height) * 0.0011)); + const gap = Math.max(thick * 3, layout.size * 0.5); + const blankTop = region.blank ? region.blank.y : box.y; + let y = layout.top - gap; + if (y - thick < blankTop + 2) { + const need = Math.ceil(blankTop + 2 + thick - y); + const lowered = { ...box, y: box.y + need, h: box.h - need }; + const again = lowered.h > 0 ? layoutText(fontInfo.fonts, item.text, lowered, { ...item.layoutOptions, maxSize: layout.size }) : null; + if (again && again.fits) { + layout = again; + box = lowered; + y = layout.top - gap; + } else { + y = null; + } + } + if (y !== null) rule = { x: box.x, y, w: box.w * 0.28, h: thick }; + // 线会碰到别的层的字就不画:真机 20261116-06a562 脚注被挪到正文中间,分隔线横穿了矩阵。 + if (rule) { + const reach = thick * 3; + const hit = (item.others || []).some((r) => r.x0 < rule.x + rule.w && r.x1 > rule.x && r.y0 < rule.y + reach && r.y1 > rule.y - reach); + if (hit) rule = null; + } + } + // 蒙版比排版框大一圈:字旁的小标点、手写抖动会出框一点。 + const margin = Math.ceil(layout.size * 0.8) + 4; + const mx0 = Math.max(0, Math.floor(box.x - margin)); + const my0 = Math.max(0, Math.floor(Math.min(box.y, rule ? rule.y - rule.h : box.y) - margin)); + const mx1 = Math.min(out.width, Math.ceil(box.x + box.w + margin)); + const my1 = Math.min(out.height, Math.ceil(box.y + box.h + margin)); + const mask = { x: mx0, y: my0, w: mx1 - mx0, h: my1 - my0, data: new Float32Array((mx1 - mx0) * (my1 - my0)) }; + // 部件分色:偏旁那一侧画进另一张蒙版,各自上色。 + const partInk = item.part && item.part.ok ? resolveInk(item.part.color, region.seed) : null; + const partMask = partInk ? { ...mask, data: new Float32Array(mask.data.length) } : null; + const partReport = partMask ? drawGlyphsSplit(mask, partMask, layout.glyphs, item.part.side) : null; + if (!partMask) drawGlyphs(mask, layout.glyphs); + if (rule) fillRule(mask, rule); + finishMask(mask, layout.size, fontInfo.handwriting, original); + blend(out, mask, ink, region.seed); + if (partMask) { + finishMask(partMask, layout.size, fontInfo.handwriting, original); + blend(out, partMask, partInk, region.seed); + } + const part = item.part + ? { + side: item.part.side, + color: partInk ? toHex(partInk.rgb) : item.part.color, + ok: Boolean(partReport && partReport.split && !partReport.failed.length), + split: partReport ? partReport.split : 0, + failed: partReport ? partReport.failed.join('') : '', + cut: partReport ? round(partReport.maxCut, 3) : 0, + font: item.part.font, + note: item.part.ok + ? partReport && partReport.failed.length + ? '「' + partReport.failed.join('') + '」分不开部件,整字用一种颜色' + : item.part.note + : item.part.note, + } + : null; + return { + ...item.base, + ok: true, + reason: '', + reasonText: '', + detail: '', + box: normalizedBox(box, original), + direction, + punctuation: direction === 'vertical' ? layer.punctuation || 'inline' : 'inline', + size: Math.round(layout.size), + rows: layout.rows, + cols: layout.cols, + lines: layout.lines, + glyphs: layout.glyphs.length, + chars: glyphCount(item.text), + dropped: layout.dropped.length, + substituted: fontInfo.substituted.join(''), + color: toHex(ink.rgb), + autoColor: ink.auto, + blend: ink.mode, + indent: item.indent, + unified: Boolean(item.unified), + superscripts: layout.superscripts || 0, + rule: Boolean(rule), + ...(part ? { part } : {}), + }; +} + +// 分隔线:一条实心横线,上下边缘按覆盖面积抗锯齿,和字一起进蒙版、一起上色。 +function fillRule(mask, rule) { + const y0 = rule.y - rule.h / 2; + const y1 = rule.y + rule.h / 2; + for (let y = Math.floor(y0); y < Math.ceil(y1); y += 1) { + const my = y - mask.y; + if (my < 0 || my >= mask.h) continue; + const cover = Math.max(0, Math.min(y1, y + 1) - Math.max(y0, y)); + for (let x = Math.floor(rule.x); x < Math.ceil(rule.x + rule.w); x += 1) { + const mx = x - mask.x; + if (mx < 0 || mx >= mask.w) continue; + const cx = Math.max(0, Math.min(rule.x + rule.w, x + 1) - Math.max(rule.x, x)); + const at = my * mask.w + mx; + mask.data[at] = Math.max(mask.data[at], cover * cx); + } + } +} + +// ---------------------------------------------------------------- 位置 + +// 规划写的 [左, 上, 右, 下](0-1 的画面比例)换成像素框。缺了、写错了(退化成一条线)才用居中 80% 的框。 +function boxToPixels(raw, width, height) { + let box = Array.isArray(raw) && raw.length === 4 ? raw.map(Number) : null; + if (!box || box.some((value) => !Number.isFinite(value))) box = [0.1, 0.1, 0.9, 0.9]; + // 写成百分数的([6, 14, 94, 86])也认。 + if (box.some((value) => value > 1.5)) box = box.map((value) => value / 100); + let [left, top, right, bottom] = box.map((value) => Math.min(1, Math.max(0, value))); + if (right < left) [left, right] = [right, left]; + if (bottom < top) [top, bottom] = [bottom, top]; + if (right - left < 0.002 || bottom - top < 0.002) [left, top, right, bottom] = [0.1, 0.1, 0.9, 0.9]; + // 单行的页眉、脚注、页码本来就矮(4K 竖图上 1.8% 也有 85 像素):保留位置,只把太窄的一边按中线撑到 2%。 + // 以前直接换成居中 80% 的框,页眉和脚注就被排进了页面正中,叠在一起又压在正文上(真机 20261116-06a562)。 + [left, right] = widen(left, right, 0.02); + [top, bottom] = widen(top, bottom, 0.02); + return { x: left * width, y: top * height, w: (right - left) * width, h: (bottom - top) * height }; +} + +function widen(a, b, min) { + if (b - a >= min) return [a, b]; + let lo = (a + b) / 2 - min / 2; + let hi = lo + min; + if (lo < 0) [lo, hi] = [0, min]; + if (hi > 1) [lo, hi] = [1 - min, 1]; + return [lo, hi]; +} + +function normalizedBox(box, image) { + return [box.x / image.width, box.y / image.height, (box.x + box.w) / image.width, (box.y + box.h) / image.height].map((value) => round(value, 4)); +} + +// 找空白的工作图:按整数倍块平均缩小,记下每个点的颜色、亮度和梯度。 +function analysisImage(image) { + const factor = Math.max(1, Math.ceil(Math.max(image.width, image.height) / WORK_EDGE)); + const w = Math.max(1, Math.floor(image.width / factor)); + const h = Math.max(1, Math.floor(image.height / factor)); + const r = new Float32Array(w * h); + const g = new Float32Array(w * h); + const b = new Float32Array(w * h); + const { data, channels } = image; + const area = factor * factor; + for (let y = 0; y < h; y += 1) { + for (let x = 0; x < w; x += 1) { + let sr = 0; + let sg = 0; + let sb = 0; + for (let yy = 0; yy < factor; yy += 1) { + let i = ((y * factor + yy) * image.width + x * factor) * channels; + for (let xx = 0; xx < factor; xx += 1, i += channels) { + if (channels >= 3) { + sr += data[i]; + sg += data[i + 1]; + sb += data[i + 2]; + } else { + sr += data[i]; + sg += data[i]; + sb += data[i]; + } + } + } + const at = y * w + x; + r[at] = sr / area; + g[at] = sg / area; + b[at] = sb / area; + } + } + const lum = new Float32Array(w * h); + for (let i = 0; i < lum.length; i += 1) lum[i] = 0.299 * r[i] + 0.587 * g[i] + 0.114 * b[i]; + const grad = new Float32Array(w * h); + for (let y = 1; y < h - 1; y += 1) { + for (let x = 1; x < w - 1; x += 1) { + const at = y * w + x; + grad[at] = Math.abs(lum[at + 1] - lum[at - 1]) + Math.abs(lum[at + w] - lum[at - w]); + } + } + // 大范围模糊后的颜色当作「这一带的底色」(见 carrierBackground):纸面从中间到边缘的明暗渐变不算笔画, + // 只有和这一带底色明显不一样的点才算有东西。 + const radius = Math.max(4, Math.round(Math.max(w, h) * 0.03)); + return { w, h, factor, r, g, b, lum, grad, radius }; +} + +function boxBlur(src, w, h, radius) { + const integral = new Float64Array((w + 1) * (h + 1)); + for (let y = 0; y < h; y += 1) { + let row = 0; + for (let x = 0; x < w; x += 1) { + row += src[y * w + x]; + integral[(y + 1) * (w + 1) + x + 1] = integral[y * (w + 1) + x + 1] + row; + } + } + const out = new Float32Array(w * h); + for (let y = 0; y < h; y += 1) { + const ya = Math.max(0, y - radius); + const yb = Math.min(h, y + radius + 1); + for (let x = 0; x < w; x += 1) { + const xa = Math.max(0, x - radius); + const xb = Math.min(w, x + radius + 1); + const sum = integral[yb * (w + 1) + xb] - integral[ya * (w + 1) + xb] - integral[yb * (w + 1) + xa] + integral[ya * (w + 1) + xa]; + out[y * w + x] = sum / ((xb - xa) * (yb - ya)); + } + } + return out; +} + +// 只拿「和样本同一块载体」的点求这一带的底色(按掩码加权的大范围模糊)。直接整片模糊会把旁边的深色背景、 +// 画面内容混进来:窄的横幅、招牌、标签条整条都被判成「和底色差得远」,25% 高的空白横幅都排不上字。 +// member:每个点(做过 3×3 平滑)的颜色离样本不超过 tolRegion,算作同一块载体;笔画、印章本身不在里面, +// 也就不会把底色拉偏。 +function carrierBackground(work, seed, tolRegion) { + const { w, h } = work; + const n = w * h; + const sr = boxBlur(work.r, w, h, 1); + const sg = boxBlur(work.g, w, h, 1); + const sb = boxBlur(work.b, w, h, 1); + const member = new Float32Array(n); + const mr = new Float32Array(n); + const mg = new Float32Array(n); + const mb = new Float32Array(n); + for (let i = 0; i < n; i += 1) { + const inside = Math.max(Math.abs(sr[i] - seed.r), Math.abs(sg[i] - seed.g), Math.abs(sb[i] - seed.b)) <= tolRegion; + if (!inside) continue; + member[i] = 1; + mr[i] = work.r[i]; + mg[i] = work.g[i]; + mb[i] = work.b[i]; + } + const weight = boxBlur(member, w, h, work.radius); + const br = boxBlur(mr, w, h, work.radius); + const bg = boxBlur(mg, w, h, work.radius); + const bb = boxBlur(mb, w, h, work.radius); + for (let i = 0; i < n; i += 1) { + const wt = weight[i]; + if (wt > 1e-3) { + br[i] /= wt; + bg[i] /= wt; + bb[i] /= wt; + } else { + br[i] = sr[i]; + bg[i] = sg[i]; + bb[i] = sb[i]; + } + } + return { member, br, bg, bb }; +} + +function carrierDistance(work, carrier, i) { + return Math.max(Math.abs(work.r[i] - carrier.br[i]), Math.abs(work.g[i] - carrier.bg[i]), Math.abs(work.b[i] - carrier.bb[i])); +} + +// 规划位置换成工作图坐标。 +function workRect(work, box) { + const f = work.factor; + return { + x0: clampInt(Math.round(box.x / f), 0, work.w - 1), + y0: clampInt(Math.round(box.y / f), 0, work.h - 1), + x1: clampInt(Math.round((box.x + box.w) / f), 1, work.w), + y1: clampInt(Math.round((box.y + box.h) / f), 1, work.h), + }; +} + +// 取样:把规划框内缩一圈分成 3×3 块,取纹理最平的一块当「空白长什么样」的样本。 +// 不直接取中心:规划框正中可能恰好被模型放了别的东西。 +function pickSeed(work, planned) { + const p = workRect(work, planned); + const inset = 0.12; + const sx0 = p.x0 + (p.x1 - p.x0) * inset; + const sy0 = p.y0 + (p.y1 - p.y0) * inset; + const sw = (p.x1 - p.x0) * (1 - 2 * inset); + const sh = (p.y1 - p.y0) * (1 - 2 * inset); + let seed = null; + for (let gy = 0; gy < 3; gy += 1) { + for (let gx = 0; gx < 3; gx += 1) { + const bx0 = Math.floor(sx0 + (sw * gx) / 3); + const by0 = Math.floor(sy0 + (sh * gy) / 3); + const bx1 = Math.max(bx0 + 1, Math.floor(sx0 + (sw * (gx + 1)) / 3)); + const by1 = Math.max(by0 + 1, Math.floor(sy0 + (sh * (gy + 1)) / 3)); + const stats = blockStats(work, bx0, by0, bx1, by1); + if (!stats) continue; + const centerPenalty = gx === 1 && gy === 1 ? 0 : 0.15; + const score = stats.grad + stats.mad * 0.5 + centerPenalty; + if (!seed || score < seed.score) seed = { ...stats, score, block: [bx0, by0, bx1, by1] }; + } + } + return seed; +} + +// 规划位置不空时,就近上下挪的距离(像素):先近后远,最远两个框高,但不超过画面高度的 6%。 +// 模型画的分隔线、色块边缘常常和规划差一点(真机 20261116-06a562:标题框压着模型画在 9% 处的横线), +// 往下挪半行就是空白;挪得太远就不是规划的版面了。 +function nudgeOffsets(planned, image) { + const step = Math.max(planned.h * 0.25, image.height * 0.004); + const limit = Math.min(Math.max(planned.h * 2, image.height * 0.03), image.height * 0.06); + const out = []; + for (let d = step; d <= limit + 1e-6; d += step) out.push(d, -d); + return out; +} + +// 在规划位置附近找空白。返回 { ok, box(排字框,像素), blank(找到的空白,像素), seed, coverage, marks, moved, reason }。 +// options.seed:用同一块载体上别的文字层的样本(这一层的规划位置整个落在载体外面时,见 surfaceSeeds); +// options.blocked:别的文字层占着的地方(它们的规划位置、已经排好的字,像素框),不算空白—— +// 一层字不能长到、挪到别的层上去。own:true 的框只挡挪动之后的位置,不挡这一层自己的规划位置 +// (规划的两个框本来就有一点重叠时,不该因此判这一层没留空); +// options.nudge:规划位置不空时就近上下挪(见 nudgeOffsets)。 +function findTextRegion(work, planned, image, options) { + const opts = options || {}; + const f = work.factor; + const W = work.w; + const H = work.h; + const p = workRect(work, planned); + const seed = opts.seed || pickSeed(work, planned); + if (!seed) return { ok: false, reason: 'not_blank', detail: '规划的位置超出画面', coverage: 0, marks: 0, seed: null }; + const tolRegion = 42; + const carrier = carrierBackground(work, seed, tolRegion); + // 容差按样本自己的纹理定:宣纸的纤维、帘纹本身就有起伏(真机底图亮度偏差 99 分位到 18、梯度 99 分位到 32), + // 按中位数定阈值会把正常纸纹当成笔画,整张空白纸判成「没留空」。墨迹的偏差在 100 以上,留出的余量足够分开。 + const local95 = blockPercentile(work, seed.block, (i) => carrierDistance(work, carrier, i), 0.95); + const tolColor = clamp(Math.max(local95 * 2, seed.mad * 4) + 10, 22, 55); + const tolGrad = clamp(seed.grad90 * 2.2, 30, 90); + const blank = new Uint8Array(W * H); + for (let i = 0; i < blank.length; i += 1) { + blank[i] = carrier.member[i] && carrierDistance(work, carrier, i) <= tolColor && work.grad[i] <= tolGrad ? 1 : 0; + } + for (const block of opts.blocked || []) { + const r = workRect(work, block); + for (let y = r.y0; y < r.y1; y += 1) { + for (let x = r.x0; x < r.x1; x += 1) { + if (block.own && x >= p.x0 && x < p.x1 && y >= p.y0 && y < p.y1) continue; + blank[y * W + x] = 0; + } + } + } + // 一个点算「空白」,要求它周围一小块里九成以上都是空白:纸纹、零星噪点放过,笔画、印章、纹样挡住。 + // 「一小块」不超过规划位置短边的五分之一:窄横幅只有十几个点高,按整张图定的窗口会把上下边各啃掉一大截。 + const plannedSide = Math.min(p.x1 - p.x0, p.y1 - p.y0); + const k = Math.max(3, Math.min(Math.round(Math.max(W, H) * 0.012), Math.round(plannedSide * 0.2))); + const dense = denseMask(blank, W, H, k, 0.9); + const plannedArea = Math.max(1, planned.w * planned.h); + + const attempt = (target) => { + const t = workRect(work, target); + const grow = 0.2; + const win = { + x0: clampInt(Math.floor(t.x0 - (t.x1 - t.x0) * grow), 0, W), + y0: clampInt(Math.floor(t.y0 - (t.y1 - t.y0) * grow), 0, H), + x1: clampInt(Math.ceil(t.x1 + (t.x1 - t.x0) * grow), 0, W), + y1: clampInt(Math.ceil(t.y1 + (t.y1 - t.y0) * grow), 0, H), + }; + const rect = maxRectangle(dense, W, win); + if (!rect.area) return { ok: false, reason: 'not_blank', detail: '规划位置附近没有一块空白', coverage: 0, marks: 0, seed }; + const blankPx = { x: rect.x0 * f, y: rect.y0 * f, w: (rect.x1 - rect.x0) * f, h: (rect.y1 - rect.y0) * f }; + // 排字框 = 规划位置 ∩ 空白内缩一圈:字贴着空白的边缘写,看起来像写出界了。内缩量按空白自己的大小封顶: + // 按整张图的比例算,一条窄横幅上下各缩掉一大截,剩下的不够规划位置的一半,空白的横幅也会判成没留空。 + const side = Math.min(blankPx.w, blankPx.h); + const pad = Math.round(Math.min(Math.max(Math.min(image.width, image.height) * 0.02, side * 0.035), side * 0.08)); + const inner = { x: blankPx.x + pad, y: blankPx.y + pad, w: blankPx.w - 2 * pad, h: blankPx.h - 2 * pad }; + const box = intersect(target, inner); + const coverage = box ? (box.w * box.h) / plannedArea : 0; + if (!box || coverage < MIN_COVERAGE) { + return { ok: false, reason: 'not_blank', detail: '规划位置只有 ' + Math.round(coverage * 100) + '% 是空白', coverage, marks: 0, seed, blank: blankPx }; + } + // 区域里零星的深色笔画(模型偷偷写的伪文字、小印章)不一定挡得住上面的九成规则,单独数一遍。 + const marks = markRatio(work, carrier, tolColor, box, f); + if (marks > 0.006) { + return { + ok: false, + reason: 'marks', + detail: '排字区域里有 ' + (marks * 100).toFixed(1) + '% 的像素是明显的字迹或色块', + coverage, + marks, + seed, + blank: blankPx, + }; + } + return { ok: true, reason: '', box, blank: blankPx, seed, coverage, marks, moved: 0 }; + }; + + const first = attempt(planned); + // 图表的面板另有一套找法(chart.mjs 的 findSmoothPanel),不挪;排字时由调用方打开。 + if (first.ok || !opts.nudge) return first; + for (const dy of nudgeOffsets(planned, image)) { + const moved = { x: planned.x, y: planned.y + dy, w: planned.w, h: planned.h }; + if (moved.y < 0 || moved.y + moved.h > image.height) continue; + const result = attempt(moved); + if (result.ok) return { ...result, moved: dy }; + } + return first; +} + +function blockStats(work, x0, y0, x1, y1) { + const W = work.w; + const rs = []; + const gs = []; + const bs = []; + const ls = []; + const grads = []; + for (let y = Math.max(0, y0); y < Math.min(work.h, y1); y += 1) { + for (let x = Math.max(0, x0); x < Math.min(W, x1); x += 1) { + const i = y * W + x; + rs.push(work.r[i]); + gs.push(work.g[i]); + bs.push(work.b[i]); + ls.push(work.lum[i]); + grads.push(work.grad[i]); + } + } + if (rs.length < 4) return null; + const lum = median(ls); + const devs = ls.map((value) => Math.abs(value - lum)); + return { + r: median(rs), + g: median(gs), + b: median(bs), + lum, + grad: median(grads), + grad90: percentile(grads, 0.9), + mad: median(devs), + }; +} + +function blockPercentile(work, block, measure, p) { + const [x0, y0, x1, y1] = block; + const values = []; + for (let y = Math.max(0, y0); y < Math.min(work.h, y1); y += 1) { + for (let x = Math.max(0, x0); x < Math.min(work.w, x1); x += 1) values.push(measure(y * work.w + x)); + } + return percentile(values, p); +} + +function denseMask(mask, W, H, k, ratio) { + const integral = new Uint32Array((W + 1) * (H + 1)); + for (let y = 0; y < H; y += 1) { + let row = 0; + for (let x = 0; x < W; x += 1) { + row += mask[y * W + x]; + integral[(y + 1) * (W + 1) + x + 1] = integral[y * (W + 1) + x + 1] + row; + } + } + const half = Math.floor(k / 2); + const out = new Uint8Array(W * H); + for (let y = 0; y < H; y += 1) { + const ya = Math.max(0, y - half); + const yb = Math.min(H, y + half + 1); + for (let x = 0; x < W; x += 1) { + // 看的是周围这一小块,不看这个点自己:单个噪点不该把整块空白切断(最大矩形要求全 1)。 + const xa = Math.max(0, x - half); + const xb = Math.min(W, x + half + 1); + const sum = integral[yb * (W + 1) + xb] - integral[ya * (W + 1) + xb] - integral[yb * (W + 1) + xa] + integral[ya * (W + 1) + xa]; + out[y * W + x] = sum >= ratio * (xb - xa) * (yb - ya) ? 1 : 0; + } + } + return out; +} + +// 二值图里面积最大的全 1 矩形(逐行直方图 + 单调栈),只在 win 范围里找。 +function maxRectangle(mask, W, win) { + const heights = new Int32Array(W); + let best = { area: 0, x0: 0, y0: 0, x1: 0, y1: 0 }; + for (let y = win.y0; y < win.y1; y += 1) { + for (let x = win.x0; x < win.x1; x += 1) heights[x] = mask[y * W + x] ? heights[x] + 1 : 0; + const stackX = []; + const stackH = []; + for (let x = win.x0; x <= win.x1; x += 1) { + const h = x < win.x1 ? heights[x] : 0; + let start = x; + while (stackH.length && stackH[stackH.length - 1] >= h) { + const top = stackH.pop(); + const left = stackX.pop(); + const area = top * (x - left); + if (area > best.area) best = { area, x0: left, x1: x, y0: y - top + 1, y1: y + 1 }; + start = left; + } + stackX.push(start); + stackH.push(h); + } + } + return best; +} + +function markRatio(work, carrier, tolColor, box, f) { + const x0 = Math.max(0, Math.floor(box.x / f)); + const y0 = Math.max(0, Math.floor(box.y / f)); + const x1 = Math.min(work.w, Math.ceil((box.x + box.w) / f)); + const y1 = Math.min(work.h, Math.ceil((box.y + box.h) / f)); + let count = 0; + let total = 0; + for (let y = y0; y < y1; y += 1) { + for (let x = x0; x < x1; x += 1) { + if (carrierDistance(work, carrier, y * work.w + x) > tolColor * 1.8) count += 1; + total += 1; + } + } + return total ? count / total : 0; +} + +// ---------------------------------------------------------------- 颜色与合成 + +function parseHex(value) { + const m = /^#?([0-9a-f]{3}|[0-9a-f]{6})$/i.exec(String(value || '').trim()); + if (!m) return null; + let hex = m[1]; + if (hex.length === 3) hex = hex.split('').map((c) => c + c).join(''); + return [0, 2, 4].map((i) => parseInt(hex.slice(i, i + 2), 16)); +} + +function toHex(rgb) { + return '#' + rgb.map((v) => Math.round(v).toString(16).padStart(2, '0')).join(''); +} + +const lumOf = (rgb) => 0.299 * rgb[0] + 0.587 * rgb[1] + 0.114 * rgb[2]; + +// 规划给的颜色和底色反差不够(比如浅米色纸上写米白字)时不照用,自动换成深墨或浅色字。 +function resolveInk(color, seed) { + const surface = [seed.r, seed.g, seed.b]; + const surfaceL = lumOf(surface); + const want = parseHex(color); + if (want && Math.abs(lumOf(want) - surfaceL) >= 70) { + return { rgb: want, mode: lumOf(want) < surfaceL ? 'ink' : 'paint', auto: false }; + } + if (surfaceL >= 120) return { rgb: [26, 24, 22], mode: 'ink', auto: true }; + return { rgb: [245, 242, 235], mode: 'paint', auto: true }; +} + +// 边缘稍微柔一点(照片本身不是矢量锐利的),毛笔字再加墨色浓淡。 +function finishMask(mask, size, handwriting, image) { + const big = Math.max(image.width, image.height) >= 2400; + if (big) boxBlur3(mask); + if (!handwriting) return; + const cell = Math.max(3, Math.round(size * 0.06)); + const gw = Math.ceil(mask.w / cell) + 2; + const gh = Math.ceil(mask.h / cell) + 2; + const grid = new Float32Array(gw * gh); + let state = 0x2545f491; + for (let i = 0; i < grid.length; i += 1) { + state ^= state << 13; + state ^= state >>> 17; + state ^= state << 5; + grid[i] = ((state >>> 0) % 1000) / 1000; + } + for (let y = 0; y < mask.h; y += 1) { + const gy = y / cell; + const iy = Math.floor(gy); + const fy = gy - iy; + for (let x = 0; x < mask.w; x += 1) { + const at = y * mask.w + x; + const a = mask.data[at]; + if (!a) continue; + const gx = x / cell; + const ix = Math.floor(gx); + const fx = gx - ix; + const n00 = grid[iy * gw + ix]; + const n10 = grid[iy * gw + ix + 1]; + const n01 = grid[(iy + 1) * gw + ix]; + const n11 = grid[(iy + 1) * gw + ix + 1]; + const n = (n00 * (1 - fx) + n10 * fx) * (1 - fy) + (n01 * (1 - fx) + n11 * fx) * fy; + // 墨色浓淡:笔画中间最多淡 12%,边缘稍微加厚一点(墨在纸上会洇开)。 + const ink = 0.88 + 0.12 * n; + const v = Math.min(1, Math.pow(a, 0.85) * ink * 1.02); + mask.data[at] = v; + } + } +} + +function boxBlur3(mask) { + const { w, h, data } = mask; + const tmp = new Float32Array(w * h); + for (let y = 0; y < h; y += 1) { + const row = y * w; + for (let x = 0; x < w; x += 1) { + const l = x > 0 ? data[row + x - 1] : data[row + x]; + const r = x < w - 1 ? data[row + x + 1] : data[row + x]; + tmp[row + x] = (l + data[row + x] * 2 + r) / 4; + } + } + for (let y = 0; y < h; y += 1) { + for (let x = 0; x < w; x += 1) { + const at = y * w + x; + const u = y > 0 ? tmp[at - w] : tmp[at]; + const d = y < h - 1 ? tmp[at + w] : tmp[at]; + data[at] = (u + tmp[at] * 2 + d) / 4; + } + } +} + +// 深色字:结果 = 墨色 × 这一点的纸面亮度 / 纸面平均亮度,阴影、渐变、纸纹都留在字上。 +// 浅色字(深底上写白字):按同样的明暗比例调一下亮度,再盖上去。 +function blend(out, mask, ink, seed) { + const { data, channels, width } = out; + const surface = [Math.max(1, seed.r), Math.max(1, seed.g), Math.max(1, seed.b)]; + for (let y = 0; y < mask.h; y += 1) { + const iy = mask.y + y; + for (let x = 0; x < mask.w; x += 1) { + const a = mask.data[y * mask.w + x]; + if (a <= 0.002) continue; + const i = (iy * width + mask.x + x) * channels; + for (let c = 0; c < Math.min(3, channels); c += 1) { + const base = data[i + c]; + const sc = channels >= 3 ? surface[c] : (surface[0] + surface[1] + surface[2]) / 3; + const light = clamp(base / sc, 0.55, 1.35); + const target = ink.mode === 'ink' ? ink.rgb[c] * light : clamp(ink.rgb[c] * (0.75 + 0.25 * light), 0, 255); + data[i + c] = clamp(Math.round(base * (1 - a) + target * a), 0, 255); + } + } + } +} + +// ---------------------------------------------------------------- 给流水线用的小工具 + +// 程序画图表(chart.mjs)找空白用的是同一套办法:底图上预留的面板和预留给文字的纸面一样,要平整、没有笔迹。 +export { analysisImage, boxToPixels, denseMask, findTextRegion as findBlankRegion, maxRectangle, normalizedBox }; + +// 一段文字有多难让模型直接画对:字多、竖排、书法这几样叠起来最容易残。 +export function textRisk(layers) { + const list = (Array.isArray(layers) ? layers : []).filter((layer) => layer && layer.text); + const counts = list.map((layer) => glyphCount(String(layer.text).split('').filter((ch) => !isPunct(ch)).join(''))); + const total = counts.reduce((sum, n) => sum + n, 0); + const longest = counts.length ? Math.max(...counts) : 0; + const vertical = list.some((layer) => layer.direction === 'vertical'); + const brush = list.some((layer) => FONT_KINDS[normalizeFontKind(layer.font)].handwriting); + const reasons = []; + if (longest >= 24) reasons.push('单段 ' + longest + ' 个字'); + else if (total >= 40) reasons.push('合计 ' + total + ' 个字'); + if (list.length >= 6) reasons.push(list.length + ' 处文字'); + if (vertical && total >= 10) reasons.push('竖排'); + if (brush && total >= 12) reasons.push('书法字'); + return { total, longest, layers: list.length, high: reasons.length > 0, reasons }; +} + +function median(values) { + return percentile(values, 0.5); +} + +function percentile(values, p) { + if (!values.length) return 0; + const sorted = Float64Array.from(values).sort(); + return sorted[Math.min(sorted.length - 1, Math.floor(p * (sorted.length - 1)))]; +} + +function intersect(a, b) { + const x0 = Math.max(a.x, b.x); + const y0 = Math.max(a.y, b.y); + const x1 = Math.min(a.x + a.w, b.x + b.w); + const y1 = Math.min(a.y + a.h, b.y + b.h); + if (x1 <= x0 || y1 <= y0) return null; + return { x: x0, y: y0, w: x1 - x0, h: y1 - y0 }; +} + +function clamp(value, min, max) { + return value < min ? min : value > max ? max : value; +} + +function clampInt(value, min, max) { + return Math.min(max, Math.max(min, value)); +} + +function round(value, digits) { + const p = Math.pow(10, digits || 0); + return Math.round((Number(value) || 0) * p) / p; +} diff --git a/plugins/liddalidd/h3-image-executor/miniapp/node/lib/workflow.mjs b/plugins/liddalidd/h3-image-executor/miniapp/node/lib/workflow.mjs index 2f60f3ad..ad7f9e84 100644 --- a/plugins/liddalidd/h3-image-executor/miniapp/node/lib/workflow.mjs +++ b/plugins/liddalidd/h3-image-executor/miniapp/node/lib/workflow.mjs @@ -1,18 +1,64 @@ // H3-Image-Executor 状态机:00 输入 -> 01 分诊 -> 02 THINK/检索 -> 03 PLAN -> 04 PAINT // -> 05 VERIFY(程序检查 + 色偏校正 + VLM 评审)-> 06 REFINE -> 07 DELIVER -> 08 增量编辑。 import { randomUUID } from 'node:crypto'; +import { existsSync } from 'node:fs'; import { readFile, writeFile, copyFile, mkdir } from 'node:fs/promises'; -import { join } from 'node:path'; +import { basename, join } from 'node:path'; import * as h3 from './h3.mjs'; import * as png from './png.mjs'; import * as review from './review.mjs'; import { searchRefImages, normalizeRefQueries, broadenQuery } from './refsearch.mjs'; +import { DIFFICULTY_LABELS, difficultyOf, hardestOf, hitsOf, normalizeDifficulty, noteToolHit, toolBudget } from './budget.mjs'; import { createRefCache } from './refcache.mjs'; // 接口返回的不一定是 PNG:实测同一个端点有时回 JPEG。只用 png.decodePng 会让整条 // 程序校验链路(尺寸 / 色偏 / 锐度 / 预览图 / 评审看图材料)在 JPEG 响应下静默失效, // 而图照样交付——用户看到的是「已交付」,看不到「其实一项都没验」。 -import { decodeImage, extensionFor } from './image.mjs'; +import { decodeImage, extensionFor, sniffFormat } from './image.mjs'; import { detail, group, kv, list, seconds, text, thumbs } from './detail.mjs'; +// 二维码:生图模型画不出能扫的码,图里要放码时由程序生成真码、贴回原位、从最终文件扫码验证。 +import { fixQrInImage } from './qrfix.mjs'; +// 文字:长段文字、竖排、书法让模型直接画很容易写残,这类图让模型画无字底图,文字由程序按原文排进去。 +import { normalizePart, overlayText, textRisk } from './textoverlay.mjs'; +import { isCjk, isPunct } from './textlayout.mjs'; +import { FONT_KINDS, describeFontKinds, normalizeFontKind } from './fonts.mjs'; +// 图表:生图模型画不准柱子高矮、刻度和数字。图里要放数据图表时,模型只画留出空白面板的底图, +// 图表由程序按数据画进去,再从最终文件量回来核对(chart.mjs)。 +import { CHART_TYPES, MAX_CHARTS, chartDataCheck, describeChart, normalizeChart, normalizeCharts, overlayCharts } from './chart.mjs'; +import { + animationInventory, + applyAnimationOps, + awaitingInfo, + confirmTarget, + deliverAnimation, + ensureState as ensureAnimationState, + hasAdjustments as hasAnimationAdjustments, + isAnimation, + normalizeAnimation, + normalizeAnimationOps, + resetForNewReference, + runFrames, + transitionOf, + usesGivenReference, +} from './animation.mjs'; +// 程序转圈:绕中心转的动画(加载圈、转动的图标)只画一张图,由程序转出各帧(motiongif.mjs)。 +import { + composeMotionGif, + mergeMotionOps, + motionGifInventory, + motionGifItem, + normalizeMotionGif, +} from './motiongif.mjs'; +// 组图拼 GIF:分格的组图由程序切格、按节奏拼成 GIF,不再调生图(panelgif.mjs)。 +import { + bounceAsked, + composePanelGif, + mergePanelOps, + normalizePanelGif, + panelGifInventory, + panelGifItem, +} from './panelgif.mjs'; +// 组图 GIF 的过渡格:相邻两格之间补一格中间状态,一起生成、从候选里挑(paneltween.mjs)。 +import { ensurePanelTweens } from './paneltween.mjs'; export const DEFAULTS = { area: '4K', @@ -28,8 +74,7 @@ export const DEFAULTS = { // (真机 20261110-5e10ae 手语字母表 26 个字母标签)就把 planner 写的「手型对不对」 // 全部挤成 soft,而 soft 不参与放行——图就这样放行了。 maxHardItems: 8, - // 各节点的工具调用硬上限(运行时强制,超了就中断并强制收尾)。 - toolBudget: { plan: 10, judge: 6, refine: 4, select: 2, edit: 4 }, + // 各节点的工具调用上限不再写死,每次调用前按任务难度现算(见 budget.mjs),超了运行时硬中断。 // 看图材料长边上限:再大 VLM 也会缩,反而拖慢上传与推理。 sheetMaxEdge: 1568, // 多图并行上限。每张图同一时刻最多占 1 个 agent 运行槽(runJson 每次都开新会话), @@ -40,13 +85,20 @@ export const DEFAULTS = { // .minimax-plugin/plugin.json——写进后者会被社区校验器判成未知字段,而且完全不生效。 // 实测瓶颈 95% 在 judge(VLM 评审,均 367s/次),paint 只要 16s,所以放开并行直接砍耗时。 imageConcurrency: 4, - // 评审节点的工具调用预算,写进提示词用于约束 agent。 - judgeToolBudget: 5, refDownloadLimit: 8, // 参考图:一批先下 3 张判定,不合格再取下一批;池子空了就换个说法再搜。 refBatch: 3, refGateRounds: 3, refSearchRounds: 2, + // 检索预算:一次规划(或一次追问)里,搜图工具调用和候选判定两样合计的上限。规划可以在 + // ref_budget 里自己定(0 = 不检索),没写就用 refBudgetDefault。refBudgetMax 是硬上限: + // 6 项 × 每项最多 3 批判定 + 2 次换说法重搜,不封顶的话一次规划能花掉 30 次以上。 + refBudgetDefault: 8, + refBudgetMax: 16, + // 生图接口临时故障、手上还一张图都没有时,等多久再试下一轮。 + outageWaitMs: 30000, + // 评审回合没跑成时,等多久重新评审同一批图。 + judgeRetryWaitMs: 15000, }; // ---------------------------------------------------------------- prompt 槽位 @@ -75,10 +127,812 @@ const COLOR_CLAUSE = const COLOR_RETRY_CLAUSE = '(上一轮输出整体偏红,这一轮必须修正白平衡:红通道降到与绿蓝相当,不要暖黄、橘红、粉红倾向,' + '画面里的中性物体必须还原成真实中性色。)'; +const COLOR_RETRY_CLAUSE_WARM = + '(上一轮画面里中性的地方(白、灰、黑)偏红,这一轮必须把它们还原成真实中性;需求要的黄、红、橙主色保持不变。)'; -function withColorRetry(prompt) { +function withColorRetry(prompt, warm) { const base = String(prompt || '').trim(); - return base.includes('上一轮输出整体偏红') ? base : base + COLOR_RETRY_CLAUSE; + if (base.includes('上一轮输出整体偏红') || base.includes('上一轮画面里中性的地方')) return base; + return base + (warm ? COLOR_RETRY_CLAUSE_WARM : COLOR_RETRY_CLAUSE); +} + +// 需求本身就要大面积的黄、红、橙(明黄底的海报、大红的喜庆主题):按「红绿蓝均衡、不要偏暖发黄」去要求, +// 等于让模型把需求要的颜色改淡;整图均值也判不了偏红——明黄的 redGain 天生接近 2(真机 20261116-d46325: +// 12 张候选 11 张判偏红,两轮没评审就重画,最后交付的那张也因此标「未通过」)。 +const WARM_RE = + /(明黄|鲜黄|亮黄|柠檬黄|金黄|黄色|橙色|橘色|橙黄|橘黄|红色|大红|朱红|猩红|正红|酒红|暖色|暖调|金色)[^,。;,;]{0,8}(背景|主色|主调|底色|色调|为主|铺满|大面积)|(以|用)[^,。;,;]{0,6}(明黄|黄色|橙色|红色|暖色|金色)[^,。;,;]{0,4}(为主|做底|铺底|打底)/; + +function warmPalette(text) { + return WARM_RE.test(String(text || '')); +} + +function imageWarm(session, image) { + const slots = (image && image.spec && image.spec.slots) || {}; + return warmPalette([session && session.instruction, slots.style, slots.scene].filter(Boolean).join(' ')); +} + +const COLOR_CLAUSE_WARM = + '色彩要求:画面的主色按需求来,大面积的黄、红、橙是有意的配色,不要改淡、改冷;' + + '白、灰、黑这些中性的地方保持中性,肤色真实,不要额外罩一层红色或橙色的滤镜。'; + +// 暖色设计的风格槽位里去掉「不要暖黄调」「不偏暖」这类要求:规划按 5b 的惯例写进去(真机 20261116-d46325 的明黄海报), +// 和需求要的主色打架。只去掉这一句,别的白平衡说法(中性的地方保持中性)留着。 +function stripWarmStyle(text) { + return String(text || '') + .replace(/[,、;,;]?\s*(?:不要|别|不)(?:整体)?(?:偏)?暖(?:黄)?(?:调|色调)?(?=[,。;、,;]|$)/g, '') + .replace(/^[,、;,;\s]+/, ''); +} + +// 暖色设计的排除项里去掉「偏红、偏暖发黄」:规划按惯例写进去的这条,正好和需求要的主色打架。 +function stripWarmNegative(text) { + return String(text || '') + .replace(/(?:不要)?整体偏红[、,]?偏暖发黄/g, '') + .replace(/(?:不要)?(?:整体)?(?:偏暖发黄|偏红|偏暖|发黄)(?=[、,;;。]|$)/g, '') + .replace(/^[、,;;\s]+|[、,;;\s]+$/g, '') + .replace(/([、,;;])[、,;;\s]+/g, '$1'); +} + +// 白纸、白底:需求写明是白色的纸面或背景时,白色的地方必须是中性的白。 +const WHITE_RE = /白色(?:的)?(?:纸|纸张|纸面|背景|底|底色|卡纸|墙面?|页面|画布)|白纸|白底|纯白|白墙|white\s+(?:background|paper)/i; +// 门槛 4 级:真机 41535c 前三轮的纸面差 0.1 / 1.7 / 3.2 级都还看着白,交付那张 5.1 级就被看出发紫了。 +const WHITE_CAST_LIMIT = 4; +const WHITE_MIN_SHARE = 0.04; +const WHITE_CHECK = { + id: 'H-white', + kind: 'hard', + verify: 'program', + text: '要求是白色的纸面或背景:白色的地方必须是中性的白,程序实测红绿蓝相差不超过 ' + (WHITE_CAST_LIMIT - 1) + ' 级', +}; +const WHITE_RETRY_MARK = '(上一轮白色的纸面或背景'; + +// 米白、暖白、乳白、象牙白这类本来就带颜色的白不是中性白,不按白纸查:规划写的「浅暖白纸面」 +// 以前也会触发这条检查(真机 20261117-f57a0f),把有意偏暖的纸面判成偏色。 +// 「空白纸面」「留白」说的是没有东西,不是颜色,也先去掉。 +const TINTED_WHITE_RE = /(?:空|留|米|暖|乳|奶|象牙|牙|蛋壳|珍珠|月|本|灰|米黄|暖调|偏暖)白/g; + +function wantsWhite(session, image) { + const slots = (image && image.spec && image.spec.slots) || {}; + const words = [...qrSources(session), slots.scene, slots.style, slots.subject].filter(Boolean).join(' ').replace(TINTED_WHITE_RE, ''); + return WHITE_RE.test(words); +} + +function withWhiteCheck(checklist, on) { + const rest = (checklist || []).filter((item) => item.id !== WHITE_CHECK.id); + return on ? rest.concat({ ...WHITE_CHECK }) : rest; +} + +// 白色往哪边偏:看最高和最低的通道。 +function whiteCastLabel(rgb) { + if (!rgb) return '偏色'; + const names = ['R', 'G', 'B']; + const hi = names[rgb.indexOf(Math.max(...rgb))]; + const lo = names[rgb.indexOf(Math.min(...rgb))]; + const table = { RB: '偏暖发黄', RG: '偏粉红', BR: '偏冷发蓝', BG: '偏粉紫', GR: '偏青绿', GB: '偏黄绿' }; + return table[hi + lo] || '偏色'; +} + +function withWhiteRetry(prompt, label) { + const base = String(prompt || '').trim(); + if (base.includes(WHITE_RETRY_MARK)) return base; + return base + WHITE_RETRY_MARK + (label ? label : '偏色') + ',这一轮必须还原成纯净中性的白:红绿蓝三个通道相等,不要发灰、发紫、发粉、发黄、发蓝。)'; +} + +// ---------------------------------------------------------------- 二维码 +// +// 生图模型画不出能扫的二维码,它画的只是长得像码的黑白方块(真机 20261111-606a7e:两张候选 +// 一个都扫不出,评审只核对了「像不像二维码」就放行)。所以分工是: +// - 规划节点只负责把码里要放的内容从用户原话里逐字抄下来(qr_codes),并在 prompt 里要求模型 +// 画一个正对镜头、白底黑块、定位方块完整的占位码; +// - 每张候选出图后,程序找到这个占位码,生成真码贴回原位,再从最终文件扫码验证(qrfix.mjs); +// - 扫不出就是 H-qr 硬项不通过,和别的硬项一样打回重画。 +// 用户要码但没给内容时照常出图,交付时注明是装饰码;用户在同一会话里补一句内容,直接在原图上换。 +const MAX_QR_CODES = 3; +// 允许规划节点在逐字抄下的内容前面补一个协议头(用户写 www.minimax.cn,码里放 https://www.minimax.cn), +// 其余一律要求逐字出现在用户说过的话里:码扫出一个用户没说过的网址,比扫不出更糟。 +const QR_SCHEMES = /^(https?:\/\/|tel:|mailto:|smsto:|sms:)/i; + +const QR_CLAUSE = + '二维码:画面里的二维码只需画出占位图案,之后会由程序换成能扫的真码。' + + '要求是正对镜头的平整正方形,纯白底上的黑色方块,左上、右上、左下三个角的回字形定位方块完整清晰,' + + '码的四周留出一圈白边,不要被手、文字或其它物体遮挡,不要弯折、倾斜过大或做成艺术化变形,中间不要放 logo,' + + '边长不小于画面短边的六分之一。'; + +// 编辑已有图时用:上一版的码已经是真码,模型重画会把它画坏,程序之后会再贴一次。 +const QR_KEEP_CLAUSE = '画面里的二维码保持原来的位置、大小和白底,不要遮挡、删除或改画它。'; + +const QR_RETRY_MARK = '(上一轮画面里的二维码没法换成真码'; + +function withQrRetry(prompt, reason) { + const base = String(prompt || '').trim(); + if (base.includes(QR_RETRY_MARK)) return base; + return ( + base + + QR_RETRY_MARK + + (reason ? ':' + reason : '') + + '。这一轮必须把二维码画大、画正:正对镜头的平整正方形,白底黑块,三个角的回字形定位方块完整清晰,' + + '四周留出白边,不要被任何东西挡住。)' + ); +} + +// 用户在这个会话里说过的所有话:首轮需求、出图前确认的回答、之后每一轮追问。 +function qrSources(session, extra) { + return [ + session.instruction, + ...(session.answered || []).map((item) => item && item.answer), + ...(session.turns || []).map((turn) => turn && turn.instruction), + ...(extra || []), + ] + .filter(Boolean) + .map(String); +} + +// 逐字核对必须按「整段」找,不能只看子串:规划把 https://www.minimax.cn/ 写成 https://minimax.cn, +// 去掉协议头的 minimax.cn 恰好是原话的子串;把 www.minimax.cn/about 截成 www.minimax.cn 也一样。 +// 所以找到的位置前后不能紧挨着网址字符。括号、引号、中文、空白都算边界(用户常写「([网址])」), +// 句末的 . , : ; ! ? 后面再没有网址字符时也算边界(「我的网站是 www.minimax.cn.」)。 +// 开头只防「丢了前缀」(www.、子域名、user@),所以 : = 这类标签分隔符不算(「官网:https://…」)。 +const TOKEN_CHAR = /[A-Za-z0-9\-._~/:?#@%=&+]/; +const LEAD_CHAR = /[A-Za-z0-9\-._~/@%]/; + +function foundAsToken(source, needle) { + if (!needle) return false; + for (let at = source.indexOf(needle); at >= 0; at = source.indexOf(needle, at + 1)) { + const before = at > 0 ? source[at - 1] : ''; + const after = source[at + needle.length] || ''; + const afterNext = source[at + needle.length + 1] || ''; + const startOk = !before || !LEAD_CHAR.test(before) || !TOKEN_CHAR.test(needle[0]); + const endOk = + !after || + !TOKEN_CHAR.test(after) || + !TOKEN_CHAR.test(needle[needle.length - 1]) || + (/[.,:;!?]/.test(after) && (!afterNext || !TOKEN_CHAR.test(afterNext))); + if (startOk && endOk) return true; + } + return false; +} + +function isVerbatim(content, sources) { + if (!content) return false; + if (sources.some((source) => foundAsToken(source, content))) return true; + const bare = content.replace(QR_SCHEMES, ''); + return bare !== content && bare.length >= 3 && sources.some((source) => foundAsToken(source, bare)); +} + +// 规划节点给的 qr_codes 归一成 [{ content, where, verbatim }]。content 只去掉首尾空白, +// 中间一个字符都不动;空内容表示「要一个码,但用户没说放什么」(装饰码)。 +function normalizeQrCodes(raw, sources) { + const rows = Array.isArray(raw) ? raw : []; + const out = []; + for (const item of rows) { + let content = ''; + let where = ''; + if (typeof item === 'string') content = item; + else if (item && typeof item === 'object') { + content = item.content === undefined || item.content === null ? '' : String(item.content); + where = String(item.where || '').trim().slice(0, 60); + } else continue; + content = content.trim(); + out.push({ content, where, verbatim: content ? isVerbatim(content, sources) : false }); + } + return out.slice(0, MAX_QR_CODES); +} + +// 要贴真码的内容:有内容、而且逐字核对过。 +function qrContents(spec) { + return ((spec && spec.qrCodes) || []).filter((item) => item.content && item.verbatim).map((item) => item.content); +} + +function qrCheckItem(contents) { + return { + id: 'H-qr', + kind: 'hard', + verify: 'program', + text: '画面里的二维码扫出来是「' + contents.join('」「') + '」(程序贴真码后从最终文件扫码实测)', + }; +} + +// 清单里挂上(或摘掉)H-qr。放在 hard 上限裁剪之外,否则会被降级成 soft,soft 不参与放行。 +function withQrCheck(checklist, qrCodes) { + const contents = qrContents({ qrCodes }); + const rest = (checklist || []).filter((item) => item.id !== 'H-qr'); + return contents.length ? rest.concat(qrCheckItem(contents)) : rest; +} + +// 交付时每张图的二维码状况:给页面显示,也决定要不要请用户补内容。 +function qrDelivery(image, chosen) { + const codes = (image.spec && image.spec.qrCodes) || []; + if (!codes.length) return null; + const fixed = (chosen && chosen.program && chosen.program.qr) || null; + const items = codes.map((code) => { + if (!code.content) return { content: '', status: 'decorative', note: '装饰码,扫不出内容' }; + if (!code.verbatim) { + return { content: code.content, status: 'unverified', note: '这段内容在你的原话里找不到,没有贴真码' }; + } + const hit = fixed && (fixed.items || []).find((item) => item.content === code.content); + if (hit && hit.ok) return { content: code.content, status: 'real', note: '已换成真码,扫码实测通过' }; + // 在原图上换码没成功时,图还是上一版,原因记在 qrAttempt 上。 + const attempt = + image.qrAttempt && (image.qrAttempt.contents || []).includes(code.content) ? image.qrAttempt.reasonText : ''; + return { + content: code.content, + status: 'failed', + note: hit ? hit.reasonText + (hit.detail ? '(' + hit.detail + ')' : '') : attempt || '没有做二维码替换', + }; + }); + return { items }; +} + +// 交付里要请用户处理的二维码问题。只挂在这一版交付上,不写进 session.questions: +// 用户补了内容、码换好之后,下一版交付就不该再提。 +function qrQuestions(image, info) { + if (!info) return []; + const out = []; + const decorative = info.items.filter((item) => item.status === 'decorative'); + const unverified = info.items.filter((item) => item.status === 'unverified'); + const failed = info.items.filter((item) => item.status === 'failed'); + if (decorative.length) { + out.push( + image.id + ' 里的二维码是装饰图案,扫不出内容。把要放的链接或文字在这个会话里发给我,' + + '我直接在原图上换成能扫的真码,不重新画', + ); + } + for (const item of unverified) { + out.push( + image.id + ' 的二维码内容「' + item.content + '」在你说过的话里找不到,没有贴真码。' + + '确认要放的内容后在这个会话里发给我,我直接在原图上换', + ); + } + for (const item of failed) { + out.push(image.id + ' 的二维码没能换成「' + item.content + '」(' + item.note + ')。可以回复「重画」重新生成这张图'); + } + return out; +} + +// ---------------------------------------------------------------- 程序排字 +// +// 生图模型写长段文字、竖排、书法时很容易写残(真机 20261113-01b902:324 字的《兰亭集序》长卷,第一轮的 +// 候选有臆造字,交付的那张漏了 36 个字,评审看不清 324 个字,只能判「拿不准」放行)。兜底做法:模型只画一张不带字的底图, +// 文字由程序用字体按原文逐字排进去(textoverlay.mjs)。分工是: +// - 规划节点给每个文字层写好放在哪、横排还是竖排、什么书体(text_overlay),以及「载体留空」版本的槽位; +// - 字多、竖排、书法这类一开始就走这条路;短标题先让模型直接画,评审判出文字画残了再切过来; +// - 每张候选出图后,程序在规划的位置附近找到留空的载体,把字排进去;没留空就是 H-text 不通过,带着原因重画。 +// 一张课本内页就有课题、拼音、两三段正文、脚注、页码,8 段不够用(真机 20261116-41535c 规划了 9 段,页码被丢了)。 +const MAX_TEXT_LAYERS = 16; +const TEXT_DIRECTIONS = new Set(['horizontal', 'vertical']); +const TEXT_ALIGNS = new Set(['start', 'center', 'end']); +const TEXT_PUNCTUATION = new Set(['inline', 'side', 'omit']); +const TEXT_ROLES = new Set(['title', 'subtitle', 'body', 'footnote', 'caption', 'label', 'page', 'pinyin']); +const BASE_SLOT_KEYS = new Set([...SLOT_SPEC.map((spec) => spec.key), 'negative']); + +const BLANK_MARK = '文字留白:'; +const TEXT_RETRY_MARK = '(上一轮底图上预留给文字的地方没有留空'; + +const TEXT_HARD_CHECK = { + id: 'H-text', + kind: 'hard', + verify: 'program', + text: '文字由程序逐字排进底图上留空的位置:位置上确实是空白、全部文字排得下、字号够大(程序实测)', +}; +const NO_TEXT_CHECK = { + id: 'H-notext', + kind: 'hard', + verify: 'vlm', + text: '除程序排进去的文字以外,画面里没有别的文字、伪文字或乱码', +}; +const TEXT_FIT_CHECK = { + id: 'S-textfit', + kind: 'soft', + verify: 'vlm', + text: '排进去的文字和画面融为一体:大小、位置、颜色和载体协调,像真的写在或印在上面,没有压边、出界或悬浮感', +}; +const OVERLAY_CHECK_IDS = new Set([TEXT_HARD_CHECK.id, NO_TEXT_CHECK.id, TEXT_FIT_CHECK.id]); + +function layerTexts(textLayers) { + return (Array.isArray(textLayers) ? textLayers : []) + .map((layer) => (typeof layer === 'string' ? layer : layer && layer.text)) + .filter(Boolean) + .map(String); +} + +function normalizeTextLayer(raw, text) { + const item = raw && typeof raw === 'object' ? raw : {}; + const box = + Array.isArray(item.box) && item.box.length === 4 && item.box.every((value) => Number.isFinite(Number(value))) + ? item.box.map(Number) + : null; + const direction = TEXT_DIRECTIONS.has(item.direction) ? item.direction : 'horizontal'; + const color = String(item.color || '').trim(); + const role = TEXT_ROLES.has(String(item.role || '').trim().toLowerCase()) ? String(item.role).trim().toLowerCase() : ''; + const indent = Number(item.indent); + return { + text, + box, + direction, + font: normalizeFontKind(item.font), + color: /^#?[0-9a-f]{3}([0-9a-f]{3})?$/i.test(color) ? (color.startsWith('#') ? color : '#' + color) : '', + align: TEXT_ALIGNS.has(item.align) ? item.align : direction === 'vertical' ? 'start' : 'center', + punctuation: direction === 'vertical' && TEXT_PUNCTUATION.has(item.punctuation) ? item.punctuation : direction === 'vertical' ? 'inline' : '', + surface: String(item.surface || '').trim().slice(0, 40), + // 同一角色的几层统一字号;body 首行缩进两字(indent 可改),footnote 上方画分隔线(rule:false 不画)。 + role, + ...(Number.isInteger(indent) && indent >= 0 && indent <= 4 ? { indent } : {}), + ...(item.rule === false ? { rule: false } : {}), + // 部件分色:这一层每个汉字靠 side 那一侧的部件用另一种颜色(「清」的「氵」橙色),见 textlayout.splitTile。 + ...(normalizePart(item.part) ? { part: normalizePart(item.part) } : {}), + }; +} + +// 规划没写这一层怎么排时,从用户原话、标题和槽位里猜:说了竖排就竖排,说了书法、毛笔就用毛笔字。 +// 不猜的话一律是横排黑体,书法长卷会被排成一页横排的印刷体。 +const SURFACE_WORDS = ['宣纸', '绢本', '洒金纸', '牌匾', '招牌', '黑板', '白板', '屏幕', '海报', '菜单', '书页', '信纸', '木板', '石碑']; + +function inferTextLayer(hint) { + const source = String(hint || ''); + const vertical = /竖排|竖写|竖向|直排|从上到下|自上而下/.test(source); + const font = /行书|草书/.test(source) + ? 'xing' + : /书法|毛笔|楷书|行楷|隶书|篆书|手卷|条幅|中堂|对联|春联|题字/.test(source) + ? 'brush' + : /宋体|衬线/.test(source) + ? 'song' + : /楷体/.test(source) + ? 'kai' + : 'hei'; + const calligraphy = font === 'brush' || font === 'xing'; + return { + direction: vertical ? 'vertical' : 'horizontal', + font, + punctuation: vertical && calligraphy && !/(带|加|保留|写上|要有|包含)标点/.test(source) ? 'omit' : undefined, + surface: SURFACE_WORDS.find((word) => source.includes(word)) || '', + }; +} + +// 规划给的 text_overlay 归一成 { use, base, layers, planned }。文字一律以 text_layers 为准:规划在 layers 里改了字、 +// 少写了一层,都按 text_layers 补齐;对不上的按顺序配,配不上的按 hint 猜排法。没有文字层时返回 null。 +// planned:规划自己写了排法。没写时只知道字,不知道该放在画面哪里。 +function normalizeTextOverlay(raw, textLayers, hint) { + const texts = layerTexts(textLayers); + if (!texts.length) return null; + const source = raw && typeof raw === 'object' ? raw : {}; + const rawLayers = Array.isArray(source.layers) ? source.layers : []; + const used = new Set(); + const layers = texts.slice(0, MAX_TEXT_LAYERS).map((text, index) => { + let hit = rawLayers.findIndex((item, i) => !used.has(i) && item && String(item.text || '').trim() === text.trim()); + if (hit < 0 && index < rawLayers.length && !used.has(index)) hit = index; + if (hit >= 0) used.add(hit); + return normalizeTextLayer(hit >= 0 ? rawLayers[hit] : inferTextLayer(hint), text); + }); + const base = {}; + if (source.base && typeof source.base === 'object') { + for (const [key, value] of Object.entries(source.base)) { + if (!BASE_SLOT_KEYS.has(key)) continue; + const textValue = asText(value); + if (textValue) base[key] = textValue; + } + } + // 排不下的层要记下来:以前多出来的悄悄丢掉,程序验的 T 项照样判「已逐字排入」。 + return { use: source.use === true, base, layers, planned: rawLayers.length > 0, dropped: texts.slice(MAX_TEXT_LAYERS) }; +} + +const percent = (value) => Math.round(Math.min(1, Math.max(0, Number(value) || 0)) * 100) + '%'; + +// 把规划的位置说成模型听得懂的话:「画面中央、从左 6% 到 94%、从上 16% 到 84% 的区域」。 +function describeBox(box) { + const b = Array.isArray(box) && box.length === 4 ? box : [0.1, 0.1, 0.9, 0.9]; + const cx = (b[0] + b[2]) / 2; + const cy = (b[1] + b[3]) / 2; + const h = cx < 0.38 ? '左' : cx > 0.62 ? '右' : ''; + const v = cy < 0.38 ? '上' : cy > 0.62 ? '下' : ''; + const where = !h && !v ? '中央' : v && h ? h + v + '方' : v ? (v === '上' ? '上方' : '下方') : h + '侧'; + return ( + '画面' + where + '从左 ' + percent(b[0]) + ' 到 ' + percent(b[2]) + '、从上 ' + percent(b[1]) + ' 到 ' + percent(b[3]) + ' 的区域' + ); +} + +// 段落多时(课本页、菜单、说明书)按载体合并成一整片区域,只用方位词描述,不写数字: +// 逐段写「从左 11% 到 42%、从上 3% 到 5%」十几组,模型会把这些百分数当成要画的字, +// 画出一整页写满数字的表格(真机 20261116-06a562 第 4 轮,15 段文字、60 个数字)。 +const BLANK_MERGE_OVER = 3; + +function describeSpan(box) { + const b = Array.isArray(box) && box.length === 4 ? box : [0.1, 0.1, 0.9, 0.9]; + const band = (y) => (y < 0.2 ? '顶部' : y < 0.4 ? '上部' : y < 0.6 ? '中部' : y < 0.8 ? '下部' : '底部'); + const top = band(b[1] + 0.01); + const bottom = band(b[3] - 0.01); + const vertical = top === bottom ? '画面' + top : '画面从' + top + '到' + bottom; + const width = b[2] - b[0]; + const cx = (b[0] + b[2]) / 2; + const horizontal = width >= 0.6 ? '横向几乎占满整幅' : cx < 0.4 ? '偏左' : cx > 0.6 ? '偏右' : '居中'; + return vertical + '、' + horizontal + '的整片区域'; +} + +function blankClause(layers) { + const list = layers || []; + let parts; + if (list.length > BLANK_MERGE_OVER) { + const groups = new Map(); + for (const layer of list) { + const key = layer.surface || ''; + const b = Array.isArray(layer.box) && layer.box.length === 4 ? layer.box : [0.1, 0.1, 0.9, 0.9]; + const union = groups.get(key); + groups.set(key, union ? [Math.min(union[0], b[0]), Math.min(union[1], b[1]), Math.max(union[2], b[2]), Math.max(union[3], b[3])] : b.slice()); + } + parts = [...groups.entries()].map(([surface, box]) => (surface ? surface + '上' : '') + describeSpan(box)); + } else { + parts = list.map((layer) => (layer.surface ? layer.surface + '上' : '') + describeBox(layer.box)); + } + return ( + BLANK_MARK + + parts.join(';') + + '保持完全空白:平整干净,没有任何文字、字母、数字、标点、笔迹、墨迹、印章、落款、签名、界格线和装饰纹样,' + + '这里的文字之后由程序排进去。画面其他地方也不要出现任何文字、伪文字或水印。' + ); +} + +function withTextRetry(prompt, reason) { + const base = String(prompt || '').trim(); + if (base.includes(TEXT_RETRY_MARK)) return base; + return ( + base + + TEXT_RETRY_MARK + + (reason ? ':' + reason : '') + + '。这一轮必须把这块区域留成完全空白、平整的载体表面,上面不要画任何字、笔迹、印章、花纹或界格线。)' + ); +} + +// 不带字的底图 prompt:槽位换成规划写的「载体留空」版本,清掉图内文字,末尾追加留白要求。 +function composeBasePrompt(spec, instruction) { + const overlay = spec && spec.textOverlay; + if (!overlay) return (spec && spec.prompt) || ''; + const grams = textGrams(overlay.layers); + const base = overlay.base || {}; + const slots = { text: '' }; + let subjectLost = false; + for (const key of BASE_SLOT_KEYS) { + const own = base[key]; + const value = own !== undefined ? own : (spec.slots || {})[key]; + if (!value) continue; + slots[key] = cleanBaseSlot(key, value, grams, own !== undefined); + if (key === 'subject' && own === undefined) subjectLost = slots[key].length < String(value).length * 0.5; + } + // 主体缺了会拿用户原话兜底,原话里就是要排的那段字。沿用的主体删掉了一大半,说明它写的主要就是这段字 + // (书法作品、菜单、说明页),剩下的只有「不要落款」这类零碎,先说清楚主体是一张空白的载体。 + if (!slots.subject) slots.subject = blankSubject(overlay.layers); + else if (subjectLost) slots.subject = blankSubject(overlay.layers) + ';' + slots.subject; + const composed = composePrompt(slots, '', instruction, true, spec.qrCodes); + return composed.prompt + blankClause(overlay.layers); +} + +// 底图槽位的清理。规划常常只改写 subject,别的槽位沿用有字版本,里面「墨字与纸面反差明确、字口锐利」 +// 「浓黑松烟墨」「共 324 字」这类分句会把模型往写字上引,删掉;说「没有 / 不要 / 留空」的分句本身就是留白要求,保留。 +// 沿用的排除项例外:「不要错字、不要横排文字」说的是字怎么写,等于告诉模型这里有字,删掉(末尾的留白要求 +// 已经说了不要任何字)。任何槽位里漏进了原文(连续 4 个汉字和文字层一样)的分句也删掉。 +const TEXTY_CLAUSE = + /墨字|墨迹|墨色|墨痕|浓墨|淡墨|松烟墨|字迹|字口|字形|字体|字样|字幅|字号|字位|字距|字间|列距|行距|单字|逐字|照写|漏字|错字|别字|标点|笔画|笔意|笔锋|书法|书写|写着|写有|写满|题字|题写|文字|汉字|诗文|正文|全文|落款|楷书|行书|行楷|草书|隶书|篆书|字帖|碑帖|印刷体|宋体|黑体|楷体|仿宋|艺术字|手写体|标语|印着|印有|刻着|刻有|横排|竖排|阅读顺序|首列|末列|第[一二三\d]+列|\d+\s*个?(?:字(?!母)|列)|\b(?:text|lettering|calligraphy|inscription|handwriting|typography|typeface|font)\b/i; +const NEGATED_CLAUSE = /没有|不要|不带|不含|不写|不出现|不能有|无任何|无字|空白|留空|留白|去掉|\bno\b|\bwithout\b/i; +// 「书法长卷」「书法手卷」里的书法说的是上面写了字,载体本身是长卷:只去掉这两个字,分句留下。 +const CARRIER_PHRASE = /书法(?=长卷|手卷|作品|条幅|横幅|立轴|中堂|卷轴|册页|扇面)/g; + +function textGrams(layers) { + const grams = new Set(); + for (const layer of layers || []) { + const chars = Array.from(String((layer && layer.text) || '')).filter((ch) => isCjk(ch) && !isPunct(ch)); + for (let i = 0; i + 4 <= chars.length; i += 1) grams.add(chars.slice(i, i + 4).join('')); + } + return grams; +} + +// 排除项常把几条写在一句里(「不要文字、不要水印、不要多余的人」),整句删掉会把不相干的几条一起丢掉: +// 按否定词开头的条目拆开,只删说字的那几条。不以否定词开头的条目(「不要印刷体、宋体、黑体」里的宋体、黑体)跟着前一条走。 +const NEGATIVE_ITEM_HEAD = /^\s*(?:不要|不能|不得|不出现|不许|避免|禁止|无|没有|no\b|without\b)/i; + +function negativeItems(clause) { + const groups = []; + for (const part of clause.split(/(?<=、)/)) { + if (!groups.length || NEGATIVE_ITEM_HEAD.test(part)) groups.push(part); + else groups[groups.length - 1] += part; + } + return groups; +} + +// planned:这个槽位是规划为底图专门写的,只删漏进来的原文。删完是空的槽位返回空串,由上层兜底。 +function cleanBaseSlot(key, value, grams, planned) { + const source = planned ? String(value) : String(value).replace(CARRIER_PHRASE, ''); + let clauses = source.match(/[^,,;;。!!??\n]+[,,;;。!!??\n]*/g) || []; + if (key === 'negative' && !planned) clauses = clauses.flatMap(negativeItems); + const kept = clauses.filter((clause) => { + const bare = Array.from(clause).filter((ch) => isCjk(ch) && !isPunct(ch)).join(''); + for (const gram of grams) if (bare.includes(gram)) return false; + if (planned || !TEXTY_CLAUSE.test(clause)) return true; + return key !== 'negative' && NEGATED_CLAUSE.test(clause); + }); + return kept.join('').replace(/[,,;;、\s]+$/, '').trim(); +} + +function blankSubject(layers) { + const surfaces = dedupe((layers || []).map((layer) => layer.surface).filter(Boolean)); + return (surfaces.length ? '一张完全空白的' + surfaces.join('、') : '完全空白的载体表面') + ',上面没有任何文字、笔迹和图案'; +} + +// 进入程序排字模式之后,T 项(文字内容)改由程序验,并挂上 H-text、H-notext、S-textfit。 +// 和 H-qr 一样挂在 hard 上限裁剪之后,否则会被降级成 soft。 +// 编辑节点重写过的清单常常把 T 项丢了(真机 01b902 的追问轮只剩「补齐第 17 字」这类条目),按文字层补回来。 +function overlayChecklist(checklist, textLayers) { + let rest = (checklist || []).filter((item) => !OVERLAY_CHECK_IDS.has(item.id)); + if (!rest.some((item) => /^T\d+$/.test(item.id))) rest = textChecks(textLayers).concat(rest); + rest = rest.map((item) => (/^T\d+$/.test(item.id) ? { ...item, verify: 'program' } : item)); + return rest.concat({ ...TEXT_HARD_CHECK }, { ...NO_TEXT_CHECK }, { ...TEXT_FIT_CHECK }); +} + +function overlayLayers(image) { + return (image && image.spec && image.spec.textOverlay && image.spec.textOverlay.layers) || []; +} + +function isOverlay(image) { + return Boolean(image && image.textMode === 'overlay' && overlayLayers(image).length); +} + +// 程序知不知道字该排在哪:规划(或编辑节点)写了位置;或者是长文、竖排、书法,字本身就是画面主体, +// 默认位置(画面中间 80%)就是载体中间的一大块。 +function canPlaceText(image) { + const overlay = image && image.spec && image.spec.textOverlay; + if (!overlay || !overlay.layers.length) return false; + return Boolean(overlay.planned) || textRisk(overlay.layers).high; +} + +// 同上,也管 1.9.0 以前没有 textOverlay 的会话(按已有的文字层和原话推一遍)。给编辑节点标「位置未知」用。 +function textPlaceable(session, image) { + if (image.spec.textOverlay) return canPlaceText(image); + const inferred = normalizeTextOverlay(null, image.spec.textLayers, [session.instruction, image.title].filter(Boolean).join(' ')); + return Boolean(inferred) && textRisk(inferred.layers).high; +} + +// 这一轮送进模型的完整 prompt:程序排字模式下用不带字的底图版本。 +function promptFor(image) { + return isOverlay(image) && image.spec.basePrompt ? image.spec.basePrompt : image.spec.prompt; +} + +function enterOverlay(session, image, reason) { + image.textMode = 'overlay'; + image.textReason = reason || image.textReason || ''; + image.spec.basePrompt = composeBasePrompt(image.spec, session.instruction); + image.spec.checklist = overlayChecklist(image.spec.checklist, image.spec.textLayers); +} + +// 字体出了问题(缺字、找不到字体)时程序排不了字,回到让模型直接画。 +function leaveOverlay(image) { + image.textMode = ''; + image.spec.checklist = (image.spec.checklist || []) + .filter((item) => !OVERLAY_CHECK_IDS.has(item.id)) + .map((item) => (/^T\d+$/.test(item.id) ? { ...item, verify: 'vlm' } : item)); +} + +function textFailText(summary) { + const failed = ((summary && summary.items) || []).filter((item) => !item.ok); + if (!failed.length) return '未知原因'; + return dedupe(failed.map((item) => item.reasonText + (item.detail ? '(' + item.detail + ')' : ''))).join(';'); +} + +function fontLabelOf(summary) { + return dedupe(((summary && summary.items) || []).map((item) => item.font)).join('、'); +} + +// 字数不算标点和空白:用户说「全文 324 字」时数的就是这个。 +function shortText(value, limit) { + const chars = Array.from(String(value || '')); + const max = limit || 24; + if (chars.length <= max) return chars.join(''); + const count = chars.filter((ch) => !/\s/.test(ch) && !isPunct(ch)).length; + return chars.slice(0, max).join('') + '…(共 ' + count + ' 字)'; +} + +const PUNCTUATION_LABELS = { omit: '标点不写', side: '标点写在字旁' }; + +function layoutSummary(item) { + if (!item || !item.ok) return ''; + const shape = + item.direction === 'vertical' + ? '竖排 ' + item.cols + ' 列、每列最多 ' + item.rows + ' 字' + : '横排 ' + item.lines + ' 行'; + const hasPunct = Array.from(String(item.text || '')).some((ch) => isPunct(ch)); + const marks = item.direction === 'vertical' && hasPunct && PUNCTUATION_LABELS[item.punctuation] ? ',' + PUNCTUATION_LABELS[item.punctuation] : ''; + const extras = [ + item.unified ? '和同类文字统一字号' : '', + item.indent ? '首行空 ' + item.indent + ' 格' : '', + item.superscripts ? item.superscripts + ' 个脚注符号排成上标' : '', + item.rule ? '上方画了分隔线' : '', + item.moved ? '规划位置不空,' + (item.moved > 0 ? '往下' : '往上') + '挪了画面高度的 ' + Math.abs(Math.round(item.moved * 1000) / 10) + '%' : '', + item.part ? (item.part.ok ? PART_SIDE_TEXT[item.part.side] + '的部件用 ' + item.part.color : '部件分色没做成') + (item.part.note ? '(' + item.part.note + ')' : '') : '', + ].filter(Boolean); + return shape + ',字高约 ' + item.size + 'px' + marks + (extras.length ? ',' + extras.join(',') : ''); +} + +// 交付时每张图的文字状况:给页面显示。 +function textDelivery(image, chosen) { + if (!isOverlay(image)) return null; + const summary = chosen && chosen.program && chosen.program.text; + if (!summary) return { status: 'missing', note: '这一版没有排字', items: [] }; + return { + status: summary.status, + font: fontLabelOf(summary), + note: + summary.status === 'overlay' + ? '文字由程序按原文逐字排入(' + fontLabelOf(summary) + ')' + : '文字没排上:' + textFailText(summary), + items: (summary.items || []).map((item) => ({ + text: shortText(item.text, 16), + ok: Boolean(item.ok), + font: item.font || '', + layout: layoutSummary(item), + note: item.ok ? '' : item.reasonText + (item.detail ? '(' + item.detail + ')' : ''), + })), + }; +} + +// ---------------------------------------------------------------- 数据图表 +// +// 和程序排字一样的分工:规划写清每张图表的数据和位置(charts),模型只画一张在那个位置留出平整面板的底图, +// 每张候选出图后程序把图表按数据画上去、从最终文件量回来核对;面板没留空就是 H-chart 不通过,带着原因重画。 +// 图表一律由程序画,不先让模型试:模型画出来的柱子高矮和刻度本来就对不上,评审也量不出来。 +const CHART_MARK = '图表留白:'; +const CHART_RETRY_MARK = '(上一轮底图上预留给图表的地方没有留空'; +const CHART_DEFAULT_BOX = [0.08, 0.12, 0.92, 0.92]; + +const CHART_HARD_CHECK = { + id: 'H-chart', + kind: 'hard', + verify: 'program', + text: '数据图表由程序按数据画进底图留空的面板:坐标轴、刻度、数值标注和比例从最终文件量回来核对通过(程序实测)', +}; +const NO_CHART_CHECK = { + id: 'H-nochart', + kind: 'hard', + verify: 'vlm', + text: '除程序画的图表以外,画面里没有模型自己画的图表、坐标轴、柱子、折线或乱码数字', +}; +const CHART_FIT_CHECK = { + id: 'S-chartfit', + kind: 'soft', + verify: 'vlm', + text: '程序画的图表和画面协调:面板平整,图表没有被遮挡或裁切,配色和画面风格相配', +}; +const CHART_CHECK_IDS = new Set([CHART_HARD_CHECK.id, NO_CHART_CHECK.id, CHART_FIT_CHECK.id]); +const CHART_PATCH_FIELDS = ['type', 'title', 'unit', 'categories', 'series', 'stacked', 'labels', 'y_min', 'y_max', 'box', 'surface', 'colors', 'decimals', 'font']; + +function imageCharts(image) { + return (image && image.spec && Array.isArray(image.spec.charts) && image.spec.charts) || []; +} + +function hasCharts(image) { + return imageCharts(image).length > 0; +} + +// 规划(或编辑节点)写的图表归一,并核对每个数是不是用户给的。 +function chartsWithCheck(list, sources) { + const text = sources.join('\n'); + return list.map((chart) => ({ ...chart, check: chartDataCheck(chart, text) })); +} + +function chartClause(charts) { + const parts = charts.map((chart) => (chart.surface ? chart.surface + ',' : '一块平整的浅色面板,') + describeBox(chart.box || CHART_DEFAULT_BOX)); + return ( + CHART_MARK + + parts.join(';') + + ':这里保持完全空白、平整干净,不要画任何图表、坐标轴、刻度、网格、柱子、折线、饼图、图例、数字或文字,' + + '图表之后由程序按数据画进去。画面其他地方也不要出现图表、坐标轴和数字。' + ); +} + +function ensureChartClause(action, image) { + if (!hasCharts(image)) return action; + const prompt = String(action.prompt || '').trim(); + if (prompt.includes(CHART_MARK)) return action; + return { ...action, prompt: prompt + (prompt && !/[。.!!??]$/.test(prompt) ? '。' : '') + chartClause(imageCharts(image)) }; +} + +function withChartRetry(prompt, reason) { + const base = String(prompt || '').trim(); + if (base.includes(CHART_RETRY_MARK)) return base; + return ( + base + + CHART_RETRY_MARK + + (reason ? ':' + reason : '') + + '。这一轮必须把这块区域留成一整块空白、平整的面板,上面不要画任何图形、线条、数字或花纹。)' + ); +} + +// 图表的三项检查挂在 hard 上限裁剪之后(和 H-qr 一样),否则会被降级成 soft。 +function chartChecklist(checklist, charts) { + const rest = (checklist || []).filter((item) => !CHART_CHECK_IDS.has(item.id)); + return charts && charts.length ? rest.concat({ ...CHART_HARD_CHECK }, { ...NO_CHART_CHECK }, { ...CHART_FIT_CHECK }) : rest; +} + +function chartFailText(summary) { + const failed = ((summary && summary.items) || []).filter((item) => !item.ok); + if (!failed.length) return '未知原因'; + return dedupe(failed.map((item) => item.reasonText + (item.detail ? '(' + item.detail + ')' : ''))).join(';'); +} + +function chartVerifyText(summary) { + return ((summary && summary.items) || []) + .map((item) => (CHART_TYPES[item.type] || '图表') + (item.verify ? ':' + item.verify.note : '')) + .join(';'); +} + +// 交付时每张图的图表状况:给页面显示,也决定要不要请用户核对数据。 +function chartDelivery(image, chosen) { + const charts = imageCharts(image); + if (!charts.length) return null; + const summary = chosen && chosen.program && chosen.program.chart; + const items = charts.map((chart, index) => { + const drawn = summary && (summary.items || [])[index]; + return { + type: chart.type, + typeLabel: CHART_TYPES[chart.type] || chart.type, + title: chart.title || '', + ok: Boolean(drawn && drawn.ok), + note: !drawn ? '这一版没有画图表' : drawn.ok ? (drawn.verify ? drawn.verify.note : '') : drawn.reasonText + (drawn.detail ? '(' + drawn.detail + ')' : ''), + verify: drawn && drawn.verify ? { ok: Boolean(drawn.verify.ok), maxErrorPx: drawn.verify.maxErrorPx, checked: drawn.verify.checked } : null, + unit: chart.unit || '', + categories: chart.categories, + series: chart.series.map((one) => ({ name: one.name, values: one.values })), + unverified: chart.check && !chart.check.ok ? chart.check.missing : [], + labelsDropped: Boolean(drawn && drawn.labelsDropped), + warning: (drawn && drawn.warning) || '', + }; + }); + const status = summary ? summary.status : 'missing'; + return { + status, + note: + status === 'overlay' + ? '图表由程序按数据画入,从最终文件量回来核对通过' + : status === 'missing' + ? '这一版没有画图表' + : '图表没画上:' + chartFailText(summary), + items, + }; +} + +// 交付里要请用户处理的图表问题,只挂在这一版交付上。 +function chartQuestions(image, info) { + if (!info) return []; + const out = []; + if (info.status === 'failed') out.push(image.id + ' 的图表没能由程序画进去(' + info.note.replace(/^图表没画上:/, '') + ')。可以回复「重画」重新生成这张图'); + info.items.forEach((item, index) => { + const name = '图表 ' + (index + 1) + (item.title ? '「' + item.title + '」' : ''); + if (item.unverified.length) { + out.push(image.id + ' 的' + name + '里有 ' + item.unverified.length + ' 个数在你的原话里找不到(' + item.unverified.join('、') + '),是按规划写的数画的,请核对;要改就直接说「把……改成……」'); + } + if (item.labelsDropped) out.push(image.id + ' 的' + name + '数值太密,没有逐个标注,数值可以对照坐标轴读'); + if (item.warning) out.push(image.id + ' 的' + name + ':' + item.warning); + }); + return out; +} + +// 只改图表时沿用上一版的评审结论,同时按这一版的程序结果改写程序验的项。 +function carryProgramVerdict(verdict, image, candidate) { + if (!verdict) return null; + const copy = JSON.parse(JSON.stringify(verdict)); + copy.checks = copy.checks || {}; + const checklist = image.spec.checklist || []; + const typed = candidate.program && candidate.program.text; + const charted = candidate.program && candidate.program.chart; + for (const item of checklist) { + if ((/^T\d+$/.test(item.id) || item.id === TEXT_HARD_CHECK.id) && typed && typed.status === 'overlay') { + copy.checks[item.id] = { result: 'pass', note: '文字由程序按原文重新排入' }; + } + if (item.id === CHART_HARD_CHECK.id && charted) { + copy.checks[item.id] = charted.status === 'overlay' ? { result: 'pass', note: '图表由程序按数据重新画入并核对' } : { result: 'fail', note: chartFailText(charted) }; + } + } + const extra = copy.extraFails || []; + const hardIds = checklist.filter((item) => item.kind === 'hard').map((item) => item.id); + const isPass = (id) => copy.checks[id] && (copy.checks[id].result === 'pass' || copy.checks[id].result === 'uncertain'); + copy.hardPassed = hardIds.filter(isPass).length; + copy.hardTotal = hardIds.length + extra.length; + copy.hardFailed = hardIds.filter((id) => !isPass(id)).concat(extra); + return copy; } function asText(value) { @@ -102,10 +956,15 @@ function normalizeSlots(raw, textLayers) { // 把槽位拼成一句完整的中文长 prompt。返回缺失的必填槽位,供上层告警。 // 完全没有 slots 的老格式(planner 直接给 prompt)原样沿用,不塞兜底槽位。 -function composePrompt(slots, freeText, instruction, hasSlots) { +// 画面里要放二维码时(不管有没有内容)都追加 QR_CLAUSE:码画得越规整,程序越容易找到它、换成真码; +// 没给内容的装饰码也一样,用户之后补一句内容就能直接在原图上换。 +function composePrompt(slots, freeText, instruction, hasSlots, qrCodes) { + const qrClause = (qrCodes || []).length ? QR_CLAUSE : ''; + const warm = warmPalette([instruction, slots && slots.style, slots && slots.scene].filter(Boolean).join(' ')); + const colorClause = warm ? COLOR_CLAUSE_WARM : COLOR_CLAUSE; if (!hasSlots) { const legacy = String(freeText || instruction || '').trim(); - return { prompt: legacy ? legacy + '。' + COLOR_CLAUSE : COLOR_CLAUSE, missing: [], slots: null }; + return { prompt: (legacy ? legacy + '。' : '') + qrClause + colorClause, missing: [], slots: null }; } const missing = []; const filled = { ...slots }; @@ -116,6 +975,7 @@ function composePrompt(slots, freeText, instruction, hasSlots) { filled[spec.key] = spec.key === 'subject' ? String(instruction || '').trim() : spec.fallback; } + if (warm && filled.style) filled.style = stripWarmStyle(filled.style); const parts = []; const head = [filled.style, filled.shot].filter(Boolean).join(','); if (head) parts.push(head + '。'); @@ -125,8 +985,10 @@ function composePrompt(slots, freeText, instruction, hasSlots) { parts.push(spec.label + ':' + filled[spec.key] + '。'); } if (filled.text) parts.push('图内文字:' + filled.text + ',字形正确、清晰可读。'); - if (filled.negative) parts.push('不要出现:' + filled.negative + '。'); - parts.push(COLOR_CLAUSE); + const negative = warm ? stripWarmNegative(filled.negative) : filled.negative; + if (negative) parts.push('不要出现:' + negative + '。'); + if (qrClause) parts.push(qrClause); + parts.push(colorClause); const extra = String(freeText || '').trim(); // planner 同时给了 slots 和自由 prompt 时,自由部分只作为补充追加,避免和槽位重复。 @@ -153,7 +1015,15 @@ export async function runSession(session, deps) { // 角色基准图必须赶在故事图之前:它就是后面每张图的外观基准。 await buildCharacterRefs(session, deps); if (isCancelled(session)) return; - const queue = session.images.filter((image) => !image.blocked); + // 拿用户给的图直接做动图的,不用先画这张图;其余照常出图,动图的参考图也在其中。 + // 规划点名的 R 编号会话里没有的,退回先画一张参考图。 + for (const image of session.images) { + if (isAnimation(image) && image.spec.animation.reference !== 'self' && !usesGivenReference(session, image)) { + image.spec.animation.reference = 'self'; + } + } + const direct = session.images.filter((image) => !image.blocked && isAnimation(image) && usesGivenReference(session, image)); + const queue = session.images.filter((image) => !image.blocked && !direct.includes(image)); const limit = Math.max(1, Math.min(DEFAULTS.imageConcurrency, deps.concurrency || DEFAULTS.imageConcurrency)); let cursor = 0; const workers = Array.from({ length: Math.min(limit, queue.length) }, async () => { @@ -179,12 +1049,220 @@ export async function runSession(session, deps) { }); await Promise.all(workers); if (isCancelled(session)) return; + for (const image of session.images) { + if (image.blocked || !isAnimation(image)) continue; + if (direct.includes(image)) { + await runAnimation(session, image, deps); + if (isCancelled(session)) return; + continue; + } + // 先画出来的参考图:交付给用户确认,帧等确认之后再出。 + if (image.chosen) { + ensureAnimationState(image).state = 'awaiting_confirm'; + deps.emit(session, 'plan', image.id + ' 的 GIF 参考图画好了,交给你确认之后再出 ' + image.spec.animation.frames.length + ' 帧'); + } + } + // 要拼成 GIF 的组图:评审通过之后补过渡格(拼 GIF 在交付时做)。 + for (const image of session.images) { + if (image.blocked || isAnimation(image) || !image.chosen || !image.spec.panelGif) continue; + await ensurePanelTweens(session, image, deps, tweenHooks()); + if (isCancelled(session)) return; + } // 所有图都出完了才比得了:逐图评审看不到别的图,跨图漂移只能在这里抓。 await crossImageReview(session, deps); if (isCancelled(session)) return; await deliver(session, deps); } +// GIF 的帧:出帧、评审、重画都在 animation.mjs 里,这里把工作流的生图、错误分类和交付工具交给它。 +function animationHooks() { + return { executeAction, describeError, isFatal, isContentPolicy, isCancelled, lastDelivered, queuePixelWork }; +} + +async function runAnimation(session, image, deps) { + try { + await runFrames(session, image, deps, animationHooks()); + } catch (error) { + if (isCancelled(session)) return; + const message = describeError(error); + const anim = ensureAnimationState(image); + anim.state = 'failed'; + anim.error = message; + if (!image.chosen) image.status = 'failed'; + if (isFatal(error)) session.fatal = message; + deps.emit(session, 'paint', image.id + ' 的 GIF 没做成:' + message, { level: 'error' }); + } +} + +// 动图的追问。返回 true 表示已经处理完并交付;false 表示这次改的是参考图本身,交给下面的普通编辑。 +// - 确认参考图(start):开始出帧。 +// - 调节奏、尺寸、循环,删帧、调顺序:不调生图,重新合成;重画、加帧:只画这几帧。 +// - 改主体或画面本身(换颜色、换衣服):照常改参考图,出过的帧作废,改完重新等用户确认。 +async function editAnimation(session, image, target, deps) { + const ops = target.animation; + const anim = ensureAnimationState(image); + const waiting = anim.state === 'awaiting_confirm' || !anim.state; + if (ops && ops.start && (waiting || anim.state === 'failed')) { + await runAnimation(session, image, deps); + if (!isCancelled(session)) await deliver(session, deps); + return true; + } + const adjusts = hasAnimationAdjustments(ops); + // 编辑节点写了这种 GIF 用不上的调整(来回播、弹一下、第一格停多久是组图拼 GIF 的),又没给 prompt: + // 当成没有可做的改动。落到下面会被当成改参考图,照原 prompt 重画一张,出过的帧全部作废。 + const inert = Boolean(ops && !ops.start && !adjusts && !target.promptGiven); + if ((adjusts || inert) && !target.promptGiven) { + const { changes, regenerate } = applyAnimationOps(image, ops); + if (waiting) { + deps.emit(session, 'edit', image.id + ' 记下 GIF 的调整:' + (changes.join(',') || '没有可做的改动') + '。确认参考图之后按新的安排出帧'); + } else { + deps.emit(session, 'edit', image.id + ' 调整 GIF:' + (changes.join(',') || '没有可做的改动') + (regenerate ? '' : ',不重新生图')); + if (regenerate || anim.state === 'failed') await runAnimation(session, image, deps); + if (isCancelled(session)) return true; + } + await deliver(session, deps); + return true; + } + if (!waiting) resetForNewReference(image); + else { + anim.state = 'awaiting_confirm'; + image.spec.animation.reference = 'self'; + } + return false; +} + +// 程序转圈 GIF 的追问(见 motiongif.mjs)。返回 true 表示已经处理完并交付。 +// - 要让这张图转起来(animation.motion):程序转出各帧,不重新生图; +// - 已经转过,只调快慢、方向、格数:重新转; +// - 要改画面本身(写了 prompt):交给下面的普通编辑,顺带说的转法先记下,交付时按新图重新转。 +async function editMotionGif(session, image, target, deps) { + const ops = target.animation; + const had = Boolean(image.spec.motionGif); + const wants = Boolean(ops && ops.motion && (!had || ops.motion.type)); + if (!had && !wants) return false; + if (target.promptGiven) { + if (ops) image.spec.motionGif = mergeMotionOps(image.spec.motionGif, ops).spec; + return false; + } + if (!ops) return false; + const { spec, changes } = mergeMotionOps(image.spec.motionGif, ops); + if (!spec) return false; + image.spec.motionGif = spec; + // 一张图只做一种 GIF:转起来就不再按组图切。 + image.spec.panelGif = null; + if (wants && image.motionGif && image.motionGif.state === 'failed') image.motionGif = null; + deps.emit( + session, + 'edit', + !had + ? image.id + ' 由程序绕圆心转出 GIF,不重新生图' + : changes.length + ? image.id + ' 调整程序转的 GIF:' + changes.join(',') + ',不重新生图' + : image.id + ' 程序转的 GIF 没有可做的改动', + ); + const state = await composeMotionGif(session, image, deps, panelHooks()); + if (isCancelled(session)) return true; + if (!had && (!state || state.state !== 'done')) { + image.spec.motionGif = null; + image.motionGif = null; + image.panelGifNotice = image.id + ' 没转成 GIF(' + ((state && state.error) || '没有可以转的图') + '),图保持原样'; + } + await deliver(session, deps); + return true; +} + +// 组图拼 GIF 的追问(见 panelgif.mjs)。返回 true 表示已经处理完并交付。 +// - 要把组图做成 GIF(from_panels,或者编辑节点按 GIF 参考图的写法写了 start):切格拼 GIF,组图不重画, +// 相邻两格之间补过渡格(一次生图、从候选里挑,见 paneltween.mjs); +// - 已经拼过,只调快慢、顺序、去掉某一格:重新拼; +// - 要改组图本身(写了 prompt):交给下面的普通编辑,顺带说的节奏先记下,交付时按新的组图重新拼。 +async function editPanelGif(session, image, target, instruction, deps) { + const ops = target.animation; + const had = Boolean(image.spec.panelGif); + const wants = Boolean(ops && (ops.fromPanels || ops.start)); + if (!had && !wants) return false; + // 切换时弹一下只在用户这次的话里点名才加。编辑节点会把清单里「切换时轻微弹一下」照抄成 bounce:true: + // 真机 20261119-250f55 第 3 轮说「重新变成gif」,又拼出了每次切换都跳一下的那一版。 + if (ops && ops.bounce === true && !bounceAsked(instruction)) { + ops.bounce = undefined; + deps.emit(session, 'edit', image.id + ' 编辑节点写了「切换时弹一下」,你这次的话里没提,没有加'); + } + if (target.promptGiven) { + if (ops) image.spec.panelGif = mergePanelOps(image.spec.panelGif, ops, image.panelGif).spec; + return false; + } + // 写了 animation、没写 prompt:编辑节点要调的是 GIF 本身,就算一项都没改成(只写了上面去掉的弹一下)也在这里交付, + // 不能落到下面的普通编辑:那里会拿用户的原话当 prompt 重画组图。 + if (!ops) return false; + const { spec, changes } = mergePanelOps(image.spec.panelGif, ops, image.panelGif); + image.spec.panelGif = spec; + // 用户又说了一次「做成 GIF」:上次没拼成的结果不当缓存,重新拼。 + if (wants && image.panelGif && image.panelGif.state === 'failed') image.panelGif = null; + // 相邻两格之间补过渡格:在做 GIF(第一次、或者又说了一遍「变成 GIF」)时补;只调节奏时只补调整后多出来的步子; + // 用户说「更顺一点」就把之前没补成的再试一次。切不出格子的这里什么都不做。 + const tweenMode = ops.smooth === true ? 'retry' : !had || wants ? 'make' : 'adjust'; + await ensurePanelTweens(session, image, deps, tweenHooks(), { mode: tweenMode }); + if (isCancelled(session)) return true; + if (ops.redo.length || ops.add.length) { + deps.emit(session, 'edit', image.id + ' 是组图拼成的 GIF,没法单独重画或加一格:要改哪一格,直接说改成什么样,改完组图会重新拼', { level: 'warn' }); + } + // 组图本身不重画;补过渡格要生图的话,上面已经单独说了。 + deps.emit( + session, + 'edit', + !had + ? image.id + ' 把组图切格拼成 GIF,组图不重画' + (changes.length ? '(' + changes.join(',') + ')' : '') + : changes.length + ? image.id + ' 调整组图拼的 GIF:' + changes.join(',') + ',组图不重画' + : image.id + (wants ? ' 重新拼组图的 GIF,设定不变' : ' 组图拼的 GIF 没有可做的改动'), + ); + const state = await composePanelGif(session, image, deps, panelHooks()); + if (isCancelled(session)) return true; + if (!had && (!state || state.state !== 'done')) { + // 拼不成就不留这个设定:之后每次交付都会再切一遍,还会一直挂着「没拼成」。 + image.spec.panelGif = null; + image.panelGif = null; + image.panelTweens = null; + // 这个会话是几张单独的图(四张一组的漫画):面板只拼一张分格组图,几张图按顺序拼要用聊天里的 h3-gif。 + const several = session.images.filter((item) => item.chosen).length >= 2; + image.panelGifNotice = + image.id + ' ' + ((state && state.error) || '没有可以拼的图') + ',这次没有做 GIF,图保持原样' + + (state && state.notGrid + ? several + ? '。这几张单独的图要按顺序拼成 GIF,可以在聊天里说「把这几张图拼成 GIF」,由 Agent 用随包的 h3-gif 工具拼' + : '。要让一张单独的图动起来,可以新开一次任务:上传这张图,说要它做什么动作' + : ''); + } + await deliver(session, deps); + return true; +} + +function panelHooks() { + return { queuePixelWork, lastDelivered, describeError }; +} + +// 过渡格要调生图:比拼 GIF 多了生图和错误分类。 +function tweenHooks() { + return { executeAction, describeError, isFatal, isContentPolicy, isCancelled, lastDelivered, queuePixelWork }; +} + +// 组图 GIF 有几步没补上过渡格:卡片上说一句为什么(不放进交付说明:GIF 照样能用,只是那几步直接切换)。 +function tweenNoteFor(image, panelState) { + const spec = image.spec.panelGif; + if (!spec || spec.smooth === false) return ''; + const state = image.panelTweens; + if (!state || state.sourceId !== panelState.sourceId) return ''; + const failed = state.pairs.filter((pair) => pair.status === 'failed'); + if (state.note && !state.pairs.length) return state.note; + if (!failed.length) return ''; + return '第 ' + failed.map((pair) => pair.from + '→' + pair.to).join('、') + ' 步没补上过渡格(' + failed[0].problem + '),这几步直接切换'; +} + +function withoutUnaskedBounce(spec, words) { + if (spec && spec.bounce && !bounceAsked(words)) spec.bounce = false; + return spec; +} + // 关键待确认必须在出图之前问。事后在交付里堆一串"需要你确认"没有意义: // 图已经生成完了,用户看到也只能重跑一遍。 const MAX_BLOCKERS = 2; @@ -241,13 +1319,135 @@ function normalizeBlockers(raw) { // 检索节点:planner 只产出「要搜什么」(ref_queries),真正的搜图由运行时调 Connector 工具。 // 失败不阻断主流程——降级成无参考图文生图,并把原因写进交付的待确认项。 -async function runRefSearch(session, queries, deps) { +// options.ask === false:失败时不往交付里加提问,由调用方自己写(追问里重新找图时按检索项逐条说)。 +// ---------------------------------------------------------------- 检索预算 +// +// 要不要检索、搜几项、肯花多大力气,由规划决定(ref_budget),运行时只负责按预算执行。 +// 计数的是两样真正花时间的调用:搜图工具(第一次检索每项各算一次,换说法重搜每次一次)和候选判定 +// (每批一次,一次判定是一个模型回合)。下载不单独计:每批判定之前才下那 3 张。 +// 每搜一项都给它预留一次判定,别的项换说法重搜、多判几批都不能占这一次(见 spendRef); +// 判定其实没调用模型的那一批不算(见 refundRef)。预算花完就停,没搜的、没判完的项照实写进交付说明。 +function normalizeRefBudget(raw, count) { + if (!(count > 0)) return 0; + const value = Number(raw); + if (raw === undefined || raw === null || raw === '' || !Number.isFinite(value)) { + return Math.min(DEFAULTS.refBudgetMax, Math.max(DEFAULTS.refBudgetDefault, count * 2)); + } + return Math.max(0, Math.min(DEFAULTS.refBudgetMax, Math.round(value))); +} + +// reserved:已经搜了、还没判过的项,每项预留的那一次判定。 +function makeRefBudget(limit) { + return { limit, used: 0, searches: 0, gates: 0, reserved: 0, stopped: [] }; +} + +// 先付后做:付不起就返回 false,调用方跳过这一步。 +// 别的项预留的判定不能占:判定阶段同时判两项,以前谁先用谁先得,前一项第一批被全否、换说法重搜, +// 会把后一项预留的那次判定用掉,后一项搜了却一次都没判(预算 6、3 项时实测)。 +// 所以换说法重搜、第 2 批以后的判定,只能用预留之外的预算;某一项自己的第一次判定用它自己那一份。 +function spendRef(budget, kind, entity) { + if (!budget) return true; + const own = kind === 'gate' && Boolean(entity && entity.reserved); + const others = budget.reserved - (own ? 1 : 0); + if (budget.used + 1 + others > budget.limit) return false; + budget.used += 1; + if (kind === 'search') budget.searches += 1; + else budget.gates += 1; + if (own) { + entity.reserved = false; + budget.reserved -= 1; + } + return true; +} + +// 扣了次数、这一步其实没调用模型(候选一张都没下载下来、只有一张可用直接选用):次数还回去, +// 用的是这一项自己预留的那一份的话,预留也还回去。 +function refundRef(budget, kind, entity, own) { + if (!budget) return; + budget.used -= 1; + if (kind === 'search') budget.searches -= 1; + else budget.gates -= 1; + if (own && entity) { + entity.reserved = true; + budget.reserved += 1; + } +} + +function refBudgetLine(budget) { + if (!budget) return ''; + return '检索预算 ' + budget.limit + ' 次,用了 ' + budget.used + ' 次(搜图 ' + budget.searches + '、判定 ' + budget.gates + ')'; +} + +// 一张候选都没拿到的项(搜图服务报错、或者一张都没回)用 alt_query 再搜一次。预算里给它留出判定的那一次: +// 第一次检索时每项都预留过一次判定,这一项还没用过,所以只要再付得起这一次搜图就行。 +async function retryWithAltQueries(session, queries, result, deps, budget) { + const hits = result.ok + ? result.hits.slice() + : queries.map((item) => ({ task_name: item.task_name, query: item.query, url: '', title: '', source: '', candidates: [] })); + let changed = false; + for (const item of queries) { + const index = hits.findIndex((hit) => hit.task_name === item.task_name); + const hit = index >= 0 ? hits[index] : null; + if (hit && hit.candidates && hit.candidates.length) continue; + if (!item.alt_query) continue; + if (!spendRef(budget, 'search')) { + deps.emit(session, 'think', '「' + item.query + '」没搜到,检索预算不够再换说法搜一次', { level: 'warn' }); + continue; + } + // 失败原因里带错误码的(「task:(CONNECTOR_PROVIDER_ERROR)…」)是服务报错,其余是一张都没回。 + const errored = String(result.reason || '').includes(item.task_name + ':('); + deps.emit(session, 'think', '「' + item.query + '」' + (errored ? '搜图服务报错' : '一张都没搜到') + ',换规划给的另一种说法再搜一次:' + item.alt_query, { level: 'warn' }); + const again = await searchRefImages(deps.context, [{ ...item, query: item.alt_query }]); + if (again.stop) break; + const got = again.ok && again.hits[0] && again.hits[0].candidates.length ? again.hits[0] : null; + if (!got) continue; + const next = { ...got, task_name: item.task_name, tried: [item.query] }; + if (index >= 0) hits[index] = next; + else hits.push(next); + changed = true; + } + if (!changed) return result; + const missing = queries.filter((item) => !hits.some((hit) => hit.task_name === item.task_name && hit.url)); + return { + ...result, + ok: true, + hits, + reason: missing.length ? '部分实体没搜到:' + missing.map((item) => item.task_name).join('、') : '', + }; +} + +async function runRefSearch(session, queries, deps, options) { + const ask = !options || options.ask !== false; + const budget = (options && options.budget) || null; + // 第一次检索每项各算一次搜图,并给它留出一次判定:只搜不判等于白搜(预算 2、3 项时, + // 先把 2 次都花在搜前两项上,结果一项都判不了)。付不起「搜 1 + 判 1」的项不搜, + // 排在后面的先让出预算:规划把最要紧的写在前面。 + if (budget) { + const affordable = []; + budget.stoppedTasks = budget.stoppedTasks || []; + for (const item of queries) { + // 这一项要搜 1 次,还要给它留 1 次判定;已经搜了的项各自留的那一次也不能动。 + if (budget.used + 1 + budget.reserved + 1 <= budget.limit && spendRef(budget, 'search')) { + budget.reserved += 1; + affordable.push(item); + } else { + budget.stopped.push(item.query); + budget.stoppedTasks.push(canonicalRef(item.task_name)); + } + } + if (affordable.length < queries.length) { + deps.emit(session, 'think', '检索预算 ' + budget.limit + ' 次不够搜全部 ' + queries.length + ' 项,后面 ' + (queries.length - affordable.length) + ' 项不搜:' + queries.slice(affordable.length).map((item) => item.query).join('、'), { level: 'warn' }); + } + queries = affordable; + if (!queries.length) return []; + } const startedAt = Date.now(); - deps.emit(session, 'think', '检索参考图:' + queries.map((item) => item.query).join('、'), { + const useOf = (item) => (REF_USE_LABELS[item.use] || REF_USE_LABELS.subject).replace(/参考$/, ''); + deps.emit(session, 'think', '检索参考图:' + queries.map((item) => item.query + '(' + useOf(item) + ')').join('、'), { detail: detail('检索节点 · 输入', [ text( '检索意图', - queries.map((item) => item.task_name + ':' + item.query).join('\n'), + queries.map((item) => item.task_name + ':' + item.query + ' · ' + useOf(item) + (item.take ? ',' + item.take : '')).join('\n'), ), list( '相关性要求', @@ -256,7 +1456,11 @@ async function runRefSearch(session, queries, deps) { ]), }); - const result = await searchRefImages(deps.context, queries); + let result = await searchRefImages(deps.context, queries); + // 有的说法搜图服务直接报错或一张都不回(真机 20261117-21581a:「尼克·杨 问号脸 表情包 原图」报 CONNECTOR_PROVIDER_ERROR, + // 重试一次还是报错),以前就此放弃:换说法重搜只在「搜到了、判定全否」时才走,规划写好的另一种说法一次都没用上。 + // 现在一张候选都没拿到的项,用规划给的另一种说法(alt_query)再搜一次。工具没有、被拒(stop)的不再试:换说法也一样。 + if (!result.stop) result = await retryWithAltQueries(session, queries, result, deps, budget); if (!result.ok) { deps.emit(session, 'think', '参考图检索未成功:' + result.reason, { level: 'warn', @@ -267,7 +1471,7 @@ async function runRefSearch(session, queries, deps) { text('影响', '本轮按无参考图走文生图;需要真人 / 车型一致性时请手动上传参考图。'), ]), }); - session.questions.push('参考图检索未成功(' + result.reason + '),如果一致性要求高请手动上传参考图。'); + if (ask) session.questions.push('参考图检索未成功(' + result.reason + '),如果一致性要求高请手动上传参考图。'); return []; } @@ -346,7 +1550,8 @@ function settleImageRefs(session, deps, plannedRefs, byUrl, byTask, knownTasks) continue; } if (known.has(canonicalRef(ref))) { - deps.emit(session, 'think', image.id + ' 想要的参考图「' + ref + '」没搜到可用图片,已忽略', { + const unsearched = session.refBudget && (session.refBudget.stoppedTasks || []).includes(canonicalRef(ref)); + deps.emit(session, 'think', image.id + ' 想要的参考图「' + ref + '」' + (unsearched ? '超出检索预算,没有检索' : '没搜到可用图片') + ',已忽略', { level: 'warn', }); continue; @@ -380,14 +1585,20 @@ function settleImageRefs(session, deps, plannedRefs, byUrl, byTask, knownTasks) // ---------------------------------------------------------------- 参考图 // 检索结果收成统一的"实体"结构。 -// 检索来的候选和规划直接给的公网地址走同一条下载 + 判定链路,只是后者不做相关性判定。 -function refEntities(session, hits, queries) { - const requirementOf = new Map(); - for (const item of queries) requirementOf.set(item.task_name, item.prompt || ''); +// 检索来的候选和用户给的图片地址走同一条下载 + 判定链路,只是后者不做相关性判定。 +// options.directUrls:直接给出的图片地址(默认取各图 spec.refs 里的地址,已经按用户原话过滤过); +// options.exclude:不再考虑的地址(追问里用户说不对的那张旧参考图); +// options.keyPrefix:判定拼图的文件名前缀,追问轮用自己的前缀,不覆盖首轮的拼图。 +function refEntities(session, hits, queries, options) { + const opts = options || {}; + const exclude = opts.exclude || new Set(); + const specOf = new Map(); + for (const item of queries) specOf.set(item.task_name, item); + const prefix = opts.keyPrefix || 'q'; const entities = []; const byHead = new Map(); for (const hit of hits) { - const pool = (hit.candidates || []).filter((item) => item && item.url); + const pool = (hit.candidates || []).filter((item) => item && item.url && !exclude.has(item.url)); if (!pool.length) continue; const head = pool[0].url; const existing = byHead.get(head); @@ -396,44 +1607,80 @@ function refEntities(session, hits, queries) { if (hit.task_name && existing.taskNames.indexOf(hit.task_name) < 0) existing.taskNames.push(hit.task_name); continue; } + const spec = specOf.get(hit.task_name) || {}; const entity = { - key: 'q' + entities.length, + key: prefix + entities.length, taskNames: hit.task_name ? [hit.task_name] : [], query: hit.query, + altQuery: spec.alt_query || '', note: hit.query, - requirement: requirementOf.get(hit.task_name) || '', + requirement: spec.prompt || '', + use: spec.use || 'subject', + take: spec.take || '', pool, cursor: 0, searches: 0, - queried: new Set([hit.query]), - seen: new Set(pool.map((item) => item.url)), + // 第一次检索报错、换了说法才搜到的,原来那个说法也算搜过,之后换说法重搜不再用它。 + queried: new Set([hit.query, ...(hit.tried || [])]), + seen: new Set([...pool.map((item) => item.url), ...exclude]), direct: false, }; byHead.set(head, entity); entities.push(entity); } - const directSeen = new Set(); + const directUrls = opts.directUrls || directRefUrls(session); + for (const ref of directUrls) { + entities.push({ + key: 'u' + entities.length, + taskNames: [], + query: ref, + altQuery: '', + // 地址可能有好几张,都叫"你给的地址"在时间线上分不清谁是谁,带上文件名。 + note: '你给的 ' + shortUrlLabel(ref), + requirement: '', + pool: [{ url: ref, title: '', source: ref }], + cursor: 0, + searches: 0, + queried: new Set(), + seen: new Set([ref]), + direct: true, + }); + } + return entities.slice(0, DEFAULTS.refDownloadLimit); +} + +function directRefUrls(session) { + const out = []; for (const image of session.images) { for (const ref of image.spec.refs || []) { - if (typeof ref !== 'string' || !/^https?:\/\//i.test(ref) || directSeen.has(ref)) continue; - directSeen.add(ref); - entities.push({ - key: 'u' + entities.length, - taskNames: [], - query: ref, - // 规划可能给好几张地址,都叫"规划给出的地址"在时间线上分不清谁是谁,带上文件名。 - note: '规划给出的 ' + shortUrlLabel(ref), - requirement: '', - pool: [{ url: ref, title: '', source: ref }], - cursor: 0, - searches: 0, - queried: new Set(), - seen: new Set([ref]), - direct: true, - }); + if (typeof ref === 'string' && /^https?:\/\//i.test(ref) && out.indexOf(ref) < 0) out.push(ref); } } - return entities.slice(0, DEFAULTS.refDownloadLimit); + return out; +} + +// 图片地址只认用户自己说过的:规划和编辑节点凭记忆写的网址多半是编的(真机 20261112-8a442a: +// 编辑节点写了一个 Wikimedia 地址,缩略图、原图、文件页全是 404,生图请求因此连败 6 轮), +// 就算碰巧能打开,直接给的地址不经过相关性判定,也可能是一张错的图。要找图就走检索。 +// 返回过滤后的 refs,丢掉的地址在时间线上说一句。 +function keepUserUrls(session, image, refs, deps, extraSources) { + const sources = qrSources(session, extraSources); + const out = []; + for (const ref of refs || []) { + if (typeof ref !== 'string' || !/^https?:\/\//i.test(ref)) { + out.push(ref); + continue; + } + if (isVerbatim(ref, sources)) { + out.push(ref); + continue; + } + deps.emit(session, 'think', image.id + ' 的参考图地址「' + shortUrlLabel(ref) + '」不在你说过的话里,已忽略;要找图请让它重新检索', { + level: 'warn', + detail: detail('忽略的参考图地址', [text('地址', ref), text('原因', '不是你给的地址。凭记忆写的网址多半打不开,也没经过相关性判定。')]), + }); + } + return out; } // 从地址里取一个能认出来的短标签:优先文件名,没有就用主机名。 @@ -458,103 +1705,137 @@ function cacheOf(deps) { // 相关性判定:把这一批候选拼成一张带编号的材料图,让 VLM 挑一张。 // 挑不出来比硬塞一张强得多——错的参考图会让成图看着完全正常却根本不像, // 评审端也发现不了(它只对照参考图,看不出参考图本身是错的)。 +// +// 返回 { entry, reason, shown, skipped }:shown 是真正拼进材料图、判定节点看过的候选(usable 下标), +// skipped 是解不开、没送去判定的。时间线要靠它们如实区分「判过不合格」和「根本没看」。 async function gateRefCandidates(session, entity, usable, deps, attempt) { - if (entity.direct) return { entry: usable[0].entry, reason: '规划直接指定,不再判定' }; - if (usable.length === 1 && !entity.requirement) { - return { entry: usable[0].entry, reason: '只有一张可用' }; - } + if (entity.direct) return { entry: usable[0].entry, reason: '你给的地址,不再判定', shown: [0], skipped: [] }; await mkdir(deps.dirs.sheets, { recursive: true }); const sheetPath = join(deps.dirs.sheets, 'cand-' + entity.key + '-' + attempt + '.png'); - let built = null; + let built; try { built = await review.buildGridSheet( - usable.map((item, index) => ({ path: item.entry.path, label: String(index + 1) })), + usable.map((item) => ({ path: item.entry.path, label: '' })), sheetPath, - { perRow: Math.min(3, usable.length) }, + { perRow: Math.min(3, usable.length), renumber: true }, ); - } catch { - built = null; + } catch (error) { + built = { sheet: null, items: [], skipped: usable.map((_, index) => ({ index, error: describeError(error) })) }; + } + const skipped = (built.skipped || []).map((item) => ({ index: item.index, error: decodeErrorText(item.error) })); + // 一张都拼不进去就是一张都没法核对:不能再像以前那样「取第一张可用图」,那张图评审端同样看不到。 + if (!built.sheet || !built.items.length) return { entry: null, reason: '候选都解不开', shown: [], skipped }; + // 拼图编号和候选的对应关系,一律以实际画进去的为准。 + const order = built.items.map((item) => item.index); + if (order.length === 1 && !entity.requirement) { + return { entry: usable[order[0]].entry, reason: '只有一张可用', shown: order, skipped }; } - // 拼图失败就别浪费一次模型调用,直接用第一张能解码的。 - if (!built || !built.items.length) return { entry: usable[0].entry, reason: '候选拼图失败,取第一张可用图' }; - // buildGridSheet 会跳过解不开的图,标签顺序和 usable 下标要重新对齐。 - const order = built.items.map((item) => usable.findIndex((row) => row.entry.path === item.path)); + // 判定只看一张拼图:只给必须的那份,不按难度加(24 个真机会话里 37 次判定全是正好 2 次)。 + const budget = toolBudget('think', { needs: [{ count: 1, what: '打开候选材料图' }], fixed: true }); + const kindOf = entity.use === 'style' || entity.use === 'layout' || entity.use === 'scene'; const instruction = [ '你是参考图筛选节点。候选图已经由程序拼成一张材料图,你只要打开它、看、给结论。', '', '要找的东西:' + entity.note, - '相关性要求:' + (entity.requirement || '(无额外要求,能清楚看到目标即可)'), + '参考用途:' + (REF_USE_LABELS[entity.use] || REF_USE_LABELS.subject) + (entity.take ? '(' + entity.take + ')' : ''), + '筛选要求:' + (entity.requirement || '(无额外要求,能清楚看到目标即可)'), '', '候选材料图:' + sheetPath, - ' 共 ' + order.length + ' 张,从左到右编号 ' + order.map((_, index) => index + 1).join('、') + ',每张左上角有数字标签。', + ' 共 ' + order.length + ' 张,每张左上角有数字标签:', + ...order.map((index, position) => { + const meta = usable[index].meta || {}; + const source = String(meta.source || '').slice(0, 40); + const title = String(meta.title || '').slice(0, 70); + return ' ' + (position + 1) + ':' + [source && '来源 ' + source, title && '标题 ' + title].filter(Boolean).join(' · '); + }), + ' 来源和标题只能帮你认出图里是什么,以图为准:电商标题经常堆关键词,不能只凭标题判定。', '', '判定规则(严格照做):', - '1. 人物必须是本人、面部清晰可辨识;车型 / logo 必须是同一款。', - '2. 这些情况一律不要选:不是本人或不是同一款、糊、严重遮挡、纯文字截图、大面积水印、明显是插画或表情包。', - '3. 拿不准就不选。宁可这一批全否,也不要塞一张错的参考图进生成。', - '4. 不要写脚本、不要裁图量像素、不要联网搜索、不要打开其它文件。只 read 上面这一张材料图。', - '5. 工具调用上限 2 次,看完立刻给结论。', + ...(kindOf + ? [ + '1. 这一项是' + REF_USE_LABELS[entity.use] + ',不要求是某一个具体的东西:只要是「要找的东西」这一类的真实样例,', + ' 而且看得清要参考的地方(画风看笔触和配色,版式看排版和留白,场景看环境),就能选。', + ' 明显是别的类型(要课本内页却是试卷,要丝网印刷海报却是照片)就不能选。', + ] + : [ + '1. 先判「是不是这个东西本身」:人物必须是本人、面部清晰可辨识;车型 / 产品 / logo 必须是同一款;建筑、地标必须是同一座;', + ' 动植物品种、食物、服饰、道具必须是同一种。认不出、或者明显是别的东西,就不能选。', + ]), + '2. 吉祥物、卡通角色、IP 形象:官方插画、官方立绘、实物雕像、正版手办或玩偶的照片都可以用,', + ' 只要认得出是它本身、外形和配色没有被改掉。同人、Q 版改造、换装、真人 cos 不要选。', + '3. 筛选要求里写的颜色、五官、肢体数量这类外观描述可能写错了:和你认出的本体对不上时,以本体为准,不要因此否掉。', + '4. 认出是本体(或这一类的真实样例)之后,再按筛选要求在这些候选里挑最合适的一张。要求里的视角、远近、构图只是偏好:', + ' 没有一张完全满足时,选最能看清它外形的那张,不要因为角度或远近不完美就全否。', + '5. 这些情况不要选:糊、严重遮挡、纯文字截图、水印或大块文字盖住主体、表情包。角落里的小水印、版权字样不算问题。', + '6. 拿不准是不是本体(或这一类)就不选。宁可这一批全否,也不要塞一张错的参考图进生成。', + '7. 不要写脚本、不要裁图量像素、不要联网搜索、不要打开其它文件。只 read 上面这一张材料图。', + '8. 工具调用上限 ' + budget.limit + ' 次,看完立刻给结论。', '', '只输出一个 JSON 对象,不要解释、不要 markdown 代码块:', '{"pick":1,"reason":"不超过 30 字"}', ' pick 填 1..' + order.length + ' 里最合适那张的编号;全都不符合要求就填 0。', ].join('\n'); + // asked:这一批真的调用了判定节点(一个模型回合)。前面那几种直接返回的没调用,检索预算不扣这一次(见 refundRef)。 let data; try { - ({ data } = await deps.agent.runJson(session, { step: 'think', instruction, maxTools: 2 })); + ({ data } = await deps.agent.runJson(session, { step: 'think', instruction, maxTools: budget.limit, budgetNote: budget.note })); } catch { - return { entry: usable[0].entry, reason: '判定节点异常,取第一张可用图' }; + return { entry: usable[order[0]].entry, reason: '判定节点异常,取第一张能看的图', shown: order, skipped, asked: true }; } const reason = asText(data.reason).slice(0, 60); const pick = Number(data.pick); - if (!pick || pick < 1 || pick > order.length) return { entry: null, reason: reason || '都不符合要求' }; - const index = order[pick - 1]; - if (index < 0) return { entry: null, reason: reason || '都不符合要求' }; - return { entry: usable[index].entry, reason: reason || '符合要求' }; + if (!pick || pick < 1 || pick > order.length) return { entry: null, reason: reason || '都不符合要求', shown: order, skipped, asked: true }; + return { entry: usable[order[pick - 1]].entry, reason: reason || '符合要求', shown: order, skipped, asked: true }; +} + +// 拼图时解不开的原因,翻成时间线上看得懂的话。 +function decodeErrorText(message) { + const value = String(message || ''); + const format = /unsupported_image_format:(\w+)/.exec(value); + if (format) return '格式不支持(' + format[1] + ')'; + if (/jpeg_cmyk_unsupported/.test(value)) return 'CMYK JPEG 不支持'; + if (/jpeg_unsupported_sof|jpeg_unsupported_precision/.test(value)) return '这种 JPEG 编码不支持'; + return '解码失败:' + value.slice(0, 40); } // 一批候选:下载到缓存 -> 机械过滤(缓存模块做)-> 相关性判定。 // 每次只取 refBatch 张:一批 3 张足够挑出一张能用的,全下既慢又把时间线塞满。 +// 返回 { picked, judged }:judged 是判定节点真正看过的张数,上层靠它决定要不要换说法重搜。 async function reviewRefBatch(session, entity, batch, deps, attempt) { const startedAt = Date.now(); const cache = cacheOf(deps); - // 检索来的候选是第三方地址,不许指向本机或内网;规划直接给的地址来自用户需求,按接口地址的规则放行。 + // 检索来的候选是第三方地址,不许指向本机或内网;用户自己给的地址按接口地址的规则放行。 const fetched = await Promise.all(batch.map((item) => cache.download(item.url, { publicOnly: !entity.direct }))); const usable = []; fetched.forEach((entry, index) => { - if (entry.ok) usable.push({ entry, meta: batch[index] }); + if (entry.ok) usable.push({ entry, meta: batch[index], fetchedIndex: index }); }); const verdict = usable.length ? await gateRefCandidates(session, entity, usable, deps, attempt) : null; const pickedPath = verdict && verdict.entry ? verdict.entry.path : ''; + const shown = new Set(verdict ? verdict.shown.map((index) => usable[index].fetchedIndex) : []); + const skippedWhy = new Map(verdict ? verdict.skipped.map((item) => [usable[item.index].fetchedIndex, item.error]) : []); const rows = fetched.map((entry, index) => { const meta = batch[index]; - if (!entry.ok) { - return { - key: '', - title: meta.title || '(无标题)', - note: meta.source || meta.url, - tag: 'warn', - text: '下载失败:' + entry.error, - }; + const base = { title: meta.title || '(无标题)', note: meta.source || meta.url }; + if (!entry.ok) return { ...base, key: '', tag: 'warn', text: '下载失败:' + entry.error }; + if (!shown.has(index)) { + return { ...base, key: 'cache/' + entry.file, tag: 'warn', text: '没送去判定:' + (skippedWhy.get(index) || '解不开') }; } const chosen = entry.path === pickedPath; - return { - key: 'cache/' + entry.file, - title: meta.title || '(无标题)', - note: meta.source || meta.url, - tag: chosen ? 'pass' : 'fail', - text: chosen ? '已选用' : '不符合要求', - }; + return { ...base, key: 'cache/' + entry.file, tag: chosen ? 'pass' : 'fail', text: chosen ? '已选用' : '不符合要求' }; }); + const judged = shown.size; + const unjudged = batch.length - judged; + const tail = unjudged ? '(另有 ' + unjudged + ' 张下载失败或解不开)' : ''; let headline; - if (verdict && verdict.entry) headline = entity.note + ':从 ' + batch.length + ' 张候选里选定 1 张'; - else if (usable.length) headline = entity.note + ':' + usable.length + ' 张候选都不符合要求,继续找'; - else headline = entity.note + ':这一批 ' + batch.length + ' 张候选都没下载成功,继续找'; + if (verdict && verdict.entry) headline = entity.note + ':判定了 ' + judged + ' 张,选定 1 张' + tail; + else if (judged) headline = entity.note + ':判定了 ' + judged + ' 张,都不符合要求' + tail + ',继续找'; + else headline = entity.note + ':这一批 ' + batch.length + ' 张候选都没能送去判定(下载失败或解不开),继续找'; deps.emit(session, 'think', headline, { level: verdict && verdict.entry ? 'info' : 'warn', @@ -568,18 +1849,36 @@ async function reviewRefBatch(session, entity, batch, deps, attempt) { ]), }); - if (!verdict || !verdict.entry) return null; - return { path: verdict.entry.path, file: verdict.entry.file, url: verdict.entry.url, reason: verdict.reason }; + const asked = Boolean(verdict && verdict.asked); + if (!verdict || !verdict.entry) return { picked: null, judged, asked }; + return { + picked: { path: verdict.entry.path, file: verdict.entry.file, url: verdict.entry.url, reason: verdict.reason }, + judged, + asked, + }; } -// 候选池用完了:换个说法再搜一轮。原查询通常太"描述性",搜索引擎更吃"实体名 + 官方图"。 -async function searchMoreCandidates(session, entity, deps) { +// 换个说法再搜一轮。两种时机:候选池用完了;或者上一批判定节点看过、全部否掉(不等池子用完)—— +// 后者说明这个说法搜回来的图本身就不对,接着往下翻同一批结果多半还是不对。 +// 先用规划给的另一种说法(原产地语言或英文的正式名称),再用通用改写。新结果排在还没判过的候选前面。 +async function searchMoreCandidates(session, entity, deps, rejected, budget) { if (entity.direct || entity.searches >= DEFAULTS.refSearchRounds) return []; - const query = broadenQuery(entity.query, entity.searches); - if (!query || entity.queried.has(query)) return []; + let query = ''; + if (entity.altQuery && !entity.queried.has(entity.altQuery)) query = entity.altQuery; + for (let round = 0; !query && round < 3; round += 1) { + const variant = broadenQuery(entity.query, round); + if (variant && !entity.queried.has(variant)) query = variant; + } + if (!query) return []; + if (!spendRef(budget, 'search')) { + entity.outOfBudget = true; + return []; + } entity.queried.add(query); entity.searches += 1; - deps.emit(session, 'think', entity.note + ':候选用完了,换个说法再搜(' + query + ')', { level: 'warn' }); + deps.emit(session, 'think', entity.note + ':' + (rejected ? '这一批都不对' : '候选用完了') + ',换个说法再搜(' + query + ')', { + level: 'warn', + }); let result; try { result = await searchRefImages(deps.context, [ @@ -591,36 +1890,128 @@ async function searchMoreCandidates(session, entity, deps) { const hit = result && result.ok ? result.hits[0] : null; const fresh = ((hit && hit.candidates) || []).filter((item) => item && item.url && !entity.seen.has(item.url)); for (const item of fresh) entity.seen.add(item.url); - entity.pool.push(...fresh); + entity.pool.splice(entity.cursor, 0, ...fresh); return fresh; } -async function resolveEntityRef(session, entity, deps) { +async function resolveEntityRef(session, entity, deps, budget) { + let rejected = false; for (let attempt = 0; attempt < DEFAULTS.refGateRounds; attempt += 1) { + if (rejected) await searchMoreCandidates(session, entity, deps, true, budget); let batch = entity.pool.slice(entity.cursor, entity.cursor + DEFAULTS.refBatch); entity.cursor += batch.length; if (!batch.length) { - const more = await searchMoreCandidates(session, entity, deps); + const more = await searchMoreCandidates(session, entity, deps, false, budget); if (!more.length) return null; batch = entity.pool.slice(entity.cursor, entity.cursor + DEFAULTS.refBatch); entity.cursor += batch.length; if (!batch.length) return null; } - const picked = await reviewRefBatch(session, entity, batch, deps, attempt); - if (picked) return picked; + // 用户给的地址不判定(见 gateRefCandidates),不花预算。这一项还没判过时用它自己预留的那一次判定。 + const own = Boolean(entity.reserved); + if (!entity.direct && !spendRef(budget, 'gate', entity)) { + entity.outOfBudget = true; + return null; + } + const result = await reviewRefBatch(session, entity, batch, deps, attempt); + // 候选一张都没下载下来、只有一张直接选用:没有调用判定节点,这一次不算。 + if (!entity.direct && !result.asked) refundRef(budget, 'gate', entity, own); + if (result.picked) return result.picked; + // 只有判定节点真看过、而且全否了,才说明是说法的问题;只是下载失败就接着翻同一批结果。 + rejected = result.judged > 0; } return null; } +// 并发 2:下载是网络等待、判定是一次模型调用,再高会顶到运行时的 agentConcurrency。 +async function resolveEntities(session, entities, deps, budget) { + const resolved = new Map(); + let cursor = 0; + // 检索时每搜一项预留了一次判定。真正要判定的是搜回了候选的项(一张都没回的不判,两项搜回同一批图的并成一项), + // 按它们重新算一遍预留,多出来的还给公共的那部分。 + if (budget) { + budget.reserved = 0; + for (const entity of entities) { + entity.reserved = !entity.direct; + if (entity.reserved) budget.reserved += 1; + } + } + const release = (entity) => { + if (!budget || !entity.reserved) return; + entity.reserved = false; + budget.reserved -= 1; + }; + const workers = Array.from({ length: Math.min(2, entities.length) }, async () => { + while (cursor < entities.length) { + const entity = entities[cursor]; + cursor += 1; + try { + const picked = await resolveEntityRef(session, entity, deps, budget); + if (picked) resolved.set(entity.key, picked); + release(entity); + } catch (error) { + release(entity); + deps.emit(session, 'think', entity.note + ' 参考图处理异常:' + String(error && error.message ? error.message : error), { + level: 'warn', + }); + } + } + }); + await Promise.all(workers); + return resolved; +} + +// 判定通过的图复制进会话,登记成 R 编号。同一份文件只登记一次;追问里重搜又搜到原来那张时沿用原编号。 +async function registerPickedRef(session, deps, entity, picked, byPath) { + let id = byPath.get(picked.path); + if (id) return id; + const existing = session.refs.find((ref) => ref.source && ref.source === picked.url); + if (existing) { + byPath.set(picked.path, existing.id); + return existing.id; + } + id = 'R' + (session.refs.length + 1); + const file = id + (picked.file.endsWith('.png') ? '.png' : '.jpg'); + await copyFile(picked.path, join(deps.dirs.refs, file)); + session.refs.push({ + id, + file, + path: join(deps.dirs.refs, file), + note: entity.note, + source: picked.url, + // 「你给的」只指从文件选择器上传的图(页面会把它放进你的消息气泡),下载来的一律记成检索所得。 + origin: 'search', + use: entity.use || 'subject', + take: entity.take || '', + }); + byPath.set(picked.path, id); + return id; +} + // 参考图:检索 -> 分批下载到缓存 -> 判定相关性 -> 合格才复制进会话并登记成 R1、R2… // 存进会话而不是直接用缓存,是为了让生成端和评审端拿到同一份稳定素材。 async function materializeRefs(session, deps) { const queries = normalizeRefQueries(session.plan && session.plan.ref_queries); - const hits = queries.length ? await runRefSearch(session, queries, deps) : []; + // 规划写进 refs 的地址只认用户原话里出现过的,见 keepUserUrls。 + for (const image of session.images) image.spec.refs = keepUserUrls(session, image, image.spec.refs, deps); + const budget = makeRefBudget(normalizeRefBudget(session.plan && session.plan.ref_budget, queries.length)); + session.refBudget = budget; + if (queries.length && !budget.limit) { + // 规划自己说预算是 0:它写的检索项一律不搜。一般是规划写了项、又判断不值得花时间。 + budget.stopped = queries.map((item) => item.query); + budget.stoppedTasks = queries.map((item) => canonicalRef(item.task_name)); + deps.emit(session, 'think', '规划写了 ' + queries.length + ' 项检索,但检索预算是 0,这次不检索:' + queries.map((item) => item.query).join('、'), { level: 'warn' }); + } + const hits = queries.length && budget.limit ? await runRefSearch(session, queries, deps, { budget }) : []; const knownTasks = new Set(queries.map((item) => canonicalRef(item.task_name)).filter(Boolean)); // 降级判定要用规划时写的原始 refs,映射之后就看不出来了。 const plannedRefs = new Map(); for (const image of session.images) plannedRefs.set(image.id, (image.spec.refs || []).slice()); + // 预算不够、根本没搜的项不会生成实体,单独列出来。 + const unsearched = budget.stopped.map((query) => ({ tag: 'warn', text: query, note: '超出检索预算,没有检索' })); + for (const query of budget.stopped) { + session.questions.push('「' + query + '」超出了这次的检索预算,没有检索参考图,如果一致性要求高请手动上传。'); + } const entities = refEntities(session, hits, queries); if (!entities.length) { @@ -632,24 +2023,7 @@ async function materializeRefs(session, deps) { // 每轮会话最多清一次缓存,失败不影响主流程。 if (cache.prune) await cache.prune(); - // 并发 2:下载是网络等待、判定是一次模型调用,再高会顶到运行时的 agentConcurrency。 - const resolved = new Map(); - let cursor = 0; - const workers = Array.from({ length: Math.min(2, entities.length) }, async () => { - while (cursor < entities.length) { - const entity = entities[cursor]; - cursor += 1; - try { - const picked = await resolveEntityRef(session, entity, deps); - if (picked) resolved.set(entity.key, picked); - } catch (error) { - deps.emit(session, 'think', entity.note + ' 参考图处理异常:' + String(error && error.message ? error.message : error), { - level: 'warn', - }); - } - } - }); - await Promise.all(workers); + const resolved = await resolveEntities(session, entities, deps, budget); const byUrl = new Map(); const byTask = new Map(); @@ -659,32 +2033,30 @@ async function materializeRefs(session, deps) { for (const entity of entities) { const picked = resolved.get(entity.key); if (!picked) { - missed.push({ tag: 'fail', text: entity.note }); - deps.emit(session, 'think', entity.note + ':没找到符合要求的参考图', { level: 'warn' }); - session.questions.push('「' + entity.note + '」没找到符合要求的参考图,如果一致性要求高请手动上传。'); + // 预算用完没判完的,照实说是预算的原因,不说成「没找到」:用户手上有图的话上传最省事。 + const why = entity.outOfBudget ? '检索预算用完,没有判完' : '没找到符合要求的参考图'; + missed.push({ tag: entity.outOfBudget ? 'warn' : 'fail', text: entity.note, note: why }); + deps.emit(session, 'think', entity.note + ':' + why, { level: 'warn' }); + session.questions.push('「' + entity.note + '」' + (entity.outOfBudget ? '检索预算用完,没有找到参考图' : '没找到符合要求的参考图') + ',如果一致性要求高请手动上传。'); continue; } - let id = byPath.get(picked.path); - if (!id) { - id = 'R' + (session.refs.length + 1); - const file = id + (picked.file.endsWith('.png') ? '.png' : '.jpg'); - await copyFile(picked.path, join(deps.dirs.refs, file)); - session.refs.push({ id, file, path: join(deps.dirs.refs, file), note: entity.note, source: picked.url, origin: 'search' }); - byPath.set(picked.path, id); - } + const id = await registerPickedRef(session, deps, entity, picked, byPath); reasons.set(id, picked.reason || ''); byUrl.set(picked.url, id); + if (entity.direct) byUrl.set(entity.query, id); for (const name of entity.taskNames) if (name) byTask.set(name, id); } + missed.push(...unsearched); settleImageRefs(session, deps, plannedRefs, byUrl, byTask, knownTasks); session.refSheet = null; if (!session.refs.length) { - deps.emit(session, 'think', '没有可用的参考图,本轮全部按文生图走', { level: 'warn' }); + deps.emit(session, 'think', '没有可用的参考图,本轮全部按文生图走(' + refBudgetLine(budget) + ')', { level: 'warn' }); return; } - deps.emit(session, 'think', '参考图就绪:' + session.refs.map((ref) => ref.id).join('、'), { + deps.emit(session, 'think', '参考图就绪:' + session.refs.map((ref) => ref.id).join('、') + '(' + refBudgetLine(budget) + ')', { detail: detail('参考图', [ + kv('检索预算', refBudgetLine(budget)), thumbs( '最终选用(' + session.refs.length + ' 张)', session.refs.map((ref) => ({ @@ -843,6 +2215,15 @@ async function ensureRefSheet(session, deps) { } export async function runIncrementalEdit(session, instruction, deps) { + // 上一轮的中断原因不能带进这一轮:以前一轮失败之后,后面每一轮哪怕交付成功,会话也一直显示「失败」。 + session.fatal = ''; + // 参考图卡片上点了「用这张参考图生成 GIF」:直接出帧,不用编辑节点再理解一遍。 + const confirmed = confirmTarget(instruction); + const confirmedImage = confirmed ? session.images.find((item) => item.id === confirmed && isAnimation(item)) : null; + if (confirmedImage) { + await editAnimation(session, confirmedImage, { animation: { start: true }, promptGiven: false }, deps); + return; + } const target = await editPlanNode(session, instruction, deps); if (isCancelled(session)) return; if (!target) { @@ -857,12 +2238,128 @@ export async function runIncrementalEdit(session, instruction, deps) { await deliver(session, deps); return; } + if (isAnimation(image) && (await editAnimation(session, image, target, deps))) return; + if (!isAnimation(image) && (await editMotionGif(session, image, target, deps))) return; + if (!isAnimation(image) && (await editPanelGif(session, image, target, instruction, deps))) return; + const hadQr = ((image.spec && image.spec.qrCodes) || []).length > 0; + // 用户要把码去掉:清掉码的要求,H-qr 跟着摘掉,之后的编辑也不再追加二维码的画法要求。 + if (target.qrRemove) image.spec.qrCodes = []; + else if (target.qrCodes) { + image.spec.qrCodes = normalizeQrCodes(target.qrCodes, qrSources(session, [instruction])); + const unverified = image.spec.qrCodes.filter((item) => item.content && !item.verbatim); + if (unverified.length) { + deps.emit( + session, + 'edit', + image.id + ' 的二维码内容在你说过的话里找不到,不贴真码:' + unverified.map((item) => '「' + item.content + '」').join('、'), + { level: 'warn' }, + ); + } + } + // 图表:先把这次对图表的改动并进规格(数据、类型、位置)。放在改字之前:只改字时也会在底图上重画图表。 + const hadCharts = hasCharts(image); + if (target.charts) applyChartPatches(session, image, target.charts, instruction, deps); + // 文字:先把这次对文字的改动并进规格(内容、位置、书体)。 + if (target.textLayers) applyTextPatches(session, image, target.textLayers, deps); + // 只改字:程序排的字直接在不带字的底图上重新排,不重新生图;模型画的字先让模型擦掉,再由程序排。 + // 1.9.0 以前的会话没有 textOverlay,用户说「漏了 36 个字,补上」时编辑节点不一定写 text_layers:按已有的文字层补出来。 + if (target.textOnly && !image.spec.textOverlay && layerTexts(image.spec.textLayers).length) { + image.spec.textOverlay = normalizeTextOverlay(null, image.spec.textLayers, [session.instruction, image.title].filter(Boolean).join(' ')); + } + if (target.textOnly && overlayLayers(image).length) { + if (isOverlay(image)) { + const done = await replaceTextInPlace(session, image, deps); + if (isCancelled(session)) return; + if (!done) restoreLastDelivered(session, image, deps); + await deliver(session, deps); + return; + } + // 不知道字该放哪(规划没写排法、编辑节点也没给 box)的短字退回让模型照新内容改:按默认位置排,会在画面正中 + // 压一大块字。长文、竖排、书法不受这个限制:字本身就是画面的主体,默认位置就是载体中间的一大块。 + if (!canPlaceText(image)) { + if (!target.promptGiven) target.prompt = rewriteTextPrompt(image, target.textLayers); + } else { + const eraseFrom = + findCandidate(image, target.candidateId) || image.chosen || lastCandidate(image) || lastDelivered(session, image, deps); + enterOverlay(session, image, '用户要改模型画的字'); + deps.emit(session, 'edit', image.id + ' 模型画的字要改:先让模型把字擦掉,再由程序按原文排字', { level: 'info' }); + image.spec.checklist = withQrCheck(image.spec.checklist, image.spec.qrCodes); + image.roundBase = (image.roundBase || 0) + (image.rounds || []).length; + image.rounds = []; + image.status = 'running'; + image.chosen = null; + image.pendingAction = eraseFrom + ? { + type: 'edit', + prompt: eraseTextPrompt(image), + parentId: eraseFrom.id, + parentPath: eraseFrom.path, + parentStats: (eraseFrom.program && eraseFrom.program.stats) || null, + refs: [], + instruction, + } + : { type: 'generate', prompt: promptFor(image), refs: image.spec.refs, parentId: null }; + await runImage(session, image, deps); + if (isCancelled(session)) return; + await deliver(session, deps); + return; + } + } + // 只改图表:在不带图表的底图上按新的数据重画,不重新生图。底图上那块地方不空(原来没有图表、这次新加一张, + // 或者挪到了别处),退回让模型先在那里留出一块空白面板,再由程序画图表。 + if (target.chartOnly && (hadCharts || hasCharts(image))) { + const outcome = await replaceChartInPlace(session, image, deps); + if (isCancelled(session)) return; + if (outcome !== 'needs_panel') { + if (outcome !== 'done') restoreLastDelivered(session, image, deps); + await deliver(session, deps); + return; + } + if (!target.promptGiven) target.prompt = chartPanelPrompt(image); + deps.emit(session, 'edit', image.id + ' 底图上预留给图表的地方不空:先让模型在那里留出一块空白面板,再由程序画图表'); + } + // 只是补(或换)二维码内容:不重新生图,直接在上一版交付图上把码换成真码。 + // 图里原来没有码的不走这条路——那是「加一个码」,得让模型先画出来。 + if (target.qrOnly && hadQr && !target.qrRemove) { + image.spec.checklist = withQrCheck(image.spec.checklist, image.spec.qrCodes); + if (qrContents(image.spec).length) await replaceQrInPlace(session, image, deps); + else deps.emit(session, 'edit', image.id + ' 这次没有可以贴的二维码内容,原图保持不变', { level: 'warn' }); + if (isCancelled(session)) return; + await deliver(session, deps); + return; + } + // 参考图:编辑节点要求重新检索的,真的去搜、下载、筛选,找到的登记成新的 R 编号并用到这次修改里; + // 用户给的图片地址同样先下载校验。以前这一步完全没有:追问「参考图不对,重新找」时编辑节点只能凭记忆 + // 写一个网址,网址原样交给接口,接口取不到,同一个请求连败 6 轮(真机 20261112-8a442a)。 + const refPlan = await prepareEditRefs(session, image, target, instruction, deps); + if (isCancelled(session)) return; + if (target.refsOnly && refPlan.searched && !refPlan.added.length) { + // 只是要重新找参考图,却一张新的都没找到:照原样再画一遍只会得到同样的图,不浪费生图请求。 + deps.emit(session, 'edit', image.id + ' 重新检索没找到新的参考图,这次没有重新生成', { level: 'warn' }); + restoreLastDelivered(session, image, deps); + await deliver(session, deps); + return; + } image.spec.prompt = target.prompt || image.spec.prompt; image.spec.entry = 'edit'; - // 只带编辑节点点名的参考图。把上一版的 R1..R4 一起留下,模型会分不清哪张是要改的底图, + // 只带编辑节点点名的参考图(加上这次重新检索找到的)。把上一版的 R1..R4 一起留下,模型会分不清哪张是要改的底图, // 转而照着参考图重画一张全新的图——实测「加个标题」被画成了另一台车、另一个人、另一个背景。 - image.spec.refs = dedupe(target.refs || []); + image.spec.refs = refPlan.refs; if (target.checklist && target.checklist.length) image.spec.checklist = target.checklist; + // 编辑节点写的清单不带 H-qr;图里要放真码的,每一轮编辑都照样要验。 + image.spec.checklist = withQrCheck(image.spec.checklist, image.spec.qrCodes); + // 程序排字的图同理:编辑节点写的清单不带 H-text、H-notext。 + if (isOverlay(image)) image.spec.checklist = overlayChecklist(image.spec.checklist, image.spec.textLayers); + // 程序画图表的图也一样:编辑节点写的清单不带 H-chart、H-nochart。 + image.spec.checklist = chartChecklist(image.spec.checklist, imageCharts(image)); + image.spec.checklist = withWhiteCheck(image.spec.checklist, wantsWhite(session, image)); + // 这次明确要改颜色、光线:上一版的颜色不再是标准,不拿它当色彩基准,改完以后重新选。 + if (image.colorAnchor && COLOR_CHANGE_RE.test(String(instruction || ''))) { + image.colorAnchor = null; + deps.emit(session, 'edit', image.id + ' 这次要改颜色或光线,不拿上一版当色彩基准'); + } + // 组图 GIF 补过过渡格的,改完组图要按新的组图重补(见下面 runImage 之后)。 + const hadTweens = Boolean(image.panelTweens && image.panelTweens.pairs.some((pair) => pair.status === 'passed' || pair.status === 'unreviewed')); // 父图必须在清空轮次之前解析出来。清空之后 findCandidate 就找不到上一版的候选了, // 编辑请求会不带原图,而轨迹上照样写着「在 A-r1-g1 上编辑」。 const parent = @@ -880,7 +2377,8 @@ export async function runIncrementalEdit(session, instruction, deps) { type: 'edit', prompt: target.prompt, parentId: parent ? parent.id : null, - parentPath: parent ? parent.path : null, + // 程序排字的图要在不带字的底图上改,字改完再重新排。 + parentPath: parent ? parentImagePath(image, parent) : null, // 轮次马上要被清空,父图对象之后再也取不到,统计必须现在抄一份带下去。 // 少了这一行,编辑轮的 parentStats 恒为 null,漂移判定会退回绝对模式。 parentStats: (parent && parent.program && parent.program.stats) || null, @@ -892,18 +2390,721 @@ export async function runIncrementalEdit(session, instruction, deps) { } await runImage(session, image, deps); if (isCancelled(session)) return; + // 要拼成 GIF 的组图改过了:原来补过过渡格的,按新的组图重新补。 + if (image.spec.panelGif && !isAnimation(image) && image.chosen && hadTweens) { + await ensurePanelTweens(session, image, deps, tweenHooks(), { mode: 'make' }); + if (isCancelled(session)) return; + } await deliver(session, deps); } +// 这个会话做过的参考图检索:规划时的 ref_queries,加上之后追问里重新检索的。 +function refHistoryQueries(session) { + return [...normalizeRefQueries(session.plan && session.plan.ref_queries), ...(session.editRefQueries || [])]; +} + +// 每个检索项的结果(找到哪张 R,或没找到),给编辑节点看。同一个 task_name 检索过几次都算: +// 首轮找到了、追问里重搜没找到的,它仍然有那张参考图;被用户点名替换掉的图不再算数。 +function refHistory(session) { + const byTask = new Map(); + for (const query of refHistoryQueries(session)) { + const entry = byTask.get(query.task_name) || { task_name: query.task_name, query: query.query, notes: [] }; + entry.query = query.query; + entry.notes.push(query.query); + byTask.set(query.task_name, entry); + } + const replaced = new Set((session.editRefQueries || []).map((query) => query.replaces).filter(Boolean)); + return [...byTask.values()].map((entry) => { + const ref = session.refs + .slice() + .reverse() + .find((item) => item.origin !== 'user' && !replaced.has(item.id) && entry.notes.indexOf(item.note) >= 0); + return { task_name: entry.task_name, query: entry.query, ref: ref ? ref.id : '' }; + }); +} + +// 编辑这一轮要用的参考图。返回 { refs, added, searched }:refs 是交给生图的 R 编号; +// added 是这次新登记的(重新检索或用户地址下载来的);searched 表示这次确实发起了检索。 +async function prepareEditRefs(session, image, target, instruction, deps) { + const queries = target.refQueries || []; + const named = keepUserUrls( + session, + image, + dedupe((target.refs || []).filter((ref) => typeof ref === 'string' && ref.trim()).map((ref) => ref.trim())), + deps, + [instruction], + ); + const urls = named.filter((ref) => /^https?:\/\//i.test(ref)); + const byTask = new Map(); + for (const item of refHistory(session)) if (item.ref) byTask.set(item.task_name, item.ref); + const result = { refs: [], added: [], searched: queries.length > 0 }; + // 用户点名说不对的旧参考图:新的检索排除它,这次修改也不再带它。 + const replaced = new Set(queries.map((item) => item.replaces).filter(Boolean)); + const byUrl = new Map(); + + if (queries.length || urls.length) { + const exclude = new Set(session.refs.filter((ref) => replaced.has(ref.id) && ref.source).map((ref) => ref.source)); + // 追问里的检索同样按编辑节点给的预算走(没写就用默认值)。 + const budget = makeRefBudget(normalizeRefBudget(target.refBudget, queries.length)); + if (queries.length && !budget.limit) { + budget.stopped = queries.map((item) => item.query); + deps.emit(session, 'think', '编辑节点写了 ' + queries.length + ' 项检索,但检索预算是 0,这次不检索', { level: 'warn' }); + } + const hits = queries.length && budget.limit ? await runRefSearch(session, queries, deps, { ask: false, budget }) : []; + const entities = refEntities(session, hits, queries, { + directUrls: urls, + exclude, + keyPrefix: 't' + (session.turns || []).length + 'q', + }); + const resolved = entities.length ? await resolveEntities(session, entities, deps, budget) : new Map(); + result.budget = budget; + const before = new Set(session.refs.map((ref) => ref.id)); + const byPath = new Map(); + const found = []; + const missed = []; + const missedBudget = new Set(budget.stopped); + for (const entity of entities) { + const picked = resolved.get(entity.key); + if (!picked) { + if (entity.outOfBudget) missedBudget.add(entity.note); + else missed.push(entity.note); + deps.emit(session, 'think', entity.note + ':' + (entity.outOfBudget ? '检索预算用完,没有判完' : '没找到符合要求的参考图'), { level: 'warn' }); + continue; + } + const id = await registerPickedRef(session, deps, entity, picked, byPath); + if (entity.direct) byUrl.set(entity.query, id); + for (const name of entity.taskNames) if (name) byTask.set(name, id); + if (!before.has(id) && result.added.indexOf(id) < 0) result.added.push(id); + found.push({ id, entity }); + } + // 一张候选都没搜回来的检索项不会生成实体,也要算「没找到」(预算不够没搜的单独算)。 + const foundTasks = new Set(found.flatMap((item) => item.entity.taskNames)); + for (const query of queries) { + const listed = entities.some((entity) => entity.taskNames.indexOf(query.task_name) >= 0); + if (!listed && !foundTasks.has(query.task_name) && !missedBudget.has(query.query)) missed.push(query.query); + } + // 检索过的项记下来:下次追问时编辑节点能看到哪些找到了、哪些没找到。 + session.editRefQueries = [...(session.editRefQueries || []), ...queries].slice(-12); + // 之前「没找到」的提问,这次找到了就撤掉,免得交付里还挂着一句过期的「请手动上传」。 + const resolvedTasks = new Set(found.flatMap((item) => item.entity.taskNames)); + const staleNotes = refHistoryQueries(session) + .filter((query) => resolvedTasks.has(query.task_name)) + .map((query) => '「' + query.query + '」没找到'); + session.questions = (session.questions || []).filter((question) => !staleNotes.some((note) => question.includes(note))); + for (const note of missed) { + const question = '「' + note + '」重新检索后仍没找到符合要求的参考图,可以在面板里手动上传一张。'; + if (session.questions.indexOf(question) < 0) session.questions.push(question); + } + for (const note of missedBudget) { + const question = '「' + note + '」超出了这次的检索预算,没有找到参考图,可以在面板里手动上传一张。'; + if (session.questions.indexOf(question) < 0) session.questions.push(question); + } + if (found.length || missed.length) { + deps.emit(session, 'think', '重新找参考图:' + (found.length ? '找到 ' + found.map((item) => item.id).join('、') : '一张都没找到'), { + level: found.length ? 'info' : 'warn', + detail: detail('追问里的参考图', [ + thumbs( + '这次用上的(' + found.length + ' 张)', + found.map((item) => { + const ref = session.refs.find((one) => one.id === item.id) || {}; + return { key: 'refs/' + ref.file, title: item.id + ' · ' + item.entity.note, note: ref.source || '', tag: 'pass', text: '' }; + }), + ), + missed.length ? list('没找到的', missed.map((note) => ({ tag: 'fail', text: note }))) : null, + replaced.size ? kv('不再使用', [...replaced].join('、')) : null, + ]), + }); + } + } + + const refs = []; + for (const ref of named) { + if (/^https?:\/\//i.test(ref)) { + // 下载不了的地址上面已经说过「没找到」,这里直接跳过。 + const id = byUrl.get(ref); + if (id) refs.push(id); + continue; + } + if (/^R\d+$/i.test(ref)) { + const id = ref.toUpperCase(); + if (session.refs.some((item) => item.id === id)) refs.push(id); + else deps.emit(session, 'edit', image.id + ' 的编辑引用了不存在的参考图 ' + id + ',已忽略', { level: 'warn' }); + continue; + } + let id = byTask.get(ref); + if (!id) { + for (const [name, value] of byTask) if (canonicalRef(name) === canonicalRef(ref)) id = value; + } + if (id) refs.push(id); + else if (!queries.some((query) => canonicalRef(query.task_name) === canonicalRef(ref))) { + deps.emit(session, 'edit', image.id + ' 的参考图标识「' + String(ref).slice(0, 40) + '」没对上任何参考图,已忽略', { level: 'warn' }); + } + } + // 这次重新检索找到的图一定带上:编辑节点漏写了 task_name,重新找图就白找了。 + for (const id of result.added) if (refs.indexOf(id) < 0) refs.push(id); + result.refs = dedupe(refs).filter((id) => !replaced.has(id)); + return result; +} + +// 这一轮没有重新生成时,把上一版交付的图接回来再交付一次(上一轮可能失败过,image.chosen 已经清空)。 +function restoreLastDelivered(session, image, deps) { + if (image.chosen) return; + const last = lastDelivered(session, image, deps); + if (!last) return; + image.chosen = last; + image.status = last.status === 'passed' ? 'passed' : 'unpassed'; + image.chosenReason = '这一轮没有重新生成,沿用上一版交付'; + // 中途停下的说明跟着那张图走:上一轮失败留下的原因和这张图无关。 + image.interrupted = last.interrupted || ''; +} + +// 在上一版交付图上把二维码换成真码,不重新生图(用户在同一会话里补了一句码的内容)。 +// 换成了:起一个新轮次、只含这一张候选,评审结论沿用底图的、把 H-qr 记成通过——画面别的地方一个像素都没动, +// 没理由再花几分钟重新评审。没换成:原图和轮次都不动,原因记在 image.qrAttempt 上,交付时告诉用户。 +async function replaceQrInPlace(session, image, deps) { + const contents = qrContents(image.spec); + const base = image.chosen || lastCandidate(image) || lastDelivered(session, image, deps); + image.qrAttempt = null; + if (!base || !base.path) { + image.qrAttempt = { contents, reasonText: '找不到上一版交付的图' }; + deps.emit(session, 'edit', image.id + ' 找不到上一版交付的图,没法直接换码', { level: 'warn' }); + return false; + } + const startedAt = Date.now(); + deps.emit(session, 'edit', image.id + ' 只换二维码:直接在 ' + (base.id || '上一版交付图') + ' 上换成真码,不重新生图', { + detail: detail('只换二维码', [ + kv('底图', base.id || '上一版交付图'), + list('要放的内容', contents.map((content) => ({ text: content }))), + ]), + }); + let buffer; + // 图表、文字是程序画上去的:从画之前的底图开始换码,换完再把图表、文字重新画上去。 + // 从交付图开始的话,图表要画在已有的图表上,那块地方不空,图表就画不上了。 + const from = (hasCharts(image) || isOverlay(image)) && base.basePath ? base.basePath : base.path; + try { + buffer = await readFile(from); + } catch (error) { + image.qrAttempt = { contents, reasonText: '读不到上一版交付的图:' + describeError(error) }; + deps.emit(session, 'edit', image.id + ' ' + image.qrAttempt.reasonText, { level: 'error' }); + return false; + } + const roundBase = (image.roundBase || 0) + (image.rounds || []).length; + const id = image.id + '-r' + (roundBase + 1) + '-g0'; + const candidate = { + id, + path: from, + file: id + extensionFor(buffer), + size: base.size, + requestId: base.requestId, + revisedPrompt: base.revisedPrompt, + parentId: base.id || null, + parentPath: from, + parentStats: (base.program && base.program.stats) || null, + actionType: 'qr', + actionPrompt: base.actionPrompt || image.spec.prompt, + round: 0, + createdAt: Date.now(), + buffer, + program: {}, + checks: [], + }; + await programChecks(session, image, candidate, deps); + const qr = candidate.program && candidate.program.qr; + if (!qr || qr.status !== 'real') { + const why = qr ? qrFailText(qr) : (candidate.program && candidate.program.error) || '未知原因'; + image.qrAttempt = { contents, reasonText: '在原图上换码没成功:' + why }; + deps.emit(session, 'edit', image.id + ' 二维码没换成:' + why + '。原图保持不变', { level: 'warn', durMs: Date.now() - startedAt }); + return false; + } + // 原图里本来就是这枚码(用户把同一个内容又说了一遍):没有生成新文件,复制一份, + // 让候选的 file 在 images/ 下真实存在(重启后靠它找回这张图)。 + if (candidate.path === from) { + const copyPath = join(deps.dirs.images, candidate.file); + await writeFile(copyPath, buffer); + candidate.path = copyPath; + } + // 贴码前的原图是底图那张(可能在 deliver/vN/ 下),不是这个候选名下的文件——那个名字从没写过盘。 + candidate.originalFile = base.file && from === join(deps.dirs.images, base.file) ? base.file : null; + candidate.originalPath = from; + candidate.verdict = carryVerdict(base.verdict, qr); + const previous = image.status; + image.roundBase = roundBase; + image.rounds = [ + { + index: 0, + action: { type: 'qr', prompt: '', parentId: base.id || null, refs: [] }, + candidates: [candidate], + verdict: { note: '在原图上换成真码,扫码实测通过' }, + startedAt, + }, + ]; + image.chosen = candidate; + image.status = candidate.verdict + ? candidate.verdict.hardTotal > 0 && !candidate.verdict.hardFailed.length + ? 'passed' + : 'unpassed' + : previous === 'passed' + ? 'passed' + : 'unpassed'; + image.chosenReason = '在上一版交付图上把二维码换成真码,没有重新生图;扫码实测内容为「' + contents.join('」「') + '」'; + deps.emit(session, 'edit', image.id + ' 二维码已在原图上换成真码,扫码实测通过', { durMs: Date.now() - startedAt }); + return true; +} + +// 只换码时沿用底图的评审结论,把 H-qr 记成通过(没有这一项的补上,计入 hardTotal)。 +function carryVerdict(verdict, qr) { + if (!verdict) return null; + const copy = JSON.parse(JSON.stringify(verdict)); + const had = copy.checks && copy.checks['H-qr']; + copy.checks = { ...(copy.checks || {}), 'H-qr': { result: 'pass', note: '扫码实测:' + qr.items.map((item) => item.content).join('、') } }; + copy.hardFailed = (copy.hardFailed || []).filter((id) => id !== 'H-qr'); + if (!had) { + copy.hardTotal = (copy.hardTotal || 0) + 1; + copy.hardPassed = (copy.hardPassed || 0) + 1; + } else if (had.result === 'fail') { + copy.hardPassed = (copy.hardPassed || 0) + 1; + } + return copy; +} + +// 编辑节点写的文字改动(text_layers):按 index 改已有的段落,没有 index 的是新加的一段,remove 是删掉。 +// 改完同步 text_layers 和 T 项;程序排字模式下底图 prompt 里的留白位置也跟着变。 +const TEXT_PATCH_FIELDS = ['box', 'direction', 'font', 'color', 'align', 'punctuation', 'surface', 'role', 'indent', 'rule', 'part']; + +function applyTextPatches(session, image, patches, deps) { + // 1.9.0 以前的会话没有 textOverlay:先按已有的 text_layers 补出来,否则按 index 改字体这类改动找不到段落, + // 文字层会被整个清空。 + const overlay = + image.spec.textOverlay || + normalizeTextOverlay(null, image.spec.textLayers, [session.instruction, image.title].filter(Boolean).join(' ')) || + { use: false, base: {}, layers: [], planned: false }; + const layers = overlay.layers.slice(); + const removed = new Set(); + for (const patch of patches || []) { + if (!patch || typeof patch !== 'object') continue; + const index = Number.isInteger(patch.index) ? patch.index : -1; + const newText = typeof patch.text === 'string' ? patch.text.trim() : ''; + if (index >= 0 && index < layers.length) { + if (patch.remove === true) { + removed.add(index); + continue; + } + const merged = { ...layers[index] }; + for (const key of TEXT_PATCH_FIELDS) if (patch[key] !== undefined && patch[key] !== null && patch[key] !== '') merged[key] = patch[key]; + layers[index] = normalizeTextLayer(merged, newText || layers[index].text); + } else if (newText) { + layers.push(normalizeTextLayer(patch, newText)); + } + } + const next = layers.filter((_, index) => !removed.has(index)).slice(0, MAX_TEXT_LAYERS); + const placed = (patches || []).some((patch) => patch && Array.isArray(patch.box)); + image.spec.textOverlay = { ...overlay, layers: next, planned: Boolean(overlay.planned || placed) }; + image.spec.textLayers = next.map((layer) => layer.text); + const rest = (image.spec.checklist || []).filter((item) => !/^T\d+$/.test(item.id)); + image.spec.checklist = textChecks(image.spec.textLayers).concat(rest); + if (isOverlay(image)) { + image.spec.basePrompt = composeBasePrompt(image.spec, session.instruction); + image.spec.checklist = overlayChecklist(image.spec.checklist, image.spec.textLayers); + } + // 文字层改过了,按版面读一遍的结果也要跟着更新:评审看到的提醒不能是上一版的。 + image.spec.readingProblems = readingProblems(session, [image]); + deps.emit(session, 'edit', image.id + ' 文字改成:' + next.map((layer) => '「' + shortText(layer.text, 20) + '」').join('、'), { + detail: detail('文字改动 · ' + image.id, [ + ...next.map((layer, index) => + group('第 ' + index + ' 段', [ + kv('内容', shortText(layer.text, 60)), + kv('位置', describeBox(layer.box)), + kv('排法', (layer.direction === 'vertical' ? '竖排' : '横排') + ' · ' + FONT_KINDS[layer.font].label + (layer.color ? ' · ' + layer.color : '')), + ]), + ), + ]), + }); +} + +// 规划没写字放在哪、编辑节点也没给 box 时,改字只能交给模型:编辑指令里写清改成什么、要换的样子。 +function rewriteTextPrompt(image, patches) { + const asks = []; + for (const patch of patches || []) { + if (!patch || typeof patch !== 'object') continue; + if (patch.font) asks.push('字体改成' + FONT_KINDS[normalizeFontKind(patch.font)].label); + if (patch.direction === 'vertical') asks.push('改成竖排'); + if (patch.direction === 'horizontal') asks.push('改成横排'); + if (patch.color) asks.push('文字颜色改成 ' + patch.color); + } + const texts = layerTexts(image.spec.textLayers); + return ( + '把画面里的文字改成' + + texts.map((value) => '「' + value + '」').join('、') + + ',逐字照写、字形正确、清晰可读' + + (asks.length ? ',' + dedupe(asks).join(',') : ',字体风格与原来一致') + + ',位置和大小与原来一致;除文字以外画面的一切保持完全不变。' + ); +} + +// 让模型把它自己画的字擦掉:原来写字的载体还原成干净的表面,别的都不动。擦完由程序排字。 +function eraseTextPrompt(image) { + const surfaces = dedupe(overlayLayers(image).map((layer) => layer.surface)).join('、'); + return ( + '把画面里所有的文字、字母、数字、标点、笔迹和印章都擦掉,原来写字的地方还原成干净、空白的' + + (surfaces || '表面') + + ',纹理和明暗与周围一致;除此之外画面的一切(构图、光线、颜色、物体、背景)保持完全不变。' + ); +} + +// 在不带字的底图上按新的文字要求重新排字,不重新生图。成了:起一个只含这一张候选的新轮次,评审结论沿用上一版的, +// 文字相关的几项记成通过(画面别的地方一个像素都没动)。没成:原图和轮次都不动,原因记在 image.textAttempt 上。 +async function replaceTextInPlace(session, image, deps) { + const base = image.chosen || lastCandidate(image) || lastDelivered(session, image, deps); + image.textAttempt = null; + const basePath = base && base.basePath; + if (!basePath) { + image.textAttempt = { reasonText: '找不到上一版不带字的底图' }; + deps.emit(session, 'edit', image.id + ' 找不到上一版不带字的底图,没法直接改字', { level: 'warn' }); + session.questions.push(image.id + ' 找不到不带字的底图,这次没有改字。可以回复「重画」重新生成这张图'); + return false; + } + const startedAt = Date.now(); + deps.emit(session, 'edit', image.id + ' 只改文字:在不带字的底图上重新排字,不重新生图'); + let buffer; + try { + buffer = await readFile(basePath); + } catch (error) { + image.textAttempt = { reasonText: '读不到不带字的底图:' + describeError(error) }; + deps.emit(session, 'edit', image.id + ' ' + image.textAttempt.reasonText, { level: 'error' }); + session.questions.push(image.id + ' ' + image.textAttempt.reasonText + ',这次没有改字'); + return false; + } + const roundBase = (image.roundBase || 0) + (image.rounds || []).length; + const id = image.id + '-r' + (roundBase + 1) + '-g0'; + const candidate = { + id, + path: basePath, + file: basePath.split(/[\\/]/).pop(), + size: base.size, + requestId: base.requestId, + revisedPrompt: base.revisedPrompt, + parentId: base.id || null, + parentPath: basePath, + parentStats: (base.program && base.program.stats) || null, + actionType: 'text', + actionPrompt: base.actionPrompt || promptFor(image), + round: 0, + createdAt: Date.now(), + buffer, + program: {}, + checks: [], + }; + await programChecks(session, image, candidate, deps); + const typed = candidate.program && candidate.program.text; + if (!typed || typed.status !== 'overlay') { + const why = typed ? textFailText(typed) : (candidate.program && candidate.program.error) || '未知原因'; + image.textAttempt = { reasonText: why }; + deps.emit(session, 'edit', image.id + ' 字没排上:' + why + '。图保持上一版', { level: 'warn', durMs: Date.now() - startedAt }); + session.questions.push(image.id + ' 按新的要求排不下这段字(' + why + '),图保持上一版。可以把字改短,或者说「字小一点」再试'); + return false; + } + // 底图上的图表也是重新画的:图表没画上就不能交,否则交出去的图少了图表。 + const charted = candidate.program && candidate.program.chart; + if (hasCharts(image) && (!charted || charted.status !== 'overlay')) { + const why = charted ? chartFailText(charted) : '未知原因'; + image.textAttempt = { reasonText: '图表没画上:' + why }; + deps.emit(session, 'edit', image.id + ' 重新排字时图表没画上:' + why + '。图保持上一版', { level: 'warn', durMs: Date.now() - startedAt }); + session.questions.push(image.id + ' 重新排字时图表没画上(' + why + '),图保持上一版'); + return false; + } + candidate.verdict = carryProgramVerdict(base.verdict, image, candidate); + const previous = image.status; + image.roundBase = roundBase; + image.rounds = [ + { + index: 0, + action: { type: 'text', prompt: '', parentId: base.id || null, refs: [], overlay: true }, + candidates: [candidate], + verdict: { note: '在不带字的底图上重新排字' }, + startedAt, + }, + ]; + image.chosen = candidate; + image.status = candidate.verdict + ? candidate.verdict.hardTotal > 0 && !candidate.verdict.hardFailed.length + ? 'passed' + : 'unpassed' + : previous === 'passed' + ? 'passed' + : 'unpassed'; + image.chosenReason = '在不带字的底图上按新的要求重新排字,没有重新生图'; + deps.emit(session, 'edit', image.id + ' 文字已在原图上重新排好', { durMs: Date.now() - startedAt }); + return true; +} + +// 只改图表:在不带图表的底图上按新的数据重画(排过的字、贴过的码照样再来一遍),不重新生图。 +// 返回 'done'(画好了)、'needs_panel'(底图上那块地方不空,得让模型先留出面板)或 'failed'(图保持上一版)。 +async function replaceChartInPlace(session, image, deps) { + const base = image.chosen || lastCandidate(image) || lastDelivered(session, image, deps); + // 原来有程序画上去的东西(图表、排的字)时底图在 basePath;没有的话交付的那张本身就是干净的底图。 + const basePath = base && (base.basePath || base.path); + if (!basePath) { + deps.emit(session, 'edit', image.id + ' 找不到上一版的底图,没法直接改图表', { level: 'warn' }); + session.questions.push(image.id + ' 找不到上一版的底图,这次没有改图表。可以回复「重画」重新生成这张图'); + return 'failed'; + } + const startedAt = Date.now(); + deps.emit(session, 'edit', image.id + ' 只改图表:在不带图表的底图上按新的数据重画,不重新生图'); + let buffer; + try { + buffer = await readFile(basePath); + } catch (error) { + const why = '读不到上一版的底图:' + describeError(error); + deps.emit(session, 'edit', image.id + ' ' + why, { level: 'error' }); + session.questions.push(image.id + ' ' + why + ',这次没有改图表'); + return 'failed'; + } + const roundBase = (image.roundBase || 0) + (image.rounds || []).length; + const candidate = { + id: image.id + '-r' + (roundBase + 1) + '-g0', + path: basePath, + file: basePath.split(/[\\/]/).pop(), + size: base.size, + requestId: base.requestId, + revisedPrompt: base.revisedPrompt, + parentId: base.id || null, + parentPath: basePath, + parentStats: (base.program && base.program.stats) || null, + actionType: 'chart', + actionPrompt: base.actionPrompt || promptFor(image), + round: 0, + createdAt: Date.now(), + buffer, + program: {}, + checks: [], + }; + await programChecks(session, image, candidate, deps); + const charted = candidate.program && candidate.program.chart; + if (hasCharts(image) && (!charted || charted.status !== 'overlay')) { + const why = charted ? chartFailText(charted) : (candidate.program && candidate.program.error) || '未知原因'; + const blank = Boolean(charted) && (charted.items || []).some((item) => ['not_blank', 'marks'].includes(item.reason)); + deps.emit(session, 'edit', image.id + ' 图表没画上:' + why + (blank ? '' : '。图保持上一版'), { level: 'warn', durMs: Date.now() - startedAt }); + if (blank) return 'needs_panel'; + session.questions.push(image.id + ' 按新的要求画不下这张图表(' + why + '),图保持上一版。可以把图表放大一点,或者少几个类目再试'); + return 'failed'; + } + const typed = candidate.program && candidate.program.text; + if (isOverlay(image) && (!typed || typed.status !== 'overlay')) { + const why = typed ? textFailText(typed) : '未知原因'; + deps.emit(session, 'edit', image.id + ' 重画图表时字没排上:' + why + '。图保持上一版', { level: 'warn', durMs: Date.now() - startedAt }); + session.questions.push(image.id + ' 重画图表时字没排上(' + why + '),图保持上一版'); + return 'failed'; + } + candidate.verdict = carryProgramVerdict(base.verdict, image, candidate); + const previous = image.status; + image.roundBase = roundBase; + image.rounds = [ + { + index: 0, + action: { type: 'chart', prompt: '', parentId: base.id || null, refs: [], overlay: isOverlay(image) }, + candidates: [candidate], + verdict: { note: '在不带图表的底图上按新的数据重画图表' }, + startedAt, + }, + ]; + image.chosen = candidate; + image.status = candidate.verdict + ? candidate.verdict.hardTotal > 0 && !candidate.verdict.hardFailed.length + ? 'passed' + : 'unpassed' + : previous === 'passed' + ? 'passed' + : 'unpassed'; + image.chosenReason = hasCharts(image) ? '在不带图表的底图上按新的数据重画图表,没有重新生图' : '去掉了程序画的图表,没有重新生图'; + deps.emit(session, 'edit', image.id + (hasCharts(image) ? ' 图表已按新的数据重画好' : ' 图表已去掉'), { durMs: Date.now() - startedAt }); + return 'done'; +} + +// 编辑节点写的图表改动并进规格:{index, 要改的字段} 改已有的那张,{index, remove:true} 删掉,不写 index 是新加一张。 +function applyChartPatches(session, image, patches, instruction, deps) { + const charts = imageCharts(image).slice(); + const changes = []; + const problems = []; + for (const patch of patches) { + if (!patch || typeof patch !== 'object') continue; + const rawIndex = patch.index; + const index = rawIndex === undefined || rawIndex === null || rawIndex === '' ? null : Number(rawIndex); + if (index !== null) { + if (!Number.isInteger(index) || index < 0 || index >= charts.length || !charts[index]) { + problems.push('没有第 ' + (Number.isFinite(index) ? index + 1 : rawIndex) + ' 张图表'); + continue; + } + if (patch.remove === true) { + charts[index] = null; + changes.push('去掉第 ' + (index + 1) + ' 张图表'); + continue; + } + // 小数位按新数据重算(不带上一版算出来的);规划写的下限值 yMin 让给补丁里的 y_min。 + const previous = { ...charts[index] }; + delete previous.check; + delete previous.decimals; + const update = {}; + for (const key of CHART_PATCH_FIELDS) if (patch[key] !== undefined) update[key] = patch[key]; + if ('y_min' in update) delete previous.yMin; + if ('y_max' in update) delete previous.yMax; + const { chart, problem } = normalizeChart({ ...previous, ...update }); + if (!chart) { + problems.push('第 ' + (index + 1) + ' 张:' + problem); + continue; + } + charts[index] = chart; + changes.push('改第 ' + (index + 1) + ' 张图表'); + continue; + } + if (patch.remove === true) continue; + if (charts.filter(Boolean).length >= MAX_CHARTS) { + problems.push('一张图最多放 ' + MAX_CHARTS + ' 张图表'); + continue; + } + const { chart, problem } = normalizeChart(patch); + if (!chart) { + problems.push('新加的图表:' + problem); + continue; + } + charts.push(chart); + changes.push('加一张' + (CHART_TYPES[chart.type] || '图表')); + } + image.spec.charts = chartsWithCheck(charts.filter(Boolean), qrSources(session, [instruction])); + image.spec.checklist = chartChecklist(image.spec.checklist, image.spec.charts); + if (changes.length) deps.emit(session, 'edit', image.id + ' 调整图表:' + changes.join(',')); + for (const problem of problems) deps.emit(session, 'edit', image.id + ' 这处图表改动没法做:' + problem, { level: 'warn' }); + const unverified = image.spec.charts.filter((chart) => chart.check && !chart.check.ok); + if (unverified.length) { + deps.emit(session, 'edit', image.id + ' 的图表里有数在你说过的话里找不到:' + unverified.map((chart) => chart.check.missing.join('、')).join(';') + ',照画,交付时请核对', { level: 'warn' }); + } +} + +// 底图上预留给图表的地方不空时,给模型的编辑指令:只在那里腾出一块空白面板,别的都不动。 +function chartPanelPrompt(image) { + const parts = imageCharts(image).map((chart) => describeBox(chart.box || CHART_DEFAULT_BOX) + '留出一块' + (chart.surface || '平整的浅色面板')); + return '在' + parts.join(',在') + ':这块地方完全空白、平整干净,原来在那里的东西挪开或去掉;画面其他地方保持不变。'; +} + // ---------------------------------------------------------------- 01/02/03 规划 +// ---------------------------------------------------------------- 规划核对:按版面读出来的文字 +// +// 程序排字只核对每一层的字排上了。规划要是把「清」拆成「氵」「青」两层并排摆,每一层都「排对了」, +// 版面上却没有「清」(真机 20261117-f57a0f,评审还判了通过)。所以规划完先把文字层按位置从上到下、 +// 从左到右读一遍,和用户原话里引号中的文字对照。只查「字都在、却读不出来」的那种:引号里的字 +// 有的根本不在文字层里,多半是用户在描述别的东西(「像“清明上河图”那样」),不是要排的字。 +const QUOTE_RE = /[“"「『]([^”"」』\n]{1,60})[”"」』]/g; +const HAN = /[\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF]/; + +// 读的时候不管空格和全角半角:「+ 青 =」和「+ 青 =」是同一串。 +function readingText(value) { + return String(value || '').normalize('NFKC').replace(/\s+/g, ''); +} + +function quotedTexts(instruction) { + const out = []; + for (const match of String(instruction || '').matchAll(QUOTE_RE)) { + const value = readingText(match[1]); + if (Array.from(value).length >= 2 && HAN.test(value) && !out.includes(value)) out.push(value); + } + return out; +} + +// 文字层按版面读出来的几行:中心落在同一条横带里的算一行,行内从左到右。有竖排的层就只按层读。 +function readingRows(layers) { + const items = (layers || []).filter((layer) => layer && layer.text && Array.isArray(layer.box) && layer.box.length === 4); + if (items.some((layer) => layer.direction === 'vertical')) return items.map((layer) => readingText(layer.text)); + const rows = []; + for (const layer of items.slice().sort((a, b) => a.box[1] + a.box[3] - (b.box[1] + b.box[3]))) { + const center = (layer.box[1] + layer.box[3]) / 2; + const row = rows.find((item) => center >= item.y0 && center <= item.y1); + if (row) { + row.items.push(layer); + row.y0 = Math.min(row.y0, layer.box[1]); + row.y1 = Math.max(row.y1, layer.box[3]); + } else { + rows.push({ y0: layer.box[1], y1: layer.box[3], items: [layer] }); + } + } + return rows.map((row) => row.items.sort((a, b) => a.box[0] - b.box[0]).map((layer) => readingText(layer.text)).join('')); +} + +function readingProblems(session, images) { + const quotes = quotedTexts(session.instruction); + if (!quotes.length) return []; + const out = []; + for (const image of images || []) { + const layers = (image.spec && image.spec.textOverlay && image.spec.textOverlay.layers) || []; + if (layers.length < 2) continue; + const rows = readingRows(layers); + const all = rows.join(''); + const present = new Set(Array.from(all)); + for (const quote of quotes) { + const glyphs = Array.from(quote).filter((ch) => HAN.test(ch)); + if (!glyphs.every((ch) => present.has(ch))) continue; + if (all.includes(quote)) continue; + out.push({ imageId: image.id, quote, rows }); + } + } + return out; +} + +function readingDetail(problems) { + return [ + list('读不出来的', problems.map((item) => ({ tag: 'fail', text: item.imageId + ':「' + item.quote + '」' }))), + text('按版面读出来是', [...new Set(problems.map((item) => item.imageId))].map((id) => id + ':' + problems.find((item) => item.imageId === id).rows.join(' / ')).join('\n')), + ]; +} + +function readingFeedback(problems, data) { + const ids = [...new Set(problems.map((item) => item.imageId))]; + return [ + '程序核对(必须改正):你上一次输出的文字层,按版面位置从上到下、从左到右读出来是:', + ...ids.map((id) => '- 图 ' + id + ':' + problems.find((item) => item.imageId === id).rows.map((row) => '「' + row + '」').join(' ')), + '用户原话里要排出来的这几处读不出来:' + problems.map((item) => '「' + item.quote + '」').join('、') + '。', + '常见原因:把一个字拆成了偏旁和部件两层并排摆(一个字要两种颜色时写成完整的字,用 part,见 4d);', + '几层的位置排错了行或左右颠倒;漏了某一层。改正文字层后重新输出完整的 JSON,别的部分可以照抄上一次的输出。', + '上一次的输出:', + JSON.stringify(data), + ].join('\n'); +} + +// 本机各书体实际用哪个字体,一句话写给规划:「kai → 本机没有楷体,用马善政毛笔楷书代替」。 +async function fontFactsLine() { + let kinds = []; + try { + kinds = await describeFontKinds(); + } catch { + return '没查到'; + } + const parts = kinds.map((item) => { + if (!item.used) return item.kind + ' → 本机没有' + item.label + '字体'; + if (item.used === item.kind) return item.kind + ' → ' + item.name; + return item.kind + ' → 本机没有' + item.label + '字体,用' + item.name + '(' + FONT_KINDS[item.used].label + ')代替'; + }); + const kai = kinds.find((item) => item.kind === 'kai'); + const tail = kai && kai.used && kai.used !== 'kai' && FONT_KINDS[kai.used].handwriting + ? '。本机的 kai 是毛笔字,识字挂图、课本、试卷这类要标准字形的版面用 song 或 hei' + : ''; + return parts.join(';') + tail; +} + +// 时间线上的难度:「难(规划判的:15 段排版文字)」「一般(按规格推断)」。 +function difficultyText(image) { + const level = difficultyOf(image); + return DIFFICULTY_LABELS[level.level] + '(' + (level.source === 'plan' ? '规划判的' : '按规格推断') + (level.reason ? ':' + level.reason : '') + ')'; +} + async function planNode(session, deps) { const startedAt = Date.now(); + // 规划还没判难度,它要做的只有打开你给的参考图:按张数给(24 次真机规划里 18 次一次工具都没用)。 + const budget = toolBudget('plan', { needs: [{ count: session.refs.length, what: '打开你给的参考图' }] }); + const fontFacts = await fontFactsLine(); deps.emit(session, 'plan', '规划中:分诊欠定项、决定要不要检索参考图、给每张图出规格', { detail: detail('规划节点 · 输入', [ text('用户原话', session.instruction), kv('已登记参考图', session.refs.length ? session.refs.map((ref) => ref.id).join('、') : '无'), - kv('工具调用预算', DEFAULTS.toolBudget.plan + ' 次(运行时硬中断)'), + kv('工具调用预算', budget.limit + ' 次(' + budget.note + ',运行时硬中断)'), ]), }); const refs = session.refs @@ -933,21 +3134,158 @@ async function planNode(session, deps) { ' (a) 用户原话里这件事有两种以上都说得通的解读;(b) 选错会让整张图作废,而不是"不够好";(c) 你没有任何依据判断哪个对。', ' 最多 2 条,通常应该是 0 条。每条形如 {"text":"问题","options":["选项一","选项二"],"default":"选项一"},', ' options 给 2-4 个互斥的具体选项,default 写你倾向的那个(用户不答就按它走)。', - '2. 需求里出现真人、具体产品、logo、车型、地标这类“必须长成某个样子”的实体,而上面没有对应参考图时,', - ' 不要自己调工具检索——把检索意图写进顶层 ref_queries,系统会替你调搜图工具并下载到本地。', + '2. 参考图:要不要检索、搜几项、肯花多少检索次数,由你按这次需求判断,不是每次都要搜。', + ' 不要自己调工具检索——把检索意图写进顶层 ref_queries,系统会替你调搜图工具、下载、筛选并编号。', ' 这是硬性分工:你只负责说清“要搜什么”,检索、下载、编号全部由运行时执行。', - ' ref_queries 每项形如 {"task_name":"tokyo_tower","query":"东京塔 白天 正面 全景","prompt":"必须是东京塔本体的清晰全景照片,不要夜景灯光秀,不要局部特写"}:', + ' 建议先检索的情况:需求里出现专有名词或特定称谓,而上面没有对应的参考图。包括:', + ' - 真人、角色或吉祥物、品牌和具体产品、logo、车型、地标、具体的建筑、文物或艺术品——这类东西长什么样是定死的,凭记忆画很容易画错;', + ' - 用户点名一部真实存在的作品(电影、剧集、动画、游戏、小说)当主题,要做它的海报、同人图时:它的官方海报或剧照,', + ' 参考主角的经典造型和服装、标志性道具、配色和片名字体(query 写「作品名 官方海报」);', + ' 人物的脸照原创画,不照搬演员(用户点名要像某位演员时除外),slots 里写清只参考造型、道具、配色和版式;', + ' - 你不确定长什么样的冷门叫法:小众品种、地方特产、特定流派或工艺的名字。', + ' 可以搜也可以不搜的:通用的东西(猫、咖啡杯、海边、写字楼)、常见的画风和版面类型(水彩、扁平插画、课本内页、菜单)。', + ' 你有把握画对就不搜;觉得参考一张真实样例能明显画得更准(版式、材质、某种工艺的质感)再搜。纯想象、现实里没有对应物的画面不搜。', + ' ref_budget:这次检索最多花多少次调用(整数 0-' + DEFAULTS.refBudgetMax + ')。搜图一次算 1 次,判定一批候选(3 张)算 1 次;', + ' 一项顺利时 2 次(搜 1 + 判 1),第一批都不对、换个说法重搜要 4–6 次。按每项有多要紧给:专有名词的主体给足(每项 4–6 次),', + ' 顺手参考的画风、版式每项 2 次就够。不搜写 0(ref_queries 写空数组)。不写时系统按每项 2 次、至少 ' + DEFAULTS.refBudgetDefault + ' 次给。', + ' 预算按 ref_queries 的顺序花,花完后面的项就不搜了:最要紧的写在前面。最多 6 项。', + ' ref_queries 每项形如 {"task_name":"tokyo_tower","query":"东京塔 官方照片","alt_query":"Tokyo Tower","use":"subject","take":"塔身的结构和红白配色","prompt":"必须是东京塔本体,要能看清整座塔的外形"}:', ' - task_name:英文小写短名,本次规划内唯一;图片的 refs 就用这个名字引用检索结果。', - ' - query:中文实体名 + 用途词(正面照 / 内饰 / 车标 / logo / 门店)。', - ' - prompt:相关性过滤条件,写清楚要什么样的图。', - ' - 一个实体一项,最多 6 项;上面已经登记成 R1、R2… 的实体不要重复写。', - ' - 没有这类实体就写空数组,不要为了凑数硬编。', - '3. 图片的 refs 只写三种值:已登记的 R 编号、ref_queries 里的 task_name、公网图片地址(http/https)。', - ' 三种系统都会归一成 R 编号并下载到本地;写别的字符串会被忽略并告警。', - '4. 为每张要交付的图给出:id(大写字母,如 A、B)、slug、title、entry(t2i 或 edit)、aspect_ratio(如 "16:9",拿不准用 "auto")、slots、refs(引用 R 编号 / task_name / 图片地址)、text_layers(画面里必须出现的文字,没有就空数组)、checklist。', + ' - query:东西的正式名称 + 一个用途词(官方图 / 照片 / 正面照 / 车标 / logo / 实拍 / 样例 / 扫描)。', + ' 不要堆「立体摆件 手办 树脂公仔」这类泛词:泛词会把结果带到无关的商品上,实体名反而被淹没。', + ' - alt_query:同一个东西换一种说法,写它在原产地语言或英文里的正式名称(例:ミャクミャク 公式、Myaku-Myaku official)。', + ' 第一批候选都不合格时系统会用它重搜。没有别的叫法就写一个同义说法。', + ' - use:这张参考图拿来做什么。subject(主体外观,照着它画这个东西)/ detail(局部:道具、服饰、材质)/', + ' style(画风:笔触、质感、配色)/ layout(版式:排版、留白、各元素的位置)/ scene(环境、场所)。', + ' 筛选按用途判:style / layout / scene 只要是这一类的真实样例就能用,不要求是某一个具体的东西。', + ' - take:一句话写从它身上取什么(例:只取版式:课题、正文、插图和页码的位置),生成时会原样告诉模型。', + ' 底图要留白给程序排字、画图表的图,layout 参考图不会送进生图(上面全是字,会诱导模型在留白处写字)。', + ' - prompt:筛选要求,只写「必须是哪个东西(或哪一类东西的真实样例)」和最多一条用途(例:要能看清全身外形)。', + ' 不要写颜色、五官、肢体数量、花纹这类外观细节:你可能记错,筛选节点会照着错的描述把真图否掉。', + ' 不要写互相冲突、实际拍不到的条件(例:既要看到完整的环形全貌、又要看清构件细节、还不要航拍)。', + ' - 同一个东西只写一项:不要把「官方形象」「手办」「雕像」拆成几项,官方插画、实物雕像、手办照片都能当参考图。', + ' 只有确实需要两种完全不同的图(例:整体外观 + 材质细节特写)时才拆,拆开的两项要求不能重叠。', + ' - 上面已经登记成 R1、R2… 的不要重复写;用户给了图的东西就用用户的图,不另搜。', + '3. 图片的 refs 只写三种值:已登记的 R 编号、ref_queries 里的 task_name、用户原话里给出的图片地址(http/https)。', + ' 不要凭记忆写网址:原话里没有的地址系统不会用。三种都会归一成 R 编号并下载到本地;写别的字符串会被忽略并告警。', + '4. 为每张要交付的图给出:id(大写字母,如 A、B)、slug、title、entry(t2i 或 edit)、aspect_ratio(如 "16:9",拿不准用 "auto")、slots、refs(引用 R 编号 / task_name / 图片地址)、text_layers(画面里必须出现的文字,没有就空数组)、text_overlay(有文字时写,见 4d)、qr_codes(画面里要放的二维码,没有就空数组,见 4c)、animation(要做动图 / GIF 时写,见 4e,不是动图写 null)、motion_gif(绕一个中心转的动画写,见 4e,不要就写 null)、panel_gif(分格组图要拼成 GIF 时写,见 4e,不要就写 null)、charts(画面里要有带数据的图表时写,见 4f,没有就空数组)、difficulty 和 difficulty_reason(见 4g)、checklist。', '4b. slug 是这张图的英文短名,直接当交付文件名用:全小写、单词之间用短横线、2-4 个词、只概括画面内容(主体 + 场景或动作),', ' 例:orange-cat-on-windowsill、girl-and-cat-sleeping、shiba-under-tokyo-tower。不要写尺寸、风格、镜头语言,不要重复 id。', ' 同一批图里 slug 必须互不相同——重名会导致交付文件互相覆盖。', + '4c. 二维码:生图模型画不出能扫的码,码由系统生成真码、贴回画面、扫码验证,你只负责记下码里放什么。', + ' 需求里要求画面出现二维码(名片、海报、菜单、门牌上的码)时,每枚码写一项 {"content":"…","where":"名片下半部分居中"}:', + ' - content 从用户原话里逐字复制(链接、文字、号码),一个字符都不要改:不要补全、不要缩写、不要翻译、不要换成你觉得对的网址。', + ' 例:用户说「二维码链接至([https://www.minimax.cn/])」,content 写 "https://www.minimax.cn/"。', + ' - 用户要码却没说码里放什么,content 写空字符串 ""。不要自己编网址,也不要把它写进 questions:照常出图,交付时系统会注明是装饰码,', + ' 用户之后补一句内容就能直接在原图上换成真码。', + ' - 多枚码按画面里从上到下、从左到右的顺序列出,最多 3 枚。', + ' - 码能不能扫、扫出什么由系统实测(自动挂一条 H-qr),checklist 里不要写「二维码能扫」「链接正确」这类条目;', + ' 可以写「名片下半部分有一枚二维码」这种看得见的条目。slots 里只写码放在哪、多大,不要描述码的图案。', + '4d. 图内文字的兜底:生图模型写长段文字、小字、竖排、书法时很容易写错、漏字、编字。系统有一条兜底链路:', + ' 先让模型画一张不带字的底图(纸面、招牌、屏幕、海报底板留空),再由程序用字体把文字按原文逐字排进去。', + ' text_layers 不为空的图都要写 text_overlay(不管这一次用不用:模型把字画残了,系统会自动切到这条路):', + ' {"use":true,"base":{"subject":"…"},"layers":[{"text":"…","box":[0.06,0.16,0.94,0.84],"direction":"vertical","font":"brush","color":"#1a1a1a","align":"start","punctuation":"omit","surface":"宣纸纸面"}]}', + ' - use:文字多(一段 20 字以上或合计 40 字以上)、要逐字准确的长文(诗词文章、书法作品、菜单、说明、价目表)、竖排、小字密排时写 true,', + ' 第一轮就走程序排字;短标题、几个字的招牌写 false,先让模型直接画。', + ' - base:不带字的底图要改写的槽位,只写要改的那几个,没写的沿用 slots。凡是提到文字内容、书法作品、墨字笔画、「写着什么」的槽位都要改', + ' (常见是 subject,有时还有 style、scene、light、negative),改成「载体留空」的说法:纸张、招牌、屏幕、海报底板这些载体照样画,载体上什么都不写。', + ' 例:subject 写「一幅完全展开、平铺的浅米色宣纸手卷,纸面完全空白,没有任何字迹、印章和界格线」,不要写「书法长卷」「写满了字」。', + ' base 里不要出现文字内容本身。', + ' - layers:text_layers 的每一项写一个,text 和 text_layers 里对应的那项逐字相同。', + ' box:文字放在画面里的什么位置,[左, 上, 右, 下],0-1 的画面比例;要落在 base 留空的载体上,离载体的边缘留出边距。', + ' 字号由系统按 box 自动取能排下的最大值:box 画多大,字就排多大,标题的 box 给扁一些,正文的 box 给大一些。', + ' direction:"horizontal" 横排 / "vertical" 竖排(从上到下、从右到左)。', + ' font:"brush" 毛笔楷书(清楚好认的毛笔字,楷书、行楷用它)/ "xing" 毛笔行书(更连贯,辨认度低一些)/ "kai" 楷体 / "song" 宋体 / "hei" 黑体。', + ' 本机实际用的字体:' + fontFacts + '。', + ' color:十六进制颜色。align:"start" / "center" / "end"(横排是左、中、右;竖排是每列从顶上写、居中、靠底)。', + ' punctuation(竖排才写):书法作品(古文、诗词、对联、手卷、条幅)写 "omit" 不写标点:古代书法本来不写标点,', + ' 「全文 324 字」这类字数也不算标点;用户明确要带标点的书法作品写 "side"(标点缩小写在字旁、不占格);', + ' 菜单、说明、现代竖排海报写 "inline"(标点占一格)。', + ' surface:文字写在什么上面,例:宣纸纸面、木招牌、海报底板、手机屏幕。', + ' role:这一层在版面上是什么,title(标题)/ subtitle / body(正文)/ footnote(脚注)/ caption(图注)/ label / page(页码)/ pinyin(注音)。', + ' 同一种 role 的几层,程序统一成同一个字号(取其中最小的),所以一篇正文的几段都写 body、几条标签都写 label;', + ' 不要为了让字号不同而拆层,一个标题写成一层(「少年闰土①」),只有注在某个字上方的拼音单独一层(role 写 pinyin)。', + ' 版面像课本、书刊、说明书时:正文每段一层、role 写 body,程序自动首行空两格(不要缩进就写 "indent":0);', + ' 脚注写成一层、role 写 footnote,程序会在它上方画一条短分隔线;正文、标题里的脚注符号(①②…)直接写在被注的字后面', + ' (「猹(chá)②尽力」),程序把它排成右上角的小字,脚注层每条开头的序号保持正常大小。', + ' 最多 16 层。页码、角标这类短字别忘了也写成一层。', + ' part:一个字里要有两种颜色时写(识字挂图:「清」的「氵」橙色、「青」深灰),{"side":"left","color":"#E8710A"}:', + ' 这一层每个汉字靠 side(left / right / top / bottom)那一侧的部件用 part.color,其余部分用这一层的 color。', + ' 程序在字形上找偏旁和其余部分之间的切线来分色;字体里切不开时会自动换宋体或黑体。', + ' 要出现的字一定写成完整的一个字:不要把「清」拆成「氵」「青」两层并排摆,也不要在整字上再叠一层偏旁:', + ' 拆开的是两个字,叠上去的偏旁和字里的偏旁位置、大小都对不上。独立的偏旁(「氵」「忄」「讠」)字形本来就窄小、', + ' 靠字身左边,要它看得清,就单独一层、role 不要和整字共用,box 按想要的大小给。', + ' 系统会把各层按位置从上到下、从左到右读一遍,和用户原话里引号中的文字对照,读不出来会退回来让你改。', + '4e. 动图(GIF):需求要的是动图、GIF、会动的表情包时,在这张图上写 animation。生图模型一次只出一张静态图,', + ' 系统先定一张参考图,第一帧从参考图改,后面每一帧在前一帧上接着改一小步(参考图一直作为对照),最后由程序拼成 GIF:', + ' {"reference":"R1","frames":[{"same":true},{"prompt":"右手抬到脸旁边,掌心朝前"},{"prompt":"右手举过头顶,向外挥"},{"prompt":"右手抬到脸旁边,掌心朝前"}],"frame_ms":300,"loop":true,"edge":768,"transition":"fade"}', + ' - reference:用户给了图、要拿这张图直接做动图,写它的 R 编号,系统不会再画这张图,slots 简单描述这张图的画面即可;', + ' 用户没给图,或者要先把他给的图改成别的样子(换画风、换场景)再动起来,写 "self":这张图先按 slots 正常画出来当参考图,', + ' 交给用户确认之后再出各帧。', + ' - frames:按播放顺序 4–16 帧。帧数按动作幅度定,不要每次都写 6 帧:目标是 GIF 看起来顺,相邻两帧只差一小步。', + ' 眨眼、挑眉、嘴型这类小动作 4–6 帧;点头、挥手、耸肩 6–10 帧;转头到另一边、手从画面外抬进来、起身这类大动作 10–16 帧。', + ' 一步里只让一两个部位动一点,几步的变化量差不多大:不要一步里同时转头、抬手、换表情,再接一步几乎不动。', + ' 帧多的时候 frame_ms 写短一点(120–200 毫秒),整组一般 1.5–3 秒。系统会量相邻两帧变了多少,一步跨得太大的地方', + ' 会自动补中间姿态,但规划时就拆细最好。', + ' {"same":true} 表示这一帧就是参考图本身,通常第 1 帧是它;其余每帧的 prompt 写这一帧和参考图', + ' 相比所有不同的地方(姿势、表情、手的位置、头的角度),不要描述没变的主体和背景:系统会要求其余部分和参考图保持一致。', + ' 每一帧的 prompt 写这一帧的完整状态(模型按它把前一帧改到这个样子):前面帧里做出的变化,后面的帧还要保持的,要在后面每一帧里再写一遍', + ' (第 3 帧把伞向右倾斜罩住小猫,第 4 帧伞还斜着、小猫挪近,第 4 帧就写「伞仍向右倾斜约三十度罩住小猫,小猫挪近半步」)。', + ' 只写这一帧新加的动作,前面的变化可能被改回参考图的样子,播放时动作来回跳。', + ' 这是关键帧动画,不是视频:把动作拆成几个清楚的姿态(挥手:手在脸旁 → 举过头顶 → 回到脸旁),相邻两帧的变化小而明确。', + ' 循环播放时播完自动接回第 1 帧,末尾不要再放 {"same":true};最后一帧到第 1 帧也是一次变化,跨度不要比中间的大,', + ' 要回到起手的姿态,就在末尾放一两帧逐步回去。只播一遍(loop 为 false)又要停在开头的姿态,才在末尾放 {"same":true}。', + ' 不要写镜头推拉、缩放、换背景这类每一帧画面都要整体变的动作。', + ' 两帧写一模一样的 prompt,系统只画一次、两帧用同一张图:来回往复的动作这样写,回到同一个姿态时画面才不抖。', + ' - 参考图要先画的(reference 写 "self"),area 写 "2K":GIF 最大 1024 像素,4K 只会更慢。', + ' - frame_ms:每帧停多少毫秒,默认 300;某一帧要停久一点,在那一帧写 "hold_ms"。loop:默认 true,循环播放。', + ' edge:GIF 长边多少像素,默认 768,最大 1024。', + ' - transition:帧和帧之间怎么接。默认 "fade",程序在相邻两帧之间补两帧渐变,动作更顺(两帧差得多时不补:叠出来是虚影);', + ' 要一下一下跳的效果(像素风、定格动画、卡点、闪烁)写 "cut",直接切换。', + ' - checklist 照常写参考图本身要满足的条件;每一帧画得对不对由动图评审单独核对,不要写进 checklist。', + ' - 组图拼 GIF:用户要的是一张分格的组图(四格、六格、九宫格的表情变化、分步、前后对比),同时要把它做成 GIF', + ' (「做成动图」「再拼成 GIF」),不写 animation,在这张组图上写 panel_gif。组图照常生成、评审,通过后由程序切格、', + ' 按顺序拼成 GIF,组图和 GIF 一起交付:{"rows":2,"cols":3,"frame_ms":700,"loop":true}', + ' rows / cols 是几行几列,和 slots 里写的排法一致。slots 里写清:格与格之间用细分隔线或留白隔开,主体在每一格里的位置和大小一样,', + ' 格子里不写序号(用户要的除外):程序靠分隔线切格,主体位置一致,播起来才不会一跳一跳的。', + ' frame_ms 每格停多少毫秒(默认 700,第一格停 1.5 倍、最后一格停 2 倍);某一格要停久一点写 hold [{"index":6,"ms":2000}];', + ' order 播放顺序(格子编号从上到下、从左到右数,默认按编号);pingpong 写 true 来回播;loop 默认 true;', + ' transition 默认 "cut",直接切换;"fade" 两格之间渐变(两格差得多时叠出来是虚影,少用)。', + ' 组图评审通过后,系统会在相邻两格之间各补一格中间状态(在原组图上改一次、一起生成,从候选里挑),GIF 一步一步地动;', + ' 要多一次生图、一两分钟。格与格之间换了场景、换了人物或机位(四格漫画讲故事)写 "smooth":false,不补。', + ' 动作要连贯顺滑(挥手、眨眼、转身)用 animation,不要用 panel_gif:组图的格子之间是一下一下切换的。', + ' - 绕一个中心转的动画(加载圈、转圈的图标、旋转的齿轮 / 风车 / 唱片 / 罗盘、亮点沿圆环一格一格跑),不写 animation,', + ' 写 motion_gif:模型只画一张图,程序按圆心把画面转出各帧,每一帧都准、首尾接得上。一帧一帧让模型改画不准', + ' 亮点在第几格,改错的帧扔掉以后亮点一下跳一格、一下跳三格。', + ' {"type":"spin","center":[0.5,0.5],"radius":0.32,"steps":12,"direction":"cw","frame_ms":80,"edge":512}', + ' center 是转动中心在画面里的位置(宽、高的比例),radius 是要转的那一块的半径(画面短边的比例),要盖住整个转动的东西;', + ' steps 写圆环上等距的点、辐条或格子数(亮点一格一格跑),整个东西连续转写 0;direction 是 cw 顺时针 / ccw 逆时针;', + ' frame_ms 一格停多少毫秒(一格一格跳的 60–120,连续转的 40–60);edge 是 GIF 长边,用户说了尺寸就照写。', + ' slots 写的是一张静态图:转动的东西正好在 center、等距的点或辐条间距完全一样;radius 那一圈里面的背景是纯色、', + ' 或者从 center 向外的渐变,不放别的东西(转的时候背景跟着转,有图案就看得出来),圆圈外面的东西不动;', + ' 亮点一格一格跑的,只画一个时刻:亮点在 12 点钟,尾巴画在亮点转过来的那一侧(顺时针转时尾巴在它的逆时针一侧)。', + '4f. 数据图表:画面里要有柱状图、条形图、折线图、面积图、饼图、环形图这类带数据的图表时,在这张图上写 charts。', + ' 生图模型画不准柱子高矮、刻度和数字,图表由程序按数据画进底图上留空的面板,画完从最终文件量回来核对:', + ' [{"type":"bar","title":"2020–2025 年销售额","unit":"万元","categories":["2020","2021","2022"],"series":[{"name":"销售额","values":[120,150,180]}],"box":[0.08,0.3,0.92,0.92],"surface":"白色卡片"}]', + ' - type:bar 柱状图(比较各项、按时间的少量数)、hbar 条形图(排名、类目名字长)、line 折线图(趋势、点多)、area 面积图、', + ' pie 饼图 / donut 环形图(各部分占整体,各项加起来就是整体)。多个系列的柱状图、条形图可以写 "stacked":true 堆叠。', + ' - 数据只抄用户给的:categories 和 values 从用户原话里逐个照抄,顺序一致,一个数都不要改、不要补、不要编;', + ' 比例、合计、增长率由程序算,不要自己算好写进去。用户没给具体的数,而图表是这张图的重点时,在 questions 里问他要数;', + ' 图表只是背景装饰(科技感海报背景里的上涨曲线)就不写 charts,在 negative 里写「不要画带数字和刻度的图表」。', + ' - values 写数字,不带单位;单位写进 unit("万元"、"%"、"人次")。百分比写 25 不写 0.25,unit 写 "%"。', + ' 折线图的数离 0 很远(体重 60–70 kg)可以写 y_min(例如 60),刻度从它开始;柱状图、条形图一律从 0 起,不写 y_min。', + ' - box:图表在画面里的位置 [左,上,右,下](0-1 的画面比例);surface:图表画在什么上面(白色卡片、黑板、纸面、屏幕)。', + ' 图表要占够地方:短边不小于画面的 35%,类目多、系列多就更大。slots 里把这块写成留空的平整面板', + ' (「右侧一块空白的白色卡片」),不要描述图表本身;系统会要求模型把这块留空。', + ' - title 可写可不写,写了就不要再在 text_layers 里写同一个标题。colors 可以给每个系列(饼图给每一块)配和画面协调的颜色(十六进制)。', + ' - checklist 不要写「柱子高度和数值一致」这类条目,程序会核对。', + '4g. difficulty:这张图难不难验、难不难改,写 "easy" / "normal" / "hard",difficulty_reason 用一句话写为什么(不超过 30 字)。', + ' 它决定后面评审、修正、选优、追问编辑这些节点每次能调用几次工具(打开看图材料、放大看细节、核对参考图):', + ' 系统按「必须打开的图 + 难度给的余量」现算,难的图多给。判断标准:', + ' - hard:要逐字核对的大段文字或多段排版(课本页、菜单、长卷)、要和参考图对上的真人或产品、多个主体之间的一致性、', + ' 精确的数量和手势姿态、8 帧以上的动图、几张图表,或者硬性条目很多(6 条以上);', + ' - easy:单一主体、没有参考图和文字、要求一眼就能看清(一只猫、一杯咖啡);', + ' - 其余写 normal。拿不准就写 normal。', '5. 不要自己写整段 prompt,改为填 slots —— 系统会按固定顺序拼成送进模型的长描述。槽位含义:', ' style 整体风格与调色。例:高端汽车广告级写实摄影,暖中性色调,真实皮肤纹理', ' shot 景别。例:车内广角中近景 / 半身特写 / 全景', @@ -961,8 +3299,12 @@ async function planNode(session, deps) { ' negative 排除项。例:不要文字、不要水印、不要多余的人', ' style/shot/composition/scene/subject/light/lens 是必填,缺一个系统会用兜底值并告警;material 可留空。', ' 每个槽位写具体可执行的描述,不要写"合适的光线""精美的构图"这种空话。', - '5b. 色彩是硬要求:style 槽位必须写清白平衡(例:中性白平衡、真实色彩还原,不要暖黄调);', - ' negative 槽位必须包含"整体偏红、偏暖发黄"。系统还会再追加一条固定的白平衡要求,不要重复抄。', + '5b. 色彩是硬要求:style 槽位必须写清白平衡(例:中性白平衡、真实色彩还原,不要暖黄调);用户要的就是以黄、红、橙为主色的画面时', + ' 不写「不要暖黄调」,写成「主色来自物体和背景本身的颜色,不是暖色滤镜;白、灰、黑这些中性的地方保持中性」;', + ' negative 槽位必须包含"整体偏红、偏暖发黄"(用户要的就是以黄、红、橙为主色的画面时不写这条,写了也会被去掉)。系统还会再追加一条固定的白平衡要求,不要重复抄。', + '5c. 要靠参考图锁定外观的实体(ref_queries 里写了的、或已登记成 R 编号的),subject 里写它的名字、在画面里的位置和大小、', + ' 材质或造型上要做的改动(例:做成树脂摆件),外观写「外观与参考图一致」。不要凭记忆补写它的颜色、五官、肢体数量和花纹:', + ' 记错了会直接画错,参考图到了还会和你写的打架。你非常确定的一两个标志性特征可以写。', '6. checklist 每项形如 {"id":"H1","kind":"hard","text":"...","verify":"vlm"};hard 是必须满足的(主体与数量、图内文字、参考图一致性、用户点名要求),soft 是风格偏好。', ' hard 最多 4 条,而且只能来自用户原话里明确说过的东西——不要把自己想到的"最好也有"写成 hard。', ' 每张图至少 1 条 hard。写不出 4 条就少写,不要凑数。', @@ -971,6 +3313,7 @@ async function planNode(session, deps) { ' 正例拆成两条「机位在车外左前方约45度,画面里能看到车头左前角与A柱」「透过前挡风玻璃与驾驶位侧窗能清楚看到车内两人」。', ' 机位、角度、景别、构图这类必须各自单独成条,并写成"画面里能看到什么"这种可核对的事实,不要写成风格描述。', ' 拆完超过 4 条时,把语义相邻的合并,但上面这类构图条目不要合并。', + ' 参考图一致性写成「画面里的 X 和参考图是同一个(认识的人一眼能认出)」,不要把它的外观细节拆成 hard 条目(理由同 5c)。', '6b. 姿势正确性:需求里出现手语/手势、瑜伽或健身体式、乐器指法、舞蹈或武术动作、', ' 攀岩抓点、握笔握筷这类「必须摆成某个特定姿势」的内容时,每个姿势单独写一条 hard,', ' 而且必须写成「姿势本身对不对」,不是「图上有没有对应的文字标签」。', @@ -980,7 +3323,7 @@ async function planNode(session, deps) { ' 同一张图里姿势多于 4 个时,按组分条(例:「A-E 五格手型分别符合 ASL 对应字母」),', ' 但仍然必须是 hard,不许降级成 soft。', '7. 默认 4K:除非用户明确要别的尺寸,area 一律写 "4K"。', - '8. route:需求简单、只出 1 张图写 "fast";多图或有参考图/文字/一致性要求写 "full"。', + '8. route:需求简单、只出 1 张图写 "fast";多图或有参考图/文字/一致性要求、或画面里要放能扫的二维码写 "full"。', '8b. 主体一致性(多图专用):images 达到 2 张,且同一个主体(人物 / 动物 / 拟人化物体)在 2 张以上的图里出现时,', ' 必须输出 character_refs,为该主体单独出一张「角色设定图」,之后相关图全部以它为外观基准。', ' 判据是"这个主体会不会被画成两个样子":漫画分镜、绘本、系列海报、同一产品的多角度图要写;', @@ -989,15 +3332,49 @@ async function planNode(session, deps) { ' prompt 必须写成角色设定稿:纯净浅灰单色背景、主体全身正面居中站姿、柔和平光无强阴影、无文字无水印,', ' 并把该主体的外观特征全部写死(脸型、发型与发色、眼睛、体型比例、服装款式与配色、标志性配件)。', ' 最多 2 个主体。没有跨图重复主体时输出空数组 []。', - '10. 规划阶段总工具调用上限 6 次,只用于 read 已登记的参考图。到达上限就用手上已有的信息出计划。', + '10. 规划阶段总工具调用上限 ' + budget.limit + ' 次,只用于 read 已登记的参考图。到达上限就用手上已有的信息出计划。', '', '只输出一个 JSON 对象,不要解释、不要 markdown 代码块:', - '{"route":"full","think":["..."],"assumptions":["柴犬坐在长椅左侧,把右侧留给塔身,因为需求要同时看清狗和塔"],"questions":[],"character_refs":[{"key":"girl","note":"16 岁少女","prompt":"角色设定稿:纯净浅灰背景,全身正面居中站姿…","images":["A","B"]}],"ref_queries":[{"task_name":"tokyo_tower","query":"东京塔 白天 正面 全景","prompt":"必须是东京塔本体的清晰全景照片"}],"images":[{"id":"A","slug":"shiba-under-tokyo-tower","title":"...","entry":"t2i","aspect_ratio":"16:9","area":"4K",', + '{"route":"full","think":["..."],"assumptions":["柴犬坐在长椅左侧,把右侧留给塔身,因为需求要同时看清狗和塔"],"questions":[],"character_refs":[{"key":"girl","note":"16 岁少女","prompt":"角色设定稿:纯净浅灰背景,全身正面居中站姿…","images":["A","B"]}],"ref_queries":[{"task_name":"tokyo_tower","query":"东京塔 官方照片","alt_query":"Tokyo Tower","use":"subject","take":"塔身的结构和红白配色","prompt":"必须是东京塔本体,要能看清整座塔的外形"}],"ref_budget":4,"images":[{"id":"A","slug":"shiba-under-tokyo-tower","title":"...","entry":"t2i","aspect_ratio":"16:9","area":"4K",', ' "slots":{"style":"...","shot":"...","composition":"...","scene":"...","subject":"...","material":"...","light":"...","lens":"...","text":"","negative":"..."},', - ' "refs":[],"text_layers":[],"checklist":[{"id":"H1","kind":"hard","text":"...","verify":"vlm"}]}]}', + ' "refs":[],"text_layers":[],"text_overlay":null,"qr_codes":[],"animation":null,"motion_gif":null,"panel_gif":null,"charts":[],"checklist":[{"id":"H1","kind":"hard","text":"...","verify":"vlm"}]}]}', ].join('\n'); - const { data } = await deps.agent.runJson(session, { step: 'plan', instruction, maxTools: DEFAULTS.toolBudget.plan }); + let { data } = await deps.agent.runJson(session, { step: 'plan', instruction, maxTools: budget.limit, budgetNote: budget.note }); + // 程序核对:文字层按版面读出来,用户引号里要排的字读不出来,就把具体问题交回规划改一次(见 readingProblems)。 + let images = normalizePlan(data, session); + let reading = readingProblems(session, images); + if (reading.length) { + deps.emit(session, 'plan', '规划的文字按版面读出来和你的原话对不上,交回规划改一次:' + reading.map((item) => '「' + item.quote + '」').join(''), { + level: 'warn', + detail: detail('文字核对 · 第 1 次规划', readingDetail(reading)), + }); + try { + const retry = await deps.agent.runJson(session, { + step: 'plan', + instruction: instruction + '\n\n' + readingFeedback(reading, data), + maxTools: budget.limit, + budgetNote: budget.note, + }); + const retryImages = normalizePlan(retry.data, session); + const again = readingProblems(session, retryImages); + if (retryImages.length && again.length <= reading.length) { + data = retry.data; + images = retryImages; + reading = again; + } + } catch (error) { + deps.emit(session, 'plan', '规划重写没跑成(' + describeError(error) + '),按第一次的规划走', { level: 'warn' }); + } + if (reading.length) { + deps.emit(session, 'plan', '改过之后还是读不出来:' + reading.map((item) => '「' + item.quote + '」').join('') + ',评审会按图核对这几处', { + level: 'warn', + detail: detail('文字核对 · 改过之后', readingDetail(reading)), + }); + } else { + deps.emit(session, 'plan', '规划改过之后,文字按版面读出来和你的原话对上了'); + } + } session.plan = data; session.route = data.route === 'fast' ? 'fast' : 'full'; const refQueries = normalizeRefQueries(data.ref_queries); @@ -1007,9 +3384,83 @@ async function planNode(session, deps) { session.think = Array.isArray(data.think) ? data.think : []; session.assumptions.push(...(Array.isArray(data.assumptions) ? data.assumptions : []).map((item) => String(item || '').trim()).filter(Boolean)); session.blockers = normalizeBlockers(data.questions); - session.images = normalizePlan(data, session); + session.images = images; + for (const image of session.images) image.spec.readingProblems = reading.filter((item) => item.imageId === image.id); + // 要贴真码的图走完整回路:快速路线每轮只出 1 张候选、最多重来 1 次,码没贴成就只能带着坏码交付。 + if (session.route === 'fast' && session.images.some((image) => qrContents(image.spec).length)) { + session.route = 'full'; + } + // 文字容易写残的图(字多、竖排、书法,或者规划自己判断)一开始就让模型画无字底图、程序排字。 + // 其余有文字的图先让模型直接画,评审判出文字画残了再切过来(见 runImage)。 + for (const image of session.images) { + const overlay = image.spec.textOverlay; + if (!overlay || !overlay.layers.length) continue; + const risk = textRisk(overlay.layers); + if (!overlay.use && !risk.high) continue; + // 规划说要走程序排字、却没写排在哪的短字:按默认位置排会在画面正中压一大块字,先让模型直接画。 + if (!canPlaceText(image)) continue; + const why = risk.high ? '文字' + risk.reasons.join('、') + ',模型直接写很容易写残' : '规划判断这段文字模型直接写容易写残'; + enterOverlay(session, image, why); + if ((overlay.dropped || []).length) { + deps.emit(session, 'plan', image.id + ' 的文字超过 ' + MAX_TEXT_LAYERS + ' 段,后面 ' + overlay.dropped.length + ' 段排不进去:' + overlay.dropped.map((value) => '「' + shortText(value, 12) + '」').join(''), { level: 'warn' }); + } + deps.emit(session, 'plan', image.id + ' ' + why + ':先画不带字的底图,文字由程序按原文排进去', { + detail: detail('程序排字 · ' + image.id, [ + kv('原因', why), + ...overlay.layers.map((layer, index) => + group('第 ' + (index + 1) + ' 段文字', [ + kv('内容', shortText(layer.text, 40)), + kv('位置', describeBox(layer.box) + (layer.surface ? '(' + layer.surface + ')' : '')), + kv('排法', (layer.direction === 'vertical' ? '竖排' : '横排') + ' · ' + FONT_KINDS[layer.font].label + (layer.color ? ' · ' + layer.color : '')), + ]), + ), + text('不带字的底图 prompt(送进模型)', image.spec.basePrompt), + ]), + }); + } + // 程序排字要靠重画来换一张留好空白的底图,快速路线只有一次重来的机会,不够用。 + if (session.route === 'fast' && session.images.some((image) => isOverlay(image))) { + session.route = 'full'; + } + for (const image of session.images) { + for (const problem of image.chartProblems || []) deps.emit(session, 'plan', image.id + ' 有一张图表写得不完整,没画:' + problem, { level: 'warn' }); + delete image.chartProblems; + const charts = imageCharts(image); + if (!charts.length) continue; + deps.emit(session, 'plan', image.id + ' 里的 ' + charts.length + ' 张图表由程序按数据画:先画留出空白面板的底图,图表画上去之后从最终文件量回来核对', { + detail: detail('数据图表 · ' + image.id, [ + ...charts.map((chart, index) => + group('第 ' + (index + 1) + ' 张', [ + kv('图表', describeChart(chart)), + kv('位置', describeBox(chart.box || CHART_DEFAULT_BOX) + (chart.surface ? '(' + chart.surface + ')' : '')), + chart.check && !chart.check.ok ? kv('数据核对', '这些数在用户原话里找不到:' + chart.check.missing.join('、')) : kv('数据核对', '每个数都能在用户原话里找到'), + ]), + ), + ]), + }); + const unverified = charts.filter((chart) => chart.check && !chart.check.ok); + if (unverified.length) { + deps.emit(session, 'plan', image.id + ' 的图表里有数在用户原话里找不到:' + unverified.map((chart) => chart.check.missing.join('、')).join(';') + ',照画,交付时请用户核对', { level: 'warn' }); + } + } + // 面板没留空要靠重画来换底图,和程序排字一样走完整回路。 + if (session.route === 'fast' && session.images.some((image) => hasCharts(image))) { + session.route = 'full'; + } + for (const image of session.images) { + const unverified = (image.spec.qrCodes || []).filter((item) => item.content && !item.verbatim); + if (!unverified.length) continue; + deps.emit( + session, + 'plan', + image.id + ' 的二维码内容在用户原话里找不到,不贴真码:' + unverified.map((item) => '「' + item.content + '」').join('、'), + { level: 'warn' }, + ); + } for (const image of session.images) { if (!image.spec.missingSlots || !image.spec.missingSlots.length) continue; + // 拿用户给的图直接做动图:这张图不会再画,槽位缺不缺无所谓。 + if (isAnimation(image) && usesGivenReference(session, image)) continue; deps.emit(session, 'plan', image.id + ' 缺失必填槽位:' + image.spec.missingSlots.join('、') + ',已用兜底值', { level: 'warn', detail: detail('槽位校验', [ @@ -1034,9 +3485,46 @@ async function planNode(session, deps) { ...session.images.map((image) => group(image.id + ' · ' + image.title, [ kv('入口', image.spec.entry === 'edit' ? '带参考图编辑' : '文生图'), + kv('难度', difficultyText(image)), kv('尺寸', image.spec.area + ' · ' + image.spec.aspectRatio), kv('参考图', image.spec.refs.length ? image.spec.refs.join('、') : '无'), + (image.spec.qrCodes || []).length + ? kv( + '二维码', + image.spec.qrCodes + .map((item) => + item.content + ? '「' + item.content + '」' + (item.verbatim ? '(逐字核对通过,出图后贴真码)' : '(原话里找不到,不贴真码)') + : '没给内容(装饰码)', + ) + .join(';'), + ) + : null, image.blocked ? kv('阻塞原因', image.blockReason) : null, + image.spec.animation + ? kv( + '动图', + image.spec.animation.frames.length + ' 帧,每帧 ' + image.spec.animation.frameMs + ' 毫秒,' + + (transitionOf(image.spec.animation) === 'cut' ? '帧间直接切换,' : '帧间渐变,') + + (usesGivenReference(session, image) + ? '直接从 ' + image.spec.animation.reference + ' 改出各帧' + : '先画参考图,你确认后再出各帧'), + ) + : null, + image.spec.motionGif + ? kv( + '程序转圈 GIF', + (image.spec.motionGif.spin.steps ? '一圈 ' + image.spec.motionGif.spin.steps + ' 格、一格一格跳' : '连续转') + + ',图画好、评审通过后由程序绕圆心转出各帧,不另外生图', + ) + : null, + image.spec.panelGif + ? kv( + '组图拼 GIF', + (image.spec.panelGif.rows && image.spec.panelGif.cols ? image.spec.panelGif.cols + ' 列 ' + image.spec.panelGif.rows + ' 行,' : '') + + '组图画好、评审通过后由程序切格拼成 GIF,不另外生图', + ) + : null, image.spec.missingSlots && image.spec.missingSlots.length ? kv('槽位告警', '缺失必填槽位 ' + image.spec.missingSlots.join('、') + ',已用兜底值') : null, @@ -1068,27 +3556,37 @@ async function planNode(session, deps) { function normalizePlan(data, session) { const list = Array.isArray(data.images) ? data.images : []; const used = new Set(); + const sources = qrSources(session); return list.map((raw, index) => { let id = String(raw.id || String.fromCharCode(65 + index)).toUpperCase().slice(0, 4); while (used.has(id)) id += '1'; used.add(id); const refs = Array.isArray(raw.refs) ? raw.refs.filter(Boolean) : []; const textLayers = Array.isArray(raw.text_layers) ? raw.text_layers : []; + const qrCodes = normalizeQrCodes(raw.qr_codes, sources); const subjectText = [raw.title, raw.prompt, raw.slots && raw.slots.subject, raw.slots && raw.slots.scene] .filter(Boolean) .join(' '); - const checklist = normalizeChecklist(raw.checklist, textLayers, subjectText); + const checklist = normalizeChecklist(raw.checklist, textLayers, subjectText, qrCodes); const questions = (Array.isArray(raw.questions) ? raw.questions : []) .map((item) => (typeof item === 'string' ? item : (item && (item.text || item.question)) || '')) .filter(Boolean); const hasSlots = Boolean(raw.slots && typeof raw.slots === 'object'); const slots = hasSlots ? normalizeSlots(raw.slots, textLayers) : null; - const composed = composePrompt(slots, raw.prompt, session.instruction, hasSlots); + const composed = composePrompt(slots, raw.prompt, session.instruction, hasSlots, qrCodes); + const textOverlay = normalizeTextOverlay( + raw.text_overlay, + textLayers, + [session.instruction, raw.title, slots && slots.style, slots && slots.subject].filter(Boolean).join(' '), + ); + const chartPlan = normalizeCharts(raw.charts); + const charts = chartsWithCheck(chartPlan.charts, sources); return { id, title: raw.title || '图 ' + id, blocked: Boolean(raw.blocked) || questions.length > 0, blockReason: questions.join(';'), + chartProblems: chartPlan.problems, spec: { slug: normalizeSlug(raw.slug, id.toLowerCase()), entry: raw.entry === 'edit' || refs.length ? 'edit' : 't2i', @@ -1101,11 +3599,23 @@ function normalizePlan(data, session) { size: raw.size || '', refs, textLayers, - checklist, + qrCodes, + textOverlay, + basePrompt: '', + animation: normalizeAnimation(raw.animation), + // 动图、程序转圈、组图拼 GIF 只取一种,按这个顺序:规划写了好几个,取前面的。切换时弹一下只在用户原话里点名才加(见 editPanelGif)。 + motionGif: normalizeAnimation(raw.animation) ? null : normalizeMotionGif(raw.motion_gif), + panelGif: normalizeAnimation(raw.animation) || normalizeMotionGif(raw.motion_gif) ? null : withoutUnaskedBounce(normalizePanelGif(raw.panel_gif), session.instruction), + charts, + // 规划判的难度:决定后面各节点的工具调用上限(见 budget.mjs)。没写就按规格推断。 + difficulty: normalizeDifficulty(raw.difficulty), + difficultyReason: String(raw.difficulty_reason || '').trim().slice(0, 60), + checklist: withWhiteCheck(chartChecklist(checklist, charts), wantsWhite(session, { spec: { slots: composed.slots } })), }, rounds: [], status: 'pending', chosen: null, + textMode: '', }; }); } @@ -1117,10 +3627,8 @@ function normalizePlan(data, session) { // 超过上限的部分合并成一条聚合项,仍然可判定,但只占一个名额。 const MAX_TEXT_HARD = 3; -function normalizeChecklist(checklist, textLayers, subjectText) { - const items = Array.isArray(checklist) ? checklist : []; - // 文字层是用户明确点名的可验证项,排在最前面,保证不会被 hard 上限截掉; - // 但数量必须封顶,否则它会把 planner 写的语义要求全部挤出去。 +// 文字层对应的 T 项:前 MAX_TEXT_HARD 条一条一项,其余合并成一条聚合项。 +function textChecks(textLayers) { const out = []; const layers = []; for (const layer of Array.isArray(textLayers) ? textLayers : []) { @@ -1147,10 +3655,20 @@ function normalizeChecklist(checklist, textLayers, subjectText) { verify: 'vlm', }); } + return out; +} + +function normalizeChecklist(checklist, textLayers, subjectText, qrCodes) { + const items = Array.isArray(checklist) ? checklist : []; + // 文字层是用户明确点名的可验证项,排在最前面,保证不会被 hard 上限截掉; + // 但数量必须封顶,否则它会把 planner 写的语义要求全部挤出去。 + const out = textChecks(textLayers); for (const item of items) { if (!item || !item.text) continue; const text = String(item.text); if (out.some((existing) => existing.text === text)) continue; + // H-qr 只能由程序按 qr_codes 挂上:评审看不出码能不能扫,规划自己写的同名条目一律不收。 + if (String(item.id || '') === 'H-qr') continue; out.push({ id: String(item.id || (item.kind === 'soft' ? 'S' : 'H') + (out.length + 1)), kind: item.kind === 'soft' ? 'soft' : 'hard', @@ -1188,7 +3706,8 @@ function normalizeChecklist(checklist, textLayers, subjectText) { && !limited.some((item) => item.id === ANATOMY_CHECK.id)) { limited.push({ ...ANATOMY_CHECK }); } - return limited; + // 二维码同样挂在裁剪之后:它是程序实测的硬项,被降级成 soft 就等于不验。 + return withQrCheck(limited, qrCodes); } // 交付文件名:H3-Image_<内容英文简写>_<时间戳>.<ext>。 @@ -1240,6 +3759,9 @@ const RED_ABS_LIMIT = 1.08; // 偏红的图,下一轮的父图本身就是红的,相对漂移恒为 0,检测会彻底失效(roundbase 回归实测)。 // 1.2 高于常见「刻意暖调」的调色量级,日落、烛光这类题材不会被误伤。 const RED_HARD_LIMIT = 1.2; +// 接近中性的像素至少占这么多,才拿它们来区分「整体偏红」和「主体本身是红的」。 +// 真机 20261112-23b706(暖光产品照)8 张候选全被判偏红;橘猫吉祥物的 GIF 参考图同样每张都被打回。 +const NEUTRAL_MIN_SHARE = 0.05; const COLOR_HARD_CHECK = { id: 'H-color', kind: 'hard', @@ -1255,11 +3777,15 @@ const COLOR_HARD_CHECK = { // ---------------------------------------------------------------- 04/05/06 单图循环 async function runImage(session, image, deps) { - const maxRefine = session.route === 'fast' ? DEFAULTS.maxRefineFast : DEFAULTS.maxRefine; + let maxRefine = session.route === 'fast' ? DEFAULTS.maxRefineFast : DEFAULTS.maxRefine; + // 评审判出字的位置有问题(压线、出界、该在色块里却在外面)时,在同一张底图上重新排字:不调生图接口, + // 几秒钟就好,所以不占生图的轮次;每张图最多两次,免得来回挪个没完。 + let textFixes = 0; + const maxTextFixes = 2; image.status = 'running'; let action = image.pendingAction || { type: image.spec.entry === 'edit' ? 'edit' : 'generate', - prompt: image.spec.prompt, + prompt: promptFor(image), refs: image.spec.refs, parentId: null, }; @@ -1270,6 +3796,24 @@ async function runImage(session, image, deps) { // 偏红重画单独计数:它不算「没改善」,不该把 stale 判定和轮次预算搅乱。 let colorRetries = 0; const maxColorRetries = session.route === 'fast' ? 1 : 2; + // 二维码没贴成的重画同理单独计数。 + let qrRetries = 0; + const maxQrRetries = session.route === 'fast' ? 1 : 2; + const wantsQr = qrContents(image.spec).length > 0; + // 程序排字时底图没留出空白的重画,同样单独计数。 + let textRetries = 0; + const maxTextRetries = session.route === 'fast' ? 1 : 2; + let chartRetries = 0; + const maxChartRetries = session.route === 'fast' ? 1 : 2; + // 白纸、白底偏色的重画也单独计数。 + let whiteRetries = 0; + const maxWhiteRetries = session.route === 'fast' ? 1 : 2; + const warm = imageWarm(session, image); + // 接口取不到送进去的图的次数,见下面的 isUnreachableMedia。 + let unreachable = 0; + // 生图接口临时故障(网关 5xx、断连)的轮数。h3.mjs 每次请求已经退避重试过两次,走到这里说明故障还没过去。 + let outages = 0; + image.interrupted = ''; let lastActionType = action.type; for (let round = 0; round <= maxRefine; round += 1) { @@ -1280,18 +3824,26 @@ async function runImage(session, image, deps) { const requestedParentId = action.parentId || null; if (action.type === 'edit') { action.parentId = parent ? parent.id || action.parentId : null; - action.parentPath = parent ? parent.path : null; + action.parentPath = parent ? parentImagePath(image, parent) : null; action.parentStats = (parent && parent.program && parent.program.stats) || action.parentStats || null; } + const retype = action.type === 'text'; + if (!retype) action = ensureChartClause(ensureTextClause(ensureQrClause(action, parent, image), image), image); + action.overlay = isOverlay(image); const record = { index: round, action: summarizeAction(action), candidates: [], startedAt: Date.now() }; image.rounds.push(record); - deps.emit(session, 'paint', image.id + ' 第 ' + (round + 1) + ' 轮:' + describeAction(action), { + const roundAnchor = action.type === 'edit' ? anchorFor(image, parent) : null; + deps.emit(session, 'paint', image.id + ' 第 ' + (round + 1) + ' 轮:' + describeAction(action) + (roundAnchor ? ',带色彩基准 ' + roundAnchor.id : ''), { detail: detail('本轮动作', [ - kv('类型', action.type !== 'edit' ? '重新生成' : parent ? '在已有图上编辑' : '按参考图生成'), - kv('底图', action.type !== 'edit' ? '无' : parent ? parent.id || '上一张' : '没找到,已退化成按参考图生成'), - kv('参考图', (action.refs || []).join('、') || '无'), - kv('候选数', session.route === 'fast' ? 1 : DEFAULTS.candidates), - text('送进模型的 prompt', action.prompt), + kv('类型', retype ? '只重新排字(不重新生图)' : action.type !== 'edit' ? '重新生成' : parent ? '在已有图上编辑' : '按参考图生成'), + kv('底图', retype ? (action.parentId || '-') + ' 不带字的底图' : action.type !== 'edit' ? '无' : parent ? parent.id || '上一张' : '没找到,已退化成按参考图生成'), + roundAnchor ? kv('色彩基准', roundAnchor.id + '(作为第 2 张图送进去,只对照颜色)') : null, + retype ? list('挪动的段落', (action.textPatches || []).map((patch) => ({ text: '第 ' + patch.index + ' 段 → ' + JSON.stringify(patch.box || patch.align) }))) : null, + retype ? null : kv('参考图', (action.refs || []).join('、') || '无'), + retype ? null : kv('候选数', session.route === 'fast' ? 1 : DEFAULTS.candidates), + isOverlay(image) && !retype ? kv('文字', '这一轮画不带字的底图,文字由程序按原文排进去') : null, + hasCharts(image) && !retype ? kv('图表', '这一轮画留出空白面板的底图,图表由程序按数据画进去') : null, + retype ? null : text('送进模型的 prompt', action.prompt), ]), }); @@ -1302,16 +3854,16 @@ async function runImage(session, image, deps) { record.error = describeError(error); image.failReason = record.error; deps.emit(session, 'paint', image.id + ' 生成失败:' + record.error, { level: 'error' }); + // 配置类错误(缺 Key、401/403、模型名不对)重试没用,立刻停下;但前面几轮已经生成好的图照样交付。 + // 以前这里直接判死,手上的图一张不交(真机 20261112-23b706 / 463fcd 各丢了 8 张、4 张)。 if (isFatal(error)) { - session.status = 'failed'; session.fatal = record.error; - image.status = 'failed'; + await stopWithExisting(session, image, deps, record.error); return; } // 安全审核拒绝没有重试价值:prompt 一个字没改,再送 5 次也是同样的 422。 // 立刻停手,把接口原文和「怎么改」一起顶到对话层,让用户改需求重来。 if (isContentPolicy(error)) { - image.status = 'failed'; image.contentPolicy = true; deps.emit(session, 'paint', image.id + ' 被内容安全审核拒绝,已停止重试', { level: 'error', @@ -1328,10 +3880,42 @@ async function runImage(session, image, deps) { session.questions.push( image.id + ' 被内容安全审核拒绝,需要改掉触发审核的描述后重画:' + record.error, ); + await stopWithExisting(session, image, deps, '后面这一轮被内容安全审核拒绝'); return; } + // 接口取不到送进去的图(media url unreachable):同样的请求再送多少次都一样。现在送进去的 + // 只有自己上传的文件,这类错误多半是接口那边一时取不到:再试一次,还不行就停手交给选优, + // 不要像以前那样原样重试到轮次用完(真机 20261112-8a442a 连败 6 轮,交付 0 张)。 + if (isUnreachableMedia(error)) { + unreachable += 1; + if (unreachable >= 2) { + deps.emit(session, 'paint', image.id + ' 接口连续两次取不到送进去的图,停止重试', { level: 'error' }); + image.interrupted = '生图接口连续两次取不到送进去的图'; + break; + } + } + // 生图接口临时故障,而且 h3.mjs 里已经退避重试过两次:手上已经有图就不再出新图,先把它们交出去; + // 一张都还没有,就再等一会儿试一轮,还不行才停。 + if (isTransient(error)) { + outages += 1; + const reason = '生图接口暂时不可用(' + shortError(record.error) + ')'; + if (hasCandidates(image)) { + deps.emit(session, 'paint', image.id + ' ' + reason + ',重试仍失败,先交付已经生成好的图', { level: 'warn' }); + image.interrupted = reason; + break; + } + if (outages >= 2 || round >= maxRefine) { + deps.emit(session, 'paint', image.id + ' ' + reason + ',连续两轮都没恢复,停止重试', { level: 'error' }); + break; + } + deps.emit(session, 'paint', image.id + ' ' + reason + ',' + Math.round(DEFAULTS.outageWaitMs / 1000) + ' 秒后再试', { + level: 'warn', + }); + await waitUnlessCancelled(session, DEFAULTS.outageWaitMs); + if (isCancelled(session)) return; + } if (round >= maxRefine) break; - action = { type: 'generate', prompt: image.spec.prompt, refs: image.spec.refs, parentId: null }; + action = { type: 'generate', prompt: promptFor(image), refs: image.spec.refs, parentId: null }; continue; } record.candidates = candidates; @@ -1350,6 +3934,7 @@ async function runImage(session, image, deps) { let judgeStartedAt = Date.now(); try { await Promise.all(candidates.map((candidate) => programChecks(session, image, candidate, deps))); + noteColorAnchor(session, image, candidates, deps); // 全部候选都被程序判成偏红时,没必要再花两三分钟让评审看一遍——它也看不出红。 // 直接重画一轮,并把「上一轮偏红」写进 prompt。评审节点照样会在别的失败上跑。 const redAll = @@ -1367,27 +3952,139 @@ async function runImage(session, image, deps) { // 那种因为 entry 被判成 edit、实际没有底图的轮次必须回到重新生成—— // 在刚生成的偏红图上做全图 edit 只会继续累积偏色。 action = requestedParentId - ? { ...action, prompt: withColorRetry(action.prompt) } - : { type: 'generate', prompt: withColorRetry(image.spec.prompt), refs: image.spec.refs, parentId: null }; + ? { ...action, prompt: withColorRetry(action.prompt, warm) } + : { type: 'generate', prompt: withColorRetry(promptFor(image), warm), refs: image.spec.refs, parentId: null }; + continue; + } + // 白纸、白底全部偏色:H-white 是硬项,评审看完也只能打回。直接带着偏的方向重画; + // 编辑轮还在同一张父图上改,但会带上色彩基准图(见 executeAction),不会越改越偏。 + const whiteAll = candidates.length > 0 && candidates.every((candidate) => candidate.program && candidate.program.whiteCast); + if (whiteAll && whiteRetries < maxWhiteRetries && round < maxRefine) { + whiteRetries += 1; + const label = candidates[0].program.whiteCast; + record.verdict = { note: '全部候选的白色纸面' + label + ',跳过评审直接重画' }; + deps.emit( + session, + 'refine', + image.id + ' 第 ' + (round + 1) + ' 轮候选的白色纸面全部' + label + ',跳过评审直接重画(' + whiteRetries + '/' + maxWhiteRetries + ')', + { level: 'warn' }, + ); + action = requestedParentId + ? { ...action, prompt: withWhiteRetry(action.prompt, label) } + : { type: 'generate', prompt: withWhiteRetry(promptFor(image), label), refs: image.spec.refs, parentId: null }; + continue; + } + // 全部候选的二维码都没贴成(找不到码、码太小、贴上扫不出):H-qr 是硬项,评审看完也只能打回, + // 而原因程序已经知道了。直接带着原因重画,把评审的两三分钟省下来。 + const qrAll = + wantsQr && + candidates.length > 0 && + candidates.every((candidate) => candidate.program && candidate.program.qr && candidate.program.qr.status === 'failed'); + if (qrAll && qrRetries < maxQrRetries && round < maxRefine) { + qrRetries += 1; + const why = qrFailText(candidates[0].program.qr); + // 送进模型的只要原因本身,像素数对生图模型没有意义。 + const whyForModel = dedupe( + candidates[0].program.qr.items.filter((item) => !item.ok).map((item) => item.reasonText), + ).join(';'); + record.verdict = { note: '全部候选的二维码都没换成真码,跳过评审直接重画' }; + deps.emit( + session, + 'refine', + image.id + ' 第 ' + (round + 1) + ' 轮候选的二维码都没换成真码(' + why + '),跳过评审直接重画(' + qrRetries + '/' + maxQrRetries + ')', + { level: 'warn' }, + ); + action = requestedParentId + ? { ...action, prompt: withQrRetry(action.prompt, whyForModel) } + : { type: 'generate', prompt: withQrRetry(promptFor(image), whyForModel), refs: image.spec.refs, parentId: null }; continue; } + // 程序排字:全部候选的底图都没给文字留出空白(或者留的地方排不下),H-text 是硬项,评审看完也只能打回。 + // 直接带着原因重画。字体缺字这类和底图无关的问题,重画没用,退回让模型直接写字。 + const textFailed = + isOverlay(image) && + candidates.length > 0 && + candidates.every((candidate) => candidate.program && candidate.program.text && candidate.program.text.status === 'failed'); + if (textFailed) { + const fontProblem = candidates.every((candidate) => + (candidate.program.text.items || []).some((item) => ['no_font', 'missing_glyphs'].includes(item.reason)), + ); + if (fontProblem && round < maxRefine) { + const why = textFailText(candidates[0].program.text); + leaveOverlay(image); + record.verdict = { note: '程序排不了这段字(' + why + '),改回让模型直接写' }; + deps.emit(session, 'refine', image.id + ' 程序排不了这段字(' + why + '),改回让模型直接写字', { level: 'warn' }); + action = { type: 'generate', prompt: image.spec.prompt, refs: image.spec.refs, parentId: null }; + continue; + } + if (!fontProblem && textRetries < maxTextRetries && round < maxRefine) { + textRetries += 1; + const why = textFailText(candidates[0].program.text); + const whyForModel = dedupe( + candidates[0].program.text.items.filter((item) => !item.ok).map((item) => item.reasonText), + ).join(';'); + record.verdict = { note: '全部候选的底图都没给文字留出空白,跳过评审直接重画' }; + deps.emit( + session, + 'refine', + image.id + ' 第 ' + (round + 1) + ' 轮候选的字没排上(' + why + '),跳过评审直接重画(' + textRetries + '/' + maxTextRetries + ')', + { level: 'warn' }, + ); + action = requestedParentId + ? { ...action, prompt: withTextRetry(action.prompt, whyForModel) } + : { type: 'generate', prompt: withTextRetry(promptFor(image), whyForModel), refs: image.spec.refs, parentId: null }; + continue; + } + } + // 图表同理:全部候选的底图都没给图表留出空白面板(或者留的地方太小),H-chart 是硬项,评审看完也只能打回。 + const chartFailed = + hasCharts(image) && + candidates.length > 0 && + candidates.every((candidate) => candidate.program && candidate.program.chart && candidate.program.chart.status === 'failed'); + if (chartFailed && chartRetries < maxChartRetries && round < maxRefine) { + const fixable = candidates.some((candidate) => + (candidate.program.chart.items || []).some((item) => ['not_blank', 'marks', 'too_small'].includes(item.reason)), + ); + if (fixable) { + chartRetries += 1; + const why = chartFailText(candidates[0].program.chart); + const whyForModel = dedupe(candidates[0].program.chart.items.filter((item) => !item.ok).map((item) => item.reasonText)).join(';'); + record.verdict = { note: '全部候选的底图都没给图表留出空白面板,跳过评审直接重画' }; + deps.emit( + session, + 'refine', + image.id + ' 第 ' + (round + 1) + ' 轮候选的图表没画上(' + why + '),跳过评审直接重画(' + chartRetries + '/' + maxChartRetries + ')', + { level: 'warn' }, + ); + action = requestedParentId + ? { ...action, prompt: withChartRetry(action.prompt, whyForModel) } + : { type: 'generate', prompt: withChartRetry(promptFor(image), whyForModel), refs: image.spec.refs, parentId: null }; + continue; + } + } judgeStartedAt = Date.now(); - verdict = await judgeNode(session, image, record, deps); + verdict = await judgeWithRetry(session, image, record, deps); + if (!verdict) { + // 评审等了一会儿重来还是没跑成:这批图本身是好好的,别扔掉去重画(以前就是这样撞上接口故障、 + // 最后一张不交的)。不再出新图,按程序校验结果和已有的评审结论交给选优。 + record.verdict = { note: '评审没跑成,这批图未经评审,按程序校验结果交给选优' }; + image.interrupted = '评审没跑成(模型服务暂时不可用),这批图未经评审'; + break; + } record.verdict = verdict.summary; } catch (error) { const message = describeError(error); record.error = message; deps.emit(session, 'verify', image.id + ' 第 ' + (round + 1) + ' 轮评审异常:' + message, { level: 'error' }); if (isFatal(error)) { - session.status = 'failed'; session.fatal = message; - image.status = 'failed'; + await stopWithExisting(session, image, deps, message); return; } // 用户主动停的,别再自顾自重试。 if (session.status === 'cancelled') return; if (round >= maxRefine) break; - action = { type: 'generate', prompt: image.spec.prompt, refs: image.spec.refs, parentId: null }; + action = { type: 'generate', prompt: promptFor(image), refs: image.spec.refs, parentId: null }; continue; } const bestVerdict = verdict.best ? verdict.best.verdict : null; @@ -1458,6 +4155,33 @@ async function runImage(session, image, deps) { return; } + // 兜底:模型直接写的字在这一轮的每张候选上都被评审判成写错、漏字(T 项 fail),换成无字底图 + 程序排字。 + // 再让模型重画同一段字多半还是残的(真机 20261113-01b902)。不知道字该放哪的短字不切(见 canPlaceText)。 + if (!isOverlay(image) && canPlaceText(image) && round < maxRefine && textBrokenEverywhere(candidates)) { + const failedIds = dedupe( + candidates.flatMap((candidate) => ((candidate.verdict && candidate.verdict.hardFailed) || []).filter((id) => /^T\d+$/.test(id))), + ); + const note = failedIds + .map((id) => (verdict.best && verdict.best.verdict.checks[id] && verdict.best.verdict.checks[id].note) || '') + .filter(Boolean) + .join(';'); + enterOverlay(session, image, '第 ' + (round + 1) + ' 轮模型写的字画残了' + (note ? '(' + note + ')' : '')); + deps.emit(session, 'refine', image.id + ' 第 ' + (round + 1) + ' 轮模型写的字画残了,改成先画不带字的底图、文字由程序按原文排进去', { + level: 'warn', + detail: detail('切换到程序排字 · ' + image.id, [ + kv('原因', image.textReason), + text('不带字的底图 prompt(送进模型)', image.spec.basePrompt), + ]), + }); + record.verdict = { ...record.verdict, note: (record.verdict && record.verdict.note ? record.verdict.note + ';' : '') + '文字画残,改用程序排字' }; + // 清单换了(T 项改由程序验),之前的 hard 通过数不再可比。 + bestHard = -1; + staleRounds = 0; + lastActionType = 'generate'; + action = { type: 'generate', prompt: promptFor(image), refs: image.spec.refs, parentId: null }; + continue; + } + if (hardPassed > bestHard) { bestHard = hardPassed; staleRounds = 0; @@ -1468,7 +4192,8 @@ async function runImage(session, image, deps) { deps.emit(session, 'refine', image.id + ' 连续 ' + staleRounds + ' 轮没有改善,提前停下并交给选优'); break; } - if (round >= maxRefine) break; + // 生图轮次用完了,但评审建议的是只重新排字:不调生图接口,照样可以做。 + if (round >= maxRefine && !(verdict.next && verdict.next.type === 'text' && isExecutable(verdict.next, image) && textFixes < maxTextFixes)) break; let next = verdict.next; if (!next || !isExecutable(next, image)) { @@ -1481,19 +4206,76 @@ async function runImage(session, image, deps) { if (staleRounds >= DEFAULTS.staleActionRounds && next.type === lastActionType) { deps.emit(session, 'refine', image.id + ' 连续没有改善,强制换一种改法'); next = (await safeRefine(session, image, record, verdict, deps, true)) || next; + // 强制换改法时修正节点也可能说放弃:不判断的话「放弃」会被当成一个动作,落到生图分支里去。 + if (next.type === 'give_up') { + deps.emit(session, 'refine', image.id + ' 修正节点建议放弃,进入选优'); + break; + } + } + if (next.type === 'text') { + if (textFixes >= maxTextFixes) { + deps.emit(session, 'refine', image.id + ' 已经在原图上重新排过 ' + maxTextFixes + ' 次字,进入选优'); + break; + } + textFixes += 1; + maxRefine += 1; } if (next.type === 'edit') { fullEdits += 1; if (fullEdits > DEFAULTS.maxFullEdits) { deps.emit(session, 'refine', image.id + ' 全图 edit 已达上限 ' + DEFAULTS.maxFullEdits + ' 次,改走重新生成'); - next = { type: 'generate', prompt: next.prompt || image.spec.prompt, refs: image.spec.refs, parentId: null }; + next = { type: 'generate', prompt: next.prompt || promptFor(image), refs: image.spec.refs, parentId: null }; } } lastActionType = next.type; action = next; } - await selectBestEffort(session, image, deps); + await selectBestEffort(session, image, deps); +} + +// 修正节点、增量编辑节点都会自己重写 prompt,经常把二维码的要求丢掉,于是模型画出一个歪的、 +// 被挡住的码,程序找不到就只能打回。每轮出图前统一补一次:底图上已经是真码时要求保持不动, +// 其余情况要求画一个规整的占位码。 +function ensureQrClause(action, parent, image) { + if (!((image.spec && image.spec.qrCodes) || []).length) return action; + const prompt = String(action.prompt || '').trim(); + const keep = + action.type === 'edit' && + parent && + parent.program && + parent.program.qr && + parent.program.qr.status === 'real'; + const clause = keep ? QR_KEEP_CLAUSE : QR_CLAUSE; + if (prompt.includes(clause)) return action; + return { ...action, prompt: prompt + (prompt && !/[。.!!??]$/.test(prompt) ? '。' : '') + clause }; +} + +// 程序排字模式下,修正节点、评审写的指令同样会把「这块留空」丢掉,模型就又在载体上写满伪文字。每轮统一补一次。 +function ensureTextClause(action, image) { + if (!isOverlay(image)) return action; + const prompt = String(action.prompt || '').trim(); + if (prompt.includes(BLANK_MARK)) return action; + return { + ...action, + prompt: prompt + (prompt && !/[。.!!??]$/.test(prompt) ? '。' : '') + blankClause(overlayLayers(image)), + }; +} + +// 编辑要送进模型的底图。程序排字、程序画图表的图送不带字、不带图表的那张:送画好的图,模型会照着重描一遍, +// 字又写残了,柱子又画歪了。 +function parentImagePath(image, parent) { + if (!parent) return null; + return (isOverlay(image) || hasCharts(image)) && parent.basePath ? parent.basePath : parent.path; +} + +// 这一轮每张评过的候选,模型写的字都被判成写错(T 项 fail)。 +function textBrokenEverywhere(candidates) { + const judged = (candidates || []).filter((candidate) => candidate && candidate.verdict); + return ( + judged.length > 0 && + judged.every((candidate) => (candidate.verdict.hardFailed || []).some((id) => /^T\d+$/.test(id))) + ); } // 修正节点也会抛(agent turn 被判 error / 回复不是 JSON)。它挂了不代表这一轮没救, @@ -1517,19 +4299,7 @@ async function selectBestEffort(session, image, deps) { deps.emit(session, 'select', image.id + ' 选优节点异常:' + describeError(error), { level: 'error' }); } if (image.chosen) return; - let best = null; - for (const round of image.rounds) { - for (const candidate of round.candidates || []) { - if (!candidate || !candidate.path) continue; - if (!best) { - best = candidate; - continue; - } - const av = candidate.verdict || { hardPassed: 0, quality: 0 }; - const bv = best.verdict || { hardPassed: 0, quality: 0 }; - if (av.hardPassed > bv.hardPassed || (av.hardPassed === bv.hardPassed && av.quality > bv.quality)) best = candidate; - } - } + const best = rankCandidates(allCandidates(image))[0] || null; if (!best) { image.status = 'failed'; const why = lastFailReason(image); @@ -1550,6 +4320,106 @@ async function selectBestEffort(session, image, deps) { deps.emit(session, 'select', image.id + ' 选优节点异常,回退交付:' + best.id, { level: 'warn' }); } +function allCandidates(image) { + const out = []; + for (const round of image.rounds || []) { + for (const candidate of round.candidates || []) if (candidate && candidate.path) out.push(candidate); + } + return out; +} + +function hasCandidates(image) { + return allCandidates(image).length > 0; +} + +// 没有模型可问时怎么排候选:程序硬项有失败的(解不开、偏红、二维码没换成)排最后,这些是确定的毛病; +// 其余的里,评过的按 hard 通过数、质量分排在没评过的前面;再一样就取后出的(后面的轮次通常是修过的)。 +function rankCandidates(list) { + const score = (candidate) => { + const programFails = (candidate.checks || []).filter((check) => check.kind === 'hard' && check.result === 'fail').length; + const verdict = candidate.verdict; + return [ + programFails ? 0 : 1, + verdict ? 1 : 0, + verdict ? Number(verdict.hardPassed) || 0 : 0, + verdict ? Number(verdict.quality) || 0 : 0, + Number(candidate.round) || 0, + Number(candidate.createdAt) || 0, + ]; + }; + return list.slice().sort((a, b) => { + const sa = score(a); + const sb = score(b); + for (let i = 0; i < sa.length; i += 1) if (sa[i] !== sb[i]) return sb[i] - sa[i]; + return 0; + }); +} + +// 出图中途停下(配置错误、审核拒绝、接口或模型服务一直不可用):手上已经有候选就交给选优交付, +// 交付卡片上写明是停下前的图;一张都没有才判失败。 +async function stopWithExisting(session, image, deps, why) { + image.interrupted = why; + if (!hasCandidates(image)) { + image.status = 'failed'; + image.failReason = why; + return; + } + deps.emit(session, 'select', image.id + ' 出图中途停下(' + shortError(why) + '),先交付已经生成好的图', { level: 'warn' }); + await selectBestEffort(session, image, deps); +} + +// 评审回合没跑成:等一会儿再评同一批图。还不行返回 null,由上层交给选优,不扔掉这批图去重画。 +// 配置类错误(Agent 权限被关掉)和用户点停止照常往上抛。 +async function judgeWithRetry(session, image, record, deps) { + try { + return await judgeNode(session, image, record, deps); + } catch (error) { + if (isFatal(error) || isCancelled(session)) throw error; + deps.emit( + session, + 'verify', + image.id + ' 评审没跑成(' + shortError(describeError(error)) + '),' + Math.round(DEFAULTS.judgeRetryWaitMs / 1000) + ' 秒后重新评审同一批图', + { level: 'warn' }, + ); + } + await waitUnlessCancelled(session, DEFAULTS.judgeRetryWaitMs); + if (isCancelled(session)) return null; + try { + return await judgeNode(session, image, record, deps); + } catch (error) { + if (isFatal(error) || isCancelled(session)) throw error; + deps.emit(session, 'verify', image.id + ' 重新评审也没跑成:' + shortError(describeError(error)) + '。这批图不再重画,交给选优', { + level: 'error', + }); + return null; + } +} + +// 等一段时间,期间用户点了停止就提前返回。 +async function waitUnlessCancelled(session, ms) { + const until = Date.now() + Math.max(0, Number(ms) || 0); + while (!isCancelled(session) && Date.now() < until) { + await new Promise((resolve) => setTimeout(resolve, Math.min(500, Math.max(1, until - Date.now())))); + } +} + +// 时间线上用的短原因:网关的错误页、长串堆栈都截掉。 +function shortError(message) { + const value = String(message || ''); + const status = /HTTP (\d{3})/.exec(value); + if (status && /Bad Gateway|<html/i.test(value)) return 'HTTP ' + status[1]; + return value.replace(/\s+/g, ' ').slice(0, 80); +} + +// 服务端的临时故障:网关 5xx、限流、断连、模型服务把回合判成 error。等一等可能就好了,不能当成配置错误判死。 +function isTransient(error) { + if (!error) return false; + if (error.transient) return true; + return /HTTP (429|500|502|503|504)\b|Bad Gateway|Service Unavailable|Gateway Time-?out|fetch failed|ECONNRESET|ECONNREFUSED|socket hang up|TURN_FAILED/i.test( + describeError(error), + ); +} + // Host 回来的 error 常常是 { code, message } 这种对象,直接 String() 会变成 [object Object], // 排查时什么都看不到(实测过一次 agent_error:[object Object],完全不知道出了啥)。 // 统一在这里展开成「code:message」。 @@ -1592,6 +4462,9 @@ function isFatal(error) { // 用户在插件详情里关掉了 Agent 权限:后面每个节点都会以同样的原因失败, // 继续跑只会白烧生图请求,必须当场停下,让页面显示去哪里重新打开。 if (error && error.code === 'NOT_PERMITTED') return true; + // 网关 5xx、断连这类临时故障不算:以前「参考图上传失败:HTTP 502」也被当成致命错误, + // 一次网关故障就让会话判死(真机 20261112-23b706 / 463fcd)。上传失败只有请求本身不对时才致命。 + if (isTransient(error)) return false; const message = describeError(error); // invalid params / unknown model 是请求本身不对(模型名下线、参数非法),重试多少次都一样。 // 不当致命错误处理的话,会一路重试到 maxRefine,白烧十几次请求再交付 0 张图。 @@ -1607,6 +4480,11 @@ function isContentPolicy(error) { ); } +// 接口说取不到某张输入图(HTTP 400 · media url unreachable,错误码 2013)。 +function isUnreachableMedia(error) { + return /media url unreachable|\(2013\)/i.test(describeError(error)); +} + // 图没出来时要能说清「为什么」。优先报最后一次真实报错, // 一条报错都没有(比如候选全被程序判死)才回落到笼统说法。 function lastFailReason(image) { @@ -1626,15 +4504,69 @@ function isExecutable(next, image) { if (next.parentId) return true; return Boolean(image.rounds.length); } + if (next.type === 'text') return isOverlay(image) && Boolean(next.parentId) && Boolean((next.textPatches || []).length); return next.type === 'give_up'; } +// ---------------------------------------------------------------- 色彩基准 +// +// 编辑叠编辑时偏色会一轮一轮累积:每一轮都只偏一点、都在门槛以下,三轮下来白纸就发紫了 +// (真机 20261116-41535c:纸面 (253,253,253) → (239,239,240) → (242,242,245) → (245,242,248))。 +// 所以每张图记下第一张颜色检测合格的候选当色彩基准,之后每次在别的候选上编辑,都把它当第 2 张图一起送进去。 +// 用户明确要改颜色、光线的那次追问不带(见 runIncrementalEdit),改完以后重新选。 +const COLOR_CHANGE_RE = + /颜色|色调|配色|色彩|暖色|冷色|调暖|调冷|偏暖|偏冷|变暖|变冷|暖一点|冷一点|亮一点|暗一点|调亮|调暗|变亮|变暗|夜景|夜晚|黄昏|傍晚|日落|夕阳|清晨|灯光|光线|打光|逆光|滤镜|黑白|饱和|白天|阴天|雨天|雪景|(?:换|改)成.{0,6}色/; + +function colorAccurate(candidate) { + const program = candidate && candidate.program; + if (!program || program.error || program.redCast || program.whiteCast) return false; + return (candidate.checks || []).every((check) => !['H-color', 'H-white', 'P-decode'].includes(check.id) || check.result === 'pass'); +} + +function noteColorAnchor(session, image, candidates, deps) { + if (image.colorAnchor && image.colorAnchor.path && existsSync(image.colorAnchor.path)) return; + const good = (candidates || []).find((candidate) => colorAccurate(candidate)); + if (!good) return; + // 程序排字、画图表的图用留白的底图:编辑的父图也是底图,两张对照的是同一种画面。 + const path = good.basePath || good.path; + image.colorAnchor = { id: good.id, path, file: basename(path) }; + deps.emit(session, 'verify', image.id + ' 色彩基准定为 ' + good.id + ':之后在别的候选上编辑时带上它对照颜色'); +} + +function anchorFor(image, parent) { + const anchor = image && image.colorAnchor; + if (!anchor || !anchor.path || !parent) return null; + if (anchor.id === parent.id || anchor.path === parentImagePath(image, parent)) return null; + return existsSync(anchor.path) ? anchor : null; +} + +function editRolesPrefix(hasAnchor, refCount, refLines) { + let prefix = '第 1 张图是要修改的原图,请在它基础上修改,保持它的构图、背景、光线与画幅不变;'; + if (hasAnchor) { + prefix += + '第 2 张图是这张图最早、颜色准确的一版,只用来对照颜色:整体色调、白平衡、纸面和背景的白度、明暗都要和第 2 张一致,' + + '不要整体变暖、变冷、变红、变紫或变暗;不要采用第 2 张里和第 1 张不一样的内容。'; + } + if (refLines && refLines.length) { + prefix += (hasAnchor ? '再后面' : '后面') + '的图是参考图,都不是底图,不要采用它们的背景、机位或构图:' + refLines.join(';') + '。'; + } else if (refCount) { + prefix += (hasAnchor ? '再后面' : '后面') + '的图只是身份/外观参考,不要采用它们的背景、机位或构图。'; + } + return prefix + '\n\n'; +} + async function executeAction(session, image, action, deps) { + if (action.type === 'text') { + const made = await retypesetRound(session, image, action, deps); + deps.emit(session, 'paint', image.id + ' 在 ' + action.parentId + ' 不带字的底图上重新排字,不调生图接口'); + return made; + } // 计量覆盖整个 DiT 环节:参考图上传 + 接口调用 + 图片下载。 // 之前 started 写在接口调用之后,把最贵的那段漏掉了。 const started = Date.now(); const config = deps.config; - const n = session.route === 'fast' ? 1 : DEFAULTS.candidates; + // GIF 的帧每帧只出 1 张:一次评审看全部帧,判了不合格的再单独重画。 + const n = image.spec.candidates || (session.route === 'fast' ? 1 : DEFAULTS.candidates); const options = { prompt: action.prompt, aspectRatio: image.spec.aspectRatio, @@ -1645,6 +4577,7 @@ async function executeAction(session, image, action, deps) { let result; let parent = null; + let anchor = null; let endpoint = 'images/generations'; if (action.type === 'edit') { endpoint = 'images/edits'; @@ -1652,34 +4585,48 @@ async function executeAction(session, image, action, deps) { const refs = []; // 父图是本地文件,必须先上传换成 mm_file://。直接把它当 image_url 发出去, // 接口会回 HTTP 400「only http(s) URL, data:image/*;base64 or mm_file:/」。 - if (parent) refs.push(await h3.resolveReference(config, parent.path)); + if (parent) refs.push(await h3.resolveReference(config, parentImagePath(image, parent))); + // 色彩基准图放第 2 张:编辑叠编辑时偏色会一轮轮累积,每次都拿最早那张颜色准确的图对照。 + anchor = anchorFor(image, parent); + if (anchor) { + try { + refs.push(await h3.resolveReference(config, anchor.path)); + } catch (error) { + deps.emit(session, 'paint', image.id + ' 色彩基准图 ' + anchor.id + ' 没传上去,这一轮不带:' + String((error && error.message) || error), { level: 'warn' }); + anchor = null; + } + } // 编辑只带动作里点名的参考图。fallback 到 image.spec.refs 会把上一版的 R1..R4 全塞进来, // 模型分不清哪张是底图,就会照着参考图重画。 - refs.push(...(await resolveActionRefs(session, image, action.refs, deps))); + const extra = await resolveActionRefs(session, image, usableRefs(session, image, action.refs || [], deps), deps); + refs.push(...extra.files); if (!refs.length) throw new Error('edit 缺少可用参考图'); - // 同时送底图和身份参考图时,必须说清谁是谁:不说的话模型会拿参考图当底图重画。 - if (parent && refs.length > 1) { - options.prompt = - '第 1 张图是要修改的原图,请在它基础上修改,保持它的构图、背景、光线与画幅不变;' + - '后面的图只是身份/外观参考,不要采用它们的背景、机位或构图。\n\n' + - options.prompt; + // 同时送底图和别的图时,必须说清谁是谁:不说的话模型会拿参考图当底图重画。 + if (parent && (anchor || extra.files.length)) { + const lines = refsHaveRoles(session, extra.ids) ? describeRefs(session, extra.ids, anchor ? 3 : 2) : []; + options.prompt = editRolesPrefix(Boolean(anchor), extra.files.length, lines) + options.prompt; + } else if (!parent && refsHaveRoles(session, extra.ids)) { + // 规划给了参考图的图第一轮就走这里(entry 被判成 edit、没有底图):同样要逐张说清用途。 + options.prompt = refGuide(session, extra.ids) + options.prompt; } result = await h3.edit(config, { ...options, images: refs.slice(0, 16) }); } else { // refine 点名的 refs 不能把角色基准图顶掉:修一轮之后主体就漂回原样了。 const explicit = action.refs && action.refs.length ? action.refs : image.spec.refs || []; const refIds = dedupe([...explicit, ...(image.spec.characterRefs || [])]); - const refs = await resolveActionRefs(session, image, refIds, deps); - if (refs.length) { + const resolved = await resolveActionRefs(session, image, usableRefs(session, image, refIds, deps), deps); + if (resolved.files.length) { endpoint = 'images/edits'; + let prefix = ''; // 角色基准图是"没有底图的条件图":不说清楚,模型会连它的浅灰背景一起照搬过来。 if (!parent && (image.spec.characterRefs || []).length) { - options.prompt = + prefix += '参考图只用于锁定主体的身份与外观(脸型、发型与发色、眼睛、体型比例、服装款式与配色、标志性配件),' + - '必须在所有图里保持完全一致;不要采用参考图的背景、场景、构图、机位与画幅。\n\n' + - options.prompt; + '必须在所有图里保持完全一致;不要采用参考图的背景、场景、构图、机位与画幅。\n\n'; } - result = await h3.edit(config, { ...options, images: refs.slice(0, 16) }); + if (refsHaveRoles(session, resolved.ids)) prefix += refGuide(session, resolved.ids); + options.prompt = prefix + options.prompt; + result = await h3.edit(config, { ...options, images: resolved.files.slice(0, 16) }); } else { result = await h3.generate(config, options); } @@ -1708,11 +4655,12 @@ async function executeAction(session, image, action, deps) { requestId: result.requestId, revisedPrompt: item.revisedPrompt, parentId: parent ? parent.id : null, - parentPath: parent ? parent.path : null, + parentPath: parent ? parentImagePath(image, parent) : null, // 统计值在 parent.program.stats 上(programChecks 写进去的),候选对象本身没有 stats 字段。 // 写成 parent.stats 会恒为 undefined:漂移判定永远退回绝对模式,自动配平阈值从 // 「相对父图 ≥0.05」变成「绝对 redGain ≥1.35」,编辑把画面推红也就没人拉回来了。 parentStats: (parent && parent.program && parent.program.stats) || action.parentStats || null, + anchorId: anchor ? anchor.id : null, actionType: action.type, actionPrompt: action.prompt, round: image.rounds.length - 1, @@ -1735,6 +4683,7 @@ async function executeAction(session, image, action, deps) { kv('尺寸', candidates.map((candidate) => candidate.size).join('、') || '-'), kv('耗时', seconds(ditMs)), kv('token', result.usage ? result.usage.total_tokens : '-'), + anchor ? kv('色彩基准', anchor.id + '(第 2 张图,只对照颜色)') : null, candidates[0] && candidates[0].revisedPrompt ? text('模型改写后的 prompt', candidates[0].revisedPrompt) : null, @@ -1743,30 +4692,105 @@ async function executeAction(session, image, action, deps) { return candidates; } -// 参考图引用只认两种:已登记的 R 编号(换成会话目录里的文件),以及能直接交给接口的地址。 -// 其余字符串一律返回空串,由调用方跳过并告警。refs 来自 Agent 的回复,是不可信输入: -// 以前不认识的字符串会原样当成本地路径,于是 uploadReference 会把那个本机文件读出来上传给接口。 +// 参考图引用只认已登记的 R 编号(换成会话目录里的文件,上传后以 mm_file:// 交给接口), +// 以及已经是接口格式的 mm_file:// / data:image。其余一律返回空串,由调用方跳过并告警。 +// refs 来自 Agent 的回复,是不可信输入:以前不认识的字符串会原样当成本地路径, +// 于是 uploadReference 会把那个本机文件读出来上传给接口。 +// http(s) 地址也不再原样交给接口:要用的地址在上游已经下载、校验、登记成 R 编号了, +// 走到这里还是地址的,就是没经过校验的(真机 20261112-8a442a:编辑节点编了一个 404 的地址, +// 接口取不到,同一个请求连败 6 轮)。 function resolveRefPath(session, ref) { if (typeof ref !== 'string' || !ref) return ''; - if (/^(https?:\/\/|mm_file:\/\/|data:image\/)/i.test(ref)) return ref; + if (/^(mm_file:\/\/|data:image\/)/i.test(ref)) return ref; const found = session.refs.find((item) => item.id === ref.toUpperCase()); return found ? found.path : ''; } // 把一组引用解析成接口能用的参考图;不认识的跳过并在时间线上说一句。 async function resolveActionRefs(session, image, list, deps) { - const out = []; + const files = []; + const ids = []; for (const ref of list || []) { const target = resolveRefPath(session, ref); if (!target) { - deps.emit(session, 'paint', image.id + ' 忽略了不认识的参考图引用「' + String(ref).slice(0, 60) + '」', { - level: 'warn', - }); + const remote = typeof ref === 'string' && /^https?:\/\//i.test(ref); + deps.emit( + session, + 'paint', + image.id + (remote ? ' 忽略了没有下载校验过的参考图地址「' : ' 忽略了不认识的参考图引用「') + String(ref).slice(0, 60) + '」', + { level: 'warn' }, + ); continue; } - out.push(await h3.resolveReference(deps.config, target)); + files.push(await h3.resolveReference(deps.config, target)); + ids.push(ref); } - return out; + return { files, ids }; +} + +// ---------------------------------------------------------------- 参考图的用途 +// +// 有参考比没参考画得准,所以规划会给画面里每个找得到真实样子的东西都搜参考图(主体、道具、画风、版式、场景)。 +// 参考图一多,不说清每张拿来做什么,模型就会把其中一张当底图、照搬它的构图和内容。 +const REF_USE_LABELS = { subject: '主体外观参考', detail: '局部参考', style: '画风参考', layout: '版式参考', scene: '场景参考' }; +const REF_USE_RULES = { + subject: '照着它画这个东西的样子', + detail: '照着它画这件道具、服饰或材质', + style: '只取笔触、质感和配色,不要照搬它的内容和构图', + layout: '只取排版、留白和各元素的位置,不要照搬上面的文字和图案', + scene: '只取环境的样子,不要照搬它的机位和构图', +}; + +// 逐张写明用途。first 是这批参考图在请求里从第几张开始(前面可能还有底图和色彩基准图)。 +function describeRefs(session, ids, first) { + return (ids || []).map((id, index) => { + const ref = (session.refs || []).find((item) => item.id === id) || (session.characterRefs || []).find((item) => item.id === id); + const position = '第 ' + (first + index) + ' 张'; + if (!ref) return position + '是参考图'; + const what = ref.note ? '(' + shortText(String(ref.note).replace(/^你给的\s*/, ''), 24) + ')' : ''; + if ((session.characterRefs || []).some((item) => item.id === id)) return position + what + '是角色基准图,锁定主体的身份与外观'; + if (ref.origin === 'search' && REF_USE_LABELS[ref.use]) { + return position + what + '是' + REF_USE_LABELS[ref.use] + ':' + (ref.take ? ref.take + ';' : '') + REF_USE_RULES[ref.use]; + } + // 老会话检索来的图没记用途。 + if (ref.origin === 'search') return position + what + '是检索来的参考图'; + return position + what + '是用户给的参考图'; + }); +} + +// 程序排字、画图表的底图必须留白。版式参考图(真实的课本页、菜单、海报)上全是字,送进去模型就会在留白处写字。 +function usableRefIds(session, image, ids) { + if (!isOverlay(image) && !hasCharts(image)) return { keep: ids, skipped: [] }; + const keep = []; + const skipped = []; + for (const id of ids || []) { + const ref = (session.refs || []).find((item) => item.id === id); + if (ref && ref.origin === 'search' && ref.use === 'layout') skipped.push(id); + else keep.push(id); + } + return { keep, skipped }; +} + +function usableRefs(session, image, ids, deps) { + const { keep, skipped } = usableRefIds(session, image, ids); + if (skipped.length && !image.layoutRefsHeld) { + image.layoutRefsHeld = true; + deps.emit(session, 'paint', image.id + ' 的底图要留白给程序排字或画图表,版式参考图 ' + skipped.join('、') + ' 不送进生图:上面全是字,送进去模型会在留白处写字'); + } + return keep; +} + +// 检索来的参考图都带用途;只有用户给的图时沿用原来的说法,不额外加说明。 +function refsHaveRoles(session, ids) { + return (ids || []).some((id) => { + const ref = (session.refs || []).find((item) => item.id === id); + return ref && ref.origin === 'search' && REF_USE_LABELS[ref.use]; + }); +} + +// 没有底图、只送参考图时的说明。 +function refGuide(session, ids) { + return '参考图说明(这几张都不是底图,构图、机位、画幅按后面的描述来):' + describeRefs(session, ids, 1).join(';') + '。\n\n'; } function findCandidate(image, id) { @@ -1804,13 +4828,16 @@ function lastCandidate(image) { return null; } -// 05 VERIFY 的程序部分:尺寸、色彩漂移(发红)、过度锐化、文字层是否写进 prompt。 +// 05 VERIFY 的程序部分:二维码替换、尺寸、色彩漂移(发红)、过度锐化、文字层是否写进 prompt。 async function programChecks(session, image, candidate, deps) { let decoded = null; + let format = 'unknown'; try { // 复用下载时拿到的字节,省掉一次 4K 图的磁盘读 + 解码。 // 必须走 decodeImage(PNG/JPEG 都认),不能只认 PNG:见文件头的说明。 - decoded = decodeImage(candidate.buffer || (await readFile(candidate.path))); + const bytes = candidate.buffer || (await readFile(candidate.path)); + format = sniffFormat(bytes); + decoded = decodeImage(bytes); } catch (error) { candidate.buffer = null; candidate.program = { error: '图片解码失败:' + String(error && error.message ? error.message : error) }; @@ -1818,6 +4845,15 @@ async function programChecks(session, image, candidate, deps) { return; } candidate.buffer = null; + // 二维码排在所有实测前面:统计、预览、评审看图材料都必须基于贴好真码之后、真正要交付的那张图。 + const qr = await applyQrFix(session, image, candidate, decoded, format, deps); + if (qr && qr.decoded) decoded = qr.decoded; + // 图表画在码之后、字之前:图表的标题、刻度是图表自己的字,用户另外要的文字排在最上面。 + const charted = await applyChartOverlay(session, image, candidate, decoded, format, deps); + if (charted && charted.image) decoded = charted.image; + // 程序排字同理,排在码之后(码贴在底图上,字再排上去)。 + const typed = await applyTextOverlay(session, image, candidate, decoded, format, deps); + if (typed && typed.image) decoded = typed.image; let stats = png.analyze(decoded); const parentStats = candidate.parentStats || null; const drift = png.detectDrift(stats, parentStats); @@ -1831,6 +4867,9 @@ async function programChecks(session, image, candidate, deps) { drift, sharpness: sharp, redCast: false, + qr: qr ? qr.summary : null, + chart: charted ? charted.summary : null, + text: typed ? typed.summary : null, }; // 色彩只做检测,不动像素:偏红由 H-color 硬项判 fail,交给 runImage 重画。 @@ -1838,8 +4877,19 @@ async function programChecks(session, image, candidate, deps) { // clamp 下限([0.75,0.75,0.75]),退化成整体压暗:色偏一点没修,还把用户明确要的亮度 // 压回去(真机 20261109-691283「改成烈日当空」那一轮,luma 被压到 ×0.748)。 // 现在程序层只负责判,判成偏红就由 runImage 打回重画一轮。 - const redCast = stats.redGain >= RED_ABS_LIMIT && (drift.cast === 'red' || stats.redGain >= RED_HARD_LIMIT); + let redCast = stats.redGain >= RED_ABS_LIMIT && (drift.cast === 'red' || stats.redGain >= RED_HARD_LIMIT); + // 红的是主体本身(橘猫、红裙子、暖色的产品)而不是整体偏色:画面里接近中性的地方是正的,就不算偏红。 + // 真偏色时中性的地方一起发红(轻微偏色量得出来),偏得厉害时中性像素几乎没有,退回整图判定。 + const subjectRed = redCast && stats.neutralShare >= NEUTRAL_MIN_SHARE && stats.neutralRedGain < RED_ABS_LIMIT; + if (subjectRed) redCast = false; + // 需求要的主色就是黄、红、橙,画面又几乎没有中性色:整图均值判不了白平衡,不算偏红,交给评审看。 + const warmDesign = redCast && stats.neutralShare < NEUTRAL_MIN_SHARE && imageWarm(session, image); + if (warmDesign) redCast = false; candidate.program.redCast = redCast; + // 白纸、白底:单独量白色的地方。整图均值会被大面积的彩色内容盖过去。 + const whiteOn = wantsWhite(session, image) && stats.whiteShare >= WHITE_MIN_SHARE && Array.isArray(stats.whiteRGB); + const whiteCast = whiteOn && stats.whiteCast >= WHITE_CAST_LIMIT; + candidate.program.whiteCast = whiteCast ? whiteCastLabel(stats.whiteRGB) : ''; const checks = []; const longEdge = candidate.program.longEdge; @@ -1856,9 +4906,25 @@ async function programChecks(session, image, candidate, deps) { verify: 'program', text: redCast ? drift.label + ':相对父图 +' + drift.strength + ',绝对 redGain ' + stats.redGain + ' ≥ ' + RED_ABS_LIMIT + ',判为偏红' - : '整体白平衡中性(redGain ' + stats.redGain + ')', + : subjectRed + ? '主体本身偏红,中性区域白平衡正常(整图 redGain ' + stats.redGain + ',中性区域 ' + stats.neutralRedGain + ')' + : warmDesign + ? '需求要的主色是黄、红、橙,画面几乎没有中性色(' + Math.round(stats.neutralShare * 1000) / 10 + '%),程序判不了白平衡,交给评审看(整图 redGain ' + stats.redGain + ')' + : '整体白平衡中性(redGain ' + stats.redGain + ')', result: redCast ? 'fail' : 'pass', }); + if (whiteOn) { + const rgb = stats.whiteRGB.map((value) => Math.round(value)).join(','); + checks.push({ + id: 'H-white', + kind: 'hard', + verify: 'program', + text: whiteCast + ? '白色的纸面或背景' + candidate.program.whiteCast + ':实测 RGB (' + rgb + '),通道差 ' + stats.whiteCast + ' 级 ≥ ' + WHITE_CAST_LIMIT + : '白色的纸面或背景是中性的白:实测 RGB (' + rgb + '),通道差 ' + stats.whiteCast + ' 级', + result: whiteCast ? 'fail' : 'pass', + }); + } // 偏冷不自动打回:冷调题材很常见,交给评审表态就够了。 if (!redCast && drift.cast === 'blue') { checks.push({ id: 'P-cast', kind: 'soft', verify: 'program', text: drift.label + '(交评审判断,不自动打回)', result: 'warn' }); @@ -1872,16 +4938,74 @@ async function programChecks(session, image, candidate, deps) { result: 'warn', }); } - for (const layer of image.spec.textLayers || []) { - const text = typeof layer === 'string' ? layer : layer && layer.text; - if (!text) continue; - const inPrompt = String(candidate.revisedPrompt || '') .includes(text) || String(image.spec.prompt || '').includes(text); + // 程序排字时文字本来就不进 prompt,「写进 prompt」这项不适用;改由排字结果给 T 项和 H-text 定结论。 + if (typed) { + const ok = typed.summary.status === 'overlay'; + const why = ok ? '' : textFailText(typed.summary); + // 超出文字层上限、没排进去的段落:重画也排不上,不判 fail(免得白白重画),T 项照实写「拿不准」,交付时告诉用户。 + const dropped = (image.spec.textOverlay && image.spec.textOverlay.dropped) || []; + for (const item of image.spec.checklist.filter((entry) => /^T\d+$/.test(entry.id))) { + const missed = ok && dropped.some((value) => item.text.includes(shortText(value, 12).replace(/….*$/, ''))); + checks.push({ + id: item.id, + kind: item.kind, + verify: 'program', + text: !ok ? '文字没排进去:' + why : missed ? '其中有段落超出 ' + MAX_TEXT_LAYERS + ' 层的上限,没排进去' : '文字由程序按原文逐字排入', + result: !ok ? 'fail' : missed ? 'uncertain' : 'pass', + }); + } + if (dropped.length) { + checks.push({ + id: 'P-textdrop', + kind: 'soft', + verify: 'program', + text: '文字层超过 ' + MAX_TEXT_LAYERS + ' 层,这几段没排:' + dropped.map((value) => '「' + shortText(value, 12) + '」').join(''), + result: 'warn', + }); + } + checks.push({ + id: 'H-text', + kind: 'hard', + verify: 'program', + text: ok + ? '文字已排进底图留空的位置(' + typed.summary.items.map((item) => layoutSummary(item)).join(';') + ')' + : '文字没排进去:' + why, + result: ok ? 'pass' : 'fail', + }); + } else { + for (const layer of image.spec.textLayers || []) { + const text = typeof layer === 'string' ? layer : layer && layer.text; + if (!text) continue; + const inPrompt = String(candidate.revisedPrompt || '') .includes(text) || String(image.spec.prompt || '').includes(text); + checks.push({ + id: 'P-text-' + text.slice(0, 6), + kind: 'hard', + verify: 'program', + text: '文字「' + text + '」已写进 prompt', + result: inPrompt ? 'pass' : 'fail', + }); + } + } + if (qr) { + const real = qr.summary.status === 'real'; + checks.push({ + id: 'H-qr', + kind: 'hard', + verify: 'program', + text: real + ? '二维码已换成真码,从最终文件扫码实测为「' + qr.summary.items.map((item) => item.content).join('」「') + '」' + : '二维码没换成真码:' + qrFailText(qr.summary), + result: real ? 'pass' : 'fail', + }); + } + if (charted) { + const ok = charted.summary.status === 'overlay'; checks.push({ - id: 'P-text-' + text.slice(0, 6), + id: CHART_HARD_CHECK.id, kind: 'hard', verify: 'program', - text: '文字「' + text + '」已写进 prompt', - result: inPrompt ? 'pass' : 'fail', + text: ok ? '图表已按数据画入并从最终文件量回来核对(' + chartVerifyText(charted.summary) + ')' : '图表没画上:' + chartFailText(charted.summary), + result: ok ? 'pass' : 'fail', }); } candidate.checks = checks; @@ -1894,6 +5018,8 @@ async function programChecks(session, image, candidate, deps) { kv('锐度', stats.sharpness + (sharp.overSharpened ? '(相对父图 ' + sharp.ratio + '×,疑似过度锐化)' : '')), kv('过曝像素占比', (Number(stats.clippedRatio || 0) * 100).toFixed(2) + '%'), kv('通道均值', 'R ' + stats.meanR + ' / G ' + stats.meanG + ' / B ' + stats.meanB + ' · luma ' + stats.luma), + qr ? kv('二维码', qr.summary.status === 'real' ? '已换成真码,扫码实测通过' : '没换成:' + qrFailText(qr.summary)) : null, + typed ? kv('文字', typed.summary.status === 'overlay' ? '由程序排入(' + fontLabelOf(typed.summary) + ')' : '没排进去:' + textFailText(typed.summary)) : null, list( '校验项', checks.map((check) => ({ tag: check.result, text: check.id + ' · ' + check.text })), @@ -1903,6 +5029,268 @@ async function programChecks(session, image, candidate, deps) { await prepareAssets(candidate, decoded, deps); } +// 把候选图里模型画的占位码换成真码,并从最终文件扫码验证(qrfix.mjs)。 +// 贴好的图另存为 <候选号>-qr.<扩展名>,候选的 path / file 改指向它(交付、编辑底图、分享页都用这张); +// 原图留在磁盘上不动,排查「贴得对不对」时要对照。没有要放的内容时返回 null,什么都不做。 +// +// 修码、排字一张一张排队做:每张 4K 图处理时要同时拿着原图、副本、编码缓冲(修码还有重新解码的图),峰值两三百 MB, +// 多图任务 4 张图 × 2 张候选同时做会叠到 GB 级。它们本来就是单线程的 CPU 活,排队不损失速度。 +let pixelQueue = Promise.resolve(); +function queuePixelWork(task) { + const run = pixelQueue.then(task); + pixelQueue = run.catch(() => undefined); + return run; +} + +async function applyQrFix(session, image, candidate, decoded, format, deps) { + const contents = qrContents(image.spec); + if (!contents.length) return null; + const startedAt = Date.now(); + let result; + try { + result = await queuePixelWork(() => fixQrInImage(decoded, format, contents)); + } catch (error) { + const message = '修码出错:' + describeError(error); + result = { + ok: false, + buffer: null, + decoded: null, + results: contents.map((content) => ({ content, ok: false, changed: false, reason: 'error', reasonText: message })), + }; + } + if (result.ok && result.buffer) { + const file = candidate.id + '-qr' + extensionFor(result.buffer); + const path = join(deps.dirs.images, file); + await writeFile(path, result.buffer); + candidate.originalFile = candidate.file; + candidate.originalPath = candidate.path; + candidate.file = file; + candidate.path = path; + } + const summary = { + status: result.ok ? 'real' : 'failed', + items: result.results.map((item) => ({ + content: item.content, + ok: Boolean(item.ok), + changed: Boolean(item.changed), + reason: item.reason || '', + reasonText: item.reasonText || '', + detail: item.detail || '', + version: item.version || 0, + ecl: item.ecl || '', + modules: item.modules || 0, + modulePx: item.modulePx || 0, + // 每个视角扫出了什么:n = 近看,f = 远看,+ 扫出正确内容,- 没扫出,x 扫出别的内容。 + views: (item.scans || []) + .map((scan) => scan.view[0] + (scan.data === null ? '-' : scan.data === item.content ? '+' : 'x')) + .join(''), + source: item.source || '', + })), + ms: Date.now() - startedAt, + }; + deps.emit( + session, + 'verify', + candidate.id + (result.ok ? ' 二维码已换成真码,扫码实测通过' : ' 二维码没换成真码:' + qrFailText(summary)), + { + level: result.ok ? 'info' : 'warn', + durMs: summary.ms, + detail: detail('二维码替换 · ' + candidate.id, [ + kv('结论', result.ok ? '通过:最终文件扫码解出的内容与要求逐字一致' : '不通过(H-qr 判 fail)'), + ...summary.items.map((item) => + group('「' + item.content + '」', [ + kv('结果', item.ok ? (item.changed ? '已贴上真码' : '原图里已经是这枚真码,没有改动') : item.reasonText + (item.detail ? '(' + item.detail + ')' : '')), + item.version ? kv('码', '版本 ' + item.version + (item.ecl ? ' · 纠错 ' + item.ecl : '') + ' · ' + item.modules + '×' + item.modules + ' 模块 · 每模块约 ' + item.modulePx + 'px') : null, + item.views ? kv('扫码视角', item.views + '(n 近看 / f 远看,+ 解出正确内容)') : null, + ]), + ), + result.ok && result.buffer ? kv('文件', candidate.file + '(原图 ' + candidate.originalFile + ' 保留)') : null, + ]), + }, + ); + return { summary, decoded: result.ok ? result.decoded : null }; +} + +function qrFailText(summary) { + const failed = ((summary && summary.items) || []).filter((item) => !item.ok); + if (!failed.length) return '未知原因'; + return failed.map((item) => item.reasonText + (item.detail ? '(' + item.detail + ')' : '')).join(';'); +} + +// 程序画图表:在候选(留出空白面板的底图)上按数据把图表画进去,再从最终文件量回来核对(chart.mjs)。 +// 画好的图另存为 <候选号>-chart.<扩展名>,候选的 path / file 改指向它;不带图表的底图记在 basePath 上, +// 之后改数、在这张图上编辑都从它开始。和修码、排字一样排队做。 +async function applyChartOverlay(session, image, candidate, decoded, format, deps) { + const charts = imageCharts(image); + if (!charts.length) return null; + const startedAt = Date.now(); + let result; + try { + result = await queuePixelWork(() => overlayCharts(decoded, format, charts)); + } catch (error) { + const message = '画图表出错:' + describeError(error); + result = { ok: false, buffer: null, image: null, results: charts.map((chart) => ({ type: chart.type, title: chart.title, ok: false, reason: 'error', reasonText: message })) }; + } + const basePath = candidate.path; + const baseFile = candidate.file; + if (result.ok && result.buffer) { + const file = candidate.id + '-chart' + extensionFor(result.buffer); + const path = join(deps.dirs.images, file); + await writeFile(path, result.buffer); + if (!candidate.originalFile) { + candidate.originalFile = baseFile; + candidate.originalPath = basePath; + } + if (!candidate.baseFile) { + candidate.baseFile = baseFile; + candidate.basePath = basePath; + } + candidate.file = file; + candidate.path = path; + } + const summary = { + status: result.ok ? 'overlay' : 'failed', + items: (result.results || []).map((item) => ({ + type: item.type || '', + title: item.title || '', + ok: Boolean(item.ok), + reason: item.reason || '', + reasonText: item.reasonText || '', + detail: item.detail || '', + font: item.font || '', + size: item.size || 0, + box: item.box || null, + planned: item.planned || null, + blank: item.blank || null, + coverage: item.coverage || 0, + scale: item.scale || null, + labelsDropped: Boolean(item.labelsDropped), + warning: item.warning || '', + verify: item.verify + ? { ok: Boolean(item.verify.ok), note: item.verify.note || '', checked: item.verify.checked || 0, maxErrorPx: item.verify.maxErrorPx, tolerancePx: item.verify.tolerancePx } + : null, + })), + ms: Date.now() - startedAt, + }; + deps.emit( + session, + 'verify', + candidate.id + (result.ok ? ' 图表已由程序按数据画入,量回来核对通过' : ' 图表没画上:' + chartFailText(summary)), + { + level: result.ok ? 'info' : 'warn', + durMs: summary.ms, + detail: detail('程序画图表 · ' + candidate.id, [ + kv('结论', result.ok ? '通过:坐标轴、刻度、数值和比例从最终文件量回来都对得上' : '不通过(H-chart 判 fail)'), + ...summary.items.map((item, index) => + group('第 ' + (index + 1) + ' 张' + (CHART_TYPES[item.type] || '图表') + (item.title ? '「' + item.title + '」' : ''), [ + kv('结果', item.ok ? '已画入' + (item.size ? ',字高约 ' + item.size + 'px' : '') : item.reasonText + (item.detail ? '(' + item.detail + ')' : '')), + item.verify ? kv('核对', item.verify.note) : null, + item.scale ? kv('数值轴', item.scale.min + ' 到 ' + item.scale.max + ',每格 ' + item.scale.step) : null, + item.coverage ? kv('留空', '规划位置有 ' + Math.round(item.coverage * 100) + '% 是空白') : null, + item.labelsDropped ? kv('标注', '数值太密,没有逐个标注') : null, + item.warning ? kv('提醒', item.warning) : null, + ]), + ), + result.ok && result.buffer ? kv('文件', candidate.file + '(不带图表的底图 ' + baseFile + ' 保留)') : null, + ]), + }, + ); + return { summary, image: result.ok ? result.image : null }; +} + +// 程序排字:在候选(不带字的底图)上找到留空的位置,把文字按原文排进去(textoverlay.mjs)。 +// 排好的图另存为 <候选号>-text.<扩展名>,候选的 path / file 改指向它(交付、评审都用这张); +// 不带字的底图记在 basePath 上:之后在这张图上编辑、改字,都从它开始,不能从排好字的图开始。 +// 和修码一样排队做,4K 图上排字也要同时拿着原图和副本。 +async function applyTextOverlay(session, image, candidate, decoded, format, deps) { + if (!isOverlay(image)) return null; + const layers = overlayLayers(image); + const startedAt = Date.now(); + let result; + try { + result = await queuePixelWork(() => overlayText(decoded, format, layers)); + } catch (error) { + const message = '排字出错:' + describeError(error); + result = { ok: false, buffer: null, image: null, results: layers.map((layer) => ({ text: layer.text, ok: false, reason: 'error', reasonText: message })) }; + } + const basePath = candidate.path; + const baseFile = candidate.file; + if (result.ok && result.buffer) { + const file = candidate.id + '-text' + extensionFor(result.buffer); + const path = join(deps.dirs.images, file); + await writeFile(path, result.buffer); + if (!candidate.originalFile) { + candidate.originalFile = baseFile; + candidate.originalPath = basePath; + } + // 图表先画过的话,底图已经记成画图表之前的那张,不能改指向带图表的这张。 + if (!candidate.baseFile) { + candidate.baseFile = baseFile; + candidate.basePath = basePath; + } + candidate.file = file; + candidate.path = path; + } + const summary = { + status: result.ok ? 'overlay' : 'failed', + items: (result.results || []).map((item) => ({ + text: item.text, + ok: Boolean(item.ok), + reason: item.reason || '', + reasonText: item.reasonText || '', + detail: item.detail || '', + font: item.font || '', + kind: item.kind || '', + direction: item.direction || '', + punctuation: item.punctuation || '', + size: item.size || 0, + rows: item.rows || 0, + cols: item.cols || 0, + lines: item.lines || 0, + chars: item.chars || 0, + dropped: item.dropped || 0, + substituted: item.substituted || '', + color: item.color || '', + autoColor: Boolean(item.autoColor), + box: item.box || null, + planned: item.planned || null, + blank: item.blank || null, + coverage: item.coverage || 0, + role: item.role || '', + unified: Boolean(item.unified), + indent: item.indent || 0, + superscripts: item.superscripts || 0, + rule: Boolean(item.rule), + moved: item.moved || 0, + ...(item.part ? { part: item.part } : {}), + })), + ms: Date.now() - startedAt, + }; + deps.emit( + session, + 'verify', + candidate.id + (result.ok ? ' 文字已由程序排入(' + fontLabelOf(summary) + ')' : ' 字没排上:' + textFailText(summary)), + { + level: result.ok ? 'info' : 'warn', + durMs: summary.ms, + detail: detail('程序排字 · ' + candidate.id, [ + kv('结论', result.ok ? '通过:文字按原文逐字排进底图留空的位置' : '不通过(H-text 判 fail)'), + ...summary.items.map((item, index) => + group('第 ' + (index + 1) + ' 段「' + shortText(item.text, 12) + '」', [ + kv('结果', item.ok ? layoutSummary(item) : item.reasonText + (item.detail ? '(' + item.detail + ')' : '')), + item.font ? kv('字体', item.font + (item.substituted ? '(缺字用别的字体补:' + item.substituted + ')' : '')) : null, + item.ok && item.color ? kv('颜色', item.color + (item.autoColor ? '(规划给的颜色和底色反差不够,自动换的)' : '')) : null, + item.ok && item.dropped ? kv('标点', '按书法习惯不写标点,省去 ' + item.dropped + ' 个') : null, + item.coverage ? kv('留空', '规划位置有 ' + Math.round(item.coverage * 100) + '% 是空白') : null, + ]), + ), + result.ok && result.buffer ? kv('文件', candidate.file + '(不带字的底图 ' + baseFile + ' 保留)') : null, + ]), + }, + ); + return { summary, image: result.ok ? result.image : null }; +} + // 一次解码同时产出页面预览和评审看图材料,避免对同一张 4K 图解码两遍。 async function prepareAssets(candidate, decoded, deps) { try { @@ -1955,6 +5343,15 @@ function extraFailsFor(rawExtra, candidateId) { async function judgeNode(session, image, record, deps) { const candidates = record.candidates; const refSheet = await ensureRefSheet(session, deps); + const level = difficultyOf(image); + const budget = toolBudget('judge', { + needs: [ + { count: candidates.length, what: '每个候选的看图材料' }, + { count: refSheet ? 1 : 0, what: '参考图材料' }, + ], + difficulty: level.level, + hits: hitsOf(image, 'judge'), + }); // 评审只看 harness 备好的"看图材料",一张图就把整体、曝光和局部全看完了。 const lines = candidates.map((candidate, index) => { @@ -1969,7 +5366,16 @@ async function judgeNode(session, image, record, deps) { ';色彩 ' + (program.drift ? program.drift.label : '正常') + (program.redCast ? '(程序判定偏红)' : '') + ';锐度 ' + program.stats.sharpness + - ';过曝像素占比 ' + (program.stats.clippedRatio !== undefined ? program.stats.clippedRatio : '未知'), + ';过曝像素占比 ' + (program.stats.clippedRatio !== undefined ? program.stats.clippedRatio : '未知') + + (program.qr + ? ';二维码 ' + (program.qr.status === 'real' ? '已由程序换成真码并扫码验证通过' : '没换成真码(' + qrFailText(program.qr) + ')') + : '') + + (program.chart + ? ';图表 ' + (program.chart.status === 'overlay' ? '已由程序按数据画入并量回来核对通过' : '没画上(' + chartFailText(program.chart) + ')') + : '') + + (program.text + ? ';文字 ' + (program.text.status === 'overlay' ? '已由程序按原文逐字排入(' + fontLabelOf(program.text) + ')' : '没排进去(' + textFailText(program.text) + ')') + : ''), ); } else if (program.error) { parts.push(' 程序实测:' + program.error); @@ -2018,7 +5424,7 @@ async function judgeNode(session, image, record, deps) { ' 仍然不要为了保险把真拿不准的写成 fail——错杀一次就要重做一整轮,代价远大于放过。', '5. soft 项只影响 quality 分(0-10),不影响通过与否。', '6. 禁止写脚本、裁图、拼图、量像素、跑统计、联网搜索。尺寸、色彩、锐度、过曝程序都测过了,直接采信,不要推翻。', - '7. 工具调用上限 ' + DEFAULTS.judgeToolBudget + ' 次:每个候选的材料图 + 参考图材料,看完立刻给结论。', + '7. 工具调用上限 ' + budget.limit + ' 次:每个候选的材料图 + 参考图材料,看完立刻给结论。', '8. 你看不到送进模型的 prompt,只能看图。', '9. 先把所有候选判完,再决定 next。', '10. note 不超过 30 字,summary 不超过 40 字。', @@ -2028,6 +5434,56 @@ async function judgeNode(session, image, record, deps) { ' 硬性要求:只要你在 summary 或 note 里写了「手型不对」「姿势不准」「略偏」「雷同」「没画出来」', ' 这类话,就必须把同一条写进 extra_fails——不许只在 summary 里吐槽完就给 pass。', ' 反过来也确实拿不准的(看不清、分辨率不够)不要写进 extra_fails。', + ...((image.spec.qrCodes || []).length + ? [ + '12. 二维码:' + + (qrContents(image.spec).length + ? '画面里的二维码由程序换成真码,并在最终文件上扫码验证过(H-qr,程序已验)。码能不能扫、扫出什么不用你判,' + : '这张图的二维码是装饰图案(用户没给内容),不要求能扫。') + + '不要因为码的黑白图案和你预想的不一样判 fail;', + ' 只在码明显错位(没对齐原来的白底、压住了文字或主体)、被遮挡、缺失或放错位置时写进 extra_fails。', + ] + : []), + ...(isOverlay(image) + ? [ + ((image.spec.qrCodes || []).length ? '13' : '12') + + '. 文字:这张图的文字是程序用字体按原文逐字排进去的(' + + overlayLayers(image).map((layer) => (layer.direction === 'vertical' ? '竖排' : '横排') + FONT_KINDS[layer.font].label).join('、') + + '),文字内容、字序、行列顺序程序已验(T 项、H-text),不要逐字核对,', + ' 也不要因为「是字体排的、不是手写的」判 fail:书体类条目按「看起来是不是要求的那种字」判。', + ' 你要看的是:H-notext(画面别处有没有多出来的字、伪文字、乱码);S-textfit(字和载体融不融合);', + ' 字压到边框或主体、写出了载体、字和载体透视明显对不上时写进 extra_fails。', + ' 字的位置有问题而底图本身没问题(压到线或色块边缘、出了载体、该在色块里却排在外面、几段挤在一起)时,', + ' next 写 {"type":"text","candidateId":"…","text_layers":[{"index":3,"box":[左,上,右,下]}],"reason":"…"}:', + ' 程序在这张候选不带字的底图上按新位置重新排字,不重新生图,几秒钟就好。box 是 0-1 的画面比例,', + ' 只写要挪的那几段,也可以写 "align"(start / center / end);不要改文字内容。各段的编号和位置:', + ...candidates + .filter((candidate) => candidate.program && candidate.program.text && candidate.program.text.status === 'overlay') + .map((candidate) => ' ' + candidate.id + ':\n' + textInventory(image, candidate)), + ' 挪位置只能挪,拼不出字:每一层是一段完整的字,两层挪得再近也成不了一个字。用户要的字没有作为一个完整的字出现', + ' (被拆成了偏旁和部件两块、少了字)就判 fail,并写进 extra_fails,不要用 text 去挪。', + ...((image.spec.readingProblems || []).length + ? [ + ' 注意:规划的文字层按版面读出来,用户要的' + image.spec.readingProblems.map((item) => '「' + item.quote + '」').join('、') + + '读不出来(读出来是 ' + image.spec.readingProblems[0].rows.map((row) => '「' + row + '」').join(' ') + ')。', + ' 清单里涉及这几处的条目看图核对,图上读不出来就判 fail。', + ] + : []), + ' 底图本身有问题(没留空、构图不对、多画了东西)才写 regenerate / edit:给模型的指令里不要写任何文字内容,字之后会重新排。', + ] + : []), + ...(hasCharts(image) + ? [ + 12 + ((image.spec.qrCodes || []).length ? 1 : 0) + (isOverlay(image) ? 1 : 0) + + '. 图表:画面里的数据图表是程序按数据画进去的(' + + imageCharts(image).map((chart) => CHART_TYPES[chart.type] + (chart.title ? '「' + chart.title + '」' : '')).join('、') + + '),坐标轴、刻度、数值标注和比例都从最终文件量回来核对过(H-chart,程序已验),', + ' 不要核对数字、不要估柱子高矮,也不要因为「是程序画的、太规整」判 fail。', + ' 你要看的是:H-nochart(画面别处有没有模型自己画的图表、坐标轴、乱码数字);S-chartfit(图表和画面协不协调);', + ' 图表被遮挡、裁切、压到主体或文字时写进 extra_fails。', + ' next 只能改画面本身:给模型的指令里不要写图表和数字,图表之后会重新画。', + ] + : []), '', '历史动作(判完候选后再看,只用于决定 next):', history || '(首轮)', @@ -2035,12 +5491,14 @@ async function judgeNode(session, image, record, deps) { '只输出一个 JSON 对象,不要解释、不要 markdown 代码块:', '{"verdicts":[{"candidateId":"' + candidates[0].id + '","checks":[{"id":"H1","result":"pass|fail|uncertain","note":"..."}],"quality":7.5,"summary":"..."}],', ' "extra_fails":[{"candidateId":"' + candidates[0].id + '","text":"清单外但违背用户原话的问题"}],', - ' "next":{"type":"regenerate|edit|give_up","candidateId":"...","prompt":"给模型的中文编辑或生成指令","reason":"..."}}', + ' "next":{"type":"' + (isOverlay(image) ? 'regenerate|edit|text|give_up' : 'regenerate|edit|give_up') + '","candidateId":"...","prompt":"给模型的中文编辑或生成指令","reason":"..."}}', '', '如果所有候选的 hard 项都通过,next 直接写 {"type":"give_up"}。', ].join('\n'); - const { data } = await deps.agent.runJson(session, { step: 'judge', instruction, maxTools: DEFAULTS.toolBudget.judge }); + const judged = await deps.agent.runJson(session, { step: 'judge', instruction, maxTools: budget.limit, budgetNote: budget.note }); + noteToolHit(image, 'judge', judged); + const { data } = judged; const verdicts = Array.isArray(data.verdicts) ? data.verdicts : []; const hardIds = image.spec.checklist.filter((item) => item.kind === 'hard').map((item) => item.id); const softIds = image.spec.checklist.filter((item) => item.kind === 'soft').map((item) => item.id); @@ -2135,6 +5593,15 @@ async function judgeNode(session, image, record, deps) { function normalizeNext(raw, candidates, image) { if (!raw || !raw.type) return null; if (raw.type === 'give_up') return { type: 'give_up' }; + // 只重新排字:在某张候选不带字的底图上按新位置重排,不调生图接口。只认排过字、有底图的候选。 + if (raw.type === 'text') { + if (!isOverlay(image)) return null; + const typed = (candidates || []).filter((c) => c.basePath && c.program && c.program.text && c.program.text.status === 'overlay'); + const target = typed.find((c) => c.id === raw.candidateId) || typed[0]; + const textPatches = normalizeLayoutPatches(raw.text_layers, overlayLayers(image).length); + if (!target || !textPatches.length) return null; + return { type: 'text', parentId: target.id, textPatches, prompt: '', reason: raw.reason || '' }; + } const prompt = String(raw.prompt || '').trim(); if (!prompt) return null; if (raw.type === 'edit') { @@ -2148,7 +5615,106 @@ function normalizeNext(raw, candidates, image) { return { type: 'generate', prompt, refs: image.spec.refs, parentId: null, reason: raw.reason || '' }; } +// ---------------------------------------------------------------- 只重新排字(修正轮) +// +// 程序排字的图,字的位置不对(压线、出界、该在色块里却在外面)是排字的问题,不是底图的问题: +// 以前评审只能选重画或改图,重画多少轮字还是排在同样的地方(真机 20261116-06a562,6 轮 12 张候选没修好)。 +// 现在评审可以只改各段的位置,程序在同一张底图上重新排,不调生图接口。 + +// 评审写的挪动:只认位置和对齐,不认文字内容(内容是用户给的,评审不能改)。 +function normalizeLayoutPatches(raw, count) { + const out = []; + for (const item of Array.isArray(raw) ? raw : []) { + if (!item || typeof item !== 'object') continue; + const index = Number(item.index); + if (!Number.isInteger(index) || index < 0 || index >= count) continue; + const patch = { index }; + if (Array.isArray(item.box) && item.box.length === 4 && item.box.every((value) => Number.isFinite(Number(value)))) { + let box = item.box.map(Number); + if (box.some((value) => value > 1.5)) box = box.map((value) => value / 100); + box = box.map((value) => Math.round(Math.min(1, Math.max(0, value)) * 1000) / 1000); + if (box[2] > box[0] && box[3] > box[1]) patch.box = box; + } + if (TEXT_ALIGNS.has(item.align)) patch.align = item.align; + if (patch.box || patch.align) out.push(patch); + } + return out; +} + +function applyLayoutPatches(session, image, patches) { + const overlay = image.spec.textOverlay; + if (!overlay) return; + const layers = overlay.layers.slice(); + for (const patch of patches || []) { + if (!layers[patch.index]) continue; + layers[patch.index] = { ...layers[patch.index], ...(patch.box ? { box: patch.box } : {}), ...(patch.align ? { align: patch.align } : {}) }; + } + image.spec.textOverlay = { ...overlay, layers, planned: true }; + // 留白的说法跟着新位置走:之后要是还得重画底图,模型留空的地方和程序排字的地方对得上。 + image.spec.basePrompt = composeBasePrompt(image.spec, session.instruction); +} + +// 每段字的规划位置和这张候选上实际排的位置,给评审、修正节点挪字时参考。 +function textInventory(image, candidate) { + const items = (candidate && candidate.program && candidate.program.text && candidate.program.text.items) || []; + return overlayLayers(image) + .map((layer, index) => { + const placed = items[index]; + // 带上实测字高和分色结果:「结果字明显放大」「偏旁是橙色」这类条目,评审能对着数字判,不只凭眼睛估。 + const where = placed + ? placed.ok && placed.box + ? '实际排在 ' + JSON.stringify(placed.box) + ',字高 ' + placed.size + 'px' + (placed.lines > 1 ? ',' + placed.lines + ' 行' : '') + partText(placed.part) + : '没排上(' + (placed.reasonText || '') + ')' + : ''; + return ' ' + index + '「' + shortText(layer.text, 16) + '」' + (layer.role ? ' ' + layer.role : '') + ' 规划 ' + JSON.stringify(layer.box) + (where ? ',' + where : ''); + }) + .join('\n'); +} + +const PART_SIDE_TEXT = { left: '左边', right: '右边', top: '上边', bottom: '下边' }; + +function partText(part) { + if (!part) return ''; + if (part.ok) return ',每个字' + PART_SIDE_TEXT[part.side] + '的部件用 ' + part.color + '(程序分色)' + (part.note ? ':' + part.note : ''); + return ',部件分色没做成' + (part.note ? ':' + part.note : ''); +} + +// 修正轮里的「只重新排字」:拿指定候选不带字的底图做一张新候选,排字留给 programChecks(用改过的位置)。 +async function retypesetRound(session, image, action, deps) { + const parent = findCandidate(image, action.parentId); + const basePath = parent && parent.basePath; + if (!basePath) throw new Error('找不到 ' + (action.parentId || '这张候选') + ' 不带字的底图'); + applyLayoutPatches(session, image, action.textPatches); + const buffer = await readFile(basePath); + const id = image.id + '-r' + ((image.roundBase || 0) + image.rounds.length) + '-g0'; + return [ + { + id, + path: basePath, + file: basename(basePath), + size: parent.size, + requestId: parent.requestId, + revisedPrompt: parent.revisedPrompt, + parentId: parent.id, + parentPath: basePath, + parentStats: (parent.program && parent.program.stats) || null, + actionType: 'text', + actionPrompt: parent.actionPrompt || '', + round: image.rounds.length - 1, + createdAt: Date.now(), + buffer, + program: {}, + checks: [], + }, + ]; +} + async function refineNode(session, image, record, verdict, deps, forceChange) { + const budget = toolBudget('refine', { + needs: [{ count: (record.candidates || []).length, what: '最新候选' }], + difficulty: difficultyOf(image).level, + hits: hitsOf(image, 'refine'), + }); const history = image.rounds .map((round) => '第' + (round.index + 1) + '轮 ' + describeAction(round.action) + ' -> ' + (round.verdict ? round.verdict.note : '')) .join('\n'); @@ -2169,8 +5735,21 @@ async function refineNode(session, image, record, verdict, deps, forceChange) { session.instruction, '', '当前 prompt:', - image.spec.prompt, + promptFor(image), '', + ...(isOverlay(image) + ? [ + '这张图的文字由程序后贴:模型只画不带字的底图,文字之后由程序按原文排进「文字留白」写的那块区域。', + '给模型的指令只描述画面本身,不要写任何文字内容,也不要让模型在留白区域画东西。', + '没通过的原因是字的位置(压线、出界、该在色块里却在外面、挤在一起)而底图本身没问题时,不要重画:', + '写 {"type":"text","candidateId":"…","text_layers":[{"index":3,"box":[左,上,右,下]}],"reason":"…"},', + '程序在这张候选不带字的底图上按新位置重新排字,不调生图接口。挪位置拼不出字:两层挪得再近也成不了一个字。各段的编号和位置:', + ...(record.candidates || []) + .filter((candidate) => candidate.program && candidate.program.text && candidate.program.text.status === 'overlay') + .map((candidate) => candidate.id + ':\n' + textInventory(image, candidate)), + '', + ] + : []), '最新候选(本地路径,可以 read 打开看):', (record.candidates || []).map((candidate) => candidate.id + ' -> ' + candidate.path).join('\n') || '(无)', '', @@ -2184,13 +5763,16 @@ async function refineNode(session, image, record, verdict, deps, forceChange) { ? '注意:连续几轮没有改善,必须换一种做法(regenerate 重新生成 / edit 在某个候选上局部编辑 / give_up 放弃)。' : '优先在最能救的候选上做局部编辑;构图本身不对才重新生成。', '经验:不带贴回框的全图 edit 每次都会累积一点偏色和过度锐化,同一条链上不要超过 2 次。', + '工具调用上限 ' + budget.limit + ' 次:只 read 要看的候选,看完立刻给结论。', '', '只输出一个 JSON 对象:', - '{"type":"regenerate|edit|give_up","candidateId":"...","prompt":"给模型的具体指令","reason":"..."}', + '{"type":"' + (isOverlay(image) ? 'regenerate|edit|text|give_up' : 'regenerate|edit|give_up') + '","candidateId":"...","prompt":"给模型的具体指令","reason":"..."}', ].join('\n'); const startedAt = Date.now(); - const { data } = await deps.agent.runJson(session, { step: 'refine', instruction, maxTools: DEFAULTS.toolBudget.refine }); + const refined = await deps.agent.runJson(session, { step: 'refine', instruction, maxTools: budget.limit, budgetNote: budget.note }); + noteToolHit(image, 'refine', refined); + const { data } = refined; const next = normalizeNext(data, record.candidates || [], image); deps.emit(session, 'refine', image.id + ' 修正方案:' + (next ? describeAction(next) : '放弃本轮'), { durMs: Date.now() - startedAt, @@ -2209,11 +5791,8 @@ async function refineNode(session, image, record, verdict, deps, forceChange) { async function selectNode(session, image, deps) { const pool = []; for (const round of image.rounds) { - const best = (round.candidates || []).slice().sort((a, b) => { - const av = a.verdict || { hardPassed: 0, quality: 0 }; - const bv = b.verdict || { hardPassed: 0, quality: 0 }; - return bv.hardPassed - av.hardPassed || bv.quality - av.quality; - })[0]; + // 每轮挑一张:没评审过的(评审没跑成)按程序校验结果排,见 rankCandidates。 + const best = rankCandidates((round.candidates || []).filter((candidate) => candidate && candidate.path))[0]; if (best) pool.push(best); } if (!pool.length) { @@ -2243,20 +5822,33 @@ async function selectNode(session, image, deps) { }); return; } - const shuffled = pool.slice().sort(() => Math.random() - 0.5).slice(0, 4); + // 先按分数取前 4 张再打乱顺序:以前是先打乱再截 4 张,轮次多的时候最好的那张可能根本没进对比图。 + const shuffled = rankCandidates(pool).slice(0, 4).sort(() => Math.random() - 0.5); // 选优同样只看一张并排对比图:顺序打乱、不给分数,避免评审被历史结论带偏。 + // 编号按实际拼进图里的顺序重排:有一张解不开时,图上的编号和「编号对应」必须还是同一套。 let sheetPath = ''; + let shown = shuffled; try { await mkdir(deps.dirs.sheets, { recursive: true }); const built = await review.buildGridSheet( - shuffled.map((candidate, index) => ({ path: candidate.path, label: String(index + 1) })), + shuffled.map((candidate) => ({ path: candidate.path, label: '' })), join(deps.dirs.sheets, image.id + '-select-sheet.png'), - { perRow: Math.min(shuffled.length, 4) }, + { perRow: Math.min(shuffled.length, 4), renumber: true }, ); - sheetPath = built.sheet; + if (built.sheet) { + sheetPath = built.sheet; + shown = built.items.map((item) => shuffled[item.index]); + } } catch { sheetPath = ''; + shown = shuffled; } + // 有对比图时只看这一张;拼不出来就得逐张打开。 + const budget = toolBudget('select', { + needs: [sheetPath ? { count: 1, what: '并排对比图' } : { count: shown.length, what: '逐张打开候选' }], + difficulty: difficultyOf(image).level, + hits: hitsOf(image, 'select'), + }); const instruction = [ '你是H3-Image-Executor 的选优节点。下面几张图来自不同轮次,历史评分不完全可比,请只看图并排对比后选一张交付。', '', @@ -2265,28 +5857,31 @@ async function selectNode(session, image, deps) { '', sheetPath ? '对比图(只 read 这一张,从左到右、从上到下依次编号,左上角有编号标签):\n ' + sheetPath + - '\n 编号对应:' + shuffled.map((candidate, index) => (index + 1) + '=' + candidate.id).join(',') + '\n 编号对应:' + shown.map((candidate, index) => (index + 1) + '=' + candidate.id).join(',') : '候选(本地绝对路径,必须用 read 逐张打开看):\n' + - shuffled.map((candidate, index) => '候选' + (index + 1) + ' -> ' + candidate.path).join('\n'), + shown.map((candidate, index) => '候选' + (index + 1) + ' -> ' + candidate.path).join('\n'), '', '不要写脚本、不要裁图、不要量像素。看图直接选,按用户原话判断哪张最合适,写明理由。', - '工具调用上限 2 次。只输出 JSON:', + '工具调用上限 ' + budget.limit + ' 次。只输出 JSON:', '{"choice":1,"reason":"..."}', ].join('\n'); try { - const { data } = await deps.agent.runJson(session, { step: 'select', instruction, maxTools: DEFAULTS.toolBudget.select }); - const index = Math.max(0, Math.min(shuffled.length - 1, Number(data.choice || 1) - 1)); - image.chosen = shuffled[index]; + const picked = await deps.agent.runJson(session, { step: 'select', instruction, maxTools: budget.limit, budgetNote: budget.note }); + noteToolHit(image, 'select', picked); + const { data } = picked; + const index = Math.max(0, Math.min(shown.length - 1, Number(data.choice || 1) - 1)); + image.chosen = shown[index]; image.chosenReason = data.reason || ''; } catch { - image.chosen = shuffled[0]; + // 选优回合没跑成(比如模型服务这时也在报错):按分数取最好的一张,不能取打乱后的第一张。 + image.chosen = rankCandidates(shown)[0]; image.chosenReason = '选优节点失败,按验收分数回退'; } image.status = 'unpassed'; deps.emit(session, 'select', image.id + ' 选优交付:' + image.chosen.id + '(' + image.chosenReason + ')', { detail: detail('选优', [ kv('候选池', pool.map((candidate) => candidate.id).join('、')), - kv('送进对比图的顺序', shuffled.map((candidate, index) => (index + 1) + '=' + candidate.id).join(',')), + kv('送进对比图的顺序', shown.map((candidate, index) => (index + 1) + '=' + candidate.id).join(',')), kv('选中', image.chosen.id), text('理由', image.chosenReason), list( @@ -2325,6 +5920,15 @@ function lastDelivered(session, image, deps) { preview: item.previewFile ? join(deps.dirs.deliver, item.previewFile) : null, size: item.size || '', program: item.program || null, + // 不带字的底图(程序排字时):之后改字、改画面都从它开始。 + basePath: item.baseFile && /^[A-Za-z0-9._-]+$/.test(item.baseFile) ? join(deps.dirs.images, item.baseFile) : null, + // 只换二维码时要沿用这几项:图是同一张,评审结论和请求号都不该丢。 + verdict: item.verdict || null, + requestId: item.requestId || '', + actionPrompt: item.prompt || '', + revisedPrompt: item.revisedPrompt || '', + status: item.status || '', + interrupted: item.interrupted || '', fromDelivery: true, }; } @@ -2332,12 +5936,60 @@ function lastDelivered(session, image, deps) { } async function editPlanNode(session, instruction, deps) { + // 追问可能指向任何一张图:每张交付图、每张参考图都可能要打开看(最多各算 3 张),再加一次对照底图或原图 + // (真机 5 次追问编辑里 4 次撞到以前固定的 4 次上限)。难度按最难的那张图算。 + const delivered = session.images.filter((image) => image.chosen || lastDelivered(session, image, deps)).length; + const fontFacts = await fontFactsLine(); + const budget = toolBudget('edit', { + needs: [ + { count: Math.min(3, Math.max(1, delivered)), what: '打开交付图' }, + { count: Math.min(3, session.refs.length), what: '打开参考图' }, + { count: 1, what: '对照底图或原图' }, + ], + difficulty: hardestOf(session.images).level, + hits: hitsOf(session, 'edit'), + }); const inventory = session.images .map((image) => { const chosen = image.chosen || lastDelivered(session, image, deps); - return image.id + '「' + image.title + '」' + (chosen ? ' 当前交付 ' + chosen.id + ' -> ' + chosen.path : '(无交付)'); + const codes = (image.spec && image.spec.qrCodes) || []; + const qr = codes.length + ? ';画面里有二维码:' + + codes.map((item) => (item.content ? '内容「' + item.content + '」' : '装饰码(还没给内容)')).join('、') + : ''; + const layers = overlayLayers(image); + const words = isOverlay(image) + ? ';文字由程序排入(底下有一张不带字的底图):' + + layers + .map( + (layer, index) => + '第 ' + index + ' 段「' + shortText(layer.text, 30) + '」' + + JSON.stringify({ box: layer.box, direction: layer.direction, font: layer.font, color: layer.color, align: layer.align, punctuation: layer.punctuation || undefined, role: layer.role || undefined, part: layer.part || undefined }), + ) + .join(';') + : layerTexts(image.spec.textLayers).length + ? ';画面里的文字是模型直接画的' + + (textPlaceable(session, image) ? '' : '(位置未知)') + + ':' + + layerTexts(image.spec.textLayers).map((value, index) => '第 ' + index + ' 段「' + shortText(value, 30) + '」').join('、') + : ''; + const charts = imageCharts(image).length + ? ';图表由程序画入(底下有一张不带图表的底图):' + + imageCharts(image) + .map( + (chart, index) => + '第 ' + index + ' 张 ' + describeChart(chart) + + JSON.stringify({ box: chart.box, surface: chart.surface || undefined, labels: chart.labels, stacked: chart.stacked || undefined, y_min: chart.yMin === null ? undefined : chart.yMin, colors: chart.colors.length ? chart.colors : undefined }), + ) + .join(';') + : ''; + return ( + image.id + '「' + image.title + '」' + (chosen ? ' 当前交付 ' + chosen.id + ' -> ' + chosen.path : '(无交付)') + qr + words + charts + + (isAnimation(image) ? animationInventory(image) : motionGifInventory(image) + panelGifInventory(image)) + ); }) .join('\n'); + const history = refHistory(session); const prompt = [ '你是H3-Image-Executor 的增量编辑节点。用户要在已交付的图上做一处改动。', '', @@ -2348,28 +6000,150 @@ async function editPlanNode(session, instruction, deps) { inventory, '', '参考图:', - session.refs.map((ref) => ref.id + ' -> ' + ref.path).join('\n') || '(无)', + session.refs.map((ref) => ref.id + ' -> ' + ref.path + (ref.note ? '(' + ref.note + ')' : '')).join('\n') || '(无)', + '', + '这个会话做过的参考图检索:', + history.length + ? history.map((item) => '- ' + item.task_name + '「' + item.query + '」:' + (item.ref ? '找到 ' + item.ref : '没找到')).join('\n') + : '(无)', '', '做指代解析:改哪张图、改哪部分、哪些必须不变。给出:目标图 imageId、要编辑的候选 candidateId(用当前交付的那张)、', '给模型的编辑指令 prompt(只描述要改的地方和必须保持不变的部分)、以及这次新的验收清单 checklist。', '', - 'refs 只在需要引入原图里没有的新实体时才写(例如"把柴犬换成橘猫"),写 R 编号或公网图片地址。', - '只是加文字、挪位置、调色这类改动,refs 一律留空——多塞参考图会让模型照着参考图重画一张全新的图。', + 'refs:这次修改要用的参考图,写 R 编号,或者检索记录 / 这次 ref_queries 里的 task_name;用户在原话里给了图片地址的,可以照抄那个地址。', + '不要凭记忆写网址:你写的地址多半打不开,系统也不会用。要找图就写 ref_queries。', + '只在要引入原图里没有的新实体、或者要照参考图改正某个实体的样子时才写 refs;只是加文字、挪位置、调色这类改动,', + 'refs 一律留空——多塞参考图会让模型照着参考图重画一张全新的图。', 'prompt 里不要用"这张图""原图"以外的模糊指代,直接说要改什么、什么必须保持不变。', '', + '重新找参考图:', + '- 要不要检索由你判断。用户说参考图不对、要重新找时写 ref_queries;这次要加进画面的东西是专有名词或特定称谓', + ' (真人、角色或吉祥物、品牌和产品、logo、车型、地标、具体的作品,或者你不确定长什么样的冷门叫法)而上面还没有它的参考图时,', + ' 建议先写 ref_queries 检索。通用的东西、常见的画风有把握画对就不搜。', + ' 系统会去搜图、下载、筛选,找到的图会自动用到这次修改里。上面检索记录里「没找到」的实体要全部重新写;', + ' 已经找到的,只有用户点名说不对、或者你从交付图上能看出它明显不对时才重写,并在 replaces 里写它的 R 编号。', + '- ref_budget:这次检索最多花多少次调用(0-' + DEFAULTS.refBudgetMax + ',搜图一次 1 次、判定一批候选 1 次,一项顺利时 2 次、要换说法重搜 4–6 次)。', + ' 不写时按每项 2 次、至少 ' + DEFAULTS.refBudgetDefault + ' 次给;最要紧的项写在前面,预算花完后面的就不搜了。', + '- 每项形如 {"task_name":"myaku_myaku","query":"…","alt_query":"…","use":"subject","take":"…","prompt":"…","replaces":""}:', + ' query 写东西的正式名称 + 一个用途词(官方图 / 照片 / 实拍 / 样例),不要堆「手办 摆件 正面」这类泛词;', + ' alt_query 写它在原产地语言或英文里的正式名称;use 写用途(subject 主体外观 / detail 局部 / style 画风 / layout 版式 / scene 场景),', + ' take 一句话写从它身上取什么;prompt 只写「必须是哪个东西(或哪一类东西的真实样例)」和最多一条用途,可以带上用户这次的反馈,', + ' 不要写颜色、五官、肢体数量这类外观细节,也不要写互相冲突、实际拍不到的条件。', + '- 这次只是重新找参考图、没有别的改动时,refs_only 写 true:重新检索一张新图都没找到时,系统不会白白重画一遍。', + '- 写了 ref_queries 时,prompt 和 checklist 里不要再写这些实体的外观细节,写「外观与参考图一致」;', + ' 之前凭记忆写进 prompt 的外观描述,这次一并去掉,否则会和参考图打架。', + '', + '二维码:码由系统生成真码、贴回画面、扫码验证,你只负责记下码里放什么。', + '- 用户这次给了或改了二维码里要放的内容(链接、文字、号码),写进 qr_codes,每项 {"content":"…","where":"…"}:', + ' content 从用户这次的话里逐字复制,一个字符都不要改,不要补全、不要缩写、不要自己编。', + '- 用户这次只是在给二维码内容或换内容、没有别的改动,而且那张图里本来就有二维码时,qr_only 写 true:', + ' 系统直接在原图上把码换成真码,不重新生图,prompt 和 checklist 可以留空。', + '- 用户还要改别的地方,或者图里本来没有二维码要新加一个,qr_only 写 false,qr_codes 照样写。', + '- 用户要把二维码去掉,qr_remove 写 true,并在 prompt 里写清楚去掉它、原位置换成什么。', + '- 这次没提二维码就不写 qr_codes。checklist 里不要写「二维码能扫」这类条目,系统会自己验。', + '', + '文字:', + '- 标着「文字由程序排入」的图,字是程序用字体排上去的。用户这次只改字(改内容、换字体、调大小、挪位置、换颜色、横排竖排、标点),', + ' text_only 写 true,并在 text_layers 里写要改的那几段:每项 {"index":0, 要改的字段},例:{"index":0,"text":"…"}、{"index":0,"font":"song"}。', + ' 可改的字段:text、box([左,上,右,下],0-1 的画面比例)、direction(horizontal / vertical)、font(brush / xing / kai / song / hei)、', + ' color(十六进制)、align(start / center / end)、punctuation(omit / side / inline,竖排才有)、', + ' role(title / subtitle / body / footnote / caption / label / page / pinyin:同一种 role 的几段统一字号;body 首行空两格、两端对齐;', + ' footnote 上方画一条短分隔线)、indent(body 首行空几格,0-4)、rule(footnote 写 false 不画分隔线)、', + ' part(一个字里要两种颜色时写,{"side":"left","color":"#E8710A"}:每个汉字 side 那一侧的部件用这个颜色,其余用 color;写 false 去掉)。', + ' 用户说「几段字号不一样」「段首没空格」「脚注上面要分隔线」,就给对应的段落补上 role;正文里的脚注符号(①②…)写在被注的字后面,程序排成上标。', + ' 用户要「清」的「氵」换个颜色,就给写着「清」的那一段加 part,不要把字拆成「氵」「青」两段拼。', + ' 本机实际用的字体:' + fontFacts + '。', + ' index 是上面写的第几段,只写要改的字段,没写的保持不变;改了内容的,text 写改完之后的整段文字,逐字照用户的话改,其余一个字都不要动。', + ' 字号跟着 box 走,「字大一点」就把 box 放大。要加一段新字,不写 index,text 和 box 必填;要删掉一段,写 {"index":1,"remove":true}。', + ' 系统直接在底图上重新排字,不重新生图,prompt 和 checklist 可以留空。', + '- 程序排字的图,用户还要改画面别的地方:text_only 写 false;字也要跟着改的照样写 text_layers,prompt 里不要写文字内容,字之后由程序重新排。', + '- 标着「模型直接画的」字,用户说写错了、漏字了、要改字时,同样写 text_only true(要改内容就写 text_layers):', + ' 系统会让模型先把字擦掉,再由程序按原文排字。标着「位置未知」的段落,在 text_layers 里补上 box:', + ' 看一眼当前交付的图,写这段字在画面里的位置 [左,上,右,下];不写的话改字交给模型重画。', + '- 模型直接画的字,用户还要改画面别的地方:text_only 写 false,prompt 里逐字写清字要改成什么,text_layers 照样写。', + '- 这次没提文字就不写 text_layers,text_only 写 false。', + '', + '图表:', + '- 标着「图表由程序画入」的图,图表是程序按数据画上去的。用户这次只改图表(改数、加减类目、换图表类型、改标题、单位、颜色、位置、标注方式),', + ' chart_only 写 true,并在 charts 里写要改的那几张:每项 {"index":0, 要改的字段},例:{"index":0,"series":[{"name":"销售额","values":[120,150,200]}]}、{"index":0,"type":"line"}。', + ' 可改的字段:type(bar / hbar / line / area / pie / donut)、title、unit、categories、series、stacked、labels(value / percent / both / none)、', + ' y_min(只有折线图、面积图能用)、box、surface、colors。index 是上面写的第几张,只写要改的字段,没写的保持不变。', + ' 改数据的,categories 和 series 写改完之后的完整数据:数从用户的话里逐个照抄,用户没提到的数一个都不要动,比例、合计由程序算。', + ' 要加一张新图表,不写 index,type、categories、series、box 必填;要删掉一张,写 {"index":1,"remove":true}。', + ' 系统直接在不带图表的底图上重画图表,不重新生图,prompt 和 checklist 可以留空。', + '- 图里原来没有程序画的图表,这次要加一张:chart_only 写 true,charts 照样写(不写 index);画面上那块地方要是不空,', + ' 系统会自动让模型先在 box 的位置留出一块空白面板,再画图表。', + '- 用户还要改画面别的地方:chart_only 写 false;图表也要跟着改的照样写 charts,prompt 里不要描述图表和数字,图表之后由程序重新画。', + '- 这次没提图表就不写 charts,chart_only 写 false。', + '', + '动图(GIF):', + '- 标着「GIF 的参考图,还没出帧」的图:用户确认参考图、说可以开始做动图时,animation 写 {"start":true},prompt 和 checklist 留空。', + ' 用户要先改参考图(换颜色、改姿势、换背景),照常写 prompt 和 checklist,不写 animation:改完系统会再请用户确认。', + '- 标着「这是 GIF」的图,用户只调动图本身时写 animation、prompt 留空,可以同时写几项:', + ' {"frame_ms":500} 每帧停多少毫秒(「慢一点」就加大,「快一点」就减小,60–3000);{"loop":false} 只播一遍;', + ' {"edge":1024} GIF 长边多少像素(240–1024);{"hold":[{"index":1,"ms":800}]} 某几帧停久一点;', + ' {"transition":"cut"} 帧和帧之间直接切换(「别要渐变」「干脆一点」),{"transition":"fade"} 加渐变过渡(「更顺一点」「加过渡」);', + ' {"drop":[6]} 去掉第几帧;{"order":[1,3,2,4]} 新的播放顺序(写出要留下的所有帧号,没写的帧会去掉);', + ' {"redo":[{"index":3,"prompt":"…"}]} 重画第几帧,prompt 写这一帧新的样子:写全这一帧和参考图相比要变的所有地方,', + ' 包括前面帧延续下来、这一帧要保持的状态(重画时模型看得到前一帧和参考图,但不知道计划里这一帧该是什么样),没有新要求就留空;', + ' {"add":[{"after":2,"prompt":"…"}]} 在第几帧后面加一帧,prompt 同样写全新帧和参考图相比要变的所有地方。', + ' 帧号就是上面列出来的第几帧。只调时长、尺寸、顺序、过渡、删帧时不调用生图,几秒就好。', + '- 用户要改的是整组动图里的主体或画面本身(换颜色、换衣服、换背景):照常写 prompt 改参考图,不写 animation。', + ' 系统会先把参考图改好交给用户确认,再重新出全部帧。', + '- 不是 GIF 的分格组图(一张图里分成好几格:六格表情、四格漫画、九宫格、分步图),用户要把它做成 GIF、动图:', + ' animation 写 {"from_panels":true},prompt 和 checklist 留空。系统把格子切开、按顺序拼成 GIF,组图本身不重画;', + ' 相邻两格之间会各补一格中间状态(一次生图、一两分钟),用户说「直接拼就行」「不要补帧」时再加 "smooth":false。', + ' 看得出几行几列就写上 "rows"、"cols";用户点名了顺序、快慢,按下一条的字段一起写。', + ' 图里只有一个画面、没有分格,也照样写 from_panels:系统切不出格子时会如实告诉用户,不会重画。', + '- 标着「组图拼成的 GIF」的图,用户只调 GIF 本身时写 animation、prompt 留空,可以同时写几项:', + ' {"frame_ms":500} 每格停多少毫秒;{"first_ms":1200}、{"last_ms":2000} 第一格、最后一格停多久;{"hold":[{"index":6,"ms":2000}]} 某几格停久一点;', + ' {"order":[1,2,3,6]} 新的播放顺序(写出要留下的所有格子编号);{"drop":[3]} 去掉第几格;{"pingpong":true} 来回播;{"loop":false} 只播一遍;', + ' {"transition":"fade"} 两格之间渐变,{"transition":"cut"} 直接切换;{"edge":1024} GIF 长边(240–1024);', + ' {"bounce":true} 切换时让新的一格先放大一点再落回(用户点名要「弹一下」才写:整幅画面会跟着跳一下);', + ' 用户说切换时画面跳一下、抖一下、闪一下,而这张 GIF 标着切换时弹一下,写 {"bounce":false};', + ' {"rows":2,"cols":3} 格子切错了,按几行几列重新切。格子编号从上到下、从左到右数。', + ' {"smooth":true} 相邻两格之间补过渡格(「更顺一点」「中间补几帧」;之前没补成的会再试一次,要多一次生图),', + ' {"smooth":false} 不补,两格之间直接切换(「不要补帧」「直接切就行」)。清单里写了「补了几步过渡格」的,用户没提就不用写。', + ' 只写用户这次要改的项:上面清单里现有的设定(每格多久、顺序、弹不弹)用户没提到的不要照抄进来;用户只说「重新做成 GIF」,写 {"from_panels":true}。', + '- 用户要让一张不是 GIF 的图绕中心转起来(「让这个加载圈转起来」「logo 转一圈」):animation 写', + ' {"motion":{"type":"spin","center":[0.5,0.5],"radius":0.32,"steps":12,"direction":"cw"}},prompt 留空。center、radius 按这张图里', + ' 转动的东西在哪、多大来写(看一眼交付图),steps 写圆环上等距的点或辐条数,连续转写 0。系统由程序转出各帧,不重新生图。', + '- 标着「程序绕圆心转出来的 GIF」的图,用户只调转法时写 animation、prompt 留空:{"motion":{"frame_ms":60}} 每格停多久(「快一点」就减小,最短 20);', + ' {"motion":{"direction":"ccw"}} 换方向;{"motion":{"steps":8}} 一圈几格(0 是连续转);{"motion":{"center":[0.5,0.48],"radius":0.3}} 圆心不对、', + ' 转的范围不对时按交付图重写;{"transition":"cut"} / {"transition":"fade"} 两格之间直接切换还是渐变;{"loop":false} 只转一圈;{"edge":512} GIF 长边。', + ' 要改画面本身(换颜色、换样式),照常写 prompt 改图,不写 animation,改完系统按新图重新转。', + '- 组图拼成的 GIF,用户要改某一格的画面(「第 3 格改成生气」):照常写 prompt 改组图,写清第几行第几格改成什么、其余格子不变,', + ' 不写 animation;改完系统按新的组图重新拼 GIF。', + '', + '工具调用上限 ' + budget.limit + ' 次:只 read 上面列出的图,判断清楚就直接给结论。', + '', '只输出 JSON:', - '{"imageId":"A","candidateId":"A-r1-g0","prompt":"...","refs":[],"keep":["..."],"checklist":[{"id":"H1","kind":"hard","text":"...","verify":"vlm"}]}', + '{"imageId":"A","candidateId":"A-r1-g0","prompt":"...","refs":[],"ref_queries":[],"ref_budget":0,"refs_only":false,"keep":["..."],"qr_only":false,"qr_remove":false,"qr_codes":[],"text_only":false,"text_layers":[],"chart_only":false,"charts":[],"animation":null,"checklist":[{"id":"H1","kind":"hard","text":"...","verify":"vlm"}]}', ].join('\n'); - const { data } = await deps.agent.runJson(session, { step: 'edit', instruction: prompt, maxTools: DEFAULTS.toolBudget.edit }); + const edited = await deps.agent.runJson(session, { step: 'edit', instruction: prompt, maxTools: budget.limit, budgetNote: budget.note }); + noteToolHit(session, 'edit', edited); + const { data } = edited; if (!data || !data.imageId) return null; return { imageId: String(data.imageId).toUpperCase(), candidateId: data.candidateId || null, prompt: data.prompt || instruction, + promptGiven: Boolean(String(data.prompt || '').trim()), refs: Array.isArray(data.refs) ? data.refs : [], + refQueries: normalizeRefQueries(data.ref_queries), + refBudget: data.ref_budget, + refsOnly: data.refs_only === true, checklist: Array.isArray(data.checklist) && data.checklist.length ? normalizeChecklist(data.checklist, [], String(data.prompt || '')) : null, + qrOnly: data.qr_only === true, + qrRemove: data.qr_remove === true, + qrCodes: Array.isArray(data.qr_codes) && data.qr_codes.length ? data.qr_codes : null, + textOnly: data.text_only === true, + textLayers: Array.isArray(data.text_layers) && data.text_layers.length ? data.text_layers : null, + chartOnly: data.chart_only === true, + charts: Array.isArray(data.charts) && data.charts.length ? data.charts : null, + animation: normalizeAnimationOps(data.animation), }; } @@ -2395,6 +6169,15 @@ async function crossImageReview(session, deps) { const characters = session.characterRefs || []; const refSheet = characters.length ? await ensureRefSheet(session, deps) : null; + // 对比图、基准图材料各打开一次;看不清时还要核对原图,靠按难度给的余量(实测上限 3 次会在读基准图时就被打断)。 + const budget = toolBudget('cross', { + needs: [ + { count: 1, what: '并排对比图' }, + { count: refSheet ? 1 : 0, what: '角色基准图材料' }, + ], + difficulty: hardestOf(picked).level, + hits: hitsOf(session, 'cross'), + }); const instruction = [ '你是H3-Image-Executor 的跨图一致性评审节点。下面这张并排对比图把同一次任务里的所有成图放在一起了,', '每张左上角的编号就是图 id。你只看这一张,不要再去打开原始大图。', @@ -2425,10 +6208,12 @@ async function crossImageReview(session, deps) { '', '只输出一个 JSON 对象,不要解释、不要 markdown 代码块:', '{"consistent":true,"summary":"一句话结论","issues":[{"image":"C","subject":"小狐狸","problem":"..."}]}', + '工具调用上限 ' + budget.limit + ' 次。', ].join('\n'); - // 预算 4 次:对比图、基准图材料、必要时核对原图,一次收尾。实测 3 次会在读基准图时就被打断。 - const { data } = await deps.agent.runJson(session, { step: 'cross', instruction, maxTools: 4 }); + const crossRun = await deps.agent.runJson(session, { step: 'cross', instruction, maxTools: budget.limit, budgetNote: budget.note }); + noteToolHit(session, 'cross', crossRun); + const { data } = crossRun; const issues = Array.isArray(data && data.issues) ? data.issues .map((item) => ({ @@ -2494,7 +6279,31 @@ async function deliver(session, deps) { // 同一批交付共用一个时间戳:它们属于同一次交付,文件名带同一个时刻更好归档。 const stampText = fileStamp(new Date()); const usedNames = new Set(); + // 二维码要请用户处理的事只挂在这一版交付上(见 qrQuestions)。 + const qrNotes = []; + // 交付文件名:H3-Image_<内容英文简写>_<时间戳>。规划节点给的 slug 可能重复(同一个主体出现在多张图里), + // 重名会互相覆盖,这里补序号。 + const nameFor = (image) => { + const slug = normalizeSlug(image.spec && image.spec.slug, String(image.id).toLowerCase()); + let base = 'H3-Image_' + slug + '_' + stampText; + for (let n = 2; usedNames.has(base); n += 1) base = 'H3-Image_' + slug + '-' + n + '_' + stampText; + usedNames.add(base); + return base; + }; for (const image of session.images) { + const anim = isAnimation(image) ? image.animation : null; + // 这一轮追问没做成的组图 GIF(不是分格的组图):只在这一版交付里说一次。 + if (image.panelGifNotice) { + qrNotes.push(image.panelGifNotice); + image.panelGifNotice = ''; + } + // 合成好的 GIF(或者用户给的参考图直接出帧、却没做成的)。 + if (anim && (anim.state === 'done' || (anim.state === 'failed' && !image.chosen))) { + const { item, notes } = await deliverAnimation(session, image, deps, animationHooks(), { dir, version: 'v' + version, base: nameFor(image) }); + items.push(item); + qrNotes.push(...notes); + continue; + } if (!image.chosen) { // reason 以前只取 blockReason,而生成失败根本没走阻塞那条路, // 结果页面上只剩一句「未生成」,用户完全看不到接口报了什么。 @@ -2509,20 +6318,51 @@ async function deliver(session, deps) { }); continue; } - // 交付文件名:H3-Image_<内容英文简写>_<时间戳>.<真实格式>。 - // 规划节点给的 slug 可能重复(同一个主体出现在多张图里),重名会互相覆盖,这里补序号。 + // 程序转的 GIF、组图拼的 GIF:同一张图、同样的设定做过就直接用,图改过、设定改过才重做。 + const motionState = !anim && image.spec.motionGif ? await composeMotionGif(session, image, deps, panelHooks()) : null; + const panelState = !anim && !motionState && image.spec.panelGif ? await composePanelGif(session, image, deps, panelHooks()) : null; const chosenExt = extensionFor(await readFile(image.chosen.path)); - const slug = normalizeSlug(image.spec && image.spec.slug, String(image.id).toLowerCase()); - let base = 'H3-Image_' + slug + '_' + stampText; - for (let n = 2; usedNames.has(base); n += 1) base = 'H3-Image_' + slug + '-' + n + '_' + stampText; - usedNames.add(base); + const base = nameFor(image); const deliverName = base + chosenExt; const previewName = base + '-preview.png'; const target = join(dir, deliverName); await copyFile(image.chosen.path, target); const preview = join(dir, previewName); if (image.chosen.preview) await copyFile(image.chosen.preview, preview); - items.push({ + const qr = qrDelivery(image, image.chosen); + qrNotes.push(...qrQuestions(image, qr)); + const typed = textDelivery(image, image.chosen); + if (typed && typed.status === 'failed') { + qrNotes.push(image.id + ' 的文字没能由程序排进去(' + typed.note.replace(/^文字没排上:/, '') + ')。可以回复「重画」重新生成这张图'); + } + const droppedText = (image.spec.textOverlay && image.spec.textOverlay.dropped) || []; + if (typed && droppedText.length) { + qrNotes.push( + image.id + ' 的文字超过 ' + MAX_TEXT_LAYERS + ' 段,这几段没排进去:' + droppedText.map((value) => '「' + shortText(value, 12) + '」').join('') + + '。可以把相邻的几段合成一段再试', + ); + } + // 部件分色没做成、或者为了分色换了字体:照实告诉用户,免得以为颜色是对的。 + if (typed && typed.status === 'overlay') { + const summaryItems = (image.chosen && image.chosen.program && image.chosen.program.text && image.chosen.program.text.items) || []; + for (const item of summaryItems) { + if (!item.part) continue; + if (!item.part.ok) qrNotes.push(image.id + ' 的「' + shortText(item.text, 8) + '」部件分色没做成' + (item.part.note ? ':' + item.part.note : '')); + else if (item.part.note) qrNotes.push(image.id + ' 的「' + shortText(item.text, 8) + '」' + item.part.note); + } + } + // 规划改过一次文字层,按版面还是读不出用户要的字:交付时照实说。 + if ((image.spec.readingProblems || []).length) { + qrNotes.push(image.id + ' 的文字按版面读不出' + image.spec.readingProblems.map((item) => '「' + item.quote + '」').join('、') + ',请核对这几处'); + } + const charted = chartDelivery(image, image.chosen); + qrNotes.push(...chartQuestions(image, charted)); + // 出图中途停下、交的是停下前的图:交付卡片上要看得到,不能只藏在折叠的详情里。 + const interrupted = image.interrupted || ''; + if (interrupted) { + qrNotes.push(image.id + ' 出图中途停下(' + interrupted + '),交付的是停下前已有的图。稍后可以在这个会话里说「重画」再试一次'); + } + const item = { id: image.id, title: image.title, status: image.status, @@ -2537,8 +6377,53 @@ async function deliver(session, deps) { program: image.chosen.program || null, prompt: image.chosen.actionPrompt || image.spec.prompt, revisedPrompt: image.chosen.revisedPrompt, - }); + qr, + text: typed, + chart: charted, + baseFile: (image.chosen.basePath && image.chosen.baseFile) || null, + interrupted, + }; + // 图照常交付,GIF 和帧条跟着这一版一起写进交付目录。 + if (motionState && motionState.state === 'done') { + const gifName = base + '.gif'; + const stripName = base + '-frames.png'; + await copyFile(join(deps.dirs.images, motionState.gifFile), join(dir, gifName)); + await copyFile(join(deps.dirs.images, motionState.stripFile), join(dir, stripName)); + item.gif = motionGifItem(motionState, { gif: 'v' + version + '/' + gifName, strip: 'v' + version + '/' + stripName }); + } else if (motionState && motionState.state === 'failed') { + qrNotes.push(image.id + ' 没转成 GIF(' + motionState.error + '),交付的是这张图。可以说「重新转」再试'); + } + if (panelState && panelState.state === 'done') { + const gifName = base + '.gif'; + const stripName = base + '-frames.png'; + await copyFile(join(deps.dirs.images, panelState.gifFile), join(dir, gifName)); + await copyFile(join(deps.dirs.images, panelState.stripFile), join(dir, stripName)); + item.gif = panelGifItem(panelState, { gif: 'v' + version + '/' + gifName, strip: 'v' + version + '/' + stripName, tweenNote: tweenNoteFor(image, panelState) }); + } else if (panelState && panelState.state === 'failed') { + qrNotes.push( + image.id + ' 的组图没拼成 GIF(' + panelState.error + '),交付的是组图。' + + (panelState.notGrid ? '确定是分格的组图的话,可以说「按 2 行 3 列切成 GIF」这样指定几行几列' : '可以说「重新拼 GIF」再试'), + ); + // 切不出格子的设定不留着:不然之后每次交付都会再切一遍、再挂一遍这句。说了几行几列会重新记下。 + if (panelState.notGrid) { + image.spec.panelGif = null; + image.panelGif = null; + } + } + // 动图的参考图:先交给用户确认,帧等确认之后再出。 + if (anim && (anim.state === 'awaiting_confirm' || !anim.state)) { + item.animation = awaitingInfo(image); + qrNotes.push( + image.id + ' 是 GIF 的参考图:满意就点「生成 GIF」(或者回复「用这张做 GIF」),开始出 ' + item.animation.frames.length + + ' 帧;要改参考图就直接说改哪里。1 分钟内没有回应会默认确认、自动开始出帧,不想自动开始就点「先别生成」', + ); + } else if (anim && anim.state === 'failed') { + item.animation = { state: 'failed', error: anim.error || '' }; + qrNotes.push(image.id + ' 的 GIF 没做成(' + (anim.error || '能用的帧不到 2 帧') + '),交付的是参考图。可以说「重新出帧」再试'); + } + items.push(item); } + const questions = session.questions.concat(qrNotes); // 跨图结论要给页面看,但对比图是本地绝对路径,不能带出去。 const cross = session.crossCheck ? { @@ -2553,7 +6438,7 @@ async function deliver(session, deps) { instruction: session.instruction, route: session.route, items, - questions: session.questions, + questions, crossCheck: cross, cost: session.cost, }; @@ -2562,7 +6447,7 @@ async function deliver(session, deps) { version, dir, items, - questions: session.questions.slice(), + questions: questions.slice(), crossCheck: cross, cost: { ...session.cost }, }; @@ -2573,7 +6458,7 @@ async function deliver(session, deps) { turn.delivery = { version, items, - questions: session.questions.slice(), + questions: questions.slice(), crossCheck: cross, cost: { ...session.cost }, }; @@ -2589,7 +6474,9 @@ async function deliver(session, deps) { session.fatal || failedItems.map((item) => item.id + ' ' + item.reason).join(';').slice(0, 500); } else { - session.status = session.fatal ? 'failed' : 'delivered'; + // 交出了图就是「已交付」:中途出过错(配置错误、接口故障)的原因已经写在交付卡片和待确认里。 + // 以前只要 fatal 有值就整轮标失败,页面上图明明在,状态却是「失败」。 + session.status = okCount || !session.fatal ? 'delivered' : 'failed'; } const deliverText = okCount ? '交付 v' + version + ':' + okCount + ' 张图' + (failedItems.length ? '(' + failedItems.length + ' 张失败)' : '') @@ -2606,6 +6493,13 @@ async function deliver(session, deps) { kv('候选', item.candidateId || '-'), kv('轮次', item.rounds || 0), kv('request_id', item.requestId || '-'), + item.qr + ? kv( + '二维码', + item.qr.items.map((one) => (one.content ? '「' + one.content + '」' : '') + one.note).join(';'), + ) + : null, + item.text ? kv('文字', item.text.note) : null, text('理由', item.reason || ''), ]), ), @@ -2630,7 +6524,7 @@ async function deliver(session, deps) { kv('生图', session.cost.ditImages + ' 张 · ' + seconds(session.cost.ditMs)), kv('Agent', session.cost.agentCalls + ' 次 · ' + seconds(session.cost.agentMs) + ' · ' + (session.cost.agentTools || 0) + ' 次工具'), session.cost.usage ? kv('token', session.cost.usage.total_tokens) : null, - list('待你确认', session.questions.map((item) => ({ text: item, tag: 'warn' }))), + list('待你确认', questions.map((item) => ({ text: item, tag: 'warn' }))), ]), }); // 交付完立刻把分享页备好:发布是 Agent 侧的事,落盘提前做完, @@ -2650,6 +6544,8 @@ function summarizeAction(action) { prompt: action.prompt, parentId: action.parentId || null, refs: action.refs || [], + overlay: Boolean(action.overlay), + ...(action.textPatches ? { textPatches: action.textPatches } : {}), }; } @@ -2659,7 +6555,10 @@ function describeAction(action) { if (action.parentPath) return '在 ' + (action.parentId || '上一张') + ' 上编辑'; return '按参考图生成(没有可用底图)'; } - if (action.type === 'generate') return '重新生成'; + if (action.type === 'generate') return '重新生成' + (action.overlay ? '(不带字的底图)' : ''); + if (action.type === 'qr') return '只换二维码(不重新生图)'; + if (action.type === 'text' && action.textPatches) return '在 ' + (action.parentId || '上一张') + ' 的底图上挪 ' + action.textPatches.length + ' 段字重新排(不重新生图)'; + if (action.type === 'text') return '只改文字(在不带字的底图上重新排,不重新生图)'; return action.type; } diff --git a/plugins/liddalidd/h3-image-executor/miniapp/node/server.mjs b/plugins/liddalidd/h3-image-executor/miniapp/node/server.mjs index 24b6d803..dfe0549f 100644 --- a/plugins/liddalidd/h3-image-executor/miniapp/node/server.mjs +++ b/plugins/liddalidd/h3-image-executor/miniapp/node/server.mjs @@ -9,6 +9,10 @@ import { runSession, runIncrementalEdit, DEFAULTS } from './lib/workflow.mjs'; import { detail, text } from './lib/detail.mjs'; import { buildShareHtml } from './lib/share.mjs'; import { createRefCache } from './lib/refcache.mjs'; +import { confirmText, restoreAnimation, snapshotAnimation } from './lib/animation.mjs'; +import { restorePanelGif, restorePanelGifSpec, snapshotPanelGif } from './lib/panelgif.mjs'; +import { normalizeMotionGif, restoreMotionGif, snapshotMotionGif } from './lib/motiongif.mjs'; +import { restoreTweens, snapshotTweens } from './lib/paneltween.mjs'; import { readClipboard, classifyClipboard, disposeClipboard, writeClipboard } from './lib/clipboard.mjs'; import { publishHtml } from './lib/publish.mjs'; @@ -24,10 +28,19 @@ function imageContentType(buffer) { if (buffer.length > 3 && buffer[0] === 0xff && buffer[1] === 0xd8 && buffer[2] === 0xff) return 'image/jpeg'; if (buffer.length > 12 && buffer.subarray(0, 4).toString('ascii') === 'RIFF' && buffer.subarray(8, 12).toString('ascii') === 'WEBP') return 'image/webp'; + if (buffer.length > 6 && /^GIF8[79]a$/.test(buffer.subarray(0, 6).toString('ascii'))) return 'image/gif'; return 'application/octet-stream'; } const MAX_BODY = 24 * 1024 * 1024; +// GIF 参考图交付后等多久没有回应就默认确认、自动开始出帧(ms)。staleMs:运行时重启时,倒计时到点已经过了这么久的 +// 不再自动开始——应用关了好几天再打开,不该突然替用户出帧、花掉接口调用。导出成对象是为了测试里能调短。 +export const AUTO_CONFIRM = { ms: 60000, staleMs: 10 * 60 * 1000 }; + +function waitText(ms) { + return ms >= 60000 && ms % 60000 === 0 ? ms / 60000 + ' 分钟' : Math.round(ms / 1000) + ' 秒'; +} + export async function start(context) { const root = context.pluginRoot; const clientDir = join(root, 'miniapp/client'); @@ -137,7 +150,17 @@ export async function start(context) { const data = parseLooseJson(raw); if (!data || !data.id || sessions.has(data.id)) continue; const restored = await rehydrate(data, dir); - if (restored) sessions.set(restored.id, restored); + if (!restored) continue; + sessions.set(restored.id, restored); + // 重启前还在等确认的参考图:点过「先别生成」的保持暂停;倒计时还在走(或者刚到点不久)的重新计满, + // 不立刻开跑——用户可能正准备回复。到点已经过了很久的(应用关了几天),不替用户开始。 + const pending = data.autoConfirm; + if (pending && awaitingImage(restored) === pending.imageId) { + if (pending.held) restored.autoConfirm = { imageId: pending.imageId, deadline: 0, held: true }; + else if (Date.now() - (Number(pending.deadline) || 0) < AUTO_CONFIRM.staleMs) { + armAutoConfirm(restored, AUTO_CONFIRM.ms, pending.imageId + ' 的参考图还在等你确认(应用重启过,倒计时重新开始):' + waitText(AUTO_CONFIRM.ms) + '内没有回应就默认确认,自动开始出帧'); + } + } } if (sessions.size) context.logger.info('h3.sessions.restored', { count: sessions.size }); } @@ -161,13 +184,15 @@ export async function start(context) { // 所以这里按"轮"记账:一轮 = { 用户这句话, 从哪条事件开始, 这一轮交付了什么 }。 // 事件不需要额外打标:下一轮的 startSeq 就是上一轮的右边界。 - function startTurn(session, instruction) { + function startTurn(session, instruction, options) { const turn = { instruction, at: Date.now(), startSeq: session.eventSeq || 0, finishedAt: 0, delivery: null, + // 用户没回应、到点自动开始的一轮:页面上要标出来,用户回来时才知道这一轮不是他发的。 + auto: Boolean(options && options.auto), // 成本是会话级累加的,扣掉这一轮开始时的基数才是"这一轮花了多少"。 baseCost: { ditImages: session.cost.ditImages, @@ -221,6 +246,7 @@ export async function start(context) { index, instruction: turn.instruction || '', at: turn.at, + auto: Boolean(turn.auto), // 下面几个是记账用的原始字段:rehydrate 全靠它们把事件重新切回各轮,不能省。 startSeq: from, finishedAt: turn.finishedAt || 0, @@ -278,6 +304,77 @@ export async function start(context) { return session.persistChain; } + // ---------------------------------------------------------------- GIF 参考图的自动确认 + // + // 参考图交付后等用户确认(点「生成 GIF」或回复)。AUTO_CONFIRM.ms 内没有任何回应就当作确认,自动开始出帧。 + // 计时放在服务端:页面关了、切走了都照样到点。这段时间里用户发了消息、点了「先别生成」、停止或删除会话,倒计时就取消。 + // 状态记在 session.autoConfirm 上并落盘:{ imageId, deadline, held },held 是用户点了「先别生成」,重启之后也不再自动开始。 + const autoTimers = new Map(); + let disposed = false; + + function awaitingImage(session) { + const items = (session.delivery && session.delivery.items) || []; + const item = items.find((one) => one && one.animation && one.animation.state === 'awaiting_confirm'); + return item ? item.id : ''; + } + + function stopAutoTimer(session) { + const timer = autoTimers.get(session.id); + if (timer) clearTimeout(timer); + autoTimers.delete(session.id); + } + + // 交付完(或者重启恢复时)调用:交付里有等确认的参考图就开始倒计时,没有就清掉。 + function armAutoConfirm(session, delayMs, note) { + stopAutoTimer(session); + const imageId = awaitingImage(session); + if (!imageId || disposed || session.status === 'cancelled') { + session.autoConfirm = null; + return; + } + session.autoConfirm = { imageId, deadline: Date.now() + delayMs, held: false }; + const timer = setTimeout(() => { + autoTimers.delete(session.id); + void fireAutoConfirm(session.id); + }, delayMs); + // 只为这个计时器不必撑着进程不退:dispose 时会统一清掉。 + if (typeof timer.unref === 'function') timer.unref(); + autoTimers.set(session.id, timer); + push(session, 'plan', note || imageId + ' 的参考图等你确认:' + waitText(delayMs) + '内没有回应就默认确认,自动开始出帧'); + } + + // 用户有了任何回应:倒计时作废。 + function cancelAutoConfirm(session) { + stopAutoTimer(session); + session.autoConfirm = null; + } + + function holdAutoConfirm(session) { + if (!session.autoConfirm || session.autoConfirm.held) return false; + stopAutoTimer(session); + session.autoConfirm = { imageId: session.autoConfirm.imageId, deadline: 0, held: true }; + push(session, 'plan', '已暂停自动开始:确认参考图之后点「生成 GIF」,或者直接说要改哪里'); + return true; + } + + async function fireAutoConfirm(id) { + const session = sessions.get(id); + if (!session || !session.autoConfirm || session.autoConfirm.held || disposed) return; + const imageId = session.autoConfirm.imageId; + session.autoConfirm = null; + // 到点再核对一遍:还在等这张参考图、没有在跑别的、没被停止。 + if (session.busy || session.taskActive || session.status === 'cancelled' || awaitingImage(session) !== imageId) { + void persist(session); + return; + } + context.logger.info('h3.auto_confirm.fired', { sessionId: session.id, imageId }); + try { + await followUp(session, confirmText(imageId), { auto: true }); + } catch (error) { + context.logger.error('h3.auto_confirm.failed', { sessionId: session.id, message: String(error && error.message ? error.message : error) }); + } + } + // 出图前的关键确认:工作流在闸门这里挂起,等页面回答,或者用户直接选"按默认方案"。 const gates = new Map(); @@ -297,11 +394,15 @@ export async function start(context) { return Boolean(resolve); } - // 重跑:拿原会话的需求和参考图开一个新会话。参考图在磁盘上,转成 dataUrl 走同一条创建路径, - // 这样重跑出来的会话和手输需求创建的完全一致,不会多出一种特殊结构。 + // 重跑:只带原需求和你当时手动上传的图,别的一概不带。以前会把这个会话里的参考图全带上:检索来的、 + // 程序画的角色基准图和 GIF 参考图,到了新会话里全变成「你上传的」,规划拿它们当参考, + // 新的一次等于照着上一次的结果再画(真机 20261117-21581a 的 GIF 参考图就会被带过去)。 + // 上传的图在磁盘上,转成 dataUrl 走同一条创建路径,重跑出来的会话和手输需求创建的完全一致。 async function rerunSession(source) { + const all = source.refs || []; + const uploaded = all.filter((ref) => ref.origin === 'user'); const refs = []; - for (const ref of (source.refs || []).slice(0, 8)) { + for (const ref of uploaded.slice(0, 8)) { try { const buffer = await readFile(ref.path); const ext = String(ref.file || '').split('.').pop().toLowerCase() || 'png'; @@ -314,7 +415,7 @@ export async function start(context) { // 参考图读不到就跳过:重跑本身不该因为少一张图而失败。 } } - return createSession({ instruction: source.instruction, refs }); + return createSession({ instruction: source.instruction, refs, rerun: { from: source.id, left: all.length - uploaded.length } }); } // 删除会话:先把整个目录挪进 .trash 再从内存摘掉,不直接 rm。 @@ -325,6 +426,7 @@ export async function start(context) { // 正在跑的会话不允许删:agent 还在往这个目录里写,删了会写出一堆无主文件。 // status 也要看:重启后 busy 会被恢复成 false,只看它会漏掉「看起来在跑」的那种。 if (session.busy || session.taskActive || session.status === 'running') return { error: 'busy' }; + cancelAutoConfirm(session); sessions.delete(id); try { const trash = join(dataRoot, '.trash'); @@ -510,6 +612,8 @@ export async function start(context) { deliveryVersion: 0, // 角色基准图:多图时先出的外观基准,只做内部参考,不进交付。 characterRefs: [], + // 追问里重新检索参考图的记录。 + editRefQueries: [], // 跨图一致性评审结论,所有图出完之后才写得出来。 crossCheck: null, fatal: '', @@ -519,6 +623,16 @@ export async function start(context) { sessions.set(id, session); startTurn(session, session.instruction); push(session, 'input', '收到需求:' + session.instruction + (refs.length ? '(附 ' + refs.length + ' 张参考图)' : '')); + // 重跑来的:写清带了什么、没带什么。只是一条记录,不进任何提示词。 + const rerun = payload.rerun && typeof payload.rerun === 'object' ? payload.rerun : null; + if (rerun && SAFE_NAME.test(String(rerun.from || ''))) { + push( + session, + 'input', + '重跑 ' + rerun.from + ':只带原需求' + (refs.length ? '和你上传的 ' + refs.length + ' 张图' : '') + + (Number(rerun.left) > 0 ? ',上一次检索来的、程序画的 ' + Number(rerun.left) + ' 张参考图和结果都没带' : ''), + ); + } // taskActive 一直到后台任务真正结束才放下。「停止」会立刻把 busy 置回 false, // 但被中止的任务还要一点时间收尾;这期间不许追问、不许删除,否则同一个会话上会跑两个任务。 @@ -537,23 +651,34 @@ export async function start(context) { session.busy = false; session.finishedAt = Date.now(); finishTurn(session); + armAutoConfirm(session, AUTO_CONFIRM.ms); await persist(session); } })(); return session; } - async function followUp(session, instruction) { + // options.auto:到点自动确认发起的这一轮(见 fireAutoConfirm)。 + async function followUp(session, instruction, options) { if (session.busy || session.taskActive) throw new Error('busy'); + const auto = Boolean(options && options.auto); + // 用户发了任何话(包括点「生成 GIF」)都算回应了,倒计时作废。 + cancelAutoConfirm(session); session.busy = true; session.status = 'running'; session.finishedAt = 0; if (!session.config) session.config = await h3.resolveConfig(true); // 这一轮的起点必须记在"收到追问"这条事件之前,它自己属于新一轮。 - startTurn(session, instruction); - push(session, 'edit', '收到追问:' + instruction, { - detail: detail('增量编辑请求', [text('用户这次的话', instruction)]), - }); + startTurn(session, instruction, { auto }); + if (auto) { + push(session, 'edit', waitText(AUTO_CONFIRM.ms) + '内没有回应,按默认确认:用这张参考图开始出 GIF', { + detail: detail('自动确认', [text('按这句话开始', instruction)]), + }); + } else { + push(session, 'edit', '收到追问:' + instruction, { + detail: detail('增量编辑请求', [text('用户这次的话', instruction)]), + }); + } session.taskActive = true; session.task = (async () => { try { @@ -568,6 +693,7 @@ export async function start(context) { session.busy = false; session.finishedAt = Date.now(); finishTurn(session); + armAutoConfirm(session, AUTO_CONFIRM.ms); await persist(session); } })(); @@ -604,13 +730,33 @@ export async function start(context) { gate: session.gate, permissions: session.permissions, pendingQuestion: session.pendingQuestion, + // GIF 参考图的自动确认:页面画倒计时和「先别生成」,重启时靠它决定要不要重新计时。 + autoConfirm: session.autoConfirm + ? { imageId: session.autoConfirm.imageId, deadline: session.autoConfirm.deadline || 0, held: Boolean(session.autoConfirm.held), ms: AUTO_CONFIRM.ms } + : null, + // 自动开始的那一轮气泡上要写明等了多久。 + autoConfirmMs: AUTO_CONFIRM.ms, // origin 决定这张图算"你给的"还是"系统搜的":用户气泡只能放前者,搜到的属于执行轨迹。 refs: session.refs.map((ref) => ({ id: ref.id, file: 'refs/' + ref.file, note: ref.note, origin: ref.origin || (ref.source ? 'search' : 'user'), + // 检索来的图是从哪个地址下的。追问里用户说某张参考图不对时,重新检索要靠它把那张排除掉; + // 不落盘的话,重启之后同一张错图还会被再搜回来。 + source: ref.source || '', + // 这张参考图拿来做什么、取它什么:重启后的追问编辑照样要逐张告诉模型。 + use: ref.use || '', + take: ref.take || '', })), + // 追问里重新检索过的项。编辑节点要靠它知道哪些实体已经找到、哪些还没找到。 + editRefQueries: session.editRefQueries || [], + // 规划给的检索预算和实际用了多少:排查「为什么没搜」「为什么搜了一半就停」用。 + refBudget: session.refBudget + ? { limit: session.refBudget.limit, used: session.refBudget.used, searches: session.refBudget.searches, gates: session.refBudget.gates, stopped: session.refBudget.stopped || [] } + : null, + // 追问编辑、跨图评审撞过几次工具调用上限:重启后同一个节点照样多给(见 budget.mjs)。 + toolHits: session.toolHits || null, // 角色基准图要跟着会话活过重启:跨图评审靠它判断"本组本该一致", // 前端也要靠它说明每张图的外观基准是谁。path 是绝对路径,不能带出去。 characterRefs: (session.characterRefs || []).map((ref) => ({ @@ -676,10 +822,42 @@ export async function start(context) { // 丢了它,重启后追问时 executeAction 就不会再强制带上角色图,主体会漂回各画各的。 characterRefs: image.spec.characterRefs || [], textLayers: image.spec.textLayers, + // 二维码内容必须落盘:重启后用户补一句「码换成 xxx」,要靠它判断这张图本来有没有码。 + qrCodes: image.spec.qrCodes || [], + // 程序排字的位置、书体和不带字的底图 prompt:重启后改字、重画都靠它。 + textOverlay: image.spec.textOverlay || null, + basePrompt: image.spec.basePrompt || '', + // GIF 的帧安排(每帧怎么变、每帧多久、循环):重启后调节奏、重画某一帧都靠它。 + animation: image.spec.animation || null, + // 组图拼 GIF 的节奏(切格提示、每格多久、顺序):重启后调快慢、改组图之后重拼都靠它。 + panelGif: image.spec.panelGif || null, + // 程序转圈 GIF 的转法(圆心、格数、方向、每格多久):重启后调快慢、换方向、改图之后重转都靠它。 + motionGif: image.spec.motionGif || null, + // 规划判的难度:重启后的追问、重画照样按它定各节点的工具调用上限。 + difficulty: image.spec.difficulty || '', + difficultyReason: image.spec.difficultyReason || '', + // 规划的文字层按版面读不出用户引号里的字(规划改过一次还是这样):评审提醒、交付说明靠它。 + readingProblems: image.spec.readingProblems || [], + // 程序画的图表(数据、位置、类型):重启后改数、换类型都靠它。 + charts: image.spec.charts || [], checklist: image.spec.checklist, }, + // GIF 的运行状态:参考图是哪张、每帧画到哪了、各帧的候选文件。 + animation: snapshotAnimation(image.animation), + // 组图拼好的 GIF:从哪张组图拼的、切成几格、文件名。重启后没改过就不用重拼。 + panelGif: snapshotPanelGif(image.panelGif), + // 程序转好的 GIF:从哪张图转的、程序最后用的圆心、文件名。 + motionGif: snapshotMotionGif(image.motionGif), + // 组图 GIF 的过渡格:每一步用哪张候选组图的哪一格。重启后不用重新生图。 + panelTweens: snapshotTweens(image.panelTweens), + textMode: image.textMode || '', + textReason: image.textReason || '', chosenReason: image.chosenReason || '', chosenId: image.chosen ? image.chosen.id : null, + // 色彩基准:这张图第一张颜色合格的候选。重启后的追问编辑照样要带它对照颜色。 + colorAnchor: image.colorAnchor ? { id: image.colorAnchor.id, file: image.colorAnchor.file } : null, + // 评审、修正、选优、动图评审在这张图上撞过几次工具调用上限。 + toolHits: image.toolHits || null, // 下一轮候选从哪个轮号继续编号。不存它,重启后增量编辑会从 r1 重新编, // 把上一轮已经交付的同名文件覆盖掉。 roundBase: image.roundBase || 0, @@ -692,11 +870,17 @@ export async function start(context) { id: candidate.id, size: candidate.size, file: candidate.file, + // 贴真码之前的原图(file 指向贴好的那张)。只用来对照排查,恢复时不依赖它。 + originalFile: candidate.originalFile || null, + // 程序排字之前、不带字的底图。之后改字、在这张图上编辑都从它开始。 + baseFile: candidate.baseFile || null, previewFile: candidate.previewFile || null, sheetFile: candidate.sheet ? candidate.sheet.split('/').pop() : null, requestId: candidate.requestId, actionType: candidate.actionType, parentId: candidate.parentId, + // 这一轮编辑一起送进去的色彩基准图(排查「为什么颜色没漂」「这轮带没带基准」用)。 + anchorId: candidate.anchorId || null, prompt: candidate.actionPrompt || '', revisedPrompt: candidate.revisedPrompt, program: candidate.program @@ -706,7 +890,14 @@ export async function start(context) { drift: candidate.program.drift, sharpness: candidate.program.sharpness, redCast: Boolean(candidate.program.redCast), + whiteCast: candidate.program.whiteCast || '', stats: candidate.program.stats, + // 二维码替换结论:只换码的追问要靠它判断底图上的码是不是真码。 + qr: candidate.program.qr || null, + // 程序排字结论:交付卡片上的「文字由程序排入」靠它。 + text: candidate.program.text || null, + // 程序画图表的结论:交付卡片上的「图表核对通过」靠它。 + chart: candidate.program.chart || null, } : null, checks: candidate.checks || [], @@ -947,7 +1138,8 @@ export async function start(context) { if (request.method === 'POST' && path === '/api/session') { const payload = await readJson(request); if (!payload || !String(payload.instruction || '').trim()) return json(response, 400, { error: 'empty_instruction' }); - const session = await createSession(payload); + // 只收需求和上传的图:rerun 这类内部字段只由服务端自己的重跑路径给。 + const session = await createSession({ instruction: payload.instruction, refs: payload.refs }); json(response, 200, { id: session.id }); return; } @@ -982,6 +1174,7 @@ export async function start(context) { const payload = await readJson(request); const session = sessions.get(String(payload.id || '')); if (!session) return json(response, 404, { error: 'no_session' }); + cancelAutoConfirm(session); const answers = Array.isArray(payload.answers) ? payload.answers : []; // Agent 的问卷带 requestId,出图前的闸门不带:按这个分流,问卷的回答才不会被当成闸门的回答。 const requestId = String(payload.requestId || ''); @@ -1007,6 +1200,7 @@ export async function start(context) { // 先标成 cancelled 再放闸门、中止回合:工作流醒来时要能认出这是用户自己停的, // 不能按默认方案接着出图,也不能把被中止的回合当成失败。 session.status = 'cancelled'; + cancelAutoConfirm(session); resolveGate(session, []); agent.cancelSession(session); session.busy = false; @@ -1015,6 +1209,16 @@ export async function start(context) { json(response, 200, { ok: true }); return; } + // 「先别生成」:停掉参考图的自动确认倒计时,之后要用户自己点「生成 GIF」或者回复才出帧。 + if (request.method === 'POST' && path === '/api/session/hold') { + const payload = await readJson(request); + const session = sessions.get(String(payload.id || '')); + if (!session) return json(response, 404, { error: 'no_session' }); + if (!holdAutoConfirm(session)) return json(response, 409, { error: 'no_countdown' }); + await persist(session); + json(response, 200, { ok: true }); + return; + } // 重跑:同一个需求再画一次,开的是新会话,原会话原封不动留着。 if (request.method === 'POST' && path === '/api/session/rerun') { const payload = await readJson(request); @@ -1051,6 +1255,10 @@ export async function start(context) { const dispose = () => { if (disposal) return disposal; context.signal.removeEventListener('abort', onAbort); + // 倒计时只清计时器,不清 session.autoConfirm:它已经落盘,重启后按它重新计时。 + disposed = true; + for (const timer of autoTimers.values()) clearTimeout(timer); + autoTimers.clear(); agent.cancelAll(); // 剪贴板子进程是我们自己拉起来的,Host 只 SIGTERM 这个 Node 入口,不会管孙进程。 disposeClipboard(); @@ -1118,6 +1326,38 @@ function sessionDirs(dir) { } // 把落盘的 snapshot 还原成可继续操作的内存会话:看图、追问、再次交付都能用。 +// 检索预算只是记录(花了多少、哪些没搜),恢复时只认数字和字符串。 +function restoreRefBudget(raw) { + if (!raw || typeof raw !== 'object') return null; + const n = (value) => (Number.isFinite(Number(value)) && Number(value) >= 0 ? Math.round(Number(value)) : 0); + return { + limit: n(raw.limit), + used: n(raw.used), + searches: n(raw.searches), + gates: n(raw.gates), + reserved: 0, + stopped: Array.isArray(raw.stopped) ? raw.stopped.filter((item) => typeof item === 'string').slice(0, 12) : [], + }; +} + +// 节点名 → 撞上限次数,只认已知的节点名和小整数。 +const TOOL_HIT_STEPS = new Set(['plan', 'judge', 'refine', 'select', 'edit', 'cross', 'frames']); +function restoreToolHits(raw) { + if (!raw || typeof raw !== 'object') return undefined; + const out = {}; + for (const [step, value] of Object.entries(raw)) { + const count = Number(value); + if (TOOL_HIT_STEPS.has(step) && Number.isInteger(count) && count > 0) out[step] = Math.min(count, 10); + } + return Object.keys(out).length ? out : undefined; +} + +// 色彩基准只认 images 目录里的文件名:session.json 是可被改写的输入,不能让它指向别处。 +function restoreColorAnchor(raw, dirs) { + if (!raw || typeof raw !== 'object' || !raw.id || !SAFE_NAME.test(String(raw.file || ''))) return null; + return { id: String(raw.id), file: raw.file, path: join(dirs.images, raw.file) }; +} + async function rehydrate(data, dir) { const dirs = sessionDirs(dir); // 老会话落盘时没记 origin。检索来的参考图 note 就是规划里写的 query, @@ -1138,6 +1378,8 @@ async function rehydrate(data, dir) { note, source: ref.source || '', origin: ref.origin || (ref.source || searchNotes.has(note.trim()) ? 'search' : 'user'), + use: typeof ref.use === 'string' ? ref.use : '', + take: typeof ref.take === 'string' ? ref.take.slice(0, 80) : '', }; }) .filter((ref) => ref.id && SAFE_NAME.test(ref.file)); @@ -1149,7 +1391,8 @@ async function rehydrate(data, dir) { try { for (const name of await readdir(dirs.images)) { // 扩展名不写死 .png:接口回 JPEG 时候选图落盘是 .jpg,写死会扫不到、轮号算错。 - const hit = /^([A-Za-z0-9]+)-r(\d+)-g\d+(?:-cc)?\.(?:png|jpe?g|webp)$/i.exec(name); + // -qr 是贴好真码的那份(qrfix),-chart 是程序画好图表的那份(chart),-text 是程序排好字的那份(textoverlay),和原图同一个轮号。 + const hit = /^([A-Za-z0-9]+)-r(\d+)-g\d+(?:-cc|-qr|-chart|-text)?\.(?:png|jpe?g|webp)$/i.exec(name); if (!hit) continue; const seen = Number(hit[2]); if (seen > (diskRounds.get(hit[1]) || 0)) diskRounds.set(hit[1], seen); @@ -1168,6 +1411,10 @@ async function rehydrate(data, dir) { id: candidate.id, file: candidate.file, path: join(dirs.images, candidate.file), + originalFile: candidate.originalFile || null, + originalPath: candidate.originalFile && SAFE_NAME.test(candidate.originalFile) ? join(dirs.images, candidate.originalFile) : null, + baseFile: candidate.baseFile || null, + basePath: candidate.baseFile && SAFE_NAME.test(candidate.baseFile) ? join(dirs.images, candidate.baseFile) : null, previewFile: candidate.previewFile || null, preview: candidate.previewFile ? join(dirs.previews, candidate.previewFile) : null, sheet: candidate.sheetFile ? join(dirs.sheets, candidate.sheetFile) : null, @@ -1175,6 +1422,7 @@ async function rehydrate(data, dir) { requestId: candidate.requestId, actionType: candidate.actionType, parentId: candidate.parentId, + anchorId: candidate.anchorId || null, actionPrompt: candidate.prompt || '', revisedPrompt: candidate.revisedPrompt, round: round.index, @@ -1223,13 +1471,31 @@ async function rehydrate(data, dir) { // 角色基准图编号要跟着会话活过重启,否则追问时 refine 会把它顶掉,主体又漂回去。 characterRefs: (image.spec && image.spec.characterRefs) || [], textLayers: (image.spec && image.spec.textLayers) || [], + qrCodes: (image.spec && Array.isArray(image.spec.qrCodes) && image.spec.qrCodes) || [], + textOverlay: (image.spec && image.spec.textOverlay) || null, + basePrompt: (image.spec && image.spec.basePrompt) || '', + animation: (image.spec && image.spec.animation) || null, + panelGif: restorePanelGifSpec(image.spec && image.spec.panelGif), + motionGif: normalizeMotionGif(image.spec && image.spec.motionGif), + difficulty: (image.spec && ['easy', 'normal', 'hard'].includes(image.spec.difficulty) && image.spec.difficulty) || '', + difficultyReason: (image.spec && typeof image.spec.difficultyReason === 'string' && image.spec.difficultyReason.slice(0, 60)) || '', + readingProblems: (image.spec && Array.isArray(image.spec.readingProblems) && image.spec.readingProblems.slice(0, 8)) || [], + charts: (image.spec && Array.isArray(image.spec.charts) && image.spec.charts) || [], checklist: (image.spec && image.spec.checklist) || [], }, + animation: restoreAnimation(image.animation, dirs), + panelGif: restorePanelGif(image.panelGif), + motionGif: restoreMotionGif(image.motionGif), + panelTweens: restoreTweens(image.panelTweens), + textMode: image.textMode === 'overlay' ? 'overlay' : '', + textReason: image.textReason || '', rounds, roundBase, status: image.status || 'pending', chosen, chosenReason: image.chosenReason || '', + colorAnchor: restoreColorAnchor(image.colorAnchor, dirs), + toolHits: restoreToolHits(image.toolHits), }; }); @@ -1310,6 +1576,10 @@ async function rehydrate(data, dir) { delivery, deliveryVersion: delivery ? delivery.version : 0, characterRefs: Array.isArray(data.characterRefs) ? data.characterRefs : [], + editRefQueries: Array.isArray(data.editRefQueries) ? data.editRefQueries : [], + // 以前重启后丢了:session.json 里的 refBudget 下一次落盘就变成 null,排查「为什么没搜」时看不到。 + refBudget: restoreRefBudget(data.refBudget), + toolHits: restoreToolHits(data.toolHits), crossCheck: data.crossCheck || (delivery && delivery.crossCheck) || null, fatal: data.status === 'running' @@ -1317,6 +1587,8 @@ async function rehydrate(data, dir) { : data.fatal || derivedFatal || '', busy: false, archived: true, + // 自动确认的倒计时由 restoreSessions 按落盘的状态决定要不要重新开始。 + autoConfirm: null, cost: data.cost || { ditImages: 0, ditMs: 0, agentCalls: 0, agentMs: 0, usage: null }, }; } @@ -1330,6 +1602,7 @@ async function restoreTurns(data, dirs, failReasons) { return data.turns.map((turn) => ({ instruction: turn.instruction || '', at: turn.at || bornAt, + auto: Boolean(turn.auto), // 追问事件自己属于新一轮,所以起点是它的前一条。 startSeq: Number(turn.startSeq) || 0, finishedAt: Number(turn.finishedAt) || 0, diff --git a/plugins/liddalidd/h3-image-executor/miniapp/node/tools/h3-gif.mjs b/plugins/liddalidd/h3-image-executor/miniapp/node/tools/h3-gif.mjs new file mode 100644 index 00000000..1d7e3531 --- /dev/null +++ b/plugins/liddalidd/h3-image-executor/miniapp/node/tools/h3-gif.mjs @@ -0,0 +1,536 @@ +#!/usr/bin/env node +// h3-gif —— GIF 命令行工具:把组图切格拼成 GIF、把几张图拼成 GIF、把一组程序画好的帧编码成 GIF、 +// 让一张图绕中心转成 GIF、检查一个 GIF +// (随包发布,零第三方依赖)。 +// +// 存在的意义:生图模型一次只出一张静态图。要动起来,要么让模型一次画一张分格的组图(同一个角色、同一套画风, +// 比一帧一帧分开画一致得多),由程序切开按顺序播;要么像 jungle-adventure、david-voxel-loop 两个例子那样, +// 静态图由模型画,会动的部分由 Agent 写程序一帧一帧算出来。两种都要同一套东西:全组共用一套调色板(颜色不会一帧一跳)、 +// 每帧时长可以不一样、循环首尾接得上、写完从文件读回来核对、出一张帧拼图给人看一眼。这个工具把这些做成现成的命令, +// Agent 不用每次自己写编码和核对的脚本。 +// +// 用法: +// node tools/h3-gif.mjs grid <组图> [--rows R --cols C | --count N] [节奏参数] [-o 输出.gif] [--sheet 帧条.png] +// node tools/h3-gif.mjs split <组图> [--rows R --cols C | --count N] [--no-align] [-o 目录] +// node tools/h3-gif.mjs slides <图1> <图2> ... [节奏参数] [-o 输出.gif] [--sheet 帧条.png] +// node tools/h3-gif.mjs frames <帧1> <帧2> ... | --dir <目录> [--frame-ms 100 | --delays 310,60,...] [--once] +// [--edge 长边 | --scale 倍数] [--dither none|ordered] [--colors N] [--stabilize N] [--fit] +// [-o 输出.gif] [--sheet 拼图.png] +// node tools/h3-gif.mjs check <GIF> [--frames N] [--loop | --once] [--ms 总毫秒] [--sheet 拼图.png] +// node tools/h3-gif.mjs spin <图片> [--center x,y] [--radius r] [--steps N | --frames N] [--ccw] [--frame-ms N] +// [--fade | --cut] [--no-refine] [--once] [--edge 长边] [-o 输出.gif] [--sheet 帧条.png] +// 绕圆心把图里的一块圆盘转起来(加载圈、转动的图标、齿轮):一格一格跑的写 --steps(圆环上等距的点数),连续转写 --frames。 +// +// 约定:成功时 stdout 只输出一个 JSON 对象,退出码 0。失败时 stderr 输出原因:用法、文件错误、切不出格子退出码 1; +// 写出去的 GIF 读回来核对没通过(这时不写输出文件),或者 check 的预期没对上,退出码 2。 +// 永远不覆盖输入文件;不带 -o 时输出写在输入旁边,重名时加序号。 +// 这是 Agent 用 node 单独运行的命令行工具,Mini App 入口(server.mjs)从不 import 它, +// 所以这里的 stdout 是这个子进程自己的,不是 Host 的。 +import { readFile, writeFile, readdir, stat, mkdir, realpath } from 'node:fs/promises'; +import { basename, dirname, extname, join, resolve } from 'node:path'; +import { decodeImage } from '../lib/image.mjs'; +import { encodePng } from '../lib/png.mjs'; +import { gifSize } from '../lib/gif.mjs'; +import { detectPanels, framePanels, renderPanel } from '../lib/panels.mjs'; +import { buildPanelGif, buildSlidesGif, buildSpinGif, contactSheet, encodeFrames, inspectGif } from '../lib/gifbuild.mjs'; + +const USAGE = `h3-gif —— 组图拼 GIF、几张图拼成 GIF、一组帧编码成 GIF、让图绕中心转、检查 GIF + + node tools/h3-gif.mjs grid <组图> [--rows R --cols C | --count N] [节奏参数] [-o 输出.gif] [--sheet 帧条.png] + node tools/h3-gif.mjs split <组图> [--rows R --cols C | --count N] [--no-align] [-o 目录] + node tools/h3-gif.mjs slides <图1> <图2> ... [节奏参数] [-o 输出.gif] [--sheet 帧条.png] + node tools/h3-gif.mjs frames <帧1> <帧2> ... | --dir <目录> [--frame-ms 100 | --delays 310,60,...] [--once] + [--edge 长边 | --scale 倍数] [--dither none|ordered] [--colors N] [--stabilize N] [--fit] + [-o 输出.gif] [--sheet 拼图.png] + node tools/h3-gif.mjs check <GIF> [--frames N] [--loop | --once] [--ms 总毫秒] [--sheet 拼图.png] + node tools/h3-gif.mjs spin <图片> [--center x,y] [--radius r] [--steps N | --frames N] [--ccw] [--frame-ms N] + [--fade | --cut] [--no-refine] [--once] [--edge 长边] [-o 输出.gif] [--sheet 帧条.png] + +spin:center 是转动中心(宽、高的比例,默认 0.5,0.5),radius 是要转的圆盘半径(短边的比例,默认 0.35,会按画面里的内容自动放大); + --steps 12 一格一格跳(圆环上等距 12 个点,默认每格 80 毫秒、两格之间渐变),--frames 24 连续转一圈 24 帧(默认每帧 50 毫秒)。 + +节奏参数(grid、slides): + --order 1,2,3 播放顺序,写格子编号(从上到下、从左到右数) + --frame-ms 700 每格停多久;--first-ms、--last-ms 第一格、最后一格停多久(默认 1.5 倍、2 倍) + --hold 3:1200 第 3 格停 1200 毫秒,可以写多次 + --pingpong 来回播(1→6→2) --once 只播一遍 + --fade 两格之间渐变过渡(默认直接切换) + --bounce 切换时轻微弹一下(默认不弹:整幅画面会跟着放大再落回) + --edge 768 GIF 长边(默认 768) + --no-align 不对齐各格(grid) +`; + +function parseArgs(argv) { + const positional = []; + const flags = { hold: [] }; + for (let i = 0; i < argv.length; i += 1) { + const token = argv[i]; + const value = () => { + if (i + 1 >= argv.length) throw new Error(token + ' 后面缺少参数'); + i += 1; + return argv[i]; + }; + const number = () => { + const raw = value(); + const n = Number(raw); + if (!Number.isFinite(n)) throw new Error(token + ' 要一个数,收到:' + raw); + return n; + }; + if (token === '-o' || token === '--out') flags.out = value(); + else if (token === '--sheet') flags.sheet = value(); + else if (token === '--rows') flags.rows = number(); + else if (token === '--cols') flags.cols = number(); + else if (token === '--count') flags.count = number(); + else if (token === '--order') flags.order = numberList(token, value()); + else if (token === '--frame-ms') flags.frameMs = number(); + else if (token === '--first-ms') flags.firstMs = number(); + else if (token === '--last-ms') flags.lastMs = number(); + else if (token === '--hold') flags.hold.push(parseHold(value())); + else if (token === '--pingpong') flags.pingpong = true; + else if (token === '--once') flags.once = true; + else if (token === '--loop') flags.loop = true; + else if (token === '--fade') flags.fade = true; + else if (token === '--bounce') flags.bounce = true; + else if (token === '--no-bounce') flags.noBounce = true; + else if (token === '--no-align') flags.noAlign = true; + else if (token === '--edge') flags.edge = number(); + else if (token === '--scale') flags.scale = number(); + else if (token === '--dir') flags.dir = value(); + else if (token === '--delays') flags.delays = numberList(token, value()); + else if (token === '--dither') flags.dither = String(value()).toLowerCase(); + else if (token === '--colors') flags.colors = number(); + else if (token === '--stabilize') flags.stabilize = number(); + else if (token === '--fit') flags.fit = true; + else if (token === '--center') flags.center = numberList(token, value()); + else if (token === '--radius') flags.radius = number(); + else if (token === '--steps') flags.steps = number(); + else if (token === '--ccw') flags.ccw = true; + else if (token === '--cut') flags.cut = true; + else if (token === '--no-refine') flags.noRefine = true; + else if (token === '--frames') flags.frames = number(); + else if (token === '--ms') flags.ms = number(); + else if (token === '-h' || token === '--help') flags.help = true; + else if (token.startsWith('-')) throw new Error('不认识的参数:' + token + '\n\n' + USAGE); + else positional.push(token); + } + return { positional, flags }; +} + +function numberList(token, raw) { + const list = String(raw).split(/[,,\s]+/).filter(Boolean).map(Number); + if (!list.length || list.some((n) => !Number.isFinite(n))) throw new Error(token + ' 要用逗号隔开的数,收到:' + raw); + return list; +} + +function parseHold(raw) { + const match = /^(\d+)[::](\d+)$/.exec(String(raw).trim()); + if (!match) throw new Error('--hold 的写法是 格子编号:毫秒,例如 --hold 6:1500,收到:' + raw); + return { index: Number(match[1]), ms: Number(match[2]) }; +} + +async function ensureReadable(path) { + const info = await stat(path).catch(() => null); + if (!info || !info.isFile()) throw new Error('文件不存在或不是普通文件:' + path); + return path; +} + +async function loadImage(path) { + const buffer = await readFile(await ensureReadable(path)); + try { + return decodeImage(buffer); + } catch (error) { + throw new Error('解不开这张图(只认 PNG、JPEG、GIF 的第一帧):' + path + '(' + (error && error.message ? error.message : error) + ')'); + } +} + +// 不覆盖已有文件:默认路径重名时加 -2、-3…… +async function freePath(path) { + const ext = extname(path); + const stem = path.slice(0, path.length - ext.length); + for (let n = 1; n < 1000; n += 1) { + const candidate = n === 1 ? path : stem + '-' + n + ext; + const exists = await stat(candidate).then(() => true, () => false); + if (!exists) return candidate; + } + throw new Error('找不到可用的输出文件名:' + path); +} + +async function outputPath(flags, inputs, fallback) { + if (flags.out) { + const out = resolve(flags.out); + const real = await realPathOf(out); + for (const input of inputs) if ((await realPathOf(input)) === real) throw new Error('输出路径不能和输入文件相同:不覆盖输入'); + return out; + } + return freePath(fallback); +} + +// 按真实路径比:macOS 上 /var 是 /private/var 的软链接,同一个文件两种写法的字符串不相等, +// 只比字符串会把输入图当成别的文件覆盖掉。输出文件还不存在时,取它所在目录的真实路径。 +async function realPathOf(path) { + try { + return await realpath(path); + } catch { + try { + return join(await realpath(dirname(path)), basename(path)); + } catch { + return path; + } + } +} + +function sheetPathFor(flags, gifPath) { + return flags.sheet ? resolve(flags.sheet) : gifPath.replace(/\.gif$/i, '') + '-frames.png'; +} + +// 节奏参数 → panelTimeline / buildPanelGif 认的 spec。 +function rhythmSpec(flags) { + return { + rows: flags.rows, + cols: flags.cols, + count: flags.count, + order: flags.order || [], + frameMs: flags.frameMs, + firstMs: flags.firstMs, + lastMs: flags.lastMs, + hold: flags.hold, + loop: !flags.once, + pingpong: Boolean(flags.pingpong), + transition: flags.fade ? 'fade' : 'cut', + // 默认不弹;--no-bounce 是以前的写法,照样认。 + bounce: Boolean(flags.bounce) && !flags.noBounce, + edge: flags.edge, + align: !flags.noAlign, + }; +} + +function gridSummary(detection) { + return { + rows: detection.rows, + cols: detection.cols, + count: detection.count, + method: detection.method, + strength: detection.strength, + panels: detection.cells.map((cell) => ({ index: cell.index, row: cell.row, col: cell.col, x: cell.x, y: cell.y, w: cell.w, h: cell.h })), + }; +} + +function timelineSummary(timeline) { + return { + order: timeline.order, + holds: timeline.holds, + transition: timeline.transition, + bounce: timeline.bounce, + loop: timeline.loop, + durationMs: timeline.durationMs, + }; +} + +async function writeVerified(built, out, sheet) { + if (!built.verify.ok) { + process.stderr.write('GIF 读回来核对没通过,没有写输出文件:' + built.verify.problems.join(';') + '\n' + JSON.stringify({ ok: false, verify: built.verify }) + '\n'); + return false; + } + await writeFile(out, built.gif.buffer); + await writeFile(sheet, encodePng(built.strip, { level: 6 })); + return true; +} + +function notGrid(input) { + return new Error( + '没在图里找到格子之间的分隔线或留白,切不出两格以上:' + input + + '。确定是组图的话,用 --rows / --cols(或 --count)说明几行几列,按等分切;不是组图就用 frames 自己出帧', + ); +} + +async function main() { + const [command, ...rest] = process.argv.slice(2); + const { positional, flags } = parseArgs(rest); + if (!command || flags.help || command === '-h' || command === '--help' || command === 'help') { + process.stdout.write(USAGE); + return 0; + } + + if (command === 'grid') { + if (positional.length !== 1) throw new Error('grid 需要且只需要 1 张组图'); + const input = resolve(positional[0]); + const image = await loadImage(input); + let built; + try { + built = buildPanelGif(image, rhythmSpec(flags)); + } catch (error) { + if (error && error.message === 'not_grid') throw notGrid(input); + if (error && error.message === 'panel_order_too_short') throw new Error('--order 留下的格子不到 2 格'); + throw error; + } + const out = await outputPath(flags, [input], join(dirname(input), basename(input, extname(input)) + '.gif')); + const sheet = sheetPathFor(flags, out); + if (!(await writeVerified(built, out, sheet))) return 2; + return emit({ + ok: true, + out, + sheet, + width: built.size.width, + height: built.size.height, + bytes: built.gif.buffer.length, + frames: built.gif.frameCount, + durationMs: built.verify.durationMs, + loop: built.verify.loop, + grid: gridSummary(built.detection), + framing: { mode: built.framing.mode, width: built.framing.width, height: built.framing.height, offsets: built.framing.frames.map((frame, i) => ({ index: i + 1, dx: frame.dx, dy: frame.dy })) }, + timeline: timelineSummary(built.timeline), + verify: built.verify, + }); + } + + if (command === 'split') { + if (positional.length !== 1) throw new Error('split 需要且只需要 1 张组图'); + const input = resolve(positional[0]); + const image = await loadImage(input); + const detection = detectPanels(image, { rows: flags.rows, cols: flags.cols, count: flags.count }); + if (detection.count < 2) throw notGrid(input); + const framing = framePanels(image, detection.cells, { align: !flags.noAlign }); + let dir = flags.out ? resolve(flags.out) : join(dirname(input), basename(input, extname(input)) + '-panels'); + if (!flags.out) { + for (let n = 2; await stat(dir).then(() => true, () => false); n += 1) dir = join(dirname(input), basename(input, extname(input)) + '-panels-' + n); + } + await mkdir(dir, { recursive: true }); + const files = []; + for (let i = 0; i < framing.frames.length; i += 1) { + const frame = framing.frames[i]; + const tile = renderPanel(image, frame, framing.width, framing.height, { mode: framing.mode }); + const file = join(dir, String(i + 1).padStart(2, '0') + '.png'); + if (await stat(file).then(() => true, () => false)) throw new Error('目录里已经有 ' + basename(file) + ',不覆盖:' + dir); + await writeFile(file, encodePng(tile, { level: 6 })); + const cell = detection.cells[i]; + files.push({ index: i + 1, row: cell.row, col: cell.col, file, crop: { x: frame.x, y: frame.y, w: frame.w, h: frame.h }, dx: frame.dx, dy: frame.dy }); + } + return emit({ ok: true, dir, width: framing.width, height: framing.height, mode: framing.mode, grid: gridSummary(detection), files }); + } + + if (command === 'slides') { + if (positional.length < 2) throw new Error('slides 至少要 2 张图'); + const inputs = positional.map((item) => resolve(item)); + const images = []; + for (const input of inputs) images.push(await loadImage(input)); + let built; + try { + built = buildSlidesGif(images, rhythmSpec(flags)); + } catch (error) { + if (error && error.message === 'panel_order_too_short') throw new Error('--order 留下的图不到 2 张'); + throw error; + } + const out = await outputPath(flags, inputs, join(dirname(inputs[0]), basename(inputs[0], extname(inputs[0])) + '-slides.gif')); + const sheet = sheetPathFor(flags, out); + if (!(await writeVerified(built, out, sheet))) return 2; + return emit({ + ok: true, + out, + sheet, + width: built.size.width, + height: built.size.height, + bytes: built.gif.buffer.length, + frames: built.gif.frameCount, + durationMs: built.verify.durationMs, + loop: built.verify.loop, + mode: built.framing.mode, + sources: inputs.map((file, i) => ({ index: i + 1, file })), + timeline: timelineSummary(built.timeline), + verify: built.verify, + }); + } + + if (command === 'frames') { + const inputs = flags.dir ? await listFrames(resolve(flags.dir)) : positional.map((item) => resolve(item)); + if (flags.dir && positional.length) throw new Error('frames 用了 --dir 就不要再列帧文件'); + if (inputs.length < 2) throw new Error('frames 至少要 2 帧'); + const images = []; + for (const input of inputs) images.push(await loadImage(input)); + const first = images[0]; + const odd = images.findIndex((image) => image.width !== first.width || image.height !== first.height); + if (odd >= 0 && !flags.fit) { + throw new Error('第 ' + (odd + 1) + ' 帧是 ' + images[odd].width + '×' + images[odd].height + ',和第 1 帧的 ' + first.width + '×' + first.height + ' 不一样。各帧要一样大;要统一成第 1 帧的画幅就加 --fit'); + } + let delays; + if (flags.delays) { + if (flags.delays.length !== images.length) throw new Error('--delays 写了 ' + flags.delays.length + ' 个数,帧有 ' + images.length + ' 帧,要一样多'); + delays = flags.delays.map((ms) => Math.max(20, Math.min(10000, Math.round(ms)))); + } else { + const each = Number.isFinite(flags.frameMs) ? Math.max(20, Math.min(10000, Math.round(flags.frameMs))) : 100; + delays = images.map(() => each); + } + if (flags.dither && !['none', 'ordered'].includes(flags.dither)) throw new Error('--dither 只能是 none 或 ordered'); + let size; + if (Number.isFinite(flags.scale)) { + const k = Math.round(flags.scale); + if (k < 1 || k > 8) throw new Error('--scale 要 1 到 8 的整数'); + size = { width: first.width * k, height: first.height * k }; + } else { + size = gifSize(first.width, first.height, Number.isFinite(flags.edge) ? Math.max(16, Math.min(2048, Math.round(flags.edge))) : 1024); + } + const encoded = encodeFrames(images, { + width: size.width, + height: size.height, + delays, + loop: !flags.once, + // 程序画的帧默认逐像素写进去:差一两级的变化(扫描线的余辉、渐隐)也是动画的一部分。 + stabilize: Number.isFinite(flags.stabilize) ? Math.max(0, Math.min(40, flags.stabilize)) : 0, + dither: flags.dither, + colors: flags.colors, + }); + const info = inspectGif(encoded.gif.buffer); + // 默认写在帧所在目录的旁边:写进帧目录里,下次 --dir 会把它当成一帧。 + const out = await outputPath(flags, inputs, join(dirname(dirname(inputs[0])), basename(dirname(inputs[0])) + '.gif')); + const sheet = sheetPathFor(flags, out); + const built = { gif: encoded.gif, verify: encoded.verify, strip: framesSheet(info.images) }; + if (!(await writeVerified(built, out, sheet))) return 2; + return emit({ + ok: true, + out, + sheet, + width: size.width, + height: size.height, + bytes: encoded.gif.buffer.length, + input: inputs.length, + frames: encoded.gif.frameCount, + delays: info.delays, + durationMs: info.durationMs, + loop: info.loop === 0, + colors: encoded.gif.colors, + dither: encoded.gif.dither, + seam: info.seam, + notes: frameNotes(encoded.gif, info, delays), + verify: encoded.verify, + }); + } + + if (command === 'spin') { + if (positional.length !== 1) throw new Error('spin 需要且只需要 1 张图'); + if (flags.center && flags.center.length !== 2) throw new Error('--center 要写两个数:x,y(画面宽、高的比例)'); + if (flags.fade && flags.cut) throw new Error('--fade 和 --cut 只能写一个'); + const input = resolve(positional[0]); + const image = await loadImage(input); + const built = buildSpinGif(image, { + spin: { + center: flags.center, + radius: flags.radius, + steps: flags.steps, + frames: flags.steps ? undefined : flags.frames, + direction: flags.ccw ? 'ccw' : 'cw', + refine: !flags.noRefine, + }, + frameMs: flags.frameMs, + loop: !flags.once, + transition: flags.fade ? 'fade' : flags.cut ? 'cut' : '', + edge: flags.edge, + }); + const out = await outputPath(flags, [input], join(dirname(input), basename(input, extname(input)) + '-spin.gif')); + const sheet = sheetPathFor(flags, out); + if (!(await writeVerified(built, out, sheet))) return 2; + const short = Math.min(image.width, image.height); + return emit({ + ok: true, + out, + sheet, + width: built.size.width, + height: built.size.height, + bytes: built.gif.buffer.length, + frames: built.count, + written: built.gif.frameCount, + durationMs: built.verify.durationMs, + loop: built.verify.loop, + steps: built.spin.steps, + direction: built.spin.direction, + transition: built.fade ? 'fade' : 'cut', + frameMs: built.frameMs, + // 程序最后用的圆心(画面比例)、转的半径(短边比例);moved 是比给的圆心挪了多少(短边比例) + center: [Math.round((built.where.cx / image.width) * 1000) / 1000, Math.round((built.where.cy / image.height) * 1000) / 1000], + radius: Math.round((built.where.radius / short) * 1000) / 1000, + moved: Math.round((built.where.refined.moved / short) * 1000) / 1000, + verify: built.verify, + }); + } + + if (command === 'check') { + if (positional.length !== 1) throw new Error('check 需要且只需要 1 个 GIF'); + const input = resolve(positional[0]); + const buffer = await readFile(await ensureReadable(input)); + let info; + try { + info = inspectGif(buffer); + } catch (error) { + throw new Error('不是能读的 GIF:' + input + '(' + (error && error.message ? error.message : error) + ')'); + } + const problems = []; + if (Number.isFinite(flags.frames) && info.frames !== Math.round(flags.frames)) problems.push('帧数 ' + info.frames + ',应为 ' + Math.round(flags.frames)); + if (flags.loop && info.loop !== 0) problems.push('没有无限循环'); + if (flags.once && info.loop !== null) problems.push('写了循环播放'); + if (Number.isFinite(flags.ms) && Math.abs(info.durationMs - flags.ms) > 10 * info.frames) problems.push('整组 ' + info.durationMs + ' 毫秒,应为 ' + Math.round(flags.ms)); + let sheet = null; + if (flags.sheet) { + sheet = resolve(flags.sheet); + if ((await realPathOf(sheet)) === (await realPathOf(input))) throw new Error('--sheet 不能和输入相同'); + await writeFile(sheet, encodePng(framesSheet(info.images), { level: 6 })); + } + const payload = { + ok: problems.length === 0, + width: info.width, + height: info.height, + bytes: buffer.length, + frames: info.frames, + delays: info.delays, + durationMs: info.durationMs, + loop: info.loop === 0 ? 'forever' : info.loop === null ? 'once' : info.loop, + steps: info.steps, + seam: info.seam, + notes: frameNotes(null, info, null), + problems, + sheet, + }; + if (problems.length) { + process.stderr.write('GIF 和预期不一样:' + problems.join(';') + '\n' + JSON.stringify(payload) + '\n'); + return 2; + } + return emit(payload); + } + + throw new Error('未知子命令:' + command + '\n\n' + USAGE); +} + +// --dir 里的帧:PNG、JPEG,按文件名里的数字自然排序(frame-2 在 frame-10 前面)。不收 GIF:多半是上一次的输出。 +async function listFrames(dir) { + const info = await stat(dir).catch(() => null); + if (!info || !info.isDirectory()) throw new Error('不是目录:' + dir); + const names = (await readdir(dir)).filter((name) => /\.(png|jpe?g)$/i.test(name) && !name.startsWith('.')); + names.sort((a, b) => a.localeCompare(b, 'en', { numeric: true })); + return names.map((name) => join(dir, name)); +} + +// 帧拼图:一行最多 4 张(16 帧就是 4×4,和两个例子的 spritesheet、check.jpg 一样),每张标帧号。 +function framesSheet(images) { + const perRow = Math.min(4, images.length); + return contactSheet(images.map((image, i) => ({ image, label: String(i + 1) })), { perRow, width: Math.min(2048, perRow * 256), fill: 246 }); +} + +function frameNotes(gif, info, delays) { + const notes = []; + if (gif && gif.frameCount < gif.changed.length) { + notes.push('有 ' + (gif.changed.length - gif.frameCount) + ' 帧和前一帧一模一样,并进了前一帧的时长'); + } + if (delays && delays.some((ms) => ms % 10)) notes.push('GIF 的时长以 10 毫秒为单位,不是 10 的倍数的已按四舍五入写入(见 delays)'); + if (info.seam && info.seam.identicalEnds) notes.push('首尾两帧完全一样:循环接缝处这一画面会连着停两帧的时长'); + else if (info.seam && info.seam.jump) notes.push('循环接缝处会跳一下:最后一帧接回第一帧的变化比中间最大的一步还大不少;要无缝循环,最后一帧应该是第一帧的前一步'); + return notes; +} + +function emit(payload) { + process.stdout.write(JSON.stringify(payload, null, 2) + '\n'); + return 0; +} + +main() + .then((code) => process.exit(code)) + .catch((error) => { + process.stderr.write(String(error && error.message ? error.message : error) + '\n'); + process.exit(1); + }); diff --git a/plugins/liddalidd/h3-image-executor/miniapp/node/tools/h3-qr.mjs b/plugins/liddalidd/h3-image-executor/miniapp/node/tools/h3-qr.mjs new file mode 100644 index 00000000..527f2b43 --- /dev/null +++ b/plugins/liddalidd/h3-image-executor/miniapp/node/tools/h3-qr.mjs @@ -0,0 +1,208 @@ +#!/usr/bin/env node +// h3-qr —— 二维码命令行工具:生成真码、扫码、找图里的码、把图里的假码换成真码(随包发布,零第三方依赖, +// 解码器用同包 vendor/jsqr 里的 jsQR,Apache-2.0)。 +// +// 存在的意义:生图模型画不出能扫的二维码。面板流水线会自动修码;这个工具给聊天里用—— +// 任何一张已有的图(不管是不是这个插件画的),都能在不重新生图的前提下把码换成真码,并扫码验证。 +// +// 用法: +// node tools/h3-qr.mjs fix <图片> --content <内容> [--content <第二枚码的内容>] [-o 输出] +// node tools/h3-qr.mjs fix <图片> --content-file <文本文件> [-o 输出] +// 找到图里的二维码,换成内容为 <内容> 的真码(跟随原来的透视和明暗),再从输出文件扫码验证。 +// 多枚码时 --content 按图里从上到下、从左到右的顺序给。只有一条内容时换图里最大的那枚。 +// 不带 -o 时写在输入图旁边:<原名>-qr.<扩展名>,重名时加序号。永远不覆盖输入图。 +// node tools/h3-qr.mjs scan <图片> +// 扫出图里所有能扫的码(内容、版本、四角坐标)。 +// node tools/h3-qr.mjs locate <图片> +// 找出图里所有二维码的位置,包括扫不出的假码(四角、模块数估计、四周留白)。 +// node tools/h3-qr.mjs make --content <内容> [-o 输出.png] [--ecl L|M|Q|H] [--module 像素] [--quiet 模块数] +// 单独生成一张二维码 PNG,并扫一遍确认能扫。不带 -o 时写在当前工作目录的 qr.png(重名加序号)。 +// +// 约定:成功时 stdout 只输出一个 JSON 对象,退出码 0。 +// 失败时 stderr 输出原因:用法或文件错误退出码 1;fix 换码后验证不通过退出码 2,不写输出文件。 +// 这是 Agent 用 node 单独运行的命令行工具,Mini App 入口(server.mjs)从不 import 它, +// 所以这里的 stdout 是这个子进程自己的,不是 Host 的。 +import { readFile, writeFile, stat, realpath } from 'node:fs/promises'; +import { basename, dirname, extname, join, resolve } from 'node:path'; +import { encodeQr, renderQr, ECL_LEVELS } from '../lib/qrcode.mjs'; +import { scanQr, scanAllQr } from '../lib/qrscan.mjs'; +import { locateQrCodes, fixQrInBuffer } from '../lib/qrfix.mjs'; +import { decodeImage } from '../lib/image.mjs'; +import { encodePng } from '../lib/png.mjs'; + +const USAGE = `h3-qr —— 二维码工具:生成、扫码、定位、把图里的假码换成真码 + + node tools/h3-qr.mjs fix <图片> --content <内容> [--content <内容>...] [-o 输出] + node tools/h3-qr.mjs fix <图片> --content-file <文本文件> [-o 输出] + node tools/h3-qr.mjs scan <图片> + node tools/h3-qr.mjs locate <图片> + node tools/h3-qr.mjs make --content <内容> [-o 输出.png] [--ecl L|M|Q|H] [--module 像素] [--quiet 模块数] +`; + +function parseArgs(argv) { + const positional = []; + const flags = { contents: [] }; + for (let i = 0; i < argv.length; i += 1) { + const token = argv[i]; + const value = () => { + if (i + 1 >= argv.length) throw new Error(token + ' 后面缺少参数'); + i += 1; + return argv[i]; + }; + if (token === '-o' || token === '--out') flags.out = value(); + else if (token === '--content') flags.contents.push({ text: value() }); + else if (token === '--content-file') flags.contents.push({ file: value() }); + else if (token === '--ecl') flags.ecl = String(value()).toUpperCase(); + else if (token === '--module') flags.module = Number(value()); + else if (token === '--quiet') flags.quiet = Number(value()); + else if (token === '-h' || token === '--help') flags.help = true; + else positional.push(token); + } + return { positional, flags }; +} + +async function ensureReadable(path) { + const info = await stat(path).catch(() => null); + if (!info || !info.isFile()) throw new Error('文件不存在或不是普通文件:' + path); + return path; +} + +// --content-file 读 UTF-8 全文,只去掉末尾一个换行(编辑器保存时常带);内容中间一个字符都不动。 +async function readContents(list) { + const out = []; + for (const item of list) { + const text = item.file ? (await readFile(resolve(item.file), 'utf8')).replace(/\r?\n$/, '') : item.text; + if (!text) throw new Error('二维码内容不能为空'); + out.push(text); + } + return out; +} + +// 按真实路径比:macOS 上 /var 是 /private/var 的软链接,同一个文件两种写法的字符串不相等, +// 只比字符串会把原图当成别的文件覆盖掉。输出文件还不存在时,取它所在目录的真实路径。 +async function realPathOf(path) { + try { + return await realpath(path); + } catch { + try { + return join(await realpath(dirname(path)), basename(path)); + } catch { + return path; + } + } +} + +// 不覆盖已有文件:默认路径重名时加 -2、-3…… +async function freePath(path) { + const ext = extname(path); + const stem = path.slice(0, path.length - ext.length); + for (let n = 1; n < 1000; n += 1) { + const candidate = n === 1 ? path : stem + '-' + n + ext; + const exists = await stat(candidate).then(() => true, () => false); + if (!exists) return candidate; + } + throw new Error('找不到可用的输出文件名:' + path); +} + +async function main() { + const [command, ...rest] = process.argv.slice(2); + const { positional, flags } = parseArgs(rest); + if (!command || flags.help || command === '-h' || command === '--help' || command === 'help') { + process.stdout.write(USAGE); + return 0; + } + + if (command === 'fix') { + if (positional.length !== 1) throw new Error('fix 需要且只需要 1 个图片参数'); + if (!flags.contents.length) throw new Error('fix 需要 --content <内容> 或 --content-file <文件>'); + const input = await ensureReadable(resolve(positional[0])); + const contents = await readContents(flags.contents); + const buffer = await readFile(input); + const result = await fixQrInBuffer(buffer, contents); + const items = result.results.map((item) => ({ + content: item.content, + ok: item.ok, + changed: item.changed, + reason: item.reasonText || '', + detail: item.detail || '', + version: item.version, + ecl: item.ecl, + modules: item.modules, + modulePx: item.modulePx, + corners: item.corners, + scans: (item.scans || []).map((scan) => ({ view: scan.view, pad: scan.pad, ok: scan.data === item.content })), + })); + if (!result.ok) { + const why = items.filter((item) => !item.ok).map((item) => '「' + item.content + '」' + item.reason + (item.detail ? '(' + item.detail + ')' : '')); + process.stderr.write('二维码没换成,没有写输出文件:' + why.join(';') + '\n' + JSON.stringify({ ok: false, results: items }) + '\n'); + return 2; + } + const ext = result.format === 'png' ? '.png' : '.jpg'; + let out; + if (flags.out) { + out = resolve(flags.out); + if ((await realPathOf(out)) === (await realPathOf(input))) throw new Error('输出路径不能和输入图相同:不覆盖原图'); + } else { + out = await freePath(join(dirname(input), basename(input, extname(input)) + '-qr' + ext)); + } + await writeFile(out, result.buffer); + return emit({ ok: true, out, changed: result.changed, format: result.format, results: items }); + } + + if (command === 'scan') { + if (positional.length !== 1) throw new Error('scan 需要且只需要 1 个图片参数'); + const image = decodeImage(await readFile(await ensureReadable(resolve(positional[0])))); + const maxEdge = Math.max(image.width, image.height) > 2048 ? 2048 : undefined; + const codes = scanAllQr(image, { max: 8, maxEdge, invert: false }).map((hit) => ({ + data: hit.data, + version: hit.version, + corners: hit.corners.map((p) => ({ x: Math.round(p.x), y: Math.round(p.y) })), + })); + return emit({ width: image.width, height: image.height, count: codes.length, codes }); + } + + if (command === 'locate') { + if (positional.length !== 1) throw new Error('locate 需要且只需要 1 个图片参数'); + const image = decodeImage(await readFile(await ensureReadable(resolve(positional[0])))); + const regions = locateQrCodes(image, { max: 8 }).map((region) => ({ + scannable: region.decoded !== null, + data: region.decoded, + source: region.source, + modules: Math.round(region.modules * 10) / 10, + modulePx: Math.round(region.modulePx * 10) / 10, + corners: region.corners.map((p) => ({ x: Math.round(p.x), y: Math.round(p.y) })), + quietModules: region.quiet ? region.quiet.min : 0, + })); + return emit({ width: image.width, height: image.height, count: regions.length, regions }); + } + + if (command === 'make') { + const contents = await readContents(flags.contents); + if (contents.length !== 1) throw new Error('make 需要且只需要 1 条 --content'); + if (flags.ecl && !ECL_LEVELS.includes(flags.ecl)) throw new Error('--ecl 只能是 L / M / Q / H'); + const qr = encodeQr(contents[0], { ecl: flags.ecl || 'M' }); + const modulePx = Number.isFinite(flags.module) ? Math.max(1, Math.min(64, Math.round(flags.module))) : 10; + const quiet = Number.isFinite(flags.quiet) ? Math.max(0, Math.min(16, Math.round(flags.quiet))) : 4; + const image = renderQr(qr, { modulePx, quiet }); + // 生成完自己扫一遍:工具说「能扫」必须是扫出来的,不是编码器自认的。 + const hit = scanQr(image); + if (!hit || hit.data !== contents[0]) throw new Error('生成的码扫码验证没通过:' + (hit ? '扫出了别的内容' : '扫不出')); + const out = flags.out ? resolve(flags.out) : await freePath(join(process.cwd(), 'qr.png')); + await writeFile(out, encodePng(image, { level: 9 })); + return emit({ ok: true, out, width: image.width, height: image.height, version: qr.version, ecl: qr.ecl, mask: qr.mask, mode: qr.mode, modules: qr.size, scanned: hit.data }); + } + + throw new Error('未知子命令:' + command + '\n\n' + USAGE); +} + +function emit(payload) { + process.stdout.write(JSON.stringify(payload, null, 2) + '\n'); + return 0; +} + +main() + .then((code) => process.exit(code)) + .catch((error) => { + process.stderr.write(String(error && error.message ? error.message : error) + '\n'); + process.exit(1); + }); diff --git a/plugins/liddalidd/h3-image-executor/miniapp/node/tools/h3-review.mjs b/plugins/liddalidd/h3-image-executor/miniapp/node/tools/h3-review.mjs index 37a21902..48845679 100644 --- a/plugins/liddalidd/h3-image-executor/miniapp/node/tools/h3-review.mjs +++ b/plugins/liddalidd/h3-image-executor/miniapp/node/tools/h3-review.mjs @@ -81,7 +81,12 @@ async function main() { // 不带 -o 时写到当前工作目录。以前默认写到插件根目录,那是 Host 安装的包目录,不该往里写东西。 const out = resolve(flags.out || join(process.cwd(), command + '-sheet.png')); const items = inputs.map((path, index) => ({ path, label: command === 'refs' ? 'R' + (index + 1) : String(index + 1) })); - return emit(await buildGridSheet(items, out, { perRow: flags.perRow || 4 })); + const built = await buildGridSheet(items, out, { perRow: flags.perRow || 4 }); + // 一张都解不开时不写文件,按失败退出;解不开的个别图列在 skipped 里,编号仍对应输入顺序。 + if (!built.sheet) { + throw new Error('没有一张图能解码:' + built.skipped.map((item) => item.path + '(' + item.error + ')').join(';')); + } + return emit(built); } if (command === 'stats') { diff --git a/plugins/liddalidd/h3-image-executor/miniapp/node/vendor/fonts/MaShanZheng-Regular.ttf b/plugins/liddalidd/h3-image-executor/miniapp/node/vendor/fonts/MaShanZheng-Regular.ttf new file mode 100644 index 00000000..11bbfb98 Binary files /dev/null and b/plugins/liddalidd/h3-image-executor/miniapp/node/vendor/fonts/MaShanZheng-Regular.ttf differ diff --git a/plugins/liddalidd/h3-image-executor/miniapp/node/vendor/fonts/OFL-MaShanZheng.txt b/plugins/liddalidd/h3-image-executor/miniapp/node/vendor/fonts/OFL-MaShanZheng.txt new file mode 100644 index 00000000..9e252e19 --- /dev/null +++ b/plugins/liddalidd/h3-image-executor/miniapp/node/vendor/fonts/OFL-MaShanZheng.txt @@ -0,0 +1,93 @@ +Copyright 2018 The Ma Shan Zheng Project Authors (https://github.com/googlefonts/mashanzheng) + +This Font Software is licensed under the SIL Open Font License, Version 1.1. +This license is copied below, and is also available with a FAQ at: +http://scripts.sil.org/OFL + + +----------------------------------------------------------- +SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007 +----------------------------------------------------------- + +PREAMBLE +The goals of the Open Font License (OFL) are to stimulate worldwide +development of collaborative font projects, to support the font creation +efforts of academic and linguistic communities, and to provide a free and +open framework in which fonts may be shared and improved in partnership +with others. + +The OFL allows the licensed fonts to be used, studied, modified and +redistributed freely as long as they are not sold by themselves. The +fonts, including any derivative works, can be bundled, embedded, +redistributed and/or sold with any software provided that any reserved +names are not used by derivative works. The fonts and derivatives, +however, cannot be released under any other type of license. The +requirement for fonts to remain under this license does not apply +to any document created using the fonts or their derivatives. + +DEFINITIONS +"Font Software" refers to the set of files released by the Copyright +Holder(s) under this license and clearly marked as such. This may +include source files, build scripts and documentation. + +"Reserved Font Name" refers to any names specified as such after the +copyright statement(s). + +"Original Version" refers to the collection of Font Software components as +distributed by the Copyright Holder(s). + +"Modified Version" refers to any derivative made by adding to, deleting, +or substituting -- in part or in whole -- any of the components of the +Original Version, by changing formats or by porting the Font Software to a +new environment. + +"Author" refers to any designer, engineer, programmer, technical +writer or other person who contributed to the Font Software. + +PERMISSION & CONDITIONS +Permission is hereby granted, free of charge, to any person obtaining +a copy of the Font Software, to use, study, copy, merge, embed, modify, +redistribute, and sell modified and unmodified copies of the Font +Software, subject to the following conditions: + +1) Neither the Font Software nor any of its individual components, +in Original or Modified Versions, may be sold by itself. + +2) Original or Modified Versions of the Font Software may be bundled, +redistributed and/or sold with any software, provided that each copy +contains the above copyright notice and this license. These can be +included either as stand-alone text files, human-readable headers or +in the appropriate machine-readable metadata fields within text or +binary files as long as those fields can be easily viewed by the user. + +3) No Modified Version of the Font Software may use the Reserved Font +Name(s) unless explicit written permission is granted by the corresponding +Copyright Holder. This restriction only applies to the primary font name as +presented to the users. + +4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font +Software shall not be used to promote, endorse or advertise any +Modified Version, except to acknowledge the contribution(s) of the +Copyright Holder(s) and the Author(s) or with their explicit written +permission. + +5) The Font Software, modified or unmodified, in part or in whole, +must be distributed entirely under this license, and must not be +distributed under any other license. The requirement for fonts to +remain under this license does not apply to any document created +using the Font Software. + +TERMINATION +This license becomes null and void if any of the above conditions are +not met. + +DISCLAIMER +THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT +OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE +COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL +DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM +OTHER DEALINGS IN THE FONT SOFTWARE. diff --git a/plugins/liddalidd/h3-image-executor/miniapp/node/vendor/fonts/OFL-ZhiMangXing.txt b/plugins/liddalidd/h3-image-executor/miniapp/node/vendor/fonts/OFL-ZhiMangXing.txt new file mode 100644 index 00000000..8e1e6c70 --- /dev/null +++ b/plugins/liddalidd/h3-image-executor/miniapp/node/vendor/fonts/OFL-ZhiMangXing.txt @@ -0,0 +1,93 @@ +Copyright 2018 The Zhi Mang Xing Project Authors (https://github.com/googlefonts/zhimangxing) + +This Font Software is licensed under the SIL Open Font License, Version 1.1. +This license is copied below, and is also available with a FAQ at: +http://scripts.sil.org/OFL + + +----------------------------------------------------------- +SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007 +----------------------------------------------------------- + +PREAMBLE +The goals of the Open Font License (OFL) are to stimulate worldwide +development of collaborative font projects, to support the font creation +efforts of academic and linguistic communities, and to provide a free and +open framework in which fonts may be shared and improved in partnership +with others. + +The OFL allows the licensed fonts to be used, studied, modified and +redistributed freely as long as they are not sold by themselves. The +fonts, including any derivative works, can be bundled, embedded, +redistributed and/or sold with any software provided that any reserved +names are not used by derivative works. The fonts and derivatives, +however, cannot be released under any other type of license. The +requirement for fonts to remain under this license does not apply +to any document created using the fonts or their derivatives. + +DEFINITIONS +"Font Software" refers to the set of files released by the Copyright +Holder(s) under this license and clearly marked as such. This may +include source files, build scripts and documentation. + +"Reserved Font Name" refers to any names specified as such after the +copyright statement(s). + +"Original Version" refers to the collection of Font Software components as +distributed by the Copyright Holder(s). + +"Modified Version" refers to any derivative made by adding to, deleting, +or substituting -- in part or in whole -- any of the components of the +Original Version, by changing formats or by porting the Font Software to a +new environment. + +"Author" refers to any designer, engineer, programmer, technical +writer or other person who contributed to the Font Software. + +PERMISSION & CONDITIONS +Permission is hereby granted, free of charge, to any person obtaining +a copy of the Font Software, to use, study, copy, merge, embed, modify, +redistribute, and sell modified and unmodified copies of the Font +Software, subject to the following conditions: + +1) Neither the Font Software nor any of its individual components, +in Original or Modified Versions, may be sold by itself. + +2) Original or Modified Versions of the Font Software may be bundled, +redistributed and/or sold with any software, provided that each copy +contains the above copyright notice and this license. These can be +included either as stand-alone text files, human-readable headers or +in the appropriate machine-readable metadata fields within text or +binary files as long as those fields can be easily viewed by the user. + +3) No Modified Version of the Font Software may use the Reserved Font +Name(s) unless explicit written permission is granted by the corresponding +Copyright Holder. This restriction only applies to the primary font name as +presented to the users. + +4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font +Software shall not be used to promote, endorse or advertise any +Modified Version, except to acknowledge the contribution(s) of the +Copyright Holder(s) and the Author(s) or with their explicit written +permission. + +5) The Font Software, modified or unmodified, in part or in whole, +must be distributed entirely under this license, and must not be +distributed under any other license. The requirement for fonts to +remain under this license does not apply to any document created +using the Font Software. + +TERMINATION +This license becomes null and void if any of the above conditions are +not met. + +DISCLAIMER +THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT +OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE +COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL +DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM +OTHER DEALINGS IN THE FONT SOFTWARE. diff --git a/plugins/liddalidd/h3-image-executor/miniapp/node/vendor/fonts/ZhiMangXing-Regular.ttf b/plugins/liddalidd/h3-image-executor/miniapp/node/vendor/fonts/ZhiMangXing-Regular.ttf new file mode 100644 index 00000000..d037d1f0 Binary files /dev/null and b/plugins/liddalidd/h3-image-executor/miniapp/node/vendor/fonts/ZhiMangXing-Regular.ttf differ diff --git a/plugins/liddalidd/h3-image-executor/miniapp/node/vendor/jsqr/LICENSE b/plugins/liddalidd/h3-image-executor/miniapp/node/vendor/jsqr/LICENSE new file mode 100644 index 00000000..8f71f43f --- /dev/null +++ b/plugins/liddalidd/h3-image-executor/miniapp/node/vendor/jsqr/LICENSE @@ -0,0 +1,202 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "{}" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright {yyyy} {name of copyright owner} + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + diff --git a/plugins/liddalidd/h3-image-executor/miniapp/node/vendor/jsqr/jsQR.cjs b/plugins/liddalidd/h3-image-executor/miniapp/node/vendor/jsqr/jsQR.cjs new file mode 100644 index 00000000..429896a9 --- /dev/null +++ b/plugins/liddalidd/h3-image-executor/miniapp/node/vendor/jsqr/jsQR.cjs @@ -0,0 +1,10102 @@ +(function webpackUniversalModuleDefinition(root, factory) { + if(typeof exports === 'object' && typeof module === 'object') + module.exports = factory(); + else if(typeof define === 'function' && define.amd) + define([], factory); + else if(typeof exports === 'object') + exports["jsQR"] = factory(); + else + root["jsQR"] = factory(); +})(typeof self !== 'undefined' ? self : this, function() { +return /******/ (function(modules) { // webpackBootstrap +/******/ // The module cache +/******/ var installedModules = {}; +/******/ +/******/ // The require function +/******/ function __webpack_require__(moduleId) { +/******/ +/******/ // Check if module is in cache +/******/ if(installedModules[moduleId]) { +/******/ return installedModules[moduleId].exports; +/******/ } +/******/ // Create a new module (and put it into the cache) +/******/ var module = installedModules[moduleId] = { +/******/ i: moduleId, +/******/ l: false, +/******/ exports: {} +/******/ }; +/******/ +/******/ // Execute the module function +/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); +/******/ +/******/ // Flag the module as loaded +/******/ module.l = true; +/******/ +/******/ // Return the exports of the module +/******/ return module.exports; +/******/ } +/******/ +/******/ +/******/ // expose the modules object (__webpack_modules__) +/******/ __webpack_require__.m = modules; +/******/ +/******/ // expose the module cache +/******/ __webpack_require__.c = installedModules; +/******/ +/******/ // define getter function for harmony exports +/******/ __webpack_require__.d = function(exports, name, getter) { +/******/ if(!__webpack_require__.o(exports, name)) { +/******/ Object.defineProperty(exports, name, { +/******/ configurable: false, +/******/ enumerable: true, +/******/ get: getter +/******/ }); +/******/ } +/******/ }; +/******/ +/******/ // getDefaultExport function for compatibility with non-harmony modules +/******/ __webpack_require__.n = function(module) { +/******/ var getter = module && module.__esModule ? +/******/ function getDefault() { return module['default']; } : +/******/ function getModuleExports() { return module; }; +/******/ __webpack_require__.d(getter, 'a', getter); +/******/ return getter; +/******/ }; +/******/ +/******/ // Object.prototype.hasOwnProperty.call +/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; +/******/ +/******/ // __webpack_public_path__ +/******/ __webpack_require__.p = ""; +/******/ +/******/ // Load entry module and return exports +/******/ return __webpack_require__(__webpack_require__.s = 3); +/******/ }) +/************************************************************************/ +/******/ ([ +/* 0 */ +/***/ (function(module, exports, __webpack_require__) { + +"use strict"; + +Object.defineProperty(exports, "__esModule", { value: true }); +var BitMatrix = /** @class */ (function () { + function BitMatrix(data, width) { + this.width = width; + this.height = data.length / width; + this.data = data; + } + BitMatrix.createEmpty = function (width, height) { + return new BitMatrix(new Uint8ClampedArray(width * height), width); + }; + BitMatrix.prototype.get = function (x, y) { + if (x < 0 || x >= this.width || y < 0 || y >= this.height) { + return false; + } + return !!this.data[y * this.width + x]; + }; + BitMatrix.prototype.set = function (x, y, v) { + this.data[y * this.width + x] = v ? 1 : 0; + }; + BitMatrix.prototype.setRegion = function (left, top, width, height, v) { + for (var y = top; y < top + height; y++) { + for (var x = left; x < left + width; x++) { + this.set(x, y, !!v); + } + } + }; + return BitMatrix; +}()); +exports.BitMatrix = BitMatrix; + + +/***/ }), +/* 1 */ +/***/ (function(module, exports, __webpack_require__) { + +"use strict"; + +Object.defineProperty(exports, "__esModule", { value: true }); +var GenericGFPoly_1 = __webpack_require__(2); +function addOrSubtractGF(a, b) { + return a ^ b; // tslint:disable-line:no-bitwise +} +exports.addOrSubtractGF = addOrSubtractGF; +var GenericGF = /** @class */ (function () { + function GenericGF(primitive, size, genBase) { + this.primitive = primitive; + this.size = size; + this.generatorBase = genBase; + this.expTable = new Array(this.size); + this.logTable = new Array(this.size); + var x = 1; + for (var i = 0; i < this.size; i++) { + this.expTable[i] = x; + x = x * 2; + if (x >= this.size) { + x = (x ^ this.primitive) & (this.size - 1); // tslint:disable-line:no-bitwise + } + } + for (var i = 0; i < this.size - 1; i++) { + this.logTable[this.expTable[i]] = i; + } + this.zero = new GenericGFPoly_1.default(this, Uint8ClampedArray.from([0])); + this.one = new GenericGFPoly_1.default(this, Uint8ClampedArray.from([1])); + } + GenericGF.prototype.multiply = function (a, b) { + if (a === 0 || b === 0) { + return 0; + } + return this.expTable[(this.logTable[a] + this.logTable[b]) % (this.size - 1)]; + }; + GenericGF.prototype.inverse = function (a) { + if (a === 0) { + throw new Error("Can't invert 0"); + } + return this.expTable[this.size - this.logTable[a] - 1]; + }; + GenericGF.prototype.buildMonomial = function (degree, coefficient) { + if (degree < 0) { + throw new Error("Invalid monomial degree less than 0"); + } + if (coefficient === 0) { + return this.zero; + } + var coefficients = new Uint8ClampedArray(degree + 1); + coefficients[0] = coefficient; + return new GenericGFPoly_1.default(this, coefficients); + }; + GenericGF.prototype.log = function (a) { + if (a === 0) { + throw new Error("Can't take log(0)"); + } + return this.logTable[a]; + }; + GenericGF.prototype.exp = function (a) { + return this.expTable[a]; + }; + return GenericGF; +}()); +exports.default = GenericGF; + + +/***/ }), +/* 2 */ +/***/ (function(module, exports, __webpack_require__) { + +"use strict"; + +Object.defineProperty(exports, "__esModule", { value: true }); +var GenericGF_1 = __webpack_require__(1); +var GenericGFPoly = /** @class */ (function () { + function GenericGFPoly(field, coefficients) { + if (coefficients.length === 0) { + throw new Error("No coefficients."); + } + this.field = field; + var coefficientsLength = coefficients.length; + if (coefficientsLength > 1 && coefficients[0] === 0) { + // Leading term must be non-zero for anything except the constant polynomial "0" + var firstNonZero = 1; + while (firstNonZero < coefficientsLength && coefficients[firstNonZero] === 0) { + firstNonZero++; + } + if (firstNonZero === coefficientsLength) { + this.coefficients = field.zero.coefficients; + } + else { + this.coefficients = new Uint8ClampedArray(coefficientsLength - firstNonZero); + for (var i = 0; i < this.coefficients.length; i++) { + this.coefficients[i] = coefficients[firstNonZero + i]; + } + } + } + else { + this.coefficients = coefficients; + } + } + GenericGFPoly.prototype.degree = function () { + return this.coefficients.length - 1; + }; + GenericGFPoly.prototype.isZero = function () { + return this.coefficients[0] === 0; + }; + GenericGFPoly.prototype.getCoefficient = function (degree) { + return this.coefficients[this.coefficients.length - 1 - degree]; + }; + GenericGFPoly.prototype.addOrSubtract = function (other) { + var _a; + if (this.isZero()) { + return other; + } + if (other.isZero()) { + return this; + } + var smallerCoefficients = this.coefficients; + var largerCoefficients = other.coefficients; + if (smallerCoefficients.length > largerCoefficients.length) { + _a = [largerCoefficients, smallerCoefficients], smallerCoefficients = _a[0], largerCoefficients = _a[1]; + } + var sumDiff = new Uint8ClampedArray(largerCoefficients.length); + var lengthDiff = largerCoefficients.length - smallerCoefficients.length; + for (var i = 0; i < lengthDiff; i++) { + sumDiff[i] = largerCoefficients[i]; + } + for (var i = lengthDiff; i < largerCoefficients.length; i++) { + sumDiff[i] = GenericGF_1.addOrSubtractGF(smallerCoefficients[i - lengthDiff], largerCoefficients[i]); + } + return new GenericGFPoly(this.field, sumDiff); + }; + GenericGFPoly.prototype.multiply = function (scalar) { + if (scalar === 0) { + return this.field.zero; + } + if (scalar === 1) { + return this; + } + var size = this.coefficients.length; + var product = new Uint8ClampedArray(size); + for (var i = 0; i < size; i++) { + product[i] = this.field.multiply(this.coefficients[i], scalar); + } + return new GenericGFPoly(this.field, product); + }; + GenericGFPoly.prototype.multiplyPoly = function (other) { + if (this.isZero() || other.isZero()) { + return this.field.zero; + } + var aCoefficients = this.coefficients; + var aLength = aCoefficients.length; + var bCoefficients = other.coefficients; + var bLength = bCoefficients.length; + var product = new Uint8ClampedArray(aLength + bLength - 1); + for (var i = 0; i < aLength; i++) { + var aCoeff = aCoefficients[i]; + for (var j = 0; j < bLength; j++) { + product[i + j] = GenericGF_1.addOrSubtractGF(product[i + j], this.field.multiply(aCoeff, bCoefficients[j])); + } + } + return new GenericGFPoly(this.field, product); + }; + GenericGFPoly.prototype.multiplyByMonomial = function (degree, coefficient) { + if (degree < 0) { + throw new Error("Invalid degree less than 0"); + } + if (coefficient === 0) { + return this.field.zero; + } + var size = this.coefficients.length; + var product = new Uint8ClampedArray(size + degree); + for (var i = 0; i < size; i++) { + product[i] = this.field.multiply(this.coefficients[i], coefficient); + } + return new GenericGFPoly(this.field, product); + }; + GenericGFPoly.prototype.evaluateAt = function (a) { + var result = 0; + if (a === 0) { + // Just return the x^0 coefficient + return this.getCoefficient(0); + } + var size = this.coefficients.length; + if (a === 1) { + // Just the sum of the coefficients + this.coefficients.forEach(function (coefficient) { + result = GenericGF_1.addOrSubtractGF(result, coefficient); + }); + return result; + } + result = this.coefficients[0]; + for (var i = 1; i < size; i++) { + result = GenericGF_1.addOrSubtractGF(this.field.multiply(a, result), this.coefficients[i]); + } + return result; + }; + return GenericGFPoly; +}()); +exports.default = GenericGFPoly; + + +/***/ }), +/* 3 */ +/***/ (function(module, exports, __webpack_require__) { + +"use strict"; + +Object.defineProperty(exports, "__esModule", { value: true }); +var binarizer_1 = __webpack_require__(4); +var decoder_1 = __webpack_require__(5); +var extractor_1 = __webpack_require__(11); +var locator_1 = __webpack_require__(12); +function scan(matrix) { + var locations = locator_1.locate(matrix); + if (!locations) { + return null; + } + for (var _i = 0, locations_1 = locations; _i < locations_1.length; _i++) { + var location_1 = locations_1[_i]; + var extracted = extractor_1.extract(matrix, location_1); + var decoded = decoder_1.decode(extracted.matrix); + if (decoded) { + return { + binaryData: decoded.bytes, + data: decoded.text, + chunks: decoded.chunks, + version: decoded.version, + location: { + topRightCorner: extracted.mappingFunction(location_1.dimension, 0), + topLeftCorner: extracted.mappingFunction(0, 0), + bottomRightCorner: extracted.mappingFunction(location_1.dimension, location_1.dimension), + bottomLeftCorner: extracted.mappingFunction(0, location_1.dimension), + topRightFinderPattern: location_1.topRight, + topLeftFinderPattern: location_1.topLeft, + bottomLeftFinderPattern: location_1.bottomLeft, + bottomRightAlignmentPattern: location_1.alignmentPattern, + }, + }; + } + } + return null; +} +var defaultOptions = { + inversionAttempts: "attemptBoth", +}; +function jsQR(data, width, height, providedOptions) { + if (providedOptions === void 0) { providedOptions = {}; } + var options = defaultOptions; + Object.keys(options || {}).forEach(function (opt) { + options[opt] = providedOptions[opt] || options[opt]; + }); + var shouldInvert = options.inversionAttempts === "attemptBoth" || options.inversionAttempts === "invertFirst"; + var tryInvertedFirst = options.inversionAttempts === "onlyInvert" || options.inversionAttempts === "invertFirst"; + var _a = binarizer_1.binarize(data, width, height, shouldInvert), binarized = _a.binarized, inverted = _a.inverted; + var result = scan(tryInvertedFirst ? inverted : binarized); + if (!result && (options.inversionAttempts === "attemptBoth" || options.inversionAttempts === "invertFirst")) { + result = scan(tryInvertedFirst ? binarized : inverted); + } + return result; +} +jsQR.default = jsQR; +exports.default = jsQR; + + +/***/ }), +/* 4 */ +/***/ (function(module, exports, __webpack_require__) { + +"use strict"; + +Object.defineProperty(exports, "__esModule", { value: true }); +var BitMatrix_1 = __webpack_require__(0); +var REGION_SIZE = 8; +var MIN_DYNAMIC_RANGE = 24; +function numBetween(value, min, max) { + return value < min ? min : value > max ? max : value; +} +// Like BitMatrix but accepts arbitry Uint8 values +var Matrix = /** @class */ (function () { + function Matrix(width, height) { + this.width = width; + this.data = new Uint8ClampedArray(width * height); + } + Matrix.prototype.get = function (x, y) { + return this.data[y * this.width + x]; + }; + Matrix.prototype.set = function (x, y, value) { + this.data[y * this.width + x] = value; + }; + return Matrix; +}()); +function binarize(data, width, height, returnInverted) { + if (data.length !== width * height * 4) { + throw new Error("Malformed data passed to binarizer."); + } + // Convert image to greyscale + var greyscalePixels = new Matrix(width, height); + for (var x = 0; x < width; x++) { + for (var y = 0; y < height; y++) { + var r = data[((y * width + x) * 4) + 0]; + var g = data[((y * width + x) * 4) + 1]; + var b = data[((y * width + x) * 4) + 2]; + greyscalePixels.set(x, y, 0.2126 * r + 0.7152 * g + 0.0722 * b); + } + } + var horizontalRegionCount = Math.ceil(width / REGION_SIZE); + var verticalRegionCount = Math.ceil(height / REGION_SIZE); + var blackPoints = new Matrix(horizontalRegionCount, verticalRegionCount); + for (var verticalRegion = 0; verticalRegion < verticalRegionCount; verticalRegion++) { + for (var hortizontalRegion = 0; hortizontalRegion < horizontalRegionCount; hortizontalRegion++) { + var sum = 0; + var min = Infinity; + var max = 0; + for (var y = 0; y < REGION_SIZE; y++) { + for (var x = 0; x < REGION_SIZE; x++) { + var pixelLumosity = greyscalePixels.get(hortizontalRegion * REGION_SIZE + x, verticalRegion * REGION_SIZE + y); + sum += pixelLumosity; + min = Math.min(min, pixelLumosity); + max = Math.max(max, pixelLumosity); + } + } + var average = sum / (Math.pow(REGION_SIZE, 2)); + if (max - min <= MIN_DYNAMIC_RANGE) { + // If variation within the block is low, assume this is a block with only light or only + // dark pixels. In that case we do not want to use the average, as it would divide this + // low contrast area into black and white pixels, essentially creating data out of noise. + // + // Default the blackpoint for these blocks to be half the min - effectively white them out + average = min / 2; + if (verticalRegion > 0 && hortizontalRegion > 0) { + // Correct the "white background" assumption for blocks that have neighbors by comparing + // the pixels in this block to the previously calculated black points. This is based on + // the fact that dark barcode symbology is always surrounded by some amount of light + // background for which reasonable black point estimates were made. The bp estimated at + // the boundaries is used for the interior. + // The (min < bp) is arbitrary but works better than other heuristics that were tried. + var averageNeighborBlackPoint = (blackPoints.get(hortizontalRegion, verticalRegion - 1) + + (2 * blackPoints.get(hortizontalRegion - 1, verticalRegion)) + + blackPoints.get(hortizontalRegion - 1, verticalRegion - 1)) / 4; + if (min < averageNeighborBlackPoint) { + average = averageNeighborBlackPoint; + } + } + } + blackPoints.set(hortizontalRegion, verticalRegion, average); + } + } + var binarized = BitMatrix_1.BitMatrix.createEmpty(width, height); + var inverted = null; + if (returnInverted) { + inverted = BitMatrix_1.BitMatrix.createEmpty(width, height); + } + for (var verticalRegion = 0; verticalRegion < verticalRegionCount; verticalRegion++) { + for (var hortizontalRegion = 0; hortizontalRegion < horizontalRegionCount; hortizontalRegion++) { + var left = numBetween(hortizontalRegion, 2, horizontalRegionCount - 3); + var top_1 = numBetween(verticalRegion, 2, verticalRegionCount - 3); + var sum = 0; + for (var xRegion = -2; xRegion <= 2; xRegion++) { + for (var yRegion = -2; yRegion <= 2; yRegion++) { + sum += blackPoints.get(left + xRegion, top_1 + yRegion); + } + } + var threshold = sum / 25; + for (var xRegion = 0; xRegion < REGION_SIZE; xRegion++) { + for (var yRegion = 0; yRegion < REGION_SIZE; yRegion++) { + var x = hortizontalRegion * REGION_SIZE + xRegion; + var y = verticalRegion * REGION_SIZE + yRegion; + var lum = greyscalePixels.get(x, y); + binarized.set(x, y, lum <= threshold); + if (returnInverted) { + inverted.set(x, y, !(lum <= threshold)); + } + } + } + } + } + if (returnInverted) { + return { binarized: binarized, inverted: inverted }; + } + return { binarized: binarized }; +} +exports.binarize = binarize; + + +/***/ }), +/* 5 */ +/***/ (function(module, exports, __webpack_require__) { + +"use strict"; + +Object.defineProperty(exports, "__esModule", { value: true }); +var BitMatrix_1 = __webpack_require__(0); +var decodeData_1 = __webpack_require__(6); +var reedsolomon_1 = __webpack_require__(9); +var version_1 = __webpack_require__(10); +// tslint:disable:no-bitwise +function numBitsDiffering(x, y) { + var z = x ^ y; + var bitCount = 0; + while (z) { + bitCount++; + z &= z - 1; + } + return bitCount; +} +function pushBit(bit, byte) { + return (byte << 1) | bit; +} +// tslint:enable:no-bitwise +var FORMAT_INFO_TABLE = [ + { bits: 0x5412, formatInfo: { errorCorrectionLevel: 1, dataMask: 0 } }, + { bits: 0x5125, formatInfo: { errorCorrectionLevel: 1, dataMask: 1 } }, + { bits: 0x5E7C, formatInfo: { errorCorrectionLevel: 1, dataMask: 2 } }, + { bits: 0x5B4B, formatInfo: { errorCorrectionLevel: 1, dataMask: 3 } }, + { bits: 0x45F9, formatInfo: { errorCorrectionLevel: 1, dataMask: 4 } }, + { bits: 0x40CE, formatInfo: { errorCorrectionLevel: 1, dataMask: 5 } }, + { bits: 0x4F97, formatInfo: { errorCorrectionLevel: 1, dataMask: 6 } }, + { bits: 0x4AA0, formatInfo: { errorCorrectionLevel: 1, dataMask: 7 } }, + { bits: 0x77C4, formatInfo: { errorCorrectionLevel: 0, dataMask: 0 } }, + { bits: 0x72F3, formatInfo: { errorCorrectionLevel: 0, dataMask: 1 } }, + { bits: 0x7DAA, formatInfo: { errorCorrectionLevel: 0, dataMask: 2 } }, + { bits: 0x789D, formatInfo: { errorCorrectionLevel: 0, dataMask: 3 } }, + { bits: 0x662F, formatInfo: { errorCorrectionLevel: 0, dataMask: 4 } }, + { bits: 0x6318, formatInfo: { errorCorrectionLevel: 0, dataMask: 5 } }, + { bits: 0x6C41, formatInfo: { errorCorrectionLevel: 0, dataMask: 6 } }, + { bits: 0x6976, formatInfo: { errorCorrectionLevel: 0, dataMask: 7 } }, + { bits: 0x1689, formatInfo: { errorCorrectionLevel: 3, dataMask: 0 } }, + { bits: 0x13BE, formatInfo: { errorCorrectionLevel: 3, dataMask: 1 } }, + { bits: 0x1CE7, formatInfo: { errorCorrectionLevel: 3, dataMask: 2 } }, + { bits: 0x19D0, formatInfo: { errorCorrectionLevel: 3, dataMask: 3 } }, + { bits: 0x0762, formatInfo: { errorCorrectionLevel: 3, dataMask: 4 } }, + { bits: 0x0255, formatInfo: { errorCorrectionLevel: 3, dataMask: 5 } }, + { bits: 0x0D0C, formatInfo: { errorCorrectionLevel: 3, dataMask: 6 } }, + { bits: 0x083B, formatInfo: { errorCorrectionLevel: 3, dataMask: 7 } }, + { bits: 0x355F, formatInfo: { errorCorrectionLevel: 2, dataMask: 0 } }, + { bits: 0x3068, formatInfo: { errorCorrectionLevel: 2, dataMask: 1 } }, + { bits: 0x3F31, formatInfo: { errorCorrectionLevel: 2, dataMask: 2 } }, + { bits: 0x3A06, formatInfo: { errorCorrectionLevel: 2, dataMask: 3 } }, + { bits: 0x24B4, formatInfo: { errorCorrectionLevel: 2, dataMask: 4 } }, + { bits: 0x2183, formatInfo: { errorCorrectionLevel: 2, dataMask: 5 } }, + { bits: 0x2EDA, formatInfo: { errorCorrectionLevel: 2, dataMask: 6 } }, + { bits: 0x2BED, formatInfo: { errorCorrectionLevel: 2, dataMask: 7 } }, +]; +var DATA_MASKS = [ + function (p) { return ((p.y + p.x) % 2) === 0; }, + function (p) { return (p.y % 2) === 0; }, + function (p) { return p.x % 3 === 0; }, + function (p) { return (p.y + p.x) % 3 === 0; }, + function (p) { return (Math.floor(p.y / 2) + Math.floor(p.x / 3)) % 2 === 0; }, + function (p) { return ((p.x * p.y) % 2) + ((p.x * p.y) % 3) === 0; }, + function (p) { return ((((p.y * p.x) % 2) + (p.y * p.x) % 3) % 2) === 0; }, + function (p) { return ((((p.y + p.x) % 2) + (p.y * p.x) % 3) % 2) === 0; }, +]; +function buildFunctionPatternMask(version) { + var dimension = 17 + 4 * version.versionNumber; + var matrix = BitMatrix_1.BitMatrix.createEmpty(dimension, dimension); + matrix.setRegion(0, 0, 9, 9, true); // Top left finder pattern + separator + format + matrix.setRegion(dimension - 8, 0, 8, 9, true); // Top right finder pattern + separator + format + matrix.setRegion(0, dimension - 8, 9, 8, true); // Bottom left finder pattern + separator + format + // Alignment patterns + for (var _i = 0, _a = version.alignmentPatternCenters; _i < _a.length; _i++) { + var x = _a[_i]; + for (var _b = 0, _c = version.alignmentPatternCenters; _b < _c.length; _b++) { + var y = _c[_b]; + if (!(x === 6 && y === 6 || x === 6 && y === dimension - 7 || x === dimension - 7 && y === 6)) { + matrix.setRegion(x - 2, y - 2, 5, 5, true); + } + } + } + matrix.setRegion(6, 9, 1, dimension - 17, true); // Vertical timing pattern + matrix.setRegion(9, 6, dimension - 17, 1, true); // Horizontal timing pattern + if (version.versionNumber > 6) { + matrix.setRegion(dimension - 11, 0, 3, 6, true); // Version info, top right + matrix.setRegion(0, dimension - 11, 6, 3, true); // Version info, bottom left + } + return matrix; +} +function readCodewords(matrix, version, formatInfo) { + var dataMask = DATA_MASKS[formatInfo.dataMask]; + var dimension = matrix.height; + var functionPatternMask = buildFunctionPatternMask(version); + var codewords = []; + var currentByte = 0; + var bitsRead = 0; + // Read columns in pairs, from right to left + var readingUp = true; + for (var columnIndex = dimension - 1; columnIndex > 0; columnIndex -= 2) { + if (columnIndex === 6) { // Skip whole column with vertical alignment pattern; + columnIndex--; + } + for (var i = 0; i < dimension; i++) { + var y = readingUp ? dimension - 1 - i : i; + for (var columnOffset = 0; columnOffset < 2; columnOffset++) { + var x = columnIndex - columnOffset; + if (!functionPatternMask.get(x, y)) { + bitsRead++; + var bit = matrix.get(x, y); + if (dataMask({ y: y, x: x })) { + bit = !bit; + } + currentByte = pushBit(bit, currentByte); + if (bitsRead === 8) { // Whole bytes + codewords.push(currentByte); + bitsRead = 0; + currentByte = 0; + } + } + } + } + readingUp = !readingUp; + } + return codewords; +} +function readVersion(matrix) { + var dimension = matrix.height; + var provisionalVersion = Math.floor((dimension - 17) / 4); + if (provisionalVersion <= 6) { // 6 and under dont have version info in the QR code + return version_1.VERSIONS[provisionalVersion - 1]; + } + var topRightVersionBits = 0; + for (var y = 5; y >= 0; y--) { + for (var x = dimension - 9; x >= dimension - 11; x--) { + topRightVersionBits = pushBit(matrix.get(x, y), topRightVersionBits); + } + } + var bottomLeftVersionBits = 0; + for (var x = 5; x >= 0; x--) { + for (var y = dimension - 9; y >= dimension - 11; y--) { + bottomLeftVersionBits = pushBit(matrix.get(x, y), bottomLeftVersionBits); + } + } + var bestDifference = Infinity; + var bestVersion; + for (var _i = 0, VERSIONS_1 = version_1.VERSIONS; _i < VERSIONS_1.length; _i++) { + var version = VERSIONS_1[_i]; + if (version.infoBits === topRightVersionBits || version.infoBits === bottomLeftVersionBits) { + return version; + } + var difference = numBitsDiffering(topRightVersionBits, version.infoBits); + if (difference < bestDifference) { + bestVersion = version; + bestDifference = difference; + } + difference = numBitsDiffering(bottomLeftVersionBits, version.infoBits); + if (difference < bestDifference) { + bestVersion = version; + bestDifference = difference; + } + } + // We can tolerate up to 3 bits of error since no two version info codewords will + // differ in less than 8 bits. + if (bestDifference <= 3) { + return bestVersion; + } +} +function readFormatInformation(matrix) { + var topLeftFormatInfoBits = 0; + for (var x = 0; x <= 8; x++) { + if (x !== 6) { // Skip timing pattern bit + topLeftFormatInfoBits = pushBit(matrix.get(x, 8), topLeftFormatInfoBits); + } + } + for (var y = 7; y >= 0; y--) { + if (y !== 6) { // Skip timing pattern bit + topLeftFormatInfoBits = pushBit(matrix.get(8, y), topLeftFormatInfoBits); + } + } + var dimension = matrix.height; + var topRightBottomRightFormatInfoBits = 0; + for (var y = dimension - 1; y >= dimension - 7; y--) { // bottom left + topRightBottomRightFormatInfoBits = pushBit(matrix.get(8, y), topRightBottomRightFormatInfoBits); + } + for (var x = dimension - 8; x < dimension; x++) { // top right + topRightBottomRightFormatInfoBits = pushBit(matrix.get(x, 8), topRightBottomRightFormatInfoBits); + } + var bestDifference = Infinity; + var bestFormatInfo = null; + for (var _i = 0, FORMAT_INFO_TABLE_1 = FORMAT_INFO_TABLE; _i < FORMAT_INFO_TABLE_1.length; _i++) { + var _a = FORMAT_INFO_TABLE_1[_i], bits = _a.bits, formatInfo = _a.formatInfo; + if (bits === topLeftFormatInfoBits || bits === topRightBottomRightFormatInfoBits) { + return formatInfo; + } + var difference = numBitsDiffering(topLeftFormatInfoBits, bits); + if (difference < bestDifference) { + bestFormatInfo = formatInfo; + bestDifference = difference; + } + if (topLeftFormatInfoBits !== topRightBottomRightFormatInfoBits) { // also try the other option + difference = numBitsDiffering(topRightBottomRightFormatInfoBits, bits); + if (difference < bestDifference) { + bestFormatInfo = formatInfo; + bestDifference = difference; + } + } + } + // Hamming distance of the 32 masked codes is 7, by construction, so <= 3 bits differing means we found a match + if (bestDifference <= 3) { + return bestFormatInfo; + } + return null; +} +function getDataBlocks(codewords, version, ecLevel) { + var ecInfo = version.errorCorrectionLevels[ecLevel]; + var dataBlocks = []; + var totalCodewords = 0; + ecInfo.ecBlocks.forEach(function (block) { + for (var i = 0; i < block.numBlocks; i++) { + dataBlocks.push({ numDataCodewords: block.dataCodewordsPerBlock, codewords: [] }); + totalCodewords += block.dataCodewordsPerBlock + ecInfo.ecCodewordsPerBlock; + } + }); + // In some cases the QR code will be malformed enough that we pull off more or less than we should. + // If we pull off less there's nothing we can do. + // If we pull off more we can safely truncate + if (codewords.length < totalCodewords) { + return null; + } + codewords = codewords.slice(0, totalCodewords); + var shortBlockSize = ecInfo.ecBlocks[0].dataCodewordsPerBlock; + // Pull codewords to fill the blocks up to the minimum size + for (var i = 0; i < shortBlockSize; i++) { + for (var _i = 0, dataBlocks_1 = dataBlocks; _i < dataBlocks_1.length; _i++) { + var dataBlock = dataBlocks_1[_i]; + dataBlock.codewords.push(codewords.shift()); + } + } + // If there are any large blocks, pull codewords to fill the last element of those + if (ecInfo.ecBlocks.length > 1) { + var smallBlockCount = ecInfo.ecBlocks[0].numBlocks; + var largeBlockCount = ecInfo.ecBlocks[1].numBlocks; + for (var i = 0; i < largeBlockCount; i++) { + dataBlocks[smallBlockCount + i].codewords.push(codewords.shift()); + } + } + // Add the rest of the codewords to the blocks. These are the error correction codewords. + while (codewords.length > 0) { + for (var _a = 0, dataBlocks_2 = dataBlocks; _a < dataBlocks_2.length; _a++) { + var dataBlock = dataBlocks_2[_a]; + dataBlock.codewords.push(codewords.shift()); + } + } + return dataBlocks; +} +function decodeMatrix(matrix) { + var version = readVersion(matrix); + if (!version) { + return null; + } + var formatInfo = readFormatInformation(matrix); + if (!formatInfo) { + return null; + } + var codewords = readCodewords(matrix, version, formatInfo); + var dataBlocks = getDataBlocks(codewords, version, formatInfo.errorCorrectionLevel); + if (!dataBlocks) { + return null; + } + // Count total number of data bytes + var totalBytes = dataBlocks.reduce(function (a, b) { return a + b.numDataCodewords; }, 0); + var resultBytes = new Uint8ClampedArray(totalBytes); + var resultIndex = 0; + for (var _i = 0, dataBlocks_3 = dataBlocks; _i < dataBlocks_3.length; _i++) { + var dataBlock = dataBlocks_3[_i]; + var correctedBytes = reedsolomon_1.decode(dataBlock.codewords, dataBlock.codewords.length - dataBlock.numDataCodewords); + if (!correctedBytes) { + return null; + } + for (var i = 0; i < dataBlock.numDataCodewords; i++) { + resultBytes[resultIndex++] = correctedBytes[i]; + } + } + try { + return decodeData_1.decode(resultBytes, version.versionNumber); + } + catch (_a) { + return null; + } +} +function decode(matrix) { + if (matrix == null) { + return null; + } + var result = decodeMatrix(matrix); + if (result) { + return result; + } + // Decoding didn't work, try mirroring the QR across the topLeft -> bottomRight line. + for (var x = 0; x < matrix.width; x++) { + for (var y = x + 1; y < matrix.height; y++) { + if (matrix.get(x, y) !== matrix.get(y, x)) { + matrix.set(x, y, !matrix.get(x, y)); + matrix.set(y, x, !matrix.get(y, x)); + } + } + } + return decodeMatrix(matrix); +} +exports.decode = decode; + + +/***/ }), +/* 6 */ +/***/ (function(module, exports, __webpack_require__) { + +"use strict"; + +Object.defineProperty(exports, "__esModule", { value: true }); +// tslint:disable:no-bitwise +var BitStream_1 = __webpack_require__(7); +var shiftJISTable_1 = __webpack_require__(8); +var Mode; +(function (Mode) { + Mode["Numeric"] = "numeric"; + Mode["Alphanumeric"] = "alphanumeric"; + Mode["Byte"] = "byte"; + Mode["Kanji"] = "kanji"; + Mode["ECI"] = "eci"; +})(Mode = exports.Mode || (exports.Mode = {})); +var ModeByte; +(function (ModeByte) { + ModeByte[ModeByte["Terminator"] = 0] = "Terminator"; + ModeByte[ModeByte["Numeric"] = 1] = "Numeric"; + ModeByte[ModeByte["Alphanumeric"] = 2] = "Alphanumeric"; + ModeByte[ModeByte["Byte"] = 4] = "Byte"; + ModeByte[ModeByte["Kanji"] = 8] = "Kanji"; + ModeByte[ModeByte["ECI"] = 7] = "ECI"; + // StructuredAppend = 0x3, + // FNC1FirstPosition = 0x5, + // FNC1SecondPosition = 0x9, +})(ModeByte || (ModeByte = {})); +function decodeNumeric(stream, size) { + var bytes = []; + var text = ""; + var characterCountSize = [10, 12, 14][size]; + var length = stream.readBits(characterCountSize); + // Read digits in groups of 3 + while (length >= 3) { + var num = stream.readBits(10); + if (num >= 1000) { + throw new Error("Invalid numeric value above 999"); + } + var a = Math.floor(num / 100); + var b = Math.floor(num / 10) % 10; + var c = num % 10; + bytes.push(48 + a, 48 + b, 48 + c); + text += a.toString() + b.toString() + c.toString(); + length -= 3; + } + // If the number of digits aren't a multiple of 3, the remaining digits are special cased. + if (length === 2) { + var num = stream.readBits(7); + if (num >= 100) { + throw new Error("Invalid numeric value above 99"); + } + var a = Math.floor(num / 10); + var b = num % 10; + bytes.push(48 + a, 48 + b); + text += a.toString() + b.toString(); + } + else if (length === 1) { + var num = stream.readBits(4); + if (num >= 10) { + throw new Error("Invalid numeric value above 9"); + } + bytes.push(48 + num); + text += num.toString(); + } + return { bytes: bytes, text: text }; +} +var AlphanumericCharacterCodes = [ + "0", "1", "2", "3", "4", "5", "6", "7", "8", + "9", "A", "B", "C", "D", "E", "F", "G", "H", + "I", "J", "K", "L", "M", "N", "O", "P", "Q", + "R", "S", "T", "U", "V", "W", "X", "Y", "Z", + " ", "$", "%", "*", "+", "-", ".", "/", ":", +]; +function decodeAlphanumeric(stream, size) { + var bytes = []; + var text = ""; + var characterCountSize = [9, 11, 13][size]; + var length = stream.readBits(characterCountSize); + while (length >= 2) { + var v = stream.readBits(11); + var a = Math.floor(v / 45); + var b = v % 45; + bytes.push(AlphanumericCharacterCodes[a].charCodeAt(0), AlphanumericCharacterCodes[b].charCodeAt(0)); + text += AlphanumericCharacterCodes[a] + AlphanumericCharacterCodes[b]; + length -= 2; + } + if (length === 1) { + var a = stream.readBits(6); + bytes.push(AlphanumericCharacterCodes[a].charCodeAt(0)); + text += AlphanumericCharacterCodes[a]; + } + return { bytes: bytes, text: text }; +} +function decodeByte(stream, size) { + var bytes = []; + var text = ""; + var characterCountSize = [8, 16, 16][size]; + var length = stream.readBits(characterCountSize); + for (var i = 0; i < length; i++) { + var b = stream.readBits(8); + bytes.push(b); + } + try { + text += decodeURIComponent(bytes.map(function (b) { return "%" + ("0" + b.toString(16)).substr(-2); }).join("")); + } + catch (_a) { + // failed to decode + } + return { bytes: bytes, text: text }; +} +function decodeKanji(stream, size) { + var bytes = []; + var text = ""; + var characterCountSize = [8, 10, 12][size]; + var length = stream.readBits(characterCountSize); + for (var i = 0; i < length; i++) { + var k = stream.readBits(13); + var c = (Math.floor(k / 0xC0) << 8) | (k % 0xC0); + if (c < 0x1F00) { + c += 0x8140; + } + else { + c += 0xC140; + } + bytes.push(c >> 8, c & 0xFF); + text += String.fromCharCode(shiftJISTable_1.shiftJISTable[c]); + } + return { bytes: bytes, text: text }; +} +function decode(data, version) { + var _a, _b, _c, _d; + var stream = new BitStream_1.BitStream(data); + // There are 3 'sizes' based on the version. 1-9 is small (0), 10-26 is medium (1) and 27-40 is large (2). + var size = version <= 9 ? 0 : version <= 26 ? 1 : 2; + var result = { + text: "", + bytes: [], + chunks: [], + version: version, + }; + while (stream.available() >= 4) { + var mode = stream.readBits(4); + if (mode === ModeByte.Terminator) { + return result; + } + else if (mode === ModeByte.ECI) { + if (stream.readBits(1) === 0) { + result.chunks.push({ + type: Mode.ECI, + assignmentNumber: stream.readBits(7), + }); + } + else if (stream.readBits(1) === 0) { + result.chunks.push({ + type: Mode.ECI, + assignmentNumber: stream.readBits(14), + }); + } + else if (stream.readBits(1) === 0) { + result.chunks.push({ + type: Mode.ECI, + assignmentNumber: stream.readBits(21), + }); + } + else { + // ECI data seems corrupted + result.chunks.push({ + type: Mode.ECI, + assignmentNumber: -1, + }); + } + } + else if (mode === ModeByte.Numeric) { + var numericResult = decodeNumeric(stream, size); + result.text += numericResult.text; + (_a = result.bytes).push.apply(_a, numericResult.bytes); + result.chunks.push({ + type: Mode.Numeric, + text: numericResult.text, + }); + } + else if (mode === ModeByte.Alphanumeric) { + var alphanumericResult = decodeAlphanumeric(stream, size); + result.text += alphanumericResult.text; + (_b = result.bytes).push.apply(_b, alphanumericResult.bytes); + result.chunks.push({ + type: Mode.Alphanumeric, + text: alphanumericResult.text, + }); + } + else if (mode === ModeByte.Byte) { + var byteResult = decodeByte(stream, size); + result.text += byteResult.text; + (_c = result.bytes).push.apply(_c, byteResult.bytes); + result.chunks.push({ + type: Mode.Byte, + bytes: byteResult.bytes, + text: byteResult.text, + }); + } + else if (mode === ModeByte.Kanji) { + var kanjiResult = decodeKanji(stream, size); + result.text += kanjiResult.text; + (_d = result.bytes).push.apply(_d, kanjiResult.bytes); + result.chunks.push({ + type: Mode.Kanji, + bytes: kanjiResult.bytes, + text: kanjiResult.text, + }); + } + } + // If there is no data left, or the remaining bits are all 0, then that counts as a termination marker + if (stream.available() === 0 || stream.readBits(stream.available()) === 0) { + return result; + } +} +exports.decode = decode; + + +/***/ }), +/* 7 */ +/***/ (function(module, exports, __webpack_require__) { + +"use strict"; + +// tslint:disable:no-bitwise +Object.defineProperty(exports, "__esModule", { value: true }); +var BitStream = /** @class */ (function () { + function BitStream(bytes) { + this.byteOffset = 0; + this.bitOffset = 0; + this.bytes = bytes; + } + BitStream.prototype.readBits = function (numBits) { + if (numBits < 1 || numBits > 32 || numBits > this.available()) { + throw new Error("Cannot read " + numBits.toString() + " bits"); + } + var result = 0; + // First, read remainder from current byte + if (this.bitOffset > 0) { + var bitsLeft = 8 - this.bitOffset; + var toRead = numBits < bitsLeft ? numBits : bitsLeft; + var bitsToNotRead = bitsLeft - toRead; + var mask = (0xFF >> (8 - toRead)) << bitsToNotRead; + result = (this.bytes[this.byteOffset] & mask) >> bitsToNotRead; + numBits -= toRead; + this.bitOffset += toRead; + if (this.bitOffset === 8) { + this.bitOffset = 0; + this.byteOffset++; + } + } + // Next read whole bytes + if (numBits > 0) { + while (numBits >= 8) { + result = (result << 8) | (this.bytes[this.byteOffset] & 0xFF); + this.byteOffset++; + numBits -= 8; + } + // Finally read a partial byte + if (numBits > 0) { + var bitsToNotRead = 8 - numBits; + var mask = (0xFF >> bitsToNotRead) << bitsToNotRead; + result = (result << numBits) | ((this.bytes[this.byteOffset] & mask) >> bitsToNotRead); + this.bitOffset += numBits; + } + } + return result; + }; + BitStream.prototype.available = function () { + return 8 * (this.bytes.length - this.byteOffset) - this.bitOffset; + }; + return BitStream; +}()); +exports.BitStream = BitStream; + + +/***/ }), +/* 8 */ +/***/ (function(module, exports, __webpack_require__) { + +"use strict"; + +Object.defineProperty(exports, "__esModule", { value: true }); +exports.shiftJISTable = { + 0x20: 0x0020, + 0x21: 0x0021, + 0x22: 0x0022, + 0x23: 0x0023, + 0x24: 0x0024, + 0x25: 0x0025, + 0x26: 0x0026, + 0x27: 0x0027, + 0x28: 0x0028, + 0x29: 0x0029, + 0x2A: 0x002A, + 0x2B: 0x002B, + 0x2C: 0x002C, + 0x2D: 0x002D, + 0x2E: 0x002E, + 0x2F: 0x002F, + 0x30: 0x0030, + 0x31: 0x0031, + 0x32: 0x0032, + 0x33: 0x0033, + 0x34: 0x0034, + 0x35: 0x0035, + 0x36: 0x0036, + 0x37: 0x0037, + 0x38: 0x0038, + 0x39: 0x0039, + 0x3A: 0x003A, + 0x3B: 0x003B, + 0x3C: 0x003C, + 0x3D: 0x003D, + 0x3E: 0x003E, + 0x3F: 0x003F, + 0x40: 0x0040, + 0x41: 0x0041, + 0x42: 0x0042, + 0x43: 0x0043, + 0x44: 0x0044, + 0x45: 0x0045, + 0x46: 0x0046, + 0x47: 0x0047, + 0x48: 0x0048, + 0x49: 0x0049, + 0x4A: 0x004A, + 0x4B: 0x004B, + 0x4C: 0x004C, + 0x4D: 0x004D, + 0x4E: 0x004E, + 0x4F: 0x004F, + 0x50: 0x0050, + 0x51: 0x0051, + 0x52: 0x0052, + 0x53: 0x0053, + 0x54: 0x0054, + 0x55: 0x0055, + 0x56: 0x0056, + 0x57: 0x0057, + 0x58: 0x0058, + 0x59: 0x0059, + 0x5A: 0x005A, + 0x5B: 0x005B, + 0x5C: 0x00A5, + 0x5D: 0x005D, + 0x5E: 0x005E, + 0x5F: 0x005F, + 0x60: 0x0060, + 0x61: 0x0061, + 0x62: 0x0062, + 0x63: 0x0063, + 0x64: 0x0064, + 0x65: 0x0065, + 0x66: 0x0066, + 0x67: 0x0067, + 0x68: 0x0068, + 0x69: 0x0069, + 0x6A: 0x006A, + 0x6B: 0x006B, + 0x6C: 0x006C, + 0x6D: 0x006D, + 0x6E: 0x006E, + 0x6F: 0x006F, + 0x70: 0x0070, + 0x71: 0x0071, + 0x72: 0x0072, + 0x73: 0x0073, + 0x74: 0x0074, + 0x75: 0x0075, + 0x76: 0x0076, + 0x77: 0x0077, + 0x78: 0x0078, + 0x79: 0x0079, + 0x7A: 0x007A, + 0x7B: 0x007B, + 0x7C: 0x007C, + 0x7D: 0x007D, + 0x7E: 0x203E, + 0x8140: 0x3000, + 0x8141: 0x3001, + 0x8142: 0x3002, + 0x8143: 0xFF0C, + 0x8144: 0xFF0E, + 0x8145: 0x30FB, + 0x8146: 0xFF1A, + 0x8147: 0xFF1B, + 0x8148: 0xFF1F, + 0x8149: 0xFF01, + 0x814A: 0x309B, + 0x814B: 0x309C, + 0x814C: 0x00B4, + 0x814D: 0xFF40, + 0x814E: 0x00A8, + 0x814F: 0xFF3E, + 0x8150: 0xFFE3, + 0x8151: 0xFF3F, + 0x8152: 0x30FD, + 0x8153: 0x30FE, + 0x8154: 0x309D, + 0x8155: 0x309E, + 0x8156: 0x3003, + 0x8157: 0x4EDD, + 0x8158: 0x3005, + 0x8159: 0x3006, + 0x815A: 0x3007, + 0x815B: 0x30FC, + 0x815C: 0x2015, + 0x815D: 0x2010, + 0x815E: 0xFF0F, + 0x815F: 0x005C, + 0x8160: 0x301C, + 0x8161: 0x2016, + 0x8162: 0xFF5C, + 0x8163: 0x2026, + 0x8164: 0x2025, + 0x8165: 0x2018, + 0x8166: 0x2019, + 0x8167: 0x201C, + 0x8168: 0x201D, + 0x8169: 0xFF08, + 0x816A: 0xFF09, + 0x816B: 0x3014, + 0x816C: 0x3015, + 0x816D: 0xFF3B, + 0x816E: 0xFF3D, + 0x816F: 0xFF5B, + 0x8170: 0xFF5D, + 0x8171: 0x3008, + 0x8172: 0x3009, + 0x8173: 0x300A, + 0x8174: 0x300B, + 0x8175: 0x300C, + 0x8176: 0x300D, + 0x8177: 0x300E, + 0x8178: 0x300F, + 0x8179: 0x3010, + 0x817A: 0x3011, + 0x817B: 0xFF0B, + 0x817C: 0x2212, + 0x817D: 0x00B1, + 0x817E: 0x00D7, + 0x8180: 0x00F7, + 0x8181: 0xFF1D, + 0x8182: 0x2260, + 0x8183: 0xFF1C, + 0x8184: 0xFF1E, + 0x8185: 0x2266, + 0x8186: 0x2267, + 0x8187: 0x221E, + 0x8188: 0x2234, + 0x8189: 0x2642, + 0x818A: 0x2640, + 0x818B: 0x00B0, + 0x818C: 0x2032, + 0x818D: 0x2033, + 0x818E: 0x2103, + 0x818F: 0xFFE5, + 0x8190: 0xFF04, + 0x8191: 0x00A2, + 0x8192: 0x00A3, + 0x8193: 0xFF05, + 0x8194: 0xFF03, + 0x8195: 0xFF06, + 0x8196: 0xFF0A, + 0x8197: 0xFF20, + 0x8198: 0x00A7, + 0x8199: 0x2606, + 0x819A: 0x2605, + 0x819B: 0x25CB, + 0x819C: 0x25CF, + 0x819D: 0x25CE, + 0x819E: 0x25C7, + 0x819F: 0x25C6, + 0x81A0: 0x25A1, + 0x81A1: 0x25A0, + 0x81A2: 0x25B3, + 0x81A3: 0x25B2, + 0x81A4: 0x25BD, + 0x81A5: 0x25BC, + 0x81A6: 0x203B, + 0x81A7: 0x3012, + 0x81A8: 0x2192, + 0x81A9: 0x2190, + 0x81AA: 0x2191, + 0x81AB: 0x2193, + 0x81AC: 0x3013, + 0x81B8: 0x2208, + 0x81B9: 0x220B, + 0x81BA: 0x2286, + 0x81BB: 0x2287, + 0x81BC: 0x2282, + 0x81BD: 0x2283, + 0x81BE: 0x222A, + 0x81BF: 0x2229, + 0x81C8: 0x2227, + 0x81C9: 0x2228, + 0x81CA: 0x00AC, + 0x81CB: 0x21D2, + 0x81CC: 0x21D4, + 0x81CD: 0x2200, + 0x81CE: 0x2203, + 0x81DA: 0x2220, + 0x81DB: 0x22A5, + 0x81DC: 0x2312, + 0x81DD: 0x2202, + 0x81DE: 0x2207, + 0x81DF: 0x2261, + 0x81E0: 0x2252, + 0x81E1: 0x226A, + 0x81E2: 0x226B, + 0x81E3: 0x221A, + 0x81E4: 0x223D, + 0x81E5: 0x221D, + 0x81E6: 0x2235, + 0x81E7: 0x222B, + 0x81E8: 0x222C, + 0x81F0: 0x212B, + 0x81F1: 0x2030, + 0x81F2: 0x266F, + 0x81F3: 0x266D, + 0x81F4: 0x266A, + 0x81F5: 0x2020, + 0x81F6: 0x2021, + 0x81F7: 0x00B6, + 0x81FC: 0x25EF, + 0x824F: 0xFF10, + 0x8250: 0xFF11, + 0x8251: 0xFF12, + 0x8252: 0xFF13, + 0x8253: 0xFF14, + 0x8254: 0xFF15, + 0x8255: 0xFF16, + 0x8256: 0xFF17, + 0x8257: 0xFF18, + 0x8258: 0xFF19, + 0x8260: 0xFF21, + 0x8261: 0xFF22, + 0x8262: 0xFF23, + 0x8263: 0xFF24, + 0x8264: 0xFF25, + 0x8265: 0xFF26, + 0x8266: 0xFF27, + 0x8267: 0xFF28, + 0x8268: 0xFF29, + 0x8269: 0xFF2A, + 0x826A: 0xFF2B, + 0x826B: 0xFF2C, + 0x826C: 0xFF2D, + 0x826D: 0xFF2E, + 0x826E: 0xFF2F, + 0x826F: 0xFF30, + 0x8270: 0xFF31, + 0x8271: 0xFF32, + 0x8272: 0xFF33, + 0x8273: 0xFF34, + 0x8274: 0xFF35, + 0x8275: 0xFF36, + 0x8276: 0xFF37, + 0x8277: 0xFF38, + 0x8278: 0xFF39, + 0x8279: 0xFF3A, + 0x8281: 0xFF41, + 0x8282: 0xFF42, + 0x8283: 0xFF43, + 0x8284: 0xFF44, + 0x8285: 0xFF45, + 0x8286: 0xFF46, + 0x8287: 0xFF47, + 0x8288: 0xFF48, + 0x8289: 0xFF49, + 0x828A: 0xFF4A, + 0x828B: 0xFF4B, + 0x828C: 0xFF4C, + 0x828D: 0xFF4D, + 0x828E: 0xFF4E, + 0x828F: 0xFF4F, + 0x8290: 0xFF50, + 0x8291: 0xFF51, + 0x8292: 0xFF52, + 0x8293: 0xFF53, + 0x8294: 0xFF54, + 0x8295: 0xFF55, + 0x8296: 0xFF56, + 0x8297: 0xFF57, + 0x8298: 0xFF58, + 0x8299: 0xFF59, + 0x829A: 0xFF5A, + 0x829F: 0x3041, + 0x82A0: 0x3042, + 0x82A1: 0x3043, + 0x82A2: 0x3044, + 0x82A3: 0x3045, + 0x82A4: 0x3046, + 0x82A5: 0x3047, + 0x82A6: 0x3048, + 0x82A7: 0x3049, + 0x82A8: 0x304A, + 0x82A9: 0x304B, + 0x82AA: 0x304C, + 0x82AB: 0x304D, + 0x82AC: 0x304E, + 0x82AD: 0x304F, + 0x82AE: 0x3050, + 0x82AF: 0x3051, + 0x82B0: 0x3052, + 0x82B1: 0x3053, + 0x82B2: 0x3054, + 0x82B3: 0x3055, + 0x82B4: 0x3056, + 0x82B5: 0x3057, + 0x82B6: 0x3058, + 0x82B7: 0x3059, + 0x82B8: 0x305A, + 0x82B9: 0x305B, + 0x82BA: 0x305C, + 0x82BB: 0x305D, + 0x82BC: 0x305E, + 0x82BD: 0x305F, + 0x82BE: 0x3060, + 0x82BF: 0x3061, + 0x82C0: 0x3062, + 0x82C1: 0x3063, + 0x82C2: 0x3064, + 0x82C3: 0x3065, + 0x82C4: 0x3066, + 0x82C5: 0x3067, + 0x82C6: 0x3068, + 0x82C7: 0x3069, + 0x82C8: 0x306A, + 0x82C9: 0x306B, + 0x82CA: 0x306C, + 0x82CB: 0x306D, + 0x82CC: 0x306E, + 0x82CD: 0x306F, + 0x82CE: 0x3070, + 0x82CF: 0x3071, + 0x82D0: 0x3072, + 0x82D1: 0x3073, + 0x82D2: 0x3074, + 0x82D3: 0x3075, + 0x82D4: 0x3076, + 0x82D5: 0x3077, + 0x82D6: 0x3078, + 0x82D7: 0x3079, + 0x82D8: 0x307A, + 0x82D9: 0x307B, + 0x82DA: 0x307C, + 0x82DB: 0x307D, + 0x82DC: 0x307E, + 0x82DD: 0x307F, + 0x82DE: 0x3080, + 0x82DF: 0x3081, + 0x82E0: 0x3082, + 0x82E1: 0x3083, + 0x82E2: 0x3084, + 0x82E3: 0x3085, + 0x82E4: 0x3086, + 0x82E5: 0x3087, + 0x82E6: 0x3088, + 0x82E7: 0x3089, + 0x82E8: 0x308A, + 0x82E9: 0x308B, + 0x82EA: 0x308C, + 0x82EB: 0x308D, + 0x82EC: 0x308E, + 0x82ED: 0x308F, + 0x82EE: 0x3090, + 0x82EF: 0x3091, + 0x82F0: 0x3092, + 0x82F1: 0x3093, + 0x8340: 0x30A1, + 0x8341: 0x30A2, + 0x8342: 0x30A3, + 0x8343: 0x30A4, + 0x8344: 0x30A5, + 0x8345: 0x30A6, + 0x8346: 0x30A7, + 0x8347: 0x30A8, + 0x8348: 0x30A9, + 0x8349: 0x30AA, + 0x834A: 0x30AB, + 0x834B: 0x30AC, + 0x834C: 0x30AD, + 0x834D: 0x30AE, + 0x834E: 0x30AF, + 0x834F: 0x30B0, + 0x8350: 0x30B1, + 0x8351: 0x30B2, + 0x8352: 0x30B3, + 0x8353: 0x30B4, + 0x8354: 0x30B5, + 0x8355: 0x30B6, + 0x8356: 0x30B7, + 0x8357: 0x30B8, + 0x8358: 0x30B9, + 0x8359: 0x30BA, + 0x835A: 0x30BB, + 0x835B: 0x30BC, + 0x835C: 0x30BD, + 0x835D: 0x30BE, + 0x835E: 0x30BF, + 0x835F: 0x30C0, + 0x8360: 0x30C1, + 0x8361: 0x30C2, + 0x8362: 0x30C3, + 0x8363: 0x30C4, + 0x8364: 0x30C5, + 0x8365: 0x30C6, + 0x8366: 0x30C7, + 0x8367: 0x30C8, + 0x8368: 0x30C9, + 0x8369: 0x30CA, + 0x836A: 0x30CB, + 0x836B: 0x30CC, + 0x836C: 0x30CD, + 0x836D: 0x30CE, + 0x836E: 0x30CF, + 0x836F: 0x30D0, + 0x8370: 0x30D1, + 0x8371: 0x30D2, + 0x8372: 0x30D3, + 0x8373: 0x30D4, + 0x8374: 0x30D5, + 0x8375: 0x30D6, + 0x8376: 0x30D7, + 0x8377: 0x30D8, + 0x8378: 0x30D9, + 0x8379: 0x30DA, + 0x837A: 0x30DB, + 0x837B: 0x30DC, + 0x837C: 0x30DD, + 0x837D: 0x30DE, + 0x837E: 0x30DF, + 0x8380: 0x30E0, + 0x8381: 0x30E1, + 0x8382: 0x30E2, + 0x8383: 0x30E3, + 0x8384: 0x30E4, + 0x8385: 0x30E5, + 0x8386: 0x30E6, + 0x8387: 0x30E7, + 0x8388: 0x30E8, + 0x8389: 0x30E9, + 0x838A: 0x30EA, + 0x838B: 0x30EB, + 0x838C: 0x30EC, + 0x838D: 0x30ED, + 0x838E: 0x30EE, + 0x838F: 0x30EF, + 0x8390: 0x30F0, + 0x8391: 0x30F1, + 0x8392: 0x30F2, + 0x8393: 0x30F3, + 0x8394: 0x30F4, + 0x8395: 0x30F5, + 0x8396: 0x30F6, + 0x839F: 0x0391, + 0x83A0: 0x0392, + 0x83A1: 0x0393, + 0x83A2: 0x0394, + 0x83A3: 0x0395, + 0x83A4: 0x0396, + 0x83A5: 0x0397, + 0x83A6: 0x0398, + 0x83A7: 0x0399, + 0x83A8: 0x039A, + 0x83A9: 0x039B, + 0x83AA: 0x039C, + 0x83AB: 0x039D, + 0x83AC: 0x039E, + 0x83AD: 0x039F, + 0x83AE: 0x03A0, + 0x83AF: 0x03A1, + 0x83B0: 0x03A3, + 0x83B1: 0x03A4, + 0x83B2: 0x03A5, + 0x83B3: 0x03A6, + 0x83B4: 0x03A7, + 0x83B5: 0x03A8, + 0x83B6: 0x03A9, + 0x83BF: 0x03B1, + 0x83C0: 0x03B2, + 0x83C1: 0x03B3, + 0x83C2: 0x03B4, + 0x83C3: 0x03B5, + 0x83C4: 0x03B6, + 0x83C5: 0x03B7, + 0x83C6: 0x03B8, + 0x83C7: 0x03B9, + 0x83C8: 0x03BA, + 0x83C9: 0x03BB, + 0x83CA: 0x03BC, + 0x83CB: 0x03BD, + 0x83CC: 0x03BE, + 0x83CD: 0x03BF, + 0x83CE: 0x03C0, + 0x83CF: 0x03C1, + 0x83D0: 0x03C3, + 0x83D1: 0x03C4, + 0x83D2: 0x03C5, + 0x83D3: 0x03C6, + 0x83D4: 0x03C7, + 0x83D5: 0x03C8, + 0x83D6: 0x03C9, + 0x8440: 0x0410, + 0x8441: 0x0411, + 0x8442: 0x0412, + 0x8443: 0x0413, + 0x8444: 0x0414, + 0x8445: 0x0415, + 0x8446: 0x0401, + 0x8447: 0x0416, + 0x8448: 0x0417, + 0x8449: 0x0418, + 0x844A: 0x0419, + 0x844B: 0x041A, + 0x844C: 0x041B, + 0x844D: 0x041C, + 0x844E: 0x041D, + 0x844F: 0x041E, + 0x8450: 0x041F, + 0x8451: 0x0420, + 0x8452: 0x0421, + 0x8453: 0x0422, + 0x8454: 0x0423, + 0x8455: 0x0424, + 0x8456: 0x0425, + 0x8457: 0x0426, + 0x8458: 0x0427, + 0x8459: 0x0428, + 0x845A: 0x0429, + 0x845B: 0x042A, + 0x845C: 0x042B, + 0x845D: 0x042C, + 0x845E: 0x042D, + 0x845F: 0x042E, + 0x8460: 0x042F, + 0x8470: 0x0430, + 0x8471: 0x0431, + 0x8472: 0x0432, + 0x8473: 0x0433, + 0x8474: 0x0434, + 0x8475: 0x0435, + 0x8476: 0x0451, + 0x8477: 0x0436, + 0x8478: 0x0437, + 0x8479: 0x0438, + 0x847A: 0x0439, + 0x847B: 0x043A, + 0x847C: 0x043B, + 0x847D: 0x043C, + 0x847E: 0x043D, + 0x8480: 0x043E, + 0x8481: 0x043F, + 0x8482: 0x0440, + 0x8483: 0x0441, + 0x8484: 0x0442, + 0x8485: 0x0443, + 0x8486: 0x0444, + 0x8487: 0x0445, + 0x8488: 0x0446, + 0x8489: 0x0447, + 0x848A: 0x0448, + 0x848B: 0x0449, + 0x848C: 0x044A, + 0x848D: 0x044B, + 0x848E: 0x044C, + 0x848F: 0x044D, + 0x8490: 0x044E, + 0x8491: 0x044F, + 0x849F: 0x2500, + 0x84A0: 0x2502, + 0x84A1: 0x250C, + 0x84A2: 0x2510, + 0x84A3: 0x2518, + 0x84A4: 0x2514, + 0x84A5: 0x251C, + 0x84A6: 0x252C, + 0x84A7: 0x2524, + 0x84A8: 0x2534, + 0x84A9: 0x253C, + 0x84AA: 0x2501, + 0x84AB: 0x2503, + 0x84AC: 0x250F, + 0x84AD: 0x2513, + 0x84AE: 0x251B, + 0x84AF: 0x2517, + 0x84B0: 0x2523, + 0x84B1: 0x2533, + 0x84B2: 0x252B, + 0x84B3: 0x253B, + 0x84B4: 0x254B, + 0x84B5: 0x2520, + 0x84B6: 0x252F, + 0x84B7: 0x2528, + 0x84B8: 0x2537, + 0x84B9: 0x253F, + 0x84BA: 0x251D, + 0x84BB: 0x2530, + 0x84BC: 0x2525, + 0x84BD: 0x2538, + 0x84BE: 0x2542, + 0x889F: 0x4E9C, + 0x88A0: 0x5516, + 0x88A1: 0x5A03, + 0x88A2: 0x963F, + 0x88A3: 0x54C0, + 0x88A4: 0x611B, + 0x88A5: 0x6328, + 0x88A6: 0x59F6, + 0x88A7: 0x9022, + 0x88A8: 0x8475, + 0x88A9: 0x831C, + 0x88AA: 0x7A50, + 0x88AB: 0x60AA, + 0x88AC: 0x63E1, + 0x88AD: 0x6E25, + 0x88AE: 0x65ED, + 0x88AF: 0x8466, + 0x88B0: 0x82A6, + 0x88B1: 0x9BF5, + 0x88B2: 0x6893, + 0x88B3: 0x5727, + 0x88B4: 0x65A1, + 0x88B5: 0x6271, + 0x88B6: 0x5B9B, + 0x88B7: 0x59D0, + 0x88B8: 0x867B, + 0x88B9: 0x98F4, + 0x88BA: 0x7D62, + 0x88BB: 0x7DBE, + 0x88BC: 0x9B8E, + 0x88BD: 0x6216, + 0x88BE: 0x7C9F, + 0x88BF: 0x88B7, + 0x88C0: 0x5B89, + 0x88C1: 0x5EB5, + 0x88C2: 0x6309, + 0x88C3: 0x6697, + 0x88C4: 0x6848, + 0x88C5: 0x95C7, + 0x88C6: 0x978D, + 0x88C7: 0x674F, + 0x88C8: 0x4EE5, + 0x88C9: 0x4F0A, + 0x88CA: 0x4F4D, + 0x88CB: 0x4F9D, + 0x88CC: 0x5049, + 0x88CD: 0x56F2, + 0x88CE: 0x5937, + 0x88CF: 0x59D4, + 0x88D0: 0x5A01, + 0x88D1: 0x5C09, + 0x88D2: 0x60DF, + 0x88D3: 0x610F, + 0x88D4: 0x6170, + 0x88D5: 0x6613, + 0x88D6: 0x6905, + 0x88D7: 0x70BA, + 0x88D8: 0x754F, + 0x88D9: 0x7570, + 0x88DA: 0x79FB, + 0x88DB: 0x7DAD, + 0x88DC: 0x7DEF, + 0x88DD: 0x80C3, + 0x88DE: 0x840E, + 0x88DF: 0x8863, + 0x88E0: 0x8B02, + 0x88E1: 0x9055, + 0x88E2: 0x907A, + 0x88E3: 0x533B, + 0x88E4: 0x4E95, + 0x88E5: 0x4EA5, + 0x88E6: 0x57DF, + 0x88E7: 0x80B2, + 0x88E8: 0x90C1, + 0x88E9: 0x78EF, + 0x88EA: 0x4E00, + 0x88EB: 0x58F1, + 0x88EC: 0x6EA2, + 0x88ED: 0x9038, + 0x88EE: 0x7A32, + 0x88EF: 0x8328, + 0x88F0: 0x828B, + 0x88F1: 0x9C2F, + 0x88F2: 0x5141, + 0x88F3: 0x5370, + 0x88F4: 0x54BD, + 0x88F5: 0x54E1, + 0x88F6: 0x56E0, + 0x88F7: 0x59FB, + 0x88F8: 0x5F15, + 0x88F9: 0x98F2, + 0x88FA: 0x6DEB, + 0x88FB: 0x80E4, + 0x88FC: 0x852D, + 0x8940: 0x9662, + 0x8941: 0x9670, + 0x8942: 0x96A0, + 0x8943: 0x97FB, + 0x8944: 0x540B, + 0x8945: 0x53F3, + 0x8946: 0x5B87, + 0x8947: 0x70CF, + 0x8948: 0x7FBD, + 0x8949: 0x8FC2, + 0x894A: 0x96E8, + 0x894B: 0x536F, + 0x894C: 0x9D5C, + 0x894D: 0x7ABA, + 0x894E: 0x4E11, + 0x894F: 0x7893, + 0x8950: 0x81FC, + 0x8951: 0x6E26, + 0x8952: 0x5618, + 0x8953: 0x5504, + 0x8954: 0x6B1D, + 0x8955: 0x851A, + 0x8956: 0x9C3B, + 0x8957: 0x59E5, + 0x8958: 0x53A9, + 0x8959: 0x6D66, + 0x895A: 0x74DC, + 0x895B: 0x958F, + 0x895C: 0x5642, + 0x895D: 0x4E91, + 0x895E: 0x904B, + 0x895F: 0x96F2, + 0x8960: 0x834F, + 0x8961: 0x990C, + 0x8962: 0x53E1, + 0x8963: 0x55B6, + 0x8964: 0x5B30, + 0x8965: 0x5F71, + 0x8966: 0x6620, + 0x8967: 0x66F3, + 0x8968: 0x6804, + 0x8969: 0x6C38, + 0x896A: 0x6CF3, + 0x896B: 0x6D29, + 0x896C: 0x745B, + 0x896D: 0x76C8, + 0x896E: 0x7A4E, + 0x896F: 0x9834, + 0x8970: 0x82F1, + 0x8971: 0x885B, + 0x8972: 0x8A60, + 0x8973: 0x92ED, + 0x8974: 0x6DB2, + 0x8975: 0x75AB, + 0x8976: 0x76CA, + 0x8977: 0x99C5, + 0x8978: 0x60A6, + 0x8979: 0x8B01, + 0x897A: 0x8D8A, + 0x897B: 0x95B2, + 0x897C: 0x698E, + 0x897D: 0x53AD, + 0x897E: 0x5186, + 0x8980: 0x5712, + 0x8981: 0x5830, + 0x8982: 0x5944, + 0x8983: 0x5BB4, + 0x8984: 0x5EF6, + 0x8985: 0x6028, + 0x8986: 0x63A9, + 0x8987: 0x63F4, + 0x8988: 0x6CBF, + 0x8989: 0x6F14, + 0x898A: 0x708E, + 0x898B: 0x7114, + 0x898C: 0x7159, + 0x898D: 0x71D5, + 0x898E: 0x733F, + 0x898F: 0x7E01, + 0x8990: 0x8276, + 0x8991: 0x82D1, + 0x8992: 0x8597, + 0x8993: 0x9060, + 0x8994: 0x925B, + 0x8995: 0x9D1B, + 0x8996: 0x5869, + 0x8997: 0x65BC, + 0x8998: 0x6C5A, + 0x8999: 0x7525, + 0x899A: 0x51F9, + 0x899B: 0x592E, + 0x899C: 0x5965, + 0x899D: 0x5F80, + 0x899E: 0x5FDC, + 0x899F: 0x62BC, + 0x89A0: 0x65FA, + 0x89A1: 0x6A2A, + 0x89A2: 0x6B27, + 0x89A3: 0x6BB4, + 0x89A4: 0x738B, + 0x89A5: 0x7FC1, + 0x89A6: 0x8956, + 0x89A7: 0x9D2C, + 0x89A8: 0x9D0E, + 0x89A9: 0x9EC4, + 0x89AA: 0x5CA1, + 0x89AB: 0x6C96, + 0x89AC: 0x837B, + 0x89AD: 0x5104, + 0x89AE: 0x5C4B, + 0x89AF: 0x61B6, + 0x89B0: 0x81C6, + 0x89B1: 0x6876, + 0x89B2: 0x7261, + 0x89B3: 0x4E59, + 0x89B4: 0x4FFA, + 0x89B5: 0x5378, + 0x89B6: 0x6069, + 0x89B7: 0x6E29, + 0x89B8: 0x7A4F, + 0x89B9: 0x97F3, + 0x89BA: 0x4E0B, + 0x89BB: 0x5316, + 0x89BC: 0x4EEE, + 0x89BD: 0x4F55, + 0x89BE: 0x4F3D, + 0x89BF: 0x4FA1, + 0x89C0: 0x4F73, + 0x89C1: 0x52A0, + 0x89C2: 0x53EF, + 0x89C3: 0x5609, + 0x89C4: 0x590F, + 0x89C5: 0x5AC1, + 0x89C6: 0x5BB6, + 0x89C7: 0x5BE1, + 0x89C8: 0x79D1, + 0x89C9: 0x6687, + 0x89CA: 0x679C, + 0x89CB: 0x67B6, + 0x89CC: 0x6B4C, + 0x89CD: 0x6CB3, + 0x89CE: 0x706B, + 0x89CF: 0x73C2, + 0x89D0: 0x798D, + 0x89D1: 0x79BE, + 0x89D2: 0x7A3C, + 0x89D3: 0x7B87, + 0x89D4: 0x82B1, + 0x89D5: 0x82DB, + 0x89D6: 0x8304, + 0x89D7: 0x8377, + 0x89D8: 0x83EF, + 0x89D9: 0x83D3, + 0x89DA: 0x8766, + 0x89DB: 0x8AB2, + 0x89DC: 0x5629, + 0x89DD: 0x8CA8, + 0x89DE: 0x8FE6, + 0x89DF: 0x904E, + 0x89E0: 0x971E, + 0x89E1: 0x868A, + 0x89E2: 0x4FC4, + 0x89E3: 0x5CE8, + 0x89E4: 0x6211, + 0x89E5: 0x7259, + 0x89E6: 0x753B, + 0x89E7: 0x81E5, + 0x89E8: 0x82BD, + 0x89E9: 0x86FE, + 0x89EA: 0x8CC0, + 0x89EB: 0x96C5, + 0x89EC: 0x9913, + 0x89ED: 0x99D5, + 0x89EE: 0x4ECB, + 0x89EF: 0x4F1A, + 0x89F0: 0x89E3, + 0x89F1: 0x56DE, + 0x89F2: 0x584A, + 0x89F3: 0x58CA, + 0x89F4: 0x5EFB, + 0x89F5: 0x5FEB, + 0x89F6: 0x602A, + 0x89F7: 0x6094, + 0x89F8: 0x6062, + 0x89F9: 0x61D0, + 0x89FA: 0x6212, + 0x89FB: 0x62D0, + 0x89FC: 0x6539, + 0x8A40: 0x9B41, + 0x8A41: 0x6666, + 0x8A42: 0x68B0, + 0x8A43: 0x6D77, + 0x8A44: 0x7070, + 0x8A45: 0x754C, + 0x8A46: 0x7686, + 0x8A47: 0x7D75, + 0x8A48: 0x82A5, + 0x8A49: 0x87F9, + 0x8A4A: 0x958B, + 0x8A4B: 0x968E, + 0x8A4C: 0x8C9D, + 0x8A4D: 0x51F1, + 0x8A4E: 0x52BE, + 0x8A4F: 0x5916, + 0x8A50: 0x54B3, + 0x8A51: 0x5BB3, + 0x8A52: 0x5D16, + 0x8A53: 0x6168, + 0x8A54: 0x6982, + 0x8A55: 0x6DAF, + 0x8A56: 0x788D, + 0x8A57: 0x84CB, + 0x8A58: 0x8857, + 0x8A59: 0x8A72, + 0x8A5A: 0x93A7, + 0x8A5B: 0x9AB8, + 0x8A5C: 0x6D6C, + 0x8A5D: 0x99A8, + 0x8A5E: 0x86D9, + 0x8A5F: 0x57A3, + 0x8A60: 0x67FF, + 0x8A61: 0x86CE, + 0x8A62: 0x920E, + 0x8A63: 0x5283, + 0x8A64: 0x5687, + 0x8A65: 0x5404, + 0x8A66: 0x5ED3, + 0x8A67: 0x62E1, + 0x8A68: 0x64B9, + 0x8A69: 0x683C, + 0x8A6A: 0x6838, + 0x8A6B: 0x6BBB, + 0x8A6C: 0x7372, + 0x8A6D: 0x78BA, + 0x8A6E: 0x7A6B, + 0x8A6F: 0x899A, + 0x8A70: 0x89D2, + 0x8A71: 0x8D6B, + 0x8A72: 0x8F03, + 0x8A73: 0x90ED, + 0x8A74: 0x95A3, + 0x8A75: 0x9694, + 0x8A76: 0x9769, + 0x8A77: 0x5B66, + 0x8A78: 0x5CB3, + 0x8A79: 0x697D, + 0x8A7A: 0x984D, + 0x8A7B: 0x984E, + 0x8A7C: 0x639B, + 0x8A7D: 0x7B20, + 0x8A7E: 0x6A2B, + 0x8A80: 0x6A7F, + 0x8A81: 0x68B6, + 0x8A82: 0x9C0D, + 0x8A83: 0x6F5F, + 0x8A84: 0x5272, + 0x8A85: 0x559D, + 0x8A86: 0x6070, + 0x8A87: 0x62EC, + 0x8A88: 0x6D3B, + 0x8A89: 0x6E07, + 0x8A8A: 0x6ED1, + 0x8A8B: 0x845B, + 0x8A8C: 0x8910, + 0x8A8D: 0x8F44, + 0x8A8E: 0x4E14, + 0x8A8F: 0x9C39, + 0x8A90: 0x53F6, + 0x8A91: 0x691B, + 0x8A92: 0x6A3A, + 0x8A93: 0x9784, + 0x8A94: 0x682A, + 0x8A95: 0x515C, + 0x8A96: 0x7AC3, + 0x8A97: 0x84B2, + 0x8A98: 0x91DC, + 0x8A99: 0x938C, + 0x8A9A: 0x565B, + 0x8A9B: 0x9D28, + 0x8A9C: 0x6822, + 0x8A9D: 0x8305, + 0x8A9E: 0x8431, + 0x8A9F: 0x7CA5, + 0x8AA0: 0x5208, + 0x8AA1: 0x82C5, + 0x8AA2: 0x74E6, + 0x8AA3: 0x4E7E, + 0x8AA4: 0x4F83, + 0x8AA5: 0x51A0, + 0x8AA6: 0x5BD2, + 0x8AA7: 0x520A, + 0x8AA8: 0x52D8, + 0x8AA9: 0x52E7, + 0x8AAA: 0x5DFB, + 0x8AAB: 0x559A, + 0x8AAC: 0x582A, + 0x8AAD: 0x59E6, + 0x8AAE: 0x5B8C, + 0x8AAF: 0x5B98, + 0x8AB0: 0x5BDB, + 0x8AB1: 0x5E72, + 0x8AB2: 0x5E79, + 0x8AB3: 0x60A3, + 0x8AB4: 0x611F, + 0x8AB5: 0x6163, + 0x8AB6: 0x61BE, + 0x8AB7: 0x63DB, + 0x8AB8: 0x6562, + 0x8AB9: 0x67D1, + 0x8ABA: 0x6853, + 0x8ABB: 0x68FA, + 0x8ABC: 0x6B3E, + 0x8ABD: 0x6B53, + 0x8ABE: 0x6C57, + 0x8ABF: 0x6F22, + 0x8AC0: 0x6F97, + 0x8AC1: 0x6F45, + 0x8AC2: 0x74B0, + 0x8AC3: 0x7518, + 0x8AC4: 0x76E3, + 0x8AC5: 0x770B, + 0x8AC6: 0x7AFF, + 0x8AC7: 0x7BA1, + 0x8AC8: 0x7C21, + 0x8AC9: 0x7DE9, + 0x8ACA: 0x7F36, + 0x8ACB: 0x7FF0, + 0x8ACC: 0x809D, + 0x8ACD: 0x8266, + 0x8ACE: 0x839E, + 0x8ACF: 0x89B3, + 0x8AD0: 0x8ACC, + 0x8AD1: 0x8CAB, + 0x8AD2: 0x9084, + 0x8AD3: 0x9451, + 0x8AD4: 0x9593, + 0x8AD5: 0x9591, + 0x8AD6: 0x95A2, + 0x8AD7: 0x9665, + 0x8AD8: 0x97D3, + 0x8AD9: 0x9928, + 0x8ADA: 0x8218, + 0x8ADB: 0x4E38, + 0x8ADC: 0x542B, + 0x8ADD: 0x5CB8, + 0x8ADE: 0x5DCC, + 0x8ADF: 0x73A9, + 0x8AE0: 0x764C, + 0x8AE1: 0x773C, + 0x8AE2: 0x5CA9, + 0x8AE3: 0x7FEB, + 0x8AE4: 0x8D0B, + 0x8AE5: 0x96C1, + 0x8AE6: 0x9811, + 0x8AE7: 0x9854, + 0x8AE8: 0x9858, + 0x8AE9: 0x4F01, + 0x8AEA: 0x4F0E, + 0x8AEB: 0x5371, + 0x8AEC: 0x559C, + 0x8AED: 0x5668, + 0x8AEE: 0x57FA, + 0x8AEF: 0x5947, + 0x8AF0: 0x5B09, + 0x8AF1: 0x5BC4, + 0x8AF2: 0x5C90, + 0x8AF3: 0x5E0C, + 0x8AF4: 0x5E7E, + 0x8AF5: 0x5FCC, + 0x8AF6: 0x63EE, + 0x8AF7: 0x673A, + 0x8AF8: 0x65D7, + 0x8AF9: 0x65E2, + 0x8AFA: 0x671F, + 0x8AFB: 0x68CB, + 0x8AFC: 0x68C4, + 0x8B40: 0x6A5F, + 0x8B41: 0x5E30, + 0x8B42: 0x6BC5, + 0x8B43: 0x6C17, + 0x8B44: 0x6C7D, + 0x8B45: 0x757F, + 0x8B46: 0x7948, + 0x8B47: 0x5B63, + 0x8B48: 0x7A00, + 0x8B49: 0x7D00, + 0x8B4A: 0x5FBD, + 0x8B4B: 0x898F, + 0x8B4C: 0x8A18, + 0x8B4D: 0x8CB4, + 0x8B4E: 0x8D77, + 0x8B4F: 0x8ECC, + 0x8B50: 0x8F1D, + 0x8B51: 0x98E2, + 0x8B52: 0x9A0E, + 0x8B53: 0x9B3C, + 0x8B54: 0x4E80, + 0x8B55: 0x507D, + 0x8B56: 0x5100, + 0x8B57: 0x5993, + 0x8B58: 0x5B9C, + 0x8B59: 0x622F, + 0x8B5A: 0x6280, + 0x8B5B: 0x64EC, + 0x8B5C: 0x6B3A, + 0x8B5D: 0x72A0, + 0x8B5E: 0x7591, + 0x8B5F: 0x7947, + 0x8B60: 0x7FA9, + 0x8B61: 0x87FB, + 0x8B62: 0x8ABC, + 0x8B63: 0x8B70, + 0x8B64: 0x63AC, + 0x8B65: 0x83CA, + 0x8B66: 0x97A0, + 0x8B67: 0x5409, + 0x8B68: 0x5403, + 0x8B69: 0x55AB, + 0x8B6A: 0x6854, + 0x8B6B: 0x6A58, + 0x8B6C: 0x8A70, + 0x8B6D: 0x7827, + 0x8B6E: 0x6775, + 0x8B6F: 0x9ECD, + 0x8B70: 0x5374, + 0x8B71: 0x5BA2, + 0x8B72: 0x811A, + 0x8B73: 0x8650, + 0x8B74: 0x9006, + 0x8B75: 0x4E18, + 0x8B76: 0x4E45, + 0x8B77: 0x4EC7, + 0x8B78: 0x4F11, + 0x8B79: 0x53CA, + 0x8B7A: 0x5438, + 0x8B7B: 0x5BAE, + 0x8B7C: 0x5F13, + 0x8B7D: 0x6025, + 0x8B7E: 0x6551, + 0x8B80: 0x673D, + 0x8B81: 0x6C42, + 0x8B82: 0x6C72, + 0x8B83: 0x6CE3, + 0x8B84: 0x7078, + 0x8B85: 0x7403, + 0x8B86: 0x7A76, + 0x8B87: 0x7AAE, + 0x8B88: 0x7B08, + 0x8B89: 0x7D1A, + 0x8B8A: 0x7CFE, + 0x8B8B: 0x7D66, + 0x8B8C: 0x65E7, + 0x8B8D: 0x725B, + 0x8B8E: 0x53BB, + 0x8B8F: 0x5C45, + 0x8B90: 0x5DE8, + 0x8B91: 0x62D2, + 0x8B92: 0x62E0, + 0x8B93: 0x6319, + 0x8B94: 0x6E20, + 0x8B95: 0x865A, + 0x8B96: 0x8A31, + 0x8B97: 0x8DDD, + 0x8B98: 0x92F8, + 0x8B99: 0x6F01, + 0x8B9A: 0x79A6, + 0x8B9B: 0x9B5A, + 0x8B9C: 0x4EA8, + 0x8B9D: 0x4EAB, + 0x8B9E: 0x4EAC, + 0x8B9F: 0x4F9B, + 0x8BA0: 0x4FA0, + 0x8BA1: 0x50D1, + 0x8BA2: 0x5147, + 0x8BA3: 0x7AF6, + 0x8BA4: 0x5171, + 0x8BA5: 0x51F6, + 0x8BA6: 0x5354, + 0x8BA7: 0x5321, + 0x8BA8: 0x537F, + 0x8BA9: 0x53EB, + 0x8BAA: 0x55AC, + 0x8BAB: 0x5883, + 0x8BAC: 0x5CE1, + 0x8BAD: 0x5F37, + 0x8BAE: 0x5F4A, + 0x8BAF: 0x602F, + 0x8BB0: 0x6050, + 0x8BB1: 0x606D, + 0x8BB2: 0x631F, + 0x8BB3: 0x6559, + 0x8BB4: 0x6A4B, + 0x8BB5: 0x6CC1, + 0x8BB6: 0x72C2, + 0x8BB7: 0x72ED, + 0x8BB8: 0x77EF, + 0x8BB9: 0x80F8, + 0x8BBA: 0x8105, + 0x8BBB: 0x8208, + 0x8BBC: 0x854E, + 0x8BBD: 0x90F7, + 0x8BBE: 0x93E1, + 0x8BBF: 0x97FF, + 0x8BC0: 0x9957, + 0x8BC1: 0x9A5A, + 0x8BC2: 0x4EF0, + 0x8BC3: 0x51DD, + 0x8BC4: 0x5C2D, + 0x8BC5: 0x6681, + 0x8BC6: 0x696D, + 0x8BC7: 0x5C40, + 0x8BC8: 0x66F2, + 0x8BC9: 0x6975, + 0x8BCA: 0x7389, + 0x8BCB: 0x6850, + 0x8BCC: 0x7C81, + 0x8BCD: 0x50C5, + 0x8BCE: 0x52E4, + 0x8BCF: 0x5747, + 0x8BD0: 0x5DFE, + 0x8BD1: 0x9326, + 0x8BD2: 0x65A4, + 0x8BD3: 0x6B23, + 0x8BD4: 0x6B3D, + 0x8BD5: 0x7434, + 0x8BD6: 0x7981, + 0x8BD7: 0x79BD, + 0x8BD8: 0x7B4B, + 0x8BD9: 0x7DCA, + 0x8BDA: 0x82B9, + 0x8BDB: 0x83CC, + 0x8BDC: 0x887F, + 0x8BDD: 0x895F, + 0x8BDE: 0x8B39, + 0x8BDF: 0x8FD1, + 0x8BE0: 0x91D1, + 0x8BE1: 0x541F, + 0x8BE2: 0x9280, + 0x8BE3: 0x4E5D, + 0x8BE4: 0x5036, + 0x8BE5: 0x53E5, + 0x8BE6: 0x533A, + 0x8BE7: 0x72D7, + 0x8BE8: 0x7396, + 0x8BE9: 0x77E9, + 0x8BEA: 0x82E6, + 0x8BEB: 0x8EAF, + 0x8BEC: 0x99C6, + 0x8BED: 0x99C8, + 0x8BEE: 0x99D2, + 0x8BEF: 0x5177, + 0x8BF0: 0x611A, + 0x8BF1: 0x865E, + 0x8BF2: 0x55B0, + 0x8BF3: 0x7A7A, + 0x8BF4: 0x5076, + 0x8BF5: 0x5BD3, + 0x8BF6: 0x9047, + 0x8BF7: 0x9685, + 0x8BF8: 0x4E32, + 0x8BF9: 0x6ADB, + 0x8BFA: 0x91E7, + 0x8BFB: 0x5C51, + 0x8BFC: 0x5C48, + 0x8C40: 0x6398, + 0x8C41: 0x7A9F, + 0x8C42: 0x6C93, + 0x8C43: 0x9774, + 0x8C44: 0x8F61, + 0x8C45: 0x7AAA, + 0x8C46: 0x718A, + 0x8C47: 0x9688, + 0x8C48: 0x7C82, + 0x8C49: 0x6817, + 0x8C4A: 0x7E70, + 0x8C4B: 0x6851, + 0x8C4C: 0x936C, + 0x8C4D: 0x52F2, + 0x8C4E: 0x541B, + 0x8C4F: 0x85AB, + 0x8C50: 0x8A13, + 0x8C51: 0x7FA4, + 0x8C52: 0x8ECD, + 0x8C53: 0x90E1, + 0x8C54: 0x5366, + 0x8C55: 0x8888, + 0x8C56: 0x7941, + 0x8C57: 0x4FC2, + 0x8C58: 0x50BE, + 0x8C59: 0x5211, + 0x8C5A: 0x5144, + 0x8C5B: 0x5553, + 0x8C5C: 0x572D, + 0x8C5D: 0x73EA, + 0x8C5E: 0x578B, + 0x8C5F: 0x5951, + 0x8C60: 0x5F62, + 0x8C61: 0x5F84, + 0x8C62: 0x6075, + 0x8C63: 0x6176, + 0x8C64: 0x6167, + 0x8C65: 0x61A9, + 0x8C66: 0x63B2, + 0x8C67: 0x643A, + 0x8C68: 0x656C, + 0x8C69: 0x666F, + 0x8C6A: 0x6842, + 0x8C6B: 0x6E13, + 0x8C6C: 0x7566, + 0x8C6D: 0x7A3D, + 0x8C6E: 0x7CFB, + 0x8C6F: 0x7D4C, + 0x8C70: 0x7D99, + 0x8C71: 0x7E4B, + 0x8C72: 0x7F6B, + 0x8C73: 0x830E, + 0x8C74: 0x834A, + 0x8C75: 0x86CD, + 0x8C76: 0x8A08, + 0x8C77: 0x8A63, + 0x8C78: 0x8B66, + 0x8C79: 0x8EFD, + 0x8C7A: 0x981A, + 0x8C7B: 0x9D8F, + 0x8C7C: 0x82B8, + 0x8C7D: 0x8FCE, + 0x8C7E: 0x9BE8, + 0x8C80: 0x5287, + 0x8C81: 0x621F, + 0x8C82: 0x6483, + 0x8C83: 0x6FC0, + 0x8C84: 0x9699, + 0x8C85: 0x6841, + 0x8C86: 0x5091, + 0x8C87: 0x6B20, + 0x8C88: 0x6C7A, + 0x8C89: 0x6F54, + 0x8C8A: 0x7A74, + 0x8C8B: 0x7D50, + 0x8C8C: 0x8840, + 0x8C8D: 0x8A23, + 0x8C8E: 0x6708, + 0x8C8F: 0x4EF6, + 0x8C90: 0x5039, + 0x8C91: 0x5026, + 0x8C92: 0x5065, + 0x8C93: 0x517C, + 0x8C94: 0x5238, + 0x8C95: 0x5263, + 0x8C96: 0x55A7, + 0x8C97: 0x570F, + 0x8C98: 0x5805, + 0x8C99: 0x5ACC, + 0x8C9A: 0x5EFA, + 0x8C9B: 0x61B2, + 0x8C9C: 0x61F8, + 0x8C9D: 0x62F3, + 0x8C9E: 0x6372, + 0x8C9F: 0x691C, + 0x8CA0: 0x6A29, + 0x8CA1: 0x727D, + 0x8CA2: 0x72AC, + 0x8CA3: 0x732E, + 0x8CA4: 0x7814, + 0x8CA5: 0x786F, + 0x8CA6: 0x7D79, + 0x8CA7: 0x770C, + 0x8CA8: 0x80A9, + 0x8CA9: 0x898B, + 0x8CAA: 0x8B19, + 0x8CAB: 0x8CE2, + 0x8CAC: 0x8ED2, + 0x8CAD: 0x9063, + 0x8CAE: 0x9375, + 0x8CAF: 0x967A, + 0x8CB0: 0x9855, + 0x8CB1: 0x9A13, + 0x8CB2: 0x9E78, + 0x8CB3: 0x5143, + 0x8CB4: 0x539F, + 0x8CB5: 0x53B3, + 0x8CB6: 0x5E7B, + 0x8CB7: 0x5F26, + 0x8CB8: 0x6E1B, + 0x8CB9: 0x6E90, + 0x8CBA: 0x7384, + 0x8CBB: 0x73FE, + 0x8CBC: 0x7D43, + 0x8CBD: 0x8237, + 0x8CBE: 0x8A00, + 0x8CBF: 0x8AFA, + 0x8CC0: 0x9650, + 0x8CC1: 0x4E4E, + 0x8CC2: 0x500B, + 0x8CC3: 0x53E4, + 0x8CC4: 0x547C, + 0x8CC5: 0x56FA, + 0x8CC6: 0x59D1, + 0x8CC7: 0x5B64, + 0x8CC8: 0x5DF1, + 0x8CC9: 0x5EAB, + 0x8CCA: 0x5F27, + 0x8CCB: 0x6238, + 0x8CCC: 0x6545, + 0x8CCD: 0x67AF, + 0x8CCE: 0x6E56, + 0x8CCF: 0x72D0, + 0x8CD0: 0x7CCA, + 0x8CD1: 0x88B4, + 0x8CD2: 0x80A1, + 0x8CD3: 0x80E1, + 0x8CD4: 0x83F0, + 0x8CD5: 0x864E, + 0x8CD6: 0x8A87, + 0x8CD7: 0x8DE8, + 0x8CD8: 0x9237, + 0x8CD9: 0x96C7, + 0x8CDA: 0x9867, + 0x8CDB: 0x9F13, + 0x8CDC: 0x4E94, + 0x8CDD: 0x4E92, + 0x8CDE: 0x4F0D, + 0x8CDF: 0x5348, + 0x8CE0: 0x5449, + 0x8CE1: 0x543E, + 0x8CE2: 0x5A2F, + 0x8CE3: 0x5F8C, + 0x8CE4: 0x5FA1, + 0x8CE5: 0x609F, + 0x8CE6: 0x68A7, + 0x8CE7: 0x6A8E, + 0x8CE8: 0x745A, + 0x8CE9: 0x7881, + 0x8CEA: 0x8A9E, + 0x8CEB: 0x8AA4, + 0x8CEC: 0x8B77, + 0x8CED: 0x9190, + 0x8CEE: 0x4E5E, + 0x8CEF: 0x9BC9, + 0x8CF0: 0x4EA4, + 0x8CF1: 0x4F7C, + 0x8CF2: 0x4FAF, + 0x8CF3: 0x5019, + 0x8CF4: 0x5016, + 0x8CF5: 0x5149, + 0x8CF6: 0x516C, + 0x8CF7: 0x529F, + 0x8CF8: 0x52B9, + 0x8CF9: 0x52FE, + 0x8CFA: 0x539A, + 0x8CFB: 0x53E3, + 0x8CFC: 0x5411, + 0x8D40: 0x540E, + 0x8D41: 0x5589, + 0x8D42: 0x5751, + 0x8D43: 0x57A2, + 0x8D44: 0x597D, + 0x8D45: 0x5B54, + 0x8D46: 0x5B5D, + 0x8D47: 0x5B8F, + 0x8D48: 0x5DE5, + 0x8D49: 0x5DE7, + 0x8D4A: 0x5DF7, + 0x8D4B: 0x5E78, + 0x8D4C: 0x5E83, + 0x8D4D: 0x5E9A, + 0x8D4E: 0x5EB7, + 0x8D4F: 0x5F18, + 0x8D50: 0x6052, + 0x8D51: 0x614C, + 0x8D52: 0x6297, + 0x8D53: 0x62D8, + 0x8D54: 0x63A7, + 0x8D55: 0x653B, + 0x8D56: 0x6602, + 0x8D57: 0x6643, + 0x8D58: 0x66F4, + 0x8D59: 0x676D, + 0x8D5A: 0x6821, + 0x8D5B: 0x6897, + 0x8D5C: 0x69CB, + 0x8D5D: 0x6C5F, + 0x8D5E: 0x6D2A, + 0x8D5F: 0x6D69, + 0x8D60: 0x6E2F, + 0x8D61: 0x6E9D, + 0x8D62: 0x7532, + 0x8D63: 0x7687, + 0x8D64: 0x786C, + 0x8D65: 0x7A3F, + 0x8D66: 0x7CE0, + 0x8D67: 0x7D05, + 0x8D68: 0x7D18, + 0x8D69: 0x7D5E, + 0x8D6A: 0x7DB1, + 0x8D6B: 0x8015, + 0x8D6C: 0x8003, + 0x8D6D: 0x80AF, + 0x8D6E: 0x80B1, + 0x8D6F: 0x8154, + 0x8D70: 0x818F, + 0x8D71: 0x822A, + 0x8D72: 0x8352, + 0x8D73: 0x884C, + 0x8D74: 0x8861, + 0x8D75: 0x8B1B, + 0x8D76: 0x8CA2, + 0x8D77: 0x8CFC, + 0x8D78: 0x90CA, + 0x8D79: 0x9175, + 0x8D7A: 0x9271, + 0x8D7B: 0x783F, + 0x8D7C: 0x92FC, + 0x8D7D: 0x95A4, + 0x8D7E: 0x964D, + 0x8D80: 0x9805, + 0x8D81: 0x9999, + 0x8D82: 0x9AD8, + 0x8D83: 0x9D3B, + 0x8D84: 0x525B, + 0x8D85: 0x52AB, + 0x8D86: 0x53F7, + 0x8D87: 0x5408, + 0x8D88: 0x58D5, + 0x8D89: 0x62F7, + 0x8D8A: 0x6FE0, + 0x8D8B: 0x8C6A, + 0x8D8C: 0x8F5F, + 0x8D8D: 0x9EB9, + 0x8D8E: 0x514B, + 0x8D8F: 0x523B, + 0x8D90: 0x544A, + 0x8D91: 0x56FD, + 0x8D92: 0x7A40, + 0x8D93: 0x9177, + 0x8D94: 0x9D60, + 0x8D95: 0x9ED2, + 0x8D96: 0x7344, + 0x8D97: 0x6F09, + 0x8D98: 0x8170, + 0x8D99: 0x7511, + 0x8D9A: 0x5FFD, + 0x8D9B: 0x60DA, + 0x8D9C: 0x9AA8, + 0x8D9D: 0x72DB, + 0x8D9E: 0x8FBC, + 0x8D9F: 0x6B64, + 0x8DA0: 0x9803, + 0x8DA1: 0x4ECA, + 0x8DA2: 0x56F0, + 0x8DA3: 0x5764, + 0x8DA4: 0x58BE, + 0x8DA5: 0x5A5A, + 0x8DA6: 0x6068, + 0x8DA7: 0x61C7, + 0x8DA8: 0x660F, + 0x8DA9: 0x6606, + 0x8DAA: 0x6839, + 0x8DAB: 0x68B1, + 0x8DAC: 0x6DF7, + 0x8DAD: 0x75D5, + 0x8DAE: 0x7D3A, + 0x8DAF: 0x826E, + 0x8DB0: 0x9B42, + 0x8DB1: 0x4E9B, + 0x8DB2: 0x4F50, + 0x8DB3: 0x53C9, + 0x8DB4: 0x5506, + 0x8DB5: 0x5D6F, + 0x8DB6: 0x5DE6, + 0x8DB7: 0x5DEE, + 0x8DB8: 0x67FB, + 0x8DB9: 0x6C99, + 0x8DBA: 0x7473, + 0x8DBB: 0x7802, + 0x8DBC: 0x8A50, + 0x8DBD: 0x9396, + 0x8DBE: 0x88DF, + 0x8DBF: 0x5750, + 0x8DC0: 0x5EA7, + 0x8DC1: 0x632B, + 0x8DC2: 0x50B5, + 0x8DC3: 0x50AC, + 0x8DC4: 0x518D, + 0x8DC5: 0x6700, + 0x8DC6: 0x54C9, + 0x8DC7: 0x585E, + 0x8DC8: 0x59BB, + 0x8DC9: 0x5BB0, + 0x8DCA: 0x5F69, + 0x8DCB: 0x624D, + 0x8DCC: 0x63A1, + 0x8DCD: 0x683D, + 0x8DCE: 0x6B73, + 0x8DCF: 0x6E08, + 0x8DD0: 0x707D, + 0x8DD1: 0x91C7, + 0x8DD2: 0x7280, + 0x8DD3: 0x7815, + 0x8DD4: 0x7826, + 0x8DD5: 0x796D, + 0x8DD6: 0x658E, + 0x8DD7: 0x7D30, + 0x8DD8: 0x83DC, + 0x8DD9: 0x88C1, + 0x8DDA: 0x8F09, + 0x8DDB: 0x969B, + 0x8DDC: 0x5264, + 0x8DDD: 0x5728, + 0x8DDE: 0x6750, + 0x8DDF: 0x7F6A, + 0x8DE0: 0x8CA1, + 0x8DE1: 0x51B4, + 0x8DE2: 0x5742, + 0x8DE3: 0x962A, + 0x8DE4: 0x583A, + 0x8DE5: 0x698A, + 0x8DE6: 0x80B4, + 0x8DE7: 0x54B2, + 0x8DE8: 0x5D0E, + 0x8DE9: 0x57FC, + 0x8DEA: 0x7895, + 0x8DEB: 0x9DFA, + 0x8DEC: 0x4F5C, + 0x8DED: 0x524A, + 0x8DEE: 0x548B, + 0x8DEF: 0x643E, + 0x8DF0: 0x6628, + 0x8DF1: 0x6714, + 0x8DF2: 0x67F5, + 0x8DF3: 0x7A84, + 0x8DF4: 0x7B56, + 0x8DF5: 0x7D22, + 0x8DF6: 0x932F, + 0x8DF7: 0x685C, + 0x8DF8: 0x9BAD, + 0x8DF9: 0x7B39, + 0x8DFA: 0x5319, + 0x8DFB: 0x518A, + 0x8DFC: 0x5237, + 0x8E40: 0x5BDF, + 0x8E41: 0x62F6, + 0x8E42: 0x64AE, + 0x8E43: 0x64E6, + 0x8E44: 0x672D, + 0x8E45: 0x6BBA, + 0x8E46: 0x85A9, + 0x8E47: 0x96D1, + 0x8E48: 0x7690, + 0x8E49: 0x9BD6, + 0x8E4A: 0x634C, + 0x8E4B: 0x9306, + 0x8E4C: 0x9BAB, + 0x8E4D: 0x76BF, + 0x8E4E: 0x6652, + 0x8E4F: 0x4E09, + 0x8E50: 0x5098, + 0x8E51: 0x53C2, + 0x8E52: 0x5C71, + 0x8E53: 0x60E8, + 0x8E54: 0x6492, + 0x8E55: 0x6563, + 0x8E56: 0x685F, + 0x8E57: 0x71E6, + 0x8E58: 0x73CA, + 0x8E59: 0x7523, + 0x8E5A: 0x7B97, + 0x8E5B: 0x7E82, + 0x8E5C: 0x8695, + 0x8E5D: 0x8B83, + 0x8E5E: 0x8CDB, + 0x8E5F: 0x9178, + 0x8E60: 0x9910, + 0x8E61: 0x65AC, + 0x8E62: 0x66AB, + 0x8E63: 0x6B8B, + 0x8E64: 0x4ED5, + 0x8E65: 0x4ED4, + 0x8E66: 0x4F3A, + 0x8E67: 0x4F7F, + 0x8E68: 0x523A, + 0x8E69: 0x53F8, + 0x8E6A: 0x53F2, + 0x8E6B: 0x55E3, + 0x8E6C: 0x56DB, + 0x8E6D: 0x58EB, + 0x8E6E: 0x59CB, + 0x8E6F: 0x59C9, + 0x8E70: 0x59FF, + 0x8E71: 0x5B50, + 0x8E72: 0x5C4D, + 0x8E73: 0x5E02, + 0x8E74: 0x5E2B, + 0x8E75: 0x5FD7, + 0x8E76: 0x601D, + 0x8E77: 0x6307, + 0x8E78: 0x652F, + 0x8E79: 0x5B5C, + 0x8E7A: 0x65AF, + 0x8E7B: 0x65BD, + 0x8E7C: 0x65E8, + 0x8E7D: 0x679D, + 0x8E7E: 0x6B62, + 0x8E80: 0x6B7B, + 0x8E81: 0x6C0F, + 0x8E82: 0x7345, + 0x8E83: 0x7949, + 0x8E84: 0x79C1, + 0x8E85: 0x7CF8, + 0x8E86: 0x7D19, + 0x8E87: 0x7D2B, + 0x8E88: 0x80A2, + 0x8E89: 0x8102, + 0x8E8A: 0x81F3, + 0x8E8B: 0x8996, + 0x8E8C: 0x8A5E, + 0x8E8D: 0x8A69, + 0x8E8E: 0x8A66, + 0x8E8F: 0x8A8C, + 0x8E90: 0x8AEE, + 0x8E91: 0x8CC7, + 0x8E92: 0x8CDC, + 0x8E93: 0x96CC, + 0x8E94: 0x98FC, + 0x8E95: 0x6B6F, + 0x8E96: 0x4E8B, + 0x8E97: 0x4F3C, + 0x8E98: 0x4F8D, + 0x8E99: 0x5150, + 0x8E9A: 0x5B57, + 0x8E9B: 0x5BFA, + 0x8E9C: 0x6148, + 0x8E9D: 0x6301, + 0x8E9E: 0x6642, + 0x8E9F: 0x6B21, + 0x8EA0: 0x6ECB, + 0x8EA1: 0x6CBB, + 0x8EA2: 0x723E, + 0x8EA3: 0x74BD, + 0x8EA4: 0x75D4, + 0x8EA5: 0x78C1, + 0x8EA6: 0x793A, + 0x8EA7: 0x800C, + 0x8EA8: 0x8033, + 0x8EA9: 0x81EA, + 0x8EAA: 0x8494, + 0x8EAB: 0x8F9E, + 0x8EAC: 0x6C50, + 0x8EAD: 0x9E7F, + 0x8EAE: 0x5F0F, + 0x8EAF: 0x8B58, + 0x8EB0: 0x9D2B, + 0x8EB1: 0x7AFA, + 0x8EB2: 0x8EF8, + 0x8EB3: 0x5B8D, + 0x8EB4: 0x96EB, + 0x8EB5: 0x4E03, + 0x8EB6: 0x53F1, + 0x8EB7: 0x57F7, + 0x8EB8: 0x5931, + 0x8EB9: 0x5AC9, + 0x8EBA: 0x5BA4, + 0x8EBB: 0x6089, + 0x8EBC: 0x6E7F, + 0x8EBD: 0x6F06, + 0x8EBE: 0x75BE, + 0x8EBF: 0x8CEA, + 0x8EC0: 0x5B9F, + 0x8EC1: 0x8500, + 0x8EC2: 0x7BE0, + 0x8EC3: 0x5072, + 0x8EC4: 0x67F4, + 0x8EC5: 0x829D, + 0x8EC6: 0x5C61, + 0x8EC7: 0x854A, + 0x8EC8: 0x7E1E, + 0x8EC9: 0x820E, + 0x8ECA: 0x5199, + 0x8ECB: 0x5C04, + 0x8ECC: 0x6368, + 0x8ECD: 0x8D66, + 0x8ECE: 0x659C, + 0x8ECF: 0x716E, + 0x8ED0: 0x793E, + 0x8ED1: 0x7D17, + 0x8ED2: 0x8005, + 0x8ED3: 0x8B1D, + 0x8ED4: 0x8ECA, + 0x8ED5: 0x906E, + 0x8ED6: 0x86C7, + 0x8ED7: 0x90AA, + 0x8ED8: 0x501F, + 0x8ED9: 0x52FA, + 0x8EDA: 0x5C3A, + 0x8EDB: 0x6753, + 0x8EDC: 0x707C, + 0x8EDD: 0x7235, + 0x8EDE: 0x914C, + 0x8EDF: 0x91C8, + 0x8EE0: 0x932B, + 0x8EE1: 0x82E5, + 0x8EE2: 0x5BC2, + 0x8EE3: 0x5F31, + 0x8EE4: 0x60F9, + 0x8EE5: 0x4E3B, + 0x8EE6: 0x53D6, + 0x8EE7: 0x5B88, + 0x8EE8: 0x624B, + 0x8EE9: 0x6731, + 0x8EEA: 0x6B8A, + 0x8EEB: 0x72E9, + 0x8EEC: 0x73E0, + 0x8EED: 0x7A2E, + 0x8EEE: 0x816B, + 0x8EEF: 0x8DA3, + 0x8EF0: 0x9152, + 0x8EF1: 0x9996, + 0x8EF2: 0x5112, + 0x8EF3: 0x53D7, + 0x8EF4: 0x546A, + 0x8EF5: 0x5BFF, + 0x8EF6: 0x6388, + 0x8EF7: 0x6A39, + 0x8EF8: 0x7DAC, + 0x8EF9: 0x9700, + 0x8EFA: 0x56DA, + 0x8EFB: 0x53CE, + 0x8EFC: 0x5468, + 0x8F40: 0x5B97, + 0x8F41: 0x5C31, + 0x8F42: 0x5DDE, + 0x8F43: 0x4FEE, + 0x8F44: 0x6101, + 0x8F45: 0x62FE, + 0x8F46: 0x6D32, + 0x8F47: 0x79C0, + 0x8F48: 0x79CB, + 0x8F49: 0x7D42, + 0x8F4A: 0x7E4D, + 0x8F4B: 0x7FD2, + 0x8F4C: 0x81ED, + 0x8F4D: 0x821F, + 0x8F4E: 0x8490, + 0x8F4F: 0x8846, + 0x8F50: 0x8972, + 0x8F51: 0x8B90, + 0x8F52: 0x8E74, + 0x8F53: 0x8F2F, + 0x8F54: 0x9031, + 0x8F55: 0x914B, + 0x8F56: 0x916C, + 0x8F57: 0x96C6, + 0x8F58: 0x919C, + 0x8F59: 0x4EC0, + 0x8F5A: 0x4F4F, + 0x8F5B: 0x5145, + 0x8F5C: 0x5341, + 0x8F5D: 0x5F93, + 0x8F5E: 0x620E, + 0x8F5F: 0x67D4, + 0x8F60: 0x6C41, + 0x8F61: 0x6E0B, + 0x8F62: 0x7363, + 0x8F63: 0x7E26, + 0x8F64: 0x91CD, + 0x8F65: 0x9283, + 0x8F66: 0x53D4, + 0x8F67: 0x5919, + 0x8F68: 0x5BBF, + 0x8F69: 0x6DD1, + 0x8F6A: 0x795D, + 0x8F6B: 0x7E2E, + 0x8F6C: 0x7C9B, + 0x8F6D: 0x587E, + 0x8F6E: 0x719F, + 0x8F6F: 0x51FA, + 0x8F70: 0x8853, + 0x8F71: 0x8FF0, + 0x8F72: 0x4FCA, + 0x8F73: 0x5CFB, + 0x8F74: 0x6625, + 0x8F75: 0x77AC, + 0x8F76: 0x7AE3, + 0x8F77: 0x821C, + 0x8F78: 0x99FF, + 0x8F79: 0x51C6, + 0x8F7A: 0x5FAA, + 0x8F7B: 0x65EC, + 0x8F7C: 0x696F, + 0x8F7D: 0x6B89, + 0x8F7E: 0x6DF3, + 0x8F80: 0x6E96, + 0x8F81: 0x6F64, + 0x8F82: 0x76FE, + 0x8F83: 0x7D14, + 0x8F84: 0x5DE1, + 0x8F85: 0x9075, + 0x8F86: 0x9187, + 0x8F87: 0x9806, + 0x8F88: 0x51E6, + 0x8F89: 0x521D, + 0x8F8A: 0x6240, + 0x8F8B: 0x6691, + 0x8F8C: 0x66D9, + 0x8F8D: 0x6E1A, + 0x8F8E: 0x5EB6, + 0x8F8F: 0x7DD2, + 0x8F90: 0x7F72, + 0x8F91: 0x66F8, + 0x8F92: 0x85AF, + 0x8F93: 0x85F7, + 0x8F94: 0x8AF8, + 0x8F95: 0x52A9, + 0x8F96: 0x53D9, + 0x8F97: 0x5973, + 0x8F98: 0x5E8F, + 0x8F99: 0x5F90, + 0x8F9A: 0x6055, + 0x8F9B: 0x92E4, + 0x8F9C: 0x9664, + 0x8F9D: 0x50B7, + 0x8F9E: 0x511F, + 0x8F9F: 0x52DD, + 0x8FA0: 0x5320, + 0x8FA1: 0x5347, + 0x8FA2: 0x53EC, + 0x8FA3: 0x54E8, + 0x8FA4: 0x5546, + 0x8FA5: 0x5531, + 0x8FA6: 0x5617, + 0x8FA7: 0x5968, + 0x8FA8: 0x59BE, + 0x8FA9: 0x5A3C, + 0x8FAA: 0x5BB5, + 0x8FAB: 0x5C06, + 0x8FAC: 0x5C0F, + 0x8FAD: 0x5C11, + 0x8FAE: 0x5C1A, + 0x8FAF: 0x5E84, + 0x8FB0: 0x5E8A, + 0x8FB1: 0x5EE0, + 0x8FB2: 0x5F70, + 0x8FB3: 0x627F, + 0x8FB4: 0x6284, + 0x8FB5: 0x62DB, + 0x8FB6: 0x638C, + 0x8FB7: 0x6377, + 0x8FB8: 0x6607, + 0x8FB9: 0x660C, + 0x8FBA: 0x662D, + 0x8FBB: 0x6676, + 0x8FBC: 0x677E, + 0x8FBD: 0x68A2, + 0x8FBE: 0x6A1F, + 0x8FBF: 0x6A35, + 0x8FC0: 0x6CBC, + 0x8FC1: 0x6D88, + 0x8FC2: 0x6E09, + 0x8FC3: 0x6E58, + 0x8FC4: 0x713C, + 0x8FC5: 0x7126, + 0x8FC6: 0x7167, + 0x8FC7: 0x75C7, + 0x8FC8: 0x7701, + 0x8FC9: 0x785D, + 0x8FCA: 0x7901, + 0x8FCB: 0x7965, + 0x8FCC: 0x79F0, + 0x8FCD: 0x7AE0, + 0x8FCE: 0x7B11, + 0x8FCF: 0x7CA7, + 0x8FD0: 0x7D39, + 0x8FD1: 0x8096, + 0x8FD2: 0x83D6, + 0x8FD3: 0x848B, + 0x8FD4: 0x8549, + 0x8FD5: 0x885D, + 0x8FD6: 0x88F3, + 0x8FD7: 0x8A1F, + 0x8FD8: 0x8A3C, + 0x8FD9: 0x8A54, + 0x8FDA: 0x8A73, + 0x8FDB: 0x8C61, + 0x8FDC: 0x8CDE, + 0x8FDD: 0x91A4, + 0x8FDE: 0x9266, + 0x8FDF: 0x937E, + 0x8FE0: 0x9418, + 0x8FE1: 0x969C, + 0x8FE2: 0x9798, + 0x8FE3: 0x4E0A, + 0x8FE4: 0x4E08, + 0x8FE5: 0x4E1E, + 0x8FE6: 0x4E57, + 0x8FE7: 0x5197, + 0x8FE8: 0x5270, + 0x8FE9: 0x57CE, + 0x8FEA: 0x5834, + 0x8FEB: 0x58CC, + 0x8FEC: 0x5B22, + 0x8FED: 0x5E38, + 0x8FEE: 0x60C5, + 0x8FEF: 0x64FE, + 0x8FF0: 0x6761, + 0x8FF1: 0x6756, + 0x8FF2: 0x6D44, + 0x8FF3: 0x72B6, + 0x8FF4: 0x7573, + 0x8FF5: 0x7A63, + 0x8FF6: 0x84B8, + 0x8FF7: 0x8B72, + 0x8FF8: 0x91B8, + 0x8FF9: 0x9320, + 0x8FFA: 0x5631, + 0x8FFB: 0x57F4, + 0x8FFC: 0x98FE, + 0x9040: 0x62ED, + 0x9041: 0x690D, + 0x9042: 0x6B96, + 0x9043: 0x71ED, + 0x9044: 0x7E54, + 0x9045: 0x8077, + 0x9046: 0x8272, + 0x9047: 0x89E6, + 0x9048: 0x98DF, + 0x9049: 0x8755, + 0x904A: 0x8FB1, + 0x904B: 0x5C3B, + 0x904C: 0x4F38, + 0x904D: 0x4FE1, + 0x904E: 0x4FB5, + 0x904F: 0x5507, + 0x9050: 0x5A20, + 0x9051: 0x5BDD, + 0x9052: 0x5BE9, + 0x9053: 0x5FC3, + 0x9054: 0x614E, + 0x9055: 0x632F, + 0x9056: 0x65B0, + 0x9057: 0x664B, + 0x9058: 0x68EE, + 0x9059: 0x699B, + 0x905A: 0x6D78, + 0x905B: 0x6DF1, + 0x905C: 0x7533, + 0x905D: 0x75B9, + 0x905E: 0x771F, + 0x905F: 0x795E, + 0x9060: 0x79E6, + 0x9061: 0x7D33, + 0x9062: 0x81E3, + 0x9063: 0x82AF, + 0x9064: 0x85AA, + 0x9065: 0x89AA, + 0x9066: 0x8A3A, + 0x9067: 0x8EAB, + 0x9068: 0x8F9B, + 0x9069: 0x9032, + 0x906A: 0x91DD, + 0x906B: 0x9707, + 0x906C: 0x4EBA, + 0x906D: 0x4EC1, + 0x906E: 0x5203, + 0x906F: 0x5875, + 0x9070: 0x58EC, + 0x9071: 0x5C0B, + 0x9072: 0x751A, + 0x9073: 0x5C3D, + 0x9074: 0x814E, + 0x9075: 0x8A0A, + 0x9076: 0x8FC5, + 0x9077: 0x9663, + 0x9078: 0x976D, + 0x9079: 0x7B25, + 0x907A: 0x8ACF, + 0x907B: 0x9808, + 0x907C: 0x9162, + 0x907D: 0x56F3, + 0x907E: 0x53A8, + 0x9080: 0x9017, + 0x9081: 0x5439, + 0x9082: 0x5782, + 0x9083: 0x5E25, + 0x9084: 0x63A8, + 0x9085: 0x6C34, + 0x9086: 0x708A, + 0x9087: 0x7761, + 0x9088: 0x7C8B, + 0x9089: 0x7FE0, + 0x908A: 0x8870, + 0x908B: 0x9042, + 0x908C: 0x9154, + 0x908D: 0x9310, + 0x908E: 0x9318, + 0x908F: 0x968F, + 0x9090: 0x745E, + 0x9091: 0x9AC4, + 0x9092: 0x5D07, + 0x9093: 0x5D69, + 0x9094: 0x6570, + 0x9095: 0x67A2, + 0x9096: 0x8DA8, + 0x9097: 0x96DB, + 0x9098: 0x636E, + 0x9099: 0x6749, + 0x909A: 0x6919, + 0x909B: 0x83C5, + 0x909C: 0x9817, + 0x909D: 0x96C0, + 0x909E: 0x88FE, + 0x909F: 0x6F84, + 0x90A0: 0x647A, + 0x90A1: 0x5BF8, + 0x90A2: 0x4E16, + 0x90A3: 0x702C, + 0x90A4: 0x755D, + 0x90A5: 0x662F, + 0x90A6: 0x51C4, + 0x90A7: 0x5236, + 0x90A8: 0x52E2, + 0x90A9: 0x59D3, + 0x90AA: 0x5F81, + 0x90AB: 0x6027, + 0x90AC: 0x6210, + 0x90AD: 0x653F, + 0x90AE: 0x6574, + 0x90AF: 0x661F, + 0x90B0: 0x6674, + 0x90B1: 0x68F2, + 0x90B2: 0x6816, + 0x90B3: 0x6B63, + 0x90B4: 0x6E05, + 0x90B5: 0x7272, + 0x90B6: 0x751F, + 0x90B7: 0x76DB, + 0x90B8: 0x7CBE, + 0x90B9: 0x8056, + 0x90BA: 0x58F0, + 0x90BB: 0x88FD, + 0x90BC: 0x897F, + 0x90BD: 0x8AA0, + 0x90BE: 0x8A93, + 0x90BF: 0x8ACB, + 0x90C0: 0x901D, + 0x90C1: 0x9192, + 0x90C2: 0x9752, + 0x90C3: 0x9759, + 0x90C4: 0x6589, + 0x90C5: 0x7A0E, + 0x90C6: 0x8106, + 0x90C7: 0x96BB, + 0x90C8: 0x5E2D, + 0x90C9: 0x60DC, + 0x90CA: 0x621A, + 0x90CB: 0x65A5, + 0x90CC: 0x6614, + 0x90CD: 0x6790, + 0x90CE: 0x77F3, + 0x90CF: 0x7A4D, + 0x90D0: 0x7C4D, + 0x90D1: 0x7E3E, + 0x90D2: 0x810A, + 0x90D3: 0x8CAC, + 0x90D4: 0x8D64, + 0x90D5: 0x8DE1, + 0x90D6: 0x8E5F, + 0x90D7: 0x78A9, + 0x90D8: 0x5207, + 0x90D9: 0x62D9, + 0x90DA: 0x63A5, + 0x90DB: 0x6442, + 0x90DC: 0x6298, + 0x90DD: 0x8A2D, + 0x90DE: 0x7A83, + 0x90DF: 0x7BC0, + 0x90E0: 0x8AAC, + 0x90E1: 0x96EA, + 0x90E2: 0x7D76, + 0x90E3: 0x820C, + 0x90E4: 0x8749, + 0x90E5: 0x4ED9, + 0x90E6: 0x5148, + 0x90E7: 0x5343, + 0x90E8: 0x5360, + 0x90E9: 0x5BA3, + 0x90EA: 0x5C02, + 0x90EB: 0x5C16, + 0x90EC: 0x5DDD, + 0x90ED: 0x6226, + 0x90EE: 0x6247, + 0x90EF: 0x64B0, + 0x90F0: 0x6813, + 0x90F1: 0x6834, + 0x90F2: 0x6CC9, + 0x90F3: 0x6D45, + 0x90F4: 0x6D17, + 0x90F5: 0x67D3, + 0x90F6: 0x6F5C, + 0x90F7: 0x714E, + 0x90F8: 0x717D, + 0x90F9: 0x65CB, + 0x90FA: 0x7A7F, + 0x90FB: 0x7BAD, + 0x90FC: 0x7DDA, + 0x9140: 0x7E4A, + 0x9141: 0x7FA8, + 0x9142: 0x817A, + 0x9143: 0x821B, + 0x9144: 0x8239, + 0x9145: 0x85A6, + 0x9146: 0x8A6E, + 0x9147: 0x8CCE, + 0x9148: 0x8DF5, + 0x9149: 0x9078, + 0x914A: 0x9077, + 0x914B: 0x92AD, + 0x914C: 0x9291, + 0x914D: 0x9583, + 0x914E: 0x9BAE, + 0x914F: 0x524D, + 0x9150: 0x5584, + 0x9151: 0x6F38, + 0x9152: 0x7136, + 0x9153: 0x5168, + 0x9154: 0x7985, + 0x9155: 0x7E55, + 0x9156: 0x81B3, + 0x9157: 0x7CCE, + 0x9158: 0x564C, + 0x9159: 0x5851, + 0x915A: 0x5CA8, + 0x915B: 0x63AA, + 0x915C: 0x66FE, + 0x915D: 0x66FD, + 0x915E: 0x695A, + 0x915F: 0x72D9, + 0x9160: 0x758F, + 0x9161: 0x758E, + 0x9162: 0x790E, + 0x9163: 0x7956, + 0x9164: 0x79DF, + 0x9165: 0x7C97, + 0x9166: 0x7D20, + 0x9167: 0x7D44, + 0x9168: 0x8607, + 0x9169: 0x8A34, + 0x916A: 0x963B, + 0x916B: 0x9061, + 0x916C: 0x9F20, + 0x916D: 0x50E7, + 0x916E: 0x5275, + 0x916F: 0x53CC, + 0x9170: 0x53E2, + 0x9171: 0x5009, + 0x9172: 0x55AA, + 0x9173: 0x58EE, + 0x9174: 0x594F, + 0x9175: 0x723D, + 0x9176: 0x5B8B, + 0x9177: 0x5C64, + 0x9178: 0x531D, + 0x9179: 0x60E3, + 0x917A: 0x60F3, + 0x917B: 0x635C, + 0x917C: 0x6383, + 0x917D: 0x633F, + 0x917E: 0x63BB, + 0x9180: 0x64CD, + 0x9181: 0x65E9, + 0x9182: 0x66F9, + 0x9183: 0x5DE3, + 0x9184: 0x69CD, + 0x9185: 0x69FD, + 0x9186: 0x6F15, + 0x9187: 0x71E5, + 0x9188: 0x4E89, + 0x9189: 0x75E9, + 0x918A: 0x76F8, + 0x918B: 0x7A93, + 0x918C: 0x7CDF, + 0x918D: 0x7DCF, + 0x918E: 0x7D9C, + 0x918F: 0x8061, + 0x9190: 0x8349, + 0x9191: 0x8358, + 0x9192: 0x846C, + 0x9193: 0x84BC, + 0x9194: 0x85FB, + 0x9195: 0x88C5, + 0x9196: 0x8D70, + 0x9197: 0x9001, + 0x9198: 0x906D, + 0x9199: 0x9397, + 0x919A: 0x971C, + 0x919B: 0x9A12, + 0x919C: 0x50CF, + 0x919D: 0x5897, + 0x919E: 0x618E, + 0x919F: 0x81D3, + 0x91A0: 0x8535, + 0x91A1: 0x8D08, + 0x91A2: 0x9020, + 0x91A3: 0x4FC3, + 0x91A4: 0x5074, + 0x91A5: 0x5247, + 0x91A6: 0x5373, + 0x91A7: 0x606F, + 0x91A8: 0x6349, + 0x91A9: 0x675F, + 0x91AA: 0x6E2C, + 0x91AB: 0x8DB3, + 0x91AC: 0x901F, + 0x91AD: 0x4FD7, + 0x91AE: 0x5C5E, + 0x91AF: 0x8CCA, + 0x91B0: 0x65CF, + 0x91B1: 0x7D9A, + 0x91B2: 0x5352, + 0x91B3: 0x8896, + 0x91B4: 0x5176, + 0x91B5: 0x63C3, + 0x91B6: 0x5B58, + 0x91B7: 0x5B6B, + 0x91B8: 0x5C0A, + 0x91B9: 0x640D, + 0x91BA: 0x6751, + 0x91BB: 0x905C, + 0x91BC: 0x4ED6, + 0x91BD: 0x591A, + 0x91BE: 0x592A, + 0x91BF: 0x6C70, + 0x91C0: 0x8A51, + 0x91C1: 0x553E, + 0x91C2: 0x5815, + 0x91C3: 0x59A5, + 0x91C4: 0x60F0, + 0x91C5: 0x6253, + 0x91C6: 0x67C1, + 0x91C7: 0x8235, + 0x91C8: 0x6955, + 0x91C9: 0x9640, + 0x91CA: 0x99C4, + 0x91CB: 0x9A28, + 0x91CC: 0x4F53, + 0x91CD: 0x5806, + 0x91CE: 0x5BFE, + 0x91CF: 0x8010, + 0x91D0: 0x5CB1, + 0x91D1: 0x5E2F, + 0x91D2: 0x5F85, + 0x91D3: 0x6020, + 0x91D4: 0x614B, + 0x91D5: 0x6234, + 0x91D6: 0x66FF, + 0x91D7: 0x6CF0, + 0x91D8: 0x6EDE, + 0x91D9: 0x80CE, + 0x91DA: 0x817F, + 0x91DB: 0x82D4, + 0x91DC: 0x888B, + 0x91DD: 0x8CB8, + 0x91DE: 0x9000, + 0x91DF: 0x902E, + 0x91E0: 0x968A, + 0x91E1: 0x9EDB, + 0x91E2: 0x9BDB, + 0x91E3: 0x4EE3, + 0x91E4: 0x53F0, + 0x91E5: 0x5927, + 0x91E6: 0x7B2C, + 0x91E7: 0x918D, + 0x91E8: 0x984C, + 0x91E9: 0x9DF9, + 0x91EA: 0x6EDD, + 0x91EB: 0x7027, + 0x91EC: 0x5353, + 0x91ED: 0x5544, + 0x91EE: 0x5B85, + 0x91EF: 0x6258, + 0x91F0: 0x629E, + 0x91F1: 0x62D3, + 0x91F2: 0x6CA2, + 0x91F3: 0x6FEF, + 0x91F4: 0x7422, + 0x91F5: 0x8A17, + 0x91F6: 0x9438, + 0x91F7: 0x6FC1, + 0x91F8: 0x8AFE, + 0x91F9: 0x8338, + 0x91FA: 0x51E7, + 0x91FB: 0x86F8, + 0x91FC: 0x53EA, + 0x9240: 0x53E9, + 0x9241: 0x4F46, + 0x9242: 0x9054, + 0x9243: 0x8FB0, + 0x9244: 0x596A, + 0x9245: 0x8131, + 0x9246: 0x5DFD, + 0x9247: 0x7AEA, + 0x9248: 0x8FBF, + 0x9249: 0x68DA, + 0x924A: 0x8C37, + 0x924B: 0x72F8, + 0x924C: 0x9C48, + 0x924D: 0x6A3D, + 0x924E: 0x8AB0, + 0x924F: 0x4E39, + 0x9250: 0x5358, + 0x9251: 0x5606, + 0x9252: 0x5766, + 0x9253: 0x62C5, + 0x9254: 0x63A2, + 0x9255: 0x65E6, + 0x9256: 0x6B4E, + 0x9257: 0x6DE1, + 0x9258: 0x6E5B, + 0x9259: 0x70AD, + 0x925A: 0x77ED, + 0x925B: 0x7AEF, + 0x925C: 0x7BAA, + 0x925D: 0x7DBB, + 0x925E: 0x803D, + 0x925F: 0x80C6, + 0x9260: 0x86CB, + 0x9261: 0x8A95, + 0x9262: 0x935B, + 0x9263: 0x56E3, + 0x9264: 0x58C7, + 0x9265: 0x5F3E, + 0x9266: 0x65AD, + 0x9267: 0x6696, + 0x9268: 0x6A80, + 0x9269: 0x6BB5, + 0x926A: 0x7537, + 0x926B: 0x8AC7, + 0x926C: 0x5024, + 0x926D: 0x77E5, + 0x926E: 0x5730, + 0x926F: 0x5F1B, + 0x9270: 0x6065, + 0x9271: 0x667A, + 0x9272: 0x6C60, + 0x9273: 0x75F4, + 0x9274: 0x7A1A, + 0x9275: 0x7F6E, + 0x9276: 0x81F4, + 0x9277: 0x8718, + 0x9278: 0x9045, + 0x9279: 0x99B3, + 0x927A: 0x7BC9, + 0x927B: 0x755C, + 0x927C: 0x7AF9, + 0x927D: 0x7B51, + 0x927E: 0x84C4, + 0x9280: 0x9010, + 0x9281: 0x79E9, + 0x9282: 0x7A92, + 0x9283: 0x8336, + 0x9284: 0x5AE1, + 0x9285: 0x7740, + 0x9286: 0x4E2D, + 0x9287: 0x4EF2, + 0x9288: 0x5B99, + 0x9289: 0x5FE0, + 0x928A: 0x62BD, + 0x928B: 0x663C, + 0x928C: 0x67F1, + 0x928D: 0x6CE8, + 0x928E: 0x866B, + 0x928F: 0x8877, + 0x9290: 0x8A3B, + 0x9291: 0x914E, + 0x9292: 0x92F3, + 0x9293: 0x99D0, + 0x9294: 0x6A17, + 0x9295: 0x7026, + 0x9296: 0x732A, + 0x9297: 0x82E7, + 0x9298: 0x8457, + 0x9299: 0x8CAF, + 0x929A: 0x4E01, + 0x929B: 0x5146, + 0x929C: 0x51CB, + 0x929D: 0x558B, + 0x929E: 0x5BF5, + 0x929F: 0x5E16, + 0x92A0: 0x5E33, + 0x92A1: 0x5E81, + 0x92A2: 0x5F14, + 0x92A3: 0x5F35, + 0x92A4: 0x5F6B, + 0x92A5: 0x5FB4, + 0x92A6: 0x61F2, + 0x92A7: 0x6311, + 0x92A8: 0x66A2, + 0x92A9: 0x671D, + 0x92AA: 0x6F6E, + 0x92AB: 0x7252, + 0x92AC: 0x753A, + 0x92AD: 0x773A, + 0x92AE: 0x8074, + 0x92AF: 0x8139, + 0x92B0: 0x8178, + 0x92B1: 0x8776, + 0x92B2: 0x8ABF, + 0x92B3: 0x8ADC, + 0x92B4: 0x8D85, + 0x92B5: 0x8DF3, + 0x92B6: 0x929A, + 0x92B7: 0x9577, + 0x92B8: 0x9802, + 0x92B9: 0x9CE5, + 0x92BA: 0x52C5, + 0x92BB: 0x6357, + 0x92BC: 0x76F4, + 0x92BD: 0x6715, + 0x92BE: 0x6C88, + 0x92BF: 0x73CD, + 0x92C0: 0x8CC3, + 0x92C1: 0x93AE, + 0x92C2: 0x9673, + 0x92C3: 0x6D25, + 0x92C4: 0x589C, + 0x92C5: 0x690E, + 0x92C6: 0x69CC, + 0x92C7: 0x8FFD, + 0x92C8: 0x939A, + 0x92C9: 0x75DB, + 0x92CA: 0x901A, + 0x92CB: 0x585A, + 0x92CC: 0x6802, + 0x92CD: 0x63B4, + 0x92CE: 0x69FB, + 0x92CF: 0x4F43, + 0x92D0: 0x6F2C, + 0x92D1: 0x67D8, + 0x92D2: 0x8FBB, + 0x92D3: 0x8526, + 0x92D4: 0x7DB4, + 0x92D5: 0x9354, + 0x92D6: 0x693F, + 0x92D7: 0x6F70, + 0x92D8: 0x576A, + 0x92D9: 0x58F7, + 0x92DA: 0x5B2C, + 0x92DB: 0x7D2C, + 0x92DC: 0x722A, + 0x92DD: 0x540A, + 0x92DE: 0x91E3, + 0x92DF: 0x9DB4, + 0x92E0: 0x4EAD, + 0x92E1: 0x4F4E, + 0x92E2: 0x505C, + 0x92E3: 0x5075, + 0x92E4: 0x5243, + 0x92E5: 0x8C9E, + 0x92E6: 0x5448, + 0x92E7: 0x5824, + 0x92E8: 0x5B9A, + 0x92E9: 0x5E1D, + 0x92EA: 0x5E95, + 0x92EB: 0x5EAD, + 0x92EC: 0x5EF7, + 0x92ED: 0x5F1F, + 0x92EE: 0x608C, + 0x92EF: 0x62B5, + 0x92F0: 0x633A, + 0x92F1: 0x63D0, + 0x92F2: 0x68AF, + 0x92F3: 0x6C40, + 0x92F4: 0x7887, + 0x92F5: 0x798E, + 0x92F6: 0x7A0B, + 0x92F7: 0x7DE0, + 0x92F8: 0x8247, + 0x92F9: 0x8A02, + 0x92FA: 0x8AE6, + 0x92FB: 0x8E44, + 0x92FC: 0x9013, + 0x9340: 0x90B8, + 0x9341: 0x912D, + 0x9342: 0x91D8, + 0x9343: 0x9F0E, + 0x9344: 0x6CE5, + 0x9345: 0x6458, + 0x9346: 0x64E2, + 0x9347: 0x6575, + 0x9348: 0x6EF4, + 0x9349: 0x7684, + 0x934A: 0x7B1B, + 0x934B: 0x9069, + 0x934C: 0x93D1, + 0x934D: 0x6EBA, + 0x934E: 0x54F2, + 0x934F: 0x5FB9, + 0x9350: 0x64A4, + 0x9351: 0x8F4D, + 0x9352: 0x8FED, + 0x9353: 0x9244, + 0x9354: 0x5178, + 0x9355: 0x586B, + 0x9356: 0x5929, + 0x9357: 0x5C55, + 0x9358: 0x5E97, + 0x9359: 0x6DFB, + 0x935A: 0x7E8F, + 0x935B: 0x751C, + 0x935C: 0x8CBC, + 0x935D: 0x8EE2, + 0x935E: 0x985B, + 0x935F: 0x70B9, + 0x9360: 0x4F1D, + 0x9361: 0x6BBF, + 0x9362: 0x6FB1, + 0x9363: 0x7530, + 0x9364: 0x96FB, + 0x9365: 0x514E, + 0x9366: 0x5410, + 0x9367: 0x5835, + 0x9368: 0x5857, + 0x9369: 0x59AC, + 0x936A: 0x5C60, + 0x936B: 0x5F92, + 0x936C: 0x6597, + 0x936D: 0x675C, + 0x936E: 0x6E21, + 0x936F: 0x767B, + 0x9370: 0x83DF, + 0x9371: 0x8CED, + 0x9372: 0x9014, + 0x9373: 0x90FD, + 0x9374: 0x934D, + 0x9375: 0x7825, + 0x9376: 0x783A, + 0x9377: 0x52AA, + 0x9378: 0x5EA6, + 0x9379: 0x571F, + 0x937A: 0x5974, + 0x937B: 0x6012, + 0x937C: 0x5012, + 0x937D: 0x515A, + 0x937E: 0x51AC, + 0x9380: 0x51CD, + 0x9381: 0x5200, + 0x9382: 0x5510, + 0x9383: 0x5854, + 0x9384: 0x5858, + 0x9385: 0x5957, + 0x9386: 0x5B95, + 0x9387: 0x5CF6, + 0x9388: 0x5D8B, + 0x9389: 0x60BC, + 0x938A: 0x6295, + 0x938B: 0x642D, + 0x938C: 0x6771, + 0x938D: 0x6843, + 0x938E: 0x68BC, + 0x938F: 0x68DF, + 0x9390: 0x76D7, + 0x9391: 0x6DD8, + 0x9392: 0x6E6F, + 0x9393: 0x6D9B, + 0x9394: 0x706F, + 0x9395: 0x71C8, + 0x9396: 0x5F53, + 0x9397: 0x75D8, + 0x9398: 0x7977, + 0x9399: 0x7B49, + 0x939A: 0x7B54, + 0x939B: 0x7B52, + 0x939C: 0x7CD6, + 0x939D: 0x7D71, + 0x939E: 0x5230, + 0x939F: 0x8463, + 0x93A0: 0x8569, + 0x93A1: 0x85E4, + 0x93A2: 0x8A0E, + 0x93A3: 0x8B04, + 0x93A4: 0x8C46, + 0x93A5: 0x8E0F, + 0x93A6: 0x9003, + 0x93A7: 0x900F, + 0x93A8: 0x9419, + 0x93A9: 0x9676, + 0x93AA: 0x982D, + 0x93AB: 0x9A30, + 0x93AC: 0x95D8, + 0x93AD: 0x50CD, + 0x93AE: 0x52D5, + 0x93AF: 0x540C, + 0x93B0: 0x5802, + 0x93B1: 0x5C0E, + 0x93B2: 0x61A7, + 0x93B3: 0x649E, + 0x93B4: 0x6D1E, + 0x93B5: 0x77B3, + 0x93B6: 0x7AE5, + 0x93B7: 0x80F4, + 0x93B8: 0x8404, + 0x93B9: 0x9053, + 0x93BA: 0x9285, + 0x93BB: 0x5CE0, + 0x93BC: 0x9D07, + 0x93BD: 0x533F, + 0x93BE: 0x5F97, + 0x93BF: 0x5FB3, + 0x93C0: 0x6D9C, + 0x93C1: 0x7279, + 0x93C2: 0x7763, + 0x93C3: 0x79BF, + 0x93C4: 0x7BE4, + 0x93C5: 0x6BD2, + 0x93C6: 0x72EC, + 0x93C7: 0x8AAD, + 0x93C8: 0x6803, + 0x93C9: 0x6A61, + 0x93CA: 0x51F8, + 0x93CB: 0x7A81, + 0x93CC: 0x6934, + 0x93CD: 0x5C4A, + 0x93CE: 0x9CF6, + 0x93CF: 0x82EB, + 0x93D0: 0x5BC5, + 0x93D1: 0x9149, + 0x93D2: 0x701E, + 0x93D3: 0x5678, + 0x93D4: 0x5C6F, + 0x93D5: 0x60C7, + 0x93D6: 0x6566, + 0x93D7: 0x6C8C, + 0x93D8: 0x8C5A, + 0x93D9: 0x9041, + 0x93DA: 0x9813, + 0x93DB: 0x5451, + 0x93DC: 0x66C7, + 0x93DD: 0x920D, + 0x93DE: 0x5948, + 0x93DF: 0x90A3, + 0x93E0: 0x5185, + 0x93E1: 0x4E4D, + 0x93E2: 0x51EA, + 0x93E3: 0x8599, + 0x93E4: 0x8B0E, + 0x93E5: 0x7058, + 0x93E6: 0x637A, + 0x93E7: 0x934B, + 0x93E8: 0x6962, + 0x93E9: 0x99B4, + 0x93EA: 0x7E04, + 0x93EB: 0x7577, + 0x93EC: 0x5357, + 0x93ED: 0x6960, + 0x93EE: 0x8EDF, + 0x93EF: 0x96E3, + 0x93F0: 0x6C5D, + 0x93F1: 0x4E8C, + 0x93F2: 0x5C3C, + 0x93F3: 0x5F10, + 0x93F4: 0x8FE9, + 0x93F5: 0x5302, + 0x93F6: 0x8CD1, + 0x93F7: 0x8089, + 0x93F8: 0x8679, + 0x93F9: 0x5EFF, + 0x93FA: 0x65E5, + 0x93FB: 0x4E73, + 0x93FC: 0x5165, + 0x9440: 0x5982, + 0x9441: 0x5C3F, + 0x9442: 0x97EE, + 0x9443: 0x4EFB, + 0x9444: 0x598A, + 0x9445: 0x5FCD, + 0x9446: 0x8A8D, + 0x9447: 0x6FE1, + 0x9448: 0x79B0, + 0x9449: 0x7962, + 0x944A: 0x5BE7, + 0x944B: 0x8471, + 0x944C: 0x732B, + 0x944D: 0x71B1, + 0x944E: 0x5E74, + 0x944F: 0x5FF5, + 0x9450: 0x637B, + 0x9451: 0x649A, + 0x9452: 0x71C3, + 0x9453: 0x7C98, + 0x9454: 0x4E43, + 0x9455: 0x5EFC, + 0x9456: 0x4E4B, + 0x9457: 0x57DC, + 0x9458: 0x56A2, + 0x9459: 0x60A9, + 0x945A: 0x6FC3, + 0x945B: 0x7D0D, + 0x945C: 0x80FD, + 0x945D: 0x8133, + 0x945E: 0x81BF, + 0x945F: 0x8FB2, + 0x9460: 0x8997, + 0x9461: 0x86A4, + 0x9462: 0x5DF4, + 0x9463: 0x628A, + 0x9464: 0x64AD, + 0x9465: 0x8987, + 0x9466: 0x6777, + 0x9467: 0x6CE2, + 0x9468: 0x6D3E, + 0x9469: 0x7436, + 0x946A: 0x7834, + 0x946B: 0x5A46, + 0x946C: 0x7F75, + 0x946D: 0x82AD, + 0x946E: 0x99AC, + 0x946F: 0x4FF3, + 0x9470: 0x5EC3, + 0x9471: 0x62DD, + 0x9472: 0x6392, + 0x9473: 0x6557, + 0x9474: 0x676F, + 0x9475: 0x76C3, + 0x9476: 0x724C, + 0x9477: 0x80CC, + 0x9478: 0x80BA, + 0x9479: 0x8F29, + 0x947A: 0x914D, + 0x947B: 0x500D, + 0x947C: 0x57F9, + 0x947D: 0x5A92, + 0x947E: 0x6885, + 0x9480: 0x6973, + 0x9481: 0x7164, + 0x9482: 0x72FD, + 0x9483: 0x8CB7, + 0x9484: 0x58F2, + 0x9485: 0x8CE0, + 0x9486: 0x966A, + 0x9487: 0x9019, + 0x9488: 0x877F, + 0x9489: 0x79E4, + 0x948A: 0x77E7, + 0x948B: 0x8429, + 0x948C: 0x4F2F, + 0x948D: 0x5265, + 0x948E: 0x535A, + 0x948F: 0x62CD, + 0x9490: 0x67CF, + 0x9491: 0x6CCA, + 0x9492: 0x767D, + 0x9493: 0x7B94, + 0x9494: 0x7C95, + 0x9495: 0x8236, + 0x9496: 0x8584, + 0x9497: 0x8FEB, + 0x9498: 0x66DD, + 0x9499: 0x6F20, + 0x949A: 0x7206, + 0x949B: 0x7E1B, + 0x949C: 0x83AB, + 0x949D: 0x99C1, + 0x949E: 0x9EA6, + 0x949F: 0x51FD, + 0x94A0: 0x7BB1, + 0x94A1: 0x7872, + 0x94A2: 0x7BB8, + 0x94A3: 0x8087, + 0x94A4: 0x7B48, + 0x94A5: 0x6AE8, + 0x94A6: 0x5E61, + 0x94A7: 0x808C, + 0x94A8: 0x7551, + 0x94A9: 0x7560, + 0x94AA: 0x516B, + 0x94AB: 0x9262, + 0x94AC: 0x6E8C, + 0x94AD: 0x767A, + 0x94AE: 0x9197, + 0x94AF: 0x9AEA, + 0x94B0: 0x4F10, + 0x94B1: 0x7F70, + 0x94B2: 0x629C, + 0x94B3: 0x7B4F, + 0x94B4: 0x95A5, + 0x94B5: 0x9CE9, + 0x94B6: 0x567A, + 0x94B7: 0x5859, + 0x94B8: 0x86E4, + 0x94B9: 0x96BC, + 0x94BA: 0x4F34, + 0x94BB: 0x5224, + 0x94BC: 0x534A, + 0x94BD: 0x53CD, + 0x94BE: 0x53DB, + 0x94BF: 0x5E06, + 0x94C0: 0x642C, + 0x94C1: 0x6591, + 0x94C2: 0x677F, + 0x94C3: 0x6C3E, + 0x94C4: 0x6C4E, + 0x94C5: 0x7248, + 0x94C6: 0x72AF, + 0x94C7: 0x73ED, + 0x94C8: 0x7554, + 0x94C9: 0x7E41, + 0x94CA: 0x822C, + 0x94CB: 0x85E9, + 0x94CC: 0x8CA9, + 0x94CD: 0x7BC4, + 0x94CE: 0x91C6, + 0x94CF: 0x7169, + 0x94D0: 0x9812, + 0x94D1: 0x98EF, + 0x94D2: 0x633D, + 0x94D3: 0x6669, + 0x94D4: 0x756A, + 0x94D5: 0x76E4, + 0x94D6: 0x78D0, + 0x94D7: 0x8543, + 0x94D8: 0x86EE, + 0x94D9: 0x532A, + 0x94DA: 0x5351, + 0x94DB: 0x5426, + 0x94DC: 0x5983, + 0x94DD: 0x5E87, + 0x94DE: 0x5F7C, + 0x94DF: 0x60B2, + 0x94E0: 0x6249, + 0x94E1: 0x6279, + 0x94E2: 0x62AB, + 0x94E3: 0x6590, + 0x94E4: 0x6BD4, + 0x94E5: 0x6CCC, + 0x94E6: 0x75B2, + 0x94E7: 0x76AE, + 0x94E8: 0x7891, + 0x94E9: 0x79D8, + 0x94EA: 0x7DCB, + 0x94EB: 0x7F77, + 0x94EC: 0x80A5, + 0x94ED: 0x88AB, + 0x94EE: 0x8AB9, + 0x94EF: 0x8CBB, + 0x94F0: 0x907F, + 0x94F1: 0x975E, + 0x94F2: 0x98DB, + 0x94F3: 0x6A0B, + 0x94F4: 0x7C38, + 0x94F5: 0x5099, + 0x94F6: 0x5C3E, + 0x94F7: 0x5FAE, + 0x94F8: 0x6787, + 0x94F9: 0x6BD8, + 0x94FA: 0x7435, + 0x94FB: 0x7709, + 0x94FC: 0x7F8E, + 0x9540: 0x9F3B, + 0x9541: 0x67CA, + 0x9542: 0x7A17, + 0x9543: 0x5339, + 0x9544: 0x758B, + 0x9545: 0x9AED, + 0x9546: 0x5F66, + 0x9547: 0x819D, + 0x9548: 0x83F1, + 0x9549: 0x8098, + 0x954A: 0x5F3C, + 0x954B: 0x5FC5, + 0x954C: 0x7562, + 0x954D: 0x7B46, + 0x954E: 0x903C, + 0x954F: 0x6867, + 0x9550: 0x59EB, + 0x9551: 0x5A9B, + 0x9552: 0x7D10, + 0x9553: 0x767E, + 0x9554: 0x8B2C, + 0x9555: 0x4FF5, + 0x9556: 0x5F6A, + 0x9557: 0x6A19, + 0x9558: 0x6C37, + 0x9559: 0x6F02, + 0x955A: 0x74E2, + 0x955B: 0x7968, + 0x955C: 0x8868, + 0x955D: 0x8A55, + 0x955E: 0x8C79, + 0x955F: 0x5EDF, + 0x9560: 0x63CF, + 0x9561: 0x75C5, + 0x9562: 0x79D2, + 0x9563: 0x82D7, + 0x9564: 0x9328, + 0x9565: 0x92F2, + 0x9566: 0x849C, + 0x9567: 0x86ED, + 0x9568: 0x9C2D, + 0x9569: 0x54C1, + 0x956A: 0x5F6C, + 0x956B: 0x658C, + 0x956C: 0x6D5C, + 0x956D: 0x7015, + 0x956E: 0x8CA7, + 0x956F: 0x8CD3, + 0x9570: 0x983B, + 0x9571: 0x654F, + 0x9572: 0x74F6, + 0x9573: 0x4E0D, + 0x9574: 0x4ED8, + 0x9575: 0x57E0, + 0x9576: 0x592B, + 0x9577: 0x5A66, + 0x9578: 0x5BCC, + 0x9579: 0x51A8, + 0x957A: 0x5E03, + 0x957B: 0x5E9C, + 0x957C: 0x6016, + 0x957D: 0x6276, + 0x957E: 0x6577, + 0x9580: 0x65A7, + 0x9581: 0x666E, + 0x9582: 0x6D6E, + 0x9583: 0x7236, + 0x9584: 0x7B26, + 0x9585: 0x8150, + 0x9586: 0x819A, + 0x9587: 0x8299, + 0x9588: 0x8B5C, + 0x9589: 0x8CA0, + 0x958A: 0x8CE6, + 0x958B: 0x8D74, + 0x958C: 0x961C, + 0x958D: 0x9644, + 0x958E: 0x4FAE, + 0x958F: 0x64AB, + 0x9590: 0x6B66, + 0x9591: 0x821E, + 0x9592: 0x8461, + 0x9593: 0x856A, + 0x9594: 0x90E8, + 0x9595: 0x5C01, + 0x9596: 0x6953, + 0x9597: 0x98A8, + 0x9598: 0x847A, + 0x9599: 0x8557, + 0x959A: 0x4F0F, + 0x959B: 0x526F, + 0x959C: 0x5FA9, + 0x959D: 0x5E45, + 0x959E: 0x670D, + 0x959F: 0x798F, + 0x95A0: 0x8179, + 0x95A1: 0x8907, + 0x95A2: 0x8986, + 0x95A3: 0x6DF5, + 0x95A4: 0x5F17, + 0x95A5: 0x6255, + 0x95A6: 0x6CB8, + 0x95A7: 0x4ECF, + 0x95A8: 0x7269, + 0x95A9: 0x9B92, + 0x95AA: 0x5206, + 0x95AB: 0x543B, + 0x95AC: 0x5674, + 0x95AD: 0x58B3, + 0x95AE: 0x61A4, + 0x95AF: 0x626E, + 0x95B0: 0x711A, + 0x95B1: 0x596E, + 0x95B2: 0x7C89, + 0x95B3: 0x7CDE, + 0x95B4: 0x7D1B, + 0x95B5: 0x96F0, + 0x95B6: 0x6587, + 0x95B7: 0x805E, + 0x95B8: 0x4E19, + 0x95B9: 0x4F75, + 0x95BA: 0x5175, + 0x95BB: 0x5840, + 0x95BC: 0x5E63, + 0x95BD: 0x5E73, + 0x95BE: 0x5F0A, + 0x95BF: 0x67C4, + 0x95C0: 0x4E26, + 0x95C1: 0x853D, + 0x95C2: 0x9589, + 0x95C3: 0x965B, + 0x95C4: 0x7C73, + 0x95C5: 0x9801, + 0x95C6: 0x50FB, + 0x95C7: 0x58C1, + 0x95C8: 0x7656, + 0x95C9: 0x78A7, + 0x95CA: 0x5225, + 0x95CB: 0x77A5, + 0x95CC: 0x8511, + 0x95CD: 0x7B86, + 0x95CE: 0x504F, + 0x95CF: 0x5909, + 0x95D0: 0x7247, + 0x95D1: 0x7BC7, + 0x95D2: 0x7DE8, + 0x95D3: 0x8FBA, + 0x95D4: 0x8FD4, + 0x95D5: 0x904D, + 0x95D6: 0x4FBF, + 0x95D7: 0x52C9, + 0x95D8: 0x5A29, + 0x95D9: 0x5F01, + 0x95DA: 0x97AD, + 0x95DB: 0x4FDD, + 0x95DC: 0x8217, + 0x95DD: 0x92EA, + 0x95DE: 0x5703, + 0x95DF: 0x6355, + 0x95E0: 0x6B69, + 0x95E1: 0x752B, + 0x95E2: 0x88DC, + 0x95E3: 0x8F14, + 0x95E4: 0x7A42, + 0x95E5: 0x52DF, + 0x95E6: 0x5893, + 0x95E7: 0x6155, + 0x95E8: 0x620A, + 0x95E9: 0x66AE, + 0x95EA: 0x6BCD, + 0x95EB: 0x7C3F, + 0x95EC: 0x83E9, + 0x95ED: 0x5023, + 0x95EE: 0x4FF8, + 0x95EF: 0x5305, + 0x95F0: 0x5446, + 0x95F1: 0x5831, + 0x95F2: 0x5949, + 0x95F3: 0x5B9D, + 0x95F4: 0x5CF0, + 0x95F5: 0x5CEF, + 0x95F6: 0x5D29, + 0x95F7: 0x5E96, + 0x95F8: 0x62B1, + 0x95F9: 0x6367, + 0x95FA: 0x653E, + 0x95FB: 0x65B9, + 0x95FC: 0x670B, + 0x9640: 0x6CD5, + 0x9641: 0x6CE1, + 0x9642: 0x70F9, + 0x9643: 0x7832, + 0x9644: 0x7E2B, + 0x9645: 0x80DE, + 0x9646: 0x82B3, + 0x9647: 0x840C, + 0x9648: 0x84EC, + 0x9649: 0x8702, + 0x964A: 0x8912, + 0x964B: 0x8A2A, + 0x964C: 0x8C4A, + 0x964D: 0x90A6, + 0x964E: 0x92D2, + 0x964F: 0x98FD, + 0x9650: 0x9CF3, + 0x9651: 0x9D6C, + 0x9652: 0x4E4F, + 0x9653: 0x4EA1, + 0x9654: 0x508D, + 0x9655: 0x5256, + 0x9656: 0x574A, + 0x9657: 0x59A8, + 0x9658: 0x5E3D, + 0x9659: 0x5FD8, + 0x965A: 0x5FD9, + 0x965B: 0x623F, + 0x965C: 0x66B4, + 0x965D: 0x671B, + 0x965E: 0x67D0, + 0x965F: 0x68D2, + 0x9660: 0x5192, + 0x9661: 0x7D21, + 0x9662: 0x80AA, + 0x9663: 0x81A8, + 0x9664: 0x8B00, + 0x9665: 0x8C8C, + 0x9666: 0x8CBF, + 0x9667: 0x927E, + 0x9668: 0x9632, + 0x9669: 0x5420, + 0x966A: 0x982C, + 0x966B: 0x5317, + 0x966C: 0x50D5, + 0x966D: 0x535C, + 0x966E: 0x58A8, + 0x966F: 0x64B2, + 0x9670: 0x6734, + 0x9671: 0x7267, + 0x9672: 0x7766, + 0x9673: 0x7A46, + 0x9674: 0x91E6, + 0x9675: 0x52C3, + 0x9676: 0x6CA1, + 0x9677: 0x6B86, + 0x9678: 0x5800, + 0x9679: 0x5E4C, + 0x967A: 0x5954, + 0x967B: 0x672C, + 0x967C: 0x7FFB, + 0x967D: 0x51E1, + 0x967E: 0x76C6, + 0x9680: 0x6469, + 0x9681: 0x78E8, + 0x9682: 0x9B54, + 0x9683: 0x9EBB, + 0x9684: 0x57CB, + 0x9685: 0x59B9, + 0x9686: 0x6627, + 0x9687: 0x679A, + 0x9688: 0x6BCE, + 0x9689: 0x54E9, + 0x968A: 0x69D9, + 0x968B: 0x5E55, + 0x968C: 0x819C, + 0x968D: 0x6795, + 0x968E: 0x9BAA, + 0x968F: 0x67FE, + 0x9690: 0x9C52, + 0x9691: 0x685D, + 0x9692: 0x4EA6, + 0x9693: 0x4FE3, + 0x9694: 0x53C8, + 0x9695: 0x62B9, + 0x9696: 0x672B, + 0x9697: 0x6CAB, + 0x9698: 0x8FC4, + 0x9699: 0x4FAD, + 0x969A: 0x7E6D, + 0x969B: 0x9EBF, + 0x969C: 0x4E07, + 0x969D: 0x6162, + 0x969E: 0x6E80, + 0x969F: 0x6F2B, + 0x96A0: 0x8513, + 0x96A1: 0x5473, + 0x96A2: 0x672A, + 0x96A3: 0x9B45, + 0x96A4: 0x5DF3, + 0x96A5: 0x7B95, + 0x96A6: 0x5CAC, + 0x96A7: 0x5BC6, + 0x96A8: 0x871C, + 0x96A9: 0x6E4A, + 0x96AA: 0x84D1, + 0x96AB: 0x7A14, + 0x96AC: 0x8108, + 0x96AD: 0x5999, + 0x96AE: 0x7C8D, + 0x96AF: 0x6C11, + 0x96B0: 0x7720, + 0x96B1: 0x52D9, + 0x96B2: 0x5922, + 0x96B3: 0x7121, + 0x96B4: 0x725F, + 0x96B5: 0x77DB, + 0x96B6: 0x9727, + 0x96B7: 0x9D61, + 0x96B8: 0x690B, + 0x96B9: 0x5A7F, + 0x96BA: 0x5A18, + 0x96BB: 0x51A5, + 0x96BC: 0x540D, + 0x96BD: 0x547D, + 0x96BE: 0x660E, + 0x96BF: 0x76DF, + 0x96C0: 0x8FF7, + 0x96C1: 0x9298, + 0x96C2: 0x9CF4, + 0x96C3: 0x59EA, + 0x96C4: 0x725D, + 0x96C5: 0x6EC5, + 0x96C6: 0x514D, + 0x96C7: 0x68C9, + 0x96C8: 0x7DBF, + 0x96C9: 0x7DEC, + 0x96CA: 0x9762, + 0x96CB: 0x9EBA, + 0x96CC: 0x6478, + 0x96CD: 0x6A21, + 0x96CE: 0x8302, + 0x96CF: 0x5984, + 0x96D0: 0x5B5F, + 0x96D1: 0x6BDB, + 0x96D2: 0x731B, + 0x96D3: 0x76F2, + 0x96D4: 0x7DB2, + 0x96D5: 0x8017, + 0x96D6: 0x8499, + 0x96D7: 0x5132, + 0x96D8: 0x6728, + 0x96D9: 0x9ED9, + 0x96DA: 0x76EE, + 0x96DB: 0x6762, + 0x96DC: 0x52FF, + 0x96DD: 0x9905, + 0x96DE: 0x5C24, + 0x96DF: 0x623B, + 0x96E0: 0x7C7E, + 0x96E1: 0x8CB0, + 0x96E2: 0x554F, + 0x96E3: 0x60B6, + 0x96E4: 0x7D0B, + 0x96E5: 0x9580, + 0x96E6: 0x5301, + 0x96E7: 0x4E5F, + 0x96E8: 0x51B6, + 0x96E9: 0x591C, + 0x96EA: 0x723A, + 0x96EB: 0x8036, + 0x96EC: 0x91CE, + 0x96ED: 0x5F25, + 0x96EE: 0x77E2, + 0x96EF: 0x5384, + 0x96F0: 0x5F79, + 0x96F1: 0x7D04, + 0x96F2: 0x85AC, + 0x96F3: 0x8A33, + 0x96F4: 0x8E8D, + 0x96F5: 0x9756, + 0x96F6: 0x67F3, + 0x96F7: 0x85AE, + 0x96F8: 0x9453, + 0x96F9: 0x6109, + 0x96FA: 0x6108, + 0x96FB: 0x6CB9, + 0x96FC: 0x7652, + 0x9740: 0x8AED, + 0x9741: 0x8F38, + 0x9742: 0x552F, + 0x9743: 0x4F51, + 0x9744: 0x512A, + 0x9745: 0x52C7, + 0x9746: 0x53CB, + 0x9747: 0x5BA5, + 0x9748: 0x5E7D, + 0x9749: 0x60A0, + 0x974A: 0x6182, + 0x974B: 0x63D6, + 0x974C: 0x6709, + 0x974D: 0x67DA, + 0x974E: 0x6E67, + 0x974F: 0x6D8C, + 0x9750: 0x7336, + 0x9751: 0x7337, + 0x9752: 0x7531, + 0x9753: 0x7950, + 0x9754: 0x88D5, + 0x9755: 0x8A98, + 0x9756: 0x904A, + 0x9757: 0x9091, + 0x9758: 0x90F5, + 0x9759: 0x96C4, + 0x975A: 0x878D, + 0x975B: 0x5915, + 0x975C: 0x4E88, + 0x975D: 0x4F59, + 0x975E: 0x4E0E, + 0x975F: 0x8A89, + 0x9760: 0x8F3F, + 0x9761: 0x9810, + 0x9762: 0x50AD, + 0x9763: 0x5E7C, + 0x9764: 0x5996, + 0x9765: 0x5BB9, + 0x9766: 0x5EB8, + 0x9767: 0x63DA, + 0x9768: 0x63FA, + 0x9769: 0x64C1, + 0x976A: 0x66DC, + 0x976B: 0x694A, + 0x976C: 0x69D8, + 0x976D: 0x6D0B, + 0x976E: 0x6EB6, + 0x976F: 0x7194, + 0x9770: 0x7528, + 0x9771: 0x7AAF, + 0x9772: 0x7F8A, + 0x9773: 0x8000, + 0x9774: 0x8449, + 0x9775: 0x84C9, + 0x9776: 0x8981, + 0x9777: 0x8B21, + 0x9778: 0x8E0A, + 0x9779: 0x9065, + 0x977A: 0x967D, + 0x977B: 0x990A, + 0x977C: 0x617E, + 0x977D: 0x6291, + 0x977E: 0x6B32, + 0x9780: 0x6C83, + 0x9781: 0x6D74, + 0x9782: 0x7FCC, + 0x9783: 0x7FFC, + 0x9784: 0x6DC0, + 0x9785: 0x7F85, + 0x9786: 0x87BA, + 0x9787: 0x88F8, + 0x9788: 0x6765, + 0x9789: 0x83B1, + 0x978A: 0x983C, + 0x978B: 0x96F7, + 0x978C: 0x6D1B, + 0x978D: 0x7D61, + 0x978E: 0x843D, + 0x978F: 0x916A, + 0x9790: 0x4E71, + 0x9791: 0x5375, + 0x9792: 0x5D50, + 0x9793: 0x6B04, + 0x9794: 0x6FEB, + 0x9795: 0x85CD, + 0x9796: 0x862D, + 0x9797: 0x89A7, + 0x9798: 0x5229, + 0x9799: 0x540F, + 0x979A: 0x5C65, + 0x979B: 0x674E, + 0x979C: 0x68A8, + 0x979D: 0x7406, + 0x979E: 0x7483, + 0x979F: 0x75E2, + 0x97A0: 0x88CF, + 0x97A1: 0x88E1, + 0x97A2: 0x91CC, + 0x97A3: 0x96E2, + 0x97A4: 0x9678, + 0x97A5: 0x5F8B, + 0x97A6: 0x7387, + 0x97A7: 0x7ACB, + 0x97A8: 0x844E, + 0x97A9: 0x63A0, + 0x97AA: 0x7565, + 0x97AB: 0x5289, + 0x97AC: 0x6D41, + 0x97AD: 0x6E9C, + 0x97AE: 0x7409, + 0x97AF: 0x7559, + 0x97B0: 0x786B, + 0x97B1: 0x7C92, + 0x97B2: 0x9686, + 0x97B3: 0x7ADC, + 0x97B4: 0x9F8D, + 0x97B5: 0x4FB6, + 0x97B6: 0x616E, + 0x97B7: 0x65C5, + 0x97B8: 0x865C, + 0x97B9: 0x4E86, + 0x97BA: 0x4EAE, + 0x97BB: 0x50DA, + 0x97BC: 0x4E21, + 0x97BD: 0x51CC, + 0x97BE: 0x5BEE, + 0x97BF: 0x6599, + 0x97C0: 0x6881, + 0x97C1: 0x6DBC, + 0x97C2: 0x731F, + 0x97C3: 0x7642, + 0x97C4: 0x77AD, + 0x97C5: 0x7A1C, + 0x97C6: 0x7CE7, + 0x97C7: 0x826F, + 0x97C8: 0x8AD2, + 0x97C9: 0x907C, + 0x97CA: 0x91CF, + 0x97CB: 0x9675, + 0x97CC: 0x9818, + 0x97CD: 0x529B, + 0x97CE: 0x7DD1, + 0x97CF: 0x502B, + 0x97D0: 0x5398, + 0x97D1: 0x6797, + 0x97D2: 0x6DCB, + 0x97D3: 0x71D0, + 0x97D4: 0x7433, + 0x97D5: 0x81E8, + 0x97D6: 0x8F2A, + 0x97D7: 0x96A3, + 0x97D8: 0x9C57, + 0x97D9: 0x9E9F, + 0x97DA: 0x7460, + 0x97DB: 0x5841, + 0x97DC: 0x6D99, + 0x97DD: 0x7D2F, + 0x97DE: 0x985E, + 0x97DF: 0x4EE4, + 0x97E0: 0x4F36, + 0x97E1: 0x4F8B, + 0x97E2: 0x51B7, + 0x97E3: 0x52B1, + 0x97E4: 0x5DBA, + 0x97E5: 0x601C, + 0x97E6: 0x73B2, + 0x97E7: 0x793C, + 0x97E8: 0x82D3, + 0x97E9: 0x9234, + 0x97EA: 0x96B7, + 0x97EB: 0x96F6, + 0x97EC: 0x970A, + 0x97ED: 0x9E97, + 0x97EE: 0x9F62, + 0x97EF: 0x66A6, + 0x97F0: 0x6B74, + 0x97F1: 0x5217, + 0x97F2: 0x52A3, + 0x97F3: 0x70C8, + 0x97F4: 0x88C2, + 0x97F5: 0x5EC9, + 0x97F6: 0x604B, + 0x97F7: 0x6190, + 0x97F8: 0x6F23, + 0x97F9: 0x7149, + 0x97FA: 0x7C3E, + 0x97FB: 0x7DF4, + 0x97FC: 0x806F, + 0x9840: 0x84EE, + 0x9841: 0x9023, + 0x9842: 0x932C, + 0x9843: 0x5442, + 0x9844: 0x9B6F, + 0x9845: 0x6AD3, + 0x9846: 0x7089, + 0x9847: 0x8CC2, + 0x9848: 0x8DEF, + 0x9849: 0x9732, + 0x984A: 0x52B4, + 0x984B: 0x5A41, + 0x984C: 0x5ECA, + 0x984D: 0x5F04, + 0x984E: 0x6717, + 0x984F: 0x697C, + 0x9850: 0x6994, + 0x9851: 0x6D6A, + 0x9852: 0x6F0F, + 0x9853: 0x7262, + 0x9854: 0x72FC, + 0x9855: 0x7BED, + 0x9856: 0x8001, + 0x9857: 0x807E, + 0x9858: 0x874B, + 0x9859: 0x90CE, + 0x985A: 0x516D, + 0x985B: 0x9E93, + 0x985C: 0x7984, + 0x985D: 0x808B, + 0x985E: 0x9332, + 0x985F: 0x8AD6, + 0x9860: 0x502D, + 0x9861: 0x548C, + 0x9862: 0x8A71, + 0x9863: 0x6B6A, + 0x9864: 0x8CC4, + 0x9865: 0x8107, + 0x9866: 0x60D1, + 0x9867: 0x67A0, + 0x9868: 0x9DF2, + 0x9869: 0x4E99, + 0x986A: 0x4E98, + 0x986B: 0x9C10, + 0x986C: 0x8A6B, + 0x986D: 0x85C1, + 0x986E: 0x8568, + 0x986F: 0x6900, + 0x9870: 0x6E7E, + 0x9871: 0x7897, + 0x9872: 0x8155, + 0x989F: 0x5F0C, + 0x98A0: 0x4E10, + 0x98A1: 0x4E15, + 0x98A2: 0x4E2A, + 0x98A3: 0x4E31, + 0x98A4: 0x4E36, + 0x98A5: 0x4E3C, + 0x98A6: 0x4E3F, + 0x98A7: 0x4E42, + 0x98A8: 0x4E56, + 0x98A9: 0x4E58, + 0x98AA: 0x4E82, + 0x98AB: 0x4E85, + 0x98AC: 0x8C6B, + 0x98AD: 0x4E8A, + 0x98AE: 0x8212, + 0x98AF: 0x5F0D, + 0x98B0: 0x4E8E, + 0x98B1: 0x4E9E, + 0x98B2: 0x4E9F, + 0x98B3: 0x4EA0, + 0x98B4: 0x4EA2, + 0x98B5: 0x4EB0, + 0x98B6: 0x4EB3, + 0x98B7: 0x4EB6, + 0x98B8: 0x4ECE, + 0x98B9: 0x4ECD, + 0x98BA: 0x4EC4, + 0x98BB: 0x4EC6, + 0x98BC: 0x4EC2, + 0x98BD: 0x4ED7, + 0x98BE: 0x4EDE, + 0x98BF: 0x4EED, + 0x98C0: 0x4EDF, + 0x98C1: 0x4EF7, + 0x98C2: 0x4F09, + 0x98C3: 0x4F5A, + 0x98C4: 0x4F30, + 0x98C5: 0x4F5B, + 0x98C6: 0x4F5D, + 0x98C7: 0x4F57, + 0x98C8: 0x4F47, + 0x98C9: 0x4F76, + 0x98CA: 0x4F88, + 0x98CB: 0x4F8F, + 0x98CC: 0x4F98, + 0x98CD: 0x4F7B, + 0x98CE: 0x4F69, + 0x98CF: 0x4F70, + 0x98D0: 0x4F91, + 0x98D1: 0x4F6F, + 0x98D2: 0x4F86, + 0x98D3: 0x4F96, + 0x98D4: 0x5118, + 0x98D5: 0x4FD4, + 0x98D6: 0x4FDF, + 0x98D7: 0x4FCE, + 0x98D8: 0x4FD8, + 0x98D9: 0x4FDB, + 0x98DA: 0x4FD1, + 0x98DB: 0x4FDA, + 0x98DC: 0x4FD0, + 0x98DD: 0x4FE4, + 0x98DE: 0x4FE5, + 0x98DF: 0x501A, + 0x98E0: 0x5028, + 0x98E1: 0x5014, + 0x98E2: 0x502A, + 0x98E3: 0x5025, + 0x98E4: 0x5005, + 0x98E5: 0x4F1C, + 0x98E6: 0x4FF6, + 0x98E7: 0x5021, + 0x98E8: 0x5029, + 0x98E9: 0x502C, + 0x98EA: 0x4FFE, + 0x98EB: 0x4FEF, + 0x98EC: 0x5011, + 0x98ED: 0x5006, + 0x98EE: 0x5043, + 0x98EF: 0x5047, + 0x98F0: 0x6703, + 0x98F1: 0x5055, + 0x98F2: 0x5050, + 0x98F3: 0x5048, + 0x98F4: 0x505A, + 0x98F5: 0x5056, + 0x98F6: 0x506C, + 0x98F7: 0x5078, + 0x98F8: 0x5080, + 0x98F9: 0x509A, + 0x98FA: 0x5085, + 0x98FB: 0x50B4, + 0x98FC: 0x50B2, + 0x9940: 0x50C9, + 0x9941: 0x50CA, + 0x9942: 0x50B3, + 0x9943: 0x50C2, + 0x9944: 0x50D6, + 0x9945: 0x50DE, + 0x9946: 0x50E5, + 0x9947: 0x50ED, + 0x9948: 0x50E3, + 0x9949: 0x50EE, + 0x994A: 0x50F9, + 0x994B: 0x50F5, + 0x994C: 0x5109, + 0x994D: 0x5101, + 0x994E: 0x5102, + 0x994F: 0x5116, + 0x9950: 0x5115, + 0x9951: 0x5114, + 0x9952: 0x511A, + 0x9953: 0x5121, + 0x9954: 0x513A, + 0x9955: 0x5137, + 0x9956: 0x513C, + 0x9957: 0x513B, + 0x9958: 0x513F, + 0x9959: 0x5140, + 0x995A: 0x5152, + 0x995B: 0x514C, + 0x995C: 0x5154, + 0x995D: 0x5162, + 0x995E: 0x7AF8, + 0x995F: 0x5169, + 0x9960: 0x516A, + 0x9961: 0x516E, + 0x9962: 0x5180, + 0x9963: 0x5182, + 0x9964: 0x56D8, + 0x9965: 0x518C, + 0x9966: 0x5189, + 0x9967: 0x518F, + 0x9968: 0x5191, + 0x9969: 0x5193, + 0x996A: 0x5195, + 0x996B: 0x5196, + 0x996C: 0x51A4, + 0x996D: 0x51A6, + 0x996E: 0x51A2, + 0x996F: 0x51A9, + 0x9970: 0x51AA, + 0x9971: 0x51AB, + 0x9972: 0x51B3, + 0x9973: 0x51B1, + 0x9974: 0x51B2, + 0x9975: 0x51B0, + 0x9976: 0x51B5, + 0x9977: 0x51BD, + 0x9978: 0x51C5, + 0x9979: 0x51C9, + 0x997A: 0x51DB, + 0x997B: 0x51E0, + 0x997C: 0x8655, + 0x997D: 0x51E9, + 0x997E: 0x51ED, + 0x9980: 0x51F0, + 0x9981: 0x51F5, + 0x9982: 0x51FE, + 0x9983: 0x5204, + 0x9984: 0x520B, + 0x9985: 0x5214, + 0x9986: 0x520E, + 0x9987: 0x5227, + 0x9988: 0x522A, + 0x9989: 0x522E, + 0x998A: 0x5233, + 0x998B: 0x5239, + 0x998C: 0x524F, + 0x998D: 0x5244, + 0x998E: 0x524B, + 0x998F: 0x524C, + 0x9990: 0x525E, + 0x9991: 0x5254, + 0x9992: 0x526A, + 0x9993: 0x5274, + 0x9994: 0x5269, + 0x9995: 0x5273, + 0x9996: 0x527F, + 0x9997: 0x527D, + 0x9998: 0x528D, + 0x9999: 0x5294, + 0x999A: 0x5292, + 0x999B: 0x5271, + 0x999C: 0x5288, + 0x999D: 0x5291, + 0x999E: 0x8FA8, + 0x999F: 0x8FA7, + 0x99A0: 0x52AC, + 0x99A1: 0x52AD, + 0x99A2: 0x52BC, + 0x99A3: 0x52B5, + 0x99A4: 0x52C1, + 0x99A5: 0x52CD, + 0x99A6: 0x52D7, + 0x99A7: 0x52DE, + 0x99A8: 0x52E3, + 0x99A9: 0x52E6, + 0x99AA: 0x98ED, + 0x99AB: 0x52E0, + 0x99AC: 0x52F3, + 0x99AD: 0x52F5, + 0x99AE: 0x52F8, + 0x99AF: 0x52F9, + 0x99B0: 0x5306, + 0x99B1: 0x5308, + 0x99B2: 0x7538, + 0x99B3: 0x530D, + 0x99B4: 0x5310, + 0x99B5: 0x530F, + 0x99B6: 0x5315, + 0x99B7: 0x531A, + 0x99B8: 0x5323, + 0x99B9: 0x532F, + 0x99BA: 0x5331, + 0x99BB: 0x5333, + 0x99BC: 0x5338, + 0x99BD: 0x5340, + 0x99BE: 0x5346, + 0x99BF: 0x5345, + 0x99C0: 0x4E17, + 0x99C1: 0x5349, + 0x99C2: 0x534D, + 0x99C3: 0x51D6, + 0x99C4: 0x535E, + 0x99C5: 0x5369, + 0x99C6: 0x536E, + 0x99C7: 0x5918, + 0x99C8: 0x537B, + 0x99C9: 0x5377, + 0x99CA: 0x5382, + 0x99CB: 0x5396, + 0x99CC: 0x53A0, + 0x99CD: 0x53A6, + 0x99CE: 0x53A5, + 0x99CF: 0x53AE, + 0x99D0: 0x53B0, + 0x99D1: 0x53B6, + 0x99D2: 0x53C3, + 0x99D3: 0x7C12, + 0x99D4: 0x96D9, + 0x99D5: 0x53DF, + 0x99D6: 0x66FC, + 0x99D7: 0x71EE, + 0x99D8: 0x53EE, + 0x99D9: 0x53E8, + 0x99DA: 0x53ED, + 0x99DB: 0x53FA, + 0x99DC: 0x5401, + 0x99DD: 0x543D, + 0x99DE: 0x5440, + 0x99DF: 0x542C, + 0x99E0: 0x542D, + 0x99E1: 0x543C, + 0x99E2: 0x542E, + 0x99E3: 0x5436, + 0x99E4: 0x5429, + 0x99E5: 0x541D, + 0x99E6: 0x544E, + 0x99E7: 0x548F, + 0x99E8: 0x5475, + 0x99E9: 0x548E, + 0x99EA: 0x545F, + 0x99EB: 0x5471, + 0x99EC: 0x5477, + 0x99ED: 0x5470, + 0x99EE: 0x5492, + 0x99EF: 0x547B, + 0x99F0: 0x5480, + 0x99F1: 0x5476, + 0x99F2: 0x5484, + 0x99F3: 0x5490, + 0x99F4: 0x5486, + 0x99F5: 0x54C7, + 0x99F6: 0x54A2, + 0x99F7: 0x54B8, + 0x99F8: 0x54A5, + 0x99F9: 0x54AC, + 0x99FA: 0x54C4, + 0x99FB: 0x54C8, + 0x99FC: 0x54A8, + 0x9A40: 0x54AB, + 0x9A41: 0x54C2, + 0x9A42: 0x54A4, + 0x9A43: 0x54BE, + 0x9A44: 0x54BC, + 0x9A45: 0x54D8, + 0x9A46: 0x54E5, + 0x9A47: 0x54E6, + 0x9A48: 0x550F, + 0x9A49: 0x5514, + 0x9A4A: 0x54FD, + 0x9A4B: 0x54EE, + 0x9A4C: 0x54ED, + 0x9A4D: 0x54FA, + 0x9A4E: 0x54E2, + 0x9A4F: 0x5539, + 0x9A50: 0x5540, + 0x9A51: 0x5563, + 0x9A52: 0x554C, + 0x9A53: 0x552E, + 0x9A54: 0x555C, + 0x9A55: 0x5545, + 0x9A56: 0x5556, + 0x9A57: 0x5557, + 0x9A58: 0x5538, + 0x9A59: 0x5533, + 0x9A5A: 0x555D, + 0x9A5B: 0x5599, + 0x9A5C: 0x5580, + 0x9A5D: 0x54AF, + 0x9A5E: 0x558A, + 0x9A5F: 0x559F, + 0x9A60: 0x557B, + 0x9A61: 0x557E, + 0x9A62: 0x5598, + 0x9A63: 0x559E, + 0x9A64: 0x55AE, + 0x9A65: 0x557C, + 0x9A66: 0x5583, + 0x9A67: 0x55A9, + 0x9A68: 0x5587, + 0x9A69: 0x55A8, + 0x9A6A: 0x55DA, + 0x9A6B: 0x55C5, + 0x9A6C: 0x55DF, + 0x9A6D: 0x55C4, + 0x9A6E: 0x55DC, + 0x9A6F: 0x55E4, + 0x9A70: 0x55D4, + 0x9A71: 0x5614, + 0x9A72: 0x55F7, + 0x9A73: 0x5616, + 0x9A74: 0x55FE, + 0x9A75: 0x55FD, + 0x9A76: 0x561B, + 0x9A77: 0x55F9, + 0x9A78: 0x564E, + 0x9A79: 0x5650, + 0x9A7A: 0x71DF, + 0x9A7B: 0x5634, + 0x9A7C: 0x5636, + 0x9A7D: 0x5632, + 0x9A7E: 0x5638, + 0x9A80: 0x566B, + 0x9A81: 0x5664, + 0x9A82: 0x562F, + 0x9A83: 0x566C, + 0x9A84: 0x566A, + 0x9A85: 0x5686, + 0x9A86: 0x5680, + 0x9A87: 0x568A, + 0x9A88: 0x56A0, + 0x9A89: 0x5694, + 0x9A8A: 0x568F, + 0x9A8B: 0x56A5, + 0x9A8C: 0x56AE, + 0x9A8D: 0x56B6, + 0x9A8E: 0x56B4, + 0x9A8F: 0x56C2, + 0x9A90: 0x56BC, + 0x9A91: 0x56C1, + 0x9A92: 0x56C3, + 0x9A93: 0x56C0, + 0x9A94: 0x56C8, + 0x9A95: 0x56CE, + 0x9A96: 0x56D1, + 0x9A97: 0x56D3, + 0x9A98: 0x56D7, + 0x9A99: 0x56EE, + 0x9A9A: 0x56F9, + 0x9A9B: 0x5700, + 0x9A9C: 0x56FF, + 0x9A9D: 0x5704, + 0x9A9E: 0x5709, + 0x9A9F: 0x5708, + 0x9AA0: 0x570B, + 0x9AA1: 0x570D, + 0x9AA2: 0x5713, + 0x9AA3: 0x5718, + 0x9AA4: 0x5716, + 0x9AA5: 0x55C7, + 0x9AA6: 0x571C, + 0x9AA7: 0x5726, + 0x9AA8: 0x5737, + 0x9AA9: 0x5738, + 0x9AAA: 0x574E, + 0x9AAB: 0x573B, + 0x9AAC: 0x5740, + 0x9AAD: 0x574F, + 0x9AAE: 0x5769, + 0x9AAF: 0x57C0, + 0x9AB0: 0x5788, + 0x9AB1: 0x5761, + 0x9AB2: 0x577F, + 0x9AB3: 0x5789, + 0x9AB4: 0x5793, + 0x9AB5: 0x57A0, + 0x9AB6: 0x57B3, + 0x9AB7: 0x57A4, + 0x9AB8: 0x57AA, + 0x9AB9: 0x57B0, + 0x9ABA: 0x57C3, + 0x9ABB: 0x57C6, + 0x9ABC: 0x57D4, + 0x9ABD: 0x57D2, + 0x9ABE: 0x57D3, + 0x9ABF: 0x580A, + 0x9AC0: 0x57D6, + 0x9AC1: 0x57E3, + 0x9AC2: 0x580B, + 0x9AC3: 0x5819, + 0x9AC4: 0x581D, + 0x9AC5: 0x5872, + 0x9AC6: 0x5821, + 0x9AC7: 0x5862, + 0x9AC8: 0x584B, + 0x9AC9: 0x5870, + 0x9ACA: 0x6BC0, + 0x9ACB: 0x5852, + 0x9ACC: 0x583D, + 0x9ACD: 0x5879, + 0x9ACE: 0x5885, + 0x9ACF: 0x58B9, + 0x9AD0: 0x589F, + 0x9AD1: 0x58AB, + 0x9AD2: 0x58BA, + 0x9AD3: 0x58DE, + 0x9AD4: 0x58BB, + 0x9AD5: 0x58B8, + 0x9AD6: 0x58AE, + 0x9AD7: 0x58C5, + 0x9AD8: 0x58D3, + 0x9AD9: 0x58D1, + 0x9ADA: 0x58D7, + 0x9ADB: 0x58D9, + 0x9ADC: 0x58D8, + 0x9ADD: 0x58E5, + 0x9ADE: 0x58DC, + 0x9ADF: 0x58E4, + 0x9AE0: 0x58DF, + 0x9AE1: 0x58EF, + 0x9AE2: 0x58FA, + 0x9AE3: 0x58F9, + 0x9AE4: 0x58FB, + 0x9AE5: 0x58FC, + 0x9AE6: 0x58FD, + 0x9AE7: 0x5902, + 0x9AE8: 0x590A, + 0x9AE9: 0x5910, + 0x9AEA: 0x591B, + 0x9AEB: 0x68A6, + 0x9AEC: 0x5925, + 0x9AED: 0x592C, + 0x9AEE: 0x592D, + 0x9AEF: 0x5932, + 0x9AF0: 0x5938, + 0x9AF1: 0x593E, + 0x9AF2: 0x7AD2, + 0x9AF3: 0x5955, + 0x9AF4: 0x5950, + 0x9AF5: 0x594E, + 0x9AF6: 0x595A, + 0x9AF7: 0x5958, + 0x9AF8: 0x5962, + 0x9AF9: 0x5960, + 0x9AFA: 0x5967, + 0x9AFB: 0x596C, + 0x9AFC: 0x5969, + 0x9B40: 0x5978, + 0x9B41: 0x5981, + 0x9B42: 0x599D, + 0x9B43: 0x4F5E, + 0x9B44: 0x4FAB, + 0x9B45: 0x59A3, + 0x9B46: 0x59B2, + 0x9B47: 0x59C6, + 0x9B48: 0x59E8, + 0x9B49: 0x59DC, + 0x9B4A: 0x598D, + 0x9B4B: 0x59D9, + 0x9B4C: 0x59DA, + 0x9B4D: 0x5A25, + 0x9B4E: 0x5A1F, + 0x9B4F: 0x5A11, + 0x9B50: 0x5A1C, + 0x9B51: 0x5A09, + 0x9B52: 0x5A1A, + 0x9B53: 0x5A40, + 0x9B54: 0x5A6C, + 0x9B55: 0x5A49, + 0x9B56: 0x5A35, + 0x9B57: 0x5A36, + 0x9B58: 0x5A62, + 0x9B59: 0x5A6A, + 0x9B5A: 0x5A9A, + 0x9B5B: 0x5ABC, + 0x9B5C: 0x5ABE, + 0x9B5D: 0x5ACB, + 0x9B5E: 0x5AC2, + 0x9B5F: 0x5ABD, + 0x9B60: 0x5AE3, + 0x9B61: 0x5AD7, + 0x9B62: 0x5AE6, + 0x9B63: 0x5AE9, + 0x9B64: 0x5AD6, + 0x9B65: 0x5AFA, + 0x9B66: 0x5AFB, + 0x9B67: 0x5B0C, + 0x9B68: 0x5B0B, + 0x9B69: 0x5B16, + 0x9B6A: 0x5B32, + 0x9B6B: 0x5AD0, + 0x9B6C: 0x5B2A, + 0x9B6D: 0x5B36, + 0x9B6E: 0x5B3E, + 0x9B6F: 0x5B43, + 0x9B70: 0x5B45, + 0x9B71: 0x5B40, + 0x9B72: 0x5B51, + 0x9B73: 0x5B55, + 0x9B74: 0x5B5A, + 0x9B75: 0x5B5B, + 0x9B76: 0x5B65, + 0x9B77: 0x5B69, + 0x9B78: 0x5B70, + 0x9B79: 0x5B73, + 0x9B7A: 0x5B75, + 0x9B7B: 0x5B78, + 0x9B7C: 0x6588, + 0x9B7D: 0x5B7A, + 0x9B7E: 0x5B80, + 0x9B80: 0x5B83, + 0x9B81: 0x5BA6, + 0x9B82: 0x5BB8, + 0x9B83: 0x5BC3, + 0x9B84: 0x5BC7, + 0x9B85: 0x5BC9, + 0x9B86: 0x5BD4, + 0x9B87: 0x5BD0, + 0x9B88: 0x5BE4, + 0x9B89: 0x5BE6, + 0x9B8A: 0x5BE2, + 0x9B8B: 0x5BDE, + 0x9B8C: 0x5BE5, + 0x9B8D: 0x5BEB, + 0x9B8E: 0x5BF0, + 0x9B8F: 0x5BF6, + 0x9B90: 0x5BF3, + 0x9B91: 0x5C05, + 0x9B92: 0x5C07, + 0x9B93: 0x5C08, + 0x9B94: 0x5C0D, + 0x9B95: 0x5C13, + 0x9B96: 0x5C20, + 0x9B97: 0x5C22, + 0x9B98: 0x5C28, + 0x9B99: 0x5C38, + 0x9B9A: 0x5C39, + 0x9B9B: 0x5C41, + 0x9B9C: 0x5C46, + 0x9B9D: 0x5C4E, + 0x9B9E: 0x5C53, + 0x9B9F: 0x5C50, + 0x9BA0: 0x5C4F, + 0x9BA1: 0x5B71, + 0x9BA2: 0x5C6C, + 0x9BA3: 0x5C6E, + 0x9BA4: 0x4E62, + 0x9BA5: 0x5C76, + 0x9BA6: 0x5C79, + 0x9BA7: 0x5C8C, + 0x9BA8: 0x5C91, + 0x9BA9: 0x5C94, + 0x9BAA: 0x599B, + 0x9BAB: 0x5CAB, + 0x9BAC: 0x5CBB, + 0x9BAD: 0x5CB6, + 0x9BAE: 0x5CBC, + 0x9BAF: 0x5CB7, + 0x9BB0: 0x5CC5, + 0x9BB1: 0x5CBE, + 0x9BB2: 0x5CC7, + 0x9BB3: 0x5CD9, + 0x9BB4: 0x5CE9, + 0x9BB5: 0x5CFD, + 0x9BB6: 0x5CFA, + 0x9BB7: 0x5CED, + 0x9BB8: 0x5D8C, + 0x9BB9: 0x5CEA, + 0x9BBA: 0x5D0B, + 0x9BBB: 0x5D15, + 0x9BBC: 0x5D17, + 0x9BBD: 0x5D5C, + 0x9BBE: 0x5D1F, + 0x9BBF: 0x5D1B, + 0x9BC0: 0x5D11, + 0x9BC1: 0x5D14, + 0x9BC2: 0x5D22, + 0x9BC3: 0x5D1A, + 0x9BC4: 0x5D19, + 0x9BC5: 0x5D18, + 0x9BC6: 0x5D4C, + 0x9BC7: 0x5D52, + 0x9BC8: 0x5D4E, + 0x9BC9: 0x5D4B, + 0x9BCA: 0x5D6C, + 0x9BCB: 0x5D73, + 0x9BCC: 0x5D76, + 0x9BCD: 0x5D87, + 0x9BCE: 0x5D84, + 0x9BCF: 0x5D82, + 0x9BD0: 0x5DA2, + 0x9BD1: 0x5D9D, + 0x9BD2: 0x5DAC, + 0x9BD3: 0x5DAE, + 0x9BD4: 0x5DBD, + 0x9BD5: 0x5D90, + 0x9BD6: 0x5DB7, + 0x9BD7: 0x5DBC, + 0x9BD8: 0x5DC9, + 0x9BD9: 0x5DCD, + 0x9BDA: 0x5DD3, + 0x9BDB: 0x5DD2, + 0x9BDC: 0x5DD6, + 0x9BDD: 0x5DDB, + 0x9BDE: 0x5DEB, + 0x9BDF: 0x5DF2, + 0x9BE0: 0x5DF5, + 0x9BE1: 0x5E0B, + 0x9BE2: 0x5E1A, + 0x9BE3: 0x5E19, + 0x9BE4: 0x5E11, + 0x9BE5: 0x5E1B, + 0x9BE6: 0x5E36, + 0x9BE7: 0x5E37, + 0x9BE8: 0x5E44, + 0x9BE9: 0x5E43, + 0x9BEA: 0x5E40, + 0x9BEB: 0x5E4E, + 0x9BEC: 0x5E57, + 0x9BED: 0x5E54, + 0x9BEE: 0x5E5F, + 0x9BEF: 0x5E62, + 0x9BF0: 0x5E64, + 0x9BF1: 0x5E47, + 0x9BF2: 0x5E75, + 0x9BF3: 0x5E76, + 0x9BF4: 0x5E7A, + 0x9BF5: 0x9EBC, + 0x9BF6: 0x5E7F, + 0x9BF7: 0x5EA0, + 0x9BF8: 0x5EC1, + 0x9BF9: 0x5EC2, + 0x9BFA: 0x5EC8, + 0x9BFB: 0x5ED0, + 0x9BFC: 0x5ECF, + 0x9C40: 0x5ED6, + 0x9C41: 0x5EE3, + 0x9C42: 0x5EDD, + 0x9C43: 0x5EDA, + 0x9C44: 0x5EDB, + 0x9C45: 0x5EE2, + 0x9C46: 0x5EE1, + 0x9C47: 0x5EE8, + 0x9C48: 0x5EE9, + 0x9C49: 0x5EEC, + 0x9C4A: 0x5EF1, + 0x9C4B: 0x5EF3, + 0x9C4C: 0x5EF0, + 0x9C4D: 0x5EF4, + 0x9C4E: 0x5EF8, + 0x9C4F: 0x5EFE, + 0x9C50: 0x5F03, + 0x9C51: 0x5F09, + 0x9C52: 0x5F5D, + 0x9C53: 0x5F5C, + 0x9C54: 0x5F0B, + 0x9C55: 0x5F11, + 0x9C56: 0x5F16, + 0x9C57: 0x5F29, + 0x9C58: 0x5F2D, + 0x9C59: 0x5F38, + 0x9C5A: 0x5F41, + 0x9C5B: 0x5F48, + 0x9C5C: 0x5F4C, + 0x9C5D: 0x5F4E, + 0x9C5E: 0x5F2F, + 0x9C5F: 0x5F51, + 0x9C60: 0x5F56, + 0x9C61: 0x5F57, + 0x9C62: 0x5F59, + 0x9C63: 0x5F61, + 0x9C64: 0x5F6D, + 0x9C65: 0x5F73, + 0x9C66: 0x5F77, + 0x9C67: 0x5F83, + 0x9C68: 0x5F82, + 0x9C69: 0x5F7F, + 0x9C6A: 0x5F8A, + 0x9C6B: 0x5F88, + 0x9C6C: 0x5F91, + 0x9C6D: 0x5F87, + 0x9C6E: 0x5F9E, + 0x9C6F: 0x5F99, + 0x9C70: 0x5F98, + 0x9C71: 0x5FA0, + 0x9C72: 0x5FA8, + 0x9C73: 0x5FAD, + 0x9C74: 0x5FBC, + 0x9C75: 0x5FD6, + 0x9C76: 0x5FFB, + 0x9C77: 0x5FE4, + 0x9C78: 0x5FF8, + 0x9C79: 0x5FF1, + 0x9C7A: 0x5FDD, + 0x9C7B: 0x60B3, + 0x9C7C: 0x5FFF, + 0x9C7D: 0x6021, + 0x9C7E: 0x6060, + 0x9C80: 0x6019, + 0x9C81: 0x6010, + 0x9C82: 0x6029, + 0x9C83: 0x600E, + 0x9C84: 0x6031, + 0x9C85: 0x601B, + 0x9C86: 0x6015, + 0x9C87: 0x602B, + 0x9C88: 0x6026, + 0x9C89: 0x600F, + 0x9C8A: 0x603A, + 0x9C8B: 0x605A, + 0x9C8C: 0x6041, + 0x9C8D: 0x606A, + 0x9C8E: 0x6077, + 0x9C8F: 0x605F, + 0x9C90: 0x604A, + 0x9C91: 0x6046, + 0x9C92: 0x604D, + 0x9C93: 0x6063, + 0x9C94: 0x6043, + 0x9C95: 0x6064, + 0x9C96: 0x6042, + 0x9C97: 0x606C, + 0x9C98: 0x606B, + 0x9C99: 0x6059, + 0x9C9A: 0x6081, + 0x9C9B: 0x608D, + 0x9C9C: 0x60E7, + 0x9C9D: 0x6083, + 0x9C9E: 0x609A, + 0x9C9F: 0x6084, + 0x9CA0: 0x609B, + 0x9CA1: 0x6096, + 0x9CA2: 0x6097, + 0x9CA3: 0x6092, + 0x9CA4: 0x60A7, + 0x9CA5: 0x608B, + 0x9CA6: 0x60E1, + 0x9CA7: 0x60B8, + 0x9CA8: 0x60E0, + 0x9CA9: 0x60D3, + 0x9CAA: 0x60B4, + 0x9CAB: 0x5FF0, + 0x9CAC: 0x60BD, + 0x9CAD: 0x60C6, + 0x9CAE: 0x60B5, + 0x9CAF: 0x60D8, + 0x9CB0: 0x614D, + 0x9CB1: 0x6115, + 0x9CB2: 0x6106, + 0x9CB3: 0x60F6, + 0x9CB4: 0x60F7, + 0x9CB5: 0x6100, + 0x9CB6: 0x60F4, + 0x9CB7: 0x60FA, + 0x9CB8: 0x6103, + 0x9CB9: 0x6121, + 0x9CBA: 0x60FB, + 0x9CBB: 0x60F1, + 0x9CBC: 0x610D, + 0x9CBD: 0x610E, + 0x9CBE: 0x6147, + 0x9CBF: 0x613E, + 0x9CC0: 0x6128, + 0x9CC1: 0x6127, + 0x9CC2: 0x614A, + 0x9CC3: 0x613F, + 0x9CC4: 0x613C, + 0x9CC5: 0x612C, + 0x9CC6: 0x6134, + 0x9CC7: 0x613D, + 0x9CC8: 0x6142, + 0x9CC9: 0x6144, + 0x9CCA: 0x6173, + 0x9CCB: 0x6177, + 0x9CCC: 0x6158, + 0x9CCD: 0x6159, + 0x9CCE: 0x615A, + 0x9CCF: 0x616B, + 0x9CD0: 0x6174, + 0x9CD1: 0x616F, + 0x9CD2: 0x6165, + 0x9CD3: 0x6171, + 0x9CD4: 0x615F, + 0x9CD5: 0x615D, + 0x9CD6: 0x6153, + 0x9CD7: 0x6175, + 0x9CD8: 0x6199, + 0x9CD9: 0x6196, + 0x9CDA: 0x6187, + 0x9CDB: 0x61AC, + 0x9CDC: 0x6194, + 0x9CDD: 0x619A, + 0x9CDE: 0x618A, + 0x9CDF: 0x6191, + 0x9CE0: 0x61AB, + 0x9CE1: 0x61AE, + 0x9CE2: 0x61CC, + 0x9CE3: 0x61CA, + 0x9CE4: 0x61C9, + 0x9CE5: 0x61F7, + 0x9CE6: 0x61C8, + 0x9CE7: 0x61C3, + 0x9CE8: 0x61C6, + 0x9CE9: 0x61BA, + 0x9CEA: 0x61CB, + 0x9CEB: 0x7F79, + 0x9CEC: 0x61CD, + 0x9CED: 0x61E6, + 0x9CEE: 0x61E3, + 0x9CEF: 0x61F6, + 0x9CF0: 0x61FA, + 0x9CF1: 0x61F4, + 0x9CF2: 0x61FF, + 0x9CF3: 0x61FD, + 0x9CF4: 0x61FC, + 0x9CF5: 0x61FE, + 0x9CF6: 0x6200, + 0x9CF7: 0x6208, + 0x9CF8: 0x6209, + 0x9CF9: 0x620D, + 0x9CFA: 0x620C, + 0x9CFB: 0x6214, + 0x9CFC: 0x621B, + 0x9D40: 0x621E, + 0x9D41: 0x6221, + 0x9D42: 0x622A, + 0x9D43: 0x622E, + 0x9D44: 0x6230, + 0x9D45: 0x6232, + 0x9D46: 0x6233, + 0x9D47: 0x6241, + 0x9D48: 0x624E, + 0x9D49: 0x625E, + 0x9D4A: 0x6263, + 0x9D4B: 0x625B, + 0x9D4C: 0x6260, + 0x9D4D: 0x6268, + 0x9D4E: 0x627C, + 0x9D4F: 0x6282, + 0x9D50: 0x6289, + 0x9D51: 0x627E, + 0x9D52: 0x6292, + 0x9D53: 0x6293, + 0x9D54: 0x6296, + 0x9D55: 0x62D4, + 0x9D56: 0x6283, + 0x9D57: 0x6294, + 0x9D58: 0x62D7, + 0x9D59: 0x62D1, + 0x9D5A: 0x62BB, + 0x9D5B: 0x62CF, + 0x9D5C: 0x62FF, + 0x9D5D: 0x62C6, + 0x9D5E: 0x64D4, + 0x9D5F: 0x62C8, + 0x9D60: 0x62DC, + 0x9D61: 0x62CC, + 0x9D62: 0x62CA, + 0x9D63: 0x62C2, + 0x9D64: 0x62C7, + 0x9D65: 0x629B, + 0x9D66: 0x62C9, + 0x9D67: 0x630C, + 0x9D68: 0x62EE, + 0x9D69: 0x62F1, + 0x9D6A: 0x6327, + 0x9D6B: 0x6302, + 0x9D6C: 0x6308, + 0x9D6D: 0x62EF, + 0x9D6E: 0x62F5, + 0x9D6F: 0x6350, + 0x9D70: 0x633E, + 0x9D71: 0x634D, + 0x9D72: 0x641C, + 0x9D73: 0x634F, + 0x9D74: 0x6396, + 0x9D75: 0x638E, + 0x9D76: 0x6380, + 0x9D77: 0x63AB, + 0x9D78: 0x6376, + 0x9D79: 0x63A3, + 0x9D7A: 0x638F, + 0x9D7B: 0x6389, + 0x9D7C: 0x639F, + 0x9D7D: 0x63B5, + 0x9D7E: 0x636B, + 0x9D80: 0x6369, + 0x9D81: 0x63BE, + 0x9D82: 0x63E9, + 0x9D83: 0x63C0, + 0x9D84: 0x63C6, + 0x9D85: 0x63E3, + 0x9D86: 0x63C9, + 0x9D87: 0x63D2, + 0x9D88: 0x63F6, + 0x9D89: 0x63C4, + 0x9D8A: 0x6416, + 0x9D8B: 0x6434, + 0x9D8C: 0x6406, + 0x9D8D: 0x6413, + 0x9D8E: 0x6426, + 0x9D8F: 0x6436, + 0x9D90: 0x651D, + 0x9D91: 0x6417, + 0x9D92: 0x6428, + 0x9D93: 0x640F, + 0x9D94: 0x6467, + 0x9D95: 0x646F, + 0x9D96: 0x6476, + 0x9D97: 0x644E, + 0x9D98: 0x652A, + 0x9D99: 0x6495, + 0x9D9A: 0x6493, + 0x9D9B: 0x64A5, + 0x9D9C: 0x64A9, + 0x9D9D: 0x6488, + 0x9D9E: 0x64BC, + 0x9D9F: 0x64DA, + 0x9DA0: 0x64D2, + 0x9DA1: 0x64C5, + 0x9DA2: 0x64C7, + 0x9DA3: 0x64BB, + 0x9DA4: 0x64D8, + 0x9DA5: 0x64C2, + 0x9DA6: 0x64F1, + 0x9DA7: 0x64E7, + 0x9DA8: 0x8209, + 0x9DA9: 0x64E0, + 0x9DAA: 0x64E1, + 0x9DAB: 0x62AC, + 0x9DAC: 0x64E3, + 0x9DAD: 0x64EF, + 0x9DAE: 0x652C, + 0x9DAF: 0x64F6, + 0x9DB0: 0x64F4, + 0x9DB1: 0x64F2, + 0x9DB2: 0x64FA, + 0x9DB3: 0x6500, + 0x9DB4: 0x64FD, + 0x9DB5: 0x6518, + 0x9DB6: 0x651C, + 0x9DB7: 0x6505, + 0x9DB8: 0x6524, + 0x9DB9: 0x6523, + 0x9DBA: 0x652B, + 0x9DBB: 0x6534, + 0x9DBC: 0x6535, + 0x9DBD: 0x6537, + 0x9DBE: 0x6536, + 0x9DBF: 0x6538, + 0x9DC0: 0x754B, + 0x9DC1: 0x6548, + 0x9DC2: 0x6556, + 0x9DC3: 0x6555, + 0x9DC4: 0x654D, + 0x9DC5: 0x6558, + 0x9DC6: 0x655E, + 0x9DC7: 0x655D, + 0x9DC8: 0x6572, + 0x9DC9: 0x6578, + 0x9DCA: 0x6582, + 0x9DCB: 0x6583, + 0x9DCC: 0x8B8A, + 0x9DCD: 0x659B, + 0x9DCE: 0x659F, + 0x9DCF: 0x65AB, + 0x9DD0: 0x65B7, + 0x9DD1: 0x65C3, + 0x9DD2: 0x65C6, + 0x9DD3: 0x65C1, + 0x9DD4: 0x65C4, + 0x9DD5: 0x65CC, + 0x9DD6: 0x65D2, + 0x9DD7: 0x65DB, + 0x9DD8: 0x65D9, + 0x9DD9: 0x65E0, + 0x9DDA: 0x65E1, + 0x9DDB: 0x65F1, + 0x9DDC: 0x6772, + 0x9DDD: 0x660A, + 0x9DDE: 0x6603, + 0x9DDF: 0x65FB, + 0x9DE0: 0x6773, + 0x9DE1: 0x6635, + 0x9DE2: 0x6636, + 0x9DE3: 0x6634, + 0x9DE4: 0x661C, + 0x9DE5: 0x664F, + 0x9DE6: 0x6644, + 0x9DE7: 0x6649, + 0x9DE8: 0x6641, + 0x9DE9: 0x665E, + 0x9DEA: 0x665D, + 0x9DEB: 0x6664, + 0x9DEC: 0x6667, + 0x9DED: 0x6668, + 0x9DEE: 0x665F, + 0x9DEF: 0x6662, + 0x9DF0: 0x6670, + 0x9DF1: 0x6683, + 0x9DF2: 0x6688, + 0x9DF3: 0x668E, + 0x9DF4: 0x6689, + 0x9DF5: 0x6684, + 0x9DF6: 0x6698, + 0x9DF7: 0x669D, + 0x9DF8: 0x66C1, + 0x9DF9: 0x66B9, + 0x9DFA: 0x66C9, + 0x9DFB: 0x66BE, + 0x9DFC: 0x66BC, + 0x9E40: 0x66C4, + 0x9E41: 0x66B8, + 0x9E42: 0x66D6, + 0x9E43: 0x66DA, + 0x9E44: 0x66E0, + 0x9E45: 0x663F, + 0x9E46: 0x66E6, + 0x9E47: 0x66E9, + 0x9E48: 0x66F0, + 0x9E49: 0x66F5, + 0x9E4A: 0x66F7, + 0x9E4B: 0x670F, + 0x9E4C: 0x6716, + 0x9E4D: 0x671E, + 0x9E4E: 0x6726, + 0x9E4F: 0x6727, + 0x9E50: 0x9738, + 0x9E51: 0x672E, + 0x9E52: 0x673F, + 0x9E53: 0x6736, + 0x9E54: 0x6741, + 0x9E55: 0x6738, + 0x9E56: 0x6737, + 0x9E57: 0x6746, + 0x9E58: 0x675E, + 0x9E59: 0x6760, + 0x9E5A: 0x6759, + 0x9E5B: 0x6763, + 0x9E5C: 0x6764, + 0x9E5D: 0x6789, + 0x9E5E: 0x6770, + 0x9E5F: 0x67A9, + 0x9E60: 0x677C, + 0x9E61: 0x676A, + 0x9E62: 0x678C, + 0x9E63: 0x678B, + 0x9E64: 0x67A6, + 0x9E65: 0x67A1, + 0x9E66: 0x6785, + 0x9E67: 0x67B7, + 0x9E68: 0x67EF, + 0x9E69: 0x67B4, + 0x9E6A: 0x67EC, + 0x9E6B: 0x67B3, + 0x9E6C: 0x67E9, + 0x9E6D: 0x67B8, + 0x9E6E: 0x67E4, + 0x9E6F: 0x67DE, + 0x9E70: 0x67DD, + 0x9E71: 0x67E2, + 0x9E72: 0x67EE, + 0x9E73: 0x67B9, + 0x9E74: 0x67CE, + 0x9E75: 0x67C6, + 0x9E76: 0x67E7, + 0x9E77: 0x6A9C, + 0x9E78: 0x681E, + 0x9E79: 0x6846, + 0x9E7A: 0x6829, + 0x9E7B: 0x6840, + 0x9E7C: 0x684D, + 0x9E7D: 0x6832, + 0x9E7E: 0x684E, + 0x9E80: 0x68B3, + 0x9E81: 0x682B, + 0x9E82: 0x6859, + 0x9E83: 0x6863, + 0x9E84: 0x6877, + 0x9E85: 0x687F, + 0x9E86: 0x689F, + 0x9E87: 0x688F, + 0x9E88: 0x68AD, + 0x9E89: 0x6894, + 0x9E8A: 0x689D, + 0x9E8B: 0x689B, + 0x9E8C: 0x6883, + 0x9E8D: 0x6AAE, + 0x9E8E: 0x68B9, + 0x9E8F: 0x6874, + 0x9E90: 0x68B5, + 0x9E91: 0x68A0, + 0x9E92: 0x68BA, + 0x9E93: 0x690F, + 0x9E94: 0x688D, + 0x9E95: 0x687E, + 0x9E96: 0x6901, + 0x9E97: 0x68CA, + 0x9E98: 0x6908, + 0x9E99: 0x68D8, + 0x9E9A: 0x6922, + 0x9E9B: 0x6926, + 0x9E9C: 0x68E1, + 0x9E9D: 0x690C, + 0x9E9E: 0x68CD, + 0x9E9F: 0x68D4, + 0x9EA0: 0x68E7, + 0x9EA1: 0x68D5, + 0x9EA2: 0x6936, + 0x9EA3: 0x6912, + 0x9EA4: 0x6904, + 0x9EA5: 0x68D7, + 0x9EA6: 0x68E3, + 0x9EA7: 0x6925, + 0x9EA8: 0x68F9, + 0x9EA9: 0x68E0, + 0x9EAA: 0x68EF, + 0x9EAB: 0x6928, + 0x9EAC: 0x692A, + 0x9EAD: 0x691A, + 0x9EAE: 0x6923, + 0x9EAF: 0x6921, + 0x9EB0: 0x68C6, + 0x9EB1: 0x6979, + 0x9EB2: 0x6977, + 0x9EB3: 0x695C, + 0x9EB4: 0x6978, + 0x9EB5: 0x696B, + 0x9EB6: 0x6954, + 0x9EB7: 0x697E, + 0x9EB8: 0x696E, + 0x9EB9: 0x6939, + 0x9EBA: 0x6974, + 0x9EBB: 0x693D, + 0x9EBC: 0x6959, + 0x9EBD: 0x6930, + 0x9EBE: 0x6961, + 0x9EBF: 0x695E, + 0x9EC0: 0x695D, + 0x9EC1: 0x6981, + 0x9EC2: 0x696A, + 0x9EC3: 0x69B2, + 0x9EC4: 0x69AE, + 0x9EC5: 0x69D0, + 0x9EC6: 0x69BF, + 0x9EC7: 0x69C1, + 0x9EC8: 0x69D3, + 0x9EC9: 0x69BE, + 0x9ECA: 0x69CE, + 0x9ECB: 0x5BE8, + 0x9ECC: 0x69CA, + 0x9ECD: 0x69DD, + 0x9ECE: 0x69BB, + 0x9ECF: 0x69C3, + 0x9ED0: 0x69A7, + 0x9ED1: 0x6A2E, + 0x9ED2: 0x6991, + 0x9ED3: 0x69A0, + 0x9ED4: 0x699C, + 0x9ED5: 0x6995, + 0x9ED6: 0x69B4, + 0x9ED7: 0x69DE, + 0x9ED8: 0x69E8, + 0x9ED9: 0x6A02, + 0x9EDA: 0x6A1B, + 0x9EDB: 0x69FF, + 0x9EDC: 0x6B0A, + 0x9EDD: 0x69F9, + 0x9EDE: 0x69F2, + 0x9EDF: 0x69E7, + 0x9EE0: 0x6A05, + 0x9EE1: 0x69B1, + 0x9EE2: 0x6A1E, + 0x9EE3: 0x69ED, + 0x9EE4: 0x6A14, + 0x9EE5: 0x69EB, + 0x9EE6: 0x6A0A, + 0x9EE7: 0x6A12, + 0x9EE8: 0x6AC1, + 0x9EE9: 0x6A23, + 0x9EEA: 0x6A13, + 0x9EEB: 0x6A44, + 0x9EEC: 0x6A0C, + 0x9EED: 0x6A72, + 0x9EEE: 0x6A36, + 0x9EEF: 0x6A78, + 0x9EF0: 0x6A47, + 0x9EF1: 0x6A62, + 0x9EF2: 0x6A59, + 0x9EF3: 0x6A66, + 0x9EF4: 0x6A48, + 0x9EF5: 0x6A38, + 0x9EF6: 0x6A22, + 0x9EF7: 0x6A90, + 0x9EF8: 0x6A8D, + 0x9EF9: 0x6AA0, + 0x9EFA: 0x6A84, + 0x9EFB: 0x6AA2, + 0x9EFC: 0x6AA3, + 0x9F40: 0x6A97, + 0x9F41: 0x8617, + 0x9F42: 0x6ABB, + 0x9F43: 0x6AC3, + 0x9F44: 0x6AC2, + 0x9F45: 0x6AB8, + 0x9F46: 0x6AB3, + 0x9F47: 0x6AAC, + 0x9F48: 0x6ADE, + 0x9F49: 0x6AD1, + 0x9F4A: 0x6ADF, + 0x9F4B: 0x6AAA, + 0x9F4C: 0x6ADA, + 0x9F4D: 0x6AEA, + 0x9F4E: 0x6AFB, + 0x9F4F: 0x6B05, + 0x9F50: 0x8616, + 0x9F51: 0x6AFA, + 0x9F52: 0x6B12, + 0x9F53: 0x6B16, + 0x9F54: 0x9B31, + 0x9F55: 0x6B1F, + 0x9F56: 0x6B38, + 0x9F57: 0x6B37, + 0x9F58: 0x76DC, + 0x9F59: 0x6B39, + 0x9F5A: 0x98EE, + 0x9F5B: 0x6B47, + 0x9F5C: 0x6B43, + 0x9F5D: 0x6B49, + 0x9F5E: 0x6B50, + 0x9F5F: 0x6B59, + 0x9F60: 0x6B54, + 0x9F61: 0x6B5B, + 0x9F62: 0x6B5F, + 0x9F63: 0x6B61, + 0x9F64: 0x6B78, + 0x9F65: 0x6B79, + 0x9F66: 0x6B7F, + 0x9F67: 0x6B80, + 0x9F68: 0x6B84, + 0x9F69: 0x6B83, + 0x9F6A: 0x6B8D, + 0x9F6B: 0x6B98, + 0x9F6C: 0x6B95, + 0x9F6D: 0x6B9E, + 0x9F6E: 0x6BA4, + 0x9F6F: 0x6BAA, + 0x9F70: 0x6BAB, + 0x9F71: 0x6BAF, + 0x9F72: 0x6BB2, + 0x9F73: 0x6BB1, + 0x9F74: 0x6BB3, + 0x9F75: 0x6BB7, + 0x9F76: 0x6BBC, + 0x9F77: 0x6BC6, + 0x9F78: 0x6BCB, + 0x9F79: 0x6BD3, + 0x9F7A: 0x6BDF, + 0x9F7B: 0x6BEC, + 0x9F7C: 0x6BEB, + 0x9F7D: 0x6BF3, + 0x9F7E: 0x6BEF, + 0x9F80: 0x9EBE, + 0x9F81: 0x6C08, + 0x9F82: 0x6C13, + 0x9F83: 0x6C14, + 0x9F84: 0x6C1B, + 0x9F85: 0x6C24, + 0x9F86: 0x6C23, + 0x9F87: 0x6C5E, + 0x9F88: 0x6C55, + 0x9F89: 0x6C62, + 0x9F8A: 0x6C6A, + 0x9F8B: 0x6C82, + 0x9F8C: 0x6C8D, + 0x9F8D: 0x6C9A, + 0x9F8E: 0x6C81, + 0x9F8F: 0x6C9B, + 0x9F90: 0x6C7E, + 0x9F91: 0x6C68, + 0x9F92: 0x6C73, + 0x9F93: 0x6C92, + 0x9F94: 0x6C90, + 0x9F95: 0x6CC4, + 0x9F96: 0x6CF1, + 0x9F97: 0x6CD3, + 0x9F98: 0x6CBD, + 0x9F99: 0x6CD7, + 0x9F9A: 0x6CC5, + 0x9F9B: 0x6CDD, + 0x9F9C: 0x6CAE, + 0x9F9D: 0x6CB1, + 0x9F9E: 0x6CBE, + 0x9F9F: 0x6CBA, + 0x9FA0: 0x6CDB, + 0x9FA1: 0x6CEF, + 0x9FA2: 0x6CD9, + 0x9FA3: 0x6CEA, + 0x9FA4: 0x6D1F, + 0x9FA5: 0x884D, + 0x9FA6: 0x6D36, + 0x9FA7: 0x6D2B, + 0x9FA8: 0x6D3D, + 0x9FA9: 0x6D38, + 0x9FAA: 0x6D19, + 0x9FAB: 0x6D35, + 0x9FAC: 0x6D33, + 0x9FAD: 0x6D12, + 0x9FAE: 0x6D0C, + 0x9FAF: 0x6D63, + 0x9FB0: 0x6D93, + 0x9FB1: 0x6D64, + 0x9FB2: 0x6D5A, + 0x9FB3: 0x6D79, + 0x9FB4: 0x6D59, + 0x9FB5: 0x6D8E, + 0x9FB6: 0x6D95, + 0x9FB7: 0x6FE4, + 0x9FB8: 0x6D85, + 0x9FB9: 0x6DF9, + 0x9FBA: 0x6E15, + 0x9FBB: 0x6E0A, + 0x9FBC: 0x6DB5, + 0x9FBD: 0x6DC7, + 0x9FBE: 0x6DE6, + 0x9FBF: 0x6DB8, + 0x9FC0: 0x6DC6, + 0x9FC1: 0x6DEC, + 0x9FC2: 0x6DDE, + 0x9FC3: 0x6DCC, + 0x9FC4: 0x6DE8, + 0x9FC5: 0x6DD2, + 0x9FC6: 0x6DC5, + 0x9FC7: 0x6DFA, + 0x9FC8: 0x6DD9, + 0x9FC9: 0x6DE4, + 0x9FCA: 0x6DD5, + 0x9FCB: 0x6DEA, + 0x9FCC: 0x6DEE, + 0x9FCD: 0x6E2D, + 0x9FCE: 0x6E6E, + 0x9FCF: 0x6E2E, + 0x9FD0: 0x6E19, + 0x9FD1: 0x6E72, + 0x9FD2: 0x6E5F, + 0x9FD3: 0x6E3E, + 0x9FD4: 0x6E23, + 0x9FD5: 0x6E6B, + 0x9FD6: 0x6E2B, + 0x9FD7: 0x6E76, + 0x9FD8: 0x6E4D, + 0x9FD9: 0x6E1F, + 0x9FDA: 0x6E43, + 0x9FDB: 0x6E3A, + 0x9FDC: 0x6E4E, + 0x9FDD: 0x6E24, + 0x9FDE: 0x6EFF, + 0x9FDF: 0x6E1D, + 0x9FE0: 0x6E38, + 0x9FE1: 0x6E82, + 0x9FE2: 0x6EAA, + 0x9FE3: 0x6E98, + 0x9FE4: 0x6EC9, + 0x9FE5: 0x6EB7, + 0x9FE6: 0x6ED3, + 0x9FE7: 0x6EBD, + 0x9FE8: 0x6EAF, + 0x9FE9: 0x6EC4, + 0x9FEA: 0x6EB2, + 0x9FEB: 0x6ED4, + 0x9FEC: 0x6ED5, + 0x9FED: 0x6E8F, + 0x9FEE: 0x6EA5, + 0x9FEF: 0x6EC2, + 0x9FF0: 0x6E9F, + 0x9FF1: 0x6F41, + 0x9FF2: 0x6F11, + 0x9FF3: 0x704C, + 0x9FF4: 0x6EEC, + 0x9FF5: 0x6EF8, + 0x9FF6: 0x6EFE, + 0x9FF7: 0x6F3F, + 0x9FF8: 0x6EF2, + 0x9FF9: 0x6F31, + 0x9FFA: 0x6EEF, + 0x9FFB: 0x6F32, + 0x9FFC: 0x6ECC, + 0xA1: 0xFF61, + 0xA2: 0xFF62, + 0xA3: 0xFF63, + 0xA4: 0xFF64, + 0xA5: 0xFF65, + 0xA6: 0xFF66, + 0xA7: 0xFF67, + 0xA8: 0xFF68, + 0xA9: 0xFF69, + 0xAA: 0xFF6A, + 0xAB: 0xFF6B, + 0xAC: 0xFF6C, + 0xAD: 0xFF6D, + 0xAE: 0xFF6E, + 0xAF: 0xFF6F, + 0xB0: 0xFF70, + 0xB1: 0xFF71, + 0xB2: 0xFF72, + 0xB3: 0xFF73, + 0xB4: 0xFF74, + 0xB5: 0xFF75, + 0xB6: 0xFF76, + 0xB7: 0xFF77, + 0xB8: 0xFF78, + 0xB9: 0xFF79, + 0xBA: 0xFF7A, + 0xBB: 0xFF7B, + 0xBC: 0xFF7C, + 0xBD: 0xFF7D, + 0xBE: 0xFF7E, + 0xBF: 0xFF7F, + 0xC0: 0xFF80, + 0xC1: 0xFF81, + 0xC2: 0xFF82, + 0xC3: 0xFF83, + 0xC4: 0xFF84, + 0xC5: 0xFF85, + 0xC6: 0xFF86, + 0xC7: 0xFF87, + 0xC8: 0xFF88, + 0xC9: 0xFF89, + 0xCA: 0xFF8A, + 0xCB: 0xFF8B, + 0xCC: 0xFF8C, + 0xCD: 0xFF8D, + 0xCE: 0xFF8E, + 0xCF: 0xFF8F, + 0xD0: 0xFF90, + 0xD1: 0xFF91, + 0xD2: 0xFF92, + 0xD3: 0xFF93, + 0xD4: 0xFF94, + 0xD5: 0xFF95, + 0xD6: 0xFF96, + 0xD7: 0xFF97, + 0xD8: 0xFF98, + 0xD9: 0xFF99, + 0xDA: 0xFF9A, + 0xDB: 0xFF9B, + 0xDC: 0xFF9C, + 0xDD: 0xFF9D, + 0xDE: 0xFF9E, + 0xDF: 0xFF9F, + 0xE040: 0x6F3E, + 0xE041: 0x6F13, + 0xE042: 0x6EF7, + 0xE043: 0x6F86, + 0xE044: 0x6F7A, + 0xE045: 0x6F78, + 0xE046: 0x6F81, + 0xE047: 0x6F80, + 0xE048: 0x6F6F, + 0xE049: 0x6F5B, + 0xE04A: 0x6FF3, + 0xE04B: 0x6F6D, + 0xE04C: 0x6F82, + 0xE04D: 0x6F7C, + 0xE04E: 0x6F58, + 0xE04F: 0x6F8E, + 0xE050: 0x6F91, + 0xE051: 0x6FC2, + 0xE052: 0x6F66, + 0xE053: 0x6FB3, + 0xE054: 0x6FA3, + 0xE055: 0x6FA1, + 0xE056: 0x6FA4, + 0xE057: 0x6FB9, + 0xE058: 0x6FC6, + 0xE059: 0x6FAA, + 0xE05A: 0x6FDF, + 0xE05B: 0x6FD5, + 0xE05C: 0x6FEC, + 0xE05D: 0x6FD4, + 0xE05E: 0x6FD8, + 0xE05F: 0x6FF1, + 0xE060: 0x6FEE, + 0xE061: 0x6FDB, + 0xE062: 0x7009, + 0xE063: 0x700B, + 0xE064: 0x6FFA, + 0xE065: 0x7011, + 0xE066: 0x7001, + 0xE067: 0x700F, + 0xE068: 0x6FFE, + 0xE069: 0x701B, + 0xE06A: 0x701A, + 0xE06B: 0x6F74, + 0xE06C: 0x701D, + 0xE06D: 0x7018, + 0xE06E: 0x701F, + 0xE06F: 0x7030, + 0xE070: 0x703E, + 0xE071: 0x7032, + 0xE072: 0x7051, + 0xE073: 0x7063, + 0xE074: 0x7099, + 0xE075: 0x7092, + 0xE076: 0x70AF, + 0xE077: 0x70F1, + 0xE078: 0x70AC, + 0xE079: 0x70B8, + 0xE07A: 0x70B3, + 0xE07B: 0x70AE, + 0xE07C: 0x70DF, + 0xE07D: 0x70CB, + 0xE07E: 0x70DD, + 0xE080: 0x70D9, + 0xE081: 0x7109, + 0xE082: 0x70FD, + 0xE083: 0x711C, + 0xE084: 0x7119, + 0xE085: 0x7165, + 0xE086: 0x7155, + 0xE087: 0x7188, + 0xE088: 0x7166, + 0xE089: 0x7162, + 0xE08A: 0x714C, + 0xE08B: 0x7156, + 0xE08C: 0x716C, + 0xE08D: 0x718F, + 0xE08E: 0x71FB, + 0xE08F: 0x7184, + 0xE090: 0x7195, + 0xE091: 0x71A8, + 0xE092: 0x71AC, + 0xE093: 0x71D7, + 0xE094: 0x71B9, + 0xE095: 0x71BE, + 0xE096: 0x71D2, + 0xE097: 0x71C9, + 0xE098: 0x71D4, + 0xE099: 0x71CE, + 0xE09A: 0x71E0, + 0xE09B: 0x71EC, + 0xE09C: 0x71E7, + 0xE09D: 0x71F5, + 0xE09E: 0x71FC, + 0xE09F: 0x71F9, + 0xE0A0: 0x71FF, + 0xE0A1: 0x720D, + 0xE0A2: 0x7210, + 0xE0A3: 0x721B, + 0xE0A4: 0x7228, + 0xE0A5: 0x722D, + 0xE0A6: 0x722C, + 0xE0A7: 0x7230, + 0xE0A8: 0x7232, + 0xE0A9: 0x723B, + 0xE0AA: 0x723C, + 0xE0AB: 0x723F, + 0xE0AC: 0x7240, + 0xE0AD: 0x7246, + 0xE0AE: 0x724B, + 0xE0AF: 0x7258, + 0xE0B0: 0x7274, + 0xE0B1: 0x727E, + 0xE0B2: 0x7282, + 0xE0B3: 0x7281, + 0xE0B4: 0x7287, + 0xE0B5: 0x7292, + 0xE0B6: 0x7296, + 0xE0B7: 0x72A2, + 0xE0B8: 0x72A7, + 0xE0B9: 0x72B9, + 0xE0BA: 0x72B2, + 0xE0BB: 0x72C3, + 0xE0BC: 0x72C6, + 0xE0BD: 0x72C4, + 0xE0BE: 0x72CE, + 0xE0BF: 0x72D2, + 0xE0C0: 0x72E2, + 0xE0C1: 0x72E0, + 0xE0C2: 0x72E1, + 0xE0C3: 0x72F9, + 0xE0C4: 0x72F7, + 0xE0C5: 0x500F, + 0xE0C6: 0x7317, + 0xE0C7: 0x730A, + 0xE0C8: 0x731C, + 0xE0C9: 0x7316, + 0xE0CA: 0x731D, + 0xE0CB: 0x7334, + 0xE0CC: 0x732F, + 0xE0CD: 0x7329, + 0xE0CE: 0x7325, + 0xE0CF: 0x733E, + 0xE0D0: 0x734E, + 0xE0D1: 0x734F, + 0xE0D2: 0x9ED8, + 0xE0D3: 0x7357, + 0xE0D4: 0x736A, + 0xE0D5: 0x7368, + 0xE0D6: 0x7370, + 0xE0D7: 0x7378, + 0xE0D8: 0x7375, + 0xE0D9: 0x737B, + 0xE0DA: 0x737A, + 0xE0DB: 0x73C8, + 0xE0DC: 0x73B3, + 0xE0DD: 0x73CE, + 0xE0DE: 0x73BB, + 0xE0DF: 0x73C0, + 0xE0E0: 0x73E5, + 0xE0E1: 0x73EE, + 0xE0E2: 0x73DE, + 0xE0E3: 0x74A2, + 0xE0E4: 0x7405, + 0xE0E5: 0x746F, + 0xE0E6: 0x7425, + 0xE0E7: 0x73F8, + 0xE0E8: 0x7432, + 0xE0E9: 0x743A, + 0xE0EA: 0x7455, + 0xE0EB: 0x743F, + 0xE0EC: 0x745F, + 0xE0ED: 0x7459, + 0xE0EE: 0x7441, + 0xE0EF: 0x745C, + 0xE0F0: 0x7469, + 0xE0F1: 0x7470, + 0xE0F2: 0x7463, + 0xE0F3: 0x746A, + 0xE0F4: 0x7476, + 0xE0F5: 0x747E, + 0xE0F6: 0x748B, + 0xE0F7: 0x749E, + 0xE0F8: 0x74A7, + 0xE0F9: 0x74CA, + 0xE0FA: 0x74CF, + 0xE0FB: 0x74D4, + 0xE0FC: 0x73F1, + 0xE140: 0x74E0, + 0xE141: 0x74E3, + 0xE142: 0x74E7, + 0xE143: 0x74E9, + 0xE144: 0x74EE, + 0xE145: 0x74F2, + 0xE146: 0x74F0, + 0xE147: 0x74F1, + 0xE148: 0x74F8, + 0xE149: 0x74F7, + 0xE14A: 0x7504, + 0xE14B: 0x7503, + 0xE14C: 0x7505, + 0xE14D: 0x750C, + 0xE14E: 0x750E, + 0xE14F: 0x750D, + 0xE150: 0x7515, + 0xE151: 0x7513, + 0xE152: 0x751E, + 0xE153: 0x7526, + 0xE154: 0x752C, + 0xE155: 0x753C, + 0xE156: 0x7544, + 0xE157: 0x754D, + 0xE158: 0x754A, + 0xE159: 0x7549, + 0xE15A: 0x755B, + 0xE15B: 0x7546, + 0xE15C: 0x755A, + 0xE15D: 0x7569, + 0xE15E: 0x7564, + 0xE15F: 0x7567, + 0xE160: 0x756B, + 0xE161: 0x756D, + 0xE162: 0x7578, + 0xE163: 0x7576, + 0xE164: 0x7586, + 0xE165: 0x7587, + 0xE166: 0x7574, + 0xE167: 0x758A, + 0xE168: 0x7589, + 0xE169: 0x7582, + 0xE16A: 0x7594, + 0xE16B: 0x759A, + 0xE16C: 0x759D, + 0xE16D: 0x75A5, + 0xE16E: 0x75A3, + 0xE16F: 0x75C2, + 0xE170: 0x75B3, + 0xE171: 0x75C3, + 0xE172: 0x75B5, + 0xE173: 0x75BD, + 0xE174: 0x75B8, + 0xE175: 0x75BC, + 0xE176: 0x75B1, + 0xE177: 0x75CD, + 0xE178: 0x75CA, + 0xE179: 0x75D2, + 0xE17A: 0x75D9, + 0xE17B: 0x75E3, + 0xE17C: 0x75DE, + 0xE17D: 0x75FE, + 0xE17E: 0x75FF, + 0xE180: 0x75FC, + 0xE181: 0x7601, + 0xE182: 0x75F0, + 0xE183: 0x75FA, + 0xE184: 0x75F2, + 0xE185: 0x75F3, + 0xE186: 0x760B, + 0xE187: 0x760D, + 0xE188: 0x7609, + 0xE189: 0x761F, + 0xE18A: 0x7627, + 0xE18B: 0x7620, + 0xE18C: 0x7621, + 0xE18D: 0x7622, + 0xE18E: 0x7624, + 0xE18F: 0x7634, + 0xE190: 0x7630, + 0xE191: 0x763B, + 0xE192: 0x7647, + 0xE193: 0x7648, + 0xE194: 0x7646, + 0xE195: 0x765C, + 0xE196: 0x7658, + 0xE197: 0x7661, + 0xE198: 0x7662, + 0xE199: 0x7668, + 0xE19A: 0x7669, + 0xE19B: 0x766A, + 0xE19C: 0x7667, + 0xE19D: 0x766C, + 0xE19E: 0x7670, + 0xE19F: 0x7672, + 0xE1A0: 0x7676, + 0xE1A1: 0x7678, + 0xE1A2: 0x767C, + 0xE1A3: 0x7680, + 0xE1A4: 0x7683, + 0xE1A5: 0x7688, + 0xE1A6: 0x768B, + 0xE1A7: 0x768E, + 0xE1A8: 0x7696, + 0xE1A9: 0x7693, + 0xE1AA: 0x7699, + 0xE1AB: 0x769A, + 0xE1AC: 0x76B0, + 0xE1AD: 0x76B4, + 0xE1AE: 0x76B8, + 0xE1AF: 0x76B9, + 0xE1B0: 0x76BA, + 0xE1B1: 0x76C2, + 0xE1B2: 0x76CD, + 0xE1B3: 0x76D6, + 0xE1B4: 0x76D2, + 0xE1B5: 0x76DE, + 0xE1B6: 0x76E1, + 0xE1B7: 0x76E5, + 0xE1B8: 0x76E7, + 0xE1B9: 0x76EA, + 0xE1BA: 0x862F, + 0xE1BB: 0x76FB, + 0xE1BC: 0x7708, + 0xE1BD: 0x7707, + 0xE1BE: 0x7704, + 0xE1BF: 0x7729, + 0xE1C0: 0x7724, + 0xE1C1: 0x771E, + 0xE1C2: 0x7725, + 0xE1C3: 0x7726, + 0xE1C4: 0x771B, + 0xE1C5: 0x7737, + 0xE1C6: 0x7738, + 0xE1C7: 0x7747, + 0xE1C8: 0x775A, + 0xE1C9: 0x7768, + 0xE1CA: 0x776B, + 0xE1CB: 0x775B, + 0xE1CC: 0x7765, + 0xE1CD: 0x777F, + 0xE1CE: 0x777E, + 0xE1CF: 0x7779, + 0xE1D0: 0x778E, + 0xE1D1: 0x778B, + 0xE1D2: 0x7791, + 0xE1D3: 0x77A0, + 0xE1D4: 0x779E, + 0xE1D5: 0x77B0, + 0xE1D6: 0x77B6, + 0xE1D7: 0x77B9, + 0xE1D8: 0x77BF, + 0xE1D9: 0x77BC, + 0xE1DA: 0x77BD, + 0xE1DB: 0x77BB, + 0xE1DC: 0x77C7, + 0xE1DD: 0x77CD, + 0xE1DE: 0x77D7, + 0xE1DF: 0x77DA, + 0xE1E0: 0x77DC, + 0xE1E1: 0x77E3, + 0xE1E2: 0x77EE, + 0xE1E3: 0x77FC, + 0xE1E4: 0x780C, + 0xE1E5: 0x7812, + 0xE1E6: 0x7926, + 0xE1E7: 0x7820, + 0xE1E8: 0x792A, + 0xE1E9: 0x7845, + 0xE1EA: 0x788E, + 0xE1EB: 0x7874, + 0xE1EC: 0x7886, + 0xE1ED: 0x787C, + 0xE1EE: 0x789A, + 0xE1EF: 0x788C, + 0xE1F0: 0x78A3, + 0xE1F1: 0x78B5, + 0xE1F2: 0x78AA, + 0xE1F3: 0x78AF, + 0xE1F4: 0x78D1, + 0xE1F5: 0x78C6, + 0xE1F6: 0x78CB, + 0xE1F7: 0x78D4, + 0xE1F8: 0x78BE, + 0xE1F9: 0x78BC, + 0xE1FA: 0x78C5, + 0xE1FB: 0x78CA, + 0xE1FC: 0x78EC, + 0xE240: 0x78E7, + 0xE241: 0x78DA, + 0xE242: 0x78FD, + 0xE243: 0x78F4, + 0xE244: 0x7907, + 0xE245: 0x7912, + 0xE246: 0x7911, + 0xE247: 0x7919, + 0xE248: 0x792C, + 0xE249: 0x792B, + 0xE24A: 0x7940, + 0xE24B: 0x7960, + 0xE24C: 0x7957, + 0xE24D: 0x795F, + 0xE24E: 0x795A, + 0xE24F: 0x7955, + 0xE250: 0x7953, + 0xE251: 0x797A, + 0xE252: 0x797F, + 0xE253: 0x798A, + 0xE254: 0x799D, + 0xE255: 0x79A7, + 0xE256: 0x9F4B, + 0xE257: 0x79AA, + 0xE258: 0x79AE, + 0xE259: 0x79B3, + 0xE25A: 0x79B9, + 0xE25B: 0x79BA, + 0xE25C: 0x79C9, + 0xE25D: 0x79D5, + 0xE25E: 0x79E7, + 0xE25F: 0x79EC, + 0xE260: 0x79E1, + 0xE261: 0x79E3, + 0xE262: 0x7A08, + 0xE263: 0x7A0D, + 0xE264: 0x7A18, + 0xE265: 0x7A19, + 0xE266: 0x7A20, + 0xE267: 0x7A1F, + 0xE268: 0x7980, + 0xE269: 0x7A31, + 0xE26A: 0x7A3B, + 0xE26B: 0x7A3E, + 0xE26C: 0x7A37, + 0xE26D: 0x7A43, + 0xE26E: 0x7A57, + 0xE26F: 0x7A49, + 0xE270: 0x7A61, + 0xE271: 0x7A62, + 0xE272: 0x7A69, + 0xE273: 0x9F9D, + 0xE274: 0x7A70, + 0xE275: 0x7A79, + 0xE276: 0x7A7D, + 0xE277: 0x7A88, + 0xE278: 0x7A97, + 0xE279: 0x7A95, + 0xE27A: 0x7A98, + 0xE27B: 0x7A96, + 0xE27C: 0x7AA9, + 0xE27D: 0x7AC8, + 0xE27E: 0x7AB0, + 0xE280: 0x7AB6, + 0xE281: 0x7AC5, + 0xE282: 0x7AC4, + 0xE283: 0x7ABF, + 0xE284: 0x9083, + 0xE285: 0x7AC7, + 0xE286: 0x7ACA, + 0xE287: 0x7ACD, + 0xE288: 0x7ACF, + 0xE289: 0x7AD5, + 0xE28A: 0x7AD3, + 0xE28B: 0x7AD9, + 0xE28C: 0x7ADA, + 0xE28D: 0x7ADD, + 0xE28E: 0x7AE1, + 0xE28F: 0x7AE2, + 0xE290: 0x7AE6, + 0xE291: 0x7AED, + 0xE292: 0x7AF0, + 0xE293: 0x7B02, + 0xE294: 0x7B0F, + 0xE295: 0x7B0A, + 0xE296: 0x7B06, + 0xE297: 0x7B33, + 0xE298: 0x7B18, + 0xE299: 0x7B19, + 0xE29A: 0x7B1E, + 0xE29B: 0x7B35, + 0xE29C: 0x7B28, + 0xE29D: 0x7B36, + 0xE29E: 0x7B50, + 0xE29F: 0x7B7A, + 0xE2A0: 0x7B04, + 0xE2A1: 0x7B4D, + 0xE2A2: 0x7B0B, + 0xE2A3: 0x7B4C, + 0xE2A4: 0x7B45, + 0xE2A5: 0x7B75, + 0xE2A6: 0x7B65, + 0xE2A7: 0x7B74, + 0xE2A8: 0x7B67, + 0xE2A9: 0x7B70, + 0xE2AA: 0x7B71, + 0xE2AB: 0x7B6C, + 0xE2AC: 0x7B6E, + 0xE2AD: 0x7B9D, + 0xE2AE: 0x7B98, + 0xE2AF: 0x7B9F, + 0xE2B0: 0x7B8D, + 0xE2B1: 0x7B9C, + 0xE2B2: 0x7B9A, + 0xE2B3: 0x7B8B, + 0xE2B4: 0x7B92, + 0xE2B5: 0x7B8F, + 0xE2B6: 0x7B5D, + 0xE2B7: 0x7B99, + 0xE2B8: 0x7BCB, + 0xE2B9: 0x7BC1, + 0xE2BA: 0x7BCC, + 0xE2BB: 0x7BCF, + 0xE2BC: 0x7BB4, + 0xE2BD: 0x7BC6, + 0xE2BE: 0x7BDD, + 0xE2BF: 0x7BE9, + 0xE2C0: 0x7C11, + 0xE2C1: 0x7C14, + 0xE2C2: 0x7BE6, + 0xE2C3: 0x7BE5, + 0xE2C4: 0x7C60, + 0xE2C5: 0x7C00, + 0xE2C6: 0x7C07, + 0xE2C7: 0x7C13, + 0xE2C8: 0x7BF3, + 0xE2C9: 0x7BF7, + 0xE2CA: 0x7C17, + 0xE2CB: 0x7C0D, + 0xE2CC: 0x7BF6, + 0xE2CD: 0x7C23, + 0xE2CE: 0x7C27, + 0xE2CF: 0x7C2A, + 0xE2D0: 0x7C1F, + 0xE2D1: 0x7C37, + 0xE2D2: 0x7C2B, + 0xE2D3: 0x7C3D, + 0xE2D4: 0x7C4C, + 0xE2D5: 0x7C43, + 0xE2D6: 0x7C54, + 0xE2D7: 0x7C4F, + 0xE2D8: 0x7C40, + 0xE2D9: 0x7C50, + 0xE2DA: 0x7C58, + 0xE2DB: 0x7C5F, + 0xE2DC: 0x7C64, + 0xE2DD: 0x7C56, + 0xE2DE: 0x7C65, + 0xE2DF: 0x7C6C, + 0xE2E0: 0x7C75, + 0xE2E1: 0x7C83, + 0xE2E2: 0x7C90, + 0xE2E3: 0x7CA4, + 0xE2E4: 0x7CAD, + 0xE2E5: 0x7CA2, + 0xE2E6: 0x7CAB, + 0xE2E7: 0x7CA1, + 0xE2E8: 0x7CA8, + 0xE2E9: 0x7CB3, + 0xE2EA: 0x7CB2, + 0xE2EB: 0x7CB1, + 0xE2EC: 0x7CAE, + 0xE2ED: 0x7CB9, + 0xE2EE: 0x7CBD, + 0xE2EF: 0x7CC0, + 0xE2F0: 0x7CC5, + 0xE2F1: 0x7CC2, + 0xE2F2: 0x7CD8, + 0xE2F3: 0x7CD2, + 0xE2F4: 0x7CDC, + 0xE2F5: 0x7CE2, + 0xE2F6: 0x9B3B, + 0xE2F7: 0x7CEF, + 0xE2F8: 0x7CF2, + 0xE2F9: 0x7CF4, + 0xE2FA: 0x7CF6, + 0xE2FB: 0x7CFA, + 0xE2FC: 0x7D06, + 0xE340: 0x7D02, + 0xE341: 0x7D1C, + 0xE342: 0x7D15, + 0xE343: 0x7D0A, + 0xE344: 0x7D45, + 0xE345: 0x7D4B, + 0xE346: 0x7D2E, + 0xE347: 0x7D32, + 0xE348: 0x7D3F, + 0xE349: 0x7D35, + 0xE34A: 0x7D46, + 0xE34B: 0x7D73, + 0xE34C: 0x7D56, + 0xE34D: 0x7D4E, + 0xE34E: 0x7D72, + 0xE34F: 0x7D68, + 0xE350: 0x7D6E, + 0xE351: 0x7D4F, + 0xE352: 0x7D63, + 0xE353: 0x7D93, + 0xE354: 0x7D89, + 0xE355: 0x7D5B, + 0xE356: 0x7D8F, + 0xE357: 0x7D7D, + 0xE358: 0x7D9B, + 0xE359: 0x7DBA, + 0xE35A: 0x7DAE, + 0xE35B: 0x7DA3, + 0xE35C: 0x7DB5, + 0xE35D: 0x7DC7, + 0xE35E: 0x7DBD, + 0xE35F: 0x7DAB, + 0xE360: 0x7E3D, + 0xE361: 0x7DA2, + 0xE362: 0x7DAF, + 0xE363: 0x7DDC, + 0xE364: 0x7DB8, + 0xE365: 0x7D9F, + 0xE366: 0x7DB0, + 0xE367: 0x7DD8, + 0xE368: 0x7DDD, + 0xE369: 0x7DE4, + 0xE36A: 0x7DDE, + 0xE36B: 0x7DFB, + 0xE36C: 0x7DF2, + 0xE36D: 0x7DE1, + 0xE36E: 0x7E05, + 0xE36F: 0x7E0A, + 0xE370: 0x7E23, + 0xE371: 0x7E21, + 0xE372: 0x7E12, + 0xE373: 0x7E31, + 0xE374: 0x7E1F, + 0xE375: 0x7E09, + 0xE376: 0x7E0B, + 0xE377: 0x7E22, + 0xE378: 0x7E46, + 0xE379: 0x7E66, + 0xE37A: 0x7E3B, + 0xE37B: 0x7E35, + 0xE37C: 0x7E39, + 0xE37D: 0x7E43, + 0xE37E: 0x7E37, + 0xE380: 0x7E32, + 0xE381: 0x7E3A, + 0xE382: 0x7E67, + 0xE383: 0x7E5D, + 0xE384: 0x7E56, + 0xE385: 0x7E5E, + 0xE386: 0x7E59, + 0xE387: 0x7E5A, + 0xE388: 0x7E79, + 0xE389: 0x7E6A, + 0xE38A: 0x7E69, + 0xE38B: 0x7E7C, + 0xE38C: 0x7E7B, + 0xE38D: 0x7E83, + 0xE38E: 0x7DD5, + 0xE38F: 0x7E7D, + 0xE390: 0x8FAE, + 0xE391: 0x7E7F, + 0xE392: 0x7E88, + 0xE393: 0x7E89, + 0xE394: 0x7E8C, + 0xE395: 0x7E92, + 0xE396: 0x7E90, + 0xE397: 0x7E93, + 0xE398: 0x7E94, + 0xE399: 0x7E96, + 0xE39A: 0x7E8E, + 0xE39B: 0x7E9B, + 0xE39C: 0x7E9C, + 0xE39D: 0x7F38, + 0xE39E: 0x7F3A, + 0xE39F: 0x7F45, + 0xE3A0: 0x7F4C, + 0xE3A1: 0x7F4D, + 0xE3A2: 0x7F4E, + 0xE3A3: 0x7F50, + 0xE3A4: 0x7F51, + 0xE3A5: 0x7F55, + 0xE3A6: 0x7F54, + 0xE3A7: 0x7F58, + 0xE3A8: 0x7F5F, + 0xE3A9: 0x7F60, + 0xE3AA: 0x7F68, + 0xE3AB: 0x7F69, + 0xE3AC: 0x7F67, + 0xE3AD: 0x7F78, + 0xE3AE: 0x7F82, + 0xE3AF: 0x7F86, + 0xE3B0: 0x7F83, + 0xE3B1: 0x7F88, + 0xE3B2: 0x7F87, + 0xE3B3: 0x7F8C, + 0xE3B4: 0x7F94, + 0xE3B5: 0x7F9E, + 0xE3B6: 0x7F9D, + 0xE3B7: 0x7F9A, + 0xE3B8: 0x7FA3, + 0xE3B9: 0x7FAF, + 0xE3BA: 0x7FB2, + 0xE3BB: 0x7FB9, + 0xE3BC: 0x7FAE, + 0xE3BD: 0x7FB6, + 0xE3BE: 0x7FB8, + 0xE3BF: 0x8B71, + 0xE3C0: 0x7FC5, + 0xE3C1: 0x7FC6, + 0xE3C2: 0x7FCA, + 0xE3C3: 0x7FD5, + 0xE3C4: 0x7FD4, + 0xE3C5: 0x7FE1, + 0xE3C6: 0x7FE6, + 0xE3C7: 0x7FE9, + 0xE3C8: 0x7FF3, + 0xE3C9: 0x7FF9, + 0xE3CA: 0x98DC, + 0xE3CB: 0x8006, + 0xE3CC: 0x8004, + 0xE3CD: 0x800B, + 0xE3CE: 0x8012, + 0xE3CF: 0x8018, + 0xE3D0: 0x8019, + 0xE3D1: 0x801C, + 0xE3D2: 0x8021, + 0xE3D3: 0x8028, + 0xE3D4: 0x803F, + 0xE3D5: 0x803B, + 0xE3D6: 0x804A, + 0xE3D7: 0x8046, + 0xE3D8: 0x8052, + 0xE3D9: 0x8058, + 0xE3DA: 0x805A, + 0xE3DB: 0x805F, + 0xE3DC: 0x8062, + 0xE3DD: 0x8068, + 0xE3DE: 0x8073, + 0xE3DF: 0x8072, + 0xE3E0: 0x8070, + 0xE3E1: 0x8076, + 0xE3E2: 0x8079, + 0xE3E3: 0x807D, + 0xE3E4: 0x807F, + 0xE3E5: 0x8084, + 0xE3E6: 0x8086, + 0xE3E7: 0x8085, + 0xE3E8: 0x809B, + 0xE3E9: 0x8093, + 0xE3EA: 0x809A, + 0xE3EB: 0x80AD, + 0xE3EC: 0x5190, + 0xE3ED: 0x80AC, + 0xE3EE: 0x80DB, + 0xE3EF: 0x80E5, + 0xE3F0: 0x80D9, + 0xE3F1: 0x80DD, + 0xE3F2: 0x80C4, + 0xE3F3: 0x80DA, + 0xE3F4: 0x80D6, + 0xE3F5: 0x8109, + 0xE3F6: 0x80EF, + 0xE3F7: 0x80F1, + 0xE3F8: 0x811B, + 0xE3F9: 0x8129, + 0xE3FA: 0x8123, + 0xE3FB: 0x812F, + 0xE3FC: 0x814B, + 0xE440: 0x968B, + 0xE441: 0x8146, + 0xE442: 0x813E, + 0xE443: 0x8153, + 0xE444: 0x8151, + 0xE445: 0x80FC, + 0xE446: 0x8171, + 0xE447: 0x816E, + 0xE448: 0x8165, + 0xE449: 0x8166, + 0xE44A: 0x8174, + 0xE44B: 0x8183, + 0xE44C: 0x8188, + 0xE44D: 0x818A, + 0xE44E: 0x8180, + 0xE44F: 0x8182, + 0xE450: 0x81A0, + 0xE451: 0x8195, + 0xE452: 0x81A4, + 0xE453: 0x81A3, + 0xE454: 0x815F, + 0xE455: 0x8193, + 0xE456: 0x81A9, + 0xE457: 0x81B0, + 0xE458: 0x81B5, + 0xE459: 0x81BE, + 0xE45A: 0x81B8, + 0xE45B: 0x81BD, + 0xE45C: 0x81C0, + 0xE45D: 0x81C2, + 0xE45E: 0x81BA, + 0xE45F: 0x81C9, + 0xE460: 0x81CD, + 0xE461: 0x81D1, + 0xE462: 0x81D9, + 0xE463: 0x81D8, + 0xE464: 0x81C8, + 0xE465: 0x81DA, + 0xE466: 0x81DF, + 0xE467: 0x81E0, + 0xE468: 0x81E7, + 0xE469: 0x81FA, + 0xE46A: 0x81FB, + 0xE46B: 0x81FE, + 0xE46C: 0x8201, + 0xE46D: 0x8202, + 0xE46E: 0x8205, + 0xE46F: 0x8207, + 0xE470: 0x820A, + 0xE471: 0x820D, + 0xE472: 0x8210, + 0xE473: 0x8216, + 0xE474: 0x8229, + 0xE475: 0x822B, + 0xE476: 0x8238, + 0xE477: 0x8233, + 0xE478: 0x8240, + 0xE479: 0x8259, + 0xE47A: 0x8258, + 0xE47B: 0x825D, + 0xE47C: 0x825A, + 0xE47D: 0x825F, + 0xE47E: 0x8264, + 0xE480: 0x8262, + 0xE481: 0x8268, + 0xE482: 0x826A, + 0xE483: 0x826B, + 0xE484: 0x822E, + 0xE485: 0x8271, + 0xE486: 0x8277, + 0xE487: 0x8278, + 0xE488: 0x827E, + 0xE489: 0x828D, + 0xE48A: 0x8292, + 0xE48B: 0x82AB, + 0xE48C: 0x829F, + 0xE48D: 0x82BB, + 0xE48E: 0x82AC, + 0xE48F: 0x82E1, + 0xE490: 0x82E3, + 0xE491: 0x82DF, + 0xE492: 0x82D2, + 0xE493: 0x82F4, + 0xE494: 0x82F3, + 0xE495: 0x82FA, + 0xE496: 0x8393, + 0xE497: 0x8303, + 0xE498: 0x82FB, + 0xE499: 0x82F9, + 0xE49A: 0x82DE, + 0xE49B: 0x8306, + 0xE49C: 0x82DC, + 0xE49D: 0x8309, + 0xE49E: 0x82D9, + 0xE49F: 0x8335, + 0xE4A0: 0x8334, + 0xE4A1: 0x8316, + 0xE4A2: 0x8332, + 0xE4A3: 0x8331, + 0xE4A4: 0x8340, + 0xE4A5: 0x8339, + 0xE4A6: 0x8350, + 0xE4A7: 0x8345, + 0xE4A8: 0x832F, + 0xE4A9: 0x832B, + 0xE4AA: 0x8317, + 0xE4AB: 0x8318, + 0xE4AC: 0x8385, + 0xE4AD: 0x839A, + 0xE4AE: 0x83AA, + 0xE4AF: 0x839F, + 0xE4B0: 0x83A2, + 0xE4B1: 0x8396, + 0xE4B2: 0x8323, + 0xE4B3: 0x838E, + 0xE4B4: 0x8387, + 0xE4B5: 0x838A, + 0xE4B6: 0x837C, + 0xE4B7: 0x83B5, + 0xE4B8: 0x8373, + 0xE4B9: 0x8375, + 0xE4BA: 0x83A0, + 0xE4BB: 0x8389, + 0xE4BC: 0x83A8, + 0xE4BD: 0x83F4, + 0xE4BE: 0x8413, + 0xE4BF: 0x83EB, + 0xE4C0: 0x83CE, + 0xE4C1: 0x83FD, + 0xE4C2: 0x8403, + 0xE4C3: 0x83D8, + 0xE4C4: 0x840B, + 0xE4C5: 0x83C1, + 0xE4C6: 0x83F7, + 0xE4C7: 0x8407, + 0xE4C8: 0x83E0, + 0xE4C9: 0x83F2, + 0xE4CA: 0x840D, + 0xE4CB: 0x8422, + 0xE4CC: 0x8420, + 0xE4CD: 0x83BD, + 0xE4CE: 0x8438, + 0xE4CF: 0x8506, + 0xE4D0: 0x83FB, + 0xE4D1: 0x846D, + 0xE4D2: 0x842A, + 0xE4D3: 0x843C, + 0xE4D4: 0x855A, + 0xE4D5: 0x8484, + 0xE4D6: 0x8477, + 0xE4D7: 0x846B, + 0xE4D8: 0x84AD, + 0xE4D9: 0x846E, + 0xE4DA: 0x8482, + 0xE4DB: 0x8469, + 0xE4DC: 0x8446, + 0xE4DD: 0x842C, + 0xE4DE: 0x846F, + 0xE4DF: 0x8479, + 0xE4E0: 0x8435, + 0xE4E1: 0x84CA, + 0xE4E2: 0x8462, + 0xE4E3: 0x84B9, + 0xE4E4: 0x84BF, + 0xE4E5: 0x849F, + 0xE4E6: 0x84D9, + 0xE4E7: 0x84CD, + 0xE4E8: 0x84BB, + 0xE4E9: 0x84DA, + 0xE4EA: 0x84D0, + 0xE4EB: 0x84C1, + 0xE4EC: 0x84C6, + 0xE4ED: 0x84D6, + 0xE4EE: 0x84A1, + 0xE4EF: 0x8521, + 0xE4F0: 0x84FF, + 0xE4F1: 0x84F4, + 0xE4F2: 0x8517, + 0xE4F3: 0x8518, + 0xE4F4: 0x852C, + 0xE4F5: 0x851F, + 0xE4F6: 0x8515, + 0xE4F7: 0x8514, + 0xE4F8: 0x84FC, + 0xE4F9: 0x8540, + 0xE4FA: 0x8563, + 0xE4FB: 0x8558, + 0xE4FC: 0x8548, + 0xE540: 0x8541, + 0xE541: 0x8602, + 0xE542: 0x854B, + 0xE543: 0x8555, + 0xE544: 0x8580, + 0xE545: 0x85A4, + 0xE546: 0x8588, + 0xE547: 0x8591, + 0xE548: 0x858A, + 0xE549: 0x85A8, + 0xE54A: 0x856D, + 0xE54B: 0x8594, + 0xE54C: 0x859B, + 0xE54D: 0x85EA, + 0xE54E: 0x8587, + 0xE54F: 0x859C, + 0xE550: 0x8577, + 0xE551: 0x857E, + 0xE552: 0x8590, + 0xE553: 0x85C9, + 0xE554: 0x85BA, + 0xE555: 0x85CF, + 0xE556: 0x85B9, + 0xE557: 0x85D0, + 0xE558: 0x85D5, + 0xE559: 0x85DD, + 0xE55A: 0x85E5, + 0xE55B: 0x85DC, + 0xE55C: 0x85F9, + 0xE55D: 0x860A, + 0xE55E: 0x8613, + 0xE55F: 0x860B, + 0xE560: 0x85FE, + 0xE561: 0x85FA, + 0xE562: 0x8606, + 0xE563: 0x8622, + 0xE564: 0x861A, + 0xE565: 0x8630, + 0xE566: 0x863F, + 0xE567: 0x864D, + 0xE568: 0x4E55, + 0xE569: 0x8654, + 0xE56A: 0x865F, + 0xE56B: 0x8667, + 0xE56C: 0x8671, + 0xE56D: 0x8693, + 0xE56E: 0x86A3, + 0xE56F: 0x86A9, + 0xE570: 0x86AA, + 0xE571: 0x868B, + 0xE572: 0x868C, + 0xE573: 0x86B6, + 0xE574: 0x86AF, + 0xE575: 0x86C4, + 0xE576: 0x86C6, + 0xE577: 0x86B0, + 0xE578: 0x86C9, + 0xE579: 0x8823, + 0xE57A: 0x86AB, + 0xE57B: 0x86D4, + 0xE57C: 0x86DE, + 0xE57D: 0x86E9, + 0xE57E: 0x86EC, + 0xE580: 0x86DF, + 0xE581: 0x86DB, + 0xE582: 0x86EF, + 0xE583: 0x8712, + 0xE584: 0x8706, + 0xE585: 0x8708, + 0xE586: 0x8700, + 0xE587: 0x8703, + 0xE588: 0x86FB, + 0xE589: 0x8711, + 0xE58A: 0x8709, + 0xE58B: 0x870D, + 0xE58C: 0x86F9, + 0xE58D: 0x870A, + 0xE58E: 0x8734, + 0xE58F: 0x873F, + 0xE590: 0x8737, + 0xE591: 0x873B, + 0xE592: 0x8725, + 0xE593: 0x8729, + 0xE594: 0x871A, + 0xE595: 0x8760, + 0xE596: 0x875F, + 0xE597: 0x8778, + 0xE598: 0x874C, + 0xE599: 0x874E, + 0xE59A: 0x8774, + 0xE59B: 0x8757, + 0xE59C: 0x8768, + 0xE59D: 0x876E, + 0xE59E: 0x8759, + 0xE59F: 0x8753, + 0xE5A0: 0x8763, + 0xE5A1: 0x876A, + 0xE5A2: 0x8805, + 0xE5A3: 0x87A2, + 0xE5A4: 0x879F, + 0xE5A5: 0x8782, + 0xE5A6: 0x87AF, + 0xE5A7: 0x87CB, + 0xE5A8: 0x87BD, + 0xE5A9: 0x87C0, + 0xE5AA: 0x87D0, + 0xE5AB: 0x96D6, + 0xE5AC: 0x87AB, + 0xE5AD: 0x87C4, + 0xE5AE: 0x87B3, + 0xE5AF: 0x87C7, + 0xE5B0: 0x87C6, + 0xE5B1: 0x87BB, + 0xE5B2: 0x87EF, + 0xE5B3: 0x87F2, + 0xE5B4: 0x87E0, + 0xE5B5: 0x880F, + 0xE5B6: 0x880D, + 0xE5B7: 0x87FE, + 0xE5B8: 0x87F6, + 0xE5B9: 0x87F7, + 0xE5BA: 0x880E, + 0xE5BB: 0x87D2, + 0xE5BC: 0x8811, + 0xE5BD: 0x8816, + 0xE5BE: 0x8815, + 0xE5BF: 0x8822, + 0xE5C0: 0x8821, + 0xE5C1: 0x8831, + 0xE5C2: 0x8836, + 0xE5C3: 0x8839, + 0xE5C4: 0x8827, + 0xE5C5: 0x883B, + 0xE5C6: 0x8844, + 0xE5C7: 0x8842, + 0xE5C8: 0x8852, + 0xE5C9: 0x8859, + 0xE5CA: 0x885E, + 0xE5CB: 0x8862, + 0xE5CC: 0x886B, + 0xE5CD: 0x8881, + 0xE5CE: 0x887E, + 0xE5CF: 0x889E, + 0xE5D0: 0x8875, + 0xE5D1: 0x887D, + 0xE5D2: 0x88B5, + 0xE5D3: 0x8872, + 0xE5D4: 0x8882, + 0xE5D5: 0x8897, + 0xE5D6: 0x8892, + 0xE5D7: 0x88AE, + 0xE5D8: 0x8899, + 0xE5D9: 0x88A2, + 0xE5DA: 0x888D, + 0xE5DB: 0x88A4, + 0xE5DC: 0x88B0, + 0xE5DD: 0x88BF, + 0xE5DE: 0x88B1, + 0xE5DF: 0x88C3, + 0xE5E0: 0x88C4, + 0xE5E1: 0x88D4, + 0xE5E2: 0x88D8, + 0xE5E3: 0x88D9, + 0xE5E4: 0x88DD, + 0xE5E5: 0x88F9, + 0xE5E6: 0x8902, + 0xE5E7: 0x88FC, + 0xE5E8: 0x88F4, + 0xE5E9: 0x88E8, + 0xE5EA: 0x88F2, + 0xE5EB: 0x8904, + 0xE5EC: 0x890C, + 0xE5ED: 0x890A, + 0xE5EE: 0x8913, + 0xE5EF: 0x8943, + 0xE5F0: 0x891E, + 0xE5F1: 0x8925, + 0xE5F2: 0x892A, + 0xE5F3: 0x892B, + 0xE5F4: 0x8941, + 0xE5F5: 0x8944, + 0xE5F6: 0x893B, + 0xE5F7: 0x8936, + 0xE5F8: 0x8938, + 0xE5F9: 0x894C, + 0xE5FA: 0x891D, + 0xE5FB: 0x8960, + 0xE5FC: 0x895E, + 0xE640: 0x8966, + 0xE641: 0x8964, + 0xE642: 0x896D, + 0xE643: 0x896A, + 0xE644: 0x896F, + 0xE645: 0x8974, + 0xE646: 0x8977, + 0xE647: 0x897E, + 0xE648: 0x8983, + 0xE649: 0x8988, + 0xE64A: 0x898A, + 0xE64B: 0x8993, + 0xE64C: 0x8998, + 0xE64D: 0x89A1, + 0xE64E: 0x89A9, + 0xE64F: 0x89A6, + 0xE650: 0x89AC, + 0xE651: 0x89AF, + 0xE652: 0x89B2, + 0xE653: 0x89BA, + 0xE654: 0x89BD, + 0xE655: 0x89BF, + 0xE656: 0x89C0, + 0xE657: 0x89DA, + 0xE658: 0x89DC, + 0xE659: 0x89DD, + 0xE65A: 0x89E7, + 0xE65B: 0x89F4, + 0xE65C: 0x89F8, + 0xE65D: 0x8A03, + 0xE65E: 0x8A16, + 0xE65F: 0x8A10, + 0xE660: 0x8A0C, + 0xE661: 0x8A1B, + 0xE662: 0x8A1D, + 0xE663: 0x8A25, + 0xE664: 0x8A36, + 0xE665: 0x8A41, + 0xE666: 0x8A5B, + 0xE667: 0x8A52, + 0xE668: 0x8A46, + 0xE669: 0x8A48, + 0xE66A: 0x8A7C, + 0xE66B: 0x8A6D, + 0xE66C: 0x8A6C, + 0xE66D: 0x8A62, + 0xE66E: 0x8A85, + 0xE66F: 0x8A82, + 0xE670: 0x8A84, + 0xE671: 0x8AA8, + 0xE672: 0x8AA1, + 0xE673: 0x8A91, + 0xE674: 0x8AA5, + 0xE675: 0x8AA6, + 0xE676: 0x8A9A, + 0xE677: 0x8AA3, + 0xE678: 0x8AC4, + 0xE679: 0x8ACD, + 0xE67A: 0x8AC2, + 0xE67B: 0x8ADA, + 0xE67C: 0x8AEB, + 0xE67D: 0x8AF3, + 0xE67E: 0x8AE7, + 0xE680: 0x8AE4, + 0xE681: 0x8AF1, + 0xE682: 0x8B14, + 0xE683: 0x8AE0, + 0xE684: 0x8AE2, + 0xE685: 0x8AF7, + 0xE686: 0x8ADE, + 0xE687: 0x8ADB, + 0xE688: 0x8B0C, + 0xE689: 0x8B07, + 0xE68A: 0x8B1A, + 0xE68B: 0x8AE1, + 0xE68C: 0x8B16, + 0xE68D: 0x8B10, + 0xE68E: 0x8B17, + 0xE68F: 0x8B20, + 0xE690: 0x8B33, + 0xE691: 0x97AB, + 0xE692: 0x8B26, + 0xE693: 0x8B2B, + 0xE694: 0x8B3E, + 0xE695: 0x8B28, + 0xE696: 0x8B41, + 0xE697: 0x8B4C, + 0xE698: 0x8B4F, + 0xE699: 0x8B4E, + 0xE69A: 0x8B49, + 0xE69B: 0x8B56, + 0xE69C: 0x8B5B, + 0xE69D: 0x8B5A, + 0xE69E: 0x8B6B, + 0xE69F: 0x8B5F, + 0xE6A0: 0x8B6C, + 0xE6A1: 0x8B6F, + 0xE6A2: 0x8B74, + 0xE6A3: 0x8B7D, + 0xE6A4: 0x8B80, + 0xE6A5: 0x8B8C, + 0xE6A6: 0x8B8E, + 0xE6A7: 0x8B92, + 0xE6A8: 0x8B93, + 0xE6A9: 0x8B96, + 0xE6AA: 0x8B99, + 0xE6AB: 0x8B9A, + 0xE6AC: 0x8C3A, + 0xE6AD: 0x8C41, + 0xE6AE: 0x8C3F, + 0xE6AF: 0x8C48, + 0xE6B0: 0x8C4C, + 0xE6B1: 0x8C4E, + 0xE6B2: 0x8C50, + 0xE6B3: 0x8C55, + 0xE6B4: 0x8C62, + 0xE6B5: 0x8C6C, + 0xE6B6: 0x8C78, + 0xE6B7: 0x8C7A, + 0xE6B8: 0x8C82, + 0xE6B9: 0x8C89, + 0xE6BA: 0x8C85, + 0xE6BB: 0x8C8A, + 0xE6BC: 0x8C8D, + 0xE6BD: 0x8C8E, + 0xE6BE: 0x8C94, + 0xE6BF: 0x8C7C, + 0xE6C0: 0x8C98, + 0xE6C1: 0x621D, + 0xE6C2: 0x8CAD, + 0xE6C3: 0x8CAA, + 0xE6C4: 0x8CBD, + 0xE6C5: 0x8CB2, + 0xE6C6: 0x8CB3, + 0xE6C7: 0x8CAE, + 0xE6C8: 0x8CB6, + 0xE6C9: 0x8CC8, + 0xE6CA: 0x8CC1, + 0xE6CB: 0x8CE4, + 0xE6CC: 0x8CE3, + 0xE6CD: 0x8CDA, + 0xE6CE: 0x8CFD, + 0xE6CF: 0x8CFA, + 0xE6D0: 0x8CFB, + 0xE6D1: 0x8D04, + 0xE6D2: 0x8D05, + 0xE6D3: 0x8D0A, + 0xE6D4: 0x8D07, + 0xE6D5: 0x8D0F, + 0xE6D6: 0x8D0D, + 0xE6D7: 0x8D10, + 0xE6D8: 0x9F4E, + 0xE6D9: 0x8D13, + 0xE6DA: 0x8CCD, + 0xE6DB: 0x8D14, + 0xE6DC: 0x8D16, + 0xE6DD: 0x8D67, + 0xE6DE: 0x8D6D, + 0xE6DF: 0x8D71, + 0xE6E0: 0x8D73, + 0xE6E1: 0x8D81, + 0xE6E2: 0x8D99, + 0xE6E3: 0x8DC2, + 0xE6E4: 0x8DBE, + 0xE6E5: 0x8DBA, + 0xE6E6: 0x8DCF, + 0xE6E7: 0x8DDA, + 0xE6E8: 0x8DD6, + 0xE6E9: 0x8DCC, + 0xE6EA: 0x8DDB, + 0xE6EB: 0x8DCB, + 0xE6EC: 0x8DEA, + 0xE6ED: 0x8DEB, + 0xE6EE: 0x8DDF, + 0xE6EF: 0x8DE3, + 0xE6F0: 0x8DFC, + 0xE6F1: 0x8E08, + 0xE6F2: 0x8E09, + 0xE6F3: 0x8DFF, + 0xE6F4: 0x8E1D, + 0xE6F5: 0x8E1E, + 0xE6F6: 0x8E10, + 0xE6F7: 0x8E1F, + 0xE6F8: 0x8E42, + 0xE6F9: 0x8E35, + 0xE6FA: 0x8E30, + 0xE6FB: 0x8E34, + 0xE6FC: 0x8E4A, + 0xE740: 0x8E47, + 0xE741: 0x8E49, + 0xE742: 0x8E4C, + 0xE743: 0x8E50, + 0xE744: 0x8E48, + 0xE745: 0x8E59, + 0xE746: 0x8E64, + 0xE747: 0x8E60, + 0xE748: 0x8E2A, + 0xE749: 0x8E63, + 0xE74A: 0x8E55, + 0xE74B: 0x8E76, + 0xE74C: 0x8E72, + 0xE74D: 0x8E7C, + 0xE74E: 0x8E81, + 0xE74F: 0x8E87, + 0xE750: 0x8E85, + 0xE751: 0x8E84, + 0xE752: 0x8E8B, + 0xE753: 0x8E8A, + 0xE754: 0x8E93, + 0xE755: 0x8E91, + 0xE756: 0x8E94, + 0xE757: 0x8E99, + 0xE758: 0x8EAA, + 0xE759: 0x8EA1, + 0xE75A: 0x8EAC, + 0xE75B: 0x8EB0, + 0xE75C: 0x8EC6, + 0xE75D: 0x8EB1, + 0xE75E: 0x8EBE, + 0xE75F: 0x8EC5, + 0xE760: 0x8EC8, + 0xE761: 0x8ECB, + 0xE762: 0x8EDB, + 0xE763: 0x8EE3, + 0xE764: 0x8EFC, + 0xE765: 0x8EFB, + 0xE766: 0x8EEB, + 0xE767: 0x8EFE, + 0xE768: 0x8F0A, + 0xE769: 0x8F05, + 0xE76A: 0x8F15, + 0xE76B: 0x8F12, + 0xE76C: 0x8F19, + 0xE76D: 0x8F13, + 0xE76E: 0x8F1C, + 0xE76F: 0x8F1F, + 0xE770: 0x8F1B, + 0xE771: 0x8F0C, + 0xE772: 0x8F26, + 0xE773: 0x8F33, + 0xE774: 0x8F3B, + 0xE775: 0x8F39, + 0xE776: 0x8F45, + 0xE777: 0x8F42, + 0xE778: 0x8F3E, + 0xE779: 0x8F4C, + 0xE77A: 0x8F49, + 0xE77B: 0x8F46, + 0xE77C: 0x8F4E, + 0xE77D: 0x8F57, + 0xE77E: 0x8F5C, + 0xE780: 0x8F62, + 0xE781: 0x8F63, + 0xE782: 0x8F64, + 0xE783: 0x8F9C, + 0xE784: 0x8F9F, + 0xE785: 0x8FA3, + 0xE786: 0x8FAD, + 0xE787: 0x8FAF, + 0xE788: 0x8FB7, + 0xE789: 0x8FDA, + 0xE78A: 0x8FE5, + 0xE78B: 0x8FE2, + 0xE78C: 0x8FEA, + 0xE78D: 0x8FEF, + 0xE78E: 0x9087, + 0xE78F: 0x8FF4, + 0xE790: 0x9005, + 0xE791: 0x8FF9, + 0xE792: 0x8FFA, + 0xE793: 0x9011, + 0xE794: 0x9015, + 0xE795: 0x9021, + 0xE796: 0x900D, + 0xE797: 0x901E, + 0xE798: 0x9016, + 0xE799: 0x900B, + 0xE79A: 0x9027, + 0xE79B: 0x9036, + 0xE79C: 0x9035, + 0xE79D: 0x9039, + 0xE79E: 0x8FF8, + 0xE79F: 0x904F, + 0xE7A0: 0x9050, + 0xE7A1: 0x9051, + 0xE7A2: 0x9052, + 0xE7A3: 0x900E, + 0xE7A4: 0x9049, + 0xE7A5: 0x903E, + 0xE7A6: 0x9056, + 0xE7A7: 0x9058, + 0xE7A8: 0x905E, + 0xE7A9: 0x9068, + 0xE7AA: 0x906F, + 0xE7AB: 0x9076, + 0xE7AC: 0x96A8, + 0xE7AD: 0x9072, + 0xE7AE: 0x9082, + 0xE7AF: 0x907D, + 0xE7B0: 0x9081, + 0xE7B1: 0x9080, + 0xE7B2: 0x908A, + 0xE7B3: 0x9089, + 0xE7B4: 0x908F, + 0xE7B5: 0x90A8, + 0xE7B6: 0x90AF, + 0xE7B7: 0x90B1, + 0xE7B8: 0x90B5, + 0xE7B9: 0x90E2, + 0xE7BA: 0x90E4, + 0xE7BB: 0x6248, + 0xE7BC: 0x90DB, + 0xE7BD: 0x9102, + 0xE7BE: 0x9112, + 0xE7BF: 0x9119, + 0xE7C0: 0x9132, + 0xE7C1: 0x9130, + 0xE7C2: 0x914A, + 0xE7C3: 0x9156, + 0xE7C4: 0x9158, + 0xE7C5: 0x9163, + 0xE7C6: 0x9165, + 0xE7C7: 0x9169, + 0xE7C8: 0x9173, + 0xE7C9: 0x9172, + 0xE7CA: 0x918B, + 0xE7CB: 0x9189, + 0xE7CC: 0x9182, + 0xE7CD: 0x91A2, + 0xE7CE: 0x91AB, + 0xE7CF: 0x91AF, + 0xE7D0: 0x91AA, + 0xE7D1: 0x91B5, + 0xE7D2: 0x91B4, + 0xE7D3: 0x91BA, + 0xE7D4: 0x91C0, + 0xE7D5: 0x91C1, + 0xE7D6: 0x91C9, + 0xE7D7: 0x91CB, + 0xE7D8: 0x91D0, + 0xE7D9: 0x91D6, + 0xE7DA: 0x91DF, + 0xE7DB: 0x91E1, + 0xE7DC: 0x91DB, + 0xE7DD: 0x91FC, + 0xE7DE: 0x91F5, + 0xE7DF: 0x91F6, + 0xE7E0: 0x921E, + 0xE7E1: 0x91FF, + 0xE7E2: 0x9214, + 0xE7E3: 0x922C, + 0xE7E4: 0x9215, + 0xE7E5: 0x9211, + 0xE7E6: 0x925E, + 0xE7E7: 0x9257, + 0xE7E8: 0x9245, + 0xE7E9: 0x9249, + 0xE7EA: 0x9264, + 0xE7EB: 0x9248, + 0xE7EC: 0x9295, + 0xE7ED: 0x923F, + 0xE7EE: 0x924B, + 0xE7EF: 0x9250, + 0xE7F0: 0x929C, + 0xE7F1: 0x9296, + 0xE7F2: 0x9293, + 0xE7F3: 0x929B, + 0xE7F4: 0x925A, + 0xE7F5: 0x92CF, + 0xE7F6: 0x92B9, + 0xE7F7: 0x92B7, + 0xE7F8: 0x92E9, + 0xE7F9: 0x930F, + 0xE7FA: 0x92FA, + 0xE7FB: 0x9344, + 0xE7FC: 0x932E, + 0xE840: 0x9319, + 0xE841: 0x9322, + 0xE842: 0x931A, + 0xE843: 0x9323, + 0xE844: 0x933A, + 0xE845: 0x9335, + 0xE846: 0x933B, + 0xE847: 0x935C, + 0xE848: 0x9360, + 0xE849: 0x937C, + 0xE84A: 0x936E, + 0xE84B: 0x9356, + 0xE84C: 0x93B0, + 0xE84D: 0x93AC, + 0xE84E: 0x93AD, + 0xE84F: 0x9394, + 0xE850: 0x93B9, + 0xE851: 0x93D6, + 0xE852: 0x93D7, + 0xE853: 0x93E8, + 0xE854: 0x93E5, + 0xE855: 0x93D8, + 0xE856: 0x93C3, + 0xE857: 0x93DD, + 0xE858: 0x93D0, + 0xE859: 0x93C8, + 0xE85A: 0x93E4, + 0xE85B: 0x941A, + 0xE85C: 0x9414, + 0xE85D: 0x9413, + 0xE85E: 0x9403, + 0xE85F: 0x9407, + 0xE860: 0x9410, + 0xE861: 0x9436, + 0xE862: 0x942B, + 0xE863: 0x9435, + 0xE864: 0x9421, + 0xE865: 0x943A, + 0xE866: 0x9441, + 0xE867: 0x9452, + 0xE868: 0x9444, + 0xE869: 0x945B, + 0xE86A: 0x9460, + 0xE86B: 0x9462, + 0xE86C: 0x945E, + 0xE86D: 0x946A, + 0xE86E: 0x9229, + 0xE86F: 0x9470, + 0xE870: 0x9475, + 0xE871: 0x9477, + 0xE872: 0x947D, + 0xE873: 0x945A, + 0xE874: 0x947C, + 0xE875: 0x947E, + 0xE876: 0x9481, + 0xE877: 0x947F, + 0xE878: 0x9582, + 0xE879: 0x9587, + 0xE87A: 0x958A, + 0xE87B: 0x9594, + 0xE87C: 0x9596, + 0xE87D: 0x9598, + 0xE87E: 0x9599, + 0xE880: 0x95A0, + 0xE881: 0x95A8, + 0xE882: 0x95A7, + 0xE883: 0x95AD, + 0xE884: 0x95BC, + 0xE885: 0x95BB, + 0xE886: 0x95B9, + 0xE887: 0x95BE, + 0xE888: 0x95CA, + 0xE889: 0x6FF6, + 0xE88A: 0x95C3, + 0xE88B: 0x95CD, + 0xE88C: 0x95CC, + 0xE88D: 0x95D5, + 0xE88E: 0x95D4, + 0xE88F: 0x95D6, + 0xE890: 0x95DC, + 0xE891: 0x95E1, + 0xE892: 0x95E5, + 0xE893: 0x95E2, + 0xE894: 0x9621, + 0xE895: 0x9628, + 0xE896: 0x962E, + 0xE897: 0x962F, + 0xE898: 0x9642, + 0xE899: 0x964C, + 0xE89A: 0x964F, + 0xE89B: 0x964B, + 0xE89C: 0x9677, + 0xE89D: 0x965C, + 0xE89E: 0x965E, + 0xE89F: 0x965D, + 0xE8A0: 0x965F, + 0xE8A1: 0x9666, + 0xE8A2: 0x9672, + 0xE8A3: 0x966C, + 0xE8A4: 0x968D, + 0xE8A5: 0x9698, + 0xE8A6: 0x9695, + 0xE8A7: 0x9697, + 0xE8A8: 0x96AA, + 0xE8A9: 0x96A7, + 0xE8AA: 0x96B1, + 0xE8AB: 0x96B2, + 0xE8AC: 0x96B0, + 0xE8AD: 0x96B4, + 0xE8AE: 0x96B6, + 0xE8AF: 0x96B8, + 0xE8B0: 0x96B9, + 0xE8B1: 0x96CE, + 0xE8B2: 0x96CB, + 0xE8B3: 0x96C9, + 0xE8B4: 0x96CD, + 0xE8B5: 0x894D, + 0xE8B6: 0x96DC, + 0xE8B7: 0x970D, + 0xE8B8: 0x96D5, + 0xE8B9: 0x96F9, + 0xE8BA: 0x9704, + 0xE8BB: 0x9706, + 0xE8BC: 0x9708, + 0xE8BD: 0x9713, + 0xE8BE: 0x970E, + 0xE8BF: 0x9711, + 0xE8C0: 0x970F, + 0xE8C1: 0x9716, + 0xE8C2: 0x9719, + 0xE8C3: 0x9724, + 0xE8C4: 0x972A, + 0xE8C5: 0x9730, + 0xE8C6: 0x9739, + 0xE8C7: 0x973D, + 0xE8C8: 0x973E, + 0xE8C9: 0x9744, + 0xE8CA: 0x9746, + 0xE8CB: 0x9748, + 0xE8CC: 0x9742, + 0xE8CD: 0x9749, + 0xE8CE: 0x975C, + 0xE8CF: 0x9760, + 0xE8D0: 0x9764, + 0xE8D1: 0x9766, + 0xE8D2: 0x9768, + 0xE8D3: 0x52D2, + 0xE8D4: 0x976B, + 0xE8D5: 0x9771, + 0xE8D6: 0x9779, + 0xE8D7: 0x9785, + 0xE8D8: 0x977C, + 0xE8D9: 0x9781, + 0xE8DA: 0x977A, + 0xE8DB: 0x9786, + 0xE8DC: 0x978B, + 0xE8DD: 0x978F, + 0xE8DE: 0x9790, + 0xE8DF: 0x979C, + 0xE8E0: 0x97A8, + 0xE8E1: 0x97A6, + 0xE8E2: 0x97A3, + 0xE8E3: 0x97B3, + 0xE8E4: 0x97B4, + 0xE8E5: 0x97C3, + 0xE8E6: 0x97C6, + 0xE8E7: 0x97C8, + 0xE8E8: 0x97CB, + 0xE8E9: 0x97DC, + 0xE8EA: 0x97ED, + 0xE8EB: 0x9F4F, + 0xE8EC: 0x97F2, + 0xE8ED: 0x7ADF, + 0xE8EE: 0x97F6, + 0xE8EF: 0x97F5, + 0xE8F0: 0x980F, + 0xE8F1: 0x980C, + 0xE8F2: 0x9838, + 0xE8F3: 0x9824, + 0xE8F4: 0x9821, + 0xE8F5: 0x9837, + 0xE8F6: 0x983D, + 0xE8F7: 0x9846, + 0xE8F8: 0x984F, + 0xE8F9: 0x984B, + 0xE8FA: 0x986B, + 0xE8FB: 0x986F, + 0xE8FC: 0x9870, + 0xE940: 0x9871, + 0xE941: 0x9874, + 0xE942: 0x9873, + 0xE943: 0x98AA, + 0xE944: 0x98AF, + 0xE945: 0x98B1, + 0xE946: 0x98B6, + 0xE947: 0x98C4, + 0xE948: 0x98C3, + 0xE949: 0x98C6, + 0xE94A: 0x98E9, + 0xE94B: 0x98EB, + 0xE94C: 0x9903, + 0xE94D: 0x9909, + 0xE94E: 0x9912, + 0xE94F: 0x9914, + 0xE950: 0x9918, + 0xE951: 0x9921, + 0xE952: 0x991D, + 0xE953: 0x991E, + 0xE954: 0x9924, + 0xE955: 0x9920, + 0xE956: 0x992C, + 0xE957: 0x992E, + 0xE958: 0x993D, + 0xE959: 0x993E, + 0xE95A: 0x9942, + 0xE95B: 0x9949, + 0xE95C: 0x9945, + 0xE95D: 0x9950, + 0xE95E: 0x994B, + 0xE95F: 0x9951, + 0xE960: 0x9952, + 0xE961: 0x994C, + 0xE962: 0x9955, + 0xE963: 0x9997, + 0xE964: 0x9998, + 0xE965: 0x99A5, + 0xE966: 0x99AD, + 0xE967: 0x99AE, + 0xE968: 0x99BC, + 0xE969: 0x99DF, + 0xE96A: 0x99DB, + 0xE96B: 0x99DD, + 0xE96C: 0x99D8, + 0xE96D: 0x99D1, + 0xE96E: 0x99ED, + 0xE96F: 0x99EE, + 0xE970: 0x99F1, + 0xE971: 0x99F2, + 0xE972: 0x99FB, + 0xE973: 0x99F8, + 0xE974: 0x9A01, + 0xE975: 0x9A0F, + 0xE976: 0x9A05, + 0xE977: 0x99E2, + 0xE978: 0x9A19, + 0xE979: 0x9A2B, + 0xE97A: 0x9A37, + 0xE97B: 0x9A45, + 0xE97C: 0x9A42, + 0xE97D: 0x9A40, + 0xE97E: 0x9A43, + 0xE980: 0x9A3E, + 0xE981: 0x9A55, + 0xE982: 0x9A4D, + 0xE983: 0x9A5B, + 0xE984: 0x9A57, + 0xE985: 0x9A5F, + 0xE986: 0x9A62, + 0xE987: 0x9A65, + 0xE988: 0x9A64, + 0xE989: 0x9A69, + 0xE98A: 0x9A6B, + 0xE98B: 0x9A6A, + 0xE98C: 0x9AAD, + 0xE98D: 0x9AB0, + 0xE98E: 0x9ABC, + 0xE98F: 0x9AC0, + 0xE990: 0x9ACF, + 0xE991: 0x9AD1, + 0xE992: 0x9AD3, + 0xE993: 0x9AD4, + 0xE994: 0x9ADE, + 0xE995: 0x9ADF, + 0xE996: 0x9AE2, + 0xE997: 0x9AE3, + 0xE998: 0x9AE6, + 0xE999: 0x9AEF, + 0xE99A: 0x9AEB, + 0xE99B: 0x9AEE, + 0xE99C: 0x9AF4, + 0xE99D: 0x9AF1, + 0xE99E: 0x9AF7, + 0xE99F: 0x9AFB, + 0xE9A0: 0x9B06, + 0xE9A1: 0x9B18, + 0xE9A2: 0x9B1A, + 0xE9A3: 0x9B1F, + 0xE9A4: 0x9B22, + 0xE9A5: 0x9B23, + 0xE9A6: 0x9B25, + 0xE9A7: 0x9B27, + 0xE9A8: 0x9B28, + 0xE9A9: 0x9B29, + 0xE9AA: 0x9B2A, + 0xE9AB: 0x9B2E, + 0xE9AC: 0x9B2F, + 0xE9AD: 0x9B32, + 0xE9AE: 0x9B44, + 0xE9AF: 0x9B43, + 0xE9B0: 0x9B4F, + 0xE9B1: 0x9B4D, + 0xE9B2: 0x9B4E, + 0xE9B3: 0x9B51, + 0xE9B4: 0x9B58, + 0xE9B5: 0x9B74, + 0xE9B6: 0x9B93, + 0xE9B7: 0x9B83, + 0xE9B8: 0x9B91, + 0xE9B9: 0x9B96, + 0xE9BA: 0x9B97, + 0xE9BB: 0x9B9F, + 0xE9BC: 0x9BA0, + 0xE9BD: 0x9BA8, + 0xE9BE: 0x9BB4, + 0xE9BF: 0x9BC0, + 0xE9C0: 0x9BCA, + 0xE9C1: 0x9BB9, + 0xE9C2: 0x9BC6, + 0xE9C3: 0x9BCF, + 0xE9C4: 0x9BD1, + 0xE9C5: 0x9BD2, + 0xE9C6: 0x9BE3, + 0xE9C7: 0x9BE2, + 0xE9C8: 0x9BE4, + 0xE9C9: 0x9BD4, + 0xE9CA: 0x9BE1, + 0xE9CB: 0x9C3A, + 0xE9CC: 0x9BF2, + 0xE9CD: 0x9BF1, + 0xE9CE: 0x9BF0, + 0xE9CF: 0x9C15, + 0xE9D0: 0x9C14, + 0xE9D1: 0x9C09, + 0xE9D2: 0x9C13, + 0xE9D3: 0x9C0C, + 0xE9D4: 0x9C06, + 0xE9D5: 0x9C08, + 0xE9D6: 0x9C12, + 0xE9D7: 0x9C0A, + 0xE9D8: 0x9C04, + 0xE9D9: 0x9C2E, + 0xE9DA: 0x9C1B, + 0xE9DB: 0x9C25, + 0xE9DC: 0x9C24, + 0xE9DD: 0x9C21, + 0xE9DE: 0x9C30, + 0xE9DF: 0x9C47, + 0xE9E0: 0x9C32, + 0xE9E1: 0x9C46, + 0xE9E2: 0x9C3E, + 0xE9E3: 0x9C5A, + 0xE9E4: 0x9C60, + 0xE9E5: 0x9C67, + 0xE9E6: 0x9C76, + 0xE9E7: 0x9C78, + 0xE9E8: 0x9CE7, + 0xE9E9: 0x9CEC, + 0xE9EA: 0x9CF0, + 0xE9EB: 0x9D09, + 0xE9EC: 0x9D08, + 0xE9ED: 0x9CEB, + 0xE9EE: 0x9D03, + 0xE9EF: 0x9D06, + 0xE9F0: 0x9D2A, + 0xE9F1: 0x9D26, + 0xE9F2: 0x9DAF, + 0xE9F3: 0x9D23, + 0xE9F4: 0x9D1F, + 0xE9F5: 0x9D44, + 0xE9F6: 0x9D15, + 0xE9F7: 0x9D12, + 0xE9F8: 0x9D41, + 0xE9F9: 0x9D3F, + 0xE9FA: 0x9D3E, + 0xE9FB: 0x9D46, + 0xE9FC: 0x9D48, + 0xEA40: 0x9D5D, + 0xEA41: 0x9D5E, + 0xEA42: 0x9D64, + 0xEA43: 0x9D51, + 0xEA44: 0x9D50, + 0xEA45: 0x9D59, + 0xEA46: 0x9D72, + 0xEA47: 0x9D89, + 0xEA48: 0x9D87, + 0xEA49: 0x9DAB, + 0xEA4A: 0x9D6F, + 0xEA4B: 0x9D7A, + 0xEA4C: 0x9D9A, + 0xEA4D: 0x9DA4, + 0xEA4E: 0x9DA9, + 0xEA4F: 0x9DB2, + 0xEA50: 0x9DC4, + 0xEA51: 0x9DC1, + 0xEA52: 0x9DBB, + 0xEA53: 0x9DB8, + 0xEA54: 0x9DBA, + 0xEA55: 0x9DC6, + 0xEA56: 0x9DCF, + 0xEA57: 0x9DC2, + 0xEA58: 0x9DD9, + 0xEA59: 0x9DD3, + 0xEA5A: 0x9DF8, + 0xEA5B: 0x9DE6, + 0xEA5C: 0x9DED, + 0xEA5D: 0x9DEF, + 0xEA5E: 0x9DFD, + 0xEA5F: 0x9E1A, + 0xEA60: 0x9E1B, + 0xEA61: 0x9E1E, + 0xEA62: 0x9E75, + 0xEA63: 0x9E79, + 0xEA64: 0x9E7D, + 0xEA65: 0x9E81, + 0xEA66: 0x9E88, + 0xEA67: 0x9E8B, + 0xEA68: 0x9E8C, + 0xEA69: 0x9E92, + 0xEA6A: 0x9E95, + 0xEA6B: 0x9E91, + 0xEA6C: 0x9E9D, + 0xEA6D: 0x9EA5, + 0xEA6E: 0x9EA9, + 0xEA6F: 0x9EB8, + 0xEA70: 0x9EAA, + 0xEA71: 0x9EAD, + 0xEA72: 0x9761, + 0xEA73: 0x9ECC, + 0xEA74: 0x9ECE, + 0xEA75: 0x9ECF, + 0xEA76: 0x9ED0, + 0xEA77: 0x9ED4, + 0xEA78: 0x9EDC, + 0xEA79: 0x9EDE, + 0xEA7A: 0x9EDD, + 0xEA7B: 0x9EE0, + 0xEA7C: 0x9EE5, + 0xEA7D: 0x9EE8, + 0xEA7E: 0x9EEF, + 0xEA80: 0x9EF4, + 0xEA81: 0x9EF6, + 0xEA82: 0x9EF7, + 0xEA83: 0x9EF9, + 0xEA84: 0x9EFB, + 0xEA85: 0x9EFC, + 0xEA86: 0x9EFD, + 0xEA87: 0x9F07, + 0xEA88: 0x9F08, + 0xEA89: 0x76B7, + 0xEA8A: 0x9F15, + 0xEA8B: 0x9F21, + 0xEA8C: 0x9F2C, + 0xEA8D: 0x9F3E, + 0xEA8E: 0x9F4A, + 0xEA8F: 0x9F52, + 0xEA90: 0x9F54, + 0xEA91: 0x9F63, + 0xEA92: 0x9F5F, + 0xEA93: 0x9F60, + 0xEA94: 0x9F61, + 0xEA95: 0x9F66, + 0xEA96: 0x9F67, + 0xEA97: 0x9F6C, + 0xEA98: 0x9F6A, + 0xEA99: 0x9F77, + 0xEA9A: 0x9F72, + 0xEA9B: 0x9F76, + 0xEA9C: 0x9F95, + 0xEA9D: 0x9F9C, + 0xEA9E: 0x9FA0, + 0xEA9F: 0x582F, + 0xEAA0: 0x69C7, + 0xEAA1: 0x9059, + 0xEAA2: 0x7464, + 0xEAA3: 0x51DC, + 0xEAA4: 0x7199, +}; + + +/***/ }), +/* 9 */ +/***/ (function(module, exports, __webpack_require__) { + +"use strict"; + +Object.defineProperty(exports, "__esModule", { value: true }); +var GenericGF_1 = __webpack_require__(1); +var GenericGFPoly_1 = __webpack_require__(2); +function runEuclideanAlgorithm(field, a, b, R) { + var _a; + // Assume a's degree is >= b's + if (a.degree() < b.degree()) { + _a = [b, a], a = _a[0], b = _a[1]; + } + var rLast = a; + var r = b; + var tLast = field.zero; + var t = field.one; + // Run Euclidean algorithm until r's degree is less than R/2 + while (r.degree() >= R / 2) { + var rLastLast = rLast; + var tLastLast = tLast; + rLast = r; + tLast = t; + // Divide rLastLast by rLast, with quotient in q and remainder in r + if (rLast.isZero()) { + // Euclidean algorithm already terminated? + return null; + } + r = rLastLast; + var q = field.zero; + var denominatorLeadingTerm = rLast.getCoefficient(rLast.degree()); + var dltInverse = field.inverse(denominatorLeadingTerm); + while (r.degree() >= rLast.degree() && !r.isZero()) { + var degreeDiff = r.degree() - rLast.degree(); + var scale = field.multiply(r.getCoefficient(r.degree()), dltInverse); + q = q.addOrSubtract(field.buildMonomial(degreeDiff, scale)); + r = r.addOrSubtract(rLast.multiplyByMonomial(degreeDiff, scale)); + } + t = q.multiplyPoly(tLast).addOrSubtract(tLastLast); + if (r.degree() >= rLast.degree()) { + return null; + } + } + var sigmaTildeAtZero = t.getCoefficient(0); + if (sigmaTildeAtZero === 0) { + return null; + } + var inverse = field.inverse(sigmaTildeAtZero); + return [t.multiply(inverse), r.multiply(inverse)]; +} +function findErrorLocations(field, errorLocator) { + // This is a direct application of Chien's search + var numErrors = errorLocator.degree(); + if (numErrors === 1) { + return [errorLocator.getCoefficient(1)]; + } + var result = new Array(numErrors); + var errorCount = 0; + for (var i = 1; i < field.size && errorCount < numErrors; i++) { + if (errorLocator.evaluateAt(i) === 0) { + result[errorCount] = field.inverse(i); + errorCount++; + } + } + if (errorCount !== numErrors) { + return null; + } + return result; +} +function findErrorMagnitudes(field, errorEvaluator, errorLocations) { + // This is directly applying Forney's Formula + var s = errorLocations.length; + var result = new Array(s); + for (var i = 0; i < s; i++) { + var xiInverse = field.inverse(errorLocations[i]); + var denominator = 1; + for (var j = 0; j < s; j++) { + if (i !== j) { + denominator = field.multiply(denominator, GenericGF_1.addOrSubtractGF(1, field.multiply(errorLocations[j], xiInverse))); + } + } + result[i] = field.multiply(errorEvaluator.evaluateAt(xiInverse), field.inverse(denominator)); + if (field.generatorBase !== 0) { + result[i] = field.multiply(result[i], xiInverse); + } + } + return result; +} +function decode(bytes, twoS) { + var outputBytes = new Uint8ClampedArray(bytes.length); + outputBytes.set(bytes); + var field = new GenericGF_1.default(0x011D, 256, 0); // x^8 + x^4 + x^3 + x^2 + 1 + var poly = new GenericGFPoly_1.default(field, outputBytes); + var syndromeCoefficients = new Uint8ClampedArray(twoS); + var error = false; + for (var s = 0; s < twoS; s++) { + var evaluation = poly.evaluateAt(field.exp(s + field.generatorBase)); + syndromeCoefficients[syndromeCoefficients.length - 1 - s] = evaluation; + if (evaluation !== 0) { + error = true; + } + } + if (!error) { + return outputBytes; + } + var syndrome = new GenericGFPoly_1.default(field, syndromeCoefficients); + var sigmaOmega = runEuclideanAlgorithm(field, field.buildMonomial(twoS, 1), syndrome, twoS); + if (sigmaOmega === null) { + return null; + } + var errorLocations = findErrorLocations(field, sigmaOmega[0]); + if (errorLocations == null) { + return null; + } + var errorMagnitudes = findErrorMagnitudes(field, sigmaOmega[1], errorLocations); + for (var i = 0; i < errorLocations.length; i++) { + var position = outputBytes.length - 1 - field.log(errorLocations[i]); + if (position < 0) { + return null; + } + outputBytes[position] = GenericGF_1.addOrSubtractGF(outputBytes[position], errorMagnitudes[i]); + } + return outputBytes; +} +exports.decode = decode; + + +/***/ }), +/* 10 */ +/***/ (function(module, exports, __webpack_require__) { + +"use strict"; + +Object.defineProperty(exports, "__esModule", { value: true }); +exports.VERSIONS = [ + { + infoBits: null, + versionNumber: 1, + alignmentPatternCenters: [], + errorCorrectionLevels: [ + { + ecCodewordsPerBlock: 7, + ecBlocks: [{ numBlocks: 1, dataCodewordsPerBlock: 19 }], + }, + { + ecCodewordsPerBlock: 10, + ecBlocks: [{ numBlocks: 1, dataCodewordsPerBlock: 16 }], + }, + { + ecCodewordsPerBlock: 13, + ecBlocks: [{ numBlocks: 1, dataCodewordsPerBlock: 13 }], + }, + { + ecCodewordsPerBlock: 17, + ecBlocks: [{ numBlocks: 1, dataCodewordsPerBlock: 9 }], + }, + ], + }, + { + infoBits: null, + versionNumber: 2, + alignmentPatternCenters: [6, 18], + errorCorrectionLevels: [ + { + ecCodewordsPerBlock: 10, + ecBlocks: [{ numBlocks: 1, dataCodewordsPerBlock: 34 }], + }, + { + ecCodewordsPerBlock: 16, + ecBlocks: [{ numBlocks: 1, dataCodewordsPerBlock: 28 }], + }, + { + ecCodewordsPerBlock: 22, + ecBlocks: [{ numBlocks: 1, dataCodewordsPerBlock: 22 }], + }, + { + ecCodewordsPerBlock: 28, + ecBlocks: [{ numBlocks: 1, dataCodewordsPerBlock: 16 }], + }, + ], + }, + { + infoBits: null, + versionNumber: 3, + alignmentPatternCenters: [6, 22], + errorCorrectionLevels: [ + { + ecCodewordsPerBlock: 15, + ecBlocks: [{ numBlocks: 1, dataCodewordsPerBlock: 55 }], + }, + { + ecCodewordsPerBlock: 26, + ecBlocks: [{ numBlocks: 1, dataCodewordsPerBlock: 44 }], + }, + { + ecCodewordsPerBlock: 18, + ecBlocks: [{ numBlocks: 2, dataCodewordsPerBlock: 17 }], + }, + { + ecCodewordsPerBlock: 22, + ecBlocks: [{ numBlocks: 2, dataCodewordsPerBlock: 13 }], + }, + ], + }, + { + infoBits: null, + versionNumber: 4, + alignmentPatternCenters: [6, 26], + errorCorrectionLevels: [ + { + ecCodewordsPerBlock: 20, + ecBlocks: [{ numBlocks: 1, dataCodewordsPerBlock: 80 }], + }, + { + ecCodewordsPerBlock: 18, + ecBlocks: [{ numBlocks: 2, dataCodewordsPerBlock: 32 }], + }, + { + ecCodewordsPerBlock: 26, + ecBlocks: [{ numBlocks: 2, dataCodewordsPerBlock: 24 }], + }, + { + ecCodewordsPerBlock: 16, + ecBlocks: [{ numBlocks: 4, dataCodewordsPerBlock: 9 }], + }, + ], + }, + { + infoBits: null, + versionNumber: 5, + alignmentPatternCenters: [6, 30], + errorCorrectionLevels: [ + { + ecCodewordsPerBlock: 26, + ecBlocks: [{ numBlocks: 1, dataCodewordsPerBlock: 108 }], + }, + { + ecCodewordsPerBlock: 24, + ecBlocks: [{ numBlocks: 2, dataCodewordsPerBlock: 43 }], + }, + { + ecCodewordsPerBlock: 18, + ecBlocks: [ + { numBlocks: 2, dataCodewordsPerBlock: 15 }, + { numBlocks: 2, dataCodewordsPerBlock: 16 }, + ], + }, + { + ecCodewordsPerBlock: 22, + ecBlocks: [ + { numBlocks: 2, dataCodewordsPerBlock: 11 }, + { numBlocks: 2, dataCodewordsPerBlock: 12 }, + ], + }, + ], + }, + { + infoBits: null, + versionNumber: 6, + alignmentPatternCenters: [6, 34], + errorCorrectionLevels: [ + { + ecCodewordsPerBlock: 18, + ecBlocks: [{ numBlocks: 2, dataCodewordsPerBlock: 68 }], + }, + { + ecCodewordsPerBlock: 16, + ecBlocks: [{ numBlocks: 4, dataCodewordsPerBlock: 27 }], + }, + { + ecCodewordsPerBlock: 24, + ecBlocks: [{ numBlocks: 4, dataCodewordsPerBlock: 19 }], + }, + { + ecCodewordsPerBlock: 28, + ecBlocks: [{ numBlocks: 4, dataCodewordsPerBlock: 15 }], + }, + ], + }, + { + infoBits: 0x07C94, + versionNumber: 7, + alignmentPatternCenters: [6, 22, 38], + errorCorrectionLevels: [ + { + ecCodewordsPerBlock: 20, + ecBlocks: [{ numBlocks: 2, dataCodewordsPerBlock: 78 }], + }, + { + ecCodewordsPerBlock: 18, + ecBlocks: [{ numBlocks: 4, dataCodewordsPerBlock: 31 }], + }, + { + ecCodewordsPerBlock: 18, + ecBlocks: [ + { numBlocks: 2, dataCodewordsPerBlock: 14 }, + { numBlocks: 4, dataCodewordsPerBlock: 15 }, + ], + }, + { + ecCodewordsPerBlock: 26, + ecBlocks: [ + { numBlocks: 4, dataCodewordsPerBlock: 13 }, + { numBlocks: 1, dataCodewordsPerBlock: 14 }, + ], + }, + ], + }, + { + infoBits: 0x085BC, + versionNumber: 8, + alignmentPatternCenters: [6, 24, 42], + errorCorrectionLevels: [ + { + ecCodewordsPerBlock: 24, + ecBlocks: [{ numBlocks: 2, dataCodewordsPerBlock: 97 }], + }, + { + ecCodewordsPerBlock: 22, + ecBlocks: [ + { numBlocks: 2, dataCodewordsPerBlock: 38 }, + { numBlocks: 2, dataCodewordsPerBlock: 39 }, + ], + }, + { + ecCodewordsPerBlock: 22, + ecBlocks: [ + { numBlocks: 4, dataCodewordsPerBlock: 18 }, + { numBlocks: 2, dataCodewordsPerBlock: 19 }, + ], + }, + { + ecCodewordsPerBlock: 26, + ecBlocks: [ + { numBlocks: 4, dataCodewordsPerBlock: 14 }, + { numBlocks: 2, dataCodewordsPerBlock: 15 }, + ], + }, + ], + }, + { + infoBits: 0x09A99, + versionNumber: 9, + alignmentPatternCenters: [6, 26, 46], + errorCorrectionLevels: [ + { + ecCodewordsPerBlock: 30, + ecBlocks: [{ numBlocks: 2, dataCodewordsPerBlock: 116 }], + }, + { + ecCodewordsPerBlock: 22, + ecBlocks: [ + { numBlocks: 3, dataCodewordsPerBlock: 36 }, + { numBlocks: 2, dataCodewordsPerBlock: 37 }, + ], + }, + { + ecCodewordsPerBlock: 20, + ecBlocks: [ + { numBlocks: 4, dataCodewordsPerBlock: 16 }, + { numBlocks: 4, dataCodewordsPerBlock: 17 }, + ], + }, + { + ecCodewordsPerBlock: 24, + ecBlocks: [ + { numBlocks: 4, dataCodewordsPerBlock: 12 }, + { numBlocks: 4, dataCodewordsPerBlock: 13 }, + ], + }, + ], + }, + { + infoBits: 0x0A4D3, + versionNumber: 10, + alignmentPatternCenters: [6, 28, 50], + errorCorrectionLevels: [ + { + ecCodewordsPerBlock: 18, + ecBlocks: [ + { numBlocks: 2, dataCodewordsPerBlock: 68 }, + { numBlocks: 2, dataCodewordsPerBlock: 69 }, + ], + }, + { + ecCodewordsPerBlock: 26, + ecBlocks: [ + { numBlocks: 4, dataCodewordsPerBlock: 43 }, + { numBlocks: 1, dataCodewordsPerBlock: 44 }, + ], + }, + { + ecCodewordsPerBlock: 24, + ecBlocks: [ + { numBlocks: 6, dataCodewordsPerBlock: 19 }, + { numBlocks: 2, dataCodewordsPerBlock: 20 }, + ], + }, + { + ecCodewordsPerBlock: 28, + ecBlocks: [ + { numBlocks: 6, dataCodewordsPerBlock: 15 }, + { numBlocks: 2, dataCodewordsPerBlock: 16 }, + ], + }, + ], + }, + { + infoBits: 0x0BBF6, + versionNumber: 11, + alignmentPatternCenters: [6, 30, 54], + errorCorrectionLevels: [ + { + ecCodewordsPerBlock: 20, + ecBlocks: [{ numBlocks: 4, dataCodewordsPerBlock: 81 }], + }, + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 1, dataCodewordsPerBlock: 50 }, + { numBlocks: 4, dataCodewordsPerBlock: 51 }, + ], + }, + { + ecCodewordsPerBlock: 28, + ecBlocks: [ + { numBlocks: 4, dataCodewordsPerBlock: 22 }, + { numBlocks: 4, dataCodewordsPerBlock: 23 }, + ], + }, + { + ecCodewordsPerBlock: 24, + ecBlocks: [ + { numBlocks: 3, dataCodewordsPerBlock: 12 }, + { numBlocks: 8, dataCodewordsPerBlock: 13 }, + ], + }, + ], + }, + { + infoBits: 0x0C762, + versionNumber: 12, + alignmentPatternCenters: [6, 32, 58], + errorCorrectionLevels: [ + { + ecCodewordsPerBlock: 24, + ecBlocks: [ + { numBlocks: 2, dataCodewordsPerBlock: 92 }, + { numBlocks: 2, dataCodewordsPerBlock: 93 }, + ], + }, + { + ecCodewordsPerBlock: 22, + ecBlocks: [ + { numBlocks: 6, dataCodewordsPerBlock: 36 }, + { numBlocks: 2, dataCodewordsPerBlock: 37 }, + ], + }, + { + ecCodewordsPerBlock: 26, + ecBlocks: [ + { numBlocks: 4, dataCodewordsPerBlock: 20 }, + { numBlocks: 6, dataCodewordsPerBlock: 21 }, + ], + }, + { + ecCodewordsPerBlock: 28, + ecBlocks: [ + { numBlocks: 7, dataCodewordsPerBlock: 14 }, + { numBlocks: 4, dataCodewordsPerBlock: 15 }, + ], + }, + ], + }, + { + infoBits: 0x0D847, + versionNumber: 13, + alignmentPatternCenters: [6, 34, 62], + errorCorrectionLevels: [ + { + ecCodewordsPerBlock: 26, + ecBlocks: [{ numBlocks: 4, dataCodewordsPerBlock: 107 }], + }, + { + ecCodewordsPerBlock: 22, + ecBlocks: [ + { numBlocks: 8, dataCodewordsPerBlock: 37 }, + { numBlocks: 1, dataCodewordsPerBlock: 38 }, + ], + }, + { + ecCodewordsPerBlock: 24, + ecBlocks: [ + { numBlocks: 8, dataCodewordsPerBlock: 20 }, + { numBlocks: 4, dataCodewordsPerBlock: 21 }, + ], + }, + { + ecCodewordsPerBlock: 22, + ecBlocks: [ + { numBlocks: 12, dataCodewordsPerBlock: 11 }, + { numBlocks: 4, dataCodewordsPerBlock: 12 }, + ], + }, + ], + }, + { + infoBits: 0x0E60D, + versionNumber: 14, + alignmentPatternCenters: [6, 26, 46, 66], + errorCorrectionLevels: [ + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 3, dataCodewordsPerBlock: 115 }, + { numBlocks: 1, dataCodewordsPerBlock: 116 }, + ], + }, + { + ecCodewordsPerBlock: 24, + ecBlocks: [ + { numBlocks: 4, dataCodewordsPerBlock: 40 }, + { numBlocks: 5, dataCodewordsPerBlock: 41 }, + ], + }, + { + ecCodewordsPerBlock: 20, + ecBlocks: [ + { numBlocks: 11, dataCodewordsPerBlock: 16 }, + { numBlocks: 5, dataCodewordsPerBlock: 17 }, + ], + }, + { + ecCodewordsPerBlock: 24, + ecBlocks: [ + { numBlocks: 11, dataCodewordsPerBlock: 12 }, + { numBlocks: 5, dataCodewordsPerBlock: 13 }, + ], + }, + ], + }, + { + infoBits: 0x0F928, + versionNumber: 15, + alignmentPatternCenters: [6, 26, 48, 70], + errorCorrectionLevels: [ + { + ecCodewordsPerBlock: 22, + ecBlocks: [ + { numBlocks: 5, dataCodewordsPerBlock: 87 }, + { numBlocks: 1, dataCodewordsPerBlock: 88 }, + ], + }, + { + ecCodewordsPerBlock: 24, + ecBlocks: [ + { numBlocks: 5, dataCodewordsPerBlock: 41 }, + { numBlocks: 5, dataCodewordsPerBlock: 42 }, + ], + }, + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 5, dataCodewordsPerBlock: 24 }, + { numBlocks: 7, dataCodewordsPerBlock: 25 }, + ], + }, + { + ecCodewordsPerBlock: 24, + ecBlocks: [ + { numBlocks: 11, dataCodewordsPerBlock: 12 }, + { numBlocks: 7, dataCodewordsPerBlock: 13 }, + ], + }, + ], + }, + { + infoBits: 0x10B78, + versionNumber: 16, + alignmentPatternCenters: [6, 26, 50, 74], + errorCorrectionLevels: [ + { + ecCodewordsPerBlock: 24, + ecBlocks: [ + { numBlocks: 5, dataCodewordsPerBlock: 98 }, + { numBlocks: 1, dataCodewordsPerBlock: 99 }, + ], + }, + { + ecCodewordsPerBlock: 28, + ecBlocks: [ + { numBlocks: 7, dataCodewordsPerBlock: 45 }, + { numBlocks: 3, dataCodewordsPerBlock: 46 }, + ], + }, + { + ecCodewordsPerBlock: 24, + ecBlocks: [ + { numBlocks: 15, dataCodewordsPerBlock: 19 }, + { numBlocks: 2, dataCodewordsPerBlock: 20 }, + ], + }, + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 3, dataCodewordsPerBlock: 15 }, + { numBlocks: 13, dataCodewordsPerBlock: 16 }, + ], + }, + ], + }, + { + infoBits: 0x1145D, + versionNumber: 17, + alignmentPatternCenters: [6, 30, 54, 78], + errorCorrectionLevels: [ + { + ecCodewordsPerBlock: 28, + ecBlocks: [ + { numBlocks: 1, dataCodewordsPerBlock: 107 }, + { numBlocks: 5, dataCodewordsPerBlock: 108 }, + ], + }, + { + ecCodewordsPerBlock: 28, + ecBlocks: [ + { numBlocks: 10, dataCodewordsPerBlock: 46 }, + { numBlocks: 1, dataCodewordsPerBlock: 47 }, + ], + }, + { + ecCodewordsPerBlock: 28, + ecBlocks: [ + { numBlocks: 1, dataCodewordsPerBlock: 22 }, + { numBlocks: 15, dataCodewordsPerBlock: 23 }, + ], + }, + { + ecCodewordsPerBlock: 28, + ecBlocks: [ + { numBlocks: 2, dataCodewordsPerBlock: 14 }, + { numBlocks: 17, dataCodewordsPerBlock: 15 }, + ], + }, + ], + }, + { + infoBits: 0x12A17, + versionNumber: 18, + alignmentPatternCenters: [6, 30, 56, 82], + errorCorrectionLevels: [ + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 5, dataCodewordsPerBlock: 120 }, + { numBlocks: 1, dataCodewordsPerBlock: 121 }, + ], + }, + { + ecCodewordsPerBlock: 26, + ecBlocks: [ + { numBlocks: 9, dataCodewordsPerBlock: 43 }, + { numBlocks: 4, dataCodewordsPerBlock: 44 }, + ], + }, + { + ecCodewordsPerBlock: 28, + ecBlocks: [ + { numBlocks: 17, dataCodewordsPerBlock: 22 }, + { numBlocks: 1, dataCodewordsPerBlock: 23 }, + ], + }, + { + ecCodewordsPerBlock: 28, + ecBlocks: [ + { numBlocks: 2, dataCodewordsPerBlock: 14 }, + { numBlocks: 19, dataCodewordsPerBlock: 15 }, + ], + }, + ], + }, + { + infoBits: 0x13532, + versionNumber: 19, + alignmentPatternCenters: [6, 30, 58, 86], + errorCorrectionLevels: [ + { + ecCodewordsPerBlock: 28, + ecBlocks: [ + { numBlocks: 3, dataCodewordsPerBlock: 113 }, + { numBlocks: 4, dataCodewordsPerBlock: 114 }, + ], + }, + { + ecCodewordsPerBlock: 26, + ecBlocks: [ + { numBlocks: 3, dataCodewordsPerBlock: 44 }, + { numBlocks: 11, dataCodewordsPerBlock: 45 }, + ], + }, + { + ecCodewordsPerBlock: 26, + ecBlocks: [ + { numBlocks: 17, dataCodewordsPerBlock: 21 }, + { numBlocks: 4, dataCodewordsPerBlock: 22 }, + ], + }, + { + ecCodewordsPerBlock: 26, + ecBlocks: [ + { numBlocks: 9, dataCodewordsPerBlock: 13 }, + { numBlocks: 16, dataCodewordsPerBlock: 14 }, + ], + }, + ], + }, + { + infoBits: 0x149A6, + versionNumber: 20, + alignmentPatternCenters: [6, 34, 62, 90], + errorCorrectionLevels: [ + { + ecCodewordsPerBlock: 28, + ecBlocks: [ + { numBlocks: 3, dataCodewordsPerBlock: 107 }, + { numBlocks: 5, dataCodewordsPerBlock: 108 }, + ], + }, + { + ecCodewordsPerBlock: 26, + ecBlocks: [ + { numBlocks: 3, dataCodewordsPerBlock: 41 }, + { numBlocks: 13, dataCodewordsPerBlock: 42 }, + ], + }, + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 15, dataCodewordsPerBlock: 24 }, + { numBlocks: 5, dataCodewordsPerBlock: 25 }, + ], + }, + { + ecCodewordsPerBlock: 28, + ecBlocks: [ + { numBlocks: 15, dataCodewordsPerBlock: 15 }, + { numBlocks: 10, dataCodewordsPerBlock: 16 }, + ], + }, + ], + }, + { + infoBits: 0x15683, + versionNumber: 21, + alignmentPatternCenters: [6, 28, 50, 72, 94], + errorCorrectionLevels: [ + { + ecCodewordsPerBlock: 28, + ecBlocks: [ + { numBlocks: 4, dataCodewordsPerBlock: 116 }, + { numBlocks: 4, dataCodewordsPerBlock: 117 }, + ], + }, + { + ecCodewordsPerBlock: 26, + ecBlocks: [{ numBlocks: 17, dataCodewordsPerBlock: 42 }], + }, + { + ecCodewordsPerBlock: 28, + ecBlocks: [ + { numBlocks: 17, dataCodewordsPerBlock: 22 }, + { numBlocks: 6, dataCodewordsPerBlock: 23 }, + ], + }, + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 19, dataCodewordsPerBlock: 16 }, + { numBlocks: 6, dataCodewordsPerBlock: 17 }, + ], + }, + ], + }, + { + infoBits: 0x168C9, + versionNumber: 22, + alignmentPatternCenters: [6, 26, 50, 74, 98], + errorCorrectionLevels: [ + { + ecCodewordsPerBlock: 28, + ecBlocks: [ + { numBlocks: 2, dataCodewordsPerBlock: 111 }, + { numBlocks: 7, dataCodewordsPerBlock: 112 }, + ], + }, + { + ecCodewordsPerBlock: 28, + ecBlocks: [{ numBlocks: 17, dataCodewordsPerBlock: 46 }], + }, + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 7, dataCodewordsPerBlock: 24 }, + { numBlocks: 16, dataCodewordsPerBlock: 25 }, + ], + }, + { + ecCodewordsPerBlock: 24, + ecBlocks: [{ numBlocks: 34, dataCodewordsPerBlock: 13 }], + }, + ], + }, + { + infoBits: 0x177EC, + versionNumber: 23, + alignmentPatternCenters: [6, 30, 54, 74, 102], + errorCorrectionLevels: [ + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 4, dataCodewordsPerBlock: 121 }, + { numBlocks: 5, dataCodewordsPerBlock: 122 }, + ], + }, + { + ecCodewordsPerBlock: 28, + ecBlocks: [ + { numBlocks: 4, dataCodewordsPerBlock: 47 }, + { numBlocks: 14, dataCodewordsPerBlock: 48 }, + ], + }, + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 11, dataCodewordsPerBlock: 24 }, + { numBlocks: 14, dataCodewordsPerBlock: 25 }, + ], + }, + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 16, dataCodewordsPerBlock: 15 }, + { numBlocks: 14, dataCodewordsPerBlock: 16 }, + ], + }, + ], + }, + { + infoBits: 0x18EC4, + versionNumber: 24, + alignmentPatternCenters: [6, 28, 54, 80, 106], + errorCorrectionLevels: [ + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 6, dataCodewordsPerBlock: 117 }, + { numBlocks: 4, dataCodewordsPerBlock: 118 }, + ], + }, + { + ecCodewordsPerBlock: 28, + ecBlocks: [ + { numBlocks: 6, dataCodewordsPerBlock: 45 }, + { numBlocks: 14, dataCodewordsPerBlock: 46 }, + ], + }, + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 11, dataCodewordsPerBlock: 24 }, + { numBlocks: 16, dataCodewordsPerBlock: 25 }, + ], + }, + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 30, dataCodewordsPerBlock: 16 }, + { numBlocks: 2, dataCodewordsPerBlock: 17 }, + ], + }, + ], + }, + { + infoBits: 0x191E1, + versionNumber: 25, + alignmentPatternCenters: [6, 32, 58, 84, 110], + errorCorrectionLevels: [ + { + ecCodewordsPerBlock: 26, + ecBlocks: [ + { numBlocks: 8, dataCodewordsPerBlock: 106 }, + { numBlocks: 4, dataCodewordsPerBlock: 107 }, + ], + }, + { + ecCodewordsPerBlock: 28, + ecBlocks: [ + { numBlocks: 8, dataCodewordsPerBlock: 47 }, + { numBlocks: 13, dataCodewordsPerBlock: 48 }, + ], + }, + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 7, dataCodewordsPerBlock: 24 }, + { numBlocks: 22, dataCodewordsPerBlock: 25 }, + ], + }, + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 22, dataCodewordsPerBlock: 15 }, + { numBlocks: 13, dataCodewordsPerBlock: 16 }, + ], + }, + ], + }, + { + infoBits: 0x1AFAB, + versionNumber: 26, + alignmentPatternCenters: [6, 30, 58, 86, 114], + errorCorrectionLevels: [ + { + ecCodewordsPerBlock: 28, + ecBlocks: [ + { numBlocks: 10, dataCodewordsPerBlock: 114 }, + { numBlocks: 2, dataCodewordsPerBlock: 115 }, + ], + }, + { + ecCodewordsPerBlock: 28, + ecBlocks: [ + { numBlocks: 19, dataCodewordsPerBlock: 46 }, + { numBlocks: 4, dataCodewordsPerBlock: 47 }, + ], + }, + { + ecCodewordsPerBlock: 28, + ecBlocks: [ + { numBlocks: 28, dataCodewordsPerBlock: 22 }, + { numBlocks: 6, dataCodewordsPerBlock: 23 }, + ], + }, + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 33, dataCodewordsPerBlock: 16 }, + { numBlocks: 4, dataCodewordsPerBlock: 17 }, + ], + }, + ], + }, + { + infoBits: 0x1B08E, + versionNumber: 27, + alignmentPatternCenters: [6, 34, 62, 90, 118], + errorCorrectionLevels: [ + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 8, dataCodewordsPerBlock: 122 }, + { numBlocks: 4, dataCodewordsPerBlock: 123 }, + ], + }, + { + ecCodewordsPerBlock: 28, + ecBlocks: [ + { numBlocks: 22, dataCodewordsPerBlock: 45 }, + { numBlocks: 3, dataCodewordsPerBlock: 46 }, + ], + }, + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 8, dataCodewordsPerBlock: 23 }, + { numBlocks: 26, dataCodewordsPerBlock: 24 }, + ], + }, + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 12, dataCodewordsPerBlock: 15 }, + { numBlocks: 28, dataCodewordsPerBlock: 16 }, + ], + }, + ], + }, + { + infoBits: 0x1CC1A, + versionNumber: 28, + alignmentPatternCenters: [6, 26, 50, 74, 98, 122], + errorCorrectionLevels: [ + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 3, dataCodewordsPerBlock: 117 }, + { numBlocks: 10, dataCodewordsPerBlock: 118 }, + ], + }, + { + ecCodewordsPerBlock: 28, + ecBlocks: [ + { numBlocks: 3, dataCodewordsPerBlock: 45 }, + { numBlocks: 23, dataCodewordsPerBlock: 46 }, + ], + }, + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 4, dataCodewordsPerBlock: 24 }, + { numBlocks: 31, dataCodewordsPerBlock: 25 }, + ], + }, + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 11, dataCodewordsPerBlock: 15 }, + { numBlocks: 31, dataCodewordsPerBlock: 16 }, + ], + }, + ], + }, + { + infoBits: 0x1D33F, + versionNumber: 29, + alignmentPatternCenters: [6, 30, 54, 78, 102, 126], + errorCorrectionLevels: [ + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 7, dataCodewordsPerBlock: 116 }, + { numBlocks: 7, dataCodewordsPerBlock: 117 }, + ], + }, + { + ecCodewordsPerBlock: 28, + ecBlocks: [ + { numBlocks: 21, dataCodewordsPerBlock: 45 }, + { numBlocks: 7, dataCodewordsPerBlock: 46 }, + ], + }, + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 1, dataCodewordsPerBlock: 23 }, + { numBlocks: 37, dataCodewordsPerBlock: 24 }, + ], + }, + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 19, dataCodewordsPerBlock: 15 }, + { numBlocks: 26, dataCodewordsPerBlock: 16 }, + ], + }, + ], + }, + { + infoBits: 0x1ED75, + versionNumber: 30, + alignmentPatternCenters: [6, 26, 52, 78, 104, 130], + errorCorrectionLevels: [ + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 5, dataCodewordsPerBlock: 115 }, + { numBlocks: 10, dataCodewordsPerBlock: 116 }, + ], + }, + { + ecCodewordsPerBlock: 28, + ecBlocks: [ + { numBlocks: 19, dataCodewordsPerBlock: 47 }, + { numBlocks: 10, dataCodewordsPerBlock: 48 }, + ], + }, + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 15, dataCodewordsPerBlock: 24 }, + { numBlocks: 25, dataCodewordsPerBlock: 25 }, + ], + }, + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 23, dataCodewordsPerBlock: 15 }, + { numBlocks: 25, dataCodewordsPerBlock: 16 }, + ], + }, + ], + }, + { + infoBits: 0x1F250, + versionNumber: 31, + alignmentPatternCenters: [6, 30, 56, 82, 108, 134], + errorCorrectionLevels: [ + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 13, dataCodewordsPerBlock: 115 }, + { numBlocks: 3, dataCodewordsPerBlock: 116 }, + ], + }, + { + ecCodewordsPerBlock: 28, + ecBlocks: [ + { numBlocks: 2, dataCodewordsPerBlock: 46 }, + { numBlocks: 29, dataCodewordsPerBlock: 47 }, + ], + }, + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 42, dataCodewordsPerBlock: 24 }, + { numBlocks: 1, dataCodewordsPerBlock: 25 }, + ], + }, + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 23, dataCodewordsPerBlock: 15 }, + { numBlocks: 28, dataCodewordsPerBlock: 16 }, + ], + }, + ], + }, + { + infoBits: 0x209D5, + versionNumber: 32, + alignmentPatternCenters: [6, 34, 60, 86, 112, 138], + errorCorrectionLevels: [ + { + ecCodewordsPerBlock: 30, + ecBlocks: [{ numBlocks: 17, dataCodewordsPerBlock: 115 }], + }, + { + ecCodewordsPerBlock: 28, + ecBlocks: [ + { numBlocks: 10, dataCodewordsPerBlock: 46 }, + { numBlocks: 23, dataCodewordsPerBlock: 47 }, + ], + }, + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 10, dataCodewordsPerBlock: 24 }, + { numBlocks: 35, dataCodewordsPerBlock: 25 }, + ], + }, + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 19, dataCodewordsPerBlock: 15 }, + { numBlocks: 35, dataCodewordsPerBlock: 16 }, + ], + }, + ], + }, + { + infoBits: 0x216F0, + versionNumber: 33, + alignmentPatternCenters: [6, 30, 58, 86, 114, 142], + errorCorrectionLevels: [ + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 17, dataCodewordsPerBlock: 115 }, + { numBlocks: 1, dataCodewordsPerBlock: 116 }, + ], + }, + { + ecCodewordsPerBlock: 28, + ecBlocks: [ + { numBlocks: 14, dataCodewordsPerBlock: 46 }, + { numBlocks: 21, dataCodewordsPerBlock: 47 }, + ], + }, + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 29, dataCodewordsPerBlock: 24 }, + { numBlocks: 19, dataCodewordsPerBlock: 25 }, + ], + }, + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 11, dataCodewordsPerBlock: 15 }, + { numBlocks: 46, dataCodewordsPerBlock: 16 }, + ], + }, + ], + }, + { + infoBits: 0x228BA, + versionNumber: 34, + alignmentPatternCenters: [6, 34, 62, 90, 118, 146], + errorCorrectionLevels: [ + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 13, dataCodewordsPerBlock: 115 }, + { numBlocks: 6, dataCodewordsPerBlock: 116 }, + ], + }, + { + ecCodewordsPerBlock: 28, + ecBlocks: [ + { numBlocks: 14, dataCodewordsPerBlock: 46 }, + { numBlocks: 23, dataCodewordsPerBlock: 47 }, + ], + }, + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 44, dataCodewordsPerBlock: 24 }, + { numBlocks: 7, dataCodewordsPerBlock: 25 }, + ], + }, + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 59, dataCodewordsPerBlock: 16 }, + { numBlocks: 1, dataCodewordsPerBlock: 17 }, + ], + }, + ], + }, + { + infoBits: 0x2379F, + versionNumber: 35, + alignmentPatternCenters: [6, 30, 54, 78, 102, 126, 150], + errorCorrectionLevels: [ + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 12, dataCodewordsPerBlock: 121 }, + { numBlocks: 7, dataCodewordsPerBlock: 122 }, + ], + }, + { + ecCodewordsPerBlock: 28, + ecBlocks: [ + { numBlocks: 12, dataCodewordsPerBlock: 47 }, + { numBlocks: 26, dataCodewordsPerBlock: 48 }, + ], + }, + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 39, dataCodewordsPerBlock: 24 }, + { numBlocks: 14, dataCodewordsPerBlock: 25 }, + ], + }, + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 22, dataCodewordsPerBlock: 15 }, + { numBlocks: 41, dataCodewordsPerBlock: 16 }, + ], + }, + ], + }, + { + infoBits: 0x24B0B, + versionNumber: 36, + alignmentPatternCenters: [6, 24, 50, 76, 102, 128, 154], + errorCorrectionLevels: [ + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 6, dataCodewordsPerBlock: 121 }, + { numBlocks: 14, dataCodewordsPerBlock: 122 }, + ], + }, + { + ecCodewordsPerBlock: 28, + ecBlocks: [ + { numBlocks: 6, dataCodewordsPerBlock: 47 }, + { numBlocks: 34, dataCodewordsPerBlock: 48 }, + ], + }, + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 46, dataCodewordsPerBlock: 24 }, + { numBlocks: 10, dataCodewordsPerBlock: 25 }, + ], + }, + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 2, dataCodewordsPerBlock: 15 }, + { numBlocks: 64, dataCodewordsPerBlock: 16 }, + ], + }, + ], + }, + { + infoBits: 0x2542E, + versionNumber: 37, + alignmentPatternCenters: [6, 28, 54, 80, 106, 132, 158], + errorCorrectionLevels: [ + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 17, dataCodewordsPerBlock: 122 }, + { numBlocks: 4, dataCodewordsPerBlock: 123 }, + ], + }, + { + ecCodewordsPerBlock: 28, + ecBlocks: [ + { numBlocks: 29, dataCodewordsPerBlock: 46 }, + { numBlocks: 14, dataCodewordsPerBlock: 47 }, + ], + }, + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 49, dataCodewordsPerBlock: 24 }, + { numBlocks: 10, dataCodewordsPerBlock: 25 }, + ], + }, + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 24, dataCodewordsPerBlock: 15 }, + { numBlocks: 46, dataCodewordsPerBlock: 16 }, + ], + }, + ], + }, + { + infoBits: 0x26A64, + versionNumber: 38, + alignmentPatternCenters: [6, 32, 58, 84, 110, 136, 162], + errorCorrectionLevels: [ + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 4, dataCodewordsPerBlock: 122 }, + { numBlocks: 18, dataCodewordsPerBlock: 123 }, + ], + }, + { + ecCodewordsPerBlock: 28, + ecBlocks: [ + { numBlocks: 13, dataCodewordsPerBlock: 46 }, + { numBlocks: 32, dataCodewordsPerBlock: 47 }, + ], + }, + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 48, dataCodewordsPerBlock: 24 }, + { numBlocks: 14, dataCodewordsPerBlock: 25 }, + ], + }, + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 42, dataCodewordsPerBlock: 15 }, + { numBlocks: 32, dataCodewordsPerBlock: 16 }, + ], + }, + ], + }, + { + infoBits: 0x27541, + versionNumber: 39, + alignmentPatternCenters: [6, 26, 54, 82, 110, 138, 166], + errorCorrectionLevels: [ + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 20, dataCodewordsPerBlock: 117 }, + { numBlocks: 4, dataCodewordsPerBlock: 118 }, + ], + }, + { + ecCodewordsPerBlock: 28, + ecBlocks: [ + { numBlocks: 40, dataCodewordsPerBlock: 47 }, + { numBlocks: 7, dataCodewordsPerBlock: 48 }, + ], + }, + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 43, dataCodewordsPerBlock: 24 }, + { numBlocks: 22, dataCodewordsPerBlock: 25 }, + ], + }, + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 10, dataCodewordsPerBlock: 15 }, + { numBlocks: 67, dataCodewordsPerBlock: 16 }, + ], + }, + ], + }, + { + infoBits: 0x28C69, + versionNumber: 40, + alignmentPatternCenters: [6, 30, 58, 86, 114, 142, 170], + errorCorrectionLevels: [ + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 19, dataCodewordsPerBlock: 118 }, + { numBlocks: 6, dataCodewordsPerBlock: 119 }, + ], + }, + { + ecCodewordsPerBlock: 28, + ecBlocks: [ + { numBlocks: 18, dataCodewordsPerBlock: 47 }, + { numBlocks: 31, dataCodewordsPerBlock: 48 }, + ], + }, + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 34, dataCodewordsPerBlock: 24 }, + { numBlocks: 34, dataCodewordsPerBlock: 25 }, + ], + }, + { + ecCodewordsPerBlock: 30, + ecBlocks: [ + { numBlocks: 20, dataCodewordsPerBlock: 15 }, + { numBlocks: 61, dataCodewordsPerBlock: 16 }, + ], + }, + ], + }, +]; + + +/***/ }), +/* 11 */ +/***/ (function(module, exports, __webpack_require__) { + +"use strict"; + +Object.defineProperty(exports, "__esModule", { value: true }); +var BitMatrix_1 = __webpack_require__(0); +function squareToQuadrilateral(p1, p2, p3, p4) { + var dx3 = p1.x - p2.x + p3.x - p4.x; + var dy3 = p1.y - p2.y + p3.y - p4.y; + if (dx3 === 0 && dy3 === 0) { // Affine + return { + a11: p2.x - p1.x, + a12: p2.y - p1.y, + a13: 0, + a21: p3.x - p2.x, + a22: p3.y - p2.y, + a23: 0, + a31: p1.x, + a32: p1.y, + a33: 1, + }; + } + else { + var dx1 = p2.x - p3.x; + var dx2 = p4.x - p3.x; + var dy1 = p2.y - p3.y; + var dy2 = p4.y - p3.y; + var denominator = dx1 * dy2 - dx2 * dy1; + var a13 = (dx3 * dy2 - dx2 * dy3) / denominator; + var a23 = (dx1 * dy3 - dx3 * dy1) / denominator; + return { + a11: p2.x - p1.x + a13 * p2.x, + a12: p2.y - p1.y + a13 * p2.y, + a13: a13, + a21: p4.x - p1.x + a23 * p4.x, + a22: p4.y - p1.y + a23 * p4.y, + a23: a23, + a31: p1.x, + a32: p1.y, + a33: 1, + }; + } +} +function quadrilateralToSquare(p1, p2, p3, p4) { + // Here, the adjoint serves as the inverse: + var sToQ = squareToQuadrilateral(p1, p2, p3, p4); + return { + a11: sToQ.a22 * sToQ.a33 - sToQ.a23 * sToQ.a32, + a12: sToQ.a13 * sToQ.a32 - sToQ.a12 * sToQ.a33, + a13: sToQ.a12 * sToQ.a23 - sToQ.a13 * sToQ.a22, + a21: sToQ.a23 * sToQ.a31 - sToQ.a21 * sToQ.a33, + a22: sToQ.a11 * sToQ.a33 - sToQ.a13 * sToQ.a31, + a23: sToQ.a13 * sToQ.a21 - sToQ.a11 * sToQ.a23, + a31: sToQ.a21 * sToQ.a32 - sToQ.a22 * sToQ.a31, + a32: sToQ.a12 * sToQ.a31 - sToQ.a11 * sToQ.a32, + a33: sToQ.a11 * sToQ.a22 - sToQ.a12 * sToQ.a21, + }; +} +function times(a, b) { + return { + a11: a.a11 * b.a11 + a.a21 * b.a12 + a.a31 * b.a13, + a12: a.a12 * b.a11 + a.a22 * b.a12 + a.a32 * b.a13, + a13: a.a13 * b.a11 + a.a23 * b.a12 + a.a33 * b.a13, + a21: a.a11 * b.a21 + a.a21 * b.a22 + a.a31 * b.a23, + a22: a.a12 * b.a21 + a.a22 * b.a22 + a.a32 * b.a23, + a23: a.a13 * b.a21 + a.a23 * b.a22 + a.a33 * b.a23, + a31: a.a11 * b.a31 + a.a21 * b.a32 + a.a31 * b.a33, + a32: a.a12 * b.a31 + a.a22 * b.a32 + a.a32 * b.a33, + a33: a.a13 * b.a31 + a.a23 * b.a32 + a.a33 * b.a33, + }; +} +function extract(image, location) { + var qToS = quadrilateralToSquare({ x: 3.5, y: 3.5 }, { x: location.dimension - 3.5, y: 3.5 }, { x: location.dimension - 6.5, y: location.dimension - 6.5 }, { x: 3.5, y: location.dimension - 3.5 }); + var sToQ = squareToQuadrilateral(location.topLeft, location.topRight, location.alignmentPattern, location.bottomLeft); + var transform = times(sToQ, qToS); + var matrix = BitMatrix_1.BitMatrix.createEmpty(location.dimension, location.dimension); + var mappingFunction = function (x, y) { + var denominator = transform.a13 * x + transform.a23 * y + transform.a33; + return { + x: (transform.a11 * x + transform.a21 * y + transform.a31) / denominator, + y: (transform.a12 * x + transform.a22 * y + transform.a32) / denominator, + }; + }; + for (var y = 0; y < location.dimension; y++) { + for (var x = 0; x < location.dimension; x++) { + var xValue = x + 0.5; + var yValue = y + 0.5; + var sourcePixel = mappingFunction(xValue, yValue); + matrix.set(x, y, image.get(Math.floor(sourcePixel.x), Math.floor(sourcePixel.y))); + } + } + return { + matrix: matrix, + mappingFunction: mappingFunction, + }; +} +exports.extract = extract; + + +/***/ }), +/* 12 */ +/***/ (function(module, exports, __webpack_require__) { + +"use strict"; + +Object.defineProperty(exports, "__esModule", { value: true }); +var MAX_FINDERPATTERNS_TO_SEARCH = 4; +var MIN_QUAD_RATIO = 0.5; +var MAX_QUAD_RATIO = 1.5; +var distance = function (a, b) { return Math.sqrt(Math.pow((b.x - a.x), 2) + Math.pow((b.y - a.y), 2)); }; +function sum(values) { + return values.reduce(function (a, b) { return a + b; }); +} +// Takes three finder patterns and organizes them into topLeft, topRight, etc +function reorderFinderPatterns(pattern1, pattern2, pattern3) { + var _a, _b, _c, _d; + // Find distances between pattern centers + var oneTwoDistance = distance(pattern1, pattern2); + var twoThreeDistance = distance(pattern2, pattern3); + var oneThreeDistance = distance(pattern1, pattern3); + var bottomLeft; + var topLeft; + var topRight; + // Assume one closest to other two is B; A and C will just be guesses at first + if (twoThreeDistance >= oneTwoDistance && twoThreeDistance >= oneThreeDistance) { + _a = [pattern2, pattern1, pattern3], bottomLeft = _a[0], topLeft = _a[1], topRight = _a[2]; + } + else if (oneThreeDistance >= twoThreeDistance && oneThreeDistance >= oneTwoDistance) { + _b = [pattern1, pattern2, pattern3], bottomLeft = _b[0], topLeft = _b[1], topRight = _b[2]; + } + else { + _c = [pattern1, pattern3, pattern2], bottomLeft = _c[0], topLeft = _c[1], topRight = _c[2]; + } + // Use cross product to figure out whether bottomLeft (A) and topRight (C) are correct or flipped in relation to topLeft (B) + // This asks whether BC x BA has a positive z component, which is the arrangement we want. If it's negative, then + // we've got it flipped around and should swap topRight and bottomLeft. + if (((topRight.x - topLeft.x) * (bottomLeft.y - topLeft.y)) - ((topRight.y - topLeft.y) * (bottomLeft.x - topLeft.x)) < 0) { + _d = [topRight, bottomLeft], bottomLeft = _d[0], topRight = _d[1]; + } + return { bottomLeft: bottomLeft, topLeft: topLeft, topRight: topRight }; +} +// Computes the dimension (number of modules on a side) of the QR Code based on the position of the finder patterns +function computeDimension(topLeft, topRight, bottomLeft, matrix) { + var moduleSize = (sum(countBlackWhiteRun(topLeft, bottomLeft, matrix, 5)) / 7 + // Divide by 7 since the ratio is 1:1:3:1:1 + sum(countBlackWhiteRun(topLeft, topRight, matrix, 5)) / 7 + + sum(countBlackWhiteRun(bottomLeft, topLeft, matrix, 5)) / 7 + + sum(countBlackWhiteRun(topRight, topLeft, matrix, 5)) / 7) / 4; + if (moduleSize < 1) { + throw new Error("Invalid module size"); + } + var topDimension = Math.round(distance(topLeft, topRight) / moduleSize); + var sideDimension = Math.round(distance(topLeft, bottomLeft) / moduleSize); + var dimension = Math.floor((topDimension + sideDimension) / 2) + 7; + switch (dimension % 4) { + case 0: + dimension++; + break; + case 2: + dimension--; + break; + } + return { dimension: dimension, moduleSize: moduleSize }; +} +// Takes an origin point and an end point and counts the sizes of the black white run from the origin towards the end point. +// Returns an array of elements, representing the pixel size of the black white run. +// Uses a variant of http://en.wikipedia.org/wiki/Bresenham's_line_algorithm +function countBlackWhiteRunTowardsPoint(origin, end, matrix, length) { + var switchPoints = [{ x: Math.floor(origin.x), y: Math.floor(origin.y) }]; + var steep = Math.abs(end.y - origin.y) > Math.abs(end.x - origin.x); + var fromX; + var fromY; + var toX; + var toY; + if (steep) { + fromX = Math.floor(origin.y); + fromY = Math.floor(origin.x); + toX = Math.floor(end.y); + toY = Math.floor(end.x); + } + else { + fromX = Math.floor(origin.x); + fromY = Math.floor(origin.y); + toX = Math.floor(end.x); + toY = Math.floor(end.y); + } + var dx = Math.abs(toX - fromX); + var dy = Math.abs(toY - fromY); + var error = Math.floor(-dx / 2); + var xStep = fromX < toX ? 1 : -1; + var yStep = fromY < toY ? 1 : -1; + var currentPixel = true; + // Loop up until x == toX, but not beyond + for (var x = fromX, y = fromY; x !== toX + xStep; x += xStep) { + // Does current pixel mean we have moved white to black or vice versa? + // Scanning black in state 0,2 and white in state 1, so if we find the wrong + // color, advance to next state or end if we are in state 2 already + var realX = steep ? y : x; + var realY = steep ? x : y; + if (matrix.get(realX, realY) !== currentPixel) { + currentPixel = !currentPixel; + switchPoints.push({ x: realX, y: realY }); + if (switchPoints.length === length + 1) { + break; + } + } + error += dy; + if (error > 0) { + if (y === toY) { + break; + } + y += yStep; + error -= dx; + } + } + var distances = []; + for (var i = 0; i < length; i++) { + if (switchPoints[i] && switchPoints[i + 1]) { + distances.push(distance(switchPoints[i], switchPoints[i + 1])); + } + else { + distances.push(0); + } + } + return distances; +} +// Takes an origin point and an end point and counts the sizes of the black white run in the origin point +// along the line that intersects with the end point. Returns an array of elements, representing the pixel sizes +// of the black white run. Takes a length which represents the number of switches from black to white to look for. +function countBlackWhiteRun(origin, end, matrix, length) { + var _a; + var rise = end.y - origin.y; + var run = end.x - origin.x; + var towardsEnd = countBlackWhiteRunTowardsPoint(origin, end, matrix, Math.ceil(length / 2)); + var awayFromEnd = countBlackWhiteRunTowardsPoint(origin, { x: origin.x - run, y: origin.y - rise }, matrix, Math.ceil(length / 2)); + var middleValue = towardsEnd.shift() + awayFromEnd.shift() - 1; // Substract one so we don't double count a pixel + return (_a = awayFromEnd.concat(middleValue)).concat.apply(_a, towardsEnd); +} +// Takes in a black white run and an array of expected ratios. Returns the average size of the run as well as the "error" - +// that is the amount the run diverges from the expected ratio +function scoreBlackWhiteRun(sequence, ratios) { + var averageSize = sum(sequence) / sum(ratios); + var error = 0; + ratios.forEach(function (ratio, i) { + error += Math.pow((sequence[i] - ratio * averageSize), 2); + }); + return { averageSize: averageSize, error: error }; +} +// Takes an X,Y point and an array of sizes and scores the point against those ratios. +// For example for a finder pattern takes the ratio list of 1:1:3:1:1 and checks horizontal, vertical and diagonal ratios +// against that. +function scorePattern(point, ratios, matrix) { + try { + var horizontalRun = countBlackWhiteRun(point, { x: -1, y: point.y }, matrix, ratios.length); + var verticalRun = countBlackWhiteRun(point, { x: point.x, y: -1 }, matrix, ratios.length); + var topLeftPoint = { + x: Math.max(0, point.x - point.y) - 1, + y: Math.max(0, point.y - point.x) - 1, + }; + var topLeftBottomRightRun = countBlackWhiteRun(point, topLeftPoint, matrix, ratios.length); + var bottomLeftPoint = { + x: Math.min(matrix.width, point.x + point.y) + 1, + y: Math.min(matrix.height, point.y + point.x) + 1, + }; + var bottomLeftTopRightRun = countBlackWhiteRun(point, bottomLeftPoint, matrix, ratios.length); + var horzError = scoreBlackWhiteRun(horizontalRun, ratios); + var vertError = scoreBlackWhiteRun(verticalRun, ratios); + var diagDownError = scoreBlackWhiteRun(topLeftBottomRightRun, ratios); + var diagUpError = scoreBlackWhiteRun(bottomLeftTopRightRun, ratios); + var ratioError = Math.sqrt(horzError.error * horzError.error + + vertError.error * vertError.error + + diagDownError.error * diagDownError.error + + diagUpError.error * diagUpError.error); + var avgSize = (horzError.averageSize + vertError.averageSize + diagDownError.averageSize + diagUpError.averageSize) / 4; + var sizeError = (Math.pow((horzError.averageSize - avgSize), 2) + + Math.pow((vertError.averageSize - avgSize), 2) + + Math.pow((diagDownError.averageSize - avgSize), 2) + + Math.pow((diagUpError.averageSize - avgSize), 2)) / avgSize; + return ratioError + sizeError; + } + catch (_a) { + return Infinity; + } +} +function recenterLocation(matrix, p) { + var leftX = Math.round(p.x); + while (matrix.get(leftX, Math.round(p.y))) { + leftX--; + } + var rightX = Math.round(p.x); + while (matrix.get(rightX, Math.round(p.y))) { + rightX++; + } + var x = (leftX + rightX) / 2; + var topY = Math.round(p.y); + while (matrix.get(Math.round(x), topY)) { + topY--; + } + var bottomY = Math.round(p.y); + while (matrix.get(Math.round(x), bottomY)) { + bottomY++; + } + var y = (topY + bottomY) / 2; + return { x: x, y: y }; +} +function locate(matrix) { + var finderPatternQuads = []; + var activeFinderPatternQuads = []; + var alignmentPatternQuads = []; + var activeAlignmentPatternQuads = []; + var _loop_1 = function (y) { + var length_1 = 0; + var lastBit = false; + var scans = [0, 0, 0, 0, 0]; + var _loop_2 = function (x) { + var v = matrix.get(x, y); + if (v === lastBit) { + length_1++; + } + else { + scans = [scans[1], scans[2], scans[3], scans[4], length_1]; + length_1 = 1; + lastBit = v; + // Do the last 5 color changes ~ match the expected ratio for a finder pattern? 1:1:3:1:1 of b:w:b:w:b + var averageFinderPatternBlocksize = sum(scans) / 7; + var validFinderPattern = Math.abs(scans[0] - averageFinderPatternBlocksize) < averageFinderPatternBlocksize && + Math.abs(scans[1] - averageFinderPatternBlocksize) < averageFinderPatternBlocksize && + Math.abs(scans[2] - 3 * averageFinderPatternBlocksize) < 3 * averageFinderPatternBlocksize && + Math.abs(scans[3] - averageFinderPatternBlocksize) < averageFinderPatternBlocksize && + Math.abs(scans[4] - averageFinderPatternBlocksize) < averageFinderPatternBlocksize && + !v; // And make sure the current pixel is white since finder patterns are bordered in white + // Do the last 3 color changes ~ match the expected ratio for an alignment pattern? 1:1:1 of w:b:w + var averageAlignmentPatternBlocksize = sum(scans.slice(-3)) / 3; + var validAlignmentPattern = Math.abs(scans[2] - averageAlignmentPatternBlocksize) < averageAlignmentPatternBlocksize && + Math.abs(scans[3] - averageAlignmentPatternBlocksize) < averageAlignmentPatternBlocksize && + Math.abs(scans[4] - averageAlignmentPatternBlocksize) < averageAlignmentPatternBlocksize && + v; // Is the current pixel black since alignment patterns are bordered in black + if (validFinderPattern) { + // Compute the start and end x values of the large center black square + var endX_1 = x - scans[3] - scans[4]; + var startX_1 = endX_1 - scans[2]; + var line = { startX: startX_1, endX: endX_1, y: y }; + // Is there a quad directly above the current spot? If so, extend it with the new line. Otherwise, create a new quad with + // that line as the starting point. + var matchingQuads = activeFinderPatternQuads.filter(function (q) { + return (startX_1 >= q.bottom.startX && startX_1 <= q.bottom.endX) || + (endX_1 >= q.bottom.startX && startX_1 <= q.bottom.endX) || + (startX_1 <= q.bottom.startX && endX_1 >= q.bottom.endX && ((scans[2] / (q.bottom.endX - q.bottom.startX)) < MAX_QUAD_RATIO && + (scans[2] / (q.bottom.endX - q.bottom.startX)) > MIN_QUAD_RATIO)); + }); + if (matchingQuads.length > 0) { + matchingQuads[0].bottom = line; + } + else { + activeFinderPatternQuads.push({ top: line, bottom: line }); + } + } + if (validAlignmentPattern) { + // Compute the start and end x values of the center black square + var endX_2 = x - scans[4]; + var startX_2 = endX_2 - scans[3]; + var line = { startX: startX_2, y: y, endX: endX_2 }; + // Is there a quad directly above the current spot? If so, extend it with the new line. Otherwise, create a new quad with + // that line as the starting point. + var matchingQuads = activeAlignmentPatternQuads.filter(function (q) { + return (startX_2 >= q.bottom.startX && startX_2 <= q.bottom.endX) || + (endX_2 >= q.bottom.startX && startX_2 <= q.bottom.endX) || + (startX_2 <= q.bottom.startX && endX_2 >= q.bottom.endX && ((scans[2] / (q.bottom.endX - q.bottom.startX)) < MAX_QUAD_RATIO && + (scans[2] / (q.bottom.endX - q.bottom.startX)) > MIN_QUAD_RATIO)); + }); + if (matchingQuads.length > 0) { + matchingQuads[0].bottom = line; + } + else { + activeAlignmentPatternQuads.push({ top: line, bottom: line }); + } + } + } + }; + for (var x = -1; x <= matrix.width; x++) { + _loop_2(x); + } + finderPatternQuads.push.apply(finderPatternQuads, activeFinderPatternQuads.filter(function (q) { return q.bottom.y !== y && q.bottom.y - q.top.y >= 2; })); + activeFinderPatternQuads = activeFinderPatternQuads.filter(function (q) { return q.bottom.y === y; }); + alignmentPatternQuads.push.apply(alignmentPatternQuads, activeAlignmentPatternQuads.filter(function (q) { return q.bottom.y !== y; })); + activeAlignmentPatternQuads = activeAlignmentPatternQuads.filter(function (q) { return q.bottom.y === y; }); + }; + for (var y = 0; y <= matrix.height; y++) { + _loop_1(y); + } + finderPatternQuads.push.apply(finderPatternQuads, activeFinderPatternQuads.filter(function (q) { return q.bottom.y - q.top.y >= 2; })); + alignmentPatternQuads.push.apply(alignmentPatternQuads, activeAlignmentPatternQuads); + var finderPatternGroups = finderPatternQuads + .filter(function (q) { return q.bottom.y - q.top.y >= 2; }) // All quads must be at least 2px tall since the center square is larger than a block + .map(function (q) { + var x = (q.top.startX + q.top.endX + q.bottom.startX + q.bottom.endX) / 4; + var y = (q.top.y + q.bottom.y + 1) / 2; + if (!matrix.get(Math.round(x), Math.round(y))) { + return; + } + var lengths = [q.top.endX - q.top.startX, q.bottom.endX - q.bottom.startX, q.bottom.y - q.top.y + 1]; + var size = sum(lengths) / lengths.length; + var score = scorePattern({ x: Math.round(x), y: Math.round(y) }, [1, 1, 3, 1, 1], matrix); + return { score: score, x: x, y: y, size: size }; + }) + .filter(function (q) { return !!q; }) // Filter out any rejected quads from above + .sort(function (a, b) { return a.score - b.score; }) + // Now take the top finder pattern options and try to find 2 other options with a similar size. + .map(function (point, i, finderPatterns) { + if (i > MAX_FINDERPATTERNS_TO_SEARCH) { + return null; + } + var otherPoints = finderPatterns + .filter(function (p, ii) { return i !== ii; }) + .map(function (p) { return ({ x: p.x, y: p.y, score: p.score + (Math.pow((p.size - point.size), 2)) / point.size, size: p.size }); }) + .sort(function (a, b) { return a.score - b.score; }); + if (otherPoints.length < 2) { + return null; + } + var score = point.score + otherPoints[0].score + otherPoints[1].score; + return { points: [point].concat(otherPoints.slice(0, 2)), score: score }; + }) + .filter(function (q) { return !!q; }) // Filter out any rejected finder patterns from above + .sort(function (a, b) { return a.score - b.score; }); + if (finderPatternGroups.length === 0) { + return null; + } + var _a = reorderFinderPatterns(finderPatternGroups[0].points[0], finderPatternGroups[0].points[1], finderPatternGroups[0].points[2]), topRight = _a.topRight, topLeft = _a.topLeft, bottomLeft = _a.bottomLeft; + var alignment = findAlignmentPattern(matrix, alignmentPatternQuads, topRight, topLeft, bottomLeft); + var result = []; + if (alignment) { + result.push({ + alignmentPattern: { x: alignment.alignmentPattern.x, y: alignment.alignmentPattern.y }, + bottomLeft: { x: bottomLeft.x, y: bottomLeft.y }, + dimension: alignment.dimension, + topLeft: { x: topLeft.x, y: topLeft.y }, + topRight: { x: topRight.x, y: topRight.y }, + }); + } + // We normally use the center of the quads as the location of the tracking points, which is optimal for most cases and will account + // for a skew in the image. However, In some cases, a slight skew might not be real and instead be caused by image compression + // errors and/or low resolution. For those cases, we'd be better off centering the point exactly in the middle of the black area. We + // compute and return the location data for the naively centered points as it is little additional work and allows for multiple + // attempts at decoding harder images. + var midTopRight = recenterLocation(matrix, topRight); + var midTopLeft = recenterLocation(matrix, topLeft); + var midBottomLeft = recenterLocation(matrix, bottomLeft); + var centeredAlignment = findAlignmentPattern(matrix, alignmentPatternQuads, midTopRight, midTopLeft, midBottomLeft); + if (centeredAlignment) { + result.push({ + alignmentPattern: { x: centeredAlignment.alignmentPattern.x, y: centeredAlignment.alignmentPattern.y }, + bottomLeft: { x: midBottomLeft.x, y: midBottomLeft.y }, + topLeft: { x: midTopLeft.x, y: midTopLeft.y }, + topRight: { x: midTopRight.x, y: midTopRight.y }, + dimension: centeredAlignment.dimension, + }); + } + if (result.length === 0) { + return null; + } + return result; +} +exports.locate = locate; +function findAlignmentPattern(matrix, alignmentPatternQuads, topRight, topLeft, bottomLeft) { + var _a; + // Now that we've found the three finder patterns we can determine the blockSize and the size of the QR code. + // We'll use these to help find the alignment pattern but also later when we do the extraction. + var dimension; + var moduleSize; + try { + (_a = computeDimension(topLeft, topRight, bottomLeft, matrix), dimension = _a.dimension, moduleSize = _a.moduleSize); + } + catch (e) { + return null; + } + // Now find the alignment pattern + var bottomRightFinderPattern = { + x: topRight.x - topLeft.x + bottomLeft.x, + y: topRight.y - topLeft.y + bottomLeft.y, + }; + var modulesBetweenFinderPatterns = ((distance(topLeft, bottomLeft) + distance(topLeft, topRight)) / 2 / moduleSize); + var correctionToTopLeft = 1 - (3 / modulesBetweenFinderPatterns); + var expectedAlignmentPattern = { + x: topLeft.x + correctionToTopLeft * (bottomRightFinderPattern.x - topLeft.x), + y: topLeft.y + correctionToTopLeft * (bottomRightFinderPattern.y - topLeft.y), + }; + var alignmentPatterns = alignmentPatternQuads + .map(function (q) { + var x = (q.top.startX + q.top.endX + q.bottom.startX + q.bottom.endX) / 4; + var y = (q.top.y + q.bottom.y + 1) / 2; + if (!matrix.get(Math.floor(x), Math.floor(y))) { + return; + } + var lengths = [q.top.endX - q.top.startX, q.bottom.endX - q.bottom.startX, (q.bottom.y - q.top.y + 1)]; + var size = sum(lengths) / lengths.length; + var sizeScore = scorePattern({ x: Math.floor(x), y: Math.floor(y) }, [1, 1, 1], matrix); + var score = sizeScore + distance({ x: x, y: y }, expectedAlignmentPattern); + return { x: x, y: y, score: score }; + }) + .filter(function (v) { return !!v; }) + .sort(function (a, b) { return a.score - b.score; }); + // If there are less than 15 modules between finder patterns it's a version 1 QR code and as such has no alignmemnt pattern + // so we can only use our best guess. + var alignmentPattern = modulesBetweenFinderPatterns >= 15 && alignmentPatterns.length ? alignmentPatterns[0] : expectedAlignmentPattern; + return { alignmentPattern: alignmentPattern, dimension: dimension }; +} + + +/***/ }) +/******/ ])["default"]; +}); \ No newline at end of file diff --git a/plugins/liddalidd/h3-image-executor/skills/h3-image-executor/SKILL.md b/plugins/liddalidd/h3-image-executor/skills/h3-image-executor/SKILL.md index cf8cea05..f6de9bf7 100644 --- a/plugins/liddalidd/h3-image-executor/skills/h3-image-executor/SKILL.md +++ b/plugins/liddalidd/h3-image-executor/skills/h3-image-executor/SKILL.md @@ -82,6 +82,62 @@ miniapp({ 3. **一次 `open` 就是一次作画。** 用户要四张图,流水线会自己拆成四张并锁定角色一致性, 不要手动开四个会话,也不要分四次调用。 +## 需求里有二维码 + +生图模型画不出能扫的二维码。流水线的做法是:规划时把码里要放的内容从需求里逐字记下,出图后由程序找到模型画的码, +换成真码并扫码验证,扫不出就打回重画。所以码里放什么必须写在需求里。 + +- **用户说了码里放什么**(链接、文字、号码):照常原样传,不用额外处理。 +- **用户要码但没说放什么**:先问一句「二维码里要放什么链接或文字?」。拿到回答后,把用户的原话接在需求后面一起传, + 例如 `…电子名片。二维码内容:https://www.minimax.cn/`,不要改写、不要补全。 + 用户说「先不放」「随便」:照常传,交付时会注明这是扫不出的装饰码。 +- **不要自己编网址**,也不要把你查到的网址当成用户的内容塞进去。 + +不是这个面板出的图(用户自己给的图、别的工具画的图)要换码,用 `h3-image-qrcode` skill 里的 `h3-qr fix`。 + +## 需求里有大段文字 + +生图模型写不好长段文字、竖排和毛笔书法,会编字、漏句。流水线遇到这类文字(一段 24 字以上、竖排、书法作品等), +会让模型只画一张载体留空的底图,再由程序用字体把规划从需求里抄下的文字逐字排上去。所以: + +- **文字照原样传**,一个字都不要改:不要缩写、不要「纠正」异体字、不要替用户补标点或删标点。 +- 用户只说了篇名(「写一幅《静夜思》」)时照原话传,规划会自己写出全文;用户给了全文就连全文一起传。 +- 书法作品默认不写标点(古代书法本来不写)。用户明确要标点的,把用户的原话一起传进去。 +- 排上去的是字体,不是手写。用户问「是不是真的毛笔写的」时如实说:底图是模型画的,字是程序用毛笔字体排的。 + +## 要做 GIF 动图 + +用户要 GIF、动图、会动的表情包时也交给这个面板,不要自己拼帧、不要改用别的工具(面板以外的图见本节最后一条)。流水线先定一张参考图, +第一帧从参考图改,后面每帧接着前一帧改一小步,一共 4–16 个关键帧(帧数按动作幅度定,一步跨太大的地方程序会补过渡帧),由程序拼成循环 GIF: + +- **用户手上有图**(「把这张猫做成挥手的 GIF」):告诉他在面板里上传这张图,需求照原话传。各帧直接照这张图改。 +- **用户没给图**:照原话传。面板会先画一张参考图交付,标「GIF 参考图 · 待确认」,用户点「生成 GIF」或回复确认后才出帧; + 卡片上有 1 分钟的倒计时:用户这段时间里没有任何回应,就按默认确认、自动开始出帧;用户点「先别生成」就停住, + 等他确认。用户想先改参考图,就带 `session` 把修改要求递进去(这也会取消倒计时)。 +- 动作写清楚就行(「先眨眼,再挥手」),帧数和每帧时长由规划定;用户点名了帧数、速度、尺寸就原话一起传。 +- 这是关键帧动画,不是视频:合成时程序会把背景锁住、相邻两帧之间补渐变,但镜头运动、背景变化做不了。 + 用户要的是这类效果时如实说。用户想要一下一下跳的效果(像素风、定格、卡点),原话传就行,面板会改成直接切换。 +- **绕中心转的 GIF**(加载圈、转圈的图标、齿轮、风车):照原话传。面板只画一张图,由程序按圆心转出各帧,不会丢帧、首尾接得上, + 一次生图就交付 GIF;用户说「转快一点」「反过来转」「一圈 8 格」也带 `session` 递进去,不重新生图。硬币翻面、地球自转这种 + 立体的转动做不了,用户要的是这种时如实说。 +- **组图做成的 GIF**(表情从笑到哭、分步动作):照原话传。规划可能让面板先画一张分格组图,评审通过后切格拼成 GIF, + 相邻两格之间各补一格过渡(一起生成、从候选里挑),组图和 GIF 一起交付。已经交付的组图,用户说「把这组图变成 GIF」时 + 带 `session` 递进去(见下一节):组图不重画,补过渡格要一次生图,一两分钟。用户说不要补帧、直接拼,也原话一起传。 +- 不是这个面板出的组图(用户自己给的、别的工具画的)要拼 GIF,几张单独的图要按顺序拼成 GIF(面板只拼一张分格组图, + 这种情况它会在交付说明里请你来拼),或者你要写程序在图上画会动的东西,用 `h3-image-gif` skill 里的 `h3-gif`。 + +## 图里要放数据图表 + +用户要柱状图、条形图、折线图、面积图、饼图、环形图,或者信息图、海报里带一张数据图表时,也交给这个面板, +不要自己算比例、不要自己写好图表的描述再传。面板让模型画一张留出空白面板的底图,图表由程序按数据画上去, +画完从成品里把柱高、数据点、扇形角度量回来核对: + +- **数要写在原话里。** 用户说了数就原样一起传(「2021 到 2025 年营业额分别是 86、120、158、203、245 万元」); + 用户只说「画一张销售额增长的图」没给数,先问他要,不要替他编。百分比、合计、增长率由程序算,不用先算好。 +- 用户点名了图表类型、颜色、位置、单位就原话一起传;没点名由规划定。 +- 交付里有数在用户原话里找不到时,面板会列出来请用户核对,如实转告,不要替他确认。 +- 程序画的是规整的平面图表,不是手绘,也不跟着面板的透视变形;散点、雷达、地图、表格画不了,用户要的是这类时如实说。 + ## 用户接着提修改 用户在聊天里说「把背景换成夜景」「再加一张」时,如果指的是刚才那张图,带上 `session`: @@ -96,6 +152,29 @@ miniapp({ 不带 `session` 会开成一张全新的图,用户会以为上一张白画了。 +用户补一句二维码内容(例如「二维码链接到 https://…」「码换成我的微信号 xxx」)也一样带 `session` 递进去, +`prompt` 照样逐字传用户的话:面板会直接在上一版交付图上把码换成真码、扫码验证,不重新生图,几秒钟就好。 + +用户说参考图不对、要重新找(例如「参考图找的不对,重新找」「吉祥物不像,换张参考图」)也带 `session` 原话递进去: +面板会给没有参考图、或者用户说不对的主体重新检索,用找到的图重画;什么都没找到时会如实说明,不会白画一遍。 +不要自己去网上找图、也不要把网址塞进 `prompt`——面板只用用户自己给的地址,别的地址一律忽略。 +用户手上有对的图时,告诉他可以在面板里直接上传参考图,这是最可靠的办法。 + +用户要改图里的字(例如「字换成宋体」「字大一点」「加上标点」「第三句改成……」)也带 `session` 原话递进去: +字是程序排的,面板直接在无字底图上重排,不重新生图,几秒钟就好;字是模型画的,面板会先让模型把字擦掉,再由程序排。 + +用户要改图表(例如「2024 年改成 280」「换成折线图」「加一年 2026 年 300」「颜色换成蓝色」「去掉图表」)也带 `session` +原话递进去:图表是程序画的,面板直接在留白的底图上按新数据重画,不重新生图,一两秒就好。 + +用户要调 GIF(例如「慢一点」「第 3 帧重画」「去掉最后一帧」「在第 2 帧后面加一帧眨眼」「只播一遍」「别要渐变」)也带 `session` +原话递进去:调快慢、顺序、尺寸、循环、渐变还是直接切换和删帧只是重新合成,不重新生图;重画、加帧只画那几帧; +改主体本身(换颜色、换衣服)会先改参考图,再请用户确认一次。 + +用户要把交付的组图变成 GIF(「把这组图变成 GIF」「做成动图」),或者调组图拼的 GIF(「每格停久一点」「来回播」「去掉第 3 格」 +「加渐变」「切换时弹一下」「更顺一点」「不要补帧」),也带 `session` 原话递进去:变成 GIF 时组图不重画、补过渡格要一次生图; +只调节奏时几秒钟重新拼,不重新生图;要改某一格的画面(「第 3 格改成生气」)会照常改组图,改完重补过渡格、重新拼。 +图里没有分格时面板会如实说切不出格子,不会重画。 + ## 交付之后 面板里会出验收报告和每张图的 `request_id`。用户问「画得怎么样」「用的什么 prompt」 @@ -107,6 +186,52 @@ miniapp({ - `turns[].events[]` —— 每个阶段的进展,带 `stage` 和 `requestId` - `images[]` —— 每张图的 `id` / `status` / `failReason` / `requestId` +- `turns[].delivery.items[].qr` —— 每张交付图的二维码状况:`real`(已换成真码、扫码通过)/ `decorative`(没给内容的装饰码)/ + `unverified`(规划给的内容在用户原话里找不到,没贴)/ `failed`(给了内容但没换成,`note` 里是原因) +- `turns[].delivery.items[].text` —— 程序排字的状况:`status` 是 `overlay`(已排入)/ `failed`(没排上,`note` 里是原因), + `font` 是用的字体,`items[].layout` 是排法(几列几行、字高、标点怎么处理,有没有和同类文字统一字号、首行空几格、 + 几个脚注符号排成上标、上方画没画分隔线、规划位置不空时往上或往下挪了多少);`baseFile` 是不带字的底图。 + 一张图最多排 16 段文字,超出的段落写在交付说明里。两段字排到一起时这张候选不交付(原因写「两段文字排到了一起」)。 + `images[].rounds[].action.type` 为 `text` 且带 `textPatches` 的轮次,是评审让程序在某张候选的底图上挪字重排,没有调生图接口 +- `images[].rounds[].candidates[].checks[]` —— 程序和评审逐项的结论。`H-color` 是偏红判定(需求要的主色就是黄、红、橙、 + 画面又几乎没有中性色时写「交给评审看」,不判偏红);`H-white` 只在需求写明白纸、白底时出现,`text` 里是白色区域实测的 + RGB 和通道差(4 级以上判不通过)。`anchorId` 是这一轮编辑带的色彩基准图(第 2 张图,只对照颜色),`images[].colorAnchor` + 是这张图当前的色彩基准 +- `refs[]` —— 参考图。`origin` 是 `user`(用户给的)/ `search`(检索来的)/ `generated`(角色基准图);检索来的带 `use` + (`subject` 主体外观 / `detail` 局部 / `style` 画风 / `layout` 版式 / `scene` 场景)和 `take`(要取它什么), + 生图时逐张写进 prompt。用户问「为什么搜了这么多图」「这张参考图拿来干嘛」,看这两个字段 +- `refBudget` —— 规划给的检索预算:`limit` 上限、`used` 用了多少(`searches` 搜图、`gates` 判定)、`stopped` 是预算不够没搜的项。 + 要不要检索由规划决定(`plan.ref_budget`,0 就是不搜)。用户问「为什么没去找参考图」「为什么只找了一半」,先看这里和 `plan.think` +- 程序排字里一个字两种颜色:`images[].spec.textOverlay.layers[].part`(`side` 偏旁在哪一侧、`color` 偏旁的颜色); + 结果在候选的 `program.text.items[].part`:`ok` 是否分成了,`note` 是换了字体或没分成的原因。 + `images[].spec.readingProblems` 不为空时,表示规划的文字层按版面读不出用户引号里的这几处(规划已经改过一次),请用户核对 +- `images[].spec.difficulty` —— 规划给这张图判的难度(`easy` / `normal` / `hard`,`difficultyReason` 是理由;空的表示规划没判, + 按规格推断)。它决定评审、修正、选优、追问编辑这些节点每次能调用几次工具:上限 = 必须打开的图 + 1 + 难度余量(1 / 2 / 4), + 撞过上限的节点下次再加 2。用户问「评审为什么被打断」「工具调用为什么是这个数」,看时间线节点详情里的「预算怎么定的」 +- `turns[].delivery.items[].chart` —— 程序画图表的状况:`status` 是 `overlay`(已画入并核对通过)/ `failed`(没画上,`note` 里是原因); + `items[]` 每张图表一项:`typeLabel` 类型、`categories` 和 `series` 是图表用的全部数据、`verify.maxErrorPx` 是从成品量回来的最大偏差、 + `unverified` 是在用户原话里找不到的数(不为空时请用户核对);`baseFile` 是留白的底图 +- `turns[].delivery.items[].gif` —— GIF 的交付:`file` 是 GIF、`strip` 是帧条,`frames` 帧数(用户说「第几帧」就是这套编号)、 + `written` 是 GIF 里实际写了几帧(含渐变帧)、`inbetween` 是程序补的过渡帧有几帧、`cuts` 是两帧差得多没补渐变的有几处、`frameMs` 是规划写的每帧毫秒、`keyMs` 是实际每帧停多久(补了过渡帧、去掉没过评审的帧之后会变)、`durationMs` 整组时长、`loop` 是否循环、`transition` 是 `fade`(渐变)还是 + `cut`(直接切换)、`verify.ok` 是写出去之后读回来核对的结果;`items[]` 是逐帧的情况(`status` 为 `passed` / `unpassed` + (没过评审,`skipped` 为 true 时没放进 GIF)/ `unreviewed` / `failed`(没画出来)/ `reference`(就是参考图),`mirrorOf` 不为 0 表示和第几帧用同一张图)。 + 这一项的 `file` 是参考图本身,不是 GIF。`source` 为 `panels` 时是组图切格拼成的:`frames` 是放进 GIF 的格子数, + `grid` 是几行几列和怎么切的(`method`:`line` 分隔线 / `gutter` 留白 / `free` 格子大小不一 / `even` 等分:格子之间没有清楚的分隔,或者按指定的行列), + `order` 是播放顺序(格子编号从上到下、从左到右数),`keyMs`、`firstMs`、`lastMs` 是每格、第一格、最后一格停多久, + `pingpong` 来回播,`bounce` 切换时弹不弹,`items[]` 是每格的行列和有没有放进 GIF(`inGif`);这时 `file` 是组图本身。 + `images[].spec.panelGif` 是组图拼 GIF 的设定(`smooth` 为 false 时不补过渡格),`images[].panelGif` 是拼好的结果(`state` 为 `failed` 时 `error` 里是原因), + `tweens` 是用上了几格过渡格、`tweenNote` 是没补上的原因。`images[].panelTweens.pairs[]` 是每一步过渡格:`from`、`to` 是哪两格, + `text` 是过渡格规划写的样子,`status`(`passed` 评审挑的 / `unreviewed` 评审没跑成、按程序挑的 / `failed` 没补上 / `skipped` 不补), + `candidateFile` 是用的哪张候选组图。 + `source` 为 `motion` 时是程序绕圆心转出来的:`steps` 一圈几格(0 是连续转)、`direction`(`cw` 顺时针 / `ccw` 逆时针)、 + `center` 程序最后用的圆心(画面宽高的比例)、`radius` 转的圆盘半径(短边的比例)、`keyMs` 每格多久、`durationMs` 一圈多久; + 这时 `file` 是原图。`images[].spec.motionGif` 是转法,`images[].motionGif` 是转好的结果 +- `images[].animation.frames[]` 里 `status` 为 `unpassed` 的帧重画后还没过评审,没放进 GIF;它前后两帧之间 `inbetween` 为 true 的帧 + 是按它原来的计划补的过渡帧 +- `turns[].delivery.items[].animation` —— `state` 为 `awaiting_confirm` 时,这张是还在等用户确认的 GIF 参考图, + `frames[]` 是计划出的各帧;`failed` 时 GIF 没做成,`error` 里是原因 +- `autoConfirm` —— 参考图在倒计时:`deadline` 是到点自动开始的时间;`held` 为 true 表示用户点了「先别生成」;为 null 表示没在倒计时。 + `turns[].auto` 为 true 的那一轮是到点自动开始的,不是用户发的 - `status` —— `delivered` / `failed` / `running` / `cancelled` 不要把 `request_id` 截断或省略,用户要拿它去查账。 diff --git a/plugins/liddalidd/h3-image-executor/skills/h3-image-gif/SKILL.md b/plugins/liddalidd/h3-image-executor/skills/h3-image-gif/SKILL.md new file mode 100644 index 00000000..dbf6bbf1 --- /dev/null +++ b/plugins/liddalidd/h3-image-executor/skills/h3-image-gif/SKILL.md @@ -0,0 +1,137 @@ +--- +name: h3-image-gif +description: 要把一张分格组图(四格、六格、九宫格、表情变化图)切开拼成 GIF,把几张单独的图按顺序拼成 GIF,把自己写程序画出来的一组帧编码成循环 GIF,或者检查一个 GIF 的帧数、每帧时长、循环和首尾接缝时使用。自带 h3-gif 命令行工具:全组共用一套调色板、每帧时长可以不一样、写完从文件读回来逐帧核对,并出一张帧拼图;不用自己写 GIF 编码和核对脚本。 +--- + +# 组图和帧拼成 GIF + +## 先记住三条 + +1. **生图模型一次只出一张静态图。** 要动起来只有两条路:让模型画一张分格的组图,切开按顺序播(`grid`); + 或者静态图由模型画,会动的部分由你写程序一帧一帧算出来(`frames`)。不要让生图模型「画一个 GIF」。 +2. **H3-Image-Executor 面板里出的图,在面板里做。** 面板交付的组图,用户说「把这组图变成 GIF」时带会话 ID 把原话 + 递进同一个面板会话(见 `h3-image-executor` skill),面板会切格拼 GIF,相邻两格之间补过渡格(组图不重画); + 这个工具不生图,拼出来是一格一格地切换;要连贯的动作(眨眼、挥手) + 也交给面板,它会按参考图一帧一帧改。这个工具给面板以外的图用:用户自己给的组图、别的工具画的图、你写程序画的帧。 + 面板交付的是几张单独的图(四张一组的漫画),用户要按顺序拼成 GIF 时,面板会说让你来拼:从会话的 `session.json` 里 + `turns[].delivery.items[].file` 找到交付文件(在会话目录的 `deliver/` 下),用 `slides` 拼,`-o` 写到工作区里。 + 不要把输出写进插件数据目录,也不要改那里的文件。 +3. **「做好了」只认工具的结果。** 退出码 0、输出 JSON 里 `verify.ok` 为 true 才算做成;再 `read` 一下帧拼图(`sheet`), + 看格子切得对不对、顺序对不对。读 GIF 文件本身通常只看得到第一帧,不能代替帧拼图。 + +## 什么时候用 + +| 场景 | 子命令 | +| --- | --- | +| 一张分格的组图(任何来源)要变成 GIF | `grid` | +| 只想把组图切成一格一格的 PNG,再自己加工 | `split` | +| 几张单独的图按顺序播(四张一组的漫画、一组表情) | `slides` | +| 你写程序画好了一组帧 | `frames` | +| 一张图里有个绕中心转的东西(加载圈、图标、齿轮),要让它转起来 | `spin` | +| 看一个 GIF:几帧、每帧多久、循环没有、首尾接不接得上 | `check` | + +## 工具位置与用法 + +工具随 H3-Image-Executor 插件发布,零第三方依赖: + +```bash +node "<PLUGIN_ROOT>/miniapp/node/tools/h3-gif.mjs" <子命令> [参数] +``` + +| 子命令 | 用途 | 成功输出 | +| --- | --- | --- | +| `grid <组图> [--rows R --cols C \| --count N] [节奏参数] [-o 输出.gif] [--sheet 帧条.png]` | 找格子、切开、对齐、按节奏拼成 GIF,读回核对 | `{ok,out,sheet,width,height,bytes,frames,durationMs,loop,grid,framing,timeline,verify}` | +| `split <组图> [--rows R --cols C \| --count N] [--no-align] [-o 目录]` | 切成同样大小的 PNG:`01.png`、`02.png`…… | `{ok,dir,width,height,grid,files}` | +| `slides <图1> <图2> … [节奏参数] [-o 输出.gif] [--sheet 帧条.png]` | 几张图按顺序拼成 GIF,按第一张的画幅统一大小 | 同 `grid`,另有 `sources`、`mode` | +| `frames <帧1> <帧2> … \| --dir <目录> [--frame-ms 100 \| --delays 310,60,…] [--once] [--edge 长边 \| --scale 倍数] [--dither none\|ordered] [--colors N] [--stabilize N] [--fit] [-o 输出.gif] [--sheet 拼图.png]` | 一组帧编码成 GIF,读回核对,看接缝。默认每帧 100 毫秒,按帧的原尺寸写,长边超过 1024 的缩到 1024,不放大 | `{ok,out,sheet,width,height,bytes,input,frames,delays,durationMs,loop,colors,dither,seam,notes,verify}` | +| `check <GIF> [--frames N] [--loop \| --once] [--ms 总毫秒] [--sheet 拼图.png]` | 读一个 GIF,按给的预期核对 | `{ok,width,height,bytes,frames,delays,durationMs,loop,steps,seam,notes,problems,sheet}` | +| `spin <图片> [--center x,y] [--radius r] [--steps N \| --frames N] [--ccw] [--frame-ms N] [--fade \| --cut] [--no-refine] [--once] [--edge 长边] [-o 输出.gif] [--sheet 帧条.png]` | 绕圆心把图里的一块圆盘转起来 | `{ok,out,sheet,width,height,bytes,frames,written,durationMs,loop,steps,direction,transition,frameMs,center,radius,moved,verify}` | + +节奏参数(`grid`、`slides`): + +| 参数 | 说明 | +| --- | --- | +| `--order 1,2,3` | 播放顺序,写格子编号(从上到下、从左到右数);没写的格子不放 | +| `--frame-ms 700` | 每格停多久,默认 700 毫秒 | +| `--first-ms`、`--last-ms` | 第一格、最后一格停多久,默认是每格的 1.5 倍、2 倍 | +| `--hold 6:1500` | 第 6 格停 1500 毫秒,可以写多次 | +| `--pingpong` | 来回播(1→6→2,再接回 1) | +| `--once` | 只播一遍(默认循环) | +| `--fade` | 两格之间叠两帧渐变(默认直接切换) | +| `--bounce` | 切换时弹一下:新的一格先放大 3%,0.08 秒后落回原大小。默认不弹:整幅画面会跟着放大再落回,各格对齐得再准,每次切换也会跳一下 | +| `--edge 768` | GIF 长边,默认 768 | +| `--no-align` | 不对齐各格(`grid`) | + +- 成功时 stdout 只有一个 JSON 对象,退出码 0。 +- 用法、文件错误、切不出格子:stderr 给原因,退出码 1。 +- 写出去的 GIF 读回来核对没通过:stderr 第一行是原因、第二行是 JSON,退出码 2,**不写输出文件**;`check` 的预期没对上也是退出码 2。 +- **永远不覆盖输入文件。** 不带 `-o` 时写在输入旁边:`grid` 写 `<组图名>.gif`,`slides` 写 `<第一张>-slides.gif`, + `frames` 写在帧目录旁边 `<目录名>.gif`;重名时加 `-2`、`-3`。帧拼图默认写在 GIF 旁边:`<GIF 名>-frames.png`。 +- 输入只认 PNG、JPEG(GIF 取第一帧)。透明背景会铺成白底:GIF 只有全透明和不透明两种,这个工具不保留透明。 + +## 组图 → GIF(grid) + +1. 跑 `grid`。默认按分隔线或留白自动找格子,从上到下、从左到右排顺序。 +2. 退出码 1、说没找到分隔:多半是白底贴纸这种格子之间什么都没有的图。看一眼图,用 `--rows 2 --cols 3` + (或 `--count 6`)说明几行几列,按等分切。用户说过几格就照用户说的给。 +3. 退出码 0:`read` 帧条(`sheet`),核对格子数、顺序和每格的画面,再把 GIF 路径告诉用户。 +4. 用户要调:慢一点加大 `--frame-ms`;结尾多停一会儿用 `--last-ms`;去掉某一格用 `--order` 写出要留下的格子; + 要柔和一点用 `--fade`(两格差得多时叠出来是半透明的虚影,先告诉用户);用户点名要切换时弹一下才加 `--bounce`。 + +输出里 `grid.panels` 是每格在原图里的位置,`grid.method` 是怎么切的(`line` 分隔线 / `gutter` 留白 / `free` 格子大小不一 / `even` 等分:格子之间没有清楚的分隔,或者按你给的行列), +`framing.offsets` 是对齐时每格挪了多少像素(模型在每一格里画的主体位置差一点,原样播主体会一跳一跳的)。 + +文件大小:一张六格的 Q 版表情组图拼成长边 768 的 GIF 是 388 KB;细节多的二次元组图(屏幕上一整面 K 线)是 909 KB。 +加 `--bounce` 每格多一整帧,文件大约大一倍。用户嫌大,减小 `--edge`。 + +## 让图里的东西转起来(spin) + +1. 看图:转动的东西的中心在哪(`--center x,y`,画面宽、高的比例),要转的范围多大(`--radius`,短边的比例,要盖住整个转动的东西; + 给小了程序会按画面内容放大)。 +2. 圆环上等距的点、辐条、格子(亮点一格一格跑)用 `--steps N`,N 就是点数:每帧转一格,暗的点正好落回原位、看起来不动, + 程序还会在给的圆心附近找「转一格之后和自己最重合」的位置;默认每格 80 毫秒、两格之间叠一帧渐变(`--cut` 不要渐变)。 + 整个东西连续转用 `--frames N`(一圈 N 帧,默认 24、每帧 50 毫秒),不找圆心,用你给的。`--ccw` 逆时针。 +3. 转的是圆心周围一整块圆盘,里面的背景跟着转、外面不动:圆盘里的背景是纯色或者从圆心往外的渐变时看不出来;有图案的话会看到它在转, + 先告诉用户。输出里 `center` 是程序最后用的圆心,`moved` 是比你给的挪了多少。 +4. 只能在画面这个平面里转,硬币翻面、地球自转这种立体的转动做不了。 + +面板里出的图要转起来,带会话 ID 把用户的话递进面板(「让它转起来」),面板直接转,不用这个工具。 + +## 程序画帧 → GIF(frames) + +做法参考 jungle-adventure、david-voxel-loop 两个例子:静态底图由生图模型画,会动的部分由程序一帧一帧算出来。 + +1. **底图**:用面板画,或者用用户给的图。程序在底图上叠加会动的东西(小人沿路径走、藤蔓摆动、鸟飞过), + 或者把底图本身拆开重组(雕像碎成粒子再复原、一道扫描线扫过)。 +2. **按周期算每一帧**:第 i 帧的相位 `p = 2π·i/N`,位置、角度、亮度都写成 `p` 的 sin/cos,第 N 帧正好回到第 0 帧, + 循环就没有接缝。可以多算一帧第 N 帧,和第 0 帧逐像素比一次(jungle 的做法)。 + 有停顿的动作(david:静止 → 碎开 → 复原 → 静止),首尾两帧用同一张图,用 `--delays` 给首尾更长的停留,例如 `--delays 310,60,55,…,115`。 +3. **像素风**:在小画布上画(例如 256×256),存帧时不要自己放大,交给 `--scale 2`(最近邻放大,像素不会糊),再加 `--dither none`。 +4. **存帧**:每帧一个 PNG,同样大小,文件名带序号(`frame-00.png`……`frame-15.png`),放进一个单独的目录。 + 写帧的代码用手上有的:装了 Pillow 就用 Python(两个例子都是);没有的话用 Node,PNG 编码器用插件自带的: + `import { encodePng } from '<PLUGIN_ROOT>/miniapp/node/lib/png.mjs'`,`encodePng({ width, height, channels: 3, data })` 返回 PNG 的字节, + `data` 是按行排的 RGB。 +5. **编码**:`frames --dir <帧目录> --frame-ms 80`。默认逐像素写进去(`--stabilize 0`),差一两级的余辉、渐隐也不会丢; + 帧是照片质感、有噪点时可以 `--stabilize 8`,文件小一些。 +6. **看结果**:`seam.jump` 为 true,说明最后一帧接回第一帧时会跳一下,回去改帧(最后一帧应该是第一帧的前一步); + `notes` 里还会写首尾两帧完全一样、有的帧和前一帧一模一样被并掉、时长被取整到 10 毫秒这类情况。 + 最后 `read` 帧拼图(每行 4 帧,16 帧就是 4×4),确认动作顺序。 + +GIF 的每帧时长以 10 毫秒为单位,输出里的 `delays` 是实际写进去的值。浏览器会把短于 20 毫秒的帧按 100 毫秒播,工具最短写 20 毫秒。 + +## 工具做了什么(用户问起时照实说) + +- **找格子**:在缩到长边 1024 的图上,按列、按行量两种证据:细线(这一列和左右隔几个像素的列都差得多、方向一致,贯穿大部分行), + 留白(整列一个颜色,两边紧挨着贯穿整列的格子边)。逐个假设「等分成 n 格」,在等分位置附近找证据,所有分隔都找得到的假设里 + 取格子最多的;先切行,每一行再单独切列,所以上面两格、下面三格也切得开。只有一格的矮行(标题栏)和整格一个颜色的空格子不算。 + 切点只贴到等分位置附近贯穿八成以上的线或留白带上;附近没有(格子直接挨着画,没画分隔线)就按等分位置切, + 不贴到格子里面的竖线上(屏幕边框、图表坐标轴、门框)。 +- **修边、对齐**:修掉格子边上残留的分隔线、外框和半条留白,只修这些结构,不修格子里的内容。各格居中取同样大小; + 再找每一格相对第一格的平移:只比每格的中上部(左右各去掉 15%、下面去掉 30%,主体的头和脸通常在这里,手和背景每格不一样), + 先在长边 320 的梯度图上粗找(最多挪格子宽高的 4%,明显更贴合才挪),再在长边 960 的图上细调到亚像素。 + 格子大小差得多的排法不裁,缩进同一个画幅、四周补底色。 +- **编码**:全组一套 255 色调色板(在所有帧上做中位切分),平涂图不抖动,其余用按像素位置固定的有序抖动,同一位置同一颜色每一帧 + 色号一样,不会一帧一闪。和上一帧没差别的像素沿用上一帧;和上一帧一模一样的帧并进前一帧的时长。 +- **核对**:写完从字节读回来,核对尺寸、帧数、每帧时长、循环,每一帧和要写的画面平均差不超过 6 级。没过就不写文件。 +- **接缝**:把各帧缩小、模糊两档以后,比较最后一帧到第一帧的变化和中间最大的一步;大三成以上判为会跳。只数变了多少像素不行: + 小物体挪一步和从画面一头跳回另一头,变的都是两个整圆。 diff --git a/plugins/liddalidd/h3-image-executor/skills/h3-image-qrcode/SKILL.md b/plugins/liddalidd/h3-image-executor/skills/h3-image-qrcode/SKILL.md new file mode 100644 index 00000000..908cb92d --- /dev/null +++ b/plugins/liddalidd/h3-image-executor/skills/h3-image-qrcode/SKILL.md @@ -0,0 +1,77 @@ +--- +name: h3-image-qrcode +description: 图里的二维码扫不出来、要把图里的码换成能扫的真码、要检查一张图里的码能不能扫或扫出什么、要单独生成一张二维码图时使用。自带 h3-qr 命令行工具:在原图上找到二维码,按原来的透视和明暗贴上真码,再从输出文件扫码验证;不重新生图,也不用自己写二维码脚本。 +--- + +# 把图里的假码换成真码 + +## 先记住三条 + +1. **生图模型画不出能扫的二维码。** 它画的只是长得像码的黑白方块。要让码能扫,就用这里的 `fix` 贴真码, + 不要让生图模型「画准一点」,也不要自己写编码或贴图脚本。 +2. **码里放什么只能来自用户。** 从用户原话里逐字复制链接、文字或号码,一个字符都不改:不补全、不缩写、 + 不换成你觉得对的网址。用户没说放什么,先问一句「二维码里要放什么链接或文字?」,不要自己编。 +3. **「能扫」只认工具的扫码结果。** `fix` 退出码为 0、输出 JSON 里 `ok: true`,才能告诉用户「已经换成能扫的码」。 + 不要凭看图下结论。 + +## 什么时候用 + +| 场景 | 做法 | +| --- | --- | +| 用户给了一张图(任何来源),说码扫不出、或者要把码换成某个链接 | `fix` | +| 用户问一张图里的码能不能扫、扫出来是什么 | `scan` | +| 想知道图里有几枚码、在哪、多大(包括扫不出的假码) | `locate` | +| 用户要一张单独的二维码图 | `make` | + +**H3-Image-Executor 面板里出的图不用这个工具。** 面板流水线出图后会自动贴真码并扫码验证; +要给装饰码补内容或换内容,带上会话 ID 把用户的话递进同一个面板会话(见 `h3-image-executor` skill), +面板会直接在原图上换码。不要用这个工具去改插件数据目录里的文件。 + +## 工具位置与用法 + +工具随 H3-Image-Executor 插件发布,零第三方依赖(扫码用同包 `miniapp/node/vendor/jsqr` 里的 jsQR,Apache-2.0): + +```bash +node "<PLUGIN_ROOT>/miniapp/node/tools/h3-qr.mjs" <子命令> [参数] +``` + +| 子命令 | 用途 | 成功输出 | +| --- | --- | --- | +| `fix <图片> --content <内容> [-o 输出]` | 把图里的码换成真码,从输出文件扫码验证 | `{ok,out,changed,format,results}` | +| `fix <图片> --content-file <文本文件> [-o 输出]` | 同上,内容从 UTF-8 文本文件读(只去掉末尾一个换行) | 同上 | +| `scan <图片>` | 扫出图里所有能扫的码 | `{width,height,count,codes:[{data,version,corners}]}` | +| `locate <图片>` | 找出所有码的位置,包括扫不出的假码 | `{count,regions:[{scannable,data,modules,modulePx,corners,quietModules}]}` | +| `make --content <内容> [-o 输出.png] [--ecl L\|M\|Q\|H]` | 单独生成一张二维码 PNG,并自扫确认 | `{ok,out,version,ecl,modules,scanned}` | + +- 成功时 stdout 只有一个 JSON 对象,退出码 0。 +- 用法或文件错误:stderr 给原因,退出码 1。 +- `fix` 换码后验证没通过:stderr 第一行是原因、第二行是 JSON,退出码 2,**不写输出文件**。 +- 内容里有引号、`$`、反斜杠、换行时,先把内容原样写进一个文本文件,再用 `--content-file`,免得被 shell 改掉。 +- 图里有多枚码时,`--content` 按图里从上到下、从左到右的顺序给多次;只给一条时换图里最大的那枚。 + +## 标准流程(换码) + +1. 用户没给内容就先问;给了就逐字照抄。 +2. 跑 `fix`。不带 `-o` 时输出写在原图旁边:`<原名>-qr.<扩展名>`,重名时加 `-2`、`-3`。**永远不覆盖原图**。 +3. 退出码 0:告诉用户输出文件路径,以及扫码实测出的内容(就是 `results[].content`)。 +4. 退出码 2:把原因如实告诉用户(见下表),不要说「已修好」。 + +## 没换成的原因和怎么办 + +| 原因 | 说明 | 建议用户怎么做 | +| --- | --- | --- | +| 没在图里找到二维码 | 找不到三个角的回字形定位方块:码被遮挡、太斜、做成了艺术化图案,或者图里根本没有码 | 换一张码画得规整的图;重新生成时要求「正对镜头、白底黑块、三个角的定位方块完整、四周留白」 | +| 图里的二维码太小,装不下这段内容 | 贴上去每个模块不到 4 像素,扫码靠运气 | 换更大的码,或者把内容换成短链接 | +| 贴上真码后扫不出来 | 透视太斜、码被局部遮挡、四周没有留白 | 同「没找到」 | +| 内容太长,生成不了二维码 | 超过二维码容量(字节模式最多约 2900 字节) | 换成短链接 | + +## 工具做了什么(用户问起时照实说) + +- 定位:先扫一遍能扫的码,再找三个角的定位方块(1:1:3:1:1 的黑白比例),沿定位方块的外边缘拟合四条边,得到透视四角。 +- 贴码:按内容生成标准二维码(纠错等级默认 M,内容太长或码太小时退到 L,码区放得下时自动提到 Q 或 H),把原来的码抹成浅色, + 按透视画进去;颜色取自原图的浅色和深色区域并跟着原来的明暗走;四周留至少 3 个模块的白边,不够时把码缩小一点让出来。 +- 验证:输出文件写出之前,先按最终格式编码(原图是 PNG 输出 PNG,否则输出质量 93 的 JPEG),再从这份字节重新解码, + 三种裁切范围下各扫「近看」和「远看」(每个模块约 2 像素)一次;近看、远看都至少两次扫出要放的内容才算通过, + 任何一次扫出别的内容判不通过。 +- 只认正色码(深色码印在浅底上):反色码多数手机扫不了,工具也不按反色验证。 +- 码里不加 ECI 声明,文字按 UTF-8 写入;微信和系统相机都按 UTF-8 解。