Debian Codegraph @

third_party/etc2comp/EtcLib/EtcCodec/EtcBlock4x4.cpp

raw
1 /*
2 * Copyright 2015 The Etc2Comp Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 /*
18 EtcBlock4x4.cpp
19
20 Implements the state associated with each 4x4 block of pixels in an image
21
22 Source images that are not a multiple of 4x4 are extended to fill the Block4x4 using pixels with an
23 alpha of NAN
24
25 */
26
27 #include "EtcConfig.h"
28 #include "EtcBlock4x4.h"
29
30 #include "EtcBlock4x4EncodingBits.h"
31 #include "EtcColor.h"
32 #include "EtcImage.h"
33 #include "EtcColorFloatRGBA.h"
34 #include "EtcBlock4x4Encoding_RGB8.h"
35 #include "EtcBlock4x4Encoding_RGBA8.h"
36 #include "EtcBlock4x4Encoding_RGB8A1.h"
37 #include "EtcBlock4x4Encoding_R11.h"
38 #include "EtcBlock4x4Encoding_RG11.h"
39
40 #include <stdio.h>
41 #include <string.h>
42 #include <assert.h>
43
44 namespace Etc
45 {
46 // ETC pixels are scanned vertically.
47 // this mapping is for when someone wants to scan the ETC pixels horizontally
48 const unsigned int Block4x4::s_auiPixelOrderHScan[PIXELS] = { 0, 4, 8, 12, 1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15 };
49
50 // ----------------------------------------------------------------------------------------------------
51 //
52 Block4x4::Block4x4(void)
53 {
54 m_pimageSource = nullptr;
55 m_uiSourceH = 0;
56 m_uiSourceV = 0;
57
58 m_sourcealphamix = SourceAlphaMix::UNKNOWN;
59 m_boolBorderPixels = false;
60 m_boolPunchThroughPixels = false;
61
62 m_pencoding = nullptr;
63
64 m_errormetric = ErrorMetric::NUMERIC;
65
66 }
67 Block4x4::~Block4x4()
68 {
69 m_pimageSource = nullptr;
70 if (m_pencoding)
71 {
72 delete m_pencoding;
73 m_pencoding = nullptr;
74 }
75 }
76 // ----------------------------------------------------------------------------------------------------
77 // initialization prior to encoding from a source image
78 // [a_uiSourceH,a_uiSourceV] is the location of the block in a_pimageSource
79 // a_paucEncodingBits is the place to store the final encoding
80 // a_errormetric is used for finding the best encoding
81 //
82 void Block4x4::InitFromSource(Image *a_pimageSource,
83 unsigned int a_uiSourceH, unsigned int a_uiSourceV,
84 unsigned char *a_paucEncodingBits,
85 ErrorMetric a_errormetric)
86 {
87
88 Block4x4();
89
90 m_pimageSource = a_pimageSource;
91 m_uiSourceH = a_uiSourceH;
92 m_uiSourceV = a_uiSourceV;
93 m_errormetric = a_errormetric;
94
95 SetSourcePixels();
96
97 // set block encoder function
98 switch (m_pimageSource->GetFormat())
99 {
100 case Image::Format::ETC1:
101 m_pencoding = new Block4x4Encoding_ETC1;
102 break;
103
104 case Image::Format::RGB8:
105 case Image::Format::SRGB8:
106 m_pencoding = new Block4x4Encoding_RGB8;
107 break;
108
109 case Image::Format::RGBA8:
110 case Image::Format::SRGBA8:
111 if (a_errormetric == RGBX)
112 {
113 m_pencoding = new Block4x4Encoding_RGBA8;
114 }
115 else
116 {
117 switch (m_sourcealphamix)
118 {
119 case SourceAlphaMix::OPAQUE:
120 m_pencoding = new Block4x4Encoding_RGBA8_Opaque;
121 break;
122
123 case SourceAlphaMix::TRANSPARENT:
124 m_pencoding = new Block4x4Encoding_RGBA8_Transparent;
125 break;
126
127 case SourceAlphaMix::TRANSLUCENT:
128 m_pencoding = new Block4x4Encoding_RGBA8;
129 break;
130
131 default:
132 assert(0);
133 break;
134 }
135 break;
136 }
137 break;
138
139 case Image::Format::RGB8A1:
140 case Image::Format::SRGB8A1:
141 switch (m_sourcealphamix)
142 {
143 case SourceAlphaMix::OPAQUE:
144 m_pencoding = new Block4x4Encoding_RGB8A1_Opaque;
145 break;
146
147 case SourceAlphaMix::TRANSPARENT:
148 m_pencoding = new Block4x4Encoding_RGB8A1_Transparent;
149 break;
150
151 case SourceAlphaMix::TRANSLUCENT:
152 if (m_boolPunchThroughPixels)
153 {
154 m_pencoding = new Block4x4Encoding_RGB8A1;
155 }
156 else
157 {
158 m_pencoding = new Block4x4Encoding_RGB8A1_Opaque;
159 }
160 break;
161
162 default:
163 assert(0);
164 break;
165 }
166 break;
167
168 case Image::Format::R11:
169 case Image::Format::SIGNED_R11:
170 m_pencoding = new Block4x4Encoding_R11;
171 break;
172 case Image::Format::RG11:
173 case Image::Format::SIGNED_RG11:
174 m_pencoding = new Block4x4Encoding_RG11;
175 break;
176 default:
177 assert(0);
178 break;
179 }
180
181 m_pencoding->InitFromSource(this, m_afrgbaSource,
182 a_paucEncodingBits, a_errormetric);
183
184 }
185
186 // ----------------------------------------------------------------------------------------------------
187 // initialization of encoding state from a prior encoding using encoding bits
188 // [a_uiSourceH,a_uiSourceV] is the location of the block in a_pimageSource
189 // a_paucEncodingBits is the place to read the prior encoding
190 // a_imageformat is used to determine how to interpret a_paucEncodingBits
191 // a_errormetric was used for the prior encoding
192 //
193 void Block4x4::InitFromEtcEncodingBits(Image::Format a_imageformat,
194 unsigned int a_uiSourceH, unsigned int a_uiSourceV,
195 unsigned char *a_paucEncodingBits,
196 Image *a_pimageSource,
197 ErrorMetric a_errormetric)
198 {
199 Block4x4();
200
201 m_pimageSource = a_pimageSource;
202 m_uiSourceH = a_uiSourceH;
203 m_uiSourceV = a_uiSourceV;
204 m_errormetric = a_errormetric;
205
206 SetSourcePixels();
207
208 // set block encoder function
209 switch (a_imageformat)
210 {
211 case Image::Format::ETC1:
212 m_pencoding = new Block4x4Encoding_ETC1;
213 break;
214
215 case Image::Format::RGB8:
216 case Image::Format::SRGB8:
217 m_pencoding = new Block4x4Encoding_RGB8;
218 break;
219
220 case Image::Format::RGBA8:
221 case Image::Format::SRGBA8:
222 m_pencoding = new Block4x4Encoding_RGBA8;
223 break;
224
225 case Image::Format::RGB8A1:
226 case Image::Format::SRGB8A1:
227 m_pencoding = new Block4x4Encoding_RGB8A1;
228 break;
229
230 case Image::Format::R11:
231 case Image::Format::SIGNED_R11:
232 m_pencoding = new Block4x4Encoding_R11;
233 break;
234 case Image::Format::RG11:
235 case Image::Format::SIGNED_RG11:
236 m_pencoding = new Block4x4Encoding_RG11;
237 break;
238 default:
239 assert(0);
240 break;
241 }
242
243 m_pencoding->InitFromEncodingBits(this, a_paucEncodingBits, m_afrgbaSource,
244 m_pimageSource->GetErrorMetric());
245
246 }
247
248 // ----------------------------------------------------------------------------------------------------
249 // set source pixels from m_pimageSource
250 // set m_alphamix
251 //
252 void Block4x4::SetSourcePixels(void)
253 {
254
255 Image::Format imageformat = m_pimageSource->GetFormat();
256
257 // alpha census
258 unsigned int uiTransparentSourcePixels = 0;
259 unsigned int uiOpaqueSourcePixels = 0;
260
261 // copy source to consecutive memory locations
262 // convert from image horizontal scan to block vertical scan
263 unsigned int uiPixel = 0;
264 for (unsigned int uiBlockPixelH = 0; uiBlockPixelH < Block4x4::COLUMNS; uiBlockPixelH++)
265 {
266 unsigned int uiSourcePixelH = m_uiSourceH + uiBlockPixelH;
267
268 for (unsigned int uiBlockPixelV = 0; uiBlockPixelV < Block4x4::ROWS; uiBlockPixelV++)
269 {
270 unsigned int uiSourcePixelV = m_uiSourceV + uiBlockPixelV;
271
272 ColorFloatRGBA *pfrgbaSource = m_pimageSource->GetSourcePixel(uiSourcePixelH, uiSourcePixelV);
273
274 // if pixel extends beyond source image because of block padding
275 if (pfrgbaSource == nullptr)
276 {
277 m_afrgbaSource[uiPixel] = ColorFloatRGBA(0.0f, 0.0f, 0.0f, NAN); // denotes border pixel
278 m_boolBorderPixels = true;
279 uiTransparentSourcePixels++;
280 }
281 else
282 {
283 //get teh current pixel data, and store some of the attributes
284 //before capping values to fit the encoder type
285
286 m_afrgbaSource[uiPixel] = (*pfrgbaSource).ClampRGBA();
287
288 if (m_afrgbaSource[uiPixel].fA == 1.0f || m_errormetric == RGBX)
289 {
290 m_pimageSource->m_iNumOpaquePixels++;
291 }
292 else if (m_afrgbaSource[uiPixel].fA == 0.0f)
293 {
294 m_pimageSource->m_iNumTransparentPixels++;
295 }
296 else if(m_afrgbaSource[uiPixel].fA > 0.0f && m_afrgbaSource[uiPixel].fA < 1.0f)
297 {
298 m_pimageSource->m_iNumTranslucentPixels++;
299 }
300 else
301 {
302 m_pimageSource->m_numOutOfRangeValues.fA++;
303 }
304
305 if (m_afrgbaSource[uiPixel].fR != 0.0f)
306 {
307 m_pimageSource->m_numColorValues.fR++;
308 //make sure we are getting a float between 0-1
309 if (m_afrgbaSource[uiPixel].fR - 1.0f > 0.0f)
310 {
311 m_pimageSource->m_numOutOfRangeValues.fR++;
312 }
313 }
314
315 if (m_afrgbaSource[uiPixel].fG != 0.0f)
316 {
317 m_pimageSource->m_numColorValues.fG++;
318 if (m_afrgbaSource[uiPixel].fG - 1.0f > 0.0f)
319 {
320 m_pimageSource->m_numOutOfRangeValues.fG++;
321 }
322 }
323 if (m_afrgbaSource[uiPixel].fB != 0.0f)
324 {
325 m_pimageSource->m_numColorValues.fB++;
326 if (m_afrgbaSource[uiPixel].fB - 1.0f > 0.0f)
327 {
328 m_pimageSource->m_numOutOfRangeValues.fB++;
329 }
330 }
331 // for formats with no alpha, set source alpha to 1
332 if (imageformat == Image::Format::ETC1 ||
333 imageformat == Image::Format::RGB8 ||
334 imageformat == Image::Format::SRGB8)
335 {
336 m_afrgbaSource[uiPixel].fA = 1.0f;
337 }
338
339 if (imageformat == Image::Format::R11 ||
340 imageformat == Image::Format::SIGNED_R11)
341 {
342 m_afrgbaSource[uiPixel].fA = 1.0f;
343 m_afrgbaSource[uiPixel].fG = 0.0f;
344 m_afrgbaSource[uiPixel].fB = 0.0f;
345 }
346
347 if (imageformat == Image::Format::RG11 ||
348 imageformat == Image::Format::SIGNED_RG11)
349 {
350 m_afrgbaSource[uiPixel].fA = 1.0f;
351 m_afrgbaSource[uiPixel].fB = 0.0f;
352 }
353
354
355 // for RGB8A1, set source alpha to 0.0 or 1.0
356 // set punch through flag
357 if (imageformat == Image::Format::RGB8A1 ||
358 imageformat == Image::Format::SRGB8A1)
359 {
360 if (m_afrgbaSource[uiPixel].fA >= 0.5f)
361 {
362 m_afrgbaSource[uiPixel].fA = 1.0f;
363 }
364 else
365 {
366 m_afrgbaSource[uiPixel].fA = 0.0f;
367 m_boolPunchThroughPixels = true;
368 }
369 }
370
371 if (m_afrgbaSource[uiPixel].fA == 1.0f || m_errormetric == RGBX)
372 {
373 uiOpaqueSourcePixels++;
374 }
375 else if (m_afrgbaSource[uiPixel].fA == 0.0f)
376 {
377 uiTransparentSourcePixels++;
378 }
379
380 }
381
382 uiPixel += 1;
383 }
384 }
385
386 if (uiOpaqueSourcePixels == PIXELS)
387 {
388 m_sourcealphamix = SourceAlphaMix::OPAQUE;
389 }
390 else if (uiTransparentSourcePixels == PIXELS)
391 {
392 m_sourcealphamix = SourceAlphaMix::TRANSPARENT;
393 }
394 else
395 {
396 m_sourcealphamix = SourceAlphaMix::TRANSLUCENT;
397 }
398
399 }
400
401 // ----------------------------------------------------------------------------------------------------
402 // return a name for the encoding mode
403 //
404 const char * Block4x4::GetEncodingModeName(void)
405 {
406
407 switch (m_pencoding->GetMode())
408 {
409 case Block4x4Encoding::MODE_ETC1:
410 return "ETC1";
411 case Block4x4Encoding::MODE_T:
412 return "T";
413 case Block4x4Encoding::MODE_H:
414 return "H";
415 case Block4x4Encoding::MODE_PLANAR:
416 return "PLANAR";
417 default:
418 return "???";
419 }
420 }
421
422 // ----------------------------------------------------------------------------------------------------
423 //
424
425 }