~ubuntu-branches/ubuntu/trusty/png-sixlegs/trusty

« back to all changes in this revision

Viewing changes to javapng-2.0/src/examples/com/sixlegs/png/examples/ExtractFrames.java

  • Committer: Package Import Robot
  • Author(s): Ying-Chun Liu (PaulLiu)
  • Date: 2012-05-18 03:14:11 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20120518031411-b1iutqskocxf6t5j
Tags: 2.0-1
* New upstream release
* Update debian/watch to match the current upstream link
* Update README.Debian for repack information
* debian/rules: modify to generate javadoc

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
com.sixlegs.png - Java package to read and display PNG images
 
3
Copyright (C) 1998-2006 Chris Nokleberg
 
4
 
 
5
This program is free software; you can redistribute it and/or modify
 
6
it under the terms of the GNU General Public License as published by
 
7
the Free Software Foundation; either version 2 of the License, or
 
8
(at your option) any later version.
 
9
 
 
10
This program is distributed in the hope that it will be useful,
 
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
GNU General Public License for more details.
 
14
 
 
15
You should have received a copy of the GNU General Public License
 
16
along with this program; if not, write to the Free Software
 
17
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
18
 
 
19
Linking this library statically or dynamically with other modules is
 
20
making a combined work based on this library.  Thus, the terms and
 
21
conditions of the GNU General Public License cover the whole
 
22
combination.
 
23
 
 
24
As a special exception, the copyright holders of this library give you
 
25
permission to link this library with independent modules to produce an
 
26
executable, regardless of the license terms of these independent
 
27
modules, and to copy and distribute the resulting executable under
 
28
terms of your choice, provided that you also meet, for each linked
 
29
independent module, the terms and conditions of the license of that
 
30
module.  An independent module is a module which is not derived from
 
31
or based on this library.  If you modify this library, you may extend
 
32
this exception to your version of the library, but you are not
 
33
obligated to do so.  If you do not wish to do so, delete this
 
34
exception statement from your version.
 
35
*/
 
36
 
 
37
package com.sixlegs.png.examples;
 
38
 
 
39
import com.sixlegs.png.*;
 
40
import java.awt.image.BufferedImage;
 
41
import java.io.*;
 
42
import java.text.DecimalFormat;
 
43
import java.util.*;
 
44
 
 
45
public class ExtractFrames
 
46
{
 
47
    public static void main(String[] args)
 
48
    throws Exception
 
49
    {
 
50
        DecimalFormat fmt = new DecimalFormat("000");
 
51
        for (int i = 0; i < args.length; i++) {
 
52
            File in = new File(args[i]);
 
53
            PngSplitter splitter = new PngSplitter(in);
 
54
            int numFrames = splitter.getNumFrames();
 
55
            for (int frame = 0; frame < numFrames; frame++) {
 
56
                File out = new File(fmt.format(frame) + "-" + in.getName());
 
57
                if (out.exists()) {
 
58
                    System.err.println("File exists, skipping: " + out);
 
59
                    break;
 
60
                }
 
61
                System.err.println("Writing " + out);
 
62
                splitter.write(out, frame);
 
63
            }
 
64
        }
 
65
    }
 
66
 
 
67
    private static class PngSplitter
 
68
    extends PngImage
 
69
    {
 
70
        private static final PngConfig CONFIG = new PngConfig.Builder()
 
71
            .warningsFatal(true)
 
72
            .readLimit(PngConfig.READ_EXCEPT_DATA)
 
73
            .build();
 
74
 
 
75
        private File in;
 
76
        private List<Chunk> commonBefore = new ArrayList<Chunk>();
 
77
        private List<Chunk> commonAfter = new ArrayList<Chunk>();
 
78
        private List<Chunk> data = new ArrayList<Chunk>();
 
79
        private List<Chunk> bySequence = new ArrayList<Chunk>();
 
80
        private List<List<Chunk>> byFrame = new ArrayList<List<Chunk>>();
 
81
        private byte[] buf = new byte[0x2000];
 
82
 
 
83
        public PngSplitter(File in)
 
84
        throws IOException
 
85
        {
 
86
            super(CONFIG);
 
87
            this.in = in;
 
88
            read(in);
 
89
            byFrame.add(data);
 
90
            List<Chunk> cur = null;
 
91
            for (Chunk chunk : bySequence) {
 
92
                if (chunk.type == AnimatedPngImage.fcTL) {
 
93
                    byFrame.add(cur = new ArrayList<Chunk>());
 
94
                } else {
 
95
                    cur.add(chunk);
 
96
                }
 
97
            }
 
98
        }
 
99
 
 
100
        public int getNumFrames()
 
101
        {
 
102
            return byFrame.size();
 
103
        }
 
104
 
 
105
        public void write(File file, int index)
 
106
        throws IOException
 
107
        {
 
108
            DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
 
109
            ChunkWriter cw = new ChunkWriter();
 
110
            try {
 
111
                out.writeLong(PngConstants.SIGNATURE);
 
112
                echo(out, commonBefore);
 
113
                echo(out, byFrame.get(index));
 
114
                echo(out, commonAfter);
 
115
            } finally {
 
116
                out.close();
 
117
            }
 
118
        }
 
119
 
 
120
        private void echo(DataOutput out, List<Chunk> chunks)
 
121
        throws IOException
 
122
        {
 
123
            ChunkWriter cw = new ChunkWriter();
 
124
            RandomAccessFile rf = new RandomAccessFile(in, "r");
 
125
            try {
 
126
                for (Chunk chunk : chunks) {
 
127
                    cw.start(chunk.type);
 
128
                    rf.seek(chunk.offset);
 
129
                    rf.readFully(buf, 0, chunk.length);
 
130
                    cw.write(buf, 0, chunk.length);
 
131
                    cw.close();
 
132
                    cw.finish(out);
 
133
                }
 
134
            } finally {
 
135
                rf.close();
 
136
            }
 
137
        }
 
138
 
 
139
        protected void readChunk(int type, DataInput in, long offset, int length)
 
140
        throws IOException
 
141
        {
 
142
            Chunk chunk = new Chunk();
 
143
            chunk.type = type;
 
144
            chunk.offset = offset;
 
145
            chunk.length = length;
 
146
            switch (type) {
 
147
            case PngConstants.IDAT:
 
148
                data.add(chunk);
 
149
                break;
 
150
            case AnimatedPngImage.acTL:
 
151
                break;
 
152
            case AnimatedPngImage.fdAT:
 
153
                chunk.type = PngConstants.IDAT;
 
154
                chunk.offset += 4;
 
155
                chunk.length -= 4;
 
156
                /* fall-through */
 
157
            case AnimatedPngImage.fcTL:
 
158
                int seq = in.readInt();
 
159
                while (bySequence.size() <= seq)
 
160
                    bySequence.add(null);
 
161
                bySequence.set(seq, chunk);
 
162
                break;
 
163
            default:
 
164
                (data.isEmpty() ? commonBefore : commonAfter).add(chunk);
 
165
                super.readChunk(type, in, offset, length);
 
166
            }
 
167
        }
 
168
    }
 
169
 
 
170
    private static class Chunk
 
171
    {
 
172
        int type;
 
173
        long offset;
 
174
        int length;
 
175
 
 
176
        public String toString()
 
177
        {
 
178
            return PngConstants.getChunkName(type);
 
179
        }
 
180
    }
 
181
}