You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Function.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. package org.apache.fop.render.shading;
  18. import java.util.Collections;
  19. import java.util.List;
  20. public class Function {
  21. /**
  22. * Required: The Type of function (0,2,3,4) default is 0.
  23. */
  24. protected int functionType = 0; // Default
  25. /**
  26. * Required: 2 * m Array of Double numbers which are possible inputs to the function
  27. */
  28. protected List<Double> domain = null;
  29. /**
  30. * Required: 2 * n Array of Double numbers which are possible outputs to the function
  31. */
  32. protected List<Double> range = null;
  33. /* ********************TYPE 0***************************** */
  34. // FunctionType 0 specific function guts
  35. /**
  36. * Required: Array containing the Integer size of the Domain and Range, respectively.
  37. * Note: This is really more like two seperate integers, sizeDomain, and sizeRange,
  38. * but since they're expressed as an array in PDF, my implementation reflects that.
  39. */
  40. protected List<Double> size = null;
  41. /**
  42. * Required for Type 0: Number of Bits used to represent each sample value.
  43. * Limited to 1,2,4,8,12,16,24, or 32
  44. */
  45. protected int bitsPerSample = 1;
  46. /**
  47. * Optional for Type 0: order of interpolation between samples.
  48. * Limited to linear (1) or cubic (3). Default is 1
  49. */
  50. protected int order = 1;
  51. /**
  52. * Optional for Type 0: A 2 * m array of Doubles which provides a
  53. * linear mapping of input values to the domain.
  54. *
  55. * Required for Type 3: A 2 * k array of Doubles that, taken
  56. * in pairs, map each subset of the domain defined by Domain
  57. * and the Bounds array to the domain of the corresponding function.
  58. * Should be two values per function, usually (0,1),
  59. * as in [0 1 0 1] for 2 functions.
  60. */
  61. protected List<Double> encode = null;
  62. /**
  63. * Optional for Type 0: A 2 * n array of Doubles which provides
  64. * a linear mapping of sample values to the range. Defaults to Range.
  65. */
  66. protected List<Double> decode = null;
  67. /**
  68. * Optional For Type 0: A stream of sample values
  69. */
  70. /**
  71. * Required For Type 4: Postscript Calculator function
  72. * composed of arithmetic, boolean, and stack operators + boolean constants
  73. */
  74. protected StringBuffer functionDataStream = null;
  75. /**
  76. * Required (possibly) For Type 0: A vector of Strings for the
  77. * various filters to be used to decode the stream.
  78. * These are how the string is compressed. Flate, LZW, etc.
  79. */
  80. protected List<String> filter = null;
  81. /* *************************TYPE 2************************** */
  82. /**
  83. * Required For Type 2: An Array of n Doubles defining
  84. * the function result when x=0. Default is [0].
  85. */
  86. protected List<Double> cZero = null;
  87. /**
  88. * Required For Type 2: An Array of n Doubles defining
  89. * the function result when x=1. Default is [1].
  90. */
  91. protected List<Double> cOne = null;
  92. /**
  93. * Required for Type 2: The interpolation exponent.
  94. * Each value x will return n results.
  95. * Must be greater than 0.
  96. */
  97. protected double interpolationExponentN = 1;
  98. /* *************************TYPE 3************************** */
  99. /**
  100. * Required for Type 3: An vector of PDFFunctions which
  101. * form an array of k single input functions making up
  102. * the stitching function.
  103. */
  104. protected List<Function> functions = null;
  105. /**
  106. * Optional for Type 3: An array of (k-1) Doubles that,
  107. * in combination with Domain, define the intervals to which
  108. * each function from the Functions array apply. Bounds
  109. * elements must be in order of increasing magnitude,
  110. * and each value must be within the value of Domain.
  111. * k is the number of functions.
  112. * If you pass null, it will output (1/k) in an array of k-1 elements.
  113. * This makes each function responsible for an equal amount of the stitching function.
  114. * It makes the gradient even.
  115. */
  116. protected List<Double> bounds = null;
  117. /**
  118. * create an complete Function object of Type 0, A Sampled function.
  119. *
  120. * Use null for an optional object parameter if you choose not to use it.
  121. * For optional int parameters, pass the default.
  122. * @param theFunctionType This is the type of function (0,2,3, or 4).
  123. * It should be 0 as this is the constructor for sampled functions.
  124. * @param theDomain List objects of Double objects.
  125. * This is the domain of the function.
  126. * See page 264 of the PDF 1.3 Spec.
  127. * @param theRange List objects of Double objects.
  128. * This is the Range of the function.
  129. * See page 264 of the PDF 1.3 Spec.
  130. * @param theSize A List object of Integer objects.
  131. * This is the number of samples in each input dimension.
  132. * I can't imagine there being more or less than two input dimensions,
  133. * so maybe this should be an array of length 2.
  134. *
  135. * See page 265 of the PDF 1.3 Spec.
  136. * @param theBitsPerSample An int specifying the number of bits
  137. used to represent each sample value.
  138. * Limited to 1,2,4,8,12,16,24 or 32.
  139. * See page 265 of the 1.3 PDF Spec.
  140. * @param theOrder The order of interpolation between samples. Default is 1 (one). Limited
  141. * to 1 (one) or 3, which means linear or cubic-spline interpolation.
  142. *
  143. * This attribute is optional.
  144. *
  145. * See page 265 in the PDF 1.3 spec.
  146. * @param theEncode List objects of Double objects.
  147. * This is the linear mapping of input values intop the domain
  148. * of the function's sample table. Default is hard to represent in
  149. * ascii, but basically [0 (Size0 1) 0 (Size1 1)...].
  150. * This attribute is optional.
  151. *
  152. * See page 265 in the PDF 1.3 spec.
  153. * @param theDecode List objects of Double objects.
  154. * This is a linear mapping of sample values into the range.
  155. * The default is just the range.
  156. *
  157. * This attribute is optional.
  158. * Read about it on page 265 of the PDF 1.3 spec.
  159. * @param theFunctionDataStream The sample values that specify
  160. * the function are provided in a stream.
  161. *
  162. * This is optional, but is almost always used.
  163. *
  164. * Page 265 of the PDF 1.3 spec has more.
  165. * @param theFilter This is a vector of String objects which are the various filters that
  166. * have are to be applied to the stream to make sense of it. Order matters,
  167. * so watch out.
  168. *
  169. * This is not documented in the Function section of the PDF 1.3 spec,
  170. * it was deduced from samples that this is sometimes used, even if we may never
  171. * use it in FOP. It is added for completeness sake.
  172. */
  173. public Function(int theFunctionType, List<Double> theDomain, List<Double> theRange,
  174. List<Double> theSize, int theBitsPerSample, int theOrder,
  175. List<Double> theEncode, List<Double> theDecode, StringBuffer theFunctionDataStream,
  176. List<String> theFilter) {
  177. this.functionType = 0; // dang well better be 0;
  178. this.size = theSize;
  179. this.bitsPerSample = theBitsPerSample;
  180. this.order = theOrder; // int
  181. this.encode = theEncode; // vector of int
  182. this.decode = theDecode; // vector of int
  183. this.functionDataStream = theFunctionDataStream;
  184. this.filter = theFilter; // vector of Strings
  185. // the domain and range are actually two dimensional arrays.
  186. // so if there's not an even number of items, bad stuff
  187. // happens.
  188. this.domain = theDomain;
  189. this.range = theRange;
  190. }
  191. /**
  192. * create an complete Function object of Type 2, an Exponential Interpolation function.
  193. *
  194. * Use null for an optional object parameter if you choose not to use it.
  195. * For optional int parameters, pass the default.
  196. * @param theFunctionType The type of the function, which should be 2.
  197. * @param theDomain List objects of Double objects.
  198. * This is the domain of the function.
  199. * See page 264 of the PDF 1.3 Spec.
  200. * @param theRange List of Doubles that is the Range of the function.
  201. * See page 264 of the PDF 1.3 Spec.
  202. * @param theCZero This is a vector of Double objects which defines the function result
  203. * when x=0.
  204. *
  205. * This attribute is optional.
  206. * It's described on page 268 of the PDF 1.3 spec.
  207. * @param theCOne This is a vector of Double objects which defines the function result
  208. * when x=1.
  209. *
  210. * This attribute is optional.
  211. * It's described on page 268 of the PDF 1.3 spec.
  212. * @param theInterpolationExponentN This is the inerpolation exponent.
  213. *
  214. * This attribute is required.
  215. * PDF Spec page 268
  216. */
  217. public Function(int theFunctionType, List<Double> theDomain, List<Double> theRange,
  218. List<Double> theCZero, List<Double> theCOne, double theInterpolationExponentN) {
  219. this.functionType = 2; // dang well better be 2;
  220. this.cZero = theCZero;
  221. this.cOne = theCOne;
  222. this.interpolationExponentN = theInterpolationExponentN;
  223. this.domain = theDomain;
  224. this.range = theRange;
  225. }
  226. /**
  227. * create an complete Function object of Type 3, a Stitching function.
  228. *
  229. * Use null for an optional object parameter if you choose not to use it.
  230. * For optional int parameters, pass the default.
  231. * @param theFunctionType This is the function type. It should be 3,
  232. * for a stitching function.
  233. * @param theDomain List objects of Double objects.
  234. * This is the domain of the function.
  235. * See page 264 of the PDF 1.3 Spec.
  236. * @param theRange List objects of Double objects.
  237. * This is the Range of the function.
  238. * See page 264 of the PDF 1.3 Spec.
  239. * @param theFunctions A List of the PDFFunction objects that the stitching function stitches.
  240. *
  241. * This attributed is required.
  242. * It is described on page 269 of the PDF spec.
  243. * @param theBounds This is a vector of Doubles representing the numbers that,
  244. * in conjunction with Domain define the intervals to which each function from
  245. * the 'functions' object applies. It must be in order of increasing magnitude,
  246. * and each must be within Domain.
  247. *
  248. * It basically sets how much of the gradient each function handles.
  249. *
  250. * This attributed is required.
  251. * It's described on page 269 of the PDF 1.3 spec.
  252. * @param theEncode List objects of Double objects.
  253. * This is the linear mapping of input values intop the domain
  254. * of the function's sample table. Default is hard to represent in
  255. * ascii, but basically [0 (Size0 1) 0 (Size1 1)...].
  256. * This attribute is required.
  257. *
  258. * See page 270 in the PDF 1.3 spec.
  259. */
  260. public Function(int theFunctionType, List<Double> theDomain, List<Double> theRange,
  261. List<Function> theFunctions, List<Double> theBounds,
  262. List<Double> theEncode) {
  263. this.functionType = 3; // dang well better be 3;
  264. this.functions = theFunctions;
  265. this.bounds = theBounds;
  266. this.encode = theEncode;
  267. this.domain = theDomain;
  268. this.range = theRange;
  269. }
  270. /**
  271. * create an complete Function object of Type 4, a postscript calculator function.
  272. *
  273. * Use null for an optional object parameter if you choose not to use it.
  274. * For optional int parameters, pass the default.
  275. * @param theFunctionType The type of function which should be 4, as this is
  276. * a Postscript calculator function
  277. * @param theDomain List object of Double objects.
  278. * This is the domain of the function.
  279. * See page 264 of the PDF 1.3 Spec.
  280. * @param theRange List object of Double objects.
  281. * This is the Range of the function.
  282. * See page 264 of the PDF 1.3 Spec.
  283. * @param theFunctionDataStream This is a stream of arithmetic,
  284. * boolean, and stack operators and boolean constants.
  285. * I end up enclosing it in the '{' and '}' braces for you, so don't do it
  286. * yourself.
  287. *
  288. * This attribute is required.
  289. * It's described on page 269 of the PDF 1.3 spec.
  290. */
  291. public Function(int theFunctionType, List<Double> theDomain, List<Double> theRange,
  292. StringBuffer theFunctionDataStream) {
  293. this.functionType = 4; // dang well better be 4;
  294. this.functionDataStream = theFunctionDataStream;
  295. this.domain = theDomain;
  296. this.range = theRange;
  297. }
  298. /**
  299. * Gets the function type
  300. */
  301. public int getFunctionType() {
  302. return functionType;
  303. }
  304. /**
  305. * Gets the function bounds
  306. */
  307. public List<Double> getBounds() {
  308. return bounds;
  309. }
  310. /**
  311. * The function domain
  312. */
  313. public List<Double> getDomain() {
  314. return domain;
  315. }
  316. /**
  317. * The function size
  318. */
  319. public List<Double> getSize() {
  320. return size;
  321. }
  322. /**
  323. * Gets the function encoding
  324. */
  325. public List<Double> getEncode() {
  326. return encode;
  327. }
  328. /**
  329. * Gets the sub-functions
  330. */
  331. public List<Function> getFunctions() {
  332. if (functions == null) {
  333. return Collections.emptyList();
  334. } else {
  335. return functions;
  336. }
  337. }
  338. /**
  339. * Gets the function filter
  340. */
  341. public List<String> getFilter() {
  342. return filter;
  343. }
  344. /**
  345. * Gets the bits per sample of the function
  346. */
  347. public int getBitsPerSample() {
  348. return bitsPerSample;
  349. }
  350. /**
  351. * Gets the interpolation exponent of the function
  352. */
  353. public double getInterpolationExponentN() {
  354. return interpolationExponentN;
  355. }
  356. /**
  357. * Gets the function order
  358. */
  359. public int getOrder() {
  360. return order;
  361. }
  362. /**
  363. * Gets the function range
  364. */
  365. public List<Double> getRange() {
  366. return range;
  367. }
  368. /**
  369. * Gets the function decoding
  370. */
  371. public List<Double> getDecode() {
  372. return decode;
  373. }
  374. /**
  375. * Gets the function data stream
  376. */
  377. public StringBuffer getDataStream() {
  378. return functionDataStream;
  379. }
  380. /**
  381. * Gets the function C0 value (color for gradient)
  382. */
  383. public List<Double> getCZero() {
  384. return cZero;
  385. }
  386. /**
  387. * Gets the function C1 value (color for gradient)
  388. */
  389. public List<Double> getCOne() {
  390. return cOne;
  391. }
  392. }