[go: up one dir, main page]

Menu

[r69]: / lpi_texture.cpp  Maximize  Restore  History

Download this file

884 lines (773 with data), 28.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
/*
Copyright (c) 2005-2007 Lode Vandevenne
All rights reserved.
This file is part of Lode's Programming Interface.
Lode's Programming Interface is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Lode's Programming Interface is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Lode's Programming Interface. If not, see <http://www.gnu.org/licenses/>.
*/
#include "lpi_texture.h"
#include "lodepng.h"
#include "lpi_screen.h"
#include "lpi_general.h"
namespace lpi
{
Texture emptyTexture;
//****************************************************************************//
////////////////////////////////////////////////////////////////////////////////
//Texture Class/////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//****************************************************************************//
Texture::Texture()
{
deleteBuffer();
generated = false;
}
void Texture::operator*=(double a)
{
for(size_t y = 0; y < v2; y++)
for(size_t x = 0; x < u2; x++)
for(size_t c = 0; c < 3; c++)
{
buffer[4 * u2 * y + 4 * x + c] = (unsigned char)(buffer[4 * u2 * y + 4 * x + c] * a);
}
upload();
}
//make memory for the buffer of the texture
void Texture::makeBuffer(int u, int v)
{
//find first larger power of two of width and store it in u2
int u2 = 1;
while(u2 < u) u2 *= 2;
//find first larger power of two of height and store it in v2
int v2 = 1;
while(v2 < v) v2 *= 2;
buffer.resize(4 * u2 * v2);
this->u = u;
this->v = v;
this->u2 = u2;
this->v2 = v2;
this->u3 = u / double(u2);
this->v3 = v / double(v2);
}
//free the memory again
void Texture::deleteBuffer()
{
buffer.clear();
}
void Texture::create(const ColorRGB& color, int w, int h)
{
//if anything is invalid, use full rectangle instead
if(w < 0) w = 0;
if(h < 0) h = 0;
deleteBuffer();
makeBuffer(w, h);
for(size_t y = 0; y < v2; y++)
for(size_t x = 0; x < u2; x++)
{
this->buffer[4 * u2 * y + 4 * x + 0] = color.r;
this->buffer[4 * u2 * y + 4 * x + 1] = color.g;
this->buffer[4 * u2 * y + 4 * x + 2] = color.b;
this->buffer[4 * u2 * y + 4 * x + 3] = color.a;
}
upload();
}
/*
This loads the texture from a buffer or from a part of the buffer in the rectangle (x1,y1)-(x2,y2)
To get full buffer, make x1 = -1
w and h are width and height of the buffer, and the function assumes the memory of the buffer is big enough for that
The width of the texture buffer becomes x2 - x1, y2 - y1
Note that the buffer has an third dimension for the color channels and alpha
*/
void Texture::create(unsigned char * buffer, int w, int h, const AlphaEffect& effect, int x1, int y1, int x2, int y2)
{
//if anything is invalid, use full rectangle instead
if(x1 < 0 || y1 < 0 || x2 < 0 || y2 < 0 || x1 > w || y1 > h || x2 > w || y2 > h || x1 > x2 || y1 > y2)
{
x1 = 0;
y1 = 0;
x2 = w;
y2 = h;
}
deleteBuffer();
makeBuffer(x2 - x1, y2 - y1);
for(int y = 0; y < y2 - y1; y++)
for(int x = 0; x < x2 - x1; x++)
for(int c = 0; c < 4; c++)
{
this->buffer[4 * u2 * y + 4 * x + c] = buffer[4 * w * (y1 + y) + 4 * (x1 + x) + c];
}
calculateAlpha(effect);
upload();
}
/*
Load the texture from a file
currently works with the png loader function
*/
void Texture::create(const std::string& filename, const AlphaEffect& effect, int x1, int y1, int x2, int y2)
{
LodePNG::Decoder pngdec;
std::vector<unsigned char> file;
LodePNG::loadFile(file, filename);
//create the image and info std::vectors
std::vector<unsigned char> image;
//load the png and if it gives an error return it
pngdec.decode(image, file);
if(pngdec.hasError())
{
std::cout << "\npng loading error " << pngdec.getError() << " in file " << filename;
return;
}
if(x1 == -1 && y1 == -1 && x2 == -1 && y2 == -1 && isPowerOfTwo(pngdec.getWidth()) && isPowerOfTwo(pngdec.getHeight())) //this should be faster
{
deleteBuffer();
makeBuffer(pngdec.getWidth(), pngdec.getHeight());
buffer.swap(image);
calculateAlpha(effect);
upload();
}
else create(&image[0], pngdec.getWidth(), pngdec.getHeight(), effect, x1, y1, x2, y2);
}
/*
Same as create, but only the alpha channel is affected, and no buffer is made so make sure the texture already has the correct size (and rgb colors) before you create its alpha channel.
The RED channel of the input image is used to make this alpha channel!
*/
void Texture::createAlpha(unsigned char * buffer, int w, int h, int x1, int y1, int x2, int y2)
{
//if anything is invalid, use full rectangle instead
if(x1 < 0 || y1 < 0 || x2 < 0 || y2 < 0 || x1 > w || y1 > h || x2 > w || y2 > h || x1 > x2 || y1 > y2)
{
x1 = 0;
y1 = 0;
x2 = w;
y2 = h;
}
for(int y = 0; y < y2 - y1; y++)
for(int x = 0; x < x2 - x1; x++)
{
this->buffer[4 * u2 * y + 4 * x + 3] = buffer[4 * w * (y1 + y) + 4 * (x1 + x) + 0];
}
upload();
}
/*
Load the texture's alpha channel from a file
Make sure the alpha channel file is NOT larger than the current size the texture has!
*/
void Texture::createAlpha(const std::string& filename, int x1, int y1, int x2, int y2)
{
LodePNG::Decoder pngdec;
std::vector<unsigned char> file;
LodePNG::loadFile(file, filename);
//create the image and info std::vectors
std::vector<unsigned char> image;
//load the png and if it gives an error return it
pngdec.decode(image, file);
if(pngdec.hasError())
{
std::cout << "\npng loading error " << pngdec.getError() << " in file " << filename;
return;
}
createAlpha(&image[0], pngdec.getWidth(), pngdec.getHeight(), x1, y1, x2, y2);
}
//Create an alpha channel for the texture with the wanted effect
void Texture::calculateAlpha(const AlphaEffect& effect)
{
createImageAlpha(&buffer[0], u2, v2, effect);
upload();
}
//This generates the OpenGL texture so that OpenGL can use it, also use after changing the texture buffer
void Texture::upload()
{
if(!generated)
{
glGenTextures(1, texture);
generated = true;
}
bind();
glTexImage2D(GL_TEXTURE_2D, 0, 4, u2, v2, 0, GL_RGBA, GL_UNSIGNED_BYTE, &buffer[0]);
}
void Texture::reupload()
{
generated = false;
upload();
}
//make this the selected one for drawing
void Texture::bind() const
{
glBindTexture(GL_TEXTURE_2D, texture[0]);
if(enabledSmoothing())
{
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
else
{
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
}
}
//Draw the texture on screen
void Texture::draw(int x, int y, const ColorRGB& colorMod, int sizex, int sizey, int skewx, int skewy) const
{
if(sizex < 0) sizex = u;
if(sizey < 0) sizey = v;
if(sizex == 0 || sizey == 0) return;
glEnable(GL_TEXTURE_2D);
glColor4ub(colorMod.r, colorMod.g, colorMod.b, colorMod.a);
bind();
setOpenGLScissor(); //everything that draws something must always do this
//note how in the texture coordinates x and y are swapped because the texture buffers are 90 degrees rotated
glBegin(GL_QUADS);
glTexCoord2d(0.0, 0.0); glVertex3d(x + 0 + skewx, y + 0 + 0 , 1);
glTexCoord2d(+u3, 0.0); glVertex3d(x + sizex + skewx, y + 0 + skewy, 1);
glTexCoord2d(+u3, +v3); glVertex3d(x + sizex + 0 , y + sizey + skewy, 1);
glTexCoord2d(0.0, +v3); glVertex3d(x + 0 + 0 , y + sizey + 0 , 1);
glEnd();
}
//Draw the texture on screen
void Texture::draw(int x1, int y1, int x2, int y2, const ColorRGB& colorMod) const
{
if(x2 - x1 == 0 || y2 - y1 == 0) return;
glEnable(GL_TEXTURE_2D);
glColor4ub(colorMod.r, colorMod.g, colorMod.b, colorMod.a);
bind();
setOpenGLScissor(); //everything that draws something must always do this
//note how in the texture coordinates x and y are swapped because the texture buffers are 90 degrees rotated
glBegin(GL_QUADS);
glTexCoord2d(0.0, 0.0); glVertex3d(x1, y1, 1);
glTexCoord2d(+u3, 0.0); glVertex3d(x2, y1, 1);
glTexCoord2d(+u3, +v3); glVertex3d(x2, y2, 1);
glTexCoord2d(0.0, +v3); glVertex3d(x1, y2 , 1);
glEnd();
}
void Texture::draw(int x, int y, double scale, const ColorRGB& colorMod) const
{
double sizex = u * scale;
double sizey = v * scale;
if(sizex == 0.0 || sizey == 0.0) return;
glEnable(GL_TEXTURE_2D);
glColor4ub(colorMod.r, colorMod.g, colorMod.b, colorMod.a);
bind();
setOpenGLScissor(); //everything that draws something must always do this
//note how in the texture coordinates x and y are swapped because the texture buffers are 90 degrees rotated
glBegin(GL_QUADS);
glTexCoord2d(0.0, 0.0); glVertex3d(x + 0 , y + 0 , 1);
glTexCoord2d(+u3, 0.0); glVertex3d(x + sizex, y + 0 , 1);
glTexCoord2d(+u3, +v3); glVertex3d(x + sizex, y + sizey, 1);
glTexCoord2d(0.0, +v3); glVertex3d(x + 0 , y + sizey, 1);
glEnd();
}
//Draw the texture on screen where x and y are the center instead of the top left corner
void Texture::drawCentered(int x, int y, const ColorRGB& colorMod, int sizex, int sizey, int skewx, int skewy) const
{
if(sizex < 0) sizex = u;
if(sizey < 0) sizey = v;
if(sizex == 0 || sizey == 0) return;
draw(x - sizex / 2, y - sizey / 2, colorMod, sizex, sizey, skewx, skewy);
}
void Texture::drawCentered(int x, int y, double scale, const ColorRGB& colorMod) const
{
int sizex = int(u * scale);
int sizey = int (v * scale);
if(sizex < 0) sizex = u;
if(sizey < 0) sizey = v;
if(sizex == 0 || sizey == 0) return;
draw(x - sizex / 2, y - sizey / 2, colorMod, sizex, sizey, 0, 0);
}
/*
draws the texture repeated, useful to fill something with a tilable texture
The texture size must be a power of 2, or you get ugly black things in between
scale is a scaling factor of the texture
*/
void Texture::drawRepeated(int x1, int y1, int x2, int y2, double scalex, double scaley, const ColorRGB& colorMod) const
{
glEnable(GL_TEXTURE_2D);
glColor4ub(colorMod.r, colorMod.g, colorMod.b, colorMod.a);
bind();
setOpenGLScissor(); //everything that draws something must always do this
double coorx = (double(x2 - x1) / u) / scalex;//(u3 * (x2 - x1)) / scalex;
double coory = (double(y2 - y1) / v) / scaley; //(v3 * (y2 - y1)) / scaley;
//note how in the texture coordinates x and y are swapped because the texture buffers are 90 degrees rotated
glBegin(GL_QUADS);
glTexCoord2d(0.0, 0.0); glVertex3d(x1, y1, 1);
glTexCoord2d(0.0, +coory); glVertex3d(x1, y2, 1);
glTexCoord2d(+coorx, +coory); glVertex3d(x2, y2, 1);
glTexCoord2d(+coorx, 0.0); glVertex3d(x2, y1, 1);
glEnd();
}
void loadTextures(std::vector<unsigned char>& buffer, std::vector<Texture>& textures, int widths, int heights, int w, int h, const AlphaEffect& effect)
{
int numx, numy;
if(widths > 0 && heights > 0)
{
numx = w / widths;
numy = h / heights;
}
else
{
numx = 1;
numy = 1;
widths = w;
heights = h;
}
//the order is row per row, column per column
for(int y = 0; y < numy; y++)
for(int x = 0; x < numx; x++)
{
textures.resize(textures.size() + 1);
textures.back().create(&buffer[0], w, h, effect, x * widths, y * heights, (x + 1) * widths, (y + 1) * heights);
}
}
/*
*/
void loadTextures(const std::string& filename, std::vector<Texture>& textures, int widths, int heights, const AlphaEffect& effect)
{
LodePNG::Decoder pngdec;
std::vector<unsigned char> file;
LodePNG::loadFile(file, filename);
std::vector<unsigned char> image;
//load the png and if it gives an error return it
pngdec.decode(image, file);
if(pngdec.hasError())
{
std::cout << "\npng loading error " << pngdec.getError() << " in file " << filename;
return;
}
loadTextures(image, textures, widths, heights, pngdec.getWidth(), pngdec.getHeight(), effect);
}
void loadTexturesAlpha(std::vector<unsigned char>& buffer, std::vector<Texture>& textures, int widths, int heights, int w, int h)
{
int numx, numy;
if(widths > 0 && heights > 0)
{
numx = w / widths;
numy = h / heights;
}
else
{
numx = 1;
numy = 1;
widths = w;
heights = h;
}
//the order is row per row, column per column
int index = 0;
for(int y = 0; y < numy; y++)
for(int x = 0; x < numx; x++)
{
textures[index].createAlpha(&buffer[0], w, h, x * widths, y * heights, (x + 1) * widths, (y + 1) * heights);
index++;
}
}
/*
*/
void loadTexturesAlpha(const std::string& filename, std::vector<Texture>& textures, int widths, int heights)
{
LodePNG::Decoder pngdec;
std::vector<unsigned char> file;
LodePNG::loadFile(file, filename);
std::vector<unsigned char> image;
//load the png and if it gives an error return it
pngdec.decode(image, file);
if(pngdec.hasError())
{
std::cout << "\npng loading error " << pngdec.getError() << " in file " << filename;
return;
}
loadTexturesAlpha(image, textures, widths, heights, pngdec.getWidth(), pngdec.getHeight());
}
//****************************************************************************//
////////////////////////////////////////////////////////////////////////////////
//ALPHA/////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//****************************************************************************//
/*
The function createImageAlpha takes an image buffer and gives it an alpha channel.
For example, it can give an image an alpha channel to make darker pixels more
translucent, brighter pixels more translucent, give the image an invisible color
(a color key), etc...
The AlphaEffect effect determines how the alpha channel of the image is calculated:
AlphaEffect contains a color, an alpha value, and the integer style:
Concerning the integer style:
The 6 least significant bits are used to ask for a certain effect.
The other bits can be turned on or off to modify the effect in a certain way.
The RGB color color decides what pixels to perform the effect on.
The alpha value decides what to set pixels to for effects that need this.
Styles of the integer "style":
In the explanation of the styles below:
A means "the alpha value of the current pixel of the image"
a means "the alpha value of the effect parameter",
K means "the alpha value of pixels of the image that have the same RGB color as
the color of the effect parameter", this is the Key color.
r, g and b mean the red, green and blue component of the current pixel.
min means the component r, g or b with the lowest value
max means the component r, g or b with the highest value
sat means the saturation of the current pixel's RGB color
For example:
A = a means: "set the alpha channel of the current image pixel to effect.alpha"
K = 0 means "make pixels of the image that have the same RGB color as alpha.color invisible".
Values of the first 6 bits (the least significant bits)
+-------+---------------------+------------------------------------------------+
| value | effect | description |
+-------+---------------------+------------------------------------------------+
| 0 | none | do nothing |
| 1 | A = a | set the alpha channel of the whole image to a |
| 2 | A = a, K = 255 | same as 1, but make the key color opaque |
| 3 | A = 255, K = a | make image opaque, but set key color to a |
+-------+---------------------+------------------------------------------------+
| 4 | A = (r + g + b) / 3 | set alpha channel to the Brightness |
| 5 | A = (min + max) / 2 | set alpha channel to the Lightness |
| 6 | A = min | set alpha channel to the Darkness |
| 7 | A = max | set alpha channel to the Value |
| 8 | A = sat | set alpha channel to the Saturation |
+-------+---------------------+------------------------------------------------+
| 9 | A = r | set alpha channel to Red |
| 10 | A = g | set alpha channel to Green |
| 11 | A = b | set alpha channel to Blue |
| 12 | A = (g + b) / 2 | set alpha channel to Cyan |
| 13 | A = (r + b) / 2 | set alpha channel to Magenta |
| 14 | A = (r + g) / 2 | set alpha channel to Yellow |
+-------+---------------------+------------------------------------------------+
| 15 | A = A / a | divide alpha channel through a (truncated) |
| 16 | A = A * a | multiply alpha channel with a (truncated) |
| 15 | A = A - a | subtract a from alpha cnannel (truncated) |
| 16 | A = A + a | add a to alpha channel (truncated) |
+-------+---------------------+------------------------------------------------+
| 19 | A = mod(RGB) | modulated |
| 20 | A = imod(RGB) | inverse modulated |
+-------+---------------------+------------------------------------------------+
Optional values of the other bits:
+-------+---------------------+------------------------------------------------+
| bit | effect | description |
+-------+---------------------+------------------------------------------------+
| &64 | A = 255 - A | after calculating A, invert it |
| &128 | K = 0 | make key color invisible, e.g. for sprites |
| &256 | color = alphaColor | after gen. alpha, fix color; eg for particles |
| &512 | color = Hp,Sp,Va | color is set to hue and sat of pixel, brightness of alphaColor |
| &1024| | convert RGB 128,0,128 to black with alpha channel = 128 |
| &2048| | convert RGB 192,0,192 to black with alpha channel = 64 |
+-------+---------------------+------------------------------------------------+
EXAMPLE: style 68 (4 | 64) makes alphachannel depend on brightness, and makes key color zero
*/
void createImageAlpha(unsigned char* image, int w, int h, const AlphaEffect& effect)
{
if(effect.style == 0) return; //no effect to be done, just return instead of going through the loop
if((effect.style & 63) > 0)
switch(effect.style & 63) //&63 to filter out the extra modifiers: those are handled later on
{
case 1:
for(int x = 0; x < w; x++)
for(int y = 0; y < h; y++)
{
int index = h * 4 * x + 4 * y;
image[index + 3] = effect.alpha;
}
break;
case 2:
for(int x = 0; x < w; x++)
for(int y = 0; y < h; y++)
{
int index = h * 4 * x + 4 * y;
int r = image[index + 0];
int g = image[index + 1];
int b = image[index + 2];
bool alphaPixel = (r == effect.alphaColor.r && g == effect.alphaColor.g && b == effect.alphaColor.b);
if(!alphaPixel) image[index + 3] = effect.alpha;
else image[index + 3] = 255;
}
break;
case 3:
for(int x = 0; x < w; x++)
for(int y = 0; y < h; y++)
{
int index = h * 4 * x + 4 * y;
int r = image[index + 0];
int g = image[index + 1];
int b = image[index + 2];
bool alphaPixel = (r == effect.alphaColor.r && g == effect.alphaColor.g && b == effect.alphaColor.b);
if(!alphaPixel) image[index + 3] = 255;
else image[index + 3] = effect.alpha;
}
break;
case 4:
for(int x = 0; x < w; x++)
for(int y = 0; y < h; y++)
{
int index = h * 4 * x + 4 * y;
int r = image[index + 0];
int g = image[index + 1];
int b = image[index + 2];
int alpha = (r + g + b) / 3;
if(alpha < 0) alpha = 0; if(alpha > 255) alpha = 255;
image[index + 3] = alpha;
}
break;
case 5:
for(int x = 0; x < w; x++)
for(int y = 0; y < h; y++)
{
int index = h * 4 * x + 4 * y;
int r = image[index + 0];
int g = image[index + 1];
int b = image[index + 2];
int minc = r; if(g < minc) minc = g; if(b < minc) minc = b;
int maxc = r; if(g > maxc) maxc = g; if(b > maxc) maxc = b;
image[index + 3] = (minc + maxc) / 2;
}
break;
case 6:
for(int x = 0; x < w; x++)
for(int y = 0; y < h; y++)
{
int index = h * 4 * x + 4 * y;
int r = image[index + 0];
int g = image[index + 1];
int b = image[index + 2];
int minc = r; if(g < minc) minc = g; if(b < minc) minc = b;
image[index + 3] = minc;
}
break;
case 7:
for(int x = 0; x < w; x++)
for(int y = 0; y < h; y++)
{
int index = h * 4 * x + 4 * y;
int r = image[index + 0];
int g = image[index + 1];
int b = image[index + 2];
int maxc = r; if(g > maxc) maxc = g; if(b > maxc) maxc = b;
image[index + 3] = maxc;
}
break;
case 8: //saturation of the HSV model
for(int x = 0; x < w; x++)
for(int y = 0; y < h; y++)
{
int index = h * 4 * x + 4 * y;
int r = image[index + 0];
int g = image[index + 1];
int b = image[index + 2];
int minc = r; if(g < minc) minc = g; if(b < minc) minc = b;
int maxc = r; if(g > maxc) maxc = g; if(b > maxc) maxc = b;
if(maxc == 0) image[index + 3] = 0;
else image[index + 3] = (maxc - minc) / maxc;
break;
}
break;
case 9:
for(int x = 0; x < w; x++)
for(int y = 0; y < h; y++)
{
int index = h * 4 * x + 4 * y;
int r = image[index + 0];
image[index + 3] = r;
}
break;
case 10:
for(int x = 0; x < w; x++)
for(int y = 0; y < h; y++)
{
int index = h * 4 * x + 4 * y;
int g = image[index + 1];
image[index + 3] = g;
}
break;
case 11:
for(int x = 0; x < w; x++)
for(int y = 0; y < h; y++)
{
int index = h * 4 * x + 4 * y;
int b = image[index + 2];
image[index + 3] = b;
}
break;
case 12:
for(int x = 0; x < w; x++)
for(int y = 0; y < h; y++)
{
int index = h * 4 * x + 4 * y;
int g = image[index + 1];
int b = image[index + 2];
image[index + 3] = (g + b) / 2;
}
break;
case 13:
for(int x = 0; x < w; x++)
for(int y = 0; y < h; y++)
{
int index = h * 4 * x + 4 * y;
int r = image[index + 0];
int b = image[index + 2];
image[index + 3] = (r + b) / 2;
}
break;
case 14:
for(int x = 0; x < w; x++)
for(int y = 0; y < h; y++)
{
int index = h * 4 * x + 4 * y;
int r = image[index + 0];
int g = image[index + 1];
image[index + 3] = (r + g) / 2;
}
break;
case 15:
for(int x = 0; x < w; x++)
for(int y = 0; y < h; y++)
{
int index = h * 4 * x + 4 * y;
image[index + 3] /= effect.alpha;
}
break;
case 16:
for(int x = 0; x < w; x++)
for(int y = 0; y < h; y++)
{
int index = h * 4 * x + 4 * y;
int temp = image[index + 3] * effect.alpha;
if(temp > 255) temp = 255;
image[index + 3] = temp;
}
break;
case 17:
for(int x = 0; x < w; x++)
for(int y = 0; y < h; y++)
{
int index = h * 4 * x + 4 * y;
int temp = image[index + 3] - effect.alpha;
if(temp < 0) temp = 0;
image[index + 3] = temp;
}
break;
case 18:
for(int x = 0; x < w; x++)
for(int y = 0; y < h; y++)
{
int index = h * 4 * x + 4 * y;
int temp = image[index + 3] + effect.alpha;
if(temp > 255) temp = 255;
image[index + 3] = temp;
}
break;
case 19: //modulated
for(int x = 0; x < w; x++)
for(int y = 0; y < h; y++)
{
int index = h * 4 * x + 4 * y;
int r = image[index + 0];
int g = image[index + 1];
int b = image[index + 2];
int alpha = (2 * abs(r - 128)
+ 2 * abs(g - 128)
+ 2 * abs(b - 128)) / 3;
if(alpha < 0) alpha = 0; if(alpha > 255) alpha = 255;
image[index + 3] = alpha;
}
break;
case 20: //inverse modulated
for(int x = 0; x < w; x++)
for(int y = 0; y < h; y++)
{
int index = h * 4 * x + 4 * y;
int r = image[index + 0];
int g = image[index + 1];
int b = image[index + 2];
int alpha = (2 * abs(r - 128)
+ 2 * abs(g - 128)
+ 2 * abs(b - 128)) / 3;
if(alpha < 0) alpha = 0; if(alpha > 255) alpha = 255;
image[index + 3] = 255 - alpha;
}
break;
default:
break;
}
if(effect.style & 64)
for(int x = 0; x < w; x++)
for(int y = 0; y < h; y++)
{
int index = h * 4 * x + 4 * y;
image[index + 3] = 255 - image[index + 3];
}
if(effect.style & 128)
for(int x = 0; x < w; x++)
for(int y = 0; y < h; y++)
{
int index = h * 4 * x + 4 * y;
int r = image[index + 0];
int g = image[index + 1];
int b = image[index + 2];
bool alphaPixel = (r == effect.alphaColor.r && g == effect.alphaColor.g && b == effect.alphaColor.b);
if(alphaPixel) image[index + 3] = 0;
}
if(effect.style & 256)
for(int x = 0; x < w; x++)
for(int y = 0; y < h; y++)
{
int index = h * 4 * x + 4 * y;
image[index + 0] = effect.alphaColor.r;
image[index + 1] = effect.alphaColor.g;
image[index + 2] = effect.alphaColor.b;
}
if(effect.style & 512)
for(int x = 0; x < w; x++)
for(int y = 0; y < h; y++)
{
int index = h * 4 * x + 4 * y;
int r = image[index + 0];
int g = image[index + 1];
int b = image[index + 2];
ColorRGB pixelRGB = ColorRGB(r, g, b);
ColorHSV pixelHSV = RGBtoHSV(pixelRGB);
ColorHSV alphaHSV = RGBtoHSV(effect.alphaColor);
ColorRGB HpSpVa = HSVtoRGB(ColorHSV(pixelHSV.h, pixelHSV.s, alphaHSV.v));
image[index + 0] = HpSpVa.r;
image[index + 1] = HpSpVa.g;
image[index + 2] = HpSpVa.b;
}
if(effect.style & 1024 || effect.style & 2048)
for(int x = 0; x < w; x++)
for(int y = 0; y < h; y++)
{
int index = h * 4 * x + 4 * y;
int r = image[index + 0];
int g = image[index + 1];
int b = image[index + 2];
if(effect.style & 1024 && r == 128 && g == 0 && b == 128)
{
image[index + 0] = 0;
image[index + 1] = 0;
image[index + 2] = 0;
image[index + 3] = 128;
}
if(effect.style & 2048 && r == 192 && g == 0 && b == 192)
{
image[index + 0] = 0;
image[index + 1] = 0;
image[index + 2] = 0;
image[index + 3] = 64;
}
}
}
AlphaEffect::AlphaEffect(int style, unsigned char alpha, const ColorRGB& alphaColor)
{
this->style = style;
this->alpha = alpha;
this->alphaColor = alphaColor;
}
void loadTexturesFromBase64PNG(std::vector<Texture>& textures, const std::string& base64, int widths, int heights, const AlphaEffect& effect)
{
LodePNG::Decoder pngdec;
std::vector<unsigned char> decoded64, pixels;
decodeBase64(decoded64, base64);
pngdec.decode(pixels, decoded64);
loadTextures(pixels, textures, widths, heights, pngdec.getWidth(), pngdec.getHeight(), effect);
}
} //namespace lpi