Exemplo n.º 1
0
 def extract_op(self, val, ch_two):
     # Update state
     self.tj_count += 1
     # Check value against settings
     if (not self.improve and abs(val) > 2**self.nbits
         ) or val == 0 or ch_two < self.redundancy or (
             self.customrange
             and not encoding.is_in_crange(val, self.nbits)):
         # Value is either out of range
         # or ruled out by redundancy
         # or invalid
         # -> Do not use TJ op
         return 0
     # Value is in range
     # -> Update state
     self.tj_count_valid += 1
     if 0:  #self.improve and self.tj_count == 1:
         return val
     # Extract data from TJ op
     return abs(val)
Exemplo n.º 2
0
	def extract_op(self,val,ch_two):
		# Update state
		self.tj_count += 1
		# Check value against settings
		if (not self.improve and abs(val) > 2**self.nbits) or val == 0 or ch_two < self.redundancy or (self.customrange and not encoding.is_in_crange(val,self.nbits)):
			# Value is either out of range
			# or ruled out by redundancy
			# or invalid
			# -> Do not use TJ op
			return 0
		# Value is in range
		# -> Update state
		self.tj_count_valid += 1
		if 0:#self.improve and self.tj_count == 1:
			return val
		# Extract data from TJ op
		return abs(val)
Exemplo n.º 3
0
	def embed_op(self,val,ch_one,ch_two,num):
		# Check the original TJ value
		if (not self.improve and abs(val) > 2**self.nbits) or val == 0:
			# Value is invalid
			# -> Do not use TJ op
			return [False,val]
		# Original TJ value is valid
		# -> Use TJ op
		self.tj_count += 1
		# -> Check improvement flag
		if self.improve:
			# Improvements are enabled
			# -> Check redundancy and custom range settings
			if ch_two < self.redundancy or num == None or (self.customrange and not encoding.is_in_crange(val,self.nbits)):
				# NB: Custom range values work because of the hack normalrange/customrange
				# TODO: update docs!!!
				#
				# Value is either out of range
				# or ruled out by redundancy
				# or all data is already embedded
				# -> Check no-random flag
				if self.norandom:
					# No-random is set
					# -> Do not use TJ op
					return [False,val]
				# Use TJ op for a random value
				# -> Check sign
				if val < 0:
					return [False,-abs(val) + (abs(val) % (2**self.nbits)) - (int((2**self.nbits - 1) * ch_one) + 1 )]
				return [False,abs(val) - (abs(val) % (2**self.nbits)) + int((2**self.nbits - 1) * ch_one) + 1]
			# Value is in range
			# -> Use TJ op for data
			self.tj_count_valid += 1
			normalrange = 1
			# NB: Hack for custom range (do not shift by 1)
			# TODO: do that better and include in docs
			if self.customrange:
				normalrange = 0
			# Compute new TJ value
			# -> Check sign
			if val < 0:
				return [True,-abs(val) + (abs(val) % (2**self.nbits)) - num - normalrange]
			return [True,abs(val) - (abs(val) % (2**self.nbits)) + num + normalrange]
		# Improvements are disabled
		# -> Check redundancy setting
		if ch_two < self.redundancy or num == None:
			# Value is either ruled out by redundancy
			# or all data is already embedded
			# -> Check no-random flag
			if self.norandom:
				# No-random is set
				# -> Do not use TJ op
				return [False,val]
			# Use TJ op for a random value
			# -> Check sign
			if val < 0:
				return [False,-(int((2**self.nbits - 1) * ch_one) + 1 )]
			return [False,int((2**self.nbits - 1) * ch_one) + 1]
		# Value is in range
		# -> Use TJ op for data
		self.tj_count_valid += 1
		# -> Check sign
		if val < 0:
			return [True, -num - 1]
		return [True, num + 1]
Exemplo n.º 4
0
 def embed_op(self, val, ch_one, ch_two, num):
     # Check the original TJ value
     if (not self.improve and abs(val) > 2**self.nbits) or val == 0:
         # Value is invalid
         # -> Do not use TJ op
         return [False, val]
     # Original TJ value is valid
     # -> Use TJ op
     self.tj_count += 1
     # -> Check improvement flag
     if self.improve:
         # Improvements are enabled
         # -> Check redundancy and custom range settings
         if ch_two < self.redundancy or num == None or (
                 self.customrange
                 and not encoding.is_in_crange(val, self.nbits)):
             # NB: Custom range values work because of the hack normalrange/customrange
             # TODO: update docs!!!
             #
             # Value is either out of range
             # or ruled out by redundancy
             # or all data is already embedded
             # -> Check no-random flag
             if self.norandom:
                 # No-random is set
                 # -> Do not use TJ op
                 return [False, val]
             # Use TJ op for a random value
             # -> Check sign
             if val < 0:
                 return [
                     False, -abs(val) + (abs(val) % (2**self.nbits)) - (int(
                         (2**self.nbits - 1) * ch_one) + 1)
                 ]
             return [
                 False,
                 abs(val) - (abs(val) % (2**self.nbits)) + int(
                     (2**self.nbits - 1) * ch_one) + 1
             ]
         # Value is in range
         # -> Use TJ op for data
         self.tj_count_valid += 1
         normalrange = 1
         # NB: Hack for custom range (do not shift by 1)
         # TODO: do that better and include in docs
         if self.customrange:
             normalrange = 0
         # Compute new TJ value
         # -> Check sign
         if val < 0:
             return [
                 True, -abs(val) + (abs(val) % (2**self.nbits)) - num -
                 normalrange
             ]
         return [
             True,
             abs(val) - (abs(val) % (2**self.nbits)) + num + normalrange
         ]
     # Improvements are disabled
     # -> Check redundancy setting
     if ch_two < self.redundancy or num == None:
         # Value is either ruled out by redundancy
         # or all data is already embedded
         # -> Check no-random flag
         if self.norandom:
             # No-random is set
             # -> Do not use TJ op
             return [False, val]
         # Use TJ op for a random value
         # -> Check sign
         if val < 0:
             return [False, -(int((2**self.nbits - 1) * ch_one) + 1)]
         return [False, int((2**self.nbits - 1) * ch_one) + 1]
     # Value is in range
     # -> Use TJ op for data
     self.tj_count_valid += 1
     # -> Check sign
     if val < 0:
         return [True, -num - 1]
     return [True, num + 1]