| 1 | /*************************************************************************** |
|---|
| 2 | * graphicsDraw.cc |
|---|
| 3 | * |
|---|
| 4 | * Thu Nov 23 2007 |
|---|
| 5 | * Copyright 2007 Dmytro Milinevskyy |
|---|
| 6 | * milinevskyy@gmail.com |
|---|
| 7 | ****************************************************************************/ |
|---|
| 8 | |
|---|
| 9 | /* |
|---|
| 10 | * This program is free software; you can redistribute it and/or modify |
|---|
| 11 | * it under the terms of the GNU Lesser General Public License version 2.1 as published by |
|---|
| 12 | * the Free Software Foundation; |
|---|
| 13 | * |
|---|
| 14 | * This program is distributed in the hope that it will be useful, |
|---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 17 | * GNU Library General Public License for more details. |
|---|
| 18 | * |
|---|
| 19 | * You should have received a copy of the GNU Lesser General Public License |
|---|
| 20 | * along with this program; if not, write to the Free Software |
|---|
| 21 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 22 | */ |
|---|
| 23 | |
|---|
| 24 | /** |
|---|
| 25 | * vim indentation settings |
|---|
| 26 | * set tabstop=4 |
|---|
| 27 | * set shiftwidth=4 |
|---|
| 28 | */ |
|---|
| 29 | |
|---|
| 30 | #include <libdodo/directives.h> |
|---|
| 31 | |
|---|
| 32 | #ifdef IMAGEMAGICK_EXT |
|---|
| 33 | #ifndef IMAGEMAGICK_PRE_63 |
|---|
| 34 | #include <magick/MagickCore.h> |
|---|
| 35 | #else |
|---|
| 36 | #include <magick/ImageMagick.h> |
|---|
| 37 | #endif |
|---|
| 38 | #include <math.h> |
|---|
| 39 | |
|---|
| 40 | #include "graphicsImage.inline" |
|---|
| 41 | |
|---|
| 42 | #include <libdodo/graphicsDraw.h> |
|---|
| 43 | #include <libdodo/types.h> |
|---|
| 44 | #include <libdodo/graphicsImage.h> |
|---|
| 45 | #include <libdodo/graphicsColor.h> |
|---|
| 46 | #include <libdodo/graphicsDrawEx.h> |
|---|
| 47 | |
|---|
| 48 | using namespace dodo::graphics; |
|---|
| 49 | |
|---|
| 50 | point::point(unsigned long x, unsigned long y) : x(x), |
|---|
| 51 | y(y) |
|---|
| 52 | { |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | //------------------------------------------------------------------- |
|---|
| 56 | |
|---|
| 57 | void |
|---|
| 58 | draw::primitive(graphics::image &image, |
|---|
| 59 | char *description, |
|---|
| 60 | const __color__ &fillColor, |
|---|
| 61 | const __color__ &borderColor, |
|---|
| 62 | unsigned short borderWidth) |
|---|
| 63 | { |
|---|
| 64 | if (image.collectedData.handle->im == NULL) |
|---|
| 65 | dodo_throw exception::basic(exception::MODULE_GRAPHICSDRAW, DRAWEX_PRIMITIVE, exception::ERRNO_IMAGEMAGICK, DRAWEX_EMPTYIMAGE, GRAPHICSDRAWEX_EMPTYIMAGE_STR, __LINE__, __FILE__); |
|---|
| 66 | |
|---|
| 67 | #ifndef IMAGEMAGICK_PRE_63 |
|---|
| 68 | DrawInfo *di = AcquireDrawInfo(); |
|---|
| 69 | #else |
|---|
| 70 | DrawInfo *di = CloneDrawInfo(image.collectedData.handle->imInfo, NULL); |
|---|
| 71 | #endif |
|---|
| 72 | |
|---|
| 73 | di->primitive = description; |
|---|
| 74 | |
|---|
| 75 | di->stroke.red = borderColor.red; |
|---|
| 76 | di->stroke.green = borderColor.green; |
|---|
| 77 | di->stroke.blue = borderColor.blue; |
|---|
| 78 | di->stroke.opacity = borderColor.opacity; |
|---|
| 79 | |
|---|
| 80 | di->stroke_width = borderWidth; |
|---|
| 81 | |
|---|
| 82 | di->fill.red = fillColor.red; |
|---|
| 83 | di->fill.green = fillColor.green; |
|---|
| 84 | di->fill.blue = fillColor.blue; |
|---|
| 85 | di->fill.opacity = fillColor.opacity; |
|---|
| 86 | |
|---|
| 87 | if (DrawImage(image.collectedData.handle->im, di) == MagickFalse) { |
|---|
| 88 | di->primitive = NULL; |
|---|
| 89 | DestroyDrawInfo(di); |
|---|
| 90 | |
|---|
| 91 | dodo_throw exception::basic(exception::MODULE_GRAPHICSDRAW, DRAWEX_PRIMITIVE, exception::ERRNO_IMAGEMAGICK, DRAWEX_CANNOTDRAWPRIMITIVE, GRAPHICSDRAWEX_CANNOTDRAWPRIMITIVE_STR, __LINE__, __FILE__); |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | di->primitive = NULL; |
|---|
| 95 | DestroyDrawInfo(di); |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | //------------------------------------------------------------------- |
|---|
| 99 | |
|---|
| 100 | void |
|---|
| 101 | draw::circle(graphics::image &image, |
|---|
| 102 | const graphics::point ¢er, |
|---|
| 103 | unsigned long radius, |
|---|
| 104 | const __color__ &fillColor, |
|---|
| 105 | const __color__ &borderColor, |
|---|
| 106 | unsigned short borderWidth) |
|---|
| 107 | { |
|---|
| 108 | char description[128]; |
|---|
| 109 | snprintf(description, 128, "circle %ld,%ld %ld,%ld", center.x, center.y, center.x + radius, center.y); |
|---|
| 110 | |
|---|
| 111 | primitive(image, description, fillColor, borderColor, borderWidth); |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | //------------------------------------------------------------------- |
|---|
| 115 | |
|---|
| 116 | void |
|---|
| 117 | draw::line(graphics::image &image, |
|---|
| 118 | const dodoArray<graphics::point> &points, |
|---|
| 119 | const __color__ &lineColor, |
|---|
| 120 | unsigned short lineWidth) |
|---|
| 121 | { |
|---|
| 122 | char pointDesc[128]; |
|---|
| 123 | |
|---|
| 124 | dodo::string description = "polyline"; |
|---|
| 125 | |
|---|
| 126 | dodoArray<graphics::point>::const_iterator i = points.begin(), j = points.end(); |
|---|
| 127 | for (; i != j; ++i) { |
|---|
| 128 | snprintf(pointDesc, 128, " %ld,%ld", i->x, i->y); |
|---|
| 129 | |
|---|
| 130 | description += pointDesc; |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | primitive(image, (char *)description.data(), color::transparent, lineColor, lineWidth); |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | //------------------------------------------------------------------- |
|---|
| 137 | |
|---|
| 138 | void |
|---|
| 139 | draw::rectangle(graphics::image &image, |
|---|
| 140 | const graphics::point &tl, |
|---|
| 141 | const graphics::point &br, |
|---|
| 142 | const __color__ &fillColor, |
|---|
| 143 | const __color__ &borderColor, |
|---|
| 144 | unsigned short borderWidth) |
|---|
| 145 | { |
|---|
| 146 | char description[128]; |
|---|
| 147 | snprintf(description, 128, "rectangle %ld,%ld %ld,%ld", tl.x, tl.y, br.x, br.y); |
|---|
| 148 | |
|---|
| 149 | primitive(image, description, fillColor, borderColor, borderWidth); |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | //------------------------------------------------------------------- |
|---|
| 153 | |
|---|
| 154 | void |
|---|
| 155 | draw::text(graphics::image &image, |
|---|
| 156 | const graphics::point &position, |
|---|
| 157 | const dodo::string &text, |
|---|
| 158 | const dodo::string &font, |
|---|
| 159 | unsigned short fontWidth, |
|---|
| 160 | const __color__ &fillColor, |
|---|
| 161 | const __color__ &borderColor, |
|---|
| 162 | unsigned short borderWidth, |
|---|
| 163 | double angle) |
|---|
| 164 | { |
|---|
| 165 | if (image.collectedData.handle->im == NULL) |
|---|
| 166 | dodo_throw exception::basic(exception::MODULE_GRAPHICSDRAW, DRAWEX_TEXT, exception::ERRNO_IMAGEMAGICK, DRAWEX_EMPTYIMAGE, GRAPHICSDRAWEX_EMPTYIMAGE_STR, __LINE__, __FILE__); |
|---|
| 167 | |
|---|
| 168 | dodo::string txt = "text 0,0 \""; |
|---|
| 169 | txt += text; |
|---|
| 170 | txt += "\""; |
|---|
| 171 | |
|---|
| 172 | #ifndef IMAGEMAGICK_PRE_63 |
|---|
| 173 | DrawInfo *di = AcquireDrawInfo(); |
|---|
| 174 | #else |
|---|
| 175 | DrawInfo *di = CloneDrawInfo(image.collectedData.handle->imInfo, NULL); |
|---|
| 176 | #endif |
|---|
| 177 | |
|---|
| 178 | double radians = angle * M_PI / 180; |
|---|
| 179 | double _cos = cos(radians); |
|---|
| 180 | double _sin = sin(radians); |
|---|
| 181 | |
|---|
| 182 | AffineMatrix affine; |
|---|
| 183 | |
|---|
| 184 | affine.sx = _cos; |
|---|
| 185 | affine.rx = _sin; |
|---|
| 186 | affine.ry = -1 * _sin; |
|---|
| 187 | affine.sy = _cos; |
|---|
| 188 | affine.tx = position.x; |
|---|
| 189 | affine.ty = position.y; |
|---|
| 190 | |
|---|
| 191 | AffineMatrix current = di->affine; |
|---|
| 192 | |
|---|
| 193 | di->affine.sx = current.sx * affine.sx + current.ry * affine.rx; |
|---|
| 194 | di->affine.rx = current.rx * affine.sx + current.sy * affine.rx; |
|---|
| 195 | |
|---|
| 196 | di->affine.ry = current.sx * affine.ry + current.ry * affine.sy; |
|---|
| 197 | di->affine.sy = current.rx * affine.ry + current.sy * affine.sy; |
|---|
| 198 | |
|---|
| 199 | di->affine.tx = current.sx * affine.tx + current.ry * affine.ty + current.tx; |
|---|
| 200 | di->affine.ty = current.rx * affine.tx + current.sy * affine.ty + current.ty; |
|---|
| 201 | |
|---|
| 202 | di->primitive = (char *)txt.data(); |
|---|
| 203 | |
|---|
| 204 | di->font = (char *)font.data(); |
|---|
| 205 | di->pointsize = fontWidth; |
|---|
| 206 | |
|---|
| 207 | di->stroke.red = borderColor.red; |
|---|
| 208 | di->stroke.green = borderColor.green; |
|---|
| 209 | di->stroke.blue = borderColor.blue; |
|---|
| 210 | di->stroke.opacity = borderColor.opacity; |
|---|
| 211 | |
|---|
| 212 | di->stroke_width = borderWidth; |
|---|
| 213 | |
|---|
| 214 | di->fill.red = fillColor.red; |
|---|
| 215 | di->fill.green = fillColor.green; |
|---|
| 216 | di->fill.blue = fillColor.blue; |
|---|
| 217 | di->fill.opacity = fillColor.opacity; |
|---|
| 218 | |
|---|
| 219 | if (DrawImage(image.collectedData.handle->im, di) == MagickFalse) { |
|---|
| 220 | di->primitive = NULL; |
|---|
| 221 | di->font = NULL; |
|---|
| 222 | DestroyDrawInfo(di); |
|---|
| 223 | |
|---|
| 224 | dodo_throw exception::basic(exception::MODULE_GRAPHICSDRAW, DRAWEX_TEXT, exception::ERRNO_IMAGEMAGICK, DRAWEX_CANNOTDRAWPRIMITIVE, GRAPHICSDRAWEX_CANNOTDRAWPRIMITIVE_STR, __LINE__, __FILE__); |
|---|
| 225 | } |
|---|
| 226 | |
|---|
| 227 | di->primitive = NULL; |
|---|
| 228 | di->font = NULL; |
|---|
| 229 | DestroyDrawInfo(di); |
|---|
| 230 | } |
|---|
| 231 | |
|---|
| 232 | //------------------------------------------------------------------- |
|---|
| 233 | |
|---|
| 234 | void |
|---|
| 235 | draw::image(graphics::image &image, |
|---|
| 236 | const graphics::point &position, |
|---|
| 237 | const graphics::image &a_im, |
|---|
| 238 | double angle) |
|---|
| 239 | { |
|---|
| 240 | if (image.collectedData.handle->im == NULL) |
|---|
| 241 | dodo_throw exception::basic(exception::MODULE_GRAPHICSDRAW, DRAWEX_IMAGE, exception::ERRNO_IMAGEMAGICK, DRAWEX_EMPTYIMAGE, GRAPHICSDRAWEX_EMPTYIMAGE_STR, __LINE__, __FILE__); |
|---|
| 242 | |
|---|
| 243 | AffineMatrix current; |
|---|
| 244 | GetAffineMatrix(¤t); |
|---|
| 245 | |
|---|
| 246 | double radians = angle * M_PI / 180; |
|---|
| 247 | double _cos = cos(radians); |
|---|
| 248 | double _sin = sin(radians); |
|---|
| 249 | |
|---|
| 250 | AffineMatrix affine; |
|---|
| 251 | |
|---|
| 252 | affine.sx = _cos; |
|---|
| 253 | affine.rx = _sin; |
|---|
| 254 | affine.ry = -1 * _sin; |
|---|
| 255 | affine.sy = _cos; |
|---|
| 256 | affine.tx = position.x; |
|---|
| 257 | affine.ty = position.y; |
|---|
| 258 | |
|---|
| 259 | AffineMatrix _current = current; |
|---|
| 260 | |
|---|
| 261 | current.sx = _current.sx * affine.sx + _current.ry * affine.rx; |
|---|
| 262 | current.rx = _current.rx * affine.sx + _current.sy * affine.rx; |
|---|
| 263 | |
|---|
| 264 | current.ry = _current.sx * affine.ry + _current.ry * affine.sy; |
|---|
| 265 | current.sy = _current.rx * affine.ry + _current.sy * affine.sy; |
|---|
| 266 | |
|---|
| 267 | current.tx = _current.sx * affine.tx + _current.ry * affine.ty + _current.tx; |
|---|
| 268 | current.ty = _current.rx * affine.tx + _current.sy * affine.ty + _current.ty; |
|---|
| 269 | |
|---|
| 270 | if (DrawAffineImage(image.collectedData.handle->im, a_im.collectedData.handle->im, ¤t) == MagickFalse) |
|---|
| 271 | dodo_throw exception::basic(exception::MODULE_GRAPHICSDRAW, DRAWEX_IMAGE, exception::ERRNO_IMAGEMAGICK, DRAWEX_CANNOTDRAWPRIMITIVE, GRAPHICSDRAWEX_CANNOTDRAWPRIMITIVE_STR, __LINE__, __FILE__); |
|---|
| 272 | } |
|---|
| 273 | |
|---|
| 274 | //------------------------------------------------------------------- |
|---|
| 275 | |
|---|
| 276 | void |
|---|
| 277 | draw::point(graphics::image &image, |
|---|
| 278 | const graphics::point &position, |
|---|
| 279 | const __color__ &pointColor, |
|---|
| 280 | unsigned short pointWidth) |
|---|
| 281 | { |
|---|
| 282 | if (image.collectedData.handle->im == NULL) |
|---|
| 283 | dodo_throw exception::basic(exception::MODULE_GRAPHICSDRAW, DRAWEX_POINT, exception::ERRNO_IMAGEMAGICK, DRAWEX_EMPTYIMAGE, GRAPHICSDRAWEX_EMPTYIMAGE_STR, __LINE__, __FILE__); |
|---|
| 284 | |
|---|
| 285 | char description[128]; |
|---|
| 286 | |
|---|
| 287 | if (pointWidth == 1) |
|---|
| 288 | snprintf(description, 128, "point %ld,%ld", position.x, position.y); |
|---|
| 289 | else |
|---|
| 290 | snprintf(description, 128, "circle %ld,%ld %ld,%ld", position.x, position.y, position.x + pointWidth, position.y); |
|---|
| 291 | |
|---|
| 292 | #ifndef IMAGEMAGICK_PRE_63 |
|---|
| 293 | DrawInfo *di = AcquireDrawInfo(); |
|---|
| 294 | #else |
|---|
| 295 | DrawInfo *di = CloneDrawInfo(image.collectedData.handle->imInfo, NULL); |
|---|
| 296 | #endif |
|---|
| 297 | |
|---|
| 298 | di->primitive = description; |
|---|
| 299 | |
|---|
| 300 | di->fill.red = pointColor.red; |
|---|
| 301 | di->fill.green = pointColor.green; |
|---|
| 302 | di->fill.blue = pointColor.blue; |
|---|
| 303 | di->fill.opacity = pointColor.opacity; |
|---|
| 304 | |
|---|
| 305 | if (DrawImage(image.collectedData.handle->im, di) == MagickFalse) { |
|---|
| 306 | di->primitive = NULL; |
|---|
| 307 | DestroyDrawInfo(di); |
|---|
| 308 | |
|---|
| 309 | dodo_throw exception::basic(exception::MODULE_GRAPHICSDRAW, DRAWEX_POINT, exception::ERRNO_IMAGEMAGICK, DRAWEX_CANNOTDRAWPRIMITIVE, GRAPHICSDRAWEX_CANNOTDRAWPRIMITIVE_STR, __LINE__, __FILE__); |
|---|
| 310 | } |
|---|
| 311 | |
|---|
| 312 | di->primitive = NULL; |
|---|
| 313 | DestroyDrawInfo(di); |
|---|
| 314 | } |
|---|
| 315 | #endif |
|---|
| 316 | |
|---|
| 317 | //------------------------------------------------------------------- |
|---|
| 318 | |
|---|