001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Rustem V. Rafikov
019: * @version $Revision: 1.3 $
020: */package javax.imageio;
021:
022: import java.awt.*;
023:
024: public abstract class IIOParam {
025: protected Rectangle sourceRegion;
026: protected int sourceXSubsampling = 1;
027: protected int sourceYSubsampling = 1;
028: protected int subsamplingXOffset;
029: protected int subsamplingYOffset;
030: protected int[] sourceBands;
031: protected ImageTypeSpecifier destinationType;
032: protected Point destinationOffset = new Point(0, 0);
033: protected IIOParamController defaultController;
034: protected IIOParamController controller;
035:
036: protected IIOParam() {
037: }
038:
039: public void setSourceRegion(Rectangle sourceRegion) {
040: if (sourceRegion != null) {
041: if (sourceRegion.x < 0) {
042: throw new IllegalArgumentException("x < 0");
043: }
044: if (sourceRegion.y < 0) {
045: throw new IllegalArgumentException("y < 0");
046: }
047: if (sourceRegion.width <= 0) {
048: throw new IllegalArgumentException("width <= 0");
049: }
050: if (sourceRegion.height <= 0) {
051: throw new IllegalArgumentException("height <= 0");
052: }
053:
054: if (sourceRegion.width <= subsamplingXOffset) {
055: throw new IllegalArgumentException(
056: "width <= subsamplingXOffset");
057: }
058:
059: if (sourceRegion.height <= subsamplingYOffset) {
060: throw new IllegalArgumentException(
061: "height <= subsamplingXOffset");
062: }
063: //-- clone it to avoid unexpected modifications
064: this .sourceRegion = (Rectangle) sourceRegion.clone();
065: } else {
066: this .sourceRegion = null;
067: }
068: }
069:
070: public Rectangle getSourceRegion() {
071: if (sourceRegion == null) {
072: return null;
073: }
074: //-- clone it to avoid unexpected modifications
075: return (Rectangle) sourceRegion.clone();
076: }
077:
078: public void setSourceSubsampling(int sourceXSubsampling,
079: int sourceYSubsampling, int subsamplingXOffset,
080: int subsamplingYOffset) {
081:
082: if (sourceXSubsampling <= 0) {
083: throw new IllegalArgumentException(
084: "sourceXSubsampling <= 0");
085: }
086: if (sourceYSubsampling <= 0) {
087: throw new IllegalArgumentException(
088: "sourceYSubsampling <= 0");
089: }
090:
091: if ((subsamplingXOffset < 0)
092: || (subsamplingXOffset >= sourceXSubsampling)) {
093: throw new IllegalArgumentException(
094: "subsamplingXOffset is wrong");
095: }
096:
097: if ((subsamplingYOffset < 0)
098: || (subsamplingYOffset >= sourceYSubsampling)) {
099: throw new IllegalArgumentException(
100: "subsamplingYOffset is wrong");
101: }
102:
103: //-- does region contain pixels
104: if (sourceRegion != null) {
105: if (sourceRegion.width <= subsamplingXOffset
106: || sourceRegion.height <= subsamplingYOffset) {
107: throw new IllegalArgumentException(
108: "there are no pixels in region");
109: }
110: }
111:
112: this .sourceXSubsampling = sourceXSubsampling;
113: this .sourceYSubsampling = sourceYSubsampling;
114: this .subsamplingXOffset = subsamplingXOffset;
115: this .subsamplingYOffset = subsamplingYOffset;
116: }
117:
118: public int getSourceXSubsampling() {
119: return sourceXSubsampling;
120: }
121:
122: public int getSourceYSubsampling() {
123: return sourceYSubsampling;
124: }
125:
126: public int getSubsamplingXOffset() {
127: return subsamplingXOffset;
128: }
129:
130: public int getSubsamplingYOffset() {
131: return subsamplingYOffset;
132: }
133:
134: public void setSourceBands(final int[] sourceBands) {
135: if (sourceBands == null) {
136: this .sourceBands = null;
137: } else {
138: for (int i = 0; i < sourceBands.length; i++) {
139: if (sourceBands[i] < 0) {
140: throw new IllegalArgumentException("negative value");
141: }
142:
143: for (int j = i + 1; j < sourceBands.length; j++) {
144: if (sourceBands[i] == sourceBands[j]) {
145: throw new IllegalArgumentException(
146: "duplicate value");
147: }
148: }
149: }
150:
151: this .sourceBands = sourceBands.clone();
152: }
153: }
154:
155: public int[] getSourceBands() {
156: return (sourceBands != null) ? sourceBands.clone() : null;
157: }
158:
159: public void setDestinationType(
160: final ImageTypeSpecifier destinationType) {
161: this .destinationType = destinationType;
162: }
163:
164: public ImageTypeSpecifier getDestinationType() {
165: return destinationType;
166: }
167:
168: public void setDestinationOffset(Point destinationOffset) {
169: if (destinationOffset == null) {
170: throw new IllegalArgumentException(
171: "destinationOffset == null!");
172: }
173:
174: this .destinationOffset = (Point) destinationOffset.clone();
175: }
176:
177: public Point getDestinationOffset() {
178: return (Point) destinationOffset.clone();
179: }
180:
181: public void setController(final IIOParamController controller) {
182: this .controller = controller;
183: }
184:
185: public IIOParamController getController() {
186: return controller;
187: }
188:
189: public IIOParamController getDefaultController() {
190: return defaultController;
191: }
192:
193: public boolean hasController() {
194: return (controller != null);
195: }
196:
197: public boolean activateController() {
198: final IIOParamController controller = getController();
199:
200: if (controller == null) {
201: throw new IllegalStateException("controller wasn't set");
202: }
203:
204: return controller.activate(this);
205: }
206: }
|