Debian Codegraph @

third_party/etc2comp/EtcTool/EtcAnalysis.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 #ifdef _WIN32
18 #define _CRT_SECURE_NO_WARNINGS (1)
19 #endif
20
21 #include "EtcAnalysis.h"
22
23 #include "EtcTool.h"
24 #include "EtcComparison.h"
25 #include "Etc.h"
26 #include "EtcFile.h"
27 #include "EtcMath.h"
28 #include "EtcImage.h"
29 #include "EtcBlock4x4.h"
30
31 #include "lodepng.h"
32 #include <stdlib.h>
33 #include <cmath> //sqrt fn()
34
35 namespace Etc
36 {
37
38 // ----------------------------------------------------------------------------------------------------
39 //
40 Analysis::Analysis(Image *a_pimage, const char *a_pstrOutputFolder)
41 {
42
43 m_pimage = a_pimage;
44 m_pstrOutputFolder = a_pstrOutputFolder;
45 m_uiComparisons = 0;
46
47 CreateNewDir(a_pstrOutputFolder);
48
49 // write log file
50 {
51 char strFilename[200];
52 sprintf(strFilename, "%s%cAnalysis.txt", m_pstrOutputFolder, ETC_PATH_SLASH);
53
54 FILE *pfileTxt = fopen(strFilename, "wt");
55 if (pfileTxt == nullptr)
56 {
57 printf("Error: couldn't create analysis log file (%s)\n", strFilename);
58 exit(1);
59 }
60
61 float fImageError = m_pimage->GetError();
62 unsigned int uiImagePixels = m_pimage->GetSourceWidth() * m_pimage->GetSourceHeight();
63
64 // output stats to both stdout and the analysis file
65 int numOutputs = 1;
66 FILE *apfile[2];
67 apfile[0] = pfileTxt;
68
69 if (m_pimage->m_bVerboseOutput)
70 {
71 apfile[1] = stdout;
72 numOutputs++;
73 }
74
75 for (int i = 0; i < numOutputs; i++)
76 {
77
78 if (a_pimage->GetFormat() == Image::Format::R11 || a_pimage->GetFormat() == Image::Format::SIGNED_R11)
79 {
80 fprintf(apfile[i], "PSNR(r) = %.4f\n", ConvertErrorToPSNR(fImageError, 1 * uiImagePixels));
81 }
82 else if (a_pimage->GetFormat() == Image::Format::RG11 || a_pimage->GetFormat() == Image::Format::SIGNED_RG11)
83 {
84 fprintf(apfile[i], "PSNR(rg) = %.4f\n", ConvertErrorToPSNR(fImageError, 2 * uiImagePixels));
85 }
86 else
87 {
88 int iComponents=3;
89 if (a_pimage->GetErrorMetric() == ErrorMetric::REC709)
90 {
91 iComponents = (int)Block4x4Encoding::LUMA_WEIGHT + 2;
92 }
93 fprintf(apfile[i], "PSNR(rgb) = %.4f\n", ConvertErrorToPSNR(fImageError, iComponents * uiImagePixels));
94 fprintf(apfile[i], "PSNR(rgba) = %.4f\n", ConvertErrorToPSNR(fImageError, (iComponents+1) * uiImagePixels));
95 }
96
97 fprintf(apfile[i], "EncodeTime = %.3f seconds\n", (float)m_pimage->GetEncodingTimeMs() / 1000.0f);
98 }
99
100
101 fclose(pfileTxt);
102 }
103
104 // scale == 1
105 DrawImage(m_pimage, m_pstrOutputFolder, false);
106
107 // scale == 2, with modes
108 DrawImage(m_pimage, m_pstrOutputFolder, true);
109
110 }
111
112 // ----------------------------------------------------------------------------------------------------
113 //
114 void Analysis::Compare(const char *a_pstrFilename, int a_iPixelX, int a_iPixelY)
115 {
116 if (m_uiComparisons >= MAX_COMPARISONS)
117 {
118 printf("Error: too many comparisons\n");
119 exit(1);
120 }
121
122 m_apcomparison[m_uiComparisons] = new Comparison(this, m_uiComparisons, a_pstrFilename, a_iPixelX, a_iPixelY);
123
124 m_uiComparisons++;
125
126 }
127
128 // ----------------------------------------------------------------------------------------------------
129 // draw 2x image
130 // optionally display encoding modes
131 void Analysis::DrawImage(Image *a_pimage, const char *a_pstrOutputFolder, bool a_boolDrawModes)
132 {
133 unsigned int uiPngWidth = a_pimage->GetExtendedWidth();
134 unsigned int uiPngHeight = a_pimage->GetExtendedHeight();
135 if (a_boolDrawModes)
136 {
137 uiPngWidth *= 2;
138 uiPngHeight *= 2;
139 }
140
141 unsigned char* paucPngPixels = new unsigned char[uiPngWidth * uiPngHeight * 4];
142 assert(paucPngPixels);
143
144 ColorR8G8B8A8 *pargba8PngPixels = (ColorR8G8B8A8 *)paucPngPixels;
145
146 for (unsigned int uiBlock = 0; uiBlock < a_pimage->GetNumberOfBlocks(); uiBlock++)
147 {
148 Block4x4 *pblock = &a_pimage->GetBlocks()[uiBlock];
149 if (a_boolDrawModes)
150 {
151 if (a_pimage->GetFormat() == Image::Format::R11 || a_pimage->GetFormat() == Image::Format::SIGNED_R11)
152 {
153 DrawBlockPixels(pblock, pargba8PngPixels, uiPngWidth, true, 2);
154 }
155 else
156 {
157 DrawBlockPixels(pblock, pargba8PngPixels, uiPngWidth, false, 2);
158 }
159 DrawBlockMode2x(pblock, pargba8PngPixels, uiPngWidth);
160 }
161 else
162 {
163 if (a_pimage->GetFormat() == Image::Format::R11 || a_pimage->GetFormat() == Image::Format::SIGNED_R11)
164 {
165 DrawBlockPixels(pblock, pargba8PngPixels, uiPngWidth, true);
166 }
167 else
168 {
169 DrawBlockPixels(pblock, pargba8PngPixels, uiPngWidth, false);
170 }
171 }
172 }
173
174 char strFilename[200];
175 if (a_boolDrawModes)
176 {
177 sprintf(strFilename, "%s%cModes.png", a_pstrOutputFolder, ETC_PATH_SLASH);
178 }
179 else
180 {
181 sprintf(strFilename, "%s%cDecoded.png", a_pstrOutputFolder, ETC_PATH_SLASH);
182 }
183
184 unsigned iResult = lodepng_encode32_file(strFilename, paucPngPixels, uiPngWidth, uiPngHeight);
185
186 if (iResult != 0)
187 {
188 if (a_boolDrawModes)
189 {
190 printf("Error couldn't write modes image (%s)\n", strFilename);
191 }
192 else
193 {
194 printf("Error couldn't write decoded image (%s)\n", strFilename);
195 }
196
197 exit(1);
198 }
199
200 }
201
202 // ----------------------------------------------------------------------------------------------------
203 //
204 void Analysis::DrawBlockPixels(Block4x4 *a_pblock,
205 ColorR8G8B8A8 *a_pargba8Output,
206 unsigned int a_uiOutputWidth,
207 bool a_bGrayscale,
208 unsigned int a_uiScale)
209 {
210
211 // output pixel coord of upper left corner of block
212 unsigned int uiBlockH = a_uiScale * a_pblock->GetSourceH();
213 unsigned int uiBlockV = a_uiScale * a_pblock->GetSourceV();
214
215 ColorR8G8B8A8 *pargba8Block = &a_pargba8Output[uiBlockV*a_uiOutputWidth + uiBlockH];
216 ColorFloatRGBA *pafrgbaDecodedColor = a_pblock->GetDecodedColors();
217 float *pafDecodedAlpha = a_pblock->GetDecodedAlphas();
218
219 for (unsigned int uiPixel = 0; uiPixel < Block4x4::PIXELS; uiPixel++)
220 {
221 ColorFloatRGBA *pfrgba = &pafrgbaDecodedColor[uiPixel];
222 int iR;
223 int iG;
224 int iB;
225 if (a_bGrayscale)
226 {
227 iR = pfrgba->IntRed(255.0f);
228 iG = pfrgba->IntRed(255.0f);
229 iB = pfrgba->IntRed(255.0f);
230 }
231 else
232 {
233 iR = pfrgba->IntRed(255.0f);
234 iG = pfrgba->IntGreen(255.0f);
235 iB = pfrgba->IntBlue(255.0f);
236 }
237
238 int iA = (int) roundf((255.0f*pafDecodedAlpha[uiPixel]));
239
240 ColorR8G8B8A8 *pargba8ScaledPixel = &pargba8Block[(a_uiScale * (uiPixel % 4))*a_uiOutputWidth +
241 a_uiScale * (uiPixel / 4)];
242
243 // draw scaled pixel
244 for (unsigned int uiV = 0; uiV < a_uiScale; uiV++)
245 {
246 for (unsigned int uiH = 0; uiH < a_uiScale; uiH++)
247 {
248 ColorR8G8B8A8 *prgba8 = &pargba8ScaledPixel[uiV*a_uiOutputWidth + uiH];
249
250 prgba8->ucR = (unsigned char)iR;
251 prgba8->ucG = (unsigned char)iG;
252 prgba8->ucB = (unsigned char)iB;
253 prgba8->ucA = (unsigned char)iA;
254 }
255 }
256 }
257
258 }
259
260 // ----------------------------------------------------------------------------------------------------
261 //
262 void Analysis::DrawBlockMode2x(Block4x4 *a_pblock,
263 ColorR8G8B8A8 *a_pargba8Output,
264 unsigned int a_uiOutputWidth)
265 {
266 static const unsigned int SCALE = 2;
267
268 typedef struct
269 {
270 int iH;
271 int iV;
272 } PixelCoord;
273
274 static const PixelCoord s_apixelcoordOutline[] = {
275 { 0,0 },{ 1,0 },{ 2,0 },{ 3,0 },{ 4,0 },{ 5,0 },{ 6,0 },{ 7,0 },
276 { 0,1 },{ 0,2 },{ 0,3 },{ 0,4 },{ 0,5 },{ 0,6 },{ 0,7 },{ -1,-1 }
277 };
278
279 static const PixelCoord s_apixelcoordFlip0[] =
280 {
281 { 4,2 },{ 4,4 },{ 4,6 },{ -1,-1 }
282 };
283
284 static const PixelCoord s_apixelcoordFlip1[] =
285 {
286 { 2,4 },{ 4,4 },{ 6,4 },{ -1,-1 }
287 };
288
289 static const PixelCoord s_apixelcoordBentDiffFlip0[] =
290 {
291 { 4,2 },{ 4,3 },{ 4,5 },{ 4,6 },{ -1,-1 }
292 };
293
294 static const PixelCoord s_apixelcoordBentDiffFlip1[] =
295 {
296 { 2,4 },{ 3,4 },{ 5,4 },{ 6,4 },{ -1,-1 }
297 };
298
299 static const PixelCoord s_apixelcoordIndividual[] =
300 {
301 { 1,1 },{ 1,7 },{ 7,1 },{ 7,7 },{ -1,-1 }
302 };
303
304 static const PixelCoord s_apixelcoordT[] =
305 {
306 { 3,2 },{ 5,2 },
307 { 4,2 },{ 4,3 },{ 4,4 },{ 4,5 },{ 4,6 },{ -1,-1 }
308 };
309
310 static const PixelCoord s_apixelcoordH[] =
311 {
312 { 4,4 },
313 { 3,2 },{ 3,3 },{ 3,4 },{ 3,5 },{ 3,6 },
314 { 5,2 },{ 5,3 },{ 5,4 },{ 5,5 },{ 5,6 },{ -1,-1 }
315 };
316
317 static const PixelCoord s_apixelcoordPlanar[] =
318 {
319 { 4,2 },{ 5,2 },{ 5,3 },{ 5,4 },{ 4,4 },
320 { 3,2 },{ 3,3 },{ 3,4 },{ 3,5 },{ 3,6 },{ -1,-1 }
321 };
322
323 static const PixelCoord s_apixelcoordUnknown[] =
324 {
325 { 3,2 },{ 4,2 },{ 5,2 },{ 5,3 },{ 5,4 },{ 4,4 },{ 4,6 },{ -1,-1 }
326 };
327
328 // output pixel coord of upper left corner of block
329 unsigned int uiBlockH = SCALE * a_pblock->GetSourceH();
330 unsigned int uiBlockV = SCALE * a_pblock->GetSourceV();
331
332 ColorR8G8B8A8 *pargba8Block = &a_pargba8Output[uiBlockV*a_uiOutputWidth + uiBlockH];
333 ColorR8G8B8A8 rgba8Gray;
334 rgba8Gray.ucR = 128;
335 rgba8Gray.ucG = 128;
336 rgba8Gray.ucB = 128;
337 rgba8Gray.ucA = 255;
338
339 // outline
340 for (const PixelCoord *pcoord = s_apixelcoordOutline; pcoord->iH >= 0; pcoord++)
341 {
342 ColorR8G8B8A8 *prgba8 = &pargba8Block[pcoord->iV*a_uiOutputWidth + pcoord->iH];
343 *prgba8 = rgba8Gray;
344 }
345
346 const PixelCoord *pacoordMode = nullptr;
347
348 switch (a_pblock->GetEncodingMode())
349 {
350 case Block4x4Encoding::MODE_ETC1:
351
352 // H/V split
353 pacoordMode = a_pblock->GetFlip() ? s_apixelcoordFlip1 : s_apixelcoordFlip0;
354
355 // individial
356 if (a_pblock->IsDifferential() == false)
357 {
358 for (const PixelCoord *pcoord = s_apixelcoordIndividual; pcoord->iH >= 0; pcoord++)
359 {
360 ColorR8G8B8A8 *prgba8 = &pargba8Block[pcoord->iV*a_uiOutputWidth + pcoord->iH];
361 *prgba8 = rgba8Gray;
362 }
363 }
364 else if (a_pblock->GetEncoding()->HasSeverelyBentDifferentialColors())
365 {
366 pacoordMode = a_pblock->GetFlip() ? s_apixelcoordBentDiffFlip1 : s_apixelcoordBentDiffFlip0;
367 }
368 break;
369
370 case Block4x4Encoding::MODE_T:
371 pacoordMode = s_apixelcoordT;
372 break;
373
374 case Block4x4Encoding::MODE_H:
375 pacoordMode = s_apixelcoordH;
376 break;
377
378 case Block4x4Encoding::MODE_PLANAR:
379 pacoordMode = s_apixelcoordPlanar;
380 break;
381
382 default:
383 pacoordMode = s_apixelcoordUnknown;
384 break;
385 }
386
387 // draw mode
388 for (const PixelCoord *pcoord = pacoordMode; pcoord->iH >= 0; pcoord++)
389 {
390 ColorR8G8B8A8 *prgba8 = &pargba8Block[pcoord->iV*a_uiOutputWidth + pcoord->iH];
391 *prgba8 = rgba8Gray;
392 }
393
394 }
395
396 // ----------------------------------------------------------------------------------------------------
397 //
398 float Analysis::ConvertErrorToPSNR(float a_fError, unsigned int a_uiTotalComponents)
399 {
400
401 float fMSE = a_fError / (float)a_uiTotalComponents;
402 float fPSNR = ConvertMSEToPSNR(fMSE);
403
404 return fPSNR;
405 }
406
407 // ----------------------------------------------------------------------------------------------------
408 //
409
410 }