0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045 function jpeg91_encoder(s,B,directorio,fichero)
0046 Ncuan = 4;
0047 opc = 1;
0048 niveles = 0;
0049 fronteras = 0;
0050 facfrec = 1.1;
0051
0052 ncontr = 100;
0053 nolin = 0;
0054 estab = 0;
0055 bitmax = 10;
0056 met = 0;
0057 fchest = 0;
0058 fc = 32;
0059 cc = 1;
0060 crit = 1;
0061 fijarmax = 0;
0062 inclcsf = [1 1];
0063 frecbaja = 0.001;
0064 calcent = 1;
0065
0066 norm = [3 mean(mean(s))];
0067 MM = 0.8;
0068
0069 tam = size(s);
0070 tam = tam(1);
0071 lados = [tam tam];
0072 imq = zeros(tam,tam);
0073 dctq = zeros(tam,tam);
0074 codq = zeros(tam,tam);
0075 lcuan = tam/(2^Ncuan);
0076 posai = [tam tam]/2-round(tam/2);
0077 posbd = [tam tam]/2+round(tam/2);
0078 coorcuanai = floor([(posai(1)-1)/lcuan+1 (posai(2)-1)/lcuan+1]);
0079 coorcuanbd = floor([(posbd(1)-1)/lcuan+1 (posbd(2)-1)/lcuan+1]);
0080 if coorcuanai(1)<1
0081 coorcuanai(1)=1;
0082 end
0083 if coorcuanai(2)<1
0084 coorcuanai(2)=1;
0085 end
0086 if coorcuanbd(1)>(2^Ncuan)
0087 coorcuanbd(1)=2^Ncuan;
0088 end
0089 if coorcuanai(2)>(2^Ncuan)
0090 coorcuanbd(2)=2^Ncuan;
0091 end
0092 H=0;
0093 Hi=0;
0094 if (opc==2)
0095 [iaf,ciaf1D,niveles,fronteras]=apeles(B,bitmax,met,fchest);
0096 if inclcsf(1) == 1
0097 [ciaf1Dp,iafp,nuqfp,nivelesp,fronterasp] = vqperc(lcuan,facfrec,ncontr,nolin,estab,B,bitmax,fc,fijarmax,0);
0098 fronterasp = fronterasp*(lcuan^2)*128;
0099
0100 tamfro = size(fronteras);
0101 for i = 1:tamfro(1)
0102 if niveles(i,1) ~= -1
0103 condic = (fronteras(i,:) <= (inclcsf(2)*fronterasp(i,1)));
0104 if (sum(condic) ~= 0)
0105 v = find(condic<1);
0106 l = length(v);
0107 if l>1
0108 front = [fronteras(i,v) maxi(fronteras(i,:))*ones(1,tamfro(2)-l)];
0109 nivel = [0 niveles(i,v(2:l)) maxi(niveles(i,:))*ones(1,tamfro(2)-l+1)];
0110 fronteras(i,:) = front;
0111 niveles(i,:) = nivel;
0112 else
0113 fronteras(i,:) = maxi(fronteras(i,:))*ones(1,tamfro(2));
0114 niveles(i,:) = [0 maxi(niveles(i,:))*ones(1,tamfro(2))];
0115 end
0116 end
0117 else
0118 fronteras(i,:) = fronteras(i,:);
0119 niveles(i,:) = niveles(i,:);
0120 end
0121 end
0122 end
0123 elseif (opc == 1)
0124 [ciaf1D,iaf,nuqf,niveles,fronteras] = vqperc(lcuan,facfrec,ncontr,nolin,estab,B,bitmax,fc,fijarmax,0);
0125 end
0126
0127 dcti = dct2r(s,Ncuan);
0128
0129 [a,Lm] = contras2(dcti,Ncuan,norm);
0130
0131 pes = ones(lcuan,lcuan);
0132 pes(1,1) = 0;
0133
0134 pes=[pes pes pes pes pes pes pes pes pes pes pes pes pes pes pes pes];
0135 pes=[pes;pes;pes;pes;pes;pes;pes;pes;pes;pes;pes;pes;pes;pes;pes;pes];
0136 a = pes.*a;
0137
0138 W = 1;
0139
0140 val_r = 100;
0141
0142 e = imdpcm(Lm,W,val_r);
0143
0144 Nc = round(0.9 * 40);
0145 me = mini(e);
0146 Me = maxi(e);
0147 extr_err = [me Me];
0148 QE = round((Nc -1) * (e - me) / (Me - me));
0149
0150 Lb = 16;
0151 Li = 256;
0152 codigazo = [];
0153 for i = 1:Lb:Li
0154 for j = 1:Lb:Li
0155 cub = zeros(Lb,Lb);
0156 cub = a(i:(i+(Lb-1)),j:(j+(Lb-1)));
0157 fre = topolar3(fc,Lb);
0158 cond = fre > frecbaja;
0159 tfs = cub;
0160 tfs = tfs .* cond;
0161 tfc = zeros(Lb,Lb);
0162 cod = zeros(Lb,Lb);
0163 for i1 = 1:Lb,
0164 for j1 = 1:Lb,
0165 d = floor(sqrt((i1-1).^2+(j1-1).^2));
0166 [tfc(i1,j1),cod(i1,j1)] = asignbbb(abs(tfs(i1,j1)),niveles(d+1,:),fronteras(d+1,:),crit);
0167
0168 end
0169 end
0170 c1 = (cub >= 0);
0171 c2 = (cub < 0);
0172 tfc = c1.*tfc - c2.*tfc;
0173 anticond = cond < 1;
0174 dctc = cc .* anticond .* cub + cond.*tfc;
0175
0176 dctc = round(2^B * dctc / MM);
0177 bloque = zigzag(dctc);
0178 bloque = bloque(2:end);
0179
0180 [codigo,ceros] = rle2(bloque);
0181 codigazo = [codigazo codigo ceros];
0182 end
0183 end
0184
0185 ristra = [mean(mean(s)) extr_err(:)' round(QE(:))' B round(codigazo)];
0186
0187 cd(directorio);
0188 save_code_jpeg91(ristra,[fichero '.bin']);
0189 zip(fichero,[fichero '.bin']);
0190
0191 delete([fichero '.bin']);