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.
 
 
 
 
 
 

222 lines
5.9 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 <= 0)
  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 <= 0)
  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. if(ctx->nimages <= 0)
  110. return -1;
  111. va_start(ap, cmd);
  112. arg = va_arg(ap, char const *);
  113. va_end(ap);
  114. src = ctx->images[ctx->nimages - 1];
  115. dst = pipi_gaussian_blur(src, atof(arg));
  116. if(dst == NULL)
  117. return -1;
  118. pipi_free(src);
  119. ctx->images[ctx->nimages - 1] = dst;
  120. }
  121. else if(!strcmp(cmd, "mean"))
  122. {
  123. pipi_image_t *dst;
  124. if(ctx->nimages < 2)
  125. return -1;
  126. dst = pipi_mean(ctx->images[ctx->nimages - 2],
  127. ctx->images[ctx->nimages - 1]);
  128. if(dst == NULL)
  129. return -1;
  130. pipi_free(ctx->images[ctx->nimages - 2]);
  131. pipi_free(ctx->images[ctx->nimages - 1]);
  132. ctx->images[ctx->nimages - 2] = dst;
  133. ctx->nimages--;
  134. }
  135. else if(!strcmp(cmd, "min"))
  136. {
  137. pipi_image_t *dst;
  138. if(ctx->nimages < 2)
  139. return -1;
  140. dst = pipi_min(ctx->images[ctx->nimages - 2],
  141. ctx->images[ctx->nimages - 1]);
  142. if(dst == NULL)
  143. return -1;
  144. pipi_free(ctx->images[ctx->nimages - 2]);
  145. pipi_free(ctx->images[ctx->nimages - 1]);
  146. ctx->images[ctx->nimages - 2] = dst;
  147. ctx->nimages--;
  148. }
  149. else if(!strcmp(cmd, "max"))
  150. {
  151. pipi_image_t *dst;
  152. if(ctx->nimages < 2)
  153. return -1;
  154. dst = pipi_max(ctx->images[ctx->nimages - 2],
  155. ctx->images[ctx->nimages - 1]);
  156. if(dst == NULL)
  157. return -1;
  158. pipi_free(ctx->images[ctx->nimages - 2]);
  159. pipi_free(ctx->images[ctx->nimages - 1]);
  160. ctx->images[ctx->nimages - 2] = dst;
  161. ctx->nimages--;
  162. }
  163. else if(!strcmp(cmd, "wrap"))
  164. {
  165. if(ctx->nimages <= 0)
  166. return -1;
  167. ctx->images[ctx->nimages - 1]->wrap = 1;
  168. }
  169. else if(!strcmp(cmd, "autocontrast"))
  170. {
  171. pipi_image_t *tmp;
  172. if(ctx->nimages <= 0)
  173. return -1;
  174. tmp = ctx->images[ctx->nimages - 1];
  175. ctx->images[ctx->nimages - 1] = pipi_autocontrast(tmp);
  176. pipi_free(tmp);
  177. }
  178. else if(!strcmp(cmd, "gray"))
  179. {
  180. if(ctx->nimages <= 0)
  181. return -1;
  182. pipi_getpixels(ctx->images[ctx->nimages - 1], PIPI_PIXELS_Y_F);
  183. }
  184. else if(!strcmp(cmd, "free"))
  185. {
  186. if(ctx->nimages <= 0)
  187. return -1;
  188. ctx->nimages--;
  189. pipi_free(ctx->images[ctx->nimages]);
  190. }
  191. else if(!strcmp(cmd, "dup"))
  192. {
  193. if(ctx->nimages <= 0)
  194. return -1;
  195. ctx->images[ctx->nimages] = pipi_copy(ctx->images[ctx->nimages - 1]);
  196. ctx->nimages++;
  197. }
  198. else
  199. {
  200. return -1;
  201. }
  202. return 0;
  203. }