[Patchanim-commit] SF.net SVN: patchanim: [149] trunk/patchanim/src/com/mebigfatguy
Brought to you by:
dbrosius
|
From: <dbr...@us...> - 2008-02-10 17:34:21
|
Revision: 149
http://patchanim.svn.sourceforge.net/patchanim/?rev=149&view=rev
Author: dbrosius
Date: 2008-02-10 09:34:27 -0800 (Sun, 10 Feb 2008)
Log Message:
-----------
break out Chunk and PrmStream to separate class, remove references to MPeg, and stub in Mng support
Modified Paths:
--------------
trunk/patchanim/src/com/mebigfatguy/encoders/APngEncoder.java
trunk/patchanim/src/com/mebigfatguy/patchanim/ExportType.java
trunk/patchanim/src/com/mebigfatguy/patchanim/gui/JPatchAnimFrame.java
trunk/patchanim/src/com/mebigfatguy/patchanim/io/PatchExporter.java
trunk/patchanim/src/com/mebigfatguy/patchanim/main/PatchAnimBundle.java
trunk/patchanim/src/com/mebigfatguy/patchanim/resources.properties
Added Paths:
-----------
trunk/patchanim/src/com/mebigfatguy/encoders/Chunk.java
trunk/patchanim/src/com/mebigfatguy/encoders/MngEncoder.java
trunk/patchanim/src/com/mebigfatguy/encoders/PngStream.java
Modified: trunk/patchanim/src/com/mebigfatguy/encoders/APngEncoder.java
===================================================================
--- trunk/patchanim/src/com/mebigfatguy/encoders/APngEncoder.java 2008-02-10 17:26:23 UTC (rev 148)
+++ trunk/patchanim/src/com/mebigfatguy/encoders/APngEncoder.java 2008-02-10 17:34:27 UTC (rev 149)
@@ -16,7 +16,7 @@
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
-package com.mebigfatguy.apng;
+package com.mebigfatguy.encoders;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
@@ -25,7 +25,6 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
-import java.util.zip.CRC32;
import javax.imageio.ImageIO;
@@ -122,7 +121,7 @@
pStrm = new PngStream(baos.toByteArray());
baos.reset();
}
- Chunk chunk = pStrm.readNextChunk(pStrm);
+ Chunk chunk = pStrm.readNextChunk();
while (chunk != null) {
switch (chunk.type) {
case IHDR:
@@ -164,7 +163,7 @@
}
break;
}
- chunk = pStrm.readNextChunk(pStrm);
+ chunk = pStrm.readNextChunk();
}
processedFirstFrame = true;
@@ -175,81 +174,4 @@
}
}
-
- static class PngStream {
- public PngStream(byte[] pngData) {
- pos = HEADER.length;
- data = pngData;
- }
-
- public Chunk readNextChunk(PngStream pStrm) {
- if (pos >= data.length)
- return null;
-
- Chunk c = new Chunk(readNextInt(), readNextInt());
-
- System.arraycopy(data, pos, c.data, 0, c.length);
- pos += c.length;
- c.crc = readNextInt();
- return c;
- }
-
- private int readNextInt() {
- int val = 0;
- for (int i = 0; i < 4; i++) {
- val <<= 8;
- val |= (0x00FF & data[pos++]);
- }
- return val;
- }
-
- public int pos;
- public byte[] data;
- }
-
- static class Chunk {
-
- public int length;
- public int type;
- public byte[] data;
- public int crc;
-
- public Chunk(int len, int chunkType) {
- length = len;
- type = chunkType;
- data = new byte[length];
- crc = 0;
- }
-
- public void write(DataOutputStream out) throws IOException {
- out.writeInt(length);
- out.writeInt(type);
- out.write(data);
- out.writeInt(crc);
- }
-
- public void injectInt(int offset, int value) {
- data[offset++] = (byte)(value >> 24 & 0x00FF);
- data[offset++] = (byte)(value >> 16 & 0x00FF);
- data[offset++] = (byte)(value >> 8 & 0x00FF);
- data[offset] = (byte)(value & 0x00FF);
- }
-
- public void injectShort(int offset, int value) {
- data[offset++] = (byte)(value >> 8 & 0x00FF);
- data[offset] = (byte)(value & 0x00FF);
- }
-
- public void calcCRC() {
- CRC32 crc32 = new CRC32();
- byte[] typeBytes = new byte[4];
- typeBytes[0] = (byte)(type >> 24 & 0x00FF);
- typeBytes[1] = (byte)(type >> 16 & 0x00FF);
- typeBytes[2] = (byte)(type >> 8 & 0x00FF);
- typeBytes[3] = (byte)(type & 0x00FF);
- crc32.update(typeBytes);
- crc32.update(data);
- crc = (int)crc32.getValue();
- }
- }
}
Added: trunk/patchanim/src/com/mebigfatguy/encoders/Chunk.java
===================================================================
--- trunk/patchanim/src/com/mebigfatguy/encoders/Chunk.java (rev 0)
+++ trunk/patchanim/src/com/mebigfatguy/encoders/Chunk.java 2008-02-10 17:34:27 UTC (rev 149)
@@ -0,0 +1,69 @@
+/*
+ * patchanim - A bezier surface patch color blend gif builder
+ * Copyright (C) 2008 Dave Brosius
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+package com.mebigfatguy.encoders;
+
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.util.zip.CRC32;
+
+public class Chunk {
+
+ public int length;
+ public int type;
+ public byte[] data;
+ public int crc;
+
+ public Chunk(int len, int chunkType) {
+ length = len;
+ type = chunkType;
+ data = new byte[length];
+ crc = 0;
+ }
+
+ public void write(DataOutputStream out) throws IOException {
+ out.writeInt(length);
+ out.writeInt(type);
+ out.write(data);
+ out.writeInt(crc);
+ }
+
+ public void injectInt(int offset, int value) {
+ data[offset++] = (byte)(value >> 24 & 0x00FF);
+ data[offset++] = (byte)(value >> 16 & 0x00FF);
+ data[offset++] = (byte)(value >> 8 & 0x00FF);
+ data[offset] = (byte)(value & 0x00FF);
+ }
+
+ public void injectShort(int offset, int value) {
+ data[offset++] = (byte)(value >> 8 & 0x00FF);
+ data[offset] = (byte)(value & 0x00FF);
+ }
+
+ public void calcCRC() {
+ CRC32 crc32 = new CRC32();
+ byte[] typeBytes = new byte[4];
+ typeBytes[0] = (byte)(type >> 24 & 0x00FF);
+ typeBytes[1] = (byte)(type >> 16 & 0x00FF);
+ typeBytes[2] = (byte)(type >> 8 & 0x00FF);
+ typeBytes[3] = (byte)(type & 0x00FF);
+ crc32.update(typeBytes);
+ crc32.update(data);
+ crc = (int)crc32.getValue();
+ }
+}
\ No newline at end of file
Property changes on: trunk/patchanim/src/com/mebigfatguy/encoders/Chunk.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
Added: trunk/patchanim/src/com/mebigfatguy/encoders/MngEncoder.java
===================================================================
--- trunk/patchanim/src/com/mebigfatguy/encoders/MngEncoder.java (rev 0)
+++ trunk/patchanim/src/com/mebigfatguy/encoders/MngEncoder.java 2008-02-10 17:34:27 UTC (rev 149)
@@ -0,0 +1,148 @@
+/*
+ * patchanim - A bezier surface patch color blend gif builder
+ * Copyright (C) 2008 Dave Brosius
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+package com.mebigfatguy.encoders;
+
+import java.awt.image.BufferedImage;
+import java.io.BufferedOutputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.DataOutputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+
+import javax.imageio.ImageIO;
+
+import com.mebigfatguy.patchanim.ExportType;
+
+/**
+ * generates mng files by simply relying on the built in png encoder of ImageIO
+ * and sewing together multiple chunks from the underlying pngs.
+ */
+public class MngEncoder {
+ private static final byte[] HEADER = new byte[] { -118, 77, 78, 71, 13, 10, 26, 10 };
+ private static final int IHDR = 0x49484452;
+ private static final int IDAT = 0x49444154;
+ private static final int IEND = 0x49454E44;
+ private static final int MHDR = 0;
+ private static final int MEND = 0;
+
+ private DataOutputStream out = null;
+ private boolean started = false;
+ private boolean closeStream = false;
+ private boolean headerWritten = false;
+ private boolean repeatInfinite = false;
+ private int delay = 100;
+ private int frameCount = 1;
+ private int seqNum = 0;
+ private boolean processedFirstFrame = false;
+
+ public void setDelay(int ms) {
+ delay = ms;
+ }
+
+ public void setRepeat(boolean infinite) {
+ repeatInfinite = infinite;
+ }
+
+ public void setNumFrames(int frames) {
+ frameCount = frames;
+ }
+
+ public boolean start(OutputStream os) {
+ if (os == null)
+ return false;
+ boolean ok = true;
+ closeStream = false;
+ out = new DataOutputStream(os);
+ try {
+ out.write(HEADER);
+ } catch (IOException e) {
+ ok = false;
+ }
+ return started = ok;
+ }
+
+ public boolean start(String file) {
+ boolean ok = true;
+ try {
+ out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
+ ok = start(out);
+ closeStream = true;
+ } catch (IOException e) {
+ ok = false;
+ }
+ return started = ok;
+ }
+
+ public boolean finish() {
+ if (!started)
+ return false;
+ try {
+ Chunk iendChunk = new Chunk(0, MEND);
+ iendChunk.calcCRC();
+ iendChunk.write(out);
+ if (closeStream)
+ out.close();
+ } catch (IOException ioe) {
+ } finally {
+ out = null;
+ started = false;
+ }
+
+ return true;
+ }
+
+ public boolean addFrame(BufferedImage im) {
+ if ((im == null) || !started)
+ return false;
+ try {
+ PngStream pStrm;
+ boolean sawIDAT = false;
+ {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream(im.getHeight() * im.getWidth()); //a conservative estimate
+ ImageIO.write(im, ExportType.Pngs.getExtension(), baos);
+ pStrm = new PngStream(baos.toByteArray());
+ baos.reset();
+ }
+ Chunk chunk = pStrm.readNextChunk();
+ while (chunk != null) {
+ switch (chunk.type) {
+ case IHDR:
+ if (!headerWritten) {
+ Chunk mhdrChunk = new Chunk(28, MHDR);
+ mhdrChunk.calcCRC();
+ mhdrChunk.write(out);
+ }
+ break;
+
+ case IDAT:
+ break;
+ }
+ chunk = pStrm.readNextChunk();
+ }
+ processedFirstFrame = true;
+
+ return true;
+ } catch (IOException ioe) {
+ return false;
+ } finally {
+
+ }
+ }
+}
Property changes on: trunk/patchanim/src/com/mebigfatguy/encoders/MngEncoder.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
Added: trunk/patchanim/src/com/mebigfatguy/encoders/PngStream.java
===================================================================
--- trunk/patchanim/src/com/mebigfatguy/encoders/PngStream.java (rev 0)
+++ trunk/patchanim/src/com/mebigfatguy/encoders/PngStream.java 2008-02-10 17:34:27 UTC (rev 149)
@@ -0,0 +1,52 @@
+/*
+ * patchanim - A bezier surface patch color blend gif builder
+ * Copyright (C) 2008 Dave Brosius
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+package com.mebigfatguy.encoders;
+
+public class PngStream {
+ private static final int HEADERLENGTH = 8;
+ public int pos;
+ public byte[] data;
+
+ public PngStream(byte[] pngData) {
+ pos = HEADERLENGTH;
+ data = pngData;
+ }
+
+ public Chunk readNextChunk() {
+ if (pos >= data.length)
+ return null;
+
+ Chunk c = new Chunk(readNextInt(), readNextInt());
+
+ System.arraycopy(data, pos, c.data, 0, c.length);
+ pos += c.length;
+ c.crc = readNextInt();
+ return c;
+ }
+
+ private int readNextInt() {
+ int val = 0;
+ for (int i = 0; i < 4; i++) {
+ val <<= 8;
+ val |= (0x00FF & data[pos++]);
+ }
+ return val;
+ }
+
+}
Property changes on: trunk/patchanim/src/com/mebigfatguy/encoders/PngStream.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
Modified: trunk/patchanim/src/com/mebigfatguy/patchanim/ExportType.java
===================================================================
--- trunk/patchanim/src/com/mebigfatguy/patchanim/ExportType.java 2008-02-10 17:26:23 UTC (rev 148)
+++ trunk/patchanim/src/com/mebigfatguy/patchanim/ExportType.java 2008-02-10 17:34:27 UTC (rev 149)
@@ -10,7 +10,7 @@
* <li><b>Gifs</b> a series of Gif files in a directory</li>
* <li><b>AnimatedGif</b> an animated gif file</li>
* <li><b>AnimatedPng</b> an animated png file</li>
- * <li><b>Mpeg</b> an animated mpeg file</li>
+ * <li><b>AnimatedMng</b> an animated mng file</li>
* </ul>
*/
public enum ExportType {
@@ -19,7 +19,7 @@
Gifs("gif", true, PatchAnimBundle.GIFSERIESFILTER),
AnimatedGif("gif", false, PatchAnimBundle.ANIMATEDGIFFILTER),
AnimatedPng("png", false, PatchAnimBundle.ANIMATEDPNGFILTER),
- MPeg("mpeg", false, PatchAnimBundle.MPEGFILTER);
+ AnimatedMng("mng", false, PatchAnimBundle.ANIMATEDMNGFILTER);
private String ext;
private boolean multi;
Modified: trunk/patchanim/src/com/mebigfatguy/patchanim/gui/JPatchAnimFrame.java
===================================================================
--- trunk/patchanim/src/com/mebigfatguy/patchanim/gui/JPatchAnimFrame.java 2008-02-10 17:26:23 UTC (rev 148)
+++ trunk/patchanim/src/com/mebigfatguy/patchanim/gui/JPatchAnimFrame.java 2008-02-10 17:34:27 UTC (rev 149)
@@ -61,6 +61,7 @@
private JMenuItem exportGifsItem;
private JMenuItem exportAnimatedGifItem;
private JMenuItem exportAnimatedPngItem;
+ private JMenuItem exportAnimatedMngItem;
private JMenuItem quitItem;
private PatchAnimDocument document;
private File documentLocation;
@@ -108,12 +109,15 @@
exportGifsItem = new JMenuItem(rb.getString(PatchAnimBundle.GIFSERIES));
exportAnimatedGifItem = new JMenuItem(rb.getString(PatchAnimBundle.ANIMATEDGIF));
exportAnimatedPngItem = new JMenuItem(rb.getString(PatchAnimBundle.ANIMATEDPNG));
+ exportAnimatedMngItem = new JMenuItem(rb.getString(PatchAnimBundle.ANIMATEDMNG));
+ exportAnimatedMngItem.setEnabled(false);
exportMenu.add(exportJpgsItem);
exportMenu.add(exportPngsItem);
exportMenu.add(exportGifsItem);
exportMenu.addSeparator();
exportMenu.add(exportAnimatedGifItem);
exportMenu.add(exportAnimatedPngItem);
+ exportMenu.add(exportAnimatedMngItem);
fileMenu.add(exportMenu);
fileMenu.addSeparator();
quitItem = new JMenuItem(rb.getString(PatchAnimBundle.QUIT));
@@ -245,6 +249,12 @@
}
});
+ exportAnimatedMngItem.addActionListener(new ActionListener() {
+ public void actionPerformed(ActionEvent ae) {
+ export(ExportType.AnimatedMng);
+ }
+ });
+
quitItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
try {
Modified: trunk/patchanim/src/com/mebigfatguy/patchanim/io/PatchExporter.java
===================================================================
--- trunk/patchanim/src/com/mebigfatguy/patchanim/io/PatchExporter.java 2008-02-10 17:26:23 UTC (rev 148)
+++ trunk/patchanim/src/com/mebigfatguy/patchanim/io/PatchExporter.java 2008-02-10 17:34:27 UTC (rev 149)
@@ -29,7 +29,8 @@
import javax.imageio.ImageIO;
import com.fmsware.gif.AnimatedGifEncoder;
-import com.mebigfatguy.apng.APngEncoder;
+import com.mebigfatguy.encoders.APngEncoder;
+import com.mebigfatguy.encoders.MngEncoder;
import com.mebigfatguy.patchanim.AnimationType;
import com.mebigfatguy.patchanim.ExportType;
import com.mebigfatguy.patchanim.OutOfBoundsColor;
@@ -44,6 +45,7 @@
private File loc;
private AnimatedGifEncoder agEncoder;
private APngEncoder apngEncoder;
+ private MngEncoder mngEncoder;
private int totalImages;
private Set<ExportListener> elisteners = new HashSet<ExportListener>();
@@ -54,8 +56,8 @@
agEncoder = new AnimatedGifEncoder();
else if (type == ExportType.AnimatedPng)
apngEncoder = new APngEncoder();
- else
- agEncoder = null;
+ else if (type == ExportType.AnimatedMng)
+ mngEncoder = new MngEncoder();
}
public void addExportListener(ExportListener el) {
@@ -76,6 +78,8 @@
agEncoder.start(new File(loc, baseName).getPath());
else if (type == ExportType.AnimatedPng)
apngEncoder.start(new File(loc, baseName).getPath());
+ else if (type == ExportType.AnimatedMng)
+ mngEncoder.start(new File(loc, baseName).getPath());
}
totalImages = calcImageCount(document);
@@ -93,6 +97,9 @@
} else if (type == ExportType.AnimatedPng) {
apngEncoder.setRepeat(atype != AnimationType.None);
apngEncoder.setNumFrames(totalImages);
+ } else if (type == ExportType.AnimatedMng) {
+ mngEncoder.setRepeat(atype != AnimationType.None);
+ mngEncoder.setNumFrames(totalImages);
}
if (lastPatch == 0) {
@@ -134,6 +141,8 @@
agEncoder.finish();
} else if (type == ExportType.AnimatedPng) {
apngEncoder.finish();
+ } else if (type == ExportType.AnimatedMng) {
+ mngEncoder.finish();
}
}
}
@@ -157,6 +166,9 @@
} else if (type == ExportType.AnimatedPng) {
apngEncoder.setDelay(100);
apngEncoder.addFrame(image);
+ } else if (type == ExportType.AnimatedMng) {
+ mngEncoder.setDelay(100);
+ mngEncoder.addFrame(image);
}
else
ImageIO.write(image, type.getExtension(), imageFile);
Modified: trunk/patchanim/src/com/mebigfatguy/patchanim/main/PatchAnimBundle.java
===================================================================
--- trunk/patchanim/src/com/mebigfatguy/patchanim/main/PatchAnimBundle.java 2008-02-10 17:26:23 UTC (rev 148)
+++ trunk/patchanim/src/com/mebigfatguy/patchanim/main/PatchAnimBundle.java 2008-02-10 17:34:27 UTC (rev 149)
@@ -40,8 +40,8 @@
public static final String ANIMATEDGIFFILTER = "patchanim.filter.animatedgif";
public static final String ANIMATEDPNG = "patchanim.apng";
public static final String ANIMATEDPNGFILTER = "patchanim.filter.apng";
- public static final String MPEG = "patchanim.mpeg";
- public static final String MPEGFILTER = "patchanim.filter.mpeg";
+ public static final String ANIMATEDMNG = "patchanim.mng";
+ public static final String ANIMATEDMNGFILTER = "patchanim.filter.mng";
public static final String EXPORTINGFILE = "patchanim.exportfile";
public static final String QUIT = "patchanim.quit";
public static final String CONTROLS = "patchanim.control";
Modified: trunk/patchanim/src/com/mebigfatguy/patchanim/resources.properties
===================================================================
--- trunk/patchanim/src/com/mebigfatguy/patchanim/resources.properties 2008-02-10 17:26:23 UTC (rev 148)
+++ trunk/patchanim/src/com/mebigfatguy/patchanim/resources.properties 2008-02-10 17:34:27 UTC (rev 149)
@@ -33,8 +33,8 @@
patchanim.filter.animatedgif = GIF Files (*.gif)
patchanim.apng = an Animated PNG
patchanim.filter.apng = (PNG Files (*.png)
-patchanim.mpeg = an MPEG
-patchanim.filter.mpeg = MPEG Files (*.mpg)
+patchanim.mng = an Animated MNG
+patchanim.filter.mng = (MNG Files (*.mng)
patchanim.exportfile = Exporting Animation
patchanim.quit = Quit
patchanim.control = Controls
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|