home *** CD-ROM | disk | FTP | other *** search
/ Ultimedia 2 / Ultimedia 2.iso / tools / animplayer / amipeg / source.lha / s24bit.s < prev    next >
Encoding:
Text File  |  1994-03-03  |  6.7 KB  |  367 lines

  1. ;
  2. ; This source handles conversion from yuv to rgb space, which includes
  3. ; initialization of the conversion & clamp tables.
  4. ;
  5. ; Original routines are in 24bit.c, which mainly contained those lovely 
  6. ; just-perfectly-fit-for-assembler-kick-ass constructions.
  7. ;
  8. ; Michael Rausch  13-2-94  8:32:00
  9. ;
  10.  
  11.     SECTION    text,CODE
  12.  
  13. ;************************************************************************************
  14.  
  15. ; We'll define the "ConvertColor" macro here to do fixed point arithmetic
  16. ; that'll convert from YCrCb to RGB using:
  17. ;    R = L + 1.40200*Cr
  18. ;    G = L - 0.34414*Cb - 0.71414*Cr
  19. ;    B = L + 1.77200*Cb
  20. ;
  21.  
  22. ; void InitColorDither(void)
  23.     XDEF    @InitColorDither
  24. @InitColorDither:            
  25.  
  26.     lea    Cb_r_tab,a0
  27. Cr_gb_off EQU    256*4
  28.     lea    Cb_r_tab+Cr_gb_off,a1
  29.  
  30.     move.w    #255,d0
  31.     moveq    #-128,d1
  32. crgbtabs:
  33.     move.w    d1,d2
  34.     muls.w    #179,d2        ; 1.40200
  35.     asr.w    #7,d2
  36.     add.w    d2,d2
  37.     move.w    d2,(a0)+    ;br
  38.     move.w    d1,d2
  39.     muls.w    #-91,d2        ; -0.71414
  40.     asr.w    #7,d2
  41.     add.w    d2,d2
  42.     move.w    d2,(a1)+    ;bg
  43.     move.w    d1,d2
  44.     muls.w    #-44,d2        ; -0.34414
  45.     asr.w    #7,d2
  46.     add.w    d2,d2
  47.     move.w    d2,(a0)+    ;rg
  48.     move.w    d1,d2
  49.     muls.w    #226,d2        ; 1.77200
  50.     asr.w    #7,d2
  51.     add.w    d2,d2
  52.     move.w    d2,(a1)+    ;rb
  53.     addq.l    #1,d1
  54.     dbra    d0,crgbtabs
  55.  
  56. ;
  57. ; create the clamp tables, including the shifted one
  58. ;
  59. ; IMPORTANT: The first versions had a MUCH too low clampsize, resulting in really
  60. ; annoying purple artifacts !!!
  61. ;
  62. clampsize EQU    192
  63.  
  64.     moveq    #0,d1
  65.     lea    _clamp-clampsize*2,a0            ; clear lower and upper 64 bytes
  66.     move.w    #clampsize-1,d0
  67. fill_ct1:move.w    #$ff00,((clampsize+256)*2,a0)
  68.     move.w    d1,(a0)+
  69.     dbra    d0,fill_ct1
  70.     move.w    #255,d0
  71. fill_ct2:move.w    d1,(a0)+
  72.     add.w    #$100,d1
  73.     dbra    d0,fill_ct2
  74.  
  75.     rts
  76.  
  77. ;************************************************************************************
  78.  
  79. ;void ColorDitherImage(unsigned char *lum, unsigned char *cr, unsigned char *cb,
  80. ;              unsigned char *out, int rows, int cols)
  81.     XDEF @ColorDitherImage_lores
  82. @ColorDitherImage_lores:
  83.  
  84. ;    a0  unsigned char *lum
  85. ;    a1  unsigned char *cr
  86. ; 4(sp)  unsigned char *cb
  87. ; 8(sp)  unsigned char *out
  88. ;    d0  int rows
  89. ;    d1  int cols
  90.  
  91. cdi_regs REG    d2-d7/a2-a6    ; 6+5=11
  92.     movem.l    cdi_regs,-(sp)
  93.  
  94. ;saved7    EQU    0
  95. saverows EQU    4
  96. savecols EQU    8
  97. localvars EQU    savecols+4
  98. cb_offset EQU    localvars+11*4+4
  99. out_offset EQU    cb_offset+4
  100.  
  101.     sub.w    #localvars,sp
  102.  
  103.     move.l    d1,(savecols,sp)
  104.     lsr.l    #1,d0
  105.     subq.l    #1,d0
  106.     move.l    d0,d7
  107.  
  108.     move.l    a0,a2            ; lum1
  109.     move.l    a2,a3
  110.     add.l    d1,a3            ; lum2 = lum1+cols
  111.  
  112.     move.l    a1,a4            ; cr channel
  113.     move.l    cb_offset(sp),d5
  114.     sub.l    a4,d5            ; cb channel is addressed via its offset to the cr channel
  115.  
  116.     lea    Cb_r_tab,a5        ; conversion tab(s)
  117.  
  118.     move.l    (out_offset,sp),a0    ; row1
  119.     move.l    a0,a1
  120.     add.l    d1,d1            
  121.     add.l    d1,a1            ; row2=row1+cols*2
  122.  
  123.     moveq    #0,d4
  124. all_rows_lores:
  125.     move.l    (savecols,sp),d6
  126.     lsr.l    #1,d6
  127.     subq.l    #1,d6
  128. all_cols_lores:
  129.  
  130.  
  131.     moveq    #0,d1
  132.     moveq    #0,d2
  133.     move.b    (a4,d5.l),d1        ; CB channel
  134.     move.b    (a4)+,d2        ; CR channel
  135.  
  136.     move.l    (a5,d1.w*4),d0            ; cb_r cb_g
  137.     move.l    (Cr_gb_off,a5,d2.w*4),d1    ; cr_g cr_b
  138.  
  139.     move.w    d1,d2
  140.     swap    d1
  141.     add.w    d0,d1
  142.     swap     d0
  143.  
  144.  
  145.     sub.w    d1,d0
  146.     sub.w    d1,d2
  147.  
  148.     ext.l    d1
  149.     add.l    #_clamp,d1
  150.  
  151. ; d0   cb_r      *2
  152. ; d1  (cr_g+cb_g) *2
  153. ; d2   cr_b      *2
  154.  
  155.     moveq    #0,d3            ; *lum++
  156.     move.b    (a2)+,d3
  157.     move.l    d1,a6            ; clamp table includes on L
  158.     add.l    d3,d3
  159.     add.l    d3,a6
  160.     move.w    (a6,d0.w),d3        ; r
  161.     move.b    (a6),d3            ; g
  162.  
  163.     swap    d3
  164.  
  165.     move.b    (a2)+,d3        ; *lum++
  166.     move.l    d1,a6            ; clamp table includes on L
  167.     add.w    d3,d3
  168.     add.w    d3,a6
  169.     move.w    (a6,d2.w),d3        ; b
  170.     move.b    (a6),d3            ; g
  171.  
  172.     move.l    d3,(a0)+        ; *row1++
  173.  
  174.  
  175.  
  176.  
  177.     moveq    #0,d3            ; *lum++
  178.     move.b    (a3)+,d3
  179.     move.l    d1,a6            ; clamp table includes on L
  180.     add.l    d3,d3
  181.     add.l    d3,a6
  182.     move.w    (a6,d0.w),d3        ; r
  183.     move.b    (a6),d3            ; g
  184.  
  185.     swap    d3
  186.  
  187.     move.b    (a3)+,d3        ; *lum++
  188.     move.l    d1,a6            ; clamp table includes on L
  189.     add.w    d3,d3
  190.     add.w    d3,a6
  191.     move.w    (a6,d2.w),d3        ; b
  192.     move.b    (a6),d3            ; g
  193.  
  194.     move.l    d3,(a1)+        ; *row1++
  195.  
  196.  
  197.  
  198.     dbra    d6,all_cols_lores
  199.  
  200.     move.l    savecols(sp),d0        ; next line in the luminance channel
  201.     add.l    d0,a2
  202.     add.l    d0,a3
  203.  
  204.     add.l    d0,d0            ; 2 bytes per pixel
  205.     add.l    d0,a0            ; next line in the output row
  206.     add.l    d0,a1
  207.  
  208.     dbra    d7,all_rows_lores
  209.  
  210.     add.w    #localvars,sp
  211.     movem.l    (sp)+,cdi_regs
  212.     rts
  213.  
  214. ;************************************************************************************
  215.  
  216.     XDEF @ColorDitherImage
  217. @ColorDitherImage:
  218.     movem.l    cdi_regs,-(sp)
  219.  
  220.     sub.w    #localvars,sp
  221.  
  222.     move.l    d1,(savecols,sp)
  223.     lsr.l    #1,d0
  224.     subq.l    #1,d0
  225.     move.l    d0,d7
  226.  
  227.     move.l    a0,a2            ; lum1
  228.     move.l    a2,a3
  229.     add.l    d1,a3            ; lum2 = lum1+cols
  230.  
  231.     move.l    a1,a4            ; cr channel
  232.     move.l    cb_offset(sp),d5
  233.     sub.l    a4,d5            ; cb channel is addressed via its offset to the cr channel
  234.  
  235.     lea    Cb_r_tab,a5        ; conversion tab(s)
  236.  
  237.     move.l    (out_offset,sp),a0    ; row1
  238.     move.l    a0,a1
  239.     lsl.l    #2,d1
  240.     add.l    d1,a1            ; row2=row1+cols*4
  241.  
  242.  
  243. all_rows:
  244.     move.l    (savecols,sp),d6
  245.     lsr.l    #1,d6
  246.     subq.l    #1,d6
  247. all_cols:
  248.  
  249. ; still free: d4
  250.  
  251.     moveq    #0,d1
  252.     moveq    #0,d2
  253.     move.b    (a4,d5.l),d1        ; CB channel
  254.     move.b    (a4)+,d2        ; CR channel
  255.  
  256.     move.l    (a5,d1.w*4),d0            ; cb_r cb_g
  257.     move.l    (Cr_gb_off,a5,d2.w*4),d1    ; cr_g cr_b
  258.  
  259.     move.w    d1,d2
  260.     swap    d1
  261.     add.w    d0,d1
  262.     swap     d0
  263.  
  264.     sub.w    d2,d0
  265.     subq.w    #1,d0            ; misaligned access on this word
  266.     sub.w    d2,d1
  267.  
  268.     ext.l    d2
  269.     add.l    #_clamp,d2
  270.  
  271. ; d0   cb_r      *2
  272. ; d1  (cr_g+cb_g) *2
  273. ; d2   cr_b      *2
  274.  
  275.     moveq    #0,d3            ; *lum++
  276.     move.b    (a2)+,d3
  277.     move.l    d2,a6            ; clamp table includes on L
  278.     add.l    d3,a6
  279.     add.l    d3,a6
  280.     move.w    (a6),d3            ; b    BRG0
  281.     or.w    (a6,d0.w),d3        ; r
  282.     swap    d3
  283.     move.w    (a6,d1.w),d3        ; g
  284.     move.l    d3,(a0)+        ; *row1++
  285.  
  286.     moveq    #0,d3            ; *lum++
  287.     move.b    (a2)+,d3
  288.     move.l    d2,a6            ; clamp table includes on L
  289.     add.l    d3,a6
  290.     add.l    d3,a6
  291.     move.w    (a6),d3            ; b    BRG0
  292.     or.w    (a6,d0.w),d3        ; r
  293.     swap    d3
  294.     move.w    (a6,d1.w),d3        ; g
  295.     move.l    d3,(a0)+        ; *row1++
  296.  
  297.  
  298.     moveq    #0,d3            ; *lum2++
  299.     move.b    (a3)+,d3
  300.     move.l    d2,a6            ; clamp table includes on L
  301.     add.l    d3,a6
  302.     add.l    d3,a6
  303.     move.w    (a6),d3            ; b    BRG0
  304.     or.w    (a6,d0.w),d3        ; r
  305.     swap    d3
  306.     move.w    (a6,d1.w),d3        ; g
  307.     move.l    d3,(a1)+        ; *row2++
  308.  
  309.     moveq    #0,d3            ; *lum2++
  310.     move.b    (a3)+,d3
  311.     move.l    d2,a6            ; clamp table includes on L
  312.     add.l    d3,a6
  313.     add.l    d3,a6
  314.     move.w    (a6),d3            ; b    BRG0
  315.     or.w    (a6,d0.w),d3        ; r
  316.     swap    d3
  317.     move.w    (a6,d1.w),d3        ; g
  318.     move.l    d3,(a1)+        ; *row2++
  319.  
  320.  
  321.     dbra    d6,all_cols
  322.  
  323.     move.l    savecols(sp),d0        ; next line in the luminance channel
  324.     add.l    d0,a2
  325.     add.l    d0,a3
  326.  
  327.     lsl.l    #2,d0            ; 4 bytes per pixel
  328.     add.l    d0,a0            ; next line in the output row
  329.     add.l    d0,a1
  330.  
  331.     dbra    d7,all_rows
  332.  
  333.     add.w    #localvars,sp
  334.     movem.l    (sp)+,cdi_regs
  335.     rts
  336.  
  337.  
  338. ;************************************************************************************
  339.  
  340.     SECTION    udata,BSS
  341.  
  342. ;
  343. ; uv conversion table; contains 4 sets describing the relation between the two
  344. ; chrominance channels and the four-times bigger luminance channel.
  345. ;
  346. Cb_r_tab: ds.l    2*256
  347.  
  348.  
  349. ; clamp table
  350. ;
  351. ;    clamp[x]    = 0xff00
  352. ; *(&clamp[x]-1) = 0x00ff
  353. ;
  354. ; an offset into this clamp table could very easily implement overall brightness control!
  355. ;
  356. ; we can reuse this special construct in video.c/sutils.s
  357. ;
  358.  
  359.     XDEF _clamp
  360.  
  361.  
  362.     ds.w    clampsize
  363. _clamp:    ds.w    256+clampsize
  364.  
  365.  
  366.     END
  367.