All pastes #156696 Raw Edit

Untitled

public text v1 · immutable
#156696 ·published 2006-08-31 22:50 UTC
rendered paste body
#!/System/Links/ExecutableBinaries/ruby -w
# ========================================================== #
# Used to draw images. Also called "std_images.rb"
# ========================================================== #
# std_drawing.rb
# ========================================================== #
%w(
x/STD/std_constants.rb
x/STD/std_modules.rb
x/STD/std_classes.rb
x/STD/std_system.rb
x/STD/custom_functions.rb
x/STD/std_audio.rb
x/STD/std_rpg.rb
x/STD/std_colours.rb
).each {|f| require f }

# ========================================================== #
# Wrapper mostly for RMagick.
# StdDrawing
# ========================================================== #
module StdDrawing
  
  include StdSystem
  include StdColours
  
  begin
    require 'RMagick'
    require 'rvg/rvg'
    include Magick
  rescue Exception => e
    puts "Error: #{e}"
  end
  
  DEFAULT_CANVAS_WIDTH  = 540
  DEFAULT_CANVAS_HEIGHT = 540
  DCW = DEFAULT_CANVAS_WIDTH
  DCH = DEFAULT_CANVAS_HEIGHT
  
  
  # For read file
  DEFAULT_IMAGE_INPUT_LOCATION = '/FOO.png'
  # For save file
  DEFAULT_IMAGE_OUTPUT_LOCATION = '/StdDrawing2.png'
   
  MIXIN_IMAGE = '/Depot/Images/alien_flower.png' # image to use as test toy
  # /Depot/Images/ferma.png
  
  # Disable this if you need a mixing Image.
  MIXING_IMAGE = DEFAULT_IMAGE_INPUT_LOCATION
  
  
  # =============================================================== #
  # === display_image
  # =============================================================== #
  def display_image(url=MIXIN_IMAGE)
    cme "Now displaying #{url}"
    url = ImageList.new(url)
    url.display
    exit
  end
  # =============================================================== #
  # === find_image_center
  # gives back the image center
  # =============================================================== #
  def find_image_center(image)
    return [ (image.columns/2), (image.rows/2)]
  end
  
  # =============================================================== #
  # === random_image
  # Just to get a random image, so that we dont spend too
  # much time worrying.
  #   StdDrawing::random_image
  # =============================================================== #
  def random_image
    tmp=rand(2)
    case tmp 
    when 0
      return FileListing.get_file_listing(HSS['IMG_RPG']+'/ADOPTION',true).rand
    when 1
      return FileListing.get_file_listing(HSS['IMG']+'/NJOY',true).rand
    end
  end
  # =============================================================== #
  # === jp2a
  # shows a jp2a image.
  # StdDrawing::jp2a
  # =============================================================== #
  def jp2a(use_this_image = random_image)
    my_image = use_this_image
    the_cmd = 'convert '+my_image+' jpg:- | jp2a -'
    cme(the_cmd)
    system(the_cmd)
    cme('The image above was: ')
    cme(my_image,CFILE)
  end
  
  # =============================================================== #
  # === write_image
  # =============================================================== #
  def write_image(which_canvas, location)
    puts "Creating Image at #{location}"
    which_canvas.write(location)
  end
  
  # ========================================================== #
  # === fAKE_RESIZING_UNFISHED
  # hmmm fake temp method
  # ========================================================== #
  def fAKE_RESIZING_UNFISHED
    puts 'You can use the #to_blob method to put the small image in a string.'
    puts 'tmp = tmp.resize(49,49)'
    puts 'small_img = tmp.to_blob'
    puts 'See http://www.simplesystems.org/RMagick/doc/comtasks.html#blob'
  end
  
  # =============================================================== #
  # === view_image
  # =============================================================== #
  def view_image(img)
    img.display()
  end
  
  # =============================================================== #
  # === new_image
  # x = StdDrawing.new_image(40,40,'slateblue')
  # =============================================================== #
  def new_image(width=100, height=100, bg_colour=nil)
    return Image.new(width, height) { 
      self.background_color = bg_colour unless bg_colour.nil? 
    }
  end
  
  # ============================================================ #  
  # This class traverses through HSS['IMG'] and 
  # makes href links.
  # Invocation Example:
  #   StdDrawing::HtmlCollector.new
  # ============================================================ #
  class HtmlCollector
    
    include StdSystem
    include StdColours
    # ====================================================== #
    # === initialize
    # ====================================================== #
    def initialize
      @html_string = '' # contains all html info
      run_it_all
    end
    # ====================================================== #
    # === invert
    # ====================================================== #
    def invert(bool_value)
      bool_value.true? ? false : true
    end
    # ====================================================== #
    # === run_it_all
    # ====================================================== #
    def run_it_all
      # first collect all files
      Dir.chdir(HSS['IMG'])
      #@all_files = Dir[HSS['IMG']+'/**']
      @all_files = Dir['**/*']
      _tmp_array = []
      @all_files.reject! do |my_image|
        #cme("#{my_image} is not an image.")
        invert(File.is_image?(my_image)) 
      end
      # Now we can build our huge string.
      draw_images(@all_files)
      # And write a html file.
      write_html_file
    end
    # ====================================================== #
    # === draw_images
    # Will just store the stuff.
    # ====================================================== #
    def draw_images(array)
      array.each do |my_image|
        @html_string << '<div style="margin-top:2em">'
        @html_string << '<b>' << HSS['IMG'] << '/'<<my_image << "</b><br>\n"
        @html_string << MyHtml.string_link(HSS['IMG']+'/'+my_image,
          HSS['IMG']+'/'+my_image,
          's2em','border-left:3px solid darkred;padding-left:1em','',1
        )
        # Here we specify a fixed width and height.
        @html_string << MyHtml.string_img(HSS['IMG']+'/'+my_image,'s2em',
        'width:30px;height:25px')
        @html_string << "</div>\n"
      end
      @html_string << N
      @html_string << "<br>A total of: #{array.size} images"
      @html_string << N
    end
    # ====================================================== #
    # === write_html_file
    # writes the final html file.
    # ====================================================== #
    def write_html_file
      html_file = '/final.html'
      cme("Writing to #{html_file}",CII)
      MyLogger.write_what_to(@html_string,html_file,'w+')
      cme("Finished writing to #{html_file}",CII)
    end
  end
  # To start the above:
  #
  
  
  extend self 
end
# =============================================================== #
# require 'x/STD/std_drawing.rb';StdDrawing.display_image 'MYSTERIOUS_WOMAN_SMALL.png'
# include StdDrawing; 
# rb $RUBY_STD/std_drawing.rb
#
# StdDrawing::HtmlCollector.new
# =============================================================== #






# =============================================================== #
# MyRmagick Wrapper
# =============================================================== #
class MyRmagick

  include StdDrawing # siehe oben
  include StdColours
  
  attr_my_accessor :image_location
  
  # =========================================================== #
  # === initialize
  # =========================================================== #
  def initialize(image_location = MIXIN_IMAGE)
    @image_location = image_location
    @main_img = attach_image(@image_location)
    @gc = Magick::Draw.new
    set_opacity
    # display_image
    @flag_draw_something = false # no idea what this does.
  end
  
  # =========================================================== #
  # === attach_image
  # attaches a new image to @main_img, and the location to 
  # @image_location
  # =========================================================== #
  def attach_image(this_image)
    @image_location = this_image
    return Magick::ImageList.new(this_image)
  end
  # =========================================================== #
  # === clear_image
  # Set image to nil.
  # =========================================================== #
  def clear_image
    @image_location = ''
    @main_img = Magick::ImageList.new
    @main_img.new_image(420,220,Magick::HatchFill.new('white', 'gray90'))
  end
  
  # =========================================================== #
  # === random_image
  # wrapper to the above module.
  # =========================================================== #
  def random_image
    StdDrawing.random_image
  end
  
  # =============================================================== #
  # === draw_rectangle
  # if make_square is true, we set the height to width, thus
  # making a square.
  # StdDrawing.draw_rectangle
  # =============================================================== #
  def draw_rectangle(
      width=200,
      height=100,
      top_left_pos = 0,
      bottom_right_pos = 0, 
      make_square=false
    )
    height = width if make_square
    gc = Magick::Draw.new
    gc.stroke( StdColours::HtmlColours::HTML_COLOURS.rand) #  colour
    gc.fill( StdColours::HtmlColours::HTML_COLOURS.rand)
    modified_coord_x = top_left_pos + width
    modified_coord_y = bottom_right_pos + width 
    gc.rectangle(top_left_pos, bottom_right_pos,modified_coord_x, modified_coord_y)
    gc.draw(@main_img)
  end
  # =============================================================== #
  # === mini_rectangle
  # Makes a 1px x 1px rectangle. mini tag
  # =============================================================== #
  def mini_rectangle(how_many_times = 50)
    cme('Now drawing small rectangle: ')
    how_many_times.times do 
      draw_rectangle(1+rand(3),1+rand(3),10+rand(155),10+rand(155))
    end
  end
  
  
  # =========================================================== #
  # === set_opacity
  # Comment the body if you dont want opacity.
  # =========================================================== # 
  def set_opacity
    # @gc.opacity(0.2)
  end
  
  # =========================================================== #
  # === invoke_test
  # =========================================================== #
  def invoke_test
    @gc.stroke('darkblue')
    @gc.line(0, 100, 300, 100)
    @gc.line(0, 100, 500, 500)
    @gc.rotate(40)
    @main_img.border!(30, 30, "red")
    @main_img.rotate(44)
  end
  
  # =========================================================== #
  # === random_action
  # Performs a random action. Currently not fully implemented
  # =========================================================== #
  def random_action
    return [multi_line, circle]
  end
  
  # =========================================================== #
  # === multi_line
  # draws several lines. Is quite random.
  # =========================================================== #
  def multi_line
    @flag_draw_something = true
    _ = StdColours::HtmlColours::HTML_COLOURS.rand
    # array, which should be used randomly.
    _array = StdColours.random_weighted_colours 
    @gc.stroke(_array.rand)
    @gc.line(0, 10, 80+rand(400), 180+rand(200))
    (1+rand(24)).times do |tmp|
      _tmp_selection = _array.rand
      @gc.stroke_width(1+rand(2))
      @gc.stroke_opacity(0.1)
      @gc.stroke(_tmp_selection)
      @gc.line(0, 40+rand(240), 100+rand(280), 200+rand(400))
    end
  end
  
  # =========================================================== #
  # === get_image_ratio
  # Gets the ratio of width to height.
  # Usage Example:
  #   MyRmagick.new(MyRmagick.new.random_image).ratio
  # =========================================================== #
  def get_image_ratio(show_debug_info = true)
    cme("Main img is: #{@image_location}") if show_debug_info
    return (@main_img.columns.to_f / @main_img.rows.to_f).to_f
  end
  alias ratio get_image_ratio
  
  # =========================================================== #
  # === get_image_information
  # Info tag
  # =========================================================== #
  def get_image_information
    @main_img = Magick::ImageList.new(DEFAULT_IMAGE_INPUT_LOCATION)
    cme('='*80)
    puts @main_img.format.to_s     + ' Format'
    puts @main_img.columns.to_s    + ' columns'
    puts @main_img.rows.to_s       + ' rows'
    puts @main_img.class_type.to_s + ' Type'
    puts @main_img.depth.to_s      + ' depth'
    puts @main_img.number_colors.to_s  + ' n colours'
    puts @main_img.filesize.to_s   + ' file size'
    puts @main_img.x_resolution.to_i.to_s  + ' x-res'
    puts @main_img.y_resolution.to_i.to_s  + ' y-res'
    puts @main_img.units.to_s      + ' units'
    puts @main_img.properties.to_s  + ' properties'
    cme('='*80)
  end
  
  # =============================================================== #
  # === circle
  # Draws a circle.
  # The first args determine the center.
  # The other two determine a radius point.
  # =============================================================== #
  def circle(x=75,y=75,xx=15,yy=33)
    # @gc.circle(origin_x, origin_y, perim_x, perim_y) -> draw
    # @gc.circle(x,y,xx,yy)
    @circle.stroke('tomato')
    @circle.fill_opacity(0)
    @circle.stroke_opacity(0.75)
    @circle.stroke_width(3)
    @circle.stroke_linecap('round')
    @circle.stroke_linejoin('round')
    @circle.ellipse(@main_img.rows/2,@main_img.columns/2, 80, 80, 0, 315)
    @circle.polyline(180,70, 173,78, 190,78, 191,62)
  end
  
  
  # =========================================================== #
  # === annotate
  # Annotate blabla
  # =========================================================== #
  def annotate
    @gc.annotate(montage, 0,0,0,40, 'Named Colors') {
      self.font_family = 'Helvetica'
      self.fill = 'white'
      self.stroke = 'transparent'
      self.pointsize = 32
      self.font_weight = BoldWeight
      self.gravity = NorthGravity
    }
  end
  
  # =========================================================== #
  # === rpg_filter
  # This is a filter for use in RPG Maps. You can lay it 
  # over another image. (RPG TAG)
  # =========================================================== #
  def rpg_filter
    cme('Now applying RPG Filter via Vignette.')
    @main_img = Magick::ImageList.new(DEFAULT_IMAGE_INPUT_LOCATION)
    max_value = find_image_center(@main_img)
    _ = (max_value[0]-20+rand(10))
    puts "MAX VALUE IS #{max_value[0]} und #{max_value[1]}"
    # MAX VALUE IS 336 und 340
    @main_img = @main_img.vignette(_, _, 20, 10)
    # rdraw
  end

  
  # =============================================================== #
  # === default_choice
  # A general wrapper.
  # This method will invoke things which are possible.
  # =============================================================== #
  def default_choice
    # get_image_information
    # multi_line
    # rpg_filter
    mini_rectangle
  end  
  
  # =============================================================== #
  # === connect
  # Mandatory write method. A bottleneck method. IF it is invoked,
  # then it will create our final image.
  # Invokes .draw method as well
  # =============================================================== #
  def connect
    @gc.draw(@main_img) if @flag_draw_something
    # @main_canvas.write('/SQUARE.png')
    cme("Creating Image at: #{DEFAULT_IMAGE_OUTPUT_LOCATION}")
    write_image(@main_img, DEFAULT_IMAGE_OUTPUT_LOCATION)
  end
end


if __FILE__ == $PROGRAM_NAME 
  # MyRmagick.new.test
  # MyRmagick.new.circle
  x=MyRmagick.new
  x.clear_image
  #x.default_choice
  x.mini_rectangle
  # x=MyRmagick.new
  # x=MyRmagick.new(true)
  # x.multi_line
  # x.default_choice
  x.connect # very important!
  # x.jp2a
  # puts x.random_image
  #object_traverse_html = StdDrawing::HtmlCollector.new
end
# rdrawing