home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Programmierung / SOURCE.mdf / programm / msdos / asm / 3dvect25 / tga2icon / tga2icon.asm < prev    next >
Encoding:
Assembly Source File  |  1993-08-10  |  13.2 KB  |  665 lines

  1.  
  2. ; Link this with FILE32, and ARGC32, and enable OPENFILE, READFILE,
  3. ; FILESIZE, CCHEKSTR, and CCHEKSWITCH in their respective libs.
  4.  
  5.         .386p
  6.         jumps
  7.  
  8. code32  segment para public use32
  9.         assume cs:code32, ds:code32, ss:code32
  10.  
  11. include pmode.inc
  12. include file.inc
  13. include argc.inc
  14.  
  15. public  _main
  16.  
  17. ;▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  18. ; DATA
  19. ;▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  20. errmsg0         db      'SYNTAX: TGA2ICON [-a#] filename',0dh,0ah,0dh,0ah
  21.                 db      '   -a add number to all numbers in sorted TGA output',0dh,0ah
  22.  
  23. errmsg1         db      'Error opening input file!$'
  24. errmsg2         db      'Not enough extended memory to load file!!!$'
  25.  
  26. inputname       db 60 dup (?)
  27. filelen         dd ?
  28. outputname      db 60 dup (?)
  29. labellen        dd ?
  30.  
  31. outptr          dd ?
  32. addvalue        db 5 dup (0)
  33.  
  34. pallet          dd 999 dup (?)
  35. psize           dw 0
  36.  
  37. xsize           dw 0
  38. xcount          dw 0
  39. ysize           dw 0
  40.  
  41. temp1           dw 0
  42. temp2           dw 0
  43.  
  44. inverted        db 0
  45.  
  46. chars      db "0123456789abcdef"
  47. byte2      db "0",0
  48. vc         dd 0
  49. xq         dw 0
  50.  
  51. ;▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  52. ; CODE
  53. ;▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  54.  
  55. include strltu.rt
  56. include strhtn.rt
  57.  
  58. ;-----------------------------------------------------------------------------
  59. exiterr2:
  60.         mov edx,offset errmsg2
  61.         jmp short exiterr
  62. ;-----------------------------------------------------------------------------
  63. exiterr1:
  64.         mov edx,offset errmsg1
  65.         jmp short exiterr
  66. ;-----------------------------------------------------------------------------
  67. exiterr0:
  68.         mov edx,offset errmsg0
  69. exiterr:
  70.         call _putdosmsg
  71.         jmp _exit
  72.  
  73. ;═════════════════════════════════════════════════════════════════════════════
  74.  
  75. _main:
  76.         xor al,al                       ; chek for filename on commandline
  77.         mov edx,offset inputname
  78.         call _cchekstr
  79.         jc exiterr0
  80.  
  81.         call _openfile                  ; attempt to open file
  82.         jc exiterr1
  83.  
  84.         mov eax,4000h                   ; setup file buffer
  85.         call _getlomem
  86.         mov _filebufloc,eax
  87.  
  88.         call _filesize
  89.         mov filelen,eax
  90.  
  91.         call _gethimem                  ; chek for, and get enough himem
  92.         jc exiterr2
  93.         mov outptr,eax
  94.  
  95.         mov edx,outptr                  ; load file into himem
  96.         mov ecx,filelen                 ; restore actual filesize for load
  97.         call _readfile
  98.         call _closefile                 ; file is in memory! kowabunga!
  99.  
  100. ;═════════════════════════════════════════════════════════════════════════════
  101.  
  102. ; test if add switch used
  103.  
  104.         mov al,'a'                      ; chek for add switch
  105.         call _cchekswitch
  106.         jc short mf0
  107.  
  108.         mov edx,offset addvalue         ; misc data ptr
  109.         call _ccheksstr
  110.         call _strltu                    ; convert string to number
  111.         call _strhtn
  112.         call vct
  113.         mov addvalue,al
  114.         jmp mf1
  115. mf0:
  116.         mov addvalue,1
  117. mf1:
  118.  
  119. ; set up output file name
  120.  
  121.         mov ecx,8
  122.         mov edi,0
  123.         mov edx,0
  124. mn:
  125.         mov al,inputname[di]
  126.         cmp al,"."
  127.         je nt
  128.         inc edx
  129.         mov outputname[di],al
  130.         inc di
  131.         loop mn
  132. nt:
  133.         mov outputname[di+0],"."
  134.         mov outputname[di+1],"i"
  135.         mov outputname[di+2],"n"
  136.         mov outputname[di+3],"c"
  137.         mov outputname[di+4],0
  138.  
  139.         mov labellen,edx
  140.  
  141.         mov edx,offset outputname
  142.         call _createfile
  143.  
  144. ; find info on this tga file
  145.  
  146.         mov esi,outptr
  147.         add esi,12
  148.         lodsw
  149.         mov xsize,ax                      ; x size
  150.         lodsw
  151.         mov ysize,ax                      ; y size
  152.         lodsb
  153.         lodsb
  154.         mov inverted,al                   ; is tga inverted, 20h if so
  155.         mov outptr,esi                    ; esi = begining of file
  156.  
  157.         movzx eax,xsize
  158.         movzx ecx,ysize
  159.         mul ecx
  160.         mov ebx,3             ; 3 bytes per pel
  161.         mul ebx
  162.         mov filelen,eax
  163.  
  164. ; find number of colours in tga file
  165.  
  166.         mov ecx,filelen
  167.         mov esi,outptr
  168.         mov psize,0
  169.  
  170.         cld
  171.  
  172. n_loop:
  173.         mov eax,0            ; output matrix
  174.         lodsb
  175.         mov dl,al
  176.         lodsb
  177.         mov bl,al
  178.         lodsb
  179.         shl eax,8
  180.         mov al,bl
  181.         shl eax,8
  182.         mov al,dl
  183.         shl eax,8
  184.  
  185.         cmp eax,0
  186.         je old1
  187.  
  188.         mov di,0
  189. k_loop:
  190.         mov bp, di
  191.         shr bp, 2
  192.         cmp bp, psize
  193.         jge foundnew
  194.         cmp eax,pallet[di]
  195.         je old1
  196.         add di,4
  197.         jmp k_loop
  198.  
  199. foundnew:
  200.         mov pallet[edi],eax
  201.         inc psize
  202.         cmp psize,999
  203.         je outfast
  204. old1:
  205.         sub ecx,3
  206.         cmp ecx,0
  207.         ja n_loop
  208. outfast:
  209.  
  210. ; write number of colours detected:x
  211. ; then write pallet lable
  212.  
  213.         call write_number
  214.  
  215.         mov ax,psize
  216.         call write_ax_asdec
  217.         call write_ret
  218.         call write_ret
  219.  
  220.         mov ecx,labellen                  ; write output name as label
  221.         mov edx,offset outputname
  222.         call _writefile
  223.         call writepallet
  224.  
  225. ; sort colours in pallet
  226.  
  227.            movzx ecx,psize
  228.  
  229.            cmp ecx,2
  230.            jle quickex
  231.  
  232. nextccx:
  233.            mov bx,0               ; sort flag
  234.            dec cx
  235.            jcxz quickex
  236.            mov si,psize
  237.            dec si
  238.            shl si,2
  239. nextddx:
  240.            sub si,4
  241.  
  242.            mov eax,pallet[si+4]
  243.            cmp eax,pallet[si]
  244.            jae donotng
  245.            xchg eax,pallet[si]
  246.            xchg eax,pallet[si+4]
  247.            inc bx                 ; flag that one sorted
  248. donotng:
  249.            cmp si,0
  250.            jne nextddx
  251.  
  252.            cmp bx,0               ; re-sort until no more sorts
  253.            jne nextccx
  254. quickex:
  255.  
  256. ; write pallet to file
  257.  
  258.           mov temp1,0
  259.           mov al,addvalue
  260.           mov ah,0
  261.           mov temp2,ax
  262.  
  263. wri_loop:
  264.           call write_db
  265.  
  266.           mov al,"0"
  267.           call writeit
  268.  
  269.           mov di,temp1
  270.           mov al,byte ptr pallet[di+3]
  271.           shr al,2
  272.           call write_al_ashex
  273.           mov al,"h"
  274.           call writeit
  275.           call write_comma
  276.           mov al,"0"
  277.           call writeit
  278.  
  279.           mov di,temp1
  280.           mov al,byte ptr pallet[di+2]
  281.           shr al,2
  282.           call write_al_ashex
  283.           mov al,"h"
  284.           call writeit
  285.           call write_comma
  286.           mov al,"0"
  287.           call writeit
  288.  
  289.           mov di,temp1
  290.           mov al,byte ptr pallet[di+1]
  291.           shr al,2
  292.           call write_al_ashex
  293.           mov al,"h"
  294.           call writeit
  295.  
  296.           add temp1,4
  297.  
  298.           mov al," "
  299.           call writeit
  300.           mov al,";"
  301.           call writeit
  302.           mov al," "
  303.           call writeit
  304.           mov ax,temp2
  305.           inc temp2
  306.           call write_ax_asdec
  307.           call write_ret
  308.  
  309.           mov ax,psize
  310.           mov bx,4
  311.           mul bx
  312.           cmp temp1,ax
  313.           jl wri_loop
  314.  
  315.         call write_ret
  316.  
  317.         mov ecx,labellen                 ; write output name as label
  318.         mov edx,offset outputname
  319.         call _writefile
  320.  
  321.         call write_dw
  322.  
  323.         mov ax,xsize
  324.         call write_ax_asdec
  325.         call write_comma
  326.         mov ax,ysize
  327.         call write_ax_asdec
  328.         call write_comment
  329.  
  330.         call write_db
  331.  
  332.         mov esi,outptr
  333.  
  334.         mov ax,xsize
  335.         mov xcount,ax
  336.  
  337.         cmp inverted,0
  338.         je  doup
  339.  
  340.  
  341. na_loop2:
  342.         mov eax,0            ; output matrix
  343.         lodsb
  344.         mov dl,al
  345.         lodsb
  346.         mov bl,al
  347.         lodsb
  348.         shl eax,8
  349.         mov al,bl
  350.         shl eax,8
  351.         mov al,dl
  352.         shl eax,8
  353.  
  354.         cmp eax,0
  355.         je old3
  356.  
  357.         mov di,0
  358.  
  359. k_loop2:
  360.         mov bp, di
  361.         shr bp, 2
  362.         cmp bp, psize
  363.         jge foundnew2
  364.         cmp eax,pallet[di]
  365.         je old2
  366.         add di,4
  367.         jmp k_loop2
  368.  
  369. old2:
  370.         mov ax,di
  371.         shr ax,2
  372.         add al,addvalue
  373. old3:
  374.         push esi
  375.         call write_ax_asdec
  376.         cmp xcount,1
  377.         je skc
  378.         call write_comma
  379. skc:
  380.         pop esi
  381.  
  382. foundnew2:
  383.         dec xcount
  384.         jne na_loop2
  385.  
  386.         push esi
  387.         call write_ret
  388.         cmp ysize,1
  389.         je  ndb
  390.         call write_db
  391. ndb:
  392.         pop esi
  393.         mov ax,xsize
  394.         mov xcount,ax
  395.  
  396.         dec ysize
  397.         jne na_loop2
  398.  
  399.         call _closefile
  400.  
  401.         jmp _exit
  402.  
  403. ; do upside down tga file (byte 18 <> $20)
  404.  
  405. doup:
  406.         movzx eax,xsize
  407.         movzx ebx,ysize
  408.         mov ecx,3
  409.         dec ebx
  410.         mul ebx
  411.         mul ecx
  412.         mov esi,eax
  413.         add esi,outptr
  414.  
  415. na_loop3:
  416.         mov eax,0            ; output matrix
  417.         lodsb
  418.         mov dl,al
  419.         lodsb
  420.         mov bl,al
  421.         lodsb
  422.         shl eax,8
  423.         mov al,bl
  424.         shl eax,8
  425.         mov al,dl
  426.         shl eax,8
  427.  
  428.         cmp eax,0
  429.         je old5
  430.  
  431.         mov di,0
  432.  
  433. k_loop3:
  434.         mov bp, di
  435.         shr bp, 2
  436.         cmp bp, psize
  437.         jge foundnew3
  438.         cmp eax,pallet[di]
  439.         je old4
  440.         add di,4
  441.         jmp k_loop3
  442.  
  443. old4:
  444.         mov ax,di
  445.         shr ax,2
  446.         add al,addvalue
  447. old5:
  448.         push esi
  449.         call write_ax_asdec
  450.         cmp xcount,1
  451.         je sk1
  452.         call write_comma
  453. sk1:
  454.         pop esi
  455.  
  456. foundnew3:
  457.         dec xcount
  458.         jne na_loop3
  459.  
  460.         push esi
  461.         call write_ret
  462.         cmp ysize,1
  463.         je  nd1
  464.         call write_db
  465. nd1:
  466.         pop esi
  467.         mov ax,xsize
  468.         mov xcount,ax
  469.  
  470.         dec ysize
  471.         jne doup
  472.  
  473.         call _closefile
  474.  
  475.         jmp _exit
  476.  
  477. write_dw:
  478.            mov ecx,4
  479.            mov edx,offset textdw
  480.            call _writefile
  481.            ret
  482.  
  483. textdw     db " dw "
  484.  
  485. write_number:
  486.            mov ecx,textgf-textde
  487.            mov edx,offset textde
  488.            call _writefile
  489.            ret
  490.  
  491. textde     db "; number of colours detected: "
  492. textgf:
  493.  
  494. write_comment:
  495.            mov ecx,textgo-textdi
  496.            mov edx,offset textdi
  497.            call _writefile
  498.            ret
  499.  
  500. textdi     db " ; x,y size of icon",13,10
  501. textgo:
  502.  
  503. write_db:
  504.            mov ecx,4
  505.            mov edx,offset textdb
  506.            call _writefile
  507.            ret
  508.  
  509. textdb     db " db "
  510.  
  511. writepallet:
  512.            mov ecx,7
  513.            mov edx,offset textdp
  514.            call _writefile
  515.            ret
  516.  
  517. textdp     db "palette"
  518.  
  519. write_ret:
  520.            mov ecx,2
  521.            mov edx,offset textret
  522.            call _writefile
  523.            ret
  524.  
  525. textret    db 13,10
  526.  
  527. write_al_ashex:
  528.            push ax
  529.            shr al,4
  530.            call dropchar
  531.            pop ax
  532.            call dropchar
  533.            ret
  534. dropchar:
  535.            and ax,0fh
  536.            mov si,ax
  537.            mov bl,chars[si]
  538.            mov byte2,bl
  539.            mov edx, offset byte2
  540.            mov ecx,1
  541.            call _writefile
  542.            ret
  543.  
  544. write_ax_asdec:
  545.            call _cv
  546.  
  547.            mov xq,0
  548.            mov vc,eax
  549.  
  550.            shr eax,16
  551.            call dropchar2
  552.            shr eax,12
  553.            call dropchar2
  554.            shr eax,8
  555.            call dropchar2
  556.            shr eax,4
  557.            call dropchar2
  558.            mov xq,1
  559.            call dropchar2
  560.            ret
  561.  
  562. dropchar2:
  563.            and ax,0fh
  564.            cmp ax,0
  565.            jne jo0
  566.            cmp xq,0
  567.            je nxq
  568. jo0:
  569.            mov xq,1
  570.            and ax,0fh
  571.            mov si,ax
  572.            mov bl,chars[si]
  573.            mov byte2,bl
  574.            mov edx, offset byte2
  575.            mov ecx,1
  576.            call _writefile
  577. nxq:
  578.            mov eax,vc
  579.            ret
  580.  
  581. _cv:
  582.         xor ecx,ecx
  583.         xor dx,dx
  584.         mov bx,10000
  585.         div bx
  586.         or cl,al
  587.         shl ecx,4
  588.         mov ax,dx
  589.         xor dx,dx
  590.         mov bx,1000
  591.         div bx
  592.         or cl,al
  593.         shl ecx,4
  594.         mov ax,dx
  595.         xor dx,dx
  596.         mov bx,100
  597.         div bx
  598.         or cl,al
  599.         shl ecx,4
  600.         mov ax,dx
  601.         xor dx,dx
  602.         mov bx,10
  603.         div bx
  604.         or cl,al
  605.         shl ecx,4
  606.         or cl,dl
  607.         mov eax,ecx
  608.         ret
  609.  
  610. vct:
  611.         push ax
  612.         shr ax,8
  613.         mov bx,100
  614.         and ax,0fh
  615.         mul bx
  616.         mov cx,ax
  617.         pop ax
  618.  
  619.         push ax
  620.         shr ax,4
  621.         mov bx,10
  622.         and ax,0fh
  623.         mul bx
  624.         add cx,ax
  625.         pop ax
  626.  
  627.         and ax,0fh
  628.         add ax,cx
  629.  
  630.         ret
  631.  
  632. write_comma:
  633.            mov al,","
  634.            call writeit
  635.            ret
  636. write_0:
  637.            mov al,"0"
  638.            call writeit
  639.            ret
  640. writeit:
  641.            mov byte2,al
  642.            mov edx,offset byte2
  643.            mov ecx,1
  644.            call _writefile
  645.            ret
  646.  
  647. _putdosmsg:
  648.         push ax
  649.         push edx
  650.         add edx,_code32a
  651.         mov al,dl
  652.         and ax,0fh
  653.         shr edx,4
  654.         mov v86r_ds,dx
  655.         mov v86r_dx,ax
  656.         mov v86r_ah,9
  657.         mov al,21h
  658.         int 33h
  659.         pop edx
  660.         pop ax
  661.         ret
  662.  
  663. code32  ends
  664.         end
  665.