lib/ruby_marks/recognizer.rb
def find_block_marks(image, x, y, group)
      expected_coordinates = group.expected_coordinates
      found_blocks = []
      expected_width  = expected_coordinates.width
      expected_height = expected_coordinates.height
      block = nil
      while x <= expected_coordinates.x2 && y <= expected_coordinates.y2
        if image[y] && image[y][x] == ' '
          block = find_in_blocks(found_blocks, x, y)
          unless block
            block = find_block(image, x, y)
            found_blocks << block

            block[:width]  = ImageUtils.calc_width(block[:x1], block[:x2])
            block[:height] = ImageUtils.calc_height(block[:y1], block[:y2])

            if @config.scan_mode == :grid
              unless block[:width] <= (expected_width + group.block_width_tolerance) &&
                     block[:width] >= (expected_width - group.block_width_tolerance)

                if block[:width] > expected_width + group.block_width_tolerance
                  ajust_width = block[:width] - expected_width
                  if @config.auto_ajust_block_width == :left
                    block[:x2] = (block[:x2] - ajust_width) + @config.edge_level
                    block[:width] = expected_width + @config.edge_level
                  elsif @config.auto_ajust_block_width == :right
                    block[:x1] = (block[:x1] + ajust_width) - @config.edge_level
                    block[:width] = expected_width + @config.edge_level
                  end
                else
                  block[:width] = 0
                end
              end
              unless block[:height] <= (expected_height + group.block_height_tolerance) &&
                     block[:height] >= (expected_height - group.block_height_tolerance)

                if block[:height] > expected_height + group.block_height_tolerance
                  ajust_width = block[:height] - expected_height
                  if @config.auto_ajust_block_height == :top
                    block[:y2] = (block[:y2] - ajust_height) + @config.edge_level
                    block[:height] = expected_height + @config.edge_level
                  elsif @config.auto_ajust_block_height == :bottom
                    block[:y1] = (block[:y1] + ajust_height) - @config.edge_level
                    block[:height] = expected_height + @config.edge_level
                  end
                else
                  block[:height] = 0
                end
              end
            end

            block_width_with_tolerance  = block[:width]  + group.block_width_tolerance
            block_height_with_tolerance = block[:height] + group.block_height_tolerance

            return block if block_width_with_tolerance >= expected_width &&
                            block_height_with_tolerance >= expected_height
          end
        end

        x += 1
        y += 1
      end
    end
def find_block_marks(image, x, y, group)
      expected_coordinates = group.expected_coordinates
      found_blocks = []
      expected_width  = expected_coordinates.width
      expected_height = expected_coordinates.height
      block = nil
      while x <= expected_coordinates.x2 && y <= expected_coordinates.y2
        if image[y] && image[y][x] == ' '
          block = find_in_blocks(found_blocks, x, y)
          unless block
            block = find_block(image, x, y)
            found_blocks << block

            block[:width]  = ImageUtils.calc_width(block[:x1], block[:x2])
            block[:height] = ImageUtils.calc_height(block[:y1], block[:y2])

            if @config.scan_mode == :grid
              unless block[:width] <= (expected_width + group.block_width_tolerance) &&
                     block[:width] >= (expected_width - group.block_width_tolerance)

                if block[:width] > expected_width + group.block_width_tolerance
                  ajust_width = block[:width] - expected_width
                  if @config.auto_ajust_block_width == :left
                    block[:x2] = (block[:x2] - ajust_width) + @config.edge_level
                    block[:width] = expected_width + @config.edge_level
                  elsif @config.auto_ajust_block_width == :right
                    block[:x1] = (block[:x1] + ajust_width) - @config.edge_level
                    block[:width] = expected_width + @config.edge_level
                  end
                else
                  block[:width] = 0
                end
              end
              unless block[:height] <= (expected_height + group.block_height_tolerance) &&
                     block[:height] >= (expected_height - group.block_height_tolerance)

                if block[:height] > expected_height + group.block_height_tolerance
                  ajust_width = block[:height] - expected_height
                  if @config.auto_ajust_block_height == :top
                    block[:y2] = (block[:y2] - ajust_height) + @config.edge_level
                    block[:height] = expected_height + @config.edge_level
                  elsif @config.auto_ajust_block_height == :bottom
                    block[:y1] = (block[:y1] + ajust_height) - @config.edge_level
                    block[:height] = expected_height + @config.edge_level
                  end
                else
                  block[:height] = 0
                end
              end
            end

            block_width_with_tolerance  = block[:width]  + group.block_width_tolerance
            block_height_with_tolerance = block[:height] + group.block_height_tolerance

            return block if block_width_with_tolerance >= expected_width &&
                            block_height_with_tolerance >= expected_height
          end
        end

        x += 1
        y += 1
      end
    end
rubocop:enable Metrics/MethodLength
 rubocop:enable Metrics/CyclomaticComplexity
 rubocop:enable Metrics/PerceivedComplexity
 rubocop:enable Metrics/BlockNesting
 rubocop:disable Metrics/CyclomaticComplexity
 rubocop:disable Metrics/MethodLength
 rubocop:disable Metrics/PerceivedComplexity
 rubocop:disable Metrics/BlockNesting