gd_rotate.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. #if HAVE_GD_BUNDLED
  2. # include "gd.h"
  3. #else
  4. # include <gd.h>
  5. #endif
  6. #include "gd_intern.h"
  7. #include <math.h>
  8. /*
  9. * Rotate function Added on 2003/12
  10. * by Pierre-Alain Joye (pierre@php.net)
  11. **/
  12. /* Begin rotate function */
  13. #ifdef ROTATE_PI
  14. #undef ROTATE_PI
  15. #endif /* ROTATE_PI */
  16. #define ROTATE_DEG2RAD 3.1415926535897932384626433832795/180
  17. void gdImageSkewX (gdImagePtr dst, gdImagePtr src, int uRow, int iOffset, double dWeight, int clrBack, int ignoretransparent)
  18. {
  19. typedef int (*FuncPtr)(gdImagePtr, int, int);
  20. int i, r, g, b, a, clrBackR, clrBackG, clrBackB, clrBackA;
  21. FuncPtr f;
  22. int pxlOldLeft, pxlLeft=0, pxlSrc;
  23. /* Keep clrBack as color index if required */
  24. if (src->trueColor) {
  25. pxlOldLeft = clrBack;
  26. f = gdImageGetTrueColorPixel;
  27. } else {
  28. pxlOldLeft = clrBack;
  29. clrBackR = gdImageRed(src, clrBack);
  30. clrBackG = gdImageGreen(src, clrBack);
  31. clrBackB = gdImageBlue(src, clrBack);
  32. clrBackA = gdImageAlpha(src, clrBack);
  33. clrBack = gdTrueColorAlpha(clrBackR, clrBackG, clrBackB, clrBackA);
  34. f = gdImageGetPixel;
  35. }
  36. for (i = 0; i < iOffset; i++) {
  37. gdImageSetPixel (dst, i, uRow, clrBack);
  38. }
  39. if (i < dst->sx) {
  40. gdImageSetPixel (dst, i, uRow, clrBack);
  41. }
  42. for (i = 0; i < src->sx; i++) {
  43. pxlSrc = f (src,i,uRow);
  44. r = (int)(gdImageRed(src,pxlSrc) * dWeight);
  45. g = (int)(gdImageGreen(src,pxlSrc) * dWeight);
  46. b = (int)(gdImageBlue(src,pxlSrc) * dWeight);
  47. a = (int)(gdImageAlpha(src,pxlSrc) * dWeight);
  48. pxlLeft = gdImageColorAllocateAlpha(src, r, g, b, a);
  49. if (pxlLeft == -1) {
  50. pxlLeft = gdImageColorClosestAlpha(src, r, g, b, a);
  51. }
  52. r = gdImageRed(src,pxlSrc) - (gdImageRed(src,pxlLeft) - gdImageRed(src,pxlOldLeft));
  53. g = gdImageGreen(src,pxlSrc) - (gdImageGreen(src,pxlLeft) - gdImageGreen(src,pxlOldLeft));
  54. b = gdImageBlue(src,pxlSrc) - (gdImageBlue(src,pxlLeft) - gdImageBlue(src,pxlOldLeft));
  55. a = gdImageAlpha(src,pxlSrc) - (gdImageAlpha(src,pxlLeft) - gdImageAlpha(src,pxlOldLeft));
  56. if (r>255) {
  57. r = 255;
  58. }
  59. if (g>255) {
  60. g = 255;
  61. }
  62. if (b>255) {
  63. b = 255;
  64. }
  65. if (a>127) {
  66. a = 127;
  67. }
  68. if (ignoretransparent && pxlSrc == dst->transparent) {
  69. pxlSrc = dst->transparent;
  70. } else {
  71. pxlSrc = gdImageColorAllocateAlpha(dst, r, g, b, a);
  72. if (pxlSrc == -1) {
  73. pxlSrc = gdImageColorClosestAlpha(dst, r, g, b, a);
  74. }
  75. }
  76. if ((i + iOffset >= 0) && (i + iOffset < dst->sx)) {
  77. gdImageSetPixel (dst, i+iOffset, uRow, pxlSrc);
  78. }
  79. pxlOldLeft = pxlLeft;
  80. }
  81. i += iOffset;
  82. if (i < dst->sx) {
  83. gdImageSetPixel (dst, i, uRow, pxlLeft);
  84. }
  85. gdImageSetPixel (dst, iOffset, uRow, clrBack);
  86. i--;
  87. while (++i < dst->sx) {
  88. gdImageSetPixel (dst, i, uRow, clrBack);
  89. }
  90. }
  91. void gdImageSkewY (gdImagePtr dst, gdImagePtr src, int uCol, int iOffset, double dWeight, int clrBack, int ignoretransparent)
  92. {
  93. typedef int (*FuncPtr)(gdImagePtr, int, int);
  94. int i, iYPos=0, r, g, b, a;
  95. FuncPtr f;
  96. int pxlOldLeft, pxlLeft=0, pxlSrc;
  97. if (src->trueColor) {
  98. f = gdImageGetTrueColorPixel;
  99. } else {
  100. f = gdImageGetPixel;
  101. }
  102. for (i = 0; i<=iOffset; i++) {
  103. gdImageSetPixel (dst, uCol, i, clrBack);
  104. }
  105. r = (int)((double)gdImageRed(src,clrBack) * dWeight);
  106. g = (int)((double)gdImageGreen(src,clrBack) * dWeight);
  107. b = (int)((double)gdImageBlue(src,clrBack) * dWeight);
  108. a = (int)((double)gdImageAlpha(src,clrBack) * dWeight);
  109. pxlOldLeft = gdImageColorAllocateAlpha(dst, r, g, b, a);
  110. for (i = 0; i < src->sy; i++) {
  111. pxlSrc = f (src, uCol, i);
  112. iYPos = i + iOffset;
  113. r = (int)((double)gdImageRed(src,pxlSrc) * dWeight);
  114. g = (int)((double)gdImageGreen(src,pxlSrc) * dWeight);
  115. b = (int)((double)gdImageBlue(src,pxlSrc) * dWeight);
  116. a = (int)((double)gdImageAlpha(src,pxlSrc) * dWeight);
  117. pxlLeft = gdImageColorAllocateAlpha(src, r, g, b, a);
  118. if (pxlLeft == -1) {
  119. pxlLeft = gdImageColorClosestAlpha(src, r, g, b, a);
  120. }
  121. r = gdImageRed(src,pxlSrc) - (gdImageRed(src,pxlLeft) - gdImageRed(src,pxlOldLeft));
  122. g = gdImageGreen(src,pxlSrc) - (gdImageGreen(src,pxlLeft) - gdImageGreen(src,pxlOldLeft));
  123. b = gdImageBlue(src,pxlSrc) - (gdImageBlue(src,pxlLeft) - gdImageBlue(src,pxlOldLeft));
  124. a = gdImageAlpha(src,pxlSrc) - (gdImageAlpha(src,pxlLeft) - gdImageAlpha(src,pxlOldLeft));
  125. if (r>255) {
  126. r = 255;
  127. }
  128. if (g>255) {
  129. g = 255;
  130. }
  131. if (b>255) {
  132. b = 255;
  133. }
  134. if (a>127) {
  135. a = 127;
  136. }
  137. if (ignoretransparent && pxlSrc == dst->transparent) {
  138. pxlSrc = dst->transparent;
  139. } else {
  140. pxlSrc = gdImageColorAllocateAlpha(dst, r, g, b, a);
  141. if (pxlSrc == -1) {
  142. pxlSrc = gdImageColorClosestAlpha(dst, r, g, b, a);
  143. }
  144. }
  145. if ((iYPos >= 0) && (iYPos < dst->sy)) {
  146. gdImageSetPixel (dst, uCol, iYPos, pxlSrc);
  147. }
  148. pxlOldLeft = pxlLeft;
  149. }
  150. i = iYPos;
  151. if (i < dst->sy) {
  152. gdImageSetPixel (dst, uCol, i, pxlLeft);
  153. }
  154. i--;
  155. while (++i < dst->sy) {
  156. gdImageSetPixel (dst, uCol, i, clrBack);
  157. }
  158. }
  159. /* Rotates an image by 90 degrees (counter clockwise) */
  160. gdImagePtr gdImageRotate90 (gdImagePtr src, int ignoretransparent)
  161. {
  162. int uY, uX;
  163. int c,r,g,b,a;
  164. gdImagePtr dst;
  165. typedef int (*FuncPtr)(gdImagePtr, int, int);
  166. FuncPtr f;
  167. if (src->trueColor) {
  168. f = gdImageGetTrueColorPixel;
  169. } else {
  170. f = gdImageGetPixel;
  171. }
  172. dst = gdImageCreateTrueColor(src->sy, src->sx);
  173. if (dst != NULL) {
  174. int old_blendmode = dst->alphaBlendingFlag;
  175. dst->alphaBlendingFlag = 0;
  176. dst->transparent = src->transparent;
  177. gdImagePaletteCopy (dst, src);
  178. for (uY = 0; uY<src->sy; uY++) {
  179. for (uX = 0; uX<src->sx; uX++) {
  180. c = f (src, uX, uY);
  181. if (!src->trueColor) {
  182. r = gdImageRed(src,c);
  183. g = gdImageGreen(src,c);
  184. b = gdImageBlue(src,c);
  185. a = gdImageAlpha(src,c);
  186. c = gdTrueColorAlpha(r, g, b, a);
  187. }
  188. if (ignoretransparent && c == dst->transparent) {
  189. gdImageSetPixel(dst, uY, (dst->sy - uX - 1), dst->transparent);
  190. } else {
  191. gdImageSetPixel(dst, uY, (dst->sy - uX - 1), c);
  192. }
  193. }
  194. }
  195. dst->alphaBlendingFlag = old_blendmode;
  196. }
  197. return dst;
  198. }
  199. /* Rotates an image by 180 degrees (counter clockwise) */
  200. gdImagePtr gdImageRotate180 (gdImagePtr src, int ignoretransparent)
  201. {
  202. int uY, uX;
  203. int c,r,g,b,a;
  204. gdImagePtr dst;
  205. typedef int (*FuncPtr)(gdImagePtr, int, int);
  206. FuncPtr f;
  207. if (src->trueColor) {
  208. f = gdImageGetTrueColorPixel;
  209. } else {
  210. f = gdImageGetPixel;
  211. }
  212. dst = gdImageCreateTrueColor(src->sx, src->sy);
  213. if (dst != NULL) {
  214. int old_blendmode = dst->alphaBlendingFlag;
  215. dst->alphaBlendingFlag = 0;
  216. dst->transparent = src->transparent;
  217. gdImagePaletteCopy (dst, src);
  218. for (uY = 0; uY<src->sy; uY++) {
  219. for (uX = 0; uX<src->sx; uX++) {
  220. c = f (src, uX, uY);
  221. if (!src->trueColor) {
  222. r = gdImageRed(src,c);
  223. g = gdImageGreen(src,c);
  224. b = gdImageBlue(src,c);
  225. a = gdImageAlpha(src,c);
  226. c = gdTrueColorAlpha(r, g, b, a);
  227. }
  228. if (ignoretransparent && c == dst->transparent) {
  229. gdImageSetPixel(dst, (dst->sx - uX - 1), (dst->sy - uY - 1), dst->transparent);
  230. } else {
  231. gdImageSetPixel(dst, (dst->sx - uX - 1), (dst->sy - uY - 1), c);
  232. }
  233. }
  234. }
  235. dst->alphaBlendingFlag = old_blendmode;
  236. }
  237. return dst;
  238. }
  239. /* Rotates an image by 270 degrees (counter clockwise) */
  240. gdImagePtr gdImageRotate270 (gdImagePtr src, int ignoretransparent)
  241. {
  242. int uY, uX;
  243. int c,r,g,b,a;
  244. gdImagePtr dst;
  245. typedef int (*FuncPtr)(gdImagePtr, int, int);
  246. FuncPtr f;
  247. if (src->trueColor) {
  248. f = gdImageGetTrueColorPixel;
  249. } else {
  250. f = gdImageGetPixel;
  251. }
  252. dst = gdImageCreateTrueColor (src->sy, src->sx);
  253. if (dst != NULL) {
  254. int old_blendmode = dst->alphaBlendingFlag;
  255. dst->alphaBlendingFlag = 0;
  256. dst->transparent = src->transparent;
  257. gdImagePaletteCopy (dst, src);
  258. for (uY = 0; uY<src->sy; uY++) {
  259. for (uX = 0; uX<src->sx; uX++) {
  260. c = f (src, uX, uY);
  261. if (!src->trueColor) {
  262. r = gdImageRed(src,c);
  263. g = gdImageGreen(src,c);
  264. b = gdImageBlue(src,c);
  265. a = gdImageAlpha(src,c);
  266. c = gdTrueColorAlpha(r, g, b, a);
  267. }
  268. if (ignoretransparent && c == dst->transparent) {
  269. gdImageSetPixel(dst, (dst->sx - uY - 1), uX, dst->transparent);
  270. } else {
  271. gdImageSetPixel(dst, (dst->sx - uY - 1), uX, c);
  272. }
  273. }
  274. }
  275. dst->alphaBlendingFlag = old_blendmode;
  276. }
  277. return dst;
  278. }
  279. gdImagePtr gdImageRotate45 (gdImagePtr src, double dAngle, int clrBack, int ignoretransparent)
  280. {
  281. typedef int (*FuncPtr)(gdImagePtr, int, int);
  282. gdImagePtr dst1,dst2,dst3;
  283. FuncPtr f;
  284. double dRadAngle, dSinE, dTan, dShear;
  285. double dOffset; /* Variable skew offset */
  286. int u, iShear, newx, newy;
  287. int clrBackR, clrBackG, clrBackB, clrBackA;
  288. /* See GEMS I for the algorithm details */
  289. dRadAngle = dAngle * ROTATE_DEG2RAD; /* Angle in radians */
  290. dSinE = sin (dRadAngle);
  291. dTan = tan (dRadAngle / 2.0);
  292. newx = (int)(src->sx + src->sy * fabs(dTan));
  293. newy = src->sy;
  294. /* 1st shear */
  295. if (src->trueColor) {
  296. f = gdImageGetTrueColorPixel;
  297. } else {
  298. f = gdImageGetPixel;
  299. }
  300. dst1 = gdImageCreateTrueColor(newx, newy);
  301. /******* Perform 1st shear (horizontal) ******/
  302. if (dst1 == NULL) {
  303. return NULL;
  304. }
  305. #ifdef HAVE_GD_BUNDLED
  306. dst1->alphaBlendingFlag = gdEffectReplace;
  307. #else
  308. gdImageAlphaBlending(dst1, 0);
  309. #endif
  310. if (dAngle == 0.0) {
  311. /* Returns copy of src */
  312. gdImageCopy (dst1, src,0,0,0,0,src->sx,src->sy);
  313. return dst1;
  314. }
  315. gdImagePaletteCopy (dst1, src);
  316. if (ignoretransparent) {
  317. if (gdImageTrueColor(src)) {
  318. dst1->transparent = src->transparent;
  319. } else {
  320. dst1->transparent = gdTrueColorAlpha(gdImageRed(src, src->transparent), gdImageBlue(src, src->transparent), gdImageGreen(src, src->transparent), 127);
  321. }
  322. }
  323. dRadAngle = dAngle * ROTATE_DEG2RAD; /* Angle in radians */
  324. dSinE = sin (dRadAngle);
  325. dTan = tan (dRadAngle / 2.0);
  326. for (u = 0; u < dst1->sy; u++) {
  327. if (dTan >= 0.0) {
  328. dShear = ((double)(u + 0.5)) * dTan;
  329. } else {
  330. dShear = ((double)(u - dst1->sy) + 0.5) * dTan;
  331. }
  332. iShear = (int)floor(dShear);
  333. gdImageSkewX(dst1, src, u, iShear, (dShear - iShear), clrBack, ignoretransparent);
  334. }
  335. /*
  336. The 1st shear may use the original clrBack as color index
  337. Convert it once here
  338. */
  339. if(!src->trueColor) {
  340. clrBackR = gdImageRed(src, clrBack);
  341. clrBackG = gdImageGreen(src, clrBack);
  342. clrBackB = gdImageBlue(src, clrBack);
  343. clrBackA = gdImageAlpha(src, clrBack);
  344. clrBack = gdTrueColorAlpha(clrBackR, clrBackG, clrBackB, clrBackA);
  345. }
  346. /* 2nd shear */
  347. newx = dst1->sx;
  348. if (dSinE > 0.0) {
  349. dOffset = (src->sx-1) * dSinE;
  350. } else {
  351. dOffset = -dSinE * (src->sx - newx);
  352. }
  353. newy = (int) ((double) src->sx * fabs( dSinE ) + (double) src->sy * cos (dRadAngle))+1;
  354. if (src->trueColor) {
  355. f = gdImageGetTrueColorPixel;
  356. } else {
  357. f = gdImageGetPixel;
  358. }
  359. dst2 = gdImageCreateTrueColor(newx, newy);
  360. if (dst2 == NULL) {
  361. gdImageDestroy(dst1);
  362. return NULL;
  363. }
  364. #ifdef HAVE_GD_BUNDLED
  365. dst2->alphaBlendingFlag = gdEffectReplace;
  366. #else
  367. gdImageAlphaBlending(dst2, 0);
  368. #endif
  369. if (ignoretransparent) {
  370. dst2->transparent = dst1->transparent;
  371. }
  372. for (u = 0; u < dst2->sx; u++, dOffset -= dSinE) {
  373. iShear = (int)floor (dOffset);
  374. gdImageSkewY(dst2, dst1, u, iShear, (dOffset - (double)iShear), clrBack, ignoretransparent);
  375. }
  376. /* 3rd shear */
  377. gdImageDestroy(dst1);
  378. newx = (int) ((double)src->sy * fabs (dSinE) + (double)src->sx * cos (dRadAngle)) + 1;
  379. newy = dst2->sy;
  380. if (src->trueColor) {
  381. f = gdImageGetTrueColorPixel;
  382. } else {
  383. f = gdImageGetPixel;
  384. }
  385. dst3 = gdImageCreateTrueColor(newx, newy);
  386. if (dst3 == NULL) {
  387. gdImageDestroy(dst2);
  388. return NULL;
  389. }
  390. #ifdef HAVE_GD_BUNDLED
  391. dst3->alphaBlendingFlag = gdEffectReplace;
  392. #else
  393. gdImageAlphaBlending(dst3, 0);
  394. #endif
  395. if (ignoretransparent) {
  396. dst3->transparent = dst2->transparent;
  397. }
  398. if (dSinE >= 0.0) {
  399. dOffset = (double)(src->sx - 1) * dSinE * -dTan;
  400. } else {
  401. dOffset = dTan * ((double)(src->sx - 1) * -dSinE + (double)(1 - newy));
  402. }
  403. for (u = 0; u < dst3->sy; u++, dOffset += dTan) {
  404. int iShear = (int)floor(dOffset);
  405. gdImageSkewX(dst3, dst2, u, iShear, (dOffset - iShear), clrBack, ignoretransparent);
  406. }
  407. gdImageDestroy(dst2);
  408. return dst3;
  409. }
  410. gdImagePtr gdImageRotate (gdImagePtr src, double dAngle, int clrBack, int ignoretransparent)
  411. {
  412. gdImagePtr pMidImg;
  413. gdImagePtr rotatedImg;
  414. if (src == NULL) {
  415. return NULL;
  416. }
  417. if (!gdImageTrueColor(src) && (clrBack < 0 || clrBack>=gdImageColorsTotal(src))) {
  418. return NULL;
  419. }
  420. while (dAngle >= 360.0) {
  421. dAngle -= 360.0;
  422. }
  423. while (dAngle < 0) {
  424. dAngle += 360.0;
  425. }
  426. if (dAngle == 90.00) {
  427. return gdImageRotate90(src, ignoretransparent);
  428. }
  429. if (dAngle == 180.00) {
  430. return gdImageRotate180(src, ignoretransparent);
  431. }
  432. if(dAngle == 270.00) {
  433. return gdImageRotate270 (src, ignoretransparent);
  434. }
  435. if ((dAngle > 45.0) && (dAngle <= 135.0)) {
  436. pMidImg = gdImageRotate90 (src, ignoretransparent);
  437. dAngle -= 90.0;
  438. } else if ((dAngle > 135.0) && (dAngle <= 225.0)) {
  439. pMidImg = gdImageRotate180 (src, ignoretransparent);
  440. dAngle -= 180.0;
  441. } else if ((dAngle > 225.0) && (dAngle <= 315.0)) {
  442. pMidImg = gdImageRotate270 (src, ignoretransparent);
  443. dAngle -= 270.0;
  444. } else {
  445. return gdImageRotate45 (src, dAngle, clrBack, ignoretransparent);
  446. }
  447. if (pMidImg == NULL) {
  448. return NULL;
  449. }
  450. rotatedImg = gdImageRotate45 (pMidImg, dAngle, clrBack, ignoretransparent);
  451. gdImageDestroy(pMidImg);
  452. return rotatedImg;
  453. }
  454. /* End Rotate function */