スキップしてメイン コンテンツに移動

投稿

Java: Extract Gif Images From an Animated Gif Image

In this post, I will show you the code for extracting gif frame images from an animated gif image. I will show you two types of code: Simply extracting gif images stored in the original animated gif image Creating gif image by rendering and accumulating each gif frames stored in the original gif image Code for Extracting Gif Image From An Animated Gif Java Code package com.dukesoftware.utils.image; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.imageio.ImageReader; import javax.imageio.stream.ImageInputStream; public class GifImageUtils { public static void main(String[] args) throws IOException { String sourceGif = "c:/temp/SmallFullColourGIF.gif"; saveAnimatedGifFramePartsToImage(sourceGif, "c:/temp/gif_parts"); } public static void saveAnimatedGifFramePartsToImage(String input, String outDir) throws IOException { ImageReade...

Java: Unsigned Right Shift Operator

When you work on image pixel manipulation, you may use bit shift operators such as below: // assume a,r,g,b is max 8 bits (0-255) value int pixel = (a << 24 )| (r << 16) | (g << 8) | b; Now I give you a simple question - what will you do for recovering original a, r, g, b value from the pixel int? You may think the code below: int aa = (v & 0xff000000) >> 24; int rr = (v & 0xff0000) >> 16; int gg = (v & 0xff00) >> 8; int bb = (v & 0xff); Actually the above code is wrong. Do you know where it is? The wrong part is right shift operator used for getting value of "aa". In Java, the most left bit is used as a signed bit. If you use the right shift operator to the int which has 1 on the most left bit, the most left bit of the right shifted int remains in 1 to save sign. In the above question case, you should use unsigned right shift operator (>>>) which fills all 0 to bits after right shifted. So the answer...

C#: Create Indexed PNG Image Using pngcs Library

I have written C# program which create an indexed packed png image from a given png image using pngcs 1_1_4. What you need to use pngcs is adding reference of dll to your Visual Studio project - Pngcs.dll and ICSharpCode.SharpZipLib.dll for .Net 2.0, and Pngcs45.dll for .Net4.5. I have used .Net 2.0 version because my Visual Studio is 2010. I have already written a program for generating indexed png in C# on this post - Create Indexed PNG Using C# .Net . But the png encoder Microsoft officially provides does not support compression level parameter. That is, even if png is indexed, the size of png image is not sufficient for me :( So I investigated other library which can handle png in C#, and finally found pncs! Main features of my program: Use minimum bit per pixel as much as possible Image which has alpha channel is is not supported because pngcs library doesn't support color palette with alpha channel If given image has alpha channel but all alpha channel value is 25...

Java: org.w3c.dom.Node to Formatted Xml String

package com.dukesoftware.xml; import java.io.StringWriter; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Node; public class XmlUtils { public static String toString(Node node) throws TransformerException { StringWriter writer = new StringWriter(); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(new DOMSource(node), new StreamResult(writer)); return writer.toString(); } }

Java: スペルミス修正プログラム

高性能なスペルミス修正アルゴリズムを How to Write a Spelling Corrector で見つけたので紹介します。 リンク先では、理論的背景とコードも説明されていますので、参考になるかと思います。 ここ のJavaバージョンのコードを私なりに書き直してみました。 package com.dukesoftware.spellcorrector; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; public class SpellCorrector { public static void main(String args[]) throws IOException { Map<String, Integer> nWords = new HashMap<String, Integer>() {{ put("spell", 1); }}; SpellCorrector spellCorrector = new SpellCorrector(nWords); System.out.println(spellCorrector.correct("superl")); } private final Map<String, Integer> nWords; private final String[] a_to_z; public SpellCorrector(Map<String, Integer> nWords) throws IOException { this.nWords = nWords; this.a_to_z = createAtoZStringArray(); } private Str...

JSONP + Push State: Youtube Application Example

Improve Performance for MySQL CALC_FOUND_ROWS with LIMIT OFFSET Push State Push state is html5 technology that records browsing history in javascript without actual page refresh. Push state is useful for recording Ajax request, which cannot record the browsing history normally, and telling the corresponding url for the Ajax request to browser. The minimum push state code is below: // push the browser state if (typeof(history.replaceState) !== "undefined") { history.pushState({prop1: "a", prop2:"b"}, null, "/url/?param1=param"); } // when browser back button is pressed, pop state event is fired // pop state handler is registered in the folloing code window.onpopstate = function (event) { if (event.state == null) { return; } // do something nice ;) // using "event.state.prop1" for example }; The usage of push state is quite simple. One thing you should...

Improve Performance for MySQL CALC_FOUND_ROWS with LIMIT OFFSET

SQL_CALC_FOUND_ROWS Performance Issue If you are working on MySQL (<=5.5) for a long time, you have an experience with performance issue on SQL_CALC_FOUND_ROWS with LIMIT OFFSET. The core cause is MySQL generates full result set as a temporary table even if user only requested first 20 rows in the query. This kind of situation often happens when you implement paging functionality. Please see following example. SELECT SQL_CALC_FOUND_ROWS tbl_a.a_id, tbl_b.*, tbl_c.*, FROM tbale_a AS tbl_a INNER JOIN table_b AS tbl_b ON tbl_b.a_id = tbl_a.a_id INNER JOIN table_c AS tbl_c ON tbl_c.b_id = tbl_b.b_id LEFT JOIN table_d AS tbl_d ON tbl_d.a_id = tbl_a.a_id WHERE tbl_b.field1 = "ABC" LIMIT 100, 20; In this example, a temporary table which has full result set is generated. If you configure SQL_CACHE, the second time query might be quite faster. However it is not a good choice because the query performance depends on status of tables, and not stable. Assume if tbl_b is fr...