172ec446fb1f00f7a380c4e2f5fcbf308f67c49b
[meviz.git] / src / main / java / eu / svjatoslav / meviz / htmlindexer / Utils.java
1 /*
2  * Meviz - Various tools collection to work with multimedia.
3  * Copyright (C) 2012 -- 2019, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of version 3 of the GNU Lesser General Public License
7  * or later as published by the Free Software Foundation.
8 */
9
10 package eu.svjatoslav.meviz.htmlindexer;
11
12 import eu.svjatoslav.meviz.htmlindexer.layouts.Layout;
13 import eu.svjatoslav.meviz.htmlindexer.layouts.MixedLayout;
14 import org.w3c.dom.NamedNodeMap;
15 import org.w3c.dom.Node;
16 import org.w3c.dom.NodeList;
17
18 import javax.imageio.ImageReader;
19 import javax.imageio.metadata.IIOMetadata;
20 import javax.imageio.metadata.IIOMetadataNode;
21 import java.awt.*;
22 import java.awt.image.BufferedImage;
23 import java.io.BufferedReader;
24 import java.io.File;
25 import java.io.FileReader;
26 import java.io.IOException;
27 import java.util.ArrayList;
28 import java.util.HashSet;
29 import java.util.zip.CRC32;
30
31 import static org.openimaj.image.ImageUtilities.createBufferedImage;
32 import static org.openimaj.image.ImageUtilities.readMBF;
33
34 public class Utils {
35     private static File lastLoadedFile;
36     private static BufferedImage lastLoadedBufferedImage;
37
38     /**
39      * Load image into {@link BufferedImage} and return it. Caches last loaded
40      * image to speed up subsequent loading attempts.
41      *
42      * @throws ImageFormatError
43      * @throws IOException
44      */
45     public static BufferedImage getBufferedImage(final File file)
46             throws ImageFormatError, IOException {
47         if (file.equals(lastLoadedFile))
48             return lastLoadedBufferedImage;
49
50         lastLoadedBufferedImage = createBufferedImage(readMBF(file));
51         lastLoadedFile = file;
52
53         if (lastLoadedBufferedImage == null) {
54             System.out.println("Error reading image: " + file);
55             throw new ImageFormatError("File: " + file
56                     + " is not a valid image.");
57
58         }
59
60         return lastLoadedBufferedImage;
61     }
62
63     public static File getLayoutIndexFile(final Layout layout,
64                                           final File directoryToIndex) {
65
66         final String indexFilePath = directoryToIndex.getAbsolutePath()
67                 + "/index" + layout.getFileNameSuffix() + ".html";
68
69         return new File(indexFilePath);
70     }
71
72     public static HashSet<Layout> getLayouts() {
73         final HashSet<Layout> layouts = new HashSet<>();
74         layouts.add(new MixedLayout());
75         return layouts;
76     }
77
78     public static String getStringCrcAsHex(final String input) {
79
80         // create a new CRC-calculating object
81         final CRC32 crc = new CRC32();
82
83         // loop, calculating CRC for each byte of the string
84         // There is no CRC16.update(byte[]) method.
85         for (final byte b : input.getBytes())
86             crc.update(b);
87
88         // note use crc.value, not crc.getValue()
89
90         return Integer.toHexString((int) crc.getValue())
91                 .toUpperCase();
92     }
93
94     public static File getThumbnailsDirectory(final File directoryToIndex) {
95         return new File(getThumbnailsDirectoryPath(directoryToIndex));
96     }
97
98     public static String getThumbnailsDirectoryPath(final File directoryToIndex) {
99         return directoryToIndex.getAbsolutePath() + "/"
100                 + Constants.THUMBNAILS_DIRECTORY_NAME + "/";
101     }
102
103     public static boolean isMevizGeneratedIndexFile(final File indexFile)
104             throws IOException {
105
106         boolean isMevizFile = false;
107
108         final FileReader fileReader = new FileReader(indexFile);
109         final BufferedReader reader = new BufferedReader(fileReader);
110
111         parseFile:
112         {
113             while (true) {
114                 final String line = reader.readLine();
115
116                 if (line == null)
117                     break parseFile;
118
119                 if (line.contains(Constants.HTML_MAGIC_STRING)) {
120                     isMevizFile = true;
121                     break parseFile;
122                 }
123             }
124         }
125
126         reader.close();
127         fileReader.close();
128         return isMevizFile;
129     }
130
131     /**
132      * TODO: URL path component is encoded differently from URL query parameter.
133      * Also some URL encoding might work for HTML on local filesystem, while other
134      * stuff works for web. Things must be cleared up here. Currently they are mixed and
135      * hacked together.
136      */
137     public static String urlEncode(String string) {
138         if (string.startsWith("./"))
139             string = string.substring(2);
140
141         // TODO: get rid of UrlParamEncoder.
142         return UrlParamEncoder.encode(string);
143         //
144 //        try {
145 //            return URLEncoder.encode(string, UTF_8).replace("+", "%20");
146 //        } catch (UnsupportedEncodingException e) {
147 //            throw new RuntimeException(e);
148 //        }
149     }
150
151     public static ImageFrame[] readGIF(ImageReader reader) throws IOException {
152         ArrayList<ImageFrame> frames = new ArrayList<ImageFrame>(2);
153
154         int width = -1;
155         int height = -1;
156
157         IIOMetadata metadata = reader.getStreamMetadata();
158         if (metadata != null) {
159             IIOMetadataNode globalRoot = (IIOMetadataNode) metadata.getAsTree(metadata.getNativeMetadataFormatName());
160
161             NodeList globalScreenDescriptor = globalRoot.getElementsByTagName("LogicalScreenDescriptor");
162
163             if (globalScreenDescriptor != null && globalScreenDescriptor.getLength() > 0) {
164                 IIOMetadataNode screenDescriptor = (IIOMetadataNode) globalScreenDescriptor.item(0);
165
166                 if (screenDescriptor != null) {
167                     width = Integer.parseInt(screenDescriptor.getAttribute("logicalScreenWidth"));
168                     height = Integer.parseInt(screenDescriptor.getAttribute("logicalScreenHeight"));
169                 }
170             }
171         }
172
173         BufferedImage master = null;
174         Graphics2D masterGraphics = null;
175
176         for (int frameIndex = 0;; frameIndex++) {
177             BufferedImage image;
178             try {
179                 image = reader.read(frameIndex);
180             } catch (IndexOutOfBoundsException io) {
181                 break;
182             }
183
184             if (width == -1 || height == -1) {
185                 width = image.getWidth();
186                 height = image.getHeight();
187             }
188
189             IIOMetadataNode root = (IIOMetadataNode) reader.getImageMetadata(frameIndex).getAsTree("javax_imageio_gif_image_1.0");
190             IIOMetadataNode gce = (IIOMetadataNode) root.getElementsByTagName("GraphicControlExtension").item(0);
191             int delay = Integer.valueOf(gce.getAttribute("delayTime"));
192             String disposal = gce.getAttribute("disposalMethod");
193
194             int x = 0;
195             int y = 0;
196
197             if (master == null) {
198                 master = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
199                 masterGraphics = master.createGraphics();
200                 masterGraphics.setBackground(new Color(0, 0, 0, 0));
201             } else {
202                 NodeList children = root.getChildNodes();
203                 for (int nodeIndex = 0; nodeIndex < children.getLength(); nodeIndex++) {
204                     Node nodeItem = children.item(nodeIndex);
205                     if (nodeItem.getNodeName().equals("ImageDescriptor")) {
206                         NamedNodeMap map = nodeItem.getAttributes();
207                         x = Integer.valueOf(map.getNamedItem("imageLeftPosition").getNodeValue());
208                         y = Integer.valueOf(map.getNamedItem("imageTopPosition").getNodeValue());
209                     }
210                 }
211             }
212             masterGraphics.drawImage(image, x, y, null);
213
214             BufferedImage copy = new BufferedImage(master.getColorModel(), master.copyData(null), master.isAlphaPremultiplied(), null);
215             frames.add(new ImageFrame(copy, delay, disposal));
216
217             if (disposal.equals("restoreToPrevious")) {
218                 BufferedImage from = null;
219                 for (int i = frameIndex - 1; i >= 0; i--) {
220                     if (!frames.get(i).getDisposal().equals("restoreToPrevious") || frameIndex == 0) {
221                         from = frames.get(i).image;
222                         break;
223                     }
224                 }
225
226                 master = new BufferedImage(from.getColorModel(), from.copyData(null), from.isAlphaPremultiplied(), null);
227                 masterGraphics = master.createGraphics();
228                 masterGraphics.setBackground(new Color(0, 0, 0, 0));
229             } else if (disposal.equals("restoreToBackgroundColor")) {
230                 masterGraphics.clearRect(x, y, image.getWidth(), image.getHeight());
231             }
232         }
233         reader.dispose();
234
235         return frames.toArray(new ImageFrame[frames.size()]);
236     }
237
238     public static class ImageFrame {
239         private final int delay;
240         public BufferedImage image;
241         private final String disposal;
242
243         public ImageFrame(BufferedImage image, int delay, String disposal) {
244             this.image = image;
245             this.delay = delay;
246             this.disposal = disposal;
247         }
248
249         public int getDelay() {
250             return delay;
251         }
252
253         public String getDisposal() {
254             return disposal;
255         }
256     }
257 }