Class: Magick::BinMagick::Image

Inherits:
MagickWrapper show all
Defined in:
lib/rmagick/bin_magick/image_attr_methods.rb,
lib/rmagick/bin_magick/image_inst_methods.rb,
lib/rmagick/bin_magick/image_class_methods.rb

Overview

Image class methods.

Instance Attribute Summary

Attributes inherited from MagickWrapper

#image

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from MagickWrapper

#initialize, #method_missing, #respond_to_missing?

Constructor Details

This class inherits a constructor from Magick::BinMagick::MagickWrapper

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Magick::BinMagick::MagickWrapper

Class Method Details

.from_file(filename) ⇒ BinMagick::Image

Read image from a file.

Parameters:

  • filename (String)

Returns:

Raises:



18
19
20
21
22
23
# File 'lib/rmagick/bin_magick/image_class_methods.rb', line 18

def self.from_file(filename)
  image = Magick::Image.read(filename).first
  new(image)
rescue Magick::ImageMagickError => e
  raise BinMagick::IOError, "Error occurred while reading image from file: #{e.message}"
end

Instance Method Details

#binary?Boolean

Check if the image is a binary image.

Returns:

  • (Boolean)


16
17
18
19
20
21
# File 'lib/rmagick/bin_magick/image_inst_methods.rb', line 16

def binary?
  accepted_colors = %w[black white]
  colors = quantize.color_histogram.transform_keys(&:to_color)

  (colors.keys - accepted_colors).empty?
end

#black_px?Boolean

Check if the image has at least one black pixel.

Returns:

  • (Boolean)


28
29
30
31
# File 'lib/rmagick/bin_magick/image_inst_methods.rb', line 28

def black_px?
  colors = color_histogram.transform_keys(&:to_color)
  colors.key?("black")
end

#crop_borderBinMagick::Image

Crop border around the image or return an unedited copy if the image can’t be cropped.

Returns:



39
40
41
# File 'lib/rmagick/bin_magick/image_inst_methods.rb', line 39

def crop_border
  _crop_border(self)
end

#crop_border!Object

A bang version of the ‘BinMagick::Image#crop_border’.



46
47
48
49
50
51
# File 'lib/rmagick/bin_magick/image_inst_methods.rb', line 46

def crop_border!
  cropped_img = crop_border
  _replace_pixels(cropped_img)

  self
end

#crop_border_clrBinMagick::Image

Crop border around the image, but treat a color image like it is already a binary one.

See also: ‘BinMagick::Image#to_binary’ method description.

Returns:



61
62
63
64
# File 'lib/rmagick/bin_magick/image_inst_methods.rb', line 61

def crop_border_clr(...)
  bin_img = to_binary(...)
  _crop_border(bin_img)
end

#crop_border_clr!Object

A bang version of the ‘BinMagick::Image#crop_border_clr’.



69
70
71
72
73
74
# File 'lib/rmagick/bin_magick/image_inst_methods.rb', line 69

def crop_border_clr!(...)
  cropped_img = crop_border_clr(...)
  _replace_pixels(cropped_img)

  self
end

#displayObject

Workaround for the ‘Magick::Image#display’ method.



79
80
81
82
83
84
85
# File 'lib/rmagick/bin_magick/image_inst_methods.rb', line 79

def display
  pixels = @image.dispatch(0, 0, columns, rows, "RGB")
  img = Magick::Image.constitute(columns, rows, "RGB", pixels)
  img.display

  self
end

#extent!(width, height, x = 0, y = 0) ⇒ Object

A bang version of the ‘Magick::Image#extent’.

Parameters:

  • width (Integer)
  • height (Integer)
  • [Integer] (Hash)

    a customizable set of options



98
99
100
101
102
103
# File 'lib/rmagick/bin_magick/image_inst_methods.rb', line 98

def extent!(width, height, x = 0, y = 0)
  extended_img = @image.extent(width, height, x, y)
  _replace_pixels(extended_img)

  self
end

#fit_to_size(max_width, max_height) ⇒ BinMagick::Image

Scale the image to fit into the size limits IF it oversize them.

Parameters:

  • max_width (Integer)
  • max_height (Integer)

Returns:



115
116
117
# File 'lib/rmagick/bin_magick/image_inst_methods.rb', line 115

def fit_to_size(max_width, max_height)
  oversize?(max_width, max_height) ? resize_to_fit(max_width, max_height) : copy
end

#fit_to_size!Object

A bang version of the ‘BinMagick::Image#fit_to_size’.



122
123
124
125
126
127
# File 'lib/rmagick/bin_magick/image_inst_methods.rb', line 122

def fit_to_size!(...)
  new_img = fit_to_size(...)
  _replace_pixels(new_img)

  self
end

#heightInteger

Return image height.

Returns:

  • (Integer)


16
17
18
# File 'lib/rmagick/bin_magick/image_attr_methods.rb', line 16

def height
  rows
end

#level!(black_point = 0.0, white_point = Magick::QuantumRange, gamma = 1.0) ⇒ Object

A bang version of the ‘Magick::Image#level2’.

Note: The ‘Magick::Image#level2’ method is exactly the same as ‘Magick::Image#level’ except that it never swaps the arguments.



135
136
137
138
139
140
# File 'lib/rmagick/bin_magick/image_inst_methods.rb', line 135

def level!(black_point = 0.0, white_point = Magick::QuantumRange, gamma = 1.0)
  new_img = @image.level2(black_point, white_point, gamma)
  _replace_pixels(new_img)

  self
end

#ordered_dither!(threshold_map = "checks") ⇒ Object

A bang version of the ‘Magick::Image#ordered_dither’.



145
146
147
148
149
150
# File 'lib/rmagick/bin_magick/image_inst_methods.rb', line 145

def ordered_dither!(threshold_map = "checks")
  new_img = @image.ordered_dither(threshold_map)
  _replace_pixels(new_img)

  self
end

#oversize?(max_width, max_height) ⇒ Boolean

Check if the image dimensions are larger than the provided height OR width.

Returns:

  • (Boolean)


157
158
159
# File 'lib/rmagick/bin_magick/image_inst_methods.rb', line 157

def oversize?(max_width, max_height)
  columns > max_width || rows > max_height
end

#quantize!(number_colors = N_GRAY_COLORS, colorspace = Magick::GRAYColorspace, dither = Magick::NoDitherMethod, tree_depth = 0, measure_error = false) ⇒ Object

A bang version of the ‘Magick::Image#quantize’.



164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/rmagick/bin_magick/image_inst_methods.rb', line 164

def quantize!(
  number_colors = N_GRAY_COLORS,
  colorspace = Magick::GRAYColorspace,
  dither = Magick::NoDitherMethod,
  tree_depth = 0,
  measure_error = false
)
  new_img = @image.quantize(number_colors, colorspace, dither, tree_depth, measure_error)
  _replace_pixels(new_img)

  self
end

#to_binary(n_gray_colors = N_GRAY_COLORS, quantize_dither = Magick::NoDitherMethod, threshold_map = "checks") ⇒ BinMagick::Image

Convert a color image to a binary image.

Parameters:

  • [Integer] (Hash)

    a customizable set of options

  • [Magick::DitherMethod] (Hash)

    a customizable set of options

  • [String] (Hash)

    a customizable set of options

Returns:



192
193
194
195
# File 'lib/rmagick/bin_magick/image_inst_methods.rb', line 192

def to_binary(n_gray_colors = N_GRAY_COLORS, quantize_dither = Magick::NoDitherMethod, threshold_map = "checks")
  grayscale_img = quantize(n_gray_colors, Magick::GRAYColorspace, quantize_dither)
  grayscale_img.ordered_dither(threshold_map)
end

#to_binary!Object

A bang version of the ‘BinMagick::Image#to_binary’.



200
201
202
203
204
205
# File 'lib/rmagick/bin_magick/image_inst_methods.rb', line 200

def to_binary!(...)
  bin_img = to_binary(...)
  _replace_pixels(bin_img)

  self
end

#widthInteger

Return image width.

Returns:

  • (Integer)


25
26
27
# File 'lib/rmagick/bin_magick/image_attr_methods.rb', line 25

def width
  columns
end