Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 28th, 2012  |  syntax: None  |  size: 4.71 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // bitmaptest.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <math.h>
  6.  
  7. using namespace Gdiplus;
  8.  
  9. #pragma comment(lib,"gdiplus.lib")
  10. #pragma comment(lib,"shlwapi.lib")
  11.  
  12. float  SCALE=1.0f;
  13.  
  14. Bitmap * pbitmap = NULL;
  15. BitmapData bitmap_data;
  16. char * data = NULL;
  17. int* hist = new int[256];
  18. int menu;
  19. bool draw_histogram = false;
  20.  
  21. #define ARRAY_LEN(x) (sizeof(x)/sizeof((x)[0]))
  22.  
  23. bool get_file_name(wchar_t * buf, size_t buf_size)
  24. {
  25.         static wchar_t old_path[260]=L"";
  26.  
  27.         if(!GetFileNameFromBrowse(NULL, old_path, ARRAY_LEN(old_path),
  28.                         L".", NULL, L"Raster images\0*.bmp;*.gif;*.png;*.jpg\0\0", L"OKG - select file"))
  29.                 return false;
  30.     if(buf_size < wcslen(old_path)*sizeof(wchar_t))
  31.                 return false;
  32.  
  33.         wcscpy(buf,old_path);
  34.         PathRemoveFileSpecW(old_path);
  35.         return true;
  36. }
  37.  
  38. void reshape(int x, int y)
  39. {
  40.   glViewport(0,0,x,y);
  41.  
  42.   glMatrixMode(GL_PROJECTION);
  43.   glLoadIdentity();
  44.   gluOrtho2D(0,x,0,y);
  45. }
  46.  
  47. void display(void)
  48. {
  49.   // clear buffer
  50.   glClearColor(0.f,0.f,.5f,0.f);
  51.   glClear(GL_COLOR_BUFFER_BIT);
  52.  
  53.   // set raster position and scale
  54.   glRasterPos2f(0,SCALE*(bitmap_data.Height-1));
  55.   glPixelZoom(SCALE,-SCALE);
  56.  
  57.   // setup format
  58.   glPixelStorei(GL_UNPACK_ROW_LENGTH, -1);
  59.   glPixelStorei(GL_UNPACK_ALIGNMENT,  bitmap_data.Stride % 4);
  60.   // draw
  61.   glDrawPixels(bitmap_data.Width, bitmap_data.Height, GL_BGR_EXT, GL_UNSIGNED_BYTE,
  62.         bitmap_data.Scan0);
  63.   if(draw_histogram) {
  64.                 // yellow polygon
  65.                 glBegin(GL_LINES);
  66.                 glColor3f(1.f,1.f,0.f);
  67.                 for(unsigned i=0; i<256; i++)
  68.                 {
  69.                         glVertex2f(i, 0);
  70.                         glVertex2f(i, hist[i]);
  71.                 }
  72.                 glEnd();
  73.   }
  74.  
  75.   glFlush();
  76.  
  77.   glutSwapBuffers();
  78.  
  79. }
  80.  
  81. void menufunc(int val)
  82. {
  83.   switch(val) {
  84.     case 1:
  85.       {
  86.         if(pbitmap) {
  87.           pbitmap->UnlockBits(&bitmap_data);
  88.           delete pbitmap;
  89.           pbitmap = NULL;
  90.         }
  91.  
  92.         wchar_t fname[100] = L"";
  93.         if(get_file_name(fname,sizeof fname))
  94.         {
  95.           pbitmap = Bitmap::FromFile(fname);
  96.           if(pbitmap && pbitmap->GetLastStatus() == Gdiplus::Ok)
  97.           {
  98.             pbitmap->LockBits(&Rect(0,0,pbitmap->GetWidth(), pbitmap->GetHeight()),
  99.               ImageLockModeRead, PixelFormat24bppRGB, &bitmap_data);
  100.  
  101.             SCALE = min(1.0f, 680.f/bitmap_data.Height);
  102.             SCALE = min(SCALE,1020.f/bitmap_data.Width/SCALE);
  103.            
  104.             glutReshapeWindow(bitmap_data.Width*SCALE,bitmap_data.Height*SCALE);
  105.           }
  106.         }
  107.         else
  108.         {
  109.           bitmap_data.Scan0 = 0;
  110.           bitmap_data.Width = bitmap_data.Height = 0;
  111.         }
  112.         break;
  113.       }
  114.         case 2:
  115.                 {
  116.                         for(int i = 0; i < 3 * bitmap_data.Width * bitmap_data.Height; i += 3){
  117.                                 *((BYTE*)bitmap_data.Scan0 + i) = 0.119 * *((BYTE*)bitmap_data.Scan0 + i) + 0.587 * *((BYTE*)bitmap_data.Scan0 + i + 1) +  0.299 * *((BYTE*)bitmap_data.Scan0 + i + 2);
  118.                                 *((BYTE*)bitmap_data.Scan0 + i + 1) =0.119 * *((BYTE*)bitmap_data.Scan0 + i) + 0.587 * *((BYTE*)bitmap_data.Scan0 + i + 1) +  0.299 * *((BYTE*)bitmap_data.Scan0 + i + 2);
  119.                                 *((BYTE*)bitmap_data.Scan0 + i + 2) = 0.119 * *((BYTE*)bitmap_data.Scan0 + i) + 0.587 * *((BYTE*)bitmap_data.Scan0 + i + 1) +  0.299 * *((BYTE*)bitmap_data.Scan0 + i + 2);
  120.                         }
  121.                         break;
  122.                    }
  123.         case 3:
  124.                 {
  125.                        
  126.                         for (int j = 0; j < 256 ; j ++){
  127.                                 hist[j]=0;
  128.                         }
  129.                         for(int i = 0; i < 3 * bitmap_data.Width * bitmap_data.Height; i += 3){
  130.                                 hist[(int)(0.119 * *((BYTE*)bitmap_data.Scan0 + i) + 0.587 * *((BYTE*)bitmap_data.Scan0 + i + 1) +  0.299 * *((BYTE*)bitmap_data.Scan0 + i + 2))] += 1;
  131.                         }
  132.                         double max = hist[0];
  133.                         for( int k = 0; k < 256; k++){
  134.                                 if(hist[k] > max){
  135.                                         max = hist[k];
  136.                                 }
  137.                         }
  138.                         double part = 1000 / max;
  139.                        
  140.                         for (int j = 0; j < 256 ; j ++){
  141.                                 hist[j]=hist[j] * part;
  142.                         }
  143.                         draw_histogram = true;
  144.  
  145.                        
  146.                 }
  147.        
  148.   }
  149.  
  150.  glutPostRedisplay();
  151.  
  152. }
  153.  
  154. int main(int argc, char* argv[])
  155. {
  156.  
  157.   // initialize GDI+
  158.   ULONG_PTR token;
  159.   GdiplusStartupInput inp;
  160.   int status = GdiplusStartup(&token,&inp,0);
  161.  
  162.   // create initial empty bitmap
  163.   pbitmap = new Bitmap(100,100,PixelFormat24bppRGB);
  164.   pbitmap->LockBits(&Rect(0,0,pbitmap->GetWidth(),pbitmap->GetHeight()),
  165.     ImageLockModeRead,
  166.     PixelFormat24bppRGB,&bitmap_data);
  167.  
  168.   glutInit(&argc, argv);
  169.  
  170.   // set-up window
  171.   glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
  172.   glutInitWindowPosition(100,100);
  173.   glutInitWindowSize(min(100,bitmap_data.Width*SCALE), min(100,bitmap_data.Height*SCALE));
  174.   int w = glutCreateWindow("OKG");
  175.  
  176.   // attach menu to right button
  177.   glutCreateMenu(menufunc);
  178.   glutAddMenuEntry("Open File ...",1);
  179.   glutAddMenuEntry("GrayScale ...",2);
  180.   glutAddMenuEntry("Histogram ...",3);
  181.   glutAttachMenu(GLUT_RIGHT_BUTTON);
  182.  
  183.   // set-up callbacks
  184.   glutDisplayFunc(display);
  185.   glutReshapeFunc(reshape);
  186.  
  187.   glutMainLoop();
  188.  
  189.   delete pbitmap;
  190.  
  191.   // deinitializa GDI+
  192.   GdiplusShutdown(token);
  193.  
  194.   return 0;
  195. }