Debian Codegraph @

libsrc/st/scaux.c

raw
1 /*===========================================================================
2 Copyright (C) 1995-2009 European Southern Observatory (ESO)
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of
7 the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public
15 License along with this program; if not, write to the Free
16 Software Foundation, Inc., 675 Massachusetts Ave, Cambridge,
17 MA 02139, USA.
18
19 Correspondence concerning ESO-MIDAS should be addressed as follows:
20 Internet e-mail: [email protected]
21 Postal address: European Southern Observatory
22 Data Management Division
23 Karl-Schwarzschild-Strasse 2
24 D 85748 Garching bei Muenchen
25 GERMANY
26 ===========================================================================*/
27
28 /*+++++++++++++++++++++ Module SCAUX +++++++++++++++++++++++++++++++++++++++
29 .LANGUAGE C
30 .IDENTIFICATION Module SCAUX.C
31 .AUTHOR Klaus Banse
32 .KEYWORDS Midas utility routines.
33 .ENVIRONMENT VMS and UNIX
34 .COMMENTS
35 holds conv_dat, conv_pix, get_byte, rddisk, wrdisk
36 .VERSION
37 090326 last modif
38
39 ------------------------------------------------------------------------*/
40
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <fileexts.h>
44 #include <osyparms.h>
45
46
47 #define AUX_READ 1
48 #define AUX_WRITE 2
49 #define AUX_GET 3
50
51 #define NO_LINES 512
52
53 static int wrkspace = -1;
54 static char *wrkptr;
55
56 int get_byte();
57
58 static struct FCT_STRUCT *fctpntr;
59
60
61 /*
62
63 */
64
65 int conv_dat(flg,imno,felem,size,actsize,newptr)
66
67 int flg /* IN: 1 or 2 for read or write */;
68 int imno /* IN: FCT entry number */;
69 int felem /* IN: first data pixel in input frame */;
70 int size /* IN: no. of data pixels to convert */;
71 int *actsize /* OUT: actual size (only for flg = AUX_GET */;
72 char **newptr /* IN/OUT: holds addresses of disk buffer/virtual memory */;
73
74 {
75 int status, ktimes, kpix, newbyte, oldbyte;
76 int newfmt, oldfmt, actsz, total;
77 register int nr, mm;
78
79 char *newbuf, *oldbuf;
80
81
82
83 fctpntr = FCT.ENTRIES + imno;
84
85 oldbyte = get_byte(fctpntr->FORMAT);
86 newbyte = get_byte(fctpntr->DATTYP);
87 status = ERR_NORMAL;
88
89
90 /* allocate virtual memory for working buffer */
91
92 if (wrkspace == -1)
93 {
94 wrkspace = NO_LINES * OUR_BLOCK_SIZE; /* this may be updated ... */
95 wrkptr = malloc((size_t)wrkspace);
96 if (wrkptr == (char *) 0) return ERR_MEMOUT;
97 }
98
99 kpix = wrkspace / oldbyte; /* no. of pixels in wrkspace */
100 ktimes = ((size - 1) / kpix) + 1;
101 if (ktimes == 1 ) kpix = size;
102
103 if (flg == AUX_WRITE) goto write_section;
104 total = 0;
105
106
107 /* if here for mapping, allocate virtual memory for data in new format */
108
109 if (flg == AUX_READ)
110 {
111 mm = (size * newbyte);
112 newptr[0] = malloc((size_t)mm);
113 if (newptr[0] == (char *) 0) return ERR_MEMOUT;
114
115 newptr[1] = newptr[0] + mm - 1;
116 }
117
118
119 /* here for reading in stuff */
120 /*******************************/
121
122 newbuf = newptr[0];
123 oldbuf = wrkptr;
124 newfmt = fctpntr->DATTYP;
125 oldfmt = fctpntr->FORMAT;
126
127
128 /* now read in from file and convert */
129
130 for (nr=0; nr<ktimes; nr++)
131 {
132 status = rddisk(imno,felem,kpix,&actsz,oldbuf);
133 if (status != ERR_NORMAL)
134 {
135 if (status != ERR_NODATA)
136 return status; /* real problem */
137 else
138 {
139 status = ERR_NORMAL; /* we reached end of data */
140 break; /* get out of loop */
141 }
142 }
143
144 conv_pix(newbuf,oldbuf,newfmt,oldfmt,actsz);
145 newbuf += (actsz*newbyte);
146 felem += actsz;
147 total += actsz;
148 size -= actsz;
149 if (size < kpix) kpix = size;
150 }
151
152 *actsize = total;
153 return status;
154
155
156 /* here for writing out stuff */
157 /*******************************/
158
159 write_section:
160
161 oldbuf = newptr[0];
162 newbuf = wrkptr;
163 oldfmt = fctpntr->DATTYP;
164 newfmt = fctpntr->FORMAT;
165
166
167 /* now convert back and write to file */
168
169 for (nr=0; nr<ktimes; nr++)
170 {
171 conv_pix(newbuf,oldbuf,newfmt,oldfmt,kpix);
172 status = wrdisk(imno,felem,kpix,newbuf);
173 if (status != ERR_NORMAL) return status;
174 oldbuf += (kpix*newbyte);
175 felem += kpix;
176 size -= kpix;
177 if (size < kpix) kpix = size;
178 }
179
180 return status;
181 }
182 /*
183
184 */
185
186 void conv_pix(newpntr,oldpntr,newfmt,oldfmt,size)
187 int newfmt, oldfmt, size;
188 char *newpntr, *oldpntr;
189
190 {
191 register int nr;
192
193 #if vms
194 unsigned char *outc, *inc; /* VMS compiler didn't like */
195 short int *outj, *inj; /* too many register pointers */
196 unsigned short int *outk, *ink;
197 int *outi, *ini;
198 float *outr, *inr;
199 double *outd, *ind;
200
201 #else
202 register unsigned char *outc, *inc;
203 register short int *outj, *inj;
204 register unsigned short int *outk, *ink;
205 register int *outi, *ini;
206 register float *outr, *inr;
207 register double *outd, *ind;
208 #endif
209
210
211 switch(oldfmt)
212 {
213 case D_I1_FORMAT: /* input is I*1 integer */
214 inc = (unsigned char *) oldpntr;
215
216 switch(newfmt)
217 {
218 case D_UI2_FORMAT: /* output is unsigned I*2 integer */
219 outk = (unsigned short int *) newpntr;
220 for (nr=0; nr<size; nr++)
221 *outk++ = (unsigned short int) *inc++;
222 break;
223
224 case D_I2_FORMAT: /* output is I*2 integer */
225 outj = (short int *) newpntr;
226 for (nr=0; nr<size; nr++)
227 *outj++ = (short int) *inc++;
228 break;
229
230 case D_I4_FORMAT: /* output is I*4 integer */
231 outi = (int *) newpntr;
232 for (nr=0; nr<size; nr++)
233 *outi++ = (int) *inc++;
234 break;
235
236 case D_R4_FORMAT: /* output is R*4 real */
237 outr = (float *) newpntr;
238 for (nr=0; nr<size; nr++)
239 *outr++ = (float) *inc++;
240 break;
241
242 case D_R8_FORMAT: /* output is R*8 real */
243 outd = (double *) newpntr;
244 for (nr=0; nr<size; nr++)
245 *outd++ = (double) *inc++;
246 break;
247
248 case D_I1_FORMAT: /* same format ... */
249 outc = (unsigned char *) newpntr;
250 for (nr=0; nr<size; nr++) *outc++ = *inc++;
251 }
252 break;
253
254 case D_I2_FORMAT: /* input is I*2 integer */
255 inj = (short int *) oldpntr;
256
257 switch(newfmt)
258 {
259 case D_I1_FORMAT: /* output is I*1 integer */
260 outc = (unsigned char *) newpntr;
261 for (nr=0; nr<size; nr++)
262 *outc++ = (unsigned char) *inj++;
263 break;
264
265 case D_UI2_FORMAT: /* output is unsigned I*2 integer */
266 outk = (unsigned short int *) newpntr;
267 for (nr=0; nr<size; nr++)
268 *outk++ = (unsigned short int) *inj++;
269 break;
270
271 case D_I4_FORMAT: /* output is I*4 integer */
272 outi = (int *) newpntr;
273 for (nr=0; nr<size; nr++)
274 *outi++ = (int ) *inj++;
275 break;
276
277 case D_R4_FORMAT: /* output is R*4 real */
278 outr = (float *) newpntr;
279 for (nr=0; nr<size; nr++)
280 *outr++ = (float) *inj++;
281 break;
282
283 case D_R8_FORMAT: /* output is R*8 real */
284 outd = (double *) newpntr;
285 for (nr=0; nr<size; nr++)
286 *outd++ = (double) *inj++;
287 break;
288
289 case D_I2_FORMAT: /* same format... */
290 outj = (short int *) newpntr;
291 for (nr=0; nr<size; nr++) *outj++ = *inj++;
292 }
293 break;
294
295 case D_UI2_FORMAT: /* input is unsigned I*2 integer */
296 ink = (unsigned short int *) oldpntr;
297
298 switch(newfmt)
299 {
300 case D_I1_FORMAT: /* output is I*1 integer */
301 outc = (unsigned char *) newpntr;
302 for (nr=0; nr<size; nr++)
303 *outc++ = (unsigned char) *ink++;
304 break;
305
306 case D_I2_FORMAT: /* output is signed I*2 integer */
307 outj = (short int *) newpntr;
308 for (nr=0; nr<size; nr++)
309 *outj++ = (short int) *ink++;
310 break;
311
312 case D_I4_FORMAT: /* output is I*4 integer */
313 outi = (int *) newpntr;
314 for (nr=0; nr<size; nr++)
315 *outi++ = (int ) *ink++;
316 break;
317
318 case D_R4_FORMAT: /* output is R*4 real */
319 outr = (float *) newpntr;
320 for (nr=0; nr<size; nr++)
321 *outr++ = (float) *ink++;
322 break;
323
324 case D_R8_FORMAT: /* output is R*8 real */
325 outd = (double *) newpntr;
326 for (nr=0; nr<size; nr++)
327 *outd++ = (double) *ink++;
328 break;
329
330 case D_UI2_FORMAT: /* same format... */
331 outk = (unsigned short int *) newpntr;
332 for (nr=0; nr<size; nr++) *outk++ = *ink++;
333 }
334 break;
335
336 case D_I4_FORMAT: /* input is I*4 integer */
337 ini = (int *) oldpntr;
338
339 switch(newfmt)
340 {
341 case D_I1_FORMAT: /* output is I*1 integer */
342 outc = (unsigned char *) newpntr;
343 for (nr=0; nr<size; nr++)
344 *outc++ = (unsigned char) *ini++;
345 break;
346
347 case D_I2_FORMAT: /* output is I*2 integer */
348 outj = (short int *) newpntr;
349 for (nr=0; nr<size; nr++)
350 *outj++ = (short int) *ini++;
351 break;
352
353 case D_UI2_FORMAT: /* output is unsigned I*2 integer */
354 outk = (unsigned short int *) newpntr;
355 for (nr=0; nr<size; nr++)
356 *outk++ = (unsigned short int) *ini++;
357 break;
358
359 case D_R4_FORMAT: /* output is R*4 real */
360 outr = (float *) newpntr;
361 for (nr=0; nr<size; nr++)
362 *outr++ = (float) *ini++;
363 break;
364
365 case D_R8_FORMAT: /* output is R*8 real */
366 outd = (double *) newpntr;
367 for (nr=0; nr<size; nr++)
368 *outd++ = (double) *ini++;
369 break;
370
371 case D_I4_FORMAT: /* same format... */
372 outi = (int *) newpntr;
373 for (nr=0; nr<size; nr++) *outi++ = *ini++;
374 }
375 break;
376
377 case D_R4_FORMAT: /* input is R*4 real */
378 inr = (float *) oldpntr;
379
380 switch(newfmt)
381 {
382 case D_I1_FORMAT: /* output is I*1 integer */
383 outc = (unsigned char *) newpntr;
384 for (nr=0; nr<size; nr++)
385 *outc++ = (unsigned char) *inr++;
386 break;
387
388 case D_I2_FORMAT: /* output is I*2 integer */
389 outj = (short int *) newpntr;
390 for (nr=0; nr<size; nr++)
391 *outj++ = (short int) *inr++;
392 break;
393
394 case D_UI2_FORMAT: /* output is unsigned I*2 integer */
395 outk = (unsigned short int *) newpntr;
396 for (nr=0; nr<size; nr++)
397 *outk++ = (unsigned short int) *inr++;
398 break;
399
400 case D_I4_FORMAT: /* output is I*4 integer */
401 outi = (int *) newpntr;
402 for (nr=0; nr<size; nr++)
403 *outi++ = (int) *inr++;
404 break;
405
406 case D_R8_FORMAT: /* output is R*8 real */
407 outd = (double *) newpntr;
408 for (nr=0; nr<size; nr++)
409 *outd++ = (double) *inr++;
410 break;
411
412 case D_R4_FORMAT: /* same format... */
413 outr = (float *) newpntr;
414 for (nr=0; nr<size; nr++) *outr++ = *inr++;
415 }
416 break;
417
418 case D_R8_FORMAT: /* input is double precision */
419 ind = (double *) oldpntr;
420
421 switch(newfmt)
422 {
423 case D_I1_FORMAT: /* output is I*1 integer */
424 outc = (unsigned char *) newpntr;
425 for (nr=0; nr<size; nr++)
426 *outc++ = (unsigned char) *ind++;
427 break;
428
429 case D_I2_FORMAT: /* output is I*2 integer */
430 outj = (short int *) newpntr;
431 for (nr=0; nr<size; nr++)
432 *outj++ = (short int) *ind++;
433 break;
434
435 case D_UI2_FORMAT: /* output is unsigned I*2 integer */
436 outk = (unsigned short int *) newpntr;
437 for (nr=0; nr<size; nr++)
438 *outk++ = (unsigned short int) *ind++;
439 break;
440
441 case D_I4_FORMAT: /* output is I*4 integer */
442 outi = (int *) newpntr;
443 for (nr=0; nr<size; nr++)
444 *outi++ = (int) *ind++;
445 break;
446
447 case D_R4_FORMAT: /* output is R*4 real */
448 outr = (float *) newpntr;
449 for (nr=0; nr<size; nr++)
450 *outr++ = (float ) *ind++;
451 break;
452
453 case D_R8_FORMAT: /* same format... */
454 outd = (double *) newpntr;
455 for (nr=0; nr<size; nr++) *outd++ = *ind++;
456 }
457 break;
458 }
459 }
460 /*
461
462 */
463
464 int get_byte(format)
465 int format;
466
467 {
468 switch(format)
469 {
470 case D_R4_FORMAT:
471 return (RR_SIZE);
472 case D_R8_FORMAT:
473 return (DD_SIZE);
474 case D_I4_FORMAT:
475 return (II_SIZE);
476 case D_I2_FORMAT:
477 case D_UI2_FORMAT:
478 return (JJ_SIZE);
479 default:
480 return (1); /* single byte */
481 }
482 }
483
484 /*
485
486 */
487
488 int rddisk(imno,felem,size,actsize,bufadr)
489
490 /*++++++++++++++++++++++++++++++++++++++++++++++++++
491 .PURPOSE
492 read data from disk frame into memory
493 .ALGORITHM
494 synchronous disk I/O
495 .RETURNS
496 return status ( 0 = o.k )
497 --------------------------------------------------------*/
498
499 int imno /* IN : file no. of data frame */;
500 int felem /* IN : 1st pixel to be accessed in data space */;
501 int size /* IN : number of data values (pixels) to be read */;
502 int *actsize /* OUT: actual no. of pixels read */;
503 char *bufadr; /* IN: address of data buffer */
504
505 {
506 int nbytes, pixblock;
507 int status, chanl;
508 int mm;
509
510 unsigned int difpix, nopix, firstpix;
511 unsigned int uu, fvbn, lovbn, difbytes, bytesize;
512
513 size_t fsize;
514
515
516
517 fctpntr = FCT.ENTRIES + imno;
518
519 chanl = fctpntr->IOCHAN; /* I/O channel of frame */
520 nbytes = fctpntr->NOBYTE; /* no. of bytes per pixel */
521 pixblock = fctpntr->PIXPBL; /* no. of pixels per block */
522 fsize = fctpntr->SIZE;
523 if (size < 1) /* if no size given, get all pixels */
524 nopix = fsize;
525 else
526 nopix = size;
527
528 if ( (felem+nopix-1) > fsize)
529 {
530 nopix = fsize - felem + 1;
531 if (nopix < 1)
532 {
533 if (nopix == 0)
534 return ERR_NODATA; /* we're at the end */
535 else
536 return ERR_INPINV;
537 }
538 }
539
540
541 /* get first virtual block + real start pixel */
542
543 uu = (unsigned int)felem * (unsigned int)nbytes;
544 fvbn = (uu - 1) / OUR_BLOCK_SIZE; /* added to 1. data block */
545 firstpix = fvbn*pixblock + 1; /* first pixel accessible */
546 difpix = felem - firstpix; /* difference to 1. required pixel */
547
548
549 /* calculate starting block */
550
551 lovbn = fctpntr->STBLOK + fvbn; /* 1. data block + offset = 1. block to map */
552 bytesize = nopix * nbytes; /* total no. of bytes we have to provide */
553
554
555 /* either we have to read two blocks */
556
557 if (difpix > 0)
558 {
559 char temp[OUR_BLOCK_SIZE];
560
561 difbytes = difpix * nbytes;
562 status = OSY_RVB(chanl,temp,(unsigned int)OUR_BLOCK_SIZE,(int)lovbn);
563 if (status != ERR_NORMAL) return status;
564
565 mm = OUR_BLOCK_SIZE - difbytes;
566 if (mm > bytesize) mm = bytesize;
567 (void) memcpy(bufadr,&temp[difbytes],(size_t)mm);
568
569 bytesize -= mm;
570 if (bytesize > 0)
571 status = OSY_RVB(chanl,bufadr+mm,bytesize,(int)(lovbn+1));
572 }
573
574
575 /* or we get it all in one go */
576
577 else
578 status = OSY_RVB(chanl,bufadr,bytesize,(int)lovbn);
579
580
581 *actsize = (int) nopix;
582 return status;
583 }
584 /*
585
586 */
587
588 int wrdisk(imno,felem,size,bufadr)
589
590 /*++++++++++++++++++++++++++++++++++++++++++++++++++
591 .PURPOSE
592 write data from memory into disk frame
593 .ALGORITHM
594 synchronous disk I/O
595 .RETURNS
596 return status ( 0 = o.k )
597 --------------------------------------------------------*/
598
599 int imno /* IN : file no. of data frame */;
600 int felem /* IN : 1st pixel to be accessed in data space */;
601 int size /* IN : no. of data values (pixels) to be written */;
602 char *bufadr; /* IN: address of data buffer */
603
604 {
605 int nbytes, pixblock;
606 int status, chanl;
607 int mm;
608
609 unsigned int difpix, nopix, firstpix;
610 unsigned int uu, fvbn, lovbn, difbytes, bytesize;
611
612 size_t fsize;
613
614
615
616 fctpntr = FCT.ENTRIES + imno;
617
618 chanl = fctpntr->IOCHAN; /* I/O channel of frame */
619 nbytes = fctpntr->NOBYTE; /* no. of bytes per pixel */
620 pixblock = fctpntr->PIXPBL; /* no. of pixels per block */
621 fsize = fctpntr->SIZE;
622 if (size < 1) /* if no size given, get all pixels */
623 nopix = fsize;
624 else
625 nopix = size;
626
627 if ( (felem+nopix-1) > fsize )
628 {
629 nopix = fsize - felem + 1;
630 if (nopix < 1) return ERR_INPINV;
631 }
632
633
634 /* get first virtual block + real start pixel */
635
636 uu = (unsigned int)felem * (unsigned int)nbytes;
637 fvbn = (uu - 1) / OUR_BLOCK_SIZE; /* added to 1. data block */
638 firstpix = (fvbn * pixblock) + 1; /* first pixel accessible */
639 difpix = felem - firstpix; /* difference to 1. required pixel */
640
641
642 /* calculate starting block */
643
644 lovbn = fctpntr->STBLOK + fvbn; /* 1. data block + offset = 1. block to map */
645 bytesize = nopix * nbytes; /* total no. of bytes we have to write */
646
647
648 /* either we have to read + write first block separately */
649
650 if (difpix > 0)
651 {
652 char temp[OUR_BLOCK_SIZE];
653
654 difbytes = difpix * nbytes;
655 status = OSY_RVB(chanl,temp,(unsigned int)OUR_BLOCK_SIZE,(int)lovbn);
656 if (status != ERR_NORMAL) return status;
657
658 mm = OUR_BLOCK_SIZE - difbytes;
659 if (mm > bytesize) mm = bytesize;
660 (void) memcpy(&temp[difbytes],bufadr,(size_t)mm);
661 status = OSY_WVB(chanl,temp,(unsigned int)OUR_BLOCK_SIZE,(int)lovbn);
662 if (status != ERR_NORMAL) return status;
663
664 if (bytesize > mm)
665 {
666 lovbn ++;
667 bufadr += mm;
668 bytesize -= mm;
669
670 #if vms
671 nvb = bytesize / OUR_BLOCK_SIZE; /* get no. of full blocks */
672 if (nvb > 0)
673 {
674 mm = nvb * OUR_BLOCK_SIZE;
675 status = OSY_WVB(chanl,bufadr,(unsigned int)mm,(int)lovbn);
676 if (status != ERR_NORMAL) return status;
677 lovbn += nvb;
678 bufadr += mm;
679 bytesize -= mm;
680 }
681
682 if (bytesize > 0) /* we must be in [0,OUR_BLOCK_SIZE-1] ... */
683 {
684 status = OSY_RVB(chanl,temp,(unsigned int)OUR_BLOCK_SIZE,(int)lovbn);
685 if (status != ERR_NORMAL) return status;
686
687 (void) memcpy(temp,bufadr,(size_t)bytesize);
688 status = OSY_WVB(chanl,temp,(unsigned int)OUR_BLOCK_SIZE,(int)lovbn);
689 }
690 #else
691
692 status = OSY_WVB(chanl,bufadr,bytesize,(int)lovbn);
693
694 #endif
695 }
696 }
697
698
699 /* or we send it all in one go */
700
701 else
702 status = OSY_WVB(chanl,bufadr,bytesize,(int)lovbn);
703
704
705 return status;
706 }