[
https://issues.apache.org/jira/browse/TIKA-4937?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18123183#comment-18123183
]
ASF GitHub Bot commented on TIKA-4937:
--------------------------------------
dschmidt commented on code in PR #3266:
URL: https://github.com/apache/tika/pull/3266#discussion_r4178717799
##########
tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-image-module/src/main/java/org/apache/tika/parser/image/ICOParser.java:
##########
@@ -0,0 +1,214 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.tika.parser.image;
+
+import java.io.IOException;
+import java.util.Set;
+
+import org.apache.commons.io.IOUtils;
+import org.xml.sax.ContentHandler;
+import org.xml.sax.SAXException;
+
+import org.apache.tika.annotation.TikaComponent;
+import org.apache.tika.exception.TikaException;
+import org.apache.tika.extractor.EmbeddedDocumentUtil;
+import org.apache.tika.io.BoundedInputStream;
+import org.apache.tika.io.EndianUtils;
+import org.apache.tika.io.TikaInputStream;
+import org.apache.tika.metadata.HttpHeaders;
+import org.apache.tika.metadata.Icon;
+import org.apache.tika.metadata.Metadata;
+import org.apache.tika.metadata.TIFF;
+import org.apache.tika.mime.MediaType;
+import org.apache.tika.parser.ParseContext;
+import org.apache.tika.parser.Parser;
+import org.apache.tika.sax.XHTMLContentHandler;
+
+/**
+ * Parser for Windows icon (ICO) and cursor (CUR) files. Reads the ICONDIR
+ * and the header of every image to report the dimensions and colour depth of
+ * the largest image, the list of all images and, for cursors, the hotspot.
+ * The images themselves are not decoded.
+ * <p>
+ * The directory's own width, height and colour fields are unreliable (256 px
+ * is stored as 0, many tools leave the bit count empty), so the values come
+ * from each image's PNG IHDR or BITMAPINFOHEADER and the directory is only
+ * the fallback.
+ */
+@TikaComponent
+public class ICOParser implements Parser {
+
+ private static final long serialVersionUID = 4212837190215395123L;
+
+ static final MediaType ICO_TYPE = MediaType.image("vnd.microsoft.icon");
+ static final MediaType CUR_TYPE = MediaType.image("x-win-bitmap");
+
+ private static final Set<MediaType> SUPPORTED_TYPES = Set.of(ICO_TYPE,
CUR_TYPE);
+
+ private static final int TYPE_ICON = 1;
+ private static final int TYPE_CURSOR = 2;
+ private static final int HEADER_SIZE = 6;
+ private static final int ENTRY_SIZE = 16;
+ private static final int BITMAP_INFO_HEADER_SIZE = 40;
+ private static final byte[] PNG_SIGNATURE =
+ {(byte) 0x89, 'P', 'N', 'G', '\r', '\n', 0x1a, '\n'};
+ private static final int PNG_IHDR_SIZE = 8 + 8 + 13;
+ // Icons are small; anything bigger is read only this far
+ private static final int MAX_FILE_SIZE = 16 * 1024 * 1024;
+
+ @Override
+ public Set<MediaType> getSupportedTypes(ParseContext context) {
+ return SUPPORTED_TYPES;
Review Comment:
Done in f172c8c6e0: the 2.4.1 baselines list ICOParser for
image/vnd.microsoft.icon.
##########
tika-core/src/main/resources/org/apache/tika/mime/tika-mimetypes.xml:
##########
@@ -6900,6 +6900,19 @@
<glob pattern="*.ico"/>
</mime-type>
+ <mime-type type="image/x-win-bitmap">
+ <acronym>CUR</acronym>
+ <_comment>Windows Cursor</_comment>
+ <tika:link>https://en.wikipedia.org/wiki/ICO_(file_format)</tika:link>
+ <magic priority="50">
+ <!
> Extract image dimensions and entry list from ICO/CUR files
> ----------------------------------------------------------
>
> Key: TIKA-4937
> URL: https://issues.apache.org/jira/browse/TIKA-4937
> Project: Tika
> Issue Type: New Feature
> Reporter: Dominik Schmidt
> Priority: Major
>
> h3. Background
> Tika detects {{image/vnd.microsoft.icon}} (alias {{image/x-icon}}) and
> {{ImageParser}} claims the type, but ImageIO has no ICO reader on the
> classpath, so a {{.ico}} yields nothing beyond the content type: no
> dimensions, no bit depth, no image count.
> Since TIKA-4936 every Windows EXE/DLL emits its icon groups as embedded
> {{.ico}} documents (the first one typed {{THUMBNAIL}}), so an index now sees
> plenty of .ico entries without width/height.
> h3. Proposal
> Read the ICONDIR/ICONDIRENTRY structure of {{.ico}} and {{.cur}} files
> directly (no decoding, no new dependency) and set:
> * {{tiff:ImageWidth}} / {{tiff:ImageLength}} and {{tiff:BitsPerSample}} of
> the largest image. Entries of 256 px are stored as 0 in the directory, and
> the directory's width/height/bpp fields are unreliable in general, so take
> the values from each image's header instead: the PNG IHDR for PNG-encoded
> entries, the BITMAPINFOHEADER for BMP-encoded ones (height there is doubled
> to include the AND mask).
> * the number of images
> * one multi-valued property listing each entry as {{WxH@bpp}} plus its
> encoding (bmp/png), in directory order
> * for cursors ({{.cur}}, type 2 in the header): the hotspot of the largest
> image
> No text content. Malformed files (directory beyond EOF, entry offsets outside
> the file, count 0, unknown encoding) must not throw: emit what can be read
> and record a warning.
> Implementation: a dedicated small parser in {{tika-parser-image-module}} for
> {{image/vnd.microsoft.icon}} and {{image/x-win-bitmap}}, replacing the
> {{image/x-icon}} entry in {{ImageParser}}'s supported types (which currently
> finds no reader).
> h3. Out of scope
> * Decoding or rendering the images (the thumbnail presets keep passing the
> .ico through as stored)
> * Animated cursors ({{.ani}}), {{RT_GROUP_CURSOR}} extraction from PE files
> h3. Acceptance criteria
> * {{tika -m}} on a .ico with 16/32 px BMP entries and a 256 px PNG entry
> reports 256x256, 32 bpp, 3 images and the per-entry list.
> * The embedded icons of TIKA-4936's fixtures show width/height in {{/rmeta}}
> output.
> * A .cur reports type cursor and the hotspot.
> * Truncated or corrupt .ico files yield a warning, not an exception.
> * Fixtures: {{testWindows-icons-app.ico}} / {{-doc.ico}} from TIKA-4936 plus
> one generated .cur.
--
This message was sent by Atlassian Jira
(v8.20.10#820010)