| 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 "EtcConfig.h" |
| 22 |
|
| 23 |
#include "EtcComparison.h" |
| 24 |
|
| 25 |
#include "EtcAnalysis.h" |
| 26 |
#include "EtcTool.h" |
| 27 |
#include "Etc.h" |
| 28 |
#include "EtcFile.h" |
| 29 |
#include "EtcMath.h" |
| 30 |
#include "EtcImage.h" |
| 31 |
#include "EtcBlock4x4.h" |
| 32 |
#include "EtcBlock4x4Encoding_ETC1.h" |
| 33 |
#include "EtcBlock4x4Encoding_RGB8.h" |
| 34 |
#include "EtcBlock4x4Encoding_R11.h" |
| 35 |
#include "EtcBlock4x4Encoding_RG11.h" |
| 36 |
|
| 37 |
#include "lodepng.h" |
| 38 |
|
| 39 |
#include <stdlib.h> |
| 40 |
#include <string.h> |
| 41 |
#include <float.h> |
| 42 |
#include <limits> |
| 43 |
|
| 44 |
namespace Etc |
| 45 |
{ |
| 46 |
const float Comparison::ERROR_EPSILON = 0.000001f; |
| 47 |
|
| 48 |
// ---------------------------------------------------------------------------------------------------- |
| 49 |
// |
| 50 |
Comparison::Comparison(Analysis *a_panalysisParent, unsigned int a_uiIndex, const char *a_pstrFilename, int a_iPixelX, int a_iPixelY) |
| 51 |
{ |
| 52 |
|
| 53 |
m_panalysisParent = a_panalysisParent; |
| 54 |
m_uiIndex = a_uiIndex; |
| 55 |
m_pstrFilename = new char[strlen(a_pstrFilename)+1]; |
| 56 |
strcpy(m_pstrFilename, a_pstrFilename); |
| 57 |
|
| 58 |
SetName(); |
| 59 |
|
| 60 |
m_pstrOutputFolder = new char[256]; |
| 61 |
sprintf(m_pstrOutputFolder, "%s%cComparison_%s", |
| 62 |
m_panalysisParent->GetOutputFolder(), ETC_PATH_SLASH, m_pstrName); |
| 63 |
|
| 64 |
CreateNewDir(m_pstrOutputFolder); |
| 65 |
|
| 66 |
// read etc file |
| 67 |
Etc::File etcfile(a_pstrFilename, Etc::File::Format::INFER_FROM_FILE_EXTENSION); |
| 68 |
|
| 69 |
etcfile.UseSingleBlock(a_iPixelX, a_iPixelY); |
| 70 |
|
| 71 |
// construct image with encoding bits |
| 72 |
m_pimage = new Image(etcfile.GetImageFormat(), |
| 73 |
etcfile.GetSourceWidth(), |
| 74 |
etcfile.GetSourceHeight(), |
| 75 |
etcfile.GetEncodingBits(), |
| 76 |
etcfile.GetEncodingBitsBytes(), |
| 77 |
a_panalysisParent->GetImage(), |
| 78 |
a_panalysisParent->GetImage()->GetErrorMetric()); |
| 79 |
|
| 80 |
if (m_pimage->GetExtendedWidth() != a_panalysisParent->GetImage()->GetExtendedWidth() || |
| 81 |
m_pimage->GetExtendedHeight() != a_panalysisParent->GetImage()->GetExtendedHeight()) |
| 82 |
{ |
| 83 |
printf("Error: comparison image (%s) has different width or height\n", m_pstrFilename); |
| 84 |
exit(1); |
| 85 |
} |
| 86 |
|
| 87 |
// scale = 1 |
| 88 |
Analysis::DrawImage(m_pimage, m_pstrOutputFolder, false); |
| 89 |
|
| 90 |
// scale = 2, with modes |
| 91 |
Analysis::DrawImage(m_pimage, m_pstrOutputFolder, true); |
| 92 |
|
| 93 |
DrawImageComparison(m_panalysisParent->GetImage()); |
| 94 |
|
| 95 |
WriteLogFile(); |
| 96 |
|
| 97 |
} |
| 98 |
|
| 99 |
// ---------------------------------------------------------------------------------------------------- |
| 100 |
// |
| 101 |
void Comparison::DrawImageComparison(Image *a_pimageUnderTest) |
| 102 |
{ |
| 103 |
|
| 104 |
assert(m_pimage->GetExtendedWidth() == a_pimageUnderTest->GetExtendedWidth() && |
| 105 |
m_pimage->GetExtendedHeight() == a_pimageUnderTest->GetExtendedHeight()); |
| 106 |
|
| 107 |
unsigned int uiPngWidth = 2 * m_pimage->GetExtendedWidth(); |
| 108 |
unsigned int uiPngHeight = 2 * m_pimage->GetExtendedHeight(); |
| 109 |
|
| 110 |
unsigned char* paucPngPixels = new unsigned char[uiPngWidth * uiPngHeight * 4]; |
| 111 |
assert(paucPngPixels); |
| 112 |
|
| 113 |
ColorR8G8B8A8 *pargba8PngPixels = (ColorR8G8B8A8 *)paucPngPixels; |
| 114 |
|
| 115 |
for (unsigned int uiBlock = 0; uiBlock < m_pimage->GetNumberOfBlocks(); uiBlock++) |
| 116 |
{ |
| 117 |
Block4x4 *pblock = &m_pimage->GetBlocks()[uiBlock]; |
| 118 |
Block4x4 *pblockUnderTest = &a_pimageUnderTest->GetBlocks()[uiBlock]; |
| 119 |
|
| 120 |
if (a_pimageUnderTest->GetFormat() == Image::Format::R11 || a_pimageUnderTest->GetFormat() == Image::Format::SIGNED_R11) |
| 121 |
{ |
| 122 |
Analysis::DrawBlockPixels(pblock, pargba8PngPixels, uiPngWidth, true, 2); |
| 123 |
} |
| 124 |
else |
| 125 |
{ |
| 126 |
Analysis::DrawBlockPixels(pblock, pargba8PngPixels, uiPngWidth, false, 2); |
| 127 |
} |
| 128 |
|
| 129 |
float fErrorBlockReference = pblock->GetError(); |
| 130 |
float fErrorBlockUnderTest = pblockUnderTest->GetError(); |
| 131 |
|
| 132 |
DrawBlockComparison2x(pblockUnderTest, pargba8PngPixels, uiPngWidth, |
| 133 |
fErrorBlockUnderTest, fErrorBlockReference); |
| 134 |
} |
| 135 |
|
| 136 |
char strFilename[200]; |
| 137 |
sprintf(strFilename, "%s%cComparison.png", m_pstrOutputFolder, ETC_PATH_SLASH); |
| 138 |
|
| 139 |
unsigned iResult = lodepng_encode32_file(strFilename, paucPngPixels, uiPngWidth, uiPngHeight); |
| 140 |
|
| 141 |
if (iResult != 0) |
| 142 |
{ |
| 143 |
printf("Error couldn't write modes image (%s)\n", strFilename); |
| 144 |
} |
| 145 |
|
| 146 |
} |
| 147 |
|
| 148 |
// ---------------------------------------------------------------------------------------------------- |
| 149 |
// |
| 150 |
void Comparison::DrawBlockComparison2x(Block4x4 *a_pblockUnderTest, |
| 151 |
ColorR8G8B8A8 *a_pargba8Output, |
| 152 |
unsigned int a_uiOutputWidth, |
| 153 |
float a_fErrorBlockUnderTest, |
| 154 |
float a_fErrorBlockReference) |
| 155 |
{ |
| 156 |
static const unsigned int SCALE = 2; |
| 157 |
|
| 158 |
typedef struct |
| 159 |
{ |
| 160 |
int iH; |
| 161 |
int iV; |
| 162 |
} PixelCoord; |
| 163 |
|
| 164 |
static const PixelCoord s_apixelcoordDifference0[] = { |
| 165 |
{ 0,0 },{ 1,0 },{ 2,0 },{ 3,0 },{ 4,0 },{ 5,0 },{ 6,0 },{ 7,0 }, |
| 166 |
{ 0,1 },{ 0,2 },{ 0,3 },{ 0,4 },{ 0,5 },{ 0,6 },{ 0,7 }, |
| 167 |
{ 1,7 },{ 2,7 },{ 3,7 },{ 4,7 },{ 5,7 },{ 6,7 },{ 7,7 }, |
| 168 |
{ 7,1 },{ 7,2 },{ 7,3 },{ 7,4 },{ 7,5 },{ 7,6 },{ -1,-1 } |
| 169 |
}; |
| 170 |
|
| 171 |
static const PixelCoord s_apixelcoordDifference5[] = { |
| 172 |
{ 3,3 }, |
| 173 |
{ 0,0 },{ 1,0 },{ 2,0 },{ 3,0 },{ 4,0 },{ 5,0 },{ 6,0 },{ 7,0 }, |
| 174 |
{ 0,1 },{ 0,2 },{ 0,3 },{ 0,4 },{ 0,5 },{ 0,6 },{ 0,7 }, |
| 175 |
{ 1,7 },{ 2,7 },{ 3,7 },{ 4,7 },{ 5,7 },{ 6,7 },{ 7,7 }, |
| 176 |
{ 7,1 },{ 7,2 },{ 7,3 },{ 7,4 },{ 7,5 },{ 7,6 },{ -1,-1 } |
| 177 |
}; |
| 178 |
|
| 179 |
static const PixelCoord s_apixelcoordDifference10[] = { |
| 180 |
{ 3,3 },{ 3,4 },{ 4,3 },{ 4,4 }, |
| 181 |
{ 0,0 },{ 1,0 },{ 2,0 },{ 3,0 },{ 4,0 },{ 5,0 },{ 6,0 },{ 7,0 }, |
| 182 |
{ 0,1 },{ 0,2 },{ 0,3 },{ 0,4 },{ 0,5 },{ 0,6 },{ 0,7 }, |
| 183 |
{ 1,7 },{ 2,7 },{ 3,7 },{ 4,7 },{ 5,7 },{ 6,7 },{ 7,7 }, |
| 184 |
{ 7,1 },{ 7,2 },{ 7,3 },{ 7,4 },{ 7,5 },{ 7,6 },{ -1,-1 } |
| 185 |
}; |
| 186 |
|
| 187 |
static const PixelCoord s_apixelcoordDifference20[] = { |
| 188 |
{ 2,2 },{ 2,3 },{ 3,2 },{ 3,3 },{ 4,4 },{ 4,5 },{ 5,4 },{ 5,5 }, |
| 189 |
{ 0,0 },{ 1,0 },{ 2,0 },{ 3,0 },{ 4,0 },{ 5,0 },{ 6,0 },{ 7,0 }, |
| 190 |
{ 0,1 },{ 0,2 },{ 0,3 },{ 0,4 },{ 0,5 },{ 0,6 },{ 0,7 }, |
| 191 |
{ 1,7 },{ 2,7 },{ 3,7 },{ 4,7 },{ 5,7 },{ 6,7 },{ 7,7 }, |
| 192 |
{ 7,1 },{ 7,2 },{ 7,3 },{ 7,4 },{ 7,5 },{ 7,6 },{ -1,-1 } |
| 193 |
}; |
| 194 |
|
| 195 |
static const PixelCoord s_apixelcoordCorners[] = { |
| 196 |
{ 0,0 },{ 7,0 },{ 0,7 },{ 7,7 },{ -1,-1 } |
| 197 |
}; |
| 198 |
|
| 199 |
// output pixel coord of upper left corner of block |
| 200 |
unsigned int uiBlockH = SCALE * a_pblockUnderTest->GetSourceH(); |
| 201 |
unsigned int uiBlockV = SCALE * a_pblockUnderTest->GetSourceV(); |
| 202 |
|
| 203 |
ColorR8G8B8A8 *pargba8Block = &a_pargba8Output[uiBlockV*a_uiOutputWidth + uiBlockH]; |
| 204 |
|
| 205 |
float fRelativeError = 0.0f; |
| 206 |
if (a_fErrorBlockUnderTest != a_fErrorBlockReference) |
| 207 |
{ |
| 208 |
fRelativeError = fabs((a_fErrorBlockUnderTest - a_fErrorBlockReference) / |
| 209 |
(a_fErrorBlockUnderTest + a_fErrorBlockReference)); |
| 210 |
|
| 211 |
if (fabsf(a_fErrorBlockUnderTest - a_fErrorBlockReference) < ERROR_EPSILON) |
| 212 |
{ |
| 213 |
fRelativeError = 0.0f; |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
ColorR8G8B8A8 rgb8Draw; |
| 218 |
rgb8Draw.ucA = 255; |
| 219 |
|
| 220 |
// equal |
| 221 |
if (fRelativeError == 0.0f) |
| 222 |
{ |
| 223 |
rgb8Draw.ucR = 128; |
| 224 |
rgb8Draw.ucG = 128; |
| 225 |
rgb8Draw.ucB = 128; |
| 226 |
} |
| 227 |
// better tthan reference |
| 228 |
else if (a_fErrorBlockUnderTest < a_fErrorBlockReference) |
| 229 |
{ |
| 230 |
rgb8Draw.ucR = 0; |
| 231 |
rgb8Draw.ucG = 255; |
| 232 |
rgb8Draw.ucB = 0; |
| 233 |
} |
| 234 |
// worse than reference |
| 235 |
else if (a_fErrorBlockUnderTest > a_fErrorBlockReference) |
| 236 |
{ |
| 237 |
rgb8Draw.ucR = 255; |
| 238 |
rgb8Draw.ucG = 0; |
| 239 |
rgb8Draw.ucB = 0; |
| 240 |
} |
| 241 |
|
| 242 |
const PixelCoord *papixelcoordDraw = s_apixelcoordCorners; |
| 243 |
|
| 244 |
// if 20% worse |
| 245 |
if (fRelativeError >= 1.44f) |
| 246 |
{ |
| 247 |
papixelcoordDraw = s_apixelcoordDifference20; |
| 248 |
} |
| 249 |
// if 10% worse |
| 250 |
else if (fRelativeError >= 1.21f) |
| 251 |
{ |
| 252 |
papixelcoordDraw = s_apixelcoordDifference10; |
| 253 |
} |
| 254 |
// if 5% worse |
| 255 |
else if (fRelativeError >= 1.1025f) |
| 256 |
{ |
| 257 |
papixelcoordDraw = s_apixelcoordDifference5; |
| 258 |
} |
| 259 |
else if (fRelativeError > 0.0f) |
| 260 |
{ |
| 261 |
papixelcoordDraw = s_apixelcoordDifference0; |
| 262 |
} |
| 263 |
|
| 264 |
// outline |
| 265 |
for (const PixelCoord *pcoord = papixelcoordDraw; pcoord->iH >= 0; pcoord++) |
| 266 |
{ |
| 267 |
ColorR8G8B8A8 *prgba8 = &pargba8Block[pcoord->iV*a_uiOutputWidth + pcoord->iH]; |
| 268 |
|
| 269 |
*prgba8 = rgb8Draw; |
| 270 |
} |
| 271 |
|
| 272 |
} |
| 273 |
|
| 274 |
// ---------------------------------------------------------------------------------------------------- |
| 275 |
// |
| 276 |
void Comparison::SetName(void) |
| 277 |
{ |
| 278 |
// alloc memory for name |
| 279 |
unsigned int uiNameBytes = (unsigned int)strlen(m_pstrFilename) + 1; |
| 280 |
if (uiNameBytes < 4) |
| 281 |
{ |
| 282 |
uiNameBytes = 4; |
| 283 |
} |
| 284 |
m_pstrName = new char[uiNameBytes]; |
| 285 |
m_pstrName[0] = 0; |
| 286 |
|
| 287 |
// first, try to find folder name of comparison image |
| 288 |
{ |
| 289 |
char *pstr = new char[strlen(m_pstrFilename) + 1]; |
| 290 |
strcpy(pstr, m_pstrFilename); |
| 291 |
|
| 292 |
int iLastSlash; |
| 293 |
int iPenultimateSlash; |
| 294 |
|
| 295 |
// find last slash |
| 296 |
for (iLastSlash = (int)strlen(pstr) - 1; iLastSlash >= 0; iLastSlash--) |
| 297 |
{ |
| 298 |
if (pstr[iLastSlash] == ETC_PATH_SLASH) |
| 299 |
{ |
| 300 |
break; |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
// find penultimate slash |
| 305 |
for (iPenultimateSlash = iLastSlash - 1; iPenultimateSlash >= 0; iPenultimateSlash--) |
| 306 |
{ |
| 307 |
if (pstr[iPenultimateSlash] == ETC_PATH_SLASH) |
| 308 |
{ |
| 309 |
break; |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
if (iLastSlash > 0) |
| 314 |
{ |
| 315 |
pstr[iLastSlash] = 0; |
| 316 |
strcpy(m_pstrName, &pstr[iPenultimateSlash] + 1); |
| 317 |
assert(strlen(m_pstrName) > 0); |
| 318 |
} |
| 319 |
|
| 320 |
delete[] pstr; |
| 321 |
} |
| 322 |
|
| 323 |
// otherwise use index as name |
| 324 |
if (m_pstrName[0] == 0) |
| 325 |
{ |
| 326 |
sprintf(m_pstrName, "%u", m_uiIndex); |
| 327 |
} |
| 328 |
|
| 329 |
} |
| 330 |
|
| 331 |
// ---------------------------------------------------------------------------------------------------- |
| 332 |
// |
| 333 |
void Comparison::WriteLogFile(void) |
| 334 |
{ |
| 335 |
|
| 336 |
char strPath[200]; |
| 337 |
sprintf(strPath, "%s%cComparison.txt", m_pstrOutputFolder, ETC_PATH_SLASH); |
| 338 |
|
| 339 |
FILE *pfile = fopen(strPath, "wt"); |
| 340 |
if (pfile == nullptr) |
| 341 |
{ |
| 342 |
printf("Error couldn't open comparison log file (%s)\n", strPath); |
| 343 |
exit(1); |
| 344 |
} |
| 345 |
|
| 346 |
float fImageError = m_panalysisParent->GetImage()->GetError(); |
| 347 |
unsigned int uiImagePixels = m_panalysisParent->GetImage()->GetSourceWidth() * |
| 348 |
m_panalysisParent->GetImage()->GetSourceHeight(); |
| 349 |
|
| 350 |
if (m_pimage->GetFormat() == Image::Format::R11 || m_pimage->GetFormat() == Image::Format::SIGNED_R11) |
| 351 |
{ |
| 352 |
fprintf(pfile, "PSNR(r) = %.4f\n", Analysis::ConvertErrorToPSNR(fImageError, 1 * uiImagePixels)); |
| 353 |
fprintf(pfile, "\n"); |
| 354 |
} |
| 355 |
else if (m_pimage->GetFormat() == Image::Format::RG11 || m_pimage->GetFormat() == Image::Format::SIGNED_RG11) |
| 356 |
{ |
| 357 |
fprintf(pfile, "PSNR(rg) = %.4f\n", Analysis::ConvertErrorToPSNR(fImageError, 2 * uiImagePixels)); |
| 358 |
fprintf(pfile, "\n"); |
| 359 |
} |
| 360 |
else |
| 361 |
{ |
| 362 |
fprintf(pfile, "PSNR(rgb) = %.4f\n", Analysis::ConvertErrorToPSNR(fImageError, 3 * uiImagePixels)); |
| 363 |
fprintf(pfile, "PSNR(rgba) = %.4f\n", Analysis::ConvertErrorToPSNR(fImageError, 4 * uiImagePixels)); |
| 364 |
fprintf(pfile, "\n"); |
| 365 |
} |
| 366 |
float fReferenceImageError = m_pimage->GetError(); |
| 367 |
|
| 368 |
fprintf(pfile, "reference image %s\n", m_pstrFilename); |
| 369 |
if (m_pimage->GetFormat() == Image::Format::R11 || m_pimage->GetFormat() == Image::Format::SIGNED_R11) |
| 370 |
{ |
| 371 |
fprintf(pfile, "reference PSNR(r) = %.4f\n", |
| 372 |
Analysis::ConvertErrorToPSNR(fReferenceImageError, 1 * uiImagePixels)); |
| 373 |
} |
| 374 |
else if (m_pimage->GetFormat() == Image::Format::RG11 || m_pimage->GetFormat() == Image::Format::SIGNED_RG11) |
| 375 |
{ |
| 376 |
fprintf(pfile, "reference PSNR(rg) = %.4f\n", |
| 377 |
Analysis::ConvertErrorToPSNR(fReferenceImageError, 2 * uiImagePixels)); |
| 378 |
} |
| 379 |
else |
| 380 |
{ |
| 381 |
fprintf(pfile, "reference PSNR(rgb) = %.4f\n", |
| 382 |
Analysis::ConvertErrorToPSNR(fReferenceImageError, 3 * uiImagePixels)); |
| 383 |
fprintf(pfile, "reference PSNR(rgba) = %.4f\n", |
| 384 |
Analysis::ConvertErrorToPSNR(fReferenceImageError, 4 * uiImagePixels)); |
| 385 |
} |
| 386 |
WriteBetterOrWorseBlocks(pfile, false); |
| 387 |
WriteBetterOrWorseBlocks(pfile, true); |
| 388 |
|
| 389 |
fclose(pfile); |
| 390 |
|
| 391 |
} |
| 392 |
|
| 393 |
// ---------------------------------------------------------------------------------------------------- |
| 394 |
// |
| 395 |
void Comparison::WriteBetterOrWorseBlocks(FILE *a_pfile, bool a_boolWorse) |
| 396 |
{ |
| 397 |
|
| 398 |
fprintf(a_pfile, "\n"); |
| 399 |
if (a_boolWorse) |
| 400 |
{ |
| 401 |
fprintf(a_pfile, "Worse Blocks\n"); |
| 402 |
} |
| 403 |
else |
| 404 |
{ |
| 405 |
fprintf(a_pfile, "Better Blocks\n"); |
| 406 |
} |
| 407 |
|
| 408 |
for (unsigned int uiBlock = 0; uiBlock < m_pimage->GetNumberOfBlocks(); uiBlock++) |
| 409 |
{ |
| 410 |
Block4x4 *pblockReference = &m_pimage->GetBlocks()[uiBlock]; |
| 411 |
Block4x4 *pblockUnderTest = &m_panalysisParent->GetImage()->GetBlocks()[uiBlock]; |
| 412 |
|
| 413 |
float fErrorBlockReference = pblockReference->GetError(); |
| 414 |
float fErrorBlockUnderTest = pblockUnderTest->GetError(); |
| 415 |
|
| 416 |
if (fabsf(fErrorBlockUnderTest - fErrorBlockReference) >= ERROR_EPSILON) |
| 417 |
{ |
| 418 |
if ((a_boolWorse && (fErrorBlockUnderTest > fErrorBlockReference)) || |
| 419 |
(!a_boolWorse && (fErrorBlockUnderTest < fErrorBlockReference))) |
| 420 |
{ |
| 421 |
fprintf(a_pfile, "HV=%u,%u\n", |
| 422 |
pblockUnderTest->GetSourceH(), pblockUnderTest->GetSourceV()); |
| 423 |
|
| 424 |
WriteBlockInfo(a_pfile, pblockUnderTest, "bsi"); |
| 425 |
WriteBlockInfo(a_pfile, pblockReference, "ref"); |
| 426 |
|
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
} |
| 431 |
|
| 432 |
} |
| 433 |
|
| 434 |
// ---------------------------------------------------------------------------------------------------- |
| 435 |
// |
| 436 |
void Comparison::WriteBlockInfo(FILE *a_pfile, Block4x4 *a_pblock, const char *a_pstrEncoder) |
| 437 |
{ |
| 438 |
|
| 439 |
Block4x4Encoding *pencoding = a_pblock->GetEncoding(); |
| 440 |
|
| 441 |
Block4x4Encoding_R11 *pencoding_r11 = static_cast<Block4x4Encoding_R11 *>(pencoding); |
| 442 |
Block4x4Encoding_RG11 *pencoding_rg11 = static_cast<Block4x4Encoding_RG11 *>(pencoding); |
| 443 |
Block4x4Encoding_RGB8 *pencoding_rgb8 = static_cast<Block4x4Encoding_RGB8 *>(pencoding); |
| 444 |
|
| 445 |
fprintf(a_pfile, " %s:", a_pstrEncoder); |
| 446 |
|
| 447 |
float fPSNR = 0.0f; |
| 448 |
if (m_pimage->GetFormat() == Image::Format::R11 || m_pimage->GetFormat() == Image::Format::SIGNED_R11) |
| 449 |
{ |
| 450 |
fPSNR = Analysis::ConvertErrorToPSNR(a_pblock->GetError(), 1 * 16); |
| 451 |
} |
| 452 |
else if (m_pimage->GetFormat() == Image::Format::RG11 || m_pimage->GetFormat() == Image::Format::SIGNED_RG11) |
| 453 |
{ |
| 454 |
fPSNR = Analysis::ConvertErrorToPSNR(a_pblock->GetError(), 2 * 16); |
| 455 |
} |
| 456 |
else |
| 457 |
{ |
| 458 |
fPSNR = Analysis::ConvertErrorToPSNR(a_pblock->GetError(), 3 * 16); |
| 459 |
} |
| 460 |
if (a_pblock->GetEncodingMode() == Block4x4Encoding::MODE_RG11) |
| 461 |
{ |
| 462 |
fprintf(a_pfile, "psnr=%6.3f\n", fPSNR); |
| 463 |
} |
| 464 |
else |
| 465 |
{ |
| 466 |
fprintf(a_pfile, "psnr=%6.3f ", fPSNR); |
| 467 |
} |
| 468 |
bool boolWriteSelectors = false; |
| 469 |
|
| 470 |
if (m_pimage->GetFormat() == Image::Format::RGBA8) |
| 471 |
{ |
| 472 |
fprintf(a_pfile, "RGBA8 "); |
| 473 |
fprintf(a_pfile, "alpha base(%4.0f) ", pencoding_r11->GetRedBase()); |
| 474 |
fprintf(a_pfile, "alpha multiplier(%3.0f) ", pencoding_r11->GetRedMultiplier()); |
| 475 |
fprintf(a_pfile, "alpha table index(%2d) ", pencoding_r11->GetRedTableIndex()); |
| 476 |
} |
| 477 |
if (a_pblock->GetEncodingMode() == Block4x4Encoding::MODE_ETC1) |
| 478 |
{ |
| 479 |
fprintf(a_pfile, "ETC1(%c,%c) ", |
| 480 |
a_pblock->IsDifferential() ? 'D' : 'I', |
| 481 |
a_pblock->GetFlip() ? 'V' : 'H' ); |
| 482 |
|
| 483 |
if (a_pblock->IsDifferential()) |
| 484 |
{ |
| 485 |
fprintf(a_pfile, "color1(%2d,%2d,%2d) ", |
| 486 |
pencoding_rgb8->GetColor1().IntRed(31.0f), |
| 487 |
pencoding_rgb8->GetColor1().IntGreen(31.0f), |
| 488 |
pencoding_rgb8->GetColor1().IntBlue(31.0f) ); |
| 489 |
fprintf(a_pfile, "color2(%2d,%2d,%2d) ", |
| 490 |
pencoding_rgb8->GetColor2().IntRed(31.0f), |
| 491 |
pencoding_rgb8->GetColor2().IntGreen(31.0f), |
| 492 |
pencoding_rgb8->GetColor2().IntBlue(31.0f)); |
| 493 |
} |
| 494 |
else |
| 495 |
{ |
| 496 |
fprintf(a_pfile, "color1(%2d,%2d,%2d) ", |
| 497 |
pencoding_rgb8->GetColor1().IntRed(15.0f), |
| 498 |
pencoding_rgb8->GetColor1().IntGreen(15.0f), |
| 499 |
pencoding_rgb8->GetColor1().IntBlue(15.0f)); |
| 500 |
fprintf(a_pfile, "color2(%2d,%2d,%2d) ", |
| 501 |
pencoding_rgb8->GetColor2().IntRed(15.0f), |
| 502 |
pencoding_rgb8->GetColor2().IntGreen(15.0f), |
| 503 |
pencoding_rgb8->GetColor2().IntBlue(15.0f)); |
| 504 |
} |
| 505 |
|
| 506 |
fprintf(a_pfile, "cw1(%d) cw2(%d) ", pencoding_rgb8->GetCW1(), pencoding_rgb8->GetCW2()); |
| 507 |
|
| 508 |
boolWriteSelectors = true; |
| 509 |
} |
| 510 |
else if (a_pblock->GetEncodingMode() == Block4x4Encoding::MODE_T || |
| 511 |
a_pblock->GetEncodingMode() == Block4x4Encoding::MODE_H) |
| 512 |
{ |
| 513 |
fprintf(a_pfile, "%-9s ", a_pblock->GetEncodingModeName()); |
| 514 |
|
| 515 |
fprintf(a_pfile, "color1(%2d,%2d,%2d) ", |
| 516 |
pencoding_rgb8->GetColor1().IntRed(15.0f), |
| 517 |
pencoding_rgb8->GetColor1().IntGreen(15.0f), |
| 518 |
pencoding_rgb8->GetColor1().IntBlue(15.0f)); |
| 519 |
fprintf(a_pfile, "color2(%2d,%2d,%2d) ", |
| 520 |
pencoding_rgb8->GetColor2().IntRed(15.0f), |
| 521 |
pencoding_rgb8->GetColor2().IntGreen(15.0f), |
| 522 |
pencoding_rgb8->GetColor2().IntBlue(15.0f)); |
| 523 |
|
| 524 |
fprintf(a_pfile, "cw1(%d) ", pencoding_rgb8->GetCW1() ); |
| 525 |
|
| 526 |
boolWriteSelectors = true; |
| 527 |
} |
| 528 |
else if (a_pblock->GetEncodingMode() == Block4x4Encoding::MODE_PLANAR) |
| 529 |
{ |
| 530 |
fprintf(a_pfile, "%-9s ", a_pblock->GetEncodingModeName()); |
| 531 |
|
| 532 |
fprintf(a_pfile, "color1(%2d,%2d,%2d) ", |
| 533 |
pencoding_rgb8->GetColor1().IntRed(63.0f), |
| 534 |
pencoding_rgb8->GetColor1().IntGreen(127.0f), |
| 535 |
pencoding_rgb8->GetColor1().IntBlue(63.0f)); |
| 536 |
fprintf(a_pfile, "color2(%2d,%2d,%2d) ", |
| 537 |
pencoding_rgb8->GetColor2().IntRed(63.0f), |
| 538 |
pencoding_rgb8->GetColor2().IntGreen(127.0f), |
| 539 |
pencoding_rgb8->GetColor2().IntBlue(63.0f)); |
| 540 |
fprintf(a_pfile, "color3(%2d,%2d,%2d) ", |
| 541 |
pencoding_rgb8->GetColor3().IntRed(63.0f), |
| 542 |
pencoding_rgb8->GetColor3().IntGreen(127.0f), |
| 543 |
pencoding_rgb8->GetColor3().IntBlue(63.0f)); |
| 544 |
} |
| 545 |
else if (a_pblock->GetEncodingMode() == Block4x4Encoding::MODE_R11) |
| 546 |
{ |
| 547 |
fprintf(a_pfile, "R11 "); |
| 548 |
fprintf(a_pfile, "base(%4.0f) ", pencoding_r11->GetRedBase()); |
| 549 |
fprintf(a_pfile, "multiplier(%3.0f) ", pencoding_r11->GetRedMultiplier()); |
| 550 |
fprintf(a_pfile, "table index(%2d) ", pencoding_r11->GetRedTableIndex()); |
| 551 |
|
| 552 |
boolWriteSelectors = true; |
| 553 |
} |
| 554 |
else if (a_pblock->GetEncodingMode() == Block4x4Encoding::MODE_RG11) |
| 555 |
{ |
| 556 |
fprintf(a_pfile, "RG11 image: %s\n", a_pstrEncoder); |
| 557 |
fprintf(a_pfile, "red base(%4.0f)\n", pencoding_rg11->GetRedBase()); |
| 558 |
fprintf(a_pfile, "grn base(%4.0f)\n\n", pencoding_rg11->GetGrnBase()); |
| 559 |
|
| 560 |
fprintf(a_pfile, "red multiplier(%3.0f)\n", pencoding_rg11->GetRedMultiplier()); |
| 561 |
fprintf(a_pfile, "grn multiplier(%3.0f)\n\n", pencoding_rg11->GetGrnMultiplier()); |
| 562 |
|
| 563 |
fprintf(a_pfile, "red table index(%2d)\n", pencoding_rg11->GetRedTableIndex()); |
| 564 |
fprintf(a_pfile, "grn table index(%2d)\n\n", pencoding_rg11->GetGrnTableIndex()); |
| 565 |
|
| 566 |
boolWriteSelectors = true; |
| 567 |
} |
| 568 |
else |
| 569 |
{ |
| 570 |
assert(0); |
| 571 |
} |
| 572 |
|
| 573 |
if (boolWriteSelectors) |
| 574 |
{ |
| 575 |
size_t selectorStringSize = Block4x4Encoding::PIXELS*4; |
| 576 |
char *redSelectors = new char[selectorStringSize]; |
| 577 |
char *grnSelectors = new char[selectorStringSize]; |
| 578 |
memset(&redSelectors[0], 0, selectorStringSize); |
| 579 |
memset(&grnSelectors[0], 0, selectorStringSize); |
| 580 |
|
| 581 |
if (a_pblock->GetEncodingMode() != Block4x4Encoding::MODE_RG11) |
| 582 |
{ |
| 583 |
fprintf(a_pfile, "selectors("); |
| 584 |
} |
| 585 |
|
| 586 |
for (unsigned int uiPixel = 0; uiPixel < Block4x4Encoding::PIXELS; uiPixel++) |
| 587 |
{ |
| 588 |
if (a_pblock->GetEncodingMode() == Block4x4Encoding::MODE_R11) |
| 589 |
{ |
| 590 |
fprintf(a_pfile, "%u", pencoding_r11->GetRedSelectors()[uiPixel]); |
| 591 |
} |
| 592 |
else if (a_pblock->GetEncodingMode() == Block4x4Encoding::MODE_RG11) |
| 593 |
{ |
| 594 |
char temp[2]; |
| 595 |
sprintf(temp,"%d",pencoding_rg11->GetRedSelectors()[uiPixel]); |
| 596 |
strcat(redSelectors,temp); |
| 597 |
sprintf(temp, "%d", pencoding_rg11->GetGrnSelectors()[uiPixel]); |
| 598 |
strcat(grnSelectors, temp); |
| 599 |
|
| 600 |
strcat(redSelectors, ",\0"); |
| 601 |
strcat(grnSelectors, ",\0"); |
| 602 |
|
| 603 |
fprintf(a_pfile, "pixel[%d]:(%f,%f,%f)\n", uiPixel, |
| 604 |
pencoding->GetDecodedColors()[uiPixel].fR, |
| 605 |
pencoding->GetDecodedColors()[uiPixel].fG, |
| 606 |
pencoding->GetDecodedColors()[uiPixel].fB); |
| 607 |
|
| 608 |
} |
| 609 |
else |
| 610 |
{ |
| 611 |
fprintf(a_pfile, "%u", pencoding_rgb8->GetSelectors()[uiPixel]); |
| 612 |
} |
| 613 |
} |
| 614 |
if (a_pblock->GetEncodingMode() == Block4x4Encoding::MODE_RG11) |
| 615 |
{ |
| 616 |
redSelectors[selectorStringSize - 1] = '\0'; |
| 617 |
grnSelectors[selectorStringSize - 1] = '\0'; |
| 618 |
fprintf(a_pfile, "selectors(red: %s)\n", redSelectors); |
| 619 |
fprintf(a_pfile, "selectors(grn: %s)\n,", grnSelectors); |
| 620 |
delete[] redSelectors; |
| 621 |
redSelectors = NULL; |
| 622 |
delete[] grnSelectors; |
| 623 |
grnSelectors = NULL; |
| 624 |
} |
| 625 |
else |
| 626 |
{ |
| 627 |
fprintf(a_pfile, ")"); |
| 628 |
} |
| 629 |
} |
| 630 |
|
| 631 |
fprintf(a_pfile, "\n"); |
| 632 |
|
| 633 |
} |
| 634 |
|
| 635 |
// ---------------------------------------------------------------------------------------------------- |
| 636 |
// |
| 637 |
} |