<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: mask code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 25 Jul 2008 02:00:41 GMT</pubDate>
    <description>DZone Snippets: mask code</description>
    <item>
      <title>Generate all subsets of a set</title>
      <link>http://snippets.dzone.com/posts/show/4631</link>
      <description>This code generates all the subsets of {1, 2, ..., n} and prints them.&lt;br /&gt;&lt;br /&gt;This also contains a very fast binary counter implementation.&lt;br /&gt;&lt;br /&gt;See &lt;a href="http://compprog.wordpress.com/2007/10/10/generating-subsets/"&gt;this&lt;/a&gt; for further explanations.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#include &lt;stdio.h&gt;&lt;br /&gt;&lt;br /&gt;/* Applies the mask to a set like {1, 2, ..., n} and prints it */&lt;br /&gt;void printv(int mask[], int n) {&lt;br /&gt;	int i;&lt;br /&gt;	printf("{ ");&lt;br /&gt;	for (i = 0; i &lt; n; ++i)&lt;br /&gt;		if (mask[i])&lt;br /&gt;			printf("%d ", i + 1); /*i+1 is part of the subset*/&lt;br /&gt;	printf("\b }\n");&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/* Generates the next mask*/&lt;br /&gt;int next(int mask[], int n) {&lt;br /&gt;	int i;&lt;br /&gt;	for (i = 0; (i &lt; n) &amp;&amp; mask[i]; ++i)&lt;br /&gt;		mask[i] = 0;&lt;br /&gt;&lt;br /&gt;	if (i &lt; n) {&lt;br /&gt;		mask[i] = 1;&lt;br /&gt;		return 1;&lt;br /&gt;	}&lt;br /&gt;	return 0;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int main(int argc, char *argv[]) {&lt;br /&gt;	int n = 3;&lt;br /&gt;&lt;br /&gt;	int mask[16]; /* Guess what this is */&lt;br /&gt;	int i;&lt;br /&gt;	for (i = 0; i &lt; n; ++i)&lt;br /&gt;		mask[i] = 0;&lt;br /&gt;&lt;br /&gt;	/* Print the first set */&lt;br /&gt;	printv(mask, n);&lt;br /&gt;&lt;br /&gt;	/* Print all the others */&lt;br /&gt;	while (next(mask, n))&lt;br /&gt;		printv(mask, n);&lt;br /&gt;&lt;br /&gt;	return 0;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 10 Oct 2007 14:43:25 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4631</guid>
      <author>scvalex (Alexandru Scvortov)</author>
    </item>
    <item>
      <title>Show all icons in avkon.mbm</title>
      <link>http://snippets.dzone.com/posts/show/1482</link>
      <description>There are 2 type of mbm files. The following code work with&lt;br /&gt;ROM-type (most mbm on Z:\) only.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&gt;&gt;&gt; from appuifw import *&lt;br /&gt;&gt;&gt;&gt; avkon = u'z:\\system\\data\\avkon.mbm'&lt;br /&gt;&gt;&gt;&gt; def showicon(id_list, maskfunc=lambda x:x):&lt;br /&gt;...     if type(id_list) == int:&lt;br /&gt;...         id_list = [id_list] # one item&lt;br /&gt;...     entries = []&lt;br /&gt;...     if type(maskfunc) == dict:  # allow dict as a func&lt;br /&gt;...         func = lambda id: maskfunc.get(id,id)&lt;br /&gt;...     else:&lt;br /&gt;...         func = maskfunc&lt;br /&gt;...     for id in id_list:&lt;br /&gt;...         item = u'%s, %s' % (id, func(id))&lt;br /&gt;...         icon = Icon(avkon, id, func(id))&lt;br /&gt;...         entries.append((item, u'', icon))&lt;br /&gt;...     app.body = Listbox(entries, lambda: None)&lt;br /&gt;...&lt;br /&gt;&gt;&gt;&gt; showicon(range(503))  # all icons, including mask&lt;br /&gt;&gt;&gt;&gt; showicon(range(0,100,2), lambda x:x+1)  # first 50 icons, with their mask brother&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 14 Feb 2006 16:41:43 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1482</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
    <item>
      <title>Using transparent gif/png with Image's blitting</title>
      <link>http://snippets.dzone.com/posts/show/1383</link>
      <description>pys60's Image class will load a transparent gif/png&lt;br /&gt;just like a non-transparent one.&lt;br /&gt;But its blit() method accept a mask paramenter.&lt;br /&gt;I have talked about this in a &lt;a href=http://bigbold.com/snippets/posts/show/926&gt;previous snippet&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;The missing link is to create a mask automatically&lt;br /&gt;from the Image. It's typically the top-left pixel.&lt;br /&gt;You can use Image's getpixel() which is undocumented.&lt;br /&gt;I show typical use of getpixel &lt;a href=http://bigbold.com/snippets/posts/show/832&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Combine them all, here's the automask function.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def automask(im):&lt;br /&gt;    width, height = im.size&lt;br /&gt;    mask = Image.new(im.size, '1') # black and white&lt;br /&gt;    tran = im.getpixel((0,0))[0]   # transparent top-left&lt;br /&gt;    for y in range(height):&lt;br /&gt;        line = im.getpixel([(x, y) for x in range(width)])&lt;br /&gt;        for x in range(width):&lt;br /&gt;            if line[x] == tran:&lt;br /&gt;                mask.point((x,y), 0)  # mask on the point&lt;br /&gt;    return mask&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;An example usage&lt;br /&gt;&lt;code&gt;&lt;br /&gt;from graphics import Image&lt;br /&gt;&lt;br /&gt;ship = Image.open('E:\\Images\\ship.gif')&lt;br /&gt;mask = automask(ship)&lt;br /&gt;&lt;br /&gt;canvas.blit(ship, mask=mask)  # don't forget to create canvas first&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;A Sprite class can be defined based on this too.</description>
      <pubDate>Thu, 09 Feb 2006 13:03:39 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1383</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
    <item>
      <title>Image/canvas bliting</title>
      <link>http://snippets.dzone.com/posts/show/926</link>
      <description>I can't remember how to call the blit method.&lt;br /&gt;So, here's just a personal reminder.&lt;br /&gt;(BTW, the pys60 doc has a wrong order between target and source)&lt;br /&gt;&lt;code&gt;&lt;br /&gt;app.body = c = Canvas()&lt;br /&gt;w,h = 20,20&lt;br /&gt;im = Image.new(size=(w,h))&lt;br /&gt;&lt;br /&gt;# here's prototype&lt;br /&gt;# blit(im, source=(0,0,w,h), target=(0,0), mask=None, scale=0)&lt;br /&gt;# here are examples&lt;br /&gt;c.blit(im)  # put entire image on top-left&lt;br /&gt;c.blit(im, target=(70,70)) # put it about mid-screen&lt;br /&gt;c.blit(im, (0,0,w/2,h/2))  # put a quater image on top-left&lt;br /&gt;&lt;br /&gt;# double the image size and put it about mid-screen&lt;br /&gt;c.blit(im, target=(60,60,100,100), scale=1)&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;If scale is not specified and source &lt;&gt; target, the image&lt;br /&gt;will be clipped instead of resized.&lt;br /&gt;&lt;br /&gt;mask is a 1-bit image with the same size as image.&lt;br /&gt;&lt;code&gt;mask = Image.new((w,h),'1')&lt;/code&gt;&lt;br /&gt;Pixel on the image will be copied if the same pixel&lt;br /&gt;on the mask is 'white'.&lt;br /&gt;So we can use mask to make 'Sprite'.</description>
      <pubDate>Thu, 01 Dec 2005 20:57:55 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/926</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
    <item>
      <title>Masked Input //Javascript Object</title>
      <link>http://snippets.dzone.com/posts/show/666</link>
      <description>&lt;a href="http://jsfromhell.com/forms/masked-input"&gt;&lt;br /&gt;Simple and generic mask routine.&lt;br /&gt;&lt;br /&gt;[UPDATED CODE AND HELP CAN BE FOUND HERE]&lt;br /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;@REQUIRES &lt;a href="http://www.jsfromhell.com/geral/event-listener"&gt;Event-Listener&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;// REQUIRES: http://jsfromhell.com/geral/event-listener&lt;br /&gt;&lt;br /&gt;//+ Jonas Raoni Soares Silva&lt;br /&gt;//@ http://jsfromhell.com/forms/masked-input [v1.0]&lt;br /&gt;&lt;br /&gt;MaskInput = function(f, m){ //v1.0&lt;br /&gt;    function mask(e){&lt;br /&gt;        var patterns = {"1": /[A-Z]/i, "2": /[0-9]/, "4": /[&#192;-&#255;]/i, "8": /./ },&lt;br /&gt;            rules = { "a": 3, "A": 7, "9": 2, "C":5, "c": 1, "*": 8};&lt;br /&gt;        function accept(c, rule){&lt;br /&gt;            for(var i = 1, r = rules[rule] || 0; i &lt;= r; i&lt;&lt;=1)&lt;br /&gt;                if(r &amp; i &amp;&amp; patterns[i].test(c))&lt;br /&gt;                    break;&lt;br /&gt;                return i &lt;= r || c == rule;&lt;br /&gt;        }&lt;br /&gt;        var k, mC, r, c = String.fromCharCode(k = e.key), l = f.value.length;&lt;br /&gt;        (!k || k == 8 ? 1 : (r = /^(.)\^(.*)$/.exec(m)) &amp;&amp; (r[0] = r[2].indexOf(c) + 1) + 1 ?&lt;br /&gt;            r[1] == "O" ? r[0] : r[1] == "E" ? !r[0] : accept(c, r[1]) || r[0]&lt;br /&gt;            : (l = (f.value += m.substr(l, (r = /[A|9|C|\*]/i.exec(m.substr(l))) ?&lt;br /&gt;            r.index : l)).length) &lt; m.length &amp;&amp; accept(c, m.charAt(l))) || e.preventDefault();&lt;br /&gt;    }&lt;br /&gt;    for(var i in !/^(.)\^(.*)$/.test(m) &amp;&amp; (f.maxLength = m.length), {keypress: 0, keyup: 1})&lt;br /&gt;        addEvent(f, i, mask);&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;//]]&gt;&lt;br /&gt;&lt;/script&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Usage (sorry, i'm lazy to write a help hahaha)&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;form action=""&gt;&lt;br /&gt;telephone "(99)9999-9999"&lt;br /&gt;&lt;br /&gt;&lt;input type="text" /&gt;&lt;br /&gt;&lt;br /&gt;date "99/99/9999"&lt;br /&gt;&lt;br /&gt;&lt;input type="text" /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;m&#225;scara = letter + letter withou accent + 2 numbers + "-" + anything + letter "Cc99-*C"&lt;br /&gt;&lt;br /&gt;&lt;input type="text" /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;everything, but a, b or c "E^abc"&lt;br /&gt;&lt;br /&gt;&lt;input type="text" /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;only a, b or c "O^abc"&lt;br /&gt;&lt;br /&gt;&lt;input type="text" /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;just letters "C^"&lt;br /&gt;&lt;br /&gt;&lt;input type="text" /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;just letters and also white-space "C^ "&lt;br /&gt;&lt;br /&gt;&lt;input type="text" /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;just numbers and also the letters a, b e c "9^abc"&lt;br /&gt;&lt;br /&gt;&lt;input type="text" /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/form&gt;&lt;br /&gt;&lt;br /&gt;&lt;script&gt;&lt;br /&gt;with( document.forms[0] ){&lt;br /&gt;MaskedInput.apply( elements[0], '(99)9999-9999' );&lt;br /&gt;MaskedInput.apply( elements[1], '99/99/9999' );&lt;br /&gt;MaskedInput.apply( elements[2], 'Cc99-*C' );&lt;br /&gt;MaskedInput.apply( elements[3], 'E^abc' );&lt;br /&gt;MaskedInput.apply( elements[4], 'O^abc' );&lt;br /&gt;MaskedInput.apply( elements[5], 'C^' );&lt;br /&gt;MaskedInput.apply( elements[6], 'C^ ' );&lt;br /&gt;MaskedInput.apply( elements[7], '9^abc' );&lt;br /&gt;}&lt;br /&gt;&lt;/script&gt;&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 09 Sep 2005 04:10:55 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/666</guid>
      <author>jonasraoni (Jonas Raoni Soares Silva)</author>
    </item>
  </channel>
</rss>
