Permalink
Browse files

png writer

git-svn-id: http://greasefire.googlecode.com/svn@35 80789420-ceb8-11dd-86c2-0f8696b7b6f9
  • Loading branch information...
skrulx
skrulx committed Jan 19, 2010
1 parent 58aa697 commit 69bbb7e9a81b97b8d5368d31af3f896415345b82
@@ -0,0 +1,49 @@
+package com.skrul.greasefire;
+
+import java.awt.image.BufferedImage;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.IOException;
+import java.io.OutputStream;
+
+import javax.imageio.ImageIO;
+
+public class AlphaPNGOutputStream extends OutputStream {
+
+ private File outputFile;
+ private ByteArrayOutputStream os;
+
+ public AlphaPNGOutputStream(File outputFile) {
+ this.outputFile = outputFile;
+ this.os = new ByteArrayOutputStream();
+ }
+
+ @Override
+ public void close() throws IOException {
+ byte[] bytes = this.os.toByteArray();
+ double size = bytes.length / 4.0;
+ if (size - Math.floor(size) > 0)
+ size++;
+
+ BufferedImage bi = new BufferedImage((int) size, 1, BufferedImage.TYPE_INT_ARGB);
+ bi.coerceData(false);
+ int pos = 0;
+ for (int i = 0; i < bytes.length; i += 4) {
+ int r = bytes[i];
+ int g = i + 1 < bytes.length ? bytes[i + 1] : 0;
+ int b = i + 2 < bytes.length ? bytes[i + 2] : 0;
+ int a = i + 3 < bytes.length ? bytes[i + 3] : 0;
+ int value = ((a & 0xFF) << 24) | ((r & 0xFF) << 16) | ((g & 0xFF) << 8) | ((b & 0xFF) << 0);
+ bi.setRGB(pos, 0, value);
+ pos++;
+ }
+
+ ImageIO.write(bi, "PNG", this.outputFile);
+ }
+
+ @Override
+ public void write(int b) throws IOException {
+ this.os.write(b);
+ }
+
+}
@@ -5,22 +5,18 @@
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
-import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
-import org.htmlcleaner.XPatherException;
public class DownloadScripts {
@@ -199,8 +199,16 @@ public void finish() throws Exception {
fos = new FileOutputStream(excludesFile);
excludesIndex.serialize(fos);
fos.close();
- }
+ PNGOutputStream pos = new PNGOutputStream(new File(destDir, "include.png"));
+ includesIndex.serialize(pos);
+ pos.close();
+
+ pos = new PNGOutputStream(new File(destDir, "exclude.png"));
+ excludesIndex.serialize(pos);
+ pos.close();
+}
+
public static void search(Node n, String s, int pos, Set<Integer> matches) {
if (n.ids != null) {
@@ -0,0 +1,46 @@
+package com.skrul.greasefire;
+
+import java.awt.image.BufferedImage;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.IOException;
+import java.io.OutputStream;
+
+import javax.imageio.ImageIO;
+
+public class GrayPNGOutputStream extends OutputStream {
+
+ private File outputFile;
+ private ByteArrayOutputStream os;
+
+ public GrayPNGOutputStream(File outputFile) {
+ this.outputFile = outputFile;
+ this.os = new ByteArrayOutputStream();
+ }
+
+ @Override
+ public void close() throws IOException {
+ byte[] bytes = this.os.toByteArray();
+ BufferedImage bi = new BufferedImage(bytes.length, 1, BufferedImage.TYPE_BYTE_GRAY);
+ for (int i = 0; i < bytes.length; i++) {
+ int b = bytes[i];
+ int value = ((b & 0xFF) << 16) | ((b & 0xFF) << 8) | ((b & 0xFF) << 0);
+ bi.setRGB(i, 0, value);
+ System.out.println(i + " " + value);
+ }
+
+ /*
+ ImageTypeSpecifier type = ImageTypeSpecifier.createFromRenderedImage(bi);
+ Iterator iter = ImageIO.getImageWriters(type, "png");
+ ImageWriter writer = (ImageWriter) iter.next();
+ ImageWriteParam param = writer.getDefaultWriteParam();
+ */
+ ImageIO.write(bi, "PNG", this.outputFile);
+ }
+
+ @Override
+ public void write(int b) throws IOException {
+ this.os.write(b);
+ }
+
+}
@@ -0,0 +1,19 @@
+package com.skrul.greasefire;
+
+import java.io.File;
+
+public class ImageTest {
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args) throws Exception {
+
+ PNGOutputStream os = new PNGOutputStream(new File("testimage.png"));
+ for (int i = 0; i < 7; i++) {
+ os.write(i);
+ }
+ os.close();
+ }
+
+}
@@ -0,0 +1,46 @@
+package com.skrul.greasefire;
+
+import java.awt.image.BufferedImage;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.IOException;
+import java.io.OutputStream;
+
+import javax.imageio.ImageIO;
+
+public class PNGOutputStream extends OutputStream {
+
+ private File outputFile;
+ private ByteArrayOutputStream os;
+
+ public PNGOutputStream(File outputFile) {
+ this.outputFile = outputFile;
+ this.os = new ByteArrayOutputStream();
+ }
+
+ @Override
+ public void close() throws IOException {
+ byte[] bytes = this.os.toByteArray();
+ double size = bytes.length / 3.0;
+ if (size - Math.floor(size) > 0)
+ size++;
+
+ BufferedImage bi = new BufferedImage((int) size, 1, BufferedImage.TYPE_INT_RGB);
+ int pos = 0;
+ for (int i = 0; i < bytes.length; i += 3) {
+ int r = bytes[i];
+ int g = i + 1 < bytes.length ? bytes[i + 1] : 0;
+ int b = i + 2 < bytes.length ? bytes[i + 2] : 0;
+ int value = ((r & 0xFF) << 16) | ((g & 0xFF) << 8) | ((b & 0xFF) << 0);
+ bi.setRGB(pos, 0, value);
+ pos++;
+ }
+ ImageIO.write(bi, "png", this.outputFile);
+ }
+
+ @Override
+ public void write(int b) throws IOException {
+ this.os.write(b);
+ }
+
+}

0 comments on commit 69bbb7e

Please sign in to comment.