Debian Codegraph @

src/com/drew/metadata/ico/IcoReader.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.ico;
22
23 import com.drew.lang.SequentialReader;
24 import com.drew.lang.annotations.NotNull;
25 import com.drew.metadata.Metadata;
26
27 import java.io.IOException;
28
29 /**
30 * Reads ICO (Windows Icon) file metadata.
31 * <ul>
32 * <li>https://en.wikipedia.org/wiki/ICO_(file_format)</li>
33 * </ul>
34 *
35 * @author Drew Noakes https://drewnoakes.com
36 */
37 public class IcoReader
38 {
39 public void extract(@NotNull final SequentialReader reader, @NotNull final Metadata metadata)
40 {
41 reader.setMotorolaByteOrder(false);
42
43 int type;
44 int imageCount;
45
46 // Read header (ICONDIR structure)
47 try {
48 int reserved = reader.getUInt16();
49
50 if (reserved != 0) {
51 IcoDirectory directory = new IcoDirectory();
52 directory.addError("Invalid header bytes");
53 metadata.addDirectory(directory);
54 return;
55 }
56
57 type = reader.getUInt16();
58
59 if (type != 1 && type != 2) {
60 IcoDirectory directory = new IcoDirectory();
61 directory.addError("Invalid type " + type + " -- expecting 1 or 2");
62 metadata.addDirectory(directory);
63 return;
64 }
65
66 imageCount = reader.getUInt16();
67
68 if (imageCount == 0) {
69 IcoDirectory directory = new IcoDirectory();
70 directory.addError("Image count cannot be zero");
71 metadata.addDirectory(directory);
72 return;
73 }
74
75 } catch (IOException ex) {
76 IcoDirectory directory = new IcoDirectory();
77 directory.addError("Exception reading ICO file metadata: " + ex.getMessage());
78 metadata.addDirectory(directory);
79 return;
80 }
81
82 // Read each embedded image
83 for (int imageIndex = 0; imageIndex < imageCount; imageIndex++) {
84 IcoDirectory directory = new IcoDirectory();
85 try {
86 directory.setInt(IcoDirectory.TAG_IMAGE_TYPE, type);
87
88 //See https://docs.fileformat.com/image/ico/ - An image width/height of 0 means 256
89 int imageWidth = reader.getUInt8();
90 int imageHeight = reader.getUInt8();
91 directory.setInt(IcoDirectory.TAG_IMAGE_WIDTH, imageWidth == 0 ? 256 : imageWidth);
92 directory.setInt(IcoDirectory.TAG_IMAGE_HEIGHT, imageHeight == 0 ? 256 : imageHeight);
93 directory.setInt(IcoDirectory.TAG_COLOUR_PALETTE_SIZE, reader.getUInt8());
94 // Ignore this byte (normally zero, though .NET's System.Drawing.Icon.Save method writes 255)
95 reader.getUInt8();
96 if (type == 1) {
97 // Icon
98 directory.setInt(IcoDirectory.TAG_COLOUR_PLANES, reader.getUInt16());
99 directory.setInt(IcoDirectory.TAG_BITS_PER_PIXEL, reader.getUInt16());
100 } else {
101 // Cursor
102 directory.setInt(IcoDirectory.TAG_CURSOR_HOTSPOT_X, reader.getUInt16());
103 directory.setInt(IcoDirectory.TAG_CURSOR_HOTSPOT_Y, reader.getUInt16());
104 }
105 directory.setLong(IcoDirectory.TAG_IMAGE_SIZE_BYTES, reader.getUInt32());
106 directory.setLong(IcoDirectory.TAG_IMAGE_OFFSET_BYTES, reader.getUInt32());
107 } catch (IOException ex) {
108 directory.addError("Exception reading ICO file metadata: " + ex.getMessage());
109 }
110 metadata.addDirectory(directory);
111 }
112 }
113 }