distorted 0.6.0 → 0.7.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.
- checksums.yaml +4 -4
- data/README.md +2 -2
- data/bin/console +14 -0
- data/bin/distorted +6 -0
- data/bin/setup +8 -0
- data/lib/distorted.rb +2 -0
- data/lib/distorted/checking_you_out.rb +116 -13
- data/lib/distorted/{types → checking_you_out}/README +0 -0
- data/lib/distorted/checking_you_out/application.yaml +33 -0
- data/lib/distorted/{types → checking_you_out}/font.yaml +0 -0
- data/lib/distorted/checking_you_out/image.yaml +108 -0
- data/lib/distorted/click_again.rb +333 -0
- data/lib/distorted/element_of_media.rb +2 -0
- data/lib/distorted/element_of_media/change.rb +119 -0
- data/lib/distorted/element_of_media/compound.rb +120 -0
- data/lib/distorted/floor.rb +17 -0
- data/lib/distorted/invoker.rb +97 -0
- data/lib/distorted/media_molecule.rb +58 -0
- data/lib/distorted/media_molecule/font.rb +195 -0
- data/lib/distorted/media_molecule/image.rb +33 -0
- data/lib/distorted/media_molecule/pdf.rb +44 -0
- data/lib/distorted/media_molecule/svg.rb +45 -0
- data/lib/distorted/media_molecule/text.rb +203 -0
- data/lib/distorted/media_molecule/video.rb +18 -0
- data/lib/distorted/modular_technology/gstreamer.rb +174 -0
- data/lib/distorted/modular_technology/vips.rb +4 -4
- data/lib/distorted/modular_technology/vips/foreign.rb +489 -0
- data/lib/distorted/modular_technology/vips/load.rb +133 -0
- data/lib/distorted/modular_technology/{vips_save.rb → vips/save.rb} +23 -34
- data/lib/distorted/monkey_business/encoding.rb +317 -0
- data/lib/distorted/monkey_business/hash.rb +0 -15
- data/lib/distorted/{modular_technology/triple_counter.rb → triple_counter.rb} +8 -1
- data/lib/distorted/version.rb +16 -16
- metadata +59 -46
- data/lib/distorted/injection_of_love.rb +0 -247
- data/lib/distorted/modular_technology/vips_load.rb +0 -77
- data/lib/distorted/molecule/C18H27NO3.rb +0 -10
- data/lib/distorted/molecule/font.rb +0 -198
- data/lib/distorted/molecule/image.rb +0 -36
- data/lib/distorted/molecule/pdf.rb +0 -119
- data/lib/distorted/molecule/svg.rb +0 -60
- data/lib/distorted/molecule/text.rb +0 -225
- data/lib/distorted/molecule/video.rb +0 -195
- data/lib/distorted/monkey_business/mnemoniq.rb +0 -8
- data/lib/distorted/types/application.yaml +0 -8
@@ -2,8 +2,8 @@ require 'set'
|
|
2
2
|
|
3
3
|
require 'distorted/checking_you_out'
|
4
4
|
|
5
|
-
require 'distorted/modular_technology/
|
6
|
-
require 'distorted/modular_technology/
|
5
|
+
require 'distorted/modular_technology/vips/load'
|
6
|
+
require 'distorted/modular_technology/vips/save'
|
7
7
|
|
8
8
|
|
9
9
|
module Cooltrainer; end
|
@@ -11,7 +11,7 @@ module Cooltrainer::DistorteD; end
|
|
11
11
|
module Cooltrainer::DistorteD::Technology; end
|
12
12
|
module Cooltrainer::DistorteD::Technology::Vips
|
13
13
|
|
14
|
-
include Cooltrainer::DistorteD::Technology::
|
15
|
-
include Cooltrainer::DistorteD::Technology::
|
14
|
+
include Cooltrainer::DistorteD::Technology::Vips::Save
|
15
|
+
include Cooltrainer::DistorteD::Technology::Vips::Load
|
16
16
|
|
17
17
|
end
|
@@ -0,0 +1,489 @@
|
|
1
|
+
require 'vips'
|
2
|
+
|
3
|
+
require 'distorted/checking_you_out'
|
4
|
+
require 'distorted/element_of_media'
|
5
|
+
|
6
|
+
# Based on https://github.com/libvips/ruby-vips/issues/186#issuecomment-433691412
|
7
|
+
module Vips
|
8
|
+
attach_function :vips_class_find, [:string, :string], :pointer
|
9
|
+
attach_function :vips_object_summary_class, [:pointer, :pointer], :void
|
10
|
+
|
11
|
+
class BufStruct < FFI::Struct
|
12
|
+
layout :base, :pointer,
|
13
|
+
:mx, :int,
|
14
|
+
:i, :int,
|
15
|
+
:full, :bool,
|
16
|
+
:lasti, :int,
|
17
|
+
:dynamic, :bool
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
module GObject
|
23
|
+
# Fundamental types not already defined in ruby-vips' `lib/vips.rb`
|
24
|
+
GBOXED_TYPE = g_type_from_name('GBoxed')
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
module Cooltrainer; end
|
29
|
+
module Cooltrainer::DistorteD; end
|
30
|
+
module Cooltrainer::DistorteD::Technology; end
|
31
|
+
module Cooltrainer::DistorteD::Technology::Vips
|
32
|
+
|
33
|
+
|
34
|
+
# 🄵🄸🄽🄳 🅃🄷🄴 🄲🄾🄼🄿🅄🅃🄴🅁 🅁🄾🄾🄼
|
35
|
+
# 🄵🄸🄽🄳 🅃🄷🄴 🄲🄾🄼🄿🅄🅃🄴🅁 🅁🄾🄾🄼
|
36
|
+
# 🄵🄸🄽🄳 🅃🄷🄴 🄲🄾🄼🄿🅄🅃🄴🅁 🅁🄾🄾🄼
|
37
|
+
Vips::vips_vector_set_enabled(1)
|
38
|
+
|
39
|
+
|
40
|
+
# All of the actual Loader/Saver classes we need to interact with
|
41
|
+
# will be tree children of one of these top-level class categories:
|
42
|
+
TOP_LEVEL_LOADER = :VipsForeignLoad
|
43
|
+
TOP_LEVEL_SAVER = :VipsForeignSave
|
44
|
+
|
45
|
+
|
46
|
+
# This has got to be built in to Ruby-GLib somewhere, right?
|
47
|
+
# Remove this if an FFI method is possible to get this mapping.
|
48
|
+
G_TYPE_VALUES = {
|
49
|
+
:gboolean => [false, true],
|
50
|
+
:gchararray => String,
|
51
|
+
:gdouble => Float,
|
52
|
+
:gint => Integer,
|
53
|
+
}
|
54
|
+
|
55
|
+
# Aliases we want to support for consistency and accessibility.
|
56
|
+
VIPS_ALIASES = {
|
57
|
+
:Q => Set[:Q, :quality],
|
58
|
+
:colours => Set[:colours, :colors],
|
59
|
+
:centre => Set[:centre, :center], # America; FUCK YEAH!
|
60
|
+
}
|
61
|
+
|
62
|
+
# GEnum valid values are detectable, but I don't know how to do the same
|
63
|
+
# for the numeric parameters. Specify them here manually for now.
|
64
|
+
VIPS_VALID = {
|
65
|
+
:"page-height" => (0..Vips::MAX_COORD),
|
66
|
+
:"quant-table" => (0..8),
|
67
|
+
:Q => (0..100),
|
68
|
+
:colours => (2..256),
|
69
|
+
:dither => (0.0..1.0),
|
70
|
+
:compression => (0..9),
|
71
|
+
:"alpha-q" => (0..100),
|
72
|
+
:"reduction-effort" => (0..6),
|
73
|
+
:kmin => (0..0x7FFFFFFF), # https://en.wikipedia.org/wiki/2,147,483,647
|
74
|
+
:kmax => (0..0x7FFFFFFF),
|
75
|
+
:"tile-width" => (0..0x8000), # 32768
|
76
|
+
:"tile-height" => (0..0x8000),
|
77
|
+
:xres => (0.001..1e+06),
|
78
|
+
:yres => (0.001..1e+06),
|
79
|
+
}
|
80
|
+
|
81
|
+
# Same with default values for numeric parameters.
|
82
|
+
VIPS_DEFAULT = {
|
83
|
+
:Q => 75,
|
84
|
+
:colours => 256,
|
85
|
+
:compression => 6,
|
86
|
+
:"alpha-q" => 100,
|
87
|
+
:"reduction-effort" => 4,
|
88
|
+
:kmin => 0x7FFFFFFF - 1,
|
89
|
+
:kmax => 0x7FFFFFFF,
|
90
|
+
:"tile-width" => 128,
|
91
|
+
:"tile-height" => 128,
|
92
|
+
:xres => 1,
|
93
|
+
:yres => 1,
|
94
|
+
}
|
95
|
+
|
96
|
+
|
97
|
+
# Store FFI results where possible to minimize memory churn 'n' general fragility.
|
98
|
+
@@vips_foreign_types = Hash[]
|
99
|
+
@@vips_foreign_suffixes = Hash[]
|
100
|
+
@@vips_foreign_options = Hash[]
|
101
|
+
|
102
|
+
|
103
|
+
# Returns a String libvips Loader class name most appropriate for the given filename suffix.
|
104
|
+
# This is a workaround for the fact that the built-in Vips::vips_foreign_find_load
|
105
|
+
# requires access of a real image file, and we are here talking only of hypothetical ones.
|
106
|
+
# See this method's call site in 'vips/load' for more detailed comments on this.
|
107
|
+
#
|
108
|
+
# irb(main):234:0> Vips::vips_filename_get_filename('fart.jpg')
|
109
|
+
# => #<FFI::Pointer address=0x0000561efe3d08e0>
|
110
|
+
# irb(main):235:0> Vips::p2str(Vips::vips_filename_get_filename('fart.jpg'))
|
111
|
+
# => "fart.jpg"
|
112
|
+
# irb(main):236:0> File.extname(Vips::p2str(Vips::vips_filename_get_filename('fart.jpg')))
|
113
|
+
# => ".jpg"
|
114
|
+
# irb(main):237:0> Vips::vips_foreign_find_save(File.extname(Vips::p2str(Vips::vips_filename_get_filename('fart.jpg'))))
|
115
|
+
# => "VipsForeignSaveJpegFile"
|
116
|
+
#
|
117
|
+
# Here are the available Operations I have on my laptop with libvips 8.8:
|
118
|
+
# [okeeblow@emi#okeeblow] vips -l|grep VipsForeign|grep File
|
119
|
+
# VipsForeignLoadPdfFile (pdfload), load PDF with libpoppler (.pdf), priority=0, is_a, get_flags, get_flags_filename, header, load
|
120
|
+
# VipsForeignLoadSvgFile (svgload), load SVG with rsvg (.svg, .svgz, .svg.gz), priority=0, is_a, get_flags, get_flags_filename, header, load
|
121
|
+
# VipsForeignLoadGifFile (gifload), load GIF with giflib (.gif), priority=0, is_a, get_flags, get_flags_filename, header, load
|
122
|
+
# VipsForeignLoadJpegFile (jpegload), load jpeg from file (.jpg, .jpeg, .jpe), priority=50, is_a, get_flags, header, load
|
123
|
+
# VipsForeignLoadWebpFile (webpload), load webp from file (.webp), priority=0, is_a, get_flags, get_flags_filename, header, load
|
124
|
+
# VipsForeignLoadTiffFile (tiffload), load tiff from file (.tif, .tiff), priority=50, is_a, get_flags, get_flags_filename, header, load
|
125
|
+
# VipsForeignLoadMagickFile (magickload), load file with ImageMagick, priority=-100, is_a, get_flags, get_flags_filename, header
|
126
|
+
# VipsForeignSaveRadFile (radsave), save image to Radiance file (.hdr), priority=0, rgb
|
127
|
+
# VipsForeignSaveDzFile (dzsave), save image to deepzoom file (.dz), priority=0, any
|
128
|
+
# VipsForeignSavePngFile (pngsave), save image to png file (.png), priority=0, rgba
|
129
|
+
# VipsForeignSaveJpegFile (jpegsave), save image to jpeg file (.jpg, .jpeg, .jpe), priority=0, rgb-cmyk
|
130
|
+
# VipsForeignSaveWebpFile (webpsave), save image to webp file (.webp), priority=0, rgba-only
|
131
|
+
# VipsForeignSaveTiffFile (tiffsave), save image to tiff file (.tif, .tiff), priority=0, any
|
132
|
+
# VipsForeignSaveMagickFile (magicksave), save file with ImageMagick (.gif, .bmp), priority=-100, any
|
133
|
+
#
|
134
|
+
# You can notice differences such as a `dzsave` and `radsave` but no `dzload` or `radload`.
|
135
|
+
# This is why we can't assume that HAS_SAVER == HAS_LOADER across the board.
|
136
|
+
# Other differences are invisible here, like different formats supported silently by `magickload`,
|
137
|
+
# so that Operation is the catch-all fallback if we don't have any better idea.
|
138
|
+
#
|
139
|
+
# We can try taking a MIME::Type's `sub_type`, capitalizing it, and trying to find a Loader Operation by that name.
|
140
|
+
# irb(main):254:0> MIME::Types::type_for('.heif').last.sub_type.capitalize
|
141
|
+
# => "Heif"
|
142
|
+
# irb(main):255:0> MIME::Types::type_for('.jpg').last.sub_type.capitalize
|
143
|
+
# => "Jpeg"
|
144
|
+
#
|
145
|
+
## NOTE: I'm writing this on an old install that lacks HEIF support in its libvips 8.8 installation,
|
146
|
+
# so this failure to find 'VipsForeignLoadHeifFile' is expected and correct for me!
|
147
|
+
# It probably won't fail for you in the future, but I want to make sure to include
|
148
|
+
# some example of varying library capability and not assume capabilities based on libvips version:
|
149
|
+
#
|
150
|
+
# irb(main):257:0> GObject::g_type_from_name("VipsForeignLoad#{MIME::Types::type_for('.jpg').last.sub_type.capitalize}File")
|
151
|
+
# => 94691057380176
|
152
|
+
# irb(main):258:0> GObject::g_type_from_name("VipsForeignLoad#{MIME::Types::type_for('.heif').last.sub_type.capitalize}File")
|
153
|
+
# => 0
|
154
|
+
def self.vips_foreign_find_load_suffix(filename)
|
155
|
+
suffix = File.extname(Vips::p2str(Vips::vips_filename_get_filename('fart.jpg')))
|
156
|
+
guessed_loader = "VipsForeignLoad#{CHECKING::YOU::OUT(suffix).first.sub_type.capitalize}File"
|
157
|
+
return self::vips_foreign_valid_operation?(guessed_loader) ? guessed_loader : 'VipsForeignLoadMagickFile'.freeze
|
158
|
+
end
|
159
|
+
|
160
|
+
|
161
|
+
# Returns a Set of MIME::Types based on the "supported suffix" lists generated
|
162
|
+
# by libvips and our other functions here in this Module.
|
163
|
+
def self.vips_get_types(basename)
|
164
|
+
@@vips_foreign_types[basename.to_sym] ||= self::vips_get_suffixes(basename).each_with_object(Set[]) { |suffix, types|
|
165
|
+
types.merge(CHECKING::YOU::OUT(suffix))
|
166
|
+
}
|
167
|
+
end
|
168
|
+
|
169
|
+
|
170
|
+
# Returns a Set of String filename suffixes supported by a tree of libvips loader/saver classes.
|
171
|
+
#
|
172
|
+
# The Array returned from self::vips_get_nickname_suffixes will be overloaded
|
173
|
+
# with all duplicate suffix possibilities for each Type according to libvips.
|
174
|
+
# e.g.
|
175
|
+
# This is unrelated to MIME::Type#preferred_extension!!
|
176
|
+
def self.vips_get_suffixes(basename)
|
177
|
+
@@vips_foreign_suffixes[basename.to_sym] ||= self::vips_get_suffixes_per_nickname(basename).values.each_with_object(Set[]) {|s,n| n.merge(s)}
|
178
|
+
end
|
179
|
+
|
180
|
+
|
181
|
+
# Returns a Hash[alias] of Compound attributes supported by a given libvips Loader/Saver class.
|
182
|
+
def self.vips_get_options(nickname)
|
183
|
+
return Hash if nickname.nil?
|
184
|
+
@@vips_foreign_options[nickname.to_sym] ||= self::vips_get_nickname_options(nickname)
|
185
|
+
end
|
186
|
+
|
187
|
+
|
188
|
+
protected
|
189
|
+
|
190
|
+
|
191
|
+
# Returns a Set of local MIME::Types supported by the given class and any of its children.
|
192
|
+
def self.vips_get_types_per_nickname(basename)
|
193
|
+
self::vips_get_suffixes_per_nickname(basename).transform_values{|s| CHECKING::YOU::OUT(s)}
|
194
|
+
end
|
195
|
+
|
196
|
+
# Returns a Hash[Type] of Set[String] class nicknames supporting that Type.
|
197
|
+
def self.vips_get_nicknames_per_type(basename)
|
198
|
+
self::vips_get_nickname_types(basename).each_with_object(Hash.new { |h,k| h[k] = Set[] }) { |(nickname,type_set), memo|
|
199
|
+
type_set.each{ |t|
|
200
|
+
memo[t] << nickname
|
201
|
+
}
|
202
|
+
}
|
203
|
+
end
|
204
|
+
|
205
|
+
# Returns a Hash[String] of Set[String]s containing the
|
206
|
+
# supported MediaType filename suffixes for all child classes of
|
207
|
+
# either VipsForeignSave or VipsForeignLoad.
|
208
|
+
#
|
209
|
+
# This is very similar to the built-in Vips::get_suffixes except
|
210
|
+
# also allows us to directly inspect Loaders — including Magick!
|
211
|
+
#
|
212
|
+
# Previously we had to take the Saver suffixes and just assume each had a matching Loader.
|
213
|
+
# This was very limiting MediaType support since OpenEXR/OpenSlide/Magick-supported
|
214
|
+
# Loader types would not have a Saver suffix and would have no way to be discovered!
|
215
|
+
# This also works around Loader type support bugs, e.g. the Magick-based BMP (MS Bitmap)
|
216
|
+
# Loader was missing prior to libvips version 8.9.1.,
|
217
|
+
# so we can stop checking versions and inserting manual workarounds for those corner cases!
|
218
|
+
#
|
219
|
+
# The FFI buffer reads will leave us with an overloaded Array containing
|
220
|
+
# duplicate suffixes for every supported suffix variation of a given type,
|
221
|
+
# e.g. ['.jpg', '.jpe', '.jpeg', '.png], '.gif', '.tif', '.tiff' … ]
|
222
|
+
def self.vips_get_suffixes_per_nickname(basename)
|
223
|
+
self::vips_get_child_class_nicknames(basename).each_with_object(Hash.new) { |nickname, nickname_suffixes|
|
224
|
+
# "Search below basename, return the first class whose name or nickname matches."
|
225
|
+
# VipsForeign is a basename for savers and loaders alike.
|
226
|
+
foreign_class = Vips::vips_class_find('VipsForeign'.freeze, nickname)
|
227
|
+
next if foreign_class.null?
|
228
|
+
|
229
|
+
buf_struct = Vips::BufStruct.new
|
230
|
+
buf_struct_string = FFI::MemoryPointer.new(:char, 2048)
|
231
|
+
buf_struct[:base] = buf_struct_string
|
232
|
+
buf_struct[:mx] = 2048
|
233
|
+
|
234
|
+
# Load the human-readable class summary into a given buffer.
|
235
|
+
Vips::vips_object_summary_class(foreign_class, buf_struct.pointer)
|
236
|
+
|
237
|
+
class_summary = buf_struct_string.read_string
|
238
|
+
|
239
|
+
suffixes = class_summary.scan(/\.\w+\.?\w+/)
|
240
|
+
nickname_suffixes.update({nickname => suffixes.to_set}) unless suffixes.empty?
|
241
|
+
}
|
242
|
+
end
|
243
|
+
|
244
|
+
# Returns a Set of String class names for libvips' Loaders/Savers.
|
245
|
+
def self.vips_get_child_class_nicknames(basename)
|
246
|
+
nicknames = Set[]
|
247
|
+
generate_class = Proc.new{ |gtype|
|
248
|
+
nickname = Vips::nickname_find(gtype)
|
249
|
+
nicknames << nickname if nickname
|
250
|
+
|
251
|
+
# https://libvips.github.io/libvips/API/current/VipsObject.html#vips-type-map
|
252
|
+
# "Map over a type's children. Stop when fn returns non-nil and return that value."
|
253
|
+
Vips::vips_type_map(gtype, generate_class, nil)
|
254
|
+
}
|
255
|
+
generate_class.call(GObject::g_type_from_name(basename))
|
256
|
+
nicknames
|
257
|
+
end
|
258
|
+
|
259
|
+
# Returns a Hash[alias] of attribute Compounds for every optional attribute of a libvips Loader/Saver class.
|
260
|
+
#
|
261
|
+
# The discarded 'required' attributes are things like filenames that we will handle ourselves in DD.
|
262
|
+
# irb> Vips::Introspect.get('jpegload').required_input
|
263
|
+
# => [{:arg_name=>"filename", :flags=>19, :gtype=>64}]
|
264
|
+
# irb> Vips::Introspect.new('jpegload').required_output
|
265
|
+
# => [{:arg_name=>"out", :flags=>35, :gtype=>94062772794288}]
|
266
|
+
#
|
267
|
+
## Example using :argument_map:
|
268
|
+
# irb> Vips::Operation.new('gifload').argument_map{|a,b,c| p "#{a[:name]} — #{a[:value_type]} — #{GObject::g_type_name(a[:value_type])}"}
|
269
|
+
# "filename — 64 — gchararray"
|
270
|
+
# "nickname — 64 — gchararray"
|
271
|
+
# "out — 94691057294304 — VipsImage"
|
272
|
+
# "description — 64 — gchararray"
|
273
|
+
# "page — 24 — gint"
|
274
|
+
# "n — 24 — gint"
|
275
|
+
# "flags — 94691059531296 — VipsForeignFlags"
|
276
|
+
# "memory — 20 — gboolean"
|
277
|
+
# "access — 94691057417952 — VipsAccess"
|
278
|
+
# "sequential — 20 — gboolean"
|
279
|
+
# "fail — 20 — gboolean"
|
280
|
+
# "disc — 20 — gboolean"
|
281
|
+
#
|
282
|
+
## Descriptions are obtained by passing the complete pspec to g_param_get_blurb:
|
283
|
+
# Example:
|
284
|
+
# irb> Vips::Operation.new('openexrload').argument_map{|a,b,c| p GObject::g_param_spec_get_blurb(a)}
|
285
|
+
# "Filename to load from"
|
286
|
+
# "Class nickname"
|
287
|
+
# "Output image"
|
288
|
+
# "Class description"
|
289
|
+
# "Flags for this file"
|
290
|
+
# "Force open via memory"
|
291
|
+
# "Required access pattern for this file"
|
292
|
+
# "Sequential read only"
|
293
|
+
# "Fail on first error"
|
294
|
+
# "Open to disc"
|
295
|
+
def self.vips_get_nickname_options(nickname)
|
296
|
+
options = Hash[]
|
297
|
+
Vips::Operation.new(nickname).argument_map{ |param_spec, argument_class, _argument_instance|
|
298
|
+
flags = argument_class[:flags]
|
299
|
+
if (flags & Vips::ARGUMENT_INPUT) != 0 # We only want "input" arguments
|
300
|
+
# …and we also only want optional non-deprecated arguments.
|
301
|
+
if (flags & Vips::ARGUMENT_REQUIRED) == 0 && (flags & Vips::ARGUMENT_DEPRECATED) == 0
|
302
|
+
# ParameterSpec name will be a String e.g. 'Q' or 'interlace' or 'page-height'
|
303
|
+
element = param_spec[:name].to_sym
|
304
|
+
|
305
|
+
# `magicksave` takes an argument `format` to choose one of its many supported types,
|
306
|
+
# but that selection in DistorteD-land is via our MIME::Types, so this option should be dropped.
|
307
|
+
# https://github.com/libvips/libvips/blob/4de9b56725862edf872ae503a3dfb4cf05da9e77/libvips/foreign/magicksave.c#L455~L460
|
308
|
+
next if element == :format
|
309
|
+
|
310
|
+
# GObject::g_type_name will return `nil` for an invalid :value_type,
|
311
|
+
# but these are coming straight from libvips so we know they're fine.
|
312
|
+
gtype_name = GObject::g_type_name(param_spec[:value_type]).to_sym
|
313
|
+
|
314
|
+
# Support aliasing options like 'Q' into 'quality' for consistency
|
315
|
+
# and 'colours' into 'colors' for accessibility.
|
316
|
+
isotopes = VIPS_ALIASES.dig(element) || Set[element]
|
317
|
+
|
318
|
+
# Keyword arguments to splat into our Compound
|
319
|
+
attributes = {
|
320
|
+
# Some libvips drivers seem to have mixed-leading-case options,
|
321
|
+
# like ppmsave and webp save for example:
|
322
|
+
# https://github.com/libvips/libvips/blob/4de9b56725862edf872ae503a3dfb4cf05da9e77/libvips/foreign/ppmsave.c#L396~L415
|
323
|
+
# https://github.com/libvips/libvips/blob/4de9b56725862edf872ae503a3dfb4cf05da9e77/libvips/foreign/webpsave.c#L152
|
324
|
+
# TODO: Inventory all of these and submit an upstream patch to capitaqlize them consistently.
|
325
|
+
# Until them (and for old versions), fix up the first letter manually.
|
326
|
+
# Avoid using just `blurb.capitalize` as that will lowercase everything after
|
327
|
+
# the first character, which is definitely worse than what I'm trying to fix lol
|
328
|
+
:blurb => GObject::g_param_spec_get_blurb(param_spec).tap{|blurb| blurb[0] = blurb[0].capitalize},
|
329
|
+
:default => self::vips_get_option_default(param_spec[:value_type]),
|
330
|
+
}
|
331
|
+
if GObject::g_type_fundamental(param_spec[:value_type]) == GObject::GENUM_TYPE
|
332
|
+
attributes[:valid] = self::vips_get_enum_values(param_spec[:value_type])
|
333
|
+
elsif VIPS_VALID.has_key?(element)
|
334
|
+
attributes[:valid] = VIPS_VALID[element]
|
335
|
+
elsif G_TYPE_VALUES.has_key?(gtype_name)
|
336
|
+
attributes[:valid] = G_TYPE_VALUES[gtype_name]
|
337
|
+
end
|
338
|
+
|
339
|
+
# Add the Compound for every alias
|
340
|
+
compound = Cooltrainer::Compound.new(isotopes, **attributes)
|
341
|
+
isotopes.each{ |isotope|
|
342
|
+
options.store(isotope, compound)
|
343
|
+
}
|
344
|
+
end
|
345
|
+
end
|
346
|
+
}
|
347
|
+
|
348
|
+
# This isn't really a 'Saver' Option — rather an argument to a separate
|
349
|
+
# :smartcrop or :thumbnail VIPS method we can call, but I want to offer
|
350
|
+
# this option on every Type and use it to control the method we call
|
351
|
+
# to write the image.
|
352
|
+
options.store(:crop, Cooltrainer::Compound.new(:crop,
|
353
|
+
blurb: 'Visual cropping method',
|
354
|
+
valid: self::vips_get_enum_values('VipsInteresting'.freeze),
|
355
|
+
default: self::vips_get_option_default('VipsInteresting'.freeze),
|
356
|
+
)) if nickname.include?('Save'.freeze) # Only for savers!!
|
357
|
+
|
358
|
+
# All done :)
|
359
|
+
options
|
360
|
+
end
|
361
|
+
|
362
|
+
|
363
|
+
# Returns the default value for any ruby-vips GObject::GValue
|
364
|
+
# based on its fundamental GType.
|
365
|
+
def self.vips_get_option_default(gtype)
|
366
|
+
gtype_id = gtype.is_a?(String) ? GObject::g_type_from_name(gtype) : gtype
|
367
|
+
# The `enum` method will actually work for several of these types,
|
368
|
+
# e.g. returns `false` for GBool, but let's skip it to avoid the whole,
|
369
|
+
# like, FFI/allocation thing.
|
370
|
+
case GObject::g_type_fundamental(gtype_id)
|
371
|
+
when GObject::GENUM_TYPE
|
372
|
+
return self.vips_get_enum_default(gtype_id)
|
373
|
+
when GObject::GBOOL_TYPE
|
374
|
+
return false
|
375
|
+
when GObject::GDOUBLE_TYPE
|
376
|
+
return 0.0
|
377
|
+
when GObject::GINT_TYPE
|
378
|
+
return 0
|
379
|
+
when GObject::GUINT64_TYPE
|
380
|
+
return 0
|
381
|
+
when GObject::GBOXED_TYPE
|
382
|
+
return self.vips_get_boxed_default(gtype_id)
|
383
|
+
else
|
384
|
+
return nil
|
385
|
+
end
|
386
|
+
end
|
387
|
+
|
388
|
+
# Returns the default for a GEnum derivative by allocating, initializing,
|
389
|
+
# and getting the contents of a GValue.
|
390
|
+
#
|
391
|
+
## Example:
|
392
|
+
# irb> gvp = GObject::GValue.alloc
|
393
|
+
# irb> gvp
|
394
|
+
# => #<GObject::GValue:0x00005603ba9d4c70>
|
395
|
+
# irb> gvp.init(GObject::g_type_from_name('VipsAccess'))
|
396
|
+
# => nil
|
397
|
+
# irb> GObject::g_type_from_name 'VipsAccess'
|
398
|
+
# => 94574011156416
|
399
|
+
# irb> gvp.get
|
400
|
+
# => :random
|
401
|
+
def self.vips_get_enum_default(gtype)
|
402
|
+
begin
|
403
|
+
gtype_id = gtype.is_a?(String) ? GObject::g_type_from_name(gtype) : gtype
|
404
|
+
# Deallocation is automatic when `gvp` goes out of scope.
|
405
|
+
gvp = GObject::GValue.alloc
|
406
|
+
gvp.init(gtype)
|
407
|
+
out = gvp.get
|
408
|
+
gvp.unset
|
409
|
+
return out
|
410
|
+
rescue FFI::NullPointerError => e
|
411
|
+
# This is happening for VipsArrayDouble gtype 94691056795136
|
412
|
+
# end I don't feel like debugging it rn lololol
|
413
|
+
nil
|
414
|
+
end
|
415
|
+
end
|
416
|
+
|
417
|
+
|
418
|
+
# Returns a Set[Symbol] of supported enum values for a given g_type
|
419
|
+
def self.vips_get_enum_values(gtype)
|
420
|
+
begin
|
421
|
+
gtype_id = gtype.is_a?(String) ? GObject::g_type_from_name(gtype) : gtype
|
422
|
+
|
423
|
+
# HACK HACK HACK:
|
424
|
+
# There *has* to be a better/native way to get this, but for now I'm just going to
|
425
|
+
# parse them out of the error message you can access after trying an obviously-wrong value.
|
426
|
+
#
|
427
|
+
# irb> Vips::vips_error_clear
|
428
|
+
# => nil
|
429
|
+
# irb> GObject::g_type_from_name 'VipsForeignTiffCompression'
|
430
|
+
# => 94691059614768
|
431
|
+
# irb> Vips::vips_enum_from_nick 'DistorteD', 94691059614768, 'lolol'
|
432
|
+
# => -1
|
433
|
+
# irb> Vips::vips_error_buffer
|
434
|
+
# => "DistorteD: enum 'VipsForeignTiffCompression' has no member 'lolol', should be one of: none, jpeg, deflate, packbits, ccittfax4, lzw\n"
|
435
|
+
Vips::vips_enum_from_nick('DistorteD'.freeze, gtype_id, 'lolol'.freeze)
|
436
|
+
error_buffer = Vips::vips_error_buffer
|
437
|
+
if error_buffer.include?('should be one of: '.freeze)
|
438
|
+
Vips::vips_error_clear
|
439
|
+
# Parse the error into a Set of Symbol options
|
440
|
+
discovered = error_buffer.split('should be one of: '.freeze)[1][0..-2].split(', '.freeze).map(&:to_sym).to_set
|
441
|
+
# For any Options with aliases, merge in the aliases.
|
442
|
+
(discovered & self::VIPS_ALIASES.keys.to_set).each { |aliased|
|
443
|
+
discovered.merge(self::VIPS_ALIASES[aliased])
|
444
|
+
}
|
445
|
+
# We need to give this back as an Array because callers will want to call :join on it,
|
446
|
+
# and we should give it back sorted so aliased aren't all piled up at the end.
|
447
|
+
discovered.to_a.sort
|
448
|
+
else
|
449
|
+
return Array[]
|
450
|
+
end
|
451
|
+
rescue
|
452
|
+
return Array[]
|
453
|
+
end
|
454
|
+
end
|
455
|
+
|
456
|
+
|
457
|
+
# Returns a Array of the boxed type (Int, Double, etc)
|
458
|
+
def self.vips_get_boxed_default(gtype)
|
459
|
+
gtype_id = gtype.is_a?(String) ? GObject::g_type_from_name(gtype) : gtype
|
460
|
+
gtype_name = GObject::g_type_name(gtype_id)
|
461
|
+
# It's not really correct to explicitly return three values here,
|
462
|
+
# but the use of this for `background` colors are the only use rn.
|
463
|
+
case gtype_name
|
464
|
+
when 'VipsArrayDouble'.freeze
|
465
|
+
return [0.0, 0.0, 0.0]
|
466
|
+
when 'VipsArrayInt'.freeze
|
467
|
+
return [0, 0, 0]
|
468
|
+
else
|
469
|
+
return []
|
470
|
+
end
|
471
|
+
end
|
472
|
+
|
473
|
+
|
474
|
+
# Returns boolean validity for libvips class names,
|
475
|
+
# e.g. for validating that a desired Loader/Saver class actually exists!
|
476
|
+
def self.vips_foreign_valid_operation?(otra)
|
477
|
+
# This doesn't seem to raise any Exception on invalid g_type, just returns 0.
|
478
|
+
# Use this to cast to a boolean return value:
|
479
|
+
#
|
480
|
+
# irb(main):243:0> GObject::g_type_from_name('VipsForeignSaveJpegFile')
|
481
|
+
# => 94691057381120
|
482
|
+
# irb(main):244:0> GObject::g_type_from_name('VipsForeignLoadJpegFile')
|
483
|
+
# => 94691057380176
|
484
|
+
# irb(main):245:0> GObject::g_type_from_name('VipsForeignLoadJpegFilgfgfgfe')
|
485
|
+
# => 0
|
486
|
+
GObject::g_type_from_name(otra) == 0 ? false : true
|
487
|
+
end
|
488
|
+
|
489
|
+
end
|