Debian Codegraph @

spectro/sa_conv.c

raw symbols SCIP
1
2 #ifdef SALONEINSTLIB
3
4 /*
5 * A very small subset of icclib, copied to here.
6 * This is just enough to support the standalone instruments
7 */
8
9 /*
10 * Argyll Color Management System
11 *
12 * Author: Graeme W. Gill
13 * Date: 28/9/97
14 *
15 * Copyright 1997 - 2013 Graeme W. Gill
16 * All rights reserved.
17 *
18 * This material is licenced under the GNU GENERAL PUBLIC LICENSE Version 2 or later :-
19 * see the License2.txt file for licencing details.
20 */
21
22 #include "sa_config.h"
23 #include "numsup.h"
24 #include "sa_conv.h"
25
26 #include <stdio.h>
27 #include <stdlib.h>
28
29 sa_XYZNumber sa_D50 = {
30 0.9642, 1.0000, 0.8249
31 };
32
33 sa_XYZNumber sa_D65 = {
34 0.9505, 1.0000, 1.0890
35 };
36
37 sa_XYZNumber sa_D50_100 = {
38 96.42, 100.00, 82.49
39 };
40
41 sa_XYZNumber sa_D65_100 = {
42 95.05, 100.00, 108.90
43 };
44
45 unsigned int sa_CSSig2nchan(icColorSpaceSignature sig) {
46 switch(sig) {
47 case icSigXYZData:
48 return 3;
49 case icSigLabData:
50 return 3;
51 case icSigLuvData:
52 return 3;
53 case icSigYCbCrData:
54 return 3;
55 case icSigYxyData:
56 return 3;
57 case icSigRgbData:
58 return 3;
59 case icSigGrayData:
60 return 1;
61 case icSigHsvData:
62 return 3;
63 case icSigHlsData:
64 return 3;
65 case icSigCmykData:
66 return 4;
67 case icSigCmyData:
68 return 3;
69 case icSig2colorData:
70 return 2;
71 case icSig3colorData:
72 return 3;
73 case icSig4colorData:
74 return 4;
75 case icSig5colorData:
76 case icSigMch5Data:
77 return 5;
78 case icSig6colorData:
79 case icSigMch6Data:
80 return 6;
81 case icSig7colorData:
82 case icSigMch7Data:
83 return 7;
84 case icSig8colorData:
85 case icSigMch8Data:
86 return 8;
87 case icSig9colorData:
88 return 9;
89 case icSig10colorData:
90 return 10;
91 case icSig11colorData:
92 return 11;
93 case icSig12colorData:
94 return 12;
95 case icSig13colorData:
96 return 13;
97 case icSig14colorData:
98 return 14;
99 case icSig15colorData:
100 return 15;
101
102 #ifdef NEVER
103 /* Non-standard and Pseudo spaces */
104 case icmSigYData:
105 return 1;
106 case icmSigLData:
107 return 1;
108 case icmSigL8Data:
109 return 1;
110 case icmSigLV2Data:
111 return 1;
112 case icmSigLV4Data:
113 return 1;
114 case icmSigPCSData:
115 return 3;
116 case icmSigLab8Data:
117 return 3;
118 case icmSigLabV2Data:
119 return 3;
120 case icmSigLabV4Data:
121 return 3;
122 #endif /* NEVER */
123
124 default:
125 break;
126 }
127 return 0;
128 }
129
130 void sa_SetUnity3x3(double mat[3][3]) {
131 int i, j;
132 for (j = 0; j < 3; j++) {
133 for (i = 0; i < 3; i++) {
134 if (i == j)
135 mat[j][i] = 1.0;
136 else
137 mat[j][i] = 0.0;
138 }
139 }
140
141 }
142
143 void sa_Cpy3x3(double dst[3][3], double src[3][3]) {
144 int i, j;
145
146 for (j = 0; j < 3; j++) {
147 for (i = 0; i < 3; i++)
148 dst[j][i] = src[j][i];
149 }
150 }
151
152 void sa_MulBy3x3(double out[3], double mat[3][3], double in[3]) {
153 double tt[3];
154
155 tt[0] = mat[0][0] * in[0] + mat[0][1] * in[1] + mat[0][2] * in[2];
156 tt[1] = mat[1][0] * in[0] + mat[1][1] * in[1] + mat[1][2] * in[2];
157 tt[2] = mat[2][0] * in[0] + mat[2][1] * in[1] + mat[2][2] * in[2];
158
159 out[0] = tt[0];
160 out[1] = tt[1];
161 out[2] = tt[2];
162 }
163
164 void sa_Mul3x3_2(double dst[3][3], double src1[3][3], double src2[3][3]) {
165 int i, j, k;
166 double td[3][3]; /* Temporary dest */
167
168 for (j = 0; j < 3; j++) {
169 for (i = 0; i < 3; i++) {
170 double tt = 0.0;
171 for (k = 0; k < 3; k++)
172 tt += src1[j][k] * src2[k][i];
173 td[j][i] = tt;
174 }
175 }
176
177 /* Copy result out */
178 for (j = 0; j < 3; j++)
179 for (i = 0; i < 3; i++)
180 dst[j][i] = td[j][i];
181 }
182
183
184 /* Matrix Inversion by Richard Carling from "Graphics Gems", Academic Press, 1990 */
185 #define det2x2(a, b, c, d) (a * d - b * c)
186
187 static void adjoint(
188 double out[3][3],
189 double in[3][3]
190 ) {
191 double a1, a2, a3, b1, b2, b3, c1, c2, c3;
192
193 /* assign to individual variable names to aid */
194 /* selecting correct values */
195
196 a1 = in[0][0]; b1 = in[0][1]; c1 = in[0][2];
197 a2 = in[1][0]; b2 = in[1][1]; c2 = in[1][2];
198 a3 = in[2][0]; b3 = in[2][1]; c3 = in[2][2];
199
200 /* row column labeling reversed since we transpose rows & columns */
201
202 out[0][0] = det2x2(b2, b3, c2, c3);
203 out[1][0] = - det2x2(a2, a3, c2, c3);
204 out[2][0] = det2x2(a2, a3, b2, b3);
205
206 out[0][1] = - det2x2(b1, b3, c1, c3);
207 out[1][1] = det2x2(a1, a3, c1, c3);
208 out[2][1] = - det2x2(a1, a3, b1, b3);
209
210 out[0][2] = det2x2(b1, b2, c1, c2);
211 out[1][2] = - det2x2(a1, a2, c1, c2);
212 out[2][2] = det2x2(a1, a2, b1, b2);
213 }
214
215 static double sa_Det3x3(double in[3][3]) {
216 double a1, a2, a3, b1, b2, b3, c1, c2, c3;
217 double ans;
218
219 a1 = in[0][0]; b1 = in[0][1]; c1 = in[0][2];
220 a2 = in[1][0]; b2 = in[1][1]; c2 = in[1][2];
221 a3 = in[2][0]; b3 = in[2][1]; c3 = in[2][2];
222
223 ans = a1 * det2x2(b2, b3, c2, c3)
224 - b1 * det2x2(a2, a3, c2, c3)
225 + c1 * det2x2(a2, a3, b2, b3);
226 return ans;
227 }
228
229 #define SA__SMALL_NUMBER 1.e-8
230
231 int sa_Inverse3x3(double out[3][3], double in[3][3]) {
232 int i, j;
233 double det;
234
235 /* calculate the 3x3 determinant
236 * if the determinant is zero,
237 * then the inverse matrix is not unique.
238 */
239 det = sa_Det3x3(in);
240
241 if ( fabs(det) < SA__SMALL_NUMBER)
242 return 1;
243
244 /* calculate the adjoint matrix */
245 adjoint(out, in);
246
247 /* scale the adjoint matrix to get the inverse */
248 for (i = 0; i < 3; i++)
249 for(j = 0; j < 3; j++)
250 out[i][j] /= det;
251 return 0;
252 }
253
254 #undef SA__SMALL_NUMBER
255 #undef det2x2
256
257 /* - - - - - - - - - - - - - - - - - - - - - - - - */
258 /* Transpose a 3x3 matrix */
259 void sa_Transpose3x3(double out[3][3], double in[3][3]) {
260 int i, j;
261 if (out != in) {
262 for (i = 0; i < 3; i++)
263 for (j = 0; j < 3; j++)
264 out[i][j] = in[j][i];
265 } else {
266 double tt[3][3];
267 for (i = 0; i < 3; i++)
268 for (j = 0; j < 3; j++)
269 tt[i][j] = in[j][i];
270 for (i = 0; i < 3; i++)
271 for (j = 0; j < 3; j++)
272 out[i][j] = tt[i][j];
273 }
274 }
275
276 /* Scale a 3 vector by the given ratio */
277 void sa_Scale3(double out[3], double in[3], double rat) {
278 out[0] = in[0] * rat;
279 out[1] = in[1] * rat;
280 out[2] = in[2] * rat;
281 }
282
283 /* Clamp a 3 vector to be +ve */
284 void sa_Clamp3(double out[3], double in[3]) {
285 int i;
286 for (i = 0; i < 3; i++)
287 out[i] = in[i] < 0.0 ? 0.0 : in[i];
288 }
289
290 /* Add two 3 vectors */
291 void sa_Add3(double out[3], double in1[3], double in2[3]) {
292 out[0] = in1[0] + in2[0];
293 out[1] = in1[1] + in2[1];
294 out[2] = in1[2] + in2[2];
295 }
296
297 /* Return the normal Delta E given two Lab values */
298 double sa_LabDE(double *Lab0, double *Lab1) {
299 double rv = 0.0, tt;
300
301 tt = Lab0[0] - Lab1[0];
302 rv += tt * tt;
303 tt = Lab0[1] - Lab1[1];
304 rv += tt * tt;
305 tt = Lab0[2] - Lab1[2];
306 rv += tt * tt;
307
308 return sqrt(rv);
309 }
310
311 /* Return the CIE94 Delta E color difference measure, squared */
312 double sa_CIE94sq(double Lab0[3], double Lab1[3]) {
313 double desq, dhsq;
314 double dlsq, dcsq;
315 double c12;
316
317 {
318 double dl, da, db;
319 dl = Lab0[0] - Lab1[0];
320 dlsq = dl * dl; /* dl squared */
321 da = Lab0[1] - Lab1[1];
322 db = Lab0[2] - Lab1[2];
323
324 /* Compute normal Lab delta E squared */
325 desq = dlsq + da * da + db * db;
326 }
327
328 {
329 double c1, c2, dc;
330
331 /* Compute chromanance for the two colors */
332 c1 = sqrt(Lab0[1] * Lab0[1] + Lab0[2] * Lab0[2]);
333 c2 = sqrt(Lab1[1] * Lab1[1] + Lab1[2] * Lab1[2]);
334 c12 = sqrt(c1 * c2); /* Symetric chromanance */
335
336 /* delta chromanance squared */
337 dc = c1 - c2;
338 dcsq = dc * dc;
339 }
340
341 /* Compute delta hue squared */
342 if ((dhsq = desq - dlsq - dcsq) < 0.0)
343 dhsq = 0.0;
344 {
345 double sc, sh;
346
347 /* Weighting factors for delta chromanance & delta hue */
348 sc = 1.0 + 0.045 * c12;
349 sh = 1.0 + 0.015 * c12;
350 return dlsq + dcsq/(sc * sc) + dhsq/(sh * sh);
351 }
352 }
353
354 /* Return the CIE94 Delta E color difference measure */
355 double sa_CIE94(double Lab0[3], double Lab1[3]) {
356 return sqrt(sa_CIE94sq(Lab0, Lab1));
357 }
358
359 /* Return the CIE94 Delta E color difference measure for two XYZ values */
360 double sa_XYZCIE94(sa_XYZNumber *w, double *in0, double *in1) {
361 double lab0[3], lab1[3];
362
363 sa_XYZ2Lab(w, lab0, in0);
364 sa_XYZ2Lab(w, lab1, in1);
365 return sqrt(sa_CIE94sq(lab0, lab1));
366 }
367
368 /* CIE XYZ to perceptual CIE 1976 L*a*b* */
369 void
370 sa_XYZ2Lab(sa_XYZNumber *w, double *out, double *in) {
371 double X = in[0], Y = in[1], Z = in[2];
372 double x,y,z,fx,fy,fz;
373
374 x = X/w->X;
375 y = Y/w->Y;
376 z = Z/w->Z;
377
378 if (x > 0.008856451586)
379 fx = pow(x,1.0/3.0);
380 else
381 fx = 7.787036979 * x + 16.0/116.0;
382
383 if (y > 0.008856451586)
384 fy = pow(y,1.0/3.0);
385 else
386 fy = 7.787036979 * y + 16.0/116.0;
387
388 if (z > 0.008856451586)
389 fz = pow(z,1.0/3.0);
390 else
391 fz = 7.787036979 * z + 16.0/116.0;
392
393 out[0] = 116.0 * fy - 16.0;
394 out[1] = 500.0 * (fx - fy);
395 out[2] = 200.0 * (fy - fz);
396 }
397
398 void sa_Lab2XYZ(sa_XYZNumber *w, double *out, double *in) {
399 double L = in[0], a = in[1], b = in[2];
400 double x,y,z,fx,fy,fz;
401
402 fy = (L + 16.0)/116.0;
403 fx = a/500.0 + fy;
404 fz = fy - b/200.0;
405
406 if (fy > 24.0/116.0)
407 y = pow(fy,3.0);
408 else
409 y = (fy - 16.0/116.0)/7.787036979;
410
411 if (fx > 24.0/116.0)
412 x = pow(fx,3.0);
413 else
414 x = (fx - 16.0/116.0)/7.787036979;
415
416 if (fz > 24.0/116.0)
417 z = pow(fz,3.0);
418 else
419 z = (fz - 16.0/116.0)/7.787036979;
420
421 out[0] = x * w->X;
422 out[1] = y * w->Y;
423 out[2] = z * w->Z;
424 }
425
426
427 void sa_Yxy2XYZ(double *out, double *in) {
428 double Y = in[0];
429 double x = in[1];
430 double y = in[2];
431 double z = 1.0 - x - y;
432 double sum;
433 if (y < 1e-9) {
434 out[0] = out[1] = out[2] = 0.0;
435 } else {
436 sum = Y/y;
437 out[0] = x * sum;
438 out[1] = Y;
439 out[2] = z * sum;
440 }
441 }
442
443 /* - - - - - - - - - - - - - - - - - - - - - - - - */
444
445 /* 32 bit linear congruent generator */
446 /* generates number between 0 and 4294967295 */
447 /* (From Knuth & H.W.Lewis) */
448 #define PSRAND32L(S) ((S) * 1664525L + 1013904223L)
449
450 /* Return a 32 bit number between 0 and 4294967295 */
451 unsigned int
452 sa_rand32( /* Return 32 bit random number */
453 unsigned int seed /* Optional seed. Non-zero re-initialized with that seed */
454 ) {
455 static unsigned int pval = 12345678;
456
457 if (seed != 0)
458 pval = seed;
459
460 pval = PSRAND32L(pval);
461
462 return pval;
463 }
464
465 /* - - - - - - - - - - - - - - - - - - - - - - - - */
466
467 /* Object for computing RFC 1321 MD5 checksums. */
468 /* Derived from Colin Plumb's 1993 public domain code. */
469
470 /* Reset the checksum */
471 static void sa_MD5_reset(sa_MD5 *p) {
472 p->tlen = 0;
473
474 p->sum[0] = 0x67452301;
475 p->sum[1] = 0xefcdab89;
476 p->sum[2] = 0x98badcfe;
477 p->sum[3] = 0x10325476;
478
479 p->fin = 0;
480 }
481
482 #define F1(x, y, z) (z ^ (x & (y ^ z)))
483 #define F2(x, y, z) F1(z, x, y)
484 #define F3(x, y, z) (x ^ y ^ z)
485 #define F4(x, y, z) (y ^ (x | ~z))
486
487 #define MD5STEP(f, w, x, y, z, pp, xtra, s) \
488 data = (pp)[0] + ((pp)[3] << 24) + ((pp)[2] << 16) + ((pp)[1] << 8); \
489 w += f(x, y, z) + data + xtra; \
490 w = (w << s) | (w >> (32-s)); \
491 w += x;
492
493 /* Add another 64 bytes to the checksum */
494 static void sa_MD5_accume(sa_MD5 *p, ORD8 *in) {
495 ORD32 data, a, b, c, d;
496
497 a = p->sum[0];
498 b = p->sum[1];
499 c = p->sum[2];
500 d = p->sum[3];
501
502 MD5STEP(F1, a, b, c, d, in + (4 * 0), 0xd76aa478, 7);
503 MD5STEP(F1, d, a, b, c, in + (4 * 1), 0xe8c7b756, 12);
504 MD5STEP(F1, c, d, a, b, in + (4 * 2), 0x242070db, 17);
505 MD5STEP(F1, b, c, d, a, in + (4 * 3), 0xc1bdceee, 22);
506 MD5STEP(F1, a, b, c, d, in + (4 * 4), 0xf57c0faf, 7);
507 MD5STEP(F1, d, a, b, c, in + (4 * 5), 0x4787c62a, 12);
508 MD5STEP(F1, c, d, a, b, in + (4 * 6), 0xa8304613, 17);
509 MD5STEP(F1, b, c, d, a, in + (4 * 7), 0xfd469501, 22);
510 MD5STEP(F1, a, b, c, d, in + (4 * 8), 0x698098d8, 7);
511 MD5STEP(F1, d, a, b, c, in + (4 * 9), 0x8b44f7af, 12);
512 MD5STEP(F1, c, d, a, b, in + (4 * 10), 0xffff5bb1, 17);
513 MD5STEP(F1, b, c, d, a, in + (4 * 11), 0x895cd7be, 22);
514 MD5STEP(F1, a, b, c, d, in + (4 * 12), 0x6b901122, 7);
515 MD5STEP(F1, d, a, b, c, in + (4 * 13), 0xfd987193, 12);
516 MD5STEP(F1, c, d, a, b, in + (4 * 14), 0xa679438e, 17);
517 MD5STEP(F1, b, c, d, a, in + (4 * 15), 0x49b40821, 22);
518
519 MD5STEP(F2, a, b, c, d, in + (4 * 1), 0xf61e2562, 5);
520 MD5STEP(F2, d, a, b, c, in + (4 * 6), 0xc040b340, 9);
521 MD5STEP(F2, c, d, a, b, in + (4 * 11), 0x265e5a51, 14);
522 MD5STEP(F2, b, c, d, a, in + (4 * 0), 0xe9b6c7aa, 20);
523 MD5STEP(F2, a, b, c, d, in + (4 * 5), 0xd62f105d, 5);
524 MD5STEP(F2, d, a, b, c, in + (4 * 10), 0x02441453, 9);
525 MD5STEP(F2, c, d, a, b, in + (4 * 15), 0xd8a1e681, 14);
526 MD5STEP(F2, b, c, d, a, in + (4 * 4), 0xe7d3fbc8, 20);
527 MD5STEP(F2, a, b, c, d, in + (4 * 9), 0x21e1cde6, 5);
528 MD5STEP(F2, d, a, b, c, in + (4 * 14), 0xc33707d6, 9);
529 MD5STEP(F2, c, d, a, b, in + (4 * 3), 0xf4d50d87, 14);
530 MD5STEP(F2, b, c, d, a, in + (4 * 8), 0x455a14ed, 20);
531 MD5STEP(F2, a, b, c, d, in + (4 * 13), 0xa9e3e905, 5);
532 MD5STEP(F2, d, a, b, c, in + (4 * 2), 0xfcefa3f8, 9);
533 MD5STEP(F2, c, d, a, b, in + (4 * 7), 0x676f02d9, 14);
534 MD5STEP(F2, b, c, d, a, in + (4 * 12), 0x8d2a4c8a, 20);
535
536 MD5STEP(F3, a, b, c, d, in + (4 * 5), 0xfffa3942, 4);
537 MD5STEP(F3, d, a, b, c, in + (4 * 8), 0x8771f681, 11);
538 MD5STEP(F3, c, d, a, b, in + (4 * 11), 0x6d9d6122, 16);
539 MD5STEP(F3, b, c, d, a, in + (4 * 14), 0xfde5380c, 23);
540 MD5STEP(F3, a, b, c, d, in + (4 * 1), 0xa4beea44, 4);
541 MD5STEP(F3, d, a, b, c, in + (4 * 4), 0x4bdecfa9, 11);
542 MD5STEP(F3, c, d, a, b, in + (4 * 7), 0xf6bb4b60, 16);
543 MD5STEP(F3, b, c, d, a, in + (4 * 10), 0xbebfbc70, 23);
544 MD5STEP(F3, a, b, c, d, in + (4 * 13), 0x289b7ec6, 4);
545 MD5STEP(F3, d, a, b, c, in + (4 * 0), 0xeaa127fa, 11);
546 MD5STEP(F3, c, d, a, b, in + (4 * 3), 0xd4ef3085, 16);
547 MD5STEP(F3, b, c, d, a, in + (4 * 6), 0x04881d05, 23);
548 MD5STEP(F3, a, b, c, d, in + (4 * 9), 0xd9d4d039, 4);
549 MD5STEP(F3, d, a, b, c, in + (4 * 12), 0xe6db99e5, 11);
550 MD5STEP(F3, c, d, a, b, in + (4 * 15), 0x1fa27cf8, 16);
551 MD5STEP(F3, b, c, d, a, in + (4 * 2), 0xc4ac5665, 23);
552
553 MD5STEP(F4, a, b, c, d, in + (4 * 0), 0xf4292244, 6);
554 MD5STEP(F4, d, a, b, c, in + (4 * 7), 0x432aff97, 10);
555 MD5STEP(F4, c, d, a, b, in + (4 * 14), 0xab9423a7, 15);
556 MD5STEP(F4, b, c, d, a, in + (4 * 5), 0xfc93a039, 21);
557 MD5STEP(F4, a, b, c, d, in + (4 * 12), 0x655b59c3, 6);
558 MD5STEP(F4, d, a, b, c, in + (4 * 3), 0x8f0ccc92, 10);
559 MD5STEP(F4, c, d, a, b, in + (4 * 10), 0xffeff47d, 15);
560 MD5STEP(F4, b, c, d, a, in + (4 * 1), 0x85845dd1, 21);
561 MD5STEP(F4, a, b, c, d, in + (4 * 8), 0x6fa87e4f, 6);
562 MD5STEP(F4, d, a, b, c, in + (4 * 15), 0xfe2ce6e0, 10);
563 MD5STEP(F4, c, d, a, b, in + (4 * 6), 0xa3014314, 15);
564 MD5STEP(F4, b, c, d, a, in + (4 * 13), 0x4e0811a1, 21);
565 MD5STEP(F4, a, b, c, d, in + (4 * 4), 0xf7537e82, 6);
566 MD5STEP(F4, d, a, b, c, in + (4 * 11), 0xbd3af235, 10);
567 MD5STEP(F4, c, d, a, b, in + (4 * 2), 0x2ad7d2bb, 15);
568 MD5STEP(F4, b, c, d, a, in + (4 * 9), 0xeb86d391, 21);
569
570 p->sum[0] += a;
571 p->sum[1] += b;
572 p->sum[2] += c;
573 p->sum[3] += d;
574 }
575
576 #undef F1
577 #undef F2
578 #undef F3
579 #undef F4
580 #undef MD5STEP
581
582 /* Add some bytes */
583 static void sa_MD5_add(sa_MD5 *p, ORD8 *ibuf, unsigned int len) {
584 unsigned int bs;
585
586 if (p->fin)
587 return; /* This is actually an error */
588
589 bs = p->tlen; /* Current bytes added */
590 p->tlen = bs + len; /* Update length after adding this buffer */
591 bs &= 0x3f; /* Bytes already in buffer */
592
593 /* Deal with any existing partial bytes in p->buf */
594 if (bs) {
595 ORD8 *np = (ORD8 *)p->buf + bs; /* Next free location in partial buffer */
596
597 bs = 64 - bs; /* Free space in partial buffer */
598
599 if (len < bs) { /* Not enought new to make a full buffer */
600 memmove(np, ibuf, len);
601 return;
602 }
603
604 memmove(np, ibuf, bs); /* Now got one full buffer */
605 sa_MD5_accume(p, np);
606 ibuf += bs;
607 len -= bs;
608 }
609
610 /* Deal with input data 64 bytes at a time */
611 while (len >= 64) {
612 sa_MD5_accume(p, ibuf);
613 ibuf += 64;
614 len -= 64;
615 }
616
617 /* Deal with any remaining bytes */
618 memmove(p->buf, ibuf, len);
619 }
620
621 /* Finalise the checksum and return the result. */
622 static void sa_MD5_get(sa_MD5 *p, ORD8 chsum[16]) {
623 int i;
624 unsigned count;
625 ORD32 bits1, bits0;
626 ORD8 *pp;
627
628 if (p->fin == 0) {
629
630 /* Compute number of bytes processed mod 64 */
631 count = p->tlen & 0x3f;
632
633 /* Set the first char of padding to 0x80. This is safe since there is
634 always at least one byte free */
635 pp = p->buf + count;
636 *pp++ = 0x80;
637
638 /* Bytes of padding needed to make 64 bytes */
639 count = 64 - 1 - count;
640
641 /* Pad out to 56 mod 64, allowing 8 bytes for length in bits. */
642 if (count < 8) { /* Not enough space for padding and length */
643
644 memset(pp, 0, count);
645 sa_MD5_accume(p, p->buf);
646
647 /* Now fill the next block with 56 bytes */
648 memset(p->buf, 0, 56);
649 } else {
650 /* Pad block to 56 bytes */
651 memset(pp, 0, count - 8);
652 }
653
654 /* Compute number of bits */
655 bits1 = 0x7 & (p->tlen >> (32 - 3));
656 bits0 = p->tlen << 3;
657
658 /* Append number of bits */
659 p->buf[64 - 8] = bits0 & 0xff;
660 p->buf[64 - 7] = (bits0 >> 8) & 0xff;
661 p->buf[64 - 6] = (bits0 >> 16) & 0xff;
662 p->buf[64 - 5] = (bits0 >> 24) & 0xff;
663 p->buf[64 - 4] = bits1 & 0xff;
664 p->buf[64 - 3] = (bits1 >> 8) & 0xff;
665 p->buf[64 - 2] = (bits1 >> 16) & 0xff;
666 p->buf[64 - 1] = (bits1 >> 24) & 0xff;
667
668 sa_MD5_accume(p, p->buf);
669
670 p->fin = 1;
671 }
672
673 /* Return the result, lsb to msb */
674 pp = chsum;
675 for (i = 0; i < 4; i++) {
676 *pp++ = p->sum[i] & 0xff;
677 *pp++ = (p->sum[i] >> 8) & 0xff;
678 *pp++ = (p->sum[i] >> 16) & 0xff;
679 *pp++ = (p->sum[i] >> 24) & 0xff;
680 }
681 }
682
683
684 /* Delete the instance */
685 static void sa_MD5_del(sa_MD5 *p) {
686
687 /* This object */
688 if (p != NULL)
689 free(p);
690 }
691
692 /* Create a new MD5 checksumming object, with a reset checksum value */
693 /* Return it or NULL if there is an error */
694 sa_MD5 *new_sa_MD5() {
695 sa_MD5 *p;
696
697 if ((p = (sa_MD5 *)calloc(1,sizeof(sa_MD5))) == NULL)
698 return NULL;
699
700 p->reset = sa_MD5_reset;
701 p->add = sa_MD5_add;
702 p->get = sa_MD5_get;
703 p->del = sa_MD5_del;
704
705 p->reset(p);
706
707 return p;
708 }
709
710 /* - - - - - - - - - - - - - - - - - - - - - - - - */
711 /* A sub-set of ludecomp code from numlib */
712
713 int sa_lu_decomp(double **a, int n, int *pivx, double *rip) {
714 int i, j;
715 double *rscale, RSCALE[10];
716
717 if (n <= 10)
718 rscale = RSCALE;
719 else
720 rscale = dvector(0, n-1);
721
722 for (i = 0; i < n; i++) {
723 double big;
724 for (big = 0.0, j=0; j < n; j++) {
725 double temp;
726 temp = fabs(a[i][j]);
727 if (temp > big)
728 big = temp;
729 }
730 if (fabs(big) <= DBL_MIN) {
731 if (rscale != RSCALE)
732 free_dvector(rscale, 0, n-1);
733 return 1;
734 }
735 rscale[i] = 1.0/big;
736 }
737
738 for (*rip = 1.0, j = 0; j < n; j++) {
739 double big;
740 int k, bigi = 0;
741
742 for (i = 0; i < j; i++) {
743 double sum;
744 sum = a[i][j];
745 for (k = 0; k < i; k++)
746 sum -= a[i][k] * a[k][j];
747 a[i][j] = sum;
748 }
749
750 for (big = 0.0, i = j; i < n; i++) {
751 double sum, temp;
752 sum = a[i][j];
753 for (k = 0; k < j; k++)
754 sum -= a[i][k] * a[k][j];
755 a[i][j] = sum;
756 temp = rscale[i] * fabs(sum);
757 if (temp >= big) {
758 big = temp;
759 bigi = i;
760 }
761 }
762
763 if (j != bigi) {
764 {
765 double *temp;
766 temp = a[bigi];
767 a[bigi] = a[j];
768 a[j] = temp;
769 }
770 *rip = -(*rip);
771 rscale[bigi] = rscale[j];
772 }
773
774 pivx[j] = bigi;
775 if (fabs(a[j][j]) <= DBL_MIN) {
776 if (rscale != RSCALE)
777 free_dvector(rscale, 0, n-1);
778 return 1;
779 }
780
781 if (j != (n-1)) {
782 double temp;
783 temp = 1.0/a[j][j];
784 for (i = j+1; i < n; i++)
785 a[i][j] *= temp;
786 }
787 }
788 if (rscale != RSCALE)
789 free_dvector(rscale, 0, n-1);
790 return 0;
791 }
792
793 void sa_lu_backsub(double **a, int n, int *pivx, double *b) {
794 int i, j;
795 int nvi;
796
797 for (nvi = -1, i = 0; i < n; i++) {
798 int px;
799 double sum;
800
801 px = pivx[i];
802 sum = b[px];
803 b[px] = b[i];
804 if (nvi >= 0) {
805 for (j = nvi; j < i; j++)
806 sum -= a[i][j] * b[j];
807 } else {
808 if (sum != 0.0)
809 nvi = i;
810 }
811 b[i] = sum;
812 }
813
814 for (i = (n-1); i >= 0; i--) {
815 double sum;
816 sum = b[i];
817 for (j = i+1; j < n; j++)
818 sum -= a[i][j] * b[j];
819 b[i] = sum/a[i][i];
820 }
821 }
822
823 int sa_lu_invert(double **a, int n) {
824 int i, j;
825 double rip;
826 int *pivx, PIVX[10];
827 double **y;
828
829 if (n <= 10)
830 pivx = PIVX;
831 else
832 pivx = ivector(0, n-1);
833
834 if (sa_lu_decomp(a, n, pivx, &rip)) {
835 if (pivx != PIVX)
836 free_ivector(pivx, 0, n-1);
837 return 1;
838 }
839
840 y = dmatrix(0, n-1, 0, n-1);
841 for (i = 0; i < n; i++) {
842 for (j = 0; j < n; j++) {
843 y[i][j] = a[i][j];
844 }
845 }
846
847 for (i = 0; i < n; i++) {
848 for (j = 0; j < n; j++)
849 a[i][j] = 0.0;
850 a[i][i] = 1.0;
851 sa_lu_backsub(y, n, pivx, a[i]);
852 }
853
854 free_dmatrix(y, 0, n-1, 0, n-1);
855 if (pivx != PIVX)
856 free_ivector(pivx, 0, n-1);
857
858 return 0;
859 }
860
861 int sa_lu_psinvert(double **out, double **in, int m, int n) {
862 int rv = 0;
863 double **tr;
864 double **sq;
865
866 tr = dmatrix(0, n-1, 0, m-1);
867 matrix_trans(tr, in, m, n);
868
869 if (m > n) {
870 sq = dmatrix(0, n-1, 0, n-1);
871 if ((rv = matrix_mult(sq, n, n, tr, n, m, in, m, n)) == 0) {
872 if ((rv = sa_lu_invert(sq, n)) == 0) {
873 rv = matrix_mult(out, n, m, sq, n, n, tr, n, m);
874 }
875 }
876 free_dmatrix(sq, 0, n-1, 0, n-1);
877 } else {
878 sq = dmatrix(0, m-1, 0, m-1);
879 if ((rv = matrix_mult(sq, m, m, in, m, n, tr, n, m)) == 0) {
880 if ((rv = sa_lu_invert(sq, m)) == 0) {
881 rv = matrix_mult(out, n, m, tr, n, m, sq, m, m);
882 }
883 }
884 free_dmatrix(sq, 0, m-1, 0, m-1);
885 }
886
887 free_dmatrix(tr, 0, n-1, 0, m-1);
888 return rv;
889 }
890
891
892 #endif /* SALONEINSTLIB */
893
894