1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
|
.help imio May83 "Image I/O Routines"
.sh
The Image Header
The major difference between the prototype IMIO interface, and the final
interface, concerns the way in which the image header is implemented and
accessed. In the prototype version, we will simply read the entire header
into core and access it as an ordinary (dynamically allocated) structure.
.nf
ptr = immap (fname, mode, hdrsize/hdrptr)
imunmap (hdrptr)
.fi
The final resolution of how image headers are implemented depends on how
we decide to implement virtual structures in the spp language. The immap
calls, and the techniques used to access the fields of the image header,
can be expected to change.
.sh
Pixel I/O
The calling sequences for the i/o routines, described below, hopefully will
not have to be changed. We will eventually add GETPIX and PUTPIX statements
to the subset preprocessor language, to automatically generate the appropriate
low level calls.
A generic, polymorphic GETPIX or PUTPIX statement is translated into a
reference to a low level Fortran function. The transformation is governed
by the following subprogram name generating function:
.rj (108 total)
GETPIX, PUTPIX --> im[gp][pls][123][silrdx]
.ks
.nf
For example (get, type real):
ptr = imgp1r (hdrptr, x, npix) # get pixels
ptr = imgp2r (hdrptr, x, y, npix)
ptr = imgp3r (hdrptr, x, y, z, npix)
ptr = imgl1r (hdrptr) # get line
ptr = imgl2r (hdrptr, y)
ptr = imgl3r (hdrptr, y, z)
ptr = imgs1r (hdrptr, x1, x2) # get section
ptr = imgs2r (hdrptr, x1, x2, y1, y2)
ptr = imgs3r (hdrptr, x1, x2, y1, y2, z1, z2)
.fi
.ke
The IM?P?? procedures access a list of pixels, the coordinates of which
are given by the X, Y, Z, etc. arrays given as arguments. Note that random
access of individual pixels is provided as a special case (npix=1).
The IM?L?? routines access the lines of an image, and the IM?S?? routines
operate on general, but connected, subsections of images.
.sh
Restrictions Imposed by the Initial Prototype:
IMMAP, IMMAPNC, IMUNMAP will be implemented for image headers that are
simple binary structures (not self describing), subject to the restriction
that a file may contain only a single header. An arbitrary selection of user
defined fields will follow the standard header. The entire header will
be read into core and accessed as a simple incore structure.
The pixels, and other variable size image substructures, will be stored
in separate files, as in the general plan. All of the standard data types
will be implemented in the disk space. The initial implementation will
support only type REAL pixels in program space.
The following i/o routines will be implemented in the first release of
the prototype:
.rj (12 total)
im[gp][sc][123][r]
In words, we will be able to read and write lines and sections, with the
applications program manipulating type REAL pixels internally. The full
range of data types will be permitted in the image file as stored on disk.
Up to three dimensional images are permitted.
.sh
IMSET Options
The prototype need not provide multiple buffering and boundary extension
initially.
.sh
Implementation
Little effort should be made to make the prototype optimal. All
buffering will be locally allocated, and data will be copied to and from
the FIO buffers (the FIO buffers will not be directly accessed). Special
cases will not be optimized. The most general entry points are IMGGSR
and IMPGSR (get/put general section). Initially, all of the other entry
points can be defined in terms of these.
.ks
.nf
Structure of the input procedures (type REAL):
imgl1r
imgl2r
imgl3r
imgs1r
imgs2r
imgs3r
imggsr
imggsc
imgibf
imopsf
calloc
imcssz
realloc
malloc
mfree
imsslv
imrdpx
imsoob
imnote
seek
read
imflip
imupkr
(datatype dependent) | (datatype independent)
.fi
.ke
The output procedures are structured somewhat differently, because the
transfer of a section occurs sometime after a "put section" returns,
rather than immediately as in the input procedures. Since the output
is buffered for a delayed write, we must have an IMFLUSH entry point, and
IMUNMAP must flush the output buffer before unmapping an image.
.ks
.nf
Structure of the output procedures (type REAL):
impl1r
impl2r
impl3r
imps1r imunmap
imps2r |
imps3r |
impgsr |
imflush
imflsr
imflsh
imflip
imwrpx
imsoob
imnote
imwrite
fstatus
seek
write
impakr
imgobf
imopsf
calloc
imcssz
realloc
malloc
mfree
(datatype dependent) | (datatype independent)
.fi
.ke
.sh
Semicode for the Basic I/O Routines
The IMGGS? and IMPGS? procedures get and put general N-dimensional
image sections of a specific datatype. There is no intrinsic limit on
the maximum number of dimensions, and the full range (8) of disk datatypes
are easily supported. The subscript for a particular dimension may run
either forward or backward. The semicode is written generically, allowing
code to be machine generated for all program space datatypes (6).
We do not address the problems of boundary extension and multiple buffering
here, but these features can be easily added in the future. This version
of IMIO assumes that pixels are stored on disk in line storage mode, with
no interlacing of bands.
IMGGS? gets a general section, and converts it to the datatype indicated
by the type suffix.
.ks
.nf
pointer procedure imggs$t (imdes, vs, ve)
imdes pointer to image descriptor structure
vs,ve coordinates of starting and ending points
begin
bp = imggsc (imdes, vs, ve, TY_PIXEL, totpix)
if (imdes.pixtype != TY_PIXEL)
call imupk$t (*bp, *bp, totpix, imdes.pixtype)
return (coerce (bp, TY_CHAR, TY_PIXEL))
end
.fi
.ke
IMGGSC gets a general section from an imagefile into a buffer. Upon
exit, the buffer contains the requested section, with the pixels still
in the same datatype they were in the imagefile. The buffer is made
large enough to accommodate the pixels in either datatype.
.ks
.nf
pointer procedure imggsc (imdes, vs, ve, dtype, totpix)
imdes pointer to image descriptor structure
vs,ve coordinates of starting and ending points
dtype datatype of pixels required by calling program
bp pointer to CHAR buffer to hold pixels
begin
# Get an (input) buffer to put the pixels into. Prepare the
# section descriptor vectors V, VINC.
bp = imgibf (imdes, vs, ve, dtype)
call imsslv (imdes, vs, ve, v, vinc, npix)
# Extract the pixels. IMRPIX reads a contiguous array of
# pixels into the buffer at the specified offset, incrementing
# the offset when done. The pixels are type converted if
# necessary.
offset = 0
repeat {
call imrdpx (imdes, *(bp+offset), v, npix)
if (vinc[1] < 0)
call imflip (*(bp+offset), npix, sizeof(imdes.pixtype))
offset = offset + npix
for (d=2; d <= imdes.ndim; d=d+1) {
v[d] += vinc[d]
if ((v[d] - ve[d] == vinc[d]) && d < imdes.ndim)
v[d] = vs[d]
else {
d = 0
break
}
}
} until (d >= imdes.ndim)
totpix = offset
return (bp)
end
.fi
.ke
Prepare the section descriptor vectors V and VINC. V is a vector specifying
the coordinates at which the next i/o transfer will take place. VINC is
a vector specifying the loop step size.
.ks
.nf
procedure imsslv (imdes, vs, ve, v, vinc, npix)
begin
# Determine the direction in which each dimension is to be
# traversed.
do i = 1, imdes.ndim
if (vs[i] <= ve[i])
vinc[i] = 1
else
vinc[i] = -1
# Initialize the extraction vector (passed to IMRDS? to read a
# contiguous array of pixels). Compute length of a line.
do i = 1, imdes.ndim
v[i] = vs[i]
if (vs[1] > ve[1]) {
v[1] = ve[1]
npix = vs[1] - ve[1] + 1
} else
npix = ve[1] - vs[1] + 1
end
.fi
.ke
The put-section procedure must write the contents of the output buffer
to the image, using the section parameters saved during the previous call.
The new section parameters are then saved, and the buffer pointer is
returned to the calling program. The calling program subsequently fills
the buffer, and the sequence repeats.
.ks
.nf
pointer procedure impgs$t (imdes, vs, ve)
imdes pointer to image descriptor structure
vs,ve coordinates of starting and ending points
begin
# Flush the output buffer, if appropriate. IMFLUSH calls
# one of the IMFLS? routines, which write out the section.
call imflush (imhdr)
# Get an (output) buffer to put the pixels into. Save the
# section parameters in the image descriptor. Save the epa
# of the typed flush procedure in the image descriptor.
bp = imgobf (imdes, vs, ve, TY_PIXEL)
imdes.flush_epa = loc (imfls$t)
return (bp)
end
.fi
.ke
Flush the output buffer, if a put procedure has been called, and the
buffer has not yet been flushed. The output buffer is flushed automatically
whenever a put procedure is called, when an image is unmapped, or when
the applications program calls IMFLUSH.
.ks
.nf
procedure imfls$t (imdes)
begin
# Ignore the flush request if the output buffer has already been
# flushed.
if (imdes.flush == YES) {
bdes = imdes.obdes
bp = bdes.bufptr
# Convert datatype of pixels, if necessary, and flush buffer.
if (imdes.pixtype != TY_PIXEL)
call impak$t (*bp, *bp, bdes.npix, imdes.pixtype)
call imflsh (imdes)
imdes.flush = NO
}
end
.fi
.ke
.ks
.nf
procedure imflsh (imdes)
begin
# Determine the direction in which each dimension is to be
# traversed.
bdes = imdes.obdes
call imsslv (imdes, bdes.vs, bdes.ve, v, vinc, npix)
# Write out the pixels. IMWRPX writes a contiguous array of
# pixels at the specified offset.
offset = 0
repeat {
if (vinc[1] < 0)
call imflip (*(bp+offset), npix, sizeof(imdes.pixtype))
call imwrpx (imdes, *(bp+offset), v, npix)
offset = offset + npix
for (d=2; d <= imdes.ndim; d=d+1) {
v[d] += vinc[d]
if ((v[d] - ve[d] == vinc[d]) && d < imdes.ndim)
v[d] = vs[d]
else {
d = 0
break
}
}
} until (d >= imdes.ndim)
end
.fi
.ke
Read a contiguous array of NPIX pixels, starting at the point defined by
the vector V, into the callers buffer.
.ks
.nf
procedure imrdpx (imdes, buf, v, npix)
begin
# Check that the access does not reference out of bounds.
if (imsoob (imdes, v, npix))
call imerr (imname, subscript_out_of_range)
# Seek to the point V in the pixel storage file. Compute size
# of transfer.
call seek (imdes.pfd, imnote (imdes, v))
nchars = npix * sizeof (imdes.pixtype)
# Read in the data.
if (read (imdes.pfd, buf, nchars, junk) != nchars)
call imerr (imname, missing_pixels)
end
.fi
.ke
Write a contiguous array of NPIX pixels, starting at the point defined by
the vector V, into the pixel storage file.
.ks
.nf
procedure imwrpx (imdes, buf, v, npix)
begin
# Check that the access does not reference out of bounds.
if (imsoob (imdes, v, npix))
call imerr (imname, subscript_out_of_range)
# Seek to the point V in the pixel storage file. Note that
# when writing to a new image, the next transfer may occur
# at a point beyond the current end of file. If so, write
# out zeros until the desired offset (which is in bounds)
# is reached.
file_offset = imnote (imdes, v)
if (file_offset > imdes.file_size)
[write zeros until the desired offset is reached]
else
call seek (imdes.pfd, file_offset)
# Compute size of transfer. If transferring an entire line,
# increase size of transfer to the physical line length,
# to avoid having to enblock the data. NOTE: buffer must
# be large enough to guarantee no memory violation.
if (v[1] == 1 && npix == imdes.len[1])
nchars = imdes.physlen[1] * sizeof (imdes.pixtype)
else
nchars = npix * sizeof (imdes.pixtype)
call write (imdes.pfd, buf, nchars)
imdes.file_size = max (imdes.file_size, file_offset+nchars)
end
.fi
.ke
IMNOTE computes the physical offset of a particular pixel in the
pixel storage file. If the disk datatype is UBYTE, this is the offset
of the char containing the subscripted byte.
.ks
.nf
long procedure imnote (imdes, v)
begin
pixel_offset = v[1]
for (i=2; i <= imdes.ndim; i=i+1)
pixel_offset += (v[i]-1) * imdes.physlen[i]
char_offset0 = (pixel_offset-1) * sizeof (imdes.pixtype)
return (imdes.pixoff + char_offset0)
end
.fi
.ke
Convert a vector of any datatype to type PIXEL ($t). The input and
output vectors may be the same, without loss of data. The input and
output datatypes may be the same, in which case no conversion is
performed.
.ks
.nf
procedure imupk$t (a, b, npix, dtype)
begin
switch (dtype) {
case TY_USHORT:
call achtu$t (a, b, npix)
case TY_SHORT:
call achts$t (a, b, npix)
case TY_INT:
call achti$t (a, b, npix)
case TY_LONG:
call achtl$t (a, b, npix)
case TY_REAL:
call achtr$t (a, b, npix)
case TY_DOUBLE:
call achtd$t (a, b, npix)
case TY_COMPLEX:
call achtx$t (a, b, npix)
default:
call syserr (unknown_datatype_in_imagefile)
}
end
.fi
.ke
Convert a vector of type PIXEL ($t) to any datatype. The input and
output vectors may be the same, without loss of data. The input and
output datatypes may be the same, in which case no conversion is
performed.
.ks
.nf
procedure impak$t (a, b, npix, dtype)
begin
switch (dtype) {
case TY_USHORT:
call acht$tu (a, b, npix)
case TY_SHORT:
call acht$ts (a, b, npix)
case TY_INT:
call acht$ti (a, b, npix)
case TY_LONG:
call acht$tl (a, b, npix)
case TY_REAL:
call acht$tr (a, b, npix)
case TY_DOUBLE:
call acht$td (a, b, npix)
case TY_COMPLEX:
call acht$tx (a, b, npix)
default:
call syserr (unknown_datatype_in_imagefile)
}
end
.fi
.ke
.sh
Data Structure Management
When an image is mapped, buffer space is allocated for a copy of
the image header, and for the image descriptor (used by IMIO while an
image is mapped). When the first i/o transfer is done on an image,
either an input or an output data buffer will be created. The size of
this buffer is governed by the size of the transfer, and by the datatypes
of the pixels on disk and in program space.
If a new image is being written, the pixel storage file is created at
the time of the first PUTPIX operation. The physical characteristics
of the new image, defined by the image header of the new image, are
unalterable once the first i/o operation has occurred. Accordingly,
the number of dimensions, length of the dimensions, datatype of the
pixels on disk, and so on must be set (in the image header structure)
before writing to the new image.
The only exception to this rule may be the addition of new lines to a
two dimensional image stored in line storage mode, or the addition of
new bands to a multiband image stored in band sequential (noninterlaced)
mode. It is not always possible to modify the dimensions or size of
an existing image.
It is possible to preallocate space for an image (using FALOC). This
may result in a more nearly contiguous file, and may make writing a
new image slightly more efficient, since it will not be necessary
to write blocks of zeros in IMPGS?. Preallocation will occur
automatically in systems where it is desirable.
.sh
Pixel Buffer Management
There may be any number of input buffers per image, but only a single
output buffer. By default there is only a single input buffer. The input
and output buffers are distinct: the same buffer is never used for both
input and output (unlike FIO).
The size of a buffer may range from one pixel, to the entire image (or
larger if boundary extension is in use). If multiple buffers are in use,
all buffers do not have to be the same size. The size of a buffer may
vary from one GETPIX or PUTPIX call to the next.
If multiple input buffers are in use, buffers are allocated in a strictly
round robin fashion, one per GETPIX call. Several buffers may contain
data from the same part of the image. Once the desired number of buffers
have been filled, a buffer "goes away" with each subsequent GETPIX call.
IMGIBF gets an input buffer. When called to get a line or section,
the vectors VS and VE specify the subsection to be extracted.
This information is saved in the buffer descriptor, along with the
datatype of the pixels and the dimension of the section.
When IMGIBF is called to get a list of pixels, VS and VE would have to be
replaced by a set of NPIX such vectors, to fully specify the section.
It is impractical to save this much information in the buffer descriptor,
so when creating a buffer to contain a list of pixels, VS and VE are faked
to indicate a one dimensional section of the appropriate size.
.ks
.nf
pointer procedure imgibf (imdes, vs, ve, dtype)
imdes image descriptor
vs,ve define the number of pixels to be buffered
dtype the datatype of the pixels in the program
begin
# If first input transfer, allocate and initialize array of
# input buffer descriptors.
if (imdes.ibdes == NULL) {
call imopsf (imdes)
call calloc (imdes.ibdes, LEN_BDES * imdes.nbufs, TY_STRUCT)
}
# Compute pointer to the next input buffer descriptor.
# Increment NGET, the count of the number of GETPIX calls.
bdes = &imdes.ibdes [mod (imdes.nget, imdes.nbuf) + 1]
imdes.nget += 1
# Compute the size of the buffer needed. Check buffer
# descriptor to see if the old buffer is the right size.
# If so, use it, otherwise make a new one.
nchars = imcssz (imdes, vs, ve, dtype)
if (nchars < bdes.bufsize)
call realloc (bdes.bufptr, nchars, TY_CHAR)
else if (nchars > bdes.bufsize) {
call mfree (bdes.bufptr, TY_CHAR)
call malloc (bdes.bufptr, nchars, TY_CHAR)
}
# Save section coordinates, datatype in buffer descriptor, and
# return buffer pointer to calling program.
bdes.bufsize = nchars
bdes.dtype = dtype
bdes.npix = totpix
do i = 1, imdes.ndim {
bdes.vs[i] = vs[i]
bdes.ve[i] = ve[i]
}
return (coerce (bdes.bufptr, TY_CHAR, dtype)
end
.fi
.ke
.ks
.nf
pointer procedure imgobf (imdes, vs, ve, dtype)
imdes image descriptor
vs,ve define the number of pixels to be buffered
dtype the datatype of the pixels in the program
begin
# If first write, and if new image, create pixel storage file,
# otherwise open pixel storage file. Allocate and initialize
# output buffer descriptor.
if (imdes.obdes == NULL) {
call imopsf (imdes)
call calloc (imdes.obdes, LEN_BDES, TY_STRUCT)
}
bdes = imdes.obdes
# Compute the size of buffer needed. Add a few extra chars
# to guarantee that there won't be a memory violation when
# writing a full physical length line.
nchars = imcssz (imdes, vs, ve, dtype) +
(imdes.physlen[1] - imdes.len[1]) * sizeof (imdes.pixtype)
if (nchars < bdes.bufsize)
call realloc (bdes.bufptr, nchars, TY_CHAR)
else if (nchars > bdes.bufsize) {
call mfree (bdes.bufptr, TY_CHAR)
call malloc (bdes.bufptr, nchars, TY_CHAR)
}
# Save section coordinates, datatype of pixels in buffer
# descriptor, and return buffer pointer to calling program.
bdes.bufsize = nchars
bdes.dtype = dtype
bdes.npix = totpix
do i = 1, imdes.ndim {
bdes.vs[i] = vs[i]
bdes.ve[i] = ve[i]
}
return (coerce (bdes.bufptr, TY_CHAR, dtype)
end
.fi
.ke
Given two vectors describing the starting and ending coordinates
of an image section, compute and return the amount of storage needed
to contain the section. Sufficient storage must be allocated to
hold the largest datatype pixels which will occupy the buffer.
.ks
.nf
long procedure imcssz (imdes, vs, ve, dtype)
begin
pix_size = max (sizeof(imdes.pixtype), sizeof(dtype))
npix = 0
do i = 1, imdes.ndim
if (vs[i] <= ve[i])
npix *= ve[i] - vs[i] + 1
else
npix *= vs[i] - ve[i] + 1
return (npix * pix_size)
end
.fi
.ke
.sh
Mapping and unmapping Image Structures
An imagefile must be "mapped" to an image structure before the
structure can be accessed. The map operation associates a file with
a defined structure.
The IMMAP procedure must allocate a buffer for the image header
structure, and for the image descriptor structure. If an existing
imagefile is being mapped, the header is copied into memory from
the imagefile. If a new image is being mapped, the header structure
is allocated and initialized.
If an image is being mapped as a "new copy", a new header
structure is created which is a copy of the header of an image which
has already been mapped. The entire image header, including any
application specific fields, is copied.
After copying an image header for a NEW_COPY image, the header field
containing the name of the pixel storage file is cleared. A "new copy"
image structure does not inherit any pixels. Any similar substructures
which describe the attributes of the pixels (i.e., the blank pixel
list, the histogram) must also be initialized.
Note that the "image descriptor" buffer allocated below actually
contains the image descriptor, followed by the standard image header
(at offset IMHDR_OFF), followed by any user fields. If an existing
image structure is being mapped, the caller supplies the length of
the user area of the header as the third argument to IMMAP.
IMMAP returns a pointer to the first field of the standard header
as the function value. The image descriptor is invisible to the
calling program.
.ks
.nf
pointer procedure immap (fname, mode, hdr_arg)
begin
# Add code here to handle section suffixes in imagefile
# name strings (e.g. "image[*,5]").
# Open image header file.
hfd = open (fname, mmap[mode], BINARY_FILE)
# Allocate buffer for image descriptor/image header. Note
# the dual use of the HDR_ARG argument.
if (mode == NEW_COPY)
sz_imhdr = hdr_arg.sz_imhdr
else
sz_imhdr = (LEN_IMHDR + int(hdr_arg)) * SZ_STRUCT
call calloc (imdes, SZ_IMDES + sz_imhdr, TY_STRUCT)
imhdr = imdes + IMHDR_OFF
[initialize the image descriptor, including the default
image section (optionally set by user with suffix above).]
# Initialize the mode dependent fields of the image header.
switch (mode) {
case NEW_COPY:
call im_init_newcopy (imdes, hdr_arg)
case NEW_IMAGE:
[initialize the image header]
default:
call seek (hfd, BOFL)
n = read (hfd, Memi[imhdr], sz_imhdr)
if (n < SZ_IMHDR || strne (IM_MAGIC(imhdr), "imhdr")) {
call mfree (imdes)
call imerr (fname, file_not_an_imagefile)
} else if (mode == READ_ONLY)
call close (hfd)
}
[initialize those fields of the image header which are not
dependent on the mode of access.]
return (imhdr_pointer)
end
.fi
.ke
.ks
.nf
procedure imunmap (imhdr)
begin
imdes = imhdr - IMHDR_OFF
# Flush the output buffer, if necessary.
call imflush (imhdr)
# Append the bad pixel list.
if (the bad pixel list has been modified) {
if (file_size < blist_offset)
[write out zeros until the offset of the bad pixel
list is reached]
[append the bad pixel list]
[free buffer space used by bad pixel list]
}
call close (imdes.pfd)
# Update the image header, if necessary (count of bad pixels,
# minimum and maximum pixel values, etc.).
if (imdes.update == YES) {
if (no write permission on image)
call imerr (imname, cannot_update_imhdr)
call imuphdr (imdes)
call close (imdes.hfd)
}
# Free buffer space.
for (i=1; i <= imdes.nbufs; i=i+1)
call mfree (imdes.ibdes[i].bufptr, TY_CHAR)
call mfree (imdes.obdes.bufptr, TY_CHAR)
call mfree (imdes, TY_STRUCT)
end
.fi
.ke
IMFLUSH indirectly references a typed flush procedure, the entry point
address of which was saved in the image descriptor at the time of the
last IMPGS? call. The problem here is that IMFLUSH must work properly
regardless of the data type of the pixels in the output buffer. To
ensure this, and to avoid having to link in the full matrix of 48 type
conversion routines, we call LOC in the put-section procedure to reference
the appropriate typed flush routine.
.ks
.nf
procedure imflush (imhdr)
begin
if (imdes.flush == YES)
call zcall1 (imdes.flush_epa, imdes)
end
.fi
.ke
The following procedure is called by the IMGOBF and IMGIBF routines
to open the pixel storage file, during the first PUTPIX operation on
a file.
.ks
.nf
procedure imopsf (imdes)
begin
switch (imdes.mode) {
case READ_ONLY, READ_WRITE, WRITE_ONLY, APPEND:
imdes.pfd = open (imdes.pixfile, imdes.mode, BINARY_FILE)
if (read (imdes.pfd, pix_hdr, SZ_PIXHDR) < SZ_IMMAGIC)
call imerr (imname, cannot_read_pixel_storage_file)
else if (strne (pix_hdr.im_magic, "impix"))
call imerr (imname, not_a_pixel_storage_file)
case NEW_COPY, NEW_FILE, TEMP_FILE:
# Get the block size for device "imdir$", and initialize
# the physical dimensions of the image, and the absolute
# file offsets of the major components of the pixel storage
# file.
dev_block_size = fdevblk ("imdir$")
[initialize im_physlen, im_pixels, im_hgmoff fields
in image header structure]
# Open the new pixel storage file (preallocate space if
# enabled on local system). Call FADDLN to tell FIO that
# the pixfile is subordinate to the header file (for delete,
# copy, etc.). Save the physical pathname of the pixfile
# in the image header, in case "imdir$" changes.
call mktemp ("imdir$im", temp, SZ_FNAME)
call fpathname (temp, imhdr.pixfile, SZ_PATHNAME)
if (preallocation of imagefiles is enabled)
call falloc (imhdr.pixfile, sz_pixfile)
imdes.pfd = open (imdes.pixfile, NEW_FILE, BINARY_FILE)
call faddln (imdes.imname, imdes.pixfile)
# Write small header into pixel storage file. Allows
# detection of headerless pixfiles, and reconstruction
# of header if it gets lost.
[write pix_hdr header structure to pixel storage file]
default:
call imerr (imname, illegal_access_mode)
}
end
.fi
.ke
.sh
Data Structures
An imagefile consists of two separate files. The first file contains
the image header. In the prototype, there may be only a single header per
header file. The header consists of the standard image header, followed
by an arbitrary number of user defined fields.
The standard part of the image header has a fixed structure. All the variable
size components of an image are stored in the pixel storage file. The
name of the pixel storage file, and the offsets to the various components
of the image, are stored in the image header. The name of the image header
file is in turn stored in the header area of the pixel storage file,
making it possible to detect headerless images.
The pixel storage file contains a small header, followed by the pixels
(aligned on a block boundary), optionally followed by a fixed size
histogram, and a variable size bad pixel list.
.ks
.nf
Structure of an Imagefile
--------- ---------
| <---- |
standard ----> header
image |
header PIXELS
| |
user histogram (optional)
fields |
| bad
\|/ pixel (optional)
---------- list
|
\|/
---------
.fi
.ke
The image header file, which is small, will reside in the users own
directory. The pixel storage file is generated and manipulated
transparently to the applications program and the user, and resides
in the temporary files system, in the logical directory "imdir$".
Storing the parts of an image in two separate files does cause problems.
The standard file operators, like DELETE, COPY, RENAME, and so on,
either cannot be used to manipulate imagefiles, or must know about
imagefiles.
To solve this problem, without requiring FIO to know anything about IMIO
or VSIO data structures, two operators will be added to FIO. The first
will tell FIO that file 'A' has a subordinate file 'B' associated with
it. Any number of subordinate files may be associated with a file.
The information will be maintained as a list of file names in an invisible
text file in the same directory as file 'A'.
The second operator will delete the link to a subordinate file. The FIO
procedures DELETE and RENAME will check for subordinate files, as will CL
utilities like COPY.
.sh
The Standard Image Header
The standard fields of an image header describe the physical
characteristics of the image (required to access the pixels), plus
a few derived or historic attributes, which are commonly associated
with images as used in scientific applications.
.ks
.nf
struct imhdr {
char im_magic[5] # contains the string "imhdr"
long im_hdrlen # length of image header
int im_pixtype # datatype of the pixels
int im_ndim # number of dimensions
long im_len[MAXDIM] # length of the dimensions
long im_physlen[MAXDIM] # physical length (as stored)
long im_pixels # offset of the pixels
long im_hgmoff # offset of hgm pixels
long im_blist # offset of bad pixel list
long im_szblist # size of bad pixel list
long im_nbpix # number of bad pixels
long im_cdate # date of image creation
long im_mdate # date of last modify
real im_max # max pixel value
real im_min # min pixel value
struct histogram im_hgm # histogram descriptor
struct coord_tran im_coord # coordinate transformations
char im_pixfile[SZ_PATHNAME] # name of pixel storage file
char im_name[SZ_IMNAME] # image name string
char im_history[SZ_IMHIST] # history comment string
}
.fi
.ke
The histogram structure, if valid, tells where in the pixel storage file
the histogram is stored, and in addition summarizes the principal
attributes of the histogram. All of these quantities are directly
calculable, except for the last three. The modal value is determined
by centering on the (major) peak of the histogram. LCUT and HCUT define
an area, centered on the modal value, which contains a certain fraction
of the total integral.
.ks
.nf
struct histogram {
int hgm_valid # YES if histogram is valid
int hgm_len # number of bins in hgm
long hgm_npix # npix used to compute hgm
real hgm_min # min hgm value
real hgm_max # max hgm value
real hgm_integral # integral of hgm
real hgm_mean # mean value
real hgm_variance # variance about mean
real hgm_skewness # skewness of hgm
real hgm_mode # modal value of hgm
real hgm_lcut # low cutoff value
real hgm_hcut # high cutoff value
}
.fi
.ke
The coordinate transformation descriptor is used to map pixel coordinates
to some user defined virtual coordinate system, (useful when displaying the
contents of an image). For lack of a significantly better scheme, we have
simply adopted the descriptor defined by the FITS standard.
.ks
.nf
struct coord_tran {
real im_bscale # pixval scale factor
real im_bzero # pixval offset
real im_crval[MAXDIM] # value at pixel
real im_crpix[MAXDIM] # index of pixel
real im_cdelt[MAXDIM] # increment along axis
real im_crota[MAXDIM] # rotation angle
char im_bunit[SZ_BUNIT] # pixval ("brightness") units
char im_ctype[SZ_IMCTYPE,MAXDIM] # coord units
}
.fi
.ke
The image and buffer descriptors are used internally by IMIO while
doing i/o on a mapped image. The image descriptor structure is
allocated immediately before the image header, is transparent to the
applications program, and is used to maintain runtime data, which
does not belong in the image header.
.ks
.nf
struct image_descriptor {
long file_size # size of pixfile
long nget # number getpix calls
int nbufs # number of in buffers
int flush # flush outbuf?
int update # update header?
int pfd # pixfile fd
int hfd # header file fd
int flush_epa # epa of imfls? routine
struct buffer_descriptor *ibdes # input bufdes
struct buffer_descriptor *obdes # output bufdes
char imname[SZ_FNAME] # imagefile name
}
.fi
.ke
.ks
.nf
struct buffer_descriptor {
char *bufptr # buffer pointer
int dtype # datatype of pixels
long npix # number of pixels in buf
long bufsize # buffer size, chars
long vs[MAXDIM] # section start vector
long ve[MAXDIM] # section end vector
}
.fi
.ke
|