<commit_before>class RasterizerController < ApplicationController
  skip_before_filter :verify_authenticity_token
  helper RasterizerHelper

  def create
    size = params[:s].to_i
    size = 250 if size < 1

    verified_parameters = verify_parameters(params, size)

    unless verified_parameters == nil
      verified_parameters.call
      return
    end

    multiplier = size.to_f / 1000.to_f

    begin
      top = MiniMagick::Image.open(params[:top])
      side = MiniMagick::Image.open(params[:side])
      front = MiniMagick::Image.open(params[:front])
    rescue Errno::ENOENT
      render :json => {:error => 'One or more files does not exist.'},
           :status => :failed_dependency
      return
    end

    x_scale = multiplier * 520
    y_scale = multiplier * 662

    top.combine_options do |c|
      c.scale("#{x_scale}x#{602 * multiplier}!")
      c.background('none')
      c.shear('0x30')
      c.rotate('-60')
      c.gravity('center')
      c.scale('100%x85%!')
      c.crop("#{2000 * multiplier}x#{602 * multiplier}+0+0")
      c.repage("-1-#{298 * multiplier}")
    end

    side.combine_options do |c|
      c.scale("#{x_scale}x#{y_scale}!")
      c.background('none')
      c.shear('0x26')
    end

    front.combine_options do |c|
      c.brightness_contrast('-20x-18')
      c.scale("#{x_scale}x#{y_scale}!")
      c.background('none')
      c.shear('0x-26')
    end

    side.combine_options 'convert' do |c|
      c.push(front.path)
      c.append.+
      c.push(top.path)
      c.background('none')
      c.layers('merge')
      c.repage.+
      c.scale("#{size}x#{size}")
    end

    send_data(side.to_blob)
  end

  private

  def verify_parameters(params, size)
    missing_params = []

    [:top, :side, :front].each do |param|
      missing_params << param if params[param].nil?<commit_msg>Refactor rasterizer out to methods<commit_after>class RasterizerController < ApplicationController
  skip_before_filter :verify_authenticity_token
  helper RasterizerHelper

  def create
    size = params[:s].to_i
    size = 250 if size < 1

    verified_parameters = verify_parameters(params, size)

    unless verified_parameters == nil
      verified_parameters.call
      return
    end

    multiplier = size.to_f / 1000.to_f

    begin
      top = MiniMagick::Image.open(params[:top])
      side = MiniMagick::Image.open(params[:side])
      front = MiniMagick::Image.open(params[:front])
    rescue Errno::ENOENT
      render :json => {:error => 'One or more files does not exist.'},
           :status => :failed_dependency
      return
    end

    x_scale = multiplier * 520
    y_scale = multiplier * 662

    convert_top(top)
    convert_side(side)
    convert_front(front)

    combine_all(side)

    send_data(side.to_blob)
  end

  private

  def combine_all(side)
    side.combine_options 'convert' do |c|
      c.push(front.path)
      c.append.+
      c.push(top.path)
      c.background('none')
      c.layers('merge')
      c.repage.+
      c.scale("#{size}x#{size}")
    end
  end

  def convert_front(front)
    front.combine_options do |c|
      c.brightness_contrast('-20x-18')
      c.scale("#{x_scale}x#{y_scale}!")
      c.background('none')
      c.shear('0x-26')
    end
  end

  def convert_side(side)
    side.combine_options do |c|
        c.scale("#{x_scale}x#{y_scale}!")
        c.background('none')
        c.shear('0x26')
      end
  end

  def convert_top(top)
    top.combine_options do |c|
      c.scale("#{x_scale}x#{602 * multiplier}!")
      c.background('none')
      c.shear('0x30')
      c.rotate('-60')
      c.gravity('center')
      c.scale('100%x85%!')
      c.crop("#{2000 * multiplier}x#{602 * multiplier}+0+0")
      c.repage("-1-#{298 * multiplier}")
    end
  end

  def verify_parameters(params, size)
    missing_params = []

    [:top, :side, :front].each do |param|
      missing_params << param if params[param].nil?
{'fraction_non_alphanumeric': 0.1402610698745841, 'fraction_numerical': 0.025595085743537242, 'mean_word_length': 2.7254528122020973, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2479712', 'n_tokens_mistral': 1432, 'n_tokens_neox': 1336, 'n_words': 251}