compositor for 2d glitch effects
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

source.pde 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. Capture cam;
  2. Movie movie;
  3. boolean moviePlaying, imageChanged;
  4. PImage fallbackImage;
  5. void movieEvent(Movie m) {
  6. if (moviePlaying) {
  7. m.read();
  8. //if mode = 1..
  9. sourceManager.setImage(movie.get());
  10. //movie.jump(random(movie.duration())); //??
  11. }
  12. }
  13. void newMovie(String path) {
  14. movie = new Movie(this, path);
  15. }
  16. boolean isVideo(String path) {
  17. String fpl = path.toLowerCase();
  18. if (fpl.endsWith(".mp4") || fpl.endsWith(".avi") || fpl.endsWith(".webm") || fpl.endsWith(".mkv") || fpl.endsWith(".mov")) return true;
  19. else return false;
  20. }
  21. boolean isImage(String path) {
  22. String fpl = path.toLowerCase();
  23. if (fpl.endsWith(".bmp") || fpl.endsWith(".png") || fpl.endsWith(".jpg") || fpl.endsWith(".jpeg") || fpl.endsWith(".gif")) return true; //gif = special case!
  24. else return false;
  25. }
  26. class SourceManager { //no constructor
  27. void setSource() { //set source to renderer
  28. renderer.beginDraw();
  29. renderer.image(source, 0, 0, renderer.width, renderer.height);
  30. renderer.endDraw();
  31. }
  32. void setImage(PImage img) { //set image to source
  33. imageChanged = true;
  34. source = img.get();
  35. //if(!webcamMode) fallbackImage = source;
  36. fallbackImage = source;
  37. nativeResoX = source.width;
  38. nativeResoY = source.height;
  39. //println(nativeResoX);
  40. }
  41. void fallback() {
  42. println("Fallback");
  43. setImage(fallbackImage); //fallbackImage should always be a copy of the last image that worked
  44. }
  45. /*
  46. void startWebcam() {
  47. String[] cameras = Capture.list();
  48. println(cameras);
  49. cam.start();
  50. webcamMode = true;
  51. }
  52. void stopWebcam() {
  53. cam.stop();
  54. webcamMode = false;
  55. }*/
  56. void update() {
  57. /*
  58. if (webcamMode) {
  59. if (cam.available()) {
  60. cam.read();
  61. setImage(cam);
  62. }
  63. }
  64. */
  65. if (source == null) {
  66. fallback();
  67. }
  68. int toW, toH;
  69. if (source.width > source.height) {
  70. toW = renderSize;
  71. toH = int(map(source.height, 0, source.width, 0, renderSize));
  72. } else {
  73. toW = int(map(source.width, 0, source.height, 0, renderSize));
  74. toH = renderSize;
  75. }
  76. toW = (toW % 2 == 0) ? toW : toW+1;
  77. toH = (toH % 2 == 0) ? toH : toH+1;
  78. renderer.setSize(toW, toH);
  79. }
  80. // This function returns all the files in a directory as an array of Strings
  81. String[] listFileNames(String dir) {
  82. File file = new File(dir);
  83. if (file.isDirectory()) {
  84. String names[] = file.list();
  85. return names;
  86. } else {
  87. return null;
  88. }
  89. }
  90. String dir;
  91. String[] selection;
  92. int selId;
  93. void importURI(String path) {
  94. File file = new File(path);
  95. if (file.isDirectory()) {
  96. dir = path;
  97. String names[] = file.list();
  98. ArrayList<String> usableFiles = new ArrayList<String>();
  99. for (int i = 0; i < names.length; i++) {
  100. if (isImage(names[i]) || isVideo(names[i])) {
  101. usableFiles.add(names[i]);
  102. }
  103. }
  104. selection = new String[usableFiles.size()];
  105. for (int i = 0; i < usableFiles.size(); i++) selection[i] = usableFiles.get(i);
  106. println("Got new selection from folder. " + selection.length + " usable files.");
  107. path = dir + "/" + selection[(int)random(selection.length)];
  108. }
  109. if (isImage(path) || isVideo(path)) {
  110. println("Importing file " + path);
  111. if (moviePlaying) movie.stop();
  112. //if (webcamMode) gui.webcam.setValue(false);
  113. if (isImage(path)) {
  114. moviePlaying = false;
  115. setImage(loadImage(path));
  116. } else if (isVideo(path)) {
  117. moviePlaying = true;
  118. newMovie(path);
  119. movie.loop();
  120. movie.volume(0);
  121. }
  122. }
  123. }
  124. }