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.
 
 
 
 
 
 

217 linhas
6.8 KiB

  1. /*
  2. * libcaca Ruby bindings
  3. * Copyright (c) 2007-2012 Pascal Terjan <pterjan@linuxfr.org>
  4. *
  5. * This library is free software. It comes without any warranty, to
  6. * the extent permitted by applicable law. You can redistribute it
  7. * and/or modify it under the terms of the Do What the Fuck You Want
  8. * to Public License, Version 2, as published by Sam Hocevar. See
  9. * http://www.wtfpl.net/ for more details.
  10. */
  11. #include <ruby.h>
  12. #include <caca.h>
  13. #include <errno.h>
  14. #include "common.h"
  15. VALUE cDither;
  16. void dither_free(void *dither)
  17. {
  18. caca_free_dither((caca_dither_t *)dither);
  19. }
  20. static const rb_data_type_t dither_dt = {
  21. .wrap_struct_name = "Caca::Dither",
  22. .function = {
  23. .dfree = dither_free,
  24. },
  25. };
  26. static VALUE dither_alloc(VALUE klass)
  27. {
  28. VALUE obj;
  29. obj = TypedData_Wrap_Struct(klass, &dither_dt, NULL);
  30. return obj;
  31. }
  32. static VALUE dither_initialize(VALUE self, VALUE bpp, VALUE w, VALUE h, VALUE pitch, VALUE rmask, VALUE gmask, VALUE bmask, VALUE amask)
  33. {
  34. caca_dither_t *dither;
  35. dither = caca_create_dither(NUM2UINT(bpp), NUM2UINT(w), NUM2UINT(h), NUM2UINT(pitch), NUM2ULONG(rmask), NUM2ULONG(gmask), NUM2ULONG(bmask), NUM2ULONG(amask));
  36. if(dither == NULL)
  37. {
  38. rb_raise(rb_eRuntimeError, "%s", strerror(errno));
  39. }
  40. _SELF = dither;
  41. return self;
  42. }
  43. static VALUE set_dither_palette(VALUE self, VALUE palette)
  44. {
  45. int i;
  46. unsigned int *red, *blue, *green, *alpha;
  47. VALUE v, r, g, b, a;
  48. int error = 0;
  49. if(RARRAY_LEN(palette) != 256)
  50. {
  51. rb_raise(rb_eArgError, "Palette must contain 256 elements");
  52. }
  53. red = ALLOC_N(unsigned int, 256);
  54. if(!red)
  55. rb_raise(rb_eNoMemError,"Out of memory");
  56. green = ALLOC_N(unsigned int, 256);
  57. if(!green)
  58. {
  59. free(red);
  60. rb_raise(rb_eNoMemError,"Out of memory");
  61. }
  62. blue = ALLOC_N(unsigned int, 256);
  63. if(!blue)
  64. {
  65. free(red);
  66. free(green);
  67. rb_raise(rb_eNoMemError,"Out of memory");
  68. }
  69. alpha = ALLOC_N(unsigned int, 256);
  70. if(!alpha)
  71. {
  72. free(red);
  73. free(green);
  74. free(blue);
  75. rb_raise(rb_eNoMemError,"Out of memory");
  76. }
  77. for(i=0; i<256; i++)
  78. {
  79. v = rb_ary_entry(palette, i);
  80. if((TYPE(v) == T_ARRAY) && (RARRAY_LEN(v) == 4))
  81. {
  82. r = rb_ary_entry(v,0);
  83. g = rb_ary_entry(v,1);
  84. b = rb_ary_entry(v,2);
  85. a = rb_ary_entry(v,3);
  86. if(rb_obj_is_kind_of(r, rb_cInteger) &&
  87. rb_obj_is_kind_of(g, rb_cInteger) &&
  88. rb_obj_is_kind_of(b, rb_cInteger) &&
  89. rb_obj_is_kind_of(a, rb_cInteger))
  90. {
  91. red[i] = NUM2INT(r);
  92. green[i] = NUM2INT(g);
  93. blue[i] = NUM2INT(b);
  94. alpha[i] = NUM2INT(a);
  95. } else
  96. error = 1;
  97. }
  98. else
  99. error = 1;
  100. }
  101. if(error)
  102. {
  103. free(red);
  104. free(green);
  105. free(blue);
  106. free(alpha);
  107. rb_raise(rb_eArgError, "Invalid palette");
  108. }
  109. if(caca_set_dither_palette(_SELF, red, green, blue, alpha)<0)
  110. {
  111. free(red);
  112. free(green);
  113. free(blue);
  114. free(alpha);
  115. rb_raise(rb_eRuntimeError, "%s", strerror(errno));
  116. }
  117. free(red);
  118. free(green);
  119. free(blue);
  120. free(alpha);
  121. return palette;
  122. }
  123. static VALUE set_dither_palette2(VALUE self, VALUE palette)
  124. {
  125. set_dither_palette(self, palette);
  126. return self;
  127. }
  128. #define set_float(x) \
  129. static VALUE set_##x(VALUE self, VALUE x) \
  130. { \
  131. if(caca_set_dither_##x(_SELF, (float)NUM2DBL(x))<0) \
  132. rb_raise(rb_eRuntimeError, "%s", strerror(errno)); \
  133. \
  134. return x; \
  135. } \
  136. \
  137. static VALUE set_##x##2(VALUE self, VALUE x) \
  138. { \
  139. set_##x(self, x); \
  140. return self; \
  141. }
  142. set_float(brightness)
  143. set_float(gamma)
  144. set_float(contrast)
  145. #define get_set_str_from_list(x) \
  146. get_double_list(dither_##x) \
  147. static VALUE set_dither_##x(VALUE self, VALUE x) \
  148. { \
  149. if(caca_set_dither_##x(_SELF, StringValuePtr(x))<0) \
  150. { \
  151. rb_raise(rb_eRuntimeError, "%s", strerror(errno)); \
  152. } \
  153. return x; \
  154. } \
  155. \
  156. static VALUE set_dither_##x##2(VALUE self, VALUE x) \
  157. { \
  158. set_dither_##x(self, x); \
  159. return self; \
  160. }
  161. get_set_str_from_list(antialias)
  162. get_set_str_from_list(color)
  163. get_set_str_from_list(charset)
  164. get_set_str_from_list(algorithm)
  165. void Init_caca_dither(VALUE mCaca)
  166. {
  167. cDither = rb_define_class_under(mCaca, "Dither", rb_cObject);
  168. rb_define_alloc_func(cDither, dither_alloc);
  169. rb_define_method(cDither, "initialize", dither_initialize, 8);
  170. rb_define_method(cDither, "palette=", set_dither_palette, 1);
  171. rb_define_method(cDither, "set_palette", set_dither_palette2, 1);
  172. rb_define_method(cDither, "brightness=", set_brightness, 1);
  173. rb_define_method(cDither, "set_brightness", set_brightness2, 1);
  174. rb_define_method(cDither, "gamma=", set_gamma, 1);
  175. rb_define_method(cDither, "set_gamma", set_gamma2, 1);
  176. rb_define_method(cDither, "contrast=", set_contrast, 1);
  177. rb_define_method(cDither, "set_contrast", set_contrast2, 1);
  178. rb_define_method(cDither, "antialias_list", dither_antialias_list, 0);
  179. rb_define_method(cDither, "antialias=", set_dither_antialias, 1);
  180. rb_define_method(cDither, "set_antialias", set_dither_antialias2, 1);
  181. rb_define_method(cDither, "color_list", dither_color_list, 0);
  182. rb_define_method(cDither, "color=", set_dither_color, 1);
  183. rb_define_method(cDither, "set_color", set_dither_color2, 1);
  184. rb_define_method(cDither, "charset_list", dither_charset_list, 0);
  185. rb_define_method(cDither, "charset=", set_dither_charset, 1);
  186. rb_define_method(cDither, "set_charset", set_dither_charset2, 1);
  187. rb_define_method(cDither, "algorithm_list", dither_algorithm_list, 0);
  188. rb_define_method(cDither, "algorithm=", set_dither_algorithm, 1);
  189. rb_define_method(cDither, "set_algorithm", set_dither_algorithm2, 1);
  190. }