1 | <?php |
---|
2 | |
---|
3 | namespace Cerdic\Geometrize; |
---|
4 | |
---|
5 | use \Cerdic\Geometrize\Model; |
---|
6 | use \Cerdic\Geometrize\Exporter\SvgExporter; |
---|
7 | use \Cerdic\Geometrize\Shape\Rectangle; |
---|
8 | |
---|
9 | class ImageRunner { |
---|
10 | |
---|
11 | /** |
---|
12 | * @var \Cerdic\Geometrize\Model |
---|
13 | */ |
---|
14 | protected $model; |
---|
15 | |
---|
16 | /** |
---|
17 | * Geometrization steps : score, color, shape |
---|
18 | * @var array |
---|
19 | */ |
---|
20 | protected $geometrizationSteps = []; |
---|
21 | |
---|
22 | |
---|
23 | /** |
---|
24 | * ImageRunner constructor. |
---|
25 | * @param \Cerdic\Geometrize\Bitmap $inputImage |
---|
26 | * @param int|bool $backgroundColor |
---|
27 | * 32bits encoded color or true for automatic $backgroundColor or false for no background (transparent) |
---|
28 | * @throws \Exception |
---|
29 | */ |
---|
30 | public function __construct($inputImage, $backgroundColor = true){ |
---|
31 | $this->model = new Model($inputImage, $backgroundColor); |
---|
32 | $this->geometrizationSteps = []; |
---|
33 | } |
---|
34 | |
---|
35 | /** |
---|
36 | * @param array $options |
---|
37 | * @param int $nb_steps |
---|
38 | * @return float |
---|
39 | * @throws \Exception |
---|
40 | */ |
---|
41 | public function steps($options, $nb_steps = 1){ |
---|
42 | for ($i=0;$i<$nb_steps;$i++) { |
---|
43 | $this->geometrizationSteps[] = $this->model->step($options['shapeTypes'], $options['alpha'], $options['candidateShapesPerStep'], $options['shapeMutationsPerStep']); |
---|
44 | } |
---|
45 | return $this->getScore(); |
---|
46 | } |
---|
47 | |
---|
48 | /** |
---|
49 | * @param \Cerdic\Geometrize\Bitmap $rescaledImage |
---|
50 | * @throws \Exception |
---|
51 | * @return float |
---|
52 | */ |
---|
53 | public function reScale($rescaledImage) { |
---|
54 | |
---|
55 | $previousSteps = $this->geometrizationSteps; |
---|
56 | $backgroundColor = $this->model->getBackgroundColor(); |
---|
57 | $this->model = new Model($rescaledImage, $backgroundColor); |
---|
58 | $this->geometrizationSteps = []; |
---|
59 | |
---|
60 | if (count($previousSteps)){ |
---|
61 | $w = $rescaledImage->width; |
---|
62 | $h = $rescaledImage->height; |
---|
63 | foreach ($previousSteps as $step) { |
---|
64 | $alpha = $step['shape']->color & 255; |
---|
65 | // rescale the shape on new bounds |
---|
66 | $step['shape']->rescale($w, $h); |
---|
67 | $this->geometrizationSteps[] = $this->model->addShape($step['shape'], $alpha); |
---|
68 | } |
---|
69 | } |
---|
70 | |
---|
71 | return $this->getScore(); |
---|
72 | } |
---|
73 | |
---|
74 | /** |
---|
75 | * Export the result as a SVG |
---|
76 | * @param int $imageWidth |
---|
77 | * @param int $imageHeight |
---|
78 | * @return string |
---|
79 | */ |
---|
80 | public function exportToSVG($imageWidth=0, $imageHeight=0) { |
---|
81 | |
---|
82 | $shapes = array_column($this->geometrizationSteps, 'shape'); |
---|
83 | |
---|
84 | // add background shape if necessary |
---|
85 | $backgroundColor = $this->model->getBackgroundColor(); |
---|
86 | // 0 or false are transparents, nothing needed |
---|
87 | if ($backgroundColor) { |
---|
88 | $backgroundShape = new Rectangle($this->model->getWidth(), $this->model->getHeight(), 1, true); |
---|
89 | $backgroundShape->color = $backgroundColor; |
---|
90 | array_unshift($shapes, $backgroundShape); |
---|
91 | } |
---|
92 | |
---|
93 | $svg_image = SvgExporter::export($shapes, $this->model->getWidth(), $this->model->getHeight(), $imageWidth, $imageHeight); |
---|
94 | return $svg_image; |
---|
95 | |
---|
96 | } |
---|
97 | |
---|
98 | /** |
---|
99 | * Get the current Score (ie score of the last step) |
---|
100 | * @return float |
---|
101 | */ |
---|
102 | public function getScore() { |
---|
103 | |
---|
104 | if (!count($this->geometrizationSteps)) { |
---|
105 | return 1.0; |
---|
106 | } |
---|
107 | $last = end($this->geometrizationSteps); |
---|
108 | return $last['score']; |
---|
109 | } |
---|
110 | |
---|
111 | |
---|
112 | /** |
---|
113 | * Get the current number of steps |
---|
114 | * @return int |
---|
115 | */ |
---|
116 | public function getNbSteps() { |
---|
117 | return count($this->geometrizationSteps); |
---|
118 | } |
---|
119 | |
---|
120 | |
---|
121 | /** |
---|
122 | * Get the results |
---|
123 | * @return array |
---|
124 | */ |
---|
125 | public function getResults() { |
---|
126 | return $this->geometrizationSteps; |
---|
127 | } |
---|
128 | |
---|
129 | /** |
---|
130 | * @return \Cerdic\Geometrize\Bitmap |
---|
131 | * @throws \Exception |
---|
132 | */ |
---|
133 | public function getImageData(){ |
---|
134 | if (!($this->model!==null)){ |
---|
135 | throw new \Exception("FAIL: model != null"); |
---|
136 | } |
---|
137 | return $this->model->getCurrent(); |
---|
138 | } |
---|
139 | |
---|
140 | /** |
---|
141 | * @return \Cerdic\Geometrize\Model |
---|
142 | */ |
---|
143 | public function getModel() { |
---|
144 | return $this->model; |
---|
145 | } |
---|
146 | |
---|
147 | } |
---|