File: LookupTableOperation.java

package info (click to toggle)
java-imaging-utilities 0.14.3-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,556 kB
  • sloc: java: 31,233; python: 71; xml: 31; makefile: 26; sh: 5
file content (196 lines) | stat: -rw-r--r-- 5,713 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
 * LookupTableOperation
 * 
 * Copyright (c) 2001, 2002, 2003, 2004, 2005 Marco Schmidt
 * All rights reserved.
 */

package net.sourceforge.jiu.ops;

import net.sourceforge.jiu.data.PixelImage;
import net.sourceforge.jiu.data.IntegerImage;
import net.sourceforge.jiu.ops.ImageToImageOperation;
import net.sourceforge.jiu.ops.MissingParameterException;
import net.sourceforge.jiu.ops.WrongParameterException;

/**
 * An operation that replaces samples with values taken from a lookup table.
 * Operations where each pixel is treated independently from its neighbors
 * and where a pixel value is always mapped to the same new pixel value
 * can be implemented this way.
 *
 * @author Marco Schmidt
 * @since 0.6.0
 */
public abstract class LookupTableOperation extends ImageToImageOperation
{
	private int[][] intTables;
	private int numTables;

	/**
	 * Creates a LookupTableOperation for one lookup table.
	 */
	public LookupTableOperation()
	{
		this(1);
	}

	/**
	 * Creates an object of this class, calling the super constructor with two <code>null</code>
	 * arguments and allocates space for the argument number of lookup tables.
	 * @param numTables number of tables to be used in this operation
	 */
	public LookupTableOperation(int numTables)
	{
		super(null, null);
		if (numTables < 1)
		{
			throw new IllegalArgumentException("The number of tables must be at least 1; got " + numTables);
		}
		intTables = new int[numTables][];
		this.numTables = numTables;
	}

	/**
	 * Returns the number of tables in this operation.
	 * @return number of tables
	 */
	public int getNumTables()
	{
		return numTables;
	}

	/**
	 * Returns one of the internal <code>int</code> lookup tables.
	 * @param channelIndex the zero-based index of the table to be returned;
	 *   from 0 to getNumTables() - 1
	 * @return the channelIndex'th table
	 */
	public int[] getTable(int channelIndex)
	{
		return intTables[channelIndex];
	}

	public void prepareImages() throws
		MissingParameterException,
		WrongParameterException
	{
		ensureInputImageIsAvailable();
		PixelImage in = getInputImage();
		if (!(in instanceof IntegerImage))
		{
			throw new WrongParameterException("Input image must be of type IntegerImage.");
		}
		PixelImage out = getOutputImage();
		if (out == null)
		{
			out = in.createCompatibleImage(in.getWidth(), in.getHeight());
			setOutputImage(out);
		}
		else
		{
			if (in.getNumChannels() != out.getNumChannels())
			{
				throw new WrongParameterException("Output image must have same number of channels as input image.");
			}
			ensureImagesHaveSameResolution();
		}
	}

	public void process() throws
		MissingParameterException,
		WrongParameterException
	{
		prepareImages();
		process((IntegerImage)getInputImage(), (IntegerImage)getOutputImage());
	}

	private void process(IntegerImage in, IntegerImage out)
	{
		boolean useFirstTableOnly = getNumTables() < in.getNumChannels();
		final int TOTAL_ITEMS = in.getHeight() * in.getNumChannels();
		int processedItems = 0;
		for (int channelIndex = 0; channelIndex < in.getNumChannels(); channelIndex++)
		{
			int tableIndex;
			if (useFirstTableOnly)
			{
				tableIndex = 0;
			}
			else
			{
				tableIndex = channelIndex;
			}
			process(in, out, channelIndex, tableIndex, processedItems, TOTAL_ITEMS);
			processedItems += in.getHeight();
		}
	}

	private void process(IntegerImage in, IntegerImage out, final int CHANNEL_INDEX,
		int tableIndex, int processedItems, final int TOTAL_ITEMS)
	{
		final int[] TABLE = getTable(tableIndex);
		final int WIDTH = in.getWidth();
		final int HEIGHT = in.getHeight();
		for (int y = 0; y < HEIGHT; y++)
		{
			for (int x = 0; x < WIDTH; x++)
			{
				out.putSample(CHANNEL_INDEX, x, y, TABLE[in.getSample(CHANNEL_INDEX, x, y)]);
			}
			setProgress(processedItems++, TOTAL_ITEMS);
		}
	}

	/**
	 * Resets the number of tables to be used in this operation to the 
	 * argument and drops all actual table data initialized so far.
	 * After a call to this method, {@link #getTable} will return
	 * <code>null</code> as long as no new table data is provided
	 * via {@link #setTable} or {@link #setTables}.
	 * @param numberOfTables the new number of tables for this operation, must be <code>1</code> or larger
	 * @throws IllegalArgumentException if the number is zero or smaller
	 */
	public void setNumTables(int numberOfTables)
	{
		if (numberOfTables < 1)
		{
			throw new IllegalArgumentException("Number of tables argument must be larger than zero.");
		}
		numTables = numberOfTables;
		intTables = new int[numTables][];
	}

	/**
	 * Provides a new lookup table for one of the channels.
	 * @param channelIndex the index of the channel for which a table is provided; must be at least <code>0</code> and smaller than {@link #getNumTables}
	 * @param tableData the actual table to be used for lookup
	 * @throws IllegalArgumentException if the channel index is not in the valid interval (see above)
	 */
	public void setTable(int channelIndex, int[] tableData)
	{
		if (channelIndex < 0)
		{
			throw new IllegalArgumentException("The channelIndex argument must be at least 0; got " + channelIndex);
		}
		if (channelIndex >= getNumTables())
		{
			throw new IllegalArgumentException("The channelIndex argument must be smaller than the number of tables " + 
				getNumTables() + "; got " + channelIndex);
		}
		intTables[channelIndex] = tableData;
	}

	/**
	 * Sets the tables for all channels to the argument table.
	 * Useful when the same table can be used for all channels.
	 * @param tableData the data that will be used as lookup table for all channels
	 */
	public void setTables(int[] tableData)
	{
		for (int i = 0; i < getNumTables(); i++)
		{
			setTable(i, tableData);
		}
	}
}