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.image;
040
041import java.awt.image.DataBuffer;
042import java.awt.image.DataBufferByte;
043import java.awt.image.SampleModel;
044
045/**
046 * @author Bill Wallace <wayfarer3130@gmail.com>
047 * @author Gunter Zeilinger <gunterze@gmail.com>
048 *
049 */
050public class SampledComponentSampleModel extends SampleModel {
051
052    private final ColorSubsampling subsampling;
053
054    public SampledComponentSampleModel(int w, int h, ColorSubsampling subsampling) {
055        super(DataBuffer.TYPE_BYTE, w, h, 3);
056        this.subsampling = subsampling;
057    }
058
059    @Override
060    public SampleModel createCompatibleSampleModel(int w, int h) {
061        return new SampledComponentSampleModel(w, h, subsampling);
062    }
063
064    @Override
065    public DataBuffer createDataBuffer() {
066        return new DataBufferByte(subsampling.frameLength(width, height));
067    }
068
069    @Override
070    public SampleModel createSubsetSampleModel(int[] bands) {
071        if (bands.length != 3 
072                || bands[0] != 0
073                || bands[1] != 1
074                || bands[2] != 2)
075            throw new UnsupportedOperationException();
076
077        return this;
078    }
079
080    @Override
081    public Object getDataElements(int x, int y, Object obj, DataBuffer data) {
082        byte[] ret;
083        if ((obj instanceof byte[]) && ((byte[]) obj).length == 3)
084            ret = (byte[]) obj;
085        else
086            ret = new byte[3];
087        DataBufferByte dbb = (DataBufferByte) data;
088        byte[] ba = dbb.getData();
089        int iy = subsampling.indexOfY(x, y, width);
090        int ibr = subsampling.indexOfBR(x, y, width);
091        ret[0] = ba[iy];
092        ret[1] = ba[ibr];
093        ret[2] = ba[ibr+1];
094        return ret;
095    }
096
097    @Override
098    public int getNumDataElements() {
099        return 3;
100    }
101
102    @Override
103    public int getSample(int x, int y, int b, DataBuffer data) {
104        return ((byte[]) getDataElements(x, y, null, data))[b];
105    }
106
107    @Override
108    public int[] getSampleSize() {
109        return new int[] { 8, 8, 8 };
110    }
111
112    @Override
113    public int getSampleSize(int band) {
114        return 8;
115    }
116
117    @Override
118    public void setDataElements(int x, int y, Object obj, DataBuffer data) {
119        throw new UnsupportedOperationException();
120    }
121
122    @Override
123    public void setSample(int x, int y, int b, int s, DataBuffer data) {
124        throw new UnsupportedOperationException();
125    }
126
127}