Changeset 18904 for issm/trunk-jpl/externalpackages/export_fig/export_fig.m
- Timestamp:
- 12/02/14 11:40:53 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
issm/trunk-jpl/externalpackages/export_fig/export_fig.m
r14174 r18904 13 13 % export_fig ... -a<val> 14 14 % export_fig ... -q<val> 15 % export_fig ... -p<val> 15 16 % export_fig ... -<renderer> 16 17 % export_fig ... -<colorspace> … … 85 86 % made transparent (png, pdf and eps output only). 86 87 % -m<val> - option where val indicates the factor to magnify the 87 % on-screen figure dimensions by when generating bitmap88 % on-screen figure pixel dimensions by when generating bitmap 88 89 % outputs. Default: '-m1'. 89 90 % -r<val> - option val indicates the resolution (in pixels per inch) to 90 % export bitmap outputs at, keeping the dimensions of the91 % o n-screen figure. Default: sprintf('-r%g', get(0,92 % 'ScreenPixelsPerInch')). Note that the -m and -r options93 % change the same property.91 % export bitmap and vector outputs at, keeping the dimensions 92 % of the on-screen figure. Default: '-r864' (for vector output 93 % only). Note that the -m option overides the -r option for 94 % bitmap outputs only. 94 95 % -native - option indicating that the output resolution (when outputting 95 96 % a bitmap format) should be such that the vertical resolution … … 118 119 % sometimes give a smaller file size than the default lossy 119 120 % compression, depending on the type of images. 121 % -p<val> - option to add a border of width val to eps and pdf files, 122 % where val is in units of the intermediate eps file. Default: 123 % 0 (i.e. no padding). 120 124 % -append - option indicating that if the file (pdfs only) already 121 125 % exists, the figure is to be appended as a new page, instead … … 133 137 % 134 138 % Some helpful examples and tips can be found at: 135 % http ://sites.google.com/site/oliverwoodford/software/export_fig139 % https://github.com/ojwoodford/export_fig 136 140 % 137 141 % See also PRINT, SAVEAS. 138 142 139 % Copyright (C) Oliver Woodford 2008-201 2143 % Copyright (C) Oliver Woodford 2008-2014 140 144 141 145 % The idea of using ghostscript is inspired by Peder Axensten's SAVEFIG … … 168 172 % 12/12/12: Add support for isolating uipanels. Thanks to michael for 169 173 % suggesting it. 174 % 25/09/13: Add support for changing resolution in vector formats. Thanks 175 % to Jan Jaap Meijer for suggesting it. 176 % 07/05/14: Add support for '~' at start of path. Thanks to Sally Warner 177 % for suggesting it. 170 178 171 179 function [im, alpha] = export_fig(varargin) … … 217 225 end 218 226 % Set all axes limit and tick modes to manual, so the limits and ticks can't change 219 set(Hlims, 'XLimMode', 'manual', 'YLimMode', 'manual', 'ZLimMode', 'manual', 'XTickMode', 'manual', 'YTickMode', 'manual', 'ZTickMode', 'manual'); 227 set(Hlims, 'XLimMode', 'manual', 'YLimMode', 'manual', 'ZLimMode', 'manual'); 228 set_tick_mode(Hlims, 'X'); 229 set_tick_mode(Hlims, 'Y'); 230 set_tick_mode(Hlims, 'Z'); 220 231 % Set to print exactly what is there 221 232 set(fig, 'InvertHardcopy', 'off'); … … 289 300 % Crop the background 290 301 if options.crop 291 [alpha, v] = crop_b ackground(alpha, 0);302 [alpha, v] = crop_borders(alpha, 0, 1); 292 303 A = A(v(1):v(2),v(3):v(4),:); 293 304 end … … 336 347 % Crop the background 337 348 if options.crop 338 A = crop_b ackground(A, tcol);349 A = crop_borders(A, tcol, 1); 339 350 end 340 351 % Downscale the image … … 406 417 end 407 418 % Generate the options for print 408 p2eArgs = {renderer };419 p2eArgs = {renderer, sprintf('-r%d', options.resolution)}; 409 420 if options.colourspace == 1 410 421 p2eArgs = [p2eArgs {'-cmyk'}]; … … 415 426 try 416 427 % Generate an eps 417 print2eps(tmp_nam, fig, p2eArgs{:});428 print2eps(tmp_nam, fig, options.bb_padding, p2eArgs{:}); 418 429 % Remove the background, if desired 419 430 if options.transparent && ~isequal(get(fig, 'Color'), 'none') 420 eps_remove_background(tmp_nam );431 eps_remove_background(tmp_nam, 1 + using_hg2(fig)); 421 432 end 422 433 % Add a bookmark to the PDF if desired … … 465 476 end 466 477 end 467 return 478 end 468 479 469 480 function [fig, options] = parse_args(nout, varargin) … … 485 496 'im', nout == 1, ... 486 497 'alpha', nout == 2, ... 487 'aa_factor', 3, ... 488 'magnify', 1, ... 498 'aa_factor', 0, ... 499 'bb_padding', 0, ... 500 'magnify', [], ... 501 'resolution', [], ... 489 502 'bookmark', false, ... 490 503 'quality', []); … … 535 548 native = true; 536 549 otherwise 537 val = str2double(regexp(varargin{a}, '(?<=-(m|M|r|R|q|Q ))(\d*\.)?\d+(e-?\d+)?', 'match'));550 val = str2double(regexp(varargin{a}, '(?<=-(m|M|r|R|q|Q|p|P))-?\d*.?\d+', 'match')); 538 551 if ~isscalar(val) 539 552 error('option %s not recognised', varargin{a}); … … 543 556 options.magnify = val; 544 557 case 'r' 545 options. magnify = val ./ get(0, 'ScreenPixelsPerInch');558 options.resolution = val; 546 559 case 'q' 547 560 options.quality = max(val, 0); 561 case 'p' 562 options.bb_padding = val; 548 563 end 549 564 end … … 573 588 end 574 589 590 % Set default anti-aliasing now we know the renderer 591 if options.aa_factor == 0 592 options.aa_factor = 1 + 2 * (~(using_hg2(fig) && strcmp(get(ancestor(fig, 'figure'), 'GraphicsSmoothing'), 'on')) | (options.renderer == 3)); 593 end 594 595 % Convert user dir '~' to full path 596 if numel(options.name) > 2 && options.name(1) == '~' && (options.name(2) == '/' || options.name(2) == '\') 597 options.name = fullfile(char(java.lang.System.getProperty('user.home')), options.name(2:end)); 598 end 599 600 % Compute the magnification and resolution 601 if isempty(options.magnify) 602 if isempty(options.resolution) 603 options.magnify = 1; 604 options.resolution = 864; 605 else 606 options.magnify = options.resolution ./ get(0, 'ScreenPixelsPerInch'); 607 end 608 elseif isempty(options.resolution) 609 options.resolution = 864; 610 end 611 575 612 % Check we have a figure handle 576 613 if isempty(fig) … … 584 621 585 622 % Check whether transparent background is wanted (old way) 586 if isequal(get(ancestor(fig , 'figure'), 'Color'), 'none')623 if isequal(get(ancestor(fig(1), 'figure'), 'Color'), 'none') 587 624 options.transparent = true; 588 625 end … … 632 669 end 633 670 end 634 return 671 end 635 672 636 673 function A = downsize(A, factor) … … 659 696 A = A(1+floor(mod(end-1, factor)/2):factor:end,1+floor(mod(end-1, factor)/2):factor:end,:); 660 697 end 661 return 698 end 662 699 663 700 function A = rgb2grey(A) 664 701 A = cast(reshape(reshape(single(A), [], 3) * single([0.299; 0.587; 0.114]), size(A, 1), size(A, 2)), class(A)); 665 return 702 end 666 703 667 704 function A = check_greyscale(A) … … 672 709 A = A(:,:,1); % Save only one channel for 8-bit output 673 710 end 674 return 675 676 function [A, v] = crop_background(A, bcol) 677 % Map the foreground pixels 678 [h, w, c] = size(A); 679 if isscalar(bcol) && c > 1 680 bcol = bcol(ones(1, c)); 681 end 682 bail = false; 683 for l = 1:w 684 for a = 1:c 685 if ~all(A(:,l,a) == bcol(a)) 686 bail = true; 687 break; 688 end 689 end 690 if bail 691 break; 692 end 693 end 694 bail = false; 695 for r = w:-1:l 696 for a = 1:c 697 if ~all(A(:,r,a) == bcol(a)) 698 bail = true; 699 break; 700 end 701 end 702 if bail 703 break; 704 end 705 end 706 bail = false; 707 for t = 1:h 708 for a = 1:c 709 if ~all(A(t,:,a) == bcol(a)) 710 bail = true; 711 break; 712 end 713 end 714 if bail 715 break; 716 end 717 end 718 bail = false; 719 for b = h:-1:t 720 for a = 1:c 721 if ~all(A(b,:,a) == bcol(a)) 722 bail = true; 723 break; 724 end 725 end 726 if bail 727 break; 728 end 729 end 730 % Crop the background, leaving one boundary pixel to avoid bleeding on 731 % resize 732 v = [max(t-1, 1) min(b+1, h) max(l-1, 1) min(r+1, w)]; 733 A = A(v(1):v(2),v(3):v(4),:); 734 return 735 736 function eps_remove_background(fname) 711 end 712 713 function eps_remove_background(fname, count) 737 714 % Remove the background of an eps file 738 715 % Open the file … … 742 719 end 743 720 % Read the file line by line 744 while true721 while count 745 722 % Get the next line 746 723 l = fgets(fh); … … 749 726 end 750 727 % Check if the line contains the background rectangle 751 if isequal(regexp(l, ' *0 +0 +\d+ +\d+ +r f*[\n\r]+', 'start'), 1)728 if isequal(regexp(l, ' *0 +0 +\d+ +\d+ +r[fe] *[\n\r]+', 'start'), 1) 752 729 % Set the line to whitespace and quit 753 730 l(1:regexp(l, '[\n\r]', 'start', 'once')-1) = ' '; 754 731 fseek(fh, -numel(l), 0); 755 732 fprintf(fh, l); 756 break; 733 % Reduce the count 734 count = count - 1; 757 735 end 758 736 end 759 737 % Close the file 760 738 fclose(fh); 761 return 739 end 762 740 763 741 function b = isvector(options) 764 742 b = options.pdf || options.eps; 765 return 743 end 766 744 767 745 function b = isbitmap(options) 768 746 b = options.png || options.tif || options.jpg || options.bmp || options.im || options.alpha; 769 return 747 end 770 748 771 749 % Helper function … … 774 752 A = {A}; 775 753 end 776 return 754 end 777 755 778 756 function add_bookmark(fname, bookmark_text) … … 808 786 end 809 787 fclose(fh); 810 return 788 end 789 790 function set_tick_mode(Hlims, ax) 791 % Set the tick mode of linear axes to manual 792 % Leave log axes alone as these are tricky 793 M = get(Hlims, [ax 'Scale']); 794 if ~iscell(M) 795 M = {M}; 796 end 797 M = cellfun(@(c) strcmp(c, 'linear'), M); 798 set(Hlims(M), [ax 'TickMode'], 'manual'); 799 end
Note:
See TracChangeset
for help on using the changeset viewer.