File: brightonBitmaps.c

package info (click to toggle)
bristol 0.60.10-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 15,652 kB
  • sloc: ansic: 124,457; sh: 10,579; makefile: 111
file content (182 lines) | stat: -rw-r--r-- 4,411 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182

/*
 *  Diverse Bristol audio routines.
 *  Copyright (c) by Nick Copeland <nickycopeland@hotmail.com> 1996,2012
 *
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <stdlib.h>
#include <string.h>

#include "brightoninternals.h"

extern brightonBitmap *xpmread(brightonWindow *, char *);

char *brightonhome = 0;

brightonBitmap *
brightonCreateBitmap(brightonWindow *bwin, int width, int height)
{
	brightonBitmap *bitmap;

	bitmap = brightonmalloc((size_t) sizeof(struct BrightonBitmap));

	bitmap->width = width;
	bitmap->height = height;
	bitmap->pixels = (int *) brightonmalloc((2 + width) * (2 + height)
		* sizeof(int));
	bitmap->ncolors = 0;
	bitmap->ctabsize = BRIGHTON_QR_COLORS;
	bitmap->colormap = (int *) brightonmalloc(BRIGHTON_QR_COLORS * sizeof(int));
	bitmap->uses = 1;

	/*
	 * Link it in to our window.
	 */
	if (bwin->bitmaps != NULL)
		bwin->bitmaps->last = bitmap;
	bitmap->next = bwin->bitmaps;
	bwin->bitmaps = bitmap;

	return(bitmap);
}

brightonBitmap *
brightonCreateNamedBitmap(brightonWindow *bwin, int width, int height, char *name)
{
	brightonBitmap *bitmap;

	bitmap = brightonCreateBitmap(bwin, bwin->width, bwin->height);

	bitmap->name = brightonmalloc(strlen(name) + 1);
	sprintf(bitmap->name, "%s", name);

	return(bitmap);
}

brightonBitmap *
brightonReadImage(brightonWindow *bwin, char *filename)
{
	char *extension, file[256];
	brightonBitmap *bitmap = bwin->bitmaps;

	/* printf("brightonReadImage(%x, %s)\n", bwin, filename); */

	if (filename == 0)
		return(0);

	if (filename[0] == '/')
		sprintf(file, "%s", filename);
	else {
		if (brightonhome == 0)
	 		brightonhome = getenv("BRIGHTON");

		sprintf(file, "%s/%s", brightonhome, filename);
	}

	/*
	 * See if we already have this image loaded on this screen. We cannot have
	 * global bitmap assignments since if we are working on different screens
	 * then we might not have the full set of GCs to render this image.
	 */
	while (bitmap != 0)
	{
		if ((bitmap->name != 0) && (strcmp(file, bitmap->name) == 0))
		{
			bitmap->uses++;
			/* printf("ReUse Bitmap: %s %i\n", bitmap->name, bitmap->uses); */
			return(bitmap);
		}
		bitmap = bitmap->next;
	}
	/*
	 * See if we have this already loaded. If so, pass back a handle, otherwise
	 * determine the type from the name and pass the brightonBitmap back to 
	 * the caller.
	 */
	if ((extension = rindex(file, (int) '.')) == 0)
		return(0);

	if (strcmp(".xpm", extension) == 0)
		return(xpmread(bwin, file));

	/*
	 * Add in any other handlers for gif/bmp, etc.
	 */
	return(0);
}

brightonBitmap *
brightonFreeBitmap(brightonWindow *bwin, brightonBitmap *bitmap)
{
	brightonBitmap *bitmaplist = bwin->bitmaps, *next = NULL;

	if (bitmap == 0)
		return(0);

	/* printf("brightonFreeBitmap(%x)\n", bitmap->name); */

	while (bitmaplist != 0)
	{
		if (bitmap == bitmaplist)
		{
			int i;

			if (--bitmap->uses <= 0)
			{
				/*printf("freeing %x (%s)\n", bitmaplist, bitmaplist->name); */
				/*
				 * Unlink the bitmap
				 */
				if (bitmaplist->next)
					bitmaplist->next->last = bitmaplist->last;
				if (bitmaplist->last)
					bitmaplist->last->next = bitmaplist->next;
				else
					bwin->bitmaps = bitmaplist->next;

				if (bitmaplist->colormap)
					for (i = 0; i < bitmaplist->ncolors;i++)
					{
						brightonFreeGC(bwin, bitmaplist->colormap[i]);
					}

				if (bitmaplist->colormap)
					brightonfree(bitmaplist->colormap);
				if (bitmaplist->pixels)
					brightonfree(bitmaplist->pixels);
				if (bitmaplist->name)
					brightonfree(bitmaplist->name);

				next = bitmaplist->next;

				brightonfree(bitmaplist);
			}
#ifdef DEBUG
			else {
				printf("bitmap %s has uses %i\n", bitmaplist->name,
					bitmaplist->uses);
			}
#endif
			return(next);
		}
		bitmaplist = bitmaplist->next;
	}

	return(0);
}