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
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <vcl/pngread.hxx>
#include <cmath>
#include <rtl/crc.h>
#include <rtl/alloc.h>
#include <tools/zcodec.hxx>
#include <tools/stream.hxx>
#include <vcl/bmpacc.hxx>
#include <vcl/svapp.hxx>
#include <vcl/alpha.hxx>
#include <osl/endian.h>
#define PNGCHUNK_IHDR 0x49484452
#define PNGCHUNK_PLTE 0x504c5445
#define PNGCHUNK_IDAT 0x49444154
#define PNGCHUNK_IEND 0x49454e44
#define PNGCHUNK_bKGD 0x624b4744
#define PNGCHUNK_gAMA 0x67414d41
#define PNGCHUNK_pHYs 0x70485973
#define PNGCHUNK_tRNS 0x74524e53
#define VIEWING_GAMMA 2.35
#define DISPLAY_GAMMA 1.0
namespace vcl
{
static const sal_uInt8 mpDefaultColorTable[ 256 ] =
{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
};
class PNGReaderImpl
{
private:
SvStream& mrPNGStream;
sal_uInt16 mnOrigStreamMode;
std::vector< vcl::PNGReader::ChunkData > maChunkSeq;
std::vector< vcl::PNGReader::ChunkData >::iterator maChunkIter;
std::vector< sal_uInt8 >::iterator maDataIter;
Bitmap* mpBmp;
BitmapWriteAccess* mpAcc;
Bitmap* mpMaskBmp;
AlphaMask* mpAlphaMask;
BitmapWriteAccess* mpMaskAcc;
ZCodec* mpZCodec;
sal_uInt8* mpInflateInBuf; // as big as the size of a scanline + alphachannel + 1
sal_uInt8* mpScanPrior; // pointer to the latest scanline
sal_uInt8* mpTransTab; // for transparency in images with palette colortype
sal_uInt8* mpScanCurrent; // pointer into the current scanline
sal_uInt8* mpColorTable;
sal_Size mnStreamSize; // estimate of PNG file size
sal_uInt32 mnChunkType; // Type of current PNG chunk
sal_Int32 mnChunkLen; // Length of current PNG chunk
Size maOrigSize; // pixel size of the full image
Size maTargetSize; // pixel size of the result image
Size maPhysSize; // preferred size in MAP_100TH_MM units
sal_uInt32 mnBPP; // number of bytes per pixel
sal_uInt32 mnScansize; // max size of scanline
sal_uInt32 mnYpos; // latest y position in full image
int mnPass; // if interlaced the latest pass ( 1..7 ) else 7
sal_uInt32 mnXStart; // the starting X for the current pass
sal_uInt32 mnXAdd; // the increment for input images X coords for the current pass
sal_uInt32 mnYAdd; // the increment for input images Y coords for the current pass
int mnPreviewShift; // shift to convert orig image coords into preview image coords
int mnPreviewMask; // == ((1 << mnPreviewShift) - 1)
sal_uInt16 mnTargetDepth; // pixel depth of target bitmap
sal_uInt8 mnTransRed;
sal_uInt8 mnTransGreen;
sal_uInt8 mnTransBlue;
sal_uInt8 mnPngDepth; // pixel depth of PNG data
sal_uInt8 mnColorType;
sal_uInt8 mnCompressionType;
sal_uInt8 mnFilterType;
sal_uInt8 mnInterlaceType;
BitmapColor mcTranspColor; // transparency mask's transparency "color"
BitmapColor mcOpaqueColor; // transparency mask's opaque "color"
bool mbTransparent; // graphic includes an tRNS Chunk or an alpha Channel
bool mbAlphaChannel; // is true for ColorType 4 and 6
bool mbRGBTriple;
bool mbPalette; // false if we need a Palette
bool mbGrayScale;
bool mbzCodecInUse;
bool mbStatus;
bool mbIDAT; // true if finished with enough IDAT chunks
bool mbGamma; // true if Gamma Correction available
bool mbpHYs; // true if pysical size of pixel available
bool mbIgnoreGammaChunk;
#if OSL_DEBUG_LEVEL > 0
// do some checks in debug mode
sal_Int32 mnAllocSizeScanline;
sal_Int32 mnAllocSizeScanlineAlpha;
#endif
// the temporary Scanline (and alpha) for direct scanline copy to Bitmap
sal_uInt8* mpScanline;
sal_uInt8* mpScanlineAlpha;
bool ReadNextChunk();
void ReadRemainingChunks();
void ImplSetPixel( sal_uInt32 y, sal_uInt32 x, const BitmapColor & );
void ImplSetPixel( sal_uInt32 y, sal_uInt32 x, sal_uInt8 nPalIndex );
void ImplSetTranspPixel( sal_uInt32 y, sal_uInt32 x, const BitmapColor &, bool bTrans );
void ImplSetAlphaPixel( sal_uInt32 y, sal_uInt32 x, sal_uInt8 nPalIndex, sal_uInt8 nAlpha );
void ImplSetAlphaPixel( sal_uInt32 y, sal_uInt32 x, const BitmapColor&, sal_uInt8 nAlpha );
void ImplReadIDAT();
bool ImplPreparePass();
void ImplApplyFilter();
void ImplDrawScanline( sal_uInt32 nXStart, sal_uInt32 nXAdd );
bool ImplReadTransparent();
void ImplGetGamma();
void ImplGetBackground();
sal_uInt8 ImplScaleColor();
bool ImplReadHeader( const Size& rPreviewSizeHint );
bool ImplReadPalette();
void ImplGetGrayPalette( sal_uInt16 );
sal_uInt32 ImplReadsal_uInt32();
public:
PNGReaderImpl( SvStream& );
~PNGReaderImpl();
BitmapEx GetBitmapEx( const Size& rPreviewSizeHint );
const std::vector< PNGReader::ChunkData >& GetAllChunks();
void SetIgnoreGammaChunk( bool bIgnore ){ mbIgnoreGammaChunk = bIgnore; };
};
PNGReaderImpl::PNGReaderImpl( SvStream& rPNGStream )
: mrPNGStream( rPNGStream ),
mpBmp ( NULL ),
mpAcc ( NULL ),
mpMaskBmp ( NULL ),
mpAlphaMask ( NULL ),
mpMaskAcc ( NULL ),
mpZCodec ( new ZCodec( DEFAULT_IN_BUFSIZE, DEFAULT_OUT_BUFSIZE, MAX_MEM_USAGE ) ),
mpInflateInBuf ( NULL ),
mpScanPrior ( NULL ),
mpTransTab ( NULL ),
mpScanCurrent ( NULL ),
mpColorTable ( (sal_uInt8*) mpDefaultColorTable ),
mnPass ( 0 ),
mbTransparent( false ),
mbAlphaChannel( false ),
mbRGBTriple( false ),
mbPalette( false ),
mbGrayScale( false ),
mbzCodecInUse ( false ),
mbStatus( true ),
mbIDAT( false ),
mbGamma ( false ),
mbpHYs ( false ),
mbIgnoreGammaChunk ( false ),
#if OSL_DEBUG_LEVEL > 0
mnAllocSizeScanline(0),
mnAllocSizeScanlineAlpha(0),
#endif
mpScanline(0),
mpScanlineAlpha(0)
{
// prepare the PNG data stream
mnOrigStreamMode = mrPNGStream.GetNumberFormatInt();
mrPNGStream.SetNumberFormatInt( NUMBERFORMAT_INT_BIGENDIAN );
// prepare the chunk reader
maChunkSeq.reserve( 16 );
maChunkIter = maChunkSeq.begin();
// estimate PNG file size (to allow sanity checks)
const sal_Size nStreamPos = mrPNGStream.Tell();
mrPNGStream.Seek( STREAM_SEEK_TO_END );
mnStreamSize = mrPNGStream.Tell();
mrPNGStream.Seek( nStreamPos );
// check the PNG header magic
sal_uInt32 nDummy = 0;
mrPNGStream.ReadUInt32( nDummy );
mbStatus = (nDummy == 0x89504e47);
mrPNGStream.ReadUInt32( nDummy );
mbStatus = (nDummy == 0x0d0a1a0a) && mbStatus;
mnPreviewShift = 0;
mnPreviewMask = (1 << mnPreviewShift) - 1;
}
PNGReaderImpl::~PNGReaderImpl()
{
mrPNGStream.SetNumberFormatInt( mnOrigStreamMode );
if ( mbzCodecInUse )
mpZCodec->EndCompression();
if( mpColorTable != mpDefaultColorTable )
delete[] mpColorTable;
delete mpBmp;
delete mpAlphaMask;
delete mpMaskBmp;
delete[] mpTransTab;
delete[] mpInflateInBuf;
delete[] mpScanPrior;
delete mpZCodec;
delete[] mpScanline;
delete[] mpScanlineAlpha;
}
bool PNGReaderImpl::ReadNextChunk()
{
if( maChunkIter == maChunkSeq.end() )
{
// get the next chunk from the stream
// unless we are at the end of the PNG stream
if( mrPNGStream.IsEof() || (mrPNGStream.GetError() != ERRCODE_NONE) )
return false;
if( !maChunkSeq.empty() && (maChunkSeq.back().nType == PNGCHUNK_IEND) )
return false;
PNGReader::ChunkData aDummyChunk;
maChunkIter = maChunkSeq.insert( maChunkSeq.end(), aDummyChunk );
PNGReader::ChunkData& rChunkData = *maChunkIter;
// read the chunk header
mrPNGStream.ReadInt32( mnChunkLen ).ReadUInt32( mnChunkType );
rChunkData.nType = mnChunkType;
// fdo#61847 truncate over-long, trailing chunks
const sal_Size nStreamPos = mrPNGStream.Tell();
if( mnChunkLen < 0 || nStreamPos + mnChunkLen >= mnStreamSize )
mnChunkLen = mnStreamSize - nStreamPos;
// calculate chunktype CRC (swap it back to original byte order)
sal_uInt32 nChunkType = mnChunkType;
#if defined(__LITTLEENDIAN) || defined(OSL_LITENDIAN)
nChunkType = OSL_SWAPDWORD( nChunkType );
#endif
sal_uInt32 nCRC32 = rtl_crc32( 0, &nChunkType, 4 );
// read the chunk data and check the CRC
if( mnChunkLen && !mrPNGStream.IsEof() )
{
rChunkData.aData.resize( mnChunkLen );
sal_Int32 nBytesRead = 0;
do {
sal_uInt8* pPtr = &rChunkData.aData[ nBytesRead ];
nBytesRead += mrPNGStream.Read( pPtr, mnChunkLen - nBytesRead );
} while ( ( nBytesRead < mnChunkLen ) && ( mrPNGStream.GetError() == ERRCODE_NONE ) );
nCRC32 = rtl_crc32( nCRC32, &rChunkData.aData[ 0 ], mnChunkLen );
maDataIter = rChunkData.aData.begin();
}
sal_uInt32 nCheck(0);
mrPNGStream.ReadUInt32( nCheck );
if( nCRC32 != nCheck )
return false;
}
else
{
// the next chunk was already read
mnChunkType = (*maChunkIter).nType;
mnChunkLen = (*maChunkIter).aData.size();
maDataIter = (*maChunkIter).aData.begin();
}
++maChunkIter;
if( mnChunkType == PNGCHUNK_IEND )
return false;
return true;
}
// read the remaining chunks from mrPNGStream
void PNGReaderImpl::ReadRemainingChunks()
{
while( ReadNextChunk() ) ;
}
const std::vector< vcl::PNGReader::ChunkData >& PNGReaderImpl::GetAllChunks()
{
ReadRemainingChunks();
return maChunkSeq;
}
BitmapEx PNGReaderImpl::GetBitmapEx( const Size& rPreviewSizeHint )
{
// reset to the first chunk
maChunkIter = maChunkSeq.begin();
// first chunk must be IDHR
if( mbStatus && ReadNextChunk() )
{
if (mnChunkType == PNGCHUNK_IHDR)
mbStatus = ImplReadHeader( rPreviewSizeHint );
else
mbStatus = false;
}
// parse the remaining chunks
bool bRetFromNextChunk;
while( mbStatus && !mbIDAT && (bRetFromNextChunk = ReadNextChunk()) )
{
switch( mnChunkType )
{
case PNGCHUNK_IHDR :
{
mbStatus = false; //IHDR should only appear as the first chunk
}
break;
case PNGCHUNK_gAMA : // the gamma chunk must precede
{ // the 'IDAT' and also the 'PLTE'(if available )
if ( !mbIgnoreGammaChunk && !mbIDAT )
ImplGetGamma();
}
break;
case PNGCHUNK_PLTE :
{
if ( !mbPalette )
mbStatus = ImplReadPalette();
}
break;
case PNGCHUNK_tRNS :
{
if ( !mbIDAT ) // the tRNS chunk must precede the IDAT
mbStatus = ImplReadTransparent();
}
break;
case PNGCHUNK_bKGD : // the background chunk must appear
{
if ( !mbIDAT && mbPalette ) // before the 'IDAT' and after the
ImplGetBackground(); // PLTE(if available ) chunk.
}
break;
case PNGCHUNK_IDAT :
{
if ( !mpInflateInBuf ) // taking care that the header has properly been read
mbStatus = false;
else if ( !mbIDAT ) // the gfx is finished, but there may be left a zlibCRC of about 4Bytes
ImplReadIDAT();
}
break;
case PNGCHUNK_pHYs :
{
if ( !mbIDAT && mnChunkLen == 9 )
{
sal_uInt32 nXPixelPerMeter = ImplReadsal_uInt32();
sal_uInt32 nYPixelPerMeter = ImplReadsal_uInt32();
sal_uInt8 nUnitSpecifier = *maDataIter++;
if( (nUnitSpecifier == 1) && nXPixelPerMeter && nYPixelPerMeter )
{
mbpHYs = true;
// convert into MAP_100TH_MM
maPhysSize.Width() = (sal_Int32)( (100000.0 * maOrigSize.Width()) / nXPixelPerMeter );
maPhysSize.Height() = (sal_Int32)( (100000.0 * maOrigSize.Height()) / nYPixelPerMeter );
}
}
}
break;
case PNGCHUNK_IEND:
mbStatus = mbIDAT; // there is a problem if the image is not complete yet
break;
}
}
// release write access of the bitmaps
if ( mpAcc )
mpBmp->ReleaseAccess( mpAcc ), mpAcc = NULL;
if ( mpMaskAcc )
{
if ( mpAlphaMask )
mpAlphaMask->ReleaseAccess( mpMaskAcc );
else if ( mpMaskBmp )
mpMaskBmp->ReleaseAccess( mpMaskAcc );
mpMaskAcc = NULL;
}
// return the resulting BitmapEx
BitmapEx aRet;
if( !mbStatus || !mbIDAT )
aRet.Clear();
else
{
if ( mpAlphaMask )
aRet = BitmapEx( *mpBmp, *mpAlphaMask );
else if ( mpMaskBmp )
aRet = BitmapEx( *mpBmp, *mpMaskBmp );
else
aRet = *mpBmp;
if ( mbpHYs && maPhysSize.Width() && maPhysSize.Height() )
{
aRet.SetPrefMapMode( MAP_100TH_MM );
aRet.SetPrefSize( maPhysSize );
}
}
return aRet;
}
bool PNGReaderImpl::ImplReadHeader( const Size& rPreviewSizeHint )
{
if( mnChunkLen < 13 )
return false;
maOrigSize.Width() = ImplReadsal_uInt32();
maOrigSize.Height() = ImplReadsal_uInt32();
if (maOrigSize.Width() <= 0 || maOrigSize.Height() <= 0)
return false;
mnPngDepth = *(maDataIter++);
mnColorType = *(maDataIter++);
mnCompressionType = *(maDataIter++);
if( mnCompressionType != 0 ) // unknown compression type
return false;
mnFilterType = *(maDataIter++);
if( mnFilterType != 0 ) // unknown filter type
return false;
mnInterlaceType = *(maDataIter++);
switch ( mnInterlaceType ) // filter type valid ?
{
case 0 : // progressive image
mnPass = 7;
break;
case 1 : // Adam7-interlaced image
mnPass = 0;
break;
default:
return false;
}
mbPalette = true;
mbIDAT = mbAlphaChannel = mbTransparent = false;
mbGrayScale = mbRGBTriple = false;
mnTargetDepth = mnPngDepth;
sal_uInt64 nScansize64 = ( ( static_cast< sal_uInt64 >( maOrigSize.Width() ) * mnPngDepth ) + 7 ) >> 3;
// valid color types are 0,2,3,4 & 6
switch ( mnColorType )
{
case 0 : // each pixel is a grayscale
{
switch ( mnPngDepth )
{
case 2 : // 2bit target not available -> use four bits
mnTargetDepth = 4; // we have to expand the bitmap
mbGrayScale = true;
break;
case 16 :
mnTargetDepth = 8; // we have to reduce the bitmap
// fall through
case 1 :
case 4 :
case 8 :
mbGrayScale = true;
break;
default :
return false;
}
}
break;
case 2 : // each pixel is an RGB triple
{
mbRGBTriple = true;
nScansize64 *= 3;
switch ( mnPngDepth )
{
case 16 : // we have to reduce the bitmap
case 8 :
mnTargetDepth = 24;
break;
default :
return false;
}
}
break;
case 3 : // each pixel is a palette index
{
switch ( mnPngDepth )
{
case 2 :
mnTargetDepth = 4; // we have to expand the bitmap
// fall through
case 1 :
case 4 :
case 8 :
mbPalette = false;
break;
default :
return false;
}
}
break;
case 4 : // each pixel is a grayscale sample followed by an alpha sample
{
nScansize64 *= 2;
mbAlphaChannel = true;
switch ( mnPngDepth )
{
case 16 :
mnTargetDepth = 8; // we have to reduce the bitmap
case 8 :
mbGrayScale = true;
break;
default :
return false;
}
}
break;
case 6 : // each pixel is an RGB triple followed by an alpha sample
{
mbRGBTriple = true;
nScansize64 *= 4;
mbAlphaChannel = true;
switch (mnPngDepth )
{
case 16 : // we have to reduce the bitmap
case 8 :
mnTargetDepth = 24;
break;
default :
return false;
}
}
break;
default :
return false;
}
mnBPP = static_cast< sal_uInt32 >( nScansize64 / maOrigSize.Width() );
if ( !mnBPP )
mnBPP = 1;
nScansize64++; // each scanline includes one filterbyte
if ( nScansize64 > SAL_MAX_UINT32 )
return false;
mnScansize = static_cast< sal_uInt32 >( nScansize64 );
// calculate target size from original size and the preview hint
if( rPreviewSizeHint.Width() || rPreviewSizeHint.Height() )
{
Size aPreviewSize( rPreviewSizeHint.Width(), rPreviewSizeHint.Height() );
maTargetSize = maOrigSize;
if( aPreviewSize.Width() == 0 ) {
aPreviewSize.setWidth( ( maOrigSize.Width()*aPreviewSize.Height() )/maOrigSize.Height() );
if( aPreviewSize.Width() <= 0 )
aPreviewSize.setWidth( 1 );
} else if( aPreviewSize.Height() == 0 ) {
aPreviewSize.setHeight( ( maOrigSize.Height()*aPreviewSize.Width() )/maOrigSize.Width() );
if( aPreviewSize.Height() <= 0 )
aPreviewSize.setHeight( 1 );
}
if( aPreviewSize.Width() < maOrigSize.Width() && aPreviewSize.Height() < maOrigSize.Height() ) {
OSL_TRACE("preview size %ldx%ld", aPreviewSize.Width(), aPreviewSize.Height() );
for( int i = 1; i < 5; ++i )
{
if( (maTargetSize.Width() >> i) < aPreviewSize.Width() )
break;
if( (maTargetSize.Height() >> i) < aPreviewSize.Height() )
break;
mnPreviewShift = i;
}
mnPreviewMask = (1 << mnPreviewShift) - 1;
}
}
maTargetSize.Width() = (maOrigSize.Width() + mnPreviewMask) >> mnPreviewShift;
maTargetSize.Height() = (maOrigSize.Height() + mnPreviewMask) >> mnPreviewShift;
//round bits up to nearest multiple of 8 and divide by 8 to get num of bytes per pixel
int nBytesPerPixel = ((mnTargetDepth + 7) & ~7)/8;
//stupidly big, forget about it
if (maTargetSize.Width() >= SAL_MAX_INT32 / nBytesPerPixel / maTargetSize.Height())
{
SAL_WARN( "vcl.gdi", "overlarge png dimensions: " <<
maTargetSize.Width() << " x " << maTargetSize.Height() << " depth: " << mnTargetDepth);
return false;
}
// TODO: switch between both scanlines instead of copying
mpInflateInBuf = new (std::nothrow) sal_uInt8[ mnScansize ];
mpScanCurrent = mpInflateInBuf;
mpScanPrior = new (std::nothrow) sal_uInt8[ mnScansize ];
if ( !mpInflateInBuf || !mpScanPrior )
return false;
mpBmp = new Bitmap( maTargetSize, mnTargetDepth );
mpAcc = mpBmp->AcquireWriteAccess();
if( !mpAcc )
return false;
if ( mbAlphaChannel )
{
mpAlphaMask = new AlphaMask( maTargetSize );
mpAlphaMask->Erase( 128 );
mpMaskAcc = mpAlphaMask->AcquireWriteAccess();
if( !mpMaskAcc )
return false;
}
if ( mbGrayScale )
ImplGetGrayPalette( mnPngDepth );
ImplPreparePass();
return true;
}
void PNGReaderImpl::ImplGetGrayPalette( sal_uInt16 nBitDepth )
{
if( nBitDepth > 8 )
nBitDepth = 8;
sal_uInt16 nPaletteEntryCount = 1 << nBitDepth;
sal_uInt32 nAdd = nBitDepth ? 256 / (nPaletteEntryCount - 1) : 0;
// no bitdepth==2 available
// but bitdepth==4 with two unused bits is close enough
if( nBitDepth == 2 )
nPaletteEntryCount = 16;
mpAcc->SetPaletteEntryCount( nPaletteEntryCount );
for ( sal_uInt32 i = 0, nStart = 0; nStart < 256; i++, nStart += nAdd )
mpAcc->SetPaletteColor( (sal_uInt16)i, BitmapColor( mpColorTable[ nStart ],
mpColorTable[ nStart ], mpColorTable[ nStart ] ) );
}
bool PNGReaderImpl::ImplReadPalette()
{
sal_uInt16 nCount = static_cast<sal_uInt16>( mnChunkLen / 3 );
if ( ( ( mnChunkLen % 3 ) == 0 ) && ( ( 0 < nCount ) && ( nCount <= 256 ) ) && mpAcc )
{
mbPalette = true;
mpAcc->SetPaletteEntryCount( (sal_uInt16) nCount );
for ( sal_uInt16 i = 0; i < nCount; i++ )
{
sal_uInt8 nRed = mpColorTable[ *maDataIter++ ];
sal_uInt8 nGreen = mpColorTable[ *maDataIter++ ];
sal_uInt8 nBlue = mpColorTable[ *maDataIter++ ];
mpAcc->SetPaletteColor( i, Color( nRed, nGreen, nBlue ) );
}
}
else
mbStatus = false;
return mbStatus;
}
bool PNGReaderImpl::ImplReadTransparent()
{
bool bNeedAlpha = false;
if ( mpTransTab == NULL )
{
switch ( mnColorType )
{
case 0 :
{
if ( mnChunkLen == 2 )
{
mpTransTab = new sal_uInt8[ 256 ];
memset( mpTransTab, 0xff, 256);
// color type 0 and 4 is always greyscale,
// so the return value can be used as index
sal_uInt8 nIndex = ImplScaleColor();
mpTransTab[ nIndex ] = 0;
mbTransparent = true;
}
}
break;
case 2 :
{
if ( mnChunkLen == 6 )
{
mnTransRed = ImplScaleColor();
mnTransGreen = ImplScaleColor();
mnTransBlue = ImplScaleColor();
mbTransparent = true;
}
}
break;
case 3 :
{
if ( mnChunkLen <= 256 )
{
mbTransparent = true;
mpTransTab = new sal_uInt8 [ 256 ];
memset( mpTransTab, 0xff, 256 );
if (mnChunkLen > 0)
{
memcpy( mpTransTab, &(*maDataIter), mnChunkLen );
maDataIter += mnChunkLen;
// need alpha transparency if not on/off masking
for( int i = 0; i < mnChunkLen; ++i )
bNeedAlpha |= (mpTransTab[i]!=0x00) && (mpTransTab[i]!=0xFF);
}
}
}
break;
}
}
if( mbTransparent && !mbAlphaChannel && !mpMaskBmp )
{
if( bNeedAlpha)
{
mpAlphaMask = new AlphaMask( maTargetSize );
mpMaskAcc = mpAlphaMask->AcquireWriteAccess();
}
else
{
mpMaskBmp = new Bitmap( maTargetSize, 1 );
mpMaskAcc = mpMaskBmp->AcquireWriteAccess();
}
mbTransparent = (mpMaskAcc != NULL);
if( !mbTransparent )
return false;
mcOpaqueColor = BitmapColor( 0x00 );
mcTranspColor = BitmapColor( 0xFF );
mpMaskAcc->Erase( 0x00 );
}
return true;
}
void PNGReaderImpl::ImplGetGamma()
{
if( mnChunkLen < 4 )
return;
sal_uInt32 nGammaValue = ImplReadsal_uInt32();
double fGamma = ( ( VIEWING_GAMMA / DISPLAY_GAMMA ) * ( (double)nGammaValue / 100000 ) );
double fInvGamma = ( fGamma <= 0.0 || fGamma > 10.0 ) ? 1.0 : ( 1.0 / fGamma );
if ( fInvGamma != 1.0 )
{
mbGamma = true;
if ( mpColorTable == mpDefaultColorTable )
mpColorTable = new sal_uInt8[ 256 ];
for ( sal_Int32 i = 0; i < 256; i++ )
mpColorTable[ i ] = (sal_uInt8)(pow((double)i/255.0, fInvGamma) * 255.0 + 0.5);
if ( mbGrayScale )
ImplGetGrayPalette( mnPngDepth );
}
}
void PNGReaderImpl::ImplGetBackground()
{
switch ( mnColorType )
{
case 3 :
{
if ( mnChunkLen == 1 )
{
sal_uInt16 nCol = *maDataIter++;
if ( nCol < mpAcc->GetPaletteEntryCount() )
{
mpAcc->Erase( mpAcc->GetPaletteColor( (sal_uInt8)nCol ) );
break;
}
}
}
break;
case 0 :
case 4 :
{
if ( mnChunkLen == 2 )
{
// the color type 0 and 4 is always greyscale,
// so the return value can be used as index
sal_uInt8 nIndex = ImplScaleColor();
mpAcc->Erase( mpAcc->GetPaletteColor( nIndex ) );
}
}
break;
case 2 :
case 6 :
{
if ( mnChunkLen == 6 )
{
sal_uInt8 nRed = ImplScaleColor();
sal_uInt8 nGreen = ImplScaleColor();
sal_uInt8 nBlue = ImplScaleColor();
mpAcc->Erase( Color( nRed, nGreen, nBlue ) );
}
}
break;
}
}
// for color type 0 and 4 (greyscale) the return value is always index to the color
// 2 and 6 (RGB) the return value is always the 8 bit color component
sal_uInt8 PNGReaderImpl::ImplScaleColor()
{
sal_uInt32 nMask = ( ( 1 << mnPngDepth ) - 1 );
sal_uInt16 nCol = ( *maDataIter++ << 8 );
nCol += *maDataIter++ & (sal_uInt16)nMask;
if ( mnPngDepth > 8 ) // convert 16bit graphics to 8
nCol >>= 8;
return (sal_uInt8) nCol;
}
// ImplReadIDAT reads as much image data as needed
void PNGReaderImpl::ImplReadIDAT()
{
if( mnChunkLen > 0 )
{
if ( !mbzCodecInUse )
{
mbzCodecInUse = true;
mpZCodec->BeginCompression( ZCODEC_PNG_DEFAULT );
}
mpZCodec->SetBreak( mnChunkLen );
SvMemoryStream aIStrm( &(*maDataIter), mnChunkLen, STREAM_READ );
while ( ( mpZCodec->GetBreak() ) )
{
// get bytes needed to fill the current scanline
sal_Int32 nToRead = mnScansize - (mpScanCurrent - mpInflateInBuf);
sal_Int32 nRead = mpZCodec->ReadAsynchron( aIStrm, mpScanCurrent, nToRead );
if ( nRead < 0 )
{
mbStatus = false;
break;
}
if ( nRead < nToRead )
{
mpScanCurrent += nRead; // more ZStream data in the next IDAT chunk
break;
}
else // this scanline is Finished
{
mpScanCurrent = mpInflateInBuf;
ImplApplyFilter();
ImplDrawScanline( mnXStart, mnXAdd );
mnYpos += mnYAdd;
}
if ( mnYpos >= (sal_uInt32)maOrigSize.Height() )
{
if( (mnPass < 7) && mnInterlaceType )
if( ImplPreparePass() )
continue;
mbIDAT = true;
break;
}
}
}
if( mbIDAT )
{
mpZCodec->EndCompression();
mbzCodecInUse = false;
}
}
bool PNGReaderImpl::ImplPreparePass()
{
struct InterlaceParams{ int mnXStart, mnYStart, mnXAdd, mnYAdd; };
static const InterlaceParams aInterlaceParams[8] =
{
// non-interlaced
{ 0, 0, 1, 1 },
// Adam7-interlaced
{ 0, 0, 8, 8 }, // pass 1
{ 4, 0, 8, 8 }, // pass 2
{ 0, 4, 4, 8 }, // pass 3
{ 2, 0, 4, 4 }, // pass 4
{ 0, 2, 2, 4 }, // pass 5
{ 1, 0, 2, 2 }, // pass 6
{ 0, 1, 1, 2 } // pass 7
};
const InterlaceParams* pParam = &aInterlaceParams[ 0 ];
if( mnInterlaceType )
{
while( ++mnPass <= 7 )
{
pParam = &aInterlaceParams[ mnPass ];
// skip this pass if the original image is too small for it
if( (pParam->mnXStart < maOrigSize.Width())
&& (pParam->mnYStart < maOrigSize.Height()) )
break;
}
if( mnPass > 7 )
return false;
// skip the last passes if possible (for scaled down target images)
if( mnPreviewMask & (pParam->mnXStart | pParam->mnYStart) )
return false;
}
mnYpos = pParam->mnYStart;
mnXStart = pParam->mnXStart;
mnXAdd = pParam->mnXAdd;
mnYAdd = pParam->mnYAdd;
// in Interlace mode the size of scanline is not constant
// so first we calculate the number of entrys
long nScanWidth = (maOrigSize.Width() - mnXStart + mnXAdd - 1) / mnXAdd;
mnScansize = nScanWidth;
if( mbRGBTriple )
mnScansize = 3 * nScanWidth;
if( mbAlphaChannel )
mnScansize += nScanWidth;
// convert to width in bytes
mnScansize = ( mnScansize*mnPngDepth + 7 ) >> 3;
++mnScansize; // scan size also needs room for the filtertype byte
memset( mpScanPrior, 0, mnScansize );
return true;
}
// ImplApplyFilter writes the complete Scanline (nY)
// in interlace mode the parameter nXStart and nXAdd are non-zero
void PNGReaderImpl::ImplApplyFilter()
{
OSL_ASSERT( mnScansize >= mnBPP + 1 );
const sal_uInt8* const pScanEnd = mpInflateInBuf + mnScansize;
sal_uInt8 nFilterType = *mpInflateInBuf; // the filter type may change each scanline
switch ( nFilterType )
{
default: // unknown Scanline Filter Type
case 0: // Filter Type "None"
// we let the pixels pass and display the data unfiltered
break;
case 1: // Scanline Filter Type "Sub"
{
sal_uInt8* p1 = mpInflateInBuf + 1;
const sal_uInt8* p2 = p1;
p1 += mnBPP;
// use left pixels
do
*p1 = static_cast<sal_uInt8>( *p1 + *(p2++) );
while( ++p1 < pScanEnd );
}
break;
case 2: // Scanline Filter Type "Up"
{
sal_uInt8* p1 = mpInflateInBuf + 1;
const sal_uInt8* p2 = mpScanPrior + 1;
// use pixels from prior line
while( p1 < pScanEnd )
{
*p1 = static_cast<sal_uInt8>( *p1 + *(p2++) );
++p1;
}
}
break;
case 3: // Scanline Filter Type "Average"
{
sal_uInt8* p1 = mpInflateInBuf + 1;
const sal_uInt8* p2 = mpScanPrior + 1;
const sal_uInt8* p3 = p1;
// use one pixel from prior line
for( int n = mnBPP; --n >= 0; ++p1, ++p2)
*p1 = static_cast<sal_uInt8>( *p1 + (*p2 >> 1) );
// predict by averaging the left and prior line pixels
while( p1 < pScanEnd )
{
*p1 = static_cast<sal_uInt8>( *p1 + ((*(p2++) + *(p3++)) >> 1) );
++p1;
}
}
break;
case 4: // Scanline Filter Type "PaethPredictor"
{
sal_uInt8* p1 = mpInflateInBuf + 1;
const sal_uInt8* p2 = mpScanPrior + 1;
const sal_uInt8* p3 = p1;
const sal_uInt8* p4 = p2;
// use one pixel from prior line
for( int n = mnBPP; --n >= 0; ++p1)
*p1 = static_cast<sal_uInt8>( *p1 + *(p2++) );
// predict by using the left and the prior line pixels
while( p1 < pScanEnd )
{
int na = *(p2++);
int nb = *(p3++);
int nc = *(p4++);
int npa = nb - (int)nc;
int npb = na - (int)nc;
int npc = npa + npb;
if( npa < 0 )
npa =-npa;
if( npb < 0 )
npb =-npb;
if( npc < 0 )
npc =-npc;
if( npa > npb )
na = nb, npa = npb;
if( npa > npc )
na = nc;
*p1 = static_cast<sal_uInt8>( *p1 + na );
++p1;
}
}
break;
}
memcpy( mpScanPrior, mpInflateInBuf, mnScansize );
}
// ImplDrawScanlines draws the complete Scanline (nY) into the target bitmap
// In interlace mode the parameter nXStart and nXAdd append to the currently used pass
void PNGReaderImpl::ImplDrawScanline( sal_uInt32 nXStart, sal_uInt32 nXAdd )
{
// optimization for downscaling
if( mnYpos & mnPreviewMask )
return;
if( nXStart & mnPreviewMask )
return;
// convert nY to pixel units in the target image
// => TODO; also do this for nX here instead of in the ImplSet*Pixel() methods
const sal_uInt32 nY = mnYpos >> mnPreviewShift;
const sal_uInt8* pTmp = mpInflateInBuf + 1;
if ( mpAcc->HasPalette() ) // alphachannel is not allowed by pictures including palette entries
{
switch ( mpAcc->GetBitCount() )
{
case 1 :
{
if ( mbTransparent )
{
for ( sal_Int32 nX = nXStart, nShift = 0; nX < maOrigSize.Width(); nX += nXAdd )
{
sal_uInt8 nCol;
nShift = (nShift - 1) & 7;
if ( nShift == 0 )
nCol = *(pTmp++);
else
nCol = static_cast<sal_uInt8>( *pTmp >> nShift );
nCol &= 1;
ImplSetAlphaPixel( nY, nX, nCol, mpTransTab[ nCol ] );
}
}
else
{ // BMP_FORMAT_1BIT_MSB_PAL
for ( sal_Int32 nX = nXStart, nShift = 0; nX < maOrigSize.Width(); nX += nXAdd )
{
nShift = (nShift - 1) & 7;
sal_uInt8 nCol;
if ( nShift == 0 )
nCol = *(pTmp++);
else
nCol = static_cast<sal_uInt8>( *pTmp >> nShift );
nCol &= 1;
ImplSetPixel( nY, nX, nCol );
}
}
}
break;
case 4 :
{
if ( mbTransparent )
{
if ( mnPngDepth == 4 ) // check if source has a two bit pixel format
{
for ( sal_Int32 nX = nXStart, nXIndex = 0; nX < maOrigSize.Width(); nX += nXAdd, ++nXIndex )
{
if( nXIndex & 1 )
{
ImplSetAlphaPixel( nY, nX, *pTmp & 0x0f, mpTransTab[ *pTmp & 0x0f ] );
pTmp++;
}
else
{
ImplSetAlphaPixel( nY, nX, ( *pTmp >> 4 ) & 0x0f, mpTransTab[ *pTmp >> 4 ] );
}
}
}
else // if ( mnPngDepth == 2 )
{
for ( sal_Int32 nX = nXStart, nXIndex = 0; nX < maOrigSize.Width(); nX += nXAdd, nXIndex++ )
{
sal_uInt8 nCol;
switch( nXIndex & 3 )
{
case 0 :
nCol = *pTmp >> 6;
break;
case 1 :
nCol = ( *pTmp >> 4 ) & 0x03 ;
break;
case 2 :
nCol = ( *pTmp >> 2 ) & 0x03;
break;
case 3 :
nCol = ( *pTmp++ ) & 0x03;
break;
default: // get rid of nCol uninitialized warning
nCol = 0;
break;
}
ImplSetAlphaPixel( nY, nX, nCol, mpTransTab[ nCol ] );
}
}
}
else
{
if ( mnPngDepth == 4 ) // maybe the source is a two bitmap graphic
{ // BMP_FORMAT_4BIT_LSN_PAL
for ( sal_Int32 nX = nXStart, nXIndex = 0; nX < maOrigSize.Width(); nX += nXAdd, nXIndex++ )
{
if( nXIndex & 1 )
ImplSetPixel( nY, nX, *pTmp++ & 0x0f );
else
ImplSetPixel( nY, nX, ( *pTmp >> 4 ) & 0x0f );
}
}
else // if ( mnPngDepth == 2 )
{
for ( sal_Int32 nX = nXStart, nXIndex = 0; nX < maOrigSize.Width(); nX += nXAdd, nXIndex++ )
{
switch( nXIndex & 3 )
{
case 0 :
ImplSetPixel( nY, nX, *pTmp >> 6 );
break;
case 1 :
ImplSetPixel( nY, nX, ( *pTmp >> 4 ) & 0x03 );
break;
case 2 :
ImplSetPixel( nY, nX, ( *pTmp >> 2 ) & 0x03 );
break;
case 3 :
ImplSetPixel( nY, nX, *pTmp++ & 0x03 );
break;
}
}
}
}
}
break;
case 8 :
{
if ( mbAlphaChannel )
{
if ( mnPngDepth == 8 ) // maybe the source is a 16 bit grayscale
{
for ( sal_Int32 nX = nXStart; nX < maOrigSize.Width(); nX += nXAdd, pTmp += 2 )
ImplSetAlphaPixel( nY, nX, pTmp[ 0 ], pTmp[ 1 ] );
}
else
{
for ( sal_Int32 nX = nXStart; nX < maOrigSize.Width(); nX += nXAdd, pTmp += 4 )
ImplSetAlphaPixel( nY, nX, pTmp[ 0 ], pTmp[ 2 ] );
}
}
else if ( mbTransparent )
{
if ( mnPngDepth == 8 ) // maybe the source is a 16 bit grayscale
{
for ( sal_Int32 nX = nXStart; nX < maOrigSize.Width(); nX += nXAdd, pTmp++ )
ImplSetAlphaPixel( nY, nX, *pTmp, mpTransTab[ *pTmp ] );
}
else
{
for ( sal_Int32 nX = nXStart; nX < maOrigSize.Width(); nX += nXAdd, pTmp += 2 )
ImplSetAlphaPixel( nY, nX, *pTmp, mpTransTab[ *pTmp ] );
}
}
else // neither alpha nor transparency
{
if ( mnPngDepth == 8 ) // maybe the source is a 16 bit grayscale
{
if( nXAdd == 1 && mnPreviewShift == 0 ) // copy raw line data if possible
{
int nLineBytes = maOrigSize.Width();
mpAcc->CopyScanline( nY, pTmp, BMP_FORMAT_8BIT_PAL, nLineBytes );
pTmp += nLineBytes;
}
else
{
for ( sal_Int32 nX = nXStart; nX < maOrigSize.Width(); nX += nXAdd )
ImplSetPixel( nY, nX, *pTmp++ );
}
}
else
{
for ( sal_Int32 nX = nXStart; nX < maOrigSize.Width(); nX += nXAdd, pTmp += 2 )
ImplSetPixel( nY, nX, *pTmp );
}
}
}
break;
default :
mbStatus = false;
break;
}
}
else // no palette => truecolor
{
// #i122985# Added fast-lane implementations using CopyScanline with direct supported mem formats
static bool bCkeckDirectScanline(true);
if( mbAlphaChannel )
{
// has RGB + alpha
if ( mnPngDepth == 8 ) // maybe the source has 16 bit per sample
{
// BMP_FORMAT_32BIT_TC_RGBA
// only use DirectScanline when we have no preview shifting stuff and accesses to content and alpha
const bool bDoDirectScanline(
bCkeckDirectScanline && !nXStart && 1 == nXAdd && !mnPreviewShift && mpAcc && mpMaskAcc);
const bool bCustomColorTable(mpColorTable != mpDefaultColorTable);
if(bDoDirectScanline)
{
// allocate scanlines on demand, reused for next line
if(!mpScanline)
{
#if OSL_DEBUG_LEVEL > 0
mnAllocSizeScanline = maOrigSize.Width() * 3;
#endif
mpScanline = new sal_uInt8[maOrigSize.Width() * 3];
}
if(!mpScanlineAlpha)
{
#if OSL_DEBUG_LEVEL > 0
mnAllocSizeScanlineAlpha = maOrigSize.Width();
#endif
mpScanlineAlpha = new sal_uInt8[maOrigSize.Width()];
}
}
if(bDoDirectScanline)
{
OSL_ENSURE(mpScanline, "No Scanline allocated (!)");
OSL_ENSURE(mpScanlineAlpha, "No ScanlineAlpha allocated (!)");
OSL_ENSURE(mnAllocSizeScanline >= maOrigSize.Width() * 3, "Allocated Scanline too small (!)");
OSL_ENSURE(mnAllocSizeScanlineAlpha >= maOrigSize.Width(), "Allocated ScanlineAlpha too small (!)");
sal_uInt8* pScanline(mpScanline);
sal_uInt8* pScanlineAlpha(mpScanlineAlpha);
for (sal_Int32 nX(0); nX < maOrigSize.Width(); nX++, pTmp += 4)
{
// prepare content line as BGR by reordering when copying
// do not forget to invert alpha (source is alpha, target is opacity)
if(bCustomColorTable)
{
*pScanline++ = mpColorTable[pTmp[2]];
*pScanline++ = mpColorTable[pTmp[1]];
*pScanline++ = mpColorTable[pTmp[0]];
*pScanlineAlpha++ = ~pTmp[3];
}
else
{
*pScanline++ = pTmp[2];
*pScanline++ = pTmp[1];
*pScanline++ = pTmp[0];
*pScanlineAlpha++ = ~pTmp[3];
}
}
// copy scanlines directly to bitmaps for content and alpha; use the formats which
// are able to copy directly to BitmapBuffer
mpAcc->CopyScanline(nY, mpScanline, BMP_FORMAT_24BIT_TC_BGR, maOrigSize.Width() * 3);
mpMaskAcc->CopyScanline(nY, mpScanlineAlpha, BMP_FORMAT_8BIT_PAL, maOrigSize.Width());
}
else
{
for ( sal_Int32 nX = nXStart; nX < maOrigSize.Width(); nX += nXAdd, pTmp += 4 )
{
if(bCustomColorTable)
{
ImplSetAlphaPixel(
nY,
nX,
BitmapColor(
mpColorTable[ pTmp[ 0 ] ],
mpColorTable[ pTmp[ 1 ] ],
mpColorTable[ pTmp[ 2 ] ]),
pTmp[ 3 ]);
}
else
{
ImplSetAlphaPixel(
nY,
nX,
BitmapColor(
pTmp[0],
pTmp[1],
pTmp[2]),
pTmp[3]);
}
}
}
}
else
{
// BMP_FORMAT_64BIT_TC_RGBA
for ( sal_Int32 nX = nXStart; nX < maOrigSize.Width(); nX += nXAdd, pTmp += 8 )
{
ImplSetAlphaPixel(
nY,
nX,
BitmapColor(
mpColorTable[ pTmp[ 0 ] ],
mpColorTable[ pTmp[ 2 ] ],
mpColorTable[ pTmp[ 4 ] ]),
pTmp[6]);
}
}
}
else if( mbTransparent ) // has RGB + transparency
{
// BMP_FORMAT_24BIT_TC_RGB
// no support currently for DirectScanline, found no real usages in current PNGs, may be added on demand
if ( mnPngDepth == 8 ) // maybe the source has 16 bit per sample
{
for ( sal_Int32 nX = nXStart; nX < maOrigSize.Width(); nX += nXAdd, pTmp += 3 )
{
sal_uInt8 nRed = pTmp[ 0 ];
sal_uInt8 nGreen = pTmp[ 1 ];
sal_uInt8 nBlue = pTmp[ 2 ];
bool bTransparent = ( ( nRed == mnTransRed )
&& ( nGreen == mnTransGreen )
&& ( nBlue == mnTransBlue ) );
ImplSetTranspPixel( nY, nX, BitmapColor( mpColorTable[ nRed ],
mpColorTable[ nGreen ],
mpColorTable[ nBlue ] ), bTransparent );
}
}
else
{
// BMP_FORMAT_48BIT_TC_RGB
for ( sal_Int32 nX = nXStart; nX < maOrigSize.Width(); nX += nXAdd, pTmp += 6 )
{
sal_uInt8 nRed = pTmp[ 0 ];
sal_uInt8 nGreen = pTmp[ 2 ];
sal_uInt8 nBlue = pTmp[ 4 ];
bool bTransparent = ( ( nRed == mnTransRed )
&& ( nGreen == mnTransGreen )
&& ( nBlue == mnTransBlue ) );
ImplSetTranspPixel( nY, nX, BitmapColor( mpColorTable[ nRed ],
mpColorTable[ nGreen ],
mpColorTable[ nBlue ] ), bTransparent );
}
}
}
else // has RGB but neither alpha nor transparency
{
// BMP_FORMAT_24BIT_TC_RGB
// only use DirectScanline when we have no preview shifting stuff and access to content
const bool bDoDirectScanline(
bCkeckDirectScanline && !nXStart && 1 == nXAdd && !mnPreviewShift);
const bool bCustomColorTable(mpColorTable != mpDefaultColorTable);
if(bDoDirectScanline && !mpScanline)
{
// allocate scanlines on demand, reused for next line
#if OSL_DEBUG_LEVEL > 0
mnAllocSizeScanline = maOrigSize.Width() * 3;
#endif
mpScanline = new sal_uInt8[maOrigSize.Width() * 3];
}
if ( mnPngDepth == 8 ) // maybe the source has 16 bit per sample
{
if(bDoDirectScanline)
{
OSL_ENSURE(mpScanline, "No Scanline allocated (!)");
OSL_ENSURE(mnAllocSizeScanline >= maOrigSize.Width() * 3, "Allocated Scanline too small (!)");
sal_uInt8* pScanline(mpScanline);
for (sal_Int32 nX(0); nX < maOrigSize.Width(); nX++, pTmp += 3)
{
// prepare content line as BGR by reordering when copying
if(bCustomColorTable)
{
*pScanline++ = mpColorTable[pTmp[2]];
*pScanline++ = mpColorTable[pTmp[1]];
*pScanline++ = mpColorTable[pTmp[0]];
}
else
{
*pScanline++ = pTmp[2];
*pScanline++ = pTmp[1];
*pScanline++ = pTmp[0];
}
}
// copy scanline directly to bitmap for content; use the format which is able to
// copy directly to BitmapBuffer
mpAcc->CopyScanline(nY, mpScanline, BMP_FORMAT_24BIT_TC_BGR, maOrigSize.Width() * 3);
}
else
{
for ( sal_Int32 nX = nXStart; nX < maOrigSize.Width(); nX += nXAdd, pTmp += 3 )
{
if(bCustomColorTable)
{
ImplSetPixel(
nY,
nX,
BitmapColor(
mpColorTable[ pTmp[ 0 ] ],
mpColorTable[ pTmp[ 1 ] ],
mpColorTable[ pTmp[ 2 ] ]));
}
else
{
ImplSetPixel(
nY,
nX,
BitmapColor(
pTmp[0],
pTmp[1],
pTmp[2]));
}
}
}
}
else
{
// BMP_FORMAT_48BIT_TC_RGB
// no support currently for DirectScanline, found no real usages in current PNGs, may be added on demand
for ( sal_Int32 nX = nXStart; nX < maOrigSize.Width(); nX += nXAdd, pTmp += 6 )
{
ImplSetPixel(
nY,
nX,
BitmapColor(
mpColorTable[ pTmp[ 0 ] ],
mpColorTable[ pTmp[ 2 ] ],
mpColorTable[ pTmp[ 4 ] ]));
}
}
}
}
}
void PNGReaderImpl::ImplSetPixel( sal_uInt32 nY, sal_uInt32 nX, const BitmapColor& rBitmapColor )
{
// TODO: get preview mode checks out of inner loop
if( nX & mnPreviewMask )
return;
nX >>= mnPreviewShift;
mpAcc->SetPixel( nY, nX, rBitmapColor );
}
void PNGReaderImpl::ImplSetPixel( sal_uInt32 nY, sal_uInt32 nX, sal_uInt8 nPalIndex )
{
// TODO: get preview mode checks out of inner loop
if( nX & mnPreviewMask )
return;
nX >>= mnPreviewShift;
mpAcc->SetPixelIndex( nY, nX, nPalIndex );
}
void PNGReaderImpl::ImplSetTranspPixel( sal_uInt32 nY, sal_uInt32 nX, const BitmapColor& rBitmapColor, bool bTrans )
{
// TODO: get preview mode checks out of inner loop
if( nX & mnPreviewMask )
return;
nX >>= mnPreviewShift;
mpAcc->SetPixel( nY, nX, rBitmapColor );
if ( bTrans )
mpMaskAcc->SetPixel( nY, nX, mcTranspColor );
else
mpMaskAcc->SetPixel( nY, nX, mcOpaqueColor );
}
void PNGReaderImpl::ImplSetAlphaPixel( sal_uInt32 nY, sal_uInt32 nX,
sal_uInt8 nPalIndex, sal_uInt8 nAlpha )
{
// TODO: get preview mode checks out of inner loop
if( nX & mnPreviewMask )
return;
nX >>= mnPreviewShift;
mpAcc->SetPixelIndex( nY, nX, nPalIndex );
mpMaskAcc->SetPixelIndex( nY, nX, ~nAlpha );
}
void PNGReaderImpl::ImplSetAlphaPixel( sal_uInt32 nY, sal_uInt32 nX,
const BitmapColor& rBitmapColor, sal_uInt8 nAlpha )
{
// TODO: get preview mode checks out of inner loop
if( nX & mnPreviewMask )
return;
nX >>= mnPreviewShift;
mpAcc->SetPixel( nY, nX, rBitmapColor );
mpMaskAcc->SetPixelIndex( nY, nX, ~nAlpha );
}
sal_uInt32 PNGReaderImpl::ImplReadsal_uInt32()
{
sal_uInt32 nRet;
nRet = *maDataIter++;
nRet <<= 8;
nRet |= *maDataIter++;
nRet <<= 8;
nRet |= *maDataIter++;
nRet <<= 8;
nRet |= *maDataIter++;
return nRet;
}
PNGReader::PNGReader( SvStream& rIStm ) :
mpImpl( new ::vcl::PNGReaderImpl( rIStm ) )
{
}
PNGReader::~PNGReader()
{
delete mpImpl;
}
BitmapEx PNGReader::Read( const Size& i_rPreviewSizeHint )
{
return mpImpl->GetBitmapEx( i_rPreviewSizeHint );
}
const std::vector< vcl::PNGReader::ChunkData >& PNGReader::GetChunks() const
{
return mpImpl->GetAllChunks();
}
void PNGReader::SetIgnoreGammaChunk( bool b )
{
mpImpl->SetIgnoreGammaChunk( b );
}
} // namespace vcl
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|