KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > rtf > rtflib > tools > ImageUtil


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: ImageUtil.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20
21 /*
22  * This file is part of the RTF library of the FOP project, which was originally
23  * created by Bertrand Delacretaz <bdelacretaz@codeconsult.ch> and by other
24  * contributors to the jfor project (www.jfor.org), who agreed to donate jfor to
25  * the FOP project.
26  */

27
28 package org.apache.fop.render.rtf.rtflib.tools;
29
30 /** Misc.utilities for images handling
31  * This class belongs to the <fo:external-graphic> tag processing.
32  * @author <a HREF="mailto:a.putz@skynamics.com">Andreas Putz</a>
33  */

34 public class ImageUtil {
35
36     //////////////////////////////////////////////////
37
// @@ Construction
38
//////////////////////////////////////////////////
39

40     /**
41      * Private constructor.
42      */

43     private ImageUtil () {
44     }
45
46
47     //////////////////////////////////////////////////
48
// @@ Public static methods
49
//////////////////////////////////////////////////
50

51     /**
52      * Determines the digits from a string.
53      *
54      * @param value String with digits
55      *
56      * @return -1 There is no digit
57      * number The digits as integer
58      */

59     public static int getInt (String JavaDoc value) {
60         String JavaDoc retString = new String JavaDoc ();
61         StringBuffer JavaDoc s = new StringBuffer JavaDoc (value);
62         int len = s.length ();
63
64         for (int i = 0; i < len; i++) {
65             if (Character.isDigit (s.charAt (i))) {
66                 retString += s.charAt (i);
67             } else {
68                 //for example "600.0pt" has to be exited,
69
//when the dot is reached.
70
break;
71             }
72         }
73
74         if (retString.length () == 0) {
75             return -1;
76         } else {
77             return Integer.parseInt (retString);
78         }
79     }
80
81     /**
82      * Checks the string for percent character at the end of string.
83      *
84      * @param value String with digits
85      *
86      * @return true The string contains a % value
87      * false Other string
88      */

89     public static boolean isPercent (String JavaDoc value) {
90         if (value.endsWith ("%")) {
91             return true;
92
93         }
94
95         return false;
96     }
97
98     /**
99      * Compares two hexadecimal values.
100      *
101      * @param pattern Target
102      * @param data Data
103      * @param searchAt Position to start compare
104      * @param searchForward Direction to compare byte arrays
105      *
106      * @return true If equal
107      * false If different
108      */

109     public static boolean compareHexValues (byte[] pattern, byte[] data, int searchAt,
110                                             boolean searchForward) {
111         if (searchAt >= data.length) {
112             return false;
113
114         }
115
116         int pLen = pattern.length;
117
118         if (searchForward) {
119             if (pLen >= (data.length - searchAt)) {
120                 return false;
121
122             }
123
124             for (int i = 0; i < pLen; i++) {
125                 if (pattern[i] != data[searchAt + i]) {
126                     return false;
127                 }
128             }
129
130             return true;
131         } else {
132             if (pLen > (searchAt + 1)) {
133                 return false;
134
135             }
136
137             for (int i = 0; i < pLen; i++) {
138                 if (pattern[pLen - i - 1] != data[searchAt - i]) {
139                     return false;
140                 }
141             }
142
143             return true;
144         }
145     }
146
147     /**
148      * Determines a integer value from a hexadecimal byte array.
149      *
150      * @param data Image
151      * @param startAt Start index to read from
152      * @param length Number of data elements to read
153      * @param searchForward True if searching forward, False if not (??)
154      *
155      * @return integer
156      */

157     public static int getIntFromByteArray (byte[] data, int startAt, int length,
158                                            boolean searchForward) {
159         int bit = 8;
160         int bitMoving = length * bit;
161         int retVal = 0;
162
163         if (startAt >= data.length) {
164             return retVal;
165
166         }
167
168         if (searchForward) {
169             if (length >= (data.length - startAt)) {
170                 return retVal;
171
172             }
173
174             for (int i = 0; i < length; i++) {
175                 bitMoving -= bit;
176                 int iData = (int) data[startAt + i];
177                 if (iData < 0) {
178                     iData += 256;
179                 }
180                 retVal += iData << bitMoving;
181             }
182         } else {
183             if (length > (startAt + 1)) {
184                 return retVal;
185
186             }
187
188             for (int i = 0; i < length; i++) {
189                 bitMoving -= bit;
190                 int iData = (int) data[startAt - i];
191                 if (iData < 0) {
192                     iData += 256;
193                 }
194                 retVal += iData << bitMoving; }
195         }
196
197         return retVal;
198     }
199 }
200
Popular Tags