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