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