home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / opus / v5 / amipeg / source / s24bit.s < prev    next >
Text File  |  1977-12-31  |  11KB  |  616 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  14-4-94  1:12:57
  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. all_rows_lores:
  124.     move.l    (savecols,sp),d6
  125.     lsr.l    #1,d6
  126.     subq.l    #1,d6
  127. all_cols_lores:
  128.  
  129. ; free: d4
  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.     or.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.     or.b    (a6),d3            ; g
  171.  
  172.     move.l    d3,(a0)+        ; *row1++
  173.  
  174.  
  175.  
  176.     moveq    #0,d3            ; *lum++
  177.     move.b    (a3)+,d3
  178.     move.l    d1,a6            ; clamp table includes on L
  179.     add.l    d3,d3
  180.     add.l    d3,a6
  181.     move.w    (a6,d0.w),d3        ; r
  182.     move.b    (a6),d3            ; g
  183.  
  184.     swap    d3
  185.  
  186.     move.b    (a3)+,d3        ; *lum++
  187.     move.l    d1,a6            ; clamp table includes on L
  188.     add.w    d3,d3
  189.     add.w    d3,a6
  190.     move.w    (a6,d2.w),d3        ; b
  191.     move.b    (a6),d3            ; g
  192.  
  193.     move.l    d3,(a1)+        ; *row1++
  194.  
  195.  
  196.  
  197.     dbra    d6,all_cols_lores
  198.  
  199.     move.l    savecols(sp),d0        ; next line in the luminance channel
  200.     add.l    d0,a2
  201.     add.l    d0,a3
  202.  
  203.     add.l    d0,d0            ; 2 bytes per pixel
  204.     add.l    d0,a0            ; next line in the output row
  205.     add.l    d0,a1
  206.  
  207.     dbra    d7,all_rows_lores
  208.  
  209.     add.w    #localvars,sp
  210.     movem.l    (sp)+,cdi_regs
  211.     rts
  212.  
  213. ;************************************************************************************
  214.     XDEF @ColorDitherImage_RGB
  215. ;    a0  unsigned char *lum
  216. ;    a1  unsigned char *cr
  217. ; 4(sp)  unsigned char *cb
  218. ; 8(sp)  unsigned char *out
  219. ;    d0  int rows
  220. ;    d1  int cols
  221.  
  222. @ColorDitherImage_RGB:
  223.     movem.l    cdi_regs,-(sp)
  224.  
  225.     sub.w    #localvars,sp
  226.  
  227.     move.l    d1,(savecols,sp)
  228.     lsr.l    #1,d0
  229.     subq.l    #1,d0
  230.     move.l    d0,d7
  231.  
  232.     move.l    a0,a2            ; lum1
  233.     move.l    a2,a3
  234.  
  235.     add.l    d1,a3            ; lum2 = lum1+cols
  236.  
  237.     move.l    a1,a4            ; cr channel
  238.     move.l    cb_offset(sp),d5
  239.     sub.l    a4,d5            ; cb channel is addressed via its offset to the cr channel
  240.  
  241.     lea    Cb_r_tab,a5        ; conversion tab(s)
  242.  
  243.     move.l    (out_offset,sp),a0    ; row1
  244.     move.l    a0,a1
  245.  
  246.     lsl.l    #2,d1
  247.     add.l    d1,a1            ; row2=row1+cols*4
  248.  
  249.     move.l    (savecols,sp),d4
  250. all_rows_rgb:
  251.     move.w    d4,d6
  252.     lsr.w    #1,d6
  253.     subq.w    #1,d6
  254. all_cols_rgb:
  255.  
  256. ; still free: d4
  257.  
  258.     moveq    #0,d1
  259.     moveq    #0,d2
  260.     move.b    (a4,d5.l),d1        ; CB channel
  261.     move.b    (a4)+,d2        ; CR channel
  262.  
  263.     move.l    (a5,d1.w*4),d0            ; cb_r cb_g
  264.     move.l    (Cr_gb_off,a5,d2.w*4),d1    ; cr_g cr_b
  265.  
  266.     move.w    d1,d2
  267.     swap    d1
  268.     add.w    d0,d1
  269.     swap     d0
  270.  
  271.     sub.w    d2,d0
  272.     sub.w    d2,d1
  273.  
  274.     ext.l    d2
  275.     add.l    #_clamp,d2
  276.  
  277.     exg        a4,d2
  278.  
  279. ; d0   cb_r      *2
  280. ; d1  (cr_g+cb_g) *2
  281. ; d2   cr_b      *2
  282.  
  283. ;    moveq    #0,d3            ; *lum++
  284.     clr.w    d3
  285.     move.b    (a2)+,d3
  286.     lea    (a4,d3.w*2),a6
  287.  
  288. ;    move.b    (a6,d0.w),d3
  289. ;    swap    d3
  290.     move.l    -1(a6,d0.w),d3
  291.     move.w    (a6,d1.w),d3
  292.     move.b    (a6),d3
  293.     move.l    d3,(a0)+
  294.  
  295. ;    moveq    #0,d3            ; *lum++
  296.     clr.w    d3
  297.     move.b    (a2)+,d3
  298.     lea    (a4,d3.w*2),a6
  299.  
  300. ;    move.b    (a6,d0.w),d3
  301. ;    swap    d3
  302.     move.l    -1(a6,d0.w),d3
  303.     move.w    (a6,d1.w),d3
  304.     move.b    (a6),d3
  305.     move.l    d3,(a0)+
  306.  
  307. ;    moveq    #0,d3            ; *lum2++
  308.     clr.w    d3
  309.     move.b    (a3)+,d3
  310.     lea    (a4,d3.w*2),a6
  311.  
  312. ;    move.b    (a6,d0.w),d3
  313. ;    swap    d3
  314.     move.l    -1(a6,d0.w),d3
  315.     move.w    (a6,d1.w),d3
  316.     move.b    (a6),d3
  317.     move.l    d3,(a1)+
  318.  
  319. ;    moveq    #0,d3            ; *lum2++
  320.     clr.w    d3
  321.     move.b    (a3)+,d3
  322.     lea    (a4,d3.w*2),a6
  323.  
  324. ;    move.b    (a6,d0.w),d3
  325. ;    swap    d3
  326.     move.l    -1(a6,d0.w),d3
  327.     move.w    (a6,d1.w),d3
  328.     move.b    (a6),d3
  329.     move.l    d3,(a1)+
  330.  
  331.     move.l    d2,a4
  332.  
  333.     dbra    d6,all_cols_rgb
  334.  
  335.     move.l    d4,d0        ; next line in the luminance channel
  336.  
  337.     add.l    d0,a2
  338.     add.l    d0,a3
  339.  
  340.     lsl.l    #2,d0            ; 4 bytes per pixel
  341.     add.l    d0,a0            ; next line in the output row
  342.     add.l    d0,a1
  343.  
  344.     dbra    d7,all_rows_rgb
  345.  
  346.     add.w    #localvars,sp
  347.     movem.l    (sp)+,cdi_regs
  348.     rts
  349.  
  350.  
  351. ;************************************************************************************
  352.  
  353.     XDEF @ColorDitherImage
  354. @ColorDitherImage:
  355.     movem.l    cdi_regs,-(sp)
  356.  
  357.     sub.w    #localvars,sp
  358.  
  359.     move.l    d1,(savecols,sp)
  360.     lsr.l    #1,d0
  361.     subq.l    #1,d0
  362.     move.l    d0,d7
  363.  
  364.     move.l    a0,a2            ; lum1
  365.     move.l    a2,a3
  366.     add.l    d1,a3            ; lum2 = lum1+cols
  367.  
  368.     move.l    a1,a4            ; cr channel
  369.     move.l    cb_offset(sp),d5
  370.     sub.l    a4,d5            ; cb channel is addressed via its offset to the cr channel
  371.  
  372.     lea    Cb_r_tab,a5        ; conversion tab(s)
  373.  
  374.     move.l    (out_offset,sp),a0    ; row1
  375.     move.l    a0,a1
  376.     lsl.l    #2,d1
  377.     add.l    d1,a1            ; row2=row1+cols*4
  378.  
  379.  
  380. all_rows:
  381.     move.l    (savecols,sp),d6
  382.     lsr.l    #1,d6
  383.     subq.l    #1,d6
  384. all_cols:
  385.  
  386. ; still free: d4
  387.  
  388.     moveq    #0,d1
  389.     moveq    #0,d2
  390.     move.b    (a4,d5.l),d1        ; CB channel
  391.     move.b    (a4)+,d2        ; CR channel
  392.  
  393.     move.l    (a5,d1.w*4),d0            ; cb_r cb_g
  394.     move.l    (Cr_gb_off,a5,d2.w*4),d1    ; cr_g cr_b
  395.  
  396.     move.w    d1,d2
  397.     swap    d1
  398.     add.w    d0,d1
  399.     swap     d0
  400.  
  401.     sub.w    d2,d0
  402.     sub.w    d2,d1
  403.  
  404.     ext.l    d2
  405.     add.l    #_clamp,d2
  406.  
  407.     exg        a4,d2
  408.  
  409. ; d0   cb_r      *2
  410. ; d1  (cr_g+cb_g) *2
  411. ; d2   cr_b      *2
  412.  
  413.     moveq    #0,d3            ; *lum++
  414.     move.b    (a2)+,d3
  415.     lea    (a4,d3.w*2),a6
  416.  
  417.     move.w    (a6),d3            ; b    BRG0
  418.     move.b    (a6,d0.w),d3        ; r
  419.     swap    d3
  420.     move.w    (a6,d1.w),d3        ; g
  421.     move.l    d3,(a0)+        ; *row1++
  422.  
  423.     moveq    #0,d3            ; *lum++
  424.     move.b    (a2)+,d3
  425.     lea    (a4,d3.w*2),a6
  426.  
  427.     move.w    (a6),d3            ; b    BRG0
  428.     move.b    (a6,d0.w),d3        ; r
  429.     swap    d3
  430.     move.w    (a6,d1.w),d3        ; g
  431.     move.l    d3,(a0)+        ; *row1++
  432.  
  433.     moveq    #0,d3            ; *lum2++
  434.     move.b    (a3)+,d3
  435.     lea        (a4,d3.w*2),a6
  436.  
  437.     move.w    (a6),d3            ; b    BRG0
  438.     move.b    (a6,d0.w),d3        ; r
  439.     swap    d3
  440.     move.w    (a6,d1.w),d3        ; g
  441.     move.l    d3,(a1)+        ; *row2++
  442.  
  443.     moveq    #0,d3            ; *lum2++
  444.     move.b    (a3)+,d3
  445.     lea        (a4,d3.w*2),a6
  446.  
  447.     move.w    (a6),d3            ; b    BRG0
  448.     move.b    (a6,d0.w),d3        ; r
  449.     swap    d3
  450.     move.w    (a6,d1.w),d3        ; g
  451.     move.l    d3,(a1)+        ; *row2++
  452.  
  453.     move.l    d2,a4
  454.  
  455.     dbra    d6,all_cols
  456.  
  457.     move.l    savecols(sp),d0        ; next line in the luminance channel
  458.     add.l    d0,a2
  459.     add.l    d0,a3
  460.  
  461.     lsl.l    #2,d0            ; 4 bytes per pixel
  462.     add.l    d0,a0            ; next line in the output row
  463.     add.l    d0,a1
  464.  
  465.     dbra    d7,all_rows
  466.  
  467.     add.w    #localvars,sp
  468.     movem.l    (sp)+,cdi_regs
  469.     rts
  470.  
  471.  
  472. ;************************************************************************************
  473.  
  474.     XDEF @ColorDitherImage_12bit
  475. @ColorDitherImage_12bit:
  476.     movem.l    cdi_regs,-(sp)
  477.  
  478.     sub.w    #localvars,sp
  479.  
  480.     move.l    d1,(savecols,sp)
  481.     lsr.l    #1,d0
  482.     subq.l    #1,d0
  483.     move.l    d0,d7
  484.  
  485.     move.l    a0,a2            ; lum1
  486.     move.l    a2,a3
  487.     add.l    d1,a3            ; lum2 = lum1+cols
  488.  
  489.     move.l    a1,a4            ; cr channel
  490.     move.l    cb_offset(sp),d5
  491.     sub.l    a4,d5            ; cb channel is addressed via its offset to the cr channel
  492.  
  493.     lea    Cb_r_tab,a5        ; conversion tab(s)
  494.  
  495.     move.l    (out_offset,sp),a0    ; row1
  496.     move.l    a0,a1
  497.     add.l    d1,a1            ; row2=row1+cols
  498.  
  499. all_rows_12bit:
  500.     move.l    (savecols,sp),d6
  501.     lsr.l    #1,d6
  502.     subq.l    #1,d6
  503. all_cols_12bit:
  504.  
  505.     moveq    #0,d1
  506.     moveq    #0,d2
  507.     move.b    (a4,d5.l),d1        ; CB channel
  508.     move.b    (a4)+,d2        ; CR channel
  509.  
  510.     move.l    (a5,d1.w*4),d0            ; cb_r cb_g
  511.     move.l    (Cr_gb_off,a5,d2.w*4),d1    ; cr_g cr_b
  512.  
  513.     move.w    d1,d2
  514.     swap    d1
  515.     add.w    d0,d1
  516.     swap     d0
  517.  
  518.     sub.w    d1,d0
  519.     sub.w    d1,d2
  520.  
  521.     ext.l    d1
  522.     add.l    #_clamp,d1
  523.  
  524. ; d0   cb_r      *2
  525. ; d1  (cr_g+cb_g) *2
  526. ; d2   cr_b      *2
  527.  
  528.     moveq    #0,d3            ; *lum++
  529.     moveq    #0,d4
  530.     move.b    (a2)+,d3
  531.     move.l    d1,a6            ; clamp table includes on L
  532.     add.l    d3,d3
  533.     add.l    d3,a6
  534.     move.w    (a6,d0.w),d3        ; r
  535.     lsl.l    #4,d3
  536.     move.w    (a6),d3            ; g
  537.     lsl.l    #4,d3
  538.  
  539.     move.b    (a2)+,d4        ; *lum++
  540.     move.l    d1,a6            ; clamp table includes on L
  541.     add.w    d4,d4
  542.     add.l    d4,a6
  543.     move.w    (a6,d2.w),d3        ; b
  544.     lsr.l    #4,d3
  545.     move.b    (a6),d3            ; g
  546.     lsr.l    #4,d3
  547.     move.w    d3,(a0)+        ; *row1++
  548.  
  549.  
  550.     moveq    #0,d3            ; *lum++
  551.     moveq    #0,d4
  552.     move.b    (a3)+,d3
  553.     move.l    d1,a6            ; clamp table includes on L
  554.     add.l    d3,d3
  555.     add.l    d3,a6
  556.     move.w    (a6,d0.w),d3        ; r
  557.     lsl.l    #4,d3
  558.     move.w    (a6),d3            ; g
  559.     lsl.l    #4,d3
  560.  
  561.     move.b    (a3)+,d4        ; *lum++
  562.     move.l    d1,a6            ; clamp table includes on L
  563.     add.w    d4,d4
  564.     add.l    d4,a6
  565.     move.w    (a6,d2.w),d3        ; b
  566.     lsr.l    #4,d3
  567.     move.b    (a6),d3            ; g
  568.     lsr.l    #4,d3
  569.     move.w    d3,(a1)+        ; *row1++
  570.  
  571.  
  572.     dbra    d6,all_cols_12bit
  573.  
  574.     move.l    savecols(sp),d0        ; next line in the luminance channel
  575.     add.l    d0,a2
  576.     add.l    d0,a3
  577.  
  578.     add.l    d0,a0            ; next line in the output row
  579.     add.l    d0,a1
  580.  
  581.     dbra    d7,all_rows_12bit
  582.  
  583.     add.w    #localvars,sp
  584.     movem.l    (sp)+,cdi_regs
  585.     rts
  586.  
  587. ;************************************************************************************
  588.  
  589.     SECTION    __MERGED,BSS
  590.  
  591. ;
  592. ; uv conversion table; contains 4 sets describing the relation between the two
  593. ; chrominance channels and the four-times bigger luminance channel.
  594. ;
  595. Cb_r_tab: ds.l    2*256
  596.  
  597.  
  598. ; clamp table
  599. ;
  600. ;    clamp[x]    = 0xff00
  601. ; *(&clamp[x]-1) = 0x00ff
  602. ;
  603. ; an offset into this clamp table could very easily implement overall brightness control!
  604. ;
  605. ; we can reuse this special construct in video.c/sutils.s
  606. ;
  607.  
  608.     XDEF _clamp
  609.  
  610.  
  611.     ds.w    clampsize
  612. _clamp:    ds.w    256+clampsize
  613.  
  614.  
  615.     END
  616.