gd2-ffij 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +15 -0
- data/COPYRIGHT +2 -1
- data/Gemfile +3 -0
- data/{README → README.rdoc} +28 -15
- data/Rakefile +8 -20
- data/gd2-ffij.gemspec +11 -76
- data/lib/gd2-ffij.rb +4 -5
- data/lib/gd2/canvas.rb +16 -16
- data/lib/gd2/color.rb +1 -1
- data/lib/gd2/ffi_struct.rb +1 -1
- data/lib/gd2/font.rb +29 -27
- data/lib/gd2/image.rb +48 -49
- data/lib/gd2/palette.rb +6 -6
- data/lib/gd2/version.rb +5 -0
- data/test/canvas_tests.rb +176 -181
- data/test/image_tests.rb +135 -140
- data/test/images/test_text.gd2 +0 -0
- data/test/images/test_text_circle.gd2 +0 -0
- data/test/test_helper.rb +16 -8
- metadata +88 -9
- data/VERSION +0 -1
data/lib/gd2/image.rb
CHANGED
@@ -114,7 +114,7 @@ module GD2
|
|
114
114
|
|
115
115
|
type = data_type(magic) or
|
116
116
|
raise UnrecognizedImageTypeError, 'Image data format is not recognized'
|
117
|
-
ptr = GD2FFI.send(create[type], *args)
|
117
|
+
ptr = ::GD2::GD2FFI.send(create[type], *args)
|
118
118
|
raise LibraryError unless ptr
|
119
119
|
|
120
120
|
ptr = FFIStruct::ImagePtr.new(ptr)
|
@@ -168,9 +168,9 @@ module GD2
|
|
168
168
|
ptr = # TODO: implement xpm and xbm imports
|
169
169
|
#if format == :xpm
|
170
170
|
#raise ArgumentError, "Unexpected options #{options.inspect}" unless options.empty?
|
171
|
-
|
171
|
+
#::GD2::GD2FFI.send(:gdImageCreateFromXpm, filename)
|
172
172
|
#elsif format == :xbm
|
173
|
-
|
173
|
+
#::GD2::GD2FFI.send(:gdImageCreateFromXbm, filename)
|
174
174
|
if format == :gd2 && !options.empty?
|
175
175
|
x, y, width, height =
|
176
176
|
options.delete(:x) || 0, options.delete(:y) || 0,
|
@@ -182,7 +182,7 @@ module GD2
|
|
182
182
|
raise ArgumentError, 'Missing required option :height' if height.nil?
|
183
183
|
# TODO:
|
184
184
|
ptr = File.open(filename, 'rb') do |file|
|
185
|
-
GD2FFI.send(:gdImageCreateFromGd2Part, file, x.to_i, y.to_i, width.to_i, height.to_i)
|
185
|
+
::GD2::GD2FFI.send(:gdImageCreateFromGd2Part, file, x.to_i, y.to_i, width.to_i, height.to_i)
|
186
186
|
end
|
187
187
|
else
|
188
188
|
raise ArgumentError, "Unexpected options #{options.inspect}" unless
|
@@ -204,7 +204,7 @@ module GD2
|
|
204
204
|
file_ptr = FFI::MemoryPointer.new(file.size, 1, false)
|
205
205
|
file_ptr.put_bytes(0, file)
|
206
206
|
|
207
|
-
GD2FFI.send(create_sym, file.size, file_ptr)
|
207
|
+
::GD2::GD2FFI.send(create_sym, file.size, file_ptr)
|
208
208
|
end
|
209
209
|
raise LibraryError unless ptr
|
210
210
|
|
@@ -222,8 +222,8 @@ module GD2
|
|
222
222
|
private_class_method :image_true_color?
|
223
223
|
|
224
224
|
def self.create_image_ptr(sx, sy, alpha_blending = true) #:nodoc:
|
225
|
-
ptr = FFIStruct::ImagePtr.new(GD2FFI.send(create_image_sym, sx.to_i, sy.to_i))
|
226
|
-
GD2FFI.send(:gdImageAlphaBlending, ptr, alpha_blending ? 1 : 0)
|
225
|
+
ptr = FFIStruct::ImagePtr.new(::GD2::GD2FFI.send(create_image_sym, sx.to_i, sy.to_i))
|
226
|
+
::GD2::GD2FFI.send(:gdImageAlphaBlending, ptr, alpha_blending ? 1 : 0)
|
227
227
|
ptr
|
228
228
|
end
|
229
229
|
|
@@ -239,7 +239,7 @@ module GD2
|
|
239
239
|
end
|
240
240
|
|
241
241
|
@palette = self.class.palette_class.new(self) unless
|
242
|
-
@palette && @palette.image == self
|
242
|
+
defined?(@palette) && @palette.image == self
|
243
243
|
self
|
244
244
|
end
|
245
245
|
|
@@ -257,7 +257,7 @@ module GD2
|
|
257
257
|
# identical, otherwise a bit field indicating the differences. See the
|
258
258
|
# GD2::CMP_* constants for individual bit flags.
|
259
259
|
def compare(other)
|
260
|
-
GD2FFI.send(:gdImageCompare, image_ptr, other.image_ptr)
|
260
|
+
::GD2::GD2FFI.send(:gdImageCompare, image_ptr, other.image_ptr)
|
261
261
|
end
|
262
262
|
|
263
263
|
# Compare this image with another image. Returns *false* if the images are
|
@@ -297,13 +297,13 @@ module GD2
|
|
297
297
|
|
298
298
|
# Return the pixel value at image location (+x+, +y+).
|
299
299
|
def get_pixel(x, y)
|
300
|
-
GD2FFI.send(:gdImageGetPixel, @image_ptr, x.to_i, y.to_i)
|
300
|
+
::GD2::GD2FFI.send(:gdImageGetPixel, @image_ptr, x.to_i, y.to_i)
|
301
301
|
end
|
302
302
|
alias pixel get_pixel
|
303
303
|
|
304
304
|
# Set the pixel value at image location (+x+, +y+).
|
305
305
|
def set_pixel(x, y, value)
|
306
|
-
GD2FFI.send(:gdImageSetPixel, @image_ptr, x.to_i, y.to_i, value.to_i)
|
306
|
+
::GD2::GD2FFI.send(:gdImageSetPixel, @image_ptr, x.to_i, y.to_i, value.to_i)
|
307
307
|
nil
|
308
308
|
end
|
309
309
|
|
@@ -320,11 +320,10 @@ module GD2
|
|
320
320
|
# Iterate over each row of pixels in the image, returning an array of
|
321
321
|
# pixel values.
|
322
322
|
def each
|
323
|
-
ptr = image_ptr
|
324
323
|
(0...height).each do |y|
|
325
|
-
row = (0...width).inject(Array.new(width)) do |
|
326
|
-
|
327
|
-
|
324
|
+
row = (0...width).inject(Array.new(width)) do |r, x|
|
325
|
+
r[x] = get_pixel(x, y)
|
326
|
+
r
|
328
327
|
end
|
329
328
|
yield row
|
330
329
|
end
|
@@ -349,7 +348,7 @@ module GD2
|
|
349
348
|
# Set whether this image will be stored in interlaced form when output as
|
350
349
|
# PNG or JPEG.
|
351
350
|
def interlaced=(bool)
|
352
|
-
GD2FFI.send(:gdImageInterlace, image_ptr, bool ? 1 : 0)
|
351
|
+
::GD2::GD2FFI.send(:gdImageInterlace, image_ptr, bool ? 1 : 0)
|
353
352
|
end
|
354
353
|
|
355
354
|
# Return *true* if colors will be alpha blended into the image when pixels
|
@@ -363,7 +362,7 @@ module GD2
|
|
363
362
|
# pixels are modified. Alpha blending is not available for IndexedColor
|
364
363
|
# images.
|
365
364
|
def alpha_blending=(bool)
|
366
|
-
GD2FFI.send(:gdImageAlphaBlending, image_ptr, bool ? 1 : 0)
|
365
|
+
::GD2::GD2FFI.send(:gdImageAlphaBlending, image_ptr, bool ? 1 : 0)
|
367
366
|
end
|
368
367
|
|
369
368
|
# Return *true* if this image will be stored with full alpha channel
|
@@ -375,7 +374,7 @@ module GD2
|
|
375
374
|
# Set whether this image will be stored with full alpha channel information
|
376
375
|
# when output as PNG.
|
377
376
|
def save_alpha=(bool)
|
378
|
-
GD2FFI.send(:gdImageSaveAlpha, image_ptr, bool ? 1 : 0)
|
377
|
+
::GD2::GD2FFI.send(:gdImageSaveAlpha, image_ptr, bool ? 1 : 0)
|
379
378
|
end
|
380
379
|
|
381
380
|
# Return the transparent color for this image, or *nil* if none has been
|
@@ -387,7 +386,7 @@ module GD2
|
|
387
386
|
|
388
387
|
# Set or unset the transparent color for this image.
|
389
388
|
def transparent=(color)
|
390
|
-
GD2FFI.send(:gdImageColorTransparent, image_ptr,
|
389
|
+
::GD2::GD2FFI.send(:gdImageColorTransparent, image_ptr,
|
391
390
|
color.nil? ? -1 : color2pixel(color))
|
392
391
|
end
|
393
392
|
|
@@ -399,7 +398,7 @@ module GD2
|
|
399
398
|
x2 = FFI::MemoryPointer.new(:pointer)
|
400
399
|
y2 = FFI::MemoryPointer.new(:pointer)
|
401
400
|
|
402
|
-
GD2FFI.send(:gdImageGetClip, image_ptr, x1, y1, x2, y2)
|
401
|
+
::GD2::GD2FFI.send(:gdImageGetClip, image_ptr, x1, y1, x2, y2)
|
403
402
|
[ x1.read_int, y1.read_int, x2.read_int, y2.read_int ]
|
404
403
|
end
|
405
404
|
|
@@ -410,18 +409,18 @@ module GD2
|
|
410
409
|
clip = clipping
|
411
410
|
begin
|
412
411
|
p clipping
|
413
|
-
GD2FFI.send(:gdImageSetClip, image_ptr, x1.to_i, y1.to_i, x2.to_i, y2.to_i)
|
412
|
+
::GD2::GD2FFI.send(:gdImageSetClip, image_ptr, x1.to_i, y1.to_i, x2.to_i, y2.to_i)
|
414
413
|
p clipping
|
415
414
|
yield self
|
416
415
|
self
|
417
416
|
ensure
|
418
|
-
GD2FFI.send(:gdImageSetClip, image_ptr, *clip)
|
417
|
+
::GD2::GD2FFI.send(:gdImageSetClip, image_ptr, *clip)
|
419
418
|
end
|
420
419
|
end
|
421
420
|
|
422
421
|
# Return *true* if the current clipping rectangle excludes the given point.
|
423
422
|
def clips?(x, y)
|
424
|
-
GD2FFI.send(:gdImageBoundsSafe, image_ptr, x.to_i, y.to_i).zero?
|
423
|
+
::GD2::GD2FFI.send(:gdImageBoundsSafe, image_ptr, x.to_i, y.to_i).zero?
|
425
424
|
end
|
426
425
|
|
427
426
|
# Provide a drawing environment for a block. See GD2::Canvas.
|
@@ -482,10 +481,10 @@ module GD2
|
|
482
481
|
|
483
482
|
File.open(filename, 'wb') do |file|
|
484
483
|
begin
|
485
|
-
img = GD2FFI.send(write_sym, image_ptr, *args)
|
484
|
+
img = ::GD2::GD2FFI.send(write_sym, image_ptr, *args)
|
486
485
|
file.write(img.get_bytes(0, size.get_int(0)))
|
487
486
|
ensure
|
488
|
-
GD2FFI.gdFree(img)
|
487
|
+
::GD2::GD2FFI.gdFree(img)
|
489
488
|
end
|
490
489
|
end
|
491
490
|
end
|
@@ -495,10 +494,10 @@ module GD2
|
|
495
494
|
# implying both higher quality and larger sizes.
|
496
495
|
def jpeg(quality = nil)
|
497
496
|
size = FFI::MemoryPointer.new(:pointer)
|
498
|
-
ptr = GD2FFI.send(:gdImageJpegPtr, image_ptr, size, quality || -1)
|
497
|
+
ptr = ::GD2::GD2FFI.send(:gdImageJpegPtr, image_ptr, size, quality || -1)
|
499
498
|
ptr.get_bytes(0, size.get_int(0))
|
500
499
|
ensure
|
501
|
-
GD2FFI.send(:gdFree, ptr)
|
500
|
+
::GD2::GD2FFI.send(:gdFree, ptr)
|
502
501
|
end
|
503
502
|
|
504
503
|
# Encode and return data for this image in PNG format. The +level+
|
@@ -506,10 +505,10 @@ module GD2
|
|
506
505
|
# compression (0 = none, 1 = minimal but fast, 9 = best but slow).
|
507
506
|
def png(level = nil)
|
508
507
|
size = FFI::MemoryPointer.new(:pointer)
|
509
|
-
ptr = GD2FFI.send(:gdImagePngPtrEx, image_ptr, size, level.to_i || -1)
|
508
|
+
ptr = ::GD2::GD2FFI.send(:gdImagePngPtrEx, image_ptr, size, level.to_i || -1)
|
510
509
|
ptr.get_bytes(0, size.get_int(0))
|
511
510
|
ensure
|
512
|
-
GD2FFI.send(:gdFree, ptr)
|
511
|
+
::GD2::GD2FFI.send(:gdFree, ptr)
|
513
512
|
end
|
514
513
|
|
515
514
|
# Encode and return data for this image in GIF format. Note that GIF only
|
@@ -518,10 +517,10 @@ module GD2
|
|
518
517
|
# Image#to_indexed_color to control this conversion more precisely.
|
519
518
|
def gif
|
520
519
|
size = FFI::MemoryPointer.new(:pointer)
|
521
|
-
ptr = GD2FFI.send(:gdImageGifPtr, image_ptr, size)
|
520
|
+
ptr = ::GD2::GD2FFI.send(:gdImageGifPtr, image_ptr, size)
|
522
521
|
ptr.get_bytes(0, size.get_int(0))
|
523
522
|
ensure
|
524
|
-
GD2FFI.send(:gdFree, ptr)
|
523
|
+
::GD2::GD2FFI.send(:gdFree, ptr)
|
525
524
|
end
|
526
525
|
|
527
526
|
# Encode and return data for this image in WBMP format. WBMP currently
|
@@ -530,20 +529,20 @@ module GD2
|
|
530
529
|
# considered “background” (white).
|
531
530
|
def wbmp(fgcolor)
|
532
531
|
size = FFI::MemoryPointer.new(:pointer)
|
533
|
-
ptr = GD2FFI.send(:gdImageWBMPPtr, image_ptr, size, color2pixel(fgcolor))
|
532
|
+
ptr = ::GD2::GD2FFI.send(:gdImageWBMPPtr, image_ptr, size, color2pixel(fgcolor))
|
534
533
|
ptr.get_bytes(0, size.get_int(0))
|
535
534
|
ensure
|
536
|
-
GD2FFI.send(:gdFree, ptr)
|
535
|
+
::GD2::GD2FFI.send(:gdFree, ptr)
|
537
536
|
end
|
538
537
|
|
539
538
|
# Encode and return data for this image in “.gd” format. This is an
|
540
539
|
# internal format used by the gd library to quickly read and write images.
|
541
540
|
def gd
|
542
541
|
size = FFI::MemoryPointer.new(:pointer)
|
543
|
-
ptr = GD2FFI.send(:gdImageGdPtr, image_ptr, size)
|
542
|
+
ptr = ::GD2::GD2FFI.send(:gdImageGdPtr, image_ptr, size)
|
544
543
|
ptr.get_bytes(0, size.get_int(0))
|
545
544
|
ensure
|
546
|
-
GD2FFI.send(:gdFree, ptr)
|
545
|
+
::GD2::GD2FFI.send(:gdFree, ptr)
|
547
546
|
end
|
548
547
|
|
549
548
|
# Encode and return data for this image in “.gd2” format. This is an
|
@@ -551,10 +550,10 @@ module GD2
|
|
551
550
|
# The specified +fmt+ may be either GD2::FMT_RAW or GD2::FMT_COMPRESSED.
|
552
551
|
def gd2(fmt = FMT_COMPRESSED, chunk_size = 0)
|
553
552
|
size = FFI::MemoryPointer.new(:pointer)
|
554
|
-
ptr = GD2FFI.send(:gdImageGd2Ptr, image_ptr, chunk_size.to_i, fmt.to_i, size)
|
553
|
+
ptr = ::GD2::GD2FFI.send(:gdImageGd2Ptr, image_ptr, chunk_size.to_i, fmt.to_i, size)
|
555
554
|
ptr.get_bytes(0, size.get_int(0))
|
556
555
|
ensure
|
557
|
-
GD2FFI.send(:gdFree, ptr)
|
556
|
+
::GD2::GD2FFI.send(:gdFree, ptr)
|
558
557
|
end
|
559
558
|
|
560
559
|
# Copy a portion of another image to this image. If +src_w+ and +src_h+ are
|
@@ -564,10 +563,10 @@ module GD2
|
|
564
563
|
dst_w, dst_h, src_w = nil, src_h = nil)
|
565
564
|
raise ArgumentError unless src_w.nil? == src_h.nil?
|
566
565
|
if src_w
|
567
|
-
GD2FFI.send(:gdImageCopyResampled, image_ptr, other.image_ptr,
|
566
|
+
::GD2::GD2FFI.send(:gdImageCopyResampled, image_ptr, other.image_ptr,
|
568
567
|
dst_x.to_i, dst_y.to_i, src_x.to_i, src_y.to_i, dst_w.to_i, dst_h.to_i, src_w.to_i, src_h.to_i)
|
569
568
|
else
|
570
|
-
GD2FFI.send(:gdImageCopy, image_ptr, other.image_ptr,
|
569
|
+
::GD2::GD2FFI.send(:gdImageCopy, image_ptr, other.image_ptr,
|
571
570
|
dst_x.to_i, dst_y.to_i, src_x.to_i, src_y.to_i, dst_w.to_i, dst_h.to_i)
|
572
571
|
end
|
573
572
|
self
|
@@ -578,7 +577,7 @@ module GD2
|
|
578
577
|
# +dst_y+ arguments indicate the _center_ of the desired destination, and
|
579
578
|
# may be floating point.
|
580
579
|
def copy_from_rotated(other, dst_x, dst_y, src_x, src_y, w, h, angle)
|
581
|
-
GD2FFI.send(:gdImageCopyRotated, image_ptr, other.image_ptr,
|
580
|
+
::GD2::GD2FFI.send(:gdImageCopyRotated, image_ptr, other.image_ptr,
|
582
581
|
dst_x.to_f, dst_y.to_f, src_x.to_i, src_y.to_i, w.to_i, h.to_i, angle.to_degrees.round.to_i)
|
583
582
|
self
|
584
583
|
end
|
@@ -588,7 +587,7 @@ module GD2
|
|
588
587
|
# Image#copy_from; a percentage of 0.0 is a no-op. Note that alpha
|
589
588
|
# channel information from the source image is ignored.
|
590
589
|
def merge_from(other, dst_x, dst_y, src_x, src_y, w, h, pct)
|
591
|
-
GD2FFI.send(:gdImageCopyMerge, image_ptr, other.image_ptr,
|
590
|
+
::GD2::GD2FFI.send(:gdImageCopyMerge, image_ptr, other.image_ptr,
|
592
591
|
dst_x.to_i, dst_y.to_i, src_x.to_i, src_y.to_i, w.to_i, h.to_i, pct.to_percent.round.to_i)
|
593
592
|
self
|
594
593
|
end
|
@@ -597,7 +596,7 @@ module GD2
|
|
597
596
|
# coordinates. Note that some of the edges of the image may be lost.
|
598
597
|
def rotate!(angle, axis_x = width / 2.0, axis_y = height / 2.0)
|
599
598
|
ptr = self.class.create_image_ptr(width, height, alpha_blending?)
|
600
|
-
GD2FFI.send(:gdImageCopyRotated, ptr, image_ptr,
|
599
|
+
::GD2::GD2FFI.send(:gdImageCopyRotated, ptr, image_ptr,
|
601
600
|
axis_x.to_f, axis_y.to_f, 0, 0, width.to_i, height.to_i, angle.to_degrees.round.to_i)
|
602
601
|
init_with_image(ptr)
|
603
602
|
end
|
@@ -611,7 +610,7 @@ module GD2
|
|
611
610
|
# (0, 0).
|
612
611
|
def crop!(x, y, w, h)
|
613
612
|
ptr = self.class.create_image_ptr(w, h, alpha_blending?)
|
614
|
-
GD2FFI.send(:gdImageCopy, ptr, image_ptr, 0, 0, x.to_i, y.to_i, w.to_i, h.to_i)
|
613
|
+
::GD2::GD2FFI.send(:gdImageCopy, ptr, image_ptr, 0, 0, x.to_i, y.to_i, w.to_i, h.to_i)
|
615
614
|
init_with_image(ptr)
|
616
615
|
end
|
617
616
|
|
@@ -625,7 +624,7 @@ module GD2
|
|
625
624
|
def uncrop!(x1, y1 = x1, x2 = x1, y2 = y1)
|
626
625
|
ptr = self.class.create_image_ptr(x1 + width + x2, y1 + height + y2,
|
627
626
|
alpha_blending?)
|
628
|
-
GD2FFI.send(:gdImageCopy, ptr, image_ptr, x1.to_i, y1.to_i, 0, 0, width.to_i, height.to_i)
|
627
|
+
::GD2::GD2FFI.send(:gdImageCopy, ptr, image_ptr, x1.to_i, y1.to_i, 0, 0, width.to_i, height.to_i)
|
629
628
|
init_with_image(ptr)
|
630
629
|
end
|
631
630
|
|
@@ -639,7 +638,7 @@ module GD2
|
|
639
638
|
# shrunk as necessary without resampling.
|
640
639
|
def resize!(w, h, resample = true)
|
641
640
|
ptr = self.class.create_image_ptr(w, h, false)
|
642
|
-
GD2FFI.send(resample ? :gdImageCopyResampled : :gdImageCopyResized,
|
641
|
+
::GD2::GD2FFI.send(resample ? :gdImageCopyResampled : :gdImageCopyResized,
|
643
642
|
ptr, image_ptr, 0, 0, 0, 0, w.to_i, h.to_i, width.to_i, height.to_i)
|
644
643
|
alpha_blending = alpha_blending?
|
645
644
|
init_with_image(ptr)
|
@@ -658,7 +657,7 @@ module GD2
|
|
658
657
|
# Note that the original image must be square.
|
659
658
|
def polar_transform!(radius)
|
660
659
|
raise 'Image must be square' unless width == height
|
661
|
-
ptr = GD2FFI.send(:gdImageSquareToCircle, image_ptr, radius.to_i)
|
660
|
+
ptr = ::GD2::GD2FFI.send(:gdImageSquareToCircle, image_ptr, radius.to_i)
|
662
661
|
raise LibraryError unless ptr
|
663
662
|
init_with_image(ptr)
|
664
663
|
end
|
@@ -684,7 +683,7 @@ module GD2
|
|
684
683
|
# +colors+ indicates the maximum number of palette colors to use, and
|
685
684
|
# +dither+ controls whether dithering is used.
|
686
685
|
def to_indexed_color(colors = MAX_COLORS, dither = true)
|
687
|
-
ptr = GD2FFI.send(:gdImageCreatePaletteFromTrueColor,
|
686
|
+
ptr = ::GD2::GD2FFI.send(:gdImageCreatePaletteFromTrueColor,
|
688
687
|
to_true_color.image_ptr, dither ? 1 : 0, colors.to_i)
|
689
688
|
raise LibraryError unless ptr
|
690
689
|
|
@@ -766,7 +765,7 @@ module GD2
|
|
766
765
|
# grey scale before the merge.
|
767
766
|
def merge_from(other, dst_x, dst_y, src_x, src_y, w, h, pct, gray = false)
|
768
767
|
return super(other, dst_x, dst_y, src_x, src_y, w, h, pct) unless gray
|
769
|
-
GD2FFI.send(:gdImageCopyMergeGray, image_ptr, other.image_ptr,
|
768
|
+
::GD2::GD2FFI.send(:gdImageCopyMergeGray, image_ptr, other.image_ptr,
|
770
769
|
dst_x.to_i, dst_y.to_i, src_x.to_i, src_y.to_i, w.to_i, h.to_i, pct.to_percent.round.to_i)
|
771
770
|
self
|
772
771
|
end
|
@@ -788,7 +787,7 @@ module GD2
|
|
788
787
|
end
|
789
788
|
|
790
789
|
def sharpen(pct) #:nodoc:
|
791
|
-
GD2FFI.send(:gdImageSharpen, image_ptr, pct.to_percent.round.to_i)
|
790
|
+
::GD2::GD2FFI.send(:gdImageSharpen, image_ptr, pct.to_percent.round.to_i)
|
792
791
|
self
|
793
792
|
end
|
794
793
|
end
|
data/lib/gd2/palette.rb
CHANGED
@@ -103,7 +103,7 @@ module GD2
|
|
103
103
|
# a color from the palette that is closest to it.
|
104
104
|
def resolve(color)
|
105
105
|
raise TypeError unless color.kind_of? Color
|
106
|
-
c = GD2FFI.send(:gdImageColorResolveAlpha, @image.image_ptr,
|
106
|
+
c = ::GD2::GD2FFI.send(:gdImageColorResolveAlpha, @image.image_ptr,
|
107
107
|
color.red.to_i, color.green.to_i, color.blue.to_i, color.alpha.to_i)
|
108
108
|
c == -1 ? nil : get_color(c)
|
109
109
|
end
|
@@ -112,7 +112,7 @@ module GD2
|
|
112
112
|
# if the color is not presently in the palette.
|
113
113
|
def exact(color)
|
114
114
|
raise TypeError unless color.kind_of? Color
|
115
|
-
c = GD2FFI.send(:gdImageColorExactAlpha, @image.image_ptr,
|
115
|
+
c = ::GD2::GD2FFI.send(:gdImageColorExactAlpha, @image.image_ptr,
|
116
116
|
color.red.to_i, color.green.to_i, color.blue.to_i, color.alpha.to_i)
|
117
117
|
c == -1 ? nil : get_color(c)
|
118
118
|
end
|
@@ -128,7 +128,7 @@ module GD2
|
|
128
128
|
# according to Euclidian distance.
|
129
129
|
def closest(color)
|
130
130
|
raise TypeError unless color.kind_of? Color
|
131
|
-
c = GD2FFI.send(:gdImageColorClosestAlpha, @image.image_ptr,
|
131
|
+
c = ::GD2::GD2FFI.send(:gdImageColorClosestAlpha, @image.image_ptr,
|
132
132
|
color.red.to_i, color.green.to_i, color.blue.to_i, color.alpha.to_i)
|
133
133
|
c == -1 ? nil : get_color(c)
|
134
134
|
end
|
@@ -137,7 +137,7 @@ module GD2
|
|
137
137
|
# according to hue, whiteness, and blackness.
|
138
138
|
def closest_hwb(color)
|
139
139
|
raise TypeError unless color.kind_of? Color
|
140
|
-
c = GD2FFI.send(:gdImageColorClosestHWB, @image.image_ptr,
|
140
|
+
c = ::GD2::GD2FFI.send(:gdImageColorClosestHWB, @image.image_ptr,
|
141
141
|
color.red.to_i, color.green.to_i, color.blue.to_i)
|
142
142
|
c == -1 ? nil : get_color(c)
|
143
143
|
end
|
@@ -147,7 +147,7 @@ module GD2
|
|
147
147
|
# raises an error for Image::IndexedColor palettes if the palette is full.
|
148
148
|
def allocate(color)
|
149
149
|
raise TypeError unless color.kind_of? Color
|
150
|
-
c = GD2FFI.send(:gdImageColorAllocateAlpha, @image.image_ptr,
|
150
|
+
c = ::GD2::GD2FFI.send(:gdImageColorAllocateAlpha, @image.image_ptr,
|
151
151
|
color.red.to_i, color.green.to_i, color.blue.to_i, color.alpha.to_i)
|
152
152
|
c == -1 ? raise(Palette::PaletteFullError, 'Palette is full') :
|
153
153
|
get_color(c)
|
@@ -164,7 +164,7 @@ module GD2
|
|
164
164
|
def deallocate(color)
|
165
165
|
color = exact(color) unless color.index
|
166
166
|
return nil if color.nil? || color.index.nil?
|
167
|
-
GD2FFI.send(:gdImageColorDeallocate, @image.image_ptr, color.index.to_i)
|
167
|
+
::GD2::GD2FFI.send(:gdImageColorDeallocate, @image.image_ptr, color.index.to_i)
|
168
168
|
nil
|
169
169
|
end
|
170
170
|
|
data/test/canvas_tests.rb
CHANGED
@@ -1,186 +1,181 @@
|
|
1
|
-
require 'test/unit'
|
2
|
-
require 'tmpdir'
|
3
|
-
|
4
|
-
require 'rubygems'
|
5
|
-
require 'gd2-ffij'
|
6
1
|
|
7
2
|
require './test/test_helper'
|
8
3
|
|
9
4
|
class CanvasTest < Test::Unit::TestCase
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
5
|
+
include TestHelper
|
6
|
+
|
7
|
+
def test_line
|
8
|
+
image = new_image
|
9
|
+
image.draw do |pen|
|
10
|
+
pen.color = image.palette.resolve(GD2::Color[255, 255, 255])
|
11
|
+
pen.line(64, 64, 192, 192)
|
12
|
+
end
|
13
|
+
assert(image == load_image('test_canvas_line.gd2'))
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_rectangle
|
17
|
+
image = new_image
|
18
|
+
image.draw do |pen|
|
19
|
+
pen.color = image.palette.resolve(GD2::Color[255, 255, 255])
|
20
|
+
pen.rectangle(64, 64, 192, 192)
|
21
|
+
end
|
22
|
+
assert(image == load_image('test_canvas_rectangle.gd2'))
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_filled_rectangle
|
26
|
+
image = new_image
|
27
|
+
image.draw do |pen|
|
28
|
+
pen.color = image.palette.resolve(GD2::Color[255, 255, 255])
|
29
|
+
pen.rectangle(64, 64, 192, 192, true)
|
30
|
+
end
|
31
|
+
assert(image == load_image('test_canvas_filled_rectangle.gd2'))
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_polygon
|
35
|
+
image = new_image
|
36
|
+
image.draw do |pen|
|
37
|
+
pen.color = image.palette.resolve(GD2::Color[255, 255, 255])
|
38
|
+
pen.polygon([
|
39
|
+
[ 64, 64 ],
|
40
|
+
[ 192, 192 ],
|
41
|
+
[ 64, 128 ]
|
42
|
+
])
|
43
|
+
end
|
44
|
+
assert(image == load_image('test_canvas_polygon.gd2'))
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_filled_polygon
|
48
|
+
image = new_image
|
49
|
+
image.draw do |pen|
|
50
|
+
pen.color = image.palette.resolve(GD2::Color[255, 255, 255])
|
51
|
+
pen.polygon([
|
52
|
+
[ 64, 64 ],
|
53
|
+
[ 192, 192 ],
|
54
|
+
[ 64, 128 ]
|
55
|
+
], true)
|
56
|
+
end
|
57
|
+
assert(image == load_image('test_canvas_filled_polygon.gd2'))
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_move_to_and_line_to
|
61
|
+
image = new_image
|
62
|
+
image.draw do |pen|
|
63
|
+
pen.color = image.palette.resolve(GD2::Color[255, 255, 255])
|
64
|
+
pen.move_to(64, 64)
|
65
|
+
pen.line_to(128, 128)
|
66
|
+
end
|
67
|
+
assert(image == load_image('test_canvas_move_to_and_line_to.gd2'))
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_fill
|
71
|
+
image = new_image
|
72
|
+
image.draw do |pen|
|
73
|
+
pen.color = image.palette.resolve(GD2::Color[32, 64, 128])
|
74
|
+
pen.rectangle(64, 64, 192, 192)
|
75
|
+
pen.move_to(65, 65)
|
76
|
+
pen.fill
|
77
|
+
end
|
78
|
+
assert(image == load_image('test_fill.gd2'))
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_arc
|
82
|
+
image = new_image
|
83
|
+
image.draw do |pen|
|
84
|
+
pen.color = image.palette.resolve(GD2::Color[32, 64, 128])
|
85
|
+
pen.arc(128, 128, 128, 128, 0..256)
|
86
|
+
end
|
87
|
+
assert(image == load_image('test_arc.gd2'))
|
88
|
+
end
|
89
|
+
|
90
|
+
def test_text
|
91
|
+
image = new_image
|
92
|
+
image.draw do |pen|
|
93
|
+
pen.color = image.palette.resolve(GD2::Color[32, 64, 128])
|
94
|
+
pen.font = GD2::Font::TrueType[PATH_TO_FONT, 32]
|
95
|
+
pen.move_to(0, 128)
|
96
|
+
pen.text("HELLO")
|
97
|
+
pen.move_to(256, 128)
|
98
|
+
pen.text("WORLD", Math::PI)
|
99
|
+
end
|
100
|
+
assert(image == load_image('test_text.gd2'))
|
101
|
+
end
|
102
|
+
|
103
|
+
def test_text_circle
|
104
|
+
image = new_image
|
105
|
+
image.draw do |pen|
|
106
|
+
pen.color = image.palette.resolve(GD2::Color[32, 64, 128])
|
107
|
+
pen.font = GD2::Font::TrueType[PATH_TO_FONT, 32]
|
108
|
+
pen.move_to(128, 128)
|
109
|
+
pen.text_circle('HELLO', 'WORLD', 100, 20, 1)
|
110
|
+
end
|
111
|
+
assert(image == load_image('test_text_circle.gd2'))
|
112
|
+
end
|
113
|
+
|
114
|
+
def test_wedge
|
115
|
+
image = new_image
|
116
|
+
image.draw do |pen|
|
117
|
+
pen.color = image.palette.resolve(GD2::Color[32, 64, 128])
|
118
|
+
pen.wedge(128, 128, 256, 256, 0..180)
|
119
|
+
end
|
120
|
+
assert(image == load_image('test_wedge.gd2'))
|
121
|
+
end
|
122
|
+
|
123
|
+
def test_filled_wedge
|
124
|
+
image = new_image
|
125
|
+
image.draw do |pen|
|
126
|
+
pen.color = image.palette.resolve(GD2::Color[32, 64, 128])
|
127
|
+
pen.wedge(128, 128, 256, 256, 0..180, true)
|
128
|
+
end
|
129
|
+
assert(image == load_image('test_filled_wedge.gd2'))
|
130
|
+
end
|
131
|
+
|
132
|
+
def test_ellipse
|
133
|
+
image = new_image
|
134
|
+
image.draw do |pen|
|
135
|
+
pen.color = image.palette.resolve(GD2::Color[32, 64, 128])
|
136
|
+
pen.ellipse(128, 128, 64, 32)
|
137
|
+
end
|
138
|
+
assert(image == load_image('test_ellipse.gd2'))
|
139
|
+
end
|
140
|
+
|
141
|
+
def test_filled_ellipse
|
142
|
+
image = new_image
|
143
|
+
image.draw do |pen|
|
144
|
+
pen.color = image.palette.resolve(GD2::Color[32, 64, 128])
|
145
|
+
pen.ellipse(128, 128, 64, 32, true)
|
146
|
+
end
|
147
|
+
assert(image == load_image('test_filled_ellipse.gd2'))
|
148
|
+
end
|
149
|
+
|
150
|
+
def test_circle
|
151
|
+
image = new_image
|
152
|
+
image.draw do |pen|
|
153
|
+
pen.color = image.palette.resolve(GD2::Color[32, 64, 128])
|
154
|
+
pen.circle(128, 128, 128)
|
155
|
+
end
|
156
|
+
assert(image == load_image('test_circle.gd2'))
|
157
|
+
end
|
158
|
+
|
159
|
+
def test_filled_circle
|
160
|
+
image = new_image
|
161
|
+
image.draw do |pen|
|
162
|
+
pen.color = image.palette.resolve(GD2::Color[32, 64, 128])
|
163
|
+
pen.circle(128, 128, 128, true)
|
164
|
+
end
|
165
|
+
assert(image == load_image('test_filled_circle.gd2'))
|
166
|
+
end
|
167
|
+
|
168
|
+
def test_fill_to
|
169
|
+
image = new_image
|
170
|
+
image.draw do |pen|
|
171
|
+
blue = image.palette.resolve(GD2::Color[32, 64, 128])
|
172
|
+
red = image.palette.resolve(GD2::Color[255, 0, 0])
|
173
|
+
pen.color = blue
|
174
|
+
pen.arc(128, 128, 256, 64, 0..(Math::PI))
|
175
|
+
pen.color = red
|
176
|
+
pen.move(128, 128)
|
177
|
+
pen.fill_to(blue)
|
178
|
+
end
|
179
|
+
assert(image == load_image('test_fill_to.gd2'))
|
180
|
+
end
|
186
181
|
end
|