1 package nom.tam.fits.compression.algorithm.quant; 2 3 /*- 4 * #%L 5 * nom.tam FITS library 6 * %% 7 * Copyright (C) 1996 - 2024 nom-tam-fits 8 * %% 9 * This is free and unencumbered software released into the public domain. 10 * 11 * Anyone is free to copy, modify, publish, use, compile, sell, or 12 * distribute this software, either in source code form or as a compiled 13 * binary, for any purpose, commercial or non-commercial, and by any 14 * means. 15 * 16 * In jurisdictions that recognize copyright laws, the author or authors 17 * of this software dedicate any and all copyright interest in the 18 * software to the public domain. We make this dedication for the benefit 19 * of the public at large and to the detriment of our heirs and 20 * successors. We intend this dedication to be an overt act of 21 * relinquishment in perpetuity of all present and future rights to this 22 * software under copyright law. 23 * 24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 27 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 28 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 29 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 30 * OTHER DEALINGS IN THE SOFTWARE. 31 * #L% 32 */ 33 34 /** 35 * A standard fixed random sequence to use for portable and reversible dither 36 * implementations. This is a modified (improved) version of the random sequence 37 * implementation in Appendix I of the <a 38 * href="https://fits.gsfc.nasa.gov/standard40/fits_standard40aa-le.pdf">FITS 39 * 4.0 standard</a>, using integer arithmetics for better performance -- but 40 * still providing the same sequence as the original algorithm. 41 * 42 * @see QuantizeProcessor 43 */ 44 public final class RandomSequence { 45 46 /** 47 * DO NOT CHANGE THIS; used when quantizing real numbers 48 */ 49 private static final int N_RANDOM = 10000; 50 51 private static final int RANDOM_FACTOR = 16807; 52 53 /** 54 * This is our cached fixed random sequence that we will use over and over, 55 * but we defer initializing it until we actually need it. 56 */ 57 private static final double[] VALUES = new double[N_RANDOM]; 58 59 /** 60 * Static initialization for the fixed sequence of random values. 61 */ 62 static { 63 long ival = 1L; 64 for (int i = 0; i < N_RANDOM; i++) { 65 ival = (ival * RANDOM_FACTOR) % Integer.MAX_VALUE; 66 VALUES[i] = (double) ival / Integer.MAX_VALUE; 67 } 68 } 69 70 /** We don't instantiate this class */ 71 private RandomSequence() { 72 } 73 74 /** 75 * Returns the <i>i</i><sup>th</sup> random value from the sequence 76 * 77 * @param i 78 * The index between 0 and {@link #length()} (exclusive). 79 * @return The fixed uniform random deviate value at that index in the range 80 * of 0.0 to 1.0 (exclusive). 81 * @see #length() 82 */ 83 public static double get(int i) { 84 return VALUES[i]; 85 } 86 87 /** 88 * Returns the number of random values in the sequence. 89 * 90 * @return The number of random values available from the fixed sequence. 91 */ 92 public static int length() { 93 return VALUES.length; 94 } 95 }