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