| 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 |
#include "EtcSourceImage.h" |
| 23 |
#include "Etc.h" |
| 24 |
|
| 25 |
#if USE_STB_IMAGE_LOAD |
| 26 |
#include "stb_image.h" |
| 27 |
#endif |
| 28 |
|
| 29 |
#include <stdio.h> |
| 30 |
#include <stdlib.h> |
| 31 |
#include <string.h> |
| 32 |
#include <assert.h> |
| 33 |
|
| 34 |
#include "lodepng.h" |
| 35 |
|
| 36 |
namespace Etc |
| 37 |
{ |
| 38 |
|
| 39 |
// ---------------------------------------------------------------------------------------------------- |
| 40 |
// |
| 41 |
SourceImage::SourceImage(const char *a_pstrFilename, int a_iPixelX, int a_iPixelY) |
| 42 |
{ |
| 43 |
m_pstrFilename = nullptr; |
| 44 |
m_pstrName = nullptr; |
| 45 |
m_pstrFileExtension = nullptr; |
| 46 |
m_uiWidth = 0; |
| 47 |
m_uiHeight = 0; |
| 48 |
m_pafrgbaPixels = nullptr; |
| 49 |
|
| 50 |
SetName(a_pstrFilename); |
| 51 |
|
| 52 |
Read(a_iPixelX, a_iPixelY); |
| 53 |
} |
| 54 |
|
| 55 |
// ---------------------------------------------------------------------------------------------------- |
| 56 |
// |
| 57 |
SourceImage::SourceImage(ColorFloatRGBA *a_pafrgbaSource, |
| 58 |
unsigned int a_uiSourceWidth, |
| 59 |
unsigned int a_uiSourceHeight) |
| 60 |
{ |
| 61 |
m_pstrFilename = nullptr; |
| 62 |
m_pstrName = nullptr; |
| 63 |
m_pstrFileExtension = nullptr; |
| 64 |
m_uiWidth = a_uiSourceWidth; |
| 65 |
m_uiHeight = a_uiSourceHeight; |
| 66 |
m_pafrgbaPixels = a_pafrgbaSource; |
| 67 |
|
| 68 |
} |
| 69 |
// ---------------------------------------------------------------------------------------------------- |
| 70 |
// |
| 71 |
SourceImage::~SourceImage() |
| 72 |
{ |
| 73 |
if (m_pstrFilename != nullptr) |
| 74 |
{ |
| 75 |
delete[] m_pstrFilename; |
| 76 |
m_pstrFilename = nullptr; |
| 77 |
} |
| 78 |
|
| 79 |
if (m_pstrName != nullptr) |
| 80 |
{ |
| 81 |
delete[] m_pstrName; |
| 82 |
m_pstrName = nullptr; |
| 83 |
m_pstrFileExtension = nullptr; |
| 84 |
} |
| 85 |
|
| 86 |
if (m_pafrgbaPixels != nullptr) |
| 87 |
{ |
| 88 |
delete[] m_pafrgbaPixels; |
| 89 |
m_pafrgbaPixels = nullptr; |
| 90 |
} |
| 91 |
m_uiWidth = 0; |
| 92 |
m_uiHeight = 0; |
| 93 |
} |
| 94 |
// ---------------------------------------------------------------------------------------------------- |
| 95 |
// |
| 96 |
void SourceImage::Read(int a_iPixelX, int a_iPixelY) |
| 97 |
{ |
| 98 |
unsigned char* paucPixels = nullptr; |
| 99 |
|
| 100 |
int iWidth = 0; |
| 101 |
int iHeight = 0; |
| 102 |
bool bool16BitImage = false; |
| 103 |
|
| 104 |
#if USE_STB_IMAGE_LOAD |
| 105 |
int iBitsPerPixel; |
| 106 |
//if stb_iamge is available, only use it to load files other than png |
| 107 |
char *fileExt = strrchr(m_pstrFilename, '.'); |
| 108 |
|
| 109 |
if (strcmp(fileExt, ".png") != 0) |
| 110 |
{ |
| 111 |
paucPixels = stbi_load(m_pstrFilename, &iWidth, &iHeight, &iBitsPerPixel, 4); |
| 112 |
|
| 113 |
if (paucPixels == nullptr) |
| 114 |
{ |
| 115 |
printf("stb_image error %s\n", stbi_failure_reason()); |
| 116 |
assert(0); |
| 117 |
exit(1); |
| 118 |
} |
| 119 |
} |
| 120 |
#endif |
| 121 |
|
| 122 |
if (paucPixels == nullptr) |
| 123 |
{ |
| 124 |
//we can load 8 or 16 bit pngs |
| 125 |
int iBitDepth = 16; |
| 126 |
int error = lodepng_decode_file(&paucPixels, |
| 127 |
(unsigned int*)&iWidth, (unsigned int*)&iHeight, |
| 128 |
m_pstrFilename, |
| 129 |
LCT_RGBA, iBitDepth); |
| 130 |
|
| 131 |
bool16BitImage = (iBitDepth == 16) ? true : false; |
| 132 |
if (error) |
| 133 |
{ |
| 134 |
printf("lodePNG error %u: %s\n", error, lodepng_error_text(error)); |
| 135 |
assert(0); |
| 136 |
exit(1); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
//the pixel cords for the top left corner of the block |
| 141 |
int iBlockX = 0; |
| 142 |
int iBlockY = 0; |
| 143 |
if (a_iPixelX > -1 && a_iPixelY > -1) |
| 144 |
{ |
| 145 |
// in 1 block mode, we basically will have an img thats 4x4 |
| 146 |
m_uiWidth = 4; |
| 147 |
m_uiHeight = 4; |
| 148 |
|
| 149 |
if(a_iPixelX > iWidth) |
| 150 |
a_iPixelX = iWidth; |
| 151 |
if (a_iPixelY > iHeight) |
| 152 |
a_iPixelY = iHeight; |
| 153 |
|
| 154 |
// remove the bottom 2 bits to get the block coordinates |
| 155 |
iBlockX = (a_iPixelX & 0xFFFFFFFC); |
| 156 |
iBlockY = (a_iPixelY & 0xFFFFFFFC); |
| 157 |
} |
| 158 |
else |
| 159 |
{ |
| 160 |
m_uiWidth = iWidth; |
| 161 |
m_uiHeight = iHeight; |
| 162 |
} |
| 163 |
|
| 164 |
m_pafrgbaPixels = new ColorFloatRGBA[m_uiWidth * m_uiHeight]; |
| 165 |
assert(m_pafrgbaPixels); |
| 166 |
|
| 167 |
int iBytesPerPixel = bool16BitImage ? 8 : 4; |
| 168 |
unsigned char *pucPixel; // = &paucPixels[(iBlockY * iWidth + iBlockX) * iBytesPerPixel]; |
| 169 |
ColorFloatRGBA *pfrgbaPixel = m_pafrgbaPixels; |
| 170 |
|
| 171 |
// convert pixels from RGBA* to ColorFloatRGBA |
| 172 |
for (unsigned int uiV = iBlockY; uiV < (iBlockY+m_uiHeight); ++uiV) |
| 173 |
{ |
| 174 |
// reset coordinate for each row |
| 175 |
pucPixel = &paucPixels[(uiV * iWidth + iBlockX) * iBytesPerPixel]; |
| 176 |
|
| 177 |
// read each row |
| 178 |
for (unsigned int uiH = iBlockX; uiH < (iBlockX+m_uiWidth); ++uiH) |
| 179 |
{ |
| 180 |
if (bool16BitImage) |
| 181 |
{ |
| 182 |
unsigned short ushR = (pucPixel[0]<<8) + pucPixel[1]; |
| 183 |
unsigned short ushG = (pucPixel[2]<<8) + pucPixel[3]; |
| 184 |
unsigned short ushB = (pucPixel[4]<<8) + pucPixel[5]; |
| 185 |
unsigned short ushA = (pucPixel[6]<<8) + pucPixel[7]; |
| 186 |
|
| 187 |
*pfrgbaPixel++ = ColorFloatRGBA((float)ushR / 65535.0f, |
| 188 |
(float)ushG / 65535.0f, |
| 189 |
(float)ushB / 65535.0f, |
| 190 |
(float)ushA / 65535.0f); |
| 191 |
} |
| 192 |
else |
| 193 |
{ |
| 194 |
*pfrgbaPixel++ = ColorFloatRGBA::ConvertFromRGBA8(pucPixel[0], pucPixel[1], |
| 195 |
pucPixel[2], pucPixel[3]); |
| 196 |
} |
| 197 |
|
| 198 |
pucPixel += iBytesPerPixel; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
#if USE_STB_IMAGE_LOAD |
| 203 |
stbi_image_free(paucPixels); |
| 204 |
#else |
| 205 |
free(paucPixels); |
| 206 |
#endif |
| 207 |
} |
| 208 |
|
| 209 |
// ---------------------------------------------------------------------------------------------------- |
| 210 |
// sets m_pstrFilename, m_pstrName and m_pstrFileExtension |
| 211 |
// |
| 212 |
void SourceImage::SetName(const char *a_pstrFilename) |
| 213 |
{ |
| 214 |
if (a_pstrFilename == nullptr) |
| 215 |
{ |
| 216 |
return; |
| 217 |
} |
| 218 |
|
| 219 |
m_pstrFilename = new char[strlen(a_pstrFilename) + 1]; |
| 220 |
strcpy(m_pstrFilename, a_pstrFilename); |
| 221 |
|
| 222 |
m_pstrName = new char[strlen(m_pstrFilename) + 1]; |
| 223 |
|
| 224 |
// ignore directory path |
| 225 |
char *pcLastSlash = strrchr(m_pstrFilename, '/'); |
| 226 |
char *pcLastBackSlash = strrchr(m_pstrFilename, '\\'); |
| 227 |
if (pcLastSlash == nullptr && pcLastBackSlash == nullptr) |
| 228 |
{ |
| 229 |
strcpy(m_pstrName, m_pstrFilename); |
| 230 |
} |
| 231 |
else if (pcLastSlash > pcLastBackSlash) |
| 232 |
{ |
| 233 |
strcpy(m_pstrName, pcLastSlash + 1); |
| 234 |
} |
| 235 |
else |
| 236 |
{ |
| 237 |
strcpy(m_pstrName, pcLastBackSlash + 1); |
| 238 |
} |
| 239 |
|
| 240 |
// find file extension and remove it from image name |
| 241 |
char *pcLastPeriod = strrchr(m_pstrName, '.'); |
| 242 |
if (pcLastPeriod != nullptr) |
| 243 |
{ |
| 244 |
m_pstrFileExtension = pcLastPeriod + 1; |
| 245 |
*strrchr(m_pstrName, '.') = 0; |
| 246 |
} |
| 247 |
|
| 248 |
} |
| 249 |
|
| 250 |
// ---------------------------------------------------------------------------------------------------- |
| 251 |
// |
| 252 |
void SourceImage::NormalizeXYZ(void) |
| 253 |
{ |
| 254 |
int iPixels = m_uiWidth * m_uiHeight; |
| 255 |
|
| 256 |
ColorFloatRGBA *pfrgbaPixel = m_pafrgbaPixels; |
| 257 |
for (int iPixel = 0; iPixel < iPixels; iPixel++) |
| 258 |
{ |
| 259 |
float fX = 2.0f*pfrgbaPixel->fR - 1.0f; |
| 260 |
float fY = 2.0f*pfrgbaPixel->fG - 1.0f; |
| 261 |
float fZ = 2.0f*pfrgbaPixel->fB - 1.0f; |
| 262 |
|
| 263 |
float fLength2 = fX*fX + fY*fY + fZ*fZ; |
| 264 |
|
| 265 |
if (fLength2 == 0.0f) |
| 266 |
{ |
| 267 |
pfrgbaPixel->fR = 1.0f; |
| 268 |
pfrgbaPixel->fG = 0.0f; |
| 269 |
pfrgbaPixel->fB = 0.0f; |
| 270 |
} |
| 271 |
else |
| 272 |
{ |
| 273 |
float fLength = sqrtf(fLength2); |
| 274 |
|
| 275 |
float fNormalizedX = fX / fLength; |
| 276 |
float fNormalizedY = fY / fLength; |
| 277 |
float fNormalizedZ = fZ / fLength; |
| 278 |
|
| 279 |
pfrgbaPixel->fR = 0.5f * (fNormalizedX + 1.0f); |
| 280 |
pfrgbaPixel->fG = 0.5f * (fNormalizedY + 1.0f); |
| 281 |
pfrgbaPixel->fB = 0.5f * (fNormalizedZ + 1.0f); |
| 282 |
} |
| 283 |
|
| 284 |
pfrgbaPixel++; |
| 285 |
} |
| 286 |
|
| 287 |
} |
| 288 |
|
| 289 |
// ---------------------------------------------------------------------------------------------------- |
| 290 |
// |
| 291 |
|
| 292 |
} // namespace Etc |