1/*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2011  VMware, Inc.  All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24/**
25 * Types, macros, etc for the GLchan datatype.
26 * The swrast module is kind of hard-coded for 8bpp color channels but
27 * may be recompiled to use 16- or 32-bit color channels.  But that
28 * feature is seldom used and is likely broken in various ways.
29 */
30
31#ifndef U_CHAN_H
32#define U_CHAN_H
33
34
35#include "main/config.h"
36
37
38/**
39 * Default bits per color channel:  8, 16 or 32
40 */
41#ifndef CHAN_BITS
42#define CHAN_BITS 8
43#endif
44
45
46/**
47 * Color channel data type.
48 */
49#if CHAN_BITS == 8
50   typedef GLubyte GLchan;
51#define CHAN_MAX 255
52#define CHAN_MAXF 255.0F
53#define CHAN_TYPE GL_UNSIGNED_BYTE
54#elif CHAN_BITS == 16
55   typedef GLushort GLchan;
56#define CHAN_MAX 65535
57#define CHAN_MAXF 65535.0F
58#define CHAN_TYPE GL_UNSIGNED_SHORT
59#elif CHAN_BITS == 32
60   typedef GLfloat GLchan;
61#define CHAN_MAX 1.0
62#define CHAN_MAXF 1.0F
63#define CHAN_TYPE GL_FLOAT
64#else
65#error "illegal number of color channel bits"
66#endif
67
68
69#if CHAN_BITS == 8
70
71#define CHAN_TO_UBYTE(c)  (c)
72#define CHAN_TO_USHORT(c) (((c) << 8) | (c))
73#define CHAN_TO_SHORT(c)  (((c) << 7) | ((c) >> 1))
74#define CHAN_TO_FLOAT(c)  UBYTE_TO_FLOAT(c)
75
76#define CLAMPED_FLOAT_TO_CHAN(c, f)    CLAMPED_FLOAT_TO_UBYTE(c, f)
77#define UNCLAMPED_FLOAT_TO_CHAN(c, f)  UNCLAMPED_FLOAT_TO_UBYTE(c, f)
78
79#define COPY_CHAN4(DST, SRC)  COPY_4UBV(DST, SRC)
80
81#elif CHAN_BITS == 16
82
83#define CHAN_TO_UBYTE(c)  ((c) >> 8)
84#define CHAN_TO_USHORT(c) (c)
85#define CHAN_TO_SHORT(c)  ((c) >> 1)
86#define CHAN_TO_FLOAT(c)  ((GLfloat) ((c) * (1.0 / CHAN_MAXF)))
87
88#define CLAMPED_FLOAT_TO_CHAN(c, f)    CLAMPED_FLOAT_TO_USHORT(c, f)
89#define UNCLAMPED_FLOAT_TO_CHAN(c, f)  UNCLAMPED_FLOAT_TO_USHORT(c, f)
90
91#define COPY_CHAN4(DST, SRC)  COPY_4V(DST, SRC)
92
93#elif CHAN_BITS == 32
94
95#define CHAN_TO_UBYTE(c)  FLOAT_TO_UBYTE(c)
96#define CHAN_TO_USHORT(c) ((GLushort) (CLAMP((c), 0.0f, 1.0f) * 65535.0))
97#define CHAN_TO_SHORT(c)  ((GLshort) (CLAMP((c), 0.0f, 1.0f) * 32767.0))
98#define CHAN_TO_FLOAT(c)  (c)
99
100#define CLAMPED_FLOAT_TO_CHAN(c, f)  c = (f)
101#define UNCLAMPED_FLOAT_TO_CHAN(c, f)      c = (f)
102
103#define COPY_CHAN4(DST, SRC)  COPY_4V(DST, SRC)
104
105#else
106
107#error unexpected CHAN_BITS size
108
109#endif
110
111
112/**
113 * Convert 4 floats to GLchan values.
114 * \param dst pointer to destination GLchan[4] array.
115 * \param f pointer to source GLfloat[4] array.
116 */
117#define UNCLAMPED_FLOAT_TO_RGBA_CHAN(dst, f)	\
118do {						\
119   UNCLAMPED_FLOAT_TO_CHAN((dst)[0], (f)[0]);	\
120   UNCLAMPED_FLOAT_TO_CHAN((dst)[1], (f)[1]);	\
121   UNCLAMPED_FLOAT_TO_CHAN((dst)[2], (f)[2]);	\
122   UNCLAMPED_FLOAT_TO_CHAN((dst)[3], (f)[3]);	\
123} while (0)
124
125
126
127#endif /* U_CHAN_H */
128