Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

context.c 15 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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, "boxblur"))
  132. {
  133. pipi_image_t *src, *dst;
  134. char const *arg;
  135. va_list ap;
  136. double 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 = h = atof(arg);
  143. arg = strchr(arg, 'x');
  144. if(arg)
  145. h = atof(arg + 1);
  146. src = ctx->images[ctx->nimages - 1];
  147. dst = pipi_box_blur_ext(src, w, h);
  148. if(dst == NULL)
  149. return -1;
  150. pipi_free(src);
  151. ctx->images[ctx->nimages - 1] = dst;
  152. }
  153. else if(!strcmp(cmd, "median"))
  154. {
  155. pipi_image_t *src, *dst;
  156. char const *arg;
  157. va_list ap;
  158. double w, h;
  159. if(ctx->nimages < 1)
  160. return -1;
  161. va_start(ap, cmd);
  162. arg = va_arg(ap, char const *);
  163. va_end(ap);
  164. w = h = atof(arg);
  165. arg = strchr(arg, 'x');
  166. if(arg)
  167. h = atof(arg + 1);
  168. src = ctx->images[ctx->nimages - 1];
  169. dst = pipi_median_ext(src, w, h);
  170. if(dst == NULL)
  171. return -1;
  172. pipi_free(src);
  173. ctx->images[ctx->nimages - 1] = dst;
  174. }
  175. else if(!strcmp(cmd, "geometry"))
  176. {
  177. pipi_image_t *src, *dst;
  178. char const *arg;
  179. va_list ap;
  180. int w, h;
  181. if(ctx->nimages < 1)
  182. return -1;
  183. va_start(ap, cmd);
  184. arg = va_arg(ap, char const *);
  185. va_end(ap);
  186. w = atoi(arg);
  187. arg = strchr(arg, 'x');
  188. if(!arg)
  189. return -1;
  190. h = atoi(arg + 1);
  191. if(w <= 0 || h <= 0)
  192. return -1;
  193. src = ctx->images[ctx->nimages - 1];
  194. dst = pipi_resize(src, w, h);
  195. if(dst == NULL)
  196. return -1;
  197. pipi_free(src);
  198. ctx->images[ctx->nimages - 1] = dst;
  199. }
  200. else if(!strcmp(cmd, "tile"))
  201. {
  202. pipi_image_t *src, *dst;
  203. char const *arg;
  204. va_list ap;
  205. int w, h;
  206. if(ctx->nimages < 1)
  207. return -1;
  208. va_start(ap, cmd);
  209. arg = va_arg(ap, char const *);
  210. va_end(ap);
  211. w = atoi(arg);
  212. arg = strchr(arg, 'x');
  213. if(!arg)
  214. return -1;
  215. h = atoi(arg + 1);
  216. if(w <= 0 || h <= 0)
  217. return -1;
  218. src = ctx->images[ctx->nimages - 1];
  219. dst = pipi_tile(src, w, h);
  220. if(dst == NULL)
  221. return -1;
  222. pipi_free(src);
  223. ctx->images[ctx->nimages - 1] = dst;
  224. }
  225. else if(!strcmp(cmd, "scale"))
  226. {
  227. pipi_image_t *src, *dst;
  228. char const *arg;
  229. va_list ap;
  230. double scale;
  231. int w, h;
  232. if(ctx->nimages < 1)
  233. return -1;
  234. src = ctx->images[ctx->nimages - 1];
  235. va_start(ap, cmd);
  236. arg = va_arg(ap, char const *);
  237. va_end(ap);
  238. scale = atof(arg);
  239. w = (int)(scale * src->w + 0.5);
  240. h = (int)(scale * src->h + 0.5);
  241. if(w <= 0 || h <= 0)
  242. return -1;
  243. dst = pipi_resize(src, w, h);
  244. if(dst == NULL)
  245. return -1;
  246. pipi_free(src);
  247. ctx->images[ctx->nimages - 1] = dst;
  248. }
  249. else if(!strcmp(cmd, "brightness"))
  250. {
  251. pipi_image_t *src, *dst;
  252. char const *arg;
  253. va_list ap;
  254. double val;
  255. if(ctx->nimages < 1)
  256. return -1;
  257. va_start(ap, cmd);
  258. arg = va_arg(ap, char const *);
  259. va_end(ap);
  260. val = atof(arg);
  261. src = ctx->images[ctx->nimages - 1];
  262. dst = pipi_brightness(src, val);
  263. if(dst == NULL)
  264. return -1;
  265. pipi_free(src);
  266. ctx->images[ctx->nimages - 1] = dst;
  267. }
  268. else if(!strcmp(cmd, "contrast"))
  269. {
  270. pipi_image_t *src, *dst;
  271. char const *arg;
  272. va_list ap;
  273. double val;
  274. if(ctx->nimages < 1)
  275. return -1;
  276. va_start(ap, cmd);
  277. arg = va_arg(ap, char const *);
  278. va_end(ap);
  279. val = atof(arg);
  280. src = ctx->images[ctx->nimages - 1];
  281. dst = pipi_contrast(src, val);
  282. if(dst == NULL)
  283. return -1;
  284. pipi_free(src);
  285. ctx->images[ctx->nimages - 1] = dst;
  286. }
  287. else if(!strcmp(cmd, "threshold"))
  288. {
  289. pipi_image_t *src, *dst;
  290. char const *arg;
  291. va_list ap;
  292. double val;
  293. if(ctx->nimages < 1)
  294. return -1;
  295. va_start(ap, cmd);
  296. arg = va_arg(ap, char const *);
  297. va_end(ap);
  298. val = atof(arg);
  299. src = ctx->images[ctx->nimages - 1];
  300. dst = pipi_threshold(src, val);
  301. if(dst == NULL)
  302. return -1;
  303. pipi_free(src);
  304. ctx->images[ctx->nimages - 1] = dst;
  305. }
  306. else if(!strcmp(cmd, "hflip"))
  307. {
  308. pipi_image_t *tmp;
  309. if(ctx->nimages < 1)
  310. return -1;
  311. tmp = ctx->images[ctx->nimages - 1];
  312. ctx->images[ctx->nimages - 1] = pipi_hflip(tmp);
  313. pipi_free(tmp);
  314. }
  315. else if(!strcmp(cmd, "vflip"))
  316. {
  317. pipi_image_t *tmp;
  318. if(ctx->nimages < 1)
  319. return -1;
  320. tmp = ctx->images[ctx->nimages - 1];
  321. ctx->images[ctx->nimages - 1] = pipi_vflip(tmp);
  322. pipi_free(tmp);
  323. }
  324. else if(!strcmp(cmd, "mean"))
  325. {
  326. pipi_image_t *dst;
  327. if(ctx->nimages < 2)
  328. return -1;
  329. dst = pipi_mean(ctx->images[ctx->nimages - 2],
  330. ctx->images[ctx->nimages - 1]);
  331. if(dst == NULL)
  332. return -1;
  333. pipi_free(ctx->images[ctx->nimages - 2]);
  334. pipi_free(ctx->images[ctx->nimages - 1]);
  335. ctx->images[ctx->nimages - 2] = dst;
  336. ctx->nimages--;
  337. }
  338. else if(!strcmp(cmd, "min"))
  339. {
  340. pipi_image_t *dst;
  341. if(ctx->nimages < 2)
  342. return -1;
  343. dst = pipi_min(ctx->images[ctx->nimages - 2],
  344. ctx->images[ctx->nimages - 1]);
  345. if(dst == NULL)
  346. return -1;
  347. pipi_free(ctx->images[ctx->nimages - 2]);
  348. pipi_free(ctx->images[ctx->nimages - 1]);
  349. ctx->images[ctx->nimages - 2] = dst;
  350. ctx->nimages--;
  351. }
  352. else if(!strcmp(cmd, "max"))
  353. {
  354. pipi_image_t *dst;
  355. if(ctx->nimages < 2)
  356. return -1;
  357. dst = pipi_max(ctx->images[ctx->nimages - 2],
  358. ctx->images[ctx->nimages - 1]);
  359. if(dst == NULL)
  360. return -1;
  361. pipi_free(ctx->images[ctx->nimages - 2]);
  362. pipi_free(ctx->images[ctx->nimages - 1]);
  363. ctx->images[ctx->nimages - 2] = dst;
  364. ctx->nimages--;
  365. }
  366. else if(!strcmp(cmd, "add"))
  367. {
  368. pipi_image_t *dst;
  369. if(ctx->nimages < 2)
  370. return -1;
  371. dst = pipi_add(ctx->images[ctx->nimages - 2],
  372. ctx->images[ctx->nimages - 1]);
  373. if(dst == NULL)
  374. return -1;
  375. pipi_free(ctx->images[ctx->nimages - 2]);
  376. pipi_free(ctx->images[ctx->nimages - 1]);
  377. ctx->images[ctx->nimages - 2] = dst;
  378. ctx->nimages--;
  379. }
  380. else if(!strcmp(cmd, "sub"))
  381. {
  382. pipi_image_t *dst;
  383. if(ctx->nimages < 2)
  384. return -1;
  385. dst = pipi_sub(ctx->images[ctx->nimages - 2],
  386. ctx->images[ctx->nimages - 1]);
  387. if(dst == NULL)
  388. return -1;
  389. pipi_free(ctx->images[ctx->nimages - 2]);
  390. pipi_free(ctx->images[ctx->nimages - 1]);
  391. ctx->images[ctx->nimages - 2] = dst;
  392. ctx->nimages--;
  393. }
  394. else if(!strcmp(cmd, "difference"))
  395. {
  396. pipi_image_t *dst;
  397. if(ctx->nimages < 2)
  398. return -1;
  399. dst = pipi_difference(ctx->images[ctx->nimages - 2],
  400. ctx->images[ctx->nimages - 1]);
  401. if(dst == NULL)
  402. return -1;
  403. pipi_free(ctx->images[ctx->nimages - 2]);
  404. pipi_free(ctx->images[ctx->nimages - 1]);
  405. ctx->images[ctx->nimages - 2] = dst;
  406. ctx->nimages--;
  407. }
  408. else if(!strcmp(cmd, "multiply"))
  409. {
  410. pipi_image_t *dst;
  411. if(ctx->nimages < 2)
  412. return -1;
  413. dst = pipi_multiply(ctx->images[ctx->nimages - 2],
  414. ctx->images[ctx->nimages - 1]);
  415. if(dst == NULL)
  416. return -1;
  417. pipi_free(ctx->images[ctx->nimages - 2]);
  418. pipi_free(ctx->images[ctx->nimages - 1]);
  419. ctx->images[ctx->nimages - 2] = dst;
  420. ctx->nimages--;
  421. }
  422. else if(!strcmp(cmd, "divide"))
  423. {
  424. pipi_image_t *dst;
  425. if(ctx->nimages < 2)
  426. return -1;
  427. dst = pipi_divide(ctx->images[ctx->nimages - 2],
  428. ctx->images[ctx->nimages - 1]);
  429. if(dst == NULL)
  430. return -1;
  431. pipi_free(ctx->images[ctx->nimages - 2]);
  432. pipi_free(ctx->images[ctx->nimages - 1]);
  433. ctx->images[ctx->nimages - 2] = dst;
  434. ctx->nimages--;
  435. }
  436. else if(!strcmp(cmd, "screen"))
  437. {
  438. pipi_image_t *dst;
  439. if(ctx->nimages < 2)
  440. return -1;
  441. dst = pipi_screen(ctx->images[ctx->nimages - 2],
  442. ctx->images[ctx->nimages - 1]);
  443. if(dst == NULL)
  444. return -1;
  445. pipi_free(ctx->images[ctx->nimages - 2]);
  446. pipi_free(ctx->images[ctx->nimages - 1]);
  447. ctx->images[ctx->nimages - 2] = dst;
  448. ctx->nimages--;
  449. }
  450. else if(!strcmp(cmd, "overlay"))
  451. {
  452. pipi_image_t *dst;
  453. if(ctx->nimages < 2)
  454. return -1;
  455. dst = pipi_overlay(ctx->images[ctx->nimages - 2],
  456. ctx->images[ctx->nimages - 1]);
  457. if(dst == NULL)
  458. return -1;
  459. pipi_free(ctx->images[ctx->nimages - 2]);
  460. pipi_free(ctx->images[ctx->nimages - 1]);
  461. ctx->images[ctx->nimages - 2] = dst;
  462. ctx->nimages--;
  463. }
  464. else if(!strcmp(cmd, "wrap"))
  465. {
  466. if(ctx->nimages < 1)
  467. return -1;
  468. ctx->images[ctx->nimages - 1]->wrap = 1;
  469. }
  470. else if(!strcmp(cmd, "autocontrast"))
  471. {
  472. pipi_image_t *tmp;
  473. if(ctx->nimages < 1)
  474. return -1;
  475. tmp = ctx->images[ctx->nimages - 1];
  476. ctx->images[ctx->nimages - 1] = pipi_autocontrast(tmp);
  477. pipi_free(tmp);
  478. }
  479. else if(!strcmp(cmd, "invert"))
  480. {
  481. pipi_image_t *tmp;
  482. if(ctx->nimages < 1)
  483. return -1;
  484. tmp = ctx->images[ctx->nimages - 1];
  485. ctx->images[ctx->nimages - 1] = pipi_invert(tmp);
  486. pipi_free(tmp);
  487. }
  488. else if(!strcmp(cmd, "dilate"))
  489. {
  490. pipi_image_t *tmp;
  491. if(ctx->nimages < 1)
  492. return -1;
  493. tmp = ctx->images[ctx->nimages - 1];
  494. ctx->images[ctx->nimages - 1] = pipi_dilate(tmp);
  495. pipi_free(tmp);
  496. }
  497. else if(!strcmp(cmd, "erode"))
  498. {
  499. pipi_image_t *tmp;
  500. if(ctx->nimages < 1)
  501. return -1;
  502. tmp = ctx->images[ctx->nimages - 1];
  503. ctx->images[ctx->nimages - 1] = pipi_erode(tmp);
  504. pipi_free(tmp);
  505. }
  506. else if(!strcmp(cmd, "gray"))
  507. {
  508. if(ctx->nimages < 1)
  509. return -1;
  510. pipi_getpixels(ctx->images[ctx->nimages - 1], PIPI_PIXELS_Y_F);
  511. }
  512. else if(!strcmp(cmd, "free"))
  513. {
  514. if(ctx->nimages < 1)
  515. return -1;
  516. ctx->nimages--;
  517. pipi_free(ctx->images[ctx->nimages]);
  518. }
  519. else if(!strcmp(cmd, "dup"))
  520. {
  521. if(ctx->nimages < 1)
  522. return -1;
  523. ctx->images[ctx->nimages] = pipi_copy(ctx->images[ctx->nimages - 1]);
  524. ctx->nimages++;
  525. }
  526. else if(!strcmp(cmd, "swap"))
  527. {
  528. pipi_image_t *tmp;
  529. if(ctx->nimages < 2)
  530. return -1;
  531. tmp = ctx->images[ctx->nimages - 1];
  532. ctx->images[ctx->nimages - 1] = ctx->images[ctx->nimages - 2];
  533. ctx->images[ctx->nimages - 2] = tmp;
  534. }
  535. else
  536. {
  537. return -1;
  538. }
  539. return 0;
  540. }