util: Use u_format_pack.py's code instead of u_format_access.py.
[mesa.git] / src / gallium / auxiliary / util / u_format.c
1 /**************************************************************************
2 *
3 * Copyright 2010 VMware, Inc.
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 VMWARE 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 * @file
30 * Pixel format accessor functions.
31 *
32 * @author Jose Fonseca <jfonseca@vmware.com>
33 */
34
35 #include "u_format.h"
36
37
38 void
39 util_format_read_4f(enum pipe_format format,
40 float *dst, unsigned dst_stride,
41 const void *src, unsigned src_stride,
42 unsigned x, unsigned y, unsigned w, unsigned h)
43 {
44 const struct util_format_description *format_desc;
45 const uint8_t *src_row;
46 float *dst_row;
47 unsigned row_blocks;
48 unsigned i;
49
50 format_desc = util_format_description(format);
51
52 assert(x % format_desc->block.width == 0);
53 assert(y % format_desc->block.height == 0);
54
55 src_row = (const uint8_t *)src + y*src_stride + x*(format_desc->block.bits/8);
56 dst_row = dst;
57 row_blocks = (w + format_desc->block.width - 1) / format_desc->block.width;
58
59 for (i = 0; i < h; i += format_desc->block.height) {
60 format_desc->unpack_float(dst_row, src_row, row_blocks);
61 src_row += src_stride;
62 dst_row += dst_stride/sizeof(*dst_row);
63 }
64 }
65
66
67 void
68 util_format_write_4f(enum pipe_format format,
69 const float *src, unsigned src_stride,
70 void *dst, unsigned dst_stride,
71 unsigned x, unsigned y, unsigned w, unsigned h)
72 {
73 const struct util_format_description *format_desc;
74 uint8_t *dst_row;
75 const float *src_row;
76 unsigned row_blocks;
77 unsigned i;
78
79 format_desc = util_format_description(format);
80
81 assert(x % format_desc->block.width == 0);
82 assert(y % format_desc->block.height == 0);
83
84 dst_row = (uint8_t *)dst + y*dst_stride + x*(format_desc->block.bits/8);
85 src_row = src;
86 row_blocks = (w + format_desc->block.width - 1) / format_desc->block.width;
87
88 for (i = 0; i < h; i += format_desc->block.height) {
89 format_desc->pack_float(dst_row, src_row, row_blocks);
90 dst_row += dst_stride;
91 src_row += src_stride/sizeof(*src_row);
92 }
93 }
94
95
96 void
97 util_format_read_4ub(enum pipe_format format, uint8_t *dst, unsigned dst_stride, const void *src, unsigned src_stride, unsigned x, unsigned y, unsigned w, unsigned h)
98 {
99 const struct util_format_description *format_desc;
100 const uint8_t *src_row;
101 uint8_t *dst_row;
102 unsigned row_blocks;
103 unsigned i;
104
105 format_desc = util_format_description(format);
106
107 assert(x % format_desc->block.width == 0);
108 assert(y % format_desc->block.height == 0);
109
110 src_row = (const uint8_t *)src + y*src_stride + x*(format_desc->block.bits/8);
111 dst_row = dst;
112 row_blocks = (w + format_desc->block.width - 1) / format_desc->block.width;
113
114 for (i = 0; i < h; i += format_desc->block.height) {
115 format_desc->unpack_8unorm(dst_row, src_row, row_blocks);
116 src_row += src_stride;
117 dst_row += dst_stride/sizeof(*dst_row);
118 }
119 }
120
121
122 void
123 util_format_write_4ub(enum pipe_format format, const uint8_t *src, unsigned src_stride, void *dst, unsigned dst_stride, unsigned x, unsigned y, unsigned w, unsigned h)
124 {
125 const struct util_format_description *format_desc;
126 uint8_t *dst_row;
127 const uint8_t *src_row;
128 unsigned row_blocks;
129 unsigned i;
130
131 format_desc = util_format_description(format);
132
133 assert(x % format_desc->block.width == 0);
134 assert(y % format_desc->block.height == 0);
135
136 dst_row = (uint8_t *)dst + y*dst_stride + x*(format_desc->block.bits/8);
137 src_row = src;
138 row_blocks = (w + format_desc->block.width - 1) / format_desc->block.width;
139
140 for (i = 0; i < h; i += format_desc->block.height) {
141 format_desc->pack_8unorm(dst_row, src_row, row_blocks);
142 dst_row += dst_stride;
143 src_row += src_stride/sizeof(*src_row);
144 }
145 }
146