// SPDX-License-Identifier: GPL-3.0-or-later // Copyright (C) 2026 ZiroEDA and contributors. // Portions derived from KiCad, copyright The KiCad Developers. See NOTICE.md. /** * `BITMAP2CMP_PANEL`'s window: `bitmap2cmp_panel_base.cpp` (wxFormBuilder) * and the three `OnPaint*` handlers of `bitmap2cmp_panel.cpp`. Everything it * shows is a field of the `BITMAP2CMP_PANEL` it is given, and every control * calls that panel's handler; nothing is decided here. * * Its stylesheet is `bitmap2cmp_panel.css`, which the window that hosts it * imports (the package does not compile CSS). */ import { CheckBox, RadioButton, TextCtrl } from '@ziroeda/wx/controls.js'; import { Fragment, type JSX, useEffect, useRef } from 'react'; import { Slider } from '@ziroeda/common/widgets/slider.js'; import { Combo } from '@ziroeda/wx/combobox.js'; import { LAYER_CHOICES, NOTEBOOK_PAGES, PIXEL_UNIT_CHOICES, type BITMAP2CMP_PANEL, type DROP_FILE, type IMAGE_FILE, } from './bitmap2cmp_panel.js'; import { DRAWING_SHEET_FMT, FOOTPRINT_FMT, POSTSCRIPT_FMT, SYMBOL_FMT, type OUTPUT_FMT_ID, } from './bitmap2component.js'; import type { WX_IMAGE } from '@ziroeda/wx/wx_image.js'; /** The Output Format radios, in `fgSizer2` order, with the Layer row after Footprint. [data] */ const FORMATS: { id: OUTPUT_FMT_ID; label: string }[] = [ { id: SYMBOL_FMT, label: 'Symbol (.kicad_sym file)' }, { id: FOOTPRINT_FMT, label: 'Footprint (.kicad_mod file)' }, { id: POSTSCRIPT_FMT, label: 'Postscript (.ps file)' }, { id: DRAWING_SHEET_FMT, label: 'Drawing Sheet (.kicad_wks file)' }, ]; /** `m_sliderThreshold->SetToolTip(...)`. [data] */ const THRESHOLD_TOOLTIP = 'Adjust the level to convert the greyscale picture to a black and white picture.'; /** * Read a file the browser hands over (the file dialog or a drop) into what * `OpenProjectFiles` takes: its name and its bytes, which wxImage::LoadFile decodes. */ export async function readImageFile(aFile: Blob & { name: string }): Promise { return { name: aFile.name, bytes: new Uint8Array(await aFile.arrayBuffer()) }; } /** * A page's wxEVT_PAINT handler: `PrepareDC` then `DrawBitmap( bmp, 0, 0, * !!bmp.GetMask() )`. The canvas is sized to the bitmap itself, never to the * pane, so the preview is 1:1 and the pane scrolls. A masked pixel, and an * alpha one, show the page through. */ function paintPage(cv: HTMLCanvasElement | null, bmp: WX_IMAGE | null): void { if (!cv || !bmp || !bmp.IsOk()) return; const w = bmp.GetWidth(); const h = bmp.GetHeight(); const data = new ImageData(w, h); for (let y = 0; y < h; y++) { for (let x = 0; x < w; x++) { const i = (y * w + x) * 4; const r = bmp.GetRed(x, y); const g = bmp.GetGreen(x, y); const b = bmp.GetBlue(x, y); const masked = bmp.HasMask() && r === bmp.GetMaskRed() && g === bmp.GetMaskGreen() && b === bmp.GetMaskBlue(); data.data[i] = r; data.data[i + 1] = g; data.data[i + 2] = b; data.data[i + 3] = masked ? 0 : bmp.HasAlpha() ? bmp.GetAlpha(x, y) : 255; } } cv.width = data.width; cv.height = data.height; cv.getContext('2d')?.putImageData(data, 0, 0); } export interface Bitmap2cmpPanelProps { panel: BITMAP2CMP_PANEL; /** The DROP_FILE installed on all three pages. */ dropTarget: DROP_FILE; /** Bumped by the panel's `Refresh()`, so this re-renders. */ version: number; } export function Bitmap2cmpPanel({ panel, dropTarget, version }: Bitmap2cmpPanelProps): JSX.Element { // One canvas per notebook page, mirroring the three wxScrolledWindows and // the three bitmaps (m_Pict_Bitmap / m_Greyscale_Bitmap / m_BN_Bitmap) that // BITMAP2CMP_PANEL keeps alive at once. Each page then scrolls on its own. const originalCanvasRef = useRef(null); const greyscaleCanvasRef = useRef(null); const bwCanvasRef = useRef(null); const pict = panel.m_Pict_Bitmap?.image ?? null; const greyscale = panel.m_Greyscale_Bitmap; const bw = panel.m_BN_Bitmap; // OnPaintInit / OnPaintGreyscale / OnPaintBW each draw their own bitmap at // (0, 0) into their own page; a threshold change rebuilds only m_BN_Bitmap, // a Negative click m_Greyscale_Bitmap and m_BN_Bitmap. `version` is not read: // the bitmaps are the dependencies, and a handler replaces the object when // it rebuilds one. useEffect(() => { paintPage(originalCanvasRef.current, pict); }, [pict]); useEffect(() => { paintPage(greyscaleCanvasRef.current, greyscale); }, [greyscale]); useEffect(() => { paintPage(bwCanvasRef.current, bw); }, [bw]); const onDrop = (e: React.DragEvent): void => { e.preventDefault(); const files = [...(e.dataTransfer.files ?? [])]; if (files.length === 0) return; void (async () => { const read = await Promise.all(files.map((f) => readImageFile(f))); await dropTarget.OnDropFiles(read); })(); }; const loaded = pict !== null; const canvases = [originalCanvasRef, greyscaleCanvasRef, bwCanvasRef]; return (
{/* bMainSizer: the notebook, then brightSizer */}
{/* The notebook has no page-change handler at all (bitmap2cmp_panel_base.cpp connects paint, buttons, fields and radios - never the notebook), so a tab click only selects a page. */}
{NOTEBOOK_PAGES.map((label, i) => ( ))}
e.preventDefault()} onDrop={onDrop}> {/* All three pages stay mounted, as AddPage() keeps all three wxScrolledWindows alive; only the selected one is visible, and each holds its own scroll offset across a tab switch. Before a file is loaded they are blank: KiCad paints no placeholder. */} {canvases.map((ref, i) => (
{loaded && }
))}
{/* brightSizer, group by group */}
Image Information
Image size: {panel.m_SizeXValue} {panel.m_SizeYValue} pixels Image PPI: {panel.m_InputXValueDPI} {panel.m_InputYValueDPI} PPI {/* Three cells then fgSizerInfo->Add( 0, 0, ... ), so "bits" is in column 3 and the empty cell trails it. */} BPP: {panel.m_BPPValue} bits
{/* brightSizer->Add( 0, 0, 1, wxEXPAND ) */}
Output Size
Size: panel.SetUnitSizeXText(aValue)} className="imgc-input ze-bare" /> panel.SetUnitSizeYText(aValue)} className="imgc-input ze-bare" /> panel.OnSizeUnitChange(Number(v))} options={PIXEL_UNIT_CHOICES.map((label, i) => ({ value: String(i), label }))} />
panel.ToggleAspectRatioLock(aChecked)} />
Options Black / white threshold: panel.OnThresholdChange(v)} /> panel.OnNegativeClicked(aChecked)} />
Output Format {/* fgSizer2 is a wxFlexGridSizer( 5, 1, 2, 0 ): five sibling rows, the Layer row third. They must stay siblings, because each row's wxFormBuilder border differs and the stylesheet addresses them by position. */}
{FORMATS.map((f) => ( panel.SelectFormat(f.id)} /> {f.id === FOOTPRINT_FMT && (
Layer: panel.SetLayerSelection(Number(v))} options={LAYER_CHOICES.map((label, i) => ({ value: String(i), label }))} />
)}
))}
); }