function setimage(newimg, pos, clip) %SETIMAGE Set and display the global variable I. % % SETIMAGE(NEWIMAGE, XYPOS, CLIP) % % SETIMAGE copies the image NEWIMAGE to the global variable I % and redisplays the image. XYPOS specifies the X and Y position % to which the NEWIMAGE will be copied (Default pos=[1 1]). % The NEWIMAGE can be smaller than the current image. In this case, only % part of the image will be changed. If pos is outside the % image border, the NEWIMAGE will be cropped to fit inside the % borders. % If CLIP is 1 (Default), the Clipboard and the Undofun are set; % if it is 0, the Cliboard is not set. % ALWAYS USE THIS FUNCTION TO MODIFY I. % % See also SHOWIMAGE, IMAGE, UNDO. % % Copyright (c) 1995 by Claudio Rivetti and Mark Young % claudio@alice.uoregon.edu, mark@alice.uoregon.edu % global I Undofun isModify if nargin < 1 error('Too few input arguments.'); end if nargin==2 clip=1; end if nargin==1 pos=[1 1]; clip=1; end f=watchon; if clip setclipboard(I); Undofun='setimage(getclipboard,inf,0);setclipboard([]);'; end if pos~=inf pos=round(pos); imgr=size(I,1); imgc=size(I,2); newimgr=size(newimg,1); newimgc=size(newimg,2); if pos(2)>imgr | pos(1)>imgc | (pos(2)+newimgr)<1 | (pos(1)+newimgc)<1 return; end if pos(1)<1 newimg=newimg(:,abs(pos(1))+1:newimgc); pos(1)=1; newimgc=size(newimg,2); end if pos(1)+newimgc > imgc newimg=newimg(:,1:imgc-pos(1)+1); newimgc=size(newimg,2); end if pos(2)+newimgr > imgr newimg=newimg(1:imgr-pos(2)+1, :); newimgr=size(newimg,1); end if pos(2)<1 newimg=newimg(abs(pos(2))+1:newimgr, :); pos(2)=1; newimgr=size(newimg,2); end I(pos(2):pos(2)+newimgr-1, pos(1):pos(1)+newimgc-1)=newimg; else I=newimg; end showimage; isModify=isModify+1; watchoff(f); return