Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.98 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. PImage img;
  4. float scale=4;
  5.  
  6. double temp=100;
  7. double cooling=0.99999;
  8. int coolCount=2000;
  9.  
  10. float[][] outp;
  11. void setup() {
  12. size(1000, 1000);
  13. selectInput("Select a file to process:", "fileSelected");
  14. background(0);
  15. };
  16.  
  17. void draw() {
  18. background(0);
  19. if (pf!=null) {
  20. pf.improvePath();
  21. pf.drawPath();
  22. //image(img, 0, 0);
  23. }
  24. };
  25.  
  26. float[][] diff1={{0, 1}, {1, 0}};
  27. float[][] diff2={{0, 0, 7}, {3, 5, 1}};
  28.  
  29. Pathfinder pf;
  30. float zf=1;
  31. void keyPressed() {
  32. background(0);
  33. if (key=='f') {
  34. ditherErrDiff(img, outp, diff2);
  35. pf=new Pathfinder(outp);
  36. }
  37. if (key=='i') {
  38. //pf.improvePath();
  39. }
  40. if (keyCode==UP) {
  41. zf*=1.1;
  42. }
  43. if (keyCode==DOWN) {
  44. zf/=1.1;
  45. }
  46. stroke(color(255));
  47. strokeWeight(1);
  48. //pf.drawPoints();
  49. pf.drawPath();
  50. };
  51.  
  52. void ditherErrDiff(PImage src, float[][] aim, float[][]diffmask) {
  53. if (aim!=null) {
  54. for (int i =0; i<aim.length; i++) {
  55. for (int j=0; j<aim[i].length; j++) {
  56. aim[i][j]=0;
  57. //println(aim[i][j]);
  58. }
  59. }
  60. float maskSum=0;
  61. float[][] maskNorm=new float[diffmask.length][diffmask[0].length];
  62. for (int i =0; i<diffmask.length; i++) {
  63. for (int j=0; j<diffmask[i].length; j++) {
  64. maskSum+=diffmask[i][j];
  65. }
  66. }
  67. for (int i =0; i<diffmask.length; i++) {
  68. for (int j=0; j<diffmask[i].length; j++) {
  69. maskNorm[i][j]=diffmask[i][j]/maskSum;
  70. }
  71. }
  72. src.loadPixels();
  73. loadPixels();
  74. for (int i =0; i<aim.length; i++) {
  75. for (int j=0; j<aim[i].length; j++) {
  76. float val=brightness(src.pixels[j*src.width+i])+aim[i][j];
  77. //println(val,aim[i][j]);
  78. aim[i][j]=val>128?255:0;
  79. float err=val-aim[i][j];
  80.  
  81. //println(i,j,err);
  82. /*try {
  83. pixels[j*width+i]=color(aim[i][j]);
  84. }
  85. catch(ArrayIndexOutOfBoundsException e) {
  86. }*/
  87. for (int k =0; k<maskNorm.length&&i+k<aim.length; k++) {
  88. for (int l=0; l<maskNorm[k].length&&j+l<aim[i+k].length; l++) {
  89.  
  90. aim[i+k][j+l]+=maskNorm[k][l]*err;
  91. //println(maskNorm[k][l],aim[i+k][j+l]);
  92. }
  93. }
  94. }
  95. }
  96. updatePixels();
  97. } else {
  98. println("ayyy");
  99. }
  100. }
  101.  
  102. void fileSelected(File selection) {
  103. if (selection == null) {
  104. println("Window was closed or the user hit cancel.");
  105. } else {
  106. println("User selected " + selection.getAbsolutePath());
  107. img=loadImage(selection.getAbsolutePath());
  108.  
  109.  
  110.  
  111. println(img.height, displayHeight);
  112. while (img.height>displayHeight) {
  113. println("DIVIDE!");
  114. img.resize(img.width/2, img.height/2);
  115. }
  116. println(img.width/2, displayWidth);
  117. while (img.width*2>displayWidth) {
  118. println("DIVIDE!");
  119. img.resize(img.width/2, img.height/2);
  120. }
  121. img.resize(int(img.width/scale), int(img.height/scale));
  122. }
  123. outp=new float[img.width][img.height];
  124. }
  125.  
  126.  
  127. class Pathfinder {
  128. int[] path;
  129. PVector[] points;
  130.  
  131. Pathfinder(float[][] src) {
  132. ArrayList<PVector> pzw=new ArrayList<PVector>();
  133. for (int i =0; i<src.length; i++) {
  134. for (int j=0; j<src[i].length; j++) {
  135. if (src[i][j]==0) {
  136. if (random(1)>0.5) {
  137. pzw.add(new PVector(i*scale, j*scale).add(PVector.random2D().mult(random(scale/2))));
  138. }
  139. }
  140. }
  141. }
  142. Collections.shuffle(pzw);
  143.  
  144. points=new PVector[pzw.size()];
  145. path=new int[pzw.size()];
  146. int i=0;
  147. for (PVector p : pzw) {
  148. points[i]=p;
  149. path[i]=points.length-i-1;
  150. i++;
  151. }
  152. }
  153.  
  154.  
  155. int count=0;
  156. void improvePath() {
  157. for (int i=0; i<1000000; i++) {
  158. int a=int(random(path.length-2)+1);
  159. int b=int(random(path.length-2)+1);
  160. if (abs(a-b)>1) {
  161. float d1=points[path[a-1]].dist(points[path[a]])+
  162. points[path[a]].dist(points[path[a+1]])+
  163. points[path[b-1]].dist(points[path[b]])+
  164. points[path[b]].dist(points[path[b+1]]);
  165. float d2=points[path[a-1]].dist(points[path[b]])+
  166. points[path[b]].dist(points[path[a+1]])+
  167. points[path[b-1]].dist(points[path[a]])+
  168. points[path[a]].dist(points[path[b+1]]);
  169. //println(d1,d2);
  170. if (d2-d1<temp) {
  171. //print(path[a],path[b]," old ");
  172. int zw=path[a];
  173. path[a]=path[b];
  174. path[b]=zw;
  175. //println(a, b, " " ,path[a],path[b], "swap");
  176. }
  177. count++;
  178. if (count%coolCount==0) {
  179. temp*=cooling;
  180. count=0;
  181. }
  182. }
  183. }
  184. }
  185.  
  186. void drawPoints() {
  187. for (PVector p : points) {
  188. point(p.x, p.y);
  189. }
  190. };
  191.  
  192. void drawPath() {
  193. PVector last=points[path[0]];
  194. PVector n;
  195. float totalLength=0;
  196. for (int i=1; i<path.length; i++) {
  197. n=points[path[i]];
  198. line(last.x*zf, last.y*zf, n.x*zf, n.y*zf);
  199. totalLength+=last.dist(n);
  200.  
  201. last=n;
  202. }
  203. println("total length: " +totalLength+ " temp: " +temp);
  204. };
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement