News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Images for beginners

Started by Grincheux, December 06, 2015, 06:54:56 PM

Previous topic - Next topic

Grincheux

This is a simple viewer. It loads a file using GDI+ then display the image using StretchBlt.

There are two useful functions :



; ==================================================================================
ALIGN 16
; ==================================================================================

Load_Image PROC __lpszFileName:LPSTR,__lphBitmap:PTR HBITMAP,__lpdwWidth:Ptr DWord,__lpdwHeight:Ptr DWord
LOCAL _hInputImage:DWord
LOCAL _szTmpFileName[MAX_PATH * 2]:Byte

mov eax,__lphBitmap
mov DWord Ptr [eax],NULL

INVOKE MultiByteToWideChar,CP_OEMCP,MB_PRECOMPOSED,__lpszFileName,-1,ADDR _szTmpFileName,SIZEOF _szTmpFileName
INVOKE GdipLoadImageFromFile,ADDR _szTmpFileName,ADDR _hInputImage

test eax,eax
jnz @Error_1

cmp _hInputImage,NULL
je @Error_1

INVOKE GdipGetImageWidth,_hInputImage,__lpdwWidth
INVOKE GdipGetImageHeight,_hInputImage,__lpdwHeight

push 000000h
push __lphBitmap
push _hInputImage
push OFFSET @Error_1
jmp GdipCreateHBITMAPFromBitmap

; **********************************************************************************
ALIGN 16
; **********************************************************************************

@Error_1 :

push _hInputImage
push OFFSET @Exit
jmp GdipDisposeImage

; **********************************************************************************
ALIGN 16
; **********************************************************************************

@Exit :

mov eax,__lphBitmap
mov eax,[eax]
ret
Load_Image ENDP


and


; ==================================================================================
ALIGN 16
; ==================================================================================

ResizeBitmap PROC __dwOriginalWidth:DWord,__dwOriginalHeight:DWord,__dwExpectedWidth:DWord,__dwExpectedHeight:DWord,__lpdwBitmapWidth:LPDWORD,__lpdwBitmapHeight:LPDWORD
LOCAL _dwNewWidth:DWord
LOCAL _dwNewHeight:DWord

mov eax,__dwOriginalWidth
mov edx,__dwOriginalHeight

cmp eax,edx
jg @Horizontal

cmp eax,edx
jl @Vertical

mov eax,__dwExpectedWidth
mov edx,__lpdwBitmapWidth
mov [edx],eax
mov edx,__lpdwBitmapHeight
mov [edx],eax

mov eax,'C'
ret

; **********************************************************************************
ALIGN 16
; **********************************************************************************
; OW = 580 OH = 339 EW = 1366 EH = 685 NW = 1172 = NH = 685 Bikinis 01401.jpg
@Horizontal :

finit

fild __dwOriginalHeight ; 339
fidiv __dwOriginalWidth ; 580 => 339 / 580 = 0.58448275862068965517241379310345
fimul __dwExpectedWidth ; 1366
fistp _dwNewHeight ; 798.40344827586206896551724137931
mov eax,__dwExpectedWidth ; 1366
mov _dwNewWidth,eax

mov eax,__dwExpectedHeight ; 685

cmp _dwNewHeight,eax
jle @Finished

fild __dwOriginalHeight ; 339
fidiv __dwOriginalWidth ; 580 => 339 / 580 = 0.58448275862068965517241379310345
fild __dwExpectedHeight ; 685
fdiv ST(0),ST(1)
fistp _dwNewWidth ; 1171.9764011799410029498525073746

mov eax,__dwExpectedHeight
mov _dwNewHeight,eax

push 'H'
jmp @Finished

; **********************************************************************************
ALIGN 16
; **********************************************************************************

@Finished :

mov eax,_dwNewWidth
mov edx,__lpdwBitmapWidth
mov [edx],eax

mov eax,_dwNewHeight
mov edx,__lpdwBitmapHeight
mov [edx],eax

pop eax
ret

; **********************************************************************************
ALIGN 16
; **********************************************************************************

@Vertical :

finit

fild __dwOriginalWidth ; 381
fidiv __dwOriginalHeight ; 520 => 381 / 520 = 0.73269230769230769230769230769231
fimul __dwExpectedHeight ; 466
fistp _dwNewWidth ; 466 * 0.7326 = 341.43461538461538461538461538462
mov eax,__dwExpectedHeight ; 466
mov _dwNewHeight,eax

mov eax,__dwExpectedWidth

cmp _dwNewWidth,eax
jle @Finished

fild __dwOriginalWidth ; 381
fidiv __dwOriginalHeight ; 520 => 381 / 520 = 0.73269230769230769230769230769231
fidiv __dwExpectedWidth ; 1009 * 0.73269230769231 = 739.28653846153846153846153846154
fistp _dwNewHeight ; 739.28653846153846153846153846154

mov eax,__dwExpectedWidth
mov _dwNewWidth,eax

push 'V'
jmp @Finished
ResizeBitmap ENDP


Optimizations could be made in computing the ratio image but I don't know how to use MMX or SSE.
Hope it can help someone.

http://www.phrio.biz/download/Img32.zip

Grincheux

http://www.phrio.biz/download/Img32.zip

This link would be better

hutch--

Works on my Win7 64 just fine. Tested it on a half meg JPG file and it displays it correctly and scales the image when resizing the window.

Grincheux

At home it runs on WIN10 PRO.

Siekmanski

Creative coders use backward thinking techniques as a strategy.

hutch--

Philippe,

I read your help file and it seems you have a sense of humour as well.  :biggrin: