-
Notifications
You must be signed in to change notification settings - Fork 219
/
Copy pathDCTDecodeFilterTest.cpp
354 lines (273 loc) · 10.7 KB
/
DCTDecodeFilterTest.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
/*
Source File : DCTDecodeFilterTest
Copyright 2012 Gal Kahana PDFWriter
Licensed 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
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifndef PDFHUMMUS_NO_DCT
#include "Trace.h"
#include "PDFWriter.h"
#include "PDFPage.h"
#include "PDFFormXObject.h"
#include "PageContentContext.h"
#include "PDFImageXObject.h"
#include "PDFParser.h"
#include "PDFDictionary.h"
#include "PDFObjectCast.h"
#include "PDFObject.h"
#include "PDFIndirectObjectReference.h"
#include "PDFStreamInput.h"
#include "PDFArray.h"
#include "PDFDocumentCopyingContext.h"
#include "ObjectsContext.h"
#include "PDFStream.h"
#include "DictionaryContext.h"
#include "OutputStreamTraits.h"
#include "testing/TestIO.h"
#include <iostream>
using namespace PDFHummus;
static EStatusCode CreateFileWithJPGImage(char* argv[],
const string& inTestFileName)
{
PDFWriter pdfWriter;
EStatusCode status;
do
{
status = pdfWriter.StartPDF(BuildRelativeOutputPath(argv,inTestFileName),ePDFVersion13);
if(status != PDFHummus::eSuccess)
{
cout<<"failed to start PDF\n";
break;
}
PDFPage* page = new PDFPage();
page->SetMediaBox(PDFRectangle(0,0,595,842));
PageContentContext* pageContentContext = pdfWriter.StartPageContentContext(page);
if(NULL == pageContentContext)
{
status = PDFHummus::eFailure;
cout<<"failed to create content context for page\n";
}
// draw a rectangle
pageContentContext->q();
pageContentContext->k(100,0,0,0);
pageContentContext->re(500,0,100,100);
pageContentContext->f();
pageContentContext->Q();
// pause stream to start writing the image
status = pdfWriter.PausePageContentContext(pageContentContext);
if(status != PDFHummus::eSuccess)
{
cout<<"failed to pause page content context\n";
break;
}
// Create image xobject from
PDFImageXObject* imageXObject = pdfWriter.CreateImageXObjectFromJPGFile(BuildRelativeInputPath(argv,"images/otherStage.JPG"));
if(!imageXObject)
{
cout<<"failed to create image XObject from file\n";
status = PDFHummus::eFailure;
break;
}
// continue page drawing size the image to 500,400
pageContentContext->q();
pageContentContext->cm(500,0,0,400,0,0);
pageContentContext->Do(page->GetResourcesDictionary().AddImageXObjectMapping(imageXObject));
pageContentContext->Q();
delete imageXObject;
status = pdfWriter.EndPageContentContext(pageContentContext);
if(status != PDFHummus::eSuccess)
{
cout<<"failed to end page content context\n";
break;
}
status = pdfWriter.WritePageAndRelease(page);
if(status != PDFHummus::eSuccess)
{
cout<<"failed to write page\n";
break;
}
status = pdfWriter.EndPDF();
if(status != PDFHummus::eSuccess)
{
cout<<"failed in end PDF\n";
break;
}
}while(false);
return status;
}
static ObjectIDType FindDCTDecodedImageObject(PDFParser* inParser)
{
ObjectIDType imageObject = 0;
do
{
// find image by looking for the first image in the first page
RefCountPtr<PDFDictionary> firstPage = inParser->ParsePage(0);
if(!firstPage)
break;
PDFObjectCastPtr<PDFDictionary> resourceDictionary(inParser->QueryDictionaryObject(firstPage.GetPtr(),"Resources"));
if(!resourceDictionary)
break;
PDFObjectCastPtr<PDFDictionary> xobjectDictionary(inParser->QueryDictionaryObject(resourceDictionary.GetPtr(), "XObject"));
if(!xobjectDictionary)
break;
MapIterator<PDFNameToPDFObjectMap> it = xobjectDictionary->GetIterator();
while(it.MoveNext())
{
if(it.GetValue()->GetType() == PDFObject::ePDFObjectIndirectObjectReference)
{
PDFObjectCastPtr<PDFStreamInput> image(
inParser->ParseNewObject(((PDFIndirectObjectReference*)it.GetValue())->mObjectID));
RefCountPtr<PDFDictionary> imageDictionary = image->QueryStreamDictionary();
PDFObjectCastPtr<PDFName> objectType = imageDictionary->QueryDirectObject("Subtype");
if(!objectType || objectType->GetValue() != "Image")
continue;
RefCountPtr<PDFObject> filters = imageDictionary->QueryDirectObject("Filter");
if(!filters)
break;
if(filters->GetType() == PDFObject::ePDFObjectName &&
((PDFName*)filters.GetPtr())->GetValue() == "DCTDecode")
{
imageObject = ((PDFIndirectObjectReference*)it.GetValue())->mObjectID;
break;
}
PDFArray* filtersArray = (PDFArray*)filters.GetPtr();
if(filtersArray->GetLength() == 1)
{
PDFObjectCastPtr<PDFName> firstDecoder(filtersArray->QueryObject(0));
if(firstDecoder->GetValue() == "DCTDecode")
{
imageObject = ((PDFIndirectObjectReference*)it.GetValue())->mObjectID;
break;
}
}
}
}
}
while (false);
return imageObject;
}
static EStatusCode ModifyImageObject(PDFWriter* inWriter,ObjectIDType inImageObject)
{
EStatusCode status = eSuccess;
PDFDocumentCopyingContext* modifiedFileContext = inWriter->CreatePDFCopyingContextForModifiedFile();
do
{
// get image source dictionary
PDFObjectCastPtr<PDFStreamInput> imageStream(inWriter->GetModifiedFileParser().ParseNewObject(inImageObject));
RefCountPtr<PDFDictionary> imageDictionary(imageStream->QueryStreamDictionary());
// strt object for modified image
inWriter->GetObjectsContext().StartModifiedIndirectObject(inImageObject);
DictionaryContext* newImageDictionary = inWriter->GetObjectsContext().StartDictionary();
MapIterator<PDFNameToPDFObjectMap> it = imageDictionary->GetIterator();
// copy all but "Filter" and "Length"
ObjectIDTypeList indirectObjects;
while (it.MoveNext())
{
if(it.GetKey()->GetValue() == "Filter" || it.GetKey()->GetValue() == "Length")
continue;
newImageDictionary->WriteKey(it.GetKey()->GetValue());
EStatusCodeAndObjectIDTypeList result = modifiedFileContext->CopyDirectObjectWithDeepCopy(it.GetValue());
if(result.first != eSuccess)
{
status = result.first;
break;
}
indirectObjects.insert(indirectObjects.end(),result.second.begin(),result.second.end());
}
if(status != eSuccess)
break;
// start image stream for this dictionary (make sure it's unfiltered)
PDFStream* newImageStream = inWriter->GetObjectsContext().StartUnfilteredPDFStream(newImageDictionary);
// copy source stream through read filter
IByteReader* sourceImage = modifiedFileContext->GetSourceDocumentParser()->StartReadingFromStream(imageStream.GetPtr());
if(!sourceImage)
{
cout<<"failed to read DCT stream\n";
status = eFailure;
break;
}
OutputStreamTraits traits(newImageStream->GetWriteStream());
status = traits.CopyToOutputStream(sourceImage);
// finalize stream
inWriter->GetObjectsContext().EndPDFStream(newImageStream);
delete newImageStream;
delete sourceImage;
// late check for status so stream is deleted
if(status != eSuccess)
break;
// copy remaining indirect objects from image stream dictionary
status = modifiedFileContext->CopyNewObjectsForDirectObject(indirectObjects);
}
while (false);
delete modifiedFileContext;
return status;
}
int DCTDecodeFilterTest(int argc, char* argv[])
{
EStatusCode status = eSuccess;
/*
what we're going to do here is this:
1. create a PDF file using a jpg image. just like the regular jpg image test
2. open the file for modification
3. the jpg image should be using DCT decoder. make sure
4. create a modified image object with the DCT decoding decompressed.
4.1 start a new stream
4.2 copy all but length and decoders details from the original image dictionary
4.3 create a stream reader for the old image that will decompress the image
4.4 write it as is to the new image object
5. now you should have a PDF that has the image decompressed. if it's showing fine...then
you got a working DCT decoder
*/
do
{
string testFileName = "DCTTest.pdf";
// ceate file
status = CreateFileWithJPGImage(argv,testFileName);
if(status != eSuccess)
{
cout<<"Failed to create jpg file\n";
break;
}
// open file for modification
PDFWriter pdfWriter;
status = pdfWriter.ModifyPDF(BuildRelativeOutputPath(argv,testFileName),ePDFVersion13,BuildRelativeOutputPath(argv,string("mod") + testFileName));
if(status != PDFHummus::eSuccess)
{
cout<<"Failed to start PDF for modification\n";
break;
}
ObjectIDType imageObject = FindDCTDecodedImageObject(&pdfWriter.GetModifiedFileParser());
if(imageObject == 0)
{
status = eFailure;
cout<<"Image object not found\n";
break;
}
status = ModifyImageObject(&pdfWriter,imageObject);
if(status != eSuccess)
{
cout<<"Failed to modify PDF\n";
break;
}
status = pdfWriter.EndPDF();
if(status != eSuccess)
{
cout<<"Failed to end PDF\n";
break;
}
}
while (false);
return status == eSuccess ? 0:1;
}
#else // PDFHUMMUS_NO_DCT
int DCTDecodeFilterTest(int argc, char* argv[]) {
return 0;
}
#endif