]> git.donarmstrong.com Git - lilypond.git/blob - guile18/doc/example-smob/image-type.c
New upstream version 2.19.65
[lilypond.git] / guile18 / doc / example-smob / image-type.c
1 /* image-type.c
2  * 
3  *      Copyright (C) 1998, 2000, 2004, 2006 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., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301 USA
19  */
20
21 #include <stdlib.h>
22 #include <libguile.h>
23
24 static scm_t_bits image_tag;
25
26 struct image {
27   int width, height;
28   char *pixels;
29
30   /* The name of this image */
31   SCM name;
32
33   /* A function to call when this image is
34      modified, e.g., to update the screen,
35      or SCM_BOOL_F if no action necessary */
36   SCM update_func;
37 };
38
39 static SCM
40 make_image (SCM name, SCM s_width, SCM s_height)
41 {
42   SCM smob;
43   struct image *image;
44   int width = scm_to_int (s_width);
45   int height = scm_to_int (s_height);
46
47   /* Step 1: Allocate the memory block.
48    */
49   image = (struct image *) scm_gc_malloc (sizeof (struct image), "image");
50
51   /* Step 2: Initialize it with straight code.
52    */
53   image->width = width;
54   image->height = height;
55   image->pixels = NULL;
56   image->name = SCM_BOOL_F;
57   image->update_func = SCM_BOOL_F;
58
59   /* Step 3: Create the smob.
60    */
61   SCM_NEWSMOB (smob, image_tag, image);
62
63   /* Step 4: Finish the initialization.
64    */
65   image->name = name;
66   image->pixels = scm_gc_malloc (width * height, "image pixels");
67
68   return smob;
69 }
70
71 SCM
72 clear_image (SCM image_smob)
73 {
74   int area;
75   struct image *image;
76
77   scm_assert_smob_type (image_tag, image_smob);
78
79   image = (struct image *) SCM_SMOB_DATA (image_smob);
80   area = image->width * image->height;
81   memset (image->pixels, 0, area);
82
83   /* Invoke the image's update function.
84    */
85   if (scm_is_true (image->update_func))
86     scm_call_0 (image->update_func);
87
88   scm_remember_upto_here_1 (image_smob);
89
90   return SCM_UNSPECIFIED;
91 }
92
93 static SCM
94 mark_image (SCM image_smob)
95 {
96   /* Mark the image's name and update function.  */
97   struct image *image = (struct image *) SCM_SMOB_DATA (image_smob);
98
99   scm_gc_mark (image->name);
100   return image->update_func;
101 }
102
103 static size_t
104 free_image (SCM image_smob)
105 {
106   struct image *image = (struct image *) SCM_SMOB_DATA (image_smob);
107
108   scm_gc_free (image->pixels, image->width * image->height, "image pixels");
109   scm_gc_free (image, sizeof (struct image), "image");
110
111   return 0;
112 }
113
114 static int
115 print_image (SCM image_smob, SCM port, scm_print_state *pstate)
116 {
117   struct image *image = (struct image *) SCM_SMOB_DATA (image_smob);
118
119   scm_puts ("#<image ", port);
120   scm_display (image->name, port);
121   scm_puts (">", port);
122
123   /* non-zero means success */
124   return 1;
125 }
126
127 void
128 init_image_type (void)
129 {
130   image_tag = scm_make_smob_type ("image", sizeof (struct image));
131   scm_set_smob_mark (image_tag, mark_image);
132   scm_set_smob_free (image_tag, free_image);
133   scm_set_smob_print (image_tag, print_image);
134
135   scm_c_define_gsubr ("clear-image", 1, 0, 0, clear_image);
136   scm_c_define_gsubr ("make-image", 3, 0, 0, make_image);
137 }