home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c185 / 2.ddi / OWLSRC.EXE / CSCAPE / SOURCE / PCNEARES.ASM < prev    next >
Encoding:
Assembly Source File  |  1989-07-21  |  2.8 KB  |  126 lines

  1. ; pcneares.asm
  2. ;
  3. ; 5/5/88 by Ted
  4. ;
  5. ; Low Level RAM functions for OWL
  6. ; Find nearest color to given RGB in color map.
  7. ; THIS FILE IS FOR LARGE-DATA MODELS ONLY
  8. ;
  9. ; Copyright (c) 1988, 1989 Oakland Group Inc.
  10. ; ALL RIGHTS RESERVED
  11. ;
  12. ;------------------------REVISION HISTORY--------------------------------------;
  13. ;------------------------------------------------------------------------------;
  14. include    PCDECL.MAC
  15.  
  16. SRGB struc
  17.     red            db    ?
  18.     green        db    ?
  19.     blue        db    ?
  20. SRGB ends
  21.  
  22. LRGB equ 3
  23.  
  24. SCOLORMAP struc
  25.     firstpix    dw    ?
  26. ;;    firstpixhi    dw    ?        ;; opixvals are reduced to 2 bytes now
  27.     nentries    dw    ?
  28.     rgblev        db    LRGB*1        dup (?)
  29. SCOLORMAP ends
  30.  
  31.     PSEG
  32. ;------------------------------------------------------------------------------;
  33. ; opixval ocolmap_nearestcol (rgb_struct *rgb, ocolmap_type tcmap)
  34. ; Find nearest color to rgb in tcmap. Return tpix.
  35.  
  36. pubproc DIGPRIV ocolmap_nearestcol <rgbaddr, dptr, tcmap, dptr>
  37.  
  38.     push bp
  39.     mov bp, sp
  40.     pushm <ds, es, si, di>
  41.  
  42.     lds si, [bp].rgbaddr
  43.     mov bl, byte ptr[si].red
  44.     mov bh, byte ptr[si].green
  45.     mov cl, byte ptr[si].blue
  46.  
  47.     lds di, [bp].tcmap
  48.  
  49.     mov dx, [di].nentries
  50.     dec dx
  51.     mov dh, dl                ; dh = lastentry;
  52.     mov ch, 0                ; ipix starts at 0
  53.  
  54. ;  bl, bh, cl now have source rgb levels
  55. ;  dh has lastentry, ch has ipix = firstpix
  56.  
  57. ; Search for closest value
  58. ; Note: distance = (30*dr)^2 + (59*dg)^2 + (11*db)^2
  59. ;; ax, si for work
  60. ;; registers: bl,bh,cl=targetcol; ch=ipix; dh = lastentry, ds:di=tcmap[ipix];
  61. ;; es=minpix
  62.  
  63.     mov bp, -1    ; min diff so far (init to biggest unsigned number)
  64. tloop:
  65.     mov al, byte ptr ds:[di].rgblev.red
  66.     sub al, bl
  67.     imul al
  68.     mov si, ax
  69.  
  70.     mov al, byte ptr ds:[di].rgblev.green
  71.     sub al, bh
  72.     imul al
  73.     add si, ax
  74.  
  75.     mov al, byte ptr ds:[di].rgblev.blue
  76.     sub al, cl
  77.     imul al
  78.     add ax, si
  79.  
  80.     cmp ax, 0
  81.     jz bingo    ; if ax is 0, we hit it exactly, so quit
  82. keepon:
  83.     add di, LRGB
  84.  
  85.     cmp ax, bp
  86.     jae notmin
  87. newmin:
  88.     mov bp, ax        ; save latest min val
  89.     mov al, ch
  90.     xor ah, ah        ; save latest min pix in es
  91.     mov es, ax
  92. notmin:
  93.  
  94.     cmp ch, dh    ; when ch is equal to lastentry we're done
  95.     je  done
  96.     inc ch
  97.     jmp short tloop
  98.  
  99. bingo:
  100.     mov bp, ax        ; save latest min val
  101.     mov al, ch
  102.     xor ah, ah        ; save latest min pix in es
  103.     mov es, ax
  104.  
  105. ; loop done now; we got min diff in bp, min entry in es
  106. done:
  107.     mov ax, es
  108. ;;    mov dx, 0        ; opixval is no longer a long
  109.  
  110.  
  111.     mov bp, sp        ; restore frame pointer for access to args again
  112.     add bp, 8        ; account for ds, es, si, di on stack
  113.  
  114.     lds di, [bp].tcmap
  115.     add ax, ds:[di].firstpix    ; add firstpix to the entry we found
  116.  
  117.     popm <di, si, es, ds>
  118.     pop    bp
  119.     ret
  120. endproc ocolmap_nearestcol
  121. ;------------------------------------------------------------------------------;
  122.     ENDPS
  123.     end
  124. ;------------------------------------------------------------------------------;
  125.  
  126.