aboutsummaryrefslogtreecommitdiffstats
path: root/src/jogl/classes/jogamp/opengl/util/pngj/chunks/PngChunkSBIT.java
blob: bc70c6e5e7952841cf40f6a410f10a96211d86f5 (plain)
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
package jogamp.opengl.util.pngj.chunks;

import jogamp.opengl.util.pngj.ImageInfo;
import jogamp.opengl.util.pngj.PngHelperInternal;
import jogamp.opengl.util.pngj.PngjException;

/**
 * sBIT chunk.
 * <p>
 * see http://www.w3.org/TR/PNG/#11sBIT
 * <p>
 * this chunk structure depends on the image type
 */
public class PngChunkSBIT extends PngChunkSingle {
	public final static String ID = ChunkHelper.sBIT;
	// http://www.w3.org/TR/PNG/#11sBIT

	// significant bits
	private int graysb, alphasb;
	private int redsb, greensb, bluesb;

	public PngChunkSBIT(ImageInfo info) {
		super(ID, info);
	}

	@Override
	public ChunkOrderingConstraint getOrderingConstraint() {
		return ChunkOrderingConstraint.BEFORE_PLTE_AND_IDAT;
	}

	private int getLen() {
		int len = imgInfo.greyscale ? 1 : 3;
		if (imgInfo.alpha)
			len += 1;
		return len;
	}

	@Override
	public void parseFromRaw(ChunkRaw c) {
		if (c.len != getLen())
			throw new PngjException("bad chunk length " + c);
		if (imgInfo.greyscale) {
			graysb = PngHelperInternal.readInt1fromByte(c.data, 0);
			if (imgInfo.alpha)
				alphasb = PngHelperInternal.readInt1fromByte(c.data, 1);
		} else {
			redsb = PngHelperInternal.readInt1fromByte(c.data, 0);
			greensb = PngHelperInternal.readInt1fromByte(c.data, 1);
			bluesb = PngHelperInternal.readInt1fromByte(c.data, 2);
			if (imgInfo.alpha)
				alphasb = PngHelperInternal.readInt1fromByte(c.data, 3);
		}
	}

	@Override
	public ChunkRaw createRawChunk() {
		ChunkRaw c = null;
		c = createEmptyChunk(getLen(), true);
		if (imgInfo.greyscale) {
			c.data[0] = (byte) graysb;
			if (imgInfo.alpha)
				c.data[1] = (byte) alphasb;
		} else {
			c.data[0] = (byte) redsb;
			c.data[1] = (byte) greensb;
			c.data[2] = (byte) bluesb;
			if (imgInfo.alpha)
				c.data[3] = (byte) alphasb;
		}
		return c;
	}

	@Override
	public void cloneDataFromRead(PngChunk other) {
		PngChunkSBIT otherx = (PngChunkSBIT) other;
		graysb = otherx.graysb;
		redsb = otherx.redsb;
		greensb = otherx.greensb;
		bluesb = otherx.bluesb;
		alphasb = otherx.alphasb;
	}

	public void setGraysb(int gray) {
		if (!imgInfo.greyscale)
			throw new PngjException("only greyscale images support this");
		graysb = gray;
	}

	public int getGraysb() {
		if (!imgInfo.greyscale)
			throw new PngjException("only greyscale images support this");
		return graysb;
	}

	public void setAlphasb(int a) {
		if (!imgInfo.alpha)
			throw new PngjException("only images with alpha support this");
		alphasb = a;
	}

	public int getAlphasb() {
		if (!imgInfo.alpha)
			throw new PngjException("only images with alpha support this");
		return alphasb;
	}

	/**
	 * Set rgb values
	 * 
	 */
	public void setRGB(int r, int g, int b) {
		if (imgInfo.greyscale || imgInfo.indexed)
			throw new PngjException("only rgb or rgba images support this");
		redsb = r;
		greensb = g;
		bluesb = b;
	}

	public int[] getRGB() {
		if (imgInfo.greyscale || imgInfo.indexed)
			throw new PngjException("only rgb or rgba images support this");
		return new int[] { redsb, greensb, bluesb };
	}
}