01: /*
02: Copyright 2006 Jerry Huxtable
03:
04: Licensed under the Apache License, Version 2.0 (the "License");
05: you may not use this file except in compliance with the License.
06: You may obtain a copy of the License at
07:
08: http://www.apache.org/licenses/LICENSE-2.0
09:
10: Unless required by applicable law or agreed to in writing, software
11: distributed under the License is distributed on an "AS IS" BASIS,
12: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: See the License for the specific language governing permissions and
14: limitations under the License.
15: */
16:
17: package com.jhlabs.composite;
18:
19: import java.awt.*;
20: import java.awt.image.*;
21:
22: public final class AddComposite extends RGBComposite {
23:
24: public AddComposite(float alpha) {
25: super (alpha);
26: }
27:
28: public CompositeContext createContext(ColorModel srcColorModel,
29: ColorModel dstColorModel, RenderingHints hints) {
30: return new Context(extraAlpha, srcColorModel, dstColorModel);
31: }
32:
33: static class Context extends RGBCompositeContext {
34: public Context(float alpha, ColorModel srcColorModel,
35: ColorModel dstColorModel) {
36: super (alpha, srcColorModel, dstColorModel);
37: }
38:
39: public void composeRGB(int[] src, int[] dst, float alpha) {
40: int w = src.length;
41:
42: for (int i = 0; i < w; i += 4) {
43: int sr = src[i];
44: int dir = dst[i];
45: int sg = src[i + 1];
46: int dig = dst[i + 1];
47: int sb = src[i + 2];
48: int dib = dst[i + 2];
49: int sa = src[i + 3];
50: int dia = dst[i + 3];
51: int dor, dog, dob;
52:
53: dor = dir + sr;
54: if (dor > 255)
55: dor = 255;
56: dog = dig + sg;
57: if (dog > 255)
58: dog = 255;
59: dob = dib + sb;
60: if (dob > 255)
61: dob = 255;
62:
63: float a = alpha * sa / 255f;
64: float ac = 1 - a;
65:
66: dst[i] = (int) (a * dor + ac * dir);
67: dst[i + 1] = (int) (a * dog + ac * dig);
68: dst[i + 2] = (int) (a * dob + ac * dib);
69: dst[i + 3] = (int) (sa * alpha + dia * ac);
70: }
71: }
72: }
73:
74: }
|