Advertisement
Guest User

carandraug

a guest
Apr 6th, 2011
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. // "Functions library for ImageJ macro language"
  2. // Copyright (c) 2011 CarnĂ« Draug <[email protected]>
  3.  
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program; If not, see <http://www.gnu.org/licenses/>.
  16.  
  17. ////////////////////////////////////////////////////////////////////////////////
  18. // Non-messy functions
  19. ////////////////////////////////////////////////////////////////////////////////
  20.  
  21. // opens an image and returns its ID
  22. function open_ID (filename) {
  23. open(filename);
  24. ID = getImageID();
  25. return ID;
  26. }
  27.  
  28. // closes the image with the specified ID
  29. function close_by_ID(ID) {
  30. selectImage(ID);
  31. close();
  32. }
  33.  
  34. // Returns the list of files a directory with the specified extension. It's recursive
  35. function get_cleansed_file_list (dir, extension) {
  36.  
  37. list = getFileList(dir);
  38. // the function newArray doesn't with no arguments, must really start that way
  39. cleansed = newArray(1);
  40.  
  41. for (i=0; i<list.length; i++) {
  42. tmp = dir + list[i];
  43. if (endsWith(list[i], extension)) {
  44. // if the first value is a zero, it's the first value to be entered on the
  45. // array and must replace the one entered to create it
  46. if (cleansed[0] == 0) {
  47. cleansed[0] = tmp;
  48. } else {
  49. cleansed = append_to_array (tmp, cleansed);
  50. }
  51. // if it's a directory, go recursive and get the files from there
  52. } else if (File.isDirectory(tmp)) {
  53. new_list = get_cleansed_file_list (tmp, extension);
  54. // if the first value is a zero, it's the first value to be entered on the
  55. // array and must replace the one entered to create it
  56. if (cleansed[0] == 0) {
  57. cleansed = new_list;
  58. } else if (new_list.length == 1 && new_list[0] == 0) {
  59. // do nothing, this directory had no good file and appending it will
  60. // append a zero to the list
  61. } else {
  62. cleansed = concatenate_array (cleansed, new_list);
  63. }
  64. } else {
  65. // do nothing, not the right file type
  66. }
  67. }
  68. return cleansed;
  69. }
  70.  
  71.  
  72. ////////////////////////////////////////////////////////////////////////////////
  73. // New array functions
  74. ////////////////////////////////////////////////////////////////////////////////
  75.  
  76. // Adds all values from array2 to the end of array 1. Returns a new array.
  77. function concatenate_array (array1, array2) {
  78. length_1 = lengthOf(array1);
  79. length_2 = lengthOf(array2);
  80. new = newArray(length_1 + length_2);
  81. for (i=0; i<length_1; i++) {
  82. new[i] = array1[i];
  83. }
  84. for (i=0; i<length_2; i++) {
  85. new[length_1 + i] = array2[i];
  86. }
  87. return new;
  88. }
  89.  
  90. // Remove the value with specific index from array. Returns a new array
  91. function remove_from_array (array, index) {
  92. length = lengthOf(array);
  93. new = newArray(length - 1);
  94. for (i=0; i<index; i++) {
  95. new[i] = array[i];
  96. }
  97. for (i=(index+1); i<length; i++) {
  98. new[i] = array[i];
  99. }
  100. return new;
  101. }
  102.  
  103. // Adds value at position in array. If necessary, expanding it. returns a new array
  104. function add_to_array (value, array, position) {
  105. ori_length = lengthOf(array);
  106. if (position < ori_length) {
  107. array[position] = value;
  108. return array;
  109. } else {
  110. new = newArray(position + 1);
  111. for (i=0; i<ori_length; i++) {
  112. new[i] = array[i];
  113. }
  114. new[position] = value;
  115. return new;
  116. }
  117. }
  118.  
  119. // Appends value to the end of array. Returns a new array.
  120. function append_to_array (value, array) {
  121. ori_length = lengthOf(array);
  122. new = newArray(ori_length + 1);
  123. for (i=0; i<ori_length; i++) {
  124. new[i] = array[i];
  125. }
  126. new[ori_length] = value;
  127. return new;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement