-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
233 lines (193 loc) · 7.14 KB
/
main.cpp
File metadata and controls
233 lines (193 loc) · 7.14 KB
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
/*
This project is based off https://github.com/galkahana/pdf-transparent-tiff
functions were copied a modified to work with the zint bitmap buffer
Thanks: galkahana (https://github.com/galkahana)
License: Apache-2.0 license
*/
#include <PDFWriter.h>
#include <PDFModifiedPage.h>
#include <PDFFormXObject.h>
#include <PDFPage.h>
#include <PageContentContext.h>
#include <PDFStream.h>
#include <DictionaryContext.h>
#include <PDFImageXObject.h>
#include <ProcsetResourcesConstants.h>
#include <XObjectContentContext.h>
#include <zint.h>
PDFFormXObject* createImageFormXObjectFromImageXObject(PDFWriter* inWriter, PDFImageXObject* imageXObject, ObjectIDType inFormXObjectID, uint32_t w, uint32_t h) {
PDFHummus::DocumentContext& docCxt = inWriter->GetDocumentContext();
PDFFormXObject* formXObject = NULL;
do {
formXObject = docCxt.StartFormXObject(PDFRectangle(0, 0, w, h), inFormXObjectID);
XObjectContentContext* xobjectContentContext = formXObject->GetContentContext();
xobjectContentContext->q();
xobjectContentContext->cm(w, 0, 0, h, 0, 0);
xobjectContentContext->Do(formXObject->GetResourcesDictionary().AddImageXObjectMapping(imageXObject));
xobjectContentContext->Q();
EStatusCode status = docCxt.EndFormXObjectNoRelease(formXObject);
if(status != PDFHummus::eSuccess) {
delete formXObject;
formXObject = NULL;
break;
}
} while(false);
return formXObject;
}
static PDFImageXObject* createImageXObjectForBitmap(PDFWriter* inWriter, uint32_t w, uint32_t h, uint8_t* raster) {
PDFImageXObject* imageXObject = NULL;
PDFStream* imageStream = NULL;
ObjectsContext& objCxt = inWriter->GetObjectsContext();
EStatusCode status = eSuccess;
// allocate image elements
ObjectIDType imageXObjectObjectId = objCxt.GetInDirectObjectsRegistry().AllocateNewObjectID();
objCxt.StartNewIndirectObject(imageXObjectObjectId);
DictionaryContext* imageContext = objCxt.StartDictionary();
// type
imageContext->WriteKey("Type");
imageContext->WriteNameValue("XObject");
// subtype
imageContext->WriteKey("Subtype");
imageContext->WriteNameValue("Image");
// Width
imageContext->WriteKey("Width");
imageContext->WriteIntegerValue(w);
// Height
imageContext->WriteKey("Height");
imageContext->WriteIntegerValue(h);
// Bits Per Component
imageContext->WriteKey("BitsPerComponent");
imageContext->WriteIntegerValue(8);
// Color Space
imageContext->WriteKey("ColorSpace");
imageContext->WriteNameValue("DeviceRGB");
// now for the image
imageStream = objCxt.StartPDFStream(imageContext);
IByteWriter* writerStream = imageStream->GetWriteStream();
for(uint32_t pixel = 0; pixel < ((w * h) * 3);) {
uint8_t r = raster[pixel++];
uint8_t g = raster[pixel++];
uint8_t b = raster[pixel++];
writerStream->Write((IOBasicTypes::Byte*)&r, 1);
writerStream->Write((IOBasicTypes::Byte*)&g, 1);
writerStream->Write((IOBasicTypes::Byte*)&b, 1);
}
objCxt.EndPDFStream(imageStream);
imageXObject = new PDFImageXObject(imageXObjectObjectId, KProcsetImageC);
if(eFailure == status) {
delete imageXObject;
imageXObject = NULL;
}
delete imageStream;
return imageXObject;
}
PDFFormXObject* CreateFormXObjectFromBitmap(PDFWriter* inWriter, uint32_t w, uint32_t h, uint8_t* raster, ObjectIDType inFormXObjectId) {
PDFFormXObject* imageFormXObject = NULL;
PDFImageXObject* imageXObject = NULL;
do {
// create image xobject based on image data
PDFImageXObject* imageXObject = createImageXObjectForBitmap(inWriter, w, h, raster);
if(!imageXObject)
break;
// create form
imageFormXObject = createImageFormXObjectFromImageXObject(inWriter, imageXObject, inFormXObjectId == 0 ? inWriter->GetObjectsContext().GetInDirectObjectsRegistry().AllocateNewObjectID() : 0, w, h);
} while(false);
delete imageXObject;
return imageFormXObject;
}
EStatusCode placeBitmap(PDFWriter& inWriter, int page_num, uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint8_t* raster, bool original) {
EStatusCode status = eSuccess;
InputFile tiffFile;
do {
if(original) {
PDFFormXObject* imageFormXObject = CreateFormXObjectFromBitmap(&inWriter, w, h, raster, 0);
if(!imageFormXObject) {
printf("failed to create xobject from bitmap");
break;
}
PDFPage* page = new PDFPage();
page->SetMediaBox(PDFRectangle(0, 0, 595, 842));
PageContentContext* pageContentContext = inWriter.StartPageContentContext(page);
// place the image in page bottom left and discard form. scaled to quarter
pageContentContext->q();
pageContentContext->cm(1, 0, 0, 1, x, y);
pageContentContext->Do(page->GetResourcesDictionary().AddFormXObjectMapping(imageFormXObject->GetObjectID()));
pageContentContext->Q();
delete imageFormXObject;
status = inWriter.EndPageContentContext(pageContentContext);
if(status != eSuccess) {
printf("failed to end page content context");
break;
}
status = inWriter.WritePageAndRelease(page);
if(status != eSuccess) {
printf("failed to write page");
break;
}
} else {
PDFFormXObject* imageFormXObject = CreateFormXObjectFromBitmap(&inWriter, w, h, raster, 0);
if(!imageFormXObject) {
printf("failed to create xobject from bitmap");
break;
}
PDFModifiedPage* page = new PDFModifiedPage(&inWriter, page_num);
AbstractContentContext* pageContentContext = page->StartContentContext();
// place the image in page bottom left and discard form. scaled to quarter
pageContentContext->q();
pageContentContext->cm(1, 0, 0, 1, x, y);
pageContentContext->Do(pageContentContext->GetResourcesDictionary()->AddFormXObjectMapping(imageFormXObject->GetObjectID()));
pageContentContext->Q();
delete imageFormXObject;
status = page->EndContentContext();
if(status != eSuccess) {
printf("failed to end content context");
break;
}
status = page->WritePage();
if(status != eSuccess) {
printf("failed to write page");
break;
}
delete page;
}
} while(false);
return status;
}
int main(int argc, char** argv) {
EStatusCode status = eSuccess;
PDFWriter writer;
bool original = (bool)atoi(argv[4]);
do {
status = writer.ModifyPDF(
argv[1],
ePDFVersion13,
argv[2],
LogConfiguration(true, true, argv[3])
);
if(status != eSuccess) {
printf("failed to start PDF\n");
break;
}
struct zint_symbol* symbol = NULL;
symbol = ZBarcode_Create();
if(symbol == NULL) {
printf("failed to generate barcode\n");
break;
}
int ret;
symbol->symbology = BARCODE_EXCODE39;
symbol->option_2 = -1;
symbol->width = 151;
symbol->height = 15;
symbol->show_hrt = false;
char barcode[] = "12345678";
ret = ZBarcode_Encode_and_Buffer(symbol, (unsigned char*)barcode, sizeof(barcode), 0);
placeBitmap(writer, 0, 0, 0, symbol->bitmap_width, symbol->bitmap_height, symbol->bitmap, original);
ZBarcode_Delete(symbol);
status = writer.EndPDF();
if(status != eSuccess) {
break;
}
} while(0);
return 0;
}