devil 0.1.2 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -1,4 +1,5 @@
1
- Devil version 0.1.2
1
+ Devil version 0.1.6
2
+ ===================
2
3
 
3
4
  * Original author: Jaroslaw Tworek <dev.jrx@gmail.com>
4
5
  * Current maintainer: John Mair (banisterfiend) http://banisterfiend.wordpress.com
@@ -6,20 +7,27 @@ Devil version 0.1.2
6
7
  Ruby bindings for the Developer's Image Library
7
8
  You need DevIL installed to build this extension
8
9
 
10
+ Installation Instructions:
11
+ ==========================
12
+
9
13
  For debian:
10
14
  * install the libdevil and libdevil-dev packages
11
15
 
12
- Or go here to download the libraries:
13
- http://openil.sourceforge.net
14
-
15
- It's also worth having Ruby OpenGL bindings to use the extensions.
16
+ For windows:
17
+ * download devil.dll and ilu.dll from here http://openil.sourceforge.net
18
+ * and copy devil.dll and ilu.dll to c:\windows\system\
16
19
 
17
- http://www2.geganet.net/~yoshi/
20
+ For other systems:
21
+ * install libdevil and lib-devil-dev using your package manager
22
+ * OR download and install the libraries from http://openil.sourceforge.net
18
23
 
19
- See INSTALL for installation instructions.
24
+ If you wish to use the Gosu and TexPlay extensions, it is necessary to also have:
25
+ * the Gosu gem (gem install gosu)
26
+ * the TexPlay gem (gem install texplay)
27
+ * ruby-opengl (gem install ruby-opengl)
20
28
 
21
29
  For now, there is support for just a subset of DevIL functions, but it is enough
22
30
  for 95% things you may want to do.
23
31
 
24
32
  For example uses, see test/ directory
25
-
33
+ (note: that many of the examples use the Gosu library...this is often just for visualization purposes. Alot of the functionality does not depend on Gosu being present)
data/Rakefile CHANGED
@@ -4,7 +4,7 @@ require 'rake/gempackagetask'
4
4
  require 'rake/testtask'
5
5
  require 'rake/rdoctask'
6
6
 
7
- DEVIL_VERSION = "0.1.2"
7
+ DEVIL_VERSION = "0.1.6"
8
8
 
9
9
  dlext = Config::CONFIG['DLEXT']
10
10
 
@@ -21,13 +21,27 @@ spec = Gem::Specification.new do |s|
21
21
  s.date = Time.now.strftime '%Y-%m-%d'
22
22
  s.require_path = 'lib'
23
23
  s.homepage = "http://banisterfiend.wordpress.com"
24
- s.platform = Gem::Platform::RUBY
25
- s.extensions = FileList["ext/**/extconf.rb"]
24
+
25
+ if RUBY_PLATFORM =~ /win/
26
+ s.platform = Gem::Platform::CURRENT
27
+ else
28
+ s.platform = Gem::Platform::RUBY
29
+ end
30
+
31
+ if RUBY_PLATFORM !~ /win/
32
+ s.extensions = FileList["ext/**/extconf.rb"]
33
+ end
34
+
26
35
  s.has_rdoc = true
27
36
  s.extra_rdoc_files = ["README"]
28
37
  s.rdoc_options << '--main' << 'README'
29
- s.files = ["Rakefile", "README", "LICENSE", "lib/devil.rb"] +
30
- FileList["ext/**/extconf.rb", "ext/**/*.h", "ext/**/*.c", "test/test*.rb"].to_a
38
+ s.files = ["Rakefile", "README", "LICENSE", "lib/devil.rb", "lib/devil/gosu.rb"] +
39
+ FileList["ext/**/extconf.rb", "ext/**/*.h", "ext/**/*.c", "test/test*.rb", "test/*.png", "test/*.jpg"].to_a
40
+
41
+ if RUBY_PLATFORM =~ /win/
42
+ s.files += ["lib/1.8/devil.so", "lib/1.9/devil.so"]
43
+ end
44
+
31
45
  end
32
46
 
33
47
  Rake::GemPackageTask.new(spec) do |pkg|
@@ -35,6 +49,8 @@ Rake::GemPackageTask.new(spec) do |pkg|
35
49
  pkg.need_tar = false
36
50
  end
37
51
 
52
+ task :compile => :clean
53
+
38
54
  Rake::ExtensionTask.new('devil', spec) do |ext|
39
55
  ext.config_script = 'extconf.rb'
40
56
  ext.cross_compile = true
@@ -49,6 +65,6 @@ end
49
65
 
50
66
  Rake::RDocTask.new do |rd|
51
67
  rd.main = "README"
52
- rd.rdoc_files.include("README", "lib/devil.rb")
68
+ rd.rdoc_files.include("README", "lib/devil.rb", "lib/devil/gosu.rb")
53
69
  end
54
70
 
@@ -1,9 +1,19 @@
1
1
  require 'mkmf'
2
2
 
3
- #$CFLAGS += ' -I/home/john/.rake-compiler/ruby/ruby-1.8.6-p287/include/'
4
- #$LDFLAGS += ' -L/home/john/.rake-compiler/ruby/ruby-1.8.6-p287/lib/'
5
- exit unless have_library("IL", "ilInit");
6
- exit unless have_library("ILU", "iluInit");
3
+ if RUBY_PLATFORM =~ /mingw/
4
+ $CFLAGS += ' -I/home/john/.rake-compiler/ruby/ruby-1.8.6-p287/include/'
5
+ $LDFLAGS += ' -L/home/john/.rake-compiler/ruby/ruby-1.8.6-p287/lib/'
6
+ end
7
+
8
+ puts "platform is #{RUBY_PLATFORM}"
9
+ if RUBY_PLATFORM =~ /win/ || RUBY_PLATFORM =~ /mingw/
10
+ exit unless have_library("DevIL");
11
+ else
12
+ exit unless have_library("IL");
13
+ end
14
+
15
+ exit unless have_library("ILU");
16
+
7
17
  #have_library("ILUT", "ilutInit");
8
18
 
9
19
  create_makefile('devil')
@@ -131,28 +131,28 @@ static VALUE il_GetData(VALUE obj) {
131
131
  return MakeImageData(data);
132
132
  }
133
133
 
134
- ILuint ilCopyPixels(ILuint XOff, ILuint YOff, ILuint ZOff, ILuint Width,
135
- /* changed from ILvoid */
136
- ILuint Height, ILuint Depth, ILenum Format, ILenum Type, void
137
- *Data);
138
-
139
- static VALUE il_CopyPixels(VALUE obj, VALUE rb_XOff, VALUE rb_YOff, VALUE rb_ZOff,
140
- VALUE rb_Width, VALUE rb_Height, VALUE rb_Depth, VALUE rb_Format,
141
- VALUE rb_Type, VALUE rb_data) {
142
- ILuint XOff = NUM2INT(rb_XOff);
143
- ILuint YOff = NUM2INT(rb_YOff);
144
- ILuint ZOff = NUM2INT(rb_ZOff);
145
- ILuint Width = NUM2INT(rb_Width);
146
- ILuint Height = NUM2INT(rb_Height);
147
- ILuint Depth = NUM2INT(rb_Depth);
148
- ILenum Format = NUM2INT(rb_Format);
149
- ILenum Type = NUM2INT(rb_Type);
150
- /* ILvoid */
151
- void* data = ImageData2Arr(rb_data);
152
-
153
- ILuint uint = ilCopyPixels(XOff, YOff, ZOff, Width, Height, Depth, Format, Type, data);
154
- return INT2FIX(uint);
155
- }
134
+ /* ILuint ilCopyPixels(ILuint XOff, ILuint YOff, ILuint ZOff, ILuint Width, */
135
+ /* /\* changed from ILvoid *\/ */
136
+ /* ILuint Height, ILuint Depth, ILenum Format, ILenum Type, void */
137
+ /* *Data); */
138
+
139
+ /* static VALUE il_CopyPixels(VALUE obj, VALUE rb_XOff, VALUE rb_YOff, VALUE rb_ZOff, */
140
+ /* VALUE rb_Width, VALUE rb_Height, VALUE rb_Depth, VALUE rb_Format, */
141
+ /* VALUE rb_Type, VALUE rb_data) { */
142
+ /* ILuint XOff = NUM2INT(rb_XOff); */
143
+ /* ILuint YOff = NUM2INT(rb_YOff); */
144
+ /* ILuint ZOff = NUM2INT(rb_ZOff); */
145
+ /* ILuint Width = NUM2INT(rb_Width); */
146
+ /* ILuint Height = NUM2INT(rb_Height); */
147
+ /* ILuint Depth = NUM2INT(rb_Depth); */
148
+ /* ILenum Format = NUM2INT(rb_Format); */
149
+ /* ILenum Type = NUM2INT(rb_Type); */
150
+ /* /\* ILvoid *\/ */
151
+ /* void* data = ImageData2Arr(rb_data); */
152
+
153
+ /* ILuint uint = ilCopyPixels(XOff, YOff, ZOff, Width, Height, Depth, Format, Type, data); */
154
+ /* return INT2FIX(uint); */
155
+ /* } */
156
156
 
157
157
  static VALUE il_SetData(VALUE obj, VALUE rb_Data) {
158
158
  /* ILvoid */
@@ -234,7 +234,7 @@ static VALUE il_ActiveImage(VALUE obj, VALUE rb_Number){
234
234
  return flag ? Qtrue : Qfalse;
235
235
  }
236
236
 
237
- /* added by banisterfiend */
237
+ /* methods below added by banisterfiend */
238
238
  static VALUE il_Enable(VALUE obj, VALUE rb_mode) {
239
239
  ILenum mode = NUM2INT(rb_mode);
240
240
 
@@ -248,6 +248,60 @@ static VALUE il_GetInteger(VALUE obj, VALUE rb_mode) {
248
248
  ILint result = ilGetInteger(mode);
249
249
  return INT2NUM(result);
250
250
  }
251
+
252
+ static VALUE il_ConvertImage(VALUE obj, VALUE rb_destformat, VALUE rb_desttype)
253
+ {
254
+ ILenum destformat = NUM2INT(rb_destformat);
255
+ ILenum desttype = NUM2INT(rb_desttype);
256
+
257
+ ILboolean flag = ilConvertImage(destformat, desttype);
258
+ return flag ? Qtrue : Qfalse;
259
+ }
260
+
261
+ /* this function is not actualy in the DevIL API, but im adding it here for convenience */
262
+ static VALUE bf_ToBlob(VALUE obj)
263
+ {
264
+ ILuint width, height;
265
+ char * img_ptr;
266
+ VALUE blob;
267
+
268
+ ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);
269
+
270
+ width = ilGetInteger(IL_IMAGE_WIDTH);
271
+ height = ilGetInteger(IL_IMAGE_HEIGHT);
272
+
273
+ img_ptr = (char *)ilGetData();
274
+
275
+ blob = rb_str_new(img_ptr, 4 * width * height);
276
+
277
+ return blob;
278
+ }
279
+
280
+ static VALUE bf_FromBlob(VALUE obj, VALUE blob, VALUE rb_width, VALUE rb_height)
281
+ {
282
+ ILubyte * data;
283
+ ILuint width, height;
284
+ ILuint image;
285
+
286
+ width = NUM2INT(rb_width);
287
+ height = NUM2INT(rb_height);
288
+
289
+ data = (ILubyte *) RSTRING_PTR(blob);
290
+
291
+ ilGenImages(1, &image);
292
+ ilBindImage(image);
293
+
294
+ ilTexImage(width, height, 1, 4, IL_RGBA, IL_UNSIGNED_BYTE, data);
295
+
296
+ return INT2NUM(image);
297
+ }
298
+
299
+ static VALUE il_CloneCurImage(VALUE obj)
300
+ {
301
+ ILuint clone = ilCloneCurImage();
302
+
303
+ return INT2NUM(clone);
304
+ }
251
305
  /* end of banisterfiend additions */
252
306
 
253
307
  void
@@ -272,7 +326,7 @@ InitializeIL() {
272
326
  rb_define_module_function(mIL, "Save", il_Save, 2);
273
327
  rb_define_module_function(mIL, "TexImage", il_TexImage, 7);
274
328
  rb_define_module_function(mIL, "GetData", il_GetData, 0);
275
- rb_define_module_function(mIL, "CopyPixels", il_CopyPixels, 9);
329
+ /* rb_define_module_function(mIL, "CopyPixels", il_CopyPixels, 9); */
276
330
  rb_define_module_function(mIL, "SetData", il_SetData, 1);
277
331
  rb_define_module_function(mIL, "SetPixels", il_SetPixels, 9);
278
332
  rb_define_module_function(mIL, "CopyImage", il_CopyImage, 1);
@@ -282,10 +336,14 @@ InitializeIL() {
282
336
  rb_define_module_function(mIL, "ActiveMipmap", il_ActiveMipmap, 1);
283
337
  rb_define_module_function(mIL, "ActiveImage", il_ActiveImage, 1);
284
338
 
285
- // METHODS ADDED BY BANISTERFIEND
339
+ /* methods added by baniterfiend */
286
340
  rb_define_module_function(mIL, "Enable", il_Enable, 1);
287
341
  rb_define_module_function(mIL, "GetInteger", il_GetInteger, 1);
288
-
342
+ rb_define_module_function(mIL, "ConvertImage", il_ConvertImage, 2);
343
+ rb_define_module_function(mIL, "ToBlob", bf_ToBlob, 0);
344
+ rb_define_module_function(mIL, "FromBlob", bf_FromBlob, 3);
345
+ rb_define_module_function(mIL, "CloneCurImage", il_CloneCurImage, 0);
346
+ /* end of methods added by banisterfiend */
289
347
 
290
348
  //////////////////////////////////
291
349
  //CONSTANTS
@@ -298,7 +356,13 @@ InitializeIL() {
298
356
  rb_define_const(mIL, "ICO", INT2NUM(IL_ICO));
299
357
  rb_define_const(mIL, "JPG", INT2NUM(IL_JPG));
300
358
  rb_define_const(mIL, "JFIF", INT2NUM(IL_JFIF));
359
+
360
+ #ifdef _WIN32
361
+ rb_define_const(mIL, "LBM", INT2NUM(IL_ILBM));
362
+ #else
301
363
  rb_define_const(mIL, "LBM", INT2NUM(IL_LBM));
364
+ #endif
365
+
302
366
  rb_define_const(mIL, "PCD", INT2NUM(IL_PCD));
303
367
  rb_define_const(mIL, "PCX", INT2NUM(IL_PCX));
304
368
  rb_define_const(mIL, "PIC", INT2NUM(IL_PIC));
@@ -373,6 +437,7 @@ InitializeIL() {
373
437
  // CONSTANTS BELOW ADDED BY BANISTERFIEND
374
438
  rb_define_const(mIL, "IMAGE_WIDTH", INT2NUM(IL_IMAGE_WIDTH));
375
439
  rb_define_const(mIL, "IMAGE_HEIGHT", INT2NUM(IL_IMAGE_HEIGHT));
440
+ rb_define_const(mIL, "IMAGE_FORMAT", INT2NUM(IL_IMAGE_FORMAT));
376
441
  rb_define_const(mIL, "FILE_OVERWRITE", INT2NUM(IL_FILE_OVERWRITE));
377
442
  }
378
443
  //////////////////////////////////////////
@@ -89,6 +89,33 @@ static VALUE ilu_BuildMipmaps(VALUE obj) {
89
89
  return flag ? Qtrue : Qfalse;
90
90
  }
91
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
+
92
119
  void
93
120
  InitializeILU() {
94
121
  mILU = rb_define_module("ILU");
@@ -108,6 +135,12 @@ InitializeILU() {
108
135
  rb_define_module_function(mILU, "ImageParameter", ilu_ImageParameter , 2);
109
136
  rb_define_module_function(mILU, "BuildMipmaps", ilu_BuildMipmaps, 0);
110
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
+
111
144
  rb_define_const(mILU, "FILTER", INT2NUM(ILU_FILTER));
112
145
  rb_define_const(mILU, "NEAREST", INT2NUM(ILU_NEAREST));
113
146
  rb_define_const(mILU, "LINEAR", INT2NUM(ILU_LINEAR));
@@ -14,39 +14,49 @@ end
14
14
 
15
15
  # Provides a high level wrapper for the low-level DevIL Ruby bindings
16
16
  module Devil
17
- VERSION = '0.1.2'
17
+ VERSION = '0.1.6'
18
18
 
19
- # loads +file+ and returns a new image
20
- # Optionally accepts a block and yields the newly created image to the block.
21
- def self.load_image(file, &block)
22
- name = IL.GenImages(1).first
23
- IL.BindImage(name)
24
- IL.LoadImage(file)
19
+ class << self
25
20
 
26
- img = Image.new(name, file)
27
- if block
28
- block.call(img)
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)
29
40
  end
30
-
31
- img
32
- end
33
41
 
34
- # initializes Devil and sets defaults
35
- # This method should never need to be called directly.
36
- def self.init
37
- # initialize DevIL
38
- IL.Init
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
39
47
 
40
- # default options
41
- IL.Enable(IL::FILE_OVERWRITE)
42
- ILU.ImageParameter(ILU::FILTER, ILU::BILINEAR)
43
- end
48
+ # default options
49
+ IL.Enable(IL::FILE_OVERWRITE)
50
+ ILU.ImageParameter(ILU::FILTER, ILU::BILINEAR)
51
+ end
44
52
 
45
- class << self
46
53
  alias_method :with_image, :load_image
54
+ alias_method :load, :load_image
47
55
  end
48
56
 
49
57
  class Image
58
+ attr_reader :name, :file
59
+
50
60
  def initialize(name, file)
51
61
  @name = name
52
62
  @file = file
@@ -70,25 +80,56 @@ module Devil
70
80
  def save(file = @file)
71
81
  set_binding
72
82
  IL.SaveImage(file)
83
+ self
73
84
  end
74
-
75
85
 
76
86
  # resize the image to +width+ and +height+. Aspect ratios of the image do not have to be the same.
77
87
  def resize(width, height)
78
88
  set_binding
79
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
80
119
  end
81
120
 
82
121
  # performs a gaussian blur on the image. The blur is performed +iter+ times.
83
122
  def blur(iter)
84
123
  set_binding
85
124
  ILU.BlurGaussian(iter)
125
+ self
86
126
  end
87
127
 
88
128
  # 'pixelize' the image using a pixel size of +pixel_size+
89
129
  def pixelize(pixel_size)
90
130
  set_binding
91
131
  ILU.Pixelize(pixel_size)
132
+ self
92
133
  end
93
134
 
94
135
  # add random noise to the image. +factor+ is the tolerance to use.
@@ -96,6 +137,7 @@ module Devil
96
137
  def noisify(factor)
97
138
  set_binding
98
139
  ILU.Noisify(factor)
140
+ self
99
141
  end
100
142
 
101
143
  # The sharpening +factor+ must be in the range of 0.0 - 2.5. A value of 1.0 for the sharpening
@@ -109,6 +151,7 @@ module Devil
109
151
  def sharpen(factor, iter)
110
152
  set_binding
111
153
  ILU.Sharpen(factor, iter)
154
+ self
112
155
  end
113
156
 
114
157
  # applies gamma correction to an image using an exponential curve.
@@ -119,12 +162,14 @@ module Devil
119
162
  def gamma_correct(factor)
120
163
  set_binding
121
164
  ILU.GammaCorrect(factor)
165
+ self
122
166
  end
123
167
 
124
168
  # invert the color of every pixel in the image.
125
169
  def negative
126
170
  set_binding
127
171
  ILU.Negative
172
+ self
128
173
  end
129
174
 
130
175
  # +factor+ describes desired contrast to use
@@ -134,6 +179,7 @@ module Devil
134
179
  def contrast(factor)
135
180
  set_binding
136
181
  ILU.Contrast(factor)
182
+ self
137
183
  end
138
184
 
139
185
  # darkens the bright colours and lightens the dark
@@ -141,13 +187,39 @@ module Devil
141
187
  def equalize
142
188
  set_binding
143
189
  ILU.Equalize
190
+ self
144
191
  end
145
-
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
+
146
217
  private
147
218
 
148
219
  def set_binding
149
220
  IL.BindImage(@name)
150
221
  end
222
+
151
223
  end
152
224
  end
153
225
 
@@ -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
Binary file
@@ -0,0 +1,25 @@
1
+ direc = File.dirname(__FILE__)
2
+
3
+ $LOAD_PATH.push("#{direc}/../lib/")
4
+
5
+ require 'rubygems'
6
+ require "devil/gosu"
7
+
8
+ img = Devil.load_image("#{direc}/texture.png")
9
+
10
+ img.crop(100,100, 200, 200)
11
+ img_dup = img.dup
12
+
13
+ img.negative
14
+
15
+ img.show(200,200)
16
+ img_dup.show(450, 200)
17
+
18
+
19
+
20
+
21
+
22
+
23
+
24
+
25
+
@@ -0,0 +1,26 @@
1
+ $direc = File.dirname(__FILE__)
2
+
3
+ $LOAD_PATH.push("#{$direc}/../lib/")
4
+
5
+ require 'rubygems'
6
+ require 'gosu'
7
+ require 'devil'
8
+
9
+ class W < Gosu::Window
10
+ def initialize
11
+ super(1024, 768, false, 20)
12
+
13
+ img = Devil.load_image("#{$direc}/texture.jpg")
14
+
15
+ @img = Gosu::Image.new(self, img)
16
+ end
17
+
18
+ def draw
19
+ @img.draw 100, 50,1
20
+ end
21
+
22
+ end
23
+
24
+ w = W.new
25
+ w.show
26
+
@@ -0,0 +1,26 @@
1
+ $direc = File.dirname(__FILE__)
2
+
3
+ $LOAD_PATH.push("#{$direc}/../lib/")
4
+
5
+ require 'rubygems'
6
+ require 'devil/gosu'
7
+
8
+ class W < Gosu::Window
9
+ def initialize
10
+ super(1024, 768, false, 20)
11
+
12
+ @img = Gosu::Image.new(self, "tank.png")
13
+ @img.circle 100, 100, 50, :color => :purple, :filled => true
14
+
15
+ @img.save("tank-modified.png")
16
+ end
17
+
18
+ def draw
19
+ @img.draw 100, 50,1
20
+ end
21
+
22
+ end
23
+
24
+ w = W.new
25
+ w.show
26
+
@@ -0,0 +1,10 @@
1
+ $direc = File.dirname(__FILE__)
2
+
3
+ $LOAD_PATH.push("#{$direc}/../lib/")
4
+
5
+ require 'rubygems'
6
+ require 'devil/gosu'
7
+
8
+ Devil.load_image("texture.png").show(512, 400)
9
+ Devil.load_image("tank.png").show(300, 400)
10
+
@@ -1,8 +1,9 @@
1
1
  direc = File.dirname(__FILE__)
2
2
 
3
- require 'rubygems'
4
- require "#{direc}/../lib/devil"
3
+ $LOAD_PATH.push("#{direc}/../lib/")
5
4
 
5
+ require 'rubygems'
6
+ require "devil"
6
7
 
7
8
  Devil.load_image("#{direc}/texture.png") do |img|
8
9
  img.negative
@@ -0,0 +1,33 @@
1
+ $direc = File.dirname(__FILE__)
2
+
3
+ $LOAD_PATH.push("#{$direc}/../lib/")
4
+
5
+ require 'rubygems'
6
+ require 'devil/gosu'
7
+
8
+ class W < Gosu::Window
9
+ def initialize
10
+ super(1024, 768, false, 20)
11
+
12
+ @img = Gosu::Image.new(self, "tank.png")
13
+ @img2 = @img.dup
14
+ @img.circle 100, 100, 50, :color => :purple, :filled => true
15
+ end
16
+
17
+ def draw
18
+ @img.draw 100, 50,1
19
+ @img.draw 300, 50, 1
20
+ @img2.draw 300, 400, 1
21
+ end
22
+
23
+ def update
24
+ if button_down?(Gosu::KbEscape)
25
+ screenshot.rotate(45).save("screenshot.png")
26
+ exit
27
+ end
28
+ end
29
+ end
30
+
31
+ w = W.new
32
+ w.show
33
+
@@ -1,20 +1,12 @@
1
- require 'rubygems'
2
- require 'devil'
3
-
4
- IL.Init
5
-
6
- name = IL.GenImages(1)[0]
7
- IL.BindImage(name)
8
- IL.LoadImage("texture.png")
1
+ $direc = File.dirname(__FILE__)
9
2
 
10
- puts "image height/width"
11
- puts IL.GetInteger(IL::IMAGE_WIDTH)
12
- puts IL.GetInteger(IL::IMAGE_HEIGHT)
3
+ $LOAD_PATH.push("#{$direc}/../lib/")
13
4
 
14
- ILU.Scale(100, 100, 1)
15
-
16
- IL.Enable(IL::FILE_OVERWRITE)
17
-
18
- IL.SaveImage("desert_thumb.png")
5
+ require 'rubygems'
6
+ require 'devil/gosu'
19
7
 
8
+ Devil.load("texture.png") do |img|
9
+ img.thumbnail(300)
10
+ img.show
11
+ end
20
12
 
Binary file
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: devil
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jaroslaw Tworek, John Mair (banisterfiend)
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-10-07 00:00:00 -04:00
12
+ date: 2009-10-12 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -26,13 +26,22 @@ files:
26
26
  - README
27
27
  - LICENSE
28
28
  - lib/devil.rb
29
+ - lib/devil/gosu.rb
29
30
  - ext/devil/extconf.rb
30
31
  - ext/devil/ruby_devil_ext.h
31
32
  - ext/devil/ruby_devil_ext.c
32
33
  - ext/devil/ruby_il.c
33
34
  - ext/devil/ruby_ilu.c
35
+ - test/test_gosu_load.rb
34
36
  - test/test_new_api.rb
37
+ - test/test_clone_and_crop.rb
38
+ - test/test_gosu_show.rb
39
+ - test/test_screenshot.rb
40
+ - test/test_gosu_save.rb
35
41
  - test/test_thumbnail.rb
42
+ - test/tank.png
43
+ - test/texture.png
44
+ - test/texture.jpg
36
45
  has_rdoc: true
37
46
  homepage: http://banisterfiend.wordpress.com
38
47
  post_install_message: