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.ComponentSampleModel;
042import java.awt.image.DataBuffer;
043import java.awt.image.DataBufferByte;
044import java.awt.image.DataBufferShort;
045import java.awt.image.DataBufferUShort;
046import java.awt.image.Raster;
047
048/**
049 * @author Gunter Zeilinger <gunterze@gmail.com>
050 *
051 */
052public abstract class LookupTable {
053
054    protected StoredValue inBits;
055    protected int outBits;
056    protected int offset;
057
058    public LookupTable(StoredValue inBits, int outBits, int offset) {
059        this.inBits = inBits;
060        this.outBits = outBits;
061        this.offset = offset;
062    }
063
064    public abstract int length();
065
066    public void lookup(Raster srcRaster, Raster destRaster) {
067        ComponentSampleModel sm =
068                (ComponentSampleModel) srcRaster.getSampleModel();
069        ComponentSampleModel destsm =
070                (ComponentSampleModel) destRaster.getSampleModel();
071        DataBuffer src = srcRaster.getDataBuffer();
072        DataBuffer dest = destRaster.getDataBuffer();
073        switch (src.getDataType()) {
074        case DataBuffer.TYPE_BYTE:
075            switch (dest.getDataType()) {
076            case DataBuffer.TYPE_BYTE:
077                lookup(sm, ((DataBufferByte) src).getData(),
078                        destsm, ((DataBufferByte) dest).getData());
079                return;
080            case DataBuffer.TYPE_USHORT:
081                lookup(sm, ((DataBufferByte) src).getData(),
082                        destsm, ((DataBufferUShort) dest).getData());
083                return;
084            }
085            break;
086        case DataBuffer.TYPE_USHORT:
087            switch (dest.getDataType()) {
088            case DataBuffer.TYPE_BYTE:
089                lookup(sm, ((DataBufferUShort) src).getData(),
090                        destsm, ((DataBufferByte) dest).getData());
091                return;
092            case DataBuffer.TYPE_USHORT:
093                lookup(sm, ((DataBufferUShort) src).getData(),
094                        destsm, ((DataBufferUShort) dest).getData());
095                return;
096            }
097            break;
098        case DataBuffer.TYPE_SHORT:
099            switch (dest.getDataType()) {
100            case DataBuffer.TYPE_BYTE:
101                lookup(sm, ((DataBufferShort) src).getData(),
102                        destsm, ((DataBufferByte) dest).getData());
103                return;
104            case DataBuffer.TYPE_USHORT:
105                lookup(sm, ((DataBufferShort) src).getData(),
106                        destsm, ((DataBufferUShort) dest).getData());
107                return;
108            }
109            break;
110        }
111        throw new UnsupportedOperationException(
112                "Lookup " + src.getClass()
113                + " -> " + dest.getClass()
114                + " not supported");
115   }
116
117    private void lookup(ComponentSampleModel sm, byte[] src,
118            ComponentSampleModel destsm, byte[] dest) {
119        int w = sm.getWidth();
120        int h = sm.getHeight();
121        int stride = sm.getScanlineStride();
122        int destStride = destsm.getScanlineStride();
123        for (int y = 0; y < h; y++)
124            lookup(src, y * stride, dest, y * destStride, w);
125    }
126
127    private void lookup(ComponentSampleModel sm, short[] src,
128            ComponentSampleModel destsm, byte[] dest) {
129        int w = sm.getWidth();
130        int h = sm.getHeight();
131        int stride = sm.getScanlineStride();
132        int destStride = destsm.getScanlineStride();
133        for (int y = 0; y < h; y++)
134            lookup(src, y * stride, dest, y * destStride, w);
135    }
136
137    private void lookup(ComponentSampleModel sm, byte[] src,
138            ComponentSampleModel destsm, short[] dest) {
139        int w = sm.getWidth();
140        int h = sm.getHeight();
141        int stride = sm.getScanlineStride();
142        int destStride = destsm.getScanlineStride();
143        for (int y = 0; y < h; y++)
144            lookup(src, y * stride, dest, y * destStride, w);
145    }
146
147    private void lookup(ComponentSampleModel sm, short[] src,
148            ComponentSampleModel destsm, short[] dest) {
149        int w = sm.getWidth();
150        int h = sm.getHeight();
151        int stride = sm.getScanlineStride();
152        int destStride = destsm.getScanlineStride();
153        for (int y = 0; y < h; y++)
154            lookup(src, y * stride, dest, y * destStride, w);
155    }
156
157    public abstract void lookup(byte[] src, int srcPost,
158            byte[] dest, int destPos, int length);
159
160    public abstract void lookup(short[] src, int srcPost,
161            byte[] dest, int destPos, int length);
162
163    public abstract void lookup(byte[] src, int srcPost,
164            short[] dest, int destPos, int length);
165
166    public abstract void lookup(short[] src, int srcPost,
167            short[] dest, int destPos, int length);
168
169    public abstract LookupTable adjustOutBits(int outBits);
170
171    public abstract void inverse();
172
173    public abstract LookupTable combine(LookupTable lut);
174
175}