header2hx.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. function header2hx(headerFileName,imgFileName,amiraScriptFileName)
  2. % Creates an Amira .hx script from a Pinnacle .header file. The .hx script
  3. % can then be run from inside Amira to open the image. The central idea is
  4. % that a separate .am file will not have to be created just to view an
  5. % image in Amira, thus saving hard drive space.
  6. %
  7. % Inputs: headerFileName: [char] % location of Pinnacle header file
  8. % imgFileName: [char] % location of Pinnacle data file
  9. % amiraScriptFileName: [char] % location of Amira .hx file
  10. %
  11. % Ryan T Flynn 10/26/06
  12. % ensure that the header file has the correct extension
  13. if strcmp(headerFileName,'.header');
  14. error([headerFileName ' does not have the .header extension.']);
  15. end
  16. % ensure that the data file has the correct extension
  17. if strcmp(imgFileName,'.img');
  18. error([imgFileName ' does not have the .img extension.']);
  19. end
  20. % ensure that the amira filename has the correct extension
  21. if strcmp(amiraScriptFileName,'.hx');
  22. error([imgFileName ' does not have the .hx extension.']);
  23. end
  24. fid = fopen(headerFileName,'r');
  25. if fid == -1
  26. error(['Unable to open ' headerFileName '.']);
  27. end
  28. tline = fgets(fid);
  29. while tline ~= -1
  30. % check the line for key words
  31. if length(findstr(tline,'x_dim')) & ~length(findstr(tline,'fname_index_start'))
  32. eval(tline); % run the line to get x_dim
  33. elseif length(findstr(tline,'y_dim'))
  34. eval(tline); % run the line to get y_dim
  35. elseif length(findstr(tline,'z_dim'))
  36. eval(tline); % run the line to get z_dim
  37. elseif length(findstr(tline,'x_pixdim'))
  38. eval(tline); % run the line to get x_pixdim
  39. elseif length(findstr(tline,'y_pixdim'))
  40. eval(tline); % run the line to get y_pixdim
  41. elseif length(findstr(tline,'z_pixdim'))
  42. eval(tline); % run the line to get z_pixdim
  43. elseif length(findstr(tline,'x_start'))
  44. eval(tline); % run the line to get x_start
  45. elseif length(findstr(tline,'y_start'))
  46. eval(tline); % run the line to get x_start
  47. elseif length(findstr(tline,'z_start'))
  48. eval(tline); % run the line to get x_start
  49. elseif length(findstr(tline,'bytes_pix'))
  50. eval(tline); % number of bytes per pixel
  51. elseif length(findstr(tline,'byte_order'))
  52. eval(tline); % number of bytes per pixel
  53. end
  54. tline = fgets(fid);
  55. end
  56. fclose(fid);
  57. % calculate axes for the image
  58. x = x_start + [0:x_dim-1]*x_pixdim;
  59. y = y_start + [0:y_dim-1]*y_pixdim;
  60. z = z_start + [0:z_dim-1]*z_pixdim;
  61. % deduce the data type of the img file
  62. if bytes_pix == 2
  63. dataType = 'short 1';
  64. elseif bytes_pix == 4
  65. dataType = 'float 1';
  66. end
  67. % assign little of big endian byte orders
  68. if byte_order == 0
  69. byteOrder = 'little';
  70. elseif byte_order == 1
  71. byteOrder = 'big';
  72. else
  73. error(['Unknown byte order in ' headerFileName '.']);
  74. end
  75. % create the .hx file
  76. fid = fopen(amiraScriptFileName,'w');
  77. % name of the structure that will be opened in Amira
  78. amiraStructName = regexprep(amiraScriptFilleName,'.hx','');
  79. % create Amira script
  80. fprintf(fid,'# Amira Script\n');
  81. fprintf(fid,['remove ' imgFileName '\n']);
  82. fprintf(fid,'\n');
  83. fprintf(fid,'set hideNewModules 0\n');
  84. fprintf(fid,['[ load -raw ' imgFileName ' ' byteOrder ' xfastest ' dataType ' ' num2str(x_dim) ' ' num2str(y_dim) ' ' num2str(z_dim) ...
  85. ' ' num2str(x(1)) ' ' num2str(x(end)) ' ' num2str(y(1)) ' ' num2str(y(end)) ' ' num2str(z(1)) ' ' num2str(z(end)) ...
  86. '] setLabel ' imgFileName '\n']);
  87. fprintf(fid,[imgFileName ' setIconPosition 20 10\n']);
  88. fprintf(fid,[imgFileName ' flip 1\n']); % flip the data along the y-axis
  89. fprintf(fid,[imgFileName ' fire\n']);
  90. fprintf(fid,[imgFileName ' setViewerMask 65535\n']);
  91. fprintf(fid,'set hideNewModules 0\n');
  92. fclose(fid);