Coverage for graphica/gui/cvd_preview.py: 100.0%
15 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-12 16:38 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-12 16:38 +0000
1"""QImage と numpy 配列の変換(色の変換は Qt に依存しない core/cvd_simulation.py)。"""
2import numpy as np
3from PySide6.QtGui import QImage
5from graphica.core.cvd_simulation import simulate_rgb_array
8def simulate_qimage(image, cvd_type):
9 """変換した新しい QImage(アルファと元の image は変えない)。"""
10 src = image.convertToFormat(QImage.Format.Format_RGBA8888)
11 width, height = src.width(), src.height()
12 # 行の末尾に詰め物があるので、bytesPerLine() の幅で読んでから width*4 に切り詰める
13 bytes_per_line = src.bytesPerLine()
14 buf = bytes(src.constBits())
15 arr = np.frombuffer(buf, dtype=np.uint8).reshape((height, bytes_per_line))
16 arr = arr[:, : width * 4].reshape((height, width, 4)).copy()
18 rgb = arr[:, :, :3].astype(np.float64)
19 simulated = np.clip(simulate_rgb_array(rgb, cvd_type), 0, 255).astype(np.uint8)
20 arr[:, :, :3] = simulated
22 result = QImage(arr.tobytes(), width, height, width * 4, QImage.Format.Format_RGBA8888)
23 return result.copy() # arr のバッファの寿命から切り離す