Saturday, January 31, 2009

2D acceleration blurs out pages - Adobe Reader

If you find pages in Adobe Reader fading out as shown below, then it means the 2D acceleration API is not functioning as expected.


You may want to turn it off to get rid of the above effect. Now goto the preferences page in the Reader and select Page Display section. Unselect the choice box shown below.

 

That should solve your problem.

Friday, January 30, 2009

Python under Moon light

After reading this post I thought of comparing the performance of python against the lightweight scripting language Lua. So to have a fair comparison I ported the MorseCode encoder/decoder wirtten in python into a Lua script with no change to the logic or the sequence.



--morse code table
codes = {
['!'] = "-.-.--", [' '] = "|", ['"'] = ".-..-.", ['$'] = "...-..-",
['\''] = ".----.", ['&'] = ".-...", [')'] = "-.--.-", ['('] = "-.--.",
['+'] = ".-.-.", ['-'] = "-....-", [','] = "--..--", ['/'] = "-..-.",
['.'] = ".-.-.-", ['1'] = ".----", ['0'] = "-----", ['3'] = "...--",
['2'] = "..---", ['5'] = ".....", ['4'] = "....-", ['7'] = "--...",
['6'] = "-....", ['9'] = "----.", ['8'] = "---..", [';'] = "-.-.-",
[':'] = "---...", ['='] = "-...-", ['?'] = "..--..", ['A'] = ".-",
['@'] = ".--.-.", ['C'] = "-.-.", ['B'] = "-...", ['E'] = ".",
['D'] = "-..", ['G'] = "--.", ['F'] = "..-.", ['I'] = "..",
['H'] = "....", ['K'] = "-.-", ['J'] = ".---", ['M'] = "--",
['L'] = ".-..", ['O'] = "---", ['N'] = "-.", ['Q'] = "--.-",
['P'] = ".--.", ['S'] = "...", ['R'] = ".-.", ['U'] = "..-",
['T'] = "-", ['W'] = ".--", ['V'] = "...-", ['Y'] = "-.--",
['X'] = "-..-", ['Z'] = "--..", ['_'] = "..--.-"
}


binary = {['.']="10",['-']="1110",[',']="000",['|']="0000000"}


function encode(value)
--encodes the value into morse code
value = string.upper(value)
value = string.gsub(value,'%*', 'X')
value = string.gsub(value,'%^', 'XX')
local morse_value=""
local length = string.len(value)
local i = 1
while i <= length do
local chr = string.sub(value,i,i)
if chr then
morse_value = morse_value .. codes[chr] .. ","
end
i = i + 1
end
return _get_binary(morse_value)
end

function _get_binary(value)
local binary_value = ""
local length = string.len(value)
local i = 1
while i <= length do
local chr = string.sub(value,i,i)
if chr then
binary_value = binary_value .. binary[chr]
end
i = i + 1
end
return binary_value
end

function decode(morse_code_value)
-- decodes the morse bytes
decoded_value = _decode_binary(morse_code_value)
ascii_value=""
for w in string.gfind(decoded_value, "[-.|]+") do
ascii_value = ascii_value .. _get_key(w)
end
return ascii_value
end

function _get_key(value)
--returns the key for the given value
for k, v in next, codes do
if v == value then
return k
end
end
return ''
end

function _decode_binary(binary)
dah_replaced = string.gsub(binary,'1110', '-')
dit_replaced = string.gsub(dah_replaced,'10', '.')
comma_replaced = string.gsub(dit_replaced,'000', ',')
zero_replaced = string.gsub(comma_replaced,'0', '|,')
return zero_replaced
end

function print_usage()
print("Usage : "..arg[0].." [d (decode) |e (encode)] [input string]")
end


if arg[1] == nil then
print_usage()
else
if arg[1] == 'd' then
print("Decoded value : "..decode(arg[2]))
elseif arg[1] == 'e' then
if arg[2] == nil then
print_usage()
else
print("Encoded value : "..encode(arg[2]))
end
else
print("Encoded value : "..encode(arg[1]))
end
end



The Lua script was much faster than the python one. May be i have not written the python script in an optimistic way, but logic remains the same.

Aanand Natarajan@AANAND /h/MorseEnDecode/src
$ time python MorseEnDecode.py aanand
encoding
Encoded : 10111000010111000011101000010111000011101000011101010000

real 0m0.140s
user 0m0.015s
sys 0m0.015s

Aanand Natarajan@AANAND /h/MorseEnDecode/src
$ time lua MorseEnDecode.lua aanand
Encoded value : 10111000010111000011101000010111000011101000011101010000

real 0m0.031s
user 0m0.015s
sys 0m0.015s


Look at the real time which is 0.140 seconds for the Python version and 0.31 seconds for the Lua version.
This doesn't mean Python is not fast. I like Python as a language, for its features and its highly performant comparing against many other scripting languages.

Monday, January 26, 2009

Morse code encoder/decoder

This is a Morse code encoder/decoder program written in python. Simply pass a string that contains Morse code supported characters and you'll see a binary format of the string, encoded as Morse code as specified @ http://en.wikipedia.org/wiki/Morse_code.

  1. short mark, dot or 'dit' (·) — 1
  2. longer mark, dash or 'dah' (–) — 111
  3. intra-character gap (between the dots and dashes within a character) — 0
  4. short gap (between letters) — 000
  5. medium gap (between words) — 0000000


#!/usr/bin/python
import sys
__author__="Aanand Natarajan"

#morse code dictionary
codes = {'1':".----",'2':"..---",'3':"...--",'4':"....-",'5':".....",'6':"-....",'7':"--...",'8':"---..",
'9':"----.",'0':"-----",'A':".-",'B':"-...",'C':"-.-.",'D':"-..",'E':".",'F':"..-.",'G':"--.",
'H':"....",'I':"..",'J':".---",'K':"-.-",'L':".-..",'M':"--",'N':"-.",'O':"---",'P':".--.",
'Q':"--.-",'R':".-.",'S':"...",'T':"-",'U':"..-",'V':"...-",'W':".--",'X':"-..-",'Y':"-.--",
'Z':"--..",
#punctuations
',':"--..--",'.':".-.-.-",'?':"..--..",';':"-.-.-",':':"---...",'/':"-..-.",
'-':"-....-","'":".----.",'(':"-.--.",')':"-.--.-",'!':"-.-.--",'&':".-...",
'=':"-...-",'+':".-.-.",'_':"..--.-",'"':".-..-.",'$':"...-..-",'@':".--.-.",
#space
' ':"|"}

binary = {'.':'10','-':'1110',',':'000','|':'0000000'}


def encode(value):
""" encodes the value into morse code """
morse_value=""
value.replace('*', 'X')
value.replace('^', 'XX')
for c in value:
try :
morse_value += codes[c.upper()]+','
except :
print "Unintended character " + c + " omitted"
return _get_binary(morse_value)

def decode(morse_code_value):
""" decodes the morse bytes """
decoded_value = _decode_binary(morse_code_value)
ascii_value=""
for v in decoded_value.split(","):
ascii_value += _get_key(v)
return ascii_value

def _get_binary(value):
binary_value = ""
for c in value:
binary_value += binary[c]
return binary_value

def _get_key(value):
""" returns the key for the given value """
for k,v in codes.items():
if v == value:
return k
return ''

def _decode_binary(binary):
dah_replaced = binary.replace('1110', '-')
dit_replaced = dah_replaced.replace('10', '.')
comma_replaced = dit_replaced.replace('000', ',')
zero_replaced = comma_replaced.replace('0', '|,')
return zero_replaced

def _do_decode(value):
print "Decoded : "+decode(value)

def _do_encode(value):
print "Encoded : "+encode(value)

if __name__ == "__main__":
if len(sys.argv) > 2:
if sys.argv[1] == 'd' :
print "decoding"
_do_decode(sys.argv[2])
else:
print "encoding"
_do_encode(sys.argv[2])
elif len(sys.argv) > 1:
print "encoding"
_do_encode(sys.argv[1])
else:
print "Usage : "+sys.argv[0]+" [d (decode) |e (encode)] [input string]"


Usage:
aanand@AanandDL:~$ ./MorseEnDecode.py "aanand aka tuxaanand"
encoding
Encoded : 101110000101110000111010000101110000111010000111010
1000000000000001011100001110101110000101110000000000000011100
0010101110000111010101110000101110000101110000111010000101110
00011101000011101010000
aanand@AanandDL:~$ ./MorseEnDecode.py d "1011100001011100001110
100001011100001110100001110101000000000000001011100001110101110
000101110000000000000011100001010111000011101010111000010111000
010111000011101000010111000011101000011101010000"
decoding
Decoded : AANAND AKA TUXAANAND
aanand@AanandDL:~$