Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

674 righe
18 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, "ost"))
  76. dst = pipi_dither_ostromoukhov(src, 0);
  77. else if(!strcmp(method, "sost"))
  78. dst = pipi_dither_ostromoukhov(src, 1);
  79. else if(!strcmp(method, "ediff"))
  80. {
  81. if(ctx->nimages < 2)
  82. return -1;
  83. dst = pipi_dither_ediff(ctx->images[ctx->nimages - 2], src, 0);
  84. pipi_free(ctx->images[ctx->nimages - 2]);
  85. ctx->nimages--;
  86. }
  87. else if(!strcmp(method, "sediff"))
  88. {
  89. if(ctx->nimages < 2)
  90. return -1;
  91. dst = pipi_dither_ediff(ctx->images[ctx->nimages - 2], src, 1);
  92. pipi_free(ctx->images[ctx->nimages - 2]);
  93. ctx->nimages--;
  94. }
  95. else if(!strcmp(method, "ordered"))
  96. {
  97. if(ctx->nimages < 2)
  98. return -1;
  99. dst = pipi_dither_ordered(ctx->images[ctx->nimages - 2], src);
  100. pipi_free(ctx->images[ctx->nimages - 2]);
  101. ctx->nimages--;
  102. }
  103. else if(!strncmp(method, "halftone", 8))
  104. {
  105. double r, angle = .0;
  106. method = strchr(method, ':');
  107. if(!method)
  108. return -1;
  109. r = atof(method + 1);
  110. method = strchr(method + 1, ':');
  111. if(method)
  112. angle = atof(method + 1);
  113. if(r < 1.)
  114. r = 1.;
  115. dst = pipi_dither_halftone(src, r, angle);
  116. }
  117. else if(!strcmp(method, "random"))
  118. dst = pipi_dither_random(src);
  119. else if(!strcmp(method, "dbs"))
  120. dst = pipi_dither_dbs(src);
  121. if(dst == NULL)
  122. return -1;
  123. pipi_free(src);
  124. ctx->images[ctx->nimages - 1] = dst;
  125. }
  126. else if(!strcmp(cmd, "blur"))
  127. {
  128. pipi_image_t *src, *dst;
  129. char const *arg;
  130. va_list ap;
  131. double w, h, a = 0.0;
  132. if(ctx->nimages < 1)
  133. return -1;
  134. va_start(ap, cmd);
  135. arg = va_arg(ap, char const *);
  136. va_end(ap);
  137. w = h = atof(arg);
  138. arg = strchr(arg, 'x');
  139. if(arg)
  140. {
  141. h = atof(arg + 1);
  142. arg = strchr(arg, 'r');
  143. if(arg)
  144. a = atof(arg + 1);
  145. }
  146. src = ctx->images[ctx->nimages - 1];
  147. dst = pipi_gaussian_blur_ext(src, w, h, a, 0.0, 0.0);
  148. if(dst == NULL)
  149. return -1;
  150. pipi_free(src);
  151. ctx->images[ctx->nimages - 1] = dst;
  152. }
  153. else if(!strcmp(cmd, "boxblur"))
  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_box_blur_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, "median"))
  176. {
  177. pipi_image_t *src, *dst;
  178. char const *arg;
  179. va_list ap;
  180. double 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 = h = atof(arg);
  187. arg = strchr(arg, 'x');
  188. if(arg)
  189. h = atof(arg + 1);
  190. src = ctx->images[ctx->nimages - 1];
  191. dst = pipi_median_ext(src, w, h);
  192. if(dst == NULL)
  193. return -1;
  194. pipi_free(src);
  195. ctx->images[ctx->nimages - 1] = dst;
  196. }
  197. else if(!strcmp(cmd, "geometry"))
  198. {
  199. pipi_image_t *src, *dst;
  200. char const *arg;
  201. va_list ap;
  202. int w, h;
  203. if(ctx->nimages < 1)
  204. return -1;
  205. va_start(ap, cmd);
  206. arg = va_arg(ap, char const *);
  207. va_end(ap);
  208. w = atoi(arg);
  209. arg = strchr(arg, 'x');
  210. if(!arg)
  211. return -1;
  212. h = atoi(arg + 1);
  213. if(w <= 0 || h <= 0)
  214. return -1;
  215. src = ctx->images[ctx->nimages - 1];
  216. dst = pipi_resize(src, w, h);
  217. if(dst == NULL)
  218. return -1;
  219. pipi_free(src);
  220. ctx->images[ctx->nimages - 1] = dst;
  221. }
  222. else if(!strcmp(cmd, "tile"))
  223. {
  224. pipi_image_t *src, *dst;
  225. char const *arg;
  226. va_list ap;
  227. int w, h;
  228. if(ctx->nimages < 1)
  229. return -1;
  230. va_start(ap, cmd);
  231. arg = va_arg(ap, char const *);
  232. va_end(ap);
  233. w = atoi(arg);
  234. arg = strchr(arg, 'x');
  235. if(!arg)
  236. return -1;
  237. h = atoi(arg + 1);
  238. if(w <= 0 || h <= 0)
  239. return -1;
  240. src = ctx->images[ctx->nimages - 1];
  241. dst = pipi_tile(src, w, h);
  242. if(dst == NULL)
  243. return -1;
  244. pipi_free(src);
  245. ctx->images[ctx->nimages - 1] = dst;
  246. }
  247. else if(!strcmp(cmd, "scale"))
  248. {
  249. pipi_image_t *src, *dst;
  250. char const *arg;
  251. va_list ap;
  252. double scale;
  253. int w, h;
  254. if(ctx->nimages < 1)
  255. return -1;
  256. src = ctx->images[ctx->nimages - 1];
  257. va_start(ap, cmd);
  258. arg = va_arg(ap, char const *);
  259. va_end(ap);
  260. scale = atof(arg);
  261. w = (int)(scale * src->w + 0.5);
  262. h = (int)(scale * src->h + 0.5);
  263. if(w <= 0 || h <= 0)
  264. return -1;
  265. dst = pipi_resize(src, w, h);
  266. if(dst == NULL)
  267. return -1;
  268. pipi_free(src);
  269. ctx->images[ctx->nimages - 1] = dst;
  270. }
  271. else if(!strcmp(cmd, "brightness"))
  272. {
  273. pipi_image_t *src, *dst;
  274. char const *arg;
  275. va_list ap;
  276. double val;
  277. if(ctx->nimages < 1)
  278. return -1;
  279. va_start(ap, cmd);
  280. arg = va_arg(ap, char const *);
  281. va_end(ap);
  282. val = atof(arg);
  283. src = ctx->images[ctx->nimages - 1];
  284. dst = pipi_brightness(src, val);
  285. if(dst == NULL)
  286. return -1;
  287. pipi_free(src);
  288. ctx->images[ctx->nimages - 1] = dst;
  289. }
  290. else if(!strcmp(cmd, "contrast"))
  291. {
  292. pipi_image_t *src, *dst;
  293. char const *arg;
  294. va_list ap;
  295. double val;
  296. if(ctx->nimages < 1)
  297. return -1;
  298. va_start(ap, cmd);
  299. arg = va_arg(ap, char const *);
  300. va_end(ap);
  301. val = atof(arg);
  302. src = ctx->images[ctx->nimages - 1];
  303. dst = pipi_contrast(src, val);
  304. if(dst == NULL)
  305. return -1;
  306. pipi_free(src);
  307. ctx->images[ctx->nimages - 1] = dst;
  308. }
  309. else if(!strcmp(cmd, "threshold"))
  310. {
  311. pipi_image_t *src, *dst;
  312. char const *arg;
  313. va_list ap;
  314. double val;
  315. if(ctx->nimages < 1)
  316. return -1;
  317. va_start(ap, cmd);
  318. arg = va_arg(ap, char const *);
  319. va_end(ap);
  320. val = atof(arg);
  321. src = ctx->images[ctx->nimages - 1];
  322. dst = pipi_threshold(src, val);
  323. if(dst == NULL)
  324. return -1;
  325. pipi_free(src);
  326. ctx->images[ctx->nimages - 1] = dst;
  327. }
  328. else if(!strcmp(cmd, "hflip"))
  329. {
  330. pipi_image_t *tmp;
  331. if(ctx->nimages < 1)
  332. return -1;
  333. tmp = ctx->images[ctx->nimages - 1];
  334. ctx->images[ctx->nimages - 1] = pipi_hflip(tmp);
  335. pipi_free(tmp);
  336. }
  337. else if(!strcmp(cmd, "vflip"))
  338. {
  339. pipi_image_t *tmp;
  340. if(ctx->nimages < 1)
  341. return -1;
  342. tmp = ctx->images[ctx->nimages - 1];
  343. ctx->images[ctx->nimages - 1] = pipi_vflip(tmp);
  344. pipi_free(tmp);
  345. }
  346. else if(!strcmp(cmd, "rotate90"))
  347. {
  348. pipi_image_t *tmp;
  349. if(ctx->nimages < 1)
  350. return -1;
  351. tmp = ctx->images[ctx->nimages - 1];
  352. ctx->images[ctx->nimages - 1] = pipi_rotate90(tmp);
  353. pipi_free(tmp);
  354. }
  355. else if(!strcmp(cmd, "rotate180"))
  356. {
  357. pipi_image_t *tmp;
  358. if(ctx->nimages < 1)
  359. return -1;
  360. tmp = ctx->images[ctx->nimages - 1];
  361. ctx->images[ctx->nimages - 1] = pipi_rotate180(tmp);
  362. pipi_free(tmp);
  363. }
  364. else if(!strcmp(cmd, "rotate270"))
  365. {
  366. pipi_image_t *tmp;
  367. if(ctx->nimages < 1)
  368. return -1;
  369. tmp = ctx->images[ctx->nimages - 1];
  370. ctx->images[ctx->nimages - 1] = pipi_rotate270(tmp);
  371. pipi_free(tmp);
  372. }
  373. else if(!strcmp(cmd, "split"))
  374. {
  375. pipi_image_t *src;
  376. if(ctx->nimages < 1)
  377. return -1;
  378. src = ctx->images[ctx->nimages - 1];
  379. ctx->nimages += 2;
  380. ctx->images[ctx->nimages - 3] = pipi_red(src);
  381. ctx->images[ctx->nimages - 2] = pipi_green(src);
  382. ctx->images[ctx->nimages - 1] = pipi_blue(src);
  383. pipi_free(src);
  384. }
  385. else if(!strcmp(cmd, "combine"))
  386. {
  387. pipi_image_t *dst;
  388. if(ctx->nimages < 3)
  389. return -1;
  390. dst = pipi_rgb(ctx->images[ctx->nimages - 3],
  391. ctx->images[ctx->nimages - 2],
  392. ctx->images[ctx->nimages - 1]);
  393. if(dst == NULL)
  394. return -1;
  395. pipi_free(ctx->images[ctx->nimages - 3]);
  396. pipi_free(ctx->images[ctx->nimages - 2]);
  397. pipi_free(ctx->images[ctx->nimages - 1]);
  398. ctx->images[ctx->nimages - 3] = dst;
  399. ctx->nimages -= 2;
  400. }
  401. else if(!strcmp(cmd, "mean"))
  402. {
  403. pipi_image_t *dst;
  404. if(ctx->nimages < 2)
  405. return -1;
  406. dst = pipi_mean(ctx->images[ctx->nimages - 2],
  407. ctx->images[ctx->nimages - 1]);
  408. if(dst == NULL)
  409. return -1;
  410. pipi_free(ctx->images[ctx->nimages - 2]);
  411. pipi_free(ctx->images[ctx->nimages - 1]);
  412. ctx->images[ctx->nimages - 2] = dst;
  413. ctx->nimages--;
  414. }
  415. else if(!strcmp(cmd, "min"))
  416. {
  417. pipi_image_t *dst;
  418. if(ctx->nimages < 2)
  419. return -1;
  420. dst = pipi_min(ctx->images[ctx->nimages - 2],
  421. ctx->images[ctx->nimages - 1]);
  422. if(dst == NULL)
  423. return -1;
  424. pipi_free(ctx->images[ctx->nimages - 2]);
  425. pipi_free(ctx->images[ctx->nimages - 1]);
  426. ctx->images[ctx->nimages - 2] = dst;
  427. ctx->nimages--;
  428. }
  429. else if(!strcmp(cmd, "max"))
  430. {
  431. pipi_image_t *dst;
  432. if(ctx->nimages < 2)
  433. return -1;
  434. dst = pipi_max(ctx->images[ctx->nimages - 2],
  435. ctx->images[ctx->nimages - 1]);
  436. if(dst == NULL)
  437. return -1;
  438. pipi_free(ctx->images[ctx->nimages - 2]);
  439. pipi_free(ctx->images[ctx->nimages - 1]);
  440. ctx->images[ctx->nimages - 2] = dst;
  441. ctx->nimages--;
  442. }
  443. else if(!strcmp(cmd, "add"))
  444. {
  445. pipi_image_t *dst;
  446. if(ctx->nimages < 2)
  447. return -1;
  448. dst = pipi_add(ctx->images[ctx->nimages - 2],
  449. ctx->images[ctx->nimages - 1]);
  450. if(dst == NULL)
  451. return -1;
  452. pipi_free(ctx->images[ctx->nimages - 2]);
  453. pipi_free(ctx->images[ctx->nimages - 1]);
  454. ctx->images[ctx->nimages - 2] = dst;
  455. ctx->nimages--;
  456. }
  457. else if(!strcmp(cmd, "sub"))
  458. {
  459. pipi_image_t *dst;
  460. if(ctx->nimages < 2)
  461. return -1;
  462. dst = pipi_sub(ctx->images[ctx->nimages - 2],
  463. ctx->images[ctx->nimages - 1]);
  464. if(dst == NULL)
  465. return -1;
  466. pipi_free(ctx->images[ctx->nimages - 2]);
  467. pipi_free(ctx->images[ctx->nimages - 1]);
  468. ctx->images[ctx->nimages - 2] = dst;
  469. ctx->nimages--;
  470. }
  471. else if(!strcmp(cmd, "difference"))
  472. {
  473. pipi_image_t *dst;
  474. if(ctx->nimages < 2)
  475. return -1;
  476. dst = pipi_difference(ctx->images[ctx->nimages - 2],
  477. ctx->images[ctx->nimages - 1]);
  478. if(dst == NULL)
  479. return -1;
  480. pipi_free(ctx->images[ctx->nimages - 2]);
  481. pipi_free(ctx->images[ctx->nimages - 1]);
  482. ctx->images[ctx->nimages - 2] = dst;
  483. ctx->nimages--;
  484. }
  485. else if(!strcmp(cmd, "multiply"))
  486. {
  487. pipi_image_t *dst;
  488. if(ctx->nimages < 2)
  489. return -1;
  490. dst = pipi_multiply(ctx->images[ctx->nimages - 2],
  491. ctx->images[ctx->nimages - 1]);
  492. if(dst == NULL)
  493. return -1;
  494. pipi_free(ctx->images[ctx->nimages - 2]);
  495. pipi_free(ctx->images[ctx->nimages - 1]);
  496. ctx->images[ctx->nimages - 2] = dst;
  497. ctx->nimages--;
  498. }
  499. else if(!strcmp(cmd, "divide"))
  500. {
  501. pipi_image_t *dst;
  502. if(ctx->nimages < 2)
  503. return -1;
  504. dst = pipi_divide(ctx->images[ctx->nimages - 2],
  505. ctx->images[ctx->nimages - 1]);
  506. if(dst == NULL)
  507. return -1;
  508. pipi_free(ctx->images[ctx->nimages - 2]);
  509. pipi_free(ctx->images[ctx->nimages - 1]);
  510. ctx->images[ctx->nimages - 2] = dst;
  511. ctx->nimages--;
  512. }
  513. else if(!strcmp(cmd, "screen"))
  514. {
  515. pipi_image_t *dst;
  516. if(ctx->nimages < 2)
  517. return -1;
  518. dst = pipi_screen(ctx->images[ctx->nimages - 2],
  519. ctx->images[ctx->nimages - 1]);
  520. if(dst == NULL)
  521. return -1;
  522. pipi_free(ctx->images[ctx->nimages - 2]);
  523. pipi_free(ctx->images[ctx->nimages - 1]);
  524. ctx->images[ctx->nimages - 2] = dst;
  525. ctx->nimages--;
  526. }
  527. else if(!strcmp(cmd, "overlay"))
  528. {
  529. pipi_image_t *dst;
  530. if(ctx->nimages < 2)
  531. return -1;
  532. dst = pipi_overlay(ctx->images[ctx->nimages - 2],
  533. ctx->images[ctx->nimages - 1]);
  534. if(dst == NULL)
  535. return -1;
  536. pipi_free(ctx->images[ctx->nimages - 2]);
  537. pipi_free(ctx->images[ctx->nimages - 1]);
  538. ctx->images[ctx->nimages - 2] = dst;
  539. ctx->nimages--;
  540. }
  541. else if(!strcmp(cmd, "wrap"))
  542. {
  543. if(ctx->nimages < 1)
  544. return -1;
  545. ctx->images[ctx->nimages - 1]->wrap = 1;
  546. }
  547. else if(!strcmp(cmd, "autocontrast"))
  548. {
  549. pipi_image_t *tmp;
  550. if(ctx->nimages < 1)
  551. return -1;
  552. tmp = ctx->images[ctx->nimages - 1];
  553. ctx->images[ctx->nimages - 1] = pipi_autocontrast(tmp);
  554. pipi_free(tmp);
  555. }
  556. else if(!strcmp(cmd, "invert"))
  557. {
  558. pipi_image_t *tmp;
  559. if(ctx->nimages < 1)
  560. return -1;
  561. tmp = ctx->images[ctx->nimages - 1];
  562. ctx->images[ctx->nimages - 1] = pipi_invert(tmp);
  563. pipi_free(tmp);
  564. }
  565. else if(!strcmp(cmd, "dilate"))
  566. {
  567. pipi_image_t *tmp;
  568. if(ctx->nimages < 1)
  569. return -1;
  570. tmp = ctx->images[ctx->nimages - 1];
  571. ctx->images[ctx->nimages - 1] = pipi_dilate(tmp);
  572. pipi_free(tmp);
  573. }
  574. else if(!strcmp(cmd, "erode"))
  575. {
  576. pipi_image_t *tmp;
  577. if(ctx->nimages < 1)
  578. return -1;
  579. tmp = ctx->images[ctx->nimages - 1];
  580. ctx->images[ctx->nimages - 1] = pipi_erode(tmp);
  581. pipi_free(tmp);
  582. }
  583. else if(!strcmp(cmd, "gray"))
  584. {
  585. if(ctx->nimages < 1)
  586. return -1;
  587. pipi_getpixels(ctx->images[ctx->nimages - 1], PIPI_PIXELS_Y_F);
  588. }
  589. else if(!strcmp(cmd, "free"))
  590. {
  591. if(ctx->nimages < 1)
  592. return -1;
  593. ctx->nimages--;
  594. pipi_free(ctx->images[ctx->nimages]);
  595. }
  596. else if(!strcmp(cmd, "dup"))
  597. {
  598. if(ctx->nimages < 1)
  599. return -1;
  600. ctx->images[ctx->nimages] = pipi_copy(ctx->images[ctx->nimages - 1]);
  601. ctx->nimages++;
  602. }
  603. else if(!strcmp(cmd, "swap"))
  604. {
  605. pipi_image_t *tmp;
  606. if(ctx->nimages < 2)
  607. return -1;
  608. tmp = ctx->images[ctx->nimages - 1];
  609. ctx->images[ctx->nimages - 1] = ctx->images[ctx->nimages - 2];
  610. ctx->images[ctx->nimages - 2] = tmp;
  611. }
  612. else if(!strcmp(cmd, "roll"))
  613. {
  614. pipi_image_t *tmp;
  615. char const *arg;
  616. va_list ap;
  617. int val;
  618. va_start(ap, cmd);
  619. arg = va_arg(ap, char const *);
  620. va_end(ap);
  621. val = atoi(arg);
  622. if(val <= 0 || ctx->nimages < val)
  623. return -1;
  624. if(val == 1)
  625. return 0;
  626. tmp = ctx->images[ctx->nimages - val];
  627. memmove(ctx->images + ctx->nimages - val,
  628. ctx->images + ctx->nimages - val + 1,
  629. (val - 1) * sizeof(*ctx->images));
  630. ctx->images[ctx->nimages - 1] = tmp;
  631. }
  632. else
  633. {
  634. return -1;
  635. }
  636. return 0;
  637. }