Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (600)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  some new blendmodes (add, multiply, overlay, etc..)  (Read 6073 times)
0 Members and 1 Guest are viewing this topic.
Offline Dx4

Junior Devvie


Medals: 5



« Posted 2011-07-23 09:32:35 »

this class allows you to add new blendmodes to java. included are:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
   public static enum Mode {
      Normal,
      Add,
      Multiply,
      Erase,
      Average,
      Overlay,
      Screen,
      Glow,
      Reflect,
      Red,
      Green,
      Blue,
      ColorDodge,
      ColorBurn,a
      HardLight,
   }


supports both premultiplied and unpremultiplied colors.

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  
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


package as.blend;

/**
 *
 * @author David
 */

public class Compositor {
   public static final int ALPHA_MASK = 0xff000000;
   public static final int RED_MASK = 0x00ff0000;
   public static final int GREEN_MASK = 0x0000ff00;
   public static final int BLUE_MASK = 0x000000ff;

   public static enum Mode {
      Normal,
      Add,
      Multiply,
      Erase,
      Average,
      Overlay,
      Screen,
      Glow,
      Reflect,
      Red,
      Green,
      Blue,
      ColorDodge,
      ColorBurn,
      HardLight,
   }

   public static int premultiply(int argb) {
      int a = argb >>> 24;

      if (a == 0) {
         return 0;
      } else if (a == 255) {
         return argb;
      } else {
         return (a << 24) | multRGB(argb, a);
      }
   }

   public static int multRGB(int src, int multiplier) {
      multiplier++;
      return ((src & RED_MASK) * multiplier) >> 8 & RED_MASK
            | ((src & GREEN_MASK) * multiplier) >> 8 & GREEN_MASK
            | ((src & BLUE_MASK) * multiplier) >> 8;
   }

   public static void blendPixelPremultiplied(int [] pixels, int offset, int srcPx, int alpha, Mode mode) {
      switch (mode) {
         case Normal:
            blendPixel_SrcOver(pixels, offset, srcPx, alpha); return;
         case Add:
            blendPixel_Add(pixels, offset, srcPx, alpha); return;
         case Multiply:
            blendPixel_Multiply(pixels, offset, srcPx, alpha); return;
         case Erase:
            blendPixel_DstOut(pixels, offset, srcPx, alpha); return;
         case Average:
            blendPixel_Average(pixels, offset, srcPx, alpha); return;
         case Overlay:
            blendPixel_Overlay(pixels, offset, srcPx, alpha); return;
         case Screen:
            blendPixel_Screen(pixels, offset, srcPx, alpha); return;
         case Glow:
            blendPixel_Glow(pixels, offset, srcPx, alpha); return;
         case Reflect:
            blendPixel_Reflect(pixels, offset, srcPx, alpha); return;
         case Red:
            blendPixel_Red(pixels, offset, srcPx, alpha); return;
         case Green:
            blendPixel_Green(pixels, offset, srcPx, alpha); return;
         case Blue:
            blendPixel_Blue(pixels, offset, srcPx, alpha); return;
         case ColorBurn:
            blendPixel_ColorBurn(pixels, offset, srcPx, alpha); return;
         case ColorDodge:
            blendPixel_ColorDodge(pixels, offset, srcPx, alpha); return;
         case HardLight:
            blendPixel_HardLight(pixels, offset, srcPx, alpha); return;
      }
      throw new BlendModeNotAvailableException(mode);
   }

   public static class BlendModeNotAvailableException extends RuntimeException {
      public BlendModeNotAvailableException(Mode mode) {
         super(mode + " is not available");
      }
   }

   /**
    * blends two non premultiplied colors together
    * @param pixels
    * @param offset
    * @param srcPx
    * @param alpha
    * @param mode
    */

   public static void blendPixelUnpremultiplied(int[] pixels, int offset, int srcPx, int alpha, Mode mode) {
      pixels[offset] = premultiplyEx(pixels[offset]);
      blendPixelPremultiplied(pixels, offset, premultiplyEx(srcPx), alpha, mode);
      pixels[offset] = unpremultiply(pixels[offset]);
   }

   public static void blendPixel_SrcOver(int pixels[], int offset, int srcPx, int alpha) {
      int destPx = pixels[offset];
      int srcA = srcPx >>> 24;
      int srcR = (srcPx & RED_MASK) >>> 16;
      int srcG = (srcPx & GREEN_MASK) >>> 8;
      int srcB = (srcPx & BLUE_MASK);
      if (alpha != 255) {
         srcR = mult(srcR, alpha);
         srcG = mult(srcG, alpha);
         srcB = mult(srcB, alpha);
         srcA = mult(srcA, alpha);
      }
      int destA = (destPx) >>> 24;
      int destR = (destPx & RED_MASK) >>> 16;
      int destG = (destPx & GREEN_MASK) >>> 8;
      int destB = (destPx & BLUE_MASK);

      final int oneMinusSrcA = 0xff - srcA;

      pixels[offset] =
            (blendOneMinus(srcA, destA, oneMinusSrcA) << 24
            | blendOneMinus(srcR, destR, oneMinusSrcA) << 16
            | blendOneMinus(srcG, destG, oneMinusSrcA) << 8
            | blendOneMinus(srcB, destB, oneMinusSrcA));
   }

   /**
    * Blends pixel using DstOut algorithm
    * @param pixels
    * @param offset
    * @param srcPx
    * @param alpha
    */

   public static void blendPixel_DstOut(int [] pixels, int offset, int srcPx, int alpha) {
      int destPx = pixels[offset];
      int srcA = srcPx >>> 24;
      if (alpha != 255) {
         srcA = mult(srcA, alpha);
      }
      int destA = (destPx) >>> 24;
      int destR = (destPx & RED_MASK) >>> 16;
      int destG = (destPx & GREEN_MASK) >>> 8;
      int destB = (destPx & BLUE_MASK);

        final int oneMinusAsAe = 0xff - mult(alpha, srcA);

      pixels[offset] =
            ( mult(destA, oneMinusAsAe) << 24
            | mult(destR, oneMinusAsAe) << 16
            | mult(destG, oneMinusAsAe) << 8
            | mult(destB, oneMinusAsAe));

   }

   public static void blendPixel_Average(int [] pixels, int offset, int srcPx, int alpha) {
      int destPx = pixels[offset];
      int srcA = srcPx >>> 24;
      int srcR = (srcPx & RED_MASK) >>> 16;
      int srcG = (srcPx & GREEN_MASK) >>> 8;
      int srcB = (srcPx & BLUE_MASK);
      if (alpha != 255) {
         srcR = mult(srcR, alpha);
         srcG = mult(srcG, alpha);
         srcB = mult(srcB, alpha);
         srcA = mult(srcA, alpha);
      }
      int destA = (destPx) >>> 24;
      int destR = (destPx & RED_MASK) >>> 16;
      int destG = (destPx & GREEN_MASK) >>> 8;
      int destB = (destPx & BLUE_MASK);

      pixels[offset] =
            ( (srcA + destA) << 23
            | (srcR + destR) << 15
            | (srcG + destG) << 7
            | (srcB + destB) >> 1);
   }

   public static void blendPixel_Multiply(int [] pixels, int offset, int srcPx, int alpha) {
      int destPx = pixels[offset];
      int srcA = srcPx >>> 24;
      int srcR = (srcPx & RED_MASK) >>> 16;
      int srcG = (srcPx & GREEN_MASK) >>> 8;
      int srcB = (srcPx & BLUE_MASK);
      if (alpha != 255) {
         srcR = mult(srcR, alpha);
         srcG = mult(srcG, alpha);
         srcB = mult(srcB, alpha);
         srcA = mult(srcA, alpha);
      }
      int destA = (destPx) >>> 24;
      int destR = (destPx & RED_MASK) >>> 16;
      int destG = (destPx & GREEN_MASK) >>> 8;
      int destB = (destPx & BLUE_MASK);

      final int oneMinusSrcA = 0xff - srcA,
              oneMinusDstA = 0xff - destA;
      pixels[offset] =
            blend(srcA, destA, srcA) << 24 |
            (mult(srcR, destR) + mult(destR, oneMinusSrcA) + mult(srcR, oneMinusDstA)) << 16 |
            (mult(srcG, destG) + mult(destG, oneMinusSrcA) + mult(srcG, oneMinusDstA)) << 8 |
            mult(srcB, destB) + mult(destB, oneMinusSrcA) + mult(srcB, oneMinusDstA);
   }

   public static void blendPixel_Add(int [] pixels, int offset, int srcPx, int alpha) {
      int destPx = pixels[offset];
      int srcA = srcPx >>> 24;
      int srcR = (srcPx & RED_MASK) >>> 16;
      int srcG = (srcPx & GREEN_MASK) >>> 8;
      int srcB = (srcPx & BLUE_MASK);
      if (alpha != 255) {
         srcR = mult(srcR, alpha);
         srcG = mult(srcG, alpha);
         srcB = mult(srcB, alpha);
         srcA = mult(srcA, alpha);
      }
      int destA = (destPx) >>> 24;
      int destR = (destPx & RED_MASK) >>> 16;
      int destG = (destPx & GREEN_MASK) >>> 8;
      int destB = (destPx & BLUE_MASK);

      pixels[offset] =
            min(srcA + destA, 0xff) << 24 |
            min(srcR + destR, 0xff) << 16 |
            min(srcG + destG, 0xff) << 8 |
            min(srcB + destB, 0xff);
   }

   public static void blendPixel_Overlay(int [] pixels, int offset, int srcPx, int alpha) {
      int destPx = pixels[offset];
      int srcA = srcPx >>> 24;
      int srcR = (srcPx & RED_MASK) >>> 16;
      int srcG = (srcPx & GREEN_MASK) >>> 8;
      int srcB = (srcPx & BLUE_MASK);
      if (alpha != 255) {
         srcR = mult(srcR, alpha);
         srcG = mult(srcG, alpha);
         srcB = mult(srcB, alpha);
         srcA = mult(srcA, alpha);
      }
      int destA = (destPx) >>> 24;
      int destR = (destPx & RED_MASK) >>> 16;
      int destG = (destPx & GREEN_MASK) >>> 8;
      int destB = (destPx & BLUE_MASK);

      /*
      if a < 128 then
        result := (a*b) SHR 7
      else
        result := 255 - ((255-a) * (255-b) SHR 7);
      */

      pixels[offset] =
            min(255, srcA + destA) << 24 |
            overlay(srcR, destR) << 16 |
            overlay(srcG, destG) << 8 |
            overlay(srcB, destB);
   }

   public static void blendPixel_Screen(int [] pixels, int offset, int srcPx, int alpha) {
      int destPx = pixels[offset];
      int srcA = srcPx >>> 24;
      int srcR = (srcPx & RED_MASK) >>> 16;
      int srcG = (srcPx & GREEN_MASK) >>> 8;
      int srcB = (srcPx & BLUE_MASK);
      if (alpha != 255) {
         srcR = mult(srcR, alpha);
         srcG = mult(srcG, alpha);
         srcB = mult(srcB, alpha);
         srcA = mult(srcA, alpha);
      }
      int destA = (destPx) >>> 24;
      int destR = (destPx & RED_MASK) >>> 16;
      int destG = (destPx & GREEN_MASK) >>> 8;
      int destB = (destPx & BLUE_MASK);

      /*
        result := 255 - ((255-a) * (255-b) SHR 7);
      */

      pixels[offset] =
            min(255, srcA + destA) << 24 |
            screen(srcR, destR) << 16 |
            screen(srcG, destG) << 8 |
            screen(srcB, destB);
   }

   public static void blendPixel_Reflect(int [] pixels, int offset, int srcPx, int alpha) {
      int destPx = pixels[offset];
      int srcA = srcPx >>> 24;
      int srcR = (srcPx & RED_MASK) >>> 16;
      int srcG = (srcPx & GREEN_MASK) >>> 8;
      int srcB = (srcPx & BLUE_MASK);
      if (alpha != 255) {
         srcR = mult(srcR, alpha);
         srcG = mult(srcG, alpha);
         srcB = mult(srcB, alpha);
         srcA = mult(srcA, alpha);
      }
      int destA = (destPx) >>> 24;
      int destR = (destPx & RED_MASK) >>> 16;
      int destG = (destPx & GREEN_MASK) >>> 8;
      int destB = (destPx & BLUE_MASK);

      pixels[offset] =
            min(255, srcA + destA) << 24 |
            reflect(srcR, destR) << 16 |
            reflect(srcG, destG) << 8 |
            reflect(srcB, destB);
   }

   public static void blendPixel_Glow(int [] pixels, int offset, int srcPx, int alpha) {
      int destPx = pixels[offset];
      int srcA = srcPx >>> 24;
      int srcR = (srcPx & RED_MASK) >>> 16;
      int srcG = (srcPx & GREEN_MASK) >>> 8;
      int srcB = (srcPx & BLUE_MASK);
      if (alpha != 255) {
         srcR = mult(srcR, alpha);
         srcG = mult(srcG, alpha);
         srcB = mult(srcB, alpha);
         srcA = mult(srcA, alpha);
      }
      int destA = (destPx) >>> 24;
      int destR = (destPx & RED_MASK) >>> 16;
      int destG = (destPx & GREEN_MASK) >>> 8;
      int destB = (destPx & BLUE_MASK);

      pixels[offset] = // inverse reflect
            min(255, srcA + destA) << 24 |
            reflect(destR, srcR) << 16 |
            reflect(destG, srcG) << 8 |
            reflect(destB, srcB);
   }

   public static void blendPixel_ColorDodge(int [] pixels, int offset, int srcPx, int alpha) {
      int destPx = pixels[offset];
      int srcA = srcPx >>> 24;
      int srcR = (srcPx & RED_MASK) >>> 16;
      int srcG = (srcPx & GREEN_MASK) >>> 8;
      int srcB = (srcPx & BLUE_MASK);
      if (alpha != 255) {
         srcR = mult(srcR, alpha);
         srcG = mult(srcG, alpha);
         srcB = mult(srcB, alpha);
         srcA = mult(srcA, alpha);
      }
      int destA = (destPx) >>> 24;
      int destR = (destPx & RED_MASK) >>> 16;
      int destG = (destPx & GREEN_MASK) >>> 8;
      int destB = (destPx & BLUE_MASK);

      pixels[offset] =
            min(255, srcA + destA) << 24 |
            colordodge(srcR, destR) << 16 |
            colordodge(srcG, destG) << 8 |
            colordodge(srcB, destB);
   }

   public static void blendPixel_ColorBurn(int [] pixels, int offset, int srcPx, int alpha) {
      int destPx = pixels[offset];
      int srcA = srcPx >>> 24;
      int srcR = (srcPx & RED_MASK) >>> 16;
      int srcG = (srcPx & GREEN_MASK) >>> 8;
      int srcB = (srcPx & BLUE_MASK);
      if (alpha != 255) {
         srcR = mult(srcR, alpha);
         srcG = mult(srcG, alpha);
         srcB = mult(srcB, alpha);
         srcA = mult(srcA, alpha);
      }
      int destA = (destPx) >>> 24;
      int destR = (destPx & RED_MASK) >>> 16;
      int destG = (destPx & GREEN_MASK) >>> 8;
      int destB = (destPx & BLUE_MASK);

      pixels[offset] =
            min(255, srcA + destA) << 24 |
            colorburn(srcR, destR) << 16 |
            colorburn(srcG, destG) << 8 |
            colorburn(srcB, destB);
   }

   public static void blendPixel_Darken(int [] pixels, int offset, int srcPx, int alpha) {
      int destPx = pixels[offset];
      int srcA = srcPx >>> 24;
      int srcR = (srcPx & RED_MASK) >>> 16;
      int srcG = (srcPx & GREEN_MASK) >>> 8;
      int srcB = (srcPx & BLUE_MASK);
      if (alpha != 255) {
         srcR = mult(srcR, alpha);
         srcG = mult(srcG, alpha);
         srcB = mult(srcB, alpha);
         srcA = mult(srcA, alpha);
      }
      int destA = (destPx) >>> 24;
      int destR = (destPx & RED_MASK) >>> 16;
      int destG = (destPx & GREEN_MASK) >>> 8;
      int destB = (destPx & BLUE_MASK);

      pixels[offset] =
            min(255, srcA + destA) << 24 |
            min(srcR, destR) << 16 |
            min(srcG, destG) << 8 |
            min(srcB, destB);
   }

   public static void blendPixel_Lighten(int [] pixels, int offset, int srcPx, int alpha) {
      int destPx = pixels[offset];
      int srcA = srcPx >>> 24;
      int srcR = (srcPx & RED_MASK) >>> 16;
      int srcG = (srcPx & GREEN_MASK) >>> 8;
      int srcB = (srcPx & BLUE_MASK);
      if (alpha != 255) {
         srcR = mult(srcR, alpha);
         srcG = mult(srcG, alpha);
         srcB = mult(srcB, alpha);
         srcA = mult(srcA, alpha);
      }
      int destA = (destPx) >>> 24;
      int destR = (destPx & RED_MASK) >>> 16;
      int destG = (destPx & GREEN_MASK) >>> 8;
      int destB = (destPx & BLUE_MASK);

      pixels[offset] =
            min(255, srcA + destA) << 24 |
            max(srcR, destR) << 16 |
            max(srcG, destG) << 8 |
            max(srcB, destB);
   }

   public static void blendPixel_Red(int [] pixels, int offset, int srcPx, int alpha) {
      int destPx = pixels[offset];
      int srcA = srcPx >>> 24;
      int srcR = (srcPx & RED_MASK) >>> 16;
      if (alpha != 255) {
         srcR = mult(srcR, alpha);
         srcA = mult(srcA, alpha);
      }
      int destA = (destPx) >>> 24;
      int destG = (destPx & GREEN_MASK) >>> 8;
      int destB = (destPx & BLUE_MASK);

      pixels[offset] =
            min(255, srcA + destA) << 24 |
            srcR << 16 |
            destG << 8 |
            destB;
   }

   public static void blendPixel_Green(int [] pixels, int offset, int srcPx, int alpha) {
      int destPx = pixels[offset];
      int srcA = srcPx >>> 24;
      int srcG = (srcPx & GREEN_MASK) >>> 8;
      if (alpha != 255) {
         srcG = mult(srcG, alpha);
         srcA = mult(srcA, alpha);
      }
      int destA = (destPx) >>> 24;
      int destR = (destPx & RED_MASK) >>> 16;
      int destB = (destPx & BLUE_MASK);

      pixels[offset] =
            min(255, srcA + destA) << 24 |
            destR << 16 |
            srcG << 8 |
            destB;
   }

   public static void blendPixel_Blue(int [] pixels, int offset, int srcPx, int alpha) {
      int destPx = pixels[offset];
      int srcA = srcPx >>> 24;
      int srcB = (srcPx & BLUE_MASK);
      if (alpha != 255) {
         srcB = mult(srcB, alpha);
         srcA = mult(srcA, alpha);
      }
      int destA = (destPx) >>> 24;
      int destR = (destPx & RED_MASK) >>> 16;
      int destG = (destPx & GREEN_MASK) >>> 8;

      pixels[offset] =
            min(255, srcA + destA) << 24 |
            destR << 16 |
            destG << 8 |
            srcB;
   }

   public static void blendPixel_HardLight(int [] pixels, int offset, int srcPx, int alpha) {
      int destPx = pixels[offset];
      int srcA = srcPx >>> 24;
      int srcR = (srcPx & RED_MASK) >>> 16;
      int srcG = (srcPx & GREEN_MASK) >>> 8;
      int srcB = (srcPx & BLUE_MASK);
      if (alpha != 255) {
         srcR = mult(srcR, alpha);
         srcG = mult(srcG, alpha);
         srcB = mult(srcB, alpha);
         srcA = mult(srcA, alpha);
      }
      int destA = (destPx) >>> 24;
      int destR = (destPx & RED_MASK) >>> 16;
      int destG = (destPx & GREEN_MASK) >>> 8;
      int destB = (destPx & BLUE_MASK);

      pixels[offset] =
            min(0xff, srcA + destA) << 24 |
            hardlight(srcR, destR) << 16 |
            hardlight(srcG, destG) << 8 |
            hardlight(srcB, destB);
   }

   public static void blendPixel_Difference(int [] pixels, int offset, int srcPx, int alpha) {
      int destPx = pixels[offset];
      int srcA = srcPx >>> 24;
      int srcR = (srcPx & RED_MASK) >>> 16;
      int srcG = (srcPx & GREEN_MASK) >>> 8;
      int srcB = (srcPx & BLUE_MASK);
      if (alpha != 255) {
         srcR = mult(srcR, alpha);
         srcG = mult(srcG, alpha);
         srcB = mult(srcB, alpha);
         srcA = mult(srcA, alpha);
      }
      int destA = (destPx) >>> 24;
      int destR = (destPx & RED_MASK) >>> 16;
      int destG = (destPx & GREEN_MASK) >>> 8;
      int destB = (destPx & BLUE_MASK);

      pixels[offset] =
            blend(srcA, destA, srcA) << 24 |
               (srcR + destR - (2 * min(mult(srcR, destA), mult(destR, srcA)))) << 16 |
               (srcG + destG - (2 * min(mult(srcG, destA), mult(destG, srcA)))) << 8 |
               (srcB + destB - (2 * min(mult(srcB, destA), mult(destB, srcA))));
   }

   public static int hardlight(int a, int b) {
      return a < 128 ? (a * b) >> 7 : 0xff - ((0xff-a) * (0xff-b) >> 7);
   }

   public static int reflect(int a, int b) {
      return b == 255 ? 255 : min(255, a * a / (255 - b));
   }
   
   public static int colorburn(int a, int b) {
      return b == 0 ? 0 : max(0, (0xff - ((0xff-a) << 8)/ b));
   }

   public static int colordodge(int a, int b) {
      return b == 0xff ? 0xff : min(0xff, ((a<<8) / (0xff-b)));
   }

   public static int overlay(int a, int b) {
      if (a < 128) {
         return (a * b) >> 7;
      } else {
         return 0xff - (((0xff-a) * (0xff-b)) >> 7);
      }
   }

   public static int screen(int a, int b) {
      return 0xff - mult(a, b);
   }

   public static int min(int a, int b) {
      return a < b ? a:b;
   }

   public static int max(int a, int b) {
      return a > b ? a:b;
   }

   public static int blendOneMinus(int src, int dest, int oneMinus) {
      return src + mult(dest, oneMinus);
   }

   public static int blend(int src, int dest, int alpha) {
      return src + mult(dest, 0xFF - alpha);
   }

   public static int mult2(int val, int multiplier) {
      return (val * (multiplier + 1)) >> 8;
   }

   public static int mult(int a, int b) {
      final int t = a * b + 128;
      return (t >> 8) + t >> 8;
   }

   public static int premultiplyEx(int rgb) {
      return premultiply(rgb, rgb>>>24);
   }

   public static int premultiply(int rgbColor, int alpha) {

      if (alpha <= 0) {
         return 0;
      } else if (alpha >= 255) {
         return 0xff000000 | rgbColor;
      } else {
         int r = (rgbColor >> 16) & 0xff;
         int g = (rgbColor >> 8) & 0xff;
         int b = rgbColor & 0xff;

         r = (alpha * r + 127) / 255;
         g = (alpha * g + 127) / 255;
         b = (alpha * b + 127) / 255;
         return (alpha << 24) | (r << 16) | (g << 8) | b;
      }
   }

   public static int unpremultiply(int preARGBColor) {
      int a = preARGBColor >>> 24;

      if (a == 0) {
         return 0;
      } else if (a == 255) {
         return preARGBColor;
      } else {
         int r = (preARGBColor >> 16) & 0xff;
         int g = (preARGBColor >> 8) & 0xff;
         int b = preARGBColor & 0xff;

         r = 255 * r / a;
         g = 255 * g / a;
         b = 255 * b / a;
         return (a << 24) | (r << 16) | (g << 8) | b;
      }
   }
}


mult and blend functions taken from praxis (http://code.google.com/p/praxis/source/browse/ripl/src/net/neilcsmith/ripl/rgbmath/RGBMath.java)

reference: http://www.pegtop.net/delphi/articles/blendmodes/intro.htm

example:

1  
2  
3  
4  
5  
int [] destination = .. // your destination array
int source = 0x80ff0000; // red with 50% opacity, can be any color
int offset = 0x20; // offset in destination array to blend
int extraAlpha = 100; // extra alpha to blend with
blendPixelPremultiplied(destination, offset, source, extraAlpha, Mode.Multiply);


the above code blends the pixel in offset 0x20 with the color red, with 50% opacity, adding 100 extra alpha while blending, using the multiply blendmode.
Offline nsigma
« Reply #1 - Posted 2011-07-23 11:05:47 »

Hi,

Cool!  For the avoidance of any doubt when I'm next working on Praxis I'll add an all-permissive header rather than GPL to the RGBMath class, and the RGBComposite as well (not sure if you've taken anything from there too?)  Guess by posting here you're happy for your code to be used in any way too.  Be good to get some optimal version of all these blend modes in one place - notice you've got some additional optimisations to me in various places, and also some additional modes.  I think you're missing Sub, Difference and BitXor from my RGBComposite class?  Both Sub and Difference I use a lot - BitXor is a bit of a psychedelic experiment!  Somewhere, I've also got code for an equivalent of Ghost from Director (not sure if that has another name elsewhere).

I think the premultiply() code is going to suffer from the same off by 1 error we discussed in previous thread - I guess that multRGB() should use mult().  Because my pipeline is premultiplied I'm only using that code in one specific place and hadn't noticed the issue.

Is there a reason you're passing in source pixels as single values rather than as an array?  I assume you're not very often working with only a single pixel.

Best wishes, Neil

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Offline Dx4

Junior Devvie


Medals: 5



« Reply #2 - Posted 2011-07-23 11:29:24 »

Hi,

Cool!  For the avoidance of any doubt when I'm next working on Praxis I'll add an all-permissive header rather than GPL to the RGBMath class, and the RGBComposite as well (not sure if you've taken anything from there too?)  Guess by posting here you're happy for your code to be used in any way too.  Be good to get some optimal version of all these blend modes in one place - notice you've got some additional optimisations to me in various places, and also some additional modes.  I think you're missing Sub, Difference and BitXor from my RGBComposite class?  Both Sub and Difference I use a lot - BitXor is a bit of a psychedelic experiment!  Somewhere, I've also got code for an equivalent of Ghost from Director (not sure if that has another name elsewhere).

I think the premultiply() code is going to suffer from the same off by 1 error we discussed in previous thread - I guess that multRGB() should use mult().  Because my pipeline is premultiplied I'm only using that code in one specific place and hadn't noticed the issue.

Is there a reason you're passing in source pixels as single values rather than as an array?  I assume you're not very often working with only a single pixel.

Best wishes, Neil

thanks.

by posting the code here, I'm just saying i'm happy for anyone to do anything they want with the code (as long as your RGBMath class is licensed that way too), just hoping that this might help some people out should they ever need it.

I took some code out of RGBComposite too, but rewrote it to use a single if statement, added a function called blendOneMinus (which doesnt require a sub instruction)

I'll add your Sub, Difference and BitXor to the class if you don't mind.

I pass in source pixels as single values because it seems easier that way, I don't have to write a massive loop around every single function which reads each source pixel, etc.

and if you need to use a source array, it should be easy to add that anyhow. also there are no virtual calls in this class, which removes the overhead of a vtable (hotspot should inline the calls).

 - David
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline nsigma
« Reply #3 - Posted 2011-07-23 11:49:02 »

I pass in source pixels as single values because it seems easier that way, I don't have to write a massive loop around every single function which reads each source pixel, etc.

and if you need to use a source array, it should be easy to add that anyhow. also there are no virtual calls in this class, which removes the overhead of a vtable (hotspot should inline the calls).

OK. Makes sense. I'm just used to pushing the loop into the composite method because I needed RGBComposite to be an abstract class. I'll port some of your optimisations and other blend modes back to mine, so if people want to work with arrays then that's already there - I'm used to passing in scanlines or whole regions at a time.

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Offline Dx4

Junior Devvie


Medals: 5



« Reply #4 - Posted 2011-07-23 12:03:35 »

I pass in source pixels as single values because it seems easier that way, I don't have to write a massive loop around every single function which reads each source pixel, etc.

and if you need to use a source array, it should be easy to add that anyhow. also there are no virtual calls in this class, which removes the overhead of a vtable (hotspot should inline the calls).

OK. Makes sense. I'm just used to pushing the loop into the composite method because I needed RGBComposite to be an abstract class. I'll port some of your optimisations and other blend modes back to mine, so if people want to work with arrays then that's already there - I'm used to passing in scanlines or whole regions at a time.

go ahead. I just wanted it to modify one pixel at a time because that seemed the most flexible to me. For example, in my project I have to construct a source pixel when blending (for example when mixing brush alpha with brush color: int source = alpha<<24|(color&0x00ffffff) ). I don't want to have to make an array each time I just want to modify a single pixel in a specific location.
Offline Dx4

Junior Devvie


Medals: 5



« Reply #5 - Posted 2011-07-23 13:43:44 »

this seems to be a more accurate alternative to your mult():

1  
2  
3  
4  
   public static int mult(int a, int b) {
      int t = a * b + 128;
      return (t >> 8) + t >> 8;
   }
Offline nsigma
« Reply #6 - Posted 2011-07-23 17:02:58 »

this seems to be a more accurate alternative to your mult():

1  
2  
3  
4  
   public static int mult(int a, int b) {
      int t = a * b + 128;
      return (t >> 8) + t >> 8;
   }


Interesting.  Is that yours or from a particular source?

Tried a few values at random and does seem to be slightly more accurate, though only 1 away on all that I tried - have you noticed any greater difference?

Personally, it's unlikely I'll use that fix as it's adding 2 extra operations every time it's called - which is going to add up considering this method may be called 8 or more times per pixel.  I'm not even sure about adding in the +1 fix we had in the previous thread.  But then our use cases are slightly different - I'm working with video so am wanting to process many full frames per second, whereas I can understand accuracy being a greater concern in your application.

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Offline Dx4

Junior Devvie


Medals: 5



« Reply #7 - Posted 2011-07-23 17:30:39 »

this seems to be a more accurate alternative to your mult():

1  
2  
3  
4  
   public static int mult(int a, int b) {
      int t = a * b + 128;
      return (t >> 8) + t >> 8;
   }


Interesting.  Is that yours or from a particular source?

Tried a few values at random and does seem to be slightly more accurate, though only 1 away on all that I tried - have you noticed any greater difference?

Personally, it's unlikely I'll use that fix as it's adding 2 extra operations every time it's called - which is going to add up considering this method may be called 8 or more times per pixel.  I'm not even sure about adding in the +1 fix we had in the previous thread.  But then our use cases are slightly different - I'm working with video so am wanting to process many full frames per second, whereas I can understand accuracy being a greater concern in your application.

yea I was experimenting and happened to come up with that. I noticed an extremely different result - sometimes I used the color 0xffffaa00 with an opacity of 50% and after stroking a couple times, it became red (0xffff0000) which was obviously a major bug for a drawing application.

After switching to this method for mult, it stayed dark yellow instead of changing to red after a certain number of strokes.

 - David
Offline nsigma
« Reply #8 - Posted 2011-07-24 11:23:32 »

yea I was experimenting and happened to come up with that. I noticed an extremely different result - sometimes I used the color 0xffffaa00 with an opacity of 50% and after stroking a couple times, it became red (0xffff0000) which was obviously a major bug for a drawing application.

After switching to this method for mult, it stayed dark yellow instead of changing to red after a certain number of strokes.

 - David

I just had a quick test in Praxis as this would be a fairly serious bug for me too, but I can't reproduce this effect using my original code.  I assume you're using Normal / SrcOver blend mode for this?  I only see this effect (just red) when the opacity goes below 2%, when rounding errors really start to kick in - no way around that while using an integer pipeline.  Could it be errors in the premultiply() -> unpremultiply() causing it to round down?

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Offline Dx4

Junior Devvie


Medals: 5



« Reply #9 - Posted 2011-07-24 12:08:32 »

yea I was experimenting and happened to come up with that. I noticed an extremely different result - sometimes I used the color 0xffffaa00 with an opacity of 50% and after stroking a couple times, it became red (0xffff0000) which was obviously a major bug for a drawing application.

After switching to this method for mult, it stayed dark yellow instead of changing to red after a certain number of strokes.

 - David

I just had a quick test in Praxis as this would be a fairly serious bug for me too, but I can't reproduce this effect using my original code.  I assume you're using Normal / SrcOver blend mode for this?  I only see this effect (just red) when the opacity goes below 2%, when rounding errors really start to kick in - no way around that while using an integer pipeline.  Could it be errors in the premultiply() -> unpremultiply() causing it to round down?

yeah I am using SrcOver. it also happens when I use premultiplied colors (eg: not calling the premultiply and unpremultiply functions)

switching to the mult function I posted above fixed it.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline nsigma
« Reply #10 - Posted 2011-07-24 15:46:53 »

yeah I am using SrcOver. it also happens when I use premultiplied colors (eg: not calling the premultiply and unpremultiply functions)

switching to the mult function I posted above fixed it.

Well, the obvious thing is stick with what works for you!   Grin

I am interested, though, in why we're getting different results.  I wonder what we're doing differently Huh  I'll be doing some work on Praxis in a couple of days, so might try plugging in your altered blend function and see if that causes it, but I can't see at the moment why any of your optimisation changes would alter the behaviour.  That altered mult() method, while more accurate, is also going to slow things down, and negate the optimisations you've made elsewhere - if something else is the root cause, it seems a shame to change it!

I wondered if it was the premultiply<>unpremultiply because those functions seem to show a slight tendency to drift, but if anything it seems to be upwards not downwards. EDIT - scratch that bit, that's me doing it on a calculator and rounding things up when converting to int.  Roll Eyes

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Offline Dx4

Junior Devvie


Medals: 5



« Reply #11 - Posted 2011-07-25 03:11:25 »

I am interested, though, in why we're getting different results.  I wonder what we're doing differently Huh  I'll be doing some work on Praxis in a couple of days, so might try plugging in your altered blend function and see if that causes it, but I can't see at the moment why any of your optimisation changes would alter the behaviour.  That altered mult() method, while more accurate, is also going to slow things down, and negate the optimisations you've made elsewhere - if something else is the root cause, it seems a shame to change it!

yeah, I can only try to optimize further now, but in my application accuracy is more important than speed - in yours probably not so much as you need to render frames at a fast rate.
If I optimize anything further I'll post it here Smiley

- David
Offline nsigma
« Reply #12 - Posted 2011-07-25 10:32:17 »

Right, license headers changed to All-Permissive in RGBMath and RGBComposite

http://code.google.com/p/praxis/source/browse/ripl/src/net/neilcsmith/ripl/rgbmath/RGBComposite.java
http://code.google.com/p/praxis/source/browse/ripl/src/net/neilcsmith/ripl/rgbmath/RGBMath.java

I haven't look at adding in any of your changes or additional blend modes yet.  Did try various versions of the code you posted with continuously overlaying yellow but can definitely not replicate the drift to red - must be something we're doing differently outside of this code.

Incidentally, as I was posting it in another topic, have you seen the various blend modes from http://filthyrichclients.org/ -> Examples -> Chapter 6.  They're for using with Java2D and aren't as optimal as the code we've got between us, but there might be some good pointers to some missing blend modes in there.

Best wishes,  Neil

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Offline Dx4

Junior Devvie


Medals: 5



« Reply #13 - Posted 2011-07-25 12:00:47 »

Right, license headers changed to All-Permissive in RGBMath and RGBComposite

http://code.google.com/p/praxis/source/browse/ripl/src/net/neilcsmith/ripl/rgbmath/RGBComposite.java
http://code.google.com/p/praxis/source/browse/ripl/src/net/neilcsmith/ripl/rgbmath/RGBMath.java

I haven't look at adding in any of your changes or additional blend modes yet.  Did try various versions of the code you posted with continuously overlaying yellow but can definitely not replicate the drift to red - must be something we're doing differently outside of this code.

Incidentally, as I was posting it in another topic, have you seen the various blend modes from http://filthyrichclients.org/ -> Examples -> Chapter 6.  They're for using with Java2D and aren't as optimal as the code we've got between us, but there might be some good pointers to some missing blend modes in there.

Best wishes,  Neil

thanks. I noticed the drift to red only happened when I used darkish yellow colors, eg 0xffffaa00, it didnt happen when I used brighter yellows.

Took a look at their blendcomposite class and it seems like their blender would be orders of magnitudes slower than the one we have between us, though I agree it would help us write more blend modes. will write more blend modes when I get some time Smiley

- David
Offline nsigma
« Reply #14 - Posted 2011-07-25 15:52:06 »

thanks. I noticed the drift to red only happened when I used darkish yellow colors, eg 0xffffaa00, it didnt happen when I used brighter yellows.
That's the exact yellow I used as you mentioned it before.  Opacities below 2% show errors because they never become yellow at all, but above that constant overlaying converges towards (approximately) the correct colour.

Took a look at their blendcomposite class and it seems like their blender would be orders of magnitudes slower than the one we have between us, though I agree it would help us write more blend modes. will write more blend modes when I get some time Smiley

Yes, it is!  Grin

Watch out with that as well that they're factoring in the alpha amount separately to the blend function, whereas all ours happen in place, so the equations won't necessarily be correct either!

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (29 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (40 views)
2014-12-09 22:41:13

BurntPizza (75 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (50 views)
2014-12-03 16:27:13

CopyableCougar4 (47 views)
2014-11-29 21:32:03

toopeicgaming1999 (113 views)
2014-11-26 15:22:04

toopeicgaming1999 (100 views)
2014-11-26 15:20:36

toopeicgaming1999 (30 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!