# Path: gsdview/imgutils.py
# class LinearStretcher(BaseStretcher):
#     r"""Linear stretch.
# 
#     Perform linear scaling (including offest application) and clipping.
# 
#     .. note:: offset is applied before scaling:
# 
# 
#         .. math:: output = scale \cdot (data - offset)
# 
#     """
# 
#     stretchtype = 'linear'
# 
#     def __init__(self, scale=1.0, offset=0, vmin=0, vmax=255, dtype='uint8'):
#         super().__init__(vmin, vmax, dtype)
#         self.scale = scale
#         self.offset = offset
# 
#     def __call__(self, data):
#         data = np.asarray(data, dtype='float32')
#         if self.offset:
#             data -= self.offset
#         if self.scale != 1.0:
#             data *= self.scale
#         return super().__call__(data)
# 
#     @property
#     def range(self):
#         imin = self.min / self.scale + self.offset
#         imax = self.max / self.scale + self.offset
#         return imin, imax
# 
#     def set_range(self, imin, imax):
#         if (imin, imax) == self.range:
#             return
#         assert imin != imax
#         omax = self.max
#         omin = self.min
# 
#         self.scale = float(omax - omin) / (imax - imin)
#         self.offset = 0.5 * ((imax + imin) - (omax + omin) / self.scale)
# 
#         return self.offset, self.scale
# 
#     # @TODO:
#     # @property
#     # def outmax(self):
#     #    return self.__call__(self.max)
# 
# class LUTStretcher(BaseStretcher):
#     """Stretch using LUT.
# 
#     Perform an arbitrary scaling on unsigned data using a look-up table
#     (LUT).
# 
#     An optional offset is applied before LUT application.
# 
#     """
# 
#     stretchtype = 'lut'
# 
#     def __init__(self, offset=0, vmin=0, vmax=255, dtype='uint8', fill=True):
#         if np.dtype(dtype) not in (np.uint8, np.uint16):
#             raise ValueError('only "uint8" and "uint16" are allowed.')
#         super().__init__(vmin, vmax, dtype)
#         self.fill = fill
#         self.offset = offset
#         self.lut = linear_lut(offset, vmax, 'uint8', fill, vmin, vmax)
# 
#     def __call__(self, data):
#         data = np.asarray(data)
#         if self.offset:
#             data -= self.offset
#         if data.dtype != self.dtype:
#             data = data.clip(0, len(self.lut) - 1, out=data)
#             data = data.astype('uint32')
#         return self.lut[data]
# 
#     @property
#     def range(self):
#         indices = np.where(self.lut != self.lut[-1])[0]
#         imax = self.offset + len(indices)
#         return self.offset, imax
# 
#     def set_range(self, imin, imax, fill=None):
#         if (imin, imax) == self.range:
#             return
#         if imin == imax:
#             self.lut[...] = imin
#             return self.lut
# 
#         omax = self.max
#         omin = self.min
#         # assert omin != omax
#         self.offset = imin
#         if imin < 0:
#             imin = 0
#             imax -= self.offset
#         if fill is None:
#             fill = self.fill
# 
#         self.lut = linear_lut(imin, imax, self.dtype, fill, omin, omax)
# 
#         return self.lut


import os
import sys
import unittest
import numpy as np
from gsdview.imgutils import LinearStretcher, LUTStretcher
        self.assert_(np.all(output == refout))

    def test_scale2(self):
        stretch = LinearStretcher(scale=0.5, dtype='float')
        data = np.arange(512)
        output = stretch(data)
        refout = np.clip(data / 2., 0, 255)
        self.assert_(np.all(output == refout))

    def test_offset_and_scale(self):
        stretch = LinearStretcher(scale=0.5, offset=10)
        data = np.arange(512) + 10
        output = stretch(data)
        refout = np.clip((data - 10) / 2, 0, 255)
        self.assert_(np.all(output == refout))

    def test_set_imput_range1(self):
        stretch = LinearStretcher()
        stretch.set_range(10, 265)

        self.assertEqual(stretch.offset, 10)
        self.assertEqual(stretch.scale, 1)

        data = np.arange(512) + 10
        output = stretch(data)
        refout = np.clip(data - 10, 0, 255)

        self.assert_(np.all(output == refout))

    def test_set_imput_range2(self):

# Path: gsdview/imgutils.py
# class LinearStretcher(BaseStretcher):
#     r"""Linear stretch.
# 
#     Perform linear scaling (including offest application) and clipping.
# 
#     .. note:: offset is applied before scaling:
# 
# 
#         .. math:: output = scale \cdot (data - offset)
# 
#     """
# 
#     stretchtype = 'linear'
# 
#     def __init__(self, scale=1.0, offset=0, vmin=0, vmax=255, dtype='uint8'):
#         super().__init__(vmin, vmax, dtype)
#         self.scale = scale
#         self.offset = offset
# 
#     def __call__(self, data):
#         data = np.asarray(data, dtype='float32')
#         if self.offset:
#             data -= self.offset
#         if self.scale != 1.0:
#             data *= self.scale
#         return super().__call__(data)
# 
#     @property
#     def range(self):
#         imin = self.min / self.scale + self.offset
#         imax = self.max / self.scale + self.offset
#         return imin, imax
# 
#     def set_range(self, imin, imax):
#         if (imin, imax) == self.range:
#             return
#         assert imin != imax
#         omax = self.max
#         omin = self.min
# 
#         self.scale = float(omax - omin) / (imax - imin)
#         self.offset = 0.5 * ((imax + imin) - (omax + omin) / self.scale)
# 
#         return self.offset, self.scale
# 
#     # @TODO:
#     # @property
#     # def outmax(self):
#     #    return self.__call__(self.max)
# 
# class LUTStretcher(BaseStretcher):
#     """Stretch using LUT.
# 
#     Perform an arbitrary scaling on unsigned data using a look-up table
#     (LUT).
# 
#     An optional offset is applied before LUT application.
# 
#     """
# 
#     stretchtype = 'lut'
# 
#     def __init__(self, offset=0, vmin=0, vmax=255, dtype='uint8', fill=True):
#         if np.dtype(dtype) not in (np.uint8, np.uint16):
#             raise ValueError('only "uint8" and "uint16" are allowed.')
#         super().__init__(vmin, vmax, dtype)
#         self.fill = fill
#         self.offset = offset
#         self.lut = linear_lut(offset, vmax, 'uint8', fill, vmin, vmax)
# 
#     def __call__(self, data):
#         data = np.asarray(data)
#         if self.offset:
#             data -= self.offset
#         if data.dtype != self.dtype:
#             data = data.clip(0, len(self.lut) - 1, out=data)
#             data = data.astype('uint32')
#         return self.lut[data]
# 
#     @property
#     def range(self):
#         indices = np.where(self.lut != self.lut[-1])[0]
#         imax = self.offset + len(indices)
#         return self.offset, imax
# 
#     def set_range(self, imin, imax, fill=None):
#         if (imin, imax) == self.range:
#             return
#         if imin == imax:
#             self.lut[...] = imin
#             return self.lut
# 
#         omax = self.max
#         omin = self.min
#         # assert omin != omax
#         self.offset = imin
#         if imin < 0:
#             imin = 0
#             imax -= self.offset
#         if fill is None:
#             fill = self.fill
# 
#         self.lut = linear_lut(imin, imax, self.dtype, fill, omin, omax)
# 
#         return self.lut


# Path: tests/test_stretcher.py
import os
import sys
import unittest
import numpy as np
from gsdview.imgutils import LinearStretcher, LUTStretcher

        self.assert_(np.all(output == refout))

    def test_scale2(self):
        stretch = LinearStretcher(scale=0.5, dtype='float')
        data = np.arange(512)
        output = stretch(data)
        refout = np.clip(data / 2., 0, 255)
        self.assert_(np.all(output == refout))

    def test_offset_and_scale(self):
        stretch = LinearStretcher(scale=0.5, offset=10)
        data = np.arange(512) + 10
        output = stretch(data)
        refout = np.clip((data - 10) / 2, 0, 255)
        self.assert_(np.all(output == refout))

    def test_set_imput_range1(self):
        stretch = LinearStretcher()
        stretch.set_range(10, 265)

        self.assertEqual(stretch.offset, 10)
        self.assertEqual(stretch.scale, 1)

        data = np.arange(512) + 10
        output = stretch(data)
        refout = np.clip(data - 10, 0, 255)

        self.assert_(np.all(output == refout))

    def test_set_imput_range2(self):

        stretch = LinearStretcher()