feat: /tools/perfect-pixel 伪像素修复工具 - detectPitch + fixPseudoPixel + 前后对比视图
This commit is contained in:
1 parent
cde64bda7a
commit
125c20906c
6 files changed
+946
No files matched your search
@@ -12,6 +12,7 @@ import PrivacyPolicyPage from './pages/legal/PrivacyPolicyPage';
|
|||||||
import TermsOfServicePage from './pages/legal/TermsOfServicePage';
|
import TermsOfServicePage from './pages/legal/TermsOfServicePage';
|
||||||
import AboutPage from './pages/legal/AboutPage';
|
import AboutPage from './pages/legal/AboutPage';
|
||||||
import ContactPage from './pages/legal/ContactPage';
|
import ContactPage from './pages/legal/ContactPage';
|
||||||
|
import PerfectPixelPage from './pages/PerfectPixelPage';
|
||||||
import Nav from './components/Nav';
|
import Nav from './components/Nav';
|
||||||
import Hero from './components/Hero';
|
import Hero from './components/Hero';
|
||||||
import PassSection, { type Plan } from './components/PassSection';
|
import PassSection, { type Plan } from './components/PassSection';
|
||||||
@@ -576,6 +577,9 @@ export default function App() {
|
|||||||
if (path === '/announcements' || path.startsWith('/announcements/')) {
|
if (path === '/announcements' || path.startsWith('/announcements/')) {
|
||||||
return <AnnouncementsPage />;
|
return <AnnouncementsPage />;
|
||||||
}
|
}
|
||||||
|
if (path === '/tools/perfect-pixel' || path.startsWith('/tools/perfect-pixel/')) {
|
||||||
|
return <PerfectPixelPage />;
|
||||||
|
}
|
||||||
if (path === '/privacy-policy' || path.startsWith('/privacy-policy/')) {
|
if (path === '/privacy-policy' || path.startsWith('/privacy-policy/')) {
|
||||||
return <PrivacyPolicyPage />;
|
return <PrivacyPolicyPage />;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -622,4 +622,28 @@ export const en: Messages = {
|
|||||||
'announcements.empty': 'No announcements yet',
|
'announcements.empty': 'No announcements yet',
|
||||||
'announcements.back': '← Back to Home',
|
'announcements.back': '← Back to Home',
|
||||||
'announcements.version': 'v{version}',
|
'announcements.version': 'v{version}',
|
||||||
|
|
||||||
|
/* ── PerfectPixel pseudo-pixel fix ── */
|
||||||
|
'perfectPixel.pageTitle': 'Perfect Pixel',
|
||||||
|
'perfectPixel.pageDesc': 'Upload an AI-generated pseudo-pixel image (with anti-aliased edges) and reconstruct it into clean true pixel art. Compare side-by-side and export.',
|
||||||
|
'perfectPixel.upload.aria': 'Upload a pseudo-pixel image',
|
||||||
|
'perfectPixel.upload.title': 'Upload an image',
|
||||||
|
'perfectPixel.upload.sub': 'Supports JPG, PNG, WEBP · up to 2048px',
|
||||||
|
'perfectPixel.reset': 'Reset',
|
||||||
|
'perfectPixel.sampling': 'Sampling',
|
||||||
|
'perfectPixel.sampling.mode': 'Mode (recommended)',
|
||||||
|
'perfectPixel.sampling.average': 'Average',
|
||||||
|
'perfectPixel.sampling.center': 'Center',
|
||||||
|
'perfectPixel.pitch': 'Grid Pitch',
|
||||||
|
'perfectPixel.pitch.auto': 'Auto-detect',
|
||||||
|
'perfectPixel.pitch.manual': 'Manual',
|
||||||
|
'perfectPixel.pitch.placeholder': 'Pitch in px',
|
||||||
|
'perfectPixel.upscale': 'Upscale',
|
||||||
|
'perfectPixel.run': 'Fix',
|
||||||
|
'perfectPixel.processing': 'Fixing…',
|
||||||
|
'perfectPixel.original': 'Original',
|
||||||
|
'perfectPixel.result': 'Result',
|
||||||
|
'perfectPixel.export': 'Export PNG',
|
||||||
|
'perfectPixel.detectedPitch': 'Detected grid pitch: {pitch} px',
|
||||||
|
'perfectPixel.back': '← Back to Home',
|
||||||
};
|
};
|
||||||
@@ -610,6 +610,30 @@ export const zhCN = {
|
|||||||
'announcements.empty': '暂无公告',
|
'announcements.empty': '暂无公告',
|
||||||
'announcements.back': '← 返回首页',
|
'announcements.back': '← 返回首页',
|
||||||
'announcements.version': 'v{version}',
|
'announcements.version': 'v{version}',
|
||||||
|
|
||||||
|
/* ── PerfectPixel 伪像素修复 ── */
|
||||||
|
'perfectPixel.pageTitle': '伪像素修复',
|
||||||
|
'perfectPixel.pageDesc': '上传 AI 生成的伪像素图(有抗锯齿边缘),自动重建为干净的真像素图,支持对比查看与导出。',
|
||||||
|
'perfectPixel.upload.aria': '上传一张伪像素图',
|
||||||
|
'perfectPixel.upload.title': '上传一张图片',
|
||||||
|
'perfectPixel.upload.sub': '支持 JPG、PNG、WEBP · 宽高不超过 2048px',
|
||||||
|
'perfectPixel.reset': '重置',
|
||||||
|
'perfectPixel.sampling': '采样方法',
|
||||||
|
'perfectPixel.sampling.mode': '众数(推荐)',
|
||||||
|
'perfectPixel.sampling.average': '平均值',
|
||||||
|
'perfectPixel.sampling.center': '中心点',
|
||||||
|
'perfectPixel.pitch': '网格间距',
|
||||||
|
'perfectPixel.pitch.auto': '自动检测',
|
||||||
|
'perfectPixel.pitch.manual': '手动输入',
|
||||||
|
'perfectPixel.pitch.placeholder': '输入间距 px',
|
||||||
|
'perfectPixel.upscale': '放大倍数',
|
||||||
|
'perfectPixel.run': '运行修复',
|
||||||
|
'perfectPixel.processing': '修复中…',
|
||||||
|
'perfectPixel.original': '原图',
|
||||||
|
'perfectPixel.result': '修复结果',
|
||||||
|
'perfectPixel.export': '导出 PNG',
|
||||||
|
'perfectPixel.detectedPitch': '检测到网格间距:{pitch} px',
|
||||||
|
'perfectPixel.back': '← 返回首页',
|
||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
export type Messages = { [K in keyof typeof zhCN]: string };
|
export type Messages = { [K in keyof typeof zhCN]: string };
|
||||||
|
|||||||
@@ -610,4 +610,28 @@ export const zhTW: Messages = {
|
|||||||
'announcements.empty': '暫無公告',
|
'announcements.empty': '暫無公告',
|
||||||
'announcements.back': '← 返回首頁',
|
'announcements.back': '← 返回首頁',
|
||||||
'announcements.version': 'v{version}',
|
'announcements.version': 'v{version}',
|
||||||
|
|
||||||
|
/* ── PerfectPixel 偽像素修復 ── */
|
||||||
|
'perfectPixel.pageTitle': '偽像素修復',
|
||||||
|
'perfectPixel.pageDesc': '上傳 AI 生成的偽像素圖(有抗鋸齒邊緣),自動重建為乾淨的真像素圖,支援對比查看與匯出。',
|
||||||
|
'perfectPixel.upload.aria': '上傳一張偽像素圖',
|
||||||
|
'perfectPixel.upload.title': '上傳一張圖片',
|
||||||
|
'perfectPixel.upload.sub': '支援 JPG、PNG、WEBP · 寬高不超過 2048px',
|
||||||
|
'perfectPixel.reset': '重置',
|
||||||
|
'perfectPixel.sampling': '取樣方法',
|
||||||
|
'perfectPixel.sampling.mode': '眾數(推薦)',
|
||||||
|
'perfectPixel.sampling.average': '平均值',
|
||||||
|
'perfectPixel.sampling.center': '中心點',
|
||||||
|
'perfectPixel.pitch': '網格間距',
|
||||||
|
'perfectPixel.pitch.auto': '自動偵測',
|
||||||
|
'perfectPixel.pitch.manual': '手動輸入',
|
||||||
|
'perfectPixel.pitch.placeholder': '輸入間距 px',
|
||||||
|
'perfectPixel.upscale': '放大倍數',
|
||||||
|
'perfectPixel.run': '執行修復',
|
||||||
|
'perfectPixel.processing': '修復中…',
|
||||||
|
'perfectPixel.original': '原圖',
|
||||||
|
'perfectPixel.result': '修復結果',
|
||||||
|
'perfectPixel.export': '匯出 PNG',
|
||||||
|
'perfectPixel.detectedPitch': '偵測到網格間距:{pitch} px',
|
||||||
|
'perfectPixel.back': '← 返回首頁',
|
||||||
};
|
};
|
||||||
@@ -0,0 +1,371 @@
|
|||||||
|
/**
|
||||||
|
* perfect-pixel.ts — 伪像素修复算法(纯函数,无 DOM 依赖)。
|
||||||
|
*
|
||||||
|
* 输入「AI 生成的伪像素图」(抗锯齿边缘、像素块大小不一),重建为干净的真像素图。
|
||||||
|
*
|
||||||
|
* 思路对齐 github.com/theamusing/perfectPixel:
|
||||||
|
* 1. 网格间距检测:像素块的边界处颜色突变、块内相对一致,因此在
|
||||||
|
* 边缘能量剖面(逐列/逐行的相邻像素差之和)上呈周期性 —— 用
|
||||||
|
* 归一化自相关定位真实周期(fundamental period),比"多点距离求和"
|
||||||
|
* 更抗棋盘格等同色相邻块场景。
|
||||||
|
* 2. 单元格重采样:按 pitch×pitch 网格对格内像素取样(mode/average/center)。
|
||||||
|
* 3. 重建:每格 1px 输出;放大导出用最近邻(保持硬边像素感)。
|
||||||
|
*/
|
||||||
|
|
||||||
|
export type SamplingMethod = 'mode' | 'average' | 'center';
|
||||||
|
|
||||||
|
export interface FixOptions {
|
||||||
|
/** 网格间距(px)。缺省时自动检测。 */
|
||||||
|
pitch?: number;
|
||||||
|
sampling: SamplingMethod;
|
||||||
|
/** 放大倍数(最近邻),默认 1。 */
|
||||||
|
upscale?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 优先尝试的候选 pitch(常见拼豆/像素块尺寸)。 */
|
||||||
|
const PREFERRED_PITCHES = [8, 10, 16, 20, 32, 50, 52, 78, 104] as const;
|
||||||
|
|
||||||
|
/** 兜底候选:4~64 按 2px 步进扫描,覆盖非标准块尺寸。 */
|
||||||
|
const FALLBACK_PITCHES = [
|
||||||
|
4, 6, 12, 14, 18, 22, 24, 26, 28, 30, 34, 36, 38, 40, 42, 44, 46, 48, 54, 56, 58, 60, 62, 64,
|
||||||
|
] as const;
|
||||||
|
|
||||||
|
/** 判定存在网格的最低打分(0~1)。低于此值认为图片无规则网格。 */
|
||||||
|
const GRID_CONFIDENCE_THRESHOLD = 0.18;
|
||||||
|
|
||||||
|
/** 最大 pitch 限制(不超过图像短边的 1/2,且 ≤ 128)。 */
|
||||||
|
const MAX_PITCH_LIMIT = 128;
|
||||||
|
|
||||||
|
// ─── 工具 ─────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
/** 把 RGBA 像素转为亮度(加权感知亮度),用于边缘能量剖面。 */
|
||||||
|
function luminance(data: Uint8ClampedArray, offset: number): number {
|
||||||
|
return 0.299 * data[offset]! + 0.587 * data[offset + 1]! + 0.114 * data[offset + 2]!;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 计算列 / 行边缘能量剖面。
|
||||||
|
* colEdge[x] = 第 x 列与第 x+1 列的相邻像素亮度差之和(按行抽样);
|
||||||
|
* rowEdge[y] 同理(按列抽样)。
|
||||||
|
*/
|
||||||
|
function buildEdgeProfiles(imageData: ImageData): { colEdge: Float64Array; rowEdge: Float64Array } {
|
||||||
|
const { width, height, data } = imageData;
|
||||||
|
const colEdge = new Float64Array(Math.max(width - 1, 0));
|
||||||
|
const rowEdge = new Float64Array(Math.max(height - 1, 0));
|
||||||
|
|
||||||
|
// 行/列抽样步长:大图降采样加速,小图全量。
|
||||||
|
const rowStep = Math.max(1, Math.floor(height / 256));
|
||||||
|
const colStep = Math.max(1, Math.floor(width / 256));
|
||||||
|
|
||||||
|
for (let y = 0; y < height; y += rowStep) {
|
||||||
|
const rowBase = y * width;
|
||||||
|
for (let x = 0; x < width - 1; x++) {
|
||||||
|
colEdge[x]! += Math.abs(luminance(data, (rowBase + x) * 4) - luminance(data, (rowBase + x + 1) * 4));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (let x = 0; x < width; x += colStep) {
|
||||||
|
for (let y = 0; y < height - 1; y++) {
|
||||||
|
rowEdge[y]! += Math.abs(luminance(data, (y * width + x) * 4) - luminance(data, ((y + 1) * width + x) * 4));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return { colEdge, rowEdge };
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 对一维剖面计算"滞后 lag 的自相关"(均值去除后,用全局方差归一化)。
|
||||||
|
* 全局方差归一化确保不同 lag 的分数可比较,且自然偏好真实周期
|
||||||
|
* (更多对齐对 = 更大值,整数倍滞后因窗口缩短而对齐减少)。
|
||||||
|
* 返回 [0,1];1 表示完美周期。
|
||||||
|
*/
|
||||||
|
function autocorrAtLag(profile: Float64Array, lag: number, totalVar: number): number {
|
||||||
|
const n = profile.length;
|
||||||
|
if (n <= lag || totalVar <= 0) return 0;
|
||||||
|
let sum = 0;
|
||||||
|
for (let i = 0; i < n; i++) sum += profile[i]!;
|
||||||
|
const mean = sum / n;
|
||||||
|
let num = 0;
|
||||||
|
const window = n - lag;
|
||||||
|
for (let i = 0; i < window; i++) {
|
||||||
|
const a = profile[i]! - mean;
|
||||||
|
const b = profile[i + lag]! - mean;
|
||||||
|
num += a * b;
|
||||||
|
}
|
||||||
|
return Math.max(0, num / totalVar);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在候选 pitch 列表中找到最优匹配。
|
||||||
|
* 返回 { pitch, score },score ∈ [0,1],1 表示剖面完美的 pitch 周期。
|
||||||
|
*/
|
||||||
|
function scoreCandidatePitches(
|
||||||
|
colEdge: Float64Array,
|
||||||
|
rowEdge: Float64Array,
|
||||||
|
pitches: readonly number[],
|
||||||
|
): { pitch: number; score: number } {
|
||||||
|
// 预计算全局方差(统一分母,使不同 lag 的分数可比较)。
|
||||||
|
const colVar = profileVariance(colEdge);
|
||||||
|
const rowVar = profileVariance(rowEdge);
|
||||||
|
if (colVar <= 0 && rowVar <= 0) return { pitch: 0, score: 0 };
|
||||||
|
|
||||||
|
let bestPitch = 0;
|
||||||
|
let bestScore = 0;
|
||||||
|
|
||||||
|
for (const pitch of pitches) {
|
||||||
|
const colScore = colVar > 0 ? autocorrAtLag(colEdge, pitch, colVar) : 0;
|
||||||
|
const rowScore = rowVar > 0 ? autocorrAtLag(rowEdge, pitch, rowVar) : 0;
|
||||||
|
const score = (colScore + rowScore) / 2;
|
||||||
|
if (score > bestScore) {
|
||||||
|
bestScore = score;
|
||||||
|
bestPitch = pitch;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return { pitch: bestPitch, score: bestScore };
|
||||||
|
}
|
||||||
|
|
||||||
|
function profileVariance(profile: Float64Array): number {
|
||||||
|
if (profile.length === 0) return 0;
|
||||||
|
let sum = 0;
|
||||||
|
for (let i = 0; i < profile.length; i++) sum += profile[i]!;
|
||||||
|
const mean = sum / profile.length;
|
||||||
|
let varSum = 0;
|
||||||
|
for (let i = 0; i < profile.length; i++) {
|
||||||
|
const d = profile[i]! - mean;
|
||||||
|
varSum += d * d;
|
||||||
|
}
|
||||||
|
return varSum;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── 网格间距检测 ─────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 自动检测伪像素图的网格间距(px)。
|
||||||
|
* @returns 检测到的 pitch;找不到规则网格时返回 0。
|
||||||
|
*/
|
||||||
|
export function detectPitch(imageData: ImageData): number {
|
||||||
|
const { width, height } = imageData;
|
||||||
|
const shortSide = Math.min(width, height);
|
||||||
|
if (shortSide < 8) return 0;
|
||||||
|
|
||||||
|
const { colEdge, rowEdge } = buildEdgeProfiles(imageData);
|
||||||
|
|
||||||
|
// 优先常用值,再兜底扫描,最后取最大 pitch 上限过滤。
|
||||||
|
const candidates: number[] = [];
|
||||||
|
for (const p of PREFERRED_PITCHES) {
|
||||||
|
if (p <= shortSide / 2) candidates.push(p);
|
||||||
|
}
|
||||||
|
for (const p of FALLBACK_PITCHES) {
|
||||||
|
if (p <= shortSide / 2 && !candidates.includes(p)) candidates.push(p);
|
||||||
|
}
|
||||||
|
const maxPitch = Math.min(MAX_PITCH_LIMIT, Math.floor(shortSide / 2));
|
||||||
|
const finalCandidates = candidates.filter((p) => p <= maxPitch);
|
||||||
|
if (finalCandidates.length === 0) return 0;
|
||||||
|
|
||||||
|
const { pitch, score } = scoreCandidatePitches(colEdge, rowEdge, finalCandidates);
|
||||||
|
if (pitch === 0 || score < GRID_CONFIDENCE_THRESHOLD) return 0;
|
||||||
|
return pitch;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── 单元格重采样 ─────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
const MODE_QUANT = 8; // mode 取样前的通道量化粒度(降噪)。
|
||||||
|
|
||||||
|
function quantizeChannel(v: number): number {
|
||||||
|
const q = ((v + MODE_QUANT / 2) >> 3) << 3; // 就近量化到 MODE_QUANT 的倍数
|
||||||
|
return q > 255 ? 255 : q;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** mode:统计格内出现最多的(量化后)颜色;平手取先出现者。 */
|
||||||
|
function sampleMode(
|
||||||
|
data: Uint8ClampedArray,
|
||||||
|
imgWidth: number,
|
||||||
|
imgHeight: number,
|
||||||
|
cellX: number,
|
||||||
|
cellY: number,
|
||||||
|
pitch: number,
|
||||||
|
): [number, number, number, number] {
|
||||||
|
const counts = new Map<number, number>();
|
||||||
|
let bestKey = -1;
|
||||||
|
let bestCount = 0;
|
||||||
|
|
||||||
|
const yEnd = Math.min(cellY + pitch, imgHeight);
|
||||||
|
const xEnd = Math.min(cellX + pitch, imgWidth);
|
||||||
|
|
||||||
|
for (let y = cellY; y < yEnd; y++) {
|
||||||
|
for (let x = cellX; x < xEnd; x++) {
|
||||||
|
const o = (y * imgWidth + x) * 4;
|
||||||
|
const key =
|
||||||
|
(quantizeChannel(data[o]!) << 24) |
|
||||||
|
(quantizeChannel(data[o + 1]!) << 16) |
|
||||||
|
(quantizeChannel(data[o + 2]!) << 8) |
|
||||||
|
quantizeChannel(data[o + 3]!);
|
||||||
|
const c = (counts.get(key) ?? 0) + 1;
|
||||||
|
counts.set(key, c);
|
||||||
|
if (c > bestCount) {
|
||||||
|
bestCount = c;
|
||||||
|
bestKey = key;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (bestKey < 0) {
|
||||||
|
const o = (cellY * imgWidth + cellX) * 4;
|
||||||
|
return [data[o]!, data[o + 1]!, data[o + 2]!, data[o + 3]!];
|
||||||
|
}
|
||||||
|
return [
|
||||||
|
(bestKey >>> 24) & 0xff,
|
||||||
|
(bestKey >>> 16) & 0xff,
|
||||||
|
(bestKey >>> 8) & 0xff,
|
||||||
|
bestKey & 0xff,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/** average:格内平均色。 */
|
||||||
|
function sampleAverage(
|
||||||
|
data: Uint8ClampedArray,
|
||||||
|
imgWidth: number,
|
||||||
|
imgHeight: number,
|
||||||
|
cellX: number,
|
||||||
|
cellY: number,
|
||||||
|
pitch: number,
|
||||||
|
): [number, number, number, number] {
|
||||||
|
let r = 0, g = 0, b = 0, a = 0, n = 0;
|
||||||
|
const yEnd = Math.min(cellY + pitch, imgHeight);
|
||||||
|
const xEnd = Math.min(cellX + pitch, imgWidth);
|
||||||
|
for (let y = cellY; y < yEnd; y++) {
|
||||||
|
for (let x = cellX; x < xEnd; x++) {
|
||||||
|
const o = (y * imgWidth + x) * 4;
|
||||||
|
r += data[o]!;
|
||||||
|
g += data[o + 1]!;
|
||||||
|
b += data[o + 2]!;
|
||||||
|
a += data[o + 3]!;
|
||||||
|
n++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (n === 0) return [0, 0, 0, 255];
|
||||||
|
return [Math.round(r / n), Math.round(g / n), Math.round(b / n), Math.round(a / n)];
|
||||||
|
}
|
||||||
|
|
||||||
|
/** center:取格中心像素。 */
|
||||||
|
function sampleCenter(
|
||||||
|
data: Uint8ClampedArray,
|
||||||
|
imgWidth: number,
|
||||||
|
_imgHeight: number,
|
||||||
|
cellX: number,
|
||||||
|
cellY: number,
|
||||||
|
pitch: number,
|
||||||
|
): [number, number, number, number] {
|
||||||
|
const cx = Math.min(cellX + Math.floor(pitch / 2), imgWidth - 1);
|
||||||
|
const cy = cellY + Math.floor(pitch / 2);
|
||||||
|
const o = (cy * imgWidth + cx) * 4;
|
||||||
|
return [data[o]!, data[o + 1]!, data[o + 2]!, data[o + 3]!];
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── 主流程 ───────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 核心修复流程:伪像素 ImageData → 每格 1px 的干净真像素 ImageData。
|
||||||
|
*
|
||||||
|
* @param imageData 输入图(RGBA)。
|
||||||
|
* @param options 修复参数(pitch 缺省时自动检测)。
|
||||||
|
*/
|
||||||
|
export function fixPseudoPixel(imageData: ImageData, options: FixOptions): ImageData {
|
||||||
|
const { width, height, data } = imageData;
|
||||||
|
if (width <= 0 || height <= 0) {
|
||||||
|
throw new Error('图片尺寸无效');
|
||||||
|
}
|
||||||
|
|
||||||
|
const pitch = options.pitch ?? 0;
|
||||||
|
if (pitch <= 0) {
|
||||||
|
throw new Error('网格间距无效,请手动指定或让系统自动检测');
|
||||||
|
}
|
||||||
|
const p = Math.floor(pitch);
|
||||||
|
if (p <= 0 || p > Math.min(width, height)) {
|
||||||
|
throw new Error(`网格间距超出范围:${pitch}px`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const cellW = Math.floor(width / p);
|
||||||
|
const cellH = Math.floor(height / p);
|
||||||
|
if (cellW <= 0 || cellH <= 0) {
|
||||||
|
throw new Error('网格间距过大,无法重建');
|
||||||
|
}
|
||||||
|
|
||||||
|
const out = new ImageData(cellW, cellH);
|
||||||
|
const outData = out.data;
|
||||||
|
|
||||||
|
for (let cy = 0; cy < cellH; cy++) {
|
||||||
|
for (let cx = 0; cx < cellW; cx++) {
|
||||||
|
const cellX = cx * p;
|
||||||
|
const cellY = cy * p;
|
||||||
|
const [r, g, b, a] = resampleCell(data, width, height, cellX, cellY, p, options.sampling);
|
||||||
|
const o = (cy * cellW + cx) * 4;
|
||||||
|
outData[o] = r;
|
||||||
|
outData[o + 1] = g;
|
||||||
|
outData[o + 2] = b;
|
||||||
|
outData[o + 3] = a;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
function resampleCell(
|
||||||
|
data: Uint8ClampedArray,
|
||||||
|
imgWidth: number,
|
||||||
|
imgHeight: number,
|
||||||
|
cellX: number,
|
||||||
|
cellY: number,
|
||||||
|
pitch: number,
|
||||||
|
sampling: SamplingMethod,
|
||||||
|
): [number, number, number, number] {
|
||||||
|
switch (sampling) {
|
||||||
|
case 'mode':
|
||||||
|
return sampleMode(data, imgWidth, imgHeight, cellX, cellY, pitch);
|
||||||
|
case 'average':
|
||||||
|
return sampleAverage(data, imgWidth, imgHeight, cellX, cellY, pitch);
|
||||||
|
case 'center':
|
||||||
|
return sampleCenter(data, imgWidth, imgHeight, cellX, cellY, pitch);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── 导出辅助 ─────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
/** 把 ImageData 拷贝到同尺寸 canvas 上。 */
|
||||||
|
export function imageDataToCanvas(data: ImageData): HTMLCanvasElement {
|
||||||
|
const canvas = document.createElement('canvas');
|
||||||
|
canvas.width = data.width;
|
||||||
|
canvas.height = data.height;
|
||||||
|
const ctx = canvas.getContext('2d');
|
||||||
|
if (!ctx) {
|
||||||
|
throw new Error('无法创建画布上下文');
|
||||||
|
}
|
||||||
|
ctx.putImageData(data, 0, 0);
|
||||||
|
return canvas;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 最近邻放大(保持像素硬边)。factor 必须为 >=1 的整数。 */
|
||||||
|
export function upscaleNearest(data: ImageData, factor: number): ImageData {
|
||||||
|
if (!Number.isInteger(factor) || factor < 1) {
|
||||||
|
throw new Error(`放大倍数无效:${factor}`);
|
||||||
|
}
|
||||||
|
if (factor === 1) return data;
|
||||||
|
const out = new ImageData(data.width * factor, data.height * factor);
|
||||||
|
const src = data.data;
|
||||||
|
const dst = out.data;
|
||||||
|
for (let y = 0; y < out.height; y++) {
|
||||||
|
const sy = Math.floor(y / factor);
|
||||||
|
const srcRow = sy * data.width;
|
||||||
|
const dstRow = y * out.width;
|
||||||
|
for (let x = 0; x < out.width; x++) {
|
||||||
|
const sx = Math.floor(x / factor);
|
||||||
|
const si = (srcRow + sx) * 4;
|
||||||
|
const di = (dstRow + x) * 4;
|
||||||
|
dst[di] = src[si]!;
|
||||||
|
dst[di + 1] = src[si + 1]!;
|
||||||
|
dst[di + 2] = src[si + 2]!;
|
||||||
|
dst[di + 3] = src[si + 3]!;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
@@ -0,0 +1,499 @@
|
|||||||
|
/**
|
||||||
|
* PerfectPixelPage — 伪像素修复工具 (/tools/perfect-pixel)
|
||||||
|
*
|
||||||
|
* 上传 AI 生成的伪像素图 → 算法重建为真像素图 → 前后对比 + 导出。
|
||||||
|
* 纯前端 Canvas 实现,零新依赖。
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { useCallback, useEffect, useRef, useState } from 'react';
|
||||||
|
import './pages.css';
|
||||||
|
import { useT } from '../i18n';
|
||||||
|
import {
|
||||||
|
detectPitch,
|
||||||
|
fixPseudoPixel,
|
||||||
|
upscaleNearest,
|
||||||
|
imageDataToCanvas,
|
||||||
|
type SamplingMethod,
|
||||||
|
} from '../lib/perfect-pixel';
|
||||||
|
|
||||||
|
// ─── 常量 ─────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
const MAX_IMAGE_DIMENSION = 2048;
|
||||||
|
const ALLOWED_TYPES = ['image/jpeg', 'image/png', 'image/webp'];
|
||||||
|
|
||||||
|
const UPSCALE_OPTIONS = [1, 4, 8, 10] as const;
|
||||||
|
|
||||||
|
const SAMPLING_OPTIONS: { value: SamplingMethod; labelKey: string }[] = [
|
||||||
|
{ value: 'mode', labelKey: 'perfectPixel.sampling.mode' },
|
||||||
|
{ value: 'average', labelKey: 'perfectPixel.sampling.average' },
|
||||||
|
{ value: 'center', labelKey: 'perfectPixel.sampling.center' },
|
||||||
|
];
|
||||||
|
|
||||||
|
// ─── 帮助函数 ─────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
/** 加载图片到 canvas 并提取 ImageData(限制尺寸 2048px)。 */
|
||||||
|
function loadImageData(
|
||||||
|
file: File,
|
||||||
|
): Promise<{ imageData: ImageData; width: number; height: number }> {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
if (!ALLOWED_TYPES.includes(file.type)) {
|
||||||
|
reject(new Error('不支持的文件格式,请上传 JPG/PNG/WebP 图片'));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const img = new Image();
|
||||||
|
const url = URL.createObjectURL(file);
|
||||||
|
|
||||||
|
img.onload = () => {
|
||||||
|
const w = img.naturalWidth;
|
||||||
|
const h = img.naturalHeight;
|
||||||
|
if (w > MAX_IMAGE_DIMENSION || h > MAX_IMAGE_DIMENSION) {
|
||||||
|
URL.revokeObjectURL(url);
|
||||||
|
reject(new Error('图片过大,宽或高不能超过 2048px'));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const canvas = document.createElement('canvas');
|
||||||
|
canvas.width = w;
|
||||||
|
canvas.height = h;
|
||||||
|
const ctx = canvas.getContext('2d');
|
||||||
|
if (!ctx) {
|
||||||
|
URL.revokeObjectURL(url);
|
||||||
|
reject(new Error('无法创建画布上下文'));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
ctx.drawImage(img, 0, 0);
|
||||||
|
URL.revokeObjectURL(url);
|
||||||
|
const imageData = ctx.getImageData(0, 0, w, h);
|
||||||
|
resolve({ imageData, width: w, height: h });
|
||||||
|
};
|
||||||
|
|
||||||
|
img.onerror = () => {
|
||||||
|
URL.revokeObjectURL(url);
|
||||||
|
reject(new Error('图片读取失败,请换一张重试'));
|
||||||
|
};
|
||||||
|
|
||||||
|
img.src = url;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 下载 canvas 为 PNG。 */
|
||||||
|
function downloadCanvas(canvas: HTMLCanvasElement, filename: string): void {
|
||||||
|
canvas.toBlob((blob) => {
|
||||||
|
if (!blob) return;
|
||||||
|
const url = URL.createObjectURL(blob);
|
||||||
|
const a = document.createElement('a');
|
||||||
|
a.href = url;
|
||||||
|
a.download = filename;
|
||||||
|
a.click();
|
||||||
|
URL.revokeObjectURL(url);
|
||||||
|
}, 'image/png');
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── 页面组件 ─────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
export default function PerfectPixelPage() {
|
||||||
|
const { t } = useT();
|
||||||
|
const fileInputRef = useRef<HTMLInputElement | null>(null);
|
||||||
|
|
||||||
|
// 上传状态
|
||||||
|
const [sourceUrl, setSourceUrl] = useState<string | null>(null);
|
||||||
|
const [sourceFile, setSourceFile] = useState<File | null>(null);
|
||||||
|
const [imageData, setImageData] = useState<ImageData | null>(null);
|
||||||
|
const [uploadError, setUploadError] = useState<string | null>(null);
|
||||||
|
const [processing, setProcessing] = useState(false);
|
||||||
|
|
||||||
|
// 参数
|
||||||
|
const [sampling, setSampling] = useState<SamplingMethod>('mode');
|
||||||
|
const [pitchAuto, setPitchAuto] = useState(true);
|
||||||
|
const [manualPitch, setManualPitch] = useState('');
|
||||||
|
const [upscale, setUpscale] = useState<number>(1);
|
||||||
|
|
||||||
|
// 结果
|
||||||
|
const [resultImageData, setResultImageData] = useState<ImageData | null>(null);
|
||||||
|
const [detectedPitch, setDetectedPitch] = useState<number>(0);
|
||||||
|
const [resultError, setResultError] = useState<string | null>(null);
|
||||||
|
|
||||||
|
// 释放旧 URL
|
||||||
|
useEffect(() => {
|
||||||
|
return () => {
|
||||||
|
if (sourceUrl) URL.revokeObjectURL(sourceUrl);
|
||||||
|
};
|
||||||
|
}, [sourceUrl]);
|
||||||
|
|
||||||
|
const handleFiles = useCallback(
|
||||||
|
(files: FileList | null) => {
|
||||||
|
const file = files?.[0];
|
||||||
|
if (!file) return;
|
||||||
|
setSourceFile(file);
|
||||||
|
setUploadError(null);
|
||||||
|
setResultImageData(null);
|
||||||
|
setResultError(null);
|
||||||
|
setDetectedPitch(0);
|
||||||
|
|
||||||
|
// 创建预览 URL
|
||||||
|
if (sourceUrl) URL.revokeObjectURL(sourceUrl);
|
||||||
|
const url = URL.createObjectURL(file);
|
||||||
|
setSourceUrl(url);
|
||||||
|
|
||||||
|
// 加载 ImageData
|
||||||
|
loadImageData(file)
|
||||||
|
.then(({ imageData }) => {
|
||||||
|
setImageData(imageData);
|
||||||
|
})
|
||||||
|
.catch((err: unknown) => {
|
||||||
|
setUploadError(err instanceof Error ? err.message : '图片加载失败');
|
||||||
|
setImageData(null);
|
||||||
|
if (sourceUrl) URL.revokeObjectURL(sourceUrl);
|
||||||
|
setSourceUrl(null);
|
||||||
|
setSourceFile(null);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
[sourceUrl],
|
||||||
|
);
|
||||||
|
|
||||||
|
const handleDrop = useCallback(
|
||||||
|
(e: React.DragEvent) => {
|
||||||
|
e.preventDefault();
|
||||||
|
handleFiles(e.dataTransfer.files);
|
||||||
|
},
|
||||||
|
[handleFiles],
|
||||||
|
);
|
||||||
|
|
||||||
|
const handleDragOver = useCallback((e: React.DragEvent) => {
|
||||||
|
e.preventDefault();
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const handleReset = useCallback(() => {
|
||||||
|
if (sourceUrl) URL.revokeObjectURL(sourceUrl);
|
||||||
|
setSourceUrl(null);
|
||||||
|
setSourceFile(null);
|
||||||
|
setImageData(null);
|
||||||
|
setUploadError(null);
|
||||||
|
setResultImageData(null);
|
||||||
|
setResultError(null);
|
||||||
|
setDetectedPitch(0);
|
||||||
|
}, [sourceUrl]);
|
||||||
|
|
||||||
|
/** 运行修复。 */
|
||||||
|
const handleRun = useCallback(() => {
|
||||||
|
if (!imageData) return;
|
||||||
|
|
||||||
|
setProcessing(true);
|
||||||
|
setResultError(null);
|
||||||
|
|
||||||
|
// 使用 setTimeout 让 UI 保持响应(大图同步处理不可分割,但限制 2048px 内可接受)。
|
||||||
|
setTimeout(() => {
|
||||||
|
try {
|
||||||
|
// 自动检测 pitch
|
||||||
|
const autoPitch = detectPitch(imageData);
|
||||||
|
const pitch = pitchAuto ? autoPitch : Math.floor(Number(manualPitch) || 0);
|
||||||
|
|
||||||
|
if (pitch <= 0) {
|
||||||
|
if (pitchAuto) {
|
||||||
|
throw new Error('未能自动检测到网格间距,请手动输入或换一张图重试');
|
||||||
|
} else {
|
||||||
|
throw new Error('请输入有效的网格间距(正整数)');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
setDetectedPitch(pitch);
|
||||||
|
|
||||||
|
const result = fixPseudoPixel(imageData, { pitch, sampling });
|
||||||
|
|
||||||
|
// 应用放大
|
||||||
|
const finalResult = upscale > 1 ? upscaleNearest(result, upscale) : result;
|
||||||
|
setResultImageData(finalResult);
|
||||||
|
} catch (err: unknown) {
|
||||||
|
setResultError(err instanceof Error ? err.message : '修复失败');
|
||||||
|
setResultImageData(null);
|
||||||
|
} finally {
|
||||||
|
setProcessing(false);
|
||||||
|
}
|
||||||
|
}, 0);
|
||||||
|
}, [imageData, pitchAuto, manualPitch, sampling, upscale]);
|
||||||
|
|
||||||
|
/** 导出修复结果。 */
|
||||||
|
const handleExport = useCallback(() => {
|
||||||
|
if (!resultImageData) return;
|
||||||
|
try {
|
||||||
|
const canvas = imageDataToCanvas(resultImageData);
|
||||||
|
const filename = sourceFile
|
||||||
|
? sourceFile.name.replace(/\.[^.]+$/, '') + '_perfect-pixel.png'
|
||||||
|
: 'perfect-pixel.png';
|
||||||
|
downloadCanvas(canvas, filename);
|
||||||
|
} catch {
|
||||||
|
setResultError('导出失败');
|
||||||
|
}
|
||||||
|
}, [resultImageData, sourceFile]);
|
||||||
|
|
||||||
|
const hasImage = imageData !== null;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="pl-page">
|
||||||
|
<main className="pl-main">
|
||||||
|
{/* 标题区 */}
|
||||||
|
<header className="pl-hero">
|
||||||
|
<h1 className="pl-h1">{t('perfectPixel.pageTitle')}</h1>
|
||||||
|
<p className="pl-sub">{t('perfectPixel.pageDesc')}</p>
|
||||||
|
</header>
|
||||||
|
|
||||||
|
{/* 上传区 */}
|
||||||
|
{!sourceUrl ? (
|
||||||
|
<div
|
||||||
|
className="upload-zone"
|
||||||
|
role="button"
|
||||||
|
tabIndex={0}
|
||||||
|
aria-label={t('perfectPixel.upload.aria')}
|
||||||
|
onClick={() => fileInputRef.current?.click()}
|
||||||
|
onKeyDown={(e) => {
|
||||||
|
if (e.key === 'Enter' || e.key === ' ') {
|
||||||
|
fileInputRef.current?.click();
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
onDrop={handleDrop}
|
||||||
|
onDragOver={handleDragOver}
|
||||||
|
>
|
||||||
|
<span className="upload-plus" aria-hidden="true">
|
||||||
|
+
|
||||||
|
</span>
|
||||||
|
<span className="upload-title">{t('perfectPixel.upload.title')}</span>
|
||||||
|
<span className="upload-sub">{t('perfectPixel.upload.sub')}</span>
|
||||||
|
{uploadError && <span className="upload-error">{uploadError}</span>}
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<div className="file-row">
|
||||||
|
{sourceUrl && (
|
||||||
|
<img
|
||||||
|
className="file-thumb"
|
||||||
|
src={sourceUrl}
|
||||||
|
alt={sourceFile?.name ?? ''}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
<div className="file-info">
|
||||||
|
<div className="file-name" title={sourceFile?.name ?? ''}>
|
||||||
|
{sourceFile?.name ?? ''}
|
||||||
|
</div>
|
||||||
|
<div className="file-meta">
|
||||||
|
{imageData?.width ?? 0} × {imageData?.height ?? 0}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<button type="button" className="file-reset" onClick={handleReset}>
|
||||||
|
{t('perfectPixel.reset')}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<input
|
||||||
|
ref={fileInputRef}
|
||||||
|
type="file"
|
||||||
|
accept="image/jpeg,image/png,image/webp"
|
||||||
|
hidden
|
||||||
|
onChange={(e) => {
|
||||||
|
handleFiles(e.target.files);
|
||||||
|
e.target.value = '';
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
|
||||||
|
{/* 参数区 */}
|
||||||
|
{hasImage && (
|
||||||
|
<section style={{ marginTop: 'var(--space-5)' }}>
|
||||||
|
{/* 采样方法 */}
|
||||||
|
<div className="bd-settings" style={{ marginBottom: 'var(--space-4)' }}>
|
||||||
|
<span className="bd-settings-label">{t('perfectPixel.sampling')}</span>
|
||||||
|
<div className="bd-settings-options">
|
||||||
|
{SAMPLING_OPTIONS.map((opt) => (
|
||||||
|
<button
|
||||||
|
key={opt.value}
|
||||||
|
type="button"
|
||||||
|
className={`bd-settings-btn${sampling === opt.value ? ' is-active' : ''}`}
|
||||||
|
aria-pressed={sampling === opt.value}
|
||||||
|
onClick={() => setSampling(opt.value)}
|
||||||
|
>
|
||||||
|
{t(opt.labelKey as any)}
|
||||||
|
</button>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* 网格间距 */}
|
||||||
|
<div className="bd-settings" style={{ marginBottom: 'var(--space-4)' }}>
|
||||||
|
<span className="bd-settings-label">{t('perfectPixel.pitch')}</span>
|
||||||
|
<div className="bd-settings-options">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className={`bd-settings-btn${pitchAuto ? ' is-active' : ''}`}
|
||||||
|
aria-pressed={pitchAuto}
|
||||||
|
onClick={() => setPitchAuto(true)}
|
||||||
|
>
|
||||||
|
{t('perfectPixel.pitch.auto')}
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className={`bd-settings-btn${!pitchAuto ? ' is-active' : ''}`}
|
||||||
|
aria-pressed={!pitchAuto}
|
||||||
|
onClick={() => setPitchAuto(false)}
|
||||||
|
>
|
||||||
|
{t('perfectPixel.pitch.manual')}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
{!pitchAuto && (
|
||||||
|
<input
|
||||||
|
className="bd-add-input"
|
||||||
|
type="number"
|
||||||
|
min={2}
|
||||||
|
step={1}
|
||||||
|
value={manualPitch}
|
||||||
|
onChange={(e) => setManualPitch(e.target.value)}
|
||||||
|
placeholder={t('perfectPixel.pitch.placeholder')}
|
||||||
|
style={{ width: 120, marginTop: 'var(--space-2)' }}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* 放大倍数 */}
|
||||||
|
<div className="bd-settings" style={{ marginBottom: 'var(--space-4)' }}>
|
||||||
|
<span className="bd-settings-label">{t('perfectPixel.upscale')}</span>
|
||||||
|
<div className="bd-settings-options">
|
||||||
|
{UPSCALE_OPTIONS.map((opt) => (
|
||||||
|
<button
|
||||||
|
key={opt}
|
||||||
|
type="button"
|
||||||
|
className={`bd-settings-btn${upscale === opt ? ' is-active' : ''}`}
|
||||||
|
aria-pressed={upscale === opt}
|
||||||
|
onClick={() => setUpscale(opt)}
|
||||||
|
>
|
||||||
|
{opt}x
|
||||||
|
</button>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* 运行按钮 */}
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="bd-empty-btn bd-empty-btn--primary"
|
||||||
|
disabled={processing}
|
||||||
|
onClick={handleRun}
|
||||||
|
style={{ marginBottom: 'var(--space-5)' }}
|
||||||
|
>
|
||||||
|
{processing ? t('perfectPixel.processing') : t('perfectPixel.run')}
|
||||||
|
</button>
|
||||||
|
|
||||||
|
{/* 错误消息 */}
|
||||||
|
{resultError && (
|
||||||
|
<p style={{ color: 'var(--danger)', fontSize: 'var(--text-sm)', marginBottom: 'var(--space-4)' }}>
|
||||||
|
{resultError}
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* 检测到间距 */}
|
||||||
|
{detectedPitch > 0 && (
|
||||||
|
<p style={{ fontSize: 'var(--text-sm)', color: 'var(--text-secondary)', marginBottom: 'var(--space-4)' }}>
|
||||||
|
{t('perfectPixel.detectedPitch', { pitch: String(detectedPitch) })}
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* 前后对比 */}
|
||||||
|
{resultImageData && (
|
||||||
|
<>
|
||||||
|
<div className="pp-compare" style={{
|
||||||
|
display: 'grid',
|
||||||
|
gridTemplateColumns: '1fr 1fr',
|
||||||
|
gap: 'var(--space-4)',
|
||||||
|
marginBottom: 'var(--space-5)',
|
||||||
|
}}>
|
||||||
|
{/* 原图 */}
|
||||||
|
<div className="pp-compare-col" style={{
|
||||||
|
background: 'var(--bg-card)',
|
||||||
|
border: '1px solid var(--border)',
|
||||||
|
borderRadius: 'var(--radius-lg)',
|
||||||
|
padding: 'var(--space-3)',
|
||||||
|
textAlign: 'center',
|
||||||
|
}}>
|
||||||
|
<span style={{
|
||||||
|
display: 'block',
|
||||||
|
fontSize: 'var(--text-sm)',
|
||||||
|
fontWeight: 600,
|
||||||
|
color: 'var(--text-secondary)',
|
||||||
|
marginBottom: 'var(--space-2)',
|
||||||
|
}}>
|
||||||
|
{t('perfectPixel.original')}
|
||||||
|
</span>
|
||||||
|
<canvas
|
||||||
|
ref={(el) => {
|
||||||
|
if (el && imageData) {
|
||||||
|
el.width = imageData.width;
|
||||||
|
el.height = imageData.height;
|
||||||
|
el.getContext('2d')?.putImageData(imageData, 0, 0);
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
style={{
|
||||||
|
maxWidth: '100%',
|
||||||
|
height: 'auto',
|
||||||
|
imageRendering: 'pixelated',
|
||||||
|
borderRadius: 'var(--radius-sm)',
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* 修复结果 */}
|
||||||
|
<div className="pp-compare-col" style={{
|
||||||
|
background: 'var(--bg-card)',
|
||||||
|
border: '1px solid var(--border)',
|
||||||
|
borderRadius: 'var(--radius-lg)',
|
||||||
|
padding: 'var(--space-3)',
|
||||||
|
textAlign: 'center',
|
||||||
|
}}>
|
||||||
|
<span style={{
|
||||||
|
display: 'block',
|
||||||
|
fontSize: 'var(--text-sm)',
|
||||||
|
fontWeight: 600,
|
||||||
|
color: 'var(--text-secondary)',
|
||||||
|
marginBottom: 'var(--space-2)',
|
||||||
|
}}>
|
||||||
|
{t('perfectPixel.result')}
|
||||||
|
</span>
|
||||||
|
<canvas
|
||||||
|
ref={(el) => {
|
||||||
|
if (el && resultImageData) {
|
||||||
|
el.width = resultImageData.width;
|
||||||
|
el.height = resultImageData.height;
|
||||||
|
el.getContext('2d')?.putImageData(resultImageData, 0, 0);
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
style={{
|
||||||
|
maxWidth: '100%',
|
||||||
|
height: 'auto',
|
||||||
|
imageRendering: 'pixelated',
|
||||||
|
borderRadius: 'var(--radius-sm)',
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* 导出按钮 */}
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="bd-empty-btn bd-empty-btn--outline"
|
||||||
|
onClick={handleExport}
|
||||||
|
>
|
||||||
|
{t('perfectPixel.export')}
|
||||||
|
</button>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</section>
|
||||||
|
)}
|
||||||
|
</main>
|
||||||
|
|
||||||
|
<footer className="pl-footer">
|
||||||
|
<div className="pl-footer-inner">
|
||||||
|
<span className="pl-footer-text">{t('footer.slogan')}</span>
|
||||||
|
<a className="pl-footer-link" href="/">
|
||||||
|
{t('perfectPixel.back')}
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
</footer>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
Reference in new issue
Block a user