- // bitmaptest.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <math.h>
- using namespace Gdiplus;
- #pragma comment(lib,"gdiplus.lib")
- #pragma comment(lib,"shlwapi.lib")
- float SCALE=1.0f;
- Bitmap * pbitmap = NULL;
- BitmapData bitmap_data;
- char * data = NULL;
- int* hist = new int[256];
- int menu;
- bool draw_histogram = false;
- #define ARRAY_LEN(x) (sizeof(x)/sizeof((x)[0]))
- bool get_file_name(wchar_t * buf, size_t buf_size)
- {
- static wchar_t old_path[260]=L"";
- if(!GetFileNameFromBrowse(NULL, old_path, ARRAY_LEN(old_path),
- L".", NULL, L"Raster images\0*.bmp;*.gif;*.png;*.jpg\0\0", L"OKG - select file"))
- return false;
- if(buf_size < wcslen(old_path)*sizeof(wchar_t))
- return false;
- wcscpy(buf,old_path);
- PathRemoveFileSpecW(old_path);
- return true;
- }
- void reshape(int x, int y)
- {
- glViewport(0,0,x,y);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluOrtho2D(0,x,0,y);
- }
- void display(void)
- {
- // clear buffer
- glClearColor(0.f,0.f,.5f,0.f);
- glClear(GL_COLOR_BUFFER_BIT);
- // set raster position and scale
- glRasterPos2f(0,SCALE*(bitmap_data.Height-1));
- glPixelZoom(SCALE,-SCALE);
- // setup format
- glPixelStorei(GL_UNPACK_ROW_LENGTH, -1);
- glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap_data.Stride % 4);
- // draw
- glDrawPixels(bitmap_data.Width, bitmap_data.Height, GL_BGR_EXT, GL_UNSIGNED_BYTE,
- bitmap_data.Scan0);
- if(draw_histogram) {
- // yellow polygon
- glBegin(GL_LINES);
- glColor3f(1.f,1.f,0.f);
- for(unsigned i=0; i<256; i++)
- {
- glVertex2f(i, 0);
- glVertex2f(i, hist[i]);
- }
- glEnd();
- }
- glFlush();
- glutSwapBuffers();
- }
- void menufunc(int val)
- {
- switch(val) {
- case 1:
- {
- if(pbitmap) {
- pbitmap->UnlockBits(&bitmap_data);
- delete pbitmap;
- pbitmap = NULL;
- }
- wchar_t fname[100] = L"";
- if(get_file_name(fname,sizeof fname))
- {
- pbitmap = Bitmap::FromFile(fname);
- if(pbitmap && pbitmap->GetLastStatus() == Gdiplus::Ok)
- {
- pbitmap->LockBits(&Rect(0,0,pbitmap->GetWidth(), pbitmap->GetHeight()),
- ImageLockModeRead, PixelFormat24bppRGB, &bitmap_data);
- SCALE = min(1.0f, 680.f/bitmap_data.Height);
- SCALE = min(SCALE,1020.f/bitmap_data.Width/SCALE);
- glutReshapeWindow(bitmap_data.Width*SCALE,bitmap_data.Height*SCALE);
- }
- }
- else
- {
- bitmap_data.Scan0 = 0;
- bitmap_data.Width = bitmap_data.Height = 0;
- }
- break;
- }
- case 2:
- {
- for(int i = 0; i < 3 * bitmap_data.Width * bitmap_data.Height; i += 3){
- *((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);
- *((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);
- *((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);
- }
- break;
- }
- case 3:
- {
- for (int j = 0; j < 256 ; j ++){
- hist[j]=0;
- }
- for(int i = 0; i < 3 * bitmap_data.Width * bitmap_data.Height; i += 3){
- 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;
- }
- double max = hist[0];
- for( int k = 0; k < 256; k++){
- if(hist[k] > max){
- max = hist[k];
- }
- }
- double part = 1000 / max;
- for (int j = 0; j < 256 ; j ++){
- hist[j]=hist[j] * part;
- }
- draw_histogram = true;
- }
- }
- glutPostRedisplay();
- }
- int main(int argc, char* argv[])
- {
- // initialize GDI+
- ULONG_PTR token;
- GdiplusStartupInput inp;
- int status = GdiplusStartup(&token,&inp,0);
- // create initial empty bitmap
- pbitmap = new Bitmap(100,100,PixelFormat24bppRGB);
- pbitmap->LockBits(&Rect(0,0,pbitmap->GetWidth(),pbitmap->GetHeight()),
- ImageLockModeRead,
- PixelFormat24bppRGB,&bitmap_data);
- glutInit(&argc, argv);
- // set-up window
- glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
- glutInitWindowPosition(100,100);
- glutInitWindowSize(min(100,bitmap_data.Width*SCALE), min(100,bitmap_data.Height*SCALE));
- int w = glutCreateWindow("OKG");
- // attach menu to right button
- glutCreateMenu(menufunc);
- glutAddMenuEntry("Open File ...",1);
- glutAddMenuEntry("GrayScale ...",2);
- glutAddMenuEntry("Histogram ...",3);
- glutAttachMenu(GLUT_RIGHT_BUTTON);
- // set-up callbacks
- glutDisplayFunc(display);
- glutReshapeFunc(reshape);
- glutMainLoop();
- delete pbitmap;
- // deinitializa GDI+
- GdiplusShutdown(token);
- return 0;
- }