dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 1 | /* |
| 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3 | % % |
| 4 | % % |
| 5 | % % |
| 6 | % DDDD DDDD SSSSS % |
| 7 | % D D D D SS % |
| 8 | % D D D D SSS % |
| 9 | % D D D D SS % |
| 10 | % DDDD DDDD SSSSS % |
| 11 | % % |
| 12 | % % |
| 13 | % Read/Write Microsoft Direct Draw Surface Image Format % |
| 14 | % % |
| 15 | % Software Design % |
| 16 | % Bianca van Schaik % |
| 17 | % March 2008 % |
| 18 | % Dirk Lemstra % |
| 19 | % September 2013 % |
| 20 | % % |
| 21 | % % |
Cristy | f6ff9ea | 2016-12-05 09:53:35 -0500 | [diff] [blame] | 22 | % Copyright 1999-2017 ImageMagick Studio LLC, a non-profit organization % |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 23 | % dedicated to making software imaging solutions freely available. % |
| 24 | % % |
| 25 | % You may not use this file except in compliance with the License. You may % |
| 26 | % obtain a copy of the License at % |
| 27 | % % |
Cristy | f19d414 | 2017-04-24 11:34:30 -0400 | [diff] [blame^] | 28 | % https://www.imagemagick.org/script/license.php % |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 29 | % % |
| 30 | % Unless required by applicable law or agreed to in writing, software % |
| 31 | % distributed under the License is distributed on an "AS IS" BASIS, % |
| 32 | % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. % |
| 33 | % See the License for the specific language governing permissions and % |
| 34 | % limitations under the License. % |
| 35 | % % |
| 36 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 37 | % |
| 38 | % |
| 39 | */ |
| 40 | |
| 41 | /* |
| 42 | Include declarations. |
| 43 | */ |
| 44 | #include "MagickCore/studio.h" |
dirk | 6398fb4 | 2014-09-08 18:19:39 +0000 | [diff] [blame] | 45 | #include "MagickCore/attribute.h" |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 46 | #include "MagickCore/blob.h" |
| 47 | #include "MagickCore/blob-private.h" |
| 48 | #include "MagickCore/cache.h" |
| 49 | #include "MagickCore/colorspace.h" |
dirk | 6398fb4 | 2014-09-08 18:19:39 +0000 | [diff] [blame] | 50 | #include "MagickCore/colorspace-private.h" |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 51 | #include "MagickCore/exception.h" |
| 52 | #include "MagickCore/exception-private.h" |
| 53 | #include "MagickCore/image.h" |
| 54 | #include "MagickCore/image-private.h" |
| 55 | #include "MagickCore/list.h" |
| 56 | #include "MagickCore/log.h" |
| 57 | #include "MagickCore/magick.h" |
| 58 | #include "MagickCore/memory_.h" |
| 59 | #include "MagickCore/monitor.h" |
| 60 | #include "MagickCore/monitor-private.h" |
dirk | 6398fb4 | 2014-09-08 18:19:39 +0000 | [diff] [blame] | 61 | #include "MagickCore/option.h" |
| 62 | #include "MagickCore/pixel-accessor.h" |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 63 | #include "MagickCore/profile.h" |
dirk | 6398fb4 | 2014-09-08 18:19:39 +0000 | [diff] [blame] | 64 | #include "MagickCore/quantum.h" |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 65 | #include "MagickCore/quantum-private.h" |
dirk | 560a910 | 2014-12-16 21:55:06 +0000 | [diff] [blame] | 66 | #include "MagickCore/resource_.h" |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 67 | #include "MagickCore/static.h" |
| 68 | #include "MagickCore/string_.h" |
dirk | 6398fb4 | 2014-09-08 18:19:39 +0000 | [diff] [blame] | 69 | #include "MagickCore/string-private.h" |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 70 | #include "MagickCore/module.h" |
| 71 | #include "MagickCore/transform.h" |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 72 | |
| 73 | /* |
| 74 | Definitions |
| 75 | */ |
| 76 | #define DDSD_CAPS 0x00000001 |
| 77 | #define DDSD_HEIGHT 0x00000002 |
| 78 | #define DDSD_WIDTH 0x00000004 |
| 79 | #define DDSD_PITCH 0x00000008 |
| 80 | #define DDSD_PIXELFORMAT 0x00001000 |
| 81 | #define DDSD_MIPMAPCOUNT 0x00020000 |
| 82 | #define DDSD_LINEARSIZE 0x00080000 |
| 83 | #define DDSD_DEPTH 0x00800000 |
| 84 | |
| 85 | #define DDPF_ALPHAPIXELS 0x00000001 |
| 86 | #define DDPF_FOURCC 0x00000004 |
| 87 | #define DDPF_RGB 0x00000040 |
dirk | 6398fb4 | 2014-09-08 18:19:39 +0000 | [diff] [blame] | 88 | #define DDPF_LUMINANCE 0x00020000 |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 89 | |
| 90 | #define FOURCC_DXT1 0x31545844 |
| 91 | #define FOURCC_DXT3 0x33545844 |
| 92 | #define FOURCC_DXT5 0x35545844 |
| 93 | |
| 94 | #define DDSCAPS_COMPLEX 0x00000008 |
| 95 | #define DDSCAPS_TEXTURE 0x00001000 |
| 96 | #define DDSCAPS_MIPMAP 0x00400000 |
| 97 | |
| 98 | #define DDSCAPS2_CUBEMAP 0x00000200 |
| 99 | #define DDSCAPS2_CUBEMAP_POSITIVEX 0x00000400 |
| 100 | #define DDSCAPS2_CUBEMAP_NEGATIVEX 0x00000800 |
| 101 | #define DDSCAPS2_CUBEMAP_POSITIVEY 0x00001000 |
| 102 | #define DDSCAPS2_CUBEMAP_NEGATIVEY 0x00002000 |
| 103 | #define DDSCAPS2_CUBEMAP_POSITIVEZ 0x00004000 |
| 104 | #define DDSCAPS2_CUBEMAP_NEGATIVEZ 0x00008000 |
| 105 | #define DDSCAPS2_VOLUME 0x00200000 |
| 106 | |
| 107 | #ifndef SIZE_MAX |
| 108 | #define SIZE_MAX ((size_t) -1) |
| 109 | #endif |
| 110 | |
| 111 | /* |
| 112 | Structure declarations. |
| 113 | */ |
| 114 | typedef struct _DDSPixelFormat |
| 115 | { |
| 116 | size_t |
| 117 | flags, |
| 118 | fourcc, |
| 119 | rgb_bitcount, |
| 120 | r_bitmask, |
| 121 | g_bitmask, |
| 122 | b_bitmask, |
| 123 | alpha_bitmask; |
| 124 | } DDSPixelFormat; |
| 125 | |
| 126 | typedef struct _DDSInfo |
| 127 | { |
| 128 | size_t |
| 129 | flags, |
| 130 | height, |
| 131 | width, |
| 132 | pitchOrLinearSize, |
| 133 | depth, |
| 134 | mipmapcount, |
| 135 | ddscaps1, |
| 136 | ddscaps2; |
| 137 | |
| 138 | DDSPixelFormat |
| 139 | pixelformat; |
| 140 | } DDSInfo; |
| 141 | |
| 142 | typedef struct _DDSColors |
| 143 | { |
| 144 | unsigned char |
| 145 | r[4], |
| 146 | g[4], |
| 147 | b[4], |
| 148 | a[4]; |
| 149 | } DDSColors; |
| 150 | |
| 151 | typedef struct _DDSVector4 |
| 152 | { |
| 153 | float |
| 154 | x, |
| 155 | y, |
| 156 | z, |
| 157 | w; |
| 158 | } DDSVector4; |
| 159 | |
| 160 | typedef struct _DDSVector3 |
| 161 | { |
| 162 | float |
| 163 | x, |
| 164 | y, |
| 165 | z; |
| 166 | } DDSVector3; |
| 167 | |
| 168 | typedef struct _DDSSourceBlock |
| 169 | { |
| 170 | unsigned char |
| 171 | start, |
| 172 | end, |
| 173 | error; |
| 174 | } DDSSourceBlock; |
| 175 | |
| 176 | typedef struct _DDSSingleColourLookup |
| 177 | { |
| 178 | DDSSourceBlock sources[2]; |
| 179 | } DDSSingleColourLookup; |
| 180 | |
| 181 | typedef MagickBooleanType |
| 182 | DDSDecoder(Image *, DDSInfo *, ExceptionInfo *); |
| 183 | |
| 184 | static const DDSSingleColourLookup DDSLookup_5_4[] = |
| 185 | { |
| 186 | { { { 0, 0, 0 }, { 0, 0, 0 } } }, |
| 187 | { { { 0, 0, 1 }, { 0, 1, 1 } } }, |
| 188 | { { { 0, 0, 2 }, { 0, 1, 0 } } }, |
| 189 | { { { 0, 0, 3 }, { 0, 1, 1 } } }, |
| 190 | { { { 0, 0, 4 }, { 0, 2, 1 } } }, |
| 191 | { { { 1, 0, 3 }, { 0, 2, 0 } } }, |
| 192 | { { { 1, 0, 2 }, { 0, 2, 1 } } }, |
| 193 | { { { 1, 0, 1 }, { 0, 3, 1 } } }, |
| 194 | { { { 1, 0, 0 }, { 0, 3, 0 } } }, |
| 195 | { { { 1, 0, 1 }, { 1, 2, 1 } } }, |
| 196 | { { { 1, 0, 2 }, { 1, 2, 0 } } }, |
| 197 | { { { 1, 0, 3 }, { 0, 4, 0 } } }, |
| 198 | { { { 1, 0, 4 }, { 0, 5, 1 } } }, |
| 199 | { { { 2, 0, 3 }, { 0, 5, 0 } } }, |
| 200 | { { { 2, 0, 2 }, { 0, 5, 1 } } }, |
| 201 | { { { 2, 0, 1 }, { 0, 6, 1 } } }, |
| 202 | { { { 2, 0, 0 }, { 0, 6, 0 } } }, |
| 203 | { { { 2, 0, 1 }, { 2, 3, 1 } } }, |
| 204 | { { { 2, 0, 2 }, { 2, 3, 0 } } }, |
| 205 | { { { 2, 0, 3 }, { 0, 7, 0 } } }, |
| 206 | { { { 2, 0, 4 }, { 1, 6, 1 } } }, |
| 207 | { { { 3, 0, 3 }, { 1, 6, 0 } } }, |
| 208 | { { { 3, 0, 2 }, { 0, 8, 0 } } }, |
| 209 | { { { 3, 0, 1 }, { 0, 9, 1 } } }, |
| 210 | { { { 3, 0, 0 }, { 0, 9, 0 } } }, |
| 211 | { { { 3, 0, 1 }, { 0, 9, 1 } } }, |
| 212 | { { { 3, 0, 2 }, { 0, 10, 1 } } }, |
| 213 | { { { 3, 0, 3 }, { 0, 10, 0 } } }, |
| 214 | { { { 3, 0, 4 }, { 2, 7, 1 } } }, |
| 215 | { { { 4, 0, 4 }, { 2, 7, 0 } } }, |
| 216 | { { { 4, 0, 3 }, { 0, 11, 0 } } }, |
| 217 | { { { 4, 0, 2 }, { 1, 10, 1 } } }, |
| 218 | { { { 4, 0, 1 }, { 1, 10, 0 } } }, |
| 219 | { { { 4, 0, 0 }, { 0, 12, 0 } } }, |
| 220 | { { { 4, 0, 1 }, { 0, 13, 1 } } }, |
| 221 | { { { 4, 0, 2 }, { 0, 13, 0 } } }, |
| 222 | { { { 4, 0, 3 }, { 0, 13, 1 } } }, |
| 223 | { { { 4, 0, 4 }, { 0, 14, 1 } } }, |
| 224 | { { { 5, 0, 3 }, { 0, 14, 0 } } }, |
| 225 | { { { 5, 0, 2 }, { 2, 11, 1 } } }, |
| 226 | { { { 5, 0, 1 }, { 2, 11, 0 } } }, |
| 227 | { { { 5, 0, 0 }, { 0, 15, 0 } } }, |
| 228 | { { { 5, 0, 1 }, { 1, 14, 1 } } }, |
| 229 | { { { 5, 0, 2 }, { 1, 14, 0 } } }, |
| 230 | { { { 5, 0, 3 }, { 0, 16, 0 } } }, |
| 231 | { { { 5, 0, 4 }, { 0, 17, 1 } } }, |
| 232 | { { { 6, 0, 3 }, { 0, 17, 0 } } }, |
| 233 | { { { 6, 0, 2 }, { 0, 17, 1 } } }, |
| 234 | { { { 6, 0, 1 }, { 0, 18, 1 } } }, |
| 235 | { { { 6, 0, 0 }, { 0, 18, 0 } } }, |
| 236 | { { { 6, 0, 1 }, { 2, 15, 1 } } }, |
| 237 | { { { 6, 0, 2 }, { 2, 15, 0 } } }, |
| 238 | { { { 6, 0, 3 }, { 0, 19, 0 } } }, |
| 239 | { { { 6, 0, 4 }, { 1, 18, 1 } } }, |
| 240 | { { { 7, 0, 3 }, { 1, 18, 0 } } }, |
| 241 | { { { 7, 0, 2 }, { 0, 20, 0 } } }, |
| 242 | { { { 7, 0, 1 }, { 0, 21, 1 } } }, |
| 243 | { { { 7, 0, 0 }, { 0, 21, 0 } } }, |
| 244 | { { { 7, 0, 1 }, { 0, 21, 1 } } }, |
| 245 | { { { 7, 0, 2 }, { 0, 22, 1 } } }, |
| 246 | { { { 7, 0, 3 }, { 0, 22, 0 } } }, |
| 247 | { { { 7, 0, 4 }, { 2, 19, 1 } } }, |
| 248 | { { { 8, 0, 4 }, { 2, 19, 0 } } }, |
| 249 | { { { 8, 0, 3 }, { 0, 23, 0 } } }, |
| 250 | { { { 8, 0, 2 }, { 1, 22, 1 } } }, |
| 251 | { { { 8, 0, 1 }, { 1, 22, 0 } } }, |
| 252 | { { { 8, 0, 0 }, { 0, 24, 0 } } }, |
| 253 | { { { 8, 0, 1 }, { 0, 25, 1 } } }, |
| 254 | { { { 8, 0, 2 }, { 0, 25, 0 } } }, |
| 255 | { { { 8, 0, 3 }, { 0, 25, 1 } } }, |
| 256 | { { { 8, 0, 4 }, { 0, 26, 1 } } }, |
| 257 | { { { 9, 0, 3 }, { 0, 26, 0 } } }, |
| 258 | { { { 9, 0, 2 }, { 2, 23, 1 } } }, |
| 259 | { { { 9, 0, 1 }, { 2, 23, 0 } } }, |
| 260 | { { { 9, 0, 0 }, { 0, 27, 0 } } }, |
| 261 | { { { 9, 0, 1 }, { 1, 26, 1 } } }, |
| 262 | { { { 9, 0, 2 }, { 1, 26, 0 } } }, |
| 263 | { { { 9, 0, 3 }, { 0, 28, 0 } } }, |
| 264 | { { { 9, 0, 4 }, { 0, 29, 1 } } }, |
| 265 | { { { 10, 0, 3 }, { 0, 29, 0 } } }, |
| 266 | { { { 10, 0, 2 }, { 0, 29, 1 } } }, |
| 267 | { { { 10, 0, 1 }, { 0, 30, 1 } } }, |
| 268 | { { { 10, 0, 0 }, { 0, 30, 0 } } }, |
| 269 | { { { 10, 0, 1 }, { 2, 27, 1 } } }, |
| 270 | { { { 10, 0, 2 }, { 2, 27, 0 } } }, |
| 271 | { { { 10, 0, 3 }, { 0, 31, 0 } } }, |
| 272 | { { { 10, 0, 4 }, { 1, 30, 1 } } }, |
| 273 | { { { 11, 0, 3 }, { 1, 30, 0 } } }, |
| 274 | { { { 11, 0, 2 }, { 4, 24, 0 } } }, |
| 275 | { { { 11, 0, 1 }, { 1, 31, 1 } } }, |
| 276 | { { { 11, 0, 0 }, { 1, 31, 0 } } }, |
| 277 | { { { 11, 0, 1 }, { 1, 31, 1 } } }, |
| 278 | { { { 11, 0, 2 }, { 2, 30, 1 } } }, |
| 279 | { { { 11, 0, 3 }, { 2, 30, 0 } } }, |
| 280 | { { { 11, 0, 4 }, { 2, 31, 1 } } }, |
| 281 | { { { 12, 0, 4 }, { 2, 31, 0 } } }, |
| 282 | { { { 12, 0, 3 }, { 4, 27, 0 } } }, |
| 283 | { { { 12, 0, 2 }, { 3, 30, 1 } } }, |
| 284 | { { { 12, 0, 1 }, { 3, 30, 0 } } }, |
| 285 | { { { 12, 0, 0 }, { 4, 28, 0 } } }, |
| 286 | { { { 12, 0, 1 }, { 3, 31, 1 } } }, |
| 287 | { { { 12, 0, 2 }, { 3, 31, 0 } } }, |
| 288 | { { { 12, 0, 3 }, { 3, 31, 1 } } }, |
| 289 | { { { 12, 0, 4 }, { 4, 30, 1 } } }, |
| 290 | { { { 13, 0, 3 }, { 4, 30, 0 } } }, |
| 291 | { { { 13, 0, 2 }, { 6, 27, 1 } } }, |
| 292 | { { { 13, 0, 1 }, { 6, 27, 0 } } }, |
| 293 | { { { 13, 0, 0 }, { 4, 31, 0 } } }, |
| 294 | { { { 13, 0, 1 }, { 5, 30, 1 } } }, |
| 295 | { { { 13, 0, 2 }, { 5, 30, 0 } } }, |
| 296 | { { { 13, 0, 3 }, { 8, 24, 0 } } }, |
| 297 | { { { 13, 0, 4 }, { 5, 31, 1 } } }, |
| 298 | { { { 14, 0, 3 }, { 5, 31, 0 } } }, |
| 299 | { { { 14, 0, 2 }, { 5, 31, 1 } } }, |
| 300 | { { { 14, 0, 1 }, { 6, 30, 1 } } }, |
| 301 | { { { 14, 0, 0 }, { 6, 30, 0 } } }, |
| 302 | { { { 14, 0, 1 }, { 6, 31, 1 } } }, |
| 303 | { { { 14, 0, 2 }, { 6, 31, 0 } } }, |
| 304 | { { { 14, 0, 3 }, { 8, 27, 0 } } }, |
| 305 | { { { 14, 0, 4 }, { 7, 30, 1 } } }, |
| 306 | { { { 15, 0, 3 }, { 7, 30, 0 } } }, |
| 307 | { { { 15, 0, 2 }, { 8, 28, 0 } } }, |
| 308 | { { { 15, 0, 1 }, { 7, 31, 1 } } }, |
| 309 | { { { 15, 0, 0 }, { 7, 31, 0 } } }, |
| 310 | { { { 15, 0, 1 }, { 7, 31, 1 } } }, |
| 311 | { { { 15, 0, 2 }, { 8, 30, 1 } } }, |
| 312 | { { { 15, 0, 3 }, { 8, 30, 0 } } }, |
| 313 | { { { 15, 0, 4 }, { 10, 27, 1 } } }, |
| 314 | { { { 16, 0, 4 }, { 10, 27, 0 } } }, |
| 315 | { { { 16, 0, 3 }, { 8, 31, 0 } } }, |
| 316 | { { { 16, 0, 2 }, { 9, 30, 1 } } }, |
| 317 | { { { 16, 0, 1 }, { 9, 30, 0 } } }, |
| 318 | { { { 16, 0, 0 }, { 12, 24, 0 } } }, |
| 319 | { { { 16, 0, 1 }, { 9, 31, 1 } } }, |
| 320 | { { { 16, 0, 2 }, { 9, 31, 0 } } }, |
| 321 | { { { 16, 0, 3 }, { 9, 31, 1 } } }, |
| 322 | { { { 16, 0, 4 }, { 10, 30, 1 } } }, |
| 323 | { { { 17, 0, 3 }, { 10, 30, 0 } } }, |
| 324 | { { { 17, 0, 2 }, { 10, 31, 1 } } }, |
| 325 | { { { 17, 0, 1 }, { 10, 31, 0 } } }, |
| 326 | { { { 17, 0, 0 }, { 12, 27, 0 } } }, |
| 327 | { { { 17, 0, 1 }, { 11, 30, 1 } } }, |
| 328 | { { { 17, 0, 2 }, { 11, 30, 0 } } }, |
| 329 | { { { 17, 0, 3 }, { 12, 28, 0 } } }, |
| 330 | { { { 17, 0, 4 }, { 11, 31, 1 } } }, |
| 331 | { { { 18, 0, 3 }, { 11, 31, 0 } } }, |
| 332 | { { { 18, 0, 2 }, { 11, 31, 1 } } }, |
| 333 | { { { 18, 0, 1 }, { 12, 30, 1 } } }, |
| 334 | { { { 18, 0, 0 }, { 12, 30, 0 } } }, |
| 335 | { { { 18, 0, 1 }, { 14, 27, 1 } } }, |
| 336 | { { { 18, 0, 2 }, { 14, 27, 0 } } }, |
| 337 | { { { 18, 0, 3 }, { 12, 31, 0 } } }, |
| 338 | { { { 18, 0, 4 }, { 13, 30, 1 } } }, |
| 339 | { { { 19, 0, 3 }, { 13, 30, 0 } } }, |
| 340 | { { { 19, 0, 2 }, { 16, 24, 0 } } }, |
| 341 | { { { 19, 0, 1 }, { 13, 31, 1 } } }, |
| 342 | { { { 19, 0, 0 }, { 13, 31, 0 } } }, |
| 343 | { { { 19, 0, 1 }, { 13, 31, 1 } } }, |
| 344 | { { { 19, 0, 2 }, { 14, 30, 1 } } }, |
| 345 | { { { 19, 0, 3 }, { 14, 30, 0 } } }, |
| 346 | { { { 19, 0, 4 }, { 14, 31, 1 } } }, |
| 347 | { { { 20, 0, 4 }, { 14, 31, 0 } } }, |
| 348 | { { { 20, 0, 3 }, { 16, 27, 0 } } }, |
| 349 | { { { 20, 0, 2 }, { 15, 30, 1 } } }, |
| 350 | { { { 20, 0, 1 }, { 15, 30, 0 } } }, |
| 351 | { { { 20, 0, 0 }, { 16, 28, 0 } } }, |
| 352 | { { { 20, 0, 1 }, { 15, 31, 1 } } }, |
| 353 | { { { 20, 0, 2 }, { 15, 31, 0 } } }, |
| 354 | { { { 20, 0, 3 }, { 15, 31, 1 } } }, |
| 355 | { { { 20, 0, 4 }, { 16, 30, 1 } } }, |
| 356 | { { { 21, 0, 3 }, { 16, 30, 0 } } }, |
| 357 | { { { 21, 0, 2 }, { 18, 27, 1 } } }, |
| 358 | { { { 21, 0, 1 }, { 18, 27, 0 } } }, |
| 359 | { { { 21, 0, 0 }, { 16, 31, 0 } } }, |
| 360 | { { { 21, 0, 1 }, { 17, 30, 1 } } }, |
| 361 | { { { 21, 0, 2 }, { 17, 30, 0 } } }, |
| 362 | { { { 21, 0, 3 }, { 20, 24, 0 } } }, |
| 363 | { { { 21, 0, 4 }, { 17, 31, 1 } } }, |
| 364 | { { { 22, 0, 3 }, { 17, 31, 0 } } }, |
| 365 | { { { 22, 0, 2 }, { 17, 31, 1 } } }, |
| 366 | { { { 22, 0, 1 }, { 18, 30, 1 } } }, |
| 367 | { { { 22, 0, 0 }, { 18, 30, 0 } } }, |
| 368 | { { { 22, 0, 1 }, { 18, 31, 1 } } }, |
| 369 | { { { 22, 0, 2 }, { 18, 31, 0 } } }, |
| 370 | { { { 22, 0, 3 }, { 20, 27, 0 } } }, |
| 371 | { { { 22, 0, 4 }, { 19, 30, 1 } } }, |
| 372 | { { { 23, 0, 3 }, { 19, 30, 0 } } }, |
| 373 | { { { 23, 0, 2 }, { 20, 28, 0 } } }, |
| 374 | { { { 23, 0, 1 }, { 19, 31, 1 } } }, |
| 375 | { { { 23, 0, 0 }, { 19, 31, 0 } } }, |
| 376 | { { { 23, 0, 1 }, { 19, 31, 1 } } }, |
| 377 | { { { 23, 0, 2 }, { 20, 30, 1 } } }, |
| 378 | { { { 23, 0, 3 }, { 20, 30, 0 } } }, |
| 379 | { { { 23, 0, 4 }, { 22, 27, 1 } } }, |
| 380 | { { { 24, 0, 4 }, { 22, 27, 0 } } }, |
| 381 | { { { 24, 0, 3 }, { 20, 31, 0 } } }, |
| 382 | { { { 24, 0, 2 }, { 21, 30, 1 } } }, |
| 383 | { { { 24, 0, 1 }, { 21, 30, 0 } } }, |
| 384 | { { { 24, 0, 0 }, { 24, 24, 0 } } }, |
| 385 | { { { 24, 0, 1 }, { 21, 31, 1 } } }, |
| 386 | { { { 24, 0, 2 }, { 21, 31, 0 } } }, |
| 387 | { { { 24, 0, 3 }, { 21, 31, 1 } } }, |
| 388 | { { { 24, 0, 4 }, { 22, 30, 1 } } }, |
| 389 | { { { 25, 0, 3 }, { 22, 30, 0 } } }, |
| 390 | { { { 25, 0, 2 }, { 22, 31, 1 } } }, |
| 391 | { { { 25, 0, 1 }, { 22, 31, 0 } } }, |
| 392 | { { { 25, 0, 0 }, { 24, 27, 0 } } }, |
| 393 | { { { 25, 0, 1 }, { 23, 30, 1 } } }, |
| 394 | { { { 25, 0, 2 }, { 23, 30, 0 } } }, |
| 395 | { { { 25, 0, 3 }, { 24, 28, 0 } } }, |
| 396 | { { { 25, 0, 4 }, { 23, 31, 1 } } }, |
| 397 | { { { 26, 0, 3 }, { 23, 31, 0 } } }, |
| 398 | { { { 26, 0, 2 }, { 23, 31, 1 } } }, |
| 399 | { { { 26, 0, 1 }, { 24, 30, 1 } } }, |
| 400 | { { { 26, 0, 0 }, { 24, 30, 0 } } }, |
| 401 | { { { 26, 0, 1 }, { 26, 27, 1 } } }, |
| 402 | { { { 26, 0, 2 }, { 26, 27, 0 } } }, |
| 403 | { { { 26, 0, 3 }, { 24, 31, 0 } } }, |
| 404 | { { { 26, 0, 4 }, { 25, 30, 1 } } }, |
| 405 | { { { 27, 0, 3 }, { 25, 30, 0 } } }, |
| 406 | { { { 27, 0, 2 }, { 28, 24, 0 } } }, |
| 407 | { { { 27, 0, 1 }, { 25, 31, 1 } } }, |
| 408 | { { { 27, 0, 0 }, { 25, 31, 0 } } }, |
| 409 | { { { 27, 0, 1 }, { 25, 31, 1 } } }, |
| 410 | { { { 27, 0, 2 }, { 26, 30, 1 } } }, |
| 411 | { { { 27, 0, 3 }, { 26, 30, 0 } } }, |
| 412 | { { { 27, 0, 4 }, { 26, 31, 1 } } }, |
| 413 | { { { 28, 0, 4 }, { 26, 31, 0 } } }, |
| 414 | { { { 28, 0, 3 }, { 28, 27, 0 } } }, |
| 415 | { { { 28, 0, 2 }, { 27, 30, 1 } } }, |
| 416 | { { { 28, 0, 1 }, { 27, 30, 0 } } }, |
| 417 | { { { 28, 0, 0 }, { 28, 28, 0 } } }, |
| 418 | { { { 28, 0, 1 }, { 27, 31, 1 } } }, |
| 419 | { { { 28, 0, 2 }, { 27, 31, 0 } } }, |
| 420 | { { { 28, 0, 3 }, { 27, 31, 1 } } }, |
| 421 | { { { 28, 0, 4 }, { 28, 30, 1 } } }, |
| 422 | { { { 29, 0, 3 }, { 28, 30, 0 } } }, |
| 423 | { { { 29, 0, 2 }, { 30, 27, 1 } } }, |
| 424 | { { { 29, 0, 1 }, { 30, 27, 0 } } }, |
| 425 | { { { 29, 0, 0 }, { 28, 31, 0 } } }, |
| 426 | { { { 29, 0, 1 }, { 29, 30, 1 } } }, |
| 427 | { { { 29, 0, 2 }, { 29, 30, 0 } } }, |
| 428 | { { { 29, 0, 3 }, { 29, 30, 1 } } }, |
| 429 | { { { 29, 0, 4 }, { 29, 31, 1 } } }, |
| 430 | { { { 30, 0, 3 }, { 29, 31, 0 } } }, |
| 431 | { { { 30, 0, 2 }, { 29, 31, 1 } } }, |
| 432 | { { { 30, 0, 1 }, { 30, 30, 1 } } }, |
| 433 | { { { 30, 0, 0 }, { 30, 30, 0 } } }, |
| 434 | { { { 30, 0, 1 }, { 30, 31, 1 } } }, |
| 435 | { { { 30, 0, 2 }, { 30, 31, 0 } } }, |
| 436 | { { { 30, 0, 3 }, { 30, 31, 1 } } }, |
| 437 | { { { 30, 0, 4 }, { 31, 30, 1 } } }, |
| 438 | { { { 31, 0, 3 }, { 31, 30, 0 } } }, |
| 439 | { { { 31, 0, 2 }, { 31, 30, 1 } } }, |
| 440 | { { { 31, 0, 1 }, { 31, 31, 1 } } }, |
| 441 | { { { 31, 0, 0 }, { 31, 31, 0 } } } |
| 442 | }; |
| 443 | |
| 444 | static const DDSSingleColourLookup DDSLookup_6_4[] = |
| 445 | { |
| 446 | { { { 0, 0, 0 }, { 0, 0, 0 } } }, |
| 447 | { { { 0, 0, 1 }, { 0, 1, 0 } } }, |
| 448 | { { { 0, 0, 2 }, { 0, 2, 0 } } }, |
| 449 | { { { 1, 0, 1 }, { 0, 3, 1 } } }, |
| 450 | { { { 1, 0, 0 }, { 0, 3, 0 } } }, |
| 451 | { { { 1, 0, 1 }, { 0, 4, 0 } } }, |
| 452 | { { { 1, 0, 2 }, { 0, 5, 0 } } }, |
| 453 | { { { 2, 0, 1 }, { 0, 6, 1 } } }, |
| 454 | { { { 2, 0, 0 }, { 0, 6, 0 } } }, |
| 455 | { { { 2, 0, 1 }, { 0, 7, 0 } } }, |
| 456 | { { { 2, 0, 2 }, { 0, 8, 0 } } }, |
| 457 | { { { 3, 0, 1 }, { 0, 9, 1 } } }, |
| 458 | { { { 3, 0, 0 }, { 0, 9, 0 } } }, |
| 459 | { { { 3, 0, 1 }, { 0, 10, 0 } } }, |
| 460 | { { { 3, 0, 2 }, { 0, 11, 0 } } }, |
| 461 | { { { 4, 0, 1 }, { 0, 12, 1 } } }, |
| 462 | { { { 4, 0, 0 }, { 0, 12, 0 } } }, |
| 463 | { { { 4, 0, 1 }, { 0, 13, 0 } } }, |
| 464 | { { { 4, 0, 2 }, { 0, 14, 0 } } }, |
| 465 | { { { 5, 0, 1 }, { 0, 15, 1 } } }, |
| 466 | { { { 5, 0, 0 }, { 0, 15, 0 } } }, |
| 467 | { { { 5, 0, 1 }, { 0, 16, 0 } } }, |
| 468 | { { { 5, 0, 2 }, { 1, 15, 0 } } }, |
| 469 | { { { 6, 0, 1 }, { 0, 17, 0 } } }, |
| 470 | { { { 6, 0, 0 }, { 0, 18, 0 } } }, |
| 471 | { { { 6, 0, 1 }, { 0, 19, 0 } } }, |
| 472 | { { { 6, 0, 2 }, { 3, 14, 0 } } }, |
| 473 | { { { 7, 0, 1 }, { 0, 20, 0 } } }, |
| 474 | { { { 7, 0, 0 }, { 0, 21, 0 } } }, |
| 475 | { { { 7, 0, 1 }, { 0, 22, 0 } } }, |
| 476 | { { { 7, 0, 2 }, { 4, 15, 0 } } }, |
| 477 | { { { 8, 0, 1 }, { 0, 23, 0 } } }, |
| 478 | { { { 8, 0, 0 }, { 0, 24, 0 } } }, |
| 479 | { { { 8, 0, 1 }, { 0, 25, 0 } } }, |
| 480 | { { { 8, 0, 2 }, { 6, 14, 0 } } }, |
| 481 | { { { 9, 0, 1 }, { 0, 26, 0 } } }, |
| 482 | { { { 9, 0, 0 }, { 0, 27, 0 } } }, |
| 483 | { { { 9, 0, 1 }, { 0, 28, 0 } } }, |
| 484 | { { { 9, 0, 2 }, { 7, 15, 0 } } }, |
| 485 | { { { 10, 0, 1 }, { 0, 29, 0 } } }, |
| 486 | { { { 10, 0, 0 }, { 0, 30, 0 } } }, |
| 487 | { { { 10, 0, 1 }, { 0, 31, 0 } } }, |
| 488 | { { { 10, 0, 2 }, { 9, 14, 0 } } }, |
| 489 | { { { 11, 0, 1 }, { 0, 32, 0 } } }, |
| 490 | { { { 11, 0, 0 }, { 0, 33, 0 } } }, |
| 491 | { { { 11, 0, 1 }, { 2, 30, 0 } } }, |
| 492 | { { { 11, 0, 2 }, { 0, 34, 0 } } }, |
| 493 | { { { 12, 0, 1 }, { 0, 35, 0 } } }, |
| 494 | { { { 12, 0, 0 }, { 0, 36, 0 } } }, |
| 495 | { { { 12, 0, 1 }, { 3, 31, 0 } } }, |
| 496 | { { { 12, 0, 2 }, { 0, 37, 0 } } }, |
| 497 | { { { 13, 0, 1 }, { 0, 38, 0 } } }, |
| 498 | { { { 13, 0, 0 }, { 0, 39, 0 } } }, |
| 499 | { { { 13, 0, 1 }, { 5, 30, 0 } } }, |
| 500 | { { { 13, 0, 2 }, { 0, 40, 0 } } }, |
| 501 | { { { 14, 0, 1 }, { 0, 41, 0 } } }, |
| 502 | { { { 14, 0, 0 }, { 0, 42, 0 } } }, |
| 503 | { { { 14, 0, 1 }, { 6, 31, 0 } } }, |
| 504 | { { { 14, 0, 2 }, { 0, 43, 0 } } }, |
| 505 | { { { 15, 0, 1 }, { 0, 44, 0 } } }, |
| 506 | { { { 15, 0, 0 }, { 0, 45, 0 } } }, |
| 507 | { { { 15, 0, 1 }, { 8, 30, 0 } } }, |
| 508 | { { { 15, 0, 2 }, { 0, 46, 0 } } }, |
| 509 | { { { 16, 0, 2 }, { 0, 47, 0 } } }, |
| 510 | { { { 16, 0, 1 }, { 1, 46, 0 } } }, |
| 511 | { { { 16, 0, 0 }, { 0, 48, 0 } } }, |
| 512 | { { { 16, 0, 1 }, { 0, 49, 0 } } }, |
| 513 | { { { 16, 0, 2 }, { 0, 50, 0 } } }, |
| 514 | { { { 17, 0, 1 }, { 2, 47, 0 } } }, |
| 515 | { { { 17, 0, 0 }, { 0, 51, 0 } } }, |
| 516 | { { { 17, 0, 1 }, { 0, 52, 0 } } }, |
| 517 | { { { 17, 0, 2 }, { 0, 53, 0 } } }, |
| 518 | { { { 18, 0, 1 }, { 4, 46, 0 } } }, |
| 519 | { { { 18, 0, 0 }, { 0, 54, 0 } } }, |
| 520 | { { { 18, 0, 1 }, { 0, 55, 0 } } }, |
| 521 | { { { 18, 0, 2 }, { 0, 56, 0 } } }, |
| 522 | { { { 19, 0, 1 }, { 5, 47, 0 } } }, |
| 523 | { { { 19, 0, 0 }, { 0, 57, 0 } } }, |
| 524 | { { { 19, 0, 1 }, { 0, 58, 0 } } }, |
| 525 | { { { 19, 0, 2 }, { 0, 59, 0 } } }, |
| 526 | { { { 20, 0, 1 }, { 7, 46, 0 } } }, |
| 527 | { { { 20, 0, 0 }, { 0, 60, 0 } } }, |
| 528 | { { { 20, 0, 1 }, { 0, 61, 0 } } }, |
| 529 | { { { 20, 0, 2 }, { 0, 62, 0 } } }, |
| 530 | { { { 21, 0, 1 }, { 8, 47, 0 } } }, |
| 531 | { { { 21, 0, 0 }, { 0, 63, 0 } } }, |
| 532 | { { { 21, 0, 1 }, { 1, 62, 0 } } }, |
| 533 | { { { 21, 0, 2 }, { 1, 63, 0 } } }, |
| 534 | { { { 22, 0, 1 }, { 10, 46, 0 } } }, |
| 535 | { { { 22, 0, 0 }, { 2, 62, 0 } } }, |
| 536 | { { { 22, 0, 1 }, { 2, 63, 0 } } }, |
| 537 | { { { 22, 0, 2 }, { 3, 62, 0 } } }, |
| 538 | { { { 23, 0, 1 }, { 11, 47, 0 } } }, |
| 539 | { { { 23, 0, 0 }, { 3, 63, 0 } } }, |
| 540 | { { { 23, 0, 1 }, { 4, 62, 0 } } }, |
| 541 | { { { 23, 0, 2 }, { 4, 63, 0 } } }, |
| 542 | { { { 24, 0, 1 }, { 13, 46, 0 } } }, |
| 543 | { { { 24, 0, 0 }, { 5, 62, 0 } } }, |
| 544 | { { { 24, 0, 1 }, { 5, 63, 0 } } }, |
| 545 | { { { 24, 0, 2 }, { 6, 62, 0 } } }, |
| 546 | { { { 25, 0, 1 }, { 14, 47, 0 } } }, |
| 547 | { { { 25, 0, 0 }, { 6, 63, 0 } } }, |
| 548 | { { { 25, 0, 1 }, { 7, 62, 0 } } }, |
| 549 | { { { 25, 0, 2 }, { 7, 63, 0 } } }, |
| 550 | { { { 26, 0, 1 }, { 16, 45, 0 } } }, |
| 551 | { { { 26, 0, 0 }, { 8, 62, 0 } } }, |
| 552 | { { { 26, 0, 1 }, { 8, 63, 0 } } }, |
| 553 | { { { 26, 0, 2 }, { 9, 62, 0 } } }, |
| 554 | { { { 27, 0, 1 }, { 16, 48, 0 } } }, |
| 555 | { { { 27, 0, 0 }, { 9, 63, 0 } } }, |
| 556 | { { { 27, 0, 1 }, { 10, 62, 0 } } }, |
| 557 | { { { 27, 0, 2 }, { 10, 63, 0 } } }, |
| 558 | { { { 28, 0, 1 }, { 16, 51, 0 } } }, |
| 559 | { { { 28, 0, 0 }, { 11, 62, 0 } } }, |
| 560 | { { { 28, 0, 1 }, { 11, 63, 0 } } }, |
| 561 | { { { 28, 0, 2 }, { 12, 62, 0 } } }, |
| 562 | { { { 29, 0, 1 }, { 16, 54, 0 } } }, |
| 563 | { { { 29, 0, 0 }, { 12, 63, 0 } } }, |
| 564 | { { { 29, 0, 1 }, { 13, 62, 0 } } }, |
| 565 | { { { 29, 0, 2 }, { 13, 63, 0 } } }, |
| 566 | { { { 30, 0, 1 }, { 16, 57, 0 } } }, |
| 567 | { { { 30, 0, 0 }, { 14, 62, 0 } } }, |
| 568 | { { { 30, 0, 1 }, { 14, 63, 0 } } }, |
| 569 | { { { 30, 0, 2 }, { 15, 62, 0 } } }, |
| 570 | { { { 31, 0, 1 }, { 16, 60, 0 } } }, |
| 571 | { { { 31, 0, 0 }, { 15, 63, 0 } } }, |
| 572 | { { { 31, 0, 1 }, { 24, 46, 0 } } }, |
| 573 | { { { 31, 0, 2 }, { 16, 62, 0 } } }, |
| 574 | { { { 32, 0, 2 }, { 16, 63, 0 } } }, |
| 575 | { { { 32, 0, 1 }, { 17, 62, 0 } } }, |
| 576 | { { { 32, 0, 0 }, { 25, 47, 0 } } }, |
| 577 | { { { 32, 0, 1 }, { 17, 63, 0 } } }, |
| 578 | { { { 32, 0, 2 }, { 18, 62, 0 } } }, |
| 579 | { { { 33, 0, 1 }, { 18, 63, 0 } } }, |
| 580 | { { { 33, 0, 0 }, { 27, 46, 0 } } }, |
| 581 | { { { 33, 0, 1 }, { 19, 62, 0 } } }, |
| 582 | { { { 33, 0, 2 }, { 19, 63, 0 } } }, |
| 583 | { { { 34, 0, 1 }, { 20, 62, 0 } } }, |
| 584 | { { { 34, 0, 0 }, { 28, 47, 0 } } }, |
| 585 | { { { 34, 0, 1 }, { 20, 63, 0 } } }, |
| 586 | { { { 34, 0, 2 }, { 21, 62, 0 } } }, |
| 587 | { { { 35, 0, 1 }, { 21, 63, 0 } } }, |
| 588 | { { { 35, 0, 0 }, { 30, 46, 0 } } }, |
| 589 | { { { 35, 0, 1 }, { 22, 62, 0 } } }, |
| 590 | { { { 35, 0, 2 }, { 22, 63, 0 } } }, |
| 591 | { { { 36, 0, 1 }, { 23, 62, 0 } } }, |
| 592 | { { { 36, 0, 0 }, { 31, 47, 0 } } }, |
| 593 | { { { 36, 0, 1 }, { 23, 63, 0 } } }, |
| 594 | { { { 36, 0, 2 }, { 24, 62, 0 } } }, |
| 595 | { { { 37, 0, 1 }, { 24, 63, 0 } } }, |
| 596 | { { { 37, 0, 0 }, { 32, 47, 0 } } }, |
| 597 | { { { 37, 0, 1 }, { 25, 62, 0 } } }, |
| 598 | { { { 37, 0, 2 }, { 25, 63, 0 } } }, |
| 599 | { { { 38, 0, 1 }, { 26, 62, 0 } } }, |
| 600 | { { { 38, 0, 0 }, { 32, 50, 0 } } }, |
| 601 | { { { 38, 0, 1 }, { 26, 63, 0 } } }, |
| 602 | { { { 38, 0, 2 }, { 27, 62, 0 } } }, |
| 603 | { { { 39, 0, 1 }, { 27, 63, 0 } } }, |
| 604 | { { { 39, 0, 0 }, { 32, 53, 0 } } }, |
| 605 | { { { 39, 0, 1 }, { 28, 62, 0 } } }, |
| 606 | { { { 39, 0, 2 }, { 28, 63, 0 } } }, |
| 607 | { { { 40, 0, 1 }, { 29, 62, 0 } } }, |
| 608 | { { { 40, 0, 0 }, { 32, 56, 0 } } }, |
| 609 | { { { 40, 0, 1 }, { 29, 63, 0 } } }, |
| 610 | { { { 40, 0, 2 }, { 30, 62, 0 } } }, |
| 611 | { { { 41, 0, 1 }, { 30, 63, 0 } } }, |
| 612 | { { { 41, 0, 0 }, { 32, 59, 0 } } }, |
| 613 | { { { 41, 0, 1 }, { 31, 62, 0 } } }, |
| 614 | { { { 41, 0, 2 }, { 31, 63, 0 } } }, |
| 615 | { { { 42, 0, 1 }, { 32, 61, 0 } } }, |
| 616 | { { { 42, 0, 0 }, { 32, 62, 0 } } }, |
| 617 | { { { 42, 0, 1 }, { 32, 63, 0 } } }, |
| 618 | { { { 42, 0, 2 }, { 41, 46, 0 } } }, |
| 619 | { { { 43, 0, 1 }, { 33, 62, 0 } } }, |
| 620 | { { { 43, 0, 0 }, { 33, 63, 0 } } }, |
| 621 | { { { 43, 0, 1 }, { 34, 62, 0 } } }, |
| 622 | { { { 43, 0, 2 }, { 42, 47, 0 } } }, |
| 623 | { { { 44, 0, 1 }, { 34, 63, 0 } } }, |
| 624 | { { { 44, 0, 0 }, { 35, 62, 0 } } }, |
| 625 | { { { 44, 0, 1 }, { 35, 63, 0 } } }, |
| 626 | { { { 44, 0, 2 }, { 44, 46, 0 } } }, |
| 627 | { { { 45, 0, 1 }, { 36, 62, 0 } } }, |
| 628 | { { { 45, 0, 0 }, { 36, 63, 0 } } }, |
| 629 | { { { 45, 0, 1 }, { 37, 62, 0 } } }, |
| 630 | { { { 45, 0, 2 }, { 45, 47, 0 } } }, |
| 631 | { { { 46, 0, 1 }, { 37, 63, 0 } } }, |
| 632 | { { { 46, 0, 0 }, { 38, 62, 0 } } }, |
| 633 | { { { 46, 0, 1 }, { 38, 63, 0 } } }, |
| 634 | { { { 46, 0, 2 }, { 47, 46, 0 } } }, |
| 635 | { { { 47, 0, 1 }, { 39, 62, 0 } } }, |
| 636 | { { { 47, 0, 0 }, { 39, 63, 0 } } }, |
| 637 | { { { 47, 0, 1 }, { 40, 62, 0 } } }, |
| 638 | { { { 47, 0, 2 }, { 48, 46, 0 } } }, |
| 639 | { { { 48, 0, 2 }, { 40, 63, 0 } } }, |
| 640 | { { { 48, 0, 1 }, { 41, 62, 0 } } }, |
| 641 | { { { 48, 0, 0 }, { 41, 63, 0 } } }, |
| 642 | { { { 48, 0, 1 }, { 48, 49, 0 } } }, |
| 643 | { { { 48, 0, 2 }, { 42, 62, 0 } } }, |
| 644 | { { { 49, 0, 1 }, { 42, 63, 0 } } }, |
| 645 | { { { 49, 0, 0 }, { 43, 62, 0 } } }, |
| 646 | { { { 49, 0, 1 }, { 48, 52, 0 } } }, |
| 647 | { { { 49, 0, 2 }, { 43, 63, 0 } } }, |
| 648 | { { { 50, 0, 1 }, { 44, 62, 0 } } }, |
| 649 | { { { 50, 0, 0 }, { 44, 63, 0 } } }, |
| 650 | { { { 50, 0, 1 }, { 48, 55, 0 } } }, |
| 651 | { { { 50, 0, 2 }, { 45, 62, 0 } } }, |
| 652 | { { { 51, 0, 1 }, { 45, 63, 0 } } }, |
| 653 | { { { 51, 0, 0 }, { 46, 62, 0 } } }, |
| 654 | { { { 51, 0, 1 }, { 48, 58, 0 } } }, |
| 655 | { { { 51, 0, 2 }, { 46, 63, 0 } } }, |
| 656 | { { { 52, 0, 1 }, { 47, 62, 0 } } }, |
| 657 | { { { 52, 0, 0 }, { 47, 63, 0 } } }, |
| 658 | { { { 52, 0, 1 }, { 48, 61, 0 } } }, |
| 659 | { { { 52, 0, 2 }, { 48, 62, 0 } } }, |
| 660 | { { { 53, 0, 1 }, { 56, 47, 0 } } }, |
| 661 | { { { 53, 0, 0 }, { 48, 63, 0 } } }, |
| 662 | { { { 53, 0, 1 }, { 49, 62, 0 } } }, |
| 663 | { { { 53, 0, 2 }, { 49, 63, 0 } } }, |
| 664 | { { { 54, 0, 1 }, { 58, 46, 0 } } }, |
| 665 | { { { 54, 0, 0 }, { 50, 62, 0 } } }, |
| 666 | { { { 54, 0, 1 }, { 50, 63, 0 } } }, |
| 667 | { { { 54, 0, 2 }, { 51, 62, 0 } } }, |
| 668 | { { { 55, 0, 1 }, { 59, 47, 0 } } }, |
| 669 | { { { 55, 0, 0 }, { 51, 63, 0 } } }, |
| 670 | { { { 55, 0, 1 }, { 52, 62, 0 } } }, |
| 671 | { { { 55, 0, 2 }, { 52, 63, 0 } } }, |
| 672 | { { { 56, 0, 1 }, { 61, 46, 0 } } }, |
| 673 | { { { 56, 0, 0 }, { 53, 62, 0 } } }, |
| 674 | { { { 56, 0, 1 }, { 53, 63, 0 } } }, |
| 675 | { { { 56, 0, 2 }, { 54, 62, 0 } } }, |
| 676 | { { { 57, 0, 1 }, { 62, 47, 0 } } }, |
| 677 | { { { 57, 0, 0 }, { 54, 63, 0 } } }, |
| 678 | { { { 57, 0, 1 }, { 55, 62, 0 } } }, |
| 679 | { { { 57, 0, 2 }, { 55, 63, 0 } } }, |
| 680 | { { { 58, 0, 1 }, { 56, 62, 1 } } }, |
| 681 | { { { 58, 0, 0 }, { 56, 62, 0 } } }, |
| 682 | { { { 58, 0, 1 }, { 56, 63, 0 } } }, |
| 683 | { { { 58, 0, 2 }, { 57, 62, 0 } } }, |
| 684 | { { { 59, 0, 1 }, { 57, 63, 1 } } }, |
| 685 | { { { 59, 0, 0 }, { 57, 63, 0 } } }, |
| 686 | { { { 59, 0, 1 }, { 58, 62, 0 } } }, |
| 687 | { { { 59, 0, 2 }, { 58, 63, 0 } } }, |
| 688 | { { { 60, 0, 1 }, { 59, 62, 1 } } }, |
| 689 | { { { 60, 0, 0 }, { 59, 62, 0 } } }, |
| 690 | { { { 60, 0, 1 }, { 59, 63, 0 } } }, |
| 691 | { { { 60, 0, 2 }, { 60, 62, 0 } } }, |
| 692 | { { { 61, 0, 1 }, { 60, 63, 1 } } }, |
| 693 | { { { 61, 0, 0 }, { 60, 63, 0 } } }, |
| 694 | { { { 61, 0, 1 }, { 61, 62, 0 } } }, |
| 695 | { { { 61, 0, 2 }, { 61, 63, 0 } } }, |
| 696 | { { { 62, 0, 1 }, { 62, 62, 1 } } }, |
| 697 | { { { 62, 0, 0 }, { 62, 62, 0 } } }, |
| 698 | { { { 62, 0, 1 }, { 62, 63, 0 } } }, |
| 699 | { { { 62, 0, 2 }, { 63, 62, 0 } } }, |
| 700 | { { { 63, 0, 1 }, { 63, 63, 1 } } }, |
| 701 | { { { 63, 0, 0 }, { 63, 63, 0 } } } |
| 702 | }; |
| 703 | |
| 704 | static const DDSSingleColourLookup* |
| 705 | DDS_LOOKUP[] = |
| 706 | { |
| 707 | DDSLookup_5_4, |
| 708 | DDSLookup_6_4, |
| 709 | DDSLookup_5_4 |
| 710 | }; |
| 711 | |
| 712 | /* |
| 713 | Macros |
| 714 | */ |
| 715 | #define C565_r(x) (((x) & 0xF800) >> 11) |
| 716 | #define C565_g(x) (((x) & 0x07E0) >> 5) |
| 717 | #define C565_b(x) ((x) & 0x001F) |
| 718 | |
| 719 | #define C565_red(x) ( (C565_r(x) << 3 | C565_r(x) >> 2)) |
| 720 | #define C565_green(x) ( (C565_g(x) << 2 | C565_g(x) >> 4)) |
| 721 | #define C565_blue(x) ( (C565_b(x) << 3 | C565_b(x) >> 2)) |
| 722 | |
| 723 | #define DIV2(x) ((x) > 1 ? ((x) >> 1) : 1) |
| 724 | |
| 725 | #define FixRange(min, max, steps) \ |
| 726 | if (min > max) \ |
| 727 | min = max; \ |
Cristy | 1e22ca4 | 2016-12-24 12:18:47 -0500 | [diff] [blame] | 728 | if ((ssize_t) max - min < steps) \ |
dirk | ee6b4cb | 2014-12-29 21:00:08 +0000 | [diff] [blame] | 729 | max = MagickMin(min + steps, 255); \ |
Cristy | 1e22ca4 | 2016-12-24 12:18:47 -0500 | [diff] [blame] | 730 | if ((ssize_t) max - min < steps) \ |
Cristy | 8ccc748 | 2016-12-25 10:49:01 -0500 | [diff] [blame] | 731 | min = MagickMax(0, (ssize_t) max - steps) |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 732 | |
| 733 | #define Dot(left, right) (left.x*right.x) + (left.y*right.y) + (left.z*right.z) |
| 734 | |
| 735 | #define VectorInit(vector, value) vector.x = vector.y = vector.z = vector.w \ |
| 736 | = value |
| 737 | #define VectorInit3(vector, value) vector.x = vector.y = vector.z = value |
| 738 | |
| 739 | #define IsBitMask(mask, r, g, b, a) (mask.r_bitmask == r && mask.g_bitmask == \ |
| 740 | g && mask.b_bitmask == b && mask.alpha_bitmask == a) |
| 741 | |
| 742 | /* |
| 743 | Forward declarations |
| 744 | */ |
dirk | ee6b4cb | 2014-12-29 21:00:08 +0000 | [diff] [blame] | 745 | /* |
| 746 | Forward declarations |
| 747 | */ |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 748 | static MagickBooleanType |
dirk | ee6b4cb | 2014-12-29 21:00:08 +0000 | [diff] [blame] | 749 | ConstructOrdering(const size_t,const DDSVector4 *,const DDSVector3, |
| 750 | DDSVector4 *, DDSVector4 *, unsigned char *, size_t), |
| 751 | ReadDDSInfo(Image *,DDSInfo *), |
| 752 | ReadDXT1(Image *,DDSInfo *,ExceptionInfo *), |
| 753 | ReadDXT3(Image *,DDSInfo *,ExceptionInfo *), |
| 754 | ReadDXT5(Image *,DDSInfo *,ExceptionInfo *), |
| 755 | ReadUncompressedRGB(Image *,DDSInfo *,ExceptionInfo *), |
| 756 | ReadUncompressedRGBA(Image *,DDSInfo *,ExceptionInfo *), |
| 757 | SkipDXTMipmaps(Image *,DDSInfo *,int,ExceptionInfo *), |
| 758 | SkipRGBMipmaps(Image *,DDSInfo *,int,ExceptionInfo *), |
| 759 | WriteDDSImage(const ImageInfo *,Image *,ExceptionInfo *), |
| 760 | WriteMipmaps(Image *,const size_t,const size_t,const size_t, |
| 761 | const MagickBooleanType,const MagickBooleanType,ExceptionInfo *); |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 762 | |
| 763 | static void |
dirk | ee6b4cb | 2014-12-29 21:00:08 +0000 | [diff] [blame] | 764 | RemapIndices(const ssize_t *,const unsigned char *,unsigned char *), |
| 765 | WriteDDSInfo(Image *,const size_t,const size_t,const size_t), |
| 766 | WriteFourCC(Image *,const size_t,const MagickBooleanType, |
| 767 | const MagickBooleanType,ExceptionInfo *), |
| 768 | WriteImageData(Image *,const size_t,const size_t,const MagickBooleanType, |
| 769 | const MagickBooleanType,ExceptionInfo *), |
| 770 | WriteIndices(Image *,const DDSVector3,const DDSVector3,unsigned char *), |
| 771 | WriteSingleColorFit(Image *,const DDSVector4 *,const ssize_t *), |
| 772 | WriteUncompressed(Image *,ExceptionInfo *); |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 773 | |
| 774 | static inline void VectorAdd(const DDSVector4 left, const DDSVector4 right, |
| 775 | DDSVector4 *destination) |
| 776 | { |
| 777 | destination->x = left.x + right.x; |
| 778 | destination->y = left.y + right.y; |
| 779 | destination->z = left.z + right.z; |
| 780 | destination->w = left.w + right.w; |
| 781 | } |
| 782 | |
| 783 | static inline void VectorClamp(DDSVector4 *value) |
| 784 | { |
dirk | ee6b4cb | 2014-12-29 21:00:08 +0000 | [diff] [blame] | 785 | value->x = MagickMin(1.0f,MagickMax(0.0f,value->x)); |
| 786 | value->y = MagickMin(1.0f,MagickMax(0.0f,value->y)); |
| 787 | value->z = MagickMin(1.0f,MagickMax(0.0f,value->z)); |
| 788 | value->w = MagickMin(1.0f,MagickMax(0.0f,value->w)); |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | static inline void VectorClamp3(DDSVector3 *value) |
| 792 | { |
dirk | ee6b4cb | 2014-12-29 21:00:08 +0000 | [diff] [blame] | 793 | value->x = MagickMin(1.0f,MagickMax(0.0f,value->x)); |
| 794 | value->y = MagickMin(1.0f,MagickMax(0.0f,value->y)); |
| 795 | value->z = MagickMin(1.0f,MagickMax(0.0f,value->z)); |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | static inline void VectorCopy43(const DDSVector4 source, |
| 799 | DDSVector3 *destination) |
| 800 | { |
| 801 | destination->x = source.x; |
| 802 | destination->y = source.y; |
| 803 | destination->z = source.z; |
| 804 | } |
| 805 | |
| 806 | static inline void VectorCopy44(const DDSVector4 source, |
| 807 | DDSVector4 *destination) |
| 808 | { |
| 809 | destination->x = source.x; |
| 810 | destination->y = source.y; |
| 811 | destination->z = source.z; |
| 812 | destination->w = source.w; |
| 813 | } |
| 814 | |
| 815 | static inline void VectorNegativeMultiplySubtract(const DDSVector4 a, |
| 816 | const DDSVector4 b, const DDSVector4 c, DDSVector4 *destination) |
| 817 | { |
| 818 | destination->x = c.x - (a.x * b.x); |
| 819 | destination->y = c.y - (a.y * b.y); |
| 820 | destination->z = c.z - (a.z * b.z); |
| 821 | destination->w = c.w - (a.w * b.w); |
| 822 | } |
| 823 | |
| 824 | static inline void VectorMultiply(const DDSVector4 left, |
| 825 | const DDSVector4 right, DDSVector4 *destination) |
| 826 | { |
| 827 | destination->x = left.x * right.x; |
| 828 | destination->y = left.y * right.y; |
| 829 | destination->z = left.z * right.z; |
| 830 | destination->w = left.w * right.w; |
| 831 | } |
| 832 | |
| 833 | static inline void VectorMultiply3(const DDSVector3 left, |
| 834 | const DDSVector3 right, DDSVector3 *destination) |
| 835 | { |
| 836 | destination->x = left.x * right.x; |
| 837 | destination->y = left.y * right.y; |
| 838 | destination->z = left.z * right.z; |
| 839 | } |
| 840 | |
| 841 | static inline void VectorMultiplyAdd(const DDSVector4 a, const DDSVector4 b, |
| 842 | const DDSVector4 c, DDSVector4 *destination) |
| 843 | { |
| 844 | destination->x = (a.x * b.x) + c.x; |
| 845 | destination->y = (a.y * b.y) + c.y; |
| 846 | destination->z = (a.z * b.z) + c.z; |
| 847 | destination->w = (a.w * b.w) + c.w; |
| 848 | } |
| 849 | |
| 850 | static inline void VectorMultiplyAdd3(const DDSVector3 a, const DDSVector3 b, |
| 851 | const DDSVector3 c, DDSVector3 *destination) |
| 852 | { |
| 853 | destination->x = (a.x * b.x) + c.x; |
| 854 | destination->y = (a.y * b.y) + c.y; |
| 855 | destination->z = (a.z * b.z) + c.z; |
| 856 | } |
| 857 | |
| 858 | static inline void VectorReciprocal(const DDSVector4 value, |
| 859 | DDSVector4 *destination) |
| 860 | { |
| 861 | destination->x = 1.0f / value.x; |
| 862 | destination->y = 1.0f / value.y; |
| 863 | destination->z = 1.0f / value.z; |
| 864 | destination->w = 1.0f / value.w; |
| 865 | } |
| 866 | |
| 867 | static inline void VectorSubtract(const DDSVector4 left, |
| 868 | const DDSVector4 right, DDSVector4 *destination) |
| 869 | { |
| 870 | destination->x = left.x - right.x; |
| 871 | destination->y = left.y - right.y; |
| 872 | destination->z = left.z - right.z; |
| 873 | destination->w = left.w - right.w; |
| 874 | } |
| 875 | |
| 876 | static inline void VectorSubtract3(const DDSVector3 left, |
| 877 | const DDSVector3 right, DDSVector3 *destination) |
| 878 | { |
| 879 | destination->x = left.x - right.x; |
| 880 | destination->y = left.y - right.y; |
| 881 | destination->z = left.z - right.z; |
| 882 | } |
| 883 | |
| 884 | static inline void VectorTruncate(DDSVector4 *value) |
| 885 | { |
| 886 | value->x = value->x > 0.0f ? floor(value->x) : ceil(value->x); |
| 887 | value->y = value->y > 0.0f ? floor(value->y) : ceil(value->y); |
| 888 | value->z = value->z > 0.0f ? floor(value->z) : ceil(value->z); |
| 889 | value->w = value->w > 0.0f ? floor(value->w) : ceil(value->w); |
| 890 | } |
| 891 | |
| 892 | static inline void VectorTruncate3(DDSVector3 *value) |
| 893 | { |
| 894 | value->x = value->x > 0.0f ? floor(value->x) : ceil(value->x); |
| 895 | value->y = value->y > 0.0f ? floor(value->y) : ceil(value->y); |
| 896 | value->z = value->z > 0.0f ? floor(value->z) : ceil(value->z); |
| 897 | } |
| 898 | |
| 899 | static void CalculateColors(unsigned short c0, unsigned short c1, |
| 900 | DDSColors *c, MagickBooleanType ignoreAlpha) |
| 901 | { |
| 902 | c->a[0] = c->a[1] = c->a[2] = c->a[3] = 0; |
| 903 | |
| 904 | c->r[0] = (unsigned char) C565_red(c0); |
| 905 | c->g[0] = (unsigned char) C565_green(c0); |
| 906 | c->b[0] = (unsigned char) C565_blue(c0); |
| 907 | |
| 908 | c->r[1] = (unsigned char) C565_red(c1); |
| 909 | c->g[1] = (unsigned char) C565_green(c1); |
| 910 | c->b[1] = (unsigned char) C565_blue(c1); |
| 911 | |
| 912 | if (ignoreAlpha != MagickFalse || c0 > c1) |
| 913 | { |
| 914 | c->r[2] = (unsigned char) ((2 * c->r[0] + c->r[1]) / 3); |
| 915 | c->g[2] = (unsigned char) ((2 * c->g[0] + c->g[1]) / 3); |
| 916 | c->b[2] = (unsigned char) ((2 * c->b[0] + c->b[1]) / 3); |
| 917 | |
| 918 | c->r[3] = (unsigned char) ((c->r[0] + 2 * c->r[1]) / 3); |
| 919 | c->g[3] = (unsigned char) ((c->g[0] + 2 * c->g[1]) / 3); |
| 920 | c->b[3] = (unsigned char) ((c->b[0] + 2 * c->b[1]) / 3); |
| 921 | } |
| 922 | else |
| 923 | { |
| 924 | c->r[2] = (unsigned char) ((c->r[0] + c->r[1]) / 2); |
| 925 | c->g[2] = (unsigned char) ((c->g[0] + c->g[1]) / 2); |
| 926 | c->b[2] = (unsigned char) ((c->b[0] + c->b[1]) / 2); |
| 927 | |
| 928 | c->r[3] = c->g[3] = c->b[3] = 0; |
| 929 | c->a[3] = 255; |
| 930 | } |
| 931 | } |
| 932 | |
| 933 | static size_t CompressAlpha(const size_t min, const size_t max, |
| 934 | const size_t steps, const ssize_t *alphas, unsigned char* indices) |
| 935 | { |
| 936 | unsigned char |
| 937 | codes[8]; |
| 938 | |
| 939 | register ssize_t |
| 940 | i; |
| 941 | |
| 942 | size_t |
| 943 | error, |
| 944 | index, |
| 945 | j, |
| 946 | least, |
| 947 | value; |
| 948 | |
| 949 | codes[0] = (unsigned char) min; |
| 950 | codes[1] = (unsigned char) max; |
| 951 | codes[6] = 0; |
| 952 | codes[7] = 255; |
| 953 | |
| 954 | for (i=1; i < (ssize_t) steps; i++) |
| 955 | codes[i+1] = (unsigned char) (((steps-i)*min + i*max) / steps); |
| 956 | |
| 957 | error = 0; |
| 958 | for (i=0; i<16; i++) |
| 959 | { |
| 960 | if (alphas[i] == -1) |
| 961 | { |
| 962 | indices[i] = 0; |
| 963 | continue; |
| 964 | } |
| 965 | |
| 966 | value = alphas[i]; |
| 967 | least = SIZE_MAX; |
| 968 | index = 0; |
| 969 | for (j=0; j<8; j++) |
| 970 | { |
| 971 | size_t |
| 972 | dist; |
| 973 | |
| 974 | dist = value - (size_t)codes[j]; |
| 975 | dist *= dist; |
| 976 | |
| 977 | if (dist < least) |
| 978 | { |
| 979 | least = dist; |
| 980 | index = j; |
| 981 | } |
| 982 | } |
| 983 | |
| 984 | indices[i] = (unsigned char)index; |
| 985 | error += least; |
| 986 | } |
| 987 | |
| 988 | return error; |
| 989 | } |
| 990 | |
| 991 | static void CompressClusterFit(const size_t count, |
| 992 | const DDSVector4 *points, const ssize_t *map, const DDSVector3 principle, |
| 993 | const DDSVector4 metric, DDSVector3 *start, DDSVector3* end, |
| 994 | unsigned char *indices) |
| 995 | { |
| 996 | DDSVector3 |
| 997 | axis; |
| 998 | |
| 999 | DDSVector4 |
| 1000 | grid, |
| 1001 | gridrcp, |
| 1002 | half, |
| 1003 | onethird_onethird2, |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 1004 | pointsWeights[16], |
| 1005 | two, |
| 1006 | twonineths, |
| 1007 | twothirds_twothirds2, |
| 1008 | xSumwSum; |
| 1009 | |
| 1010 | float |
| 1011 | bestError = 1e+37f; |
| 1012 | |
| 1013 | size_t |
| 1014 | bestIteration = 0, |
| 1015 | besti = 0, |
| 1016 | bestj = 0, |
| 1017 | bestk = 0, |
dirk | 560a910 | 2014-12-16 21:55:06 +0000 | [diff] [blame] | 1018 | iterationIndex; |
| 1019 | |
| 1020 | ssize_t |
| 1021 | i; |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 1022 | |
| 1023 | unsigned char |
| 1024 | *o, |
| 1025 | order[128], |
| 1026 | unordered[16]; |
| 1027 | |
| 1028 | VectorInit(half,0.5f); |
| 1029 | VectorInit(two,2.0f); |
| 1030 | |
| 1031 | VectorInit(onethird_onethird2,1.0f/3.0f); |
| 1032 | onethird_onethird2.w = 1.0f/9.0f; |
| 1033 | VectorInit(twothirds_twothirds2,2.0f/3.0f); |
| 1034 | twothirds_twothirds2.w = 4.0f/9.0f; |
| 1035 | VectorInit(twonineths,2.0f/9.0f); |
| 1036 | |
| 1037 | grid.x = 31.0f; |
| 1038 | grid.y = 63.0f; |
| 1039 | grid.z = 31.0f; |
| 1040 | grid.w = 0.0f; |
| 1041 | |
| 1042 | gridrcp.x = 1.0f/31.0f; |
| 1043 | gridrcp.y = 1.0f/63.0f; |
| 1044 | gridrcp.z = 1.0f/31.0f; |
| 1045 | gridrcp.w = 0.0f; |
| 1046 | |
| 1047 | xSumwSum.x = 0.0f; |
| 1048 | xSumwSum.y = 0.0f; |
| 1049 | xSumwSum.z = 0.0f; |
| 1050 | xSumwSum.w = 0.0f; |
| 1051 | |
| 1052 | ConstructOrdering(count,points,principle,pointsWeights,&xSumwSum,order,0); |
| 1053 | |
| 1054 | for (iterationIndex = 0;;) |
| 1055 | { |
dirk | 560a910 | 2014-12-16 21:55:06 +0000 | [diff] [blame] | 1056 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 1057 | #pragma omp parallel for schedule(dynamic,1) \ |
| 1058 | num_threads(GetMagickResourceLimit(ThreadResource)) |
| 1059 | #endif |
| 1060 | for (i=0; i < (ssize_t) count; i++) |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 1061 | { |
dirk | 560a910 | 2014-12-16 21:55:06 +0000 | [diff] [blame] | 1062 | DDSVector4 |
| 1063 | part0, |
| 1064 | part1, |
| 1065 | part2; |
| 1066 | |
| 1067 | size_t |
| 1068 | ii, |
| 1069 | j, |
| 1070 | k, |
| 1071 | kmin; |
| 1072 | |
| 1073 | VectorInit(part0,0.0f); |
| 1074 | for(ii=0; ii < (size_t) i; ii++) |
| 1075 | VectorAdd(pointsWeights[ii],part0,&part0); |
| 1076 | |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 1077 | VectorInit(part1,0.0f); |
dirk | 560a910 | 2014-12-16 21:55:06 +0000 | [diff] [blame] | 1078 | for (j=(size_t) i;;) |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 1079 | { |
| 1080 | if (j == 0) |
| 1081 | { |
| 1082 | VectorCopy44(pointsWeights[0],&part2); |
| 1083 | kmin = 1; |
| 1084 | } |
| 1085 | else |
| 1086 | { |
| 1087 | VectorInit(part2,0.0f); |
| 1088 | kmin = j; |
| 1089 | } |
| 1090 | |
| 1091 | for (k=kmin;;) |
| 1092 | { |
| 1093 | DDSVector4 |
| 1094 | a, |
| 1095 | alpha2_sum, |
| 1096 | alphax_sum, |
| 1097 | alphabeta_sum, |
| 1098 | b, |
| 1099 | beta2_sum, |
| 1100 | betax_sum, |
| 1101 | e1, |
| 1102 | e2, |
dirk | 560a910 | 2014-12-16 21:55:06 +0000 | [diff] [blame] | 1103 | factor, |
| 1104 | part3; |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 1105 | |
| 1106 | float |
| 1107 | error; |
| 1108 | |
| 1109 | VectorSubtract(xSumwSum,part2,&part3); |
| 1110 | VectorSubtract(part3,part1,&part3); |
| 1111 | VectorSubtract(part3,part0,&part3); |
| 1112 | |
| 1113 | VectorMultiplyAdd(part1,twothirds_twothirds2,part0,&alphax_sum); |
| 1114 | VectorMultiplyAdd(part2,onethird_onethird2,alphax_sum,&alphax_sum); |
| 1115 | VectorInit(alpha2_sum,alphax_sum.w); |
| 1116 | |
| 1117 | VectorMultiplyAdd(part2,twothirds_twothirds2,part3,&betax_sum); |
| 1118 | VectorMultiplyAdd(part1,onethird_onethird2,betax_sum,&betax_sum); |
| 1119 | VectorInit(beta2_sum,betax_sum.w); |
| 1120 | |
| 1121 | VectorAdd(part1,part2,&alphabeta_sum); |
| 1122 | VectorInit(alphabeta_sum,alphabeta_sum.w); |
| 1123 | VectorMultiply(twonineths,alphabeta_sum,&alphabeta_sum); |
| 1124 | |
| 1125 | VectorMultiply(alpha2_sum,beta2_sum,&factor); |
| 1126 | VectorNegativeMultiplySubtract(alphabeta_sum,alphabeta_sum,factor, |
| 1127 | &factor); |
| 1128 | VectorReciprocal(factor,&factor); |
| 1129 | |
| 1130 | VectorMultiply(alphax_sum,beta2_sum,&a); |
| 1131 | VectorNegativeMultiplySubtract(betax_sum,alphabeta_sum,a,&a); |
| 1132 | VectorMultiply(a,factor,&a); |
| 1133 | |
| 1134 | VectorMultiply(betax_sum,alpha2_sum,&b); |
| 1135 | VectorNegativeMultiplySubtract(alphax_sum,alphabeta_sum,b,&b); |
| 1136 | VectorMultiply(b,factor,&b); |
| 1137 | |
| 1138 | VectorClamp(&a); |
| 1139 | VectorMultiplyAdd(grid,a,half,&a); |
| 1140 | VectorTruncate(&a); |
| 1141 | VectorMultiply(a,gridrcp,&a); |
| 1142 | |
| 1143 | VectorClamp(&b); |
| 1144 | VectorMultiplyAdd(grid,b,half,&b); |
| 1145 | VectorTruncate(&b); |
| 1146 | VectorMultiply(b,gridrcp,&b); |
| 1147 | |
| 1148 | VectorMultiply(b,b,&e1); |
| 1149 | VectorMultiply(e1,beta2_sum,&e1); |
| 1150 | VectorMultiply(a,a,&e2); |
| 1151 | VectorMultiplyAdd(e2,alpha2_sum,e1,&e1); |
| 1152 | |
| 1153 | VectorMultiply(a,b,&e2); |
| 1154 | VectorMultiply(e2,alphabeta_sum,&e2); |
| 1155 | VectorNegativeMultiplySubtract(a,alphax_sum,e2,&e2); |
| 1156 | VectorNegativeMultiplySubtract(b,betax_sum,e2,&e2); |
| 1157 | VectorMultiplyAdd(two,e2,e1,&e2); |
| 1158 | VectorMultiply(e2,metric,&e2); |
| 1159 | |
| 1160 | error = e2.x + e2.y + e2.z; |
| 1161 | |
| 1162 | if (error < bestError) |
dirk | 560a910 | 2014-12-16 21:55:06 +0000 | [diff] [blame] | 1163 | { |
| 1164 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 1165 | #pragma omp critical (DDS_CompressClusterFit) |
| 1166 | #endif |
| 1167 | { |
| 1168 | if (error < bestError) |
| 1169 | { |
| 1170 | VectorCopy43(a,start); |
| 1171 | VectorCopy43(b,end); |
| 1172 | bestError = error; |
| 1173 | besti = i; |
| 1174 | bestj = j; |
| 1175 | bestk = k; |
| 1176 | bestIteration = iterationIndex; |
| 1177 | } |
| 1178 | } |
| 1179 | } |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 1180 | |
| 1181 | if (k == count) |
| 1182 | break; |
| 1183 | |
| 1184 | VectorAdd(pointsWeights[k],part2,&part2); |
| 1185 | k++; |
| 1186 | } |
| 1187 | |
| 1188 | if (j == count) |
| 1189 | break; |
| 1190 | |
| 1191 | VectorAdd(pointsWeights[j],part1,&part1); |
| 1192 | j++; |
| 1193 | } |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 1194 | } |
| 1195 | |
| 1196 | if (bestIteration != iterationIndex) |
| 1197 | break; |
| 1198 | |
| 1199 | iterationIndex++; |
| 1200 | if (iterationIndex == 8) |
| 1201 | break; |
| 1202 | |
| 1203 | VectorSubtract3(*end,*start,&axis); |
| 1204 | if (ConstructOrdering(count,points,axis,pointsWeights,&xSumwSum,order, |
| 1205 | iterationIndex) == MagickFalse) |
| 1206 | break; |
| 1207 | } |
| 1208 | |
| 1209 | o = order + (16*bestIteration); |
| 1210 | |
cristy | 385d866 | 2014-12-22 19:49:01 +0000 | [diff] [blame] | 1211 | for (i=0; i < (ssize_t) besti; i++) |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 1212 | unordered[o[i]] = 0; |
cristy | 385d866 | 2014-12-22 19:49:01 +0000 | [diff] [blame] | 1213 | for (i=besti; i < (ssize_t) bestj; i++) |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 1214 | unordered[o[i]] = 2; |
cristy | 385d866 | 2014-12-22 19:49:01 +0000 | [diff] [blame] | 1215 | for (i=bestj; i < (ssize_t) bestk; i++) |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 1216 | unordered[o[i]] = 3; |
cristy | 385d866 | 2014-12-22 19:49:01 +0000 | [diff] [blame] | 1217 | for (i=bestk; i < (ssize_t) count; i++) |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 1218 | unordered[o[i]] = 1; |
| 1219 | |
| 1220 | RemapIndices(map,unordered,indices); |
| 1221 | } |
| 1222 | |
| 1223 | static void CompressRangeFit(const size_t count, |
| 1224 | const DDSVector4* points, const ssize_t *map, const DDSVector3 principle, |
| 1225 | const DDSVector4 metric, DDSVector3 *start, DDSVector3 *end, |
| 1226 | unsigned char *indices) |
| 1227 | { |
| 1228 | float |
| 1229 | d, |
| 1230 | bestDist, |
| 1231 | max, |
| 1232 | min, |
| 1233 | val; |
| 1234 | |
| 1235 | DDSVector3 |
| 1236 | codes[4], |
| 1237 | grid, |
| 1238 | gridrcp, |
| 1239 | half, |
| 1240 | dist; |
| 1241 | |
| 1242 | register ssize_t |
| 1243 | i; |
| 1244 | |
| 1245 | size_t |
| 1246 | bestj, |
| 1247 | j; |
| 1248 | |
| 1249 | unsigned char |
| 1250 | closest[16]; |
| 1251 | |
| 1252 | VectorInit3(half,0.5f); |
| 1253 | |
| 1254 | grid.x = 31.0f; |
| 1255 | grid.y = 63.0f; |
| 1256 | grid.z = 31.0f; |
| 1257 | |
| 1258 | gridrcp.x = 1.0f/31.0f; |
| 1259 | gridrcp.y = 1.0f/63.0f; |
| 1260 | gridrcp.z = 1.0f/31.0f; |
| 1261 | |
| 1262 | if (count > 0) |
| 1263 | { |
| 1264 | VectorCopy43(points[0],start); |
| 1265 | VectorCopy43(points[0],end); |
| 1266 | |
| 1267 | min = max = Dot(points[0],principle); |
| 1268 | for (i=1; i < (ssize_t) count; i++) |
| 1269 | { |
| 1270 | val = Dot(points[i],principle); |
| 1271 | if (val < min) |
| 1272 | { |
| 1273 | VectorCopy43(points[i],start); |
| 1274 | min = val; |
| 1275 | } |
| 1276 | else if (val > max) |
| 1277 | { |
| 1278 | VectorCopy43(points[i],end); |
| 1279 | max = val; |
| 1280 | } |
| 1281 | } |
| 1282 | } |
| 1283 | |
| 1284 | VectorClamp3(start); |
| 1285 | VectorMultiplyAdd3(grid,*start,half,start); |
| 1286 | VectorTruncate3(start); |
| 1287 | VectorMultiply3(*start,gridrcp,start); |
| 1288 | |
| 1289 | VectorClamp3(end); |
| 1290 | VectorMultiplyAdd3(grid,*end,half,end); |
| 1291 | VectorTruncate3(end); |
| 1292 | VectorMultiply3(*end,gridrcp,end); |
| 1293 | |
| 1294 | codes[0] = *start; |
| 1295 | codes[1] = *end; |
| 1296 | codes[2].x = (start->x * (2.0f/3.0f)) + (end->x * (1.0f/3.0f)); |
| 1297 | codes[2].y = (start->y * (2.0f/3.0f)) + (end->y * (1.0f/3.0f)); |
| 1298 | codes[2].z = (start->z * (2.0f/3.0f)) + (end->z * (1.0f/3.0f)); |
| 1299 | codes[3].x = (start->x * (1.0f/3.0f)) + (end->x * (2.0f/3.0f)); |
| 1300 | codes[3].y = (start->y * (1.0f/3.0f)) + (end->y * (2.0f/3.0f)); |
| 1301 | codes[3].z = (start->z * (1.0f/3.0f)) + (end->z * (2.0f/3.0f)); |
| 1302 | |
| 1303 | for (i=0; i < (ssize_t) count; i++) |
| 1304 | { |
| 1305 | bestDist = 1e+37f; |
| 1306 | bestj = 0; |
| 1307 | for (j=0; j < 4; j++) |
| 1308 | { |
| 1309 | dist.x = (points[i].x - codes[j].x) * metric.x; |
| 1310 | dist.y = (points[i].y - codes[j].y) * metric.y; |
| 1311 | dist.z = (points[i].z - codes[j].z) * metric.z; |
| 1312 | |
| 1313 | d = Dot(dist,dist); |
| 1314 | if (d < bestDist) |
| 1315 | { |
| 1316 | bestDist = d; |
| 1317 | bestj = j; |
| 1318 | } |
| 1319 | } |
| 1320 | |
| 1321 | closest[i] = (unsigned char) bestj; |
| 1322 | } |
| 1323 | |
| 1324 | RemapIndices(map, closest, indices); |
| 1325 | } |
| 1326 | |
| 1327 | static void ComputeEndPoints(const DDSSingleColourLookup *lookup[], |
| 1328 | const unsigned char *color, DDSVector3 *start, DDSVector3 *end, |
| 1329 | unsigned char *index) |
| 1330 | { |
| 1331 | register ssize_t |
| 1332 | i; |
| 1333 | |
| 1334 | size_t |
| 1335 | c, |
| 1336 | maxError = SIZE_MAX; |
| 1337 | |
| 1338 | for (i=0; i < 2; i++) |
| 1339 | { |
| 1340 | const DDSSourceBlock* |
| 1341 | sources[3]; |
| 1342 | |
| 1343 | size_t |
| 1344 | error = 0; |
| 1345 | |
| 1346 | for (c=0; c < 3; c++) |
| 1347 | { |
| 1348 | sources[c] = &lookup[c][color[c]].sources[i]; |
| 1349 | error += ((size_t) sources[c]->error) * ((size_t) sources[c]->error); |
| 1350 | } |
| 1351 | |
| 1352 | if (error > maxError) |
| 1353 | continue; |
| 1354 | |
| 1355 | start->x = (float) sources[0]->start / 31.0f; |
| 1356 | start->y = (float) sources[1]->start / 63.0f; |
| 1357 | start->z = (float) sources[2]->start / 31.0f; |
| 1358 | |
| 1359 | end->x = (float) sources[0]->end / 31.0f; |
| 1360 | end->y = (float) sources[1]->end / 63.0f; |
| 1361 | end->z = (float) sources[2]->end / 31.0f; |
| 1362 | |
| 1363 | *index = (unsigned char) (2*i); |
| 1364 | maxError = error; |
| 1365 | } |
| 1366 | } |
| 1367 | |
| 1368 | static void ComputePrincipleComponent(const float *covariance, |
| 1369 | DDSVector3 *principle) |
| 1370 | { |
| 1371 | DDSVector4 |
| 1372 | row0, |
| 1373 | row1, |
| 1374 | row2, |
| 1375 | v; |
| 1376 | |
| 1377 | register ssize_t |
| 1378 | i; |
| 1379 | |
| 1380 | row0.x = covariance[0]; |
| 1381 | row0.y = covariance[1]; |
| 1382 | row0.z = covariance[2]; |
| 1383 | row0.w = 0.0f; |
| 1384 | |
| 1385 | row1.x = covariance[1]; |
| 1386 | row1.y = covariance[3]; |
| 1387 | row1.z = covariance[4]; |
| 1388 | row1.w = 0.0f; |
| 1389 | |
| 1390 | row2.x = covariance[2]; |
| 1391 | row2.y = covariance[4]; |
| 1392 | row2.z = covariance[5]; |
| 1393 | row2.w = 0.0f; |
| 1394 | |
| 1395 | VectorInit(v,1.0f); |
| 1396 | |
| 1397 | for (i=0; i < 8; i++) |
| 1398 | { |
| 1399 | DDSVector4 |
| 1400 | w; |
| 1401 | |
| 1402 | float |
| 1403 | a; |
| 1404 | |
| 1405 | w.x = row0.x * v.x; |
| 1406 | w.y = row0.y * v.x; |
| 1407 | w.z = row0.z * v.x; |
| 1408 | w.w = row0.w * v.x; |
| 1409 | |
| 1410 | w.x = (row1.x * v.y) + w.x; |
| 1411 | w.y = (row1.y * v.y) + w.y; |
| 1412 | w.z = (row1.z * v.y) + w.z; |
| 1413 | w.w = (row1.w * v.y) + w.w; |
| 1414 | |
| 1415 | w.x = (row2.x * v.z) + w.x; |
| 1416 | w.y = (row2.y * v.z) + w.y; |
| 1417 | w.z = (row2.z * v.z) + w.z; |
| 1418 | w.w = (row2.w * v.z) + w.w; |
| 1419 | |
dirk | ee6b4cb | 2014-12-29 21:00:08 +0000 | [diff] [blame] | 1420 | a = 1.0f / MagickMax(w.x,MagickMax(w.y,w.z)); |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 1421 | |
| 1422 | v.x = w.x * a; |
| 1423 | v.y = w.y * a; |
| 1424 | v.z = w.z * a; |
| 1425 | v.w = w.w * a; |
| 1426 | } |
| 1427 | |
| 1428 | VectorCopy43(v,principle); |
| 1429 | } |
| 1430 | |
| 1431 | static void ComputeWeightedCovariance(const size_t count, |
| 1432 | const DDSVector4 *points, float *covariance) |
| 1433 | { |
| 1434 | DDSVector3 |
| 1435 | centroid; |
| 1436 | |
| 1437 | float |
| 1438 | total; |
| 1439 | |
| 1440 | size_t |
| 1441 | i; |
| 1442 | |
| 1443 | total = 0.0f; |
| 1444 | VectorInit3(centroid,0.0f); |
| 1445 | |
| 1446 | for (i=0; i < count; i++) |
| 1447 | { |
| 1448 | total += points[i].w; |
| 1449 | centroid.x += (points[i].x * points[i].w); |
| 1450 | centroid.y += (points[i].y * points[i].w); |
| 1451 | centroid.z += (points[i].z * points[i].w); |
| 1452 | } |
| 1453 | |
| 1454 | if( total > 1.192092896e-07F) |
| 1455 | { |
| 1456 | centroid.x /= total; |
| 1457 | centroid.y /= total; |
| 1458 | centroid.z /= total; |
| 1459 | } |
| 1460 | |
| 1461 | for (i=0; i < 6; i++) |
| 1462 | covariance[i] = 0.0f; |
| 1463 | |
| 1464 | for (i = 0; i < count; i++) |
| 1465 | { |
| 1466 | DDSVector3 |
| 1467 | a, |
| 1468 | b; |
| 1469 | |
| 1470 | a.x = points[i].x - centroid.x; |
| 1471 | a.y = points[i].y - centroid.y; |
| 1472 | a.z = points[i].z - centroid.z; |
| 1473 | |
| 1474 | b.x = points[i].w * a.x; |
| 1475 | b.y = points[i].w * a.y; |
| 1476 | b.z = points[i].w * a.z; |
| 1477 | |
| 1478 | covariance[0] += a.x*b.x; |
| 1479 | covariance[1] += a.x*b.y; |
| 1480 | covariance[2] += a.x*b.z; |
| 1481 | covariance[3] += a.y*b.y; |
| 1482 | covariance[4] += a.y*b.z; |
| 1483 | covariance[5] += a.z*b.z; |
| 1484 | } |
| 1485 | } |
| 1486 | |
| 1487 | static MagickBooleanType ConstructOrdering(const size_t count, |
| 1488 | const DDSVector4 *points, const DDSVector3 axis, DDSVector4 *pointsWeights, |
| 1489 | DDSVector4 *xSumwSum, unsigned char *order, size_t iteration) |
| 1490 | { |
| 1491 | float |
| 1492 | dps[16], |
| 1493 | f; |
| 1494 | |
| 1495 | register ssize_t |
| 1496 | i; |
| 1497 | |
| 1498 | size_t |
| 1499 | j; |
| 1500 | |
| 1501 | unsigned char |
| 1502 | c, |
| 1503 | *o, |
| 1504 | *p; |
| 1505 | |
| 1506 | o = order + (16*iteration); |
| 1507 | |
| 1508 | for (i=0; i < (ssize_t) count; i++) |
| 1509 | { |
| 1510 | dps[i] = Dot(points[i],axis); |
| 1511 | o[i] = (unsigned char)i; |
| 1512 | } |
| 1513 | |
| 1514 | for (i=0; i < (ssize_t) count; i++) |
| 1515 | { |
| 1516 | for (j=i; j > 0 && dps[j] < dps[j - 1]; j--) |
| 1517 | { |
| 1518 | f = dps[j]; |
| 1519 | dps[j] = dps[j - 1]; |
| 1520 | dps[j - 1] = f; |
| 1521 | |
| 1522 | c = o[j]; |
| 1523 | o[j] = o[j - 1]; |
| 1524 | o[j - 1] = c; |
| 1525 | } |
| 1526 | } |
| 1527 | |
| 1528 | for (i=0; i < (ssize_t) iteration; i++) |
| 1529 | { |
| 1530 | MagickBooleanType |
| 1531 | same; |
| 1532 | |
| 1533 | p = order + (16*i); |
| 1534 | same = MagickTrue; |
| 1535 | |
| 1536 | for (j=0; j < count; j++) |
| 1537 | { |
| 1538 | if (o[j] != p[j]) |
| 1539 | { |
| 1540 | same = MagickFalse; |
| 1541 | break; |
| 1542 | } |
| 1543 | } |
| 1544 | |
| 1545 | if (same != MagickFalse) |
| 1546 | return MagickFalse; |
| 1547 | } |
| 1548 | |
| 1549 | xSumwSum->x = 0; |
| 1550 | xSumwSum->y = 0; |
| 1551 | xSumwSum->z = 0; |
| 1552 | xSumwSum->w = 0; |
| 1553 | |
| 1554 | for (i=0; i < (ssize_t) count; i++) |
| 1555 | { |
| 1556 | DDSVector4 |
| 1557 | v; |
| 1558 | |
| 1559 | j = (size_t) o[i]; |
| 1560 | |
| 1561 | v.x = points[j].w * points[j].x; |
| 1562 | v.y = points[j].w * points[j].y; |
| 1563 | v.z = points[j].w * points[j].z; |
| 1564 | v.w = points[j].w * 1.0f; |
| 1565 | |
| 1566 | VectorCopy44(v,&pointsWeights[i]); |
| 1567 | VectorAdd(*xSumwSum,v,xSumwSum); |
| 1568 | } |
| 1569 | |
| 1570 | return MagickTrue; |
| 1571 | } |
| 1572 | |
| 1573 | /* |
| 1574 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1575 | % % |
| 1576 | % % |
| 1577 | % % |
| 1578 | % I s D D S % |
| 1579 | % % |
| 1580 | % % |
| 1581 | % % |
| 1582 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1583 | % |
| 1584 | % IsDDS() returns MagickTrue if the image format type, identified by the |
| 1585 | % magick string, is DDS. |
| 1586 | % |
| 1587 | % The format of the IsDDS method is: |
| 1588 | % |
| 1589 | % MagickBooleanType IsDDS(const unsigned char *magick,const size_t length) |
| 1590 | % |
| 1591 | % A description of each parameter follows: |
| 1592 | % |
| 1593 | % o magick: compare image format pattern against these bytes. |
| 1594 | % |
| 1595 | % o length: Specifies the length of the magick string. |
| 1596 | % |
| 1597 | */ |
| 1598 | static MagickBooleanType IsDDS(const unsigned char *magick, const size_t length) |
| 1599 | { |
| 1600 | if (length < 4) |
| 1601 | return(MagickFalse); |
| 1602 | if (LocaleNCompare((char *) magick,"DDS ", 4) == 0) |
| 1603 | return(MagickTrue); |
| 1604 | return(MagickFalse); |
| 1605 | } |
| 1606 | /* |
| 1607 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1608 | % % |
| 1609 | % % |
| 1610 | % % |
| 1611 | % R e a d D D S I m a g e % |
| 1612 | % % |
| 1613 | % % |
| 1614 | % % |
| 1615 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1616 | % |
| 1617 | % ReadDDSImage() reads a DirectDraw Surface image file and returns it. It |
| 1618 | % allocates the memory necessary for the new Image structure and returns a |
| 1619 | % pointer to the new image. |
| 1620 | % |
| 1621 | % The format of the ReadDDSImage method is: |
| 1622 | % |
| 1623 | % Image *ReadDDSImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 1624 | % |
| 1625 | % A description of each parameter follows: |
| 1626 | % |
| 1627 | % o image_info: The image info. |
| 1628 | % |
| 1629 | % o exception: return any errors or warnings in this structure. |
| 1630 | % |
| 1631 | */ |
| 1632 | |
| 1633 | static Image *ReadDDSImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 1634 | { |
| 1635 | Image |
| 1636 | *image; |
| 1637 | |
| 1638 | MagickBooleanType |
| 1639 | status, |
| 1640 | cubemap = MagickFalse, |
| 1641 | volume = MagickFalse; |
| 1642 | |
| 1643 | CompressionType |
| 1644 | compression; |
| 1645 | |
| 1646 | DDSInfo |
| 1647 | dds_info; |
| 1648 | |
| 1649 | DDSDecoder |
| 1650 | *decoder; |
| 1651 | |
| 1652 | PixelTrait |
| 1653 | alpha_trait; |
| 1654 | |
| 1655 | size_t |
| 1656 | n, |
| 1657 | num_images; |
| 1658 | |
| 1659 | /* |
| 1660 | Open image file. |
| 1661 | */ |
| 1662 | assert(image_info != (const ImageInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 1663 | assert(image_info->signature == MagickCoreSignature); |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 1664 | if (image_info->debug != MagickFalse) |
| 1665 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
| 1666 | image_info->filename); |
| 1667 | assert(exception != (ExceptionInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 1668 | assert(exception->signature == MagickCoreSignature); |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 1669 | image=AcquireImage(image_info,exception); |
| 1670 | status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); |
| 1671 | if (status == MagickFalse) |
| 1672 | { |
| 1673 | image=DestroyImageList(image); |
| 1674 | return((Image *) NULL); |
| 1675 | } |
| 1676 | |
| 1677 | /* |
| 1678 | Initialize image structure. |
| 1679 | */ |
| 1680 | if (ReadDDSInfo(image, &dds_info) != MagickTrue) { |
| 1681 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 1682 | } |
| 1683 | |
| 1684 | if (dds_info.ddscaps2 & DDSCAPS2_CUBEMAP) |
| 1685 | cubemap = MagickTrue; |
| 1686 | |
| 1687 | if (dds_info.ddscaps2 & DDSCAPS2_VOLUME && dds_info.depth > 0) |
| 1688 | volume = MagickTrue; |
| 1689 | |
| 1690 | (void) SeekBlob(image, 128, SEEK_SET); |
| 1691 | |
| 1692 | /* |
| 1693 | Determine pixel format |
| 1694 | */ |
| 1695 | if (dds_info.pixelformat.flags & DDPF_RGB) |
| 1696 | { |
| 1697 | compression = NoCompression; |
| 1698 | if (dds_info.pixelformat.flags & DDPF_ALPHAPIXELS) |
| 1699 | { |
| 1700 | alpha_trait = BlendPixelTrait; |
| 1701 | decoder = ReadUncompressedRGBA; |
| 1702 | } |
| 1703 | else |
| 1704 | { |
| 1705 | alpha_trait = UndefinedPixelTrait; |
| 1706 | decoder = ReadUncompressedRGB; |
| 1707 | } |
| 1708 | } |
dirk | 6398fb4 | 2014-09-08 18:19:39 +0000 | [diff] [blame] | 1709 | else if (dds_info.pixelformat.flags & DDPF_LUMINANCE) |
| 1710 | { |
| 1711 | compression = NoCompression; |
| 1712 | if (dds_info.pixelformat.flags & DDPF_ALPHAPIXELS) |
| 1713 | { |
| 1714 | /* Not sure how to handle this */ |
| 1715 | ThrowReaderException(CorruptImageError, "ImageTypeNotSupported"); |
| 1716 | } |
| 1717 | else |
| 1718 | { |
| 1719 | alpha_trait = UndefinedPixelTrait; |
| 1720 | decoder = ReadUncompressedRGB; |
| 1721 | } |
| 1722 | } |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 1723 | else if (dds_info.pixelformat.flags & DDPF_FOURCC) |
| 1724 | { |
| 1725 | switch (dds_info.pixelformat.fourcc) |
| 1726 | { |
| 1727 | case FOURCC_DXT1: |
| 1728 | { |
| 1729 | alpha_trait = UndefinedPixelTrait; |
| 1730 | compression = DXT1Compression; |
| 1731 | decoder = ReadDXT1; |
| 1732 | break; |
| 1733 | } |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 1734 | case FOURCC_DXT3: |
| 1735 | { |
| 1736 | alpha_trait = BlendPixelTrait; |
| 1737 | compression = DXT3Compression; |
| 1738 | decoder = ReadDXT3; |
| 1739 | break; |
| 1740 | } |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 1741 | case FOURCC_DXT5: |
| 1742 | { |
| 1743 | alpha_trait = BlendPixelTrait; |
| 1744 | compression = DXT5Compression; |
| 1745 | decoder = ReadDXT5; |
| 1746 | break; |
| 1747 | } |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 1748 | default: |
| 1749 | { |
| 1750 | /* Unknown FOURCC */ |
| 1751 | ThrowReaderException(CorruptImageError, "ImageTypeNotSupported"); |
| 1752 | } |
| 1753 | } |
| 1754 | } |
| 1755 | else |
| 1756 | { |
| 1757 | /* Neither compressed nor uncompressed... thus unsupported */ |
| 1758 | ThrowReaderException(CorruptImageError, "ImageTypeNotSupported"); |
| 1759 | } |
| 1760 | |
| 1761 | num_images = 1; |
| 1762 | if (cubemap) |
| 1763 | { |
| 1764 | /* |
| 1765 | Determine number of faces defined in the cubemap |
| 1766 | */ |
| 1767 | num_images = 0; |
| 1768 | if (dds_info.ddscaps2 & DDSCAPS2_CUBEMAP_POSITIVEX) num_images++; |
| 1769 | if (dds_info.ddscaps2 & DDSCAPS2_CUBEMAP_NEGATIVEX) num_images++; |
| 1770 | if (dds_info.ddscaps2 & DDSCAPS2_CUBEMAP_POSITIVEY) num_images++; |
| 1771 | if (dds_info.ddscaps2 & DDSCAPS2_CUBEMAP_NEGATIVEY) num_images++; |
| 1772 | if (dds_info.ddscaps2 & DDSCAPS2_CUBEMAP_POSITIVEZ) num_images++; |
| 1773 | if (dds_info.ddscaps2 & DDSCAPS2_CUBEMAP_NEGATIVEZ) num_images++; |
| 1774 | } |
| 1775 | |
| 1776 | if (volume) |
| 1777 | num_images = dds_info.depth; |
| 1778 | |
| 1779 | for (n = 0; n < num_images; n++) |
| 1780 | { |
| 1781 | if (n != 0) |
| 1782 | { |
| 1783 | /* Start a new image */ |
| 1784 | AcquireNextImage(image_info,image,exception); |
| 1785 | if (GetNextImageInList(image) == (Image *) NULL) |
| 1786 | return(DestroyImageList(image)); |
| 1787 | image=SyncNextImageInList(image); |
| 1788 | } |
| 1789 | |
| 1790 | image->alpha_trait=alpha_trait; |
| 1791 | image->compression = compression; |
| 1792 | image->columns = dds_info.width; |
| 1793 | image->rows = dds_info.height; |
| 1794 | image->storage_class = DirectClass; |
| 1795 | image->endian = LSBEndian; |
| 1796 | image->depth = 8; |
| 1797 | if (image_info->ping != MagickFalse) |
| 1798 | { |
| 1799 | (void) CloseBlob(image); |
| 1800 | return(GetFirstImageInList(image)); |
| 1801 | } |
cristy | acabb84 | 2014-12-14 23:36:33 +0000 | [diff] [blame] | 1802 | status=SetImageExtent(image,image->columns,image->rows,exception); |
| 1803 | if (status == MagickFalse) |
| 1804 | return(DestroyImageList(image)); |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 1805 | if ((decoder)(image, &dds_info, exception) != MagickTrue) |
| 1806 | { |
| 1807 | (void) CloseBlob(image); |
| 1808 | return(GetFirstImageInList(image)); |
| 1809 | } |
| 1810 | } |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 1811 | (void) CloseBlob(image); |
| 1812 | return(GetFirstImageInList(image)); |
| 1813 | } |
| 1814 | |
| 1815 | static MagickBooleanType ReadDDSInfo(Image *image, DDSInfo *dds_info) |
| 1816 | { |
| 1817 | size_t |
| 1818 | hdr_size, |
| 1819 | required; |
| 1820 | |
| 1821 | /* Seek to start of header */ |
| 1822 | (void) SeekBlob(image, 4, SEEK_SET); |
| 1823 | |
| 1824 | /* Check header field */ |
| 1825 | hdr_size = ReadBlobLSBLong(image); |
| 1826 | if (hdr_size != 124) |
| 1827 | return MagickFalse; |
| 1828 | |
| 1829 | /* Fill in DDS info struct */ |
| 1830 | dds_info->flags = ReadBlobLSBLong(image); |
| 1831 | |
| 1832 | /* Check required flags */ |
| 1833 | required=(size_t) (DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT); |
| 1834 | if ((dds_info->flags & required) != required) |
| 1835 | return MagickFalse; |
| 1836 | |
| 1837 | dds_info->height = ReadBlobLSBLong(image); |
| 1838 | dds_info->width = ReadBlobLSBLong(image); |
| 1839 | dds_info->pitchOrLinearSize = ReadBlobLSBLong(image); |
| 1840 | dds_info->depth = ReadBlobLSBLong(image); |
| 1841 | dds_info->mipmapcount = ReadBlobLSBLong(image); |
| 1842 | |
| 1843 | (void) SeekBlob(image, 44, SEEK_CUR); /* reserved region of 11 DWORDs */ |
| 1844 | |
| 1845 | /* Read pixel format structure */ |
| 1846 | hdr_size = ReadBlobLSBLong(image); |
| 1847 | if (hdr_size != 32) |
| 1848 | return MagickFalse; |
| 1849 | |
| 1850 | dds_info->pixelformat.flags = ReadBlobLSBLong(image); |
| 1851 | dds_info->pixelformat.fourcc = ReadBlobLSBLong(image); |
| 1852 | dds_info->pixelformat.rgb_bitcount = ReadBlobLSBLong(image); |
| 1853 | dds_info->pixelformat.r_bitmask = ReadBlobLSBLong(image); |
| 1854 | dds_info->pixelformat.g_bitmask = ReadBlobLSBLong(image); |
| 1855 | dds_info->pixelformat.b_bitmask = ReadBlobLSBLong(image); |
| 1856 | dds_info->pixelformat.alpha_bitmask = ReadBlobLSBLong(image); |
| 1857 | |
| 1858 | dds_info->ddscaps1 = ReadBlobLSBLong(image); |
| 1859 | dds_info->ddscaps2 = ReadBlobLSBLong(image); |
| 1860 | (void) SeekBlob(image, 12, SEEK_CUR); /* 3 reserved DWORDs */ |
| 1861 | |
| 1862 | return MagickTrue; |
| 1863 | } |
| 1864 | |
dirk | ea2173b | 2016-08-29 15:26:37 +0200 | [diff] [blame] | 1865 | static MagickBooleanType SetDXT1Pixels(Image *image,ssize_t x,ssize_t y, |
| 1866 | DDSColors colors,size_t bits,Quantum *q) |
| 1867 | { |
| 1868 | register ssize_t |
| 1869 | i; |
| 1870 | |
| 1871 | ssize_t |
| 1872 | j; |
| 1873 | |
| 1874 | unsigned char |
| 1875 | code; |
| 1876 | |
| 1877 | for (j = 0; j < 4; j++) |
| 1878 | { |
| 1879 | for (i = 0; i < 4; i++) |
| 1880 | { |
dirk | 2c3d3bc | 2016-08-29 20:58:36 +0200 | [diff] [blame] | 1881 | if ((x + i) < (ssize_t) image->columns && |
| 1882 | (y + j) < (ssize_t) image->rows) |
dirk | ea2173b | 2016-08-29 15:26:37 +0200 | [diff] [blame] | 1883 | { |
| 1884 | code=(unsigned char) ((bits >> ((j*4+i)*2)) & 0x3); |
| 1885 | SetPixelRed(image,ScaleCharToQuantum(colors.r[code]),q); |
| 1886 | SetPixelGreen(image,ScaleCharToQuantum(colors.g[code]),q); |
| 1887 | SetPixelBlue(image,ScaleCharToQuantum(colors.b[code]),q); |
| 1888 | SetPixelOpacity(image,ScaleCharToQuantum(colors.a[code]),q); |
| 1889 | if ((colors.a[code] != 0) && |
| 1890 | (image->alpha_trait == UndefinedPixelTrait)) |
| 1891 | return(MagickFalse); |
| 1892 | q+=GetPixelChannels(image); |
| 1893 | } |
| 1894 | } |
| 1895 | } |
| 1896 | return(MagickTrue); |
| 1897 | } |
| 1898 | |
| 1899 | static MagickBooleanType ReadDXT1(Image *image,DDSInfo *dds_info, |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 1900 | ExceptionInfo *exception) |
| 1901 | { |
| 1902 | DDSColors |
| 1903 | colors; |
| 1904 | |
| 1905 | register Quantum |
| 1906 | *q; |
dirk | e1097a6 | 2016-08-29 14:48:34 +0200 | [diff] [blame] | 1907 | |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 1908 | register ssize_t |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 1909 | x; |
dirk | e1097a6 | 2016-08-29 14:48:34 +0200 | [diff] [blame] | 1910 | |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 1911 | size_t |
| 1912 | bits; |
| 1913 | |
| 1914 | ssize_t |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 1915 | y; |
dirk | e1097a6 | 2016-08-29 14:48:34 +0200 | [diff] [blame] | 1916 | |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 1917 | unsigned short |
| 1918 | c0, |
| 1919 | c1; |
dirk | e1097a6 | 2016-08-29 14:48:34 +0200 | [diff] [blame] | 1920 | |
dirk | 2c3d3bc | 2016-08-29 20:58:36 +0200 | [diff] [blame] | 1921 | for (y = 0; y < (ssize_t) image->rows; y += 4) |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 1922 | { |
dirk | 2c3d3bc | 2016-08-29 20:58:36 +0200 | [diff] [blame] | 1923 | for (x = 0; x < (ssize_t) image->columns; x += 4) |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 1924 | { |
| 1925 | /* Get 4x4 patch of pixels to write on */ |
dirk | 2c3d3bc | 2016-08-29 20:58:36 +0200 | [diff] [blame] | 1926 | q=QueueAuthenticPixels(image,x,y,MagickMin(4,image->columns-x), |
| 1927 | MagickMin(4,image->rows-y),exception); |
dirk | e1097a6 | 2016-08-29 14:48:34 +0200 | [diff] [blame] | 1928 | |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 1929 | if (q == (Quantum *) NULL) |
| 1930 | return MagickFalse; |
dirk | e1097a6 | 2016-08-29 14:48:34 +0200 | [diff] [blame] | 1931 | |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 1932 | /* Read 8 bytes of data from the image */ |
dirk | e1097a6 | 2016-08-29 14:48:34 +0200 | [diff] [blame] | 1933 | c0=ReadBlobLSBShort(image); |
| 1934 | c1=ReadBlobLSBShort(image); |
| 1935 | bits=ReadBlobLSBLong(image); |
| 1936 | |
| 1937 | CalculateColors(c0,c1,&colors,MagickFalse); |
| 1938 | |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 1939 | /* Write the pixels */ |
dirk | ea2173b | 2016-08-29 15:26:37 +0200 | [diff] [blame] | 1940 | if (SetDXT1Pixels(image,x,y,colors,bits,q) == MagickFalse) |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 1941 | { |
dirk | ea2173b | 2016-08-29 15:26:37 +0200 | [diff] [blame] | 1942 | /* Correct alpha */ |
| 1943 | SetImageAlpha(image,QuantumRange,exception); |
dirk | 2c3d3bc | 2016-08-29 20:58:36 +0200 | [diff] [blame] | 1944 | q=QueueAuthenticPixels(image,x,y,MagickMin(4,image->columns-x), |
| 1945 | MagickMin(4,image->rows-y),exception); |
Cristy | a99f53f | 2016-12-24 12:05:52 -0500 | [diff] [blame] | 1946 | if (q != (Quantum *) NULL) |
| 1947 | SetDXT1Pixels(image,x,y,colors,bits,q); |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 1948 | } |
dirk | e1097a6 | 2016-08-29 14:48:34 +0200 | [diff] [blame] | 1949 | |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 1950 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 1951 | return MagickFalse; |
| 1952 | } |
| 1953 | } |
| 1954 | |
dirk | ee6b4cb | 2014-12-29 21:00:08 +0000 | [diff] [blame] | 1955 | return(SkipDXTMipmaps(image,dds_info,8,exception)); |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 1956 | } |
| 1957 | |
| 1958 | static MagickBooleanType ReadDXT3(Image *image, DDSInfo *dds_info, |
| 1959 | ExceptionInfo *exception) |
| 1960 | { |
| 1961 | DDSColors |
| 1962 | colors; |
| 1963 | |
| 1964 | register Quantum |
| 1965 | *q; |
| 1966 | |
| 1967 | register ssize_t |
| 1968 | i, |
| 1969 | x; |
| 1970 | |
| 1971 | unsigned char |
| 1972 | alpha; |
| 1973 | |
| 1974 | size_t |
| 1975 | a0, |
| 1976 | a1, |
| 1977 | bits, |
| 1978 | code; |
| 1979 | |
| 1980 | ssize_t |
| 1981 | j, |
| 1982 | y; |
| 1983 | |
| 1984 | unsigned short |
| 1985 | c0, |
| 1986 | c1; |
| 1987 | |
| 1988 | for (y = 0; y < (ssize_t) dds_info->height; y += 4) |
| 1989 | { |
| 1990 | for (x = 0; x < (ssize_t) dds_info->width; x += 4) |
| 1991 | { |
| 1992 | /* Get 4x4 patch of pixels to write on */ |
dirk | ee6b4cb | 2014-12-29 21:00:08 +0000 | [diff] [blame] | 1993 | q = QueueAuthenticPixels(image, x, y, MagickMin(4, dds_info->width - x), |
| 1994 | MagickMin(4, dds_info->height - y),exception); |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 1995 | |
| 1996 | if (q == (Quantum *) NULL) |
| 1997 | return MagickFalse; |
| 1998 | |
| 1999 | /* Read alpha values (8 bytes) */ |
| 2000 | a0 = ReadBlobLSBLong(image); |
| 2001 | a1 = ReadBlobLSBLong(image); |
| 2002 | |
| 2003 | /* Read 8 bytes of data from the image */ |
| 2004 | c0 = ReadBlobLSBShort(image); |
| 2005 | c1 = ReadBlobLSBShort(image); |
| 2006 | bits = ReadBlobLSBLong(image); |
| 2007 | |
| 2008 | CalculateColors(c0, c1, &colors, MagickTrue); |
| 2009 | |
| 2010 | /* Write the pixels */ |
| 2011 | for (j = 0; j < 4; j++) |
| 2012 | { |
| 2013 | for (i = 0; i < 4; i++) |
| 2014 | { |
| 2015 | if ((x + i) < (ssize_t) dds_info->width && (y + j) < (ssize_t) dds_info->height) |
| 2016 | { |
| 2017 | code = (bits >> ((4*j+i)*2)) & 0x3; |
| 2018 | SetPixelRed(image,ScaleCharToQuantum(colors.r[code]),q); |
| 2019 | SetPixelGreen(image,ScaleCharToQuantum(colors.g[code]),q); |
| 2020 | SetPixelBlue(image,ScaleCharToQuantum(colors.b[code]),q); |
| 2021 | /* |
| 2022 | Extract alpha value: multiply 0..15 by 17 to get range 0..255 |
| 2023 | */ |
| 2024 | if (j < 2) |
| 2025 | alpha = 17U * (unsigned char) ((a0 >> (4*(4*j+i))) & 0xf); |
| 2026 | else |
| 2027 | alpha = 17U * (unsigned char) ((a1 >> (4*(4*(j-2)+i))) & 0xf); |
| 2028 | SetPixelAlpha(image,ScaleCharToQuantum((unsigned char) alpha),q); |
| 2029 | q+=GetPixelChannels(image); |
| 2030 | } |
| 2031 | } |
| 2032 | } |
| 2033 | |
| 2034 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 2035 | return MagickFalse; |
| 2036 | } |
| 2037 | } |
| 2038 | |
dirk | ee6b4cb | 2014-12-29 21:00:08 +0000 | [diff] [blame] | 2039 | return(SkipDXTMipmaps(image,dds_info,16,exception)); |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 2040 | } |
| 2041 | |
| 2042 | static MagickBooleanType ReadDXT5(Image *image, DDSInfo *dds_info, |
| 2043 | ExceptionInfo *exception) |
| 2044 | { |
| 2045 | DDSColors |
| 2046 | colors; |
| 2047 | |
| 2048 | MagickSizeType |
| 2049 | alpha_bits; |
| 2050 | |
| 2051 | register Quantum |
| 2052 | *q; |
| 2053 | |
| 2054 | register ssize_t |
| 2055 | i, |
| 2056 | x; |
| 2057 | |
| 2058 | unsigned char |
| 2059 | a0, |
| 2060 | a1; |
| 2061 | |
| 2062 | size_t |
| 2063 | alpha, |
| 2064 | bits, |
| 2065 | code, |
| 2066 | alpha_code; |
| 2067 | |
| 2068 | ssize_t |
| 2069 | j, |
| 2070 | y; |
| 2071 | |
| 2072 | unsigned short |
| 2073 | c0, |
| 2074 | c1; |
| 2075 | |
| 2076 | for (y = 0; y < (ssize_t) dds_info->height; y += 4) |
| 2077 | { |
| 2078 | for (x = 0; x < (ssize_t) dds_info->width; x += 4) |
| 2079 | { |
| 2080 | /* Get 4x4 patch of pixels to write on */ |
dirk | ee6b4cb | 2014-12-29 21:00:08 +0000 | [diff] [blame] | 2081 | q = QueueAuthenticPixels(image, x, y, MagickMin(4, dds_info->width - x), |
| 2082 | MagickMin(4, dds_info->height - y),exception); |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 2083 | |
| 2084 | if (q == (Quantum *) NULL) |
| 2085 | return MagickFalse; |
| 2086 | |
| 2087 | /* Read alpha values (8 bytes) */ |
| 2088 | a0 = (unsigned char) ReadBlobByte(image); |
| 2089 | a1 = (unsigned char) ReadBlobByte(image); |
| 2090 | |
| 2091 | alpha_bits = (MagickSizeType)ReadBlobLSBLong(image); |
| 2092 | alpha_bits = alpha_bits | ((MagickSizeType)ReadBlobLSBShort(image) << 32); |
| 2093 | |
| 2094 | /* Read 8 bytes of data from the image */ |
| 2095 | c0 = ReadBlobLSBShort(image); |
| 2096 | c1 = ReadBlobLSBShort(image); |
| 2097 | bits = ReadBlobLSBLong(image); |
| 2098 | |
| 2099 | CalculateColors(c0, c1, &colors, MagickTrue); |
| 2100 | |
| 2101 | /* Write the pixels */ |
| 2102 | for (j = 0; j < 4; j++) |
| 2103 | { |
| 2104 | for (i = 0; i < 4; i++) |
| 2105 | { |
| 2106 | if ((x + i) < (ssize_t) dds_info->width && |
| 2107 | (y + j) < (ssize_t) dds_info->height) |
| 2108 | { |
| 2109 | code = (bits >> ((4*j+i)*2)) & 0x3; |
| 2110 | SetPixelRed(image,ScaleCharToQuantum(colors.r[code]),q); |
| 2111 | SetPixelGreen(image,ScaleCharToQuantum(colors.g[code]),q); |
| 2112 | SetPixelBlue(image,ScaleCharToQuantum(colors.b[code]),q); |
| 2113 | /* Extract alpha value */ |
| 2114 | alpha_code = (size_t) (alpha_bits >> (3*(4*j+i))) & 0x7; |
| 2115 | if (alpha_code == 0) |
| 2116 | alpha = a0; |
| 2117 | else if (alpha_code == 1) |
| 2118 | alpha = a1; |
| 2119 | else if (a0 > a1) |
| 2120 | alpha = ((8-alpha_code) * a0 + (alpha_code-1) * a1) / 7; |
| 2121 | else if (alpha_code == 6) |
| 2122 | alpha = 0; |
| 2123 | else if (alpha_code == 7) |
| 2124 | alpha = 255; |
| 2125 | else |
| 2126 | alpha = (((6-alpha_code) * a0 + (alpha_code-1) * a1) / 5); |
| 2127 | SetPixelAlpha(image,ScaleCharToQuantum((unsigned char) alpha),q); |
| 2128 | q+=GetPixelChannels(image); |
| 2129 | } |
| 2130 | } |
| 2131 | } |
| 2132 | |
| 2133 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 2134 | return MagickFalse; |
| 2135 | } |
| 2136 | } |
| 2137 | |
dirk | ee6b4cb | 2014-12-29 21:00:08 +0000 | [diff] [blame] | 2138 | return(SkipDXTMipmaps(image,dds_info,16,exception)); |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 2139 | } |
| 2140 | |
| 2141 | static MagickBooleanType ReadUncompressedRGB(Image *image, DDSInfo *dds_info, |
| 2142 | ExceptionInfo *exception) |
| 2143 | { |
| 2144 | register Quantum |
| 2145 | *q; |
| 2146 | |
| 2147 | ssize_t |
| 2148 | x, y; |
| 2149 | |
| 2150 | unsigned short |
| 2151 | color; |
| 2152 | |
dirk | 6398fb4 | 2014-09-08 18:19:39 +0000 | [diff] [blame] | 2153 | if (dds_info->pixelformat.rgb_bitcount == 8) |
| 2154 | (void) SetImageType(image,GrayscaleType,exception); |
| 2155 | else if (dds_info->pixelformat.rgb_bitcount == 16 && !IsBitMask( |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 2156 | dds_info->pixelformat,0xf800,0x07e0,0x001f,0x0000)) |
| 2157 | ThrowBinaryException(CorruptImageError,"ImageTypeNotSupported", |
| 2158 | image->filename); |
| 2159 | |
| 2160 | for (y = 0; y < (ssize_t) dds_info->height; y++) |
| 2161 | { |
| 2162 | q = QueueAuthenticPixels(image, 0, y, dds_info->width, 1,exception); |
| 2163 | |
| 2164 | if (q == (Quantum *) NULL) |
| 2165 | return MagickFalse; |
| 2166 | |
| 2167 | for (x = 0; x < (ssize_t) dds_info->width; x++) |
| 2168 | { |
dirk | 6398fb4 | 2014-09-08 18:19:39 +0000 | [diff] [blame] | 2169 | if (dds_info->pixelformat.rgb_bitcount == 8) |
| 2170 | SetPixelGray(image,ScaleCharToQuantum(ReadBlobByte(image)),q); |
| 2171 | else if (dds_info->pixelformat.rgb_bitcount == 16) |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 2172 | { |
| 2173 | color=ReadBlobShort(image); |
| 2174 | SetPixelRed(image,ScaleCharToQuantum((unsigned char) |
| 2175 | (((color >> 11)/31.0)*255)),q); |
| 2176 | SetPixelGreen(image,ScaleCharToQuantum((unsigned char) |
| 2177 | ((((unsigned short)(color << 5) >> 10)/63.0)*255)),q); |
| 2178 | SetPixelBlue(image,ScaleCharToQuantum((unsigned char) |
| 2179 | ((((unsigned short)(color << 11) >> 11)/31.0)*255)),q); |
| 2180 | } |
| 2181 | else |
| 2182 | { |
| 2183 | SetPixelBlue(image,ScaleCharToQuantum((unsigned char) |
| 2184 | ReadBlobByte(image)),q); |
| 2185 | SetPixelGreen(image,ScaleCharToQuantum((unsigned char) |
| 2186 | ReadBlobByte(image)),q); |
| 2187 | SetPixelRed(image,ScaleCharToQuantum((unsigned char) |
| 2188 | ReadBlobByte(image)),q); |
| 2189 | if (dds_info->pixelformat.rgb_bitcount == 32) |
| 2190 | (void) ReadBlobByte(image); |
| 2191 | } |
| 2192 | q+=GetPixelChannels(image); |
| 2193 | } |
| 2194 | |
| 2195 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 2196 | return MagickFalse; |
| 2197 | } |
| 2198 | |
dirk | ee6b4cb | 2014-12-29 21:00:08 +0000 | [diff] [blame] | 2199 | return(SkipRGBMipmaps(image,dds_info,3,exception)); |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 2200 | } |
| 2201 | |
| 2202 | static MagickBooleanType ReadUncompressedRGBA(Image *image, DDSInfo *dds_info, |
| 2203 | ExceptionInfo *exception) |
| 2204 | { |
| 2205 | register Quantum |
| 2206 | *q; |
| 2207 | |
| 2208 | ssize_t |
| 2209 | alphaBits, |
| 2210 | x, |
| 2211 | y; |
| 2212 | |
| 2213 | unsigned short |
| 2214 | color; |
| 2215 | |
| 2216 | alphaBits=0; |
| 2217 | if (dds_info->pixelformat.rgb_bitcount == 16) |
| 2218 | { |
| 2219 | if (IsBitMask(dds_info->pixelformat,0x7c00,0x03e0,0x001f,0x8000)) |
| 2220 | alphaBits=1; |
dirk | 6398fb4 | 2014-09-08 18:19:39 +0000 | [diff] [blame] | 2221 | else if (IsBitMask(dds_info->pixelformat,0x00ff,0x00ff,0x00ff,0xff00)) |
| 2222 | { |
| 2223 | alphaBits=2; |
cristy | def23e5 | 2015-01-22 11:52:01 +0000 | [diff] [blame] | 2224 | (void) SetImageType(image,GrayscaleAlphaType,exception); |
dirk | 6398fb4 | 2014-09-08 18:19:39 +0000 | [diff] [blame] | 2225 | } |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 2226 | else if (IsBitMask(dds_info->pixelformat,0x0f00,0x00f0,0x000f,0xf000)) |
| 2227 | alphaBits=4; |
| 2228 | else |
| 2229 | ThrowBinaryException(CorruptImageError,"ImageTypeNotSupported", |
| 2230 | image->filename); |
| 2231 | } |
| 2232 | |
| 2233 | for (y = 0; y < (ssize_t) dds_info->height; y++) |
| 2234 | { |
| 2235 | q = QueueAuthenticPixels(image, 0, y, dds_info->width, 1,exception); |
| 2236 | |
| 2237 | if (q == (Quantum *) NULL) |
| 2238 | return MagickFalse; |
| 2239 | |
| 2240 | for (x = 0; x < (ssize_t) dds_info->width; x++) |
| 2241 | { |
| 2242 | if (dds_info->pixelformat.rgb_bitcount == 16) |
| 2243 | { |
| 2244 | color=ReadBlobShort(image); |
| 2245 | if (alphaBits == 1) |
| 2246 | { |
| 2247 | SetPixelAlpha(image,(color & (1 << 15)) ? QuantumRange : 0,q); |
| 2248 | SetPixelRed(image,ScaleCharToQuantum((unsigned char) |
| 2249 | ((((unsigned short)(color << 1) >> 11)/31.0)*255)),q); |
| 2250 | SetPixelGreen(image,ScaleCharToQuantum((unsigned char) |
| 2251 | ((((unsigned short)(color << 6) >> 11)/31.0)*255)),q); |
| 2252 | SetPixelBlue(image,ScaleCharToQuantum((unsigned char) |
| 2253 | ((((unsigned short)(color << 11) >> 11)/31.0)*255)),q); |
| 2254 | } |
dirk | 6398fb4 | 2014-09-08 18:19:39 +0000 | [diff] [blame] | 2255 | else if (alphaBits == 2) |
| 2256 | { |
| 2257 | SetPixelAlpha(image,ScaleCharToQuantum((unsigned char) |
| 2258 | (color >> 8)),q); |
| 2259 | SetPixelGray(image,ScaleCharToQuantum((unsigned char)color),q); |
| 2260 | } |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 2261 | else |
| 2262 | { |
| 2263 | SetPixelAlpha(image,ScaleCharToQuantum((unsigned char) |
| 2264 | (((color >> 12)/15.0)*255)),q); |
| 2265 | SetPixelRed(image,ScaleCharToQuantum((unsigned char) |
| 2266 | ((((unsigned short)(color << 4) >> 12)/15.0)*255)),q); |
| 2267 | SetPixelGreen(image,ScaleCharToQuantum((unsigned char) |
| 2268 | ((((unsigned short)(color << 8) >> 12)/15.0)*255)),q); |
| 2269 | SetPixelBlue(image,ScaleCharToQuantum((unsigned char) |
| 2270 | ((((unsigned short)(color << 12) >> 12)/15.0)*255)),q); |
| 2271 | } |
| 2272 | } |
| 2273 | else |
| 2274 | { |
| 2275 | SetPixelBlue(image,ScaleCharToQuantum((unsigned char) |
| 2276 | ReadBlobByte(image)),q); |
| 2277 | SetPixelGreen(image,ScaleCharToQuantum((unsigned char) |
| 2278 | ReadBlobByte(image)),q); |
| 2279 | SetPixelRed(image,ScaleCharToQuantum((unsigned char) |
| 2280 | ReadBlobByte(image)),q); |
| 2281 | SetPixelAlpha(image,ScaleCharToQuantum((unsigned char) |
| 2282 | ReadBlobByte(image)),q); |
| 2283 | } |
| 2284 | q+=GetPixelChannels(image); |
| 2285 | } |
| 2286 | |
| 2287 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 2288 | return MagickFalse; |
| 2289 | } |
| 2290 | |
dirk | ee6b4cb | 2014-12-29 21:00:08 +0000 | [diff] [blame] | 2291 | return(SkipRGBMipmaps(image,dds_info,4,exception)); |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 2292 | } |
| 2293 | |
| 2294 | /* |
| 2295 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2296 | % % |
| 2297 | % % |
| 2298 | % % |
| 2299 | % R e g i s t e r D D S I m a g e % |
| 2300 | % % |
| 2301 | % % |
| 2302 | % % |
| 2303 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2304 | % |
| 2305 | % RegisterDDSImage() adds attributes for the DDS image format to |
| 2306 | % the list of supported formats. The attributes include the image format |
| 2307 | % tag, a method to read and/or write the format, whether the format |
| 2308 | % supports the saving of more than one frame to the same file or blob, |
| 2309 | % whether the format supports native in-memory I/O, and a brief |
| 2310 | % description of the format. |
| 2311 | % |
| 2312 | % The format of the RegisterDDSImage method is: |
| 2313 | % |
| 2314 | % RegisterDDSImage(void) |
| 2315 | % |
| 2316 | */ |
| 2317 | ModuleExport size_t RegisterDDSImage(void) |
| 2318 | { |
| 2319 | MagickInfo |
| 2320 | *entry; |
| 2321 | |
dirk | 06b627a | 2015-04-06 18:59:17 +0000 | [diff] [blame] | 2322 | entry = AcquireMagickInfo("DDS","DDS","Microsoft DirectDraw Surface"); |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 2323 | entry->decoder = (DecodeImageHandler *) ReadDDSImage; |
| 2324 | entry->encoder = (EncodeImageHandler *) WriteDDSImage; |
| 2325 | entry->magick = (IsImageFormatHandler *) IsDDS; |
Dirk Lemstra | 8974d77 | 2017-01-22 03:19:27 +0100 | [diff] [blame] | 2326 | entry->flags|=CoderDecoderSeekableStreamFlag; |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 2327 | (void) RegisterMagickInfo(entry); |
dirk | 06b627a | 2015-04-06 18:59:17 +0000 | [diff] [blame] | 2328 | entry = AcquireMagickInfo("DDS","DXT1","Microsoft DirectDraw Surface"); |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 2329 | entry->decoder = (DecodeImageHandler *) ReadDDSImage; |
| 2330 | entry->encoder = (EncodeImageHandler *) WriteDDSImage; |
| 2331 | entry->magick = (IsImageFormatHandler *) IsDDS; |
Dirk Lemstra | 8974d77 | 2017-01-22 03:19:27 +0100 | [diff] [blame] | 2332 | entry->flags|=CoderDecoderSeekableStreamFlag; |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 2333 | (void) RegisterMagickInfo(entry); |
dirk | 06b627a | 2015-04-06 18:59:17 +0000 | [diff] [blame] | 2334 | entry = AcquireMagickInfo("DDS","DXT5","Microsoft DirectDraw Surface"); |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 2335 | entry->decoder = (DecodeImageHandler *) ReadDDSImage; |
| 2336 | entry->encoder = (EncodeImageHandler *) WriteDDSImage; |
| 2337 | entry->magick = (IsImageFormatHandler *) IsDDS; |
Dirk Lemstra | 8974d77 | 2017-01-22 03:19:27 +0100 | [diff] [blame] | 2338 | entry->flags|=CoderDecoderSeekableStreamFlag; |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 2339 | (void) RegisterMagickInfo(entry); |
| 2340 | return(MagickImageCoderSignature); |
| 2341 | } |
| 2342 | |
| 2343 | static void RemapIndices(const ssize_t *map, const unsigned char *source, |
| 2344 | unsigned char *target) |
| 2345 | { |
| 2346 | register ssize_t |
| 2347 | i; |
| 2348 | |
| 2349 | for (i = 0; i < 16; i++) |
| 2350 | { |
| 2351 | if (map[i] == -1) |
| 2352 | target[i] = 3; |
| 2353 | else |
| 2354 | target[i] = source[map[i]]; |
| 2355 | } |
| 2356 | } |
| 2357 | |
| 2358 | /* |
| 2359 | Skip the mipmap images for compressed (DXTn) dds files |
| 2360 | */ |
dirk | ee6b4cb | 2014-12-29 21:00:08 +0000 | [diff] [blame] | 2361 | static MagickBooleanType SkipDXTMipmaps(Image *image,DDSInfo *dds_info, |
| 2362 | int texel_size,ExceptionInfo *exception) |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 2363 | { |
| 2364 | MagickOffsetType |
| 2365 | offset; |
| 2366 | |
| 2367 | register ssize_t |
| 2368 | i; |
| 2369 | |
| 2370 | size_t |
| 2371 | h, |
| 2372 | w; |
| 2373 | |
| 2374 | /* |
| 2375 | Only skip mipmaps for textures and cube maps |
| 2376 | */ |
dirk | 8e2b7d0 | 2014-12-29 22:23:28 +0000 | [diff] [blame] | 2377 | if (EOFBlob(image) != MagickFalse) |
| 2378 | { |
dirk | 791aa82 | 2016-05-12 21:55:29 +0200 | [diff] [blame] | 2379 | ThrowFileException(exception,CorruptImageWarning,"UnexpectedEndOfFile", |
dirk | 8e2b7d0 | 2014-12-29 22:23:28 +0000 | [diff] [blame] | 2380 | image->filename); |
| 2381 | return(MagickFalse); |
| 2382 | } |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 2383 | if (dds_info->ddscaps1 & DDSCAPS_MIPMAP |
| 2384 | && (dds_info->ddscaps1 & DDSCAPS_TEXTURE |
| 2385 | || dds_info->ddscaps2 & DDSCAPS2_CUBEMAP)) |
| 2386 | { |
| 2387 | w = DIV2(dds_info->width); |
| 2388 | h = DIV2(dds_info->height); |
| 2389 | |
| 2390 | /* |
| 2391 | Mipmapcount includes the main image, so start from one |
| 2392 | */ |
| 2393 | for (i = 1; (i < (ssize_t) dds_info->mipmapcount) && w && h; i++) |
| 2394 | { |
| 2395 | offset = (MagickOffsetType) ((w + 3) / 4) * ((h + 3) / 4) * texel_size; |
cristy | cc2a4d2 | 2015-01-11 19:17:25 +0000 | [diff] [blame] | 2396 | if (SeekBlob(image, offset, SEEK_CUR) < 0) |
| 2397 | break; |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 2398 | w = DIV2(w); |
| 2399 | h = DIV2(h); |
| 2400 | } |
| 2401 | } |
dirk | ee6b4cb | 2014-12-29 21:00:08 +0000 | [diff] [blame] | 2402 | return(MagickTrue); |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 2403 | } |
| 2404 | |
| 2405 | /* |
| 2406 | Skip the mipmap images for uncompressed (RGB or RGBA) dds files |
| 2407 | */ |
dirk | ee6b4cb | 2014-12-29 21:00:08 +0000 | [diff] [blame] | 2408 | static MagickBooleanType SkipRGBMipmaps(Image *image,DDSInfo *dds_info, |
| 2409 | int pixel_size,ExceptionInfo *exception) |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 2410 | { |
| 2411 | MagickOffsetType |
| 2412 | offset; |
| 2413 | |
| 2414 | register ssize_t |
| 2415 | i; |
| 2416 | |
| 2417 | size_t |
| 2418 | h, |
| 2419 | w; |
| 2420 | |
| 2421 | /* |
| 2422 | Only skip mipmaps for textures and cube maps |
| 2423 | */ |
dirk | 8e2b7d0 | 2014-12-29 22:23:28 +0000 | [diff] [blame] | 2424 | if (EOFBlob(image) != MagickFalse) |
| 2425 | { |
| 2426 | ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile", |
| 2427 | image->filename); |
| 2428 | return(MagickFalse); |
| 2429 | } |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 2430 | if (dds_info->ddscaps1 & DDSCAPS_MIPMAP |
| 2431 | && (dds_info->ddscaps1 & DDSCAPS_TEXTURE |
| 2432 | || dds_info->ddscaps2 & DDSCAPS2_CUBEMAP)) |
| 2433 | { |
| 2434 | w = DIV2(dds_info->width); |
| 2435 | h = DIV2(dds_info->height); |
| 2436 | |
| 2437 | /* |
| 2438 | Mipmapcount includes the main image, so start from one |
| 2439 | */ |
| 2440 | for (i=1; (i < (ssize_t) dds_info->mipmapcount) && w && h; i++) |
| 2441 | { |
| 2442 | offset = (MagickOffsetType) w * h * pixel_size; |
cristy | cc2a4d2 | 2015-01-11 19:17:25 +0000 | [diff] [blame] | 2443 | if (SeekBlob(image, offset, SEEK_CUR) < 0) |
| 2444 | break; |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 2445 | w = DIV2(w); |
| 2446 | h = DIV2(h); |
| 2447 | } |
| 2448 | } |
dirk | ee6b4cb | 2014-12-29 21:00:08 +0000 | [diff] [blame] | 2449 | return(MagickTrue); |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 2450 | } |
| 2451 | |
| 2452 | /* |
| 2453 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2454 | % % |
| 2455 | % % |
| 2456 | % % |
| 2457 | % U n r e g i s t e r D D S I m a g e % |
| 2458 | % % |
| 2459 | % % |
| 2460 | % % |
| 2461 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2462 | % |
| 2463 | % UnregisterDDSImage() removes format registrations made by the |
| 2464 | % DDS module from the list of supported formats. |
| 2465 | % |
| 2466 | % The format of the UnregisterDDSImage method is: |
| 2467 | % |
| 2468 | % UnregisterDDSImage(void) |
| 2469 | % |
| 2470 | */ |
| 2471 | ModuleExport void UnregisterDDSImage(void) |
| 2472 | { |
| 2473 | (void) UnregisterMagickInfo("DDS"); |
| 2474 | (void) UnregisterMagickInfo("DXT1"); |
| 2475 | (void) UnregisterMagickInfo("DXT5"); |
| 2476 | } |
| 2477 | |
| 2478 | static void WriteAlphas(Image *image, const ssize_t *alphas, size_t min5, |
| 2479 | size_t max5, size_t min7, size_t max7) |
| 2480 | { |
| 2481 | register ssize_t |
| 2482 | i; |
| 2483 | |
| 2484 | size_t |
| 2485 | err5, |
| 2486 | err7, |
| 2487 | j; |
| 2488 | |
| 2489 | unsigned char |
| 2490 | indices5[16], |
| 2491 | indices7[16]; |
| 2492 | |
| 2493 | FixRange(min5,max5,5); |
| 2494 | err5 = CompressAlpha(min5,max5,5,alphas,indices5); |
| 2495 | |
| 2496 | FixRange(min7,max7,7); |
| 2497 | err7 = CompressAlpha(min7,max7,7,alphas,indices7); |
| 2498 | |
| 2499 | if (err7 < err5) |
| 2500 | { |
| 2501 | for (i=0; i < 16; i++) |
| 2502 | { |
| 2503 | unsigned char |
| 2504 | index; |
| 2505 | |
| 2506 | index = indices7[i]; |
| 2507 | if( index == 0 ) |
| 2508 | indices5[i] = 1; |
| 2509 | else if (index == 1) |
| 2510 | indices5[i] = 0; |
| 2511 | else |
| 2512 | indices5[i] = 9 - index; |
| 2513 | } |
| 2514 | |
| 2515 | min5 = max7; |
| 2516 | max5 = min7; |
| 2517 | } |
| 2518 | |
| 2519 | (void) WriteBlobByte(image,(unsigned char) min5); |
| 2520 | (void) WriteBlobByte(image,(unsigned char) max5); |
| 2521 | |
| 2522 | for(i=0; i < 2; i++) |
| 2523 | { |
| 2524 | size_t |
| 2525 | value = 0; |
| 2526 | |
| 2527 | for (j=0; j < 8; j++) |
| 2528 | { |
| 2529 | size_t index = (size_t) indices5[j + i*8]; |
| 2530 | value |= ( index << 3*j ); |
| 2531 | } |
| 2532 | |
| 2533 | for (j=0; j < 3; j++) |
| 2534 | { |
| 2535 | size_t byte = (value >> 8*j) & 0xff; |
| 2536 | (void) WriteBlobByte(image,(unsigned char) byte); |
| 2537 | } |
| 2538 | } |
| 2539 | } |
| 2540 | |
| 2541 | static void WriteCompressed(Image *image, const size_t count, |
| 2542 | DDSVector4 *points, const ssize_t *map, const MagickBooleanType clusterFit) |
| 2543 | { |
| 2544 | float |
| 2545 | covariance[16]; |
| 2546 | |
| 2547 | DDSVector3 |
| 2548 | end, |
| 2549 | principle, |
| 2550 | start; |
| 2551 | |
| 2552 | DDSVector4 |
| 2553 | metric; |
| 2554 | |
| 2555 | unsigned char |
| 2556 | indices[16]; |
| 2557 | |
| 2558 | VectorInit(metric,1.0f); |
| 2559 | VectorInit3(start,0.0f); |
| 2560 | VectorInit3(end,0.0f); |
| 2561 | |
| 2562 | ComputeWeightedCovariance(count,points,covariance); |
| 2563 | ComputePrincipleComponent(covariance,&principle); |
| 2564 | |
cristy | 771c884 | 2015-01-09 12:13:22 +0000 | [diff] [blame] | 2565 | if ((clusterFit == MagickFalse) || (count == 0)) |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 2566 | CompressRangeFit(count,points,map,principle,metric,&start,&end,indices); |
| 2567 | else |
| 2568 | CompressClusterFit(count,points,map,principle,metric,&start,&end,indices); |
| 2569 | |
| 2570 | WriteIndices(image,start,end,indices); |
| 2571 | } |
| 2572 | |
| 2573 | /* |
| 2574 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2575 | % % |
| 2576 | % % |
| 2577 | % % |
| 2578 | % W r i t e D D S I m a g e % |
| 2579 | % % |
| 2580 | % % |
| 2581 | % % |
| 2582 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2583 | % |
| 2584 | % WriteDDSImage() writes a DirectDraw Surface image file in the DXT5 format. |
| 2585 | % |
| 2586 | % The format of the WriteBMPImage method is: |
| 2587 | % |
| 2588 | % MagickBooleanType WriteDDSImage(const ImageInfo *image_info,Image *image) |
| 2589 | % |
| 2590 | % A description of each parameter follows. |
| 2591 | % |
| 2592 | % o image_info: the image info. |
| 2593 | % |
| 2594 | % o image: The image. |
| 2595 | % |
| 2596 | */ |
| 2597 | static MagickBooleanType WriteDDSImage(const ImageInfo *image_info, |
| 2598 | Image *image, ExceptionInfo *exception) |
| 2599 | { |
| 2600 | const char |
| 2601 | *option; |
| 2602 | |
| 2603 | size_t |
| 2604 | compression, |
| 2605 | columns, |
| 2606 | maxMipmaps, |
| 2607 | mipmaps, |
| 2608 | pixelFormat, |
| 2609 | rows; |
| 2610 | |
| 2611 | MagickBooleanType |
| 2612 | clusterFit, |
| 2613 | status, |
| 2614 | weightByAlpha; |
| 2615 | |
| 2616 | assert(image_info != (const ImageInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 2617 | assert(image_info->signature == MagickCoreSignature); |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 2618 | assert(image != (Image *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 2619 | assert(image->signature == MagickCoreSignature); |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 2620 | if (image->debug != MagickFalse) |
| 2621 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 2622 | status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception); |
| 2623 | if (status == MagickFalse) |
| 2624 | return(status); |
| 2625 | (void) TransformImageColorspace(image,sRGBColorspace,exception); |
| 2626 | pixelFormat=DDPF_FOURCC; |
| 2627 | compression=FOURCC_DXT5; |
| 2628 | |
cristy | 17f11b0 | 2014-12-20 19:37:04 +0000 | [diff] [blame] | 2629 | if (image->alpha_trait == UndefinedPixelTrait) |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 2630 | compression=FOURCC_DXT1; |
| 2631 | |
| 2632 | if (LocaleCompare(image_info->magick,"dxt1") == 0) |
| 2633 | compression=FOURCC_DXT1; |
| 2634 | |
| 2635 | option=GetImageOption(image_info,"dds:compression"); |
| 2636 | if (option != (char *) NULL) |
| 2637 | { |
| 2638 | if (LocaleCompare(option,"dxt1") == 0) |
| 2639 | compression=FOURCC_DXT1; |
| 2640 | if (LocaleCompare(option,"none") == 0) |
| 2641 | pixelFormat=DDPF_RGB; |
| 2642 | } |
| 2643 | |
| 2644 | clusterFit=MagickFalse; |
| 2645 | weightByAlpha=MagickFalse; |
| 2646 | |
| 2647 | if (pixelFormat == DDPF_FOURCC) |
| 2648 | { |
| 2649 | option=GetImageOption(image_info,"dds:cluster-fit"); |
dirk | cf97f61 | 2015-02-14 16:36:16 +0000 | [diff] [blame] | 2650 | if (IsStringTrue(option) != MagickFalse) |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 2651 | { |
| 2652 | clusterFit=MagickTrue; |
| 2653 | if (compression != FOURCC_DXT1) |
| 2654 | { |
| 2655 | option=GetImageOption(image_info,"dds:weight-by-alpha"); |
dirk | cf97f61 | 2015-02-14 16:36:16 +0000 | [diff] [blame] | 2656 | if (IsStringTrue(option) != MagickFalse) |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 2657 | weightByAlpha=MagickTrue; |
| 2658 | } |
| 2659 | } |
| 2660 | } |
| 2661 | |
| 2662 | maxMipmaps=SIZE_MAX; |
| 2663 | mipmaps=0; |
| 2664 | if ((image->columns & (image->columns - 1)) == 0 && |
| 2665 | (image->rows & (image->rows - 1)) == 0) |
| 2666 | { |
| 2667 | option=GetImageOption(image_info,"dds:mipmaps"); |
| 2668 | if (option != (char *) NULL) |
| 2669 | maxMipmaps=StringToUnsignedLong(option); |
| 2670 | |
| 2671 | if (maxMipmaps != 0) |
| 2672 | { |
| 2673 | columns=image->columns; |
| 2674 | rows=image->rows; |
Cristy | ccb2417 | 2016-09-14 07:14:47 -0400 | [diff] [blame] | 2675 | while ((columns != 1 || rows != 1) && mipmaps != maxMipmaps) |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 2676 | { |
| 2677 | columns=DIV2(columns); |
| 2678 | rows=DIV2(rows); |
| 2679 | mipmaps++; |
| 2680 | } |
| 2681 | } |
| 2682 | } |
| 2683 | |
| 2684 | WriteDDSInfo(image,pixelFormat,compression,mipmaps); |
| 2685 | |
| 2686 | WriteImageData(image,pixelFormat,compression,clusterFit,weightByAlpha, |
| 2687 | exception); |
| 2688 | |
| 2689 | if (mipmaps > 0 && WriteMipmaps(image,pixelFormat,compression,mipmaps, |
| 2690 | clusterFit,weightByAlpha,exception) == MagickFalse) |
| 2691 | return(MagickFalse); |
| 2692 | |
| 2693 | (void) CloseBlob(image); |
| 2694 | return(MagickTrue); |
| 2695 | } |
| 2696 | |
| 2697 | static void WriteDDSInfo(Image *image, const size_t pixelFormat, |
| 2698 | const size_t compression, const size_t mipmaps) |
| 2699 | { |
| 2700 | char |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 2701 | software[MagickPathExtent]; |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 2702 | |
| 2703 | register ssize_t |
| 2704 | i; |
| 2705 | |
| 2706 | unsigned int |
| 2707 | format, |
| 2708 | caps, |
| 2709 | flags; |
| 2710 | |
| 2711 | flags=(unsigned int) (DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | |
Hrnchamd | 3e69523 | 2016-02-23 11:51:38 +0000 | [diff] [blame] | 2712 | DDSD_PIXELFORMAT); |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 2713 | caps=(unsigned int) DDSCAPS_TEXTURE; |
| 2714 | format=(unsigned int) pixelFormat; |
| 2715 | |
Hrnchamd | 3e69523 | 2016-02-23 11:51:38 +0000 | [diff] [blame] | 2716 | if (format == DDPF_FOURCC) |
| 2717 | flags=flags | DDSD_LINEARSIZE; |
| 2718 | else |
| 2719 | flags=flags | DDSD_PITCH; |
| 2720 | |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 2721 | if (mipmaps > 0) |
| 2722 | { |
| 2723 | flags=flags | (unsigned int) DDSD_MIPMAPCOUNT; |
| 2724 | caps=caps | (unsigned int) (DDSCAPS_MIPMAP | DDSCAPS_COMPLEX); |
| 2725 | } |
| 2726 | |
cristy | 17f11b0 | 2014-12-20 19:37:04 +0000 | [diff] [blame] | 2727 | if (format != DDPF_FOURCC && image->alpha_trait != UndefinedPixelTrait) |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 2728 | format=format | DDPF_ALPHAPIXELS; |
| 2729 | |
| 2730 | (void) WriteBlob(image,4,(unsigned char *) "DDS "); |
| 2731 | (void) WriteBlobLSBLong(image,124); |
| 2732 | (void) WriteBlobLSBLong(image,flags); |
dirk | a436849 | 2014-12-17 07:32:49 +0000 | [diff] [blame] | 2733 | (void) WriteBlobLSBLong(image,(unsigned int) image->rows); |
| 2734 | (void) WriteBlobLSBLong(image,(unsigned int) image->columns); |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 2735 | |
Dan Skorupski | 7cd5b95 | 2016-01-17 20:37:31 -0600 | [diff] [blame] | 2736 | if (pixelFormat == DDPF_FOURCC) |
| 2737 | { |
dirk | 4fab205 | 2016-04-03 19:55:11 +0200 | [diff] [blame] | 2738 | /* Compressed DDS requires linear compressed size of first image */ |
Dan Skorupski | 7cd5b95 | 2016-01-17 20:37:31 -0600 | [diff] [blame] | 2739 | if (compression == FOURCC_DXT1) |
dirk | 4fab205 | 2016-04-03 19:55:11 +0200 | [diff] [blame] | 2740 | (void) WriteBlobLSBLong(image,(unsigned int) (MagickMax(1, |
| 2741 | (image->columns+3)/4)*MagickMax(1,(image->rows+3)/4)*8)); |
Hrnchamd | 3e69523 | 2016-02-23 11:51:38 +0000 | [diff] [blame] | 2742 | else /* DXT5 */ |
dirk | 4fab205 | 2016-04-03 19:55:11 +0200 | [diff] [blame] | 2743 | (void) WriteBlobLSBLong(image,(unsigned int) (MagickMax(1, |
| 2744 | (image->columns+3)/4)*MagickMax(1,(image->rows+3)/4)*16)); |
Dan Skorupski | 7cd5b95 | 2016-01-17 20:37:31 -0600 | [diff] [blame] | 2745 | } |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 2746 | else |
Dan Skorupski | 7cd5b95 | 2016-01-17 20:37:31 -0600 | [diff] [blame] | 2747 | { |
dirk | 4fab205 | 2016-04-03 19:55:11 +0200 | [diff] [blame] | 2748 | /* Uncompressed DDS requires byte pitch of first image */ |
Dan Skorupski | 7cd5b95 | 2016-01-17 20:37:31 -0600 | [diff] [blame] | 2749 | if (image->alpha_trait != UndefinedPixelTrait) |
Hrnchamd | b22ff3c | 2016-02-23 22:19:01 +0000 | [diff] [blame] | 2750 | (void) WriteBlobLSBLong(image,(unsigned int) (image->columns * 4)); |
Dan Skorupski | 7cd5b95 | 2016-01-17 20:37:31 -0600 | [diff] [blame] | 2751 | else |
Hrnchamd | b22ff3c | 2016-02-23 22:19:01 +0000 | [diff] [blame] | 2752 | (void) WriteBlobLSBLong(image,(unsigned int) (image->columns * 3)); |
Dan Skorupski | 7cd5b95 | 2016-01-17 20:37:31 -0600 | [diff] [blame] | 2753 | } |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 2754 | |
| 2755 | (void) WriteBlobLSBLong(image,0x00); |
| 2756 | (void) WriteBlobLSBLong(image,(unsigned int) mipmaps+1); |
| 2757 | (void) ResetMagickMemory(software,0,sizeof(software)); |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 2758 | (void) CopyMagickString(software,"IMAGEMAGICK",MagickPathExtent); |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 2759 | (void) WriteBlob(image,44,(unsigned char *) software); |
| 2760 | |
| 2761 | (void) WriteBlobLSBLong(image,32); |
| 2762 | (void) WriteBlobLSBLong(image,format); |
| 2763 | |
| 2764 | if (pixelFormat == DDPF_FOURCC) |
| 2765 | { |
| 2766 | (void) WriteBlobLSBLong(image,(unsigned int) compression); |
| 2767 | for(i=0;i < 5;i++) // bitcount / masks |
| 2768 | (void) WriteBlobLSBLong(image,0x00); |
| 2769 | } |
| 2770 | else |
| 2771 | { |
| 2772 | (void) WriteBlobLSBLong(image,0x00); |
cristy | 17f11b0 | 2014-12-20 19:37:04 +0000 | [diff] [blame] | 2773 | if (image->alpha_trait != UndefinedPixelTrait) |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 2774 | { |
| 2775 | (void) WriteBlobLSBLong(image,32); |
| 2776 | (void) WriteBlobLSBLong(image,0xff0000); |
| 2777 | (void) WriteBlobLSBLong(image,0xff00); |
| 2778 | (void) WriteBlobLSBLong(image,0xff); |
| 2779 | (void) WriteBlobLSBLong(image,0xff000000); |
| 2780 | } |
| 2781 | else |
| 2782 | { |
| 2783 | (void) WriteBlobLSBLong(image,24); |
Dan Skorupski | 7cd5b95 | 2016-01-17 20:37:31 -0600 | [diff] [blame] | 2784 | (void) WriteBlobLSBLong(image,0xff0000); |
| 2785 | (void) WriteBlobLSBLong(image,0xff00); |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 2786 | (void) WriteBlobLSBLong(image,0xff); |
| 2787 | (void) WriteBlobLSBLong(image,0x00); |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 2788 | } |
| 2789 | } |
| 2790 | |
| 2791 | (void) WriteBlobLSBLong(image,caps); |
| 2792 | for(i=0;i < 4;i++) // ddscaps2 + reserved region |
| 2793 | (void) WriteBlobLSBLong(image,0x00); |
| 2794 | } |
| 2795 | |
| 2796 | static void WriteFourCC(Image *image, const size_t compression, |
| 2797 | const MagickBooleanType clusterFit, const MagickBooleanType weightByAlpha, |
| 2798 | ExceptionInfo *exception) |
| 2799 | { |
| 2800 | register ssize_t |
| 2801 | x; |
| 2802 | |
| 2803 | ssize_t |
| 2804 | i, |
| 2805 | y, |
| 2806 | bx, |
| 2807 | by; |
| 2808 | |
| 2809 | register const Quantum |
| 2810 | *p; |
| 2811 | |
| 2812 | for (y=0; y < (ssize_t) image->rows; y+=4) |
| 2813 | { |
| 2814 | for (x=0; x < (ssize_t) image->columns; x+=4) |
| 2815 | { |
| 2816 | MagickBooleanType |
| 2817 | match; |
| 2818 | |
| 2819 | DDSVector4 |
| 2820 | point, |
| 2821 | points[16]; |
| 2822 | |
| 2823 | size_t |
| 2824 | count = 0, |
| 2825 | max5 = 0, |
| 2826 | max7 = 0, |
| 2827 | min5 = 255, |
| 2828 | min7 = 255, |
| 2829 | columns = 4, |
| 2830 | rows = 4; |
| 2831 | |
| 2832 | ssize_t |
| 2833 | alphas[16], |
| 2834 | map[16]; |
| 2835 | |
| 2836 | unsigned char |
| 2837 | alpha; |
| 2838 | |
| 2839 | if (x + columns >= image->columns) |
| 2840 | columns = image->columns - x; |
| 2841 | |
| 2842 | if (y + rows >= image->rows) |
| 2843 | rows = image->rows - y; |
| 2844 | |
| 2845 | p=GetVirtualPixels(image,x,y,columns,rows,exception); |
| 2846 | if (p == (const Quantum *) NULL) |
| 2847 | break; |
| 2848 | |
| 2849 | for (i=0; i<16; i++) |
| 2850 | { |
| 2851 | map[i] = -1; |
| 2852 | alphas[i] = -1; |
| 2853 | } |
| 2854 | |
Hrnchamd | 3e69523 | 2016-02-23 11:51:38 +0000 | [diff] [blame] | 2855 | for (by=0; by < (ssize_t) rows; by++) |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 2856 | { |
Hrnchamd | 3e69523 | 2016-02-23 11:51:38 +0000 | [diff] [blame] | 2857 | for (bx=0; bx < (ssize_t) columns; bx++) |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 2858 | { |
| 2859 | if (compression == FOURCC_DXT5) |
| 2860 | alpha = ScaleQuantumToChar(GetPixelAlpha(image,p)); |
| 2861 | else |
| 2862 | alpha = 255; |
| 2863 | |
Hrnchamd | 3e69523 | 2016-02-23 11:51:38 +0000 | [diff] [blame] | 2864 | if (compression == FOURCC_DXT5) |
| 2865 | { |
| 2866 | if (alpha < min7) |
| 2867 | min7 = alpha; |
| 2868 | if (alpha > max7) |
| 2869 | max7 = alpha; |
| 2870 | if (alpha != 0 && alpha < min5) |
| 2871 | min5 = alpha; |
| 2872 | if (alpha != 255 && alpha > max5) |
| 2873 | max5 = alpha; |
| 2874 | } |
| 2875 | |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 2876 | alphas[4*by + bx] = (size_t)alpha; |
| 2877 | |
| 2878 | point.x = (float)ScaleQuantumToChar(GetPixelRed(image,p)) / 255.0f; |
| 2879 | point.y = (float)ScaleQuantumToChar(GetPixelGreen(image,p)) / 255.0f; |
| 2880 | point.z = (float)ScaleQuantumToChar(GetPixelBlue(image,p)) / 255.0f; |
| 2881 | point.w = weightByAlpha ? (float)(alpha + 1) / 256.0f : 1.0f; |
| 2882 | p+=GetPixelChannels(image); |
| 2883 | |
| 2884 | match = MagickFalse; |
Hrnchamd | 3e69523 | 2016-02-23 11:51:38 +0000 | [diff] [blame] | 2885 | for (i=0; i < (ssize_t) count; i++) |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 2886 | { |
| 2887 | if ((points[i].x == point.x) && |
| 2888 | (points[i].y == point.y) && |
| 2889 | (points[i].z == point.z) && |
| 2890 | (alpha >= 128 || compression == FOURCC_DXT5)) |
| 2891 | { |
| 2892 | points[i].w += point.w; |
| 2893 | map[4*by + bx] = i; |
| 2894 | match = MagickTrue; |
| 2895 | break; |
| 2896 | } |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 2897 | } |
Hrnchamd | 3e69523 | 2016-02-23 11:51:38 +0000 | [diff] [blame] | 2898 | |
| 2899 | if (match != MagickFalse) |
| 2900 | continue; |
| 2901 | |
| 2902 | points[count].x = point.x; |
| 2903 | points[count].y = point.y; |
| 2904 | points[count].z = point.z; |
| 2905 | points[count].w = point.w; |
| 2906 | map[4*by + bx] = count; |
| 2907 | count++; |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 2908 | } |
Hrnchamd | 3e69523 | 2016-02-23 11:51:38 +0000 | [diff] [blame] | 2909 | } |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 2910 | |
dirk | 2c3d3bc | 2016-08-29 20:58:36 +0200 | [diff] [blame] | 2911 | for (i=0; i < (ssize_t) count; i++) |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 2912 | points[i].w = sqrt(points[i].w); |
| 2913 | |
| 2914 | if (compression == FOURCC_DXT5) |
| 2915 | WriteAlphas(image,alphas,min5,max5,min7,max7); |
| 2916 | |
| 2917 | if (count == 1) |
| 2918 | WriteSingleColorFit(image,points,map); |
| 2919 | else |
| 2920 | WriteCompressed(image,count,points,map,clusterFit); |
| 2921 | } |
| 2922 | } |
| 2923 | } |
| 2924 | |
| 2925 | static void WriteImageData(Image *image, const size_t pixelFormat, |
| 2926 | const size_t compression,const MagickBooleanType clusterFit, |
| 2927 | const MagickBooleanType weightByAlpha, ExceptionInfo *exception) |
| 2928 | { |
| 2929 | if (pixelFormat == DDPF_FOURCC) |
| 2930 | WriteFourCC(image,compression,clusterFit,weightByAlpha,exception); |
| 2931 | else |
| 2932 | WriteUncompressed(image,exception); |
| 2933 | } |
| 2934 | |
| 2935 | static inline size_t ClampToLimit(const float value, const size_t limit) |
| 2936 | { |
| 2937 | size_t |
| 2938 | result = (int) (value + 0.5f); |
| 2939 | |
| 2940 | if (result < 0.0f) |
| 2941 | return(0); |
| 2942 | if (result > limit) |
| 2943 | return(limit); |
| 2944 | return result; |
| 2945 | } |
| 2946 | |
| 2947 | static inline size_t ColorTo565(const DDSVector3 point) |
| 2948 | { |
| 2949 | size_t r = ClampToLimit(31.0f*point.x,31); |
| 2950 | size_t g = ClampToLimit(63.0f*point.y,63); |
| 2951 | size_t b = ClampToLimit(31.0f*point.z,31); |
| 2952 | |
| 2953 | return (r << 11) | (g << 5) | b; |
| 2954 | } |
| 2955 | |
| 2956 | static void WriteIndices(Image *image, const DDSVector3 start, |
| 2957 | const DDSVector3 end, unsigned char *indices) |
| 2958 | { |
| 2959 | register ssize_t |
| 2960 | i; |
| 2961 | |
| 2962 | size_t |
| 2963 | a, |
| 2964 | b; |
| 2965 | |
| 2966 | unsigned char |
| 2967 | remapped[16]; |
| 2968 | |
| 2969 | const unsigned char |
| 2970 | *ind; |
| 2971 | |
| 2972 | a = ColorTo565(start); |
| 2973 | b = ColorTo565(end); |
| 2974 | |
| 2975 | for (i=0; i<16; i++) |
| 2976 | { |
| 2977 | if( a < b ) |
| 2978 | remapped[i] = (indices[i] ^ 0x1) & 0x3; |
| 2979 | else if( a == b ) |
| 2980 | remapped[i] = 0; |
| 2981 | else |
| 2982 | remapped[i] = indices[i]; |
| 2983 | } |
| 2984 | |
| 2985 | if( a < b ) |
| 2986 | Swap(a,b); |
| 2987 | |
| 2988 | (void) WriteBlobByte(image,(unsigned char) (a & 0xff)); |
| 2989 | (void) WriteBlobByte(image,(unsigned char) (a >> 8)); |
| 2990 | (void) WriteBlobByte(image,(unsigned char) (b & 0xff)); |
| 2991 | (void) WriteBlobByte(image,(unsigned char) (b >> 8)); |
| 2992 | |
| 2993 | for (i=0; i<4; i++) |
| 2994 | { |
| 2995 | ind = remapped + 4*i; |
| 2996 | (void) WriteBlobByte(image,ind[0] | (ind[1] << 2) | (ind[2] << 4) | |
| 2997 | (ind[3] << 6)); |
| 2998 | } |
| 2999 | } |
| 3000 | |
| 3001 | static MagickBooleanType WriteMipmaps(Image *image, const size_t pixelFormat, |
| 3002 | const size_t compression, const size_t mipmaps, |
| 3003 | const MagickBooleanType clusterFit, const MagickBooleanType weightByAlpha, |
| 3004 | ExceptionInfo *exception) |
| 3005 | { |
| 3006 | Image* |
| 3007 | resize_image; |
| 3008 | |
| 3009 | register ssize_t |
| 3010 | i; |
| 3011 | |
| 3012 | size_t |
| 3013 | columns, |
| 3014 | rows; |
| 3015 | |
| 3016 | columns = image->columns; |
| 3017 | rows = image->rows; |
| 3018 | |
| 3019 | for (i=0; i< (ssize_t) mipmaps; i++) |
| 3020 | { |
Cristy | ccb2417 | 2016-09-14 07:14:47 -0400 | [diff] [blame] | 3021 | resize_image = ResizeImage(image,DIV2(columns),DIV2(rows),TriangleFilter, |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 3022 | exception); |
| 3023 | |
| 3024 | if (resize_image == (Image *) NULL) |
| 3025 | return(MagickFalse); |
| 3026 | |
| 3027 | DestroyBlob(resize_image); |
| 3028 | resize_image->blob=ReferenceBlob(image->blob); |
| 3029 | |
| 3030 | WriteImageData(resize_image,pixelFormat,compression,weightByAlpha, |
| 3031 | clusterFit,exception); |
| 3032 | |
| 3033 | resize_image=DestroyImage(resize_image); |
| 3034 | |
| 3035 | columns = DIV2(columns); |
| 3036 | rows = DIV2(rows); |
| 3037 | } |
| 3038 | |
| 3039 | return(MagickTrue); |
| 3040 | } |
| 3041 | |
| 3042 | static void WriteSingleColorFit(Image *image, const DDSVector4 *points, |
| 3043 | const ssize_t *map) |
| 3044 | { |
| 3045 | DDSVector3 |
| 3046 | start, |
| 3047 | end; |
| 3048 | |
| 3049 | register ssize_t |
| 3050 | i; |
| 3051 | |
| 3052 | unsigned char |
| 3053 | color[3], |
| 3054 | index, |
| 3055 | indexes[16], |
| 3056 | indices[16]; |
| 3057 | |
| 3058 | color[0] = (unsigned char) ClampToLimit(255.0f*points->x,255); |
| 3059 | color[1] = (unsigned char) ClampToLimit(255.0f*points->y,255); |
| 3060 | color[2] = (unsigned char) ClampToLimit(255.0f*points->z,255); |
| 3061 | |
| 3062 | index=0; |
| 3063 | ComputeEndPoints(DDS_LOOKUP,color,&start,&end,&index); |
| 3064 | |
| 3065 | for (i=0; i< 16; i++) |
| 3066 | indexes[i]=index; |
| 3067 | RemapIndices(map,indexes,indices); |
| 3068 | WriteIndices(image,start,end,indices); |
| 3069 | } |
| 3070 | |
| 3071 | static void WriteUncompressed(Image *image, ExceptionInfo *exception) |
| 3072 | { |
| 3073 | register const Quantum |
| 3074 | *p; |
| 3075 | |
| 3076 | register ssize_t |
| 3077 | x; |
| 3078 | |
| 3079 | ssize_t |
| 3080 | y; |
| 3081 | |
| 3082 | for (y=0; y < (ssize_t) image->rows; y++) |
| 3083 | { |
| 3084 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
| 3085 | if (p == (const Quantum *) NULL) |
| 3086 | break; |
| 3087 | |
| 3088 | for (x=0; x < (ssize_t) image->columns; x++) |
| 3089 | { |
| 3090 | (void) WriteBlobByte(image,ScaleQuantumToChar(GetPixelBlue(image,p))); |
| 3091 | (void) WriteBlobByte(image,ScaleQuantumToChar(GetPixelGreen(image,p))); |
| 3092 | (void) WriteBlobByte(image,ScaleQuantumToChar(GetPixelRed(image,p))); |
cristy | 17f11b0 | 2014-12-20 19:37:04 +0000 | [diff] [blame] | 3093 | if (image->alpha_trait != UndefinedPixelTrait) |
dirk | 8692172 | 2014-07-07 18:47:28 +0000 | [diff] [blame] | 3094 | (void) WriteBlobByte(image,ScaleQuantumToChar(GetPixelAlpha(image,p))); |
| 3095 | p+=GetPixelChannels(image); |
| 3096 | } |
| 3097 | } |
| 3098 | } |