print_convert

PURPOSE ^

PRINT_CONVERT: print figures with anti-aliasing and trim them

SYNOPSIS ^

function print_convert(filename, varargin)

DESCRIPTION ^

PRINT_CONVERT: print figures with anti-aliasing and trim them
  PRINT_CONVERT(FILENAME,OPT) prints current figure to FILENAME, which 
  must include extenstion.

  OPT is either a string specifying dpi in the format '-r150' or a struct
  with (some of) these fields:
   opt.resolution   = 150;              % 150 dpi (default: 125)
   opt.pagesize     = [width, height];  % whatever matlab's current units
   opt.jpeg_quality = 85;               % jpeg quality (default: 85)
   opt.imwrite_opts = {'BitDepth',8};   % options to IMWRITE (default: '')
   opt.horz_cut     = 50;               % remove horizontal gaps larger
                                        % than 50 pixels (default: 0)
   opt.horz_space   = 10;               % replace the removed horizontal
                                        % gaps with 10 px of background
   opt.vert_cut     = 50;               % remove vertical gaps larger
                                        % than 50 pixels (default: 0)
   opt.vert_space   = 10;               % replace the removed vertical
                                        % gaps with 10 px of background
   opt.supersampling_factor = 2;        % anti-aliasing (default: 1 for
                                        % images, 2 for graphs). Higher is
                                        % smoother.
   opt.crop_slack   = [top,bot,left,right] %don't crop right to boundary
   opt.figno        = number            % print figure, otherwise gca

  Note that opt.imwrite_opts takes precedence over opt.jpeq_quality.

  print_convert has pre-version 3.7 options, which are deprecated

  Examples
   print_convert('outname.png')         % uses default options
   print_convert outname.png
   print_convert outname.png -r150       % set dpi=150
   print_convert('outname.png',opts);   % use options

 See also IMWRITE

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function print_convert(filename, varargin)
0002 %PRINT_CONVERT: print figures with anti-aliasing and trim them
0003 %  PRINT_CONVERT(FILENAME,OPT) prints current figure to FILENAME, which
0004 %  must include extenstion.
0005 %
0006 %  OPT is either a string specifying dpi in the format '-r150' or a struct
0007 %  with (some of) these fields:
0008 %   opt.resolution   = 150;              % 150 dpi (default: 125)
0009 %   opt.pagesize     = [width, height];  % whatever matlab's current units
0010 %   opt.jpeg_quality = 85;               % jpeg quality (default: 85)
0011 %   opt.imwrite_opts = {'BitDepth',8};   % options to IMWRITE (default: '')
0012 %   opt.horz_cut     = 50;               % remove horizontal gaps larger
0013 %                                        % than 50 pixels (default: 0)
0014 %   opt.horz_space   = 10;               % replace the removed horizontal
0015 %                                        % gaps with 10 px of background
0016 %   opt.vert_cut     = 50;               % remove vertical gaps larger
0017 %                                        % than 50 pixels (default: 0)
0018 %   opt.vert_space   = 10;               % replace the removed vertical
0019 %                                        % gaps with 10 px of background
0020 %   opt.supersampling_factor = 2;        % anti-aliasing (default: 1 for
0021 %                                        % images, 2 for graphs). Higher is
0022 %                                        % smoother.
0023 %   opt.crop_slack   = [top,bot,left,right] %don't crop right to boundary
0024 %   opt.figno        = number            % print figure, otherwise gca
0025 %
0026 %  Note that opt.imwrite_opts takes precedence over opt.jpeq_quality.
0027 %
0028 %  print_convert has pre-version 3.7 options, which are deprecated
0029 %
0030 %  Examples
0031 %   print_convert('outname.png')         % uses default options
0032 %   print_convert outname.png
0033 %   print_convert outname.png -r150       % set dpi=150
0034 %   print_convert('outname.png',opts);   % use options
0035 %
0036 % See also IMWRITE
0037  
0038 % (C) Andy Adler and Bartlomiej Grychtol 2010-2013. License: GPL v2 or v3.
0039 % $Id: print_convert.m 5924 2019-03-20 07:56:29Z aadler $
0040 %
0041 %  Compatibility with pre-3.7 features:
0042 %  ------------------------------------
0043 %  PRINT_CONVERT(FILENAME, OPTIONS, PAGEHWR)
0044 %
0045 %  FILENAME : filename to print to file type is the extension
0046 %  OPTIONS  : specify dpi as '-density N'
0047 %  PAGEHWR  : set page hight/width to control shape of figures
0048 %
0049 %   print_convert('outname.png','-density 150') % resolution to 150 dpi
0050 %   print_convert('outname.png','-density 150', 0.5)
0051  
0052 if ischar(filename) && strcmp(filename,'UNIT_TEST'); do_unit_test; return; end
0053 
0054 pp = parse_options(filename, varargin{:});
0055 
0056 tmpnam = [tempname,'.png'];
0057 
0058 old_ihc = get(pp.figno, 'InvertHardcopy');
0059 old_col = get(pp.figno, 'Color');
0060 set(pp.figno,'InvertHardCopy','off'); 
0061 set(pp.figno,'Color','w');
0062 
0063 set(pp.figno,'PaperPosition',pp.posn); % I wish matlab had unwind protect - like octave does!
0064 %%% The -dpng driver is broken in R2014a on linux (and maybe others);
0065 print(pp.figno,'-dpng',pp.resolution,tmpnam);
0066 set(pp.figno,'PaperPosition',pp.page);
0067  
0068 set(pp.figno,'InvertHardCopy',old_ihc); %
0069 set(pp.figno,'Color',old_col);
0070  
0071 im = imread(tmpnam,'png');
0072 delete(tmpnam);
0073 
0074 im = bitmap_downsize(im, pp.factor);
0075 im = crop_image(im,pp);
0076 try
0077    imwrite(im,filename,pp.imwrite_opts{:});
0078 catch e
0079    eidors_msg(['Call to IMWRITE failed.'...
0080                'Probably opt.imwrite_opts is incorrect for %s files.'],...
0081                upper(pp.fmt), 1);
0082    disp('opt.imwrite_opts:');
0083    disp(pp.imwrite_opts);
0084    rethrow(e);
0085 end
0086 
0087 function im = crop_image(im,pp)
0088    tu = pp.crop_slack(1);
0089    bu = pp.crop_slack(2) + 1; %remove starting one more
0090    lu = pp.crop_slack(3);
0091    ru = pp.crop_slack(4) + 1;
0092 
0093    szim = size(im);
0094    bdr = squeeze(mean(double(im(1,:,:)),2));
0095  
0096    isbdr = true(szim(1),szim(2));
0097    for i=1:szim(3);
0098      isbdr = isbdr & (im(:,:,i) == bdr(i));
0099    end
0100  
0101    horz = [true,all(isbdr,1),true];
0102    horzpt = find(diff(horz)) - 1; % first 'true'
0103    if isempty(horzpt)
0104       eidors_msg('Image is blank. Cropping aborted.',1);
0105       return
0106    end
0107    im(:,horzpt(end)+ru:end,:)= []; % remove higher first
0108    if pp.horz_cut >0;
0109       horz_e_pt = find(diff(horz)==-1) -1; horz_e_pt(1) = [];
0110       horz_s_pt = find(diff(horz)==+1)   ; horz_s_pt(end) = [];
0111       idx = find(horz_e_pt - horz_s_pt > pp.horz_cut);
0112       for i=fliplr(idx) % remove higher first
0113         im(:,horz_s_pt(i)+pp.horz_space:horz_e_pt(i),:)= [];
0114       end
0115    end
0116    im(:,1:horzpt(1)-lu ,:)= [];
0117  
0118    vert = [true,all(isbdr,2)',true];
0119    vertpt = find(diff(vert)) - 1; % first 'true'
0120    im(vertpt(end)+bu:end,:,:)= [];
0121    if pp.vert_cut >0;
0122       vert_e_pt = find(diff(vert)==-1) -1; vert_e_pt(1) = [];
0123       vert_s_pt = find(diff(vert)==+1)   ; vert_s_pt(end) = [];
0124       idx = find(vert_e_pt - vert_s_pt > pp.vert_cut);
0125       for i=fliplr(idx) % remove higher first
0126         im(vert_s_pt(i)+pp.vert_space:vert_e_pt(i),:,:)= [];
0127       end
0128    end
0129    im(1:vertpt(1)-tu ,:,:)= [];
0130  
0131     
0132 % factor = 1 if all plots only contain images, 2 otherwise
0133 function f = default_factor
0134    f = 1; % default for images
0135    sp = get(gcf,'Children'); % subplots
0136    for i = 1:length(sp)
0137       obj = get(sp(i),'Children');
0138       tp  = get(obj,'Type');
0139       if ~all(strcmp(tp,'image'))
0140          f = 2;
0141          return;
0142       end
0143    end 
0144    
0145 function fmt = parse_format(filename)   
0146     ext = lower(regexp(filename,'(?<=\.).+$','match'));
0147     if isempty(ext); error('no filename extension detected (%s)',filename); end
0148     switch ext{1}
0149        case {'jpg', 'jpeg'}
0150           fmt = 'jpg';
0151        case {'j2c', 'j2k', 'jp2'}
0152           fmt = 'jp2';
0153        case {'tif','tiff'}
0154           fmt = 'tif';
0155        otherwise
0156           fmt = ext{1};
0157     end
0158        
0159 function pp = parse_options(filename,varargin)
0160    
0161    pp.fmt = parse_format(filename);
0162 
0163    pp.page = get(gcf,'PaperPosition');
0164    pp.posn = pp.page;
0165    pp.jpeg_quality = 85; % default jpeg quality
0166    pp.imwrite_opts = {}; % initial
0167    pp.horz_cut = 50;
0168    pp.horz_space = 10;
0169    pp.vert_cut = 50;
0170    pp.vert_space = 10;
0171    pp.factor = default_factor;
0172    pp.resolution = sprintf('-r%d',125 * pp.factor);
0173    pp.crop_slack = [0,0,0,0];
0174    if exist('OCTAVE_VERSION')
0175       pp.figno = gcf;
0176    else
0177       pp.figno = get(gcf,'Number');
0178    end
0179    
0180    
0181  
0182 % Old options
0183    if nargin< 2;   
0184       return; 
0185    end
0186    if nargin>=3
0187       pp.posn(4) = pp.posn(3)*varargin{2};  
0188    end
0189  
0190    opt = varargin{1};
0191    if ischar(opt)
0192       val =regexp(opt,'-density (\d+)','tokens');
0193       if ~isempty(val);
0194          pp.resolution = sprintf('-r%d', str2double(val{1}{1}) * pp.factor);
0195       end
0196       val =regexp(opt,'-r(\d+)','tokens');
0197       if ~isempty(val);
0198          pp.resolution = sprintf('-r%d', str2double(val{1}{1}) * pp.factor);
0199       end
0200    elseif isstruct(opt)
0201      if isfield(opt,'figno');
0202         pp.figno = opt.figno;
0203         pp.page = get(pp.figno,'PaperPosition');
0204         pp.posn = pp.page;
0205      end
0206      if isfield(opt,'supersampling_factor')
0207         pp.factor = opt.supersampling_factor;
0208      end
0209      if isfield(opt,'resolution');
0210          pp.resolution = sprintf('-r%d', opt.resolution * pp.factor);
0211      else
0212          pp.resolution = sprintf('-r%d',125 * pp.factor);
0213      end
0214      if isfield(opt,'pagesize');
0215          pp.posn(3:4) = opt.pagesize;
0216      end
0217      % TODO, this code can copy from opt to pp
0218      if isfield(opt,'jpeg_quality')
0219         pp.jpeg_quality = opt.jpeg_quality;
0220      end
0221      if strcmp(pp.fmt,'jpg')
0222         pp.imwrite_opts = {'quality',pp.jpeg_quality}; 
0223      end
0224      if isfield(opt,'imwrite_opts');
0225         pp.imwrite_opts = opt.imwrite_opts;
0226         if strcmp(pp.fmt,'jpg') && ~any(strcmpi(pp.imwrite_opts,'quality'))
0227            pp.imwrite_opts(end+1:end+2) = {'quality',pp.jpeg_quality};
0228         end
0229      end
0230     
0231      if isfield(opt,'horz_cut');
0232          pp.horz_cut = opt.horz_cut;
0233      end
0234      if isfield(opt,'vert_cut');
0235          pp.vert_cut = opt.vert_cut;
0236      end
0237      if isfield(opt,'vert_space');
0238         if opt.vert_space >= pp.vert_cut;
0239            warrning('Option vert_space must be smaller than vert_cut. Ingoring');
0240         else
0241            pp.vert_space = opt.vert_space;
0242         end
0243      end
0244      if isfield(opt,'horz_space');
0245         if opt.horz_space >= pp.horz_cut;
0246            warrning('Option vert_space must be smaller than vert_cut. Ingoring');
0247         else
0248            pp.horz_space = opt.horz_space;
0249         end
0250      end
0251      if isfield(opt,'crop_slack');
0252         pp.crop_slack = opt.crop_slack;
0253      end
0254    else
0255       error('Can''t parse options');
0256    end
0257  
0258  
0259 function do_unit_test
0260    fprintf('does unit test \n');
0261    fid = fopen('print_convert_test.html','w');
0262    fprintf(fid,'<HTML><BODY>\n');
0263    for i=1:26;
0264      switch i;
0265        case {9,10}; typ = 'jpg';
0266        otherwise; typ = 'png';
0267      end
0268      fprintf(fid,...
0269      '<H1>%02d</H1><table border=1><tr><td><img src="pc%02d.%s"></table>\n',i,i,typ);
0270    end
0271    fprintf(fid,'</BODY></HTML>\n');
0272    fclose(fid);
0273    eidors_msg('TO VIEW OUTPUT, OPEN FILE print_convert_test.html',1);
0274  
0275    im = mk_image( ng_mk_cyl_models(1,[16,.5],.05),1);
0276    clf; show_fem(im);
0277    print_convert pc01.png
0278  
0279    im = mk_image( mk_common_model('b2c2',8), 1:256);
0280    clf; show_fem(im);
0281    print_convert pc02.png
0282  
0283  
0284    clf; subplot(221); show_fem(im); 
0285         subplot(224); show_slices(im); 
0286    print_convert pc03.png
0287  
0288    print_convert pc04.png '-density 50'
0289    print_convert pc05.png '-r100'
0290  
0291    print_convert('pc06.png', '-r100' , 1/8)
0292  
0293    print_convert('pc06.png', '-r100' , 3/1)
0294  
0295    opt.resolution = 100;
0296    print_convert('pc07.png', opt);
0297    opt.pagesize = [8,2];
0298    print_convert('pc08.png', opt);
0299  
0300    print_convert('pc09a.jpg');
0301    
0302    opt.pagesize = [8,6];
0303    print_convert('pc09.jpg', opt);
0304   
0305    
0306    opt.imwrite_opts = {'Quality',20};
0307    print_convert('pc10.jpg', opt);
0308    
0309    opt.imwrite_opts = {'Quality',100};
0310    print_convert('pc10b.jpg', opt);
0311  
0312    opt.imwrite_opts = {};
0313    opt.horz_cut = 50;
0314    print_convert('pc11.png', opt);
0315  
0316    opt.vert_cut = 50;
0317    print_convert('pc12.png', opt);
0318  
0319    clf; subplot(331); show_fem(im); 
0320         subplot(335); show_slices(im); 
0321         subplot(339); show_slices(im); 
0322    opt.vert_cut = 30;
0323    print_convert('pc13.png', opt);
0324    
0325    im = mk_image( ng_mk_cyl_models(1,[16,.5],.05),1);
0326    clf; show_fem(im);
0327    clear opt; opt.supersampling_factor = 1;
0328    print_convert('pc14.png', opt);
0329  
0330    clear opt; opt.supersampling_factor = 2;
0331    print_convert('pc15.png', opt);
0332  
0333    clear opt; opt.supersampling_factor = 3;
0334    print_convert('pc16.png', opt);
0335    
0336    clear opt; opt.supersampling_factor = 4;
0337    print_convert('pc17.png', opt);
0338    
0339    clear opt; opt.supersampling_factor = 8;
0340    print_convert('pc18.png', opt);
0341  
0342

Generated on Tue 31-Dec-2019 17:03:26 by m2html © 2005