gd_crop.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 <stdlib.h>
  21. #include <string.h>
  22. #include <math.h>
  23. #include "gd.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. break;
  99. }
  100. /* TODO: Add gdImageGetRowPtr and works with ptr at the row level
  101. * for the true color and palette images
  102. * new formats will simply work with ptr
  103. */
  104. match = 1;
  105. for (y = 0; match && y < height; y++) {
  106. for (x = 0; match && x < width; x++) {
  107. int c2 = gdImageGetPixel(im, x, y);
  108. match = (color == c2);
  109. }
  110. }
  111. /* Whole image would be cropped > bye */
  112. if (match) {
  113. return NULL;
  114. }
  115. crop.y = y - 1;
  116. match = 1;
  117. for (y = height - 1; match && y >= 0; y--) {
  118. for (x = 0; match && x < width; x++) {
  119. match = (color == gdImageGetPixel(im, x,y));
  120. }
  121. }
  122. crop.height = y - crop.y + 2;
  123. match = 1;
  124. for (x = 0; match && x < width; x++) {
  125. for (y = 0; match && y < crop.y + crop.height; y++) {
  126. match = (color == gdImageGetPixel(im, x,y));
  127. }
  128. }
  129. crop.x = x - 1;
  130. match = 1;
  131. for (x = width - 1; match && x >= 0; x--) {
  132. for (y = 0; match && y < crop.y + crop.height; y++) {
  133. match = (color == gdImageGetPixel(im, x,y));
  134. }
  135. }
  136. crop.width = x - crop.x + 2;
  137. return gdImageCrop(im, &crop);
  138. }
  139. /*TODOs: Implement DeltaE instead, way better perceptual differences */
  140. /**
  141. * Function: gdImageThresholdCrop
  142. * Crop an image using a given color. The threshold argument defines
  143. * the tolerance to be used while comparing the image color and the
  144. * color to crop. The method used to calculate the color difference
  145. * is based on the color distance in the RGB(a) cube.
  146. *
  147. *
  148. * Parameters:
  149. * im - Source image
  150. * color - color to crop
  151. * threshold - tolerance (0..100)
  152. *
  153. * Returns:
  154. * <gdImagePtr> on success or NULL
  155. *
  156. * See also:
  157. * <gdCropMode>, <gdImageAutoCrop> or <gdImageCrop>
  158. */
  159. gdImagePtr gdImageCropThreshold(gdImagePtr im, const unsigned int color, const float threshold)
  160. {
  161. const int width = gdImageSX(im);
  162. const int height = gdImageSY(im);
  163. int x,y;
  164. int match;
  165. gdRect crop;
  166. crop.x = 0;
  167. crop.y = 0;
  168. crop.width = 0;
  169. crop.height = 0;
  170. /* Pierre: crop everything sounds bad */
  171. if (threshold > 100.0) {
  172. return NULL;
  173. }
  174. if (!gdImageTrueColor(im) && color >= gdImageColorsTotal(im)) {
  175. return NULL;
  176. }
  177. /* TODO: Add gdImageGetRowPtr and works with ptr at the row level
  178. * for the true color and palette images
  179. * new formats will simply work with ptr
  180. */
  181. match = 1;
  182. for (y = 0; match && y < height; y++) {
  183. for (x = 0; match && x < width; x++) {
  184. match = (gdColorMatch(im, color, gdImageGetPixel(im, x,y), threshold)) > 0;
  185. }
  186. }
  187. /* Whole image would be cropped > bye */
  188. if (match) {
  189. return NULL;
  190. }
  191. crop.y = y - 1;
  192. match = 1;
  193. for (y = height - 1; match && y >= 0; y--) {
  194. for (x = 0; match && x < width; x++) {
  195. match = (gdColorMatch(im, color, gdImageGetPixel(im, x, y), threshold)) > 0;
  196. }
  197. }
  198. crop.height = y - crop.y + 2;
  199. match = 1;
  200. for (x = 0; match && x < width; x++) {
  201. for (y = 0; match && y < crop.y + crop.height; y++) {
  202. match = (gdColorMatch(im, color, gdImageGetPixel(im, x,y), threshold)) > 0;
  203. }
  204. }
  205. crop.x = x - 1;
  206. match = 1;
  207. for (x = width - 1; match && x >= 0; x--) {
  208. for (y = 0; match && y < crop.y + crop.height; y++) {
  209. match = (gdColorMatch(im, color, gdImageGetPixel(im, x,y), threshold)) > 0;
  210. }
  211. }
  212. crop.width = x - crop.x + 2;
  213. return gdImageCrop(im, &crop);
  214. }
  215. /* This algorithm comes from pnmcrop (http://netpbm.sourceforge.net/)
  216. * Three steps:
  217. * - if 3 corners are equal.
  218. * - if two are equal.
  219. * - Last solution: average the colors
  220. */
  221. static int gdGuessBackgroundColorFromCorners(gdImagePtr im, int *color)
  222. {
  223. const int tl = gdImageGetPixel(im, 0, 0);
  224. const int tr = gdImageGetPixel(im, gdImageSX(im) - 1, 0);
  225. const int bl = gdImageGetPixel(im, 0, gdImageSY(im) -1);
  226. const int br = gdImageGetPixel(im, gdImageSX(im) - 1, gdImageSY(im) -1);
  227. if (tr == bl && tr == br) {
  228. *color = tr;
  229. return 3;
  230. } else if (tl == bl && tl == br) {
  231. *color = tl;
  232. return 3;
  233. } else if (tl == tr && tl == br) {
  234. *color = tl;
  235. return 3;
  236. } else if (tl == tr && tl == bl) {
  237. *color = tl;
  238. return 3;
  239. } else if (tl == tr || tl == bl || tl == br) {
  240. *color = tl;
  241. return 2;
  242. } else if (tr == bl || tr == br) {
  243. *color = tr;
  244. return 2;
  245. } else if (br == bl) {
  246. *color = bl;
  247. return 2;
  248. } else {
  249. register int r,b,g,a;
  250. r = (int)(0.5f + (gdImageRed(im, tl) + gdImageRed(im, tr) + gdImageRed(im, bl) + gdImageRed(im, br)) / 4);
  251. g = (int)(0.5f + (gdImageGreen(im, tl) + gdImageGreen(im, tr) + gdImageGreen(im, bl) + gdImageGreen(im, br)) / 4);
  252. b = (int)(0.5f + (gdImageBlue(im, tl) + gdImageBlue(im, tr) + gdImageBlue(im, bl) + gdImageBlue(im, br)) / 4);
  253. a = (int)(0.5f + (gdImageAlpha(im, tl) + gdImageAlpha(im, tr) + gdImageAlpha(im, bl) + gdImageAlpha(im, br)) / 4);
  254. *color = gdImageColorClosestAlpha(im, r, g, b, a);
  255. return 0;
  256. }
  257. }
  258. static int gdColorMatch(gdImagePtr im, int col1, int col2, float threshold)
  259. {
  260. const int dr = gdImageRed(im, col1) - gdImageRed(im, col2);
  261. const int dg = gdImageGreen(im, col1) - gdImageGreen(im, col2);
  262. const int db = gdImageBlue(im, col1) - gdImageBlue(im, col2);
  263. const int da = gdImageAlpha(im, col1) - gdImageAlpha(im, col2);
  264. const int dist = dr * dr + dg * dg + db * db + da * da;
  265. return (100.0 * dist / 195075) < threshold;
  266. }
  267. /*
  268. * To be implemented when we have more image formats.
  269. * Buffer like gray8 gray16 or rgb8 will require some tweak
  270. * and can be done in this function (called from the autocrop
  271. * function. (Pierre)
  272. */
  273. #if 0
  274. static int colors_equal (const int col1, const in col2)
  275. {
  276. }
  277. #endif