import tensorflow as tf
import copy

def create_list_object(Object, count):
    """ 
    create a list of obejct using deep copy in 
    cased used in different theads

    Args:
            Object: object to be copied
            count: the number of copies
    Return:
            a list of objects
    """
    res_list = []
    for _ in xrange(count):
            res_list.append(Object)
    return res_list	

class DataClass():
    """ DataClass:
            used for decode line
    """
    def __init__(self, data_format):
        self.data_format = data_format
        self.decode_class = None

class BINClass():
    """ 
            used for load binary file
    """
    def __init__(self, shape, dtype = tf.float32):
        """ shape: a list """
        self.decode_fun = tf.decode_raw	
        self.dtype = dtype
        self.shape = shape

    def decode(self, filename, distort_data = False, whiten_data = False):
        """ distort_data and whiten_data are not used """
        bin_file = tf.read_file(filename)
        bin_tensor = tf.decode_raw(bin_file, self.dtype)
        bin_tensor = tf.to_float(bin_tensor)
        bin_tensor = tf.reshape(bin_tensor, self.shape)
        return bin_tensor

class ImageClass():
    def __init__(self, shape, channels, offset, ratio = None, name = None):
        """offset: the difference between cropped image and orginal image 
                final shape will be shape
        """
        self.channels = channels
        self.ratio = ratio
        self.name = name
        self.shape = shape
        self.offset = offset
        self.decode_fun = None

    def decode(self, filename, distort_data, whiten_data = True):
        """distort: random distort the iamge"""
        image_tensor = tf.read_file(filename)
        image_tensor = self.decode_fun(image_tensor, channels = self.channels, ratio = self.ratio)
        image_tensor = tf.image.convert_image_dtype(image_tensor, tf.float32)
        image_tensor = tf.image.resize_images(image_tensor,( 
                                        self.shape[0] + self.offset, self.shape[1] + self.offset))
        
        if distort_data:
                # it will crop in the function
                image_tensor = self.distort_op(image_tensor)
        else:
                image_tensor = tf.image.resize_image_with_crop_or_pad(image_tensor,
                                                                                self.shape[0], self.shape[1])
        if whiten_data:
                # Subtract off the mean and divide by the variance of the pixels.
                image_tensor = tf.image.per_image_whitening(image_tensor)
        
        return image_tensor

    def distort_op(self, image_tensor):
        """ copied from tensorflow cifar10 tutorial"""
        # Randomly crop a [height, width] section of the image.
        distorted_image = tf.random_crop(image_tensor, [self.shape[0],self.shape[1], self.channels])

        # Randomly flip the image horizontally.
        distorted_image = tf.image.random_flip_left_right(distorted_image)

        # Because these operations are not commutative, consider randomizing
        # the order their operation.
        # distorted_image = tf.image.random_brightness(distorted_image,
        # 									   max_delta=63)
        # distorted_image = tf.image.random_contrast(distorted_image,
        # 									 lower=0.2, upper=1.8)

        return distorted_image


class JPGClass(ImageClass):
    def __init__(self, shape, channels = None, off_set = None, ratio = None, name = None):
        ImageClass.__init__(self, shape, channels, off_set, ratio, name)
        """ 
            used for load jpg image file
        """
        self.decode_fun = tf.image.decode_jpeg
		
class PNGClass(ImageClass):
    """ 
        used for load png image file
    """
    def __init__(self, shape, channels = None, off_set = None, ratio = None, name = None):
        ImageClass.__init__(self, shape, channels, off_set, ratio, name)
        self.decode_fun = tf.image.decode_png


@@ -1999,17 +1999,16 @@
 _tensor,
-(
  %0A      
@@ -2033,32 +2033,33 @@
                 
+(
 self.shape%5B0%5D + 
@@ -2760,24 +2760,63 @@
 tutorial%22%22%22%0A
+        distorted_image = image_tensor%0A
         # Ra
@@ -2867,32 +2867,33 @@
  image.%0A        
+#
 distorted_image 
@@ -3018,32 +3018,33 @@
 ntally.%0A        
+#
 distorted_image 
@@ -3208,34 +3208,32 @@
 eration.%0A       
- #
  distorted_image
@@ -3279,33 +3279,32 @@
 _image,%0A        
-#
  %09%09%09%09%09%09%09%09%09   max
@@ -3314,10 +3314,11 @@
 lta=
-63
+0.2
 )%0A  
@@ -3322,18 +3322,16 @@
 %0A       
- #
  distort
@@ -3391,17 +3391,16 @@
         
-#
  %09%09%09%09%09%09%09
@@ -3414,17 +3414,17 @@
 r=0.
-2
+1
 , upper=
 1.8)
@@ -3423,11 +3423,11 @@
 per=
-1.8
+0.4
 )%0A%0A 

change data aug