Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

99
LINES

< > BotCompany Repo | #1036321 // GIFRewriter - e.g. for scaling GIFs [dev.]

JavaX fragment (include) [tags: use-pretranspiled]

Transpiled version (9899L) is out of date.

1  
import javax.imageio.metadata.*;
2  
import javax.imageio.stream.*;
3  
4  
sclass GIFRewriter {
5  
  settable File inputFile;
6  
  settable bool debug;
7  
  byte[] data;
8  
  int numImages;
9  
  ImageReader reader;
10  
  S metaFormatName;
11  
  
12  
  Image get() ctex {
13  
    if (!inputFile.exists()) null;
14  
15  
    // Load anything but GIF the normal way
16  
    if (!isGIF(inputFile))
17  
      ret ImageIO.read(inputFile);
18  
    
19  
    reader = ImageIO.getImageReadersByFormatName("gif").next();
20  
    // Give it the stream to decode from
21  
    reader.setInput(ImageIO.createImageInputStream(inputFile));
22  
  
23  
    numImages = reader.getNumImages(true);
24  
  
25  
    // Get 'metaFormatName'. Need first frame for that.
26  
    IIOMetadata imageMetaData = reader.getImageMetadata(0);
27  
    metaFormatName = imageMetaData.getNativeMetadataFormatName();
28  
  
29  
    // Prepare streams for image encoding
30  
    new ByteArrayOutputStream baoStream;
31  
    
32  
    {
33  
      temp ImageOutputStream ios = ImageIO.createImageOutputStream(baoStream);
34  
      // Get GIF writer that's compatible with reader
35  
      ImageWriter writer = ImageIO.getImageWriter(reader);
36  
      // Give it the stream to encode to
37  
      writer.setOutput(ios);
38  
39  
      writer.prepareWriteSequence(null);
40  
41  
      for i to numImages: {
42  
        // Get input image
43  
        BufferedImage frameIn = reader.read(i);
44  
        
45  
        frameIn = transformImage(frameIn);
46  
47  
        // Get input metadata
48  
        IIOMetadataNode root = (IIOMetadataNode)reader.getImageMetadata(i).getAsTree(metaFormatName);
49  
50  
        // Find GraphicControlExtension node
51  
        int nNodes = root.getLength();
52  
        for j to nNodes: {
53  
          org.w3c.dom.Node node = root.item(j);
54  
          if (node.getNodeName().equalsIgnoreCase("GraphicControlExtension")) {
55  
            cast node to IIOMetadataNode;
56  
            if (parseInt(node.getAttribute("delayTime")) == 0)
57  
              node.setAttribute("delayTime", "10");
58  
          }
59  
  
60  
          // Create output metadata
61  
          IIOMetadata metadata = writer.getDefaultImageMetadata(new ImageTypeSpecifier(frameIn), null);
62  
          // Copy metadata to output metadata
63  
          metadata.setFromTree(metadata.getNativeMetadataFormatName(), root);
64  
  
65  
          // Create output image
66  
          IIOImage frameOut = new IIOImage(frameIn, null, metadata);
67  
68  
          // Encode output image
69  
          writer.writeToSequence(frameOut, writer.getDefaultWriteParam());
70  
        }
71  
  
72  
        writer.endWriteSequence();
73  
      }
74  
  
75  
      // Create image using encoded data
76  
      data = baoStream.toByteArray();
77  
      if (debug) print("Data size: " + l(data));
78  
      ret Toolkit.getDefaultToolkit().createImage(data);
79  
    }
80  
  }
81  
  
82  
  bool animationSpeedIsBugged() ctex {
83  
    for i to numImages: {
84  
      IIOMetadataNode root = cast reader.getImageMetadata(i).getAsTree(metaFormatName);
85  
86  
      int nNodes = root.getLength();
87  
      for j to nNodes: {
88  
        org.w3c.dom.Node node = root.item(j);
89  
        if (node.getNodeName().equalsIgnoreCase("GraphicControlExtension")) {
90  
          cast node to IIOMetadataNode;
91  
          if (parseInt(node.getAttribute("delayTime")) == 0) true;
92  
        }
93  
      }
94  
    }
95  
    false;
96  
  }
97  
  
98  
  swappable BufferedImage transformImage(BufferedImage image) { ret image; }
99  
}

Author comment

Began life as a copy of #1015991

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1036321
Snippet name: GIFRewriter - e.g. for scaling GIFs [dev.]
Eternal ID of this version: #1036321/11
Text MD5: bc211f0041d480e167c7a62759bfef7e
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-11-16 15:57:17
Source code size: 3283 bytes / 99 lines
Pitched / IR pitched: No / No
Views / Downloads: 133 / 231
Version history: 10 change(s)
Referenced in: [show references]