File size: 18,636 Bytes
25e7739
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
'use client'

import { useCallback, useMemo, useState } from 'react'
import { useStaticConvolution } from '@/hooks/useStaticConvolution'
import { useAppStore } from '@/store'
import { Card, CardContent, Badge, Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui'
import { cn } from '@/utils/helpers'
import { Calculator, Grid, Palette } from 'lucide-react'

interface StaticConvolutionViewProps {
  className?: string
  inspectedPosition: { x: number; y: number } | null
  onClearInspection: () => void
}

export function StaticConvolutionView({ className, inspectedPosition, onClearInspection }: StaticConvolutionViewProps) {
  const {
    computeStep,
    getPaddedImagePreview,
    kernel,
    normalize,
    padding,
    imageWidth,
    imageHeight,
    hasImage,
    hasKernel,
  } = useStaticConvolution()

  const { settings } = useAppStore()
  const [activeChannel, setActiveChannel] = useState(0)
  const [activeTab, setActiveTab] = useState<string>('overview')

  if (!hasImage || !hasKernel) {
    return (
      <Card className={cn('w-full', className)}>
        <CardContent className="p-6 text-center text-gray-500 dark:text-gray-400">
          <Grid className="h-12 w-12 mx-auto mb-3 opacity-50" />
          <p>Upload an image and select a kernel to inspect convolution steps</p>
        </CardContent>
      </Card>
    )
  }

  const step = inspectedPosition ? computeStep(inspectedPosition.x, inspectedPosition.y, activeChannel) : null

  const channelLabels = ['Red', 'Green', 'Blue', 'Alpha']
  const channelColors = ['text-red-600', 'text-green-600', 'text-blue-600', 'text-gray-600']

  // Get padded image preview for visualization
  const paddedPreview = inspectedPosition ? getPaddedImagePreview(kernel.length) : null
  const halfKernel = Math.floor(kernel.length / 2)
  const centerPx = halfKernel
  const centerPy = halfKernel

  return (
    <Card className={cn('w-full flex flex-col', className)}>
      <CardContent className="p-4 flex-1 flex flex-col min-h-0">
        {/* Header */}
        <div className="flex items-center justify-between mb-4">
          <h3 className="font-medium flex items-center gap-2">
            <Calculator className="h-5 w-5" />
            Static Convolution Inspector
          </h3>
          <div className="flex items-center gap-2">
            <Badge variant="outline" className="text-xs">
              {padding} padding
            </Badge>
            {normalize && <Badge variant="secondary" className="text-xs">Normalized</Badge>}
          </div>
        </div>

        {/* Inspected Position Info */}
        {inspectedPosition && (
          <div className="mb-4 p-3 bg-gray-50 dark:bg-gray-800 rounded-lg">
            <div className="flex items-center justify-between">
              <div className="flex items-center gap-4">
                <span className="font-medium">Position: ({inspectedPosition.x}, {inspectedPosition.y})</span>
                <div className="flex items-center gap-2">
                  <Palette className="h-4 w-4 text-gray-500" />
                  <select
                    value={activeChannel}
                    onChange={(e) => setActiveChannel(parseInt(e.target.value))}
                    className="text-sm border border-gray-300 rounded px-2 py-1 dark:border-gray-600 dark:bg-gray-700"
                  >
                    {channelLabels.map((label, i) => (
                      <option key={i} value={i} className={channelColors[i]}>
                        {label} Channel
                      </option>
                    ))}
                  </select>
                </div>
              </div>
              <button
                onClick={onClearInspection}
                className="text-sm text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-200"
              >
                Clear Selection
              </button>
            </div>
          </div>
        )}

        {/* Kernel Matrix Display */}
        <div className="mb-4">
          <h4 className="text-sm font-medium mb-2">Kernel Matrix ({kernel.length}×{kernel.length})</h4>
          <div className="grid gap-1 mb-4" style={{ gridTemplateColumns: `repeat(${kernel.length}, 1fr)` }}>
            {kernel.map((row, i) =>
              row.map((val, j) => (
                <div
                  key={`${i}-${j}`}
                  className={cn(
                    'p-2 text-center text-sm font-mono rounded border',
                    val > 0
                      ? 'bg-green-50 text-green-800 dark:bg-green-900/30 dark:text-green-300 border-green-200'
                      : val < 0
                      ? 'bg-red-50 text-red-800 dark:bg-red-900/30 dark:text-red-300 border-red-200'
                      : 'bg-gray-50 text-gray-800 dark:bg-gray-800 dark:text-gray-300 border-gray-200'
                  )}
                >
                  {val.toFixed(val === Math.round(val) ? 0 : 2)}
                </div>
              ))
            )}
          </div>
        </div>

        {/* Step Details Tabs */}
        {step && (
          <Tabs value={activeTab} onValueChange={setActiveTab} className="flex-1 flex flex-col min-h-0">
            <TabsList className="grid w-full grid-cols-5 mb-4">
              <TabsTrigger value="overview">Overview</TabsTrigger>
              <TabsTrigger value="patch">Image Patch</TabsTrigger>
              <TabsTrigger value="multiplications">Multiplications</TabsTrigger>
              <TabsTrigger value="summation">Summation</TabsTrigger>
              <TabsTrigger value="padding">Padding</TabsTrigger>
            </TabsList>

            <TabsContent value="overview" className="flex-1 overflow-auto space-y-4">
              <div className="grid gap-4 sm:grid-cols-2">
                <div className="p-3 bg-gray-50 dark:bg-gray-800 rounded-lg">
                  <h5 className="text-sm font-medium text-gray-500 dark:text-gray-400 mb-1">Input Patch (Normalized 0-1)</h5>
                  <div className="grid gap-0.5 text-xs font-mono" style={{ gridTemplateColumns: `repeat(${kernel.length}, 1fr)` }}>
                    {step.inputPatch.map((row, i) =>
                      row.map((val, j) => (
                        <div
                          key={`${i}-${j}`}
                          className={cn(
                            'p-1.5 text-center rounded border',
                            val > 0.5 ? 'bg-blue-50 text-blue-800 dark:bg-blue-900/30 dark:text-blue-300' : 'bg-gray-100 text-gray-800 dark:bg-gray-700'
                          )}
                        >
                          {val.toFixed(3)}
                        </div>
                      ))
                    )}
                  </div>
                </div>

                <div className="p-3 bg-gray-50 dark:bg-gray-800 rounded-lg">
                  <h5 className="text-sm font-medium text-gray-500 dark:text-gray-400 mb-1">Kernel Values</h5>
                  <div className="grid gap-0.5 text-xs font-mono" style={{ gridTemplateColumns: `repeat(${kernel.length}, 1fr)` }}>
                    {step.kernel.map((row, i) =>
                      row.map((val, j) => (
                        <div
                          key={`${i}-${j}`}
                          className={cn(
                            'p-1.5 text-center rounded border',
                            val > 0 ? 'bg-green-50 text-green-800 dark:bg-green-900/30 dark:text-green-300' :
                            val < 0 ? 'bg-red-50 text-red-800 dark:bg-red-900/30 dark:text-red-300' :
                            'bg-gray-50 text-gray-800 dark:bg-gray-800 dark:text-gray-300'
                          )}
                        >
                          {val.toFixed(val === Math.round(val) ? 0 : 2)}
                        </div>
                      ))
                    )}
                  </div>
                </div>

                <div className="p-3 bg-gray-50 dark:bg-gray-800 rounded-lg sm:col-span-2">
                  <h5 className="text-sm font-medium text-gray-500 dark:text-gray-400 mb-1">Computation Summary</h5>
                  <div className="grid gap-2 sm:grid-cols-3 text-sm">
                    <div className="font-mono">
                      <span className="text-gray-500">Raw Sum: </span>
                      <span className="text-blue-600 dark:text-blue-400">{step.sum.toFixed(6)}</span>
                    </div>
                    <div className="font-mono">
                      <span className="text-gray-500">Normalized: </span>
                      <span className="text-green-600 dark:text-green-400">{step.normalizedValue.toFixed(6)}</span>
                    </div>
                    <div className="font-mono">
                      <span className="text-gray-500">Output (0-255): </span>
                      <span className="text-purple-600 dark:text-purple-400">{Math.round(step.normalizedValue * 255)}</span>
                    </div>
                    <div className="font-mono sm:col-span-3">
                      <span className="text-gray-500">Progress: </span>
                      <span>{Math.round(step.progress * 100)}%</span>
                    </div>
                  </div>
                </div>
              </div>
            </TabsContent>

            <TabsContent value="patch" className="flex-1 overflow-auto">
              <h5 className="text-sm font-medium text-gray-500 dark:text-gray-400 mb-2">
                Image Patch Under Kernel at ({inspectedPosition?.x}, {inspectedPosition?.y}) - Channel: {channelLabels[activeChannel]}
              </h5>
              <div className="grid gap-1" style={{ gridTemplateColumns: `repeat(${kernel.length}, 1fr)` }}>
                {step.inputPatch.map((row, i) =>
                  row.map((val, j) => (
                    <div
                      key={`${i}-${j}`}
                      className={cn(
                        'p-3 text-center text-sm font-mono rounded border aspect-square flex items-center justify-center',
                        val > 0.5 ? 'bg-blue-50 text-blue-800 dark:bg-blue-900/30 dark:text-blue-300 border-blue-200' : 'bg-gray-100 text-gray-800 dark:bg-gray-700 border-gray-200'
                      )}
                    >
                      <div className="text-xs text-gray-500">{val.toFixed(3)}</div>
                      <div className="text-lg font-bold">{val.toFixed(3)}</div>
                    </div>
                  ))
                )}
              </div>
            </TabsContent>

            <TabsContent value="multiplications" className="flex-1 overflow-auto">
              <h5 className="text-sm font-medium text-gray-500 dark:text-gray-400 mb-2">
                Element-wise Multiplication: Pixel × Kernel
              </h5>
              <div className="space-y-4">
                {step.multiplications.map((row, i) =>
                  row.map((mult, j) => {
                    const pixelVal = step.inputPatch[i]?.[j] ?? 0
                    const kernelVal = step.kernel[i]?.[j] ?? 0
                    return (
                      <div key={`${i}-${j}`} className="flex items-center justify-center gap-4 p-2 bg-gray-50 dark:bg-gray-800 rounded">
                        <div className="w-20 text-right text-sm font-mono text-gray-600 dark:text-gray-400">
                          {pixelVal.toFixed(3)}
                        </div>
                        <span className="text-gray-400">×</span>
                        <div className="w-20 text-center text-sm font-mono" style={{ color: kernelVal > 0 ? '#16a34a' : kernelVal < 0 ? '#dc2626' : '#6b7280' }}>
                          {kernelVal.toFixed(2)}
                        </div>
                        <span className="text-gray-400">=</span>
                        <div className="w-24 text-left text-sm font-mono font-medium text-purple-600 dark:text-purple-400">
                          {mult.toFixed(6)}
                        </div>
                      </div>
                    )
                  })
                )}
              </div>
            </TabsContent>

            <TabsContent value="summation" className="flex-1 overflow-auto">
              <h5 className="text-sm font-medium text-gray-500 dark:text-gray-400 mb-2">
                Summation: Σ(Pixel × Kernel)
              </h5>
              <div className="space-y-2 font-mono text-sm">
                {step.multiplications.map((row, i) =>
                  row.map((mult, j) => {
                    const pixelVal = step.inputPatch[i]?.[j] ?? 0
                    const kernelVal = step.kernel[i]?.[j] ?? 0
                    return (
                      <div key={`${i}-${j}`} className="flex items-center gap-2 text-gray-700 dark:text-gray-300">
                        <span className="text-gray-400">({pixelVal.toFixed(3)} × {kernelVal.toFixed(2)})</span>
                        <span className="text-purple-600 dark:text-purple-400">= {mult.toFixed(6)}</span>
                      </div>
                    )
                  })
                )}
                <div className="border-t border-gray-300 dark:border-gray-600 pt-2 mt-2 flex items-center justify-between">
                  <span className="font-medium">Sum:</span>
                  <span className="font-bold text-blue-600 dark:text-blue-400">{step.sum.toFixed(6)}</span>
                </div>
                <div className="flex items-center justify-between text-green-600 dark:text-green-400">
                  <span>After normalization (÷ {kernel.flat().reduce((a, b) => a + b, 0).toFixed(normalize ? 0 : 2)}):</span>
                  <span className="font-bold">{step.normalizedValue.toFixed(6)}</span>
                </div>
                <div className="flex items-center justify-between text-purple-600 dark:text-purple-400 border-t border-gray-300 dark:border-gray-600 pt-2 mt-2">
                  <span className="font-medium">Final Output (0-255):</span>
                  <span className="font-bold text-lg">{Math.round(step.normalizedValue * 255)}</span>
                </div>
              </div>
            </TabsContent>

            <TabsContent value="padding" className="flex-1 overflow-auto">
              <h5 className="text-sm font-medium text-gray-500 dark:text-gray-400 mb-2">
                Padded Image Preview ({padding} padding) - Kernel centered at ({inspectedPosition?.x}, {inspectedPosition?.y})
              </h5>
              {paddedPreview && (
                <div className="space-y-4">
                  <div className="text-sm text-gray-500 dark:text-gray-400">
                    Padded dimensions: {paddedPreview.width} × {paddedPreview.height} (original: {imageWidth} × {imageHeight})
                    <br />
                    Kernel center position in padded image: ({centerPx}, {centerPy})
                  </div>
                  <div className="grid gap-0.5" style={{ gridTemplateColumns: `repeat(${paddedPreview.width}, 1fr)` }}>
                    {Array.from({ length: paddedPreview.height }, (_, py) =>
                      Array.from({ length: paddedPreview.width }, (_, px) => {
                        // Show only first 3 channels (RGB) as grayscale
                        const idx = (py * paddedPreview.width + px) * 4
                        const r = paddedPreview.data[idx] ?? 0
                        const g = paddedPreview.data[idx + 1] ?? 0
                        const b = paddedPreview.data[idx + 2] ?? 0
                        const gray = Math.round(0.299 * r + 0.587 * g + 0.114 * b)
                        const isCenter = px === centerPx && py === centerPy
                        const isKernelArea =
                          px >= centerPx - halfKernel &&
                          px < centerPx + halfKernel + 1 &&
                          py >= centerPy - halfKernel &&
                          py < centerPy + halfKernel + 1

                        return (
                          <div
                            key={`${px}-${py}`}
                            className={cn(
                              'aspect-square text-center text-xs font-mono rounded border flex items-center justify-center',
                              isCenter
                                ? 'bg-blue-100 text-blue-800 dark:bg-blue-900/30 dark:text-blue-300 border-blue-400 font-bold'
                                : isKernelArea
                                ? 'bg-yellow-50 text-yellow-800 dark:bg-yellow-900/30 dark:text-yellow-300 border-yellow-300'
                                : px < halfKernel || px >= paddedPreview.width - halfKernel || py < halfKernel || py >= paddedPreview.height - halfKernel
                                ? 'bg-gray-100 text-gray-600 dark:bg-gray-800 dark:text-gray-400 border-gray-200'
                                : 'bg-white text-gray-800 dark:bg-gray-900 dark:text-gray-200 border-gray-200'
                            )}
                            title={`Padded[${px},${py}] = ${gray}`}
                          >
                            {gray}
                          </div>
                        )
                      })
                    )}
                  </div>
                  <div className="flex items-center gap-4 text-xs text-gray-500 dark:text-gray-400">
                    <span className="flex items-center gap-1">
                      <span className="w-3 h-3 rounded border bg-blue-100 dark:bg-blue-900/30" />
                      Kernel Center
                    </span>
                    <span className="flex items-center gap-1">
                      <span className="w-3 h-3 rounded border bg-yellow-50 dark:bg-yellow-900/30" />
                      Kernel Area
                    </span>
                    <span className="flex items-center gap-1">
                      <span className="w-3 h-3 rounded border bg-gray-100 dark:bg-gray-800" />
                      Padding Area
                    </span>
                    <span className="flex items-center gap-1">
                      <span className="w-3 h-3 rounded border bg-white dark:bg-gray-900" />
                      Original Image
                    </span>
                  </div>
                </div>
              )}
            </TabsContent>
          </Tabs>
        )}

        {!inspectedPosition && (
          <div className="flex-1 flex items-center justify-center text-gray-400">
            <p className="text-center">Click on the image to inspect a convolution step</p>
          </div>
        )}
      </CardContent>
    </Card>
  )
}