| 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 <assert.h> |
| 20 |
|
| 21 |
namespace Etc |
| 22 |
{ |
| 23 |
|
| 24 |
// ################################################################################ |
| 25 |
// Block4x4EncodingBits |
| 26 |
// Base class for Block4x4EncodingBits_XXXX |
| 27 |
// ################################################################################ |
| 28 |
|
| 29 |
class Block4x4EncodingBits |
| 30 |
{ |
| 31 |
public: |
| 32 |
|
| 33 |
enum class Format |
| 34 |
{ |
| 35 |
UNKNOWN, |
| 36 |
// |
| 37 |
RGB8, |
| 38 |
RGBA8, |
| 39 |
R11, |
| 40 |
RG11, |
| 41 |
RGB8A1, |
| 42 |
// |
| 43 |
FORMATS |
| 44 |
}; |
| 45 |
|
| 46 |
static unsigned int GetBytesPerBlock(Format a_format) |
| 47 |
{ |
| 48 |
switch (a_format) |
| 49 |
{ |
| 50 |
case Format::RGB8: |
| 51 |
case Format::R11: |
| 52 |
case Format::RGB8A1: |
| 53 |
return 8; |
| 54 |
break; |
| 55 |
|
| 56 |
case Format::RGBA8: |
| 57 |
case Format::RG11: |
| 58 |
return 16; |
| 59 |
break; |
| 60 |
|
| 61 |
default: |
| 62 |
return 0; |
| 63 |
break; |
| 64 |
} |
| 65 |
|
| 66 |
} |
| 67 |
|
| 68 |
}; |
| 69 |
|
| 70 |
// ################################################################################ |
| 71 |
// Block4x4EncodingBits_RGB8 |
| 72 |
// Encoding bits for the RGB portion of ETC1, RGB8, RGB8A1 and RGBA8 |
| 73 |
// ################################################################################ |
| 74 |
|
| 75 |
class Block4x4EncodingBits_RGB8 |
| 76 |
{ |
| 77 |
public: |
| 78 |
|
| 79 |
static const unsigned int BYTES_PER_BLOCK = 8; |
| 80 |
|
| 81 |
inline Block4x4EncodingBits_RGB8(void) |
| 82 |
{ |
| 83 |
assert(sizeof(Block4x4EncodingBits_RGB8) == BYTES_PER_BLOCK); |
| 84 |
|
| 85 |
for (unsigned int uiByte = 0; uiByte < BYTES_PER_BLOCK; uiByte++) |
| 86 |
{ |
| 87 |
auc[uiByte] = 0; |
| 88 |
} |
| 89 |
|
| 90 |
} |
| 91 |
|
| 92 |
typedef struct |
| 93 |
{ |
| 94 |
unsigned red2 : 4; |
| 95 |
unsigned red1 : 4; |
| 96 |
// |
| 97 |
unsigned green2 : 4; |
| 98 |
unsigned green1 : 4; |
| 99 |
// |
| 100 |
unsigned blue2 : 4; |
| 101 |
unsigned blue1 : 4; |
| 102 |
// |
| 103 |
unsigned flip : 1; |
| 104 |
unsigned diff : 1; |
| 105 |
unsigned cw2 : 3; |
| 106 |
unsigned cw1 : 3; |
| 107 |
// |
| 108 |
unsigned int selectors; |
| 109 |
} Individual; |
| 110 |
|
| 111 |
typedef struct |
| 112 |
{ |
| 113 |
signed dred2 : 3; |
| 114 |
unsigned red1 : 5; |
| 115 |
// |
| 116 |
signed dgreen2 : 3; |
| 117 |
unsigned green1 : 5; |
| 118 |
// |
| 119 |
signed dblue2 : 3; |
| 120 |
unsigned blue1 : 5; |
| 121 |
// |
| 122 |
unsigned flip : 1; |
| 123 |
unsigned diff : 1; |
| 124 |
unsigned cw2 : 3; |
| 125 |
unsigned cw1 : 3; |
| 126 |
// |
| 127 |
unsigned int selectors; |
| 128 |
} Differential; |
| 129 |
|
| 130 |
typedef struct |
| 131 |
{ |
| 132 |
unsigned red1b : 2; |
| 133 |
unsigned detect2 : 1; |
| 134 |
unsigned red1a : 2; |
| 135 |
unsigned detect1 : 3; |
| 136 |
// |
| 137 |
unsigned blue1 : 4; |
| 138 |
unsigned green1 : 4; |
| 139 |
// |
| 140 |
unsigned green2 : 4; |
| 141 |
unsigned red2 : 4; |
| 142 |
// |
| 143 |
unsigned db : 1; |
| 144 |
unsigned diff : 1; |
| 145 |
unsigned da : 2; |
| 146 |
unsigned blue2 : 4; |
| 147 |
// |
| 148 |
unsigned int selectors; |
| 149 |
} T; |
| 150 |
|
| 151 |
typedef struct |
| 152 |
{ |
| 153 |
unsigned green1a : 3; |
| 154 |
unsigned red1 : 4; |
| 155 |
unsigned detect1 : 1; |
| 156 |
// |
| 157 |
unsigned blue1b : 2; |
| 158 |
unsigned detect3 : 1; |
| 159 |
unsigned blue1a : 1; |
| 160 |
unsigned green1b : 1; |
| 161 |
unsigned detect2 : 3; |
| 162 |
// |
| 163 |
unsigned green2a : 3; |
| 164 |
unsigned red2 : 4; |
| 165 |
unsigned blue1c : 1; |
| 166 |
// |
| 167 |
unsigned db : 1; |
| 168 |
unsigned diff : 1; |
| 169 |
unsigned da : 1; |
| 170 |
unsigned blue2 : 4; |
| 171 |
unsigned green2b : 1; |
| 172 |
// |
| 173 |
unsigned int selectors; |
| 174 |
} H; |
| 175 |
|
| 176 |
typedef struct |
| 177 |
{ |
| 178 |
unsigned originGreen1 : 1; |
| 179 |
unsigned originRed : 6; |
| 180 |
unsigned detect1 : 1; |
| 181 |
// |
| 182 |
unsigned originBlue1 : 1; |
| 183 |
unsigned originGreen2 : 6; |
| 184 |
unsigned detect2 : 1; |
| 185 |
// |
| 186 |
unsigned originBlue3 : 2; |
| 187 |
unsigned detect4 : 1; |
| 188 |
unsigned originBlue2 : 2; |
| 189 |
unsigned detect3 : 3; |
| 190 |
// |
| 191 |
unsigned horizRed2 : 1; |
| 192 |
unsigned diff : 1; |
| 193 |
unsigned horizRed1 : 5; |
| 194 |
unsigned originBlue4 : 1; |
| 195 |
// |
| 196 |
unsigned horizBlue1: 1; |
| 197 |
unsigned horizGreen : 7; |
| 198 |
// |
| 199 |
unsigned vertRed1 : 3; |
| 200 |
unsigned horizBlue2 : 5; |
| 201 |
// |
| 202 |
unsigned vertGreen1 : 5; |
| 203 |
unsigned vertRed2 : 3; |
| 204 |
// |
| 205 |
unsigned vertBlue : 6; |
| 206 |
unsigned vertGreen2 : 2; |
| 207 |
} Planar; |
| 208 |
|
| 209 |
union |
| 210 |
{ |
| 211 |
unsigned char auc[BYTES_PER_BLOCK]; |
| 212 |
unsigned long int ul; |
| 213 |
Individual individual; |
| 214 |
Differential differential; |
| 215 |
T t; |
| 216 |
H h; |
| 217 |
Planar planar; |
| 218 |
}; |
| 219 |
|
| 220 |
}; |
| 221 |
|
| 222 |
// ################################################################################ |
| 223 |
// Block4x4EncodingBits_A8 |
| 224 |
// Encoding bits for the A portion of RGBA8 |
| 225 |
// ################################################################################ |
| 226 |
|
| 227 |
class Block4x4EncodingBits_A8 |
| 228 |
{ |
| 229 |
public: |
| 230 |
|
| 231 |
static const unsigned int BYTES_PER_BLOCK = 8; |
| 232 |
static const unsigned int SELECTOR_BYTES = 6; |
| 233 |
|
| 234 |
typedef struct |
| 235 |
{ |
| 236 |
unsigned base : 8; |
| 237 |
unsigned table : 4; |
| 238 |
unsigned multiplier : 4; |
| 239 |
unsigned selectors0 : 8; |
| 240 |
unsigned selectors1 : 8; |
| 241 |
unsigned selectors2 : 8; |
| 242 |
unsigned selectors3 : 8; |
| 243 |
unsigned selectors4 : 8; |
| 244 |
unsigned selectors5 : 8; |
| 245 |
} Data; |
| 246 |
|
| 247 |
Data data; |
| 248 |
|
| 249 |
}; |
| 250 |
|
| 251 |
// ################################################################################ |
| 252 |
// Block4x4EncodingBits_R11 |
| 253 |
// Encoding bits for the R portion of R11 |
| 254 |
// ################################################################################ |
| 255 |
|
| 256 |
class Block4x4EncodingBits_R11 |
| 257 |
{ |
| 258 |
public: |
| 259 |
|
| 260 |
static const unsigned int BYTES_PER_BLOCK = 8; |
| 261 |
static const unsigned int SELECTOR_BYTES = 6; |
| 262 |
|
| 263 |
typedef struct |
| 264 |
{ |
| 265 |
unsigned base : 8; |
| 266 |
unsigned table : 4; |
| 267 |
unsigned multiplier : 4; |
| 268 |
unsigned selectors0 : 8; |
| 269 |
unsigned selectors1 : 8; |
| 270 |
unsigned selectors2 : 8; |
| 271 |
unsigned selectors3 : 8; |
| 272 |
unsigned selectors4 : 8; |
| 273 |
unsigned selectors5 : 8; |
| 274 |
} Data; |
| 275 |
|
| 276 |
Data data; |
| 277 |
|
| 278 |
}; |
| 279 |
|
| 280 |
class Block4x4EncodingBits_RG11 |
| 281 |
{ |
| 282 |
public: |
| 283 |
|
| 284 |
static const unsigned int BYTES_PER_BLOCK = 16; |
| 285 |
static const unsigned int SELECTOR_BYTES = 12; |
| 286 |
|
| 287 |
typedef struct |
| 288 |
{ |
| 289 |
//Red portion |
| 290 |
unsigned baseR : 8; |
| 291 |
unsigned tableIndexR : 4; |
| 292 |
unsigned multiplierR : 4; |
| 293 |
unsigned selectorsR0 : 8; |
| 294 |
unsigned selectorsR1 : 8; |
| 295 |
unsigned selectorsR2 : 8; |
| 296 |
unsigned selectorsR3 : 8; |
| 297 |
unsigned selectorsR4 : 8; |
| 298 |
unsigned selectorsR5 : 8; |
| 299 |
//Green portion |
| 300 |
unsigned baseG : 8; |
| 301 |
unsigned tableIndexG : 4; |
| 302 |
unsigned multiplierG : 4; |
| 303 |
unsigned selectorsG0 : 8; |
| 304 |
unsigned selectorsG1 : 8; |
| 305 |
unsigned selectorsG2 : 8; |
| 306 |
unsigned selectorsG3 : 8; |
| 307 |
unsigned selectorsG4 : 8; |
| 308 |
unsigned selectorsG5 : 8; |
| 309 |
} Data; |
| 310 |
|
| 311 |
Data data; |
| 312 |
|
| 313 |
}; |
| 314 |
|
| 315 |
} |