1 module qrcode.renderer.image.png;
2 
3 import qrcode.renderer.image.abstractrender;
4 import qrcode.renderer.color.colorinterface;
5 
6 import dmagick.Image;
7 import dmagick.Geometry;
8 import dmagick.Color;
9 import dmagick.DrawingContext;
10 
11 
12 import qrcode.renderer.color.colorinterface;
13 
14 /**
15 * PNG backend.
16 */
17 class Png : AbstractRenderer
18 {
19 	 
20 	protected Image image;
21 
22 	protected DrawingContext draw;
23 
24     /**
25 	* Colors used for drawing.
26 	*
27 	* @var array
28 	*/
29     protected  Color[string] colors;
30 
31 	protected Image mergeImage;
32 
33 
34 	/**
35 	* init(): defined by RendererInterface.
36 	*
37 	* @see    ImageRendererInterface::init()
38 	* @return void
39 	*/
40     public override void  initRender()
41     {
42 		Geometry geo;
43 		geo.width = this.finalWidth;
44 		geo.height = this.finalHeight;
45 
46 		auto ci =  this.getBackgroundColor();
47         this.image = new Image(geo, this.getMagicColor(ci));
48 		this.draw = new DrawingContext();
49     }
50 
51 	private Color getMagicColor(ColorInterface ci)
52 	{
53 		import dmagick.ColorRGB;
54 		auto tmp = ci.toRgb();
55 		return new ColorRGB(tmp.getRed(), tmp.getGreen(),tmp.getBlue);
56 	}
57 
58 	
59 	/**
60 	* addColor(): defined by RendererInterface.
61 	*
62 	* @see    ImageRendererInterface::addColor()
63 	* @param  string         $id
64 	* @param  ColorInterface $color
65 	* @return void
66 	* @throws Exception\RuntimeException
67 	*/
68 	public override void addColor(string id, ColorInterface color)
69 	{
70 		this.colors[id] = this.getMagicColor(color);
71 	}
72     /**
73 	* drawBackground(): defined by RendererInterface.
74 	*
75 	* @see    ImageRendererInterface::drawBackground()
76 	* @param  string $colorId
77 	* @return void
78 	*/
79     public override void drawBackground(string colorId)
80     {
81 
82     }
83     /**
84 	* drawBlock(): defined by RendererInterface.
85 	*
86 	* @see    ImageRendererInterface::drawBlock()
87 	* @param  integer $x
88 	* @param  integer $y
89 	* @param  string  $colorId
90 	* @return void
91 	*/
92     public override void drawBlock(int x, int y, string colorId)
93     {
94         this.draw.rectangle(x,y,x+this.blockSize-1, y + this.blockSize -1);
95 		this.draw.fill(this.colors[colorId]);
96     }
97     /**
98 	* getByteStream(): defined by RendererInterface.
99 	*
100 	* @see    ImageRendererInterface::getByteStream()
101 	* @return string
102 	*/
103     public override string getByteStream()
104     {
105 		this.draw.draw(this.image);
106 		if(this.mergeImage !is null)
107 		{
108 			auto sourceSize = this.image.size();
109 			auto mergeSize = this.mergeImage.size();
110 
111 			auto centerY = (sourceSize.height / 2) - (mergeSize.height / 2);
112 			auto centerX = (sourceSize.width / 2) - (mergeSize.width / 2);
113 			
114 			import dmagick.c.composite;
115 			this.image.composite(cast(const)this.mergeImage, CompositeOperator.AddCompositeOp, cast(long)centerX,cast(long)centerY);
116 		}
117 
118 		return cast(string)(this.image.toBlob("png"));
119 		//return string.init;
120     }
121 
122 	public void setMergeImage(string merge_file)
123 	{
124 		this.mergeImage = new Image(merge_file);
125 	}
126 }