1 module stb.image; 2 3 import core.bitop, 4 std.file, 5 std.path, 6 std.conv, 7 std.math, 8 std.range, 9 std.string, 10 std.mmfile, 11 std.typecons, 12 std.bitmanip, 13 std.exception, 14 std.algorithm, 15 16 core.stdc.string, 17 etc.c.zlib, 18 19 stb.image.binding; 20 21 struct Color { 22 this(uint r, uint g, uint b, uint a = 0) { 23 this.r = cast(ubyte) r; 24 this.g = cast(ubyte) g; 25 this.b = cast(ubyte) b; 26 this.a = cast(ubyte) a; 27 } 28 29 static fromInt(uint n) { 30 n = n.bswap; 31 return *cast(Color*)&n; 32 } 33 34 const { 35 auto opBinary(string op : `*`)(in Color c) { 36 return Color( 37 r * c.r / 255, 38 g * c.g / 255, 39 b * c.b / 255, 40 a * c.a / 255 41 ); 42 } 43 44 auto opBinary(string op : `+`)(in Color c) { 45 return Color( 46 min(r + c.r, 255), 47 min(g + c.g, 255), 48 min(b + c.b, 255), 49 min(a + c.a, 255) 50 ); 51 } 52 53 auto opBinary(string op : `^`)(in Color c) { 54 // TODO: CHECK FORCORRECTNESS 55 auto od = 255 - c.a; 56 auto ra = c.a + a * od / 255; 57 58 // TODO: WORKAROUND 59 if (!ra) 60 ra = 1; 61 62 return Color( 63 (c.r * c.a + r * a * od / 255) / ra, 64 (c.g * c.a + g * a * od / 255) / ra, 65 (c.b * c.a + b * a * od / 255) / ra, 66 ra 67 ); 68 } 69 70 bool isGray(ubyte n) { 71 auto a = min(r, g, b), b = max(r, g, b); 72 return abs(a - b) < 30 && b < n; 73 } 74 75 bool compare(in Color c, ubyte d) { 76 return abs(r - c.r) + abs(g - c.g) + abs(b - c.b) + abs(a - c.a) <= d * 4; 77 } 78 } 79 80 ref opOpAssign(string op)(in Color c) { 81 return this = this.opBinary!op(c); 82 } 83 84 ubyte r, 85 g, 86 b, 87 a; 88 } 89 90 static assert(Color.sizeof == 4); 91 92 enum { 93 colorGray = Color(128, 128, 128, 200), 94 colorBlack = Color(0, 0, 0, 255), 95 colorWhite = Color(255, 255, 255, 255), 96 colorTransparent = Color.init, 97 } 98 99 enum { 100 IM_BMP, 101 IM_TGA, 102 IM_PNG, 103 IM_JPG, 104 IM_GIF 105 } 106 107 final class Image { 108 this(string name) { 109 auto f = new MmFile(name); 110 111 scope (exit) { 112 f.destroy; 113 } 114 115 this(f[]); 116 } 117 118 this(in void[] data) { 119 if (data[0..3] == "GIF") { 120 // in void* buffer, int len, int** delays, 121 // int* x, int* y, int* z, int* comp, int req_comp 122 // uint* _delayPtr = _delays.ptr; 123 auto p = cast(Color*) stbi_load_gif_from_memory( 124 data.ptr, cast(uint) data.length, cast(int**) &_delays, 125 cast(int*) &_w, cast(int*) &_h, cast(int*) &_frames, null, 4); 126 enforce(p, `unknown/corrupted image`); 127 128 _data = p[0 .. _w * _h * _frames].dup; 129 stbi_image_free(p); 130 } else { 131 // in void* buffer, int len, int* x, int* y, 132 // int* channels_in_file, int desired_channels 133 auto p = cast(Color*) stbi_load_from_memory( 134 data.ptr, cast(uint) data.length, 135 cast(int*)&_w, cast(int*)&_h, null, 4); 136 enforce(p, `unknown/corrupted image`); 137 138 _data = p[0 .. _w * _h].dup; 139 stbi_image_free(p); 140 141 _frames = 1; 142 _delays = cast(uint*) [1].ptr; 143 } 144 } 145 146 this(uint w, uint h, const(void)[] data = null) { 147 assert(w && h); 148 149 if (data.length) { 150 _data = cast(Color[]) data; 151 assert(_data.length == w * h); 152 } else { 153 _data = new Color[w * h]; 154 } 155 156 _w = w; 157 _h = h; 158 } 159 160 this(in Image im) { 161 _w = im._w; 162 _h = im._h; 163 _data = im._data.dup; 164 } 165 166 auto blit(in Image im, uint x, uint y) { 167 assert(x + im._w <= _w && y + im._h <= _h); 168 169 foreach (j; 0 .. im._h) { 170 memcpy(&this[x, j + y], &im[0, j], im.w * 4); 171 } 172 173 return this; 174 } 175 176 const { 177 auto dup() { 178 return new Image(this); 179 } 180 181 // void saveToFile(string name) 182 // { 183 // ubyte f; 184 185 // switch(name.extension.stripLeft(`.`).toLower) 186 // { 187 // case `png`: 188 // f = IM_PNG; 189 // break; 190 // case `bmp`: 191 // f = IM_BMP; 192 // break; 193 // case `tga`: 194 // f = IM_TGA; 195 // break; 196 // case `jpg`: 197 // case `jpeg`: 198 // f = IM_JPG; 199 // break; 200 // default: 201 // throw new Exception(`unknown image extension`); 202 // } 203 204 // saveToFile(name, f); 205 // } 206 207 // void saveToFile(string name, ubyte format) 208 // { 209 // std.file.write(name, save(format)); 210 // } 211 212 // auto save(ubyte format) 213 // { 214 // bool res; 215 // void[] data; 216 217 // final switch(format) 218 // { 219 // case IM_BMP: 220 // res = !!stbi_write_bmp_to_func(&writerCallback, &data, _w, _h, 4, _data.ptr); 221 // break; 222 // case IM_TGA: 223 // res = !!stbi_write_tga_to_func(&writerCallback, &data, _w, _h, 4, _data.ptr); 224 // break; 225 // case IM_PNG: 226 // res = !!stbi_write_png_to_func(&writerCallback, &data, _w, _h, 4, _data.ptr, 0); 227 // break; 228 // case IM_JPG: 229 // res = !!stbi_write_jpg_to_func(&writerCallback, &data, _w, _h, 4, _data.ptr, 95); 230 // } 231 232 // assert(res); 233 // return data; 234 // } 235 236 auto subImage(uint x, uint y, uint w, uint h) { 237 assert(x + w <= _w && y + h <= _h); 238 auto res = new Image(w, h); 239 240 foreach (i; 0 .. w) 241 foreach (j; 0 .. h) 242 res[j][] = this[y + j][x .. x + w][]; 243 244 return res; 245 } 246 247 // auto resize(uint w, uint h) 248 // { 249 // auto res = new Image(w, h); 250 // stbir_resize_uint8(cast(ubyte*)_data.ptr, _w, _h, 0, cast(ubyte*)res[].ptr, w, h, 0, 4); 251 // return res; 252 // } 253 254 /*auto rotate(float angle) 255 { 256 Image res; 257 if(angle > 85 && angle < 95) 258 { 259 res = new Image(_h, _w); 260 261 foreach(i; 0.._w) 262 foreach(j; 0.._h) 263 res[j, i] = this[i, j]; // TODO: IT'S -90, NOT 90 264 //res[j, i] = this[_w - i - 1, j]; 265 } 266 return res; 267 }*/ 268 } 269 270 void clean() { 271 _data 272 .filter!((ref a) => a.compare(Color(255, 0, 255, 255), 10)) 273 .each!((ref a) => a.a = 0); 274 275 normalizeTransparentPixels; 276 } 277 278 void normalizeTransparentPixels() { 279 const uint N = _w * _h; 280 281 auto opaque = new byte[N]; 282 auto loose = new bool[N]; 283 284 uint[] pending; 285 uint[] pendingNext; 286 287 static immutable byte[2][8] offsets = 288 [ 289 [-1, -1], 290 [0, -1], 291 [1, -1], 292 [-1, 0], 293 [1, 0], 294 [-1, 1], 295 [0, 1], 296 [1, 1] 297 ]; 298 299 auto image = cast(ubyte[]) _data; 300 301 for (uint i = 0, j = 3; i < N; i++, j += 4) { 302 if (image[j] == 0) { 303 bool isLoose = true; 304 305 int x = i % _w; 306 int y = i / _w; 307 308 for (int k = 0; k < 8; k++) { 309 int s = offsets[k][0]; 310 int t = offsets[k][1]; 311 312 if (x + s >= 0 && x + s < _w && y + t >= 0 && y + t < _h) { 313 uint index = j + 4 * (s + t * _w); 314 315 if (image[index] != 0) { 316 isLoose = false; 317 break; 318 } 319 } 320 } 321 322 if (isLoose) { 323 loose[i] = true; 324 } else { 325 pending ~= i; 326 } 327 } else { 328 opaque[i] = -1; 329 } 330 } 331 332 while (pending.length > 0) { 333 pendingNext = null; 334 335 for (uint p = 0; p < pending.length; p++) { 336 uint i = pending[p] * 4; 337 uint j = pending[p]; 338 339 int x = j % _w; 340 int y = j / _w; 341 342 int r = 0; 343 int g = 0; 344 int b = 0; 345 346 int count = 0; 347 348 for (uint k = 0; k < 8; k++) { 349 int s = offsets[k][0]; 350 int t = offsets[k][1]; 351 352 if (x + s >= 0 && x + s < _w && y + t >= 0 && y + t < _h) { 353 t *= _w; 354 355 if (opaque[j + s + t] & 1) { 356 uint index = i + 4 * (s + t); 357 358 r += image[index + 0]; 359 g += image[index + 1]; 360 b += image[index + 2]; 361 362 count++; 363 } 364 } 365 } 366 367 if (count > 0) { 368 image[i + 0] = cast(ubyte)(r / count); 369 image[i + 1] = cast(ubyte)(g / count); 370 image[i + 2] = cast(ubyte)(b / count); 371 372 opaque[j] = cast(byte) 0xFE; 373 374 for (uint k = 0; k < 8; k++) { 375 int s = offsets[k][0]; 376 int t = offsets[k][1]; 377 378 if (x + s >= 0 && x + s < _w && y + t >= 0 && y + t < _h) { 379 uint index = j + s + t * _w; 380 381 if (loose[index]) { 382 pendingNext ~= index; 383 loose[index] = false; 384 } 385 } 386 } 387 } else { 388 pendingNext ~= j; 389 } 390 } 391 392 if (pendingNext.length > 0) { 393 for (uint p = 0; p < pending.length; p++) { 394 opaque[pending[p]] >>= 1; 395 } 396 } 397 398 pending.swap(pendingNext); 399 } 400 } 401 402 auto flip() { 403 foreach (j; 0 .. _h / 2) 404 foreach (i; 0 .. _w) { 405 swap(this[i, j], this[i, _h - j - 1]); 406 } 407 408 return this; 409 } 410 411 auto mirror() { 412 foreach (i; 0 .. _w / 2) 413 foreach (j; 0 .. _h) { 414 swap(this[i, j], this[_w - i - 1, j]); 415 } 416 417 return this; 418 } 419 420 inout { 421 // auto toMipmaps() 422 // { 423 // inout(Image)[] res = [ this ]; 424 425 // while(true) 426 // { 427 // auto im = res.back; 428 429 // if(im.w == 1 && im.h == 1) 430 // { 431 // break; 432 // } 433 434 // auto 435 // w = max(im.w / 2, 1), 436 // h = max(im.h / 2, 1); 437 438 // res ~= cast(inout(Image))resize(w, h); 439 // } 440 441 // return res; 442 // } 443 444 ref opIndex(uint x, uint y) { 445 return _data[y * _w + x]; 446 } 447 448 auto opIndex(uint y) { 449 auto c = y * _w; 450 return _data[c .. c + _w]; 451 } 452 453 auto opSlice() { 454 return _data; 455 } 456 } 457 458 @property const { 459 auto w() { 460 return _w; 461 } 462 463 auto h() { 464 return _h; 465 } 466 467 auto frames() { 468 return _frames; 469 } 470 471 auto delays() { 472 return _delays; 473 } 474 } 475 476 private: 477 static extern (C) void writerCallback(void* ptr, void* data, int len) { 478 *cast(void[]*) ptr ~= data[0 .. len]; 479 } 480 481 Color[] _data; 482 483 uint _w, 484 _h, _frames; 485 486 uint* _delays; 487 } 488 489 // static this() 490 // { 491 // // To prevent linker from stripping the symbol 492 // auto f = &compress_for_stb_image_write; 493 // } 494 495 // private extern(C): 496 497 // ubyte* compress_for_stb_image_write(in ubyte* data, uint len, uint* resLen, int level) 498 // { 499 // import core.stdc.stdlib; 500 501 // auto dst = compressBound(len); 502 // auto res = cast(ubyte*)malloc(dst); 503 504 // if(compress2(res, &dst, data, len, level) == Z_OK) 505 // { 506 // *resLen = cast(uint)dst; 507 // return res; 508 // } 509 510 // free(res); 511 // return null; 512 // }