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 OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25/**
26 * Types, macros, etc for the GLchan datatype.
27 * The swrast module is kind of hard-coded for 8bpp color channels but
28 * may be recompiled to use 16- or 32-bit color channels.  But that
29 * feature is seldom used and is likely broken in various ways.
30 */
31
32#ifndef U_CHAN_H
33#define U_CHAN_H
34
35
36#include "main/config.h"
37
38
39/**
40 * Default bits per color channel:  8, 16 or 32
41 */
42#ifndef CHAN_BITS
43#define CHAN_BITS 8
44#endif
45
46
47/**
48 * Color channel data type.
49 */
50#if CHAN_BITS == 8
51   typedef GLubyte GLchan;
52#define CHAN_MAX 255
53#define CHAN_MAXF 255.0F
54#define CHAN_TYPE GL_UNSIGNED_BYTE
55#elif CHAN_BITS == 16
56   typedef GLushort GLchan;
57#define CHAN_MAX 65535
58#define CHAN_MAXF 65535.0F
59#define CHAN_TYPE GL_UNSIGNED_SHORT
60#elif CHAN_BITS == 32
61   typedef GLfloat GLchan;
62#define CHAN_MAX 1.0
63#define CHAN_MAXF 1.0F
64#define CHAN_TYPE GL_FLOAT
65#else
66#error "illegal number of color channel bits"
67#endif
68
69
70#if CHAN_BITS == 8
71
72#define CHAN_TO_UBYTE(c)  (c)
73#define CHAN_TO_USHORT(c) (((c) << 8) | (c))
74#define CHAN_TO_SHORT(c)  (((c) << 7) | ((c) >> 1))
75#define CHAN_TO_FLOAT(c)  UBYTE_TO_FLOAT(c)
76
77#define CLAMPED_FLOAT_TO_CHAN(c, f)    CLAMPED_FLOAT_TO_UBYTE(c, f)
78#define UNCLAMPED_FLOAT_TO_CHAN(c, f)  UNCLAMPED_FLOAT_TO_UBYTE(c, f)
79
80#define COPY_CHAN4(DST, SRC)  COPY_4UBV(DST, SRC)
81
82#elif CHAN_BITS == 16
83
84#define CHAN_TO_UBYTE(c)  ((c) >> 8)
85#define CHAN_TO_USHORT(c) (c)
86#define CHAN_TO_SHORT(c)  ((c) >> 1)
87#define CHAN_TO_FLOAT(c)  ((GLfloat) ((c) * (1.0 / CHAN_MAXF)))
88
89#define CLAMPED_FLOAT_TO_CHAN(c, f)    CLAMPED_FLOAT_TO_USHORT(c, f)
90#define UNCLAMPED_FLOAT_TO_CHAN(c, f)  UNCLAMPED_FLOAT_TO_USHORT(c, f)
91
92#define COPY_CHAN4(DST, SRC)  COPY_4V(DST, SRC)
93
94#elif CHAN_BITS == 32
95
96#define CHAN_TO_UBYTE(c)  FLOAT_TO_UBYTE(c)
97#define CHAN_TO_USHORT(c) ((GLushort) (CLAMP((c), 0.0f, 1.0f) * 65535.0))
98#define CHAN_TO_SHORT(c)  ((GLshort) (CLAMP((c), 0.0f, 1.0f) * 32767.0))
99#define CHAN_TO_FLOAT(c)  (c)
100
101#define CLAMPED_FLOAT_TO_CHAN(c, f)  c = (f)
102#define UNCLAMPED_FLOAT_TO_CHAN(c, f)      c = (f)
103
104#define COPY_CHAN4(DST, SRC)  COPY_4V(DST, SRC)
105
106#else
107
108#error unexpected CHAN_BITS size
109
110#endif
111
112
113/**
114 * Convert 4 floats to GLchan values.
115 * \param dst pointer to destination GLchan[4] array.
116 * \param f pointer to source GLfloat[4] array.
117 */
118#define UNCLAMPED_FLOAT_TO_RGBA_CHAN(dst, f)	\
119do {						\
120   UNCLAMPED_FLOAT_TO_CHAN((dst)[0], (f)[0]);	\
121   UNCLAMPED_FLOAT_TO_CHAN((dst)[1], (f)[1]);	\
122   UNCLAMPED_FLOAT_TO_CHAN((dst)[2], (f)[2]);	\
123   UNCLAMPED_FLOAT_TO_CHAN((dst)[3], (f)[3]);	\
124} while (0)
125
126
127
128#endif /* U_CHAN_H */
129