pikl 0.2.2-x86-mswin32

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.
data/lib/pikl/const.rb ADDED
@@ -0,0 +1,54 @@
1
+ module Pikl
2
+
3
+ JPEG = 1
4
+ JPG = JPEG
5
+ PNG = 2
6
+ BITMAP = 3
7
+ BMP = BITMAP
8
+
9
+ ANGLE000 = 0
10
+ ANGLE090 = 1
11
+ ANGLE180 = 2
12
+ ANGLE270 = 3
13
+
14
+ SAMPLE_NN = 1 #nearest neighbor
15
+ SAMPLE_BL = 2 #bilinear
16
+ SAMPLE_BC = 3 #bicubic
17
+ SAMPLE_PA = 4 #pixcel average(reduce only)
18
+ SAMPLE_LZ = 5 #lanczos
19
+
20
+ SAMPLES = {
21
+ :nearest_neighbor => SAMPLE_NN,
22
+ :bilinear => SAMPLE_BL,
23
+ :bicubic => SAMPLE_BC,
24
+ :pixcel_averate => SAMPLE_PA,
25
+ :lanczos => SAMPLE_LZ,
26
+ :nn => SAMPLE_NN,
27
+ :bl => SAMPLE_BL,
28
+ :bc => SAMPLE_BC,
29
+ :pa => SAMPLE_PA,
30
+ :lz => SAMPLE_LZ,
31
+ }
32
+
33
+ ROTATE_ANGLE = {
34
+ 0 => ANGLE000,
35
+ 90 => ANGLE090,
36
+ 180 => ANGLE180,
37
+ 270 => ANGLE270,
38
+ 360 => ANGLE000,
39
+ }
40
+
41
+
42
+ EXTENSIONS_FORMATS = {
43
+ "jpeg" => JPEG,
44
+ "jpg" => JPEG,
45
+ "png" => PNG,
46
+ "bmp" => BITMAP,
47
+ }
48
+
49
+ EXTENSIONS = EXTENSIONS_FORMATS.keys
50
+ FORMATS = EXTENSIONS_FORMATS.values.uniq
51
+
52
+ PIX_LIMIT = 6000
53
+
54
+ end
@@ -0,0 +1,7 @@
1
+ module Pikl
2
+
3
+ class ParameterException < Exception; end
4
+
5
+ class ImageProcessException < Exception; end
6
+
7
+ end
data/lib/pikl/ext.rb ADDED
@@ -0,0 +1,35 @@
1
+ module Pikl
2
+ require "dl/import"
3
+ require "dl/struct"
4
+ require "rbconfig"
5
+
6
+ module Ext
7
+ extend DL::Importable
8
+
9
+ dlext = (RbConfig::CONFIG["host_os"] == 'mswin32') ? 'dll' : RbConfig::CONFIG['DLEXT']
10
+ dlload "#{File.dirname(__FILE__)}/pikl.#{dlext}"
11
+
12
+ typealias("KKImage", "void")
13
+ typealias("PKL_FORMAT", "int")
14
+ typealias("PKL_ANGLE", "int")
15
+ typealias("PKL_SAMPLE", "int")
16
+
17
+ extern "PKLImage *pkl_open(char*)"
18
+ extern "void pkl_close(PKLImage*)"
19
+ extern "int pkl_save(PKLImage*, const char *, PKL_FORMAT)"
20
+ extern "int pkl_compress(PKLImage*, int)"
21
+ extern "PKL_FORMAT pkl_format(PKLImage *)"
22
+ extern "int pkl_width(PKLImage *)"
23
+ extern "int pkl_height(PKLImage *)"
24
+ extern "int pkl_trim(PKLImage *, int, int, int, int)"
25
+ extern "int pkl_rotate(PKLImage *, PKL_ANGLE)"
26
+ extern "int pkl_resize(PKLImage*, int, int, PKL_SAMPLE)"
27
+ extern "int pkl_unsharp(PKLImage *, int, double)"
28
+ extern "int pkl_contrast(PKLImage *, int)"
29
+ extern "int pkl_level(PKLImage *, double, double, double)"
30
+ extern "int pkl_brightness(PKLImage*, int)"
31
+ extern "int pkl_hls(PKLImage*, double, double, double)"
32
+ extern "int pkl_gamma(PKLImage*, double)"
33
+
34
+ end
35
+ end
data/lib/pikl/image.rb ADDED
@@ -0,0 +1,230 @@
1
+ module Pikl
2
+
3
+ class Image
4
+
5
+ def self.open(inpath, &block)
6
+ image = Pikl::Image.new(inpath)
7
+ image.instance_variable_set(:@block,block)
8
+
9
+ return image unless block_given?
10
+
11
+ begin
12
+ block.call(image)
13
+ ensure
14
+ image.close if image
15
+ end
16
+
17
+ end
18
+
19
+ def initialize( inpath )
20
+ @pkl_image = Ext.pkl_open(File.expand_path(inpath))
21
+ end
22
+
23
+ def save(outpath, format = nil, compress = nil)
24
+ raise Pikl::ImageProcessException.new("image already closed.") unless @pkl_image
25
+ format ||= (split_extensions(outpath) || @format)
26
+ validate_format(format)
27
+ if compress
28
+ validate_compress(compress)
29
+ Ext.pkl_compress(@pkl_image, compress.to_i)
30
+ end
31
+ Ext.pkl_save(@pkl_image, File.expand_path(outpath), EXTENSIONS_FORMATS[format.to_s] || format.to_i)
32
+ self.close unless(@block)
33
+ @pkl_image
34
+ end
35
+
36
+ def close()
37
+ Ext.pkl_close(@pkl_image) if @pkl_image
38
+ @pkl_image = nil
39
+ end
40
+
41
+ def width
42
+ Ext.pkl_width(@pkl_image) if(@pkl_image)
43
+ end
44
+
45
+ def height
46
+ Ext.pkl_height(@pkl_image) if(@pkl_image)
47
+ end
48
+
49
+ def format
50
+ Ext.pkl_format(@pkl_image) if(@pkl_image)
51
+ end
52
+
53
+ def trim(x, y, dist_x, dist_y)
54
+ validate_trim(x, y, dist_x, dist_y)
55
+ dist_x = trim_dist(x, self.width, dist_x)
56
+ dist_y = trim_dist(y, self.height, dist_y)
57
+
58
+ Ext.pkl_trim(@pkl_image, x, y, dist_x, dist_y)
59
+
60
+ self
61
+ end
62
+
63
+ def trim_dist(start, dist_from, dist_to)
64
+ if dist_to == :auto || dist_to > (dist_from - start)
65
+ dist_to = dist_from - start
66
+ elsif dist_to < 0
67
+ dist_to += (dist_from - start)
68
+ else
69
+ dist_to
70
+ end
71
+ end
72
+
73
+ def rotate(angle)
74
+ validate_rotate(angle)
75
+ Ext.pkl_rotate(@pkl_image, ROTATE_ANGLE[angle.to_i])
76
+ self
77
+ end
78
+
79
+ def resize(width, height, sample = :pixcel_averate)
80
+ validate_auto(width,height)
81
+
82
+ case sample.class.name
83
+ when 'Symbol', 'String'
84
+ sample = SAMPLES[sample.to_sym]
85
+ end
86
+
87
+ width = self.width * height / self.height if(width == :auto)
88
+ height = self.height * width / self.width if(height == :auto)
89
+
90
+ validate_resize(width, height)
91
+
92
+ Ext.pkl_resize(@pkl_image, width, height, sample)
93
+ self
94
+ end
95
+
96
+ # threshold=0-255
97
+ # threshold=0の時は変化しません
98
+ # threshold=255の時は全ての色にアンシャープ処理が働きます
99
+ # edge=-10 .. 10
100
+ # edge=0の時は変化しません
101
+ # edge>0の時は値が大きいほど先鋭化されます
102
+ # edge<0の時は値が小さいほどぼやけます
103
+ #
104
+ # 想定結果が得られる範囲は-10 .. 10程度です。
105
+ # これを超える場合は、品質の保証はありません
106
+ # ※画質によりこの通りではありません。-10..10の範囲より小さくなる可能性があります。
107
+ def unshapmask(threshold, edge)
108
+ Ext.pkl_unsharp(@pkl_image, threshold.to_i, edge.to_f)
109
+ self
110
+ end
111
+
112
+ # コントラスト調整
113
+ # コントラストを強くするということは、白い部分をより白く、
114
+ # 黒い部分をより黒くするような変換を意味します。
115
+ # 逆にコントラストを弱くした場合、白い部分と黒い部分の差が無くなり、灰色っぽい画像になります。
116
+ # <rate>
117
+ # 範囲:-127 .. 127
118
+ # rate=0の時は変化しません
119
+ # * rateが0以上の時は周波数によるコントラスト強調がおこなわれます
120
+ # * rateが0未満の時は直線的に平坦化されます
121
+ def contrast(rate)
122
+ Ext.pkl_contrast(@pkl_image, rate.to_f)
123
+ self
124
+ end
125
+
126
+ # レベル補正
127
+ # ヒストグラムの平坦化をおこないます。各色の分布に偏りがある画像に有効な場合があります。
128
+ # coeff = 平坦化時の係数です。1が標準です。1より小さい場合は暗く、1より大きい場合は明るくなります(0.0..2.0)
129
+ # low = 全ピクセル数に対して、切り捨てる暗い色の総数の割合(0-100%)
130
+ # high = 全ピクセル数に対して、切り捨てる明るい色の総数の割合(0-100%)
131
+ def level(low, high, coeff)
132
+ Ext.pkl_level(@pkl_image, low.to_f, high.to_f, coeff.to_f)
133
+ self
134
+ end
135
+
136
+ # 明るさ調整
137
+ # この明るさ調整は各色からcolor値を加算する単純な処理です。
138
+ # colorに255を指定すれば、ただの白い画像になります
139
+ # colorに-255を指定すると、ただの黒い画像になります。
140
+ def brightness(color)
141
+ Ext.pkl_brightness(@pkl_image, color.to_i)
142
+ self
143
+ end
144
+
145
+ # 輝度(明るさ)・彩度(鮮やかさ)・色相(色合い)調整
146
+ # * 具体的にどういうことかを理解したい場合は、mspaintの「色の編集」を見ると良いでしょう。
147
+ # ym(輝度) -1 .. 1(0.1で10%up).0では変化なし
148
+ # sm(彩度) -1 .. 1(0.1で10%up).0では変化なし
149
+ # hd(色相) 360.0度回転(R=113.2/Ye=173.0/G=225.0/Cy=293.2/B=353.0/Mg=45.0).360の倍数では変化なし
150
+ def hls(ym, sm ,hd)
151
+ Ext.pkl_hls(@pkl_image, ym.to_f, sm.to_f, hd.to_f)
152
+ self
153
+ end
154
+
155
+ # ガンマ補正
156
+ # ガンマ補正をします。
157
+ # gmは補正係数です。
158
+ # gm=0以上の値が指定できます。
159
+ # 1.0より小さい時は暗く、1.0より大きい時は明るく調整されます。
160
+ # 1.0を指定した時は調整されません。
161
+ def gamma(gm)
162
+ Ext.pkl_gamma(@pkl_image, gm.to_f)
163
+ self
164
+ end
165
+
166
+ # regular expressions to try for identifying extensions
167
+ def split_extensions(path)
168
+ filename = path.split('/').last
169
+ (filename =~ %r|.+\.([a-z,A-Z]+)$|) ? $1 : nil
170
+ end
171
+
172
+ def validate_auto(width, height)
173
+ error("invalid :autoto in the both of width and height.") if(width == :auto && height == :auto)
174
+ end
175
+
176
+ # validate identifying extensions
177
+ def validate_format(ext)
178
+ error(ext) unless (ext || EXTENSIONS.include?(ext.to_s.downcase) || FORMATS.include?(ext.to_i))
179
+ end
180
+
181
+ def validate_trim(x, y, dist_x, dist_y)
182
+ validate_numeric(x, y)
183
+
184
+ validate_pix(dist_x, true)
185
+ validate_pix(dist_y, true)
186
+
187
+ error("left is outside the range. #{x}") if x > self.width.to_i
188
+ error("top is outside the range. #{y}") if y > self.height.to_i
189
+ end
190
+
191
+ def validate_rotate(angle)
192
+ validate_numeric(angle)
193
+ error("invalid angle. # => #{angle}") unless ROTATE_ANGLE.has_key?(angle)
194
+ end
195
+
196
+ def validate_resize(width, height)
197
+ validate_auto(width, height)
198
+ validate_pix(width)
199
+ validate_pix(height)
200
+ end
201
+
202
+ def validate_pix(value, allow_minus = false)
203
+ return if value == :auto
204
+ validate_numeric(value)
205
+ error("value is outside the range. #{value}") if value == 0
206
+ error("value is outside the range. #{value}") if !allow_minus && value < 0
207
+ error("value is outside the range. #{value}") if value > PIX_LIMIT
208
+ end
209
+
210
+ def validate_numeric(*args)
211
+ args = [args] unless args.is_a?(Array)
212
+ args.each do |v|
213
+ error("invalid parameter. # => #{v}") unless /^[-\d]+$/ =~ v.to_s
214
+ end
215
+ end
216
+
217
+ def validate_compress(v)
218
+ error("invalid compress parameter. # => #{v}") unless /^\d+$/ =~ v.to_s
219
+ error("invalid compress parameter. # => #{v}") unless v.to_i >= 0 && v.to_i <= 10
220
+ end
221
+
222
+ def error(message)
223
+ #self.close
224
+ raise Pikl::ParameterException.new(message)
225
+ end
226
+ end
227
+ end
228
+
229
+
230
+
data/lib/pikl/pikl.dll ADDED
Binary file
@@ -0,0 +1,9 @@
1
+ module Pikl #:nodoc:
2
+ module VERSION #:nodoc:
3
+ MAJOR = 0
4
+ MINOR = 2
5
+ TINY = 2
6
+
7
+ STRING = [MAJOR, MINOR, TINY].join('.')
8
+ end
9
+ end
data/lib/pikl.rb ADDED
@@ -0,0 +1,14 @@
1
+ #--
2
+ # = Pikl - a simple image library.
3
+ # Author:: Ryota Maruko and Keiko Soejima
4
+ # Copyright:: (c) 2008 Ryota Maruko and Keiko Soejima
5
+ # License:: MIT License
6
+ #++
7
+ $:.unshift(File.dirname(__FILE__)) unless
8
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
9
+ module Pikl
10
+ require "pikl/const"
11
+ require "pikl/ext"
12
+ require "pikl/errors"
13
+ require "pikl/image"
14
+ end