Skip to content

Commit

Permalink
1.3: extended margin option
Browse files Browse the repository at this point in the history
  • Loading branch information
zvezdochiot committed Nov 30, 2019
1 parent cf11b19 commit 74a5a3c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 61 deletions.
10 changes: 2 additions & 8 deletions jpeg2pdf.1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.TH JPEG2PDF 1 "27 Dec 2017" "1.2.1" "User Manual"
.TH JPEG2PDF 1 "30 Nou 2019" "1.3" "User Manual"
.SH DESCRIPTION
jpeg2pdf
Lossless conversion of jpeg images to PDF.
Expand Down Expand Up @@ -27,7 +27,7 @@ A0-A10,Letter,Legal,Junior,Ledger,Tabloid,auto default:auto
\fB-n\fP orientation
auto,portrait,landscape default:auto
.TP
\fB-m\fP marginsize
\fB-m\fP l[,t[,r,b]]
margins size in inches (specify 'mm' for millimeters, 'p' for points) default:0
.TP
\fB-x\fP widthpage
Expand All @@ -36,12 +36,6 @@ width page in inches (specify 'mm' for millimeters, 'p' for points)
\fB-y\fP heightpage
height page in inches (specify 'mm' for millimeters, 'p' for points)
.TP
\fB-l\fP leftsize
left size in inches (specify 'mm' for millimeters, 'p' for points)
.TP
\fB-b\fP bottomsize
bottom size in inches (specify 'mm' for millimeters, 'p' for points)
.TP
\fB-z\fP scale
fit,fw,fh,reduce,rw,rh,none default:fit
.TP
Expand Down
17 changes: 9 additions & 8 deletions src/jpeg2pdf.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ static void Jpeg2PDF_SetXREF(PJPEG2PDF pPDF, int index, int offset, char c)
if(JPEG2PDF_DEBUG) logMsg("pPDF->pdfXREF[%d] = %s", index, (int)pPDF->pdfXREF[index], 3,4,5,6);
}

PJPEG2PDF Jpeg2PDF_BeginDocument(double pdfW, double pdfH, double margin) // width and height in default (portrait) orientation
PJPEG2PDF Jpeg2PDF_BeginDocument(double pdfW, double pdfH, JPEG2PDF_MARGIN margin) // width and height in default (portrait) orientation
{
PJPEG2PDF pPDF;

Expand All @@ -35,8 +35,8 @@ PJPEG2PDF Jpeg2PDF_BeginDocument(double pdfW, double pdfH, double margin) // w
pPDF->pageH = (double)(pdfH * PDF_DOT_PER_INCH);
//Maximum image size without margins
pPDF->margin = margin;
pPDF->maxImgW = (double) pPDF->pageW - (2.0 * margin * PDF_DOT_PER_INCH);
pPDF->maxImgH = (double) pPDF->pageH - (2.0 * margin * PDF_DOT_PER_INCH);
pPDF->maxImgW = (double) pPDF->pageW - (margin.width * PDF_DOT_PER_INCH);
pPDF->maxImgH = (double) pPDF->pageH - (margin.height * PDF_DOT_PER_INCH);
if(JPEG2PDF_DEBUG) logMsg("PDF Page Size (%d %d) Max Image Size (%f %f)\n", pPDF->pageW, pPDF->pageH, pPDF->maxImgW, pPDF->maxImgH,3,4,5,6);

pPDF->currentOffSet = 0;
Expand All @@ -52,10 +52,11 @@ PJPEG2PDF Jpeg2PDF_BeginDocument(double pdfW, double pdfH, double margin) // w
}

STATUS Jpeg2PDF_AddJpeg(PJPEG2PDF pPDF, UINT32 imgW, UINT32 imgH, UINT32 fileSize, UINT8 *pJpeg, UINT8 isColor, PageOrientation pageOrientation,
double dpiX, double dpiY, ScaleMethod scale, double pageLeft, double pageBottom, bool cropHeight, bool cropWidth)
double dpiX, double dpiY, ScaleMethod scale, bool cropHeight, bool cropWidth)
{
STATUS result = ERROR;
PJPEG2PDF_NODE pNode;
double pageLeft, pageBottom;
double imgAspect, newImgW, newImgH;

if(pPDF)
Expand Down Expand Up @@ -176,8 +177,8 @@ STATUS Jpeg2PDF_AddJpeg(PJPEG2PDF pPDF, UINT32 imgW, UINT32 imgH, UINT32 fileSiz
}

// Set paper size from image size (possibly fitted/reduced to specific paper size) or properly rotate the page:
pageWidth = cropWidth ? (newImgW+pPDF->margin) : (pagePortrait ? pPDF->pageW : pPDF->pageH);
pageHeight = cropHeight ? (newImgH+pPDF->margin) : (pagePortrait ? pPDF->pageH : pPDF->pageW);
pageWidth = cropWidth ? (newImgW+pPDF->margin.width) : (pagePortrait ? pPDF->pageW : pPDF->pageH);
pageHeight = cropHeight ? (newImgH+pPDF->margin.height) : (pagePortrait ? pPDF->pageH : pPDF->pageW);

/* Page Object */
Jpeg2PDF_SetXREF(pPDF, INDEX_USE_PPDF, pPDF->currentOffSet, 'n');
Expand All @@ -190,8 +191,8 @@ STATUS Jpeg2PDF_AddJpeg(PJPEG2PDF pPDF, UINT32 imgW, UINT32 imgH, UINT32 fileSiz

/* Contents Object in Page Object */
Jpeg2PDF_SetXREF(pPDF, INDEX_USE_PPDF, pPDF->currentOffSet, 'n');
pageLeft = (pageLeft < 0) ? (pageWidth - newImgW)/2 : (pageLeft * PDF_DOT_PER_INCH);
pageBottom = (pageBottom < 0) ? (pageHeight - newImgH)/2 : (pageBottom * PDF_DOT_PER_INCH);
pageLeft = (pPDF->margin.left < 0) ? (pageWidth - newImgW)/2 : (pPDF->margin.left * PDF_DOT_PER_INCH);
pageBottom = (pPDF->margin.bottom < 0) ? (pageHeight - newImgH)/2 : (pPDF->margin.bottom * PDF_DOT_PER_INCH);
sprintf(lenStr, "q\n1 0 0 1 %.2f %.2f cm\n%.2f 0 0 %.2f 0 0 cm\n/I%d Do\nQ\n",
pageLeft, pageBottom, newImgW, newImgH, pPDF->imgObj); // center image
nChars = sprintf(pFormat, "%d 0 obj\n<</Length %d 0 R>>stream\n%sendstream\nendobj\n",
Expand Down
18 changes: 15 additions & 3 deletions src/jpeg2pdf.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,18 @@ struct jpeg2pdf_Node_struct
UINT8 pstFormat[MAX_PDF_PSTFORMAT_SIZE];
};

struct jpeg2pdf_Margin
{
double left;
double top;
double right;
double bottom;
double width;
double height;
};

typedef struct jpeg2pdf_Node_struct JPEG2PDF_NODE, *PJPEG2PDF_NODE;
typedef struct jpeg2pdf_Margin JPEG2PDF_MARGIN, *PJPEG2PDF_MARGIN;

#define XREF_ENTRY_LEN 20 /* Each XREF entry is 20 Bytes */
#define OBJNUM_EXTRA 3 /* First Free Object; Kids Object; Catalog Object */
Expand All @@ -58,7 +69,8 @@ typedef struct
UINT8 pdfTailer[MAX_PDF_TAILER]; /* 28K Bytes */
UINT8 pdfXREF[MAX_PDF_XREF][XREF_ENTRY_LEN + 1]; /* 27K Bytes */
UINT32 pdfObj, currentOffSet, imgObj;
double pageW, pageH, margin;
double pageW, pageH;
JPEG2PDF_MARGIN margin;
double maxImgW, maxImgH;
} JPEG2PDF, *PJPEG2PDF;

Expand All @@ -67,8 +79,8 @@ typedef enum {ScaleFit, ScaleFitWidth, ScaleFitHeight, ScaleReduce, ScaleReduceW
typedef enum {FitWidth, FitHeight, FitNone} Fit; // how we should actually fit the image
typedef enum {false=0, true} bool;

PJPEG2PDF Jpeg2PDF_BeginDocument(double pdfW, double pdfH, double margin); /* pdfW, pdfH: Page Size in Inch ( 1 inch=25.4 mm ) */
STATUS Jpeg2PDF_AddJpeg(PJPEG2PDF pPDF, UINT32 imgW, UINT32 imgH, UINT32 fileSize, UINT8 *pJpeg, UINT8 isColor, PageOrientation pageOrientation, double dpiX, double dpiY, ScaleMethod scale, double pageLeft, double pageBottom, bool cropHeight, bool cropWidth);
PJPEG2PDF Jpeg2PDF_BeginDocument(double pdfW, double pdfH, JPEG2PDF_MARGIN margin); /* pdfW, pdfH: Page Size in Inch ( 1 inch=25.4 mm ) */
STATUS Jpeg2PDF_AddJpeg(PJPEG2PDF pPDF, UINT32 imgW, UINT32 imgH, UINT32 fileSize, UINT8 *pJpeg, UINT8 isColor, PageOrientation pageOrientation, double dpiX, double dpiY, ScaleMethod scale, bool cropHeight, bool cropWidth);
UINT32 Jpeg2PDF_EndDocument(PJPEG2PDF pPDF, char *timestamp, char* title, char* author, char* keywords, char* subject, char *creator);
STATUS Jpeg2PDF_GetFinalDocumentAndCleanup(PJPEG2PDF pPDF, UINT8 *outPDF, UINT32 *outPDFSize);

Expand Down
79 changes: 37 additions & 42 deletions src/jpeg2pdfcli.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

#include "jpeg2pdf.h"

unsigned char *JPEG2PDF_VERSION = "1.2";
unsigned char *JPEG2PDF_VERSION = "1.3";

//Gets the JPEG size from the array of data passed to the function, file reference: http://www.obrador.com/essentialjpeg/headerinfo.htm
static int get_jpeg_size(unsigned char* data, unsigned int data_size, unsigned short *width, unsigned short *height, unsigned char *colors, double* dpiX, double* dpiY)
Expand Down Expand Up @@ -81,7 +81,7 @@ static int get_jpeg_size(unsigned char* data, unsigned int data_size, unsigned s
}
}

void insertJPEGFile(const char *fileName, int fileSize, PJPEG2PDF pdfId, PageOrientation pageOrientation, ScaleMethod scale, int pageDpi, double pageLeft, double pageBottom, bool cropHeight, bool cropWidth)
void insertJPEGFile(const char *fileName, int fileSize, PJPEG2PDF pdfId, PageOrientation pageOrientation, ScaleMethod scale, int pageDpi, bool cropHeight, bool cropWidth)
{
FILE *fp;
unsigned char *jpegBuf;
Expand Down Expand Up @@ -118,7 +118,7 @@ void insertJPEGFile(const char *fileName, int fileSize, PJPEG2PDF pdfId, PageOri
}
printf("Adding %s (%dx%d, %.0fx%.0f dpi)\n", fileName, jpegImgW, jpegImgH, dpiX, dpiY);
/* Add JPEG File into PDF */
Jpeg2PDF_AddJpeg(pdfId, jpegImgW, jpegImgH, readInSize, jpegBuf, (3==colors), pageOrientation, dpiX, dpiY, scale, pageLeft, pageBottom, cropHeight, cropWidth);
Jpeg2PDF_AddJpeg(pdfId, jpegImgW, jpegImgH, readInSize, jpegBuf, (3==colors), pageOrientation, dpiX, dpiY, scale, cropHeight, cropWidth);
}
else
{
Expand Down Expand Up @@ -279,11 +279,9 @@ void printHelp( char *appname )
" [-d dpi] global page dpi, default: get in image\n" \
" [-p papersize] A0-A10,Letter,Legal,Junior,Ledger,Tabloid,auto default:auto\n" \
" [-n orientation] auto,portrait,landscape default:auto\n" \
" [-m marginsize] margins size in inches (specify 'mm' for millimeters, 'p' for points) default:0\n" \
" [-m l,t,r,b] margins size in inches (specify 'mm' for millimeters, 'p' for points) default:0\n" \
" [-x widthpage] width page in inches (specify 'mm' for millimeters, 'p' for points) default:0\n" \
" [-y heightpage] height page in inches (specify 'mm' for millimeters, 'p' for points) default:0\n" \
" [-l leftsize] left size in inches (specify 'mm' for millimeters, 'p' for points) default:0\n" \
" [-b bottomsize] bottom size in inches (specify 'mm' for millimeters, 'p' for points) default:0\n" \
" [-z scale] fit,fw,fh,reduce,rw,rh,none default:fit\n" \
" [-r crop] none,height,width,both default:none crop/expand page to image size\n" \
" [-t title] default: none\n" \
Expand All @@ -306,7 +304,7 @@ int main(int argc, char *argv[])
FILE *fp;
char *title=NULL, *author=NULL, *keywords=NULL, *subject=NULL, *creator=NULL;
int opt;
double pageWidth=8.27, pageHeight=11.69, pageMargins=0, pageWidthDef=0, pageHeightDef=0, pageLeftDef=-1, pageBottomDef=-1;
double pageWidth=8.27, pageHeight=11.69, pageWidthDef=0, pageHeightDef=0;
int globindex, globlen;
#ifdef HAVE_GLOB
glob_t globbuf;
Expand All @@ -322,6 +320,10 @@ int main(int argc, char *argv[])
struct tm *tmp;
char timestamp[40];
int keywordslen, pageDpi=-1;
int np;
JPEG2PDF_MARGIN pageMargins;
pageMargins.left = pageMargins.right = pageMargins.top = pageMargins.bottom = -1.0;
pageMargins.width = pageMargins.height = 0;

PageOrientation pageOrientation=PageOrientationAuto;
ScaleMethod scale=ScaleFit;
Expand All @@ -334,7 +336,7 @@ int main(int argc, char *argv[])
printHelp( argv[0] );
}

while ((opt = getopt(argc, argv, "o:d:p:n:z:m:x:y:l:b:a:k:s:c:r:h")) != -1)
while ((opt = getopt(argc, argv, "o:d:p:n:z:m:x:y:a:k:s:c:r:h")) != -1)
{
switch (opt)
{
Expand Down Expand Up @@ -435,16 +437,33 @@ int main(int argc, char *argv[])
}
break;
case 'm':
pageMargins = atof(optarg);
np = sscanf(optarg, "%lf,%lf,%lf,%lf", &pageMargins.left, &pageMargins.top, &pageMargins.right, &pageMargins.bottom);
if (np < 2)
{
pageMargins.top = pageMargins.right = pageMargins.bottom = pageMargins.left;
}
else if (np < 3)
{
pageMargins.right = pageMargins.left;
pageMargins.bottom = pageMargins.top;
}
/* mm */
if( strchr(optarg,'m')!=NULL )
{
pageMargins /= 25.4;
pageMargins.left /= 25.4;
pageMargins.top /= 25.4;
pageMargins.right /= 25.4;
pageMargins.bottom /= 25.4;
}
else if( strchr(optarg,'p')!=NULL )
{
pageMargins /= ((pageDpi < 0) ? PDF_DEF_DENSITY : pageDpi);
pageMargins.left /= ((pageDpi < 0) ? PDF_DEF_DENSITY : pageDpi);
pageMargins.top /= ((pageDpi < 0) ? PDF_DEF_DENSITY : pageDpi);
pageMargins.right /= ((pageDpi < 0) ? PDF_DEF_DENSITY : pageDpi);
pageMargins.bottom /= ((pageDpi < 0) ? PDF_DEF_DENSITY : pageDpi);
}
pageMargins.width = pageMargins.left + pageMargins.right;
pageMargins.height = pageMargins.top + pageMargins.bottom;
break;
case 'x':
pageWidthDef = atof(optarg);
Expand All @@ -455,8 +474,8 @@ int main(int argc, char *argv[])
}
else if( strchr(optarg,'p')!=NULL )
{
pageWidthDef /= ((pageDpi < 0) ? PDF_DEF_DENSITY : pageDpi);
}
pageWidthDef /= ((pageDpi < 0) ? PDF_DEF_DENSITY : pageDpi);
}
break;
case 'y':
pageHeightDef = atof(optarg);
Expand All @@ -467,31 +486,7 @@ int main(int argc, char *argv[])
}
else if( strchr(optarg,'p')!=NULL )
{
pageHeightDef /= ((pageDpi < 0) ? PDF_DEF_DENSITY : pageDpi);
}
break;
case 'l':
pageLeftDef = atof(optarg);
/* mm */
if( strchr(optarg,'m')!=NULL )
{
pageLeftDef /= 25.4;
}
else if( strchr(optarg,'p')!=NULL )
{
pageLeftDef /= ((pageDpi < 0) ? PDF_DEF_DENSITY : pageDpi);
}
break;
case 'b':
pageBottomDef = atof(optarg);
/* mm */
if( strchr(optarg,'m')!=NULL )
{
pageBottomDef /= 25.4;
}
else if( strchr(optarg,'p')!=NULL )
{
pageBottomDef /= ((pageDpi < 0) ? PDF_DEF_DENSITY : pageDpi);
pageHeightDef /= ((pageDpi < 0) ? PDF_DEF_DENSITY : pageDpi);
}
break;
case 't':
Expand Down Expand Up @@ -732,8 +727,8 @@ int main(int argc, char *argv[])
#ifdef HAVE_FINDFIRST
findMaximumDimensions(filesarray, globlen, pageDpi, pageOrientation==Portrait || pageOrientation==Landscape, &pageWidth, &pageHeight);
#endif
pageWidth += (pageMargins * 2.0);
pageHeight += (pageMargins * 2.0);
pageWidth += pageMargins.width;
pageHeight += pageMargins.height;
printf("Selected paper size: %.2f x %.2f \" or %.2f x %.2f cm.\n", pageWidth, pageHeight, pageWidth*2.54, pageHeight*2.54);
}
}
Expand All @@ -753,7 +748,7 @@ int main(int argc, char *argv[])
perror("stat");
exit(EXIT_FAILURE);
}
insertJPEGFile(globbuf.gl_pathv[globindex], sb.st_size, pdfId, pageOrientation, scale, pageDpi, pageLeftDef, pageBottomDef, cropHeight, cropWidth);
insertJPEGFile(globbuf.gl_pathv[globindex], sb.st_size, pdfId, pageOrientation, scale, pageDpi, cropHeight, cropWidth);
}
globfree(&globbuf);
#endif
Expand All @@ -765,7 +760,7 @@ int main(int argc, char *argv[])
perror("stat");
exit(EXIT_FAILURE);
}
insertJPEGFile(filesarray[globindex], sb.st_size, pdfId, pageOrientation, scale, pageDpi, pageLeftDef, pageBottomDef, cropHeight, cropWidth);
insertJPEGFile(filesarray[globindex], sb.st_size, pdfId, pageOrientation, scale, pageDpi, cropHeight, cropWidth);
}
#endif

Expand Down

1 comment on commit 74a5a3c

@zvezdochiot
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1.3:

  • extended margin option
  • remove left ant bottom offset options

Please sign in to comment.