File size: 18,107 Bytes
7aa32c8 0a485c6 7aa32c8 0a485c6 7aa32c8 0a485c6 7aa32c8 0a485c6 7aa32c8 0a485c6 7aa32c8 0a485c6 7aa32c8 0a485c6 7aa32c8 0a485c6 7aa32c8 0a485c6 7aa32c8 0a485c6 7aa32c8 0a485c6 7aa32c8 0a485c6 7aa32c8 | 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 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 | """
Unit tests for image processor module.
Tests curve application, inversion, and export functionality.
"""
import io
import tempfile
from pathlib import Path
import numpy as np
import pytest
from PIL import Image
from ptpd_calibration.core.models import CurveData
from ptpd_calibration.imaging import (
ImageProcessor,
ImageFormat,
ExportSettings,
ProcessingResult,
)
from ptpd_calibration.imaging.processor import ColorMode
class TestImageFormat:
"""Tests for ImageFormat enum."""
def test_all_formats_defined(self):
"""All expected formats should be defined."""
assert ImageFormat.TIFF
assert ImageFormat.TIFF_16BIT
assert ImageFormat.PNG
assert ImageFormat.PNG_16BIT
assert ImageFormat.JPEG
assert ImageFormat.JPEG_HIGH
assert ImageFormat.ORIGINAL
def test_format_values(self):
"""Format values should be valid strings."""
assert ImageFormat.TIFF.value == "tiff"
assert ImageFormat.PNG.value == "png"
assert ImageFormat.JPEG.value == "jpeg"
assert ImageFormat.ORIGINAL.value == "original"
class TestColorMode:
"""Tests for ColorMode enum."""
def test_color_mode_values(self):
"""Color mode values should be valid."""
assert ColorMode.GRAYSCALE.value == "grayscale"
assert ColorMode.RGB.value == "rgb"
assert ColorMode.PRESERVE.value == "preserve"
class TestExportSettings:
"""Tests for ExportSettings dataclass."""
def test_default_settings(self):
"""Default settings should be sensible."""
settings = ExportSettings()
assert settings.format == ImageFormat.ORIGINAL
assert settings.jpeg_quality == 95
assert settings.preserve_metadata is True
assert settings.preserve_resolution is True
def test_custom_settings(self):
"""Custom settings should be applied."""
settings = ExportSettings(
format=ImageFormat.JPEG_HIGH,
jpeg_quality=100,
preserve_metadata=False,
target_dpi=300,
)
assert settings.format == ImageFormat.JPEG_HIGH
assert settings.jpeg_quality == 100
assert settings.preserve_metadata is False
assert settings.target_dpi == 300
class TestProcessingResult:
"""Tests for ProcessingResult dataclass."""
@pytest.fixture
def sample_result(self):
"""Create a sample processing result."""
img = Image.new("RGB", (100, 100), color=(128, 128, 128))
return ProcessingResult(
image=img,
original_size=(100, 100),
original_mode="RGB",
original_format="PNG",
original_dpi=(300, 300),
curve_applied=True,
inverted=False,
processing_notes=["Test note"],
)
def test_get_info(self, sample_result):
"""get_info should return valid dictionary."""
info = sample_result.get_info()
assert "size" in info
assert "original_size" in info
assert "mode" in info
assert "curve_applied" in info
assert "inverted" in info
assert "notes" in info
def test_info_values(self, sample_result):
"""Info values should match result."""
info = sample_result.get_info()
assert info["size"] == "100x100"
assert info["original_mode"] == "RGB"
assert info["curve_applied"] is True
assert info["inverted"] is False
class TestImageProcessor:
"""Tests for ImageProcessor class."""
@pytest.fixture
def processor(self):
"""Create image processor."""
return ImageProcessor()
@pytest.fixture
def grayscale_image(self):
"""Create a grayscale test image."""
arr = np.zeros((100, 100), dtype=np.uint8)
# Create gradient
for i in range(100):
arr[i, :] = int(i * 2.55)
return Image.fromarray(arr, mode="L")
@pytest.fixture
def rgb_image(self):
"""Create an RGB test image."""
arr = np.zeros((100, 100, 3), dtype=np.uint8)
# Create color gradient
for i in range(100):
arr[i, :, 0] = int(i * 2.55) # Red
arr[i, :, 1] = int((100 - i) * 2.55) # Green
arr[i, :, 2] = 128 # Blue
return Image.fromarray(arr, mode="RGB")
@pytest.fixture
def linear_curve(self):
"""Create a linear curve (no change)."""
return CurveData(
name="Linear",
input_values=[i / 10 for i in range(11)],
output_values=[i / 10 for i in range(11)],
)
@pytest.fixture
def contrast_curve(self):
"""Create an S-curve for contrast."""
inputs = [i / 10 for i in range(11)]
# S-curve formula
outputs = [0.5 + 0.5 * np.tanh(2 * (x - 0.5)) for x in inputs]
outputs = [(o - min(outputs)) / (max(outputs) - min(outputs)) for o in outputs]
return CurveData(
name="Contrast",
input_values=inputs,
output_values=outputs,
)
def test_load_image_from_pil(self, processor, grayscale_image):
"""Load image from PIL Image."""
result = processor.load_image(grayscale_image)
assert result.image is not None
assert result.original_size == (100, 100)
assert result.original_mode == "L"
assert result.curve_applied is False
assert result.inverted is False
def test_load_image_from_numpy(self, processor):
"""Load image from numpy array."""
arr = np.ones((50, 50), dtype=np.uint8) * 128
result = processor.load_image(arr)
assert result.image is not None
assert result.original_size == (50, 50)
assert result.original_mode == "L"
def test_load_image_rgb_from_numpy(self, processor):
"""Load RGB image from numpy array."""
arr = np.ones((50, 50, 3), dtype=np.uint8) * 128
result = processor.load_image(arr)
assert result.image is not None
assert result.original_mode == "RGB"
def test_load_image_from_bytes(self, processor, grayscale_image):
"""Load image from bytes."""
buffer = io.BytesIO()
grayscale_image.save(buffer, format="PNG")
buffer.seek(0)
result = processor.load_image(buffer.getvalue())
assert result.image is not None
assert result.original_size == (100, 100)
def test_apply_linear_curve_no_change(self, processor, grayscale_image, linear_curve):
"""Linear curve should not change image significantly."""
result = processor.load_image(grayscale_image)
processed = processor.apply_curve(result, linear_curve)
assert processed.curve_applied is True
assert processed.image.size == grayscale_image.size
# Values should be approximately the same
orig_arr = np.array(grayscale_image)
proc_arr = np.array(processed.image)
assert np.allclose(orig_arr, proc_arr, atol=2)
def test_apply_contrast_curve(self, processor, grayscale_image, contrast_curve):
"""Contrast curve should modify image."""
result = processor.load_image(grayscale_image)
processed = processor.apply_curve(result, contrast_curve)
assert processed.curve_applied is True
# Midtones should be preserved, but darks darker and lights lighter
orig_arr = np.array(grayscale_image)
proc_arr = np.array(processed.image)
# Not exactly the same
assert not np.allclose(orig_arr, proc_arr, atol=5)
def test_apply_curve_rgb(self, processor, rgb_image, linear_curve):
"""Curve should apply to RGB image."""
result = processor.load_image(rgb_image)
processed = processor.apply_curve(result, linear_curve, ColorMode.RGB)
assert processed.curve_applied is True
assert processed.image.mode == "RGB"
def test_apply_curve_grayscale_conversion(self, processor, rgb_image, linear_curve):
"""RGB image should convert to grayscale when requested."""
result = processor.load_image(rgb_image)
processed = processor.apply_curve(result, linear_curve, ColorMode.GRAYSCALE)
assert processed.image.mode == "L"
def test_invert_grayscale(self, processor, grayscale_image):
"""Inverting grayscale should flip values."""
result = processor.load_image(grayscale_image)
inverted = processor.invert(result)
assert inverted.inverted is True
orig_arr = np.array(grayscale_image)
inv_arr = np.array(inverted.image)
# Check inversion
assert np.allclose(inv_arr, 255 - orig_arr)
def test_invert_rgb(self, processor, rgb_image):
"""Inverting RGB should flip all channels."""
result = processor.load_image(rgb_image)
inverted = processor.invert(result)
assert inverted.inverted is True
orig_arr = np.array(rgb_image)
inv_arr = np.array(inverted.image)
assert np.allclose(inv_arr, 255 - orig_arr)
def test_double_invert_restores_original(self, processor, grayscale_image):
"""Double inversion should restore original."""
result = processor.load_image(grayscale_image)
inverted1 = processor.invert(result)
inverted2 = processor.invert(inverted1)
# inverted twice = not inverted
assert inverted2.inverted is False
orig_arr = np.array(grayscale_image)
double_arr = np.array(inverted2.image)
assert np.allclose(orig_arr, double_arr)
def test_create_digital_negative(self, processor, grayscale_image, linear_curve):
"""Create complete digital negative."""
result = processor.create_digital_negative(
grayscale_image,
curve=linear_curve,
invert=True,
color_mode=ColorMode.GRAYSCALE,
)
assert result.curve_applied is True
assert result.inverted is True
assert result.image.mode == "L"
def test_create_digital_negative_no_curve(self, processor, grayscale_image):
"""Digital negative without curve (invert only)."""
result = processor.create_digital_negative(
grayscale_image,
curve=None,
invert=True,
)
assert result.curve_applied is False
assert result.inverted is True
def test_create_digital_negative_no_invert(self, processor, grayscale_image, linear_curve):
"""Digital negative with curve but no inversion."""
result = processor.create_digital_negative(
grayscale_image,
curve=linear_curve,
invert=False,
)
assert result.curve_applied is True
assert result.inverted is False
def test_preview_curve_effect(self, processor, grayscale_image, linear_curve):
"""Preview should return both original and processed images."""
original, processed = processor.preview_curve_effect(
grayscale_image,
linear_curve,
)
assert original is not None
assert processed is not None
assert original.size == processed.size
def test_preview_with_thumbnail(self, processor, grayscale_image, linear_curve):
"""Preview with thumbnail size should resize."""
original, processed = processor.preview_curve_effect(
grayscale_image,
linear_curve,
thumbnail_size=(50, 50),
)
assert max(original.size) <= 50
assert max(processed.size) <= 50
def test_export_to_file_png(self, processor, grayscale_image, tmp_path):
"""Export to PNG file."""
result = processor.load_image(grayscale_image)
output_path = tmp_path / "test_output.png"
settings = ExportSettings(format=ImageFormat.PNG)
processor.export(result, output_path, settings)
assert output_path.exists()
# Verify it's a valid image
loaded = Image.open(output_path)
assert loaded.size == grayscale_image.size
def test_export_to_file_jpeg(self, processor, rgb_image, tmp_path):
"""Export to JPEG file."""
result = processor.load_image(rgb_image)
output_path = tmp_path / "test_output.jpg"
settings = ExportSettings(format=ImageFormat.JPEG, jpeg_quality=90)
processor.export(result, output_path, settings)
assert output_path.exists()
loaded = Image.open(output_path)
assert loaded.size == rgb_image.size
def test_export_to_file_tiff(self, processor, grayscale_image, tmp_path):
"""Export to TIFF file."""
result = processor.load_image(grayscale_image)
output_path = tmp_path / "test_output.tiff"
settings = ExportSettings(format=ImageFormat.TIFF)
processor.export(result, output_path, settings)
assert output_path.exists()
def test_export_to_bytes(self, processor, grayscale_image):
"""Export to bytes."""
result = processor.load_image(grayscale_image)
settings = ExportSettings(format=ImageFormat.PNG)
data, ext = processor.export_to_bytes(result, settings)
assert data is not None
assert len(data) > 0
assert ext == ".png"
# Verify it's a valid image
img = Image.open(io.BytesIO(data))
assert img.size == grayscale_image.size
def test_export_jpeg_quality(self, processor, rgb_image):
"""Higher JPEG quality should produce larger files."""
result = processor.load_image(rgb_image)
low_quality = ExportSettings(format=ImageFormat.JPEG, jpeg_quality=50)
high_quality = ExportSettings(format=ImageFormat.JPEG, jpeg_quality=100)
low_data, _ = processor.export_to_bytes(result, low_quality)
high_data, _ = processor.export_to_bytes(result, high_quality)
# High quality should be larger
assert len(high_data) > len(low_data)
def test_get_supported_formats(self):
"""Supported formats should include common types."""
formats = ImageProcessor.get_supported_formats()
assert ".jpg" in formats
assert ".png" in formats
assert ".tiff" in formats
def test_get_export_formats(self):
"""Export formats should be available."""
formats = ImageProcessor.get_export_formats()
assert len(formats) > 0
# Each format should be (value, description) tuple
for value, desc in formats:
assert isinstance(value, str)
assert isinstance(desc, str)
class TestCurveLUT:
"""Tests for LUT creation and application."""
@pytest.fixture
def processor(self):
return ImageProcessor()
def test_lut_creates_256_entries(self, processor):
"""LUT should have 256 entries."""
curve = CurveData(
name="Test",
input_values=[0, 0.5, 1],
output_values=[0, 0.5, 1],
)
lut = processor._create_lut(curve)
assert len(lut) == 256
def test_lut_interpolates(self, processor):
"""LUT should interpolate between curve points."""
curve = CurveData(
name="Test",
input_values=[0, 1],
output_values=[0, 1],
)
lut = processor._create_lut(curve)
# Check midpoint interpolation
assert lut[127] in range(125, 130)
assert lut[0] == 0
assert lut[255] == 255
def test_lut_caches(self, processor):
"""LUT should be cached for repeated use."""
curve = CurveData(
name="CachedCurve",
input_values=[0, 1],
output_values=[0, 1],
)
lut1 = processor._create_lut(curve)
lut2 = processor._create_lut(curve)
# Should be the same object from cache
assert lut1 is lut2
class TestEdgeCases:
"""Tests for edge cases."""
@pytest.fixture
def processor(self):
return ImageProcessor()
def test_load_unsupported_type_raises(self, processor):
"""Loading unsupported type should raise."""
with pytest.raises(TypeError):
processor.load_image(12345)
def test_invert_rgba_preserves_alpha(self, processor):
"""Inverting RGBA should preserve alpha channel."""
arr = np.ones((50, 50, 4), dtype=np.uint8) * 128
arr[:, :, 3] = 200 # Set alpha
img = Image.fromarray(arr, mode="RGBA")
result = processor.load_image(img)
inverted = processor.invert(result)
inv_arr = np.array(inverted.image)
# Alpha should be preserved
assert np.all(inv_arr[:, :, 3] == 200)
# RGB should be inverted
assert np.all(inv_arr[:, :, 0] == 127)
def test_curve_with_single_point_interpolates(self, processor):
"""Curve with few points should still work."""
curve = CurveData(
name="Sparse",
input_values=[0, 1],
output_values=[0.2, 0.8],
)
arr = np.ones((10, 10), dtype=np.uint8) * 128
img = Image.fromarray(arr, mode="L")
result = processor.load_image(img)
processed = processor.apply_curve(result, curve)
assert processed.image is not None
def test_empty_processing_notes(self, processor):
"""New result should have empty notes."""
arr = np.ones((10, 10), dtype=np.uint8) * 128
img = Image.fromarray(arr, mode="L")
result = processor.load_image(img)
assert result.processing_notes == []
def test_processing_notes_accumulate(self, processor):
"""Notes should accumulate through processing."""
curve = CurveData(
name="Test",
input_values=[0, 1],
output_values=[0, 1],
)
arr = np.ones((10, 10), dtype=np.uint8) * 128
img = Image.fromarray(arr, mode="L")
result = processor.load_image(img)
result = processor.apply_curve(result, curve)
result = processor.invert(result)
assert len(result.processing_notes) == 2
|