News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

I would like a lesson

Started by Grincheux, December 08, 2015, 05:18:30 AM

Previous topic - Next topic

Grincheux

I come back with my grey function.


Effect_Grey_Average      PROC   __lpImageBuffer:LPBYTE


;                  LOCAL   time,time_low,time_high:DWORD
;                  LOCAL   mhz:DWORD ; 150000000      ;// 150 MHz processor;
;
;                  mov      mhz,150000000
;                  rdtsc                        ;// Read time stamp to EAX
;                  mov      time_low, eax
;                  mov      time_high, edx
;
                  push   edi


                  mov      edi,__lpImageBuffer
                  mov      ecx,[edi].BITMAPINFO.bmiHeader.biSizeImage
                  add      edi,SIZEOF BITMAPINFO
                  jmp      @Loop

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

@Loop :

                  prefetch   [edi]

                  movzx   eax,Byte Ptr [edi + 2]               ; Red
                  movzx   edx,Byte Ptr [edi + 1]               ; Green
                  add      eax,edx
                  mov      dl,Byte Ptr [edi + 0]               ; Blue
                  add      eax,edx
                  mov      al,Byte Ptr [OFFSET TabDiv3 + eax]

                  mov      ah,al
                  shl      eax,8
                  mov      al,ah

                  mov      [edi],eax

                  add      edi,SIZEOF DWord
                  sub      ecx,SIZEOF DWord
                  jnz      @Loop

                  pop      edi

;                  rdtsc
;                  sub      eax,time_low         ;// Find the difference
;                  sub      edx,time_high
;                  div      mhz                  ;// Unsigned divide EDX:EAX by mhz
;                  mov      time,eax

                  ret
Effect_Grey_Average      ENDP

Do I use it correctly? : prefetch   [edi]
I would like to use Siekmanski's function but there are compile errors for the registers xmm0...
                  .686p                     ; create 32 bit code
                  .MMX                      ; enable MMX instructions
                  .XMM                      ; enable SSE instructions
                  .MODEL flat, stdcall      ; 32 bit memory model
                  OPTION casemap :none      ; case sensitive

Kenavo (Bye)
----------------------
Help me if you can, I'm feeling down...

jj2007

Quote from: Grincheux on December 10, 2015, 12:49:41 AMI would like to use Siekmanski's function but there are compile errors for the registers xmm0...
                  .686p                     ; create 32 bit code
                  .MMX                      ; enable MMX instructions
                  .XMM                      ; enable SSE instructions
                  .MODEL flat, stdcall      ; 32 bit memory model
                  OPTION casemap :none      ; case sensitive

Use JWasm or AsmC. The old ML 6.14 doesn't understand SSE2.

Siekmanski

You can try to include these SSE2 macros for MASM 6.14 by daydreamer aka Magnus Svensson.
Creative coders use backward thinking techniques as a strategy.

hutch--

Philippe,

Just get a later version of ML.EXE. My Win7 64 SDK had version 10 in it and it handles most of the later SSE instructions.

Grincheux

Does this instruction is a good idea for being faster?

Quoteprefetch   [edi]
Kenavo (Bye)
----------------------
Help me if you can, I'm feeling down...

Grincheux

Thanks Hutch and Siekmanski.


I have updated ML.exe rather than using JWasm.

Kenavo (Bye)
----------------------
Help me if you can, I'm feeling down...

hutch--

Philippe,

From my experience the instruction (hint),

prefetch   [edi]


was only useful on a PIV and then it was hard to clock the difference.

Grincheux




It's ok with Siekmanski's instructions.
I have been obliged to change many things.
The buffer was aligned to a multiple of 4, pgm crashed :eusa_naughty:
It made changes to align it at a multiple of 16 and that' ok  :greenclp:
I don't understand why dividing by 85 (256 / 3) div3 gives a good result result on grey conversion.



div3 EQU 256/3
;MagicDiv3            DWord   0aaaaaaabh,0aaaaaaabh,0aaaaaaabh,0aaaaaaabh
MagicDiv3            DWord   div3,div3,div3,div3
ByteMask            DWord   000000ffh,000000ffh,000000ffh,000000ffh
RoundUp               DWord   3,3,3,3


                  .Code
;   -------------------------------------------------------------------------------------------------------------


Effect_0014            PROC   __lpImageBuffer:LPBYTE
                  LOCAL   _Counter128:DWord


                  push         edi


                  mov      edi,__lpImageBuffer
                  mov      ecx,[edi].IMAGEINFOS.Bmi.bmiHeader.biSizeImage
                  lea      edi,[edi].IMAGEINFOS.Start


                  mov      edx,ecx
                  shr      edx,4                  ; SizeImage / 128
                  push      edx
                  shl      edx,4
                  sub      ecx,edx                  ; SizeImage - (SizeImage / 128)
                  pop      eax
                  jmp      @Loop_128


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


@Loop_128 :


                  movaps   xmm0,OWord Ptr [edi]
                  movaps   xmm1,OWord Ptr ByteMask
                  movaps   xmm2,xmm0   ; __ B3 G3 R3,  __ B2 G2 R2,  __ B1 G1 R1,  __ B0 G0 R0
                  psrld   xmm0,8      ; __ __ B3 G3,  __ __ B2 G2,  __ __ B1 G1,  __ __ B0 G0
                  movaps   xmm3,xmm0   ; __ __ B3 G3,  __ __ B2 G2,  __ __ B1 G1,  __ __ B0 G0
                  psrld      xmm0,8      ; __ __ __ B3,  __ __ __ B2,  __ __ __ B1,  __ __ __ B0
                  pand      xmm2,xmm1   ; __ __ __ R3,  __ __ __ R2,  __ __ __ R1,  __ __ __ R0
                  pand      xmm3,xmm1   ; __ __ __ G3,  __ __ __ G2,  __ __ __ G1,  __ __ __ G0
                  pand         xmm0,xmm1   ; __ __ __ B3,  __ __ __ B2,  __ __ __ B1,  __ __ __ B0
                  paddd   xmm0,xmm3   ; B + G
                  paddd   xmm0,xmm2   ; B + G + R
                  paddd   xmm0,OWord Ptr RoundUp
                  pmullw   xmm0,OWord Ptr MagicDiv3
                  psrld      xmm0,8      ; __ __ __ A3,  __ __ __ A2,  __ __ __ A1,  __ __ __ A0
                  movaps   xmm1,xmm0
                  movaps   xmm2,xmm0
                  pslld   xmm1,8      ; __ __ A3 __,  __ __ A2 __,  __ __ A1 __,  __ __ A0 __
                  pslld   xmm2,16     ; __ A3 __ __,  __ A2 __ __,  __ A1 __ __,  __ A0 __ __
                  pxor   xmm0,xmm1   ; __ __ A3 A3,  __ __ A2 A2,  __ __ A1 A1,  __ __ A0 A0
                  pxor   xmm0,xmm2   ; __ A3 A3 A3,  __ A2 A2 A2,  __ A1 A1 A1,  __ A0 A0 A0
                  movaps   OWord Ptr [edi],xmm0 ; A3=130 A2=107 A1=177 A0=70;


                  add      edi,4 * (SIZEOF DWord)
                  sub      eax,1
                  jnz      @Loop_128


                  sub      ecx,0
                  jz      @EndLoop


@Loop :


                  prefetch   [edi]


                  movzx   eax,Byte Ptr [edi + 2]               ; Red
                  movzx   edx,Byte Ptr [edi + 1]               ; Green
                  add      eax,edx
                  mov      dl,Byte Ptr [edi + 0]               ; Blue
                  add      eax,edx
                  mov      al,Byte Ptr [OFFSET TabDiv3 + eax]


                  mov      ah,al
                  shl      eax,8
                  mov      al,ah

                  mov      [edi],eax

                  add      edi,SIZEOF DWord
                  sub      ecx,SIZEOF DWord
                  jnz      @Loop

@EndLoop :

                  pop      edi

                  ret
Effect_0014            ENDP


[/code]
Kenavo (Bye)
----------------------
Help me if you can, I'm feeling down...

Siekmanski

Quote from: Grincheux on December 10, 2015, 04:15:36 PM
I don't understand why dividing by 85 (256 / 3) div3 gives a good result result on grey conversion.

To do a multiplication instead of a division by 3:

MagicDiv3 = 256/3 = 85
color white = 255,255,255 ( RGB )
R+G+B = 255+255+255 = 765
765+3=768 (+3 for round up)

average = 768*85/256 = 255

        paddd   xmm0,xmm3   ; B + G
        paddd   xmm0,xmm2   ; B + G + R
        paddd   xmm0,OWord Ptr RoundUp   ; +3
        pmullw  xmm0,OWord Ptr MagicDiv3  ; *85
        psrld     xmm0,8  ; /256
Creative coders use backward thinking techniques as a strategy.

dedndave

did you go to Arizona State University ?   :biggrin:

Siekmanski

#25
I made a wrong post sorry...
Creative coders use backward thinking techniques as a strategy.

Grincheux

Arizona State University Cheerleaders
Kenavo (Bye)
----------------------
Help me if you can, I'm feeling down...

Siekmanski

Rather see those girls in full color than in grayscale.  :biggrin:
Creative coders use backward thinking techniques as a strategy.

GoneFishing

You should use Lena for image processing  ;)

hutch--

Guys, one favour here, while I think pretty girls are wonderful things, if the forum starts to get titz 'n arse images posted then it effects those folks who clock in from work where they likely have a porn filter. Just keep it to pretty faces and it does not come back at me.