added transparency spec
authorMatthias Kramm <kramm@quiss.org>
Fri, 5 Feb 2010 05:38:08 +0000 (21:38 -0800)
committerMatthias Kramm <kramm@quiss.org>
Fri, 5 Feb 2010 05:38:08 +0000 (21:38 -0800)
spec/spec_helper.rb
spec/transparency.pdf [new file with mode: 0644]
spec/transparency.py [new file with mode: 0644]
spec/transparency.spec.rb [new file with mode: 0644]

index 86e098b..44c1424 100644 (file)
@@ -67,7 +67,7 @@ end
 
 def rgb_to_int(rgb)
   # ImageMagick rgb triples are 16 bit
-  (rgb.reverse+"\0").unpack("i")[0]
+  (rgb.reverse+[0]).map {|c| c>>8}.pack("CCCC").unpack("i")[0]
 end
 
 class Pixel
@@ -80,13 +80,13 @@ class Pixel
     color1 == color2 or raise WrongColor.new(self)
   end
   def should_be_brighter_than(pixel)
-    gray1 = @rgb.inject(0) {|sum,e| sum+e[0]}
-    gray2 = pixel.rgb.inject(0) {|sum,e| sum+e[0]}
+    gray1 = @rgb.inject(0) {|sum,e| sum+e}
+    gray2 = pixel.rgb.inject(0) {|sum,e| sum+e}
     gray1 > gray2 or raise PixelError.new(self,"is not brighter than",pixel)
   end
   def should_be_darker_than(pixel)
-    gray1 = @rgb.inject(0) {|sum,e| sum+e[0]}
-    gray2 = pixel.rgb.inject(0) {|sum,e| sum+e[0]}
+    gray1 = @rgb.inject(0) {|sum,e| sum+e}
+    gray2 = pixel.rgb.inject(0) {|sum,e| sum+e}
     gray1 < gray2 or raise PixelError.new(self,"is not less bright than",pixel)
   end
   def should_be_the_same_as(pixel)
@@ -162,7 +162,7 @@ class DocFile
   end
   def pixel_at(x,y)
     self.render()
-    data = @img.export_pixels_to_str(x, y, 1, 1, "RGB")
+    data = @img.export_pixels(x, y, 1, 1, "RGB")
     return Pixel.new(x,y,data)
   end
 end
diff --git a/spec/transparency.pdf b/spec/transparency.pdf
new file mode 100644 (file)
index 0000000..88378de
Binary files /dev/null and b/spec/transparency.pdf differ
diff --git a/spec/transparency.py b/spec/transparency.py
new file mode 100644 (file)
index 0000000..4bdb4f8
--- /dev/null
@@ -0,0 +1,82 @@
+import sys
+from pdflib_py import * 
+from math import sin,cos
+import Image
+import ImageDraw
+
+img = Image.new("L", (3, 3))
+draw = ImageDraw.Draw(img)
+draw.point((0,1), fill=255)
+draw.point((0,2), fill=255)
+draw.point((1,0), fill=255)
+draw.point((1,2), fill=255)
+draw.point((2,0), fill=255)
+draw.point((2,1), fill=255)
+img.save("/tmp/mask1.png")
+
+img = Image.new("L", (16, 1))
+draw = ImageDraw.Draw(img)
+for i in range(16):
+    draw.point((i,0), fill=i*16)
+img.save("/tmp/mask2.png")
+
+img = Image.new("RGB", (3, 3))
+draw = ImageDraw.Draw(img)
+draw.point((0,1), fill=(0,0,0))
+draw.point((0,2), fill=(255,0,0))
+draw.point((1,0), fill=(0,255,0))
+draw.point((1,2), fill=(0,0,255))
+draw.point((2,0), fill=(255,255,0))
+draw.point((2,1), fill=(0,255,255))
+img.save("/tmp/img1.png")
+
+img = Image.new("RGB", (16, 1))
+draw = ImageDraw.Draw(img)
+for i in range(16):
+    draw.point((i,0), fill=(0,255,0))
+img.save("/tmp/img2.png")
+
+p = PDF_new() 
+PDF_open_file(p, "transparency.pdf")
+font = PDF_load_font(p, "Helvetica", "host", "")
+
+PDF_set_parameter(p, "usercoordinates", "true")
+
+width = 400
+height = 400
+PDF_begin_page(p, width, height)
+
+PDF_setcolor(p, "fill", "rgb", 0.0,0.0,0.0,1.0)
+PDF_moveto(p, 0,0)
+PDF_lineto(p, width, 0)
+PDF_lineto(p, width, height)
+PDF_lineto(p, 0, height)
+PDF_lineto(p, 0, 0)
+PDF_fill(p)
+
+PDF_setfont(p, font, 10.0)
+PDF_setcolor(p, "fill", "rgb", 1.0,1.0,1.0,1.0)
+PDF_set_text_pos(p, 50, 205);PDF_show(p, "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz")
+PDF_set_text_pos(p, 50, 105);PDF_show(p, "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz")
+
+mask = PDF_load_image(p, "png", "/tmp/mask1.png", "mask")
+i = PDF_load_image(p, "png", "/tmp/img1.png", "masked "+str(mask))
+PDF_place_image(p, i, 100, 300, 20)
+
+mask2 = PDF_load_image(p, "png", "/tmp/mask2.png", "mask")
+i2 = PDF_load_image(p, "png", "/tmp/img2.png", "masked "+str(mask2))
+PDF_place_image(p, i2, 0, 200, 25)
+
+PDF_setcolor(p, "fill", "rgb", 1.0,1.0,1.0,1.0)
+gstate = PDF_create_gstate(p, "opacityfill 0.25") # blendmode multiply opacityfill 0.5")
+PDF_set_gstate(p, gstate)
+PDF_moveto(p, 50, 75)
+PDF_lineto(p, 50+300, 75)
+PDF_lineto(p, 50+300, 150)
+PDF_lineto(p, 50, 150)
+PDF_lineto(p, 50, 75)
+PDF_fill(p)
+
+PDF_end_page(p)
+PDF_close(p)
+PDF_delete(p);
diff --git a/spec/transparency.spec.rb b/spec/transparency.spec.rb
new file mode 100644 (file)
index 0000000..f15be8f
--- /dev/null
@@ -0,0 +1,27 @@
+require File.dirname(__FILE__) + '/spec_helper'
+
+describe "pdf conversion" do
+    convert_file "transparency.pdf" do
+        pixel_at(174,135).should_be_of_color 0xff0000
+        pixel_at(167,112).should_be_of_color 0x000000
+        pixel_at(172,75).should_be_of_color 0x000000
+        pixel_at(199,108).should_be_of_color 0x000000
+        pixel_at(234,141).should_be_of_color 0x000000
+        pixel_at(202,142).should_be_of_color 0x0000ff
+        pixel_at(233,111).should_be_of_color 0x00ffff
+        pixel_at(233,71).should_be_of_color 0xffff00
+        pixel_at(199,71).should_be_of_color 0x00ff00
+        pixel_at(594,277).should_be_brighter_than pixel_at(439,279)
+        pixel_at(552,284).should_be_brighter_than pixel_at(361,285)
+        pixel_at(474,294).should_be_brighter_than pixel_at(325,277)
+        pixel_at(283,276).should_be_brighter_than pixel_at(94,277)
+        area_at(86,290,107,301).should_not_be_plain_colored
+        area_at(158,287,186,300).should_not_be_plain_colored
+        area_at(234,288,262,300).should_not_be_plain_colored
+        area_at(312,287,337,300).should_not_be_plain_colored
+        area_at(162,438,244,455).should_not_be_plain_colored
+        pixel_at(201,400).should_be_brighter_than pixel_at(174,345)
+        pixel_at(299,477).should_be_brighter_than pixel_at(237,555)
+        area_at(407,400,435,422).should_be_plain_colored
+    end
+end