001/*
002 * **** BEGIN LICENSE BLOCK *****
003 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
004 *
005 * The contents of this file are subject to the Mozilla Public License Version
006 * 1.1 (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 * http://www.mozilla.org/MPL/
009 *
010 * Software distributed under the License is distributed on an "AS IS" basis,
011 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
012 * for the specific language governing rights and limitations under the
013 * License.
014 *
015 * The Original Code is part of dcm4che, an implementation of DICOM(TM) in
016 * Java(TM), hosted at https://github.com/dcm4che.
017 *
018 * The Initial Developer of the Original Code is Agfa Healthcare.
019 * Portions created by the Initial Developer are Copyright (C) 2011-2015
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 */
039
040package org.dcm4che3.imageio.codec;
041
042import org.dcm4che3.data.Attributes;
043import org.dcm4che3.data.Tag;
044import org.dcm4che3.data.VR;
045import org.dcm4che3.image.PhotometricInterpretation;
046
047/**
048 * @author Gunter Zeilinger <gunterze@gmail.com>
049 * @since Jan 2015.
050 */
051public class ImageParams {
052
053    private final int rows;
054    private final int cols;
055    private final int samples;
056    private final int pixelRepresentation;
057    private final int bitsAllocated;
058    private final int bitsStored;
059    private final int frames;
060    private final int frameLength;
061    private final int length;
062
063    private PhotometricInterpretation pmi;
064    private int planarConfiguration;
065
066    public ImageParams(Attributes attrs) {
067        this.rows = attrs.getInt(Tag.Rows, 0);
068        this.cols = attrs.getInt(Tag.Columns, 0);
069        this.samples = attrs.getInt(Tag.SamplesPerPixel, 0);
070        this.pmi = PhotometricInterpretation.fromString(
071                attrs.getString(Tag.PhotometricInterpretation, "MONOCHROME2"));
072        this.bitsAllocated = attrs.getInt(Tag.BitsAllocated, 8);
073        this.bitsStored = attrs.getInt(Tag.BitsStored, bitsAllocated);
074        this.planarConfiguration = attrs.getInt(Tag.PlanarConfiguration, 0);
075        this.pixelRepresentation = attrs.getInt(Tag.PixelRepresentation, 0);
076        this.frames = attrs.getInt(Tag.NumberOfFrames, 1);
077        this.frameLength = rows * cols * samples * (bitsAllocated >>> 3);
078        this.length = frameLength * frames;
079    }
080
081    public int getRows() {
082        return rows;
083    }
084
085    public int getColumns() {
086        return cols;
087    }
088
089    public int getSamples() {
090        return samples;
091    }
092
093    public int getBitsAllocated() {
094        return bitsAllocated;
095    }
096
097    public int getBitsStored() {
098        return bitsStored;
099    }
100
101    public int getFrameLength() {
102        return frameLength;
103    }
104
105    public int getLength() {
106        return length;
107    }
108
109    public PhotometricInterpretation getPhotometricInterpretation() {
110        return pmi;
111    }
112
113    public int getEncodedLength() {
114        return (length + 1) & ~1;
115    }
116
117    public boolean paddingNull() {
118        return (length & 1) != 0;
119    }
120
121    public boolean isBanded() {
122        return planarConfiguration != 0;
123    }
124
125    public boolean isSigned() {
126        return pixelRepresentation != 0;
127    }
128
129    public int getFrames() {
130        return frames;
131    }
132
133    public void decompress(Attributes attrs, TransferSyntaxType tstype) {
134        if (samples > 1) {
135            pmi = pmi.decompress();
136            planarConfiguration = tstype.getPlanarConfiguration();
137            attrs.setString(Tag.PhotometricInterpretation, VR.CS, pmi.toString());
138            attrs.setInt(Tag.PlanarConfiguration, VR.US, planarConfiguration);
139        }
140    }
141
142    public void compress(Attributes attrs, TransferSyntaxType tstype) {
143        if (samples > 1) {
144            pmi = tstype.compress(pmi);
145            planarConfiguration = tstype.getPlanarConfiguration();
146            attrs.setString(Tag.PhotometricInterpretation, VR.CS, pmi.toString());
147            attrs.setInt(Tag.PlanarConfiguration, VR.US, planarConfiguration);
148        }
149    }
150
151}