Debian Codegraph @

src/com/drew/metadata/pcx/PcxReader.java

raw symbols SCIP
1 /*
2 * Copyright 2002-2019 Drew Noakes and contributors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * More information about this project is available at:
17 *
18 * https://drewnoakes.com/code/exif/
19 * https://github.com/drewnoakes/metadata-extractor
20 */
21 package com.drew.metadata.pcx;
22
23 import com.drew.imaging.ImageProcessingException;
24 import com.drew.lang.SequentialReader;
25 import com.drew.lang.annotations.NotNull;
26 import com.drew.metadata.Metadata;
27
28 /**
29 * Reads PCX image file metadata.
30 *
31 * <ul>
32 * <li>https://courses.engr.illinois.edu/ece390/books/labmanual/graphics-pcx.html</li>
33 * <li>http://www.fileformat.info/format/pcx/egff.htm</li>
34 * <li>http://fileformats.archiveteam.org/wiki/PCX</li>
35 * </ul>
36 *
37 * @author Drew Noakes https://drewnoakes.com
38 */
39 public class PcxReader
40 {
41 public void extract(@NotNull final SequentialReader reader, @NotNull final Metadata metadata)
42 {
43 reader.setMotorolaByteOrder(false);
44
45 PcxDirectory directory = new PcxDirectory();
46 metadata.addDirectory(directory);
47
48 try {
49 byte identifier = reader.getInt8();
50 if (identifier != 0x0A)
51 throw new ImageProcessingException("Invalid PCX identifier byte");
52
53 directory.setInt(PcxDirectory.TAG_VERSION, reader.getInt8());
54
55 byte encoding = reader.getInt8();
56 if (encoding != 0x01)
57 throw new ImageProcessingException("Invalid PCX encoding byte");
58
59 directory.setInt(PcxDirectory.TAG_BITS_PER_PIXEL, reader.getUInt8());
60 directory.setInt(PcxDirectory.TAG_XMIN, reader.getUInt16());
61 directory.setInt(PcxDirectory.TAG_YMIN, reader.getUInt16());
62 directory.setInt(PcxDirectory.TAG_XMAX, reader.getUInt16());
63 directory.setInt(PcxDirectory.TAG_YMAX, reader.getUInt16());
64 directory.setInt(PcxDirectory.TAG_HORIZONTAL_DPI, reader.getUInt16());
65 directory.setInt(PcxDirectory.TAG_VERTICAL_DPI, reader.getUInt16());
66 directory.setByteArray(PcxDirectory.TAG_PALETTE, reader.getBytes(48));
67 reader.skip(1);
68 directory.setInt(PcxDirectory.TAG_COLOR_PLANES, reader.getUInt8());
69 directory.setInt(PcxDirectory.TAG_BYTES_PER_LINE, reader.getUInt16());
70
71 int paletteType = reader.getUInt16();
72 if (paletteType != 0)
73 directory.setInt(PcxDirectory.TAG_PALETTE_TYPE, paletteType);
74
75 int hScrSize = reader.getUInt16();
76 if (hScrSize != 0)
77 directory.setInt(PcxDirectory.TAG_HSCR_SIZE, hScrSize);
78
79 int vScrSize = reader.getUInt16();
80 if (vScrSize != 0)
81 directory.setInt(PcxDirectory.TAG_VSCR_SIZE, vScrSize);
82
83 } catch (Exception ex) {
84 directory.addError("Exception reading PCX file metadata: " + ex.getMessage());
85 }
86 }
87 }