image-type.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* image-type.c
  2. *
  3. * Copyright (C) 1998, 2000, 2004, 2006, 2011 Free Software Foundation, Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public License
  7. * as published by the Free Software Foundation; either version 3, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this software; see the file COPYING.LESSER. If
  17. * not, write to the Free Software Foundation, Inc., 51 Franklin
  18. * Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <stdlib.h>
  21. #include <libguile.h>
  22. static scm_t_bits image_tag;
  23. struct image
  24. {
  25. int width, height;
  26. char *pixels;
  27. /* The name of this image */
  28. SCM name;
  29. /* A function to call when this image is
  30. modified, e.g., to update the screen,
  31. or SCM_BOOL_F if no action necessary */
  32. SCM update_func;
  33. };
  34. static SCM
  35. make_image (SCM name, SCM s_width, SCM s_height)
  36. {
  37. SCM smob;
  38. struct image *image;
  39. int width = scm_to_int (s_width);
  40. int height = scm_to_int (s_height);
  41. /* Step 1: Allocate the memory block.
  42. */
  43. image = (struct image *) scm_gc_malloc (sizeof (struct image), "image");
  44. /* Step 2: Initialize it with straight code.
  45. */
  46. image->width = width;
  47. image->height = height;
  48. image->pixels = NULL;
  49. image->name = SCM_BOOL_F;
  50. image->update_func = SCM_BOOL_F;
  51. /* Step 3: Create the smob.
  52. */
  53. SCM_NEWSMOB (smob, image_tag, image);
  54. /* Step 4: Finish the initialization.
  55. */
  56. image->name = name;
  57. image->pixels = scm_gc_malloc_pointerless (width * height, "image pixels");
  58. return smob;
  59. }
  60. SCM
  61. clear_image (SCM image_smob)
  62. {
  63. int area;
  64. struct image *image;
  65. scm_assert_smob_type (image_tag, image_smob);
  66. image = (struct image *) SCM_SMOB_DATA (image_smob);
  67. area = image->width * image->height;
  68. memset (image->pixels, 0, area);
  69. /* Invoke the image's update function.
  70. */
  71. if (scm_is_true (image->update_func))
  72. scm_call_0 (image->update_func);
  73. scm_remember_upto_here_1 (image_smob);
  74. return SCM_UNSPECIFIED;
  75. }
  76. static int
  77. print_image (SCM image_smob, SCM port, scm_print_state *pstate)
  78. {
  79. struct image *image = (struct image *) SCM_SMOB_DATA (image_smob);
  80. scm_puts ("#<image ", port);
  81. scm_display (image->name, port);
  82. scm_puts (">", port);
  83. /* non-zero means success */
  84. return 1;
  85. }
  86. void
  87. init_image_type (void)
  88. {
  89. image_tag = scm_make_smob_type ("image", sizeof (struct image));
  90. scm_set_smob_print (image_tag, print_image);
  91. scm_c_define_gsubr ("clear-image", 1, 0, 0, clear_image);
  92. scm_c_define_gsubr ("make-image", 3, 0, 0, make_image);
  93. }