001/* ***** BEGIN LICENSE BLOCK *****
002 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
003 *
004 * The contents of this file are subject to the Mozilla Public License Version
005 * 1.1 (the "License"); you may not use this file except in compliance with
006 * the License. You may obtain a copy of the License at
007 * http://www.mozilla.org/MPL/
008 *
009 * Software distributed under the License is distributed on an "AS IS" basis,
010 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
011 * for the specific language governing rights and limitations under the
012 * License.
013 *
014 * The Original Code is part of dcm4che, an implementation of DICOM(TM) in
015 * Java(TM), hosted at https://github.com/gunterze/dcm4che.
016 *
017 * The Initial Developer of the Original Code is
018 * Agfa Healthcare.
019 * Portions created by the Initial Developer are Copyright (C) 2013
020 * the Initial Developer. All Rights Reserved.
021 *
022 * Contributor(s):
023 * See @authors listed below
024 *
025 * Alternatively, the contents of this file may be used under the terms of
026 * either the GNU General Public License Version 2 or later (the "GPL"), or
027 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
028 * in which case the provisions of the GPL or the LGPL are applicable instead
029 * of those above. If you wish to allow use of your version of this file only
030 * under the terms of either the GPL or the LGPL, and not to allow others to
031 * use your version of this file under the terms of the MPL, indicate your
032 * decision by deleting the provisions above and replace them with the notice
033 * and other provisions required by the GPL or the LGPL. If you do not delete
034 * the provisions above, a recipient may use your version of this file under
035 * the terms of any one of the MPL, the GPL or the LGPL.
036 *
037 * ***** END LICENSE BLOCK ***** */
038
039package org.dcm4che3.imageio.codec.jpeg;
040
041/**
042 * @author Gunter Zeilinger <gunterze@gmail.com>
043 *
044 */
045public class JPEGLSCodingParam {
046
047    private int offset;
048    private final int maxVal;
049    private final int t1;
050    private final int t2;
051    private final int t3;
052    private final int reset;
053
054    public JPEGLSCodingParam(int maxVal, int t1, int t2, int t3, int reset) {
055        super();
056        this.maxVal = maxVal;
057        this.t1 = t1;
058        this.t2 = t2;
059        this.t3 = t3;
060        this.reset = reset;
061    }
062
063    public int getOffset() {
064        return offset;
065    }
066
067    public void setOffset(int offset) {
068        this.offset = offset;
069    }
070
071    public final int getMaxVal() {
072        return maxVal;
073    }
074
075    public final int getT1() {
076        return t1;
077    }
078
079    public final int getT2() {
080        return t2;
081    }
082
083    public final int getT3() {
084        return t3;
085    }
086
087    public final int getReset() {
088        return reset;
089    }
090
091    public byte[] getBytes() {
092        return new byte[] {
093                -1, (byte) JPEG.LSE, 0, 13, 1,
094                (byte) (maxVal >> 8), (byte) (maxVal),
095                (byte) (t1 >> 8), (byte) (t1),
096                (byte) (t2 >> 8), (byte) (t2),
097                (byte) (t3 >> 8), (byte) (t3),
098                (byte) (reset >> 8), (byte) (reset) };
099    }
100
101    private static JPEGLSCodingParam getDefaultJPEGLSEncodingParam(
102            int maxVal, int clampedMaxVal, int near) {
103        int factor = (clampedMaxVal + 128) >> 8;
104        int t1 = factor + 2 + 3 * near;
105        if (t1 > maxVal || t1 < near+1)
106            t1 = near+1;
107        int t2 = factor * 4 + 3 + 5 * near;
108        if (t2 > maxVal || t2 < t1)
109            t2 = t1;
110        int t3 = factor * 17 + 4 + 7 * near;
111        if (t3 > maxVal || t3 < t2)
112            t3 = t2;
113        return new JPEGLSCodingParam(maxVal, t1, t2, t3, 64);
114    }
115
116    public static JPEGLSCodingParam getDefaultJPEGLSCodingParam(int p, int near) {
117        int maxVal = (1<<p)-1;
118        return getDefaultJPEGLSEncodingParam(maxVal, Math.min(maxVal, 4095), near);
119    }
120
121    public static JPEGLSCodingParam getJAIJPEGLSCodingParam(int p) {
122        int maxVal = (1<<p)-1;
123        return getDefaultJPEGLSEncodingParam(maxVal, maxVal, 0);
124    }
125
126    @Override
127    public String toString() {
128        return "JPEGLSCodingParam[MAXVAL=" + maxVal
129                + ", T1=" + t1
130                + ", T2=" + t2
131                + ", T3=" + t3
132                + ", RESET=" + reset
133                + "]";
134    }
135
136}