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: /* $Id: ImageUtil.java 426576 2006-07-28 15:44:37Z jeremias $ */
019:
020: /*
021: * This file is part of the RTF library of the FOP project, which was originally
022: * created by Bertrand Delacretaz <bdelacretaz@codeconsult.ch> and by other
023: * contributors to the jfor project (www.jfor.org), who agreed to donate jfor to
024: * the FOP project.
025: */
026:
027: package org.apache.fop.render.rtf.rtflib.tools;
028:
029: /** Misc.utilities for images handling
030: * This class belongs to the <fo:external-graphic> tag processing.
031: * @author <a href="mailto:a.putz@skynamics.com">Andreas Putz</a>
032: */
033: public class ImageUtil {
034:
035: //////////////////////////////////////////////////
036: // @@ Construction
037: //////////////////////////////////////////////////
038:
039: /**
040: * Private constructor.
041: */
042: private ImageUtil() {
043: }
044:
045: //////////////////////////////////////////////////
046: // @@ Public static methods
047: //////////////////////////////////////////////////
048:
049: /**
050: * Determines the digits from a string.
051: *
052: * @param value String with digits
053: *
054: * @return -1 There is no digit
055: * number The digits as integer
056: */
057: public static int getInt(String value) {
058: String retString = new String();
059: StringBuffer s = new StringBuffer(value);
060: int len = s.length();
061:
062: for (int i = 0; i < len; i++) {
063: if (Character.isDigit(s.charAt(i))) {
064: retString += s.charAt(i);
065: } else {
066: //for example "600.0pt" has to be exited,
067: //when the dot is reached.
068: break;
069: }
070: }
071:
072: if (retString.length() == 0) {
073: return -1;
074: } else {
075: return Integer.parseInt(retString);
076: }
077: }
078:
079: /**
080: * Checks the string for percent character at the end of string.
081: *
082: * @param value String with digits
083: *
084: * @return true The string contains a % value
085: * false Other string
086: */
087: public static boolean isPercent(String value) {
088: if (value.endsWith("%")) {
089: return true;
090:
091: }
092:
093: return false;
094: }
095:
096: /**
097: * Compares two hexadecimal values.
098: *
099: * @param pattern Target
100: * @param data Data
101: * @param searchAt Position to start compare
102: * @param searchForward Direction to compare byte arrays
103: *
104: * @return true If equal
105: * false If different
106: */
107: public static boolean compareHexValues(byte[] pattern, byte[] data,
108: int searchAt, boolean searchForward) {
109: if (searchAt >= data.length) {
110: return false;
111:
112: }
113:
114: int pLen = pattern.length;
115:
116: if (searchForward) {
117: if (pLen >= (data.length - searchAt)) {
118: return false;
119:
120: }
121:
122: for (int i = 0; i < pLen; i++) {
123: if (pattern[i] != data[searchAt + i]) {
124: return false;
125: }
126: }
127:
128: return true;
129: } else {
130: if (pLen > (searchAt + 1)) {
131: return false;
132:
133: }
134:
135: for (int i = 0; i < pLen; i++) {
136: if (pattern[pLen - i - 1] != data[searchAt - i]) {
137: return false;
138: }
139: }
140:
141: return true;
142: }
143: }
144:
145: /**
146: * Determines a integer value from a hexadecimal byte array.
147: *
148: * @param data Image
149: * @param startAt Start index to read from
150: * @param length Number of data elements to read
151: * @param searchForward True if searching forward, False if not (??)
152: *
153: * @return integer
154: */
155: public static int getIntFromByteArray(byte[] data, int startAt,
156: int length, boolean searchForward) {
157: int bit = 8;
158: int bitMoving = length * bit;
159: int retVal = 0;
160:
161: if (startAt >= data.length) {
162: return retVal;
163:
164: }
165:
166: if (searchForward) {
167: if (length >= (data.length - startAt)) {
168: return retVal;
169:
170: }
171:
172: for (int i = 0; i < length; i++) {
173: bitMoving -= bit;
174: int iData = (int) data[startAt + i];
175: if (iData < 0) {
176: iData += 256;
177: }
178: retVal += iData << bitMoving;
179: }
180: } else {
181: if (length > (startAt + 1)) {
182: return retVal;
183:
184: }
185:
186: for (int i = 0; i < length; i++) {
187: bitMoving -= bit;
188: int iData = (int) data[startAt - i];
189: if (iData < 0) {
190: iData += 256;
191: }
192: retVal += iData << bitMoving;
193: }
194: }
195:
196: return retVal;
197: }
198: }
|