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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. 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. void startWebcam() {
  45. String[] cameras = Capture.list();
  46. println(cameras);
  47. cam.start();
  48. webcamMode = true;
  49. }
  50. void stopWebcam() {
  51. cam.stop();
  52. webcamMode = false;
  53. }
  54. void update() {
  55. if (webcamMode) {
  56. if (cam.available()) {
  57. cam.read();
  58. setImage(cam);
  59. }
  60. }
  61. if (source == null) {
  62. fallback();
  63. }
  64. int toW, toH;
  65. if (source.width > source.height) {
  66. toW = renderSize;
  67. toH = int(map(source.height, 0, source.width, 0, renderSize));
  68. } else {
  69. toW = int(map(source.width, 0, source.height, 0, renderSize));
  70. toH = renderSize;
  71. }
  72. toW = (toW % 2 == 0) ? toW : toW+1;
  73. toH = (toH % 2 == 0) ? toH : toH+1;
  74. renderer.setSize(toW, toH);
  75. }
  76. // This function returns all the files in a directory as an array of Strings
  77. String[] listFileNames(String dir) {
  78. File file = new File(dir);
  79. if (file.isDirectory()) {
  80. String names[] = file.list();
  81. return names;
  82. } else {
  83. return null;
  84. }
  85. }
  86. String dir;
  87. String[] selection;
  88. int selId;
  89. void importURI(String path) {
  90. File file = new File(path);
  91. if (file.isDirectory()) {
  92. dir = path;
  93. String names[] = file.list();
  94. ArrayList<String> usableFiles = new ArrayList<String>();
  95. for (int i = 0; i < names.length; i++) {
  96. if (isImage(names[i]) || isVideo(names[i])) {
  97. usableFiles.add(names[i]);
  98. }
  99. }
  100. selection = new String[usableFiles.size()];
  101. for (int i = 0; i < usableFiles.size(); i++) selection[i] = usableFiles.get(i);
  102. println("Got new selection from folder. " + selection.length + " usable files.");
  103. path = dir + "/" + selection[(int)random(selection.length)];
  104. }
  105. if (isImage(path) || isVideo(path)) {
  106. println("Importing file " + path);
  107. if (moviePlaying) movie.stop();
  108. if (webcamMode) gui.webcam.setValue(false);
  109. if (isImage(path)) {
  110. moviePlaying = false;
  111. setImage(loadImage(path));
  112. } else if (isVideo(path)) {
  113. moviePlaying = true;
  114. newMovie(path);
  115. movie.loop();
  116. movie.volume(0);
  117. }
  118. }
  119. }
  120. }