brush_scale.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /* The GIMP -- an image manipulation program
  2. * Copyright (C) 1995 Spencer Kimball and Peter Mattis
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. #include <glib.h>
  19. #include "brush_scale.h"
  20. MaskBuf *
  21. brush_scale_mask (MaskBuf *brush_mask,
  22. int dest_width,
  23. int dest_height)
  24. {
  25. MaskBuf *scale_brush;
  26. int src_width;
  27. int src_height;
  28. int value;
  29. int area;
  30. int i, j;
  31. int x, x0, y, y0;
  32. int dx, dx0, dy, dy0;
  33. int fx, fx0, fy, fy0;
  34. unsigned char *src, *dest;
  35. g_return_val_if_fail (brush_mask != NULL &&
  36. dest_width != 0 && dest_height != 0, NULL);
  37. src_width = brush_mask->width;
  38. src_height = brush_mask->height;
  39. scale_brush = mask_buf_new (dest_width, dest_height);
  40. g_return_val_if_fail (scale_brush != NULL, NULL);
  41. /* get the data */
  42. dest = mask_buf_data (scale_brush);
  43. src = mask_buf_data (brush_mask);
  44. fx = fx0 = (256.0 * src_width) / dest_width;
  45. fy = fy0 = (256.0 * src_height) / dest_height;
  46. area = fx0 * fy0;
  47. x = x0 = 0;
  48. y = y0 = 0;
  49. dx = dx0 = 0;
  50. dy = dy0 = 0;
  51. for (i=0; i<dest_height; i++)
  52. {
  53. for (j=0; j<dest_width; j++)
  54. {
  55. value = 0;
  56. fy = fy0;
  57. y = y0;
  58. dy = dy0;
  59. if (dy)
  60. {
  61. fx = fx0;
  62. x = x0;
  63. dx = dx0;
  64. if (dx)
  65. {
  66. value += dx * dy * src[x + src_width * y];
  67. x++;
  68. fx -= dx;
  69. dx = 0;
  70. }
  71. while (fx >= 256)
  72. {
  73. value += 256 * dy * src[x + src_width * y];
  74. x++;
  75. fx -= 256;
  76. }
  77. if (fx)
  78. {
  79. value += fx * dy * src[x + src_width * y];
  80. dx = 256 - fx;
  81. }
  82. y++;
  83. fy -= dy;
  84. dy = 0;
  85. }
  86. while (fy >= 256)
  87. {
  88. fx = fx0;
  89. x = x0;
  90. dx = dx0;
  91. if (dx)
  92. {
  93. value += dx * 256 * src[x + src_width * y];
  94. x++;
  95. fx -= dx;
  96. dx = 0;
  97. }
  98. while (fx >= 256)
  99. {
  100. value += 256 * 256 * src[x + src_width * y];
  101. x++;
  102. fx -= 256;
  103. }
  104. if (fx)
  105. {
  106. value += fx * 256 * src[x + src_width * y];
  107. dx = 256 - fx;
  108. }
  109. y++;
  110. fy -= 256;
  111. }
  112. if (fy)
  113. {
  114. fx = fx0;
  115. x = x0;
  116. dx = dx0;
  117. if (dx)
  118. {
  119. value += dx * fy * src[x + src_width * y];
  120. x++;
  121. fx -= dx;
  122. dx = 0;
  123. }
  124. while (fx >= 256)
  125. {
  126. value += 256 * fy * src[x + src_width * y];
  127. x++;
  128. fx -= 256;
  129. }
  130. if (fx)
  131. {
  132. value += fx * fy * src[x + src_width * y];
  133. dx = 256 - fx;
  134. }
  135. dy = 256 - fy;
  136. }
  137. *dest++ = MIN ((value / area), 255);
  138. x0 = x;
  139. dx0 = dx;
  140. }
  141. x0 = 0;
  142. dx0 = 0;
  143. y0 = y;
  144. dy0 = dy;
  145. }
  146. return scale_brush;
  147. }
  148. #define ADD_RGB(dest, factor, src) \
  149. dest[0] += factor * src[0]; \
  150. dest[1] += factor * src[1]; \
  151. dest[2] += factor * src[2];
  152. MaskBuf *
  153. brush_scale_pixmap (MaskBuf *pixmap,
  154. int dest_width,
  155. int dest_height)
  156. {
  157. MaskBuf *scale_brush;
  158. int src_width;
  159. int src_height;
  160. int value[3];
  161. int factor;
  162. int area;
  163. int i, j;
  164. int x, x0, y, y0;
  165. int dx, dx0, dy, dy0;
  166. int fx, fx0, fy, fy0;
  167. unsigned char *src, *src_ptr, *dest;
  168. g_return_val_if_fail (pixmap != NULL && pixmap->bytes == 3 &&
  169. dest_width != 0 && dest_height != 0, NULL);
  170. src_width = pixmap->width;
  171. src_height = pixmap->height;
  172. scale_brush = temp_buf_new (dest_width, dest_height, 3, 0, 0, NULL);
  173. g_return_val_if_fail (scale_brush != NULL, NULL);
  174. /* get the data */
  175. dest = mask_buf_data (scale_brush);
  176. src = mask_buf_data (pixmap);
  177. fx = fx0 = (256.0 * src_width) / dest_width;
  178. fy = fy0 = (256.0 * src_height) / dest_height;
  179. area = fx0 * fy0;
  180. x = x0 = 0;
  181. y = y0 = 0;
  182. dx = dx0 = 0;
  183. dy = dy0 = 0;
  184. for (i=0; i<dest_height; i++)
  185. {
  186. for (j=0; j<dest_width; j++)
  187. {
  188. value[0] = 0;
  189. value[1] = 0;
  190. value[2] = 0;
  191. fy = fy0;
  192. y = y0;
  193. dy = dy0;
  194. if (dy)
  195. {
  196. fx = fx0;
  197. x = x0;
  198. dx = dx0;
  199. if (dx)
  200. {
  201. factor = dx * dy;
  202. src_ptr = src + 3 * (x + y * src_width);
  203. ADD_RGB (value, factor, src_ptr);
  204. x++;
  205. fx -= dx;
  206. dx = 0;
  207. }
  208. while (fx >= 256)
  209. {
  210. factor = 256 * dy;
  211. src_ptr = src + 3 * (x + y * src_width);
  212. ADD_RGB (value, factor, src_ptr);
  213. x++;
  214. fx -= 256;
  215. }
  216. if (fx)
  217. {
  218. factor = fx * dy;
  219. src_ptr = src + 3 * (x + y * src_width);
  220. ADD_RGB (value, factor, src_ptr);
  221. dx = 256 - fx;
  222. }
  223. y++;
  224. fy -= dy;
  225. dy = 0;
  226. }
  227. while (fy >= 256)
  228. {
  229. fx = fx0;
  230. x = x0;
  231. dx = dx0;
  232. if (dx)
  233. {
  234. factor = dx * 256;
  235. src_ptr = src + 3 * (x + y * src_width);
  236. ADD_RGB (value, factor, src_ptr);
  237. x++;
  238. fx -= dx;
  239. dx = 0;
  240. }
  241. while (fx >= 256)
  242. {
  243. factor = 256 * 256;
  244. src_ptr = src + 3 * (x + y * src_width);
  245. ADD_RGB (value, factor, src_ptr);
  246. x++;
  247. fx -= 256;
  248. }
  249. if (fx)
  250. {
  251. factor = fx * 256;
  252. src_ptr = src + 3 * (x + y * src_width);
  253. ADD_RGB (value, factor, src_ptr);
  254. dx = 256 - fx;
  255. }
  256. y++;
  257. fy -= 256;
  258. }
  259. if (fy)
  260. {
  261. fx = fx0;
  262. x = x0;
  263. dx = dx0;
  264. if (dx)
  265. {
  266. factor = dx * fy;
  267. src_ptr = src + 3 * (x + y * src_width);
  268. ADD_RGB (value, factor, src_ptr);
  269. x++;
  270. fx -= dx;
  271. dx = 0;
  272. }
  273. while (fx >= 256)
  274. {
  275. factor = 256 * fy;
  276. src_ptr = src + 3 * (x + y * src_width);
  277. ADD_RGB (value, factor, src_ptr);
  278. x++;
  279. fx -= 256;
  280. }
  281. if (fx)
  282. {
  283. factor = fx * fy;
  284. src_ptr = src + 3 * (x + y * src_width);
  285. ADD_RGB (value, factor, src_ptr);
  286. dx = 256 - fx;
  287. }
  288. dy = 256 - fy;
  289. }
  290. *dest++ = MIN ((value[0] / area), 255);
  291. *dest++ = MIN ((value[1] / area), 255);
  292. *dest++ = MIN ((value[2] / area), 255);
  293. x0 = x;
  294. dx0 = dx;
  295. }
  296. x0 = 0;
  297. dx0 = 0;
  298. y0 = y;
  299. dy0 = dy;
  300. }
  301. return scale_brush;
  302. }
  303. #undef ADD_RGB