1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
.386
.model flat,stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc
include \masm32\include\gdi32.inc ; pour SetDibToDevice()
include \masm32\include\comdlg32.inc ; pour GetOpenFileName()
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\gdi32.lib
includelib \masm32\lib\comdlg32.lib
.data
ClassName db "WinClass",0
AppName db "Image",0
MenuName db "WinMenu",0
; filtre de fichier de la boite de dialogue (OpenFileName)
strFilter db "Image (*.bmp)",0,"*.bmp",0
db "Image (*.jpg)",0,"*.jpg",0
db "Image (*.gif)",0,"*.gif",0
db "Image (*.png)",0,"*.png",0, 0 ;le dernier "0" indique la fin du filtre
DefExtn db "jpg",0 ;fichier par default
.data?
hInstance HINSTANCE ? ; instance programme
wc WNDCLASSEX<> ; classe fenetre
msg MSG<> ; message feneter
hwnd HWND ? ; handle fenetre
ofn OPENFILENAME <> ;sctructure file
szFileName db MAX_PATH DUP (?)
szTitleName db MAX_PATH DUP (?)
ps PAINTSTRUCT<>
hdc HDC ?
pbmfh DD ? ; Pointeur de structure BITMAPFILEHEADER
pbmi DD ? ; Pointeur de structure BITMAPINFO
pBits DD ?
cxDib DD ?
cyDib DD ?
cxBitMap DD ? ; largeur
cyBitMap DD ? ; hauteur
hMemory DD ? ; Pointeur du bloc memoire alloué
hFile DD ? ; pointeur de fichier
ptrMSB DD ? ; taille du fichier mot poid fort
ptrLSB DD ? ; taille du fichier mot poid faible
ByteRead DD ? ;nombre d'octets lu (donnee)
.const
IDM_OPEN equ 1
MAX_PATH equ 260
.code
start:
; /###################################\
; (### CREATION D'UNE FENETRE ###)
; \###################################/
invoke GetModuleHandle, NULL
mov hInstance,eax
mov wc.cbSize,SIZEOF WNDCLASSEX
mov wc.style, CS_HREDRAW or CS_VREDRAW
mov wc.lpfnWndProc, OFFSET WndProc
mov wc.cbClsExtra,NULL
mov wc.cbWndExtra,NULL
push hInstance
pop wc.hInstance
mov wc.hbrBackground,COLOR_BTNFACE+1
mov wc.lpszMenuName,OFFSET MenuName
mov wc.lpszClassName,OFFSET ClassName
invoke LoadIcon,hInstance,1
mov wc.hIcon,eax
invoke LoadIcon,hInstance,2
mov wc.hIconSm,eax
invoke LoadCursor,NULL,IDC_ARROW
mov wc.hCursor,eax
invoke RegisterClassEx, OFFSET wc
INVOKE CreateWindowEx,WS_EX_CLIENTEDGE,\
OFFSET ClassName,\
OFFSET AppName,\
WS_OVERLAPPEDWINDOW,\
10,10,\
500,300,NULL,NULL,\
hInstance,NULL
mov hwnd,eax
INVOKE ShowWindow, hwnd,SW_SHOWNORMAL
INVOKE UpdateWindow, hwnd
.WHILE TRUE
INVOKE GetMessage,OFFSET msg,NULL,0,0
.BREAK .IF (!eax)
INVOKE TranslateMessage, OFFSET msg
INVOKE DispatchMessage, OFFSET msg
.ENDW
mov eax,msg.wParam
invoke ExitProcess,eax
WndProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
LOCAL success:DWORD
.IF uMsg==WM_DESTROY
.IF hMemory
invoke GlobalUnlock, pbmfh ; deverouillage bloc memoire
invoke GlobalFree, hMemory ; delocation memoire
.ENDIF
invoke PostQuitMessage,NULL
; /###################################\
; (### AFFICHAGE DE L'IMAGE ###)
; \###################################/
.ELSEIF uMsg==WM_PAINT
invoke BeginPaint,hWnd, ADDR ps
mov hdc,eax
.IF pbmfh
; affichage de l'image dans la zone client
invoke SetDIBitsToDevice,hdc,10,10,cxDib,cyDib,0,0,0,cyDib,pBits,pbmi,DIB_RGB_COLORS
.ENDIF
invoke EndPaint, hWnd, ADDR ps
ret
; /############################\
; (### COPIE L'IMAGE EN RAM ###)
; \############################/
.ELSEIF uMsg==WM_COMMAND
mov eax,wParam
.IF ax==IDM_OPEN
; initialisation de la structure de fichier
; --------------------------------------------
mov ofn.lStructSize, sizeof OPENFILENAME
push hWnd
pop ofn.hWndOwner
mov ofn.hInstance, 0
mov ofn.lpstrFilter, offset strFilter
mov ofn.lpstrCustomFilter, 0
mov ofn.nMaxCustFilter, 0
mov ofn.nFilterIndex, 0
push OFFSET szFileName
pop ofn.lpstrFile
mov ofn.nMaxFile, MAX_PATH
push OFFSET szTitleName
pop ofn.lpstrFileTitle
mov ofn.nMaxFileTitle, MAX_PATH
mov ofn.lpstrInitialDir, 0
mov ofn.lpstrTitle, 0
mov ofn.Flags, 0
mov ofn.nFileOffset, 0
mov ofn.nFileExtension, 0
mov ofn.lpstrDefExt,offset DefExtn ; extension par default : JPG
mov ofn.lCustData, 0
mov ofn.lpfnHook, 0
mov ofn.lpTemplateName, 0
:ouverture du fichier (boite de dialogue)
invoke GetOpenFileName, ADDR ofn
.IF !eax
ret ; ouverture annuler
invoke MessageBox,NULL,NULL,NULL,NULL
.ENDIF
.IF pbmfh ; If there's an existing DIB, free the memory
invoke GlobalUnlock, pbmfh
invoke GlobalFree, hMemory
.ENDIF
;chargement de l'image en memoire (RAM)
;----------------------------------------------------
invoke CreateFile,ofn.lpstrFile,\
GENERIC_READ,\
FILE_SHARE_READ,\
0,\
OPEN_EXISTING,\
FILE_FLAG_SEQUENTIAL_SCAN,\
0
mov hFile,eax ;prend l'handle de l'entete du fichier
; fichier introuvable
.IF eax==INVALID_HANDLE_VALUE
jmp @F
badopen db"n'arrive pas à charger le fichier!",0
@@:
invoke MessageBox,NULL,OFFSET badopen,NULL,NULL
ret ; erreur
.ENDIF
; recuperation de la taille du fichier (taille donnée)
invoke GetFileSize,hFile,OFFSET ptrMSB ;taille des info du fichier
mov ptrLSB,eax
.if ptrMSB
invoke CloseHandle, hFile
jmp @F
badsize db"Le fichier est trop volumineu!",0
@@:
invoke MessageBox,NULL,OFFSET badsize,NULL,NULL
ret
.endif
; alloue de la memoire ram
invoke GlobalAlloc,GMEM_MOVEABLE or GMEM_ZEROINIT,ptrLSB
mov hMemory,eax ;handle du block memoire
invoke GlobalLock,hMemory ; verouillage de bloc memoire
mov pbmfh,eax ; got pointer to BITMAPFILEHEADER
mov ebx,eax
.IF pbmfh==NULL ; If it fails
jmp @F
badupload db"n'arrive pas à charger!",0
@@:
invoke MessageBox,NULL,OFFSET badupload,NULL,NULL
invoke CloseHandle, hFile
ret
.ENDIF
;copie de l'image dans notre bloc memoire
invoke ReadFile,hFile,pbmfh,ptrLSB,OFFSET ByteRead,0
mov success,eax
invoke CloseHandle,hFile ; on en a fini avec le ficher (on l'a copié)
mov eax, ByteRead
mov cx, BITMAPFILEHEADER.bfType[ebx] ; Get file type from header; must be BM
mov edx,BITMAPFILEHEADER.bfSize[ebx] ; Size (in bytes) of bitmap
.IF !success || eax != ByteRead || cx != "MB" || edx != ByteRead
invoke GlobalUnlock,pbmfh
invoke GlobalFree, hMemory
ret
.ENDIF
;preparation de la zone cliente
invoke InvalidateRect, hWnd, NULL, TRUE ;Invalidate client area for later update
mov eax, sizeof BITMAPFILEHEADER ; BITMAPINFO immediately follows
add eax,pbmfh
mov pbmi,eax
mov esi,eax ;save in reg as well
mov eax,BITMAPFILEHEADER.bfOffBits[ebx]; offset from BMFH struct to pix bits
add eax,ebx
mov pBits,eax ; This is a pointer to the actual DIB pixel bits
.IF BITMAPINFO.bmiHeader.biSize[esi]== sizeof BITMAPCOREHEADER
; width & height are WORDs
xor eax,eax ; Make sure we clear high word
mov ax, BITMAPCOREHEADER.bcWidth[esi] ; Width of bitmap
mov cxDib, eax
mov ax, BITMAPCOREHEADER.bcHeight[esi] ; Height
mov cyDib, eax
.ELSE ; width & heght are DWORDs
xor eax,eax ; Make sure we clear high word
mov eax, BITMAPINFO.bmiHeader.biWidth[esi]
mov cxDib, eax
mov eax, BITMAPINFO.bmiHeader.biHeight[esi]
.IF eax < 1
neg eax ; Get abs value
.ENDIF
mov cyDib, eax
.ENDIF
.ENDIF
ret
.ELSE
invoke DefWindowProc,hWnd,uMsg,wParam,lParam
ret
.ENDIF
xor eax,eax
ret
WndProc endp
end start |
Partager