def test_colorSnap(self):
     self.assertEqual(color_snap((0, 0, 0), ((0, 0, 0), (255, 255, 255))),
                      (0, 0, 0))
     self.assertEqual(
         color_snap((100, 100, 100), ((0, 0, 0), (255, 255, 255))),
         (0, 0, 0))
     self.assertEqual(
         color_snap((235, 210, 255), ((0, 0, 0), (255, 255, 255))),
         (255, 255, 255))
Example #2
0
    def _blocks_to_bits(self, how_many, palette_type):
        '''This is an internal method that, based on how many blocks it was told to scan as well as the palette type
        used, will scan that amount on the image and return those converted bits.
        '''

        bit_string = BitStream()
        active_color_set = self.palette_dict[palette_type].color_set
        active_palette_dict = self.palette_conversion_dict[palette_type]

        for block in range(how_many):
            block_coords = next(self.next_block)
            raw_rgb = scan_block(self.image, self.pixel_width, block_coords[0], block_coords[1])
            if active_color_set:
                bit_string.append(active_palette_dict.get_value(color_snap(raw_rgb, active_color_set)))

            else:
                bit_string.append(active_palette_dict.get_value(raw_rgb))

        self.block_position += how_many
        config.stats_handler.blocks_read += how_many

        return bit_string
Example #3
0
def verify_blocks_x(image, pixel_width, block_width_estimate, combined_colors, initializer_palette_a_dict,
                    initializer_palette_b_dict, override = False):
    '''This is a function used within frame_lock_on().  It verifies the correct values for the X axis.'''

    calibrator_bits_x = BitArray()
    for x_block in range(17):
        snapped_value = color_snap(scan_block(image, pixel_width, x_block, 0), combined_colors)

        if x_block % 2 == 0:
            calibrator_bits_x.append(initializer_palette_a_dict.get_value(snapped_value))

        else:
            calibrator_bits_x.append(initializer_palette_b_dict.get_value(snapped_value))

    calibrator_bits_x.reverse()
    read_calibrator_x = ConstBitStream(calibrator_bits_x)

    if read_calibrator_x.read('uint:16') != block_width_estimate:
        if override == True:
            logging.warning('block_width_override is not equal to what was read on calibrator.  Aborting...')

        else:
            logging.warning('block_width verification does not match initial read.  This could be the result of \n'
                            'sufficiently distorted frames.  Aborting...')

        return False

    if read_calibrator_x.read('bool') != False:
        logging.warning('0,0 block unexpected value.  Aborting...')
        return False

    if override == True:
        logging.info('block_width_override successfully verified.')

    else:
        logging.debug('block_width successfully verified.')

    return True
Example #4
0
def pixel_creep(image, initializer_palette_a, initializer_palette_b, combined_colors, initializer_palette_a_dict,
                initializer_palette_b_dict, image_width, image_height, width):
    '''This function moves across the calibrator on the top and left of the frame one pixel at a time, and after
    'snapping' the colors, decodes an unsigned integer from each axis, which if read correctly, is the block width and
    block height of the frame.
    '''

    calibrator_bits = BitArray()
    snapped_values = []
    active_color = (0, 0, 0)
    active_distance = 0
    pixel_on_dimension = 1
    palette_a_is_active = False

    if width == True:
        axis_on_image = pixel_on_dimension, 0
        axis_analyzed = image_width

    else:
        axis_on_image = 0, pixel_on_dimension
        axis_analyzed = image_height

    for value in range(16):
        while True:
            if width == True:
                axis_on_image = 0, pixel_on_dimension
                axis_analyzed = image_width

            else:
                axis_on_image = pixel_on_dimension, 0
                axis_analyzed = image_height

            new_palette_locked = False
            active_scan = flip(image[axis_on_image])
            active_distance = return_distance(active_scan, active_color)

            pixel_on_dimension += 1
            if active_distance < 100:  # Iterating over same colored blocks, until distance exceeds 100.
                continue

            else:  # We are determining if we are within < 100 dist of a new color, or are in fuzzy space.
                if palette_a_is_active == False:
                    active_palette = initializer_palette_b.color_set

                else:
                    active_palette = initializer_palette_a.color_set

                for color in active_palette:
                    active_distance = return_distance(active_scan, color)

                    if active_distance < 100:
                        palette_a_is_active = not palette_a_is_active
                        new_palette_locked = True
                        break

                    else:
                        continue

            if new_palette_locked == True:
                break

        active_color = color_snap(active_scan, combined_colors)
        snapped_values.append(active_color)

        if value % 2 != 0:
            calibrator_bits.append(initializer_palette_a_dict.get_value(active_color))

        else:
            calibrator_bits.append(initializer_palette_b_dict.get_value(active_color))

        active_distance = 0

    calibrator_bits.reverse()
    read_calibrator_bits = ConstBitStream(calibrator_bits)
    block_dimension_guess = read_calibrator_bits.read('uint:16')
    pixel_width = axis_analyzed / block_dimension_guess

    return pixel_width, block_dimension_guess