|
| 1 | +"""Tests for per-channel black level corrections (user_cblack parameter).""" |
| 2 | +from __future__ import division, print_function, absolute_import |
| 3 | + |
| 4 | +import os |
| 5 | +import pytest |
| 6 | +import numpy as np |
| 7 | +from numpy.testing import assert_array_equal |
| 8 | + |
| 9 | +import rawpy |
| 10 | + |
| 11 | +thisDir = os.path.dirname(__file__) |
| 12 | +rawTestPath = os.path.join(thisDir, 'iss030e122639.NEF') |
| 13 | + |
| 14 | + |
| 15 | +def test_user_cblack_parameter_acceptance(): |
| 16 | + """Test that the user_cblack parameter is accepted in Params constructor.""" |
| 17 | + # Test with valid 4-element list |
| 18 | + params = rawpy.Params(user_cblack=[100, 200, 150, 200]) |
| 19 | + assert params.user_cblack == [100, 200, 150, 200] |
| 20 | + |
| 21 | + # Test with None (default) |
| 22 | + params = rawpy.Params() |
| 23 | + assert params.user_cblack == [0, 0, 0, 0] |
| 24 | + |
| 25 | + |
| 26 | +def test_user_cblack_parameter_validation(): |
| 27 | + """Test that user_cblack parameter validates list length.""" |
| 28 | + # Should raise assertion error for wrong length |
| 29 | + with pytest.raises(AssertionError): |
| 30 | + rawpy.Params(user_cblack=[100, 200]) |
| 31 | + |
| 32 | + with pytest.raises(AssertionError): |
| 33 | + rawpy.Params(user_cblack=[100, 200, 150]) |
| 34 | + |
| 35 | + with pytest.raises(AssertionError): |
| 36 | + rawpy.Params(user_cblack=[100, 200, 150, 200, 250]) |
| 37 | + |
| 38 | + |
| 39 | +def test_user_cblack_postprocess(): |
| 40 | + """Test that user_cblack can be used in postprocessing without errors.""" |
| 41 | + with rawpy.imread(rawTestPath) as raw: |
| 42 | + # Process with per-channel black levels |
| 43 | + rgb = raw.postprocess(user_cblack=[100, 100, 100, 100], no_auto_bright=True) |
| 44 | + assert rgb.shape[2] == 3 # RGB image |
| 45 | + |
| 46 | + # Process with different per-channel values |
| 47 | + rgb2 = raw.postprocess(user_cblack=[50, 100, 150, 100], no_auto_bright=True) |
| 48 | + assert rgb2.shape[2] == 3 |
| 49 | + |
| 50 | + # Images should be different when different black levels are applied |
| 51 | + assert not np.array_equal(rgb, rgb2) |
| 52 | + |
| 53 | + |
| 54 | +def test_user_cblack_vs_user_black(): |
| 55 | + """Test that user_cblack and user_black can be used together in a single call. |
| 56 | + |
| 57 | + user_cblack values are corrections/offsets applied on top of user_black. |
| 58 | + For example: user_black=100, user_cblack=[10, 20, 30, 20] results in |
| 59 | + effective black levels of [110, 120, 130, 120] for each channel. |
| 60 | + """ |
| 61 | + with rawpy.imread(rawTestPath) as raw: |
| 62 | + # Process with both user_black and user_cblack in a single call |
| 63 | + # user_cblack provides per-channel corrections on top of user_black base value |
| 64 | + rgb = raw.postprocess( |
| 65 | + user_black=100, |
| 66 | + user_cblack=[10, 20, 30, 20], |
| 67 | + no_auto_bright=True |
| 68 | + ) |
| 69 | + assert rgb.shape[2] == 3 # RGB image |
| 70 | + |
| 71 | + # Verify that using both parameters together works without errors |
| 72 | + # and produces a valid image |
| 73 | + assert rgb.dtype == np.uint8 # Default output_bps is 8 |
| 74 | + |
| 75 | +if __name__ == '__main__': |
| 76 | + pytest.main([__file__, '-v']) |
0 commit comments