Class: Gluttonberg::Library::QuickMagick::Image

Inherits:
Object
  • Object
show all
Includes:
Draw, OperatorsAndSettings, Serialization
Defined in:
lib/gluttonberg/library/quick_magick/image.rb

Constant Summary

Constants included from OperatorsAndSettings

OperatorsAndSettings::IMAGE_OPERATORS_METHODS, OperatorsAndSettings::IMAGE_SETTINGS_METHODS, OperatorsAndSettings::SPECIAL_COMMANDS, OperatorsAndSettings::WITH_EQUAL_METHODS, OperatorsAndSettings::WITH_GEOMETRY_METHODS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Serialization

#save, #save!, #to_blob

Methods included from Draw

#draw_arc, #draw_bezier, #draw_circle, #draw_ellipse, #draw_image, #draw_line, #draw_path, #draw_point, #draw_polygon, #draw_polyline, #draw_rectangle, #draw_round_rectangle, #draw_text

Constructor Details

#initialize(filename, index = 0, info_line = nil, pseudo_image = false) ⇒ Image

constructor



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/gluttonberg/library/quick_magick/image.rb', line 102

def initialize(filename, index=0, info_line=nil, pseudo_image=false)
  @image_filename = filename
  @index = index
  @pseudo_image = pseudo_image
  if info_line
    @image_infoline = info_line.split
    process_info_line
    #@image_infoline[0..1] = @image_infoline[0..1].join(' ') while @image_infoline.size > 1 && !@image_infoline[0].start_with?(image_filename)
  end
  @arguments = ""
end

Instance Attribute Details

#image_filenameObject (readonly) Also known as: original_filename

define attribute readers (getters)



98
99
100
# File 'lib/gluttonberg/library/quick_magick/image.rb', line 98

def image_filename
  @image_filename
end

Class Method Details

.gradient(width, height, type = QuickMagick::LinearGradient, color1 = nil, color2 = nil) {|i| ... } ⇒ Object

Creates a new image initially set to gradient Default gradient is linear gradient from black to white

Yields:

  • (i)


17
18
19
20
21
22
23
24
25
# File 'lib/gluttonberg/library/quick_magick/image.rb', line 17

def self.gradient(width, height, type=QuickMagick::LinearGradient, color1=nil, color2=nil)
  template_name = type + ":"
  template_name << color1.to_s if color1
  template_name << '-' << color2.to_s if color2
  i = self.new(template_name, 0, nil, true)
  i.size = QuickMagick::geometry(width, height)
  yield(i) if block_given?
  i
end

.identify(filename) ⇒ Object

returns info for an image using identify command



48
49
50
# File 'lib/gluttonberg/library/quick_magick/image.rb', line 48

def self.identify(filename)
  QuickMagick.exec3 "identify #{QuickMagick.c filename}"
end

.pattern(width, height, pattern) {|i| ... } ⇒ Object

Creates an image from pattern

Yields:

  • (i)

Raises:



38
39
40
41
42
43
44
45
# File 'lib/gluttonberg/library/quick_magick/image.rb', line 38

def self.pattern(width, height, pattern)
  raise QuickMagick::QuickMagickError, "Invalid pattern '#{pattern.to_s}'" unless QuickMagick::Patterns.include?(pattern.to_s)
  template_name = "pattern:#{pattern.to_s}"
  i = self.new(template_name, 0, nil, true)
  i.size = QuickMagick::geometry(width, height)
  yield(i) if block_given?
  i
end

.solid(width, height, color = nil) {|i| ... } ⇒ Object

Creates an image with solid color

Yields:

  • (i)


28
29
30
31
32
33
34
35
# File 'lib/gluttonberg/library/quick_magick/image.rb', line 28

def self.solid(width, height, color=nil)
  template_name = QuickMagick::SolidColor+":"
  template_name << color.to_s if color
  i = self.new(template_name, 0, nil, true)
  i.size = QuickMagick::geometry(width, height)
  yield(i) if block_given?
  i
end

Instance Method Details

#animateObject

displays the current image as animated image



208
209
210
# File 'lib/gluttonberg/library/quick_magick/image.rb', line 208

def animate
  `animate #{command_line}`
end

#antialias=(flag) ⇒ Object

Enables/Disables flood fill. Pass a boolean argument.



93
94
95
# File 'lib/gluttonberg/library/quick_magick/image.rb', line 93

def antialias=(flag)
  append_basic flag ? '-antialias' : '+antialias'
end

#append_basic(arg) ⇒ Object

append the given string as is. Used to append special arguments like antialias or debug



64
65
66
# File 'lib/gluttonberg/library/quick_magick/image.rb', line 64

def append_basic(arg)
  @arguments << arg << ' '
end

#append_to_operators(arg, value = nil) ⇒ Object

append the given option, value pair to the args for the current image



69
70
71
72
73
74
75
76
77
78
# File 'lib/gluttonberg/library/quick_magick/image.rb', line 69

def append_to_operators(arg, value=nil)
  is_draw = (arg == 'draw')
  if @last_is_draw && is_draw
    @arguments.insert(@arguments.rindex('"'), " #{value}")
  else
    @arguments << %Q<-#{arg} #{QuickMagick.c value} >
  end
  @last_is_draw = is_draw
  self
end

#append_to_settings(arg, value = nil) ⇒ Object

append the given option, value pair to the settings of the current image



57
58
59
60
61
# File 'lib/gluttonberg/library/quick_magick/image.rb', line 57

def append_to_settings(arg, value=nil)
  @arguments << "-#{arg} #{QuickMagick.c value} "
  @last_is_draw = false
  self
end

#argumentsObject



161
162
163
# File 'lib/gluttonberg/library/quick_magick/image.rb', line 161

def arguments
  @arguments
end

#bit_depthObject

Bit depth



184
185
186
# File 'lib/gluttonberg/library/quick_magick/image.rb', line 184

def bit_depth
  image_infoline[4].to_i
end

#colorsObject

Number of different colors used in this image



189
190
191
# File 'lib/gluttonberg/library/quick_magick/image.rb', line 189

def colors
  image_infoline[6].to_i
end

#columnsObject Also known as: width

columns of image in pixels



170
171
172
# File 'lib/gluttonberg/library/quick_magick/image.rb', line 170

def columns
  image_infoline[2].split('x').first.to_i
end

#command_lineObject

The command line so far that will be used to convert or save the image



115
116
117
# File 'lib/gluttonberg/library/quick_magick/image.rb', line 115

def command_line
  %Q< "(" #{@arguments} #{QuickMagick.c(image_filename + (@pseudo_image ? "" : "[#{@index}]"))} ")" >
end

#displayObject

displays the current image to the x-windowing system



213
214
215
# File 'lib/gluttonberg/library/quick_magick/image.rb', line 213

def display
  `display #{command_line}`
end

#floodfill(width, height = nil, x = nil, y = nil, flag = nil, color = nil) ⇒ Object

Fills a rectangle with a solid color



88
89
90
# File 'lib/gluttonberg/library/quick_magick/image.rb', line 88

def floodfill(width, height=nil, x=nil, y=nil, flag=nil, color=nil)
  append_to_operators "floodfill", QuickMagick::geometry(width, height, x, y, flag), color
end

#formatObject

image file format



165
166
167
# File 'lib/gluttonberg/library/quick_magick/image.rb', line 165

def format
  image_infoline[1]
end

#get_pixel(x, y) ⇒ Object

Reads a pixel from the image. WARNING: This is done through command line which is very slow. It is not recommended at all to use this method for image processing for example.



201
202
203
204
205
# File 'lib/gluttonberg/library/quick_magick/image.rb', line 201

def get_pixel(x, y)
  result = QuickMagick.exec3("identify -verbose -crop #{QuickMagick::geometry(1,1,x,y)} #{QuickMagick.c image_filename}[#{@index}]")
  result =~ /Histogram:\s*\d+:\s*\(\s*(\d+),\s*(\d+),\s*(\d+)\)/
  return [$1.to_i, $2.to_i, $3.to_i]
end

#image_infolineObject

An information line about the image obtained using ‘identify’ command line



120
121
122
123
124
125
126
127
128
# File 'lib/gluttonberg/library/quick_magick/image.rb', line 120

def image_infoline
  return nil if @pseudo_image
  unless @image_infoline
    @image_infoline = QuickMagick::Image::identify(command_line).split
    process_info_line
    #@image_infoline[0..1] = @image_infoline[0..1].join(' ') while @image_infoline.size > 1 && !@image_infoline[0].start_with?(image_filename)
  end
  @image_infoline
end

#options_to_str(options) ⇒ Object

converts options passed to any primitive to a string that can be passed to ImageMagick options allowed are:

  • rotate degrees

  • translate dx,dy

  • scale sx,sy

  • skewX degrees

  • skewY degrees

  • gravity NorthWest, North, NorthEast, West, Center, East, SouthWest, South, or SouthEast

  • stroke color

  • fill color

The rotate primitive rotates subsequent shape primitives and text primitives about the origin of the main image. If you set the region before the draw command, the origin for transformations is the upper left corner of the region. The translate primitive translates subsequent shape and text primitives. The scale primitive scales them. The skewX and skewY primitives skew them with respect to the origin of the main image or the region. The text gravity primitive only affects the placement of text and does not interact with the other primitives. It is equivalent to using the gravity method, except that it is limited in scope to the draw_text option in which it appears.



147
148
149
# File 'lib/gluttonberg/library/quick_magick/image.rb', line 147

def options_to_str(options)
  options.to_a.flatten.join " "
end

#points_to_str(points) ⇒ Object

Converts an array of coordinates to a string that can be passed to polygon, polyline and bezier



152
153
154
155
156
157
158
159
# File 'lib/gluttonberg/library/quick_magick/image.rb', line 152

def points_to_str(points)
  raise QuickMagick::QuickMagickError, "Points must be an even number of coordinates" if points.size.odd?
  points_str = ""
  points.each_slice(2) do |point|
    points_str << point.join(",") << " "
  end
  points_str
end

#revert!Object

Reverts this image to its last saved state. Note that you cannot revert an image created from scratch.



82
83
84
85
# File 'lib/gluttonberg/library/quick_magick/image.rb', line 82

def revert!
  raise QuickMagick::QuickMagickError, "Cannot revert a pseudo image" if @pseudo_image
  @arguments = ""
end

#rowsObject Also known as: height

rows of image in pixels



177
178
179
# File 'lib/gluttonberg/library/quick_magick/image.rb', line 177

def rows
  image_infoline[2].split('x').last.to_i
end

#sizeObject

returns size of image in bytes



194
195
196
# File 'lib/gluttonberg/library/quick_magick/image.rb', line 194

def size
  File.size?(image_filename)
end