diff options
Diffstat (limited to 'SD-VBS/benchmarks/texture_synthesis/src/matlab/pgmWrite.m')
| -rwxr-xr-x | SD-VBS/benchmarks/texture_synthesis/src/matlab/pgmWrite.m | 120 |
1 files changed, 0 insertions, 120 deletions
diff --git a/SD-VBS/benchmarks/texture_synthesis/src/matlab/pgmWrite.m b/SD-VBS/benchmarks/texture_synthesis/src/matlab/pgmWrite.m deleted file mode 100755 index 09c14c9..0000000 --- a/SD-VBS/benchmarks/texture_synthesis/src/matlab/pgmWrite.m +++ /dev/null | |||
| @@ -1,120 +0,0 @@ | |||
| 1 | % RANGE = pgmWrite(MTX, FILENAME, RANGE, TYPE, COMMENT) | ||
| 2 | % | ||
| 3 | % Write a MatLab matrix to a pgm (graylevel image) file. | ||
| 4 | % This format is accessible from the XV image browsing utility. | ||
| 5 | % | ||
| 6 | % RANGE (optional) is a 2-vector specifying the values that map to | ||
| 7 | % black and white, respectively. Passing a value of 'auto' (default) | ||
| 8 | % sets RANGE=[min,max] (as in MatLab's imagesc). 'auto2' sets | ||
| 9 | % RANGE=[mean-2*stdev, mean+2*stdev]. 'auto3' sets | ||
| 10 | % RANGE=[p1-(p2-p1)/8, p2+(p2-p1)/8], where p1 is the 10th percentile | ||
| 11 | % value of the sorted MATRIX samples, and p2 is the 90th percentile | ||
| 12 | % value. | ||
| 13 | % | ||
| 14 | % TYPE (optional) should be 'raw' or 'ascii'. Defaults to 'raw'. | ||
| 15 | |||
| 16 | % Hany Farid, Spring '96. Modified by Eero Simoncelli, 6/96. | ||
| 17 | |||
| 18 | function range = pgmWrite(mtx, fname, range, type, comment ); | ||
| 19 | |||
| 20 | [fid,msg] = fopen( fname, 'w' ); | ||
| 21 | |||
| 22 | if (fid == -1) | ||
| 23 | error(msg); | ||
| 24 | end | ||
| 25 | |||
| 26 | %------------------------------------------------------------ | ||
| 27 | %% optional ARGS: | ||
| 28 | |||
| 29 | if (exist('range') ~= 1) | ||
| 30 | range = 'auto'; | ||
| 31 | end | ||
| 32 | |||
| 33 | if (exist('type') ~= 1) | ||
| 34 | type = 'raw'; | ||
| 35 | end | ||
| 36 | %------------------------------------------------------------ | ||
| 37 | |||
| 38 | %% Automatic range calculation: | ||
| 39 | if (strcmp(range,'auto1') | strcmp(range,'auto')) | ||
| 40 | [mn,mx] = range2(mtx); | ||
| 41 | range = [mn,mx]; | ||
| 42 | |||
| 43 | elseif strcmp(range,'auto2') | ||
| 44 | stdev = sqrt(var2(mtx)); | ||
| 45 | av = mean2(mtx); | ||
| 46 | range = [av-2*stdev,av+2*stdev]; % MAGIC NUMBER: 2 stdevs | ||
| 47 | |||
| 48 | elseif strcmp(range, 'auto3') | ||
| 49 | percentile = 0.1; % MAGIC NUMBER: 0<p<0.5 | ||
| 50 | [N,X] = histo(mtx); | ||
| 51 | binsz = X(2)-X(1); | ||
| 52 | N = N+1e-10; % Ensure cumsum will be monotonic for call to interp1 | ||
| 53 | cumN = [0, cumsum(N)]/sum(N); | ||
| 54 | cumX = [X(1)-binsz, X] + (binsz/2); | ||
| 55 | ctrRange = interp1(cumN,cumX, [percentile, 1-percentile]); | ||
| 56 | range = mean(ctrRange) + (ctrRange-mean(ctrRange))/(1-2*percentile); | ||
| 57 | |||
| 58 | elseif isstr(range) | ||
| 59 | error(sprintf('Bad RANGE argument: %s',range)) | ||
| 60 | |||
| 61 | end | ||
| 62 | |||
| 63 | if ((range(2) - range(1)) <= eps) | ||
| 64 | range(1) = range(1) - 0.5; | ||
| 65 | range(2) = range(2) + 0.5; | ||
| 66 | end | ||
| 67 | |||
| 68 | |||
| 69 | %%% First line contains ID string: | ||
| 70 | %%% "P1" = ascii bitmap, "P2" = ascii greymap, | ||
| 71 | %%% "P3" = ascii pixmap, "P4" = raw bitmap, | ||
| 72 | %%% "P5" = raw greymap, "P6" = raw pixmap | ||
| 73 | if strcmp(type,'raw') | ||
| 74 | fprintf(fid,'P5\n'); | ||
| 75 | format = 5; | ||
| 76 | elseif strcmp(type,'ascii') | ||
| 77 | fprintf(fid,'P2\n'); | ||
| 78 | format = 2; | ||
| 79 | else | ||
| 80 | error(sprintf('PGMWRITE: Bad type argument: %s',type)); | ||
| 81 | end | ||
| 82 | |||
| 83 | fprintf(fid,'# MatLab PGMWRITE file, saved %s\n',date); | ||
| 84 | |||
| 85 | if (exist('comment') == 1) | ||
| 86 | fprintf(fid,'# %s\n', comment); | ||
| 87 | end | ||
| 88 | |||
| 89 | %%% dimensions | ||
| 90 | fprintf(fid,'%d %d\n',size(mtx,2),size(mtx,1)); | ||
| 91 | |||
| 92 | %%% Maximum pixel value | ||
| 93 | fprintf(fid,'255\n'); | ||
| 94 | |||
| 95 | |||
| 96 | %% MatLab's "fprintf" floors when writing floats, so we compute | ||
| 97 | %% (mtx-r1)*255/(r2-r1)+0.5 | ||
| 98 | mult = (255 / (range(2)-range(1))); | ||
| 99 | mtx = (mult * mtx) + (0.5 - mult * range(1)); | ||
| 100 | |||
| 101 | mtx = max(-0.5+eps,min(255.5-eps,mtx)); | ||
| 102 | |||
| 103 | if (format == 2) | ||
| 104 | count = fprintf(fid,'%d ',mtx'); | ||
| 105 | elseif (format == 5) | ||
| 106 | count = fwrite(fid,mtx','uchar'); | ||
| 107 | end | ||
| 108 | |||
| 109 | fclose(fid); | ||
| 110 | |||
| 111 | if (count ~= size(mtx,1)*size(mtx,2)) | ||
| 112 | fprintf(1,'Warning: File output terminated early!'); | ||
| 113 | end | ||
| 114 | |||
| 115 | %%% TEST: | ||
| 116 | % foo = 257*rand(100)-1; | ||
| 117 | % pgmWrite(foo,'foo.pgm',[0 255]); | ||
| 118 | % foo2=pgmRead('foo.pgm'); | ||
| 119 | % size(find((foo2-round(foo))~=0)) | ||
| 120 | % foo(find((foo2-round(foo))~=0)) | ||
