forked from OSGeo/gdal
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpdfdataset.cpp
7824 lines (7041 loc) · 284 KB
/
pdfdataset.cpp
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
/******************************************************************************
*
* Project: PDF driver
* Purpose: GDALDataset driver for PDF dataset.
* Author: Even Rouault, <even dot rouault at spatialys.com>
*
******************************************************************************
*
* Support for open-source PDFium library
*
* Copyright (C) 2015 Klokan Technologies GmbH (http://www.klokantech.com/)
* Author: Martin Mikita <martin.mikita@klokantech.com>, xmikit00 @ FIT VUT Brno
*
******************************************************************************
* Copyright (c) 2010-2014, Even Rouault <even dot rouault at spatialys.com>
*
* SPDX-License-Identifier: MIT
****************************************************************************/
#include "gdal_pdf.h"
#include "cpl_json_streaming_writer.h"
#include "cpl_vsi_virtual.h"
#include "cpl_spawn.h"
#include "cpl_string.h"
#include "gdal_frmts.h"
#include "gdalalgorithm.h"
#include "ogr_spatialref.h"
#include "ogr_geometry.h"
#ifdef HAVE_POPPLER
#include "cpl_multiproc.h"
#include "pdfio.h"
#endif // HAVE_POPPLER
#include "pdfcreatecopy.h"
#include "pdfdrivercore.h"
#include <algorithm>
#include <cassert>
#include <limits>
#include <set>
#ifdef HAVE_PDFIUM
// To be able to use
// https://github.com/rouault/pdfium_build_gdal_3_5/releases/download/v1_pdfium_5106/install-win10-vs2019-x64-rev5106.zip
// with newer Visual Studio versions.
// Trick from https://github.com/conan-io/conan-center-index/issues/4826
#if _MSC_VER >= 1932 // Visual Studio 2022 version 17.2+
#pragma comment( \
linker, \
"/alternatename:__imp___std_init_once_complete=__imp_InitOnceComplete")
#pragma comment( \
linker, \
"/alternatename:__imp___std_init_once_begin_initialize=__imp_InitOnceBeginInitialize")
#endif
#endif
/* g++ -fPIC -g -Wall frmts/pdf/pdfdataset.cpp -shared -o gdal_PDF.so -Iport
* -Igcore -Iogr -L. -lgdal -lpoppler -I/usr/include/poppler */
#ifdef HAVE_PDF_READ_SUPPORT
static double Get(GDALPDFObject *poObj, int nIndice = -1);
#ifdef HAVE_POPPLER
static CPLMutex *hGlobalParamsMutex = nullptr;
/************************************************************************/
/* GDALPDFOutputDev */
/************************************************************************/
class GDALPDFOutputDev : public SplashOutputDev
{
private:
int bEnableVector;
int bEnableText;
int bEnableBitmap;
void skipBytes(Stream *str, int width, int height, int nComps, int nBits)
{
int nVals = width * nComps;
int nLineSize = (nVals * nBits + 7) >> 3;
int nBytes = nLineSize * height;
for (int i = 0; i < nBytes; i++)
{
if (str->getChar() == EOF)
break;
}
}
public:
GDALPDFOutputDev(SplashColorMode colorModeA, int bitmapRowPadA,
bool reverseVideoA, SplashColorPtr paperColorA)
: SplashOutputDev(colorModeA, bitmapRowPadA, reverseVideoA,
paperColorA),
bEnableVector(TRUE), bEnableText(TRUE), bEnableBitmap(TRUE)
{
}
void SetEnableVector(int bFlag)
{
bEnableVector = bFlag;
}
void SetEnableText(int bFlag)
{
bEnableText = bFlag;
}
void SetEnableBitmap(int bFlag)
{
bEnableBitmap = bFlag;
}
virtual void startPage(int pageNum, GfxState *state, XRef *xrefIn) override
{
SplashOutputDev::startPage(pageNum, state, xrefIn);
SplashBitmap *poBitmap = getBitmap();
memset(poBitmap->getDataPtr(), 255,
static_cast<size_t>(poBitmap->getRowSize()) *
poBitmap->getHeight());
}
virtual void stroke(GfxState *state) override
{
if (bEnableVector)
SplashOutputDev::stroke(state);
}
virtual void fill(GfxState *state) override
{
if (bEnableVector)
SplashOutputDev::fill(state);
}
virtual void eoFill(GfxState *state) override
{
if (bEnableVector)
SplashOutputDev::eoFill(state);
}
virtual void drawChar(GfxState *state, double x, double y, double dx,
double dy, double originX, double originY,
CharCode code, int nBytes, const Unicode *u,
int uLen) override
{
if (bEnableText)
SplashOutputDev::drawChar(state, x, y, dx, dy, originX, originY,
code, nBytes, u, uLen);
}
virtual void beginTextObject(GfxState *state) override
{
if (bEnableText)
SplashOutputDev::beginTextObject(state);
}
virtual void endTextObject(GfxState *state) override
{
if (bEnableText)
SplashOutputDev::endTextObject(state);
}
virtual void drawImageMask(GfxState *state, Object *ref, Stream *str,
int width, int height, bool invert,
bool interpolate, bool inlineImg) override
{
if (bEnableBitmap)
SplashOutputDev::drawImageMask(state, ref, str, width, height,
invert, interpolate, inlineImg);
else
{
VSIPDFFileStream::resetNoCheckReturnValue(str);
if (inlineImg)
{
skipBytes(str, width, height, 1, 1);
}
str->close();
}
}
virtual void setSoftMaskFromImageMask(GfxState *state, Object *ref,
Stream *str, int width, int height,
bool invert, bool inlineImg,
double *baseMatrix) override
{
if (bEnableBitmap)
SplashOutputDev::setSoftMaskFromImageMask(
state, ref, str, width, height, invert, inlineImg, baseMatrix);
else
str->close();
}
virtual void unsetSoftMaskFromImageMask(GfxState *state,
double *baseMatrix) override
{
if (bEnableBitmap)
SplashOutputDev::unsetSoftMaskFromImageMask(state, baseMatrix);
}
virtual void drawImage(GfxState *state, Object *ref, Stream *str, int width,
int height, GfxImageColorMap *colorMap,
bool interpolate, const int *maskColors,
bool inlineImg) override
{
if (bEnableBitmap)
SplashOutputDev::drawImage(state, ref, str, width, height, colorMap,
interpolate, maskColors, inlineImg);
else
{
VSIPDFFileStream::resetNoCheckReturnValue(str);
if (inlineImg)
{
skipBytes(str, width, height, colorMap->getNumPixelComps(),
colorMap->getBits());
}
str->close();
}
}
virtual void drawMaskedImage(GfxState *state, Object *ref, Stream *str,
int width, int height,
GfxImageColorMap *colorMap, bool interpolate,
Stream *maskStr, int maskWidth, int maskHeight,
bool maskInvert, bool maskInterpolate) override
{
if (bEnableBitmap)
SplashOutputDev::drawMaskedImage(
state, ref, str, width, height, colorMap, interpolate, maskStr,
maskWidth, maskHeight, maskInvert, maskInterpolate);
else
str->close();
}
virtual void drawSoftMaskedImage(GfxState *state, Object *ref, Stream *str,
int width, int height,
GfxImageColorMap *colorMap,
bool interpolate, Stream *maskStr,
int maskWidth, int maskHeight,
GfxImageColorMap *maskColorMap,
bool maskInterpolate) override
{
if (bEnableBitmap)
{
if (maskColorMap->getBits() <=
0) /* workaround poppler bug (robustness) */
{
str->close();
return;
}
SplashOutputDev::drawSoftMaskedImage(
state, ref, str, width, height, colorMap, interpolate, maskStr,
maskWidth, maskHeight, maskColorMap, maskInterpolate);
}
else
str->close();
}
};
#endif // ~ HAVE_POPPLER
/************************************************************************/
/* Dump routines */
/************************************************************************/
class GDALPDFDumper
{
private:
FILE *f = nullptr;
const int nDepthLimit;
std::set<int> aoSetObjectExplored{};
const bool bDumpParent;
void DumpSimplified(GDALPDFObject *poObj);
CPL_DISALLOW_COPY_ASSIGN(GDALPDFDumper)
public:
GDALPDFDumper(const char *pszFilename, const char *pszDumpFile,
int nDepthLimitIn = -1)
: nDepthLimit(nDepthLimitIn),
bDumpParent(CPLGetConfigOption("PDF_DUMP_PARENT", "FALSE"))
{
if (strcmp(pszDumpFile, "stderr") == 0)
f = stderr;
else if (EQUAL(pszDumpFile, "YES"))
f = fopen(CPLSPrintf("dump_%s.txt", CPLGetFilename(pszFilename)),
"wt");
else
f = fopen(pszDumpFile, "wt");
if (f == nullptr)
f = stderr;
}
~GDALPDFDumper()
{
if (f != stderr)
fclose(f);
}
void Dump(GDALPDFObject *poObj, int nDepth = 0);
void Dump(GDALPDFDictionary *poDict, int nDepth = 0);
void Dump(GDALPDFArray *poArray, int nDepth = 0);
};
void GDALPDFDumper::Dump(GDALPDFArray *poArray, int nDepth)
{
if (nDepthLimit >= 0 && nDepth > nDepthLimit)
return;
int nLength = poArray->GetLength();
int i;
CPLString osIndent;
for (i = 0; i < nDepth; i++)
osIndent += " ";
for (i = 0; i < nLength; i++)
{
fprintf(f, "%sItem[%d]:", osIndent.c_str(), i);
GDALPDFObject *poObj = nullptr;
if ((poObj = poArray->Get(i)) != nullptr)
{
if (poObj->GetType() == PDFObjectType_String ||
poObj->GetType() == PDFObjectType_Null ||
poObj->GetType() == PDFObjectType_Bool ||
poObj->GetType() == PDFObjectType_Int ||
poObj->GetType() == PDFObjectType_Real ||
poObj->GetType() == PDFObjectType_Name)
{
fprintf(f, " ");
DumpSimplified(poObj);
fprintf(f, "\n");
}
else
{
fprintf(f, "\n");
Dump(poObj, nDepth + 1);
}
}
}
}
void GDALPDFDumper::DumpSimplified(GDALPDFObject *poObj)
{
switch (poObj->GetType())
{
case PDFObjectType_String:
fprintf(f, "%s (string)", poObj->GetString().c_str());
break;
case PDFObjectType_Null:
fprintf(f, "null");
break;
case PDFObjectType_Bool:
fprintf(f, "%s (bool)", poObj->GetBool() ? "true" : "false");
break;
case PDFObjectType_Int:
fprintf(f, "%d (int)", poObj->GetInt());
break;
case PDFObjectType_Real:
fprintf(f, "%f (real)", poObj->GetReal());
break;
case PDFObjectType_Name:
fprintf(f, "%s (name)", poObj->GetName().c_str());
break;
default:
fprintf(f, "unknown !");
break;
}
}
void GDALPDFDumper::Dump(GDALPDFObject *poObj, int nDepth)
{
if (nDepthLimit >= 0 && nDepth > nDepthLimit)
return;
int i;
CPLString osIndent;
for (i = 0; i < nDepth; i++)
osIndent += " ";
fprintf(f, "%sType = %s", osIndent.c_str(), poObj->GetTypeName());
int nRefNum = poObj->GetRefNum().toInt();
if (nRefNum != 0)
fprintf(f, ", Num = %d, Gen = %d", nRefNum, poObj->GetRefGen());
fprintf(f, "\n");
if (nRefNum != 0)
{
if (aoSetObjectExplored.find(nRefNum) != aoSetObjectExplored.end())
return;
aoSetObjectExplored.insert(nRefNum);
}
switch (poObj->GetType())
{
case PDFObjectType_Array:
Dump(poObj->GetArray(), nDepth + 1);
break;
case PDFObjectType_Dictionary:
Dump(poObj->GetDictionary(), nDepth + 1);
break;
case PDFObjectType_String:
case PDFObjectType_Null:
case PDFObjectType_Bool:
case PDFObjectType_Int:
case PDFObjectType_Real:
case PDFObjectType_Name:
fprintf(f, "%s", osIndent.c_str());
DumpSimplified(poObj);
fprintf(f, "\n");
break;
default:
fprintf(f, "%s", osIndent.c_str());
fprintf(f, "unknown !\n");
break;
}
GDALPDFStream *poStream = poObj->GetStream();
if (poStream != nullptr)
{
fprintf(f,
"%sHas stream (" CPL_FRMT_GIB
" uncompressed bytes, " CPL_FRMT_GIB " raw bytes)\n",
osIndent.c_str(), static_cast<GIntBig>(poStream->GetLength()),
static_cast<GIntBig>(poStream->GetRawLength()));
}
}
void GDALPDFDumper::Dump(GDALPDFDictionary *poDict, int nDepth)
{
if (nDepthLimit >= 0 && nDepth > nDepthLimit)
return;
CPLString osIndent;
for (int i = 0; i < nDepth; i++)
osIndent += " ";
int i = 0;
const auto &oMap = poDict->GetValues();
for (const auto &[osKey, poObj] : oMap)
{
fprintf(f, "%sItem[%d] : %s", osIndent.c_str(), i, osKey.c_str());
++i;
if (osKey == "Parent" && !bDumpParent)
{
if (poObj->GetRefNum().toBool())
fprintf(f, ", Num = %d, Gen = %d", poObj->GetRefNum().toInt(),
poObj->GetRefGen());
fprintf(f, "\n");
continue;
}
if (poObj != nullptr)
{
if (poObj->GetType() == PDFObjectType_String ||
poObj->GetType() == PDFObjectType_Null ||
poObj->GetType() == PDFObjectType_Bool ||
poObj->GetType() == PDFObjectType_Int ||
poObj->GetType() == PDFObjectType_Real ||
poObj->GetType() == PDFObjectType_Name)
{
fprintf(f, " = ");
DumpSimplified(poObj);
fprintf(f, "\n");
}
else
{
fprintf(f, "\n");
Dump(poObj, nDepth + 1);
}
}
}
}
/************************************************************************/
/* PDFRasterBand() */
/************************************************************************/
PDFRasterBand::PDFRasterBand(PDFDataset *poDSIn, int nBandIn,
int nResolutionLevelIn)
: nResolutionLevel(nResolutionLevelIn)
{
poDS = poDSIn;
nBand = nBandIn;
eDataType = GDT_Byte;
if (nResolutionLevel > 0)
{
nBlockXSize = 256;
nBlockYSize = 256;
poDSIn->SetMetadataItem("INTERLEAVE", "PIXEL", "IMAGE_STRUCTURE");
}
else if (poDSIn->m_nBlockXSize)
{
nBlockXSize = poDSIn->m_nBlockXSize;
nBlockYSize = poDSIn->m_nBlockYSize;
poDSIn->SetMetadataItem("INTERLEAVE", "PIXEL", "IMAGE_STRUCTURE");
}
else if (poDSIn->GetRasterXSize() <
64 * 1024 * 1024 / poDSIn->GetRasterYSize())
{
nBlockXSize = poDSIn->GetRasterXSize();
nBlockYSize = 1;
}
else
{
nBlockXSize = std::min(1024, poDSIn->GetRasterXSize());
nBlockYSize = std::min(1024, poDSIn->GetRasterYSize());
poDSIn->SetMetadataItem("INTERLEAVE", "PIXEL", "IMAGE_STRUCTURE");
}
}
/************************************************************************/
/* InitOverviews() */
/************************************************************************/
void PDFDataset::InitOverviews()
{
#ifdef HAVE_PDFIUM
// Only if used pdfium, make "arbitrary overviews"
// Blocks are 256x256
if (m_bUseLib.test(PDFLIB_PDFIUM) && m_apoOvrDS.empty() &&
m_apoOvrDSBackup.empty())
{
int nXSize = nRasterXSize;
int nYSize = nRasterYSize;
constexpr int minSize = 256;
int nDiscard = 1;
while (nXSize > minSize || nYSize > minSize)
{
nXSize = (nXSize + 1) / 2;
nYSize = (nYSize + 1) / 2;
auto poOvrDS = std::make_unique<PDFDataset>(this, nXSize, nYSize);
for (int i = 0; i < nBands; i++)
poOvrDS->SetBand(
i + 1, new PDFRasterBand(poOvrDS.get(), i + 1, nDiscard));
m_apoOvrDS.emplace_back(std::move(poOvrDS));
++nDiscard;
}
}
#endif
#if defined(HAVE_POPPLER) || defined(HAVE_PODOFO)
if (!m_bUseLib.test(PDFLIB_PDFIUM) && m_apoOvrDS.empty() &&
m_apoOvrDSBackup.empty() && m_osUserPwd != "ASK_INTERACTIVE")
{
int nXSize = nRasterXSize;
int nYSize = nRasterYSize;
constexpr int minSize = 256;
double dfDPI = m_dfDPI;
while (nXSize > minSize || nYSize > minSize)
{
nXSize = (nXSize + 1) / 2;
nYSize = (nYSize + 1) / 2;
dfDPI /= 2;
GDALOpenInfo oOpenInfo(GetDescription(), GA_ReadOnly);
CPLStringList aosOpenOptions(CSLDuplicate(papszOpenOptions));
aosOpenOptions.SetNameValue("DPI", CPLSPrintf("%g", dfDPI));
aosOpenOptions.SetNameValue("BANDS", CPLSPrintf("%d", nBands));
aosOpenOptions.SetNameValue("@OPEN_FOR_OVERVIEW", "YES");
if (!m_osUserPwd.empty())
aosOpenOptions.SetNameValue("USER_PWD", m_osUserPwd.c_str());
oOpenInfo.papszOpenOptions = aosOpenOptions.List();
auto poOvrDS = std::unique_ptr<PDFDataset>(Open(&oOpenInfo));
if (!poOvrDS || poOvrDS->nBands != nBands)
break;
poOvrDS->m_bIsOvrDS = true;
m_apoOvrDS.emplace_back(std::move(poOvrDS));
}
}
#endif
}
/************************************************************************/
/* GetColorInterpretation() */
/************************************************************************/
GDALColorInterp PDFRasterBand::GetColorInterpretation()
{
PDFDataset *poGDS = cpl::down_cast<PDFDataset *>(poDS);
if (poGDS->nBands == 1)
return GCI_GrayIndex;
else
return static_cast<GDALColorInterp>(GCI_RedBand + (nBand - 1));
}
/************************************************************************/
/* GetOverviewCount() */
/************************************************************************/
int PDFRasterBand::GetOverviewCount()
{
PDFDataset *poGDS = cpl::down_cast<PDFDataset *>(poDS);
if (poGDS->m_bIsOvrDS)
return 0;
if (GDALPamRasterBand::GetOverviewCount() > 0)
return GDALPamRasterBand::GetOverviewCount();
else
{
poGDS->InitOverviews();
return static_cast<int>(poGDS->m_apoOvrDS.size());
}
}
/************************************************************************/
/* GetOverview() */
/************************************************************************/
GDALRasterBand *PDFRasterBand::GetOverview(int iOverviewIndex)
{
if (GDALPamRasterBand::GetOverviewCount() > 0)
return GDALPamRasterBand::GetOverview(iOverviewIndex);
else if (iOverviewIndex < 0 || iOverviewIndex >= GetOverviewCount())
return nullptr;
else
{
PDFDataset *poGDS = cpl::down_cast<PDFDataset *>(poDS);
return poGDS->m_apoOvrDS[iOverviewIndex]->GetRasterBand(nBand);
}
}
/************************************************************************/
/* ~PDFRasterBand() */
/************************************************************************/
PDFRasterBand::~PDFRasterBand()
{
}
/************************************************************************/
/* IReadBlockFromTile() */
/************************************************************************/
CPLErr PDFRasterBand::IReadBlockFromTile(int nBlockXOff, int nBlockYOff,
void *pImage)
{
PDFDataset *poGDS = cpl::down_cast<PDFDataset *>(poDS);
int nReqXSize = nBlockXSize;
int nReqYSize = nBlockYSize;
if ((nBlockXOff + 1) * nBlockXSize > nRasterXSize)
nReqXSize = nRasterXSize - nBlockXOff * nBlockXSize;
if ((nBlockYOff + 1) * nBlockYSize > nRasterYSize)
nReqYSize = nRasterYSize - nBlockYOff * nBlockYSize;
int nXBlocks = DIV_ROUND_UP(nRasterXSize, nBlockXSize);
int iTile = poGDS->m_aiTiles[nBlockYOff * nXBlocks + nBlockXOff];
if (iTile < 0)
{
memset(pImage, 0, static_cast<size_t>(nBlockXSize) * nBlockYSize);
return CE_None;
}
GDALPDFTileDesc &sTile = poGDS->m_asTiles[iTile];
GDALPDFObject *poImage = sTile.poImage;
if (nBand == 4)
{
GDALPDFDictionary *poImageDict = poImage->GetDictionary();
GDALPDFObject *poSMask = poImageDict->Get("SMask");
if (poSMask != nullptr &&
poSMask->GetType() == PDFObjectType_Dictionary)
{
GDALPDFDictionary *poSMaskDict = poSMask->GetDictionary();
GDALPDFObject *poWidth = poSMaskDict->Get("Width");
GDALPDFObject *poHeight = poSMaskDict->Get("Height");
GDALPDFObject *poColorSpace = poSMaskDict->Get("ColorSpace");
GDALPDFObject *poBitsPerComponent =
poSMaskDict->Get("BitsPerComponent");
double dfBits = 0;
if (poBitsPerComponent)
dfBits = Get(poBitsPerComponent);
if (poWidth && Get(poWidth) == nReqXSize && poHeight &&
Get(poHeight) == nReqYSize && poColorSpace &&
poColorSpace->GetType() == PDFObjectType_Name &&
poColorSpace->GetName() == "DeviceGray" &&
(dfBits == 1 || dfBits == 8))
{
GDALPDFStream *poStream = poSMask->GetStream();
GByte *pabyStream = nullptr;
if (poStream == nullptr)
return CE_Failure;
pabyStream = reinterpret_cast<GByte *>(poStream->GetBytes());
if (pabyStream == nullptr)
return CE_Failure;
const int nReqXSize1 = (nReqXSize + 7) / 8;
if ((dfBits == 8 &&
static_cast<size_t>(poStream->GetLength()) !=
static_cast<size_t>(nReqXSize) * nReqYSize) ||
(dfBits == 1 &&
static_cast<size_t>(poStream->GetLength()) !=
static_cast<size_t>(nReqXSize1) * nReqYSize))
{
VSIFree(pabyStream);
return CE_Failure;
}
GByte *pabyData = static_cast<GByte *>(pImage);
if (nReqXSize != nBlockXSize || nReqYSize != nBlockYSize)
{
memset(pabyData, 0,
static_cast<size_t>(nBlockXSize) * nBlockYSize);
}
if (dfBits == 8)
{
for (int j = 0; j < nReqYSize; j++)
{
for (int i = 0; i < nReqXSize; i++)
{
pabyData[j * nBlockXSize + i] =
pabyStream[j * nReqXSize + i];
}
}
}
else
{
for (int j = 0; j < nReqYSize; j++)
{
for (int i = 0; i < nReqXSize; i++)
{
if (pabyStream[j * nReqXSize1 + i / 8] &
(1 << (7 - (i % 8))))
pabyData[j * nBlockXSize + i] = 255;
else
pabyData[j * nBlockXSize + i] = 0;
}
}
}
VSIFree(pabyStream);
return CE_None;
}
}
memset(pImage, 255, static_cast<size_t>(nBlockXSize) * nBlockYSize);
return CE_None;
}
if (poGDS->m_nLastBlockXOff == nBlockXOff &&
poGDS->m_nLastBlockYOff == nBlockYOff &&
poGDS->m_pabyCachedData != nullptr)
{
#ifdef DEBUG
CPLDebug("PDF", "Using cached block (%d, %d)", nBlockXOff, nBlockYOff);
#endif
// do nothing
}
else
{
if (!poGDS->m_bTried)
{
poGDS->m_bTried = true;
poGDS->m_pabyCachedData =
static_cast<GByte *>(VSIMalloc3(3, nBlockXSize, nBlockYSize));
}
if (poGDS->m_pabyCachedData == nullptr)
return CE_Failure;
GDALPDFStream *poStream = poImage->GetStream();
GByte *pabyStream = nullptr;
if (poStream == nullptr)
return CE_Failure;
pabyStream = reinterpret_cast<GByte *>(poStream->GetBytes());
if (pabyStream == nullptr)
return CE_Failure;
if (static_cast<size_t>(poStream->GetLength()) !=
static_cast<size_t>(sTile.nBands) * nReqXSize * nReqYSize)
{
VSIFree(pabyStream);
return CE_Failure;
}
memcpy(poGDS->m_pabyCachedData, pabyStream,
static_cast<size_t>(poStream->GetLength()));
VSIFree(pabyStream);
poGDS->m_nLastBlockXOff = nBlockXOff;
poGDS->m_nLastBlockYOff = nBlockYOff;
}
GByte *pabyData = static_cast<GByte *>(pImage);
if (nBand != 4 && (nReqXSize != nBlockXSize || nReqYSize != nBlockYSize))
{
memset(pabyData, 0, static_cast<size_t>(nBlockXSize) * nBlockYSize);
}
if (poGDS->nBands >= 3 && sTile.nBands == 3)
{
for (int j = 0; j < nReqYSize; j++)
{
for (int i = 0; i < nReqXSize; i++)
{
pabyData[j * nBlockXSize + i] =
poGDS
->m_pabyCachedData[3 * (j * nReqXSize + i) + nBand - 1];
}
}
}
else if (sTile.nBands == 1)
{
for (int j = 0; j < nReqYSize; j++)
{
for (int i = 0; i < nReqXSize; i++)
{
pabyData[j * nBlockXSize + i] =
poGDS->m_pabyCachedData[j * nReqXSize + i];
}
}
}
return CE_None;
}
/************************************************************************/
/* GetSuggestedBlockAccessPattern() */
/************************************************************************/
GDALSuggestedBlockAccessPattern
PDFRasterBand::GetSuggestedBlockAccessPattern() const
{
PDFDataset *poGDS = cpl::down_cast<PDFDataset *>(poDS);
if (!poGDS->m_aiTiles.empty())
return GSBAP_RANDOM;
return GSBAP_LARGEST_CHUNK_POSSIBLE;
}
/************************************************************************/
/* IReadBlock() */
/************************************************************************/
CPLErr PDFRasterBand::IReadBlock(int nBlockXOff, int nBlockYOff, void *pImage)
{
PDFDataset *poGDS = cpl::down_cast<PDFDataset *>(poDS);
if (!poGDS->m_aiTiles.empty())
{
if (IReadBlockFromTile(nBlockXOff, nBlockYOff, pImage) == CE_None)
{
return CE_None;
}
else
{
poGDS->m_aiTiles.resize(0);
poGDS->m_bTried = false;
CPLFree(poGDS->m_pabyCachedData);
poGDS->m_pabyCachedData = nullptr;
poGDS->m_nLastBlockXOff = -1;
poGDS->m_nLastBlockYOff = -1;
}
}
int nReqXSize = nBlockXSize;
int nReqYSize = nBlockYSize;
if ((nBlockXOff + 1) * nBlockXSize > nRasterXSize)
nReqXSize = nRasterXSize - nBlockXOff * nBlockXSize;
if (nBlockYSize == 1)
nReqYSize = nRasterYSize;
else if ((nBlockYOff + 1) * nBlockYSize > nRasterYSize)
nReqYSize = nRasterYSize - nBlockYOff * nBlockYSize;
if (!poGDS->m_bTried)
{
poGDS->m_bTried = true;
if (nBlockYSize == 1)
poGDS->m_pabyCachedData = static_cast<GByte *>(VSIMalloc3(
std::max(3, poGDS->nBands), nRasterXSize, nRasterYSize));
else
poGDS->m_pabyCachedData = static_cast<GByte *>(VSIMalloc3(
std::max(3, poGDS->nBands), nBlockXSize, nBlockYSize));
}
if (poGDS->m_pabyCachedData == nullptr)
return CE_Failure;
if (poGDS->m_nLastBlockXOff == nBlockXOff &&
(nBlockYSize == 1 || poGDS->m_nLastBlockYOff == nBlockYOff) &&
poGDS->m_pabyCachedData != nullptr)
{
/*CPLDebug("PDF", "Using cached block (%d, %d)",
nBlockXOff, nBlockYOff);*/
// do nothing
}
else
{
#ifdef HAVE_PODOFO
if (poGDS->m_bUseLib.test(PDFLIB_PODOFO) && nBand == 4)
{
memset(pImage, 255, nBlockXSize * nBlockYSize);
return CE_None;
}
#endif
const int nReqXOff = nBlockXOff * nBlockXSize;
const int nReqYOff = (nBlockYSize == 1) ? 0 : nBlockYOff * nBlockYSize;
const GSpacing nPixelSpace = 1;
const GSpacing nLineSpace = nBlockXSize;
const GSpacing nBandSpace =
static_cast<GSpacing>(nBlockXSize) *
((nBlockYSize == 1) ? nRasterYSize : nBlockYSize);
CPLErr eErr = poGDS->ReadPixels(nReqXOff, nReqYOff, nReqXSize,
nReqYSize, nPixelSpace, nLineSpace,
nBandSpace, poGDS->m_pabyCachedData);
if (eErr == CE_None)
{
poGDS->m_nLastBlockXOff = nBlockXOff;
poGDS->m_nLastBlockYOff = nBlockYOff;
}
else
{
CPLFree(poGDS->m_pabyCachedData);
poGDS->m_pabyCachedData = nullptr;
}
}
if (poGDS->m_pabyCachedData == nullptr)
return CE_Failure;
if (nBlockYSize == 1)
memcpy(pImage,
poGDS->m_pabyCachedData +
(nBand - 1) * nBlockXSize * nRasterYSize +
nBlockYOff * nBlockXSize,
nBlockXSize);
else
{
memcpy(pImage,
poGDS->m_pabyCachedData +
static_cast<size_t>(nBand - 1) * nBlockXSize * nBlockYSize,
static_cast<size_t>(nBlockXSize) * nBlockYSize);
if (poGDS->m_bCacheBlocksForOtherBands && nBand == 1)
{
for (int iBand = 2; iBand <= poGDS->nBands; ++iBand)
{
auto poOtherBand = cpl::down_cast<PDFRasterBand *>(
poGDS->papoBands[iBand - 1]);
GDALRasterBlock *poBlock =
poOtherBand->TryGetLockedBlockRef(nBlockXOff, nBlockYOff);
if (poBlock)
{
poBlock->DropLock();
}
else
{
poBlock = poOtherBand->GetLockedBlockRef(nBlockXOff,
nBlockYOff, TRUE);
if (poBlock)
{
memcpy(poBlock->GetDataRef(),
poGDS->m_pabyCachedData +
static_cast<size_t>(iBand - 1) *
nBlockXSize * nBlockYSize,
static_cast<size_t>(nBlockXSize) * nBlockYSize);
poBlock->DropLock();
}
}
}
}
}
return CE_None;
}
/************************************************************************/
/* PDFEnterPasswordFromConsoleIfNeeded() */
/************************************************************************/
static const char *PDFEnterPasswordFromConsoleIfNeeded(const char *pszUserPwd)
{
if (EQUAL(pszUserPwd, "ASK_INTERACTIVE"))
{
static char szPassword[81];
printf("Enter password (will be echo'ed in the console): "); /*ok*/
if (nullptr == fgets(szPassword, sizeof(szPassword), stdin))
{
fprintf(stderr, "WARNING: Error getting password.\n"); /*ok*/
}
szPassword[sizeof(szPassword) - 1] = 0;
char *sz10 = strchr(szPassword, '\n');