Sunday, November 21, 2010

Trees builder

Hello world!
Suddenly I’ve decided to make the simple trees builder.
No sooner said than done.

You can see result in the image below.


Every time the algorithm generates a new tree.
After some variations variables, I found the results' similarity with the real species of trees.

[Show / hide source code]
//------------------------------------------------------------------------------
// Build a new tree
//------------------------------------------------------------------------------
void BuildTree(Point startPoint, Point endPoint, int k)
{
    Random rnd = new Random((int)DateTime.Now.Ticks);
    Pen pen = new Pen(Color.Black, k);

    if (hasLeaves)
    {
        // set the color to rnd green and width = 5 for last branch as for a leaf
        if (k < 1) pen = new Pen(Color.FromArgb(0, rnd.Next(80, 160), 0), 5);
    }

    m_Graphics.DrawLine(pen, startPoint, endPoint);

    if (k > 0)
    {
        Point pNext = new Point();
     
        // add branch to the right
        pNext = new Point(endPoint.X + rnd.Next(0, spread), endPoint.Y - rnd.Next(0, speed));
        BuildTree(endPoint, pNext, k - 1);
     
        // add branch to the left
        pNext = new Point(endPoint.X - rnd.Next(0, spread), endPoint.Y - rnd.Next(0, speed));
        BuildTree(endPoint, pNext, k - 1);

        Application.DoEvents();
    }
}

* This source code was highlighted with Source Code Highlighter.

Monday, October 18, 2010

Steganography

The word steganography means "concealed writing" from the Greek words steganos meaning "covered", and graphein meaning "to write".

This is very ancient technology. So I’ll try to explain how it works.
It’s not a secret that every color can be represented as tuples of numbers, typically as three or four values or color components (e.g. RGB and CMYK).

It’s possible to use least significant bits of each color component for data storage. Thus it’s possible to use images for information hiding or steganography.
Let’s estimate the color component loss level for data storage.
The maximum loss level for 1 bit for color component coding will be 1b = 1 and the error will be:
1/255 * 100 % = 0,392 %
The maximum loss level for 2 bits per color component coding will be 11b = 3. So the maximum error per color component is equal:
3/255 * 100 % = 1,176 %
For 3 bits the maximum loss level will be 111b = 8 and the error will be:
8/255 * 100 % = 3,137 %
In common, human eye couldn’t distinguish so little difference in colors.

The picture below shows a principle of steganography using 2 bits per color channel coding. 
 
To hide ‘A’ char using 2 bits per color component coding required 4 color components or 2 pixels (RGB and yet another R).
Below you can see source code in C# for 2 bits per color component images coding and decoding.

[Show / hide source code]
//---------------------------------------------------------------------
// CodeStegoImage
//---------------------------------------------------------------------
private Bitmap CodeStegoImage(Image inputImage, string inputText)
{     
  string header = "DSTG"; // Steganorgaphy header
  int textLen = header.Length + inputText.Length + 4; // 4 bytes (65535 chars) for text length
  string lenStr = textLen.ToString("0000"); // make formated NNNN text length string
  string text = header + lenStr + inputText;

  byte[] bytes = new byte[text.Length * 4 + 4]; // 4 bytes for char using 2 bits coding + 4 bytes extra

  for (int i = 0; i < text.Length; i++)
  {
    bytes[i*4+0] = (byte)((System.Convert.ToChar(text[i]) & (byte)System.Convert.ToInt32("11000000", 2)) >> 6);
    bytes[i*4+1] = (byte)((System.Convert.ToChar(text[i]) & (byte)System.Convert.ToInt32("00110000", 2)) >> 4);
    bytes[i*4+2] = (byte)((System.Convert.ToChar(text[i]) & (byte)System.Convert.ToInt32("00001100", 2)) >> 2);
    bytes[i*4+3] = (byte)(System.Convert.ToChar(text[i]) & (byte)System.Convert.ToInt32("00000011", 2));
  }

  Bitmap bmIn = new Bitmap(inputImage);   // input bitmap
  Bitmap bmOut = new Bitmap(inputImage);  // output bitmap
  int counter = 0;

  for (int i = 0; i < inputImage.Height; i++)
  {
    for (int j = 0; j < inputImage.Width; j++)
    {
      Color colIn = bmIn.GetPixel(j, i);

      // clear 2 LSB
      uint ro = (uint)colIn.R & 0xFC; // 0xFC = 11111100b
      uint go = (uint)colIn.G & 0xFC;
      uint bo = (uint)colIn.B & 0xFC;

      Color colOut; // output color
      if (counter < text.Length * 4) // 4 bytes per char
      {
        colOut = Color.FromArgb((int)ro + bytes[counter + 0], (int)go + bytes[counter + 1], (int)bo + bytes[counter + 2]);
        counter += 3; // +3 bytes to next RGB pixel
      }
      else
        colOut = colIn;
      bmOut.SetPixel(j, i, colOut);
    }
  }
  return bmOut;
}

* This source code was highlighted with Source Code Highlighter.

Thursday, September 30, 2010

Solving the riddle

Solving the riddle "Hidden Message #3" from The Science of Deduction I've found the solution.

It turned out that it is encrypted with pigpen cipher.

The pigpen cipher (sometimes referred to as the masonic cipher, Freemason's cipher, or Rosicrucian cipher) is a geometric simple substitution cipher which exchanges letters for symbols which are fragments of a grid. The example key shows one way the letters can be assigned to the grid.


The solution to the riddle, using the picture above is obvious:
Sherlock I have found you

Monday, September 13, 2010

Programmer Day

Happy Programmer's Day!

Tuesday, August 24, 2010

HB

Happy Birthday Aximedia Soft! С днём рождения Аксимедиа Софт.
Aximedia Soft logo

Sunday, July 4, 2010

The IT Crowd

I like the 4th season of "The IT Crowd" very much. That's awesome! It made me laugh out loud )))

Friday, July 2, 2010

Riddles

В перерывах между работой решили занять себя математическими и логическими загадками.
Началось всё с простого и понеслась. Начали загадывать друг другу известные всем последовательности.
Russian version English equivalent
1) О Д Т Ч П ... O T T F F S S E N ...
2) П В С Ч П ... S M T W T ...
3) И Р Д В Т ?
4) К О Ж З ... R O Y G ...
5) Д Т П С О ... T T F S E ...
6) М В З М Ю ... M V E M J ...
7) 1 5 10 50 1 2 ...
8) E A D G ...
9) (1, 1) – (2, 3) – (1, 5) – (2, 7) – (4, 8) – (6, 7) – ?
10) 1 2 7 0 0 ?
11) IIIIIVIVIXII... ( "," present )
[+/-] Show / hide answers
1) Один Два Три Четыре Пять Шесть ...
One Two Three Four Five Six Seven Eight Nine Ten ...

2) Понедельник Вторник Среда Четверг Пятница Суббота ...
Sunday Monday Tuesday Wednesday Thursday Friday ...

3) Именительный Родительный Дательный Винительный Творительный Предложный

4) Красный Оранжевый Жёлтый Зелёный Голубой Синий ...
Red Orange Yellow Green Blue Violet

5) 2 3 5 7 11 13 17... Простые числа
Prime numbers

6) Меркурий Венера Земля Марс Юпитер Сатурн ...
Mercury Venus Earth Mars Jupiter Saturn ...

7) 1 коп 5 коп 10 коп 50 коп 1 руб 2 руб 5 руб ...

8) E A D G B E Основная настройка гитары

9) (1, 1) – (2, 3) – (1, 5) – (2, 7) – (4, 8) – (6, 7) – (8, 8) - ...
Ход шахматного коня

10) IP 127.0.0.1

11) 3,1415926535...