1 | scope loadBufferedImageFixingGIFs. |
2 | |
3 | import javax.imageio.metadata.*; |
4 | import javax.imageio.stream.*; |
5 | |
6 | sbool #debug; |
7 | static ThreadLocal<Var<byte[]>> #output = new ThreadLocal; |
8 | |
9 | static Image loadBufferedImageFixingGIFs(File file) ctex { |
10 | if (!file.exists()) null; |
11 | |
12 | // Load anything but GIF the normal way |
13 | if (!isGIF(file)) |
14 | ret ImageIO.read(file); |
15 | |
16 | if (debug) print(__scope + ": checking gif"); |
17 | |
18 | // Get GIF reader |
19 | ImageReader reader = ImageIO.getImageReadersByFormatName("gif").next(); |
20 | // Give it the stream to decode from |
21 | reader.setInput(ImageIO.createImageInputStream(file)); |
22 | |
23 | int numImages = reader.getNumImages(true); |
24 | |
25 | // Get 'metaFormatName'. Need first frame for that. |
26 | IIOMetadata imageMetaData = reader.getImageMetadata(0); |
27 | String metaFormatName = imageMetaData.getNativeMetadataFormatName(); |
28 | |
29 | // Find out if GIF is bugged |
30 | boolean foundBug = false; |
31 | for (int i = 0; i < numImages && !foundBug; i++) { |
32 | // Get metadata |
33 | IIOMetadataNode root = (IIOMetadataNode)reader.getImageMetadata(i).getAsTree(metaFormatName); |
34 | |
35 | // Find GraphicControlExtension node |
36 | int nNodes = root.getLength(); |
37 | for (int j = 0; j < nNodes; j++) { |
38 | org.w3c.dom.Node node = root.item(j); |
39 | if (node.getNodeName().equalsIgnoreCase("GraphicControlExtension")) { |
40 | // Get delay value |
41 | String delay = ((IIOMetadataNode)node).getAttribute("delayTime"); |
42 | |
43 | // Check if delay is bugged |
44 | if (Integer.parseInt(delay) == 0) { |
45 | foundBug = true; |
46 | } |
47 | |
48 | break; |
49 | } |
50 | } |
51 | } |
52 | |
53 | if (debug) print(__scope + ": " + f2s(file) + " foundBug=" + foundBug); |
54 | |
55 | // Load non-bugged GIF the normal way |
56 | Image image; |
57 | if (!foundBug) { |
58 | image = Toolkit.getDefaultToolkit().createImage(f2s(file)); |
59 | } else { |
60 | // Prepare streams for image encoding |
61 | ByteArrayOutputStream baoStream = new ByteArrayOutputStream(); |
62 | { |
63 | temp ImageOutputStream ios = ImageIO.createImageOutputStream(baoStream); |
64 | // Get GIF writer that's compatible with reader |
65 | ImageWriter writer = ImageIO.getImageWriter(reader); |
66 | // Give it the stream to encode to |
67 | writer.setOutput(ios); |
68 | |
69 | writer.prepareWriteSequence(null); |
70 | |
71 | for (int i = 0; i < numImages; i++) { |
72 | // Get input image |
73 | BufferedImage frameIn = reader.read(i); |
74 | |
75 | // Get input metadata |
76 | IIOMetadataNode root = (IIOMetadataNode)reader.getImageMetadata(i).getAsTree(metaFormatName); |
77 | |
78 | // Find GraphicControlExtension node |
79 | int nNodes = root.getLength(); |
80 | for (int j = 0; j < nNodes; j++) { |
81 | org.w3c.dom.Node node = root.item(j); |
82 | if (node.getNodeName().equalsIgnoreCase("GraphicControlExtension")) { |
83 | // Get delay value |
84 | String delay = ((IIOMetadataNode)node).getAttribute("delayTime"); |
85 | |
86 | // Check if delay is bugged |
87 | if (Integer.parseInt(delay) == 0) { |
88 | // Overwrite with a valid delay value |
89 | ((IIOMetadataNode)node).setAttribute("delayTime", "10"); |
90 | } |
91 | |
92 | break; |
93 | } |
94 | } |
95 | |
96 | // Create output metadata |
97 | IIOMetadata metadata = writer.getDefaultImageMetadata(new ImageTypeSpecifier(frameIn), null); |
98 | // Copy metadata to output metadata |
99 | metadata.setFromTree(metadata.getNativeMetadataFormatName(), root); |
100 | |
101 | // Create output image |
102 | IIOImage frameOut = new IIOImage(frameIn, null, metadata); |
103 | |
104 | // Encode output image |
105 | writer.writeToSequence(frameOut, writer.getDefaultWriteParam()); |
106 | } |
107 | |
108 | writer.endWriteSequence(); |
109 | } |
110 | |
111 | // Create image using encoded data |
112 | byte[] data = baoStream.toByteArray(); |
113 | setVar(output!, data); |
114 | if (debug) print("Data size: " + l(data)); |
115 | image = Toolkit.getDefaultToolkit().createImage(data); |
116 | } |
117 | |
118 | ret image; |
119 | } |
120 | |
121 | end scope |
download show line numbers debug dex old transpilations
Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mowyntqkapby, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1015991 |
Snippet name: | loadBufferedImageFixingGIFs |
Eternal ID of this version: | #1015991/14 |
Text MD5: | 6b6ec87f2c3a746ecbba7a025ba65883 |
Author: | stefan |
Category: | javax / gui |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2018-06-16 16:37:09 |
Source code size: | 4118 bytes / 121 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 439 / 532 |
Version history: | 13 change(s) |
Referenced in: | [show references] |