Advertisement
Guest User

Untitled

a guest
May 30th, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. xed=640:yed=400:wdwd=1:ratio$="pal":delaysec=2
  2. setdisplay(xed,yed,32,wdwd):paper(0xFFFFFF):ink(0):cls
  3.  
  4. '- c64 lores picture converter (webcam capture) - sdlBasic version - Paulo Silva - june'10 - (converter started august'02) - GPL licence
  5. '- only tested on Ubuntu Linux - terminal command line below install needed packages (Ubuntu and Debian):
  6. '-   sudo apt-get install sdlbrt imagemagick fswebcam sdlbasic
  7.  
  8. dim cr[40,25,16],mt[40,25],cmu[16],ag[40,25,4],iklv[16],kv[4],colourid[16],colourid2[16],pke[16384]
  9. ' cr[,,] - ammount of pixels from each colour in each attribute
  10. ' mt[,]  - ammount of colours in each attribute
  11. ' cmu[]  - ammount of attributes with 4 colours or more which each colour belongs
  12. ' cnt    - most used colour in attributes with 4 colours or more
  13. ' asg[,,]- attributes
  14.  
  15. colourid[0]=0x000:colourid[1]=0xFFF:colourid[2]=0x931:colourid[3]=0x5BD
  16. colourid[4]=0x93C:colourid[5]=0x4A1:colourid[6]=0x32C:colourid[7]=0xBD3
  17. colourid[8]=0x950:colourid[9]=0x440:colourid[10]=0xC64:colourid[11]=0x444
  18. colourid[12]=0x777:colourid[13]=0x8E5:colourid[14]=0x75F:colourid[15]=0x999
  19.  
  20. for i=0 to 15
  21.   u=colourid[i]
  22.   rlv=(int(u/256))
  23.   glv=bitwiseand((int(u/16)),15)
  24.   blv=bitwiseand(u,15)
  25.   iklv[i]=((blv*11)+(glv*59)+(rlv*30))/15
  26.   next
  27. for i=0 to 15
  28.   u=colourid[i]
  29.   rlv=(int(u/256))
  30.   glv=bitwiseand((int(u/16)),15)
  31.   blv=bitwiseand(u,15)
  32.   colourid2[i]=(int((rlv/15)*255))*65536+(int((glv/15)*255))*256+(int((blv/15)*255))
  33.   next
  34.  
  35. while 0=0
  36.  
  37. setcaption("webcam - ratio: "+ratio$)
  38. finp$="_tmpr2.jpg"
  39. shell("fswebcam -d /dev/video0 _tmpr1.jpg")
  40.  
  41. if ratio$="normal" then:
  42.   shell("convert _tmpr1.jpg -resize 320x200\\! "+finp$)
  43.   loadimage(finp$,1):pasteicon(0,0,1)
  44.   end if
  45. if ratio$="pal" then:
  46.   shell("convert _tmpr1.jpg -resize 320x256\\! "+finp$)
  47.   loadimage(finp$,1):pasteicon(0,-28,1)
  48.   end if
  49. if ratio$="ntsc" then:
  50.   shell("convert _tmpr1.jpg -resize 320x214\\! _tmpr2.jpg")
  51.   loadimage(finp$,1):pasteicon(0,-7,1)
  52.   end if
  53.  
  54. ' ----- step 1
  55. ' ----- counts ammount of pixels from each colour from each attribute
  56. for y1=0 to 24
  57.   setcaption("step 1: "+str$(y1)+"/24")
  58.   for x1=0 to 39
  59.     for y2=0 to 7
  60.       for x2=0 to 3
  61.         x=x2+(X1*4)
  62.         y=y2+(y1*8)
  63.  
  64.         u2=point(x*2,y)
  65.         u=0:dis=1000
  66.         for i=0 to 15
  67.           qq=colourid[i]
  68.           blv=qq mod 16:qq=int(qq/16)
  69.           glv=qq mod 16:qq=int(qq/16)
  70.           rlv=qq mod 16
  71.           qq=u2
  72.           blv2=int(qq/16) mod 16:qq=int(qq/256)
  73.           glv2=int(qq/16) mod 16:qq=int(qq/256)
  74.           rlv2=int(qq/16) mod 16
  75.           bdist=abs(blv-blv2)
  76.           gdist=abs(glv-glv2)
  77.           rdist=abs(rlv-rlv2)
  78.           rgbdist=sqr((rdist^2)+(gdist^2)+(bdist^2))
  79.           if rgbdist<dis then:dis=rgbdist:u=i:end if
  80.           next
  81.  
  82.         cr[x1,y1,u]=cr[x1,y1,u]+1
  83.         next:next
  84.     next:next
  85.  
  86. ' ----- step 2
  87. ' ----- counts ammount of colours in each attribute
  88. for y1=0 to 24
  89.   setcaption("step 2: "+str$(y1)+"/24")
  90.   for x1=0 to 39
  91.     es=0
  92.     for e=0 to 15
  93.       if cr[x1,y1,e]<>0 then:es=es+1:end if
  94.       next
  95.     mt[x1,y1]=es
  96.     next:next
  97.  
  98. ' ----- step 3
  99. ' ----- counts the most used colours in attributes with 4 colours or more
  100. for Y1=0 to 24
  101.   setcaption("step 3: "+str$(y1)+"/24")
  102.   for x1=0 to 39
  103.     if mt[x1,y1]>=4 then:
  104.       for e=0 to 15
  105.         if cr[x1,y1,e]>0 then:
  106.           cmu[e]=cmu[e]+1
  107.           end if
  108.         next
  109.       end if
  110.     next:next
  111.  
  112. ' ----- step 4
  113. ' ----- looks for the most used colour from attributes with 4 colours or more
  114. setcaption("step 4")
  115. curc=15:cnt=0
  116. for e=15 to 0 step -1
  117.   If cmu[e]>cnt then:
  118.     cnt=cmu[e]:curc=e
  119.     end if
  120.   next
  121.  
  122. ' ----- step 5
  123. ' ----- sets index 0 from each attribute with most used colour in attribute with 4 colours or more
  124. for y1=0 to 24
  125.   setcaption("step 5: "+str$(y1)+"/24")
  126.   for x1=0 to 39
  127.     for g=0 to 3
  128.       ag[x1,y1,g]=16
  129.       next
  130.     ag[x1,y1,0]=curc:cr[x1,y1,curc]=0
  131.     next:next
  132.  
  133. ' ----- step 6
  134. ' ----- sets index from 1 to 3 from each attribute with remaining colours
  135. for y1=0 to 24
  136.   setcaption("step 6: "+str$(y1)+"/24")
  137.   for x1=0 to 39
  138.     for g=1 to 3
  139.       mx=0:ct=0
  140.       for e=0 to 15
  141.         if cr[x1,y1,e]>mx then:
  142.           ct=e:mx=cr[x1,y1,e]
  143.           end if
  144.         next
  145.       cr[x1,y1,ct]=0:ag[x1,y1,g]=ct
  146.       next:next:next
  147.  
  148. ' ----- step 7
  149. ' ----- cleans value 16 generated from step 5 (becomes background0)
  150. for y1=0 to 24
  151.   setcaption("step 7: "+str$(y1)+"/24")
  152.   for x1=0 to 39
  153.     for g=1 to 3
  154.       if ag[x1,y1,g]>15 then:
  155.         ag[x1,y1,g]=ag[0,0,0]
  156.         end if
  157.       next:next:next
  158.  
  159. ' ----- step 8
  160. ' ----- shows the colours being used
  161. ink(colourid2[ag[0,0,0]]):bar(0,200,319,399)
  162. for y1=0 to 24
  163.   setcaption("step 8: "+str$(y1)+"/24")
  164.   for x1=0 to 39
  165.     for e=1 to 3
  166.       ink (colourid2[ag[x1,y1,e]])
  167.       px=320+(x1*8)+1:py=(y1*8)+((e-1)*2)+1
  168.       bar(px,py,px+5,py+1)
  169.       next:next:next
  170.  
  171. ' ----- step 9
  172. ' ----- shows the picture about to be saved, and creates the picture of the final binary file
  173. ink (colourid2[ag[0,0,0]]):bar(0,200,319,399)
  174. for y1=0 to 24
  175.   setcaption("step 9: "+str$(y1)+"/24")
  176.   for x1=0 to 39
  177.     for e=0 to 3:kv[e]=iklv[ag[x1,y1,e]]:next
  178.     for y2=0 to 7
  179.       vac=0
  180.       for x2=0 to 3
  181.         x=(x1*4)+x2:y=(y1*8)+y2
  182.  
  183.         u2=point((x*2),y)
  184.         u=0:dis=1000
  185.         for i=0 to 15
  186.           qq=colourid[i]
  187.           blv=qq mod 16:qq=int(qq/16)
  188.           glv=qq mod 16:qq=int(qq/16)
  189.           rlv=qq mod 16
  190.           qq=u2
  191.           blv2=int(qq/16) mod 16:qq=int(qq/256)
  192.           glv2=int(qq/16) mod 16:qq=int(qq/256)
  193.           rlv2=int(qq/16) mod 16
  194.           bdist=abs(blv-blv2)
  195.           gdist=abs(glv-glv2)
  196.           rdist=abs(rlv-rlv2)
  197.           rgbdist=sqr((rdist^2)+(gdist^2)+(bdist^2))
  198.           if rgbdist<dis then:dis=rgbdist:u=i:end if
  199.           next
  200.         kvv=iklv[u]
  201.  
  202.         diff=1000:vap=0
  203.         for e=0 to 3
  204.           if diff>(abs(kvv-kv[e])) then:
  205.             vap=e:diff=(abs(kvv-kv[e]))      
  206.             end if
  207.           next
  208.  
  209.         ink(colourid2[ag[x1,y1,vap]]):dot((x*2),y+200):dot((x*2)+1,y+200)
  210.         ink(colourid2[vap]):dot((x*2)+320,y+200) : dot((x*2)+321,y+200)
  211.         vac=vac+(vap*(4^(3-x2)))
  212.         next
  213.       adrp=0+2+y2+(x1*8)+(y1*320)
  214.       pke[adrp]=vac
  215.       next:next:next
  216.  
  217. ' ----- step 10
  218. ' ----- create binary from attributes
  219. for y1=0 to 24
  220.   setcaption("step 10: "+str$(y1)+"/24")
  221.   'locate 41,51 : Print "passo 10 - ";Y1;"/24     "
  222.  
  223.   for x1=0 to 39
  224.     adrp=0+x1+(y1*40)
  225.     pke[adrp+8002]=((ag[x1,y1,1])*16)+ag[x1,y1,2]
  226.     pke[adrp+9018]=ag[x1,y1,3]
  227.     next:next
  228.   pke[0+9002]=0
  229.   pke[0+9003]=ag[0,0,0]
  230.   pke[0+0]=0
  231.   pke[0+1]=32
  232.  
  233. ' ----- step 10
  234. ' ----- saves the final binary
  235. setcaption("step 10: saving...")
  236. 'Bsave Fsel$('*.mpic','*.mpic','*.mpic'),Start(4) to Start(4)+10018
  237. open finp$+".mpic" for output as #1
  238.   for i=0 to 9009
  239.     writebyte(1,pke[i])
  240.     next
  241.   close #1
  242.  
  243. grab (1,0,200,320,200):saveimage(finp$+"_.bmp",1)
  244. shell("convert "+finp$+"_.bmp"+" "+finp$+"_.png"):shell("rm "+finp$+"_.bmp")
  245.  
  246. setcaption("waiting "+str$(delaysec)+" seconds...")
  247. wait(delaysec*1000)
  248.  
  249. wend
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement