Advertisement
Guest User

Untitled

a guest
Oct 9th, 2016
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. import java.util.*;
  2. PImage src;
  3. PImage res;
  4.  
  5. // Bayer matrix
  6. int[][] matrix = {
  7. {
  8. 1, 9, 3, 11
  9. }
  10. ,
  11. {
  12. 13, 5, 15, 7
  13. }
  14. ,
  15. {
  16. 4, 12, 2, 10
  17. }
  18. ,
  19. {
  20. 16, 8, 14, 6
  21. }
  22. };
  23.  
  24.  
  25.  
  26. float mratio = 1.0 / 18.5; //okay as is
  27. float mfactor = 255.0 / 5; //okay as is
  28. int coloram = 12; //IMPORTANT Amount of colors you want in your result
  29. color[] altpal = new color[coloram];
  30.  
  31. void setup() {
  32. src = loadImage("wew.png");
  33. res = createImage(src.width, src.height, RGB);
  34. size(src.width, src.height, JAVA2D);
  35.  
  36. noLoop();
  37. noStroke();
  38. noSmooth();
  39. }
  40.  
  41. void draw() {
  42. // Init canvas
  43. background(0, 0, 0);
  44.  
  45. image(src, 0, 0);
  46.  
  47. // Define step
  48. int s = 1;
  49. collorcollector();
  50. // Scan image
  51. for (int x = 0; x < src.width; x+=s) {
  52. for (int y = 0; y < src.height; y+=s) {
  53. // Calculate pixel
  54. color oldpixel = src.get(x, y);
  55. color value = color( (oldpixel >> 16 & 0xFF) + (mratio*matrix[x%4][y%4] * mfactor), (oldpixel >> 8 & 0xFF) + (mratio*matrix[x%4][y%4] * mfactor), (oldpixel & 0xFF) + + (mratio*matrix[x%4][y%4] * mfactor) );
  56. color newpixel = findClosestColor(value);
  57. src.set(x, y, newpixel);
  58. // Draw
  59. stroke(newpixel);
  60. //point(x, y);
  61. line(x,y,x+s,y+s);
  62. }
  63. }
  64.  
  65. save(hour() + second() + millis()*100 + "result.png");
  66. exit();
  67.  
  68. }
  69.  
  70. // Find closest colors in palette
  71. color findClosestColor(color in) {
  72.  
  73. //Palette colors
  74. color[] palette2 = {
  75. color(0),
  76. color(255),
  77. color(255, 0, 0),
  78. color(0, 255, 0),
  79. color(0, 0, 255),
  80. color(255, 255, 0),
  81. color(0, 255, 255),
  82. color(255, 0, 255),
  83. color(0, 255, 255),
  84. };
  85.  
  86. color[] palette = altpal;
  87.  
  88.  
  89. PVector[] vpalette = new PVector[palette.length];
  90. PVector vcolor = new PVector( (in >> 16 & 0xFF) , (in >> 8 & 0xFF), (in & 0xFF));
  91. int current = 0;
  92. float distance = vcolor.dist(new PVector(0,0,0));
  93.  
  94. for (int i=0; i<palette.length; i++) {
  95. // Using bit shifting in for loop is faster
  96. int r = (palette[i] >> 16 & 0xFF);
  97. int g = (palette[i] >> 8 & 0xFF);
  98. int b = (palette[i] & 0xFF);
  99. vpalette[i] = new PVector(r, g, b);
  100. float d = vcolor.dist(vpalette[i]);
  101. if (d < distance) {
  102. distance = d;
  103. current = i;
  104. }
  105. }
  106. return palette[current];
  107. }
  108.  
  109.  
  110.  
  111. void collorcollector() {
  112. int rx;
  113. int ry;
  114. int i = 0;
  115. rx = int(random(src.width));
  116. ry = int(random(src.height));
  117. for (int x = rx; x < src.width; x++) {
  118. for (int y = ry; y < src.height; y++) {
  119. //i < altpal.length
  120. if( !Arrays.asList(altpal).contains(color(get(x,y)))){
  121.  
  122. if(i == altpal.length){
  123. break;
  124. }
  125.  
  126. altpal[i]=color(get(x,y));
  127. i++;
  128.  
  129. x = int(random(src.width));
  130. y = int(random(src.height));
  131. System.out.println("flag1 " + altpal[i-1] + i + color(get(x,y)));
  132. }
  133. }
  134. }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement