Skip to content
This repository has been archived by the owner on Oct 4, 2020. It is now read-only.

Commit

Permalink
Add rotation option
Browse files Browse the repository at this point in the history
Fixes #72

Add slot for auto_stretch option
  • Loading branch information
SamWhited committed Jun 27, 2015
1 parent 1453926 commit 9eb0e5f
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 13 deletions.
42 changes: 41 additions & 1 deletion rawkit/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,9 @@ class Options(object):
'_colorspace',
'_cropbox',
'_gamma',
'_interpolation'
'_interpolation',
'_auto_stretch',
'_rotation',
]
"""The options which are supported by this class."""

Expand Down Expand Up @@ -562,6 +564,41 @@ def auto_stretch(self):
"""
return True

@option
def rotation(self):
"""
Rotates the image by the given number of degrees. Must be a multiple of
90 (0, 90, 180, 270, etc).
The default (None) is to use the rotation provided by the camera.
:type: :class:`int`
:default: None
:dcraw: ``-t``
:libraw: :class:`libraw.structs.libraw_output_params_t.user_flip`
"""
return None

@rotation.setter
def rotation(self, value):
if value is not None and value > 0:
value = ((value + 3600) % 360)

if value in (None, 0, 90, 180, 270):
self._rotation = value
else:
raise ValueError('Rotation must be None (use camera rotation) or '
'a multiple of 90')

@rotation.param_writer
def rotation(self, params):
params.user_flip = ctypes.c_int({
270: 5,
180: 3,
90: 6,
0: 0
}[self._rotation])

def _map_to_libraw_params(self, params):
"""
Internal method that writes rawkit options into the libraw options
Expand All @@ -575,3 +612,6 @@ def _map_to_libraw_params(self, params):
opt = getattr(Options, prop)
if type(opt) is option and getattr(self, prop) is not None:
opt.write_param(self, params)

# This generally isn't needed, except for testing.
return params
54 changes: 42 additions & 12 deletions tests/unit/options_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,21 @@ def test_option_from_class_should_return_decorator():


def test_custom_wb_param_writer_writes_rgbg_and_greybox(options):
params = Mock()
options.white_balance = WhiteBalance(greybox=(0, 0, 0, 0),
rgbg=(0, 0, 0, 0))
options._map_to_libraw_params(params)
assert params.greybox is not None
assert params.rgbg is not None
options.white_balance = WhiteBalance(greybox=(7, 7, 7, 7),
rgbg=(42, 42, 42, 42))
params = options._map_to_libraw_params(Mock())

for v in params.greybox:
assert v == 7
for v in params.user_mul:
assert v == 42


def test_bps_must_be_8_or_16(options):
with pytest.raises(ValueError):
options.bps = 5


def test_map_params_fails_on_invalid(options):
# with pytest.raises(AttributeError):
params = Mock()
options._map_to_libraw_params(params)


def test_options_are_iterable(options):
options.half_size = True
assert 'half_size' in options
Expand All @@ -84,3 +80,37 @@ def test_options_values(options):
options.half_size = True

assert options.values() == [True]


def test_set_rotation_invalid_type(options):
with pytest.raises(TypeError):
options.rotation = 'fish'


def test_set_rotation_value_is_reduced(options):
options.rotation = 270 + 90
assert options.rotation == 0

options.rotation = 270 + 180
assert options.rotation == 90

options.rotation = None
assert options.rotation is None


def test_set_rotation_invalid_value(options):
with pytest.raises(ValueError):
options.rotation = 93.5


def test_rotation_param_writer_values(options):
values = {
270: 5,
180: 3,
90: 6,
0: 0
}
for value in values.keys():
options.rotation = value
params = options._map_to_libraw_params(Mock())
assert params.user_flip.value == values[value]

0 comments on commit 9eb0e5f

Please sign in to comment.