This repository was archived by the owner on Feb 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio.c
1169 lines (1005 loc) · 30.6 KB
/
io.c
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
/*************************************************************
Copyright (C) 1990, 1991, 1993 Andy C. Hung, all rights reserved.
PUBLIC DOMAIN LICENSE: Stanford University Portable Video Research
Group. If you use this software, you agree to the following: This
program package is purely experimental, and is licensed "as is".
Permission is granted to use, modify, and distribute this program
without charge for any purpose, provided this license/ disclaimer
notice appears in the copies. No warranty or maintenance is given,
either expressed or implied. In no event shall the author(s) be
liable to you or a third party for any special, incidental,
consequential, or other damages, arising out of the use or inability
to use the program for any purpose (or the loss of data), even if we
have been advised of such possibilities. Any public reference or
advertisement of this source code should refer to it as the Portable
Video Research Group (PVRG) code, and not by any author(s) (or
Stanford University) name.
*************************************************************/
/*
************************************************************
io.c
This package is used to manipulate the raw image files.
There are two standards: block based, assumed to be in sizes of the
DCT block, herein defined as BlockWidth and BlockHeight; and a special
case, two-line-based, assumed to be of two lines per.
************************************************************
*/
/*LABEL io.c */
/* Include definitions. */
#include "globals.h"
#ifdef SYSV
#include <sys/fcntl.h>
#include <sys/unistd.h>
#endif
#include <stdlib.h> /* malloc */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h> /* memcpy */
#include <assert.h> /* memcpy */
#ifdef WIN32
#include <io.h> /* lseek on win32 */
#endif
/* Functions which are local and which are exported. */
/*PUBLIC*/
static BUFFER *MakeXBuffer();
static void WriteXBuffer();
static void ReadXBuffer();
static void ReadResizeBuffer();
static void FlushBuffer();
static void BlockMoveTo();
static void ReadXBound();
static void WriteXBound();
static void LineMoveTo();
extern void ReadBlock();
extern void WriteBlock();
extern void ResizeIob();
extern void RewindIob();
extern void FlushIob();
extern void SeekEndIob();
extern void CloseIob();
extern void MakeIob();
extern void PrintIob();
extern void InstallIob();
extern void TerminateFile();
extern void ReadLine();
extern void ReadPreambleLine();
extern void WriteLine();
extern void LineResetBuffers();
/*PRIVATE*/
/* External variables */
extern int Loud;
extern int PointTransform; /* Used for shifting the pels from the I/O */
extern IMAGE *CImage;
extern FRAME *CFrame;
extern SCAN *CScan;
/* Internal variables. */
static IOBUF *Iob=NULL; /* Internal I/O buffer. */
static int BlockWidth = BLOCKWIDTH; /* Block width. */
static int BlockHeight = BLOCKHEIGHT; /* Block height. */
/* Buffer calculation information */
#define BufferIndex(i,iobuf) (iobuf)->blist[(i)]
#define TrueBufferPos(buffer) (((buffer)->currentoffs - \
((buffer)->tptr - (buffer)->bptr))/buffer->wsize)
/*START*/
/*BFUNC
MakeXBuffer() constructs a holding buffer for the stream input. It
takes up a size passed into it and returns the appropriate buffer
structure.
EFUNC*/
static BUFFER *MakeXBuffer(nelem,wsize)
int nelem;
int wsize;
{
BEGIN("MakeXBuffer")
BUFFER *temp;
if (!(temp = MakeStructure(BUFFER))) /* Make structure */
{
WHEREAMI();
printf("Cannot allocate buffer structure.\n");
exit(ERROR_MEMORY);
}
temp->disable=0; /* Not disabled */
temp->wsize = wsize; /* Set up word size */
temp->size=nelem*wsize; /* Set up size, offset */
temp->currentoffs = 0;
temp->streamoffs = 0;
if (!(temp->space =(unsigned char *) /* Allocate buffer space */
calloc(temp->size+1,sizeof(unsigned char))))
{
WHEREAMI();
printf("Cannot allocate buffer memory.\n");
exit(ERROR_MEMORY);
}
temp->tptr = temp->bptr = temp->space;
return(temp);
}
/*BFUNC
ResizeIob() is used to resize the Iob height and width to conform to
that of the CScan. This is used for the dynamic Number-of-lines
rescaling.
EFUNC*/
void ResizeIob()
{
BEGIN("ResizeIob")
int index;
for(index=0;index<CScan->NumberComponents;index++)
{
CScan->Iob[index]->width = CFrame->Width[CScan->ci[index]];
CScan->Iob[index]->height = CFrame->Height[CScan->ci[index]];
}
}
/*BFUNC
MakeIob() is used to create an Iob structure for use in the CScan
structure. An Iob consists of several Buffer structures with some
additional sizing information. The input flags set up the parameters
of the stream.
EFUNC*/
void MakeIob(type,flags,wsize)
int type;
int flags;
int wsize;
{
BEGIN("MakeIob")
int index,sofs;
BUFFER **current;
IOBUF *temp;
for(index=0;index<CScan->NumberComponents;index++) /* Make IOBUF */
{ /* For each component */
if (!(temp = MakeStructure(IOBUF)))
{
WHEREAMI();
printf("Cannot allocate IOBUF structure.\n");
exit(ERROR_MEMORY);
}
temp->linelastdefault=(1<<(CFrame->DataPrecision-PointTransform-1));
temp->type = type;
temp->wsize = wsize;
temp->hpos=0;
temp->vpos=0;
temp->width = CFrame->Width[CScan->ci[index]]; /* Set up widthxheight */
temp->height = CFrame->Height[CScan->ci[index]];
if (CScan->NumberComponents==1)
{
temp->hor = 1; /* For non-interleaved mode the freq */
temp->ver = 1; /* is always 1x1 */
}
else
{
temp->hor = CFrame->hf[CScan->ci[index]]; /* and hf x vf */
temp->ver = CFrame->vf[CScan->ci[index]];
}
switch(temp->type)
{
case IOB_BLOCK: /* BLOCK TYPE */
temp->num = temp->ver*BlockHeight;
break;
case IOB_LINE: /* LINE TYPE */
temp->num = temp->ver + 1;
break;
default:
WHEREAMI();
printf("Illegal type specified: %d.\n",type);
exit(ERROR_BOUNDS);
}
temp->flags = flags; /* and also flags */
if (!(temp->blist = /*Set up buffer list */
(BUFFER **) calloc(temp->num,sizeof(BUFFER *))))
{
WHEREAMI();
printf("Cannot allocate Iob bufferlist.\n");
exit(ERROR_MEMORY);
}
if( CFrame->tmpfile )
{
temp->file = CFrame->tmpfile;
}
else
{
if ((temp->file = /* Open file */
open(CFrame->ComponentFileName[CScan->ci[index]],
flags,UMASK)) < 0)
{
WHEREAMI();
printf("Cannot open file %s.\n",
CFrame->ComponentFileName[CScan->ci[index]]);
exit(ERROR_INIT_FILE);
} /* Make buffer for every line of component in MDU */
}
for(sofs=0,current=temp->blist;current<temp->blist+temp->num;current++)
{
*current = MakeXBuffer(CFrame->BufferSize, wsize);
(*current)->streamoffs = sofs;
(*current)->iob = temp;
(*current)->data_linelast = temp->linelastdefault;
if (!temp->height || (current - temp->blist) < temp->height-1)
{
sofs += CFrame->Width[CScan->ci[index]]*wsize;
}
}
CScan->Iob[index] = temp;
}
}
/*BFUNC
PrintIob() is used to print the current input buffer to the stdio
stream.
EFUNC*/
void PrintIob()
{
BEGIN("PrintIob")
if (Iob)
{
printf("*** Iob ID: %p ***\n",(void*)Iob);
printf("Number of Buffers: %d Width: %d Height: %d\n",
Iob->num,Iob->width,Iob->height);
printf("hpos: %d vpos: %d hor-freq: %d ver-freq: %d\n",
Iob->hpos,Iob->vpos,Iob->hor,Iob->ver);
printf("filed: %d flags: %d BufferListId: %p\n",
Iob->file,Iob->flags,(void*)Iob->blist);
}
else
{
printf("*** Iob ID: NULL ***\n");
}
}
/*BFUNC
WriteXBuffer() writes out len elements from storage out to the buffer
structure specified. This is can result in a multiple of len bytes
being written out depending on the element structure.
EFUNC*/
static void WriteXBuffer(len,storage,buffer)
int len;
int *storage;
BUFFER *buffer;
{
BEGIN("WriteXBuffer")
int diff,wout;
if (buffer->disable)
{
WHEREAMI();
printf("Attempting to write to disabled buffer!\n");
}
/* printf("Writing:%d bytes\n",len);*/
diff = buffer->size - (buffer->bptr - buffer->space); /* Find room left */
diff = diff/buffer->wsize; /* Scale by element # */
if(len > diff)
{ /* Put as many elems in */
WriteXBuffer(diff,storage,buffer); /* If no room, then flush current */
FlushBuffer(buffer); /* buffer out to disk */
len -= diff;
storage += diff;
}
switch(buffer->wsize) /* Because of compatibility differences between */
{ /* UNIX implementations, we are forced to do this */
case 1: /* explicit ordering of bytes... */
while(len--) /* Write the rest of the buffer out to the disk */
{
wout = *(storage++)<<PointTransform;
*(buffer->bptr++) = (unsigned char) wout;
}
break;
case 2:
while(len--) /* Write the rest of the buffer out to the disk */
{
wout = *(storage++)<<PointTransform;
*(buffer->bptr++) = (unsigned char) (wout>>8)&0xff;
*(buffer->bptr++) = (unsigned char) wout&0xff;
}
break;
case 3:
while(len--) /* Write the rest of the buffer out to the disk */
{
wout = *(storage++)<<PointTransform;
*(buffer->bptr++) = (unsigned char) (wout>>16)&0xff;
*(buffer->bptr++) = (unsigned char) (wout>>8)&0xff;
*(buffer->bptr++) = (unsigned char) wout&0xff;
}
break;
case 4:
while(len--) /* Write the rest of the buffer out to the disk */
{
wout = *(storage++)<<PointTransform;
*(buffer->bptr++) = (unsigned char) (wout>>24)&0xff;
*(buffer->bptr++) = (unsigned char) (wout>>16)&0xff;
*(buffer->bptr++) = (unsigned char) (wout>>8)&0xff;
*(buffer->bptr++) = (unsigned char) wout&0xff;
}
break;
default:
WHEREAMI();
printf("Illegal word size in characters %d.\n",buffer->wsize);
exit(ERROR_BOUNDS);
break;
}
}
/*BFUNC
ReadXBuffer() is fetches len amount of elements into storage from the
buffer structure. This may actually amount to an arbitrary number of
characters depending on the word size.
EFUNC*/
static void ReadXBuffer(len,storage,buffer)
int len;
int *storage;
BUFFER *buffer;
{
BEGIN("ReadXBuffer")
int i,numchars,maxelem,rin;
if (buffer->disable)
{
for(i=0;i<len;i++) *(storage++)=buffer->data_linelast;
return;
}
numchars = len*buffer->wsize;
/* The following command recurses because */
/* it's slightly more efficient that way */
/* when the probability of recursion is */
/* negligible. */
while (numchars > buffer->size) /* We ask more than the buffer can handle */
{ /* Inefficient for small buffer sizes */
maxelem = buffer->size/buffer->wsize;
ReadXBuffer(maxelem, storage, buffer); /* Split up into several reads */
storage += maxelem;
len -= maxelem;
numchars -= maxelem*buffer->wsize;
}
if(numchars > (buffer->tptr - buffer->bptr)) /* If we request > bytes */
ReadResizeBuffer(numchars,buffer); /* Read those bytes in */
switch(buffer->wsize) /* Again, explicit input of bytes */
{
case 1:
while(len--) /* Now copy over to storage */
{
rin = (int) *(buffer->bptr++);
*(storage++) = rin >> PointTransform;
}
break;
case 2:
while(len--) /* Now copy over to storage */
{
rin = (((int)*(buffer->bptr++))<<8);
rin |= *(buffer->bptr++);
*(storage++) = rin >> PointTransform;
}
break;
case 3:
while(len--) /* Now copy over to storage */
{
rin = (((int)*(buffer->bptr++))<<16);
rin |= (((int)*(buffer->bptr++))<<8);
rin |= (*(buffer->bptr++));
*(storage++) = rin >> PointTransform;
}
break;
case 4:
while(len--) /* Now copy over to storage */
{
rin = (((int)*(buffer->bptr++))<<24);
rin |= (((int)*(buffer->bptr++))<<16);
rin |= (((int)*(buffer->bptr++))<<8);
rin |= (*(buffer->bptr++));
*(storage++) = rin >> PointTransform;
}
break;
default:
WHEREAMI();
printf("Illegal word size in characters %d.\n",buffer->wsize);
exit(ERROR_BOUNDS);
break;
}
#ifdef IO_DEBUG
WHEREAMI();
printf("last read: %d",*(storage-1));
printf("\n");
#endif
}
/*BFUNC
ReadResizeBuffer() reads len bytes from the stream and puts it
into the buffer.
EFUNC*/
static void ReadResizeBuffer(len,buffer)
int len;
BUFFER *buffer;
{
BEGIN("ReadResizeBuffer")
int retval,diff,location,amount;
diff = buffer->tptr - buffer->bptr; /* Find out the current usage */
if (len > buffer->size-1) /* calculate if we can hold it */
{
WHEREAMI();
printf("Length Request Too Large.\n");
exit(ERROR_PARAMETER);
}
#ifdef IO_DEBUG
printf("SPACE: %x BPTR: %x DIFF: %d\n",buffer->space,buffer->bptr,diff);
printf("ReadLseek %d\n",buffer->streamoffs+buffer->currentoffs);
#endif
assert( diff >= 0 );
memcpy(buffer->space,buffer->bptr,diff); /* Move buffer down. */
buffer->bptr = buffer->space; /* Reset pointers. */
buffer->tptr = buffer->space + diff;
location = buffer->streamoffs+buffer->currentoffs;
amount = buffer->size-(buffer->tptr - buffer->space);
lseek(buffer->iob->file,location,SEEK_SET);
#ifdef IO_DEBUG
printf("Read: Filed %d Buf: %x NBytes: %d\n",
buffer->iob->file,buffer->tptr,amount);
#endif
if ((retval = read(buffer->iob->file, /* Do the read */
buffer->tptr,
amount)) < 0)
{
WHEREAMI();
printf("Cannot Resize.\n");
exit(ERROR_READ);
}
#ifdef IO_DEBUG
printf("ReadReturn numbytes %d\n",retval);
#endif
buffer->tptr += retval; /* Alter pointers */
buffer->currentoffs += retval;
}
/*BFUNC
FlushBuffer() saves the rest of the bytes in the buffer out to the
disk.
EFUNC*/
static void FlushBuffer(buffer)
BUFFER *buffer;
{
BEGIN("FlushBuffer")
int retval;
#ifdef IO_DEBUG
printf("WriteLseek %d\n",buffer->streamoffs+buffer->currentoffs);
#endif
lseek(buffer->iob->file,buffer->streamoffs+buffer->currentoffs,SEEK_SET);
if ((retval = write(buffer->iob->file,
buffer->space,
(buffer->bptr - buffer->space))) < 0)
{
WHEREAMI();
printf("Cannot flush buffer.\n");
exit(ERROR_WRITE);
}
buffer->currentoffs += (buffer->bptr - buffer->space);
buffer->bptr = buffer->space;
}
/*BFUNC
ReadBlock() is used to get a block from the current Iob. This function
returns (for the JPEG case) 64 bytes in the store integer array. It
is stored in row-major form; that is, the row index changes least
rapidly.
EFUNC*/
void ReadBlock(store)
int *store;
{
BEGIN("ReadBlock")
int i,voffs;
voffs = (Iob->vpos % Iob->ver)*BlockHeight; /* Find current v offset*/
#ifdef IO_DEBUG
for(i=0;i<BlockHeight;i++)
{
printf("%d Iob %x\n",i,Iob->blist[i]->Iob);
}
#endif
for(i=voffs;i<voffs+BlockHeight;i++) /* Use voffs to index into */
{ /* Buffer list of IOB */
#ifdef IO_DEBUG
printf("%d Iob %x\n",i,Iob->blist[i]->Iob);
#endif
ReadXBound(BlockWidth,store,Iob->blist[i]); /* get blockwidth elms */
store+=BlockWidth; /* Storage array & increment */
} /* by blockwidth */
if ((++Iob->hpos % Iob->hor)==0) /* Increment MDU block pos */
{
if ((++Iob->vpos % Iob->ver) == 0)
{
if (Iob->hpos < CScan->MDUWide*Iob->hor)
{
Iob->vpos -= Iob->ver;
}
else
{
Iob->hpos = 0; /* If at end of raster width*/
BlockMoveTo(); /* Reload buffers from start */
} /* of next line. */
}
else
{
Iob->hpos -= Iob->hor;
}
}
}
/*BFUNC
WriteBlock() writes an array of data in the integer array pointed to
by store out to the driver specified by the IOB. The integer array is
stored in row-major form, that is, the first row of (8) elements, the
second row of (8) elements....
EFUNC*/
void WriteBlock(store)
int *store;
{
BEGIN("WriteBlock")
int i,voffs;
voffs = (Iob->vpos % Iob->ver)*BlockHeight; /* Find vertical buffer offs. */
for(i=voffs;i<voffs+BlockHeight;i++)
{
if (!Iob->height || (((Iob->vpos/Iob->ver)*BlockHeight + i) <
Iob->height))
{
WriteXBound(BlockWidth,store,Iob->blist[i]); /* write Block elms */
store+=BlockWidth; /* Iob indexed by offset */
}
}
if ((++Iob->hpos % Iob->hor)==0) /* Increment block position */
{ /* in MDU. */
if ((++Iob->vpos % Iob->ver) == 0)
{
if (Iob->hpos < CScan->MDUWide*Iob->hor)
{
Iob->vpos -= Iob->ver;
}
else
{
Iob->hpos = 0; /* If at end of image (width) */
FlushIob(); /* Flush current IOB and */
BlockMoveTo(); /* Move to next lower MDU line */
}
}
else
{
Iob->hpos -= Iob->hor;
}
}
}
/*BFUNC
BlockMoveTo() is used to move to a specific vertical and horizontal
location (block wise) specified by the current Iob. That means you set
the current Iob parameters and then call BlockMoveTo().
EFUNC*/
static void BlockMoveTo()
{
BEGIN("BlockMoveTo")
int i,vertical,horizontal;
if (Loud > MUTE)
{
WHEREAMI();
printf("%p Moving To [Horizontal:Vertical] [%d:%d] \n",
(void*)Iob,Iob->hpos,Iob->vpos);
}
horizontal = Iob->hpos * BlockWidth; /* Calculate actual */
vertical = Iob->vpos * BlockHeight; /* Pixel position */
for(i=0;i<Iob->ver*BlockHeight;i++)
{
if (Iob->height)
{
vertical =
((vertical < Iob->height) ?
vertical : Iob->height-1);
}
Iob->blist[i]->tptr = /* Reset pointer space */
Iob->blist[i]->bptr = /* To show no contents */
Iob->blist[i]->space;
Iob->blist[i]->currentoffs = horizontal* Iob->wsize;/* reset h offset */
Iob->blist[i]->streamoffs = vertical * Iob->width *
Iob->wsize; /* Reset v offset */
vertical++;
}
}
/*BFUNC
RewindIob() brings all the pointers to the start of the file. The reset
does not flush the buffers if writing.
EFUNC*/
void RewindIob()
{
BEGIN("RewindIob")
int i;
switch(Iob->type)
{
case IOB_BLOCK:
BlockWidth = BLOCKWIDTH; /* Block width. */
BlockHeight = BLOCKHEIGHT; /* Block height. */
for(i=0;i<Iob->ver*BlockHeight;i++)
{
Iob->blist[i]->tptr =
Iob->blist[i]->bptr =
Iob->blist[i]->space;
Iob->blist[i]->currentoffs = 0;
Iob->blist[i]->streamoffs = i * Iob->width * Iob->wsize;
}
Iob->hpos = Iob->vpos = 0;
break;
case IOB_LINE:
Iob->linelastdefault=(1<<(CFrame->DataPrecision-PointTransform-1));
for(i= 0;i<Iob->ver+1;i++)
{
Iob->blist[i]->tptr =
Iob->blist[i]->bptr =
Iob->blist[i]->space;
Iob->blist[i]->currentoffs = 0;
if (!i)
{
Iob->blist[i]->streamoffs = 0;
Iob->blist[i]->disable=1;
}
else
{
Iob->blist[i]->streamoffs = (i-1) * Iob->width * Iob->wsize;
Iob->blist[i]->disable=0;
}
Iob->blist[i]->data_linelast = Iob->linelastdefault;
}
Iob->hpos = 0;
Iob->vpos = -1;
break;
default:
WHEREAMI();
printf("Bad IOB type: %d\n",Iob->type);
break;
}
}
/*BFUNC
FlushIob() is used to flush all the buffers in the current Iob. This
is done at the conclusion of a write on the current buffers.
EFUNC*/
void FlushIob()
{
BEGIN("FlushIob")
int i;
if (Loud > MUTE)
printf("IOB: %p Flushing buffers\n",(void*)Iob);
switch(Iob->type)
{
case IOB_BLOCK:
for(i=0;i<Iob->ver*BlockHeight;i++)
FlushBuffer(Iob->blist[i]);
break;
case IOB_LINE:
Iob->blist[0]->data_linelast=Iob->linelastdefault;
for(i=1;i<Iob->ver+1;i++)
{
Iob->blist[i]->data_linelast=Iob->linelastdefault;
FlushBuffer(Iob->blist[i]);
}
break;
default:
WHEREAMI();
printf("Illegal IOB type: %d.\n",Iob->type);
break;
}
}
/*BFUNC
SeekEndIob() is used to seek the end of all the buffers in the current
Iob. This is done at the conclusion of a write, to avoid DNL problems.
EFUNC*/
void SeekEndIob()
{
BEGIN("SeekEndIob")
int size,tsize;
static unsigned char Terminator[] = {0x80,0x00};
size = lseek(Iob->file,0,SEEK_END);
tsize = Iob->width*Iob->height*Iob->wsize;
if (size != tsize)
{
WHEREAMI();
printf("End not flush, making flush (actual: %d != target:%d)\n",
size,tsize);
if (size<tsize)
{
lseek(Iob->file,tsize-1,SEEK_SET); /* Seek and terminate */
write(Iob->file,Terminator,1);
}
else if (size > tsize)
{
#ifdef NOTRUNCATE
WHEREAMI();
printf("file is too large, only first %d bytes valid\n",
tsize);
#else
#ifdef WIN32
chsize(Iob->file,tsize); /* no ftruncate on WIN32... */
#else
ftruncate(Iob->file,tsize); /* simply truncate*/
#endif
#endif
}
}
}
/*BFUNC
CloseIob() is used to close the current Iob.
EFUNC*/
void CloseIob()
{
BEGIN("CloseIob")
if( !CFrame->tmpfile ) /* if file is closed we loose it for good */
{
close(Iob->file);
}
}
/*BFUNC
ReadXBound() reads nelem elements of information from the specified
buffer. It detects to see whether a load is necessary or not, or
whether the current buffer is out of the image width bounds.
EFUNC*/
static void ReadXBound(nelem,cstore,buffer)
int nelem;
int *cstore;
BUFFER *buffer;
{
BEGIN("ReadXBound")
int i,diff;
if ((diff = buffer->iob->width - TrueBufferPos(buffer)) <= nelem)
{
#ifdef IO_DEBUG
printf("ReadBound: Trailing Edge Detected. Diff: %d\n",diff);
#endif
if (diff <= 0)
{
for(i=0;i<nelem;i++) /* Pure outside bounds */
*(cstore++) = buffer->overflow;
}
else
{
ReadXBuffer(diff,cstore,buffer);
buffer->overflow = (unsigned int) cstore[diff-1];
for(i=diff;i<nelem;i++) /* Replicate to bounds */
cstore[i] = cstore[i-1];
}
}
else
ReadXBuffer(nelem,cstore,buffer);
}
/*BFUNC
WriteXBound() writes the integer array input to the buffer. It checks
to see whether the bounds of the image width are exceeded, if so, the
excess information is ignored.
EFUNC*/
static void WriteXBound(nelem,cstore,buffer)
int nelem;
int *cstore;
BUFFER *buffer;
{
BEGIN("WriteXBound")
int diff;
if ((diff = buffer->iob->width - TrueBufferPos(buffer)) <= nelem)
{ /* Diff is balance to write to disk */
if (diff > 0) /* Write balance out to disk */
WriteXBuffer(diff,cstore,buffer);
}
else /* If more than numberelem, then can put all */
WriteXBuffer(nelem,cstore,buffer); /* to the buffer. */
}
/*BFUNC
InstallIob() is used to install the Iob in the current scan as the
real Iob.
EFUNC*/
void InstallIob(index)
int index;
{
BEGIN("InstallIob")
if (!(Iob = CScan->Iob[index]))
{
WHEREAMI();
printf("Warning, NULL Iob installed.\n");
}
}
/*BFUNC
TerminateFile() is a function that ensures that the entire file
defined by the Iob is properly flush with the filesize specifications.
This function is used when some fatal error occurs.
EFUNC*/
void TerminateFile()
{
BEGIN("TerminateFile")
int i,size;
static unsigned char Terminator[] = {0x80,0x00};
if (CFrame->GlobalHeight)
{
printf("> GH:%d GW:%d R:%d\n",
CFrame->GlobalHeight,
CFrame->GlobalWidth,
CFrame->ResyncInterval);
for(i=0;i<CScan->NumberComponents;i++)
{
if (CScan->Iob[i])
{
printf(">> C:%d N:%s H:%d W:%d hf:%d vf:%d\n",
CScan->ci[i],
CFrame->ComponentFileName[CScan->ci[i]],
CFrame->Height[CScan->ci[i]],
CFrame->Width[CScan->ci[i]],
CFrame->hf[CScan->ci[i]],
CFrame->vf[CScan->ci[i]]);
InstallIob(i);
FlushIob();
size = lseek(CScan->Iob[i]->file,0,SEEK_END);
if (size !=
CFrame->Width[CScan->ci[i]]*CFrame->Height[CScan->ci[i]]*
CScan->Iob[i]->wsize)
{ /* Terminate file */
lseek(CScan->Iob[i]->file, /* by seeking to end */
(CFrame->Width[CScan->ci[i]]* /* And writing byte */
CFrame->Height[CScan->ci[i]]*
CScan->Iob[i]->wsize)-1, /* Making flush with */
SEEK_SET); /* Original size */
write(CScan->Iob[i]->file,Terminator,1);
}
}
}
}
else
{
WHEREAMI();
printf("Unknown number of lines. Cannot flush file.\n");
}
}
/*BFUNC
ReadLine() reads in the lines required by the lossless function. The
array *store should be large enough to handle the line information
read.
In total, there should be (HORIZONTALFREQUENCY+1) * nelem
(VERTICALFREQUENCY+1) elements in the *store array. This forms a
matrix with each line consisting of:
[PastPredictor 1 element] nelem* [HORIZONTALFREQUENCY elements]
And there are (VERTICALFREQUENCY+1) of such lines in the matrix:
Previous line (2**Precision-1) if beyond specifications of window
Active line 1...
...
Active line VERTICALFREQUENCY...
EFUNC*/
void ReadLine(nelem,store)
int nelem;
int *store;
{
BEGIN("ReadLine")
int i;
for(i=0;i<Iob->ver+1;i++) /* Use voffs to index into */
{ /* Buffer list of IOB */
#ifdef IO_DEBUG
printf("%d Iob %x\n",i,Iob->blist[i]->Iob);
#endif
*(store++)=Iob->blist[i]->data_linelast;
ReadXBound(Iob->hor*nelem,store,Iob->blist[i]);
store+=Iob->hor*nelem;
Iob->blist[i]->data_linelast = *(store-1);
}
Iob->hpos += Iob->hor*nelem;
if (Iob->hpos >= CScan->MDUWide*Iob->hor)
{
Iob->vpos += Iob->ver;