1 
2 /*
3  *  Diverse Bristol audio routines.
4  *  Copyright (c) by Nick Copeland <nickycopeland@hotmail.com> 1996,2012
5  *
6  *
7  *   This program is free software; you can redistribute it and/or modify
8  *   it under the terms of the GNU General Public License as published by
9  *   the Free Software Foundation; either version 3 of the License, or
10  *   (at your option) any later version.
11  *
12  *   This program is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with this program; if not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #include <stdlib.h>
23 #include <string.h>
24 
25 #include "brightoninternals.h"
26 
27 extern brightonBitmap *xpmread(brightonWindow *, char *);
28 
29 char *brightonhome = 0;
30 
31 brightonBitmap *
brightonCreateBitmap(brightonWindow * bwin,int width,int height)32 brightonCreateBitmap(brightonWindow *bwin, int width, int height)
33 {
34 	brightonBitmap *bitmap;
35 
36 	bitmap = brightonmalloc((size_t) sizeof(struct BrightonBitmap));
37 
38 	bitmap->width = width;
39 	bitmap->height = height;
40 	bitmap->pixels = (int *) brightonmalloc((2 + width) * (2 + height)
41 		* sizeof(int));
42 	bitmap->ncolors = 0;
43 	bitmap->ctabsize = BRIGHTON_QR_COLORS;
44 	bitmap->colormap = (int *) brightonmalloc(BRIGHTON_QR_COLORS * sizeof(int));
45 	bitmap->uses = 1;
46 
47 	/*
48 	 * Link it in to our window.
49 	 */
50 	if (bwin->bitmaps != NULL)
51 		bwin->bitmaps->last = bitmap;
52 	bitmap->next = bwin->bitmaps;
53 	bwin->bitmaps = bitmap;
54 
55 	return(bitmap);
56 }
57 
58 brightonBitmap *
brightonCreateNamedBitmap(brightonWindow * bwin,int width,int height,char * name)59 brightonCreateNamedBitmap(brightonWindow *bwin, int width, int height, char *name)
60 {
61 	brightonBitmap *bitmap;
62 
63 	bitmap = brightonCreateBitmap(bwin, bwin->width, bwin->height);
64 
65 	bitmap->name = brightonmalloc(strlen(name) + 1);
66 	sprintf(bitmap->name, "%s", name);
67 
68 	return(bitmap);
69 }
70 
71 brightonBitmap *
brightonReadImage(brightonWindow * bwin,char * filename)72 brightonReadImage(brightonWindow *bwin, char *filename)
73 {
74 	char *extension, file[256];
75 	brightonBitmap *bitmap = bwin->bitmaps;
76 
77 	/* printf("brightonReadImage(%x, %s)\n", bwin, filename); */
78 
79 	if (filename == 0)
80 		return(0);
81 
82 	if (filename[0] == '/')
83 		sprintf(file, "%s", filename);
84 	else {
85 		if (brightonhome == 0)
86 	 		brightonhome = getenv("BRIGHTON");
87 
88 		sprintf(file, "%s/%s", brightonhome, filename);
89 	}
90 
91 	/*
92 	 * See if we already have this image loaded on this screen. We cannot have
93 	 * global bitmap assignments since if we are working on different screens
94 	 * then we might not have the full set of GCs to render this image.
95 	 */
96 	while (bitmap != 0)
97 	{
98 		if ((bitmap->name != 0) && (strcmp(file, bitmap->name) == 0))
99 		{
100 			bitmap->uses++;
101 			/* printf("ReUse Bitmap: %s %i\n", bitmap->name, bitmap->uses); */
102 			return(bitmap);
103 		}
104 		bitmap = bitmap->next;
105 	}
106 	/*
107 	 * See if we have this already loaded. If so, pass back a handle, otherwise
108 	 * determine the type from the name and pass the brightonBitmap back to
109 	 * the caller.
110 	 */
111 	if ((extension = rindex(file, (int) '.')) == 0)
112 		return(0);
113 
114 	if (strcmp(".xpm", extension) == 0)
115 		return(xpmread(bwin, file));
116 
117 	/*
118 	 * Add in any other handlers for gif/bmp, etc.
119 	 */
120 	return(0);
121 }
122 
123 brightonBitmap *
brightonFreeBitmap(brightonWindow * bwin,brightonBitmap * bitmap)124 brightonFreeBitmap(brightonWindow *bwin, brightonBitmap *bitmap)
125 {
126 	brightonBitmap *bitmaplist = bwin->bitmaps, *next = NULL;
127 
128 	if (bitmap == 0)
129 		return(0);
130 
131 	/* printf("brightonFreeBitmap(%x)\n", bitmap->name); */
132 
133 	while (bitmaplist != 0)
134 	{
135 		if (bitmap == bitmaplist)
136 		{
137 			int i;
138 
139 			if (--bitmap->uses <= 0)
140 			{
141 				/*printf("freeing %x (%s)\n", bitmaplist, bitmaplist->name); */
142 				/*
143 				 * Unlink the bitmap
144 				 */
145 				if (bitmaplist->next)
146 					bitmaplist->next->last = bitmaplist->last;
147 				if (bitmaplist->last)
148 					bitmaplist->last->next = bitmaplist->next;
149 				else
150 					bwin->bitmaps = bitmaplist->next;
151 
152 				if (bitmaplist->colormap)
153 					for (i = 0; i < bitmaplist->ncolors;i++)
154 					{
155 						brightonFreeGC(bwin, bitmaplist->colormap[i]);
156 					}
157 
158 				if (bitmaplist->colormap)
159 					brightonfree(bitmaplist->colormap);
160 				if (bitmaplist->pixels)
161 					brightonfree(bitmaplist->pixels);
162 				if (bitmaplist->name)
163 					brightonfree(bitmaplist->name);
164 
165 				next = bitmaplist->next;
166 
167 				brightonfree(bitmaplist);
168 			}
169 #ifdef DEBUG
170 			else {
171 				printf("bitmap %s has uses %i\n", bitmaplist->name,
172 					bitmaplist->uses);
173 			}
174 #endif
175 			return(next);
176 		}
177 		bitmaplist = bitmaplist->next;
178 	}
179 
180 	return(0);
181 }
182 
183