summaryrefslogtreecommitdiff
path: root/src/glitz_util.c
blob: 2e661edbe46f57afe865fc7affcd9808edec75a3 (plain)
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
/*
 * Copyright © 2004 David Reveman, Peter Nilsson
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without
 * fee, provided that the above copyright notice appear in all copies
 * and that both that copyright notice and this permission notice
 * appear in supporting documentation, and that the names of
 * David Reveman and Peter Nilsson not be used in advertising or
 * publicity pertaining to distribution of the software without
 * specific, written prior permission. David Reveman and Peter Nilsson
 * makes no representations about the suitability of this software for
 * any purpose. It is provided "as is" without express or implied warranty.
 *
 * DAVID REVEMAN AND PETER NILSSON DISCLAIMS ALL WARRANTIES WITH
 * REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL DAVID REVEMAN AND
 * PETER NILSSON BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 *
 * Authors: David Reveman <c99drn@cs.umu.se>
 *          Peter Nilsson <c99pnn@cs.umu.se>
 */

#ifdef HAVE_CONFIG_H
#  include "../config.h"
#endif

#include "glitzint.h"

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

void
glitz_intersect_region (glitz_region_box_t *box1,
                        glitz_region_box_t *box2,
                        glitz_region_box_t *return_box)
{
  return_box->x1 = (box1->x1 >= box2->x1)? box1->x1: box2->x1;
  return_box->x2 = (box1->x2 <= box2->x2)? box1->x2: box2->x2;
  return_box->y1 = (box1->y1 >= box2->y1)? box1->y1: box2->y1;
  return_box->y2 = (box1->y2 <= box2->y2)? box1->y2: box2->y2;

  if (return_box->x1 >= return_box->x2)
    return_box->x1 = return_box->x2 = 0;
  
  if (return_box->y1 >= return_box->y2)
    return_box->y1 = return_box->y2 = 0;
}

void
glitz_union_region (glitz_region_box_t *box1,
                    glitz_region_box_t *box2,
                    glitz_region_box_t *return_box)
{
  return_box->x1 = (box1->x1 <= box2->x1)? box1->x1: box2->x1;
  return_box->x2 = (box1->x2 >= box2->x2)? box1->x2: box2->x2;
  return_box->y1 = (box1->y1 <= box2->y1)? box1->y1: box2->y1;
  return_box->y2 = (box1->y2 >= box2->y2)? box1->y2: box2->y2;
}

void
glitz_intersect_sub_pixel_region (glitz_sub_pixel_region_box_t *box1,
                                  glitz_sub_pixel_region_box_t *box2,
                                  glitz_sub_pixel_region_box_t *return_box)
{
  return_box->x1 = (box1->x1 >= box2->x1)? box1->x1: box2->x1;
  return_box->x2 = (box1->x2 <= box2->x2)? box1->x2: box2->x2;
  return_box->y1 = (box1->y1 >= box2->y1)? box1->y1: box2->y1;
  return_box->y2 = (box1->y2 <= box2->y2)? box1->y2: box2->y2;

  if (return_box->x1 >= return_box->x2)
    return_box->x1 = return_box->x2 = 0.0;
  
  if (return_box->y1 >= return_box->y2)
    return_box->y1 = return_box->y2 = 0.0;
}

void
glitz_union_sub_pixel_region (glitz_sub_pixel_region_box_t *box1,
                              glitz_sub_pixel_region_box_t *box2,
                              glitz_sub_pixel_region_box_t *return_box)
{
  return_box->x1 = (box1->x1 <= box2->x1)? box1->x1: box2->x1;
  return_box->x2 = (box1->x2 >= box2->x2)? box1->x2: box2->x2;
  return_box->y1 = (box1->y1 <= box2->y1)? box1->y1: box2->y1;
  return_box->y2 = (box1->y2 >= box2->y2)? box1->y2: box2->y2;
}

static int
big_endian (void)
{
  int a = 1;
  char *c;
  
  c = (char *) &a;
  if (c[0])
    return 0;
  else
    return 1;
}

glitz_gl_enum_t
glitz_get_gl_format_from_bpp (unsigned short bpp)
{
  switch (bpp) {
  case 8:
    return GLITZ_GL_ALPHA;
    break;
  case 24:
    if (big_endian ())
      return GLITZ_GL_RGB;
    else
      return GLITZ_GL_BGR;
    break;
  default:
    return GLITZ_GL_BGRA;
    break;
  }
}

glitz_gl_enum_t
glitz_get_gl_data_type_from_bpp (unsigned short bpp)
{
  if (bpp == 32 && big_endian ())
    return GLITZ_GL_UNSIGNED_INT_8_8_8_8_REV;
  else
    return GLITZ_GL_UNSIGNED_BYTE;
}

static glitz_bool_t
_glitz_extension_check (const char *extensions,
                        const char *ext_name)
{
  char *end;
  char *p = (char *) extensions;
  int ext_name_len = strlen (ext_name);

  if (! p)
    return 0;

  end = p + strlen (p);

  while (p < end) {
    int n = strcspn (p, " ");

    if ((ext_name_len == n) && (strncmp (ext_name, p, n) == 0)) {
      return 1;
    }
    p += (n + 1);
  }
  return 0;
}

long int
glitz_extensions_query (const char *extensions_string,
                        glitz_extension_map *extensions_map)
{
  long int mask = 0;
  int i;

  for (i = 0; extensions_map[i].name; i++)
    if (_glitz_extension_check (extensions_string, extensions_map[i].name))
      mask |= extensions_map[i].mask;

  return mask;
}

glitz_bool_t
glitz_uint_is_power_of_two (unsigned int value)
{
  unsigned int x = 1;
  
  while (x < value)
    x <<= 1;

  return (x == value);
}

void
glitz_uint_to_power_of_two (unsigned int *value)
{
  unsigned int x = 1;
  
  while (x < *value)
    x <<= 1;

  *value = x;
}