00001
00002
00003
00004 #include "stdafx.h"
00005
00006 void usage()
00007 {
00008 printf( "Usage: asview [-h]|[image]\n");
00009 printf( "Where: image - filename of the image to display.\n");
00010 }
00011
00012 LRESULT CALLBACK MyWindowProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
00013 void show_system_error();
00014
00015
00016 HINSTANCE hinst;
00017 HWND hWnd = NULL ;
00018 void *bmbits = NULL ;
00019 BITMAPINFO *bmi = NULL ;
00020
00021
00022 int APIENTRY
00023 WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR lpCmdLine, int nCmdShow)
00024 {
00025 char *image_file = "../apps/rose512.jpg" ;
00026
00027
00028 ASImage *im ;
00029 WNDCLASSEX wnd_class ;
00030 ASVisual *asv ;
00031 MSG msg;
00032
00033 if( lpCmdLine != NULL && strncmp( lpCmdLine, "-h", 2 ) == 0 )
00034 {
00035 usage();
00036 return 0;
00037 }else if( lpCmdLine != NULL && strlen(lpCmdLine) > 0 )
00038 image_file = lpCmdLine ;
00039 else
00040 usage();
00041
00042 if( image_file[0] == '\"' )
00043 {
00044 int i = 0;
00045 while( image_file[i+1] != '\0' && image_file[i+1] != '\"')
00046 {
00047 image_file[i] = image_file[i+1] ;
00048 ++i ;
00049 }
00050 image_file[i] = '\0' ;
00051 }
00052
00053 asv = create_asvisual( NULL, 0, 0, NULL );
00054
00055 im = file2ASImage( image_file, 0xFFFFFFFF, SCREEN_GAMMA, 0, NULL );
00056 if( im == NULL )
00057 {
00058 MessageBox( NULL, "Unable to load image from file.", image_file, MB_OK | MB_ICONINFORMATION );
00059 return 0 ;
00060 }
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070 bmbits = NULL ;
00071
00072 bmi = ASImage2DBI( asv, im, 0, 0, im->width, im->height, &bmbits );
00073
00074 if( bmi == NULL )
00075 {
00076 MessageBox( NULL, "Failed to convert image into Windows bitmap.", image_file, MB_OK | MB_ICONINFORMATION );
00077 return 0 ;
00078 }
00079
00080 memset( &wnd_class, 0x00, sizeof(wnd_class));
00081 wnd_class.cbSize = sizeof(wnd_class);
00082 wnd_class.hInstance = hInstance ;
00083 wnd_class.lpszClassName = "ASView" ;
00084 wnd_class.lpfnWndProc = MyWindowProc ;
00085 wnd_class.hIcon = LoadIcon((HINSTANCE) NULL, IDI_APPLICATION);
00086 wnd_class.hCursor = LoadCursor((HINSTANCE) NULL, IDC_ARROW);
00087 wnd_class.hbrBackground = (struct HBRUSH__ *)GetStockObject(WHITE_BRUSH);
00088
00089 if( !RegisterClassEx( &wnd_class ) )
00090 {
00091 show_system_error();
00092 return 0 ;
00093 }
00094
00095 hinst = hInstance ;
00096
00097 hWnd = CreateWindow( "ASView", image_file,
00098 WS_OVERLAPPEDWINDOW,
00099 CW_USEDEFAULT, CW_USEDEFAULT,
00100 bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight,
00101 (HWND) NULL, (HMENU)NULL, hinst, NULL );
00102 if (!hWnd)
00103 {
00104 show_system_error();
00105 return FALSE;
00106 }
00107
00108
00109 ShowWindow(hWnd, nCmdShow);
00110 UpdateWindow(hWnd);
00111
00112
00113 while (GetMessage(&msg, hWnd, 0, 0) > 0 )
00114 {
00115 TranslateMessage(&msg);
00116 DispatchMessage(&msg);
00117 }
00118 return 0;
00119 }
00120
00121 LRESULT CALLBACK
00122 MyWindowProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
00123 {
00124 if( uMsg == WM_PAINT )
00125 {
00126
00127 if( bmi != NULL && bmbits != NULL )
00128 {
00129 PAINTSTRUCT ps ;
00130 HDC dc = BeginPaint(hWnd, &ps );
00131 StretchDIBits( dc,
00132 0, 0, bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight,
00133 0, 0, bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight,
00134 bmbits, bmi, DIB_RGB_COLORS, SRCCOPY );
00135 EndPaint(hWnd, &ps );
00136
00137 }
00138 return 0;
00139 }
00140
00141 return DefWindowProc( hwnd, uMsg, wParam, lParam ) ;
00142 }
00143
00144 void
00145 show_system_error()
00146 {
00147 LPVOID lpMsgBuf;
00148 FormatMessage(
00149 FORMAT_MESSAGE_ALLOCATE_BUFFER |
00150 FORMAT_MESSAGE_FROM_SYSTEM |
00151 FORMAT_MESSAGE_IGNORE_INSERTS,
00152 NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf,
00153 0, NULL );
00154 MessageBox( NULL, (LPCTSTR)lpMsgBuf, "ASView System Error", MB_OK | MB_ICONINFORMATION );
00155
00156 LocalFree( lpMsgBuf );
00157 }
00158
00159