xpm to python with functional bitmap compression

A place to submit all custom code, scripts, and programs.
Forum rules
Do NOT post malicious code or programs. Please review all code posted in this forum before downloading or running any of the code or programs here.

xpm to python with functional bitmap compression

Post by MadM0use on Mon Jul 06, 2015 11:34 am
([msg=88860]see xpm to python with functional bitmap compression[/msg])

Another blast from the past, i should go through old code more often lol

Code: Select all
#!/usr/bin/env python
# program to convert xpm to compressed python code, similar to the logo below
# Written By: MadMouse/Jesus

import sys,xtermcolor
############################################################################################################
def draw_logo(bitmap):
   sys.stderr.write("\r\n")
   for row in bitmap:
      for item in row:
         for pixel in item:
            sys.stderr.write(xtermcolor.colorize('#',pixel[1]).split('#')[0]+pixel[0].encode('utf-8'))
      sys.stderr.write("\r\n")
   sys.stderr.write("\r\n\r\n\r\n")
def f1(n):
   return [(' ', 16777215),]*n
def f2(n):
   return [(u'\u2592', 65280),]*n
logo = [[f1(1),f2(4),f1(2),f2(4),f1(3),f2(8),f1(7),f2(2),f1(3),f2(3),f1(4),f2(4),f1(3),f2(6),
f1(3),f2(2),f1(3),f2(2),f1(1),f2(6),f1(2),f2(6),],[f1(1),f2(4),f1(1),f2(5),f1(3),f2(8),f1(1),
f2(8),f1(3),f2(4),f1(3),f2(4),f1(2),f2(8),f1(2),f2(2),f1(3),f2(2),f1(1),f2(2),f1(6),f2(1),
f1(6)],[f1(1),f2(10),f1(3),f2(8),f1(1),f2(8),f1(3),f2(2),f1(1),f2(2),f1(1),f2(5),f1(2),f2(2),
f1(4),f2(2),f1(2),f2(2),f1(3),f2(2),f1(1),f2(6),f1(2),f2(1),f1(6)],[f1(1),f2(10),f1(3),f2(8),
f1(1),f2(2),f1(4),f2(2),f1(3),f2(2),f1(1),f2(8),f1(2),f2(2),f1(4),f2(2),f1(2),f2(2),f1(3),
f2(2),f1(1),f2(6),f1(2),f2(3),f1(4)],[f1(1),f2(2),f1(2),f2(2),f1(2),f2(2),f1(3),f2(2),f1(4),
f2(2),f1(1),f2(8),f1(3),f2(2),f1(2),f2(3),f1(2),f2(2),f1(2),f2(8),f1(2),f2(7),f1(5),f2(2),
f1(2),f2(1),f1(6)],[f1(1),f2(2),f1(6),f2(2),f1(3),f2(8),f1(1),f2(8),f1(3),f2(2),f1(3),f2(1),
f1(3),f2(2),f1(3),f2(6),f1(3),f2(7),f1(1),f2(6),f1(2),f2(6),f1(1)]]

draw_logo(logo)
##############################################################################################################

if len(sys.argv) != 3:
   print("\nUsage: "+sys.argv[0]+" input.xpm output.py\n")
   exit(1)


def remove_comments(filename):
   f = open(filename,"r")
   lines = f.readlines()
   f.close()
   f = open(filename,"w")
   for line in lines:
      if line.find("/*"):
         f.write(line)
   f.close()


def xpixmap2bitlist(filename):
   out = []
   inl = []
   f = open(filename)
   while True:
      line = f.readline()
      if line.find('{')>-1: break
   lines=f.readline().split(' ')
   dimensions = (int(lines[0].strip('\"')),int(lines[1]))
   color_map = []
   out_map = []
   while True:
      line = f.readline()
      if not len(line)<dimensions[0]: break
      if list(line)[1] == ' ':
         line = line.strip('\"').rstrip('\",\n').split('\t')
      else:
         line = line.strip('\"').rstrip('\",\n').split('\t')
      line[1] = line[1].strip("c")
      line[1] = line[1].strip("g")
      for i in range(0,len(line[0])):
         line[0]=line[0].strip(' ')
      for i in range(0,len(line[1])):
         line[1]=line[1].strip(' ')
         if not line[0]: line[0] = ' '
      color_map.append(line) # append this shit to the color map
   while True:
      line=f.readline().rstrip('\n')
      line=line.strip('"')
      line=line.rstrip("\",")
      inl.append(list(line.strip("\"};")))
      if line.find("\"};")>-1: break
   for color in color_map:
      if color[1] == "None":
         out_map.append(0x000000)
      elif color[1].lower() == "orange":
         out_map.append(0xE88D19)
      elif color[1].lower() == "yellow":
         out_map.append(0xFFEA58)
      elif color[1].lower() == "red":
         out_map.append(0xE81600)
      elif color[1].lower() == "white":
         out_map.append(0xffffff)
      elif color[1].lower() == "blue":
         out_map.append(0x0024FF)
      elif color[1].lower() == "green":
         out_map.append(0x00E80D)
      elif color[1].lower() == "black":
         out_map.append(0x000000)
      elif color[1].lower() == "slate grey":
         out_map.append(0x666666)
      elif color[1].lower() == "grey":
         out_map.append(0x888888)
      elif color[1].lower() == "magenta":
         out_map.append(0xE800B0)
      elif color[1].lower() == "light sea green":
         out_map.append(0x55E83C)
      elif color[1].lower() == "seagreen":
         out_map.append(0x369426)
      elif color[1].lower() == "brown4":
         out_map.append(0x8F8000)
      elif color[1].lower() == "sienna":
         out_map.append(0xFF4700)
      else:
         color_int = int(color[1].strip('#'),16)
         out_map.append(color_int)
   for ilist in inl:
      olist=[]
      for i in ilist:
         for item in color_map:
            if item[0].find(i)!=-1:
               if item[1] == "None":
                  olist.append((" ",0x000000))
                  break
               else:
                  color_int = int(item[1].strip('#'),16)
                  olist.append((unichr(9618),color_int))
                  break
      out.append(olist)
   return (out,out_map)


def draw_bitmap(bitmap):
   for row in bitmap:
      for item in row:
         sys.stdout.write(xtermcolor.colorize('#',item[1]).split('#')[0]+item[0].encode('utf-8'))
      sys.stdout.write('\r\n')

def code_row(row,color_map):
   code="["
   counter = 0
   prev = row[0]
   for pixel in row:
      index = color_map.index(prev[1])
      if pixel != prev:
         code=code+"f"+str(index)+"("+str(counter)+"),"
         prev = pixel
         counter = 1
      else:
         counter+=1
   return code+"],\n"
def codify_bitmap(bitmap,color_map):
   code = "#!/usr/bin/env python\n# This image generated using xpm2py written by: MadMouse\nimport sys,xtermcolor\ndef draw_logo(bitmap):\n\tsys.stderr.write(\"\\r\\n\")\n\tfor row in bitmap:\n\t\tfor item in row:\n\t\t\tfor pixel in item:\n\t\t\t\tsys.stderr.write(xtermcolor.colorize('#',pixel[1]).split('#')[0]+pixel[0].encode('utf-8'))\n\t\tsys.stderr.write(\"\\r\\n\")\n\tsys.stderr.write(\"\\r\\n\")\n"
   for i in color_map:
      index = color_map.index(i)
      if index == 0:
         code = code+"def f"+str(index)+"(n):\n\treturn [(' ', "+str(i)+"),]*n\n"
      else:
         code = code+"def f"+str(index)+"(n):\n\treturn [(u'\u2592', "+str(i)+"),]*n\n"
   code = code+"image = [\n"
   for row in bitmap:
      code=code+code_row(row,color_map)   # lol, its just cleaner this way
   return code+"]\ndraw_logo(image)"


remove_comments(sys.argv[1])
(bitmap,color_map) = xpixmap2bitlist(sys.argv[1])
code = codify_bitmap(bitmap,color_map)
f = open(sys.argv[2],"w")
f.write(code)
f.close()
draw_bitmap(bitmap)
sys.stdout.write(xtermcolor.colorize("All done, code file written to: "+sys.argv[2]+"\r\n",0x00ff00))


















const char main[]="\xeb\xfe -> A fully functional program in C";

<@MadMouse> i am forgot what i was doing today but i had motivation and a distinct plan when i woke up stoned right now

http://pastebin.com/FnwUG5KS
Books:
http://goo.gl/muPm3d
User avatar
MadM0use
Experienced User
Experienced User
 
Posts: 70
Joined: Thu Sep 11, 2014 10:30 pm
Blog: View Blog (0)


Re: xpm to python with functional bitmap compression

Post by pretentious on Wed Jul 08, 2015 5:39 am
([msg=88870]see Re: xpm to python with functional bitmap compression[/msg])

MadM0use wrote:i should go through old code more often lol

Hell yeah! and if you don't need it, put it in a dedicated junk code folder then use something like http://www.lesbonscomptes.com/recoll/ if you ever need to reference something you did years ago ;) This is beautiful if you build of previous ideas
Goatboy wrote:Oh, that's simple. All you need to do is dedicate many years of your life to studying security.

IF you feel like exchanging ASCII arrays, let me know ;)
Can you say brainwashing It's a non stop disco
User avatar
pretentious
Addict
Addict
 
Posts: 1203
Joined: Wed Mar 03, 2010 12:48 am
Blog: View Blog (0)


Re: xpm to python with functional bitmap compression

Post by MadM0use on Wed Jul 08, 2015 2:33 pm
([msg=88878]see Re: xpm to python with functional bitmap compression[/msg])

pretentious wrote:
MadM0use wrote:i should go through old code more often lol

Hell yeah! and if you don't need it, put it in a dedicated junk code folder then use something like http://www.lesbonscomptes.com/recoll/ if you ever need to reference something you did years ago ;) This is beautiful if you build of previous ideas


amen dude :D i am really organized and like digging manually though, like when you keep boxes in the attic you know lol. half the fun is digging
const char main[]="\xeb\xfe -> A fully functional program in C";

<@MadMouse> i am forgot what i was doing today but i had motivation and a distinct plan when i woke up stoned right now

http://pastebin.com/FnwUG5KS
Books:
http://goo.gl/muPm3d
User avatar
MadM0use
Experienced User
Experienced User
 
Posts: 70
Joined: Thu Sep 11, 2014 10:30 pm
Blog: View Blog (0)



Return to Custom Code

Who is online

Users browsing this forum: No registered users and 0 guests