File: TIFFDecoderPackbits.java

package info (click to toggle)
java-imaging-utilities 0.14.2%2B3-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 2,280 kB
  • ctags: 3,725
  • sloc: java: 31,190; sh: 238; makefile: 53; xml: 30
file content (65 lines) | stat: -rw-r--r-- 1,512 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
/*
 * TIFFDecoderPackbits
 * 
 * Copyright (c) 2002, 2003 Marco Schmidt.
 * All rights reserved.
 */

package net.sourceforge.jiu.codecs.tiff;

import java.io.DataInput;
import java.io.IOException;
import net.sourceforge.jiu.codecs.tiff.TIFFDecoder;
import net.sourceforge.jiu.codecs.InvalidFileStructureException;

/**
 * A TIFF decoder for files compressed with the <em>Packbits</em> method.
 * This compression algorithm has the value <code>32773</code> 
 * in the compression tag of an image file directory.
 * @author Marco Schmidt
 * @since 0.9.0
 */
public class TIFFDecoderPackbits extends TIFFDecoder
{
	public void decode() throws 
		InvalidFileStructureException,
		IOException
	{
		DataInput in = getInput();
		byte[] row = new byte[getBytesPerRow()];
		for (int y = getY1(); y <= getY2(); y++)
		{
			int index = 0;
			do
			{
				byte value = in.readByte();
				if (value >= 0)
				{
					int numSamples = value + 1;
					// copy bytes literally
					in.readFully(row, index, numSamples);
					index += numSamples;
				}
				else
				if (value != (byte)-128)
				{
					int numSamples = - value + 1;
					// write run
					byte sample = in.readByte();
					while (numSamples-- != 0)
					{
						row[index++] = sample;
					}
				}
			}
			while (index != row.length);
			putBytes(row, 0, row.length);
		}
	}


	public Integer[] getCompressionTypes()
	{
		return new Integer[] {new Integer(TIFFConstants.COMPRESSION_PACKBITS)};
	}
}