gd_crop.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /**
  2. * Title: Crop
  3. *
  4. * A couple of functions to crop images, automatically (auto detection of
  5. * the borders color), using a given color (with or without tolerance)
  6. * or using a selection.
  7. *
  8. * The threshold method works relatively well but it can be improved.
  9. * Maybe L*a*b* and Delta-E will give better results (and a better
  10. * granularity).
  11. *
  12. * Example:
  13. * (start code)
  14. * im2 = gdImageAutoCrop(im, GD_CROP_SIDES);
  15. * if (im2) {
  16. * }
  17. * gdImageDestroy(im2);
  18. * (end code)
  19. **/
  20. #include <gd.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <math.h>
  24. static int gdGuessBackgroundColorFromCorners(gdImagePtr im, int *color);
  25. static int gdColorMatch(gdImagePtr im, int col1, int col2, float threshold);
  26. /**
  27. * Function: gdImageCrop
  28. * Crops the src image using the area defined by the <crop> rectangle.
  29. * The result is returned as a new image.
  30. *
  31. *
  32. * Parameters:
  33. * src - Source image
  34. * crop - Rectangular region to crop
  35. *
  36. * Returns:
  37. * <gdImagePtr> on success or NULL
  38. */
  39. gdImagePtr gdImageCrop(gdImagePtr src, const gdRectPtr crop)
  40. {
  41. gdImagePtr dst;
  42. int alphaBlendingFlag;
  43. if (gdImageTrueColor(src)) {
  44. dst = gdImageCreateTrueColor(crop->width, crop->height);
  45. } else {
  46. dst = gdImageCreate(crop->width, crop->height);
  47. }
  48. if (!dst) return NULL;
  49. alphaBlendingFlag = dst->alphaBlendingFlag;
  50. gdImageAlphaBlending(dst, gdEffectReplace);
  51. gdImageCopy(dst, src, 0, 0, crop->x, crop->y, crop->width, crop->height);
  52. gdImageAlphaBlending(dst, alphaBlendingFlag);
  53. return dst;
  54. }
  55. /**
  56. * Function: gdImageAutoCrop
  57. * Automatic croping of the src image using the given mode
  58. * (see <gdCropMode>)
  59. *
  60. *
  61. * Parameters:
  62. * im - Source image
  63. * mode - crop mode
  64. *
  65. * Returns:
  66. * <gdImagePtr> on success or NULL
  67. *
  68. * See also:
  69. * <gdCropMode>
  70. */
  71. gdImagePtr gdImageCropAuto(gdImagePtr im, const unsigned int mode)
  72. {
  73. const int width = gdImageSX(im);
  74. const int height = gdImageSY(im);
  75. int x,y;
  76. int color, match;
  77. gdRect crop;
  78. crop.x = 0;
  79. crop.y = 0;
  80. crop.width = 0;
  81. crop.height = 0;
  82. switch (mode) {
  83. case GD_CROP_TRANSPARENT:
  84. color = gdImageGetTransparent(im);
  85. break;
  86. case GD_CROP_BLACK:
  87. color = gdImageColorClosestAlpha(im, 0, 0, 0, 0);
  88. break;
  89. case GD_CROP_WHITE:
  90. color = gdImageColorClosestAlpha(im, 255, 255, 255, 0);
  91. break;
  92. case GD_CROP_SIDES:
  93. gdGuessBackgroundColorFromCorners(im, &color);
  94. break;
  95. case GD_CROP_DEFAULT:
  96. default:
  97. color = gdImageGetTransparent(im);
  98. if (color == -1) {
  99. gdGuessBackgroundColorFromCorners(im, &color);
  100. }
  101. break;
  102. }
  103. /* TODO: Add gdImageGetRowPtr and works with ptr at the row level
  104. * for the true color and palette images
  105. * new formats will simply work with ptr
  106. */
  107. match = 1;
  108. for (y = 0; match && y < height; y++) {
  109. for (x = 0; match && x < width; x++) {
  110. int c2 = gdImageGetPixel(im, x, y);
  111. match = (color == c2);
  112. }
  113. }
  114. /* Whole image would be cropped > bye */
  115. if (match) {
  116. return NULL;
  117. }
  118. crop.y = y - 1;
  119. match = 1;
  120. for (y = height - 1; match && y >= 0; y--) {
  121. for (x = 0; match && x < width; x++) {
  122. match = (color == gdImageGetPixel(im, x,y));
  123. }
  124. }
  125. crop.height = y - crop.y + 2;
  126. match = 1;
  127. for (x = 0; match && x < width; x++) {
  128. for (y = 0; match && y < crop.y + crop.height; y++) {
  129. match = (color == gdImageGetPixel(im, x,y));
  130. }
  131. }
  132. crop.x = x - 1;
  133. match = 1;
  134. for (x = width - 1; match && x >= 0; x--) {
  135. for (y = 0; match && y < crop.y + crop.height; y++) {
  136. match = (color == gdImageGetPixel(im, x,y));
  137. }
  138. }
  139. crop.width = x - crop.x + 2;
  140. if (crop.x < 0 || crop.y < 0 || crop.width <= 0 || crop.height <= 0) {
  141. return NULL;
  142. }
  143. return gdImageCrop(im, &crop);
  144. }
  145. /*TODOs: Implement DeltaE instead, way better perceptual differences */
  146. /**
  147. * Function: gdImageThresholdCrop
  148. * Crop an image using a given color. The threshold argument defines
  149. * the tolerance to be used while comparing the image color and the
  150. * color to crop. The method used to calculate the color difference
  151. * is based on the color distance in the RGB(a) cube.
  152. *
  153. *
  154. * Parameters:
  155. * im - Source image
  156. * color - color to crop
  157. * threshold - tolerance (0..100)
  158. *
  159. * Returns:
  160. * <gdImagePtr> on success or NULL
  161. *
  162. * See also:
  163. * <gdCropMode>, <gdImageAutoCrop> or <gdImageCrop>
  164. */
  165. gdImagePtr gdImageCropThreshold(gdImagePtr im, const unsigned int color, const float threshold)
  166. {
  167. const int width = gdImageSX(im);
  168. const int height = gdImageSY(im);
  169. int x,y;
  170. int match;
  171. gdRect crop;
  172. crop.x = 0;
  173. crop.y = 0;
  174. crop.width = 0;
  175. crop.height = 0;
  176. /* Pierre: crop everything sounds bad */
  177. if (threshold > 1.0) {
  178. return NULL;
  179. }
  180. if (!gdImageTrueColor(im) && color >= gdImageColorsTotal(im)) {
  181. return NULL;
  182. }
  183. /* TODO: Add gdImageGetRowPtr and works with ptr at the row level
  184. * for the true color and palette images
  185. * new formats will simply work with ptr
  186. */
  187. match = 1;
  188. for (y = 0; match && y < height; y++) {
  189. for (x = 0; match && x < width; x++) {
  190. match = (gdColorMatch(im, color, gdImageGetPixel(im, x,y), threshold)) > 0;
  191. }
  192. }
  193. /* Whole image would be cropped > bye */
  194. if (match) {
  195. return NULL;
  196. }
  197. crop.y = y - 1;
  198. match = 1;
  199. for (y = height - 1; match && y >= 0; y--) {
  200. for (x = 0; match && x < width; x++) {
  201. match = (gdColorMatch(im, color, gdImageGetPixel(im, x, y), threshold)) > 0;
  202. }
  203. }
  204. crop.height = y - crop.y + 2;
  205. match = 1;
  206. for (x = 0; match && x < width; x++) {
  207. for (y = 0; match && y < crop.y + crop.height; y++) {
  208. match = (gdColorMatch(im, color, gdImageGetPixel(im, x,y), threshold)) > 0;
  209. }
  210. }
  211. crop.x = x - 1;
  212. match = 1;
  213. for (x = width - 1; match && x >= 0; x--) {
  214. for (y = 0; match && y < crop.y + crop.height; y++) {
  215. match = (gdColorMatch(im, color, gdImageGetPixel(im, x,y), threshold)) > 0;
  216. }
  217. }
  218. crop.width = x - crop.x + 2;
  219. return gdImageCrop(im, &crop);
  220. }
  221. /* This algorithm comes from pnmcrop (http://netpbm.sourceforge.net/)
  222. * Three steps:
  223. * - if 3 corners are equal.
  224. * - if two are equal.
  225. * - Last solution: average the colors
  226. */
  227. static int gdGuessBackgroundColorFromCorners(gdImagePtr im, int *color)
  228. {
  229. const int tl = gdImageGetPixel(im, 0, 0);
  230. const int tr = gdImageGetPixel(im, gdImageSX(im) - 1, 0);
  231. const int bl = gdImageGetPixel(im, 0, gdImageSY(im) -1);
  232. const int br = gdImageGetPixel(im, gdImageSX(im) - 1, gdImageSY(im) -1);
  233. if (tr == bl && tr == br) {
  234. *color = tr;
  235. return 3;
  236. } else if (tl == bl && tl == br) {
  237. *color = tl;
  238. return 3;
  239. } else if (tl == tr && tl == br) {
  240. *color = tl;
  241. return 3;
  242. } else if (tl == tr && tl == bl) {
  243. *color = tl;
  244. return 3;
  245. } else if (tl == tr || tl == bl || tl == br) {
  246. *color = tl;
  247. return 2;
  248. } else if (tr == bl || tr == br) {
  249. *color = tr;
  250. return 2;
  251. } else if (br == bl) {
  252. *color = bl;
  253. return 2;
  254. } else {
  255. register int r,b,g,a;
  256. r = (int)(0.5f + (gdImageRed(im, tl) + gdImageRed(im, tr) + gdImageRed(im, bl) + gdImageRed(im, br)) / 4);
  257. g = (int)(0.5f + (gdImageGreen(im, tl) + gdImageGreen(im, tr) + gdImageGreen(im, bl) + gdImageGreen(im, br)) / 4);
  258. b = (int)(0.5f + (gdImageBlue(im, tl) + gdImageBlue(im, tr) + gdImageBlue(im, bl) + gdImageBlue(im, br)) / 4);
  259. a = (int)(0.5f + (gdImageAlpha(im, tl) + gdImageAlpha(im, tr) + gdImageAlpha(im, bl) + gdImageAlpha(im, br)) / 4);
  260. *color = gdImageColorClosestAlpha(im, r, g, b, a);
  261. return 0;
  262. }
  263. }
  264. static int gdColorMatch(gdImagePtr im, int col1, int col2, float threshold)
  265. {
  266. const int dr = gdImageRed(im, col1) - gdImageRed(im, col2);
  267. const int dg = gdImageGreen(im, col1) - gdImageGreen(im, col2);
  268. const int db = gdImageBlue(im, col1) - gdImageBlue(im, col2);
  269. const int da = gdImageAlpha(im, col1) - gdImageAlpha(im, col2);
  270. const double dist = sqrt(dr * dr + dg * dg + db * db + da * da);
  271. const double dist_perc = sqrt(dist / (255^2 + 255^2 + 255^2));
  272. return (dist_perc <= threshold);
  273. //return (100.0 * dist / 195075) < threshold;
  274. }
  275. /*
  276. * To be implemented when we have more image formats.
  277. * Buffer like gray8 gray16 or rgb8 will require some tweak
  278. * and can be done in this function (called from the autocrop
  279. * function. (Pierre)
  280. */
  281. #if 0
  282. static int colors_equal (const int col1, const in col2)
  283. {
  284. }
  285. #endif