001: /*
002: *
003: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
004: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License version
008: * 2 only, as published by the Free Software Foundation.
009: *
010: * This program is distributed in the hope that it will be useful, but
011: * WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * General Public License version 2 for more details (a copy is
014: * included at /legal/license.txt).
015: *
016: * You should have received a copy of the GNU General Public License
017: * version 2 along with this work; if not, write to the Free Software
018: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
019: * 02110-1301 USA
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
022: * Clara, CA 95054 or visit www.sun.com if you need additional
023: * information or have any questions.
024: */
025:
026: package com.sun.pisces;
027:
028: public abstract class AbstractSurface implements Surface,
029: NativeFinalization {
030: static {
031: PiscesLibrary.load();
032: }
033:
034: private final class AbstractSurfaceDestination implements
035: SurfaceDestination {
036: public void drawSurface(Surface ps, int srcX, int srcY,
037: int dstX, int dstY, int width, int height, float opacity) {
038: if (ps instanceof NativeSurface) {
039: drawSurfaceImpl((NativeSurface) ps, srcX, srcY, dstX,
040: dstY, width, height, opacity);
041: return;
042: }
043:
044: int srcW = ps.getWidth();
045: int srcH = ps.getHeight();
046: int dstW = getWidth();
047: int dstH = getHeight();
048:
049: if (srcX < 0) {
050: dstX -= srcX;
051: width += srcX;
052: srcX = 0;
053: }
054:
055: if (srcY < 0) {
056: dstY -= srcY;
057: height += srcY;
058: srcY = 0;
059: }
060:
061: if (dstX < 0) {
062: srcX -= dstX;
063: width += dstX;
064: dstX = 0;
065: }
066:
067: if (dstY < 0) {
068: srcY -= dstY;
069: height += dstY;
070: dstY = 0;
071: }
072:
073: if ((srcX + width) > srcW) {
074: width = srcW - srcX;
075: }
076:
077: if ((srcY + height) > srcH) {
078: height = srcH - srcY;
079: }
080:
081: if ((dstX + width) > dstW) {
082: width = dstW - dstX;
083: }
084:
085: if ((dstY + height) > dstH) {
086: height = dstH - dstY;
087: }
088:
089: if ((width > 0) && (height > 0) && (opacity > 0)) {
090: int size = width * height;
091: int[] srcRGB = new int[size];
092:
093: ps.getRGB(srcRGB, 0, width, srcX, srcY, width, height);
094: drawRGBImpl(srcRGB, 0, width, dstX, dstY, width,
095: height, opacity);
096: }
097: }
098:
099: public void drawRGB(int[] argb, int offset, int scanLength,
100: int x, int y, int width, int height, float opacity) {
101: drawRGBImpl(argb, offset, scanLength, x, y, width, height,
102: opacity);
103: }
104: }
105:
106: protected long nativePtr = 0L;
107: private final NativeFinalizer finalizer;
108:
109: protected AbstractSurface() {
110: this .finalizer = NativeFinalizer.createInstance(this );
111: }
112:
113: public SurfaceDestination createSurfaceDestination() {
114: return new AbstractSurfaceDestination();
115: }
116:
117: public native int getWidth();
118:
119: public native int getHeight();
120:
121: public native void getRGB(int[] argb, int offset, int scanLength,
122: int x, int y, int width, int height);
123:
124: public native void setRGB(int[] argb, int offset, int scanLength,
125: int x, int y, int width, int height);
126:
127: private native void drawSurfaceImpl(NativeSurface ps, int srcX,
128: int srcY, int dstX, int dstY, int width, int height,
129: float opacity);
130:
131: private native void drawRGBImpl(int[] argb, int offset,
132: int scanLength, int x, int y, int width, int height,
133: float opacity);
134:
135: public native void nativeFinalize();
136: }
|