File: visual.c

package info (click to toggle)
libggi 1%3A2.2.2-5
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 10,752 kB
  • ctags: 9,218
  • sloc: ansic: 65,689; sh: 9,211; makefile: 1,226; pascal: 183
file content (205 lines) | stat: -rw-r--r-- 6,030 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/* $Id: visual.c,v 1.9 2005/07/30 11:39:56 cegger Exp $
******************************************************************************

   Generic color handling library

   Copyright (C) 1998 Andrew Apted  [andrew@ggi-project.org]

   Permission is hereby granted, free of charge, to any person obtaining a
   copy of this software and associated documentation files (the "Software"),
   to deal in the Software without restriction, including without limitation
   the rights to use, copy, modify, merge, publish, distribute, sublicense,
   and/or sell copies of the Software, and to permit persons to whom the
   Software is furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included in
   all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
   THE AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
   IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

******************************************************************************
*/

#include <stdlib.h>

#include "color.h"


static int calc_total(ggi_pixel mask)
{
	int total;

	for (total=0; mask != 0; mask >>= 1, total++) {
	}

	return total;
}


static int calc_nbits(ggi_pixel mask)
{
	int nbits;

	while (!(mask & 0x0001)) mask >>= 1;

	for (nbits=0; mask != 0; mask >>= 1, nbits++) {
	}

	return nbits;
}


static void do_setup_color_info(ggi_visual *vis)
{
	if (GT_SCHEME(LIBGGI_GT(vis)) == GT_TRUECOLOR) {
		color_truepriv *priv = vis->colorpriv;
		int redtot   = calc_total(LIBGGI_PIXFMT(vis)->red_mask);
		int greentot = calc_total(LIBGGI_PIXFMT(vis)->green_mask);
		int bluetot  = calc_total(LIBGGI_PIXFMT(vis)->blue_mask);

		priv->red_map     = redtot - 16;
		priv->red_unmap   = 16 - redtot;
		priv->red_mask    = LIBGGI_PIXFMT(vis)->red_mask;
		priv->red_nbits     = calc_nbits(priv->red_mask);
		priv->green_map   = greentot - 16;
		priv->green_unmap = 16 - greentot;
		priv->green_mask  = LIBGGI_PIXFMT(vis)->green_mask;
		priv->green_nbits   = calc_nbits(priv->green_mask);
		priv->blue_map    = bluetot - 16;
		priv->blue_unmap  = 16 - bluetot;
		priv->blue_mask   = LIBGGI_PIXFMT(vis)->blue_mask;
		priv->blue_nbits    = calc_nbits(priv->blue_mask);
	} else if (GT_SCHEME(LIBGGI_GT(vis)) == GT_PALETTE ||
		   GT_SCHEME(LIBGGI_GT(vis)) == GT_STATIC_PALETTE) {
		color_palpriv *priv = vis->colorpriv;

		priv->numcols = 1 << GT_DEPTH(LIBGGI_GT(vis));
		/* prev_col doesn't need to be initialized */
		priv->prev_val = 0;
	} else if (GT_SCHEME(LIBGGI_GT(vis)) == GT_GREYSCALE) {
		color_greypriv *priv = vis->colorpriv;

		priv->shift = 24 - GT_DEPTH(LIBGGI_GT(vis));
	}
}


static int GGIopen(ggi_visual *vis, struct ggi_dlhandle *dlh,
			const char *args, void *argptr, uint32_t *dlret)
{
	vis->colorpriv = malloc(sizeof(color_priv));
	if (vis->colorpriv == NULL) return GGI_ENOMEM;

	do_setup_color_info(vis);
	
	/* Color mapping
	 */
	switch (GT_SCHEME(LIBGGI_GT(vis))) {
		case GT_PALETTE:
		case GT_STATIC_PALETTE:
			vis->opcolor->mapcolor   = GGI_color_PAL_mapcolor;
			vis->opcolor->unmappixel = GGI_color_PAL_unmappixel;
			vis->opcolor->getpalvec  = GGI_color_getpalvec;
			vis->opcolor->setpalvec  = GGI_color_setpalvec;
			break;

		case GT_TRUECOLOR:
			vis->opcolor->mapcolor   = GGI_color_TRUE_mapcolor;
			if (COLOR_TRUEPRIV(vis)->red_nbits >= 8 &&
			    COLOR_TRUEPRIV(vis)->green_nbits >= 8 &&
			    COLOR_TRUEPRIV(vis)->blue_nbits >= 8)
				vis->opcolor->unmappixel = 
					GGI_color_TRUE_unmappixel_gte8;
			else if (COLOR_TRUEPRIV(vis)->red_nbits >= 4 &&
				 COLOR_TRUEPRIV(vis)->green_nbits >= 4 &&
				 COLOR_TRUEPRIV(vis)->blue_nbits >= 4)
				vis->opcolor->unmappixel = 
					GGI_color_TRUE_unmappixel_gte4;
			else if (COLOR_TRUEPRIV(vis)->red_nbits >= 2 &&
				 COLOR_TRUEPRIV(vis)->green_nbits >= 2 &&
				 COLOR_TRUEPRIV(vis)->blue_nbits >= 2)
					vis->opcolor->unmappixel = 
						GGI_color_TRUE_unmappixel_gte2;
			else vis->opcolor->unmappixel = 
				GGI_color_TRUE_unmappixel_gte1;
			if (GT_SIZE(LIBGGI_GT(vis)) == 16) {
			  vis->opcolor->mapcolor   = GGI_color_TRUE16_mapcolor;
			  if (vis->opcolor->unmappixel == 
			      GGI_color_TRUE_unmappixel_gte4)
			  	vis->opcolor->unmappixel = 
					GGI_color_TRUE16_unmappixel_4to7;
			}

			break;

		case GT_GREYSCALE:
			vis->opcolor->mapcolor   = GGI_color_GREY_mapcolor;
			vis->opcolor->unmappixel = GGI_color_GREY_unmappixel;
			break;
	}

	if (! (GT_SUBSCHEME(LIBGGI_GT(vis)) & GT_SUB_PACKED_GETPUT)) {
		switch (GT_ByPP(LIBGGI_GT(vis))) {
		case 1: vis->opcolor->packcolors   = GGI_color_L1_packcolors;
			vis->opcolor->unpackpixels = GGI_color_L1_unpackpixels;
			break;

		case 2: vis->opcolor->packcolors   = GGI_color_L2_packcolors;
			vis->opcolor->unpackpixels = GGI_color_L2_unpackpixels;
			break;

		case 3: vis->opcolor->packcolors   = GGI_color_L3_packcolors;
			vis->opcolor->unpackpixels = GGI_color_L3_unpackpixels;
			break;

		case 4: vis->opcolor->packcolors   = GGI_color_L4_packcolors;
			vis->opcolor->unpackpixels = GGI_color_L4_unpackpixels;
			break;
		}
	}

	/* Gamma mapping 
	 */
	vis->opcolor->getgamma = GGI_color_getgamma;	
	vis->opcolor->setgamma = GGI_color_setgamma;	

	*dlret = GGI_DL_OPCOLOR;
	return 0;
}

static int GGIclose(ggi_visual *vis, struct ggi_dlhandle *dlh)
{
	free(vis->colorpriv);

	return 0;
}


EXPORTFUNC
int GGIdl_color(int func, void **funcptr);

int GGIdl_color(int func, void **funcptr)
{
	switch (func) {
	case GGIFUNC_open:
		*funcptr = (void *)GGIopen;
		return 0;
	case GGIFUNC_exit:
		*funcptr = NULL;
		return 0;
	case GGIFUNC_close:
		*funcptr = (void *)GGIclose;
		return 0;
	default:
		*funcptr = NULL;
	}

	return GGI_ENOTFOUND;
}

#include <ggi/internal/ggidlinit.h>