1/**************************************************************************
2 *
3 * Copyright (C) 1999-2007  Brian Paul   All Rights Reserved.
4 * Copyright (c) 2008 VMware, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 **************************************************************************/
25
26#include "u_format.h"
27#include "u_format_bptc.h"
28#include "util/format_srgb.h"
29#include "util/u_math.h"
30
31#define BPTC_BLOCK_DECODE
32#include "../../../mesa/main/texcompress_bptc_tmp.h"
33
34void
35util_format_bptc_rgba_unorm_unpack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
36                                               const uint8_t *src_row, unsigned src_stride,
37                                               unsigned width, unsigned height)
38{
39  decompress_rgba_unorm(width, height,
40                        src_row, src_stride,
41                        dst_row, dst_stride);
42}
43
44void
45util_format_bptc_rgba_unorm_pack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
46                                             const uint8_t *src_row, unsigned src_stride,
47                                             unsigned width, unsigned height)
48{
49   compress_rgba_unorm(width, height,
50                       src_row, src_stride,
51                       dst_row, dst_stride);
52}
53
54void
55util_format_bptc_rgba_unorm_unpack_rgba_float(float *dst_row, unsigned dst_stride,
56                                              const uint8_t *src_row, unsigned src_stride,
57                                              unsigned width, unsigned height)
58{
59   uint8_t *temp_block;
60   temp_block = malloc(width * height * 4 * sizeof(uint8_t));
61   decompress_rgba_unorm(width, height,
62                         src_row, src_stride,
63                         temp_block, width * 4 * sizeof(uint8_t));
64   util_format_read_4f(PIPE_FORMAT_R8G8B8A8_UNORM,
65                       dst_row, dst_stride,
66                       temp_block, width * 4 * sizeof(uint8_t),
67                       0, 0, width, height);
68   free((void *) temp_block);
69}
70
71void
72util_format_bptc_rgba_unorm_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride,
73                                            const float *src_row, unsigned src_stride,
74                                            unsigned width, unsigned height)
75{
76   uint8_t *temp_block;
77   temp_block = malloc(width * height * 4 * sizeof(uint8_t));
78   util_format_read_4ub(PIPE_FORMAT_R32G32B32A32_FLOAT,
79                        temp_block, width * 4 * sizeof(uint8_t),
80                        src_row, src_stride,
81                        0, 0, width, height);
82   compress_rgba_unorm(width, height,
83                       temp_block, width * 4 * sizeof(uint8_t),
84                       dst_row, dst_stride);
85   free((void *) temp_block);
86}
87
88void
89util_format_bptc_rgba_unorm_fetch_rgba_float(float *dst, const uint8_t *src,
90                                             unsigned width, unsigned height)
91{
92   uint8_t temp_block[4];
93
94   fetch_rgba_unorm_from_block(src + ((width * sizeof(uint8_t)) * (height / 4) + (width / 4)) * 16,
95                               temp_block, (width % 4) + (height % 4) * 4);
96
97   util_format_read_4f(PIPE_FORMAT_R8G8B8A8_UNORM,
98                       dst, 4 * sizeof(float),
99                       temp_block, 4 * sizeof(uint8_t),
100                       0, 0, 1, 1);
101}
102
103void
104util_format_bptc_srgba_unpack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
105                                          const uint8_t *src_row, unsigned src_stride,
106                                          unsigned width, unsigned height)
107{
108   decompress_rgba_unorm(width, height,
109                         src_row, src_stride,
110                         dst_row, dst_stride);
111}
112
113void
114util_format_bptc_srgba_pack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
115                                        const uint8_t *src_row, unsigned src_stride,
116                                        unsigned width, unsigned height)
117{
118   compress_rgba_unorm(width, height,
119                       src_row, src_stride,
120                       dst_row, dst_stride);
121}
122
123void
124util_format_bptc_srgba_unpack_rgba_float(float *dst_row, unsigned dst_stride,
125                                         const uint8_t *src_row, unsigned src_stride,
126                                         unsigned width, unsigned height)
127{
128   uint8_t *temp_block;
129   temp_block = malloc(width * height * 4 * sizeof(uint8_t));
130   decompress_rgba_unorm(width, height,
131                         src_row, src_stride,
132                         temp_block, width * 4 * sizeof(uint8_t));
133   util_format_read_4f(PIPE_FORMAT_R8G8B8A8_SRGB,
134                       dst_row, dst_stride,
135                       temp_block, width * 4 * sizeof(uint8_t),
136                       0, 0, width, height);
137   free((void *) temp_block);
138}
139
140void
141util_format_bptc_srgba_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride,
142                                       const float *src_row, unsigned src_stride,
143                                       unsigned width, unsigned height)
144{
145   compress_rgb_float(width, height,
146                      src_row, src_stride,
147                      dst_row, dst_stride,
148                      true);
149}
150
151void
152util_format_bptc_srgba_fetch_rgba_float(float *dst, const uint8_t *src,
153                                        unsigned width, unsigned height)
154{
155   uint8_t temp_block[4];
156
157   fetch_rgba_unorm_from_block(src + ((width * sizeof(uint8_t)) * (height / 4) + (width / 4)) * 16,
158                               temp_block, (width % 4) + (height % 4) * 4);
159   util_format_read_4f(PIPE_FORMAT_R8G8B8A8_SRGB,
160                       dst, 4 * sizeof(float),
161                       temp_block, width * 4 * sizeof(uint8_t),
162                       0, 0, 1, 1);
163}
164
165void
166util_format_bptc_rgb_float_unpack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
167                                              const uint8_t *src_row, unsigned src_stride,
168                                              unsigned width, unsigned height)
169{
170   float *temp_block;
171   temp_block = malloc(width * height * 4 * sizeof(float));
172   decompress_rgb_float(width, height,
173                        src_row, src_stride,
174                        temp_block, width * 4 * sizeof(float),
175                        true);
176   util_format_read_4ub(PIPE_FORMAT_R32G32B32A32_FLOAT,
177                        dst_row, dst_stride,
178                        temp_block, width * 4 * sizeof(float),
179                        0, 0, width, height);
180   free((void *) temp_block);
181}
182
183void
184util_format_bptc_rgb_float_pack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
185                                            const uint8_t *src_row, unsigned src_stride,
186                                            unsigned width, unsigned height)
187{
188   compress_rgba_unorm(width, height,
189                       src_row, src_stride,
190                       dst_row, dst_stride);
191}
192
193void
194util_format_bptc_rgb_float_unpack_rgba_float(float *dst_row, unsigned dst_stride,
195                                             const uint8_t *src_row, unsigned src_stride,
196                                             unsigned width, unsigned height)
197{
198   decompress_rgb_float(width, height,
199                        src_row, src_stride,
200                        dst_row, dst_stride,
201                        true);
202}
203
204void
205util_format_bptc_rgb_float_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride,
206                                           const float *src_row, unsigned src_stride,
207                                           unsigned width, unsigned height)
208{
209   compress_rgb_float(width, height,
210                      src_row, src_stride,
211                      dst_row, dst_stride,
212                      true);
213}
214
215void
216util_format_bptc_rgb_float_fetch_rgba_float(float *dst, const uint8_t *src,
217                                            unsigned width, unsigned height)
218{
219   fetch_rgb_float_from_block(src + ((width * sizeof(uint8_t)) * (height / 4) + (width / 4)) * 16,
220                              dst, (width % 4) + (height % 4) * 4, true);
221}
222
223void
224util_format_bptc_rgb_ufloat_unpack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
225                                               const uint8_t *src_row, unsigned src_stride,
226                                               unsigned width, unsigned height)
227{
228   float *temp_block;
229   temp_block = malloc(width * height * 4 * sizeof(float));
230   decompress_rgb_float(width, height,
231                        src_row, src_stride,
232                        temp_block, width * 4 * sizeof(float),
233                        false);
234   util_format_read_4ub(PIPE_FORMAT_R32G32B32A32_FLOAT,
235                        dst_row, dst_stride,
236                        temp_block, width * 4 * sizeof(float),
237                        0, 0, width, height);
238   free((void *) temp_block);
239}
240
241void
242util_format_bptc_rgb_ufloat_pack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
243                                             const uint8_t *src_row, unsigned src_stride,
244                                             unsigned width, unsigned height)
245{
246   compress_rgba_unorm(width, height,
247                       src_row, src_stride,
248                       dst_row, dst_stride);
249}
250
251void
252util_format_bptc_rgb_ufloat_unpack_rgba_float(float *dst_row, unsigned dst_stride,
253                                              const uint8_t *src_row, unsigned src_stride,
254                                              unsigned width, unsigned height)
255{
256   decompress_rgb_float(width, height,
257                        src_row, src_stride,
258                        dst_row, dst_stride,
259                        false);
260}
261
262void
263util_format_bptc_rgb_ufloat_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride,
264                                            const float *src_row, unsigned src_stride,
265                                            unsigned width, unsigned height)
266{
267   compress_rgb_float(width, height,
268                      src_row, src_stride,
269                      dst_row, dst_stride,
270                      false);
271}
272
273void
274util_format_bptc_rgb_ufloat_fetch_rgba_float(float *dst, const uint8_t *src,
275                                             unsigned width, unsigned height)
276{
277   fetch_rgb_float_from_block(src + ((width * sizeof(uint8_t)) * (height / 4) + (width / 4)) * 16,
278                              dst, (width % 4) + (height % 4) * 4, false);
279}
280