gallium: move surface utility functions into u_surface.c
[mesa.git] / src / gallium / auxiliary / util / u_rect.c
1 /**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * 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
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /**
29 * Rectangle-related helper functions.
30 */
31
32
33 #include "util/u_format.h"
34 #include "util/u_rect.h"
35
36
37 /**
38 * Copy 2D rect from one place to another.
39 * Position and sizes are in pixels.
40 * src_stride may be negative to do vertical flip of pixels from source.
41 */
42 void
43 util_copy_rect(ubyte * dst,
44 enum pipe_format format,
45 unsigned dst_stride,
46 unsigned dst_x,
47 unsigned dst_y,
48 unsigned width,
49 unsigned height,
50 const ubyte * src,
51 int src_stride,
52 unsigned src_x,
53 unsigned src_y)
54 {
55 unsigned i;
56 int src_stride_pos = src_stride < 0 ? -src_stride : src_stride;
57 int blocksize = util_format_get_blocksize(format);
58 int blockwidth = util_format_get_blockwidth(format);
59 int blockheight = util_format_get_blockheight(format);
60
61 assert(blocksize > 0);
62 assert(blockwidth > 0);
63 assert(blockheight > 0);
64
65 dst_x /= blockwidth;
66 dst_y /= blockheight;
67 width = (width + blockwidth - 1)/blockwidth;
68 height = (height + blockheight - 1)/blockheight;
69 src_x /= blockwidth;
70 src_y /= blockheight;
71
72 dst += dst_x * blocksize;
73 src += src_x * blocksize;
74 dst += dst_y * dst_stride;
75 src += src_y * src_stride_pos;
76 width *= blocksize;
77
78 if (width == dst_stride && width == src_stride)
79 memcpy(dst, src, height * width);
80 else {
81 for (i = 0; i < height; i++) {
82 memcpy(dst, src, width);
83 dst += dst_stride;
84 src += src_stride;
85 }
86 }
87 }
88
89 void
90 util_fill_rect(ubyte * dst,
91 enum pipe_format format,
92 unsigned dst_stride,
93 unsigned dst_x,
94 unsigned dst_y,
95 unsigned width,
96 unsigned height,
97 uint32_t value)
98 {
99 unsigned i, j;
100 unsigned width_size;
101 int blocksize = util_format_get_blocksize(format);
102 int blockwidth = util_format_get_blockwidth(format);
103 int blockheight = util_format_get_blockheight(format);
104
105 assert(blocksize > 0);
106 assert(blockwidth > 0);
107 assert(blockheight > 0);
108
109 dst_x /= blockwidth;
110 dst_y /= blockheight;
111 width = (width + blockwidth - 1)/blockwidth;
112 height = (height + blockheight - 1)/blockheight;
113
114 dst += dst_x * blocksize;
115 dst += dst_y * dst_stride;
116 width_size = width * blocksize;
117
118 switch (blocksize) {
119 case 1:
120 if(dst_stride == width_size)
121 memset(dst, (ubyte) value, height * width_size);
122 else {
123 for (i = 0; i < height; i++) {
124 memset(dst, (ubyte) value, width_size);
125 dst += dst_stride;
126 }
127 }
128 break;
129 case 2:
130 for (i = 0; i < height; i++) {
131 uint16_t *row = (uint16_t *)dst;
132 for (j = 0; j < width; j++)
133 *row++ = (uint16_t) value;
134 dst += dst_stride;
135 }
136 break;
137 case 4:
138 for (i = 0; i < height; i++) {
139 uint32_t *row = (uint32_t *)dst;
140 for (j = 0; j < width; j++)
141 *row++ = value;
142 dst += dst_stride;
143 }
144 break;
145 default:
146 assert(0);
147 break;
148 }
149 }