image-type.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* image-type.c
  2. *
  3. * Copyright (C) 1998, 2000 Free Software Foundation, Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2, or (at your option)
  8. * any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this software; see the file COPYING. If not, write to
  17. * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  18. * Boston, MA 02111-1307 USA
  19. */
  20. #include <stdlib.h>
  21. #include <libguile.h>
  22. static long image_tag;
  23. struct image {
  24. int width, height;
  25. char *pixels;
  26. /* The name of this image */
  27. SCM name;
  28. /* A function to call when this image is
  29. modified, e.g., to update the screen,
  30. or SCM_BOOL_F if no action necessary */
  31. SCM update_func;
  32. };
  33. static SCM
  34. make_image (SCM name, SCM s_width, SCM s_height)
  35. {
  36. struct image *image;
  37. int width, height;
  38. SCM_ASSERT (SCM_NIMP (name) && SCM_STRINGP (name), name,
  39. SCM_ARG1, "make-image");
  40. SCM_ASSERT (SCM_INUMP (s_width), s_width, SCM_ARG2, "make-image");
  41. SCM_ASSERT (SCM_INUMP (s_height), s_height, SCM_ARG3, "make-image");
  42. width = SCM_INUM (s_width);
  43. height = SCM_INUM (s_height);
  44. image = (struct image *) scm_must_malloc (sizeof (struct image), "image");
  45. image->width = width;
  46. image->height = height;
  47. image->pixels = scm_must_malloc (width * height, "image pixels");
  48. image->name = name;
  49. image->update_func = SCM_BOOL_F;
  50. SCM_RETURN_NEWSMOB (image_tag, image);
  51. }
  52. static SCM
  53. clear_image (SCM image_smob)
  54. {
  55. int area;
  56. struct image *image;
  57. SCM_ASSERT ((SCM_NIMP (image_smob)
  58. && SCM_CAR (image_smob) == image_tag),
  59. image_smob, SCM_ARG1, "clear-image");
  60. image = (struct image *) SCM_CDR (image_smob);
  61. area = image->width * image->height;
  62. memset (image->pixels, 0, area);
  63. /* Invoke the image's update function. */
  64. if (image->update_func != SCM_BOOL_F)
  65. scm_apply (image->update_func, SCM_EOL, SCM_EOL);
  66. return SCM_UNSPECIFIED;
  67. }
  68. static SCM
  69. mark_image (SCM image_smob)
  70. {
  71. struct image *image = (struct image *) SCM_CDR (image_smob);
  72. scm_gc_mark (image->name);
  73. return image->update_func;
  74. }
  75. static scm_sizet
  76. free_image (SCM image_smob)
  77. {
  78. struct image *image = (struct image *) SCM_CDR (image_smob);
  79. scm_sizet size = image->width * image->height + sizeof (struct image);
  80. free (image->pixels);
  81. free (image);
  82. return size;
  83. }
  84. static int
  85. print_image (SCM image_smob, SCM port, scm_print_state *pstate)
  86. {
  87. struct image *image = (struct image *) SCM_CDR (image_smob);
  88. scm_puts ("#<image ", port);
  89. scm_display (image->name, port);
  90. scm_puts (">", port);
  91. /* non-zero means success */
  92. return 1;
  93. }
  94. void
  95. init_image_type ()
  96. {
  97. image_tag = scm_make_smob_type ("image", sizeof (struct image));
  98. scm_set_smob_mark (image_tag, mark_image);
  99. scm_set_smob_free (image_tag, free_image);
  100. scm_set_smob_print (image_tag, print_image);
  101. scm_make_gsubr ("clear-image", 1, 0, 0, clear_image);
  102. scm_make_gsubr ("make-image", 3, 0, 0, make_image);
  103. }