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.Transparency;
042import java.awt.color.ColorSpace;
043import java.awt.image.ColorModel;
044import java.awt.image.DataBuffer;
045import java.awt.image.Raster;
046import java.awt.image.SampleModel;
047
048/**
049 * @author Bill Wallace <wayfarer3130@gmail.com>
050 * @author Gunter Zeilinger <gunterze@gmail.com>
051 *
052 */
053public class SampledComponentColorModel extends ColorModel {
054
055    private static final int[] BITS = { 8, 8, 8 };
056
057    private final ColorSubsampling subsampling;
058
059    public SampledComponentColorModel(ColorSpace cspace,
060            ColorSubsampling subsampling) {
061        super(24, BITS, cspace, false, false,
062                Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
063        this.subsampling = subsampling;
064    }
065
066    @Override
067    public boolean isCompatibleRaster(Raster raster) {
068        return isCompatibleSampleModel(raster.getSampleModel());
069    }
070
071    @Override
072    public boolean isCompatibleSampleModel(SampleModel sm) {
073        return sm instanceof SampledComponentSampleModel;
074    }
075
076    @Override
077    public SampleModel createCompatibleSampleModel(int w, int h) {
078        return new SampledComponentSampleModel(w, h, subsampling);
079    }
080
081    @Override
082    public int getAlpha(int pixel) {
083        return 255;
084    }
085
086    @Override
087    public int getBlue(int pixel) {
088        return pixel & 0xFF;
089    }
090
091    @Override
092    public int getGreen(int pixel) {
093        return pixel & 0xFF00;
094    }
095
096    @Override
097    public int getRed(int pixel) {
098        return pixel & 0xFF0000;
099    }
100
101    @Override
102    public int getAlpha(Object inData) {
103        return 255;
104    }
105
106    @Override
107    public int getBlue(Object inData) {
108        return getRGB(inData) & 0xFF;
109    }
110
111    @Override
112    public int getGreen(Object inData) {
113        return (getRGB(inData) >> 8) & 0xFF;
114    }
115
116    @Override
117    public int getRed(Object inData) {
118        return getRGB(inData) >> 16;
119    }
120
121    @Override
122    public int getRGB(Object inData) {
123        byte[] ba = (byte[]) inData;
124        ColorSpace cs = getColorSpace();
125        float[] fba = new float[] { (ba[0] & 0xFF) / 255f,
126                (ba[1] & 0xFF) / 255f, (ba[2] & 0xFF) / 255f };
127        float[] rgb = cs.toRGB(fba);
128        int ret = (((int) (rgb[0] * 255)) << 16)
129                | (((int) (rgb[1] * 255)) << 8) | (((int) (rgb[2] * 255)));
130        return ret;
131    }
132
133}