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

Commit

Permalink
Make options its own class
Browse files Browse the repository at this point in the history
  • Loading branch information
SamWhited committed May 23, 2015
1 parent a4304f6 commit 9508ab6
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 82 deletions.
32 changes: 0 additions & 32 deletions rawkit/color.py

This file was deleted.

104 changes: 104 additions & 0 deletions rawkit/options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
""":mod:`rawkit.options` --- Options for processing raw files
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"""

import ctypes


class WhiteBalance(object):

"""
Represents the white balance of a photo.
:param auto: determines if we should automatically set the WB
:type auto: :class:`boolean`
:param camera: causes us to use the camera defined WB if present
:type camera: :class:`boolean`
:param greybox: set the WB based on a neutral grey region of the image
:type greybox: :class:`4 int tuple`
:param rgbg: set the WB manually based on an RGBG channel multiplier
:type rgbg: :class:`4 float tuple`
:returns: A white blance object
:rtype: :class:`WhiteBalance`
"""

def __init__(self,
auto=False,
camera=False,
greybox=None,
rgbg=None):
"""Initialize a white balance object."""
self.auto = auto
self.camera = camera
self.greybox = greybox
self.rgbg = rgbg


class Options(object):

"""
Represents a set of options which can be used when processing raw data.
"""

__slots__ = [
'_bps',
'brightness',
'darkness',
'half_size',
'noise_threshold',
'rgbg',
'saturation',
'shot',
'use_camera_matrix',
'white_balance',
'greybox'
]

def __init__(self):
for i in self.__slots__:
setattr(self, i, None)
self._bps = 8
self.white_balance = WhiteBalance(auto=True, camera=True)
self.half_size = False

@property
def bps(self):
return self._bps

@bps.setter
def bps(self, value):
if value in (8, 16):
self._bps = value
else:
raise ValueError("BPS must be 8 or 16")

def map_to_libraw_params(self, params):
"""
Internal method that writes rawkit options into the libraw options
struct with the proper C data types.
"""
params.output_bps = ctypes.c_int(self.bps)

if self.brightness is not None:
params.bright = ctypes.c_float(self.brightness)
if self.darkness is not None:
params.user_black = ctypes.c_int(self.darkness)

params.half_size = ctypes.c_int(self.half_size)
if self.noise_threshold is not None:
params.threshold = ctypes.c_float(self.noise_threshold)
if self.rgbg is not None:
params.four_color_rgb = ctypes.c_int(self.rgbg)
if self.saturation is not None:
params.user_sat = ctypes.c_int(self.saturation)
if self.shot is not None:
params.shot_select = ctypes.c_uint(self.shot)
if self.use_camera_matrix is not None:
params.use_camera_matrix = ctypes.c_int(self.use_camera_matrix)

if self.white_balance.greybox is not None:
params.greybox = (ctypes.c_uint * 4)(*self.white_balance.greybox)
if self.white_balance.rgbg is not None:
params.user_mul = (ctypes.c_float * 4)(*self.white_balance.rgbg)
params.use_camera_wb = ctypes.c_int(self.white_balance.camera)
params.use_auto_wb = ctypes.c_int(self.white_balance.auto)
53 changes: 3 additions & 50 deletions rawkit/raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from rawkit.libraw import libraw
from rawkit.metadata import Metadata
from rawkit.options import Options


class Raw(object):
Expand Down Expand Up @@ -33,7 +34,7 @@ def __init__(self, filename=None):
self.data = libraw.libraw_init(0)
libraw.libraw_open_file(self.data, filename.encode('ascii'))

self.options = {}
self.options = Options()

self.image_unpacked = False
self.thumb_unpacked = False
Expand Down Expand Up @@ -62,57 +63,9 @@ def unpack_thumb(self):
libraw.libraw_unpack_thumb(self.data)
self.thumb_unpacked = True

def _set_options(self):
"""
Internal method that writes rawkit options into the libraw options
struct with the proper C data types.
"""
if 'bps' in self.options:
self.data.contents.params.output_bps = ctypes.c_int(
self.options['bps'])
if 'brightness' in self.options:
self.data.contents.params.bright = ctypes.c_float(
self.options['brightness'])
if 'darkness' in self.options:
self.data.contents.params.user_black = ctypes.c_int(
self.options['darkness'])
if 'half_size' in self.options:
self.data.contents.params.half_size = ctypes.c_int(
self.options['half_size'])
if 'noise_threshold' in self.options:
self.data.contents.params.threshold = ctypes.c_float(
self.options['noise_threshold'])
if 'rgbg' in self.options:
self.data.contents.params.four_color_rgb = ctypes.c_int(
self.options['rgbg'])
if 'saturation' in self.options:
self.data.contents.params.user_sat = ctypes.c_int(
self.options['saturation'])
if 'shot' in self.options:
self.data.contents.params.shot_select = ctypes.c_uint(
self.options['shot'])
if 'use_camera_color_matrix' in self.options and self.options['use_camera_color_matrix'] is True:
self.data.contents.params.use_camera_matrix = ctypes.c_int(1)
else:
self.data.contents.params.use_camera_matrix = ctypes.c_int(0)
if 'white_balance' not in self.options:
self.data.contents.params.use_camera_wb = ctypes.c_int(1)
self.data.contents.params.use_auto_wb = ctypes.c_int(1)
else:
if self.options['white_balance'].greybox is not None:
self.data.contents.params.greybox = (
ctypes.c_uint * 4)(*self.options['white_balance'].greybox)
if self.options['white_balance'].rgbg is not None:
self.data.contents.params.user_mul = (
ctypes.c_float * 4)(*self.options['white_balance'].rgbg)
self.data.contents.params.use_camera_wb = ctypes.c_int(
self.options['white_balance'].camera)
self.data.contents.params.use_auto_wb = ctypes.c_int(
self.options['white_balance'].auto)

def process(self):
"""Process the raw data based on self.options"""
self._set_options()
self.options.map_to_libraw_params(self.data.contents.params)
libraw.libraw_dcraw_process(self.data)

def save(self, filename=None, filetype='ppm'):
Expand Down

0 comments on commit 9508ab6

Please sign in to comment.