img_to_script-mk90_client_api 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rubocop.yml +13 -0
- data/.vscode/launch.json +21 -0
- data/CHANGELOG.md +5 -0
- data/LICENSE.txt +21 -0
- data/README.md +116 -0
- data/Rakefile +16 -0
- data/img_to_script-mk90_client_api.gemspec +39 -0
- data/lib/img_to_script/mk90_client_api/allowed_input.rb +43 -0
- data/lib/img_to_script/mk90_client_api/app_contract.rb +12 -0
- data/lib/img_to_script/mk90_client_api/autoloader.rb +21 -0
- data/lib/img_to_script/mk90_client_api/formatter_options_contract.rb +23 -0
- data/lib/img_to_script/mk90_client_api/generator_options_contract.rb +25 -0
- data/lib/img_to_script/mk90_client_api/image_processor.rb +67 -0
- data/lib/img_to_script/mk90_client_api/query_contract.rb +30 -0
- data/lib/img_to_script/mk90_client_api/query_handler.rb +193 -0
- data/lib/img_to_script/mk90_client_api/version.rb +7 -0
- data/lib/img_to_script/mk90_client_api.rb +37 -0
- data/sig/img_to_script/mk90_client_api.rbs +6 -0
- metadata +165 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 36909558e771569b7ebb2a476d10cc5a4eddf1fa0d0fdfe00050931e8d9d5349
|
4
|
+
data.tar.gz: 9d15c37ba3b5983ec1fa2ad85dc3fbde3ae3241c7c7303ca197aa65fbfafc973
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0db48159c36dff75a1885ce2df3d17f71b81d480a7eedd56874665cf14848145027378cfd02c59c85e58ff5494847e62fd08399eb816d12769722bfc538e2ef9
|
7
|
+
data.tar.gz: 4c7ec132a7d13f0ab7cb5a83957fb7e35c1cb99596eaece9c3e15c3f66cb9be8dd630b8a9b2b1ef1347051003761668dcbf7a5e18fbb45a403547ef1f1b175d5
|
data/.rubocop.yml
ADDED
data/.vscode/launch.json
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
{
|
2
|
+
// Use IntelliSense to learn about possible attributes.
|
3
|
+
// Hover to view descriptions of existing attributes.
|
4
|
+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
5
|
+
"version": "0.2.0",
|
6
|
+
"configurations": [
|
7
|
+
{
|
8
|
+
"type": "rdbg",
|
9
|
+
"name": "Debug current file with rdbg",
|
10
|
+
"request": "launch",
|
11
|
+
"script": "${file}",
|
12
|
+
"args": [],
|
13
|
+
"askParameters": true
|
14
|
+
},
|
15
|
+
{
|
16
|
+
"type": "rdbg",
|
17
|
+
"name": "Attach with rdbg",
|
18
|
+
"request": "attach"
|
19
|
+
}
|
20
|
+
]
|
21
|
+
}
|
data/CHANGELOG.md
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2023 8bit-m8
|
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,116 @@
|
|
1
|
+
# img_to_script-mk90_client_api
|
2
|
+
|
3
|
+
Provides a handy API between an img_to_mk90_bas application and the **[img_to_script](https://github.com/8bit-mate/img_to_script.rb)** gem. Instead of working with the img_to_script objects directly, just pass a hash with parameters to the img_to_script-mk90_client_api. This is handy when you get the parameters as a JSON string, e.g. from a web front-end.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem "img_to_script-mk90_client_api"
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle install
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install img_to_script-mk90_client_api
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
ImgToScript::MK90ClientAPI.call(query) -> Array\<String\>
|
24
|
+
|
25
|
+
### Argument
|
26
|
+
|
27
|
+
**query**: hash with parameters.
|
28
|
+
|
29
|
+
### Returns
|
30
|
+
|
31
|
+
Generated BASIC program.
|
32
|
+
|
33
|
+
### Raises
|
34
|
+
|
35
|
+
- **ImgToScript::MK90ClientAPI::QueryError**
|
36
|
+
|
37
|
+
A key is missing, has a value of an unsupported type, or has an illegal value.
|
38
|
+
|
39
|
+
- **ImgToScript::MK90ClientAPI::InvalidImage**
|
40
|
+
|
41
|
+
Invalid image: the image can't be decoded from the base64 string.
|
42
|
+
|
43
|
+
## Query description
|
44
|
+
|
45
|
+
| Key | Type | Necessity | Description | Allowed values
|
46
|
+
| ------------- | ------------- | --------- | ------------- | ---------------
|
47
|
+
| :basic_version | String | Required | Defines target BASIC version | <ul><li>1.0</li><li>2.0</li></ul>
|
48
|
+
| :encoding_method | String | Required | Defines target encoding method | <ul><li>hex_mask_enhanced</li><li>hex_mask_default</li><li>rle_v</li><li>rle_h</li><li>segmental_direct_v</li><li>segmental_direct_h</li><li>segmental_data_v</li><li>segmental_data_h</li></ul>
|
49
|
+
| :image | String | Required | Image to convert | A base64-encoded image
|
50
|
+
| :output_format | String | Required | Defines output format | <ul><li>bas</li></ul>
|
51
|
+
| :generator_options | Hash{ Symbol => Object } | Optional | Defines script generator options | -
|
52
|
+
| :formatter_options | Hash{ Symbol => Object } | Optional | Defines script formatter options | -
|
53
|
+
|
54
|
+
### Generator options
|
55
|
+
|
56
|
+
| Key | Type | Necessity | Description | Allowed values
|
57
|
+
| ------------- | ------------- | --------- | ------------- | ---------------
|
58
|
+
| :x_offset | Integer | Optional | Defines image horizontal offset | -120..120
|
59
|
+
| :y_offset | Integer | Optional | Defines image vertical offset | -64..64
|
60
|
+
| :clear_screen | Boolean | Optional | Add a clear screen statement | true/false
|
61
|
+
| :pause_program | Boolean | Optional | Add a pause statement | true/false
|
62
|
+
|
63
|
+
### Formatter options
|
64
|
+
|
65
|
+
| Key | Type | Necessity | Description | Allowed values
|
66
|
+
| ------------- | ------------- | --------- | ------------- | ---------------
|
67
|
+
| :line_offset | Integer | Optional | Defines first BASIC line offset | 1..8000
|
68
|
+
| :line_step | Integer | Optional | Defines step between BASIC lines | 1..100
|
69
|
+
|
70
|
+
## Usage example
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
require "img_to_script/mk90_client_api"
|
74
|
+
require "rmagick/bin_magick"
|
75
|
+
|
76
|
+
def self.read_image(filename)
|
77
|
+
img = Magick::BinMagick::Image.from_file(filename)
|
78
|
+
img.colorspace = Magick::GRAYColorspace
|
79
|
+
Base64.strict_encode64(
|
80
|
+
img.to_blob
|
81
|
+
)
|
82
|
+
end
|
83
|
+
|
84
|
+
script = ImgToScript::MK90ClientAPI.call(
|
85
|
+
{
|
86
|
+
basic_version: "1.0",
|
87
|
+
encoding_method: "hex_mask_enhanced",
|
88
|
+
image: read_image("my_image.png"),
|
89
|
+
output_format: "bas",
|
90
|
+
generator_options: {
|
91
|
+
x_offset: -5,
|
92
|
+
y_offset: -10
|
93
|
+
},
|
94
|
+
formatter_options: {
|
95
|
+
line_offset: 10,
|
96
|
+
line_step: 5
|
97
|
+
}
|
98
|
+
}
|
99
|
+
)
|
100
|
+
|
101
|
+
puts script
|
102
|
+
```
|
103
|
+
|
104
|
+
## Development
|
105
|
+
|
106
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
107
|
+
|
108
|
+
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).
|
109
|
+
|
110
|
+
## Contributing
|
111
|
+
|
112
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/8bit-mate/img_to_script-mk90_client_api.rb.
|
113
|
+
|
114
|
+
## License
|
115
|
+
|
116
|
+
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]
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/img_to_script/mk90_client_api/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "img_to_script-mk90_client_api"
|
7
|
+
spec.version = ImgToScript::MK90ClientAPI::VERSION
|
8
|
+
spec.authors = ["8bit-m8"]
|
9
|
+
spec.email = ["you@example.com"]
|
10
|
+
|
11
|
+
spec.summary = "Provides an API between a client app and the img_to_script gem."
|
12
|
+
spec.homepage = "https://github.com/8bit-mate/img_to_script-mk90_client_api.rb"
|
13
|
+
spec.license = "MIT"
|
14
|
+
spec.required_ruby_version = ">= 3.0.0"
|
15
|
+
|
16
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
17
|
+
|
18
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
19
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
20
|
+
spec.metadata["changelog_uri"] = spec.homepage
|
21
|
+
|
22
|
+
# Specify which files should be added to the gem when it is released.
|
23
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
24
|
+
spec.files = Dir.chdir(__dir__) do
|
25
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
26
|
+
(File.expand_path(f) == __FILE__) ||
|
27
|
+
f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor Gemfile])
|
28
|
+
end
|
29
|
+
end
|
30
|
+
spec.bindir = "exe"
|
31
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
32
|
+
spec.require_paths = ["lib"]
|
33
|
+
|
34
|
+
spec.add_dependency "dry-schema", "~> 1.13", ">= 1.13.3"
|
35
|
+
spec.add_dependency "dry-validation", "~> 1.10", ">= 1.10.0"
|
36
|
+
spec.add_dependency "img_to_script", "~> 1.0", ">= 1.0.0"
|
37
|
+
spec.add_dependency "rmagick-bin_magick", "~> 0.2", ">= 0.2.0"
|
38
|
+
spec.add_dependency "zeitwerk", "~> 2.6", ">= 2.6.12"
|
39
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ImgToScript
|
4
|
+
module MK90ClientAPI
|
5
|
+
#
|
6
|
+
# Namespace for constants that define allowed input values.
|
7
|
+
#
|
8
|
+
module AllowedInput
|
9
|
+
BASIC_VERSION = %w[
|
10
|
+
1.0
|
11
|
+
2.0
|
12
|
+
].freeze
|
13
|
+
ENCODING_METHOD = %w[
|
14
|
+
hex_mask_enhanced
|
15
|
+
hex_mask_default
|
16
|
+
rle_v
|
17
|
+
rle_h
|
18
|
+
segmental_direct_v
|
19
|
+
segmental_direct_h
|
20
|
+
segmental_data_v
|
21
|
+
segmental_data_h
|
22
|
+
].freeze
|
23
|
+
IMAGE_TYPE = %w[
|
24
|
+
base64
|
25
|
+
bin_magick
|
26
|
+
].freeze
|
27
|
+
OUTPUT_FORMAT = %w[
|
28
|
+
bas
|
29
|
+
].freeze
|
30
|
+
|
31
|
+
X_OFFSET_MIN = -120
|
32
|
+
X_OFFSET_MAX = 120
|
33
|
+
Y_OFFSET_MIN = -64
|
34
|
+
Y_OFFSET_MAX = 64
|
35
|
+
|
36
|
+
LINE_OFFSET_MIN = 1
|
37
|
+
LINE_OFFSET_MAX = 8000 # 8191 is the max. supported by the MK90 BASIC
|
38
|
+
|
39
|
+
LINE_STEP_MIN = 1
|
40
|
+
LINE_STEP_MAX = 100
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ImgToScript
|
4
|
+
module MK90ClientAPI
|
5
|
+
#
|
6
|
+
# Gem's autoloader.
|
7
|
+
#
|
8
|
+
class Autoloader
|
9
|
+
class << self
|
10
|
+
def setup
|
11
|
+
loader = Zeitwerk::Loader.new
|
12
|
+
loader.push_dir(Pathname(__dir__).join("../../")) # lib
|
13
|
+
loader.inflector.inflect(
|
14
|
+
"mk90_client_api" => "MK90ClientAPI"
|
15
|
+
)
|
16
|
+
loader.setup
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ImgToScript
|
4
|
+
module MK90ClientAPI
|
5
|
+
#
|
6
|
+
# Sets rules for the formatter options input data.
|
7
|
+
#
|
8
|
+
class FormatterOptionsContract < AppContract
|
9
|
+
json do
|
10
|
+
optional(:line_offset).value(
|
11
|
+
:integer,
|
12
|
+
gteq?: AllowedInput::LINE_OFFSET_MIN,
|
13
|
+
lteq?: AllowedInput::LINE_OFFSET_MAX
|
14
|
+
)
|
15
|
+
optional(:line_step).value(
|
16
|
+
:integer,
|
17
|
+
gteq?: AllowedInput::LINE_STEP_MIN,
|
18
|
+
lteq?: AllowedInput::LINE_STEP_MAX
|
19
|
+
)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ImgToScript
|
4
|
+
module MK90ClientAPI
|
5
|
+
#
|
6
|
+
# Sets rules for the generator options input data.
|
7
|
+
#
|
8
|
+
class GeneratorOptionsContract < AppContract
|
9
|
+
json do
|
10
|
+
optional(:x_offset).value(
|
11
|
+
:integer,
|
12
|
+
gteq?: AllowedInput::X_OFFSET_MIN,
|
13
|
+
lteq?: AllowedInput::X_OFFSET_MAX
|
14
|
+
)
|
15
|
+
optional(:y_offset).value(
|
16
|
+
:integer,
|
17
|
+
gteq?: AllowedInput::Y_OFFSET_MIN,
|
18
|
+
lteq?: AllowedInput::Y_OFFSET_MAX
|
19
|
+
)
|
20
|
+
optional(:clear_screen).value(:bool)
|
21
|
+
optional(:pause_program).value(:bool)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ImgToScript
|
4
|
+
module MK90ClientAPI
|
5
|
+
#
|
6
|
+
# Resizes the image and converts to binary.
|
7
|
+
#
|
8
|
+
class ImageProcessor
|
9
|
+
#
|
10
|
+
# Call image processor.
|
11
|
+
#
|
12
|
+
# @param [Magick::BinMagick::Image] image
|
13
|
+
# @param [Hash{ Symbol => Object }] kwargs
|
14
|
+
#
|
15
|
+
# @return [Magick::BinMagick::Image] @image
|
16
|
+
#
|
17
|
+
def call(image:, **kwargs)
|
18
|
+
_call(
|
19
|
+
image: image,
|
20
|
+
scr_width: ImgToScript::Languages::MK90Basic::SCR_WIDTH,
|
21
|
+
scr_height: ImgToScript::Languages::MK90Basic::SCR_HEIGHT,
|
22
|
+
**kwargs
|
23
|
+
)
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
#
|
29
|
+
# Call image processor.
|
30
|
+
#
|
31
|
+
# @param [Magick::BinMagick::Image] image
|
32
|
+
# @param [Integer] scr_width
|
33
|
+
# @param [Integer] scr_height
|
34
|
+
#
|
35
|
+
# @return [Magick::BinMagick::Image] @image
|
36
|
+
#
|
37
|
+
def _call(image:, scr_width:, scr_height:, **kwargs)
|
38
|
+
@image = image
|
39
|
+
|
40
|
+
_resize_image(scr_width, scr_height) if @image.oversize?(scr_width, scr_height)
|
41
|
+
_to_binary(**kwargs) unless @image.binary?
|
42
|
+
|
43
|
+
@image
|
44
|
+
end
|
45
|
+
|
46
|
+
#
|
47
|
+
# Forcibly resize the image if it doesn't fit to the device's screen resolution.
|
48
|
+
#
|
49
|
+
def _resize_image(width, height)
|
50
|
+
@image.fit_to_size!(width, height)
|
51
|
+
end
|
52
|
+
|
53
|
+
#
|
54
|
+
# Forcibly convert to binary.
|
55
|
+
#
|
56
|
+
def _to_binary(**kwargs)
|
57
|
+
if kwargs[:to_binary_params]
|
58
|
+
# Convert with custom params [n_gray_colors, quantize_dither, threshold_map]
|
59
|
+
@image.to_binary!(kwargs[:to_binary_params])
|
60
|
+
else
|
61
|
+
# Convert with default params.
|
62
|
+
@image.to_binary!
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ImgToScript
|
4
|
+
module MK90ClientAPI
|
5
|
+
#
|
6
|
+
# Sets rules for the query input data.
|
7
|
+
#
|
8
|
+
class QueryContract < AppContract
|
9
|
+
json do
|
10
|
+
required(:basic_version).value(:string, included_in?: AllowedInput::BASIC_VERSION)
|
11
|
+
required(:encoding_method).value(:string, included_in?: AllowedInput::ENCODING_METHOD)
|
12
|
+
required(:image).value(:string)
|
13
|
+
required(:output_format).value(:string, included_in?: AllowedInput::OUTPUT_FORMAT)
|
14
|
+
|
15
|
+
optional(:generator_options).hash(GeneratorOptionsContract.schema)
|
16
|
+
optional(:formatter_options).hash(FormatterOptionsContract.schema)
|
17
|
+
end
|
18
|
+
|
19
|
+
rule(:image) do
|
20
|
+
key.failure("not a valid base64 string") unless _base64?(value)
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def _base64?(value)
|
26
|
+
value.is_a?(String) && Base64.strict_encode64(Base64.decode64(value)) == value
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,193 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ImgToScript
|
4
|
+
module MK90ClientAPI
|
5
|
+
#
|
6
|
+
# Handles query.
|
7
|
+
#
|
8
|
+
class QueryHandler
|
9
|
+
#
|
10
|
+
# Handle query:
|
11
|
+
# 1. validate input query;
|
12
|
+
# 2. if query is correct: configure and call an ImgToScript
|
13
|
+
# task instance;
|
14
|
+
# 3. return result of the ImgToScript::Task call.
|
15
|
+
#
|
16
|
+
# @param [Hash{ Symbol => Object }, Hash{ String => Object }] query
|
17
|
+
#
|
18
|
+
# @return [Array<String>]
|
19
|
+
# Generated BASIC script.
|
20
|
+
#
|
21
|
+
def call(query)
|
22
|
+
valid_query = _validate_input(query)
|
23
|
+
|
24
|
+
task = _init_task(valid_query)
|
25
|
+
|
26
|
+
image = _prepare_image(
|
27
|
+
_read_image(valid_query[:image])
|
28
|
+
)
|
29
|
+
|
30
|
+
task.run(
|
31
|
+
image: image,
|
32
|
+
scr_width: ImgToScript::Languages::MK90Basic::SCR_WIDTH,
|
33
|
+
scr_height: ImgToScript::Languages::MK90Basic::SCR_HEIGHT
|
34
|
+
)
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
#
|
40
|
+
# Validate input query.
|
41
|
+
#
|
42
|
+
# @param [Hash{ Symbol => Object }, Hash{ String => Object }] query
|
43
|
+
#
|
44
|
+
# @return [Hash{ Symbol => Object }] result.schema_result.output
|
45
|
+
# Validated hash with symbolic keys.
|
46
|
+
#
|
47
|
+
# @raise [ImgToScript::MK90ClientAPI::QueryError]
|
48
|
+
#
|
49
|
+
def _validate_input(query)
|
50
|
+
result = QueryContract.new.call(query)
|
51
|
+
|
52
|
+
raise QueryError, result.errors.to_h.first.join(": ") unless result.success?
|
53
|
+
|
54
|
+
result.schema_result.output
|
55
|
+
end
|
56
|
+
|
57
|
+
#
|
58
|
+
# Read a base64-encoded image from the query.
|
59
|
+
#
|
60
|
+
# @param [String] base64_string
|
61
|
+
#
|
62
|
+
# @return [BinMagick::Image]
|
63
|
+
#
|
64
|
+
# @raise [ImgToScript::MK90ClientAPI::InvalidImage]
|
65
|
+
# Invalid image: the image can't be decoded from the base64 string.
|
66
|
+
#
|
67
|
+
def _read_image(base64_string)
|
68
|
+
_base64_to_img(base64_string)
|
69
|
+
rescue StandardError => e
|
70
|
+
raise InvalidImage, e.message
|
71
|
+
end
|
72
|
+
|
73
|
+
#
|
74
|
+
# Convert a base64-encoded string into a BinMagick image object.
|
75
|
+
#
|
76
|
+
# @param [String] base64_str
|
77
|
+
#
|
78
|
+
# @return [Magick::BinMagick::Image]
|
79
|
+
#
|
80
|
+
def _base64_to_img(base64_str)
|
81
|
+
Magick::BinMagick::Image.new(
|
82
|
+
Magick::Image.read_inline(base64_str).first
|
83
|
+
)
|
84
|
+
end
|
85
|
+
|
86
|
+
#
|
87
|
+
# Prepare the image: resize and convert to binary.
|
88
|
+
#
|
89
|
+
# @param [Magick::BinMagick::Image] image
|
90
|
+
# Original image.
|
91
|
+
#
|
92
|
+
# @return [Magick::BinMagick::Image]
|
93
|
+
# Edited image.
|
94
|
+
#
|
95
|
+
def _prepare_image(image)
|
96
|
+
ImageProcessor.new.call(image: image)
|
97
|
+
end
|
98
|
+
|
99
|
+
#
|
100
|
+
# Init. and configure a comvertion task.
|
101
|
+
#
|
102
|
+
# @param [Hash{ Symbol => Object }] query
|
103
|
+
#
|
104
|
+
# @return [ImgToScript::Task] task
|
105
|
+
# Configured task object.
|
106
|
+
#
|
107
|
+
def _init_task(query)
|
108
|
+
@generator = _init_generator(query[:encoding_method])
|
109
|
+
translator = _init_translator(query[:basic_version])
|
110
|
+
@formatter = _init_formatter
|
111
|
+
|
112
|
+
_configure_generator(query[:generator_options]) if query[:generator_options]
|
113
|
+
_configure_formatter(query[:formatter_options]) if query[:formatter_options]
|
114
|
+
|
115
|
+
ImgToScript::Task.new(
|
116
|
+
generator: @generator,
|
117
|
+
translator: translator,
|
118
|
+
formatter: @formatter
|
119
|
+
)
|
120
|
+
end
|
121
|
+
|
122
|
+
#
|
123
|
+
# Init. generator object
|
124
|
+
#
|
125
|
+
# @param [String] name
|
126
|
+
#
|
127
|
+
# @return [Object]
|
128
|
+
#
|
129
|
+
def _init_generator(name)
|
130
|
+
case name
|
131
|
+
when "hex_mask_enhanced"
|
132
|
+
ImgToScript::Generators::HexMask::Enhanced.new
|
133
|
+
when "hex_mask_default"
|
134
|
+
ImgToScript::Generators::HexMask::Default.new
|
135
|
+
when "rle_v"
|
136
|
+
ImgToScript::Generators::RunLengthEncoding::Vertical.new
|
137
|
+
when "rle_h"
|
138
|
+
ImgToScript::Generators::RunLengthEncoding::Horizontal.new
|
139
|
+
when "segmental_direct_v"
|
140
|
+
ImgToScript::Generators::Segmental::DirectDraw::Vertical.new
|
141
|
+
when "segmental_direct_h"
|
142
|
+
ImgToScript::Generators::Segmental::DirectDraw::Horizontal.new
|
143
|
+
when "segmental_data_v"
|
144
|
+
ImgToScript::Generators::Segmental::DataReadDraw::Vertical.new
|
145
|
+
when "segmental_data_h"
|
146
|
+
ImgToScript::Generators::Segmental::DataReadDraw::Horizontal.new
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
#
|
151
|
+
# Init. translator object
|
152
|
+
#
|
153
|
+
# @param [String] basic_version
|
154
|
+
#
|
155
|
+
# @return [Object]
|
156
|
+
#
|
157
|
+
def _init_translator(basic_version)
|
158
|
+
case basic_version
|
159
|
+
when "1.0"
|
160
|
+
ImgToScript::Languages::MK90Basic::Translators::MK90Basic10.new
|
161
|
+
when "2.0"
|
162
|
+
ImgToScript::Languages::MK90Basic::Translators::MK90Basic20.new
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
def _init_formatter
|
167
|
+
ImgToScript::Languages::MK90Basic::Formatters::Minificator.new
|
168
|
+
end
|
169
|
+
|
170
|
+
#
|
171
|
+
# Configure @generator object.
|
172
|
+
#
|
173
|
+
# @param [Hash{ Symbol => Object }] params
|
174
|
+
#
|
175
|
+
def _configure_generator(params)
|
176
|
+
@generator.configure do |config|
|
177
|
+
params.each { |key, value| config.send("#{key}=", value) }
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
#
|
182
|
+
# Configure @formatter object.
|
183
|
+
#
|
184
|
+
# @param [Hash{ Symbol => Object }] params
|
185
|
+
#
|
186
|
+
def _configure_formatter(params)
|
187
|
+
@formatter.configure do |config|
|
188
|
+
params.each { |key, value| config.send("#{key}=", value) }
|
189
|
+
end
|
190
|
+
end
|
191
|
+
end
|
192
|
+
end
|
193
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "base64"
|
4
|
+
require "dry/validation"
|
5
|
+
require "img_to_script"
|
6
|
+
require "rmagick/bin_magick"
|
7
|
+
require "zeitwerk"
|
8
|
+
|
9
|
+
require_relative "mk90_client_api/autoloader"
|
10
|
+
require_relative "mk90_client_api/version"
|
11
|
+
|
12
|
+
ImgToScript::MK90ClientAPI::Autoloader.setup
|
13
|
+
|
14
|
+
module ImgToScript
|
15
|
+
#
|
16
|
+
# Provides an API between an img_to_mk90_bas client app,
|
17
|
+
# and the img_to_script lib.
|
18
|
+
#
|
19
|
+
module MK90ClientAPI
|
20
|
+
class Error < StandardError; end
|
21
|
+
|
22
|
+
class QueryError < Error; end
|
23
|
+
|
24
|
+
class InvalidImage < Error; end
|
25
|
+
|
26
|
+
#
|
27
|
+
# Generate BASIC program by calling the img_to_script gem.
|
28
|
+
#
|
29
|
+
# @param [Hash{ Symbol => Object}, Hash{ String => Object }] query
|
30
|
+
#
|
31
|
+
# @return [Array<String>]
|
32
|
+
#
|
33
|
+
def self.call(query)
|
34
|
+
QueryHandler.new.call(query)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
metadata
ADDED
@@ -0,0 +1,165 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: img_to_script-mk90_client_api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- 8bit-m8
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-12-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: dry-schema
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.13'
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 1.13.3
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - "~>"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.13'
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 1.13.3
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: dry-validation
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '1.10'
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: 1.10.0
|
43
|
+
type: :runtime
|
44
|
+
prerelease: false
|
45
|
+
version_requirements: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - "~>"
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '1.10'
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: 1.10.0
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
name: img_to_script
|
55
|
+
requirement: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - "~>"
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '1.0'
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: 1.0.0
|
63
|
+
type: :runtime
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - "~>"
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '1.0'
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: 1.0.0
|
73
|
+
- !ruby/object:Gem::Dependency
|
74
|
+
name: rmagick-bin_magick
|
75
|
+
requirement: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - "~>"
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0.2'
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.2.0
|
83
|
+
type: :runtime
|
84
|
+
prerelease: false
|
85
|
+
version_requirements: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0.2'
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: 0.2.0
|
93
|
+
- !ruby/object:Gem::Dependency
|
94
|
+
name: zeitwerk
|
95
|
+
requirement: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - "~>"
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '2.6'
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: 2.6.12
|
103
|
+
type: :runtime
|
104
|
+
prerelease: false
|
105
|
+
version_requirements: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - "~>"
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '2.6'
|
110
|
+
- - ">="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: 2.6.12
|
113
|
+
description:
|
114
|
+
email:
|
115
|
+
- you@example.com
|
116
|
+
executables: []
|
117
|
+
extensions: []
|
118
|
+
extra_rdoc_files: []
|
119
|
+
files:
|
120
|
+
- ".rubocop.yml"
|
121
|
+
- ".vscode/launch.json"
|
122
|
+
- CHANGELOG.md
|
123
|
+
- LICENSE.txt
|
124
|
+
- README.md
|
125
|
+
- Rakefile
|
126
|
+
- img_to_script-mk90_client_api.gemspec
|
127
|
+
- lib/img_to_script/mk90_client_api.rb
|
128
|
+
- lib/img_to_script/mk90_client_api/allowed_input.rb
|
129
|
+
- lib/img_to_script/mk90_client_api/app_contract.rb
|
130
|
+
- lib/img_to_script/mk90_client_api/autoloader.rb
|
131
|
+
- lib/img_to_script/mk90_client_api/formatter_options_contract.rb
|
132
|
+
- lib/img_to_script/mk90_client_api/generator_options_contract.rb
|
133
|
+
- lib/img_to_script/mk90_client_api/image_processor.rb
|
134
|
+
- lib/img_to_script/mk90_client_api/query_contract.rb
|
135
|
+
- lib/img_to_script/mk90_client_api/query_handler.rb
|
136
|
+
- lib/img_to_script/mk90_client_api/version.rb
|
137
|
+
- sig/img_to_script/mk90_client_api.rbs
|
138
|
+
homepage: https://github.com/8bit-mate/img_to_script-mk90_client_api.rb
|
139
|
+
licenses:
|
140
|
+
- MIT
|
141
|
+
metadata:
|
142
|
+
allowed_push_host: https://rubygems.org
|
143
|
+
homepage_uri: https://github.com/8bit-mate/img_to_script-mk90_client_api.rb
|
144
|
+
source_code_uri: https://github.com/8bit-mate/img_to_script-mk90_client_api.rb
|
145
|
+
changelog_uri: https://github.com/8bit-mate/img_to_script-mk90_client_api.rb
|
146
|
+
post_install_message:
|
147
|
+
rdoc_options: []
|
148
|
+
require_paths:
|
149
|
+
- lib
|
150
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
151
|
+
requirements:
|
152
|
+
- - ">="
|
153
|
+
- !ruby/object:Gem::Version
|
154
|
+
version: 3.0.0
|
155
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - ">="
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: '0'
|
160
|
+
requirements: []
|
161
|
+
rubygems_version: 3.4.10
|
162
|
+
signing_key:
|
163
|
+
specification_version: 4
|
164
|
+
summary: Provides an API between a client app and the img_to_script gem.
|
165
|
+
test_files: []
|