| 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 |
#pragma once |
| 18 |
|
| 19 |
//#include "Etc.h" |
| 20 |
#include "EtcColorFloatRGBA.h" |
| 21 |
#include "EtcBlock4x4EncodingBits.h" |
| 22 |
#include "EtcErrorMetric.h" |
| 23 |
|
| 24 |
|
| 25 |
namespace Etc |
| 26 |
{ |
| 27 |
class Block4x4; |
| 28 |
class EncoderSpec; |
| 29 |
class SortedBlockList; |
| 30 |
|
| 31 |
class Image |
| 32 |
{ |
| 33 |
public: |
| 34 |
|
| 35 |
//the differnt warning and errors that can come up during encoding |
| 36 |
enum EncodingStatus |
| 37 |
{ |
| 38 |
SUCCESS = 0, |
| 39 |
// |
| 40 |
WARNING_THRESHOLD = 1 << 0, |
| 41 |
// |
| 42 |
WARNING_EFFORT_OUT_OF_RANGE = 1 << 1, |
| 43 |
WARNING_JOBS_OUT_OF_RANGE = 1 << 2, |
| 44 |
WARNING_SOME_NON_OPAQUE_PIXELS = 1 << 3,//just for opaque formats, etc1, rgb8, r11, rg11 |
| 45 |
WARNING_ALL_OPAQUE_PIXELS = 1 << 4, |
| 46 |
WARNING_ALL_TRANSPARENT_PIXELS = 1 << 5, |
| 47 |
WARNING_SOME_TRANSLUCENT_PIXELS = 1 << 6,//just for rgb8A1 |
| 48 |
WARNING_SOME_RGBA_NOT_0_TO_1 = 1 << 7, |
| 49 |
WARNING_SOME_BLUE_VALUES_ARE_NOT_ZERO = 1 << 8, |
| 50 |
WARNING_SOME_GREEN_VALUES_ARE_NOT_ZERO = 1 << 9, |
| 51 |
// |
| 52 |
ERROR_THRESHOLD = 1 << 16, |
| 53 |
// |
| 54 |
ERROR_UNKNOWN_FORMAT = 1 << 17, |
| 55 |
ERROR_UNKNOWN_ERROR_METRIC = 1 << 18, |
| 56 |
ERROR_ZERO_WIDTH_OR_HEIGHT = 1 << 19, |
| 57 |
// |
| 58 |
}; |
| 59 |
|
| 60 |
enum class Format |
| 61 |
{ |
| 62 |
UNKNOWN, |
| 63 |
// |
| 64 |
ETC1, |
| 65 |
// |
| 66 |
// ETC2 formats |
| 67 |
RGB8, |
| 68 |
SRGB8, |
| 69 |
RGBA8, |
| 70 |
SRGBA8, |
| 71 |
R11, |
| 72 |
SIGNED_R11, |
| 73 |
RG11, |
| 74 |
SIGNED_RG11, |
| 75 |
RGB8A1, |
| 76 |
SRGB8A1, |
| 77 |
// |
| 78 |
FORMATS, |
| 79 |
// |
| 80 |
DEFAULT = SRGB8 |
| 81 |
}; |
| 82 |
|
| 83 |
// constructor using source image |
| 84 |
Image(float *a_pafSourceRGBA, unsigned int a_uiSourceWidth, |
| 85 |
unsigned int a_uiSourceHeight, |
| 86 |
ErrorMetric a_errormetric); |
| 87 |
|
| 88 |
// constructor using encoding bits |
| 89 |
Image(Format a_format, |
| 90 |
unsigned int a_uiSourceWidth, unsigned int a_uiSourceHeight, |
| 91 |
unsigned char *a_paucEncidingBits, unsigned int a_uiEncodingBitsBytes, |
| 92 |
Image *a_pimageSource, |
| 93 |
ErrorMetric a_errormetric); |
| 94 |
|
| 95 |
~Image(void); |
| 96 |
|
| 97 |
EncodingStatus Encode(Format a_format, ErrorMetric a_errormetric, float a_fEffort, |
| 98 |
unsigned int a_uiJobs, unsigned int a_uiMaxJobs); |
| 99 |
|
| 100 |
inline void AddToEncodingStatus(EncodingStatus a_encStatus) |
| 101 |
{ |
| 102 |
m_encodingStatus = (EncodingStatus)((unsigned int)m_encodingStatus | (unsigned int)a_encStatus); |
| 103 |
} |
| 104 |
|
| 105 |
inline unsigned int GetSourceWidth(void) |
| 106 |
{ |
| 107 |
return m_uiSourceWidth; |
| 108 |
} |
| 109 |
|
| 110 |
inline unsigned int GetSourceHeight(void) |
| 111 |
{ |
| 112 |
return m_uiSourceHeight; |
| 113 |
} |
| 114 |
|
| 115 |
inline unsigned int GetExtendedWidth(void) |
| 116 |
{ |
| 117 |
return m_uiExtendedWidth; |
| 118 |
} |
| 119 |
|
| 120 |
inline unsigned int GetExtendedHeight(void) |
| 121 |
{ |
| 122 |
return m_uiExtendedHeight; |
| 123 |
} |
| 124 |
|
| 125 |
inline unsigned int GetNumberOfBlocks() |
| 126 |
{ |
| 127 |
return m_uiBlockColumns * m_uiBlockRows; |
| 128 |
} |
| 129 |
|
| 130 |
inline Block4x4 * GetBlocks() |
| 131 |
{ |
| 132 |
return m_pablock; |
| 133 |
} |
| 134 |
|
| 135 |
inline unsigned char * GetEncodingBits(void) |
| 136 |
{ |
| 137 |
return m_paucEncodingBits; |
| 138 |
} |
| 139 |
|
| 140 |
inline unsigned int GetEncodingBitsBytes(void) |
| 141 |
{ |
| 142 |
return m_uiEncodingBitsBytes; |
| 143 |
} |
| 144 |
|
| 145 |
inline int GetEncodingTimeMs(void) |
| 146 |
{ |
| 147 |
return m_iEncodeTime_ms; |
| 148 |
} |
| 149 |
|
| 150 |
float GetError(void); |
| 151 |
|
| 152 |
inline ColorFloatRGBA * GetSourcePixel(unsigned int a_uiH, unsigned int a_uiV) |
| 153 |
{ |
| 154 |
if (a_uiH >= m_uiSourceWidth || a_uiV >= m_uiSourceHeight) |
| 155 |
{ |
| 156 |
return nullptr; |
| 157 |
} |
| 158 |
|
| 159 |
return &m_pafrgbaSource[a_uiV*m_uiSourceWidth + a_uiH]; |
| 160 |
} |
| 161 |
|
| 162 |
inline Format GetFormat(void) |
| 163 |
{ |
| 164 |
return m_format; |
| 165 |
} |
| 166 |
|
| 167 |
static Block4x4EncodingBits::Format DetermineEncodingBitsFormat(Format a_format); |
| 168 |
|
| 169 |
inline static unsigned short CalcExtendedDimension(unsigned short a_ushOriginalDimension) |
| 170 |
{ |
| 171 |
return (unsigned short)((a_ushOriginalDimension + 3) & ~3); |
| 172 |
} |
| 173 |
|
| 174 |
inline ErrorMetric GetErrorMetric(void) |
| 175 |
{ |
| 176 |
return m_errormetric; |
| 177 |
} |
| 178 |
|
| 179 |
static const char * EncodingFormatToString(Image::Format a_format); |
| 180 |
const char * EncodingFormatToString(void); |
| 181 |
//used to get basic information about the image data |
| 182 |
int m_iNumOpaquePixels; |
| 183 |
int m_iNumTranslucentPixels; |
| 184 |
int m_iNumTransparentPixels; |
| 185 |
|
| 186 |
ColorFloatRGBA m_numColorValues; |
| 187 |
ColorFloatRGBA m_numOutOfRangeValues; |
| 188 |
|
| 189 |
bool m_bVerboseOutput; |
| 190 |
private: |
| 191 |
//add a warning or error to check for while encoding |
| 192 |
inline void TrackEncodingWarning(EncodingStatus a_encStatus) |
| 193 |
{ |
| 194 |
m_warningsToCapture = (EncodingStatus)((unsigned int)m_warningsToCapture | (unsigned int)a_encStatus); |
| 195 |
} |
| 196 |
|
| 197 |
//report the warning if it is something we care about for this encoding |
| 198 |
inline void AddToEncodingStatusIfSignfigant(EncodingStatus a_encStatus) |
| 199 |
{ |
| 200 |
if ((EncodingStatus)((unsigned int)m_warningsToCapture & (unsigned int)a_encStatus) == a_encStatus) |
| 201 |
{ |
| 202 |
AddToEncodingStatus(a_encStatus); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
Image(void); |
| 207 |
void FindEncodingWarningTypesForCurFormat(); |
| 208 |
void FindAndSetEncodingWarnings(); |
| 209 |
|
| 210 |
void InitBlocksAndBlockSorter(void); |
| 211 |
|
| 212 |
void RunFirstPass(unsigned int a_uiMultithreadingOffset, |
| 213 |
unsigned int a_uiMultithreadingStride); |
| 214 |
|
| 215 |
void SetEncodingBits(unsigned int a_uiMultithreadingOffset, |
| 216 |
unsigned int a_uiMultithreadingStride); |
| 217 |
|
| 218 |
unsigned int IterateThroughWorstBlocks(unsigned int a_uiMaxBlocks, |
| 219 |
unsigned int a_uiMultithreadingOffset, |
| 220 |
unsigned int a_uiMultithreadingStride); |
| 221 |
|
| 222 |
// inputs |
| 223 |
ColorFloatRGBA *m_pafrgbaSource; |
| 224 |
unsigned int m_uiSourceWidth; |
| 225 |
unsigned int m_uiSourceHeight; |
| 226 |
unsigned int m_uiExtendedWidth; |
| 227 |
unsigned int m_uiExtendedHeight; |
| 228 |
unsigned int m_uiBlockColumns; |
| 229 |
unsigned int m_uiBlockRows; |
| 230 |
// intermediate data |
| 231 |
Block4x4 *m_pablock; |
| 232 |
// encoding |
| 233 |
Format m_format; |
| 234 |
Block4x4EncodingBits::Format m_encodingbitsformat; |
| 235 |
unsigned int m_uiEncodingBitsBytes; // for entire image |
| 236 |
unsigned char *m_paucEncodingBits; |
| 237 |
ErrorMetric m_errormetric; |
| 238 |
float m_fEffort; |
| 239 |
// stats |
| 240 |
int m_iEncodeTime_ms; |
| 241 |
|
| 242 |
SortedBlockList *m_psortedblocklist; |
| 243 |
//this will hold any warning or errors that happen during encoding |
| 244 |
EncodingStatus m_encodingStatus; |
| 245 |
//these will be the warnings we are tracking |
| 246 |
EncodingStatus m_warningsToCapture; |
| 247 |
}; |
| 248 |
|
| 249 |
} // namespace Etc |