magro 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0b0bad63e73de3bde86fdd749536f8949fd84e8c
4
+ data.tar.gz: a6c86139fe5cc7c401a9090a34400bf2387f5cc6
5
+ SHA512:
6
+ metadata.gz: 872b26cc6825cd1bb7954e5104d6b196944798126d2d3ae66f08380d7017e747a8eb4748b6fbe2d688feff6ed79fbdb0d082ec29ed188b6cab38601125d7543e
7
+ data.tar.gz: 53de5a2b240ce63404a8831f61b6285bf3009d3647c487ac6fb7fa32cada5372dced92d839297af911ee4a6b4c6e062bef88a692aac8d4ccdfa7ca1c7f5f45ea
@@ -0,0 +1,18 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+
14
+ *.swp
15
+ *.bundle
16
+ tags
17
+ .DS_Store
18
+ .ruby-version
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,14 @@
1
+ ---
2
+ sudo: true
3
+ os: linux
4
+ dist: bionic
5
+ language: ruby
6
+ cache: bundler
7
+ rvm:
8
+ - '2.4'
9
+ - '2.5'
10
+ - '2.6'
11
+
12
+ before_install:
13
+ - sudo apt-get install -y libpng-dev libjpeg-dev
14
+ - gem install bundler -v 2.0.2
@@ -0,0 +1,2 @@
1
+ # 0.1.0
2
+ - First release.
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at yoshoku@outlook.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in magro.gemspec
4
+ gemspec
@@ -0,0 +1,27 @@
1
+ Copyright (c) 2019 Atsushi Tatsuma
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+
7
+ * Redistributions of source code must retain the above copyright notice, this
8
+ list of conditions and the following disclaimer.
9
+
10
+ * Redistributions in binary form must reproduce the above copyright notice,
11
+ this list of conditions and the following disclaimer in the documentation
12
+ and/or other materials provided with the distribution.
13
+
14
+ * Neither the name of the copyright holder nor the names of its
15
+ contributors may be used to endorse or promote products derived from
16
+ this software without specific prior written permission.
17
+
18
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,61 @@
1
+ # Magro
2
+
3
+ [![Build Status](https://travis-ci.org/yoshoku/magro.svg?branch=master)](https://travis-ci.org/yoshoku/magro)
4
+ [![BSD 3-Clause License](https://img.shields.io/badge/License-BSD%203--Clause-orange.svg)](https://github.com/yoshoku/numo-liblinear/blob/master/LICENSE.txt)
5
+
6
+ Magro is an image processing library in Ruby.
7
+ Magro uses [Numo::NArray](https://github.com/ruby-numo/numo-narray) arrays as image objects.
8
+
9
+ ## Installation
10
+
11
+ Magro dependents some external libraries to provides functions loading image file.
12
+
13
+ macOS:
14
+
15
+ $ brew install libpng libjpeg
16
+
17
+ Ubuntu:
18
+
19
+ $ sudo apt-get install libpng-dev libjpeg-dev
20
+
21
+ Add this line to your application's Gemfile:
22
+
23
+ ```ruby
24
+ gem 'magro'
25
+ ```
26
+
27
+ And then execute:
28
+
29
+ $ bundle
30
+
31
+ Or install it yourself as:
32
+
33
+ $ gem install magro
34
+
35
+ ## Usage
36
+
37
+ ```ruby
38
+ > require 'magro'
39
+ => true
40
+ > image = Magro::IO.imread('lena.png')
41
+ => Numo::UInt8#shape=[512,512,3]
42
+ > grayscale = image.median(axis: 2)
43
+ => Numo::UInt8#shape=[512,512]
44
+ > Magro::IO.imsave('lena_gray.png', grayscale)
45
+ => true
46
+ ```
47
+
48
+ ## Development
49
+
50
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
51
+
52
+ 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
53
+
54
+ ## Contributing
55
+
56
+ Bug reports and pull requests are welcome on GitHub at https://github.com/yoshoku/magro.
57
+ This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
58
+
59
+ ## Code of Conduct
60
+
61
+ Everyone interacting in the Magro project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/yoshoku/magro/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,14 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ require 'rake/extensiontask'
7
+
8
+ task build: :compile
9
+
10
+ Rake::ExtensionTask.new('magro') do |ext|
11
+ ext.lib_dir = 'lib/magro'
12
+ end
13
+
14
+ task default: %i[clobber compile spec]
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "magro"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -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,54 @@
1
+ require 'mkmf'
2
+ require 'numo/narray'
3
+
4
+ $LOAD_PATH.each do |lp|
5
+ if File.exist?(File.join(lp, 'numo/numo/narray.h'))
6
+ $INCFLAGS = "-I#{lp}/numo #{$INCFLAGS}"
7
+ break
8
+ end
9
+ end
10
+
11
+ unless have_header('numo/narray.h')
12
+ puts 'numo/narray.h not found.'
13
+ exit(1)
14
+ end
15
+
16
+ if RUBY_PLATFORM =~ /mswin|cygwin|mingw/
17
+ $LOAD_PATH.each do |lp|
18
+ if File.exist?(File.join(lp, 'numo/libnarray.a'))
19
+ $LDFLAGS = "-L#{lp}/numo #{$LDFLAGS}"
20
+ break
21
+ end
22
+ end
23
+ unless have_library('narray', 'nary_new')
24
+ puts 'libnarray.a not found.'
25
+ exit(1)
26
+ end
27
+ end
28
+
29
+ unless have_header('setjmp.h')
30
+ puts 'setjmp.h not found.'
31
+ exit(1)
32
+ end
33
+
34
+ unless have_header('png.h')
35
+ puts 'png.h not found.'
36
+ exit(1)
37
+ end
38
+
39
+ unless have_header('jpeglib.h')
40
+ puts 'jpeglib.h not found.'
41
+ exit(1)
42
+ end
43
+
44
+ unless have_library('png')
45
+ puts 'libpng not found.'
46
+ exit(1)
47
+ end
48
+
49
+ unless have_library('jpeg')
50
+ puts 'libjpeg not found.'
51
+ exit(1)
52
+ end
53
+
54
+ create_makefile('magro/magro')
@@ -0,0 +1,403 @@
1
+ #include "magro.h"
2
+
3
+ VALUE mMagro;
4
+ VALUE mIO;
5
+
6
+ /**
7
+ * @!visibility private
8
+ */
9
+ static
10
+ VALUE magro_io_read_png(VALUE self, VALUE filename_)
11
+ {
12
+ char* filename = StringValuePtr(filename_);
13
+ FILE* file_ptr = fopen(filename, "rb");
14
+ unsigned char header[8];
15
+ png_structp png_ptr;
16
+ png_infop info_ptr;
17
+ png_bytep* row_ptr_ptr;
18
+ png_bytep row_ptr;
19
+ png_uint_32 width, height;
20
+ int color_type;
21
+ int bit_depth;
22
+ png_uint_32 y;
23
+ int n_dims = 0;
24
+ int n_ch;
25
+ size_t shape[3] = { 0 };
26
+ VALUE nary;
27
+ uint8_t* nary_ptr;
28
+
29
+ if (file_ptr == NULL) {
30
+ return Qnil;
31
+ }
32
+
33
+ if (fread(header, 1, 8, file_ptr) < 8) {
34
+ fclose(file_ptr);
35
+ return Qnil;
36
+ }
37
+
38
+ if (png_sig_cmp(header, 0, 8)) {
39
+ fclose(file_ptr);
40
+ return Qnil;
41
+ }
42
+
43
+ png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
44
+ if (png_ptr == NULL) {
45
+ fclose(file_ptr);
46
+ return Qnil;
47
+ }
48
+ info_ptr = png_create_info_struct(png_ptr);
49
+ if (info_ptr == NULL) {
50
+ png_destroy_read_struct(&png_ptr, NULL, NULL);
51
+ fclose(file_ptr);
52
+ return Qnil;
53
+ }
54
+ if (setjmp(png_jmpbuf(png_ptr))) {
55
+ png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
56
+ fclose(file_ptr);
57
+ return Qnil;
58
+ }
59
+
60
+ png_init_io(png_ptr, file_ptr);
61
+ png_set_sig_bytes(png_ptr, 8);
62
+
63
+ png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_PACKING | PNG_TRANSFORM_EXPAND | PNG_TRANSFORM_STRIP_16, NULL);
64
+ row_ptr_ptr = png_get_rows(png_ptr, info_ptr);
65
+ png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, NULL, NULL, NULL);
66
+
67
+ if (color_type == PNG_COLOR_TYPE_PALETTE) {
68
+ png_set_palette_to_rgb(png_ptr);
69
+ png_read_update_info(png_ptr, info_ptr);
70
+ png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, NULL, NULL, NULL);
71
+ }
72
+
73
+ switch (color_type) {
74
+ case PNG_COLOR_TYPE_GRAY:
75
+ n_ch = 1;
76
+ n_dims = 2;
77
+ shape[0] = height;
78
+ shape[1] = width;
79
+ break;
80
+ case PNG_COLOR_TYPE_GRAY_ALPHA:
81
+ n_ch = 2;
82
+ n_dims = 3;
83
+ shape[0] = height;
84
+ shape[1] = width;
85
+ shape[2] = 2;
86
+ break;
87
+ case PNG_COLOR_TYPE_RGB:
88
+ n_ch = 3;
89
+ n_dims = 3;
90
+ shape[0] = height;
91
+ shape[1] = width;
92
+ shape[2] = 3;
93
+ break;
94
+ case PNG_COLOR_TYPE_RGB_ALPHA:
95
+ n_ch = 4;
96
+ n_dims = 3;
97
+ shape[0] = height;
98
+ shape[1] = width;
99
+ shape[2] = 4;
100
+ break;
101
+ default:
102
+ n_dims = 0;
103
+ break;
104
+ }
105
+
106
+ if (n_dims == 0) {
107
+ fclose(file_ptr);
108
+ png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
109
+ return Qnil;
110
+ }
111
+
112
+ nary = rb_narray_new(numo_cUInt8, n_dims, shape);
113
+ nary_ptr = (uint8_t*)na_get_pointer_for_write(nary);
114
+
115
+ for (y = 0; y < height; y++) {
116
+ row_ptr = row_ptr_ptr[y];
117
+ memcpy(nary_ptr + y * width * n_ch, row_ptr, width * n_ch);
118
+ }
119
+
120
+ fclose(file_ptr);
121
+ png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
122
+
123
+ return nary;
124
+ }
125
+
126
+ /**
127
+ * @!visibility private
128
+ */
129
+ static
130
+ VALUE magro_io_save_png(VALUE self, VALUE filename_, VALUE image)
131
+ {
132
+ char* filename = StringValuePtr(filename_);
133
+ FILE* file_ptr = fopen(filename, "wb");
134
+ png_structp png_ptr;
135
+ png_infop info_ptr;
136
+ png_bytep* row_ptr_ptr;
137
+ png_uint_32 width, height;
138
+ int color_type;
139
+ int bit_depth = 8;
140
+ png_uint_32 y;
141
+ int n_ch;
142
+ int n_dims;
143
+ narray_t* image_nary;
144
+ uint8_t* image_ptr;
145
+
146
+ if (CLASS_OF(image) != numo_cUInt8) {
147
+ image = rb_funcall(numo_cUInt8, rb_intern("cast"), 1, image);
148
+ }
149
+ if (!RTEST(nary_check_contiguous(image))) {
150
+ image = nary_dup(image);
151
+ }
152
+
153
+ GetNArray(image, image_nary);
154
+ n_dims = NA_NDIM(image_nary);
155
+ height = (png_uint_32)NA_SHAPE(image_nary)[0];
156
+ width = (png_uint_32)NA_SHAPE(image_nary)[1];
157
+ image_ptr = (uint8_t*)na_get_pointer_for_read(image);
158
+
159
+ n_ch = 1;
160
+ if (n_dims == 3) {
161
+ n_ch = (int)NA_SHAPE(image_nary)[2];
162
+ }
163
+
164
+ switch (n_ch) {
165
+ case 4:
166
+ color_type = PNG_COLOR_TYPE_RGBA;
167
+ break;
168
+ case 3:
169
+ color_type = PNG_COLOR_TYPE_RGB;
170
+ break;
171
+ case 2:
172
+ color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
173
+ break;
174
+ default:
175
+ color_type = PNG_COLOR_TYPE_GRAY;
176
+ break;
177
+ }
178
+
179
+ if (file_ptr == NULL) {
180
+ return Qfalse;
181
+ }
182
+ png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
183
+ if (png_ptr == NULL) {
184
+ fclose(file_ptr);
185
+ return Qfalse;
186
+ }
187
+ info_ptr = png_create_info_struct(png_ptr);
188
+ if (info_ptr == NULL) {
189
+ png_destroy_read_struct(&png_ptr, NULL, NULL);
190
+ fclose(file_ptr);
191
+ return Qfalse;
192
+ }
193
+ if (setjmp(png_jmpbuf(png_ptr))) {
194
+ png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
195
+ fclose(file_ptr);
196
+ return Qfalse;
197
+ }
198
+
199
+ png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth, color_type,
200
+ PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
201
+
202
+ row_ptr_ptr = png_malloc(png_ptr, height * sizeof(png_bytep));
203
+ for (y = 0; y < height; y++) {
204
+ row_ptr_ptr[y] = png_malloc(png_ptr, width * n_ch * sizeof(png_byte));
205
+ memcpy(row_ptr_ptr[y], image_ptr + y * width * n_ch, width * n_ch);
206
+ }
207
+
208
+ png_init_io(png_ptr, file_ptr);
209
+ png_set_rows(png_ptr, info_ptr, row_ptr_ptr);
210
+ png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
211
+
212
+ fclose(file_ptr);
213
+ for (y = 0; y < height; y++) {
214
+ png_free(png_ptr, row_ptr_ptr[y]);
215
+ }
216
+ png_free(png_ptr, row_ptr_ptr);
217
+ png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
218
+
219
+ return Qtrue;
220
+ }
221
+
222
+ struct my_error_mgr {
223
+ struct jpeg_error_mgr pub;
224
+ jmp_buf setjmp_buffer;
225
+ };
226
+
227
+ static void
228
+ my_error_exit(j_common_ptr cinfo)
229
+ {
230
+ struct my_error_mgr* my_err = (struct my_error_mgr*)cinfo->err;
231
+ (*cinfo->err->output_message)(cinfo);
232
+ longjmp(my_err->setjmp_buffer, 1);
233
+ }
234
+
235
+ /**
236
+ * @!visibility private
237
+ */
238
+ static
239
+ VALUE magro_io_read_jpg(VALUE self, VALUE filename_)
240
+ {
241
+ char* filename = StringValuePtr(filename_);
242
+ FILE* file_ptr = fopen(filename, "rb");
243
+ struct jpeg_decompress_struct jpeg;
244
+ struct my_error_mgr err;
245
+ unsigned int width, height;
246
+ int n_colors;
247
+ size_t shape[3] = { 0 };
248
+ int n_dims;
249
+ unsigned int y;
250
+ VALUE nary;
251
+ uint8_t* nary_ptr;
252
+ JSAMPLE* tmp;
253
+
254
+
255
+ if (file_ptr == NULL) {
256
+ return Qnil;
257
+ }
258
+
259
+ jpeg.err = jpeg_std_error(&err.pub);
260
+ err.pub.error_exit = my_error_exit;
261
+ if (setjmp(err.setjmp_buffer)) {
262
+ return Qnil;
263
+ }
264
+
265
+ jpeg_create_decompress(&jpeg);
266
+ jpeg_stdio_src(&jpeg, file_ptr);
267
+ jpeg_read_header(&jpeg, TRUE);
268
+ jpeg_start_decompress(&jpeg);
269
+
270
+ width = jpeg.output_width;
271
+ height = jpeg.output_height;
272
+ n_colors = jpeg.out_color_components;
273
+
274
+ n_dims = n_colors == 1 ? 2 : 3;
275
+ shape[0] = height;
276
+ shape[1] = width;
277
+ shape[2] = n_colors;
278
+ nary = rb_narray_new(numo_cUInt8, n_dims, shape);
279
+ nary_ptr = (uint8_t*)na_get_pointer_for_write(nary);
280
+
281
+ for (y = 0; y < height; y++) {
282
+ tmp = nary_ptr + y * width * n_colors;
283
+ jpeg_read_scanlines(&jpeg, &tmp, 1);
284
+ }
285
+
286
+ fclose(file_ptr);
287
+ jpeg_finish_decompress(&jpeg);
288
+ jpeg_destroy_decompress(&jpeg);
289
+
290
+ return nary;
291
+ }
292
+
293
+ /**
294
+ * @!visibility private
295
+ */
296
+ static
297
+ VALUE magro_io_save_jpg(int argc, VALUE* argv, VALUE self)
298
+ {
299
+ VALUE filename_;
300
+ VALUE image;
301
+ VALUE quality_;
302
+ char* filename;
303
+ FILE* file_ptr;
304
+ struct jpeg_compress_struct jpeg;
305
+ struct my_error_mgr err;
306
+ narray_t* image_nary;
307
+ int quality;
308
+ int n_dims, n_ch;
309
+ unsigned int width, height, y;
310
+ uint8_t* image_ptr;
311
+ JSAMPLE* tmp;
312
+
313
+ rb_scan_args(argc, argv, "21", &filename_, &image, &quality_);
314
+
315
+ if (NIL_P(quality_)) {
316
+ quality = 95;
317
+ } else {
318
+ quality = NUM2INT(quality_);
319
+ }
320
+
321
+ filename = StringValuePtr(filename_);
322
+
323
+ if (CLASS_OF(image) != numo_cUInt8) {
324
+ image = rb_funcall(numo_cUInt8, rb_intern("cast"), 1, image);
325
+ }
326
+ if (!RTEST(nary_check_contiguous(image))) {
327
+ image = nary_dup(image);
328
+ }
329
+
330
+ jpeg.err = jpeg_std_error(&err.pub);
331
+ err.pub.error_exit = my_error_exit;
332
+ if (setjmp(err.setjmp_buffer)) {
333
+ return Qfalse;
334
+ }
335
+
336
+ jpeg_create_compress(&jpeg);
337
+
338
+ file_ptr = fopen(filename, "wb");
339
+ if (file_ptr == NULL) {
340
+ jpeg_destroy_compress(&jpeg);
341
+ return Qfalse;
342
+ }
343
+
344
+ GetNArray(image, image_nary);
345
+ n_dims = NA_NDIM(image_nary);
346
+ height = (unsigned int)NA_SHAPE(image_nary)[0];
347
+ width = (unsigned int)NA_SHAPE(image_nary)[1];
348
+ image_ptr = (uint8_t*)na_get_pointer_for_read(image);
349
+
350
+ n_ch = 1;
351
+ if (n_dims == 3) {
352
+ n_ch = (int)NA_SHAPE(image_nary)[2];
353
+ }
354
+
355
+ jpeg_stdio_dest(&jpeg, file_ptr);
356
+
357
+ jpeg.image_height = height;
358
+ jpeg.image_width = width;
359
+ jpeg.input_components = n_ch;
360
+
361
+ switch (n_ch) {
362
+ case 3:
363
+ jpeg.in_color_space = JCS_RGB;
364
+ break;
365
+ case 1:
366
+ jpeg.in_color_space = JCS_GRAYSCALE;
367
+ break;
368
+ default:
369
+ jpeg.in_color_space = JCS_UNKNOWN;
370
+ break;
371
+ }
372
+
373
+ jpeg_set_defaults(&jpeg);
374
+
375
+ jpeg_set_quality(&jpeg, quality, TRUE);
376
+
377
+ jpeg_start_compress(&jpeg, TRUE);
378
+
379
+ for (y = 0; y < height; y++) {
380
+ tmp = image_ptr + y * width * n_ch;
381
+ jpeg_write_scanlines(&jpeg, &tmp, 1);
382
+ }
383
+
384
+ jpeg_finish_compress(&jpeg);
385
+ jpeg_destroy_compress(&jpeg);
386
+
387
+ fclose(file_ptr);
388
+
389
+ return Qtrue;
390
+ }
391
+
392
+ void Init_magro()
393
+ {
394
+ rb_require("numo/narray");
395
+
396
+ mMagro = rb_define_module("Magro");
397
+
398
+ mIO = rb_define_module_under(mMagro, "IO");
399
+ rb_define_module_function(mIO, "read_png", magro_io_read_png, 1);
400
+ rb_define_module_function(mIO, "save_png", magro_io_save_png, 2);
401
+ rb_define_module_function(mIO, "read_jpg", magro_io_read_jpg, 1);
402
+ rb_define_module_function(mIO, "save_jpg", magro_io_save_jpg, -1);
403
+ }
@@ -0,0 +1,15 @@
1
+ #ifndef MAGRO_H
2
+ #define MAGRO_H 1
3
+
4
+ #include <math.h>
5
+ #include <string.h>
6
+
7
+ #include <setjmp.h>
8
+ #include <png.h>
9
+ #include <jpeglib.h>
10
+
11
+ #include <ruby.h>
12
+ #include <numo/narray.h>
13
+ #include <numo/template.h>
14
+
15
+ #endif /* MAGRO_H */
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'numo/narray'
4
+
5
+ require 'magro/version'
6
+ require 'magro/magro'
7
+
8
+ require 'magro/io'
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Magro
4
+ # IO module provides functions for input and output of image file.
5
+ module IO
6
+ module_function
7
+
8
+ # Loads an image from file.
9
+ # @param filename [String] Path to image file to be loaded.
10
+ # Currently, the following file formats are support:
11
+ # Portbale Network Graphics (*.png) and JPEG files (*.jpeg, *.jpg, *jpe).
12
+ # @return [Numo::UInt8] (shape: [height, width, n_channels]) Loaded image.
13
+ def imread(filename)
14
+ raise ArgumentError, 'Expect class of filename to be String.' unless filename.is_a?(String)
15
+ return read_jpg(filename) if filename =~ /\.(jpeg|jpg|jpe)$/
16
+ return read_png(filename) if filename =~ /\.png$/
17
+ end
18
+
19
+ # Saves an image to file.
20
+ # @param filename [String] Path to image file to be saved.
21
+ # Currently, the following file formats are support:
22
+ # Portbale Network Graphics (*.png) and JPEG files (*.jpeg, *.jpg, *jpe).
23
+ # @param image [Numo::UInt8] (shape: [height, width, n_channels]) Image data to be saved.
24
+ # @param quality [Integer] Quality parameter of jpeg image that takes a value between 0 to 100.
25
+ # @return [Boolean] true if file save is successful.
26
+ def imsave(filename, image, quality: nil)
27
+ raise ArgumentError, 'Expect class of filename to be String.' unless filename.is_a?(String)
28
+ raise ArgumentError, 'Expect class of image to be Numo::NArray.' unless image.is_a?(Numo::NArray)
29
+
30
+ if filename =~ /\.(jpeg|jpg|jpe)$/
31
+ unless quality.nil?
32
+ raise ArgumentError, 'Expect class of quality to be Numeric.' unless quality.is_a?(Numeric)
33
+ raise ArgumentError, 'Range of quality value between 0 to 100.' unless quality.between?(0, 100)
34
+ end
35
+ return save_jpg(filename, image, quality)
36
+ end
37
+
38
+ return save_png(filename, image) if filename =~ /\.png$/
39
+
40
+ false
41
+ end
42
+
43
+ private_class_method :read_jpg, :read_png, :save_jpg, :save_png
44
+ end
45
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Magro is an image processing library in Ruby.
4
+ module Magro
5
+ # The version of Magro you are using.
6
+ VERSION = '0.1.0'
7
+ end
@@ -0,0 +1,34 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'magro/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'magro'
7
+ spec.version = Magro::VERSION
8
+ spec.authors = ['yoshoku']
9
+ spec.email = ['yoshoku@outlook.com']
10
+
11
+ spec.summary = 'Magro is an image processing library for Ruby.'
12
+ spec.description = 'Magro is an image processing library for Ruby.'
13
+
14
+ spec.homepage = 'https://github.com/yoshoku/magro'
15
+ spec.license = 'BSD-3-Clause'
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+
23
+ spec.bindir = 'exe'
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ['lib']
26
+ spec.extensions = ['ext/magro/extconf.rb']
27
+
28
+ spec.add_runtime_dependency 'numo-narray', '~> 0.9.1'
29
+
30
+ spec.add_development_dependency 'bundler', '~> 2.0'
31
+ spec.add_development_dependency 'rake', '~> 10.0'
32
+ spec.add_development_dependency 'rake-compiler', '~> 1.0'
33
+ spec.add_development_dependency 'rspec', '~> 3.0'
34
+ end
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: magro
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - yoshoku
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-08-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: numo-narray
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.9.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.9.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake-compiler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description: Magro is an image processing library for Ruby.
84
+ email:
85
+ - yoshoku@outlook.com
86
+ executables: []
87
+ extensions:
88
+ - ext/magro/extconf.rb
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - ".travis.yml"
94
+ - CHANGELOG.md
95
+ - CODE_OF_CONDUCT.md
96
+ - Gemfile
97
+ - LICENSE.txt
98
+ - README.md
99
+ - Rakefile
100
+ - bin/console
101
+ - bin/setup
102
+ - ext/magro/extconf.rb
103
+ - ext/magro/magro.c
104
+ - ext/magro/magro.h
105
+ - lib/magro.rb
106
+ - lib/magro/io.rb
107
+ - lib/magro/version.rb
108
+ - magro.gemspec
109
+ homepage: https://github.com/yoshoku/magro
110
+ licenses:
111
+ - BSD-3-Clause
112
+ metadata: {}
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ requirements: []
128
+ rubyforge_project:
129
+ rubygems_version: 2.6.14.4
130
+ signing_key:
131
+ specification_version: 4
132
+ summary: Magro is an image processing library for Ruby.
133
+ test_files: []