File: art_rgba.c

package info (click to toggle)
libart-lgpl 2.3.21-4
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, buster, sid, trixie
  • size: 2,028 kB
  • sloc: ansic: 10,727; sh: 10,283; makefile: 155
file content (258 lines) | stat: -rw-r--r-- 6,720 bytes parent folder | download | duplicates (15)
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
 * art_rgba.c: Functions for manipulating RGBA pixel data.
 *
 * Libart_LGPL - library of basic graphic primitives
 * Copyright (C) 2000 Raph Levien
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "config.h"
#include "art_rgba.h"

#define ART_OPTIMIZE_SPACE

#ifndef ART_OPTIMIZE_SPACE
#include "art_rgba_table.c"
#endif

/**
 * art_rgba_rgba_composite: Composite RGBA image over RGBA buffer.
 * @dst: Destination RGBA buffer.
 * @src: Source RGBA buffer.
 * @n: Number of RGBA pixels to composite.
 *
 * Composites the RGBA pixels in @dst over the @src buffer.
 **/
void
art_rgba_rgba_composite (art_u8 *dst, const art_u8 *src, int n)
{
  int i;
#ifdef WORDS_BIGENDIAN
  art_u32 src_rgba, dst_rgba;
#else
  art_u32 src_abgr, dst_abgr;
#endif
  art_u8 src_alpha, dst_alpha;

  for (i = 0; i < n; i++)
    {
#ifdef WORDS_BIGENDIAN
      src_rgba = ((art_u32 *)src)[i];
      src_alpha = src_rgba & 0xff;
#else
      src_abgr = ((art_u32 *)src)[i];
      src_alpha = (src_abgr >> 24) & 0xff;
#endif
      if (src_alpha)
	{
	  if (src_alpha == 0xff ||
	      (
#ifdef WORDS_BIGENDIAN
	       dst_rgba = ((art_u32 *)dst)[i],
	       dst_alpha = dst_rgba & 0xff,
#else
	       dst_abgr = ((art_u32 *)dst)[i],
	       dst_alpha = (dst_abgr >> 24),
#endif
	       dst_alpha == 0))
#ifdef WORDS_BIGENDIAN
	    ((art_u32 *)dst)[i] = src_rgba;
#else
	    ((art_u32 *)dst)[i] = src_abgr;
#endif
	  else
	    {
	      int r, g, b, a;
	      int src_r, src_g, src_b;
	      int dst_r, dst_g, dst_b;
	      int tmp;
	      int c;

#ifdef ART_OPTIMIZE_SPACE
	      tmp = (255 - src_alpha) * (255 - dst_alpha) + 0x80;
	      a = 255 - ((tmp + (tmp >> 8)) >> 8);
	      c = ((src_alpha << 16) + (a >> 1)) / a;
#else
	      tmp = art_rgba_composite_table[(src_alpha << 8) + dst_alpha];
	      c = tmp & 0x1ffff;
	      a = tmp >> 24;
#endif
#ifdef WORDS_BIGENDIAN
	      src_r = (src_rgba >> 24) & 0xff;
	      src_g = (src_rgba >> 16) & 0xff;
	      src_b = (src_rgba >> 8) & 0xff;
	      dst_r = (dst_rgba >> 24) & 0xff;
	      dst_g = (dst_rgba >> 16) & 0xff;
	      dst_b = (dst_rgba >> 8) & 0xff;
#else
	      src_r = src_abgr & 0xff;
	      src_g = (src_abgr >> 8) & 0xff;
	      src_b = (src_abgr >> 16) & 0xff;
	      dst_r = dst_abgr & 0xff;
	      dst_g = (dst_abgr >> 8) & 0xff;
	      dst_b = (dst_abgr >> 16) & 0xff;
#endif
	      r = dst_r + (((src_r - dst_r) * c + 0x8000) >> 16);
	      g = dst_g + (((src_g - dst_g) * c + 0x8000) >> 16);
	      b = dst_b + (((src_b - dst_b) * c + 0x8000) >> 16);
#ifdef WORDS_BIGENDIAN
	    ((art_u32 *)dst)[i] = (r << 24) | (g << 16) | (b << 8) | a;
#else
	    ((art_u32 *)dst)[i] = (a << 24) | (b << 16) | (g << 8) | r;
#endif	      
	    }
	}
#if 0
      /* it's not clear to me this optimization really wins */
      else
	{
	  /* skip over run of transparent pixels */
	  for (; i < n - 1; i++)
	    {
#ifdef WORDS_BIGENDIAN
	      src_rgba = ((art_u32 *)src)[i + 1];
	      if (src_rgba & 0xff)
		break;
#else
	      src_abgr = ((art_u32 *)src)[i + 1];
	      if (src_abgr & 0xff000000)
		break;
#endif
	    }
	}
#endif
    }
}

/**
 * art_rgba_fill_run: fill an RGBA buffer a solid RGB color.
 * @buf: Buffer to fill.
 * @r: Red, range 0..255.
 * @g: Green, range 0..255.
 * @b: Blue, range 0..255.
 * @n: Number of RGB triples to fill.
 *
 * Fills a buffer with @n copies of the (@r, @g, @b) triple, solid
 * alpha. Thus, locations @buf (inclusive) through @buf + 4 * @n
 * (exclusive) are written.
 **/
void
art_rgba_fill_run (art_u8 *buf, art_u8 r, art_u8 g, art_u8 b, int n)
{
  int i;
#ifdef WORDS_BIGENDIAN
  art_u32 src_rgba;
#else
  art_u32 src_abgr;
#endif

#ifdef WORDS_BIGENDIAN
  src_rgba = (r << 24) | (g << 16) | (b << 8) | 255;
#else
  src_abgr = (255 << 24) | (b << 16) | (g << 8) | r;
#endif
  for (i = 0; i < n; i++)
    {
#ifdef WORDS_BIGENDIAN
      ((art_u32 *)buf)[i] = src_rgba;
#else
      ((art_u32 *)buf)[i] = src_abgr;
#endif
    }    
}

/**
 * art_rgba_run_alpha: Render semitransparent color over RGBA buffer.
 * @buf: Buffer for rendering.
 * @r: Red, range 0..255.
 * @g: Green, range 0..255.
 * @b: Blue, range 0..255.
 * @alpha: Alpha, range 0..255.
 * @n: Number of RGB triples to render.
 *
 * Renders a sequential run of solid (@r, @g, @b) color over @buf with
 * opacity @alpha. Note that the range of @alpha is 0..255, in contrast
 * to art_rgb_run_alpha, which has a range of 0..256.
 **/
void
art_rgba_run_alpha (art_u8 *buf, art_u8 r, art_u8 g, art_u8 b, int alpha, int n)
{
  int i;
#ifdef WORDS_BIGENDIAN
  art_u32 src_rgba, dst_rgba;
#else
  art_u32 src_abgr, dst_abgr;
#endif
  art_u8 dst_alpha;
  int a;
  int dst_r, dst_g, dst_b;
  int tmp;
  int c;

#ifdef WORDS_BIGENDIAN
  src_rgba = (r << 24) | (g << 16) | (b << 8) | alpha;
#else
  src_abgr = (alpha << 24) | (b << 16) | (g << 8) | r;
#endif
  for (i = 0; i < n; i++)
    {
#ifdef WORDS_BIGENDIAN
      dst_rgba = ((art_u32 *)buf)[i];
      dst_alpha = dst_rgba & 0xff;
#else
      dst_abgr = ((art_u32 *)buf)[i];
      dst_alpha = (dst_abgr >> 24) & 0xff;
#endif
      if (dst_alpha)
	{
#ifdef ART_OPTIMIZE_SPACE
	  tmp = (255 - alpha) * (255 - dst_alpha) + 0x80;
	  a = 255 - ((tmp + (tmp >> 8)) >> 8);
	  c = ((alpha << 16) + (a >> 1)) / a;
#else
	  tmp = art_rgba_composite_table[(alpha << 8) + dst_alpha];
	  c = tmp & 0x1ffff;
	  a = tmp >> 24;
#endif
#ifdef WORDS_BIGENDIAN
	  dst_r = (dst_rgba >> 24) & 0xff;
	  dst_g = (dst_rgba >> 16) & 0xff;
	  dst_b = (dst_rgba >> 8) & 0xff;
#else
	  dst_r = dst_abgr & 0xff;
	  dst_g = (dst_abgr >> 8) & 0xff;
	  dst_b = (dst_abgr >> 16) & 0xff;
#endif
	  dst_r += (((r - dst_r) * c + 0x8000) >> 16);
	  dst_g += (((g - dst_g) * c + 0x8000) >> 16);
	  dst_b += (((b - dst_b) * c + 0x8000) >> 16);
#ifdef WORDS_BIGENDIAN
	  ((art_u32 *)buf)[i] = (dst_r << 24) | (dst_g << 16) | (dst_b << 8) | a;
#else
	  ((art_u32 *)buf)[i] = (a << 24) | (dst_b << 16) | (dst_g << 8) | dst_r;
#endif
	}
      else
	{
#ifdef WORDS_BIGENDIAN
	  ((art_u32 *)buf)[i] = src_rgba;
#else
	  ((art_u32 *)buf)[i] = src_abgr;
#endif
	}
    }
}