File size: 3,645 Bytes
a34a6d1
 
 
 
 
 
 
 
 
 
 
 
1a616a5
 
a34a6d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
"""
Unified Processing Pipeline for VectorForge.
Coordinates AI super-resolution and mathematical vectorization.
"""

from dataclasses import dataclass
from pathlib import Path
import time
import uuid
from typing import Optional, Dict, Any
from PIL import Image

from engine.upscaler import AI2DUpscaler
from engine.vectorizer import Vectorizer


@dataclass
class ProcessingResult:
    job_id: str
    mode: str
    original_size: tuple[int, int]
    enhanced_size: tuple[int, int]
    original_filesize: int
    enhanced_png_filesize: Optional[int]
    svg_filesize: Optional[int]
    elapsed_seconds: float
    png_url: Optional[str]
    svg_url: Optional[str]
    svg_preview: Optional[str]


class ProcessingPipeline:
    def __init__(self, output_dir: Path):
        self.output_dir = Path(output_dir)
        self.output_dir.mkdir(parents=True, exist_ok=True)
        self.upscaler = AI2DUpscaler()

    def process(
        self,
        input_image_path: Path,
        mode: str = "hybrid",          # "hybrid", "ai_upscale", "vectorize"
        scale: int = 4,                # 2 or 4
        denoise: bool = True,
        vector_preset: str = "character",
        custom_vector_params: Optional[Dict[str, Any]] = None,
    ) -> ProcessingResult:
        t0 = time.time()
        job_id = uuid.uuid4().hex[:12]

        with Image.open(input_image_path) as img:
            orig_w, orig_h = img.size
        orig_filesize = input_image_path.stat().st_size

        png_path: Optional[Path] = None
        svg_path: Optional[Path] = None
        enhanced_w, enhanced_h = orig_w, orig_h
        svg_preview_content: Optional[str] = None

        if mode in ("hybrid", "ai_upscale"):
            png_path = self.output_dir / f"{job_id}_enhanced.png"
            self.upscaler.upscale_file(
                input_image_path,
                png_path,
                scale=scale,
                denoise=denoise,
            )
            with Image.open(png_path) as up_img:
                enhanced_w, enhanced_h = up_img.size

        if mode == "vectorize":
            svg_path = self.output_dir / f"{job_id}_vector.svg"
            Vectorizer.vectorize_file(
                input_image_path,
                svg_path,
                preset=vector_preset,
                custom_params=custom_vector_params,
            )
            enhanced_w, enhanced_h = orig_w, orig_h

        elif mode == "hybrid":
            # Vectorize the cleaned/upscaled raster
            svg_path = self.output_dir / f"{job_id}_vector.svg"
            Vectorizer.vectorize_file(
                png_path,
                svg_path,
                preset=vector_preset,
                custom_params=custom_vector_params,
            )

        # Read SVG content for inline UI preview (up to 200KB)
        if svg_path and svg_path.exists():
            svg_text = svg_path.read_text(encoding="utf-8")
            svg_preview_content = svg_text

        elapsed = round(time.time() - t0, 2)

        return ProcessingResult(
            job_id=job_id,
            mode=mode,
            original_size=(orig_w, orig_h),
            enhanced_size=(enhanced_w, enhanced_h),
            original_filesize=orig_filesize,
            enhanced_png_filesize=png_path.stat().st_size if png_path and png_path.exists() else None,
            svg_filesize=svg_path.stat().st_size if svg_path and svg_path.exists() else None,
            elapsed_seconds=elapsed,
            png_url=f"/api/download/{job_id}/png" if png_path else None,
            svg_url=f"/api/download/{job_id}/svg" if svg_path else None,
            svg_preview=svg_preview_content,
        )