File: MemoryPaletted8Image.java

package info (click to toggle)
java-imaging-utilities 0.14.2%2B3-4
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 2,304 kB
  • ctags: 3,737
  • sloc: java: 31,190; sh: 238; xml: 30; makefile: 19
file content (136 lines) | stat: -rw-r--r-- 3,244 bytes parent folder | download | duplicates (2)
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
/*
 * Paletted8Image
 *
 * Copyright (c) 2000, 2001, 2002, 2003 Marco Schmidt.
 * All rights reserved.
 */

package net.sourceforge.jiu.data;

import net.sourceforge.jiu.data.Palette;
import net.sourceforge.jiu.data.PixelImage;

/**
 * This class stores a paletted image with one byte per sample in memory.
 *
 * @author Marco Schmidt
 * @see net.sourceforge.jiu.data.ByteChannelImage
 * @see net.sourceforge.jiu.data.IntegerImage
 * @see net.sourceforge.jiu.data.Palette
 */
public class MemoryPaletted8Image extends MemoryByteChannelImage implements Paletted8Image
{
	/**
	 * This image's palette.
	 */
	private Palette palette;
	private int maxSampleValue;

	/**
	 * Create an image of byte channels.
	 * Image data will be completely in memory, so memory requirements are 
	 * <code>width * height * numChannels</code> bytes.
	 * Note that the data will not be initialized, so you should not assume
	 * anything about its content.
	 * @param width the horizontal resolution, must be non-zero and positive
	 * @param height the vertical resolution, must be non-zero and positive
	 * @throws IllegalArgumentException if any of the parameters are smaller than 1
	 */
	public MemoryPaletted8Image(int width, int height)
	{
		super(1, width, height);
		palette = null;
		maxSampleValue = 255;
	}

	public MemoryPaletted8Image(int width, int height, Palette palette)
	{
		this(width, height);
		setPalette(palette);
	}

	public static void checkPalette(Palette palette)
	{
		if (palette == null)
		{
			throw new IllegalArgumentException("Palette must be non-null.");
		}
		else
		{
			int numEntries = palette.getNumEntries();
			if (numEntries < 1 || numEntries > 256)
			{
				throw new IllegalArgumentException("Number of entries must " +
					"be from 1..256 for a Paletted8Image; got: " + numEntries);
			}
		}
	}

	public PixelImage createCompatibleImage(int width, int height)
	{
		Palette newPalette = null;
		Palette myPalette = getPalette();
		if (myPalette != null)
		{
			newPalette = (Palette)myPalette.clone();
		}
		return new MemoryPaletted8Image(width, height, newPalette);
	}

	public long getAllocatedMemory()
	{
		long result = super.getAllocatedMemory();
		Palette myPalette = getPalette();
		if (myPalette != null)
		{
			result += myPalette.getAllocatedMemory();
		}
		return result;
	}

	public Class getImageType()
	{
		return Paletted8Image.class;
	}

	public int getMaxSample(int channel)
	{
		return maxSampleValue;
	}

	/**
	 * Returns this image's palette.
	 * @see #setPalette
	 */
	public Palette getPalette()
	{
		return palette;
	}

	public String getTypeDescription()
	{
		return "Paletted image, 8 bits per pixel";
	}

	/**
	 * Sets this image's palette to a new value.
	 * @see #getPalette
	 */
	public void setPalette(Palette palette)
	{
		if (palette != null && palette.getNumEntries() > 256)
		{
			throw new IllegalArgumentException("Cannot use palette with more " +
				"than 256 entries in a Paletted8Image.");
		}
		this.palette = palette;
		if (palette == null)
		{
			maxSampleValue = 255;
		}
		else
		{
			maxSampleValue = palette.getNumEntries() - 1;
		}
	}
}