Archive

Archive for the ‘Experiments’ Category

Twitter Image

June 18th, 2009 rjzii No comments

Background

This project arose out of a contest that was held on StackOverflow that grew out someone else’s project involving image compression. The overall objective of the project is to be able to encode an image in such a way that you can send it via a 140 character tweet on Twitter.

Algorithm

The basic idea behind the algorithm is as follows:

  1. Down sample the image to gray-scale such that there are about 16 shades of gray.
  2. Set a sampling block size for the image.
  3. Encode the image using RLE.
  4. Pack the results into the UTF-16 characters such that there are two run lengths to a character and four color shades to a character.
  5. Preform additional RLE on the packed data such that duplicated run-leghts are replaced by a number followed by a value.
  6. Check to see if the resulting string is less than or equal to 140 characters, if so return it, otherwise, return to step two.

Results

Reference Image Tweet Output Image Output Run Time Tweet Size
cornell-box.png cornell-box.txt cornell-box.bmp 61ms 135 characters
cornell-box-large.png cornell-box-large.txt cornell-box-large.bmp 365ms 124 characters
lena.png lena.txt lena.bmp 39ms 103 characters
lena-large.png lena-large.txt lena-large.bmp 1159ms 137 characters
mona-lisa.png mona-lisa.txt mona-lisa.bmp 26ms 97 characters
mona-lisa-large.png mona-lisa-large.txt mona-lisa-large.bmp 2187ms 131 characters
stackoverflow-logo.png stackoverflow-logo.txt stackoverflow-logo.bmp 37ms 111 characters

Discussion

Twitter makes use of Unicode characters; however, based upon discussion on the contest page it seems that web submission only recognizes UTF-16 where as the API can use the full UTF-32 space. For the purposes of this implementation the code is limited to making use of the UTF-16 space which is also conveniently the char data type in C#.

Give the simple nature of the algorithm it is unlikely that this project broke any new ground; however, it did server as an excellent exercise for my own skills in that it exposed me to image manipulation which is not something I do in the course of my job and likewise, it had been a long time since I had to do any bitwise manipulations to data.

Source Code

There are two main files for this project: program.cs and twitterimage.cs. The following is the cloc output for the files:

http://cloc.sourceforge.net v 1.07  T=0.5 s (4.0 files/s, 832.0 lines/s)
-------------------------------------------------------------------------------
Language          files     blank   comment      code    scale   3rd gen. equiv
-------------------------------------------------------------------------------
C#                    2        23       106       303 x   1.36 =         412.08
-------------------------------------------------------------------------------
	
Categories: Experiments Tags: , ,