A simple algorithm for generating QQ hidden graphs

Source: Internet
Author: User

The hidden picture is not something new, it is in most social software, the preview is a picture, and when you open it you see another picture. Although very early to see this kind of picture, but has not carefully studied its principle, today thought about a bit, found quite interesting, so I also wrote a simple algorithm to make two pictures together into a hidden picture.

For example, the picture below.

When the background color is white, it is usually in the preview state, it looks like this

And when the background color turns black, it is usually after the opening of the picture, it is such a son.

Hidden graph principle

  We know that a pixel with transparency in one image will overlay a portion of the background color, so when the background color is white, all white pixels with transparency are all shown as pure white, and when the background color is black, all the transparent black will appear as pure black. so we just need to convert all the pixels in the picture to a different transparent black according to their grayscale values, convert all the pixels in the picture two to a different transparency based on their grayscale values, and cross-arrange all the pixels of the two graph in random order to generate a hidden graph. This way, when the background color is black, all the pixels of the Tuyi will be shown as pure black, and all the pixels of Figure II will show different shades of gray because of their transparency, when the figure is hidden, and the second one appears, and vice versa.

Algorithm Implementation

The basic algorithm idea has already mentioned above, can say is a quite simple algorithm. However, there are a few points to note:

    1. By its principle, the hidden map can only be black and white, not color, so when you encounter color pixels, you need to convert it to grayscale. For color to grayscale, there is a formula in psychology:Gray = r*0.299 + g*0.587 + b*0.114, What we need to do is to set the pixel transparency based on the calculated grayscale. On a white background, the grayscale of black pixels decreases as transparency increases , and in the black background, the grayscale of the white pixels increases as the transparency increases .
    2. Considering that the two images that need to be synthesized are not of the same size, in order to ensure that the generated hidden graph can complete the retention of two picture information without distortion, we need to set the length and width of the final image to the maximum length and width of the two picture dimensions .

All right, let's stick to my code implementation.

1 usingSystem;2 usingSystem.IO;3 usingSystem.Drawing; 4 5 classMainClass6     {7          Public Static voidMain (string[] args)8         {9             //file path for picture oneTenStream Blackimagereader =NewFileStream ("/users/shiyidu/desktop//1.jpg", FileMode.Open); OneBitmap Blackimage =NewBitmap (blackimagereader); A  -             //file path for picture two -Stream Whiteimagereader =NewFileStream ("/users/shiyidu/desktop//2.jpg", FileMode.Open); theBitmap Whiteimage =NewBitmap (whiteimagereader); -  -             //Generate final Picture -Bitmap Finalimage =calculatehiddenimage (Blackimage, whiteimage); +  -             //Final picture save path +Stream Imagecreater =NewFileStream ("/users/shiyidu/desktop//final.png", FileMode.Create); A Finalimage.save (Imagecreater, System.Drawing.Imaging.ImageFormat.Png); at         } -  -         Private StaticBitmap calculatehiddenimage (Bitmap blackimage, Bitmap whiteimage) -         { -             intB_width =Blackimage.width; -             intB_height =Blackimage.height; in             intW_width =Whiteimage.width; -             intW_height =Whiteimage.height; to  +             //set the size of the final picture -             intF_width =Math.max (B_width, w_width); the             intF_height =Math.max (B_height, w_height); *  $Bitmap result =NewBitmap (F_width, f_height);Panax Notoginseng  -             //the distance from the edge of the black picture the             intB_widthoffset = B_width = = F_width?0: (f_width-b_width)/2; +             intB_heightoffset = B_height = = F_height?0: (f_height-b_height)/2; A  the             //white picture distance from Edge +             intW_widthoffset = W_width = = F_width?0: (f_width-w_width)/2; -             intW_heightoffset = W_height = = F_height?0: (f_height-w_height)/2; $  $              for(intx =0; x < f_width; X + +) { -                  for(inty =0; Y < F_height; y++) { -                     //cross-align black and white pixels up or down the                     BOOLBlackpixel = (x + y)%2==0?true:false; - Wuyi                     intcoor_x; the                     intcoor_y; -                     //determines whether the current pixel position corresponds to a pixel of figure one or figure two, and if not, skips the loop Wu                     BOOLValidpixel =true; -                     if(blackpixel) { Aboutcoor_x = x-B_widthoffset; $                         if(Coor_x > B_width-1) Validpixel =false; -coor_y = y-B_heightoffset; -                         if(Coor_y > B_height-1) Validpixel =false; -}Else { Acoor_x = x-W_widthoffset; +                         if(Coor_x > W_width-1) Validpixel =false; thecoor_y = y-W_heightoffset; -                         if(Coor_y > W_height-1) Validpixel =false; $                     } the  theValidpixel = Validpixel && coor_x >=0&& coor_y >=0; the                     if(!validpixel)Continue; the  -                     //calculate pixel grayscale based on color, set transparency in                     if(blackpixel) { theColor origin =Blackimage.getpixel (coor_x, coor_y); the                         intGray = (origin. R19595+ Origin. G38469+ Origin. B7472) >> -; AboutColor Finalcolor = Color.FromArgb (255-Gray, Color.Black); the result. SetPixel (x, y, finalcolor); the}Else { theColor origin =Whiteimage.getpixel (coor_x, coor_y); +                         intGray = (origin. R19595+ Origin. G38469+ Origin. B7472) >> -; -Color Finalcolor =Color.FromArgb (Gray, Color.White); the result. SetPixel (x, y, finalcolor);Bayi                     } the                 } the             } -  -             returnresult; the         } the}
View Code

A simple algorithm for generating QQ hidden graphs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.