PageRenderTime 40ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/+pr/saveas3dGif.m

https://bitbucket.org/bigdelys/measure-projection
Objective C | 75 lines | 57 code | 18 blank | 0 comment | 9 complexity | 65159a6d2ef1942e0e79443248f55002 MD5 | raw file
Possible License(s): GPL-2.0, BSD-2-Clause
  1. function saveas3dGif(outputFilename, varargin)
  2. % saveas3dGif(outputFilename, varargin)
  3. % create animated GIF from Matlab figure.
  4. %
  5. % 'rotationRange' 'real' [1 360] 5;... % maximum rotation in azimuth
  6. % 'numberOfFrames' 'integer' [2 Inf] 5;... % number of rotation levels (actual number of frame in the gif file will be twice - 1 of this number).
  7. % 'dpi' 'integer' [10 800] 100;... % quality (dot per inch = DPI) of the output images. This will determine the image size of output file.
  8. % 'dither' 'boolean' [] false ;... % dithering when reducing number of colors will increase the number of perceived colors but add some spatial high frequency noise.
  9. % 'emitWindowButtonMotionFcn' 'boolean' [] false;... % used for some special case with pr.plotCortex
  10. % 'frameDelay' 'real' [0 Inf] 0.1;... % how much each frame should be display. Lower values results in faster animation.
  11. % 'figurehandle' 'real' [] gcf; % handle of the figure to be used.
  12. %
  13. % Written by Nima Bigdely-Shamlo, Swartz Center. Copyright 2012, UCSD.
  14. inputOptions = finputcheck(varargin, ...
  15. { 'rotationRange' 'real' [1 360] 5;... % maximum rotation in azimuth
  16. 'numberOfFrames' 'integer' [2 Inf] 5;... % number of rotation levels (actual number of frame in the gif file will be twice - 1 of this number).
  17. 'dpi' 'integer' [10 800] 100;... % quality (dot per inch = DPI) of the output images. This will determine the image size of output file.
  18. 'dither' 'boolean' [] false ;... % dithering when reducing number of colors will increase the number of perceived colors but add some spatial high frequency noise.
  19. 'emitWindowButtonMotionFcn' 'boolean' [] false;... % used for some special case with pr.plotCortex
  20. 'frameDelay' 'real' [0 Inf] 0.1;... % how much each frame should be display. Lower values results in faster animation.
  21. 'figurehandle' 'real' [] gcf; % handle of the figure to be used.
  22. });
  23. [OriginalAz,originalEl] = view;
  24. %outputFilename = '~/plot/test3d.gif';
  25. %azimuth = linspace(OriginalAz, OriginalAz + inputOptions.rotationRange, inputOptions.numberOfFrames);
  26. nonlinearPower = 5;
  27. azimuth = linspace(OriginalAz^nonlinearPower, (OriginalAz + inputOptions.rotationRange) ^nonlinearPower, inputOptions.numberOfFrames) .^ (1/nonlinearPower);
  28. if inputOptions.emitWindowButtonMotionFcn
  29. callback = get(gcf, 'WindowButtonMotionFcn');
  30. end;
  31. % save pictures with rotated azimuth in a temporary location
  32. for i=1:length(azimuth)
  33. frameFilename{i}= [tempname '.png'];
  34. view(azimuth(i), originalEl);
  35. if inputOptions.emitWindowButtonMotionFcn
  36. callback{1}(gcf, [],callback{2}, callback{3}, callback{4}, callback{5}, callback{6});
  37. end;
  38. print('-dpng', ['-r' num2str(inputOptions.dpi)], frameFilename{i});
  39. end;
  40. % make a 3D gif from these images
  41. % make the colormap index
  42. % read, convert to indexed image and add to gif file.
  43. clear frameInIndex
  44. for i=1:length(azimuth)
  45. frameInRGB = imread(frameFilename{i});
  46. frameInRGB = im2double(frameInRGB);
  47. if i == 1
  48. [frameInIndex(:,:,1,i) colormapIndex] = rgb2ind(frameInRGB, 256, fastif(inputOptions.dither, 'dither', 'nodither'));
  49. else
  50. frameInIndex(:,:,1,i) = rgb2ind(frameInRGB, colormapIndex, fastif(inputOptions.dither, 'dither', 'nodither'));
  51. end;
  52. end;
  53. for i=(length(azimuth)-1):-1:1
  54. frameInIndex(:,:,1,end+1) = frameInIndex(:,:,1,i);
  55. end;
  56. imwrite(frameInIndex, colormapIndex, outputFilename, 'DelayTime', inputOptions.frameDelay, ...
  57. 'LoopCount', inf);