5 |
|
* General matrix operations. |
6 |
|
*/ |
7 |
|
|
8 |
– |
#include <stdio.h> |
8 |
|
#include <stdlib.h> |
9 |
< |
#include <string.h> |
10 |
< |
#include <fcntl.h> |
9 |
> |
#include <errno.h> |
10 |
> |
#include "rtio.h" |
11 |
> |
#include "platform.h" |
12 |
|
#include "resolu.h" |
13 |
< |
#include "rtprocess.h" |
13 |
> |
#include "paths.h" |
14 |
|
#include "rmatrix.h" |
15 |
+ |
#if !defined(_WIN32) && !defined(_WIN64) |
16 |
+ |
#include <sys/mman.h> |
17 |
+ |
#endif |
18 |
|
|
19 |
< |
static char rmx_mismatch_warn[] = "WARNING: data type mismatch\n"; |
19 |
> |
static const char rmx_mismatch_warn[] = "WARNING: data type mismatch\n"; |
20 |
|
|
21 |
< |
/* Allocate a nr x nc matrix with n components */ |
21 |
> |
#define array_size(rm) (sizeof(double)*(rm)->nrows*(rm)->ncols*(rm)->ncomp) |
22 |
> |
#define mapped_size(rm) ((char *)(rm)->mtx + array_size(rm) - (char *)(rm)->mapped) |
23 |
> |
|
24 |
> |
/* Initialize a RMATRIX struct but don't allocate array space */ |
25 |
|
RMATRIX * |
26 |
< |
rmx_alloc(int nr, int nc, int n) |
26 |
> |
rmx_new(int nr, int nc, int n) |
27 |
|
{ |
28 |
|
RMATRIX *dnew; |
29 |
|
|
30 |
< |
if ((nr <= 0) | (nc <= 0) | (n <= 0)) |
30 |
> |
if (n <= 0) |
31 |
|
return(NULL); |
32 |
< |
dnew = (RMATRIX *)malloc(sizeof(RMATRIX)-sizeof(dnew->mtx) + |
33 |
< |
sizeof(dnew->mtx[0])*(n*nr*nc)); |
34 |
< |
if (dnew == NULL) |
32 |
> |
|
33 |
> |
dnew = (RMATRIX *)calloc(1, sizeof(RMATRIX)); |
34 |
> |
if (!dnew) |
35 |
|
return(NULL); |
36 |
< |
dnew->nrows = nr; dnew->ncols = nc; dnew->ncomp = n; |
36 |
> |
|
37 |
|
dnew->dtype = DTdouble; |
38 |
< |
dnew->info = NULL; |
38 |
> |
dnew->nrows = nr; |
39 |
> |
dnew->ncols = nc; |
40 |
> |
dnew->ncomp = n; |
41 |
> |
setcolor(dnew->cexp, 1.f, 1.f, 1.f); |
42 |
> |
memcpy(dnew->wlpart, WLPART, sizeof(dnew->wlpart)); |
43 |
> |
|
44 |
|
return(dnew); |
45 |
|
} |
46 |
|
|
47 |
< |
/* Free a RMATRIX array */ |
47 |
> |
/* Prepare a RMATRIX for writing (allocate array if needed) */ |
48 |
> |
int |
49 |
> |
rmx_prepare(RMATRIX *rm) |
50 |
> |
{ |
51 |
> |
if (!rm) return(0); |
52 |
> |
if (rm->mtx) |
53 |
> |
return(1); |
54 |
> |
if ((rm->nrows <= 0) | (rm->ncols <= 0) | (rm->ncomp <= 0)) |
55 |
> |
return(0); |
56 |
> |
rm->mtx = (double *)malloc(array_size(rm)); |
57 |
> |
return(rm->mtx != NULL); |
58 |
> |
} |
59 |
> |
|
60 |
> |
/* Call rmx_new() and rmx_prepare() */ |
61 |
> |
RMATRIX * |
62 |
> |
rmx_alloc(int nr, int nc, int n) |
63 |
> |
{ |
64 |
> |
RMATRIX *dnew = rmx_new(nr, nc, n); |
65 |
> |
|
66 |
> |
if (dnew && !rmx_prepare(dnew)) { |
67 |
> |
rmx_free(dnew); |
68 |
> |
dnew = NULL; |
69 |
> |
} |
70 |
> |
return(dnew); |
71 |
> |
} |
72 |
> |
|
73 |
> |
/* Clear state by freeing info and matrix data */ |
74 |
|
void |
75 |
< |
rmx_free(RMATRIX *rm) |
75 |
> |
rmx_reset(RMATRIX *rm) |
76 |
|
{ |
77 |
|
if (!rm) return; |
78 |
< |
if (rm->info) |
78 |
> |
if (rm->info) { |
79 |
|
free(rm->info); |
80 |
+ |
rm->info = NULL; |
81 |
+ |
} |
82 |
+ |
if (rm->mtx) { |
83 |
+ |
#ifdef MAP_FILE |
84 |
+ |
if (rm->mapped) { |
85 |
+ |
munmap(rm->mapped, mapped_size(rm)); |
86 |
+ |
rm->mapped = NULL; |
87 |
+ |
} else |
88 |
+ |
#endif |
89 |
+ |
free(rm->mtx); |
90 |
+ |
rm->mtx = NULL; |
91 |
+ |
} |
92 |
+ |
} |
93 |
+ |
|
94 |
+ |
/* Free an RMATRIX struct and data */ |
95 |
+ |
void |
96 |
+ |
rmx_free(RMATRIX *rm) |
97 |
+ |
{ |
98 |
+ |
if (!rm) return; |
99 |
+ |
rmx_reset(rm); |
100 |
|
free(rm); |
101 |
|
} |
102 |
|
|
104 |
|
int |
105 |
|
rmx_newtype(int dtyp1, int dtyp2) |
106 |
|
{ |
107 |
< |
if ((dtyp1==DTxyze) | (dtyp1==DTrgbe) | |
108 |
< |
(dtyp2==DTxyze) | (dtyp2==DTrgbe) |
107 |
> |
if ((dtyp1==DTxyze) | (dtyp1==DTrgbe) | (dtyp1==DTspec) | |
108 |
> |
(dtyp2==DTxyze) | (dtyp2==DTrgbe) | (dtyp2==DTspec) |
109 |
|
&& dtyp1 != dtyp2) |
110 |
|
return(0); |
111 |
|
if (dtyp1 < dtyp2) |
117 |
|
int |
118 |
|
rmx_addinfo(RMATRIX *rm, const char *info) |
119 |
|
{ |
120 |
< |
if (!info || !*info) |
120 |
> |
int oldlen = 0; |
121 |
> |
|
122 |
> |
if (!rm || !info || !*info) |
123 |
|
return(0); |
124 |
|
if (!rm->info) { |
125 |
|
rm->info = (char *)malloc(strlen(info)+1); |
126 |
|
if (rm->info) rm->info[0] = '\0'; |
127 |
< |
} else |
127 |
> |
} else { |
128 |
> |
oldlen = strlen(rm->info); |
129 |
|
rm->info = (char *)realloc(rm->info, |
130 |
< |
strlen(rm->info)+strlen(info)+1); |
130 |
> |
oldlen+strlen(info)+1); |
131 |
> |
} |
132 |
|
if (!rm->info) |
133 |
|
return(0); |
134 |
< |
strcat(rm->info, info); |
134 |
> |
strcpy(rm->info+oldlen, info); |
135 |
|
return(1); |
136 |
|
} |
137 |
|
|
139 |
|
get_dminfo(char *s, void *p) |
140 |
|
{ |
141 |
|
RMATRIX *ip = (RMATRIX *)p; |
142 |
< |
char fmt[64]; |
142 |
> |
char fmt[MAXFMTLEN]; |
143 |
|
int i; |
144 |
|
|
145 |
< |
if (headidval(fmt, s)) |
145 |
> |
if (headidval(NULL, s)) |
146 |
|
return(0); |
147 |
< |
if (!strncmp(s, "NCOMP=", 6)) { |
148 |
< |
ip->ncomp = atoi(s+6); |
147 |
> |
if (isncomp(s)) { |
148 |
> |
ip->ncomp = ncompval(s); |
149 |
|
return(0); |
150 |
|
} |
151 |
|
if (!strncmp(s, "NROWS=", 6)) { |
156 |
|
ip->ncols = atoi(s+6); |
157 |
|
return(0); |
158 |
|
} |
159 |
+ |
if ((i = isbigendian(s)) >= 0) { |
160 |
+ |
ip->swapin = (nativebigendian() != i); |
161 |
+ |
return(0); |
162 |
+ |
} |
163 |
+ |
if (isexpos(s)) { |
164 |
+ |
float f = exposval(s); |
165 |
+ |
scalecolor(ip->cexp, f); |
166 |
+ |
return(0); |
167 |
+ |
} |
168 |
+ |
if (iscolcor(s)) { |
169 |
+ |
COLOR ctmp; |
170 |
+ |
colcorval(ctmp, s); |
171 |
+ |
multcolor(ip->cexp, ctmp); |
172 |
+ |
return(0); |
173 |
+ |
} |
174 |
+ |
if (iswlsplit(s)) { |
175 |
+ |
wlsplitval(ip->wlpart, s); |
176 |
+ |
return(0); |
177 |
+ |
} |
178 |
|
if (!formatval(fmt, s)) { |
179 |
|
rmx_addinfo(ip, s); |
180 |
|
return(0); |
181 |
< |
} |
181 |
> |
} /* else check format */ |
182 |
|
for (i = 1; i < DTend; i++) |
183 |
|
if (!strcmp(fmt, cm_fmt_id[i])) { |
184 |
|
ip->dtype = i; |
188 |
|
} |
189 |
|
|
190 |
|
static int |
191 |
< |
rmx_load_ascii(RMATRIX *rm, FILE *fp) |
191 |
> |
rmx_load_ascii(double *drp, const RMATRIX *rm, FILE *fp) |
192 |
|
{ |
193 |
< |
int i, j, k; |
194 |
< |
#ifdef _WIN32 |
195 |
< |
_setmode(fileno(fp), _O_TEXT); |
196 |
< |
#endif |
197 |
< |
for (i = 0; i < rm->nrows; i++) |
198 |
< |
for (j = 0; j < rm->ncols; j++) |
119 |
< |
for (k = 0; k < rm->ncomp; k++) |
120 |
< |
if (fscanf(fp, "%lf", &rmx_lval(rm,i,j,k)) != 1) |
121 |
< |
return(0); |
193 |
> |
int j, k; |
194 |
> |
|
195 |
> |
for (j = 0; j < rm->ncols; j++) |
196 |
> |
for (k = rm->ncomp; k-- > 0; ) |
197 |
> |
if (fscanf(fp, "%lf", drp++) != 1) |
198 |
> |
return(0); |
199 |
|
return(1); |
200 |
|
} |
201 |
|
|
202 |
|
static int |
203 |
< |
rmx_load_float(RMATRIX *rm, FILE *fp) |
203 |
> |
rmx_load_float(double *drp, const RMATRIX *rm, FILE *fp) |
204 |
|
{ |
205 |
< |
int i, j, k; |
205 |
> |
int j, k; |
206 |
|
float val[100]; |
207 |
|
|
208 |
|
if (rm->ncomp > 100) { |
209 |
|
fputs("Unsupported # components in rmx_load_float()\n", stderr); |
210 |
|
exit(1); |
211 |
|
} |
212 |
< |
for (i = 0; i < rm->nrows; i++) |
213 |
< |
for (j = 0; j < rm->ncols; j++) { |
214 |
< |
if (fread(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp) |
215 |
< |
return(0); |
216 |
< |
for (k = rm->ncomp; k--; ) |
217 |
< |
rmx_lval(rm,i,j,k) = val[k]; |
218 |
< |
} |
212 |
> |
for (j = 0; j < rm->ncols; j++) { |
213 |
> |
if (getbinary(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp) |
214 |
> |
return(0); |
215 |
> |
if (rm->swapin) |
216 |
> |
swap32((char *)val, rm->ncomp); |
217 |
> |
for (k = 0; k < rm->ncomp; k++) |
218 |
> |
*drp++ = val[k]; |
219 |
> |
} |
220 |
|
return(1); |
221 |
|
} |
222 |
|
|
223 |
|
static int |
224 |
< |
rmx_load_double(RMATRIX *rm, FILE *fp) |
224 |
> |
rmx_load_double(double *drp, const RMATRIX *rm, FILE *fp) |
225 |
|
{ |
226 |
< |
int i, j, k; |
227 |
< |
double val[100]; |
226 |
> |
if (getbinary(drp, sizeof(*drp)*rm->ncomp, rm->ncols, fp) != rm->ncols) |
227 |
> |
return(0); |
228 |
> |
if (rm->swapin) |
229 |
> |
swap64((char *)drp, rm->ncols*rm->ncomp); |
230 |
> |
return(1); |
231 |
> |
} |
232 |
|
|
233 |
< |
if (rm->ncomp > 100) { |
234 |
< |
fputs("Unsupported # components in rmx_load_double()\n", stderr); |
235 |
< |
exit(1); |
233 |
> |
static int |
234 |
> |
rmx_load_rgbe(double *drp, const RMATRIX *rm, FILE *fp) |
235 |
> |
{ |
236 |
> |
COLR *scan; |
237 |
> |
COLOR col; |
238 |
> |
int j; |
239 |
> |
|
240 |
> |
if (rm->ncomp != 3) |
241 |
> |
return(0); |
242 |
> |
scan = (COLR *)tempbuffer(sizeof(COLR)*rm->ncols); |
243 |
> |
if (!scan) |
244 |
> |
return(0); |
245 |
> |
if (freadcolrs(scan, rm->ncols, fp) < 0) |
246 |
> |
return(0); |
247 |
> |
for (j = 0; j < rm->ncols; j++) { |
248 |
> |
colr_color(col, scan[j]); |
249 |
> |
*drp++ = colval(col,RED); |
250 |
> |
*drp++ = colval(col,GRN); |
251 |
> |
*drp++ = colval(col,BLU); |
252 |
|
} |
155 |
– |
for (i = 0; i < rm->nrows; i++) |
156 |
– |
for (j = 0; j < rm->ncols; j++) { |
157 |
– |
if (fread(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp) |
158 |
– |
return(0); |
159 |
– |
for (k = rm->ncomp; k--; ) |
160 |
– |
rmx_lval(rm,i,j,k) = val[k]; |
161 |
– |
} |
253 |
|
return(1); |
254 |
|
} |
255 |
|
|
256 |
|
static int |
257 |
< |
rmx_load_rgbe(RMATRIX *rm, FILE *fp) |
257 |
> |
rmx_load_spec(double *drp, const RMATRIX *rm, FILE *fp) |
258 |
|
{ |
259 |
< |
COLOR *scan = (COLOR *)malloc(sizeof(COLOR)*rm->ncols); |
260 |
< |
int i, j; |
259 |
> |
uby8 *scan; |
260 |
> |
SCOLOR scol; |
261 |
> |
int j, k; |
262 |
|
|
263 |
< |
if (scan == NULL) |
263 |
> |
if ((rm->ncomp < 3) | (rm->ncomp > MAXCSAMP)) |
264 |
|
return(0); |
265 |
< |
for (i = 0; i < rm->nrows; i++) { |
266 |
< |
if (freadscan(scan, rm->ncols, fp) < 0) { |
175 |
< |
free(scan); |
265 |
> |
scan = (uby8 *)tempbuffer((rm->ncomp+1)*rm->ncols); |
266 |
> |
if (!scan) |
267 |
|
return(0); |
268 |
< |
} |
269 |
< |
for (j = rm->ncols; j--; ) { |
270 |
< |
rmx_lval(rm,i,j,0) = colval(scan[j],RED); |
271 |
< |
rmx_lval(rm,i,j,1) = colval(scan[j],GRN); |
272 |
< |
rmx_lval(rm,i,j,2) = colval(scan[j],BLU); |
273 |
< |
} |
268 |
> |
if (freadscolrs(scan, rm->ncomp, rm->ncols, fp) < 0) |
269 |
> |
return(0); |
270 |
> |
for (j = 0; j < rm->ncols; j++) { |
271 |
> |
scolr2scolor(scol, scan+j*(rm->ncomp+1), rm->ncomp); |
272 |
> |
for (k = 0; k < rm->ncomp; k++) |
273 |
> |
*drp++ = scol[k]; |
274 |
|
} |
184 |
– |
free(scan); |
275 |
|
return(1); |
276 |
|
} |
277 |
|
|
278 |
+ |
/* Read matrix header from input stream (cannot be XML) */ |
279 |
+ |
int |
280 |
+ |
rmx_load_header(RMATRIX *rm, FILE *fp) |
281 |
+ |
{ |
282 |
+ |
if (!rm | !fp) |
283 |
+ |
return(0); |
284 |
+ |
rmx_reset(rm); /* clear state */ |
285 |
+ |
if (rm->nrows | rm->ncols | !rm->dtype) { |
286 |
+ |
rm->nrows = rm->ncols = 0; |
287 |
+ |
rm->ncomp = 3; |
288 |
+ |
setcolor(rm->cexp, 1.f, 1.f, 1.f); |
289 |
+ |
memcpy(rm->wlpart, WLPART, sizeof(rm->wlpart)); |
290 |
+ |
rm->swapin = 0; |
291 |
+ |
} |
292 |
+ |
rm->dtype = DTascii; /* assumed w/o FORMAT */ |
293 |
+ |
if (getheader(fp, get_dminfo, rm) < 0) { |
294 |
+ |
fputs("Unrecognized matrix format\n", stderr); |
295 |
+ |
return(0); |
296 |
+ |
} |
297 |
+ |
if ((rm->dtype == DTrgbe) | (rm->dtype == DTxyze) && |
298 |
+ |
rm->ncomp != 3) |
299 |
+ |
return(0); |
300 |
+ |
if (rm->ncols <= 0 && /* resolution string? */ |
301 |
+ |
!fscnresolu(&rm->ncols, &rm->nrows, fp)) |
302 |
+ |
return(0); |
303 |
+ |
if (rm->dtype == DTascii) /* set file type (WINDOWS) */ |
304 |
+ |
SET_FILE_TEXT(fp); |
305 |
+ |
else |
306 |
+ |
SET_FILE_BINARY(fp); |
307 |
+ |
return(1); |
308 |
+ |
} |
309 |
+ |
|
310 |
+ |
/* Load next row as double (cannot be XML) */ |
311 |
+ |
int |
312 |
+ |
rmx_load_row(double *drp, const RMATRIX *rm, FILE *fp) |
313 |
+ |
{ |
314 |
+ |
switch (rm->dtype) { |
315 |
+ |
case DTascii: |
316 |
+ |
return(rmx_load_ascii(drp, rm, fp)); |
317 |
+ |
case DTfloat: |
318 |
+ |
return(rmx_load_float(drp, rm, fp)); |
319 |
+ |
case DTdouble: |
320 |
+ |
return(rmx_load_double(drp, rm, fp)); |
321 |
+ |
case DTrgbe: |
322 |
+ |
case DTxyze: |
323 |
+ |
return(rmx_load_rgbe(drp, rm, fp)); |
324 |
+ |
case DTspec: |
325 |
+ |
return(rmx_load_spec(drp, rm, fp)); |
326 |
+ |
default: |
327 |
+ |
fputs("Unsupported data type in rmx_load_row()\n", stderr); |
328 |
+ |
} |
329 |
+ |
return(0); |
330 |
+ |
} |
331 |
+ |
|
332 |
+ |
/* Allocate & load post-header data from stream given type set in rm->dtype */ |
333 |
+ |
int |
334 |
+ |
rmx_load_data(RMATRIX *rm, FILE *fp) |
335 |
+ |
{ |
336 |
+ |
int i; |
337 |
+ |
#ifdef MAP_FILE |
338 |
+ |
long pos; /* map memory for file > 1MB if possible */ |
339 |
+ |
if ((rm->dtype == DTdouble) & !rm->swapin && array_size(rm) >= 1L<<20 && |
340 |
+ |
(pos = ftell(fp)) >= 0 && !(pos % sizeof(double))) { |
341 |
+ |
rm->mapped = mmap(NULL, array_size(rm)+pos, PROT_READ|PROT_WRITE, |
342 |
+ |
MAP_PRIVATE, fileno(fp), 0); |
343 |
+ |
if (rm->mapped != MAP_FAILED) { |
344 |
+ |
rm->mtx = (double *)rm->mapped + pos/sizeof(double); |
345 |
+ |
return(1); |
346 |
+ |
} /* else fall back on reading into memory */ |
347 |
+ |
rm->mapped = NULL; |
348 |
+ |
} |
349 |
+ |
#endif |
350 |
+ |
if (!rmx_prepare(rm)) { /* need in-core matrix array */ |
351 |
+ |
fprintf(stderr, "Cannot allocate %g MByte matrix array\n", |
352 |
+ |
(1./(1L<<20))*(double)array_size(rm)); |
353 |
+ |
return(0); |
354 |
+ |
} |
355 |
+ |
for (i = 0; i < rm->nrows; i++) |
356 |
+ |
if (!rmx_load_row(rmx_lval(rm,i,0), rm, fp)) |
357 |
+ |
return(0); |
358 |
+ |
return(1); |
359 |
+ |
} |
360 |
+ |
|
361 |
|
/* Load matrix from supported file type */ |
362 |
|
RMATRIX * |
363 |
< |
rmx_load(const char *inspec) |
363 |
> |
rmx_load(const char *inspec, RMPref rmp) |
364 |
|
{ |
365 |
< |
FILE *fp = stdin; |
193 |
< |
RMATRIX dinfo; |
365 |
> |
FILE *fp; |
366 |
|
RMATRIX *dnew; |
367 |
+ |
int ok; |
368 |
|
|
369 |
< |
if (inspec == NULL) { /* reading from stdin? */ |
370 |
< |
inspec = "<stdin>"; |
371 |
< |
#ifdef _WIN32 |
372 |
< |
_setmode(fileno(stdin), _O_BINARY); |
373 |
< |
#endif |
374 |
< |
} else if (inspec[0] == '!') { |
375 |
< |
if ((fp = popen(inspec+1, "r")) == NULL) |
376 |
< |
return(NULL); |
377 |
< |
#ifdef _WIN32 |
205 |
< |
_setmode(fileno(fp), _O_BINARY); |
206 |
< |
#endif |
207 |
< |
} else { |
369 |
> |
if (!inspec) |
370 |
> |
inspec = stdin_name; |
371 |
> |
else if (!*inspec) |
372 |
> |
return(NULL); |
373 |
> |
if (inspec == stdin_name) /* reading from stdin? */ |
374 |
> |
fp = stdin; |
375 |
> |
else if (inspec[0] == '!') |
376 |
> |
fp = popen(inspec+1, "r"); |
377 |
> |
else if (rmp != RMPnone) { |
378 |
|
const char *sp = inspec; /* check suffix */ |
379 |
|
while (*sp) |
380 |
|
++sp; |
381 |
|
while (sp > inspec && sp[-1] != '.') |
382 |
|
--sp; |
383 |
|
if (!strcasecmp(sp, "XML")) { /* assume it's a BSDF */ |
384 |
< |
CMATRIX *cm = cm_loadBTDF((char *)inspec); |
385 |
< |
if (cm == NULL) |
384 |
> |
CMATRIX *cm = rmp==RMPtrans ? cm_loadBTDF(inspec) : |
385 |
> |
cm_loadBRDF(inspec, rmp==RMPreflB) ; |
386 |
> |
if (!cm) |
387 |
|
return(NULL); |
388 |
|
dnew = rmx_from_cmatrix(cm); |
389 |
|
cm_free(cm); |
390 |
< |
return(dnew); |
391 |
< |
} |
392 |
< |
/* else open it ourselves */ |
393 |
< |
if ((fp = fopen(inspec, "rb")) == NULL) |
223 |
< |
return(NULL); |
390 |
> |
dnew->dtype = DTascii; |
391 |
> |
return(dnew); /* return here */ |
392 |
> |
} /* else open it ourselves */ |
393 |
> |
fp = fopen(inspec, "r"); |
394 |
|
} |
395 |
+ |
if (!fp) |
396 |
+ |
return(NULL); |
397 |
|
#ifdef getc_unlocked |
398 |
|
flockfile(fp); |
399 |
|
#endif |
400 |
< |
dinfo.nrows = dinfo.ncols = dinfo.ncomp = 0; |
401 |
< |
dinfo.dtype = DTascii; /* assumed w/o FORMAT */ |
402 |
< |
dinfo.info = NULL; |
403 |
< |
if (getheader(fp, get_dminfo, &dinfo) < 0) { |
404 |
< |
fclose(fp); |
400 |
> |
SET_FILE_BINARY(fp); /* load header info */ |
401 |
> |
if (!rmx_load_header(dnew = rmx_new(0,0,3), fp)) { |
402 |
> |
fprintf(stderr, "Bad header in: %s\n", inspec); |
403 |
> |
if (inspec[0] == '!') pclose(fp); |
404 |
> |
else fclose(fp); |
405 |
> |
rmx_free(dnew); |
406 |
|
return(NULL); |
407 |
|
} |
408 |
< |
if ((dinfo.nrows <= 0) | (dinfo.ncols <= 0)) { |
409 |
< |
if (!fscnresolu(&dinfo.ncols, &dinfo.nrows, fp)) { |
410 |
< |
fclose(fp); |
238 |
< |
return(NULL); |
239 |
< |
} |
240 |
< |
if (dinfo.ncomp <= 0) |
241 |
< |
dinfo.ncomp = 3; |
242 |
< |
else if ((dinfo.dtype == DTrgbe) | (dinfo.dtype == DTxyze) && |
243 |
< |
dinfo.ncomp != 3) { |
244 |
< |
fclose(fp); |
245 |
< |
return(NULL); |
246 |
< |
} |
247 |
< |
} |
248 |
< |
dnew = rmx_alloc(dinfo.nrows, dinfo.ncols, dinfo.ncomp); |
249 |
< |
if (dnew == NULL) { |
250 |
< |
fclose(fp); |
251 |
< |
return(NULL); |
252 |
< |
} |
253 |
< |
dnew->info = dinfo.info; |
254 |
< |
switch (dinfo.dtype) { |
255 |
< |
case DTascii: |
256 |
< |
if (!rmx_load_ascii(dnew, fp)) |
257 |
< |
goto loaderr; |
258 |
< |
dnew->dtype = DTascii; /* should leave double? */ |
259 |
< |
break; |
260 |
< |
case DTfloat: |
261 |
< |
if (!rmx_load_float(dnew, fp)) |
262 |
< |
goto loaderr; |
263 |
< |
dnew->dtype = DTfloat; |
264 |
< |
break; |
265 |
< |
case DTdouble: |
266 |
< |
if (!rmx_load_double(dnew, fp)) |
267 |
< |
goto loaderr; |
268 |
< |
dnew->dtype = DTdouble; |
269 |
< |
break; |
270 |
< |
case DTrgbe: |
271 |
< |
case DTxyze: |
272 |
< |
if (!rmx_load_rgbe(dnew, fp)) |
273 |
< |
goto loaderr; |
274 |
< |
dnew->dtype = dinfo.dtype; |
275 |
< |
break; |
276 |
< |
default: |
277 |
< |
goto loaderr; |
278 |
< |
} |
279 |
< |
if (fp != stdin) { |
408 |
> |
ok = rmx_load_data(dnew, fp); /* allocate & load data */ |
409 |
> |
|
410 |
> |
if (fp != stdin) { /* close input stream */ |
411 |
|
if (inspec[0] == '!') |
412 |
|
pclose(fp); |
413 |
|
else |
417 |
|
else |
418 |
|
funlockfile(fp); |
419 |
|
#endif |
420 |
+ |
if (!ok) { /* load failure? */ |
421 |
+ |
fprintf(stderr, "Error loading data from: %s\n", inspec); |
422 |
+ |
rmx_free(dnew); |
423 |
+ |
return(NULL); |
424 |
+ |
} |
425 |
+ |
/* undo exposure? */ |
426 |
+ |
if ((dnew->cexp[0] != 1.f) | |
427 |
+ |
(dnew->cexp[1] != 1.f) | (dnew->cexp[2] != 1.f)) { |
428 |
+ |
double cmlt[MAXCSAMP]; |
429 |
+ |
int i; |
430 |
+ |
cmlt[0] = 1./dnew->cexp[0]; |
431 |
+ |
cmlt[1] = 1./dnew->cexp[1]; |
432 |
+ |
cmlt[2] = 1./dnew->cexp[2]; |
433 |
+ |
if (dnew->ncomp > MAXCSAMP) { |
434 |
+ |
fprintf(stderr, "Excess spectral components in: %s\n", |
435 |
+ |
inspec); |
436 |
+ |
rmx_free(dnew); |
437 |
+ |
return(NULL); |
438 |
+ |
} |
439 |
+ |
for (i = dnew->ncomp; i-- > 3; ) |
440 |
+ |
cmlt[i] = cmlt[1]; |
441 |
+ |
rmx_scale(dnew, cmlt); |
442 |
+ |
setcolor(dnew->cexp, 1.f, 1.f, 1.f); |
443 |
+ |
} |
444 |
|
return(dnew); |
290 |
– |
loaderr: /* should report error? */ |
291 |
– |
if (inspec[0] == '!') |
292 |
– |
pclose(fp); |
293 |
– |
else |
294 |
– |
fclose(fp); |
295 |
– |
rmx_free(dnew); |
296 |
– |
return(NULL); |
445 |
|
} |
446 |
|
|
447 |
|
static int |
448 |
< |
rmx_write_ascii(const RMATRIX *rm, FILE *fp) |
448 |
> |
rmx_write_ascii(const double *dp, int nc, int len, FILE *fp) |
449 |
|
{ |
450 |
< |
int i, j, k; |
451 |
< |
#ifdef _WIN32 |
452 |
< |
_setmode(fileno(fp), _O_TEXT); |
453 |
< |
#endif |
306 |
< |
for (i = 0; i < rm->nrows; i++) { |
307 |
< |
for (j = 0; j < rm->ncols; j++) { |
308 |
< |
for (k = 0; k < rm->ncomp; k++) |
309 |
< |
fprintf(fp, " %.15e", rmx_lval(rm,i,j,k)); |
450 |
> |
while (len-- > 0) { |
451 |
> |
int k = nc; |
452 |
> |
while (nc-- > 0) |
453 |
> |
fprintf(fp, " %.7e", *dp++); |
454 |
|
fputc('\t', fp); |
311 |
– |
} |
312 |
– |
fputc('\n', fp); |
455 |
|
} |
456 |
< |
return(1); |
456 |
> |
return(fputc('\n', fp) != EOF); |
457 |
|
} |
458 |
|
|
459 |
|
static int |
460 |
< |
rmx_write_float(const RMATRIX *rm, FILE *fp) |
460 |
> |
rmx_write_float(const double *dp, int len, FILE *fp) |
461 |
|
{ |
462 |
< |
int i, j, k; |
321 |
< |
float val[100]; |
462 |
> |
float val; |
463 |
|
|
464 |
< |
if (rm->ncomp > 100) { |
465 |
< |
fputs("Unsupported # components in rmx_write_float()\n", stderr); |
466 |
< |
exit(1); |
326 |
< |
} |
327 |
< |
for (i = 0; i < rm->nrows; i++) |
328 |
< |
for (j = 0; j < rm->ncols; j++) { |
329 |
< |
for (k = rm->ncomp; k--; ) |
330 |
< |
val[k] = (float)rmx_lval(rm,i,j,k); |
331 |
< |
if (fwrite(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp) |
464 |
> |
while (len--) { |
465 |
> |
val = *dp++; |
466 |
> |
if (putbinary(&val, sizeof(float), 1, fp) != 1) |
467 |
|
return(0); |
468 |
< |
} |
468 |
> |
} |
469 |
|
return(1); |
470 |
|
} |
471 |
|
|
472 |
|
static int |
473 |
< |
rmx_write_double(const RMATRIX *rm, FILE *fp) |
473 |
> |
rmx_write_rgbe(const double *dp, int nc, int len, FILE *fp) |
474 |
|
{ |
475 |
< |
int i, j, k; |
476 |
< |
double val[100]; |
475 |
> |
COLR *scan; |
476 |
> |
int j; |
477 |
|
|
478 |
< |
if (rm->ncomp > 100) { |
479 |
< |
fputs("Unsupported # components in rmx_write_double()\n", stderr); |
480 |
< |
exit(1); |
478 |
> |
if ((nc != 1) & (nc != 3)) return(0); |
479 |
> |
scan = (COLR *)tempbuffer(sizeof(COLR)*len); |
480 |
> |
if (!scan) return(0); |
481 |
> |
|
482 |
> |
for (j = 0; j < len; j++, dp += nc) |
483 |
> |
if (nc == 1) |
484 |
> |
setcolr(scan[j], dp[0], dp[0], dp[0]); |
485 |
> |
else |
486 |
> |
setcolr(scan[j], dp[0], dp[1], dp[2]); |
487 |
> |
|
488 |
> |
return(fwritecolrs(scan, len, fp) >= 0); |
489 |
> |
} |
490 |
> |
|
491 |
> |
static int |
492 |
> |
rmx_write_spec(const double *dp, int nc, int len, FILE *fp) |
493 |
> |
{ |
494 |
> |
uby8 *scan; |
495 |
> |
SCOLOR scol; |
496 |
> |
int j, k; |
497 |
> |
|
498 |
> |
if (nc < 3) return(0); |
499 |
> |
scan = (uby8 *)tempbuffer((nc+1)*len); |
500 |
> |
if (!scan) return(0); |
501 |
> |
for (j = len; j--; dp += nc) { |
502 |
> |
for (k = nc; k--; ) |
503 |
> |
scol[k] = dp[k]; |
504 |
> |
scolor2scolr(scan+j*(nc+1), scol, nc); |
505 |
|
} |
506 |
< |
for (i = 0; i < rm->nrows; i++) |
348 |
< |
for (j = 0; j < rm->ncols; j++) { |
349 |
< |
for (k = rm->ncomp; k--; ) |
350 |
< |
val[k] = rmx_lval(rm,i,j,k); |
351 |
< |
if (fwrite(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp) |
352 |
< |
return(0); |
353 |
< |
} |
354 |
< |
return(1); |
506 |
> |
return(fwritescolrs(scan, nc, len, fp) >= 0); |
507 |
|
} |
508 |
|
|
509 |
+ |
/* Check if CIE XYZ primaries were specified */ |
510 |
|
static int |
511 |
< |
rmx_write_rgbe(const RMATRIX *rm, FILE *fp) |
511 |
> |
findCIEprims(const char *info) |
512 |
|
{ |
513 |
< |
COLOR *scan = (COLOR *)malloc(sizeof(COLOR)*rm->ncols); |
361 |
< |
int i, j; |
513 |
> |
RGBPRIMS prims; |
514 |
|
|
515 |
< |
if (scan == NULL) |
515 |
> |
if (!info) |
516 |
|
return(0); |
517 |
< |
for (i = 0; i < rm->nrows; i++) { |
518 |
< |
for (j = rm->ncols; j--; ) |
367 |
< |
setcolor(scan[j], rmx_lval(rm,i,j,0), |
368 |
< |
rmx_lval(rm,i,j,1), |
369 |
< |
rmx_lval(rm,i,j,2) ); |
370 |
< |
if (fwritescan(scan, rm->ncols, fp) < 0) { |
371 |
< |
free(scan); |
517 |
> |
info = strstr(info, PRIMARYSTR); |
518 |
> |
if (!info || !primsval(prims, info)) |
519 |
|
return(0); |
520 |
< |
} |
521 |
< |
} |
522 |
< |
free(scan); |
523 |
< |
return(1); |
520 |
> |
|
521 |
> |
return((prims[RED][CIEX] > .99) & (prims[RED][CIEY] < .01) && |
522 |
> |
(prims[GRN][CIEX] < .01) & (prims[GRN][CIEY] > .99) && |
523 |
> |
(prims[BLU][CIEX] < .01) & (prims[BLU][CIEY] < .01)); |
524 |
|
} |
525 |
|
|
526 |
< |
/* Write matrix to file type indicated by dtype */ |
526 |
> |
/* Finish writing header data with resolution and format, returning type used */ |
527 |
|
int |
528 |
< |
rmx_write(const RMATRIX *rm, int dtype, FILE *fp) |
528 |
> |
rmx_write_header(const RMATRIX *rm, int dtype, FILE *fp) |
529 |
|
{ |
530 |
< |
RMATRIX *mydm = NULL; |
384 |
< |
int ok = 1; |
385 |
< |
|
386 |
< |
if ((rm == NULL) | (fp == NULL)) |
530 |
> |
if (!rm | !fp || rm->ncols <= 0) |
531 |
|
return(0); |
388 |
– |
/* complete header */ |
532 |
|
if (rm->info) |
533 |
|
fputs(rm->info, fp); |
534 |
|
if (dtype == DTfromHeader) |
535 |
|
dtype = rm->dtype; |
536 |
< |
else if ((dtype == DTrgbe) & (rm->dtype == DTxyze)) |
536 |
> |
else if (dtype == DTrgbe && (rm->dtype == DTxyze || |
537 |
> |
findCIEprims(rm->info))) |
538 |
|
dtype = DTxyze; |
539 |
|
else if ((dtype == DTxyze) & (rm->dtype == DTrgbe)) |
540 |
|
dtype = DTrgbe; |
541 |
< |
if ((dtype != DTrgbe) & (dtype != DTxyze)) { |
542 |
< |
fprintf(fp, "NROWS=%d\n", rm->nrows); |
541 |
> |
if ((dtype == DTspec) & (rm->ncomp < 3)) |
542 |
> |
return(0); |
543 |
> |
|
544 |
> |
if (dtype == DTascii) /* set file type (WINDOWS) */ |
545 |
> |
SET_FILE_TEXT(fp); |
546 |
> |
else |
547 |
> |
SET_FILE_BINARY(fp); |
548 |
> |
/* write exposure? */ |
549 |
> |
if (rm->ncomp == 3 && (rm->cexp[RED] != rm->cexp[GRN]) | |
550 |
> |
(rm->cexp[GRN] != rm->cexp[BLU])) |
551 |
> |
fputcolcor(rm->cexp, fp); |
552 |
> |
else if (rm->cexp[GRN] != 1.f) |
553 |
> |
fputexpos(rm->cexp[GRN], fp); |
554 |
> |
/* matrix size? */ |
555 |
> |
if ((dtype > DTspec) | (rm->nrows <= 0)) { |
556 |
> |
if (rm->nrows > 0) |
557 |
> |
fprintf(fp, "NROWS=%d\n", rm->nrows); |
558 |
|
fprintf(fp, "NCOLS=%d\n", rm->ncols); |
400 |
– |
fprintf(fp, "NCOMP=%d\n", rm->ncomp); |
401 |
– |
} else if (rm->ncomp != 3) { /* wrong # components? */ |
402 |
– |
double cmtx[3]; |
403 |
– |
if (rm->ncomp != 1) /* only convert grayscale */ |
404 |
– |
return(0); |
405 |
– |
cmtx[0] = cmtx[1] = cmtx[2] = 1; |
406 |
– |
mydm = rmx_transform(rm, 3, cmtx); |
407 |
– |
if (mydm == NULL) |
408 |
– |
return(0); |
409 |
– |
rm = mydm; |
559 |
|
} |
560 |
< |
fputformat((char *)cm_fmt_id[dtype], fp); |
561 |
< |
fputc('\n', fp); |
562 |
< |
switch (dtype) { /* write data */ |
560 |
> |
if (dtype >= DTspec) { /* # components & split? */ |
561 |
> |
fputncomp(rm->ncomp, fp); |
562 |
> |
if (dtype == DTspec || (rm->ncomp > 3 && |
563 |
> |
memcmp(rm->wlpart, WLPART, sizeof(WLPART)))) |
564 |
> |
fputwlsplit(rm->wlpart, fp); |
565 |
> |
} else if ((rm->ncomp != 3) & (rm->ncomp != 1)) |
566 |
> |
return(0); /* wrong # components */ |
567 |
> |
if ((dtype == DTfloat) | (dtype == DTdouble)) |
568 |
> |
fputendian(fp); /* important to record */ |
569 |
> |
fputformat(cm_fmt_id[dtype], fp); |
570 |
> |
fputc('\n', fp); /* end of header */ |
571 |
> |
if ((dtype <= DTspec) & (rm->nrows > 0)) |
572 |
> |
fprtresolu(rm->ncols, rm->nrows, fp); |
573 |
> |
return(dtype); |
574 |
> |
} |
575 |
> |
|
576 |
> |
/* Write out matrix data (usually by row) */ |
577 |
> |
int |
578 |
> |
rmx_write_data(const double *dp, int nc, int len, int dtype, FILE *fp) |
579 |
> |
{ |
580 |
> |
switch (dtype) { |
581 |
|
case DTascii: |
582 |
< |
ok = rmx_write_ascii(rm, fp); |
416 |
< |
break; |
582 |
> |
return(rmx_write_ascii(dp, nc, len, fp)); |
583 |
|
case DTfloat: |
584 |
< |
ok = rmx_write_float(rm, fp); |
419 |
< |
break; |
584 |
> |
return(rmx_write_float(dp, nc*len, fp)); |
585 |
|
case DTdouble: |
586 |
< |
ok = rmx_write_double(rm, fp); |
422 |
< |
break; |
586 |
> |
return(putbinary(dp, sizeof(*dp)*nc, len, fp) == len); |
587 |
|
case DTrgbe: |
588 |
|
case DTxyze: |
589 |
< |
fprtresolu(rm->ncols, rm->nrows, fp); |
590 |
< |
ok = rmx_write_rgbe(rm, fp); |
591 |
< |
break; |
428 |
< |
default: |
429 |
< |
return(0); |
589 |
> |
return(rmx_write_rgbe(dp, nc, len, fp)); |
590 |
> |
case DTspec: |
591 |
> |
return(rmx_write_spec(dp, nc, len, fp)); |
592 |
|
} |
593 |
< |
ok &= (fflush(fp) == 0); |
594 |
< |
rmx_free(mydm); |
593 |
> |
return(0); |
594 |
> |
} |
595 |
> |
|
596 |
> |
/* Write matrix using file format indicated by dtype */ |
597 |
> |
int |
598 |
> |
rmx_write(const RMATRIX *rm, int dtype, FILE *fp) |
599 |
> |
{ |
600 |
> |
int ok = 0; |
601 |
> |
int i; |
602 |
> |
/* complete header */ |
603 |
> |
dtype = rmx_write_header(rm, dtype, fp); |
604 |
> |
if (dtype <= 0) |
605 |
> |
return(0); |
606 |
> |
#ifdef getc_unlocked |
607 |
> |
flockfile(fp); |
608 |
> |
#endif |
609 |
> |
if (dtype == DTdouble) /* write all at once? */ |
610 |
> |
ok = rmx_write_data(rm->mtx, rm->ncomp, |
611 |
> |
rm->nrows*rm->ncols, dtype, fp); |
612 |
> |
else /* else row by row */ |
613 |
> |
for (i = 0; i < rm->nrows; i++) { |
614 |
> |
ok = rmx_write_data(rmx_val(rm,i,0), rm->ncomp, |
615 |
> |
rm->ncols, dtype, fp); |
616 |
> |
if (!ok) break; |
617 |
> |
} |
618 |
> |
|
619 |
> |
if (ok) ok = (fflush(fp) == 0); |
620 |
> |
#ifdef getc_unlocked |
621 |
> |
funlockfile(fp); |
622 |
> |
#endif |
623 |
> |
if (!ok) fputs("Error writing matrix\n", stderr); |
624 |
|
return(ok); |
625 |
|
} |
626 |
|
|
631 |
|
RMATRIX *rid = rmx_alloc(dim, dim, n); |
632 |
|
int i, k; |
633 |
|
|
634 |
< |
if (rid == NULL) |
634 |
> |
if (!rid) |
635 |
|
return(NULL); |
636 |
< |
memset(rid->mtx, 0, sizeof(rid->mtx[0])*n*dim*dim); |
637 |
< |
for (i = dim; i--; ) |
636 |
> |
memset(rid->mtx, 0, array_size(rid)); |
637 |
> |
for (i = dim; i--; ) { |
638 |
> |
double *dp = rmx_lval(rid,i,i); |
639 |
|
for (k = n; k--; ) |
640 |
< |
rmx_lval(rid,i,i,k) = 1; |
640 |
> |
dp[k] = 1.; |
641 |
> |
} |
642 |
|
return(rid); |
643 |
|
} |
644 |
|
|
645 |
< |
/* Duplicate the given matrix */ |
645 |
> |
/* Duplicate the given matrix (may be unallocated) */ |
646 |
|
RMATRIX * |
647 |
|
rmx_copy(const RMATRIX *rm) |
648 |
|
{ |
649 |
|
RMATRIX *dnew; |
650 |
|
|
651 |
< |
if (rm == NULL) |
651 |
> |
if (!rm) |
652 |
|
return(NULL); |
653 |
< |
dnew = rmx_alloc(rm->nrows, rm->ncols, rm->ncomp); |
654 |
< |
if (dnew == NULL) |
653 |
> |
dnew = rmx_new(rm->nrows, rm->ncols, rm->ncomp); |
654 |
> |
if (!dnew) |
655 |
|
return(NULL); |
656 |
+ |
if (rm->mtx) { |
657 |
+ |
if (!rmx_prepare(dnew)) { |
658 |
+ |
rmx_free(dnew); |
659 |
+ |
return(NULL); |
660 |
+ |
} |
661 |
+ |
memcpy(dnew->mtx, rm->mtx, array_size(dnew)); |
662 |
+ |
} |
663 |
|
rmx_addinfo(dnew, rm->info); |
664 |
|
dnew->dtype = rm->dtype; |
665 |
< |
memcpy(dnew->mtx, rm->mtx, |
666 |
< |
sizeof(rm->mtx[0])*rm->ncomp*rm->nrows*rm->ncols); |
665 |
> |
copycolor(dnew->cexp, rm->cexp); |
666 |
> |
memcpy(dnew->wlpart, rm->wlpart, sizeof(dnew->wlpart)); |
667 |
|
return(dnew); |
668 |
|
} |
669 |
|
|
672 |
|
rmx_transpose(const RMATRIX *rm) |
673 |
|
{ |
674 |
|
RMATRIX *dnew; |
675 |
< |
int i, j, k; |
675 |
> |
int i, j; |
676 |
|
|
677 |
< |
if (rm == NULL) |
677 |
> |
if (!rm || !rm->mtx) |
678 |
|
return(0); |
679 |
+ |
if ((rm->nrows == 1) | (rm->ncols == 1)) { |
680 |
+ |
dnew = rmx_copy(rm); |
681 |
+ |
if (!dnew) |
682 |
+ |
return(NULL); |
683 |
+ |
dnew->nrows = rm->ncols; |
684 |
+ |
dnew->ncols = rm->nrows; |
685 |
+ |
return(dnew); |
686 |
+ |
} |
687 |
|
dnew = rmx_alloc(rm->ncols, rm->nrows, rm->ncomp); |
688 |
< |
if (dnew == NULL) |
688 |
> |
if (!dnew) |
689 |
|
return(NULL); |
690 |
|
if (rm->info) { |
691 |
|
rmx_addinfo(dnew, rm->info); |
692 |
|
rmx_addinfo(dnew, "Transposed rows and columns\n"); |
693 |
|
} |
694 |
|
dnew->dtype = rm->dtype; |
695 |
< |
for (i = dnew->nrows; i--; ) |
696 |
< |
for (j = dnew->ncols; j--; ) |
697 |
< |
for (k = dnew->ncomp; k--; ) |
698 |
< |
rmx_lval(dnew,i,j,k) = rmx_lval(rm,j,i,k); |
695 |
> |
copycolor(dnew->cexp, rm->cexp); |
696 |
> |
memcpy(dnew->wlpart, rm->wlpart, sizeof(dnew->wlpart)); |
697 |
> |
for (j = dnew->ncols; j--; ) |
698 |
> |
for (i = dnew->nrows; i--; ) |
699 |
> |
memcpy(rmx_lval(dnew,i,j), rmx_val(rm,j,i), |
700 |
> |
sizeof(double)*dnew->ncomp); |
701 |
|
return(dnew); |
702 |
|
} |
703 |
|
|
708 |
|
RMATRIX *mres; |
709 |
|
int i, j, k, h; |
710 |
|
|
711 |
< |
if ((m1 == NULL) | (m2 == NULL) || |
711 |
> |
if (!m1 | !m2 || !m1->mtx | !m2->mtx | |
712 |
|
(m1->ncomp != m2->ncomp) | (m1->ncols != m2->nrows)) |
713 |
|
return(NULL); |
714 |
|
mres = rmx_alloc(m1->nrows, m2->ncols, m1->ncomp); |
715 |
< |
if (mres == NULL) |
715 |
> |
if (!mres) |
716 |
|
return(NULL); |
717 |
|
i = rmx_newtype(m1->dtype, m2->dtype); |
718 |
|
if (i) |
722 |
|
for (i = mres->nrows; i--; ) |
723 |
|
for (j = mres->ncols; j--; ) |
724 |
|
for (k = mres->ncomp; k--; ) { |
725 |
< |
long double d = 0; |
725 |
> |
double d = 0; |
726 |
|
for (h = m1->ncols; h--; ) |
727 |
< |
d += rmx_lval(m1,i,h,k) * rmx_lval(m2,h,j,k); |
728 |
< |
rmx_lval(mres,i,j,k) = (double)d; |
727 |
> |
d += rmx_val(m1,i,h)[k] * rmx_val(m2,h,j)[k]; |
728 |
> |
rmx_lval(mres,i,j)[k] = d; |
729 |
|
} |
730 |
|
return(mres); |
731 |
|
} |
732 |
|
|
733 |
+ |
/* Element-wise multiplication (or division) of m2 into m1 */ |
734 |
+ |
int |
735 |
+ |
rmx_elemult(RMATRIX *m1, const RMATRIX *m2, int divide) |
736 |
+ |
{ |
737 |
+ |
int zeroDivides = 0; |
738 |
+ |
int i, j, k; |
739 |
+ |
|
740 |
+ |
if (!m1 | !m2 || !m1->mtx | !m2->mtx | |
741 |
+ |
(m1->ncols != m2->ncols) | (m1->nrows != m2->nrows)) |
742 |
+ |
return(0); |
743 |
+ |
if ((m2->ncomp > 1) & (m2->ncomp != m1->ncomp)) |
744 |
+ |
return(0); |
745 |
+ |
i = rmx_newtype(m1->dtype, m2->dtype); |
746 |
+ |
if (i) |
747 |
+ |
m1->dtype = i; |
748 |
+ |
else |
749 |
+ |
rmx_addinfo(m1, rmx_mismatch_warn); |
750 |
+ |
for (i = m1->nrows; i--; ) |
751 |
+ |
for (j = m1->ncols; j--; ) |
752 |
+ |
if (divide) { |
753 |
+ |
double d; |
754 |
+ |
if (m2->ncomp == 1) { |
755 |
+ |
d = rmx_val(m2,i,j)[0]; |
756 |
+ |
if (d == 0) { |
757 |
+ |
++zeroDivides; |
758 |
+ |
for (k = m1->ncomp; k--; ) |
759 |
+ |
rmx_lval(m1,i,j)[k] = 0; |
760 |
+ |
} else { |
761 |
+ |
d = 1./d; |
762 |
+ |
for (k = m1->ncomp; k--; ) |
763 |
+ |
rmx_lval(m1,i,j)[k] *= d; |
764 |
+ |
} |
765 |
+ |
} else |
766 |
+ |
for (k = m1->ncomp; k--; ) { |
767 |
+ |
d = rmx_val(m2,i,j)[k]; |
768 |
+ |
if (d == 0) { |
769 |
+ |
++zeroDivides; |
770 |
+ |
rmx_lval(m1,i,j)[k] = 0; |
771 |
+ |
} else |
772 |
+ |
rmx_lval(m1,i,j)[k] /= d; |
773 |
+ |
} |
774 |
+ |
} else { |
775 |
+ |
if (m2->ncomp == 1) { |
776 |
+ |
const double d = rmx_val(m2,i,j)[0]; |
777 |
+ |
for (k = m1->ncomp; k--; ) |
778 |
+ |
rmx_lval(m1,i,j)[k] *= d; |
779 |
+ |
} else |
780 |
+ |
for (k = m1->ncomp; k--; ) |
781 |
+ |
rmx_lval(m1,i,j)[k] *= rmx_val(m2,i,j)[k]; |
782 |
+ |
} |
783 |
+ |
if (zeroDivides) { |
784 |
+ |
rmx_addinfo(m1, "WARNING: zero divide(s) corrupted results\n"); |
785 |
+ |
errno = ERANGE; |
786 |
+ |
} |
787 |
+ |
return(1); |
788 |
+ |
} |
789 |
+ |
|
790 |
|
/* Sum second matrix into first, applying scale factor beforehand */ |
791 |
|
int |
792 |
|
rmx_sum(RMATRIX *msum, const RMATRIX *madd, const double sf[]) |
794 |
|
double *mysf = NULL; |
795 |
|
int i, j, k; |
796 |
|
|
797 |
< |
if ((msum == NULL) | (madd == NULL) || |
797 |
> |
if (!msum | !madd || !msum->mtx | !madd->mtx | |
798 |
|
(msum->nrows != madd->nrows) | |
799 |
|
(msum->ncols != madd->ncols) | |
800 |
|
(msum->ncomp != madd->ncomp)) |
801 |
|
return(0); |
802 |
< |
if (sf == NULL) { |
802 |
> |
if (!sf) { |
803 |
|
mysf = (double *)malloc(sizeof(double)*msum->ncomp); |
804 |
< |
if (mysf == NULL) |
804 |
> |
if (!mysf) |
805 |
|
return(0); |
806 |
|
for (k = msum->ncomp; k--; ) |
807 |
|
mysf[k] = 1; |
813 |
|
else |
814 |
|
rmx_addinfo(msum, rmx_mismatch_warn); |
815 |
|
for (i = msum->nrows; i--; ) |
816 |
< |
for (j = msum->ncols; j--; ) |
816 |
> |
for (j = msum->ncols; j--; ) { |
817 |
> |
const double *da = rmx_val(madd,i,j); |
818 |
> |
double *ds = rmx_lval(msum,i,j); |
819 |
|
for (k = msum->ncomp; k--; ) |
820 |
< |
rmx_lval(msum,i,j,k) += sf[k] * rmx_lval(madd,i,j,k); |
821 |
< |
|
822 |
< |
free(mysf); |
820 |
> |
ds[k] += sf[k] * da[k]; |
821 |
> |
} |
822 |
> |
if (mysf) |
823 |
> |
free(mysf); |
824 |
|
return(1); |
825 |
|
} |
826 |
|
|
830 |
|
{ |
831 |
|
int i, j, k; |
832 |
|
|
833 |
< |
if ((rm == NULL) | (sf == NULL)) |
833 |
> |
if (!rm | !sf || !rm->mtx) |
834 |
|
return(0); |
835 |
|
for (i = rm->nrows; i--; ) |
836 |
< |
for (j = rm->ncols; j--; ) |
836 |
> |
for (j = rm->ncols; j--; ) { |
837 |
> |
double *dp = rmx_lval(rm,i,j); |
838 |
|
for (k = rm->ncomp; k--; ) |
839 |
< |
rmx_lval(rm,i,j,k) *= sf[k]; |
840 |
< |
|
839 |
> |
dp[k] *= sf[k]; |
840 |
> |
} |
841 |
> |
if (rm->info) |
842 |
> |
rmx_addinfo(rm, "Applied scalar\n"); |
843 |
> |
/* XXX: should record as exposure for COLR and SCOLR types? */ |
844 |
|
return(1); |
845 |
|
} |
846 |
|
|
851 |
|
int i, j, ks, kd; |
852 |
|
RMATRIX *dnew; |
853 |
|
|
854 |
< |
if ((msrc == NULL) | (n <= 0) | (cmat == NULL)) |
854 |
> |
if (!msrc | (n <= 0) | !cmat || !msrc->mtx) |
855 |
|
return(NULL); |
856 |
|
dnew = rmx_alloc(msrc->nrows, msrc->ncols, n); |
857 |
< |
if (dnew == NULL) |
857 |
> |
if (!dnew) |
858 |
|
return(NULL); |
859 |
+ |
if (msrc->info) { |
860 |
+ |
char buf[128]; |
861 |
+ |
sprintf(buf, "Applied %dx%d component transform\n", |
862 |
+ |
dnew->ncomp, msrc->ncomp); |
863 |
+ |
rmx_addinfo(dnew, msrc->info); |
864 |
+ |
rmx_addinfo(dnew, buf); |
865 |
+ |
} |
866 |
|
dnew->dtype = msrc->dtype; |
867 |
|
for (i = dnew->nrows; i--; ) |
868 |
< |
for (j = dnew->ncols; j--; ) |
868 |
> |
for (j = dnew->ncols; j--; ) { |
869 |
> |
const double *ds = rmx_val(msrc,i,j); |
870 |
|
for (kd = dnew->ncomp; kd--; ) { |
871 |
|
double d = 0; |
872 |
|
for (ks = msrc->ncomp; ks--; ) |
873 |
< |
d += cmat[kd*msrc->ncomp + ks] * rmx_lval(msrc,i,j,ks); |
874 |
< |
rmx_lval(dnew,i,j,kd) = d; |
873 |
> |
d += cmat[kd*msrc->ncomp + ks] * ds[ks]; |
874 |
> |
rmx_lval(dnew,i,j)[kd] = d; |
875 |
|
} |
876 |
+ |
} |
877 |
|
return(dnew); |
878 |
|
} |
879 |
|
|
884 |
|
int i, j; |
885 |
|
RMATRIX *dnew; |
886 |
|
|
887 |
< |
if (cm == NULL) |
887 |
> |
if (!cm) |
888 |
|
return(NULL); |
889 |
|
dnew = rmx_alloc(cm->nrows, cm->ncols, 3); |
890 |
< |
if (dnew == NULL) |
890 |
> |
if (!dnew) |
891 |
|
return(NULL); |
892 |
|
dnew->dtype = DTfloat; |
893 |
|
for (i = dnew->nrows; i--; ) |
894 |
|
for (j = dnew->ncols; j--; ) { |
895 |
|
const COLORV *cv = cm_lval(cm,i,j); |
896 |
< |
rmx_lval(dnew,i,j,0) = cv[0]; |
897 |
< |
rmx_lval(dnew,i,j,1) = cv[1]; |
898 |
< |
rmx_lval(dnew,i,j,2) = cv[2]; |
896 |
> |
double *dp = rmx_lval(dnew,i,j); |
897 |
> |
dp[0] = cv[0]; |
898 |
> |
dp[1] = cv[1]; |
899 |
> |
dp[2] = cv[2]; |
900 |
|
} |
901 |
|
return(dnew); |
902 |
|
} |
908 |
|
int i, j; |
909 |
|
CMATRIX *cnew; |
910 |
|
|
911 |
< |
if (rm == NULL || rm->ncomp != 3) |
911 |
> |
if (!rm || !rm->mtx | (rm->ncomp == 2)) |
912 |
|
return(NULL); |
913 |
|
cnew = cm_alloc(rm->nrows, rm->ncols); |
914 |
< |
if (cnew == NULL) |
914 |
> |
if (!cnew) |
915 |
|
return(NULL); |
916 |
|
for (i = cnew->nrows; i--; ) |
917 |
|
for (j = cnew->ncols; j--; ) { |
918 |
< |
COLORV *cv = cm_lval(cnew,i,j); |
919 |
< |
cv[0] = (COLORV)rmx_lval(rm,i,j,0); |
920 |
< |
cv[1] = (COLORV)rmx_lval(rm,i,j,1); |
921 |
< |
cv[2] = (COLORV)rmx_lval(rm,i,j,2); |
918 |
> |
const double *dp = rmx_val(rm,i,j); |
919 |
> |
COLORV *cv = cm_lval(cnew,i,j); |
920 |
> |
switch (rm->ncomp) { |
921 |
> |
case 3: |
922 |
> |
setcolor(cv, dp[0], dp[1], dp[2]); |
923 |
> |
break; |
924 |
> |
case 1: |
925 |
> |
setcolor(cv, dp[0], dp[0], dp[0]); |
926 |
> |
break; |
927 |
> |
default: { |
928 |
> |
SCOLOR scol; |
929 |
> |
int k; |
930 |
> |
for (k = rm->ncomp; k--; ) |
931 |
> |
scol[k] = dp[k]; |
932 |
> |
scolor2color(cv, scol, rm->ncomp, rm->wlpart); |
933 |
> |
} break; |
934 |
> |
} |
935 |
|
} |
936 |
|
return(cnew); |
937 |
|
} |