@duyafeng/pseudocolor-viewer 0.1.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- <template>
1
+ <template>
2
2
  <el-dialog fullscreen :show-header="false" :visible.sync="imageDialog" @closed="handleDialogClosed">
3
3
  <div class="closed" @click="handleBeforeClose"><i class="el-icon-circle-close"></i></div>
4
4
  <div class="container">
@@ -107,10 +107,501 @@
107
107
  </template>
108
108
 
109
109
  <script>
110
- import { MedicalColorProcessor, ImageDisplayManager, WindowLevelController, canvasUtils, ImageQueue } from './lib/index.js';
110
+ import { MedicalColorProcessor, ImageDisplayManager, WindowLevelController, canvasUtils, ImageQueue } from '@duyafeng/pseudocolor/legacy/pseudoColor';
111
111
  import RectInfo from './Rect.vue';
112
112
  import { getDefaultPositions } from './positionPresets.js';
113
113
 
114
+ // ══════════════════════════════════════════════════════════════
115
+ // Monkey-patches for @duyafeng/pseudocolor legacy ImageDisplayManager
116
+ // legacy 版硬编码 288×384,原版动态匹配 256×192 / 288×384 + 旋转处理
117
+ // ══════════════════════════════════════════════════════════════
118
+ const CANDIDATE_FRAMES = [
119
+ { width: 256, height: 192, rotate: true },
120
+ { width: 288, height: 384, rotate: false },
121
+ ];
122
+ const TEMP_BYTES_PER_PIXEL = [1, 4, 8];
123
+
124
+ // Patch 1: 缺 setRed1Mode
125
+ ImageDisplayManager.prototype.setRed1Mode = function(mode) {
126
+ this.red1Mode = !!mode;
127
+ };
128
+
129
+ // Patch 2: 动态匹配帧尺寸(256×192 / 288×384)
130
+ ImageDisplayManager.prototype.resolveSourceFrame = function(byteLength, bytesPerPixelList) {
131
+ bytesPerPixelList = bytesPerPixelList || [1];
132
+ for (const bytesPerPixel of bytesPerPixelList) {
133
+ for (const frame of CANDIDATE_FRAMES) {
134
+ if (byteLength === frame.width * frame.height * bytesPerPixel) {
135
+ return { ...frame, bytesPerPixel };
136
+ }
137
+ }
138
+ }
139
+ throw new Error(
140
+ `数据长度不匹配!检测到 ${byteLength} 字节,预期尺寸 ${CANDIDATE_FRAMES.map(f => `${f.width}×${f.height}`).join(' 或 ')}、单像素 ${bytesPerPixelList.join('/')} 字节`
141
+ );
142
+ };
143
+
144
+ // Patch 3: 旋转相关(256×192 需逆时针旋转 90°)
145
+ ImageDisplayManager.prototype.getDisplayFrame = function({ width, height, rotate }) {
146
+ return rotate ? { width: height, height: width } : { width, height };
147
+ };
148
+ ImageDisplayManager.prototype.rotateCounterclockwise90 = function(data, width, height, stride) {
149
+ stride = stride || 1;
150
+ const rotated = new data.constructor(data.length);
151
+ for (let y = 0; y < height; y++) {
152
+ for (let x = 0; x < width; x++) {
153
+ const from = (y * width + x) * stride;
154
+ const to = ((width - 1 - x) * height + y) * stride;
155
+ for (let c = 0; c < stride; c++) {
156
+ rotated[to + c] = data[from + c];
157
+ }
158
+ }
159
+ }
160
+ return rotated;
161
+ };
162
+ ImageDisplayManager.prototype.orientData = function(data, frame, stride) {
163
+ stride = stride || 1;
164
+ return frame.rotate
165
+ ? this.rotateCounterclockwise90(data, frame.width, frame.height, stride)
166
+ : data;
167
+ };
168
+
169
+ // Patch 4: parseImageFromBase64(原版,支持动态尺寸 + 旋转)
170
+ ImageDisplayManager.prototype.parseImageFromBase64 = function(base64Input) {
171
+ const binaryString = atob(base64Input.trim());
172
+ const bytes = new Uint8Array(binaryString.length);
173
+ for (let i = 0; i < binaryString.length; i++) {
174
+ bytes[i] = binaryString.charCodeAt(i);
175
+ }
176
+ const frame = this.resolveSourceFrame(bytes.length);
177
+ const tempCanvas = document.createElement('canvas');
178
+ tempCanvas.width = frame.width;
179
+ tempCanvas.height = frame.height;
180
+ const tempCtx = tempCanvas.getContext('2d');
181
+ const imageData = tempCtx.createImageData(frame.width, frame.height);
182
+ let minGray = 255, maxGray = 0, sumGray = 0;
183
+ for (let i = 0; i < bytes.length; i++) {
184
+ const gray = bytes[i];
185
+ imageData.data[i * 4] = gray;
186
+ imageData.data[i * 4 + 1] = gray;
187
+ imageData.data[i * 4 + 2] = gray;
188
+ imageData.data[i * 4 + 3] = 255;
189
+ minGray = Math.min(minGray, gray);
190
+ maxGray = Math.max(maxGray, gray);
191
+ sumGray += gray;
192
+ }
193
+ this.currentGrayStats = { min: minGray, max: maxGray };
194
+ const displayFrame = this.getDisplayFrame(frame);
195
+ const displayGray = this.orientData(imageData.data, frame, 4);
196
+ const orientedImageData = new ImageData(displayGray, displayFrame.width, displayFrame.height);
197
+ this.originalImageData = orientedImageData;
198
+ this.canvas.width = orientedImageData.width;
199
+ this.canvas.height = orientedImageData.height;
200
+ return {
201
+ imageData: orientedImageData,
202
+ grayStats: { min: minGray, max: maxGray, avg: parseFloat((sumGray / bytes.length).toFixed(2)) },
203
+ };
204
+ };
205
+
206
+ // Patch 5: parseTemperatureData(原版,支持动态尺寸 + 旋转)
207
+ ImageDisplayManager.prototype.parseTemperatureData = function(base64Input) {
208
+ const binaryString = atob(base64Input.trim());
209
+ const bytes = new Uint8Array(binaryString.length);
210
+ for (let i = 0; i < binaryString.length; i++) {
211
+ bytes[i] = binaryString.charCodeAt(i);
212
+ }
213
+ const frame = this.resolveSourceFrame(bytes.length, TEMP_BYTES_PER_PIXEL);
214
+ let floatArray;
215
+ if (frame.bytesPerPixel === 1) {
216
+ floatArray = new Float32Array(bytes.length);
217
+ for (let i = 0; i < bytes.length; i++) floatArray[i] = bytes[i];
218
+ } else if (frame.bytesPerPixel === 4) {
219
+ floatArray = new Float32Array(bytes.buffer);
220
+ } else {
221
+ const float64Array = new Float64Array(bytes.buffer);
222
+ floatArray = new Float32Array(float64Array.length);
223
+ for (let i = 0; i < float64Array.length; i++) floatArray[i] = float64Array[i];
224
+ }
225
+ let minTemp = Infinity, maxTemp = -Infinity, sumTemp = 0;
226
+ for (let i = 0; i < floatArray.length; i++) {
227
+ const t = floatArray[i];
228
+ minTemp = Math.min(minTemp, t);
229
+ maxTemp = Math.max(maxTemp, t);
230
+ sumTemp += t;
231
+ }
232
+ const orientedTemps = this.orientData(floatArray, frame, 1);
233
+ this.temperatureData = orientedTemps;
234
+ const stats = {
235
+ min: parseFloat(minTemp.toFixed(2)),
236
+ max: parseFloat(maxTemp.toFixed(2)),
237
+ avg: parseFloat((sumTemp / floatArray.length).toFixed(2)),
238
+ initValue: parseFloat((minTemp + (maxTemp - minTemp) * 1/3).toFixed(2)),
239
+ };
240
+ this.temperatureStats = stats;
241
+ // 返回 { data, stats } 结构,与调用方期望一致
242
+ return { data: orientedTemps, stats };
243
+ };
244
+
245
+ // ══════════════════════════════════════════════════════════════
246
+ // Patch 6: MedicalColorProcessor.applyRed1ColorMap(红色热区高亮模式)
247
+ // 复刻 pc 端 comparisonManager 的 applyRed1ColorMap
248
+ // 三种分支:最高温→纯红 | 高于平均→灰度循环表 | 低于平均→白色
249
+ // ══════════════════════════════════════════════════════════════
250
+ MedicalColorProcessor.prototype.applyRed1ColorMap = function(imageData, grayWL, grayWW, grayStats, redThresholdGray) {
251
+ const pixelData = imageData.data;
252
+ const averageGray = parseFloat(grayStats && grayStats.avg) || 0;
253
+ const wndRange = grayWW !== undefined ? grayWW : 128;
254
+
255
+ let start = null;
256
+ let end = null;
257
+ let strict = false;
258
+
259
+ if (typeof redThresholdGray === 'number' && isFinite(redThresholdGray)) {
260
+ end = redThresholdGray;
261
+ start = redThresholdGray - wndRange;
262
+ strict = true;
263
+ } else if (grayWL !== undefined && grayWW !== undefined) {
264
+ start = grayWL - grayWW / 4;
265
+ end = start + wndRange;
266
+ }
267
+
268
+ const fSlope = end !== null && end > start ? 255 / (end - start) : 0;
269
+ const gradientMax = strict ? 254 : 255;
270
+
271
+ for (let i = 0; i < pixelData.length; i += 4) {
272
+ const gray = pixelData[i];
273
+ let value;
274
+
275
+ if (start === null) {
276
+ value = gray;
277
+ } else if (gray < start) {
278
+ value = 0;
279
+ } else if (strict ? gray > end : gray >= end) {
280
+ value = 255;
281
+ } else {
282
+ value = Math.trunc((gray - start) * fSlope);
283
+ if (value > gradientMax) value = gradientMax;
284
+ }
285
+
286
+ if (value === 255) {
287
+ // 最高温区域 → 纯红色
288
+ pixelData[i] = 255; pixelData[i + 1] = 0; pixelData[i + 2] = 0;
289
+ } else if (gray > averageGray) {
290
+ // 高于平均温度 → 灰度循环表
291
+ const g = Math.floor((value % 40) / 4) * 4;
292
+ pixelData[i] = g; pixelData[i + 1] = g; pixelData[i + 2] = g;
293
+ } else {
294
+ // 低于平均温度 → 白色
295
+ pixelData[i] = 255; pixelData[i + 1] = 255; pixelData[i + 2] = 255;
296
+ }
297
+ pixelData[i + 3] = 255;
298
+ }
299
+ return imageData;
300
+ };
301
+
302
+ // ══════════════════════════════════════════════════════════════
303
+ // Patch 7: 覆盖 ImageDisplayManager.updateDisplay —— 支持 red1Mode
304
+ // 原版只调用 applyTemperatureColorMap,现在根据 red1Mode 切换
305
+ // ══════════════════════════════════════════════════════════════
306
+ const _origUpdateDisplay = ImageDisplayManager.prototype.updateDisplay;
307
+ ImageDisplayManager.prototype.updateDisplay = function(canvasData) {
308
+ const wwValue = Number(canvasData.wwValue) * 10;
309
+ const wlValue = Number(canvasData.wlValue);
310
+ let { imageData, grayStats, tempStats } = canvasData;
311
+ this.originalImageData = imageData;
312
+ this.currentGrayStats = grayStats;
313
+ this.temperatureStats = tempStats;
314
+ if (!this.originalImageData) return;
315
+
316
+ const tempData = new ImageData(
317
+ new Uint8ClampedArray(this.originalImageData.data),
318
+ this.originalImageData.width,
319
+ this.originalImageData.height,
320
+ );
321
+
322
+ // 将温度窗位转换为灰度窗位
323
+ let tempWL = parseFloat(wlValue);
324
+ let grayWW = parseInt(wwValue);
325
+ let grayWL = undefined;
326
+ if (tempWL !== undefined && this.temperatureStats && this.temperatureStats.min !== null
327
+ && this.currentGrayStats && this.currentGrayStats.min !== undefined) {
328
+ grayWL = this.calculateGrayForTemp(
329
+ tempWL,
330
+ this.currentGrayStats.min,
331
+ this.currentGrayStats.max,
332
+ this.temperatureStats.min,
333
+ this.temperatureStats.max,
334
+ );
335
+ }
336
+
337
+ // 根据 red1Mode 选择颜色映射
338
+ let coloredData;
339
+ if (this.red1Mode) {
340
+ coloredData = this.processor.applyRed1ColorMap(
341
+ tempData, grayWL, grayWW, grayStats || { avg: 0 },
342
+ );
343
+ } else {
344
+ coloredData = this.processor.applyTemperatureColorMap(
345
+ tempData, grayWL, grayWW,
346
+ this.currentGrayStats.min, this.currentGrayStats.max,
347
+ this.temperatureStats && this.temperatureStats.min,
348
+ this.temperatureStats && this.temperatureStats.max,
349
+ );
350
+ }
351
+
352
+ // 临时画布 + 绘制
353
+ const tempCanvas = document.createElement('canvas');
354
+ tempCanvas.width = this.originalImageData.width;
355
+ tempCanvas.height = this.originalImageData.height;
356
+ const tempCtx = tempCanvas.getContext('2d');
357
+ tempCtx.putImageData(coloredData, 0, 0);
358
+
359
+ this.ctx.imageSmoothingEnabled = false;
360
+ this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
361
+ this.ctx.drawImage(tempCanvas, 0, 0, this.canvas.width, this.canvas.height);
362
+ };
363
+
364
+ // ══════════════════════════════════════════════════════════════
365
+ // Patch 8: 覆盖 canvasUtils —— 完整的测温工具函数
366
+ // npm 包的 canvasRectangle 缺少 hitTestMeasurement,
367
+ // extractRegionData 硬编码 288×384,drawExistingRectangles 不支持样式
368
+ // ══════════════════════════════════════════════════════════════
369
+ (function patchCanvasUtils() {
370
+ const C = canvasUtils;
371
+
372
+ // 获取画布缩放比(canvas 像素 / 页面显示像素)
373
+ function getScale(canvas) {
374
+ const rect = canvas.getBoundingClientRect();
375
+ return {
376
+ x: rect.width ? canvas.width / rect.width : 1,
377
+ y: rect.height ? canvas.height / rect.height : 1,
378
+ };
379
+ }
380
+
381
+ // 区域取点(支持 rect / circle / point 三种形状)
382
+ function extractRegionPixels(temperatureData, width, height, region) {
383
+ if (!temperatureData || temperatureData.length === 0) return [];
384
+ const shape = region.shape || 'rect';
385
+ const values = [];
386
+ const pushPixel = (px, py) => {
387
+ if (px >= 0 && px < width && py >= 0 && py < height) {
388
+ values.push(temperatureData[py * width + px]);
389
+ }
390
+ };
391
+ if (shape === 'point') {
392
+ const cx = Math.floor(region.startX);
393
+ const cy = Math.floor(region.startY);
394
+ for (let dy = -1; dy <= 1; dy++) {
395
+ for (let dx = -1; dx <= 1; dx++) pushPixel(cx + dx, cy + dy);
396
+ }
397
+ return values;
398
+ }
399
+ const x1 = Math.min(region.startX, region.endX);
400
+ const x2 = Math.max(region.startX, region.endX);
401
+ const y1 = Math.min(region.startY, region.endY);
402
+ const y2 = Math.max(region.startY, region.endY);
403
+ if (shape === 'circle') {
404
+ const cx = (x1 + x2) / 2, cy = (y1 + y2) / 2;
405
+ const rx = (x2 - x1) / 2, ry = (y2 - y1) / 2;
406
+ if (rx < 2 || ry < 2) return [];
407
+ for (let y = Math.floor(y1); y < Math.ceil(y2); y++) {
408
+ for (let x = Math.floor(x1); x < Math.ceil(x2); x++) {
409
+ const nx = (x - cx) / rx, ny = (y - cy) / ry;
410
+ if (nx * nx + ny * ny <= 1) pushPixel(x, y);
411
+ }
412
+ }
413
+ return values;
414
+ }
415
+ for (let y = Math.floor(y1); y < Math.floor(y2); y++) {
416
+ for (let x = Math.floor(x1); x < Math.floor(x2); x++) pushPixel(x, y);
417
+ }
418
+ return values;
419
+ }
420
+
421
+ // 覆盖 extractRegionData —— 接受 canvas 对象(取 canvas.width/height)+ shape
422
+ C.extractRegionData = function(temperatureData, startX, startY, endX, endY, canvas, shape) {
423
+ if (!temperatureData) return [];
424
+ if (!canvas || typeof canvas.getBoundingClientRect !== 'function') return [];
425
+ const scale = getScale(canvas);
426
+ return extractRegionPixels(
427
+ temperatureData, canvas.width, canvas.height,
428
+ {
429
+ startX: startX * scale.x, startY: startY * scale.y,
430
+ endX: endX * scale.x, endY: endY * scale.y,
431
+ shape: shape || 'rect',
432
+ },
433
+ );
434
+ };
435
+
436
+ // 命中测试(返回 { rect, part, dir } 或 null)
437
+ C.hitTestMeasurement = function(rectangles, x, y, selectedId) {
438
+ if (!rectangles || rectangles.length === 0) return null;
439
+ for (let i = rectangles.length - 1; i >= 0; i--) {
440
+ const rect = rectangles[i];
441
+ const shape = rect.shape || 'rect';
442
+ if (rect.id === selectedId && shape !== 'point') {
443
+ const x1 = Math.min(rect.startX, rect.endX), x2 = Math.max(rect.startX, rect.endX);
444
+ const y1 = Math.min(rect.startY, rect.endY), y2 = Math.max(rect.startY, rect.endY);
445
+ const mx = (x1 + x2) / 2, my = (y1 + y2) / 2;
446
+ const handles = [
447
+ { dir: 'nw', px: x1, py: y1 }, { dir: 'n', px: mx, py: y1 }, { dir: 'ne', px: x2, py: y1 },
448
+ { dir: 'w', px: x1, py: my }, { dir: 'e', px: x2, py: my },
449
+ { dir: 'sw', px: x1, py: y2 }, { dir: 's', px: mx, py: y2 }, { dir: 'se', px: x2, py: y2 },
450
+ ];
451
+ for (const h of handles) {
452
+ if (Math.abs(x - h.px) <= 6 && Math.abs(y - h.py) <= 6) {
453
+ return { rect, part: 'handle', dir: h.dir };
454
+ }
455
+ }
456
+ }
457
+ if (shape === 'circle') {
458
+ const x1 = Math.min(rect.startX, rect.endX), x2 = Math.max(rect.startX, rect.endX);
459
+ const y1 = Math.min(rect.startY, rect.endY), y2 = Math.max(rect.startY, rect.endY);
460
+ const cx = (x1 + x2) / 2, cy = (y1 + y2) / 2;
461
+ const rx = (x2 - x1) / 2, ry = (y2 - y1) / 2;
462
+ const nx = rx > 0 ? (x - cx) / rx : 0, ny = ry > 0 ? (y - cy) / ry : 0;
463
+ if (nx * nx + ny * ny <= 1) return { rect, part: 'body' };
464
+ } else if (shape === 'point') {
465
+ const d = Math.sqrt(Math.pow(x - rect.startX, 2) + Math.pow(y - rect.startY, 2));
466
+ if (d <= 8) return { rect, part: 'body' };
467
+ } else {
468
+ const x1 = Math.min(rect.startX, rect.endX), x2 = Math.max(rect.startX, rect.endX);
469
+ const y1 = Math.min(rect.startY, rect.endY), y2 = Math.max(rect.startY, rect.endY);
470
+ if (x >= x1 && x <= x2 && y >= y1 && y <= y2) return { rect, part: 'body' };
471
+ }
472
+ }
473
+ return null;
474
+ };
475
+
476
+ // 绘制图形(支持 rect / circle / point,含选中状态的 handle)
477
+ function drawShapeAndHandles(ctx, rect, sx, sy, ex, ey, scaleX, options) {
478
+ const strokeColor = options.strokeColor || '#000000';
479
+ const textColor = options.textColor || '#000000';
480
+ const selectedId = options.selectedId;
481
+ const shape = rect.shape || 'rect';
482
+ ctx.strokeStyle = strokeColor;
483
+ ctx.fillStyle = textColor;
484
+ ctx.lineWidth = 2;
485
+ ctx.setLineDash([]);
486
+ let info;
487
+ if (shape === 'circle') {
488
+ const bx = Math.min(sx, ex), by = Math.min(sy, ey);
489
+ const bw = Math.abs(ex - sx), bh = Math.abs(ey - sy);
490
+ ctx.beginPath();
491
+ ctx.ellipse(bx + bw / 2, by + bh / 2, Math.max(bw / 2, 0.5), Math.max(bh / 2, 0.5), 0, 0, Math.PI * 2);
492
+ ctx.stroke();
493
+ ctx.font = 'bold 14px Arial';
494
+ ctx.fillText(String(rect.orderCount), bx + 3, by + 15);
495
+ info = { x: bx, y: by, width: bw, height: bh };
496
+ } else if (shape === 'point') {
497
+ ctx.lineWidth = 1;
498
+ ctx.beginPath(); ctx.arc(sx, sy, 3, 0, Math.PI * 2); ctx.stroke();
499
+ ctx.beginPath();
500
+ ctx.moveTo(sx - 5, sy); ctx.lineTo(sx + 5, sy);
501
+ ctx.moveTo(sx, sy - 5); ctx.lineTo(sx, sy + 5);
502
+ ctx.stroke();
503
+ ctx.font = 'bold 14px Arial';
504
+ ctx.fillText(String(rect.orderCount), sx + 4, sy - 4);
505
+ info = { x: sx - 3, y: sy - 3, width: 6, height: 6 };
506
+ } else {
507
+ const rx = Math.min(sx, ex), ry = Math.min(sy, ey);
508
+ const rw = Math.abs(ex - sx), rh = Math.abs(ey - sy);
509
+ ctx.beginPath(); ctx.rect(rx, ry, rw, rh); ctx.stroke();
510
+ ctx.font = 'bold 14px Arial';
511
+ ctx.fillText(String(rect.orderCount), rx + 5, ry + 15);
512
+ info = { x: rx, y: ry, width: rw, height: rh };
513
+ }
514
+ // 选中状态:绘制蓝色虚线框 + 8 个 handle
515
+ if (selectedId != null && rect.id === selectedId && shape !== 'point') {
516
+ ctx.setLineDash([4, 2]);
517
+ ctx.strokeStyle = '#4a90e2';
518
+ ctx.lineWidth = 1;
519
+ ctx.beginPath();
520
+ ctx.rect(info.x, info.y, info.width, info.height);
521
+ ctx.stroke();
522
+ ctx.setLineDash([]);
523
+ const HANDLE = 6 * scaleX;
524
+ const pts = [
525
+ [info.x, info.y], [info.x + info.width / 2, info.y], [info.x + info.width, info.y],
526
+ [info.x, info.y + info.height / 2], [info.x + info.width, info.y + info.height / 2],
527
+ [info.x, info.y + info.height], [info.x + info.width / 2, info.y + info.height], [info.x + info.width, info.y + info.height],
528
+ ];
529
+ for (const p of pts) {
530
+ ctx.fillStyle = '#ffffff';
531
+ ctx.fillRect(p[0] - HANDLE / 2, p[1] - HANDLE / 2, HANDLE, HANDLE);
532
+ ctx.strokeStyle = '#4a90e2';
533
+ ctx.lineWidth = 1;
534
+ ctx.strokeRect(p[0] - HANDLE / 2, p[1] - HANDLE / 2, HANDLE, HANDLE);
535
+ }
536
+ ctx.fillStyle = textColor;
537
+ }
538
+ return info;
539
+ }
540
+
541
+ // 覆盖 drawExistingRectangles —— 支持样式参数(strokeColor / textColor / selectedId)
542
+ const _origDrawExisting = C.drawExistingRectangles.bind(C);
543
+ C.drawExistingRectangles = function(ctx, canvas, rectangles, style) {
544
+ if (!rectangles || rectangles.length === 0) return [];
545
+ style = style || {};
546
+ const scale = getScale(canvas);
547
+ return rectangles.map((rect) => {
548
+ const sx = rect.startX * scale.x;
549
+ const sy = rect.startY * scale.y;
550
+ const ex = rect.endX * scale.x;
551
+ const ey = rect.endY * scale.y;
552
+ const info = drawShapeAndHandles(ctx, rect, sx, sy, ex, ey, scale.x, style);
553
+ return { ...rect, _renderInfo: info };
554
+ });
555
+ };
556
+
557
+ // 覆盖 createRectangle —— 原版没保存 shape 字段,圆画完变方
558
+ C.createRectangle = function(rectData, rectCount) {
559
+ return {
560
+ id: Date.now(),
561
+ shape: rectData.shape || 'rect', // ✅ 保存形状
562
+ startX: rectData.startX, startY: rectData.startY,
563
+ endX: rectData.endX, endY: rectData.endY,
564
+ width: rectData.width, height: rectData.height,
565
+ regionAvgTemp: rectData.regionAvgTemp,
566
+ regionMaxTemp: rectData.regionMaxTemp,
567
+ regionMinTemp: rectData.regionMinTemp,
568
+ regionVariance: rectData.regionVariance,
569
+ orderCount: rectCount,
570
+ };
571
+ };
572
+
573
+ // 覆盖 drawSelectionRectangle —— 支持 shape 和 strokeColor
574
+ C.drawSelectionRectangle = function(ctx, params) {
575
+ const { startX, startY, currentX, currentY, scaleFactor, shape, strokeColor } = params;
576
+ const scaleX = scaleFactor.x, scaleY = scaleFactor.y;
577
+ const sx = startX * scaleX, sy = startY * scaleY;
578
+ const cx = currentX * scaleX, cy = currentY * scaleY;
579
+ ctx.strokeStyle = strokeColor || '#000000';
580
+ ctx.lineWidth = 2;
581
+ ctx.setLineDash([]);
582
+ const s = shape || 'rect';
583
+ if (s === 'circle') {
584
+ const bx = Math.min(sx, cx), by = Math.min(sy, cy);
585
+ const bw = Math.abs(cx - sx), bh = Math.abs(cy - sy);
586
+ ctx.beginPath();
587
+ ctx.ellipse(bx + bw / 2, by + bh / 2, Math.max(bw / 2, 0.5), Math.max(bh / 2, 0.5), 0, 0, Math.PI * 2);
588
+ ctx.stroke();
589
+ return { rectX: bx, rectY: by, rectWidth: bw, rectHeight: bh };
590
+ }
591
+ if (s === 'point') {
592
+ ctx.beginPath(); ctx.arc(sx, sy, 3, 0, Math.PI * 2); ctx.stroke();
593
+ return { x: sx, y: sy };
594
+ }
595
+ ctx.beginPath();
596
+ ctx.rect(Math.min(sx, cx), Math.min(sy, cy), Math.abs(cx - sx), Math.abs(cy - sy));
597
+ ctx.stroke();
598
+ return {
599
+ rectX: Math.min(sx, cx), rectY: Math.min(sy, cy),
600
+ rectWidth: Math.abs(cx - sx), rectHeight: Math.abs(cy - sy),
601
+ };
602
+ };
603
+ })();
604
+
114
605
  export default {
115
606
  name: 'PseudoColorViewer',
116
607
  components: { RectInfo },
@@ -152,6 +643,14 @@ export default {
152
643
  flag: 'imageFlag', // 部位标记字段
153
644
  }),
154
645
  },
646
+ /**
647
+ * 场景标识(可选,'body' | 'face',默认 'body')
648
+ * 决定不传 positions 时自动选哪套预设:body 按性别选男/女,face 用 4 面预设
649
+ */
650
+ scene: {
651
+ type: String,
652
+ default: 'body',
653
+ },
155
654
  },
156
655
  data() {
157
656
  return {
@@ -299,12 +798,6 @@ export default {
299
798
  gray: 'bodyImageGray', temp: 'bodyImageGrayT', flag: 'imageFlag',
300
799
  };
301
800
  const { gray: grayKey, temp: tempKey, flag: flagKey } = dataMap;
302
- console.log('[PseudoColorViewer] getImage 入参 item:', item);
303
- console.log('[PseudoColorViewer] dataMap 解析结果:', { grayKey, tempKey, flagKey });
304
- console.log('[PseudoColorViewer] fetchImage 类型:', typeof this.fetchImage);
305
- console.log('[PseudoColorViewer] positions prop:', this.positions);
306
- console.log('[PseudoColorViewer] dataMap prop:', this.dataMap);
307
-
308
801
  // 1. 用户信息提取(仅在 prop 未传时从 item 里取)
309
802
  if (!this.userInfo) {
310
803
  let rawSex = item.userSex;
@@ -323,14 +816,12 @@ export default {
323
816
  };
324
817
  }
325
818
 
326
- // 2. 部位列表:优先用 prop positions,否则按性别选默认
819
+ // 2. 部位列表:优先用 prop positions,否则按 scene+sex 选默认
327
820
  const rawSex = item.userSex;
328
821
  let posList = this.positions;
329
822
  if (!posList || !posList.length) {
330
- posList = getDefaultPositions(rawSex);
823
+ posList = getDefaultPositions({ scene: this.scene || 'body', sex: rawSex });
331
824
  }
332
- console.log('[PseudoColorViewer] 最终部位列表 posList:', posList?.map(p => p.bodyFlag + '=' + p.btnName));
333
-
334
825
  this.canvasList = this.buildCanvasList(posList);
335
826
  this.imageQueue = new ImageQueue(2);
336
827
  this.thumbnailList = [
@@ -344,32 +835,22 @@ export default {
344
835
  orderCheckId: item.orderCheckId,
345
836
  currentNumber: item.collectCount,
346
837
  };
347
- console.log('[PseudoColorViewer] 准备调接口 fetchImage(', data, ')');
348
838
  let res;
349
839
  try {
350
840
  res = await this.fetchImage(data);
351
- console.log('[PseudoColorViewer] 接口返回 raw:', res);
352
841
  } catch (e) {
353
- console.error('[PseudoColorViewer] fetchImage 异常:', e);
354
842
  this.$emit('error', { message: 'fetchImage 异常: ' + e.message });
355
843
  return;
356
844
  }
357
845
  let code = res?.code;
358
846
  let dataList = res?.data;
359
- console.log('[PseudoColorViewer] 解析 code:', code, 'dataList.length:', dataList?.length);
360
847
  if (dataList && dataList.length) {
361
- console.log('[PseudoColorViewer] dataList[0] 字段:', Object.keys(dataList[0]));
362
- console.log('[PseudoColorViewer] dataList[0]:', dataList[0]);
363
848
  }
364
- console.log('[PseudoColorViewer] canvasList keys:', Object.keys(this.canvasList));
365
-
366
849
  if (code != 0 && code != '0') {
367
- console.warn('[PseudoColorViewer] code 非0,退出');
368
850
  this.$emit('error', { message: '接口返回非0: code=' + code });
369
851
  return;
370
852
  }
371
853
  if (!dataList || !dataList.length) {
372
- console.warn('[PseudoColorViewer] dataList 为空,退出');
373
854
  return;
374
855
  }
375
856
 
@@ -378,15 +859,11 @@ export default {
378
859
  const grayBase64 = it[grayKey];
379
860
  const tempBase64 = it[tempKey];
380
861
  const itFlag = it[flagKey] || it.bodyFlag || it.faceFlag;
381
- console.log(`[PseudoColorViewer] dataList[${i}] flagKey=${flagKey} it[flagKey]=${it[flagKey]} → itFlag=${itFlag}, gray=${!!grayBase64}, temp=${!!tempBase64}`);
382
-
383
862
  let target = itFlag ? this.canvasList[itFlag] : null;
384
863
  if (!target) {
385
864
  target = Object.values(this.canvasList).find(c => c.bodyFlag === itFlag);
386
- if (target) console.log(`[PseudoColorViewer] find 兜底命中 bodyFlag=${itFlag}`);
387
865
  }
388
866
  if (!target) {
389
- console.warn(`[PseudoColorViewer] target 未找到 bodyFlag=${itFlag},跳过`);
390
867
  return;
391
868
  }
392
869
 
@@ -395,14 +872,16 @@ export default {
395
872
  target.imgName = img.src;
396
873
  target.bodyImageGray = grayBase64;
397
874
  target.bodyImageGrayT = tempBase64;
875
+ target.partId = target.bodyFlag; // ImageQueue.isExist 依赖 partId 字段
398
876
 
399
877
  this.$nextTick(() => {
400
878
  const refName = 'resultCanvas' + target.bodyFlag;
401
- console.log(`[PseudoColorViewer] $nextTick ref=${refName} 存在?`, !!this.$refs[refName]);
402
879
  if (!this.$refs[refName]) return;
403
880
  let canvas = this.$refs[refName][0];
404
881
  let displayManager = new ImageDisplayManager(canvas, this.processor);
405
882
  displayManager.setRed1Mode(this.red1Mode);
883
+ this.$set(target, 'displayManager', displayManager);
884
+ this.$set(target, 'canvas', canvas);
406
885
  let result = this.loadTemperatureData({ displayManager, tempBase64 });
407
886
  target.tempStats = Object.assign({}, result.stats);
408
887
  canvas.tempData = result.data;
@@ -411,7 +890,7 @@ export default {
411
890
  displayManager,
412
891
  grayBase64,
413
892
  canvasData: target,
414
- canvas: this.$refs['resultCanvas' + target.bodyFlag][0],
893
+ canvas,
415
894
  });
416
895
  });
417
896
  });
@@ -460,13 +939,16 @@ export default {
460
939
  if (!canvasRefs) return;
461
940
  const resultCanvas = Array.isArray(canvasRefs) ? canvasRefs[0] : canvasRefs;
462
941
  let canvasData = this.canvasList[it.bodyFlag];
463
- let displayManager = canvasData.displayManager;
942
+ // 右侧大 canvas 用独立的 displayManager(不复用左侧小 canvas 的)
943
+ let key = 'rightDisplayManager';
944
+ let displayManager = canvasData[key];
464
945
  if (!displayManager) {
465
946
  displayManager = new ImageDisplayManager(resultCanvas, this.processor);
466
947
  displayManager.setRed1Mode(this.red1Mode);
467
- this.$set(canvasData, 'displayManager', displayManager);
468
- this.$set(canvasData, 'canvas', resultCanvas);
948
+ this.$set(canvasData, key, displayManager);
949
+ resultCanvas.canvasData = canvasData; // handleMouseDown 靠这个取值
469
950
  if (canvasData.bodyImageGray && canvasData.bodyImageGrayT) {
951
+ // 复用已解析好的 tempStats(左侧 getImage 里已经算过)
470
952
  if (!canvasData.tempData) {
471
953
  const r = this.loadTemperatureData({ displayManager, tempBase64: canvasData.bodyImageGrayT });
472
954
  canvasData.tempStats = Object.assign({}, r.stats);
@@ -525,42 +1007,26 @@ export default {
525
1007
  Object.keys(this.canvasList).forEach((bodyFlag) => {
526
1008
  const item = this.canvasList[bodyFlag];
527
1009
  if (!item) return;
528
- let dm = item.displayManager;
529
- if (!dm) {
530
- const c = this.$refs['resultCanvas_' + bodyFlag];
531
- if (c && c[0] && c[0].canvasData) dm = c[0].canvasData.displayManager;
532
- }
533
- if (!dm) return;
534
- dm.setRed1Mode(this.red1Mode);
535
- dm.updateDisplay(item);
536
- if (item.cavansRects && item.cavansRects.length) {
537
- const c = this.$refs['resultCanvas_' + bodyFlag];
538
- if (c && c[0]) {
539
- const ctx = c[0].getContext('2d');
540
- canvasUtils.drawExistingRectangles(ctx, c[0], item.cavansRects, {
541
- strokeColor: this.red1Mode ? "#ffffff" : "#000000",
542
- textColor: this.red1Mode ? "#ffffff" : "#000000",
543
- selectedId: this.selectedRectId,
544
- });
545
- }
546
- }
1010
+ let dmLeft = item.displayManager;
1011
+ let dmRight = item.rightDisplayManager;
1012
+ if (dmLeft) { dmLeft.setRed1Mode(this.red1Mode); dmLeft.updateDisplay(item); }
1013
+ if (dmRight) { dmRight.setRed1Mode(this.red1Mode); dmRight.updateDisplay(item); }
547
1014
  });
548
1015
  },
549
1016
  upDateDisplay({ grayWW, tempWL, canvasData }) {
550
1017
  this.windowController.updateWindow({ grayWW, tempWL, canvasData });
551
- canvasData.displayManager.updateDisplay(canvasData);
1018
+ // 只更新右侧大 canvas,左侧缩略图保持初始状态不受影响
1019
+ if (canvasData.rightDisplayManager) canvasData.rightDisplayManager.updateDisplay(canvasData);
552
1020
  this.redrawRectsOnCanvas(canvasData);
553
1021
  },
554
1022
  redrawRectsOnCanvas(canvasData) {
555
1023
  if (!canvasData || !canvasData.cavansRects || canvasData.cavansRects.length === 0) return;
556
- let canvas = canvasData.canvas;
557
- if (!canvas) {
558
- const ref = this.$refs['resultCanvas_' + canvasData.bodyFlag];
559
- canvas = ref && ref[0];
560
- }
561
- if (!canvas) return;
562
- const ctx = canvas.getContext('2d');
563
- const updated = canvasUtils.drawExistingRectangles(ctx, canvas, canvasData.cavansRects, {
1024
+ // 只画右侧大 canvas(下划线前缀),左侧缩略图不显示测温框
1025
+ const ref = this.$refs['resultCanvas_' + canvasData.bodyFlag];
1026
+ const c = ref && ref[0];
1027
+ if (!c) return;
1028
+ const ctx = c.getContext('2d');
1029
+ const updated = canvasUtils.drawExistingRectangles(ctx, c, canvasData.cavansRects, {
564
1030
  strokeColor: this.red1Mode ? '#ffffff' : '#000000',
565
1031
  textColor: this.red1Mode ? '#ffffff' : '#000000',
566
1032
  selectedId: this.selectedRectId,
@@ -914,9 +1380,8 @@ export default {
914
1380
  const canvas = ref && ref[0];
915
1381
  if (canvas) {
916
1382
  canvas.style.cursor = '';
917
- if (canvas.canvasData && canvas.canvasData.displayManager) {
918
- canvas.canvasData.displayManager.updateDisplay(item);
919
- }
1383
+ const dm = item.rightDisplayManager || item.displayManager;
1384
+ if (dm) dm.updateDisplay(item);
920
1385
  }
921
1386
  });
922
1387
  this.rectTableData.infoList = [];
@@ -932,7 +1397,11 @@ export default {
932
1397
  ctx.clearRect(0, 0, canvas.width, canvas.height);
933
1398
  let { canvasData } = canvas;
934
1399
  if (canvas.canvasData && canvas.canvasData.imageData) {
935
- canvasData.displayManager.updateDisplay(canvasData);
1400
+ // 优先用 rightDisplayManager(右侧大 canvas 的 manager),
1401
+ // 因为 canvasUtils.drawExistingRectangles 是画到 canvas 参数(右侧大 canvas)上,
1402
+ // 而 updateDisplay 必须也画到同一个 canvas 才对
1403
+ const dm = canvasData.rightDisplayManager || canvasData.displayManager;
1404
+ if (dm) dm.updateDisplay(canvasData);
936
1405
  }
937
1406
  this.drawExistingRectangles(ctx, canvas);
938
1407
  }