% runs swarp on 3 channel ppm image,
%
% calls version of swarp compiled for HPs
% 
% input is 3 matlab variables + parameters; output is 3 matlab variables

function [Rs, Gs, Bs] = f(R,G,B,x,y,a,b,c)

if nargin<8
  error('swarp_ppm: error: needs 8 input arguments')
end%if

[Mr,Nr]=size(R); [Mg,Ng]=size(G); [Mb,Nb]=size(B); M=Mr; N=Nr; MN=M*N;
if ~((Mr==Mg)&(Mr==Mb)&(Nr==Ng)&(Nr==Nb))
  error('savepnm: error: the 3 input images are different sizes')
end%if

% might already exist, and mkdir fails but then need to delete descriptor
!mkdir porkdatfiler
!rm porkdatfiler/descriptor
!mkdir porkdatfileg
!rm porkdatfileg/descriptor
!mkdir porkdatfileb
!rm porkdatfileb/descriptor

rint=fopen('porkdatfiler/descriptor','w');
gint=fopen('porkdatfileg/descriptor','w');
bint=fopen('porkdatfileb/descriptor','w');
if ((rint<0)|(gint<0)|(bint<0))
  error('savepnm: fopen returned a negative integer meaning file write failed')
end%if

fprintf(rint,'(_data_files 1)\n');
fprintf(rint,'(_data_sets 1)\n');
fprintf(rint,'(_channels 3)\n');
fprintf(rint,'(_dimensions %d %d)\n',M,N);
fprintf(rint,'(_data_type unsigned_1)\n');
fprintf(rint,'(_comment temporary datfile created by matlab)\n');
fprintf(gint,'(_data_files 1)\n');
fprintf(gint,'(_data_sets 1)\n');
fprintf(gint,'(_channels 3)\n');
fprintf(gint,'(_dimensions %d %d)\n',M,N);
fprintf(gint,'(_data_type unsigned_1)\n');
fprintf(gint,'(_comment temporary datfile created by matlab)\n');
fprintf(bint,'(_data_files 1)\n');
fprintf(bint,'(_data_sets 1)\n');
fprintf(bint,'(_channels 3)\n');
fprintf(bint,'(_dimensions %d %d)\n',M,N);
fprintf(bint,'(_data_type unsigned_1)\n');
fprintf(bint,'(_comment temporary datfile created by matlab)\n');

saveraw('porkdatfiler/data',R)  % when number of frames = 1, use `data'
saveraw('porkdatfileg/data',G)  % when number of frames > 1, use `data0', etc.
saveraw('porkdatfileb/data',B)

eval(sprintf('!swarp_hp -w -x %d  -y %d -a %d -b %d -c %d -o porkdatfileswarpr porkdatfiler',x,y,a,b,c))
eval(sprintf('!swarp_hp -w -x %d  -y %d -a %d -b %d -c %d -o porkdatfileswarpg porkdatfileg',x,y,a,b,c))
eval(sprintf('!swarp_hp -w -x %d  -y %d -a %d -b %d -c %d -o porkdatfileswarpb porkdatfileb',x,y,a,b,c))

Rs=eval(sprintf('loadraw(''porkdatfileswarpr/data'',%d,%d)',M,N));
Gs=eval(sprintf('loadraw(''porkdatfileswarpg/data'',%d,%d)',M,N));
Bs=eval(sprintf('loadraw(''porkdatfileswarpb/data'',%d,%d)',M,N));

