Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

192 řádky
5.0 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, "wrap"))
  136. {
  137. if(ctx->nimages <= 0)
  138. return -1;
  139. ctx->images[ctx->nimages - 1]->wrap = 1;
  140. }
  141. else if(!strcmp(cmd, "autocontrast"))
  142. {
  143. pipi_image_t *tmp;
  144. if(ctx->nimages <= 0)
  145. return -1;
  146. tmp = ctx->images[ctx->nimages - 1];
  147. ctx->images[ctx->nimages - 1] = pipi_autocontrast(tmp);
  148. pipi_free(tmp);
  149. }
  150. else if(!strcmp(cmd, "gray"))
  151. {
  152. if(ctx->nimages <= 0)
  153. return -1;
  154. pipi_getpixels(ctx->images[ctx->nimages - 1], PIPI_PIXELS_Y_F);
  155. }
  156. else if(!strcmp(cmd, "free"))
  157. {
  158. if(ctx->nimages <= 0)
  159. return -1;
  160. ctx->nimages--;
  161. pipi_free(ctx->images[ctx->nimages]);
  162. }
  163. else if(!strcmp(cmd, "dup"))
  164. {
  165. if(ctx->nimages <= 0)
  166. return -1;
  167. ctx->images[ctx->nimages] = pipi_copy(ctx->images[ctx->nimages - 1]);
  168. ctx->nimages++;
  169. }
  170. else
  171. {
  172. return -1;
  173. }
  174. return 0;
  175. }