rmagick-bin_magick 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 29ff31c416b63fd2836b37082fccf0dea82da5808ff9e2bf98a84cbbc3872ca3
4
+ data.tar.gz: ec4a093d837270c8c1cd3984d3b1049e8e3a81e7cd4e14b1ab2863fc40e9b80c
5
+ SHA512:
6
+ metadata.gz: 273dfe1d97525e26d06854104758b011101dbc6e52218a9321f8e5312d892a898814668ffd440e3a235c0ee002e346a45e231d04c630938d09f46f0ab8b48cc9
7
+ data.tar.gz: d7bdd7e46ef7a4f509e2c4a68ea209285ef049c09b4c87d550fc07208b7cdc0ab919d933473912eb0e9c194adf7caacedf89264ee544bcba5edceb0a3ef36056
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.0
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,11 @@
1
+ ### 0.1.2 / 2023-04-01
2
+
3
+ - Fixed the *display* method.
4
+
5
+ ### 0.1.1 / 2023-04-01
6
+
7
+ - Fixed a bug in the *fit_to_size* and *fit_to_size!* methods.
8
+
9
+ ### 0.1.0 / 2023-04-01
10
+
11
+ - Initial version.
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in rmagick-bin_magick.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "minitest", "~> 5.0"
11
+
12
+ gem "rubocop", "~> 0.80"
data/Gemfile.lock ADDED
@@ -0,0 +1,46 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rmagick-bin_magick (0.1.0)
5
+ rmagick (~> 5.2, >= 5.2.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ast (2.4.2)
11
+ minitest (5.16.3)
12
+ parallel (1.22.1)
13
+ parser (3.1.3.0)
14
+ ast (~> 2.4.1)
15
+ pkg-config (1.5.1)
16
+ rainbow (3.1.1)
17
+ rake (13.0.6)
18
+ regexp_parser (2.6.1)
19
+ rexml (3.2.5)
20
+ rmagick (5.2.0)
21
+ pkg-config (~> 1.4)
22
+ rubocop (0.93.1)
23
+ parallel (~> 1.10)
24
+ parser (>= 2.7.1.5)
25
+ rainbow (>= 2.2.2, < 4.0)
26
+ regexp_parser (>= 1.8)
27
+ rexml
28
+ rubocop-ast (>= 0.6.0)
29
+ ruby-progressbar (~> 1.7)
30
+ unicode-display_width (>= 1.4.0, < 2.0)
31
+ rubocop-ast (1.24.0)
32
+ parser (>= 3.1.1.0)
33
+ ruby-progressbar (1.11.0)
34
+ unicode-display_width (1.8.0)
35
+
36
+ PLATFORMS
37
+ x86_64-linux
38
+
39
+ DEPENDENCIES
40
+ minitest (~> 5.0)
41
+ rake (~> 13.0)
42
+ rmagick-bin_magick!
43
+ rubocop (~> 0.80)
44
+
45
+ BUNDLED WITH
46
+ 2.2.3
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Mate
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,469 @@
1
+ # rmagick/bin_magick
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/rmagick-bin_magick.svg)](https://badge.fury.io/rb/rmagick-bin_magick)
4
+
5
+ ## Description
6
+
7
+ **rmagick/bin_magick** is an extension of the popular **[rmagick](https://github.com/rmagick/rmagick)** image processing gem. The extension provides custom methods for easy conversion of color images to binary (monochrome) format. It also introduces in-place versions of some existing *Magick::Image* methods that, by default, lack an in-place version
8
+
9
+ The *Image* class of bin_magick inherits from the *Magick::Image* class, which allows both *Magick::Image* and *Magick::BinMagick::Image* instance methods to be called on a *BinMagick::Image* object.
10
+
11
+ bin_magick was developed for the [img_to_MK90_bas](https://github.com/8bit-mate/img_to_MK90_bas.rb) project, which converts images to [Elektronika MK90](https://museum.dataart.com/en/artifacts/portativnaya-evm-elektronika-mk-90) executable BASIC scripts, but it may also be useful for similar applications.
12
+
13
+ ## Prerequisites
14
+
15
+ - Ruby ver. >= 3.0.0;
16
+ - RMagick ver. >= 5.2.0.
17
+
18
+ ## Installation
19
+
20
+ Add this line to your application's Gemfile:
21
+
22
+ ```ruby
23
+ gem 'rmagick-bin_magick'
24
+ ```
25
+
26
+ And then execute:
27
+
28
+ $ bundle install
29
+
30
+ Or install it yourself as:
31
+
32
+ $ gem install bin_magick
33
+
34
+ ## Usage
35
+
36
+ 1. Add **rmagick-bin_magick** to the list of required modules:
37
+
38
+ ```ruby
39
+ require "rmagick/bin_magick"
40
+ ```
41
+
42
+ 2. Read an image from a file:
43
+
44
+ ```ruby
45
+ image = Magick::BinMagick::Image.from_file("filename.png")
46
+ ```
47
+
48
+ Or initialize an instance based on the existing Magick::Image instance:
49
+
50
+ ```ruby
51
+ image = Magick::BinMagick::Image.new(magick_image_object)
52
+ ```
53
+
54
+ 3. Call Magick::BinMagick::Image or Magick::Image attribute and instance methods (see the examples section and the reference section for more details).
55
+
56
+ ### Examples
57
+
58
+ #### Method chaining ####
59
+
60
+ This example shows how different *Magick::BinMagick::Image* and *Magick::Image* instance methods may be combined in one method call chain:
61
+
62
+ ```ruby
63
+ require "rmagick/bin_magick"
64
+
65
+ # Read an image from a file:
66
+ begin
67
+ image = Magick::BinMagick::Image.from_file("./data/test_0.png")
68
+ rescue Magick::BinMagick::BinMagickError => e
69
+ puts e.message
70
+ exit
71
+ end
72
+
73
+ image
74
+ .level(0, Magick::QuantumRange * 0.95, 0.4) # Adjust levels (a RMagick method).
75
+ .crop_border_clr # Crop white border (a BinMagick method).
76
+ .fit_to_size!(192, 192) # Resize to fit a 192 x 192 square (a BinMagick method).
77
+ .to_binary(64, Magick::NoDitherMethod, "o2x2") # Convert to binary (a BinMagick method).
78
+ .write("./data/test_0_result.png") # Save result to a file (a RMagick method).
79
+ ```
80
+
81
+ The code above will produce the following result:
82
+
83
+ | Original image | Result image |
84
+ | --- | --- |
85
+ | ![Alt text](data/test_0.png?raw=true "Original image") | ![Alt text](data/test_0_result.png?raw=true "Result image") |
86
+
87
+ #### In-place methods usage ####
88
+
89
+ You can also use in-place methods:
90
+
91
+ ```ruby
92
+ require "rmagick/bin_magick"
93
+
94
+ # Read an image from the file './data/test_1.png'
95
+ # ...
96
+
97
+ # Adjust levels (a BinMagick method):
98
+ image.level!(0, Magick::QuantumRange, 0.5)
99
+
100
+ # Crop white border (a BinMagick method):
101
+ image.crop_border_clr!
102
+
103
+ # Resize to fit a 64 x 64 square (a BinMagick method):
104
+ image.fit_to_size!(64, 64)
105
+
106
+ # Convert to binary (a BinMagick method):
107
+ image.to_binary!(16)
108
+
109
+ # Extent to a 120 x 64 rectangle, put image in the center (a BinMagick method):
110
+ x_offset = - (120 / 2 - image.width / 2)
111
+ y_offset = - (64 - image.height)
112
+ image.extent!(120, 64, x_offset, y_offset)
113
+
114
+ # Save result to a file (a RMagick method):
115
+ image.write("./data/test_1_result.png")
116
+ ```
117
+
118
+ The code above will produce the following result:
119
+
120
+ | Original image | Result image |
121
+ | --- | --- |
122
+ | ![Alt text](data/test_1.png?raw=true "Original image") | ![Alt text](data/test_1_result.png?raw=true "Result image") |
123
+
124
+ ## Reference
125
+
126
+ ### Class Magick::BinMagick::Image: attribute methods (get only)
127
+
128
+ - **height**
129
+
130
+ *img*.height -> *integer*
131
+
132
+ The height of the image in pixels. An alias of the Magick::Image#[rows](https://rmagick.github.io/imageattrs.html#rows).
133
+
134
+ ***
135
+
136
+ - **width**
137
+
138
+ *img*.width -> *integer*
139
+
140
+ The width of the image in pixels. An alias of the Magick::Image#[columns](https://rmagick.github.io/imageattrs.html#columns).
141
+
142
+ ***
143
+
144
+ ### Class Magick::BinMagick::Image: class methods
145
+
146
+ - **from_file**
147
+
148
+ Magick::BinMagick::Image.from_file(*filename*) -> *image*
149
+
150
+ Reads the first image from the file *filename*.
151
+
152
+ **Note**: an image file can contain multiple images or multiple image layers, but the method always returns the first one. To load a specific image from a file (e.g. a specific frame from an animated GIF), you can use the Magick::Image#read method, and then initialize a BinMagick::Image instance with the BinMagick::Image#new method.
153
+
154
+ **Arguments**:
155
+
156
+ - filename
157
+
158
+ An image file name.
159
+
160
+ **Returns**: a new image.
161
+
162
+ ***
163
+
164
+ - **new**
165
+
166
+ Magick::BinMagick::Image.new(*magick_image*) -> *image*
167
+
168
+ Creates a new Magick::BinMagick::Image instance from a Magick::Image object. The source Magick::Image object should not be a destroyed image, otherwise the Magick::BinMagickError::DestroyedImageError exception is raised.
169
+
170
+ **Arguments**:
171
+
172
+ - magick_image
173
+
174
+ A Magick::Image object.
175
+
176
+ **Returns**: a new image.
177
+
178
+ **Raises**: Magick::BinMagickError::DestroyedImageError.
179
+
180
+ ***
181
+
182
+ ### Class Magick::BinMagick::Image: instance methods
183
+
184
+ - **black_px?**
185
+
186
+ *img*.black_px? -> *boolean*
187
+
188
+ Checks if the image has at least one black pixel.
189
+
190
+ **Returns**: a boolean value.
191
+
192
+ ***
193
+
194
+ - **crop_border**
195
+
196
+ *img*.crop_border -> *image*
197
+
198
+ Crop border around the image. If the image is blank (has all pixels of white color): returns an unedited copy of the target image.
199
+
200
+ Methods calculates a bounding box value for the target image (see Magick::Image#[bounding_box](https://rmagick.github.io/image1.html#bounding_box)), and uses that value to crop a copy of the target image.
201
+
202
+ Works best with binary images. For color images you might want to use the *crop_border_clr* version of the method.
203
+
204
+ **Returns**: a new image.
205
+
206
+ ***
207
+
208
+ - **crop_border!**
209
+
210
+ *img*.crop_border! -> *self*
211
+
212
+ In-place form of **crop_border**.
213
+
214
+ **Returns**: self.
215
+
216
+ ***
217
+
218
+ - **crop_border_clr**
219
+
220
+ *img*.crop_border_clr(n_gray_colors = 64, quantize_dither = Magick::NoDitherMethod, threshold_map = "checks") -> *image*
221
+
222
+ Same as *crop_border*, but treats a color image like it is already a binary one. The bounding box value is being calculated for a binary version of the target image, then a copy of the target image is being cropped used that value.
223
+
224
+ Method **does not** convert the target image to a binary, but uses a binary version only to calculate the bounding box value. For that purpose it accepts and utilizes all *to_binary* arguments.
225
+
226
+ **Arguments**: see *to_binary* arguments description.
227
+
228
+ **Returns**: a new image.
229
+
230
+ ***
231
+
232
+ - **crop_border_clr!**
233
+
234
+ *img*.crop_border_clr!(n_gray_colors = 64, quantize_dither = Magick::NoDitherMethod, threshold_map = "checks") -> *self*
235
+
236
+ In-place form of **crop_border_clr**.
237
+
238
+ **Returns**: self.
239
+
240
+ ***
241
+
242
+ - **extent!**
243
+
244
+ *img*.extent!(width, height, x = 0, y = 0) -> *self*
245
+
246
+ In-place form of Magick::Image#[extent](https://rmagick.github.io/image2.html#extent).
247
+
248
+ If width or height is greater than the target image's width or height, extends the width and height of the target image to the specified values. The new pixels are set to the background color. If width or height is less than the target image's width or height, crops the target image.
249
+
250
+ **Arguments**:
251
+
252
+ - width
253
+
254
+ The width of the new image.
255
+
256
+ - height
257
+
258
+ The height of the new image.
259
+
260
+ - x, y
261
+
262
+ The upper-left corner of the new image is positioned at -x, -y.
263
+
264
+ **Returns**: self.
265
+
266
+ ***
267
+
268
+ - **fit_to_size**
269
+
270
+ *img*.fit_to_size(max_width, max_height) -> *image*
271
+
272
+ Scales down the image to fit within the specified dimensions while retaining the original aspect ratio, but only if the image is larger than provided *max_width*, *max_height* dimensions.
273
+
274
+ **Arguments**:
275
+
276
+ - max_width, max_height
277
+
278
+ Maximum allowed image dimension.
279
+
280
+ **Returns**: a new image.
281
+
282
+ ***
283
+
284
+ - **fit_to_size!**
285
+
286
+ *img*.fit_to_size(max_width, max_height) -> *self*
287
+
288
+ In-place form of **fit_to_size**.
289
+
290
+ **Returns**: self.
291
+
292
+ ***
293
+
294
+ - **level!**
295
+
296
+ *img*.level!(black_point = 0.0, white_point = Magick::QuantumRange, gamma = 1.0) -> *self*
297
+
298
+ In-place form of Magick::Image#[level2](https://rmagick.github.io/image2.html#level).
299
+
300
+ Adjusts the levels of an image by scaling the colors falling between specified white and black points to the full available quantum range.
301
+
302
+ **Arguments**:
303
+
304
+ - black_point
305
+
306
+ A black point level in the range 0..QuantumRange. The default is 0.0.
307
+
308
+ - white_point
309
+
310
+ A white point level in the range 0..QuantumRange. The default is QuantumRange.
311
+
312
+ - gamma
313
+
314
+ A gamma correction in the range 0.0..10.0 The default is 1.0.
315
+
316
+ **Returns**: self.
317
+
318
+ ***
319
+
320
+ - **ordered_dither!**
321
+
322
+ *img*.ordered_dither!(threshold_map = "checks") -> *self*
323
+
324
+ In-place form of Magick::Image#[ordered_dither](https://rmagick.github.io/image2.html#ordered_dither).
325
+
326
+ **Note**: the default value of the *threshold_map* option is "checks" rather than "2x2" used in the Magick::Image#ordered_dither method.
327
+
328
+ **Arguments**:
329
+
330
+ - threshold_map
331
+
332
+ A dither pattern to use. Available options depend on the used ImageMagick implementation. To print a complete list of the thresholds that have been defined, run the ImageMagick with the `-list threshold` option.
333
+
334
+ Some common options (from the ImageMagick [documentation](https://imagemagick.org/script/command-line-options.php?#ordered-dither)):
335
+ ```
336
+ threshold 1x1 Threshold 1x1 (non-dither)
337
+ checks 2x1 Checkerboard 2x1 (dither)
338
+ o2x2 2x2 Ordered 2x2 (dispersed)
339
+ o3x3 3x3 Ordered 3x3 (dispersed)
340
+ o4x4 4x4 Ordered 4x4 (dispersed)
341
+ o8x8 8x8 Ordered 8x8 (dispersed)
342
+ h4x4a 4x1 Halftone 4x4 (angled)
343
+ h6x6a 6x1 Halftone 6x6 (angled)
344
+ h8x8a 8x1 Halftone 8x8 (angled)
345
+ h4x4o Halftone 4x4 (orthogonal)
346
+ h6x6o Halftone 6x6 (orthogonal)
347
+ h8x8o Halftone 8x8 (orthogonal)
348
+ h16x16o Halftone 16x16 (orthogonal)
349
+ c5x5b c5x5 Circles 5x5 (black)
350
+ c5x5w Circles 5x5 (white)
351
+ c6x6b c6x6 Circles 6x6 (black)
352
+ c6x6w Circles 6x6 (white)
353
+ c7x7b c7x7 Circles 7x7 (black)
354
+ c7x7w Circles 7x7 (white)
355
+ ```
356
+
357
+ **Returns**: self.
358
+
359
+ ***
360
+
361
+ - **oversize?**
362
+
363
+ *img*.oversize?(max_width, max_height) -> *boolean*
364
+
365
+ Checks if the target image dimensions exceed provided height **or** width.
366
+
367
+ **Returns**: a boolean value.
368
+
369
+ ***
370
+
371
+ - **quantize!**
372
+
373
+ *img*.quantize!(number_colors = 64, colorspace = Magick::GRAYColorspace, dither = Magick::NoDitherMethod, tree_depth = 0, measure_error = false) -> *self*
374
+
375
+ In-place form of Magick::Image#[quantize](https://rmagick.github.io/image3.html#quantize).
376
+
377
+ **Notes**:
378
+
379
+ - the default value of the *number_colors* option is 64 rather than 256 used in the Magick::Image#quantize method;
380
+
381
+ - the default value of the *colorspace* option is Magick::GRAYColorspace rather than Magick::RGBColorspace used in the Magick::Image#quantize method;
382
+
383
+ - the default value of the *dither* option is Magick::NoDitherMethod rather than Magick::RiemersmaDitherMethod used in the Magick::Image#quantize method;
384
+
385
+ **Arguments**:
386
+
387
+ - number_colors
388
+
389
+ The maximum number of colors in the result image.
390
+
391
+ - colorspace
392
+
393
+ The colorspace to quantize in.
394
+
395
+ - dither
396
+
397
+ Error diffusion dither that will be applied on the color reduction. A Magick::DitherMethod value.
398
+
399
+ - tree_depth
400
+
401
+ The tree depth to use while quantizing
402
+
403
+ - measure_error
404
+
405
+ Set to true to calculate quantization errors when quantizing the image.
406
+
407
+ **Returns**: self.
408
+
409
+ ***
410
+
411
+ - **to_binary**
412
+
413
+ *img*.to_binary(n_gray_colors = 64, quantize_dither = Magick::NoDitherMethod, threshold_map = "checks") -> *image*
414
+
415
+ Converts a color image to a binary (monochrome) image.
416
+
417
+ Combines two RMagick methods:
418
+
419
+ 1. Magick::Image#[quantize](https://rmagick.github.io/image3.html#quantize) (color reducing step);
420
+
421
+ 2. Magick::Image#[ordered_dither](https://rmagick.github.io/image2.html#ordered_dither) (ordered dither filter).
422
+
423
+ The first step converts image to a grayscale format with limited number of gray tones (specified by the *n_gray_colors* option). The second step applies the ordered dither filter to the grayscale image, resulting a binary image.
424
+
425
+ **Arguments**:
426
+
427
+ - n_gray_colors
428
+
429
+ Maximum number of gray tones in the quantized image.
430
+
431
+ - quantize_dither
432
+
433
+ Error diffusion dither that will be applied on the color reduction step. A Magick::[DitherMethod](https://rmagick.github.io/constants.html#DitherMethod) value.
434
+
435
+ - threshold_map
436
+
437
+ A dither pattern to use.
438
+
439
+ **Returns**: a new image.
440
+
441
+ ***
442
+
443
+ - **to_binary!**
444
+
445
+ *img*.to_binary!(n_gray_colors = 64, quantize_dither = Magick::NoDitherMethod, threshold_map = "checks") -> *self*
446
+
447
+ The in-place form of **to_binary!**.
448
+
449
+ **Returns**: self.
450
+
451
+ ***
452
+
453
+ ## Development
454
+
455
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
456
+
457
+ You can also run `bin/console` for an interactive prompt that will allow you to experiment.
458
+
459
+ ## Contributing
460
+
461
+ Bug reports and pull requests are welcome on GitHub at https://github.com/8bit-mate/bin_magick.rb.
462
+
463
+ ## Acknowledge
464
+
465
+ Test images were generated with the https://make.girls.moe/ project.
466
+
467
+ ## License
468
+
469
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/*_test.rb"]
10
+ end
11
+
12
+ require "rubocop/rake_task"
13
+
14
+ RuboCop::RakeTask.new
15
+
16
+ task default: %i[test rubocop]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "rmagick/bin_magick"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Magick
4
+ module BinMagick
5
+ #
6
+ # Base error class.
7
+ #
8
+ class BinMagickError < ::StandardError
9
+ attr_reader :message
10
+
11
+ def initialize(message)
12
+ @message = message
13
+ super
14
+ end
15
+ end
16
+
17
+ #
18
+ # Raises on an I/O operation fail.
19
+ #
20
+ class IOError < BinMagickError
21
+ attr_reader :message
22
+
23
+ def initialize(message)
24
+ @message = message
25
+ super
26
+ end
27
+ end
28
+
29
+ #
30
+ # Raises when an Magick::BinMagick::Image instance is being initialized with a destroyed image.
31
+ #
32
+ class DestroyedImageError < BinMagickError
33
+ attr_reader :message
34
+
35
+ def initialize(message)
36
+ @message = message
37
+ super
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "magick_wrapper"
4
+
5
+ module Magick
6
+ module BinMagick
7
+ #
8
+ # Image attribute methods.
9
+ #
10
+ class Image < MagickWrapper
11
+ #
12
+ # Return image height.
13
+ #
14
+ # @return [Integer]
15
+ #
16
+ def height
17
+ rows
18
+ end
19
+
20
+ #
21
+ # Return image width.
22
+ #
23
+ # @return [Integer]
24
+ #
25
+ def width
26
+ columns
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Magick
4
+ module BinMagick
5
+ #
6
+ # Image class methods.
7
+ #
8
+ class Image < MagickWrapper
9
+ #
10
+ # Read image from a file.
11
+ #
12
+ # @param [String] filename
13
+ #
14
+ # @return [BinMagick::Image]
15
+ #
16
+ # @raise [BinMagick::IOError]
17
+ #
18
+ def self.from_file(filename)
19
+ image = Magick::Image.read(filename).first
20
+ new(image)
21
+ rescue Magick::ImageMagickError => e
22
+ raise BinMagick::IOError, "Error occurred while reading image from file: #{e.message}"
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,232 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Magick
4
+ module BinMagick
5
+ N_GRAY_COLORS = 64 # Default number of gray tones when image is being quantized to a grayscale.
6
+
7
+ #
8
+ # Image instance methods.
9
+ #
10
+ class Image < MagickWrapper
11
+ #
12
+ # Check if the image has at least one black pixel.
13
+ #
14
+ # @return [Boolean]
15
+ #
16
+ def black_px?
17
+ colors = color_histogram
18
+ colors = colors.transform_keys(&:to_color)
19
+
20
+ colors.key?("black")
21
+ end
22
+
23
+ #
24
+ # Crop border around the image. If the image is blank: return an unedited copy of the target image.
25
+ #
26
+ # @return [BinMagick::Image]
27
+ # A cropped image.
28
+ #
29
+ def crop_border
30
+ return copy unless black_px?
31
+
32
+ bb = bounding_box
33
+
34
+ crop(
35
+ bb.x,
36
+ bb.y,
37
+ bb.width,
38
+ bb.height
39
+ )
40
+ end
41
+
42
+ #
43
+ # A bang version of the 'BinMagick::Image#crop_border'.
44
+ #
45
+ def crop_border!
46
+ cropped_img = crop_border
47
+ _replace_pixels(cropped_img)
48
+
49
+ self
50
+ end
51
+
52
+ #
53
+ # Crop border around the image, but treat a color image like it is already a binary one.
54
+ #
55
+ # See also: 'BinMagick::Image#to_binary' method description.
56
+ #
57
+ # @return [BinMagick::Image]
58
+ # A cropped image.
59
+ #
60
+ def crop_border_clr(...)
61
+ bin_img = to_binary(...)
62
+
63
+ return copy unless bin_img.black_px?
64
+
65
+ bb = bin_img.bounding_box
66
+
67
+ crop(
68
+ bb.x,
69
+ bb.y,
70
+ bb.width,
71
+ bb.height
72
+ )
73
+ end
74
+
75
+ #
76
+ # A bang version of the 'BinMagick::Image#crop_border_clr'.
77
+ #
78
+ def crop_border_clr!(...)
79
+ cropped_img = crop_border_clr(...)
80
+ _replace_pixels(cropped_img)
81
+
82
+ self
83
+ end
84
+
85
+ #
86
+ # Workaround for the 'Magick::Image#display' method.
87
+ #
88
+ def display
89
+ pixels = @image.dispatch(0, 0, columns, rows, "RGB")
90
+ img = Magick::Image.constitute(columns, rows, "RGB", pixels)
91
+ img.display
92
+
93
+ self
94
+ end
95
+
96
+ #
97
+ # A bang version of the 'Magick::Image#extent'.
98
+ #
99
+ # @param [Integer] width
100
+ #
101
+ # @param [Integer] height
102
+ #
103
+ # @option [Integer] x (0)
104
+ #
105
+ # @option [Integer] y (0)
106
+ #
107
+ def extent!(width, height, x = 0, y = 0)
108
+ extended_img = @image.extent(width, height, x, y)
109
+ _replace_pixels(extended_img)
110
+
111
+ self
112
+ end
113
+
114
+ #
115
+ # Scale the image to fit into the size limits *IF* it oversize them.
116
+ #
117
+ # @param [Integer] max_width
118
+ #
119
+ # @param [Integer] max_height
120
+ #
121
+ # @return [BinMagick::Image]
122
+ # A resized image.
123
+ #
124
+ def fit_to_size(max_width, max_height)
125
+ oversize?(max_width, max_height) ? resize_to_fit(max_width, max_height) : copy
126
+ end
127
+
128
+ #
129
+ # A bang version of the 'BinMagick::Image#fit_to_size'.
130
+ #
131
+ def fit_to_size!(...)
132
+ new_img = fit_to_size(...)
133
+ _replace_pixels(new_img)
134
+
135
+ self
136
+ end
137
+
138
+ #
139
+ # A bang version of the 'Magick::Image#level2'.
140
+ #
141
+ # Note: The 'Magick::Image#level2' method is exactly the same as 'Magick::Image#level' except that it never
142
+ # swaps the arguments.
143
+ #
144
+ def level!(black_point = 0.0, white_point = Magick::QuantumRange, gamma = 1.0)
145
+ new_img = @image.level2(black_point, white_point, gamma)
146
+ _replace_pixels(new_img)
147
+
148
+ self
149
+ end
150
+
151
+ #
152
+ # A bang version of the 'Magick::Image#ordered_dither'.
153
+ #
154
+ def ordered_dither!(threshold_map = "checks")
155
+ new_img = @image.ordered_dither(threshold_map)
156
+ _replace_pixels(new_img)
157
+
158
+ self
159
+ end
160
+
161
+ #
162
+ # Check if the image dimensions are larger than the provided height OR width.
163
+ #
164
+ # @return [Boolean]
165
+ #
166
+ def oversize?(max_width, max_height)
167
+ columns > max_width || rows > max_height
168
+ end
169
+
170
+ #
171
+ # A bang version of the 'Magick::Image#quantize'.
172
+ #
173
+ def quantize!(
174
+ number_colors = N_GRAY_COLORS,
175
+ colorspace = Magick::GRAYColorspace,
176
+ dither = Magick::NoDitherMethod,
177
+ tree_depth = 0,
178
+ measure_error = false
179
+ )
180
+ new_img = @image.quantize(number_colors, colorspace, dither, tree_depth, measure_error)
181
+ _replace_pixels(new_img)
182
+
183
+ self
184
+ end
185
+
186
+ #
187
+ # Convert a color image to a binary image.
188
+ #
189
+ # @option [Integer] n_gray_colors (N_GRAY_COLORS)
190
+ # Number of grayscale colors when image is being quantized.
191
+ #
192
+ # @option [Magick::DitherMethod] quantize_dither (Magick::NoDitherMethod)
193
+ # Error diffusion dither that will be applied on the color reduction step.
194
+ #
195
+ # @option [String] threshold_map ("checks")
196
+ # Dither pattern for convertion from grayscale to binary.
197
+ #
198
+ # @return [BinMagick::Image]
199
+ # Binary version of the image.
200
+ #
201
+ def to_binary(n_gray_colors = N_GRAY_COLORS, quantize_dither = Magick::NoDitherMethod, threshold_map = "checks")
202
+ grayscale_img = quantize(n_gray_colors, Magick::GRAYColorspace, quantize_dither)
203
+ grayscale_img.ordered_dither(threshold_map)
204
+ end
205
+
206
+ #
207
+ # A bang version of the 'BinMagick::Image#to_binary'.
208
+ #
209
+ def to_binary!(...)
210
+ bin_img = to_binary(...)
211
+ _replace_pixels(bin_img)
212
+
213
+ self
214
+ end
215
+
216
+ private
217
+
218
+ #
219
+ # Replace pixels in the target image with pixels from the image 'img' that is a result of a BinMagick::Image or
220
+ # a Magick::Image instance method call.
221
+ #
222
+ # Method used to create bang versions of the existing Magick::Image methods that do not have a bang version.
223
+ #
224
+ # @param [Magick::Image] img
225
+ #
226
+ def _replace_pixels(img)
227
+ pixels = img.dispatch(0, 0, img.columns, img.rows, "RGB")
228
+ @image = Magick::Image.constitute(img.columns, img.rows, "RGB", pixels)
229
+ end
230
+ end
231
+ end
232
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Magick
4
+ module BinMagick
5
+ #
6
+ # A wrapper around the "Magick::Image" class, providing a dynamic interface for forwarding method calls to the
7
+ # underlying "Magick::Image" object. When a method is called on "MagickWrapper", it first checks if the
8
+ # underlying "Magick::Image" object responds to that method, and if so, it invokes the method on the object and
9
+ # returns the result.
10
+ #
11
+ # If the result is another "Magick::Image" object, it wraps it in a new "MagickWrapper" object, allowing chaining
12
+ # of method calls.
13
+ #
14
+ # If the result is not a "Magick::Image" object, it returns the result as is.
15
+ #
16
+ # If the underlying "Magick::Image" object does not respond to the method, it raises a "NoMethodError".
17
+ #
18
+ class MagickWrapper
19
+ attr_reader :image
20
+
21
+ def initialize(image)
22
+ raise BinMagick::DestroyedImageError, "Destroyed image" if image.destroyed?
23
+
24
+ @image = image
25
+ end
26
+
27
+ def method_missing(method_name, *args, &block)
28
+ if @image.respond_to?(method_name)
29
+ result = @image.send(method_name, *args, &block)
30
+ if result.is_a?(Magick::Image)
31
+ self.class.new(result)
32
+ else
33
+ result
34
+ end
35
+ else
36
+ super
37
+ end
38
+ end
39
+
40
+ def respond_to_missing?(method_name, include_private = false)
41
+ @image.respond_to?(method_name) || super
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Magick
4
+ module BinMagick
5
+ VERSION = "0.1.2"
6
+ end
7
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rmagick"
4
+
5
+ require_relative "bin_magick/image_attr_methods"
6
+ require_relative "bin_magick/image_class_methods"
7
+ require_relative "bin_magick/image_inst_methods"
8
+ require_relative "bin_magick/bin_magick_error"
9
+ require_relative "bin_magick/magick_wrapper"
10
+ require_relative "bin_magick/version"
11
+
12
+ module Magick
13
+ module BinMagick; end
14
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/rmagick/bin_magick/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "rmagick-bin_magick"
7
+ spec.version = Magick::BinMagick::VERSION
8
+ spec.authors = ["Mate"]
9
+
10
+ spec.summary = "A tiny gem to create binary images (using RMagick)."
11
+ spec.homepage = "https://github.com/8bit-mate/rmagick-bin_magick.rb"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 3.0.0")
14
+
15
+ spec.metadata["allowed_push_host"] = "https://rubygems.org/"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = spec.homepage
19
+ spec.metadata["changelog_uri"] = "https://github.com/8bit-mate/rmagick-bin_magick.rb/blob/main/CHANGELOG.md"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features|data)/}) }
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_runtime_dependency "rmagick", "~> 5.2", ">= 5.2.0"
31
+
32
+ # For more information and examples about making a new gem, checkout our
33
+ # guide at: https://bundler.io/guides/creating_gem.html
34
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rmagick-bin_magick
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Mate
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-04-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rmagick
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5.2'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 5.2.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '5.2'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 5.2.0
33
+ description:
34
+ email:
35
+ executables: []
36
+ extensions: []
37
+ extra_rdoc_files: []
38
+ files:
39
+ - ".gitignore"
40
+ - ".rubocop.yml"
41
+ - CHANGELOG.md
42
+ - Gemfile
43
+ - Gemfile.lock
44
+ - LICENSE.txt
45
+ - README.md
46
+ - Rakefile
47
+ - bin/console
48
+ - bin/setup
49
+ - lib/rmagick/bin_magick.rb
50
+ - lib/rmagick/bin_magick/bin_magick_error.rb
51
+ - lib/rmagick/bin_magick/image_attr_methods.rb
52
+ - lib/rmagick/bin_magick/image_class_methods.rb
53
+ - lib/rmagick/bin_magick/image_inst_methods.rb
54
+ - lib/rmagick/bin_magick/magick_wrapper.rb
55
+ - lib/rmagick/bin_magick/version.rb
56
+ - rmagick-bin_magick.gemspec
57
+ homepage: https://github.com/8bit-mate/rmagick-bin_magick.rb
58
+ licenses:
59
+ - MIT
60
+ metadata:
61
+ allowed_push_host: https://rubygems.org/
62
+ homepage_uri: https://github.com/8bit-mate/rmagick-bin_magick.rb
63
+ source_code_uri: https://github.com/8bit-mate/rmagick-bin_magick.rb
64
+ changelog_uri: https://github.com/8bit-mate/rmagick-bin_magick.rb/blob/main/CHANGELOG.md
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: 3.0.0
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubygems_version: 3.2.3
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: A tiny gem to create binary images (using RMagick).
84
+ test_files: []