devil 0.1.6-x86-mswin32-60

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,154 @@
1
+ #include <ruby.h>
2
+ #include <IL/ilu.h>
3
+ #include "ruby_devil_ext.h"
4
+
5
+ static VALUE mILU;
6
+
7
+ static VALUE ilu_Init(VALUE obj) {
8
+ iluInit();
9
+ return Qnil;
10
+ }
11
+
12
+ static VALUE ilu_ErrorString(VALUE obj, VALUE rb_error) {
13
+ ILenum num = NUM2INT(rb_error);
14
+ const char* error = iluErrorString(num);
15
+ return rb_str_new2(error);
16
+ }
17
+
18
+ static VALUE ilu_Alienify(VALUE obj) {
19
+ ILboolean flag = iluAlienify();
20
+ return flag ? Qtrue : Qfalse;
21
+ }
22
+
23
+ static VALUE ilu_BlurAvg(VALUE obj, VALUE rb_iter) {
24
+ ILuint iter = NUM2INT(rb_iter);
25
+ ILboolean flag = iluBlurAvg(iter);
26
+ return flag ? Qtrue : Qfalse;
27
+ }
28
+
29
+ static VALUE ilu_BlurGaussian(VALUE obj, VALUE rb_iter) {
30
+ ILuint iter = NUM2INT(rb_iter);
31
+ ILboolean flag = iluBlurGaussian(iter);
32
+ return flag ? Qtrue : Qfalse;
33
+ }
34
+
35
+ static VALUE ilu_Contrast(VALUE obj, VALUE rb_cont) {
36
+ ILfloat cont = NUM2DBL(rb_cont);
37
+ ILboolean flag = iluContrast(cont);
38
+ return flag ? Qtrue : Qfalse;
39
+ }
40
+
41
+ static VALUE ilu_Equalize(VALUE obj) {
42
+ ILboolean flag = iluEqualize();
43
+ return flag ? Qtrue : Qfalse;
44
+ }
45
+
46
+ static VALUE ilu_GammaCorrect (VALUE obj, VALUE rb_gamma) {
47
+ ILfloat gamma = NUM2DBL(rb_gamma);
48
+ ILboolean flag = iluGammaCorrect(gamma);
49
+ return flag ? Qtrue : Qfalse;
50
+ }
51
+ static VALUE ilu_Negative (VALUE obj) {
52
+ ILboolean flag = iluNegative();
53
+ return flag ? Qtrue : Qfalse;
54
+ }
55
+ static VALUE ilu_Noisify (VALUE obj, VALUE rb_tolerance) {
56
+ ILclampf tolerance = NUM2DBL(rb_tolerance);
57
+ ILboolean flag = iluNoisify(tolerance);
58
+ return flag ? Qtrue : Qfalse;
59
+ }
60
+ static VALUE ilu_Pixelize (VALUE obj, VALUE rb_pix_size) {
61
+ ILuint pix = NUM2INT(rb_pix_size);
62
+ ILboolean flag = iluPixelize(pix);
63
+ return flag ? Qtrue : Qfalse;
64
+ }
65
+ static VALUE ilu_Sharpen (VALUE obj, VALUE rb_factor, VALUE rb_iter) {
66
+ ILfloat factor = NUM2DBL(rb_factor);
67
+ ILuint iter = NUM2INT(rb_iter);
68
+ ILboolean flag = iluSharpen(factor, iter);
69
+ return flag ? Qtrue : Qfalse;
70
+ }
71
+
72
+ static VALUE ilu_Scale(VALUE obj, VALUE rb_Width, VALUE rb_Height, VALUE rb_Depth) {
73
+ ILuint Width = NUM2INT(rb_Width);
74
+ ILuint Height = NUM2INT(rb_Height);
75
+ ILuint Depth = NUM2INT(rb_Depth);
76
+ ILboolean flag = iluScale(Width, Height, Depth);
77
+ return flag ? Qtrue : Qfalse;
78
+ }
79
+
80
+ static VALUE ilu_ImageParameter(VALUE obj, VALUE rb_PName, VALUE rb_Param) {
81
+ ILenum PName = NUM2INT(rb_PName);
82
+ ILenum Param = NUM2INT(rb_Param);
83
+ iluImageParameter(PName, Param);
84
+ return Qnil;
85
+ }
86
+
87
+ static VALUE ilu_BuildMipmaps(VALUE obj) {
88
+ ILboolean flag = iluBuildMipmaps();
89
+ return flag ? Qtrue : Qfalse;
90
+ }
91
+
92
+ /* functions added by banisterfiend */
93
+ static VALUE ilu_FlipImage(VALUE obj) {
94
+ ILboolean flag = iluFlipImage();
95
+ return flag ? Qtrue : Qfalse;
96
+ }
97
+
98
+ static VALUE ilu_Rotate(VALUE obj, VALUE rb_angle) {
99
+ ILfloat angle = NUM2DBL(rb_angle);
100
+
101
+ ILboolean flag = iluRotate(angle);
102
+
103
+ return flag ? Qtrue : Qfalse;
104
+ }
105
+
106
+ static VALUE ilu_Crop(VALUE obj, VALUE rb_XOff, VALUE rb_YOff, VALUE rb_width, VALUE rb_height)
107
+ {
108
+ ILuint XOff = NUM2INT(rb_XOff);
109
+ ILuint YOff = NUM2INT(rb_YOff);
110
+ ILuint width = NUM2INT(rb_width);
111
+ ILuint height = NUM2INT(rb_height);
112
+
113
+ ILboolean flag = iluCrop(XOff, YOff, 1, width, height, 1);
114
+
115
+ return flag ? Qtrue : Qfalse;
116
+ }
117
+ /* end of functions added by banisterfiend */
118
+
119
+ void
120
+ InitializeILU() {
121
+ mILU = rb_define_module("ILU");
122
+ rb_define_module_function(mILU, "Init", ilu_Init, 0);
123
+ rb_define_module_function(mILU, "ErrorString", ilu_ErrorString, 1);
124
+ rb_define_module_function(mILU, "Alienify", ilu_Alienify, 0);
125
+ rb_define_module_function(mILU, "BlurAvg", ilu_BlurAvg, 1);
126
+ rb_define_module_function(mILU, "BlurGaussian", ilu_BlurGaussian, 1);
127
+ rb_define_module_function(mILU, "Contrast",ilu_Contrast , 1);
128
+ rb_define_module_function(mILU, "Equalize",ilu_Equalize , 0);
129
+ rb_define_module_function(mILU, "GammaCorrect",ilu_GammaCorrect , 1);
130
+ rb_define_module_function(mILU, "Negative", ilu_Negative , 0);
131
+ rb_define_module_function(mILU, "Noisify", ilu_Noisify , 1);
132
+ rb_define_module_function(mILU, "Pixelize", ilu_Pixelize , 1);
133
+ rb_define_module_function(mILU, "Sharpen", ilu_Sharpen, 2);
134
+ rb_define_module_function(mILU, "Scale", ilu_Scale , 3);
135
+ rb_define_module_function(mILU, "ImageParameter", ilu_ImageParameter , 2);
136
+ rb_define_module_function(mILU, "BuildMipmaps", ilu_BuildMipmaps, 0);
137
+
138
+ /* methods added by banisterfiend */
139
+ rb_define_module_function(mILU, "FlipImage", ilu_FlipImage, 0);
140
+ rb_define_module_function(mILU, "Rotate", ilu_Rotate, 1);
141
+ rb_define_module_function(mILU, "Crop", ilu_Crop, 4);
142
+ /* end of functions added by banisterfiend */
143
+
144
+ rb_define_const(mILU, "FILTER", INT2NUM(ILU_FILTER));
145
+ rb_define_const(mILU, "NEAREST", INT2NUM(ILU_NEAREST));
146
+ rb_define_const(mILU, "LINEAR", INT2NUM(ILU_LINEAR));
147
+ rb_define_const(mILU, "BILINEAR", INT2NUM(ILU_BILINEAR));
148
+ rb_define_const(mILU, "SCALE_BOX", INT2NUM(ILU_SCALE_BOX));
149
+ rb_define_const(mILU, "SCALE_TRIANGLE", INT2NUM(ILU_SCALE_TRIANGLE));
150
+ rb_define_const(mILU, "SCALE_BELL", INT2NUM(ILU_SCALE_BELL));
151
+ rb_define_const(mILU, "SCALE_BSPLINE", INT2NUM(ILU_SCALE_BSPLINE));
152
+ rb_define_const(mILU, "SCALE_LANCZOS3", INT2NUM(ILU_SCALE_LANCZOS3));
153
+ rb_define_const(mILU, "SCALE_MITCHELL", INT2NUM(ILU_SCALE_MITCHELL));
154
+ }
data/lib/1.8/devil.so ADDED
Binary file
data/lib/1.9/devil.so ADDED
Binary file
data/lib/devil.rb ADDED
@@ -0,0 +1,226 @@
1
+ require 'rbconfig'
2
+
3
+ direc = File.dirname(__FILE__)
4
+ dlext = Config::CONFIG['DLEXT']
5
+ begin
6
+ if RUBY_VERSION && RUBY_VERSION =~ /1.9/
7
+ require "#{direc}/1.9/devil.#{dlext}"
8
+ else
9
+ require "#{direc}/1.8/devil.#{dlext}"
10
+ end
11
+ rescue LoadError => e
12
+ require "#{direc}/devil.#{dlext}"
13
+ end
14
+
15
+ # Provides a high level wrapper for the low-level DevIL Ruby bindings
16
+ module Devil
17
+ VERSION = '0.1.6'
18
+
19
+ class << self
20
+
21
+ # loads +file+ and returns a new image
22
+ # Optionally accepts a block and yields the newly created image to the block.
23
+ def load_image(file, &block)
24
+ name = IL.GenImages(1).first
25
+ IL.BindImage(name)
26
+ IL.LoadImage(file)
27
+
28
+ img = Image.new(name, file)
29
+ if block
30
+ block.call(img)
31
+ end
32
+
33
+ img
34
+ end
35
+
36
+ # convert an image +blob+ with +width+ and +height+
37
+ # to a bona fide image
38
+ def from_blob(blob, width, height)
39
+ Image.new(IL.FromBlob(blob, width, height), nil)
40
+ end
41
+
42
+ # initializes Devil and sets defaults
43
+ # This method should never need to be called directly.
44
+ def init
45
+ # initialize DevIL
46
+ IL.Init
47
+
48
+ # default options
49
+ IL.Enable(IL::FILE_OVERWRITE)
50
+ ILU.ImageParameter(ILU::FILTER, ILU::BILINEAR)
51
+ end
52
+
53
+ alias_method :with_image, :load_image
54
+ alias_method :load, :load_image
55
+ end
56
+
57
+ class Image
58
+ attr_reader :name, :file
59
+
60
+ def initialize(name, file)
61
+ @name = name
62
+ @file = file
63
+
64
+ ObjectSpace.define_finalizer( self, proc { IL.DeleteImages(1, [name]) } )
65
+ end
66
+
67
+ # returns the width of the image
68
+ def width
69
+ set_binding
70
+ IL.GetInteger(IL::IMAGE_WIDTH)
71
+ end
72
+
73
+ # returns the height of the image
74
+ def height
75
+ set_binding
76
+ IL.GetInteger(IL::IMAGE_HEIGHT)
77
+ end
78
+
79
+ # saves the image to +file+. If no +file+ is provided default to the opened file.
80
+ def save(file = @file)
81
+ set_binding
82
+ IL.SaveImage(file)
83
+ self
84
+ end
85
+
86
+ # resize the image to +width+ and +height+. Aspect ratios of the image do not have to be the same.
87
+ def resize(width, height)
88
+ set_binding
89
+ ILU.Scale(width, height, 1)
90
+ self
91
+ end
92
+
93
+ # Creates a proportional thumbnail of the image scaled so its longest
94
+ # edge is resized to +size+
95
+ def thumbnail(size)
96
+ # this thumbnail code from image_science.rb
97
+
98
+ w, h = width, height
99
+ scale = size.to_f / (w > h ? w : h)
100
+ resize((w * scale).to_i, (h * scale).to_i)
101
+ self
102
+ end
103
+
104
+ # return a deep copy of the current image
105
+ def dup
106
+ set_binding
107
+ Image.new(IL.CloneCurImage, nil)
108
+ end
109
+
110
+ # crop the current image
111
+ # +xoff+ number of pixels to skip in x direction
112
+ # +yoff+ number of pixels to skip in y direction
113
+ # +width+ number of pixels to preserve in x direction
114
+ # +height+ number of pixels to preserve in y direction
115
+ def crop(xoff, yoff, width, height)
116
+ set_binding
117
+ ILU.Crop(xoff, yoff, width, height)
118
+ self
119
+ end
120
+
121
+ # performs a gaussian blur on the image. The blur is performed +iter+ times.
122
+ def blur(iter)
123
+ set_binding
124
+ ILU.BlurGaussian(iter)
125
+ self
126
+ end
127
+
128
+ # 'pixelize' the image using a pixel size of +pixel_size+
129
+ def pixelize(pixel_size)
130
+ set_binding
131
+ ILU.Pixelize(pixel_size)
132
+ self
133
+ end
134
+
135
+ # add random noise to the image. +factor+ is the tolerance to use.
136
+ # accepeted values range from 0.0 - 1.0
137
+ def noisify(factor)
138
+ set_binding
139
+ ILU.Noisify(factor)
140
+ self
141
+ end
142
+
143
+ # The sharpening +factor+ must be in the range of 0.0 - 2.5. A value of 1.0 for the sharpening
144
+ # factor will have no effect on the image. Values in the range 1.0 - 2.5 will sharpen the
145
+ # image, with 2.5 having the most pronounced sharpening effect. Values from 0.0 to 1.0 do
146
+ # a type of reverse sharpening, blurring the image. Values outside of the 0.0 - 2.5 range
147
+ # produce undefined results.
148
+ #
149
+ # The number of +iter+ (iterations) to perform will usually be 1, but to achieve more sharpening,
150
+ # increase the number of iterations.
151
+ def sharpen(factor, iter)
152
+ set_binding
153
+ ILU.Sharpen(factor, iter)
154
+ self
155
+ end
156
+
157
+ # applies gamma correction to an image using an exponential curve.
158
+ # +factor+ is gamma correction factor to use.
159
+ # A value of 1.0 leaves the image unmodified.
160
+ # Values in the range 0.0 - 1.0 darken the image
161
+ # Values above 1.0 brighten the image.
162
+ def gamma_correct(factor)
163
+ set_binding
164
+ ILU.GammaCorrect(factor)
165
+ self
166
+ end
167
+
168
+ # invert the color of every pixel in the image.
169
+ def negative
170
+ set_binding
171
+ ILU.Negative
172
+ self
173
+ end
174
+
175
+ # +factor+ describes desired contrast to use
176
+ # A value of 1.0 has no effect on the image.
177
+ # Values between 1.0 and 1.7 increase the amount of contrast (values above 1.7 have no effect)
178
+ # Valid range of +factor+ is 0.0 - 1.7
179
+ def contrast(factor)
180
+ set_binding
181
+ ILU.Contrast(factor)
182
+ self
183
+ end
184
+
185
+ # darkens the bright colours and lightens the dark
186
+ # colours, reducing the contrast in an image or 'equalizing' it.
187
+ def equalize
188
+ set_binding
189
+ ILU.Equalize
190
+ self
191
+ end
192
+
193
+ # returns the image data in the form of a ruby string
194
+ # The image data is formatted to RGBA / UNSIGNED BYTE
195
+ def to_blob
196
+ set_binding
197
+ IL.ToBlob
198
+ end
199
+
200
+ # flip the image about its x axis
201
+ def flip
202
+ set_binding
203
+ ILU.FlipImage
204
+ self
205
+ end
206
+
207
+ # rotate an image about its central point by +angle+ degrees
208
+ def rotate(angle)
209
+ set_binding
210
+ ILU.Rotate(angle)
211
+ self
212
+ end
213
+
214
+ alias_method :columns, :width
215
+ alias_method :rows, :height
216
+
217
+ private
218
+
219
+ def set_binding
220
+ IL.BindImage(@name)
221
+ end
222
+
223
+ end
224
+ end
225
+
226
+ Devil.init
data/lib/devil/gosu.rb ADDED
@@ -0,0 +1,96 @@
1
+ require 'texplay'
2
+ require 'devil'
3
+
4
+ module TexPlay
5
+
6
+ # save a Gosu::Image to +file+
7
+ # This method is only available if require 'devil/gosu' is used
8
+ def save(file)
9
+ capture {
10
+ save_image = Devil.from_blob(self.to_blob, self.width, self.height)
11
+ save_image.flip
12
+ save_image.save(file)
13
+ }
14
+ end
15
+
16
+ # convert a Gosu::Image to a Devil::Image
17
+ # This method is only available if require 'devil/gosu' is used
18
+ def to_devil
19
+ devil_img = nil
20
+ capture {
21
+ devil_img = Devil.from_blob(self.to_blob, self.width, self.height)
22
+ devil_img.flip
23
+ }
24
+ devil_img
25
+ end
26
+ end
27
+
28
+ class Gosu::Window
29
+
30
+ # return a screenshot of the framebuffer as a Devil::Image
31
+ # This method is only available if require 'devil/gosu' is used
32
+ def screenshot
33
+ require 'opengl'
34
+
35
+ canvas_texture_id = glGenTextures(1).first
36
+
37
+ img = nil
38
+ self.gl do
39
+ glEnable(GL_TEXTURE_2D)
40
+ glBindTexture(GL_TEXTURE_2D, canvas_texture_id)
41
+
42
+ glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, 1)
43
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
44
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
45
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, self.width, self.height, 0, GL_RGB, GL_UNSIGNED_BYTE, "\0" * self.width * self.height * 3)
46
+
47
+ glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 0, 0, self.width, self.height, 0)
48
+
49
+ data = glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE)
50
+ img = Devil.from_blob(data, self.width, self.height)
51
+ end
52
+
53
+ img
54
+ end
55
+ end
56
+
57
+ class Devil::Image
58
+
59
+ # convert a Devil::Image to a Gosu::Image.
60
+ # Must provide a +window+ parameter, as per Gosu::Image#new()
61
+ def to_gosu(window)
62
+ Gosu::Image.new(window, self)
63
+ end
64
+
65
+ # display the Devil images on screen utilizing the Gosu library for visualization
66
+ # if +x+ and +y+ are specified then show the image centered at this location, otherwise
67
+ # draw the image at the center of the screen
68
+ # This method is only available if require 'devil/gosu' is used
69
+ def show(x = 512, y = 384)
70
+ if !Devil.const_defined?(:Window)
71
+ c = Class.new(Gosu::Window) do
72
+ attr_accessor :show_list
73
+
74
+ def initialize
75
+ super(1024, 768, false)
76
+ @show_list = []
77
+ end
78
+
79
+ def draw # :nodoc:
80
+ @show_list.each { |v| v[:image].draw_rot(v[:x], v[:y], 1, 0) }
81
+ end
82
+ end
83
+
84
+ Devil.const_set :Window, c
85
+ end
86
+
87
+ if !defined? @@window
88
+ @@window ||= Devil::Window.new
89
+
90
+ at_exit { @@window.show }
91
+ end
92
+
93
+ @@window.show_list.push :image => Gosu::Image.new(@@window, self), :x => x, :y => y
94
+ end
95
+
96
+ end