Gray algorithm

Game development specific discussions.
xywhsoft
Posts: 74
Joined: Aug 28, 2009 6:28
Contact:

Gray algorithm

Postby xywhsoft » Jan 28, 2021 15:09

16bit Low performance algorithm:

Code: Select all

Sub Gray_16(ByVal Surface As Any Ptr,ByVal Pitch As Integer,ByVal h As Integer)
   Dim As Integer x,y
   Dim PD As UShort Ptr
   Dim PC As UShort
   For y = 0 To h-1
      PD = Surface + (y * Pitch)
      For x = 0 To (Pitch \ 2) -1
         PC = PD[x]
         PC = (((PC And MaskR) Shr 11) + (((PC And MaskG) \ 2) Shr 5) + (PC And MaskB)) \ 3
         PC = (PC Shl 11) Or (PC*2 Shl 5) Or PC
         PD[x] = PC
      Next
   Next
End Sub


16bit MMX instruction set optimization algorithm:

Code: Select all

Dim Shared MASKR As ULongInt = &HF800F800F800F800
Dim Shared MASKG As ULongInt = &H07E007E007E007E0
Dim Shared MASKB As ULongInt = &H001F001F001F001F
Dim Shared MASKSHIFT As ULongInt = &H0001000100010001
Dim Shared ADD64 As ULongInt = &H0040004000400040



' It can achieve more than 7 times performance improvement
Sub gray_565_mmx(Scr As Any Ptr, ScrPitch As Integer, Dst As Any Ptr, DstPitch As Integer, x As Integer, y As Integer, sx As Integer, sy As Integer, w As Integer, h As Integer, ColorKey As UShort)
   Dim SrcOffset As Integer = (sy*DstPitch) + (sx*2)
   Dim DstOffset As Integer = (y*ScrPitch) + (x*2)
   Dim KeyColor64 As ULongInt = ColorKey*MASKSHIFT
   w = w*2
   Asm
      mov eax,[DstPitch]
      mov ebx,[ScrPitch]
      mov esi,[Dst]
      add esi,[SrcOffset]
      mov edi,[Scr]
      add edi,[DstOffset]
      
      mov edx,[h]            ' H
      movq mm4,[MASKB]
      
      START:
      mov ecx,[w]            ' iDstW/4
      shr ecx,3
      
      NEXT_POINT:
      movq mm0,[esi]
      movq mm5,[edi]
      
      movq mm1,mm0
      movq mm2,mm0
      movq mm6,mm0
      
      psrlw mm1,6            ' G
      psrlw mm2,11         ' B
      
      pand mm0,mm4
      pand mm1,mm4
      pand mm2,mm4
      
      paddusw mm0,mm1
      paddusw mm0,mm2
      movq mm1,mm0
      
      psrlw mm0,2
      psrlw mm1,3
      paddusw mm0,mm1
      movq mm1,mm0
      pcmpgtw mm1,mm4
      por mm0,mm1
      pand mm0,mm4
      
      movq mm1,mm0
      psllw mm1,6
      
      movq mm2,mm0
      psllw mm2,11
      por mm0,mm1
      por mm0,mm2
      
      pcmpeqw mm6,[KeyColor64]
      pand mm5,mm6
      pandn mm6,mm0
      por mm5,mm6
      
      movq [edi],mm5
      add esi,8
      add edi,8
      dec ecx
      cmp ecx,0
      jnz NEXT_POINT
      
      'NEXT_LINE:
      add esi,eax
      sub esi,[w]
      add edi,ebx
      sub edi,[w]
      
      dec edx
      cmp edx,0
      jnz START
      
      'DONE:
      EMMS
   End Asm
End Sub


32bit Mathematical optimization algorithm::

Code: Select all

      Sub Blend_Gray(SrcAddr As Any Ptr, SrcPitch As Integer, SrcLineS As Integer, DstAddr As Any Ptr, DstPitch As Integer, DstLineS As Integer, w As Integer, h As Integer, param As Integer)
         Dim As UInteger Ptr DstLine, SrcLine
         Dim TmpColor As UInteger
         For y As Integer = 0 To h
            SrcLine = SrcAddr + (SrcPitch * y) + SrcLineS
            DstLine = DstAddr + (DstPitch * y) + DstLineS
            For x As Integer = 0 To w
               TmpColor = SrcLine[x]
               If (TmpColor And COLOR_32) <> COLOR_MASK_32 Then
                  TmpColor = (((TmpColor And COLOR_R) Shr 16) * 1224 + ((TmpColor And COLOR_G) Shr 8) * 2405 + (TmpColor And COLOR_B) * 467) Shr 12
                  TmpColor = (TmpColor Shl 16) Or (TmpColor Shl 8) Or TmpColor
                  DstLine[x] = TmpColor
               EndIf
            Next
         Next
      End Sub
xywhsoft
Posts: 74
Joined: Aug 28, 2009 6:28
Contact:

Re: Gray algorithm

Postby xywhsoft » Jan 28, 2021 15:11

This is some of the results of xGe being eliminated. When sorting out the data today, we found that 16bit image processing is rarely used now. If someone needs to post it.

Return to “Game Dev”

Who is online

Users browsing this forum: No registered users and 3 guests