001: /*
002: Copyright 2006 Jerry Huxtable
003:
004: Licensed under the Apache License, Version 2.0 (the "License");
005: you may not use this file except in compliance with the License.
006: You may obtain a copy of the License at
007:
008: http://www.apache.org/licenses/LICENSE-2.0
009:
010: Unless required by applicable law or agreed to in writing, software
011: distributed under the License is distributed on an "AS IS" BASIS,
012: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: See the License for the specific language governing permissions and
014: limitations under the License.
015: */
016:
017: package com.jhlabs.composite;
018:
019: import java.awt.*;
020: import java.awt.image.*;
021:
022: public abstract class RGBComposite implements Composite {
023:
024: protected float extraAlpha;
025:
026: public RGBComposite() {
027: this (1.0f);
028: }
029:
030: public RGBComposite(float alpha) {
031: if (alpha < 0.0f || alpha > 1.0f)
032: throw new IllegalArgumentException(
033: "RGBComposite: alpha must be between 0 and 1");
034: this .extraAlpha = alpha;
035: }
036:
037: public float getAlpha() {
038: return extraAlpha;
039: }
040:
041: public int hashCode() {
042: return Float.floatToIntBits(extraAlpha);
043: }
044:
045: public boolean equals(Object o) {
046: if (!(o instanceof RGBComposite))
047: return false;
048: RGBComposite c = (RGBComposite) o;
049:
050: if (extraAlpha != c.extraAlpha)
051: return false;
052: return true;
053: }
054:
055: public abstract static class RGBCompositeContext implements
056: CompositeContext {
057:
058: private float alpha;
059: private ColorModel srcColorModel;
060: private ColorModel dstColorModel;
061:
062: public RGBCompositeContext(float alpha,
063: ColorModel srcColorModel, ColorModel dstColorModel) {
064: this .alpha = alpha;
065: this .srcColorModel = srcColorModel;
066: this .dstColorModel = dstColorModel;
067: }
068:
069: public void dispose() {
070: }
071:
072: // Multiply two numbers in the range 0..255 such that 255*255=255
073: static int multiply255(int a, int b) {
074: int t = a * b + 0x80;
075: return ((t >> 8) + t) >> 8;
076: }
077:
078: static int clamp(int a) {
079: return a < 0 ? 0 : a > 255 ? 255 : a;
080: }
081:
082: public abstract void composeRGB(int[] src, int[] dst,
083: float alpha);
084:
085: public void compose(Raster src, Raster dstIn,
086: WritableRaster dstOut) {
087: float alpha = this .alpha;
088:
089: int[] srcPix = null;
090: int[] dstPix = null;
091:
092: int x = dstOut.getMinX();
093: int w = dstOut.getWidth();
094: int y0 = dstOut.getMinY();
095: int y1 = y0 + dstOut.getHeight();
096:
097: for (int y = y0; y < y1; y++) {
098: srcPix = src.getPixels(x, y, w, 1, srcPix);
099: dstPix = dstIn.getPixels(x, y, w, 1, dstPix);
100: composeRGB(srcPix, dstPix, alpha);
101: dstOut.setPixels(x, y, w, 1, dstPix);
102: }
103: }
104:
105: }
106: }
|