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