-
Notifications
You must be signed in to change notification settings - Fork 74
/
UIFFFormat.inc1.p
796 lines (538 loc) · 14.3 KB
/
UIFFFormat.inc1.p
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
{Photoshop version 1.0.1, file: UIFFFormat.inc1.p
Computer History Museum, www.computerhistory.org
This material is (C)Copyright 1990 Adobe Systems Inc.
It may not be distributed to third parties.
It is licensed for non-commercial use according to
www.computerhistory.org/softwarelicense/photoshop/ }
{$I UAssembly.a.inc}
{$I UIFFFormat.a.inc}
{$I UResize.p.inc}
{*****************************************************************************}
{$S AInit}
PROCEDURE TIFFFormat.IImageFormat; OVERRIDE;
BEGIN
INHERITED IImageFormat;
fCanRead := TRUE;
fReadType1 := 'ILBM';
fReadType2 := 'IFF ';
fFileType := 'ILBM';
fUsesDataFork := TRUE;
fDialogID := 2800;
fRadioClusters := 1;
fRadio1Item := 4;
fRadio1Count := 8
END;
{*****************************************************************************}
{$S AWriteFile}
FUNCTION TIFFFormat.CanWrite (doc: TImageDocument): BOOLEAN; OVERRIDE;
BEGIN
CanWrite := (doc.fMode = HalftoneMode) OR
(doc.fMode = MonochromeMode) OR
(doc.fMode = IndexedColorMode) OR
(doc.fMode = MultichannelMode)
END;
{*****************************************************************************}
{$S AIFFFormat}
PROCEDURE TIFFFormat.SetFormatOptions (doc: TImageDocument); OVERRIDE;
BEGIN
gTables.CompTables (doc, 0, FALSE, FALSE, 8, 8, FALSE, TRUE, 1);
fDepth := gTables.fResolution;
fRadio1 := fDepth - 1;
DoOptionsDialog;
fDepth := fRadio1 + 1
END;
{*****************************************************************************}
{$S AIFFFormat}
FUNCTION TIFFFormat.ReadCMap (VAR cMap: TRGBLookUpTable): INTEGER;
VAR
size: LONGINT;
gray: INTEGER;
colors: INTEGER;
zero4Bits: BOOLEAN;
BEGIN
size := GetLong;
IF (size < 3) OR (size > 768) OR (size MOD 3 <> 0) THEN
Failure (errBadIFF, 0);
DoSetBytes (@cMap, SIZEOF (TRGBLookUpTable), 0);
colors := size DIV 3;
FOR gray := 0 TO colors - 1 DO
BEGIN
cMap.R [gray] := CHR (GetByte);
cMap.G [gray] := CHR (GetByte);
cMap.B [gray] := CHR (GetByte)
END;
IF ODD (size) THEN SkipBytes (1);
zero4Bits := TRUE;
FOR gray := 0 TO colors - 1 DO
zero4Bits := zero4Bits AND (BAND (ORD (cMap.R [gray]), $F) = 0)
AND (BAND (ORD (cMap.G [gray]), $F) = 0)
AND (BAND (ORD (cMap.B [gray]), $F) = 0);
IF zero4Bits THEN
FOR gray := 0 TO colors - 1 DO
BEGIN
cMap.R [gray] := CHR (BSR (ORD (cMap.R [gray]), 4) +
ORD (cMap.R [gray]));
cMap.G [gray] := CHR (BSR (ORD (cMap.G [gray]), 4) +
ORD (cMap.G [gray]));
cMap.B [gray] := CHR (BSR (ORD (cMap.B [gray]), 4) +
ORD (cMap.B [gray]))
END;
ReadCMap := colors
END;
{*****************************************************************************}
{$S AIFFFormat}
PROCEDURE TIFFFormat.TransCMap (doc: TImageDocument;
VAR cMap: TRGBLookUpTable;
nPlanes: INTEGER;
depth: INTEGER;
planePick: INTEGER;
planeOnOff: INTEGER;
planeMask: INTEGER);
VAR
gray: INTEGER;
color: INTEGER;
inBit: INTEGER;
outBit: INTEGER;
BEGIN
DoSetBytes (@doc.fIndexedColorTable, SIZEOF (TRGBLookUpTable), 0);
FOR gray := 0 TO BSL (1, nPlanes) - 1 DO
BEGIN
inBit := 0;
color := 0;
FOR outBit := 0 TO depth - 1 DO
IF BTST (planePick, outBit) THEN
BEGIN
color := color + BSL (ORD (BTST (gray, inBit)), outBit);
inBit := inBit + 1
END
ELSE
color := color + BSL (ORD (BTST (planeOnOff, outBit)), outBit);
color := BAND (color, planeMask);
doc.fIndexedColorTable.R [gray] := cMap.R [color];
doc.fIndexedColorTable.G [gray] := cMap.G [color];
doc.fIndexedColorTable.B [gray] := cMap.B [color]
END
END;
{*****************************************************************************}
{$S AIFFFormat}
PROCEDURE TIFFFormat.ReadBody (doc: TImageDocument;
nPlanes: INTEGER;
masked: BOOLEAN;
compressed: BOOLEAN);
VAR
srcPtr: Ptr;
dstPtr: Ptr;
row: INTEGER;
plane: INTEGER;
sPlanes: INTEGER;
rowBytes: INTEGER;
dataUsed: LONGINT;
dataSize: LONGINT;
dataStart: LONGINT;
packedBytes: INTEGER;
BEGIN
dataUsed := 0;
dataSize := GetLong;
dataStart := GetFilePosition;
sPlanes := nPlanes + ORD (masked);
rowBytes := BSL (BSR (doc.fCols + 15, 4), 1);
packedBytes := rowBytes + 1 + BSR (rowBytes, 7);
IF GetPtrSize (gBuffer) < sPlanes * ORD4 (rowBytes + packedBytes) THEN
Failure (errBadIFF, 0);
doc.fData [0] . SetBytes (0);
FOR row := 0 TO doc.fRows - 1 DO
BEGIN
UpdateProgress (row, doc.fRows);
IF compressed THEN
BEGIN
SeekTo (dataStart + dataUsed);
srcPtr := Ptr (ORD4 (gBuffer) + rowBytes * sPlanes);
dstPtr := gBuffer;
GetBytes (Min (packedBytes * sPlanes,
dataSize - dataUsed), srcPtr);
UnpackBits (srcPtr, dstPtr, rowBytes * sPlanes);
dataUsed := dataUsed + ORD4 (srcPtr) - ORD4 (dstPtr)
END
ELSE
BEGIN
GetBytes (rowBytes * sPlanes, gBuffer);
dataUsed := dataUsed + rowBytes * sPlanes
END;
IF dataUsed > dataSize THEN Failure (errBadIFF, 0);
dstPtr := doc.fData [0] . NeedPtr (row, row, TRUE);
FOR plane := 0 TO nPlanes - 1 DO
StuffPlane (Ptr (ORD4 (gBuffer) + plane * rowBytes),
dstPtr,
doc.fCols,
plane);
doc.fData [0] . DoneWithPtr
END;
UpdateProgress (1, 1)
END;
{*****************************************************************************}
{$S AIFFFormat}
PROCEDURE DeHAM (doc: TImageDocument);
VAR
r: CHAR;
g: CHAR;
b: CHAR;
rPtr: Ptr;
gPtr: Ptr;
bPtr: Ptr;
fi: FailInfo;
row: INTEGER;
col: INTEGER;
gray: INTEGER;
rArray: TVMArray;
gArray: TVMArray;
bArray: TVMArray;
table: TRGBLookUpTable;
PROCEDURE CleanUp (error: INTEGER; message: LONGINT);
BEGIN
FreeObject (rArray);
FreeObject (gArray);
FreeObject (bArray)
END;
BEGIN
rArray := NIL;
gArray := NIL;
bArray := NIL;
CatchFailures (fi, CleanUp);
rArray := doc.fData [0] . CopyArray (3);
gArray := NewVMArray (doc.fRows, doc.fCols, 2);
bArray := NewVMArray (doc.fRows, doc.fCols, 1);
table := doc.fIndexedColorTable;
FOR row := 0 TO doc.fRows - 1 DO
BEGIN
MoveHands (NOT doc.fReverting);
UpdateProgress (row, doc.fRows);
r := table.R [0];
g := table.G [0];
b := table.B [0];
rPtr := rArray.NeedPtr (row, row, TRUE);
gPtr := gArray.NeedPtr (row, row, TRUE);
bPtr := bArray.NeedPtr (row, row, TRUE);
FOR col := 1 TO doc.fCols DO
BEGIN
gray := BAND (rPtr^, $F);
CASE BSR (rPtr^, 4) OF
0: BEGIN
r := table.R [gray];
g := table.G [gray];
b := table.B [gray]
END;
1: b := CHR (gray + BSL (gray, 4));
2: r := CHR (gray + BSL (gray, 4));
3: g := CHR (gray + BSL (gray, 4))
END;
{$PUSH}
{$R-}
rPtr^ := ORD (r);
rPtr := Ptr (ORD4 (rPtr) + 1);
gPtr^ := ORD (g);
gPtr := Ptr (ORD4 (gPtr) + 1);
bPtr^ := ORD (b);
bPtr := Ptr (ORD4 (bPtr) + 1)
{$POP}
END;
rArray.DoneWithPtr;
gArray.DoneWithPtr;
bArray.DoneWithPtr;
END;
UpdateProgress (1, 1);
rArray.Flush;
gArray.Flush;
bArray.Flush;
Success (fi);
doc.fData [0] . Free;
doc.fData [0] := rArray;
doc.fData [1] := gArray;
doc.fData [2] := bArray;
doc.fChannels := 3;
doc.fMode := RGBColorMode
END;
{*****************************************************************************}
{$S AIFFFormat}
PROCEDURE TIFFFormat.DoRead (doc: TImageDocument;
refNum: INTEGER;
rsrcExists: BOOLEAN); OVERRIDE;
TYPE
PBoolean = ^BOOLEAN;
HBoolean = ^PBoolean;
VAR
fi: FailInfo;
code: OSType;
info: HBoolean;
depth: INTEGER;
colors: INTEGER;
adjust: BOOLEAN;
hamMode: BOOLEAN;
nPlanes: INTEGER;
xAspect: INTEGER;
yAspect: INTEGER;
newRows: LONGINT;
newCols: LONGINT;
channel: INTEGER;
compCode: INTEGER;
maskCode: INTEGER;
haveBMHD: BOOLEAN;
aVMArray: TVMArray;
planePick: INTEGER;
planeMask: INTEGER;
planeOnOff: INTEGER;
cMap: TRGBLookUpTable;
PROCEDURE CleanUp (error: INTEGER; message: LONGINT);
BEGIN
aVMArray.Free
END;
BEGIN
fRefNum := refNum;
MoveHands (NOT doc.fReverting);
REPEAT
IF GetFilePosition + 4 > GetFileLength THEN
Failure (errBadIFF, 0);
GetBytes (4, @code);
IF code = 'CAT ' THEN
SkipBytes (8)
ELSE IF code = 'FORM' THEN
BEGIN
SkipBytes (4);
GetBytes (4, @code);
IF code = 'ILBM' THEN LEAVE
END
ELSE IF GetFilePosition = 4 THEN
Failure (errBadIFF, 0)
ELSE
SkipBytes (BSL (BSR (GetLong + 1, 1), 1))
UNTIL FALSE;
colors := 0;
haveBMHD := FALSE;
REPEAT
GetBytes (4, @code);
IF code = 'BMHD' THEN
BEGIN
IF GetLong <> 20 THEN Failure (errBadIFF, 0);
doc.fCols := GetWord;
doc.fRows := GetWord;
IF NOT doc.ValidSize THEN Failure (errBadIFF, 0);
SkipBytes (4);
nPlanes := GetByte;
maskCode := GetByte;
compCode := GetByte;
IF (nPlanes <= 0) OR (nPlanes > 8) OR
(maskCode < 0) OR (maskCode > 3) OR
(compCode < 0) OR (compCode > 1) THEN
Failure (errBadIFF, 0);
SkipBytes (3);
xAspect := GetByte;
yAspect := GetByte;
IF (xAspect = 0) OR (yAspect = 0) THEN
BEGIN
xAspect := 1;
yAspect := 1
END;
{ Special cases: Fix aspect ratios for common errors }
IF (doc.fRows = 400) AND (doc.fCols = 320) AND
(xAspect = 10) AND (yAspect = 11) THEN
xAspect := 20;
IF ((doc.fRows = 400) AND (doc.fCols = 640) OR
(doc.fRows = 200) AND (doc.fCols = 320)) AND
(xAspect = 20) AND (yAspect = 11) THEN
xAspect := 10;
SkipBytes (4);
depth := nPlanes;
planePick := -1;
planeOnOff := 0;
planeMask := -1;
haveBMHD := TRUE
END
ELSE IF code = 'CMAP' THEN
colors := ReadCMap (cMap)
ELSE IF code = 'DEST' THEN
BEGIN
IF GetLong <> 8 THEN Failure (errBadIFF, 0);
depth := GetByte;
IF (depth < 1) OR (depth > 8) THEN Failure (errBadIFF, 0);
SkipBytes (1);
planePick := GetWord;
planeOnOff := GetWord;
planeMask := GetWord
END
ELSE IF code <> 'BODY' THEN
SkipBytes (BSL (BSR (GetLong + 1, 1), 1))
UNTIL code = 'BODY';
IF NOT haveBMHD OR (colors = 0) THEN
Failure (errBadIFF, 0);
hamMode := (colors = 16) AND (depth = 6);
aVMArray := NewVMArray (doc.fRows, doc.fCols, 1);
doc.fData [0] := aVMArray;
TransCMap (doc, cMap, nPlanes, depth, planePick, planeOnOff, planeMask);
StartTask (1);
IF hamMode THEN
StartTask (0.6);
ReadBody (doc, nPlanes, maskCode = 1, compCode = 1);
doc.fMode := IndexedColorMode;
IF hamMode THEN
BEGIN
FinishTask;
DeHAM (doc)
END;
FinishTask;
IF xAspect <> yAspect THEN
BEGIN
IF doc.fRevertInfo = NIL THEN
BEGIN
adjust := AskAdjustAspect;
info := HBoolean (NewPermHandle (SIZEOF (BOOLEAN)));
FailMemError;
info^^ := adjust;
doc.fRevertInfo := Handle (info)
END
ELSE
adjust := HBoolean (doc.fRevertInfo)^^;
IF adjust THEN
BEGIN
newRows := doc.fRows;
newCols := doc.fCols;
IF xAspect > yAspect THEN
newCols := (newCols * xAspect + BSR (yAspect, 1)) DIV yAspect
ELSE
newRows := (newRows * yAspect + BSR (xAspect, 1)) DIV xAspect;
IF (newRows > kMaxCoord) OR (newCols > kMaxCoord) THEN
Failure (errBadIFF, 0);
FOR channel := 0 TO doc.fChannels - 1 DO
BEGIN
aVMArray := NewVMArray (newRows,
newCols, doc.Interleave (channel));
CatchFailures (fi, CleanUp);
ResizeArray (doc.fData [channel],
aVMArray,
doc.fMode = IndexedColorMode,
NOT doc.fReverting);
Success (fi);
doc.fData [channel] . Free;
doc.fData [channel] := aVMArray
END;
doc.fRows := newRows;
doc.fCols := newCols
END
END;
TestForMonochrome (doc);
IF nPlanes = 1 THEN
TestForHalftone (doc)
END;
{*****************************************************************************}
{$S AIFFFormat}
PROCEDURE TIFFFormat.WriteBody (doc: TImageDocument; bounds: Rect);
VAR
r: Rect;
srcPtr: Ptr;
dstPtr: Ptr;
fi: FailInfo;
row: INTEGER;
size: LONGINT;
plane: INTEGER;
buffer: Handle;
outBytes: INTEGER;
rowBytes: INTEGER;
startPosition: LONGINT;
PROCEDURE CleanUp (error: INTEGER; message: LONGINT);
BEGIN
FreeLargeHandle (buffer)
END;
BEGIN
startPosition := GetFilePosition;
PutLong (0);
outBytes := BSL (BSR (bounds.right + 15, 4), 1);
rowBytes := BSL (BSR (bounds.right + 1, 1), 1);
buffer := NewLargeHandle (rowBytes +
ORD4 (outBytes) * 2 + 1 + BSR (outBytes, 7));
CatchFailures (fi, CleanUp);
MoveHHi (buffer);
HLock (buffer);
r := bounds;
FOR row := 0 TO bounds.bottom - 1 DO
BEGIN
UpdateProgress (row, bounds.bottom);
r.top := row;
r.bottom := row + 1;
gTables.DitherRect (doc, 0, 1, r, buffer^, TRUE);
FOR plane := 0 TO fDepth - 1 DO
BEGIN
srcPtr := buffer^;
dstPtr := Ptr (ORD4 (buffer^) + rowBytes);
DoSetBytes (dstPtr, outBytes, 0);
ExtractPlane (srcPtr, dstPtr, bounds.right, plane);
srcPtr := Ptr (ORD4 (buffer^) + rowBytes);
dstPtr := Ptr (ORD4 (srcPtr) + outBytes);
MyPackBits (srcPtr, dstPtr, outBytes);
PutBytes (ORD4 (dstPtr) - ORD4 (srcPtr), srcPtr)
END
END;
UpdateProgress (1, 1);
Success (fi);
CleanUp (0, 0);
size := GetFilePosition - startPosition - 4;
IF ODD (size) THEN PutByte (0);
SeekTo (startPosition);
PutLong (size)
END;
{*****************************************************************************}
{$S AIFFFormat}
PROCEDURE TIFFFormat.DoWrite (doc: TImageDocument; refNum: INTEGER); OVERRIDE;
VAR
bounds: Rect;
code: OSType;
gray: INTEGER;
color: RGBColor;
BEGIN
fRefNum := refNum;
MoveHands (FALSE);
gTables.CompTables (doc, 0, FALSE, FALSE, 8, fDepth, FALSE, FALSE, 1);
SetRect (bounds, 0, 0, doc.fCols, doc.fRows);
code := 'FORM';
PutBytes (4, @code);
PutLong (0);
code := 'ILBM';
PutBytes (4, @code);
code := 'BMHD';
PutBytes (4, @code);
PutLong (20);
PutWord (bounds.right);
PutWord (bounds.bottom);
PutWord (0);
PutWord (0);
PutByte (fDepth);
PutByte (0);
PutByte (1);
PutByte (0);
PutWord (0);
PutByte (1);
PutByte (1);
PutWord (bounds.right);
PutWord (bounds.bottom);
code := 'CMAP';
PutBytes (4, @code);
PutLong (3 * BSL (1, fDepth));
FOR gray := 0 TO gTables.fColorTable^^.ctSize DO
BEGIN
{$PUSH}
{$R-}
color := gTables.fColorTable^^.ctTable[gray].rgb;
{$POP}
PutByte (BSR (color.red , 8));
PutByte (BSR (color.green, 8));
PutByte (BSR (color.blue , 8))
END;
FOR gray := gTables.fColorTable^^.ctSize + 1 TO BSL (1, fDepth) - 1 DO
BEGIN
PutByte (0);
PutByte (0);
PutByte (0)
END;
code := 'BODY';
PutBytes (4, @code);
WriteBody (doc, bounds);
SeekTo (4);
PutLong (GetFileLength - 8)
END;