define SAVEJPG int generateOutData const std vector uint8_t decodedDat

  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
//#define SAVEJPG
int generateOutData(const std::vector<uint8_t> &decodedData,
const std::string &outFile, bool forceJpgOut,
float dpi = 300.0f, bool addSpaces = false) {
constexpr size_t minHeight = 1754;
float pt_per_px = 72.0f / dpi;
HPDF_Doc pdf;
HPDF_Destination dst;
pdf = HPDF_New(error_handler, NULL);
if (!pdf) {
printf("error: cannot create PdfDoc object\n");
return 1;
}
scope_guard { HPDF_Free(pdf); };
HPDF_SetCompressionMode(pdf, HPDF_COMP_ALL);
size_t size = decodedData.size();
uint8_t jpegEnd[] = {0xFF, 0xD9};
auto start = (void *)&decodedData[0];
int jpegNum = 0;
while (size > 0) {
auto jpegEndPtr = memmem(start, size, jpegEnd, sizeof(jpegEnd));
if (!jpegEndPtr) {
break;
}
jpegEndPtr += sizeof(jpegEnd);
size_t imageSize = (uint8_t *)jpegEndPtr - (uint8_t *)start;
HPDF_Page page;
page = HPDF_AddPage(pdf);
HPDF_Image image;
if (forceJpgOut) {
std::ofstream fout(std::to_string(jpegNum) + ".jpg");
for (size_t i = 0; i < imageSize; i++) {
fout << ((uint8_t *)start)[i];
}
jpegNum++;
fout.close();
}
image = HPDF_LoadJpegImageFromMem(pdf, (uint8_t *)start, imageSize);
auto localSize = HPDF_Image_GetSize(image);
float imageWidth = pt_per_px * localSize.x;
float imageHeight = pt_per_px * localSize.y;
HPDF_Page_SetWidth(page, imageWidth);
if(imageHeight > minHeight) {
HPDF_Page_SetHeight(page, imageHeight);
} else {
HPDF_Page_SetHeight(page, imageWidth/0.70695553021);
}
dst = HPDF_Page_CreateDestination(page);
auto pageWidth = HPDF_Page_GetWidth(page), pageHeight = HPDF_Page_GetHeight(page);
HPDF_Destination_SetXYZ(dst, 0, pageHeight, 1);
HPDF_Page currentpage = HPDF_GetCurrentPage(pdf);
HPDF_SetOpenAction(pdf, dst);
writeLogf(General, "Size: %.1fx%.1fpx, %.3fx%.fpt", localSize.x, localSize.y, imageWidth, imageHeight);
if(addSpaces) {
if(imageHeight > 100) {
HPDF_Page_DrawImage(currentpage, image, 9, pageHeight - imageHeight + 9, imageWidth - 18, imageHeight - 18);
} else {
HPDF_Page_DrawImage(currentpage, image, 9, pageHeight - imageHeight - 9, imageWidth, imageHeight);
}
} else {
HPDF_Page_DrawImage(currentpage, image, 0, pageHeight - imageHeight, imageWidth, imageHeight);
}
size -= imageSize;
start = jpegEndPtr;
}
if (outFile.empty()) {
HPDF_SaveToStream(pdf);
HPDF_ResetStream(pdf);
for (;;) {
HPDF_BYTE buf[4096];
HPDF_UINT32 size = 4096;
HPDF_ReadFromStream(pdf, buf, &size);
if (size == 0) break;
if (fwrite(buf, size, 1, stdout) != 1) {
fprintf(stderr, "cannot write zjs pdf to stdout");
break;
}
}
} else {
HPDF_SaveToFile(pdf, outFile.c_str());
}
return 0;
}