home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / videotlk.zip / SAMPLES / EX5 / RGBCONV.ASM < prev    next >
Assembly Source File  |  1995-04-27  |  17KB  |  394 lines

  1. ;*****************************************************************************/
  2. ;
  3. ; COPYRIGHT    Copyright (C) 1992 IBM Corporation
  4. ;
  5. ;    The following IBM OS/2 2.1 source code is provided to you solely for
  6. ;    the purpose of assisting you in your development of OS/2 2.x device
  7. ;    drivers. You may use this code in accordance with the IBM License
  8. ;    Agreement provided in the IBM Device Driver Source Kit for OS/2. This
  9. ;    Copyright statement may not be removed.;
  10. ;*****************************************************************************/
  11. ;.386p
  12.         TITLE   RGBCONV.ASM
  13.         .386
  14.         .387
  15.         INCLUDELIB os2386.lib
  16.         INCLUDELIB dde4mbs.lib
  17. CODE32  SEGMENT PARA USE32 PUBLIC 'CODE'
  18. CODE32  ENDS
  19. DATA32  SEGMENT PARA USE32 PUBLIC 'DATA'
  20. DATA32  ENDS
  21. CONST32 SEGMENT PARA USE32 PUBLIC 'CONST'
  22. CONST32 ENDS
  23. BSS32   SEGMENT PARA USE32 PUBLIC 'BSS'
  24. BSS32   ENDS
  25. DGROUP  GROUP CONST32, BSS32, DATA32
  26.         ASSUME  CS:FLAT, DS:FLAT, SS:FLAT, ES:FLAT
  27. DATA32  SEGMENT
  28. ;_DATA   SEGMENT  DWORD USE32 PUBLIC 'DATA'      ; changed from code to data
  29. ;        ASSUME   DS: FLAT
  30.  
  31.  
  32. ;** EXTRN        ConvTable:BYTE
  33. ConvTable      DD   0
  34.  
  35. ;_DATA   ENDS
  36. DATA32  ENDS
  37. BSS32   SEGMENT
  38. BSS32   ENDS
  39. CONST32 SEGMENT
  40. CONST32 ENDS
  41. CODE32  SEGMENT
  42. ;_TEXT   SEGMENT  DWORD USE32 PUBLIC 'CODE'
  43. ;        ASSUME   CS:_TEXT, DS: FLAT, SS: FLAT, ES: FLAT
  44. ;*********************************************
  45. ; Define the C calling macros
  46. LBegin  macro len
  47.         push    ebp
  48.         mov     ebp,esp
  49.         sub     esp,len
  50.         endm
  51. LEnd    macro
  52.         mov     esp,ebp
  53.         pop     ebp
  54.         endm
  55. ;*********************************************
  56.  
  57. ;*********************************************
  58. ; Define the RGB16_to_PAL8 - convert RGB16 to a PAL8 value
  59. RGB16_to_PAL8 macro r_offset, d_offset, w_offset
  60.         mov   ax,   Word Ptr [esi+r_offset]; get the RGB16 value   ; 4
  61.         shr   ax,   1                      ; shift off LSB blue    ; 3
  62.         ror   al,   4                      ; get green in LSB      ; 3
  63.         shr   ax,   1                      ; shift out LSB green   ; 3
  64.         shl   eax,  2                      ; x4 for table index    ; 3
  65.         mov   al,   offset flat:ConvTable[eax+d_offset]            ; 4
  66.         mov   Byte Ptr [edi+w_offset], al  ; Save Converted Value  ; 4
  67.         endm
  68. ;*********************************************
  69.  
  70. ;*********************************************
  71. ; Define the RGB16_to_2PAL8 - convert RGB16 to 2 PAL8 values
  72. RGB16_to_2PAL8 macro r_offset, w2_offset, w_offset
  73.         xor   eax,  eax
  74.         mov   ax,   Word Ptr [esi+r_offset]; get the RGB16 value   ; 4
  75.         shr   ax,   1                      ; shift off LSB blue    ; 3
  76.         ror   al,   4                      ; get green in LSB      ; 3
  77.         shr   ax,   1                      ; shift out LSB green   ; 3
  78.         shl   eax,  2                      ; x4 for table index    ; 3
  79.         mov   eax,  offset flat:ConvTable[eax]                     ; 4
  80.         mov   Word Ptr [edi+w_offset], ax  ; Save Converted Value  ; 4
  81.         ror   eax, 16                                              ; 2
  82.         mov   Word Ptr [edi+w2_offset], ax ; Save Converted Value  ; 4
  83.         endm
  84. ;*********************************************
  85.  
  86. ;*********************************************
  87. ; Define the COPY_ESI_EDI - Copy a word from ESI+offset1 to EDI+offset2
  88. COPY_ESI_EDI macro offsetD, offsetS
  89.         mov   ax,   Word Ptr [esi+offsetS] ; get the Word value   ; 4
  90.         mov   Word Ptr [edi+offsetD], ax   ; save Word Value      ; 2
  91.         endm
  92. ;*********************************************
  93.  
  94. ;*********************************************
  95. ; Define the COPY_ESI_EDI2 - Copy a word from ESI+offS to EDI+offD and
  96. ; EDI+offD+2 and EDI+offD+offL and EDI+offD+offL+2
  97. ; This macro doubles the data
  98. COPY_ESI_EDI2 macro offD, offS, OffL
  99.         mov   ax,   Word Ptr [esi+offS]        ; get the Word value   ; 4
  100.  
  101.                                                ; Double on first scanline
  102.         mov   Word Ptr [edi+offD],        ax   ; save Word Value      ; 2
  103.         mov   Word Ptr [edi+offD+2],      ax   ; save Word Value      ; 2
  104.                                                ; Double on next Scanline
  105.         mov   Word Ptr [edi+offD+offL],   ax   ; save Word Value      ; 2
  106.         mov   Word Ptr [edi+offD+offL+2], ax   ; save Word Value      ; 2
  107.         endm
  108. ;*********************************************
  109.  
  110.  
  111. ;*****************************************************************************;
  112. ; Procedure Name  :     Convert YUV422 to RGB24  and Invert                   ;
  113. ;                                                                             ;
  114. ; Description     :     Convert a Width x Height 16-bit YUV422 image          ;
  115. ;                       to RGB24 (8-Red, 8-Green, 8-Blue format) and          ;
  116. ;                       Inver the image                                  .    ;
  117. ;                                                                             ;
  118. ; Register status :                                                           ;
  119. ;*****************************************************************************;
  120. ALIGN 4
  121. ;_YUV422_to_IRGB24 PROC NEAR
  122.  
  123.     PUBLIC YUV422_TO_IRGB24
  124. YUV422_TO_IRGB24 PROC
  125.  
  126.    ;*The-Stack***********************************
  127.    ; The local variables:
  128.                 luma1                  equ ebp-4
  129.                 luma2                  equ ebp-5
  130.                 backup                 equ ebp-12
  131.  
  132.    ; The passed parameters:
  133. ;                aSrc                    equ ebp+8
  134. ;                aDst                    equ ebp+12
  135. ;                lWidth                  equ ebp+16
  136. ;                lHeight                 equ ebp+20
  137.                 lHeight                 equ ebp+8
  138.                 lWidth                  equ ebp+12
  139.                 aDst                    equ ebp+16
  140.                 aSrc                    equ ebp+20
  141.    ;*The-Stack***********************************
  142.  
  143.                 ; Leave some room on the stack for local variables
  144.                 enter   20,0
  145.                 pushfd
  146.                 pushad
  147.  
  148.                 ; Move the image from source pointer
  149.                 mov     esi,    [aSrc]
  150.  
  151.                 ; Move the image to the destination pointer
  152.                 mov     edi,    [adst]
  153.                 mov     edx,[lHeight]   ; Height of image
  154.                 sub   edx,1             ; less 1 line for start of last line
  155.                 mov   ecx,[lWidth]      ; Size Width in PELs
  156.                 mov   eax,ecx           ;
  157.                 shl   eax,1             ; * 2
  158.                 add   eax,ecx           ; eax = width * 3 (bytes per PEL)
  159.                 mul   EDX               ; EAX*EDX goes into EDX:EAX
  160.                                         ;  EDX will be zero
  161.                 add   EDI,EAX           ; point to last line of output buffer
  162.  
  163.                 ; Length to backup after each scanline in Destination buffer
  164.                 mov     eax,[lwidth]       ; Each Pixel is 3 Bytes (RGB24)
  165.                 shl     eax,1              ; Pixel_width*2
  166.                 add     eax,[lwidth]       ; Pixel_width*2 + Pixel_width = 3
  167.                 shl     eax,1              ; * 2 (back up for next Inverted line)
  168.                 mov    [backup],eax        ; Detla to Previous/Next line in Dest
  169.  
  170.                 ; set direction flag to increment
  171.                 cld
  172.  
  173. ICONVYUV_LINEA:
  174. ;  int 3
  175.                 ; Set ECX to PELs in a ROW
  176.                 mov     ecx,    [lWidth]   ; Width in PELs
  177.                 shr     ecx,1              ; Width / 2
  178.  
  179. ICONV_2PEL:
  180.                 lodsw
  181.                 mov    [luma1],al          ; Save Y = Lumance
  182.                 mov    dh,ah               ; U into DH
  183.  
  184.                 lodsw
  185.                 mov    [luma2],al          ; Save Y = Lumance
  186.                 mov    dl,ah               ; V into DL
  187.  
  188.  
  189.                 call   UnScaleChroma2      ; EDX V = bit 31-16  U = bits 15-0
  190.  
  191.                 movzx  bx,byte ptr [luma1] ; Y
  192.                 call   ConvertYUVToRGB24   ; BL=Red AH=Green AL=Blue
  193.                 shl    ebx,16              ; Move Red up to proper position
  194.                 and    eax,0FFFFh          ; Clear top for byte of Red
  195.                 or     eax,ebx             ; Combine Red in with Green and Blue
  196.                 mov    [edi],eax           ; Store RGB24 to 1 extra byte
  197.                 add    edi,3               ; Only 3 Valid Bytes but 4 written
  198.  
  199.                 movzx  bx,byte ptr [luma2] ; Y
  200.                 call   ConvertYUVToRGB24
  201.                 shl    ebx,16              ; Move Red up to proper position
  202.                 and    eax,0FFFFh          ; Clear top for byte of Red
  203.                 or     eax,ebx             ; Combine Red in with Green and Blue
  204.                 mov    [edi],eax           ; Store RGB24 to 1 extra byte
  205.                 add    edi,3               ; Only 3 Valid Bytes but 4 written
  206.  
  207.  
  208.                 ; Check to see if we are at the end of the line.
  209.                 dec     ecx
  210.                 jnz     ICONV_2PEL
  211.  
  212.                 sub     edi,[backup]       ; Backup to previous line to Invert
  213.                 dec     dword ptr [lHeight]
  214.                 jnz     ICONVYUV_LINEA
  215.  
  216.  
  217.                 ; Restore registers and the stack.
  218.                 popad
  219.                 popfd
  220.                 leave
  221.                 ret
  222.  
  223. ;_YUV422_to_IRGB24 ENDP
  224. YUV422_to_IRGB24 ENDP
  225.  
  226.  
  227. ;ZDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD?
  228. ;3 ConvertYUVToRGB24                                                    3
  229. ;3                                                                      3
  230. ;3 This routine converts one YUV value to a 24 bit RGB value and        3
  231. ;3 returns the result in AX and BL.                                     3
  232. ;3                                                                      3
  233. ;3      U = B-Y         ->      B = U+Y                                 3
  234. ;3      V = R-Y         ->      R = V+Y                                 3
  235. ;3      Y  = 0.299*R + 0.587*G + 0.114*B        ->                      3
  236. ;3      G = 1.704*Y - 0.5094*R - 0.1942*B                               3
  237. ;3                                                                      3
  238. ;3 Entry:       DX  = U = B-Y                                           3
  239. ;3              EDX = V = R-Y (bit 31-16)                                          3
  240. ;3              BX  = Y = luma component                                3
  241. ;3 Exit:        AH  = Green                                             3
  242. ;3              AL  = Blue                                              3
  243. ;3              BL  = Red                                               3
  244. ;3 Note:        U and V are signed numbers.  Luma is unsigned.          3
  245. ;3 Modifies:    AX,BX,DX                                                3
  246. ;@DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDY
  247. ALIGN 4
  248. ;ConvertYUVToRGB24 PROC NEAR
  249. ConvertYUVToRGB24 PROC
  250.         push    edx
  251. Blue:   mov     ax,dx
  252.         add     ax,bx                  ;AX = U + Y = Blue
  253.         jge     @F
  254.         xor     ax,ax
  255.         jmp     Red
  256. @@:     cmp     ax,0ffh
  257.         jle     Red
  258.         mov     ax,0ffh
  259.  
  260. Red:    shr     edx,16                  ; V
  261.         add     dx,bx                   ;DX = V + Y = Red
  262.         jge     @F
  263.         xor     dx,dx
  264.         jmp     have_red_and_blue
  265. @@:     cmp     dx,0ffh
  266.         jle     have_red_and_blue
  267.         mov     dl,0ffh
  268.  
  269. have_red_and_blue:
  270.         mov     dh,al
  271.         xchg    dx,bx                   ;BH = Blue   BL = Red   DX = Luma
  272.  
  273. Green:  mov     ax,436                  ;(436/256) = 1.703
  274.         mul     dx
  275.         mov     dh,dl
  276.         mov     dl,ah                   ;DX = 1.704 * luma
  277.  
  278.         mov     al,bl                   ;Red
  279.         mov     ah,130                  ;(130/256) = 0.5078
  280.         mul     ah
  281.         mov     al,ah
  282.         xor     ah,ah
  283.         sub     dx,ax                   ;DX = 1.704*luma - 0.508*red
  284.         mov     al,bh                   ;Blue
  285.         mov     ah,50
  286.         mul     ah
  287.         mov     al,ah
  288.         xor     ah,ah
  289.         sub     dx,ax                   ;DX = 1.704*luma - 0.508*red - 0.195*blue
  290.         jge     check_green_overflow
  291.         xor     dl,dl                   ;Set green to zero.
  292.         jmp     have_rgb
  293.  
  294. check_green_overflow:
  295.         cmp     dh,0                    ;Is green > 255?
  296.         je      have_rgb                ;No.
  297.         mov     dl,0ffh                 ;Yes, set it to 255.
  298.  
  299. have_rgb:
  300.         mov     al,bh
  301.         mov     ah,dl                   ;AH = Green   AL = Blue   BL = Red
  302.         pop     edx
  303.         ret
  304. ConvertYUVToRGB24 ENDP
  305.  
  306. ;ZDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD?
  307. ;3 UnScaleChroma                                                        3
  308. ;3                                                                      3
  309. ;3 This routine multiplies the two scaled 8 bits chroma components, U   3
  310. ;3 and V by the fractions 229/127 and 179/127, respectively.  The       3
  311. ;3 results are stored in the local variables U and V.                   3
  312. ;3                                                                      3
  313. ;3 Entry:       DL = scaled V                                           3
  314. ;3              DH = scaled U                                           3
  315. ;3 Exit:        EDX V = unscaled V bits 31-16                           3
  316. ;3              EDX U = unscaled U bits 15-0                            3
  317. ;3 Destroys:    AX,BX,DX                                                3
  318. ;@DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDY
  319. ALIGN 4
  320. ;UnScaleChroma PROC NEAR
  321. UnScaleChroma PROC
  322.         push    ecx
  323.         mov     bl,dh                   ;BL = scaled U
  324.         mov     al,dl
  325.         cbw                             ;AX = scaled V (sign extended)
  326.         mov     dx,361                  ;(361/256 = 179/127)
  327.         imul    dx
  328.         mov     al,ah
  329.         mov     ah,dl
  330.         mov     cx,ax                    ;CX = unscaled V
  331.  
  332.         mov     al,bl
  333.         cbw                             ;AX = scaled U (sign extended)
  334.         mov     dx,456                  ;(456/256 = 226/127)
  335.         imul    dx
  336.         mov     al,ah
  337.         mov     ah,dl
  338. ;       mov     U,ax                    ;AX = unscaled U
  339.         xor     edx,edx
  340.         mov     dx,ax
  341.         shl     ecx,16
  342.         or      edx,ecx                 ; V bit 31-16 and U bit 15-0 of EDX
  343.         pop     ecx
  344.         ret
  345. UnScaleChroma ENDP
  346.  
  347.  
  348. ;ZDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD?
  349. ;3 UnScaleChroma2                                                       3
  350. ;3                                                                      3
  351. ;3 This routine multiplies the two scaled 8 bits chroma components, U   3
  352. ;3 and V by the fractions 229/127 and 179/127, respectively.  The       3
  353. ;3 results are stored in the local variables U and V.                   3
  354. ;3                                                                      3
  355. ;3 Note:... on input U and V are normalize to -127 to +127              3
  356. ;3                                                                      3
  357. ;3 Entry:       DL = scaled V                                           3
  358. ;3              DH = scaled U                                           3
  359. ;3 Exit:        EDX V = unscaled V bits 31-16                           3
  360. ;3              EDX U = unscaled U bits 15-0                            3
  361. ;3 Destroys:    AX,BX,DX                                                3
  362. ;@DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDY
  363. ALIGN 4
  364. ;UnScaleChroma2 PROC NEAR
  365. UnScaleChroma2 PROC
  366.         push    ecx
  367.         mov     bl,dh                   ;BL = scaled U
  368.         movzx   ax,dl                   ; V
  369.         sub     ax,80h                  ;AX = scaled V
  370.         mov     dx,361                  ;(361/256 = 179/127)
  371.         imul    dx
  372.         mov     al,ah
  373.         mov     ah,dl
  374.         mov     cx,ax                    ;CX = unscaled V
  375.  
  376.         movzx   ax,bl                   ; U
  377.         sub     ax,80H                  ;AX = scaled U
  378.         mov     dx,456                  ;(456/256 = 226/127)
  379.         imul    dx
  380.         mov     al,ah
  381.         mov     ah,dl
  382. ;       mov     U,ax                    ;AX = unscaled U
  383.         xor     edx,edx
  384.         mov     dx,ax
  385.         shl     ecx,16
  386.         or      edx,ecx                 ; V bit 31-16 and U bit 15-0 of EDX
  387.         pop     ecx
  388.         ret
  389. UnScaleChroma2 ENDP
  390.  
  391. ;_TEXT   ENDS
  392. CODE32  ENDS
  393. END
  394.