You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

472 rivejä
13 KiB

  1. /*
  2. * libpipi Proper image processing implementation library
  3. * Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This library is free software. It comes without any warranty, to
  9. * the extent permitted by applicable law. You can redistribute it
  10. * and/or modify it under the terms of the Do What The Fuck You Want
  11. * To Public License, Version 2, as published by Sam Hocevar. See
  12. * http://sam.zoy.org/wtfpl/COPYING for more details.
  13. */
  14. /*
  15. * context.c: processing stack handling routines
  16. */
  17. #include "config.h"
  18. #include "common.h"
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <stdarg.h>
  22. #include <string.h>
  23. #include "pipi.h"
  24. #include "pipi_internals.h"
  25. pipi_context_t *pipi_create_context()
  26. {
  27. pipi_context_t *ret;
  28. ret = malloc(sizeof(pipi_context_t));
  29. memset(ret, 0, sizeof(pipi_context_t));
  30. return ret;
  31. }
  32. void pipi_destroy_context(pipi_context_t *ctx)
  33. {
  34. free(ctx);
  35. }
  36. int pipi_command(pipi_context_t *ctx, char const *cmd, ...)
  37. {
  38. if(!strcmp(cmd, "load"))
  39. {
  40. char const *file;
  41. va_list ap;
  42. va_start(ap, cmd);
  43. file = va_arg(ap, char const *);
  44. va_end(ap);
  45. ctx->images[ctx->nimages] = pipi_load(file);
  46. if(ctx->images[ctx->nimages] == NULL)
  47. return -1;
  48. ctx->nimages++;
  49. }
  50. else if(!strcmp(cmd, "save"))
  51. {
  52. char const *file;
  53. va_list ap;
  54. if(ctx->nimages < 1)
  55. return -1;
  56. ctx->nimages--;
  57. va_start(ap, cmd);
  58. file = va_arg(ap, char const *);
  59. va_end(ap);
  60. pipi_save(ctx->images[ctx->nimages], file);
  61. pipi_free(ctx->images[ctx->nimages]);
  62. }
  63. else if(!strcmp(cmd, "dither"))
  64. {
  65. pipi_image_t *src, *dst;
  66. char const *method;
  67. va_list ap;
  68. if(ctx->nimages < 1)
  69. return -1;
  70. va_start(ap, cmd);
  71. method = va_arg(ap, char const *);
  72. va_end(ap);
  73. src = ctx->images[ctx->nimages - 1];
  74. dst = NULL;
  75. if(!strcmp(method, "fs"))
  76. dst = pipi_dither_floydsteinberg(src, 0);
  77. else if(!strcmp(method, "sfs"))
  78. dst = pipi_dither_floydsteinberg(src, 1);
  79. else if(!strcmp(method, "jajuni"))
  80. dst = pipi_dither_jajuni(src, 0);
  81. else if(!strcmp(method, "sjajuni"))
  82. dst = pipi_dither_jajuni(src, 1);
  83. else if(!strcmp(method, "ost"))
  84. dst = pipi_dither_ostromoukhov(src, 0);
  85. else if(!strcmp(method, "sost"))
  86. dst = pipi_dither_ostromoukhov(src, 1);
  87. else if(!strcmp(method, "ordered"))
  88. {
  89. if(ctx->nimages < 2)
  90. return -1;
  91. dst = pipi_dither_ordered(ctx->images[ctx->nimages - 2], src);
  92. pipi_free(ctx->images[ctx->nimages - 2]);
  93. ctx->nimages--;
  94. }
  95. else if(!strcmp(method, "random"))
  96. dst = pipi_dither_random(src);
  97. else if(!strcmp(method, "dbs"))
  98. dst = pipi_dither_dbs(src);
  99. if(dst == NULL)
  100. return -1;
  101. pipi_free(src);
  102. ctx->images[ctx->nimages - 1] = dst;
  103. }
  104. else if(!strcmp(cmd, "blur"))
  105. {
  106. pipi_image_t *src, *dst;
  107. char const *arg;
  108. va_list ap;
  109. double w, h, a = 0.0;
  110. if(ctx->nimages < 1)
  111. return -1;
  112. va_start(ap, cmd);
  113. arg = va_arg(ap, char const *);
  114. va_end(ap);
  115. w = h = atof(arg);
  116. arg = strchr(arg, 'x');
  117. if(arg)
  118. {
  119. h = atof(arg + 1);
  120. arg = strchr(arg, 'r');
  121. if(arg)
  122. a = atof(arg + 1);
  123. }
  124. src = ctx->images[ctx->nimages - 1];
  125. dst = pipi_gaussian_blur_ext(src, w, h, a, 0.0, 0.0);
  126. if(dst == NULL)
  127. return -1;
  128. pipi_free(src);
  129. ctx->images[ctx->nimages - 1] = dst;
  130. }
  131. else if(!strcmp(cmd, "geometry"))
  132. {
  133. pipi_image_t *src, *dst;
  134. char const *arg;
  135. va_list ap;
  136. int w, h;
  137. if(ctx->nimages < 1)
  138. return -1;
  139. va_start(ap, cmd);
  140. arg = va_arg(ap, char const *);
  141. va_end(ap);
  142. w = atoi(arg);
  143. arg = strchr(arg, 'x');
  144. if(!arg)
  145. return -1;
  146. h = atoi(arg + 1);
  147. if(w <= 0 || h <= 0)
  148. return -1;
  149. src = ctx->images[ctx->nimages - 1];
  150. dst = pipi_resize(src, w, h);
  151. if(dst == NULL)
  152. return -1;
  153. pipi_free(src);
  154. ctx->images[ctx->nimages - 1] = dst;
  155. }
  156. else if(!strcmp(cmd, "tile"))
  157. {
  158. pipi_image_t *src, *dst;
  159. char const *arg;
  160. va_list ap;
  161. int w, h;
  162. if(ctx->nimages < 1)
  163. return -1;
  164. va_start(ap, cmd);
  165. arg = va_arg(ap, char const *);
  166. va_end(ap);
  167. w = atoi(arg);
  168. arg = strchr(arg, 'x');
  169. if(!arg)
  170. return -1;
  171. h = atoi(arg + 1);
  172. if(w <= 0 || h <= 0)
  173. return -1;
  174. src = ctx->images[ctx->nimages - 1];
  175. dst = pipi_tile(src, w, h);
  176. if(dst == NULL)
  177. return -1;
  178. pipi_free(src);
  179. ctx->images[ctx->nimages - 1] = dst;
  180. }
  181. else if(!strcmp(cmd, "scale"))
  182. {
  183. pipi_image_t *src, *dst;
  184. char const *arg;
  185. va_list ap;
  186. double scale;
  187. int w, h;
  188. if(ctx->nimages < 1)
  189. return -1;
  190. src = ctx->images[ctx->nimages - 1];
  191. va_start(ap, cmd);
  192. arg = va_arg(ap, char const *);
  193. va_end(ap);
  194. scale = atof(arg);
  195. w = (int)(scale * src->w + 0.5);
  196. h = (int)(scale * src->h + 0.5);
  197. if(w <= 0 || h <= 0)
  198. return -1;
  199. dst = pipi_resize(src, w, h);
  200. if(dst == NULL)
  201. return -1;
  202. pipi_free(src);
  203. ctx->images[ctx->nimages - 1] = dst;
  204. }
  205. else if(!strcmp(cmd, "brightness"))
  206. {
  207. pipi_image_t *src, *dst;
  208. char const *arg;
  209. va_list ap;
  210. double val;
  211. if(ctx->nimages < 1)
  212. return -1;
  213. va_start(ap, cmd);
  214. arg = va_arg(ap, char const *);
  215. va_end(ap);
  216. val = atof(arg);
  217. src = ctx->images[ctx->nimages - 1];
  218. dst = pipi_brightness(src, val);
  219. if(dst == NULL)
  220. return -1;
  221. pipi_free(src);
  222. ctx->images[ctx->nimages - 1] = dst;
  223. }
  224. else if(!strcmp(cmd, "contrast"))
  225. {
  226. pipi_image_t *src, *dst;
  227. char const *arg;
  228. va_list ap;
  229. double val;
  230. if(ctx->nimages < 1)
  231. return -1;
  232. va_start(ap, cmd);
  233. arg = va_arg(ap, char const *);
  234. va_end(ap);
  235. val = atof(arg);
  236. src = ctx->images[ctx->nimages - 1];
  237. dst = pipi_contrast(src, val);
  238. if(dst == NULL)
  239. return -1;
  240. pipi_free(src);
  241. ctx->images[ctx->nimages - 1] = dst;
  242. }
  243. else if(!strcmp(cmd, "mean"))
  244. {
  245. pipi_image_t *dst;
  246. if(ctx->nimages < 2)
  247. return -1;
  248. dst = pipi_mean(ctx->images[ctx->nimages - 2],
  249. ctx->images[ctx->nimages - 1]);
  250. if(dst == NULL)
  251. return -1;
  252. pipi_free(ctx->images[ctx->nimages - 2]);
  253. pipi_free(ctx->images[ctx->nimages - 1]);
  254. ctx->images[ctx->nimages - 2] = dst;
  255. ctx->nimages--;
  256. }
  257. else if(!strcmp(cmd, "min"))
  258. {
  259. pipi_image_t *dst;
  260. if(ctx->nimages < 2)
  261. return -1;
  262. dst = pipi_min(ctx->images[ctx->nimages - 2],
  263. ctx->images[ctx->nimages - 1]);
  264. if(dst == NULL)
  265. return -1;
  266. pipi_free(ctx->images[ctx->nimages - 2]);
  267. pipi_free(ctx->images[ctx->nimages - 1]);
  268. ctx->images[ctx->nimages - 2] = dst;
  269. ctx->nimages--;
  270. }
  271. else if(!strcmp(cmd, "max"))
  272. {
  273. pipi_image_t *dst;
  274. if(ctx->nimages < 2)
  275. return -1;
  276. dst = pipi_max(ctx->images[ctx->nimages - 2],
  277. ctx->images[ctx->nimages - 1]);
  278. if(dst == NULL)
  279. return -1;
  280. pipi_free(ctx->images[ctx->nimages - 2]);
  281. pipi_free(ctx->images[ctx->nimages - 1]);
  282. ctx->images[ctx->nimages - 2] = dst;
  283. ctx->nimages--;
  284. }
  285. else if(!strcmp(cmd, "add"))
  286. {
  287. pipi_image_t *dst;
  288. if(ctx->nimages < 2)
  289. return -1;
  290. dst = pipi_add(ctx->images[ctx->nimages - 2],
  291. ctx->images[ctx->nimages - 1]);
  292. if(dst == NULL)
  293. return -1;
  294. pipi_free(ctx->images[ctx->nimages - 2]);
  295. pipi_free(ctx->images[ctx->nimages - 1]);
  296. ctx->images[ctx->nimages - 2] = dst;
  297. ctx->nimages--;
  298. }
  299. else if(!strcmp(cmd, "sub"))
  300. {
  301. pipi_image_t *dst;
  302. if(ctx->nimages < 2)
  303. return -1;
  304. dst = pipi_sub(ctx->images[ctx->nimages - 2],
  305. ctx->images[ctx->nimages - 1]);
  306. if(dst == NULL)
  307. return -1;
  308. pipi_free(ctx->images[ctx->nimages - 2]);
  309. pipi_free(ctx->images[ctx->nimages - 1]);
  310. ctx->images[ctx->nimages - 2] = dst;
  311. ctx->nimages--;
  312. }
  313. else if(!strcmp(cmd, "difference"))
  314. {
  315. pipi_image_t *dst;
  316. if(ctx->nimages < 2)
  317. return -1;
  318. dst = pipi_difference(ctx->images[ctx->nimages - 2],
  319. ctx->images[ctx->nimages - 1]);
  320. if(dst == NULL)
  321. return -1;
  322. pipi_free(ctx->images[ctx->nimages - 2]);
  323. pipi_free(ctx->images[ctx->nimages - 1]);
  324. ctx->images[ctx->nimages - 2] = dst;
  325. ctx->nimages--;
  326. }
  327. else if(!strcmp(cmd, "multiply"))
  328. {
  329. pipi_image_t *dst;
  330. if(ctx->nimages < 2)
  331. return -1;
  332. dst = pipi_multiply(ctx->images[ctx->nimages - 2],
  333. ctx->images[ctx->nimages - 1]);
  334. if(dst == NULL)
  335. return -1;
  336. pipi_free(ctx->images[ctx->nimages - 2]);
  337. pipi_free(ctx->images[ctx->nimages - 1]);
  338. ctx->images[ctx->nimages - 2] = dst;
  339. ctx->nimages--;
  340. }
  341. else if(!strcmp(cmd, "divide"))
  342. {
  343. pipi_image_t *dst;
  344. if(ctx->nimages < 2)
  345. return -1;
  346. dst = pipi_divide(ctx->images[ctx->nimages - 2],
  347. ctx->images[ctx->nimages - 1]);
  348. if(dst == NULL)
  349. return -1;
  350. pipi_free(ctx->images[ctx->nimages - 2]);
  351. pipi_free(ctx->images[ctx->nimages - 1]);
  352. ctx->images[ctx->nimages - 2] = dst;
  353. ctx->nimages--;
  354. }
  355. else if(!strcmp(cmd, "screen"))
  356. {
  357. pipi_image_t *dst;
  358. if(ctx->nimages < 2)
  359. return -1;
  360. dst = pipi_screen(ctx->images[ctx->nimages - 2],
  361. ctx->images[ctx->nimages - 1]);
  362. if(dst == NULL)
  363. return -1;
  364. pipi_free(ctx->images[ctx->nimages - 2]);
  365. pipi_free(ctx->images[ctx->nimages - 1]);
  366. ctx->images[ctx->nimages - 2] = dst;
  367. ctx->nimages--;
  368. }
  369. else if(!strcmp(cmd, "overlay"))
  370. {
  371. pipi_image_t *dst;
  372. if(ctx->nimages < 2)
  373. return -1;
  374. dst = pipi_overlay(ctx->images[ctx->nimages - 2],
  375. ctx->images[ctx->nimages - 1]);
  376. if(dst == NULL)
  377. return -1;
  378. pipi_free(ctx->images[ctx->nimages - 2]);
  379. pipi_free(ctx->images[ctx->nimages - 1]);
  380. ctx->images[ctx->nimages - 2] = dst;
  381. ctx->nimages--;
  382. }
  383. else if(!strcmp(cmd, "wrap"))
  384. {
  385. if(ctx->nimages < 1)
  386. return -1;
  387. ctx->images[ctx->nimages - 1]->wrap = 1;
  388. }
  389. else if(!strcmp(cmd, "autocontrast"))
  390. {
  391. pipi_image_t *tmp;
  392. if(ctx->nimages < 1)
  393. return -1;
  394. tmp = ctx->images[ctx->nimages - 1];
  395. ctx->images[ctx->nimages - 1] = pipi_autocontrast(tmp);
  396. pipi_free(tmp);
  397. }
  398. else if(!strcmp(cmd, "invert"))
  399. {
  400. pipi_image_t *tmp;
  401. if(ctx->nimages < 1)
  402. return -1;
  403. tmp = ctx->images[ctx->nimages - 1];
  404. ctx->images[ctx->nimages - 1] = pipi_invert(tmp);
  405. pipi_free(tmp);
  406. }
  407. else if(!strcmp(cmd, "gray"))
  408. {
  409. if(ctx->nimages < 1)
  410. return -1;
  411. pipi_getpixels(ctx->images[ctx->nimages - 1], PIPI_PIXELS_Y_F);
  412. }
  413. else if(!strcmp(cmd, "free"))
  414. {
  415. if(ctx->nimages < 1)
  416. return -1;
  417. ctx->nimages--;
  418. pipi_free(ctx->images[ctx->nimages]);
  419. }
  420. else if(!strcmp(cmd, "dup"))
  421. {
  422. if(ctx->nimages < 1)
  423. return -1;
  424. ctx->images[ctx->nimages] = pipi_copy(ctx->images[ctx->nimages - 1]);
  425. ctx->nimages++;
  426. }
  427. else if(!strcmp(cmd, "swap"))
  428. {
  429. pipi_image_t *tmp;
  430. if(ctx->nimages < 2)
  431. return -1;
  432. tmp = ctx->images[ctx->nimages - 1];
  433. ctx->images[ctx->nimages - 1] = ctx->images[ctx->nimages - 2];
  434. ctx->images[ctx->nimages - 2] = tmp;
  435. }
  436. else
  437. {
  438. return -1;
  439. }
  440. return 0;
  441. }