Hi all!
Just wanted to share a litte patch for file-pdf.c to support a max width/height
and a page number as parameters.
Maybe someone can clean the code and push it into the next gimp release.
Usage:
gimp -i -b '(pdftojpg inputfilename maxWidth maxHeight outputfilename quality
radius delta brightness contrast page)' -b '(gimp-quit 0)'
Example:
gimp -i -b '(pdftojpg "/tmp/input.pdf" 1000 1800 "/tmp/output.jpg" 0.85 3.5 15
-10 5 3)' -b '(gimp-quit 0)'
Have fun,
Thomas
--
Thomas Frieling
http://www.kaufda.de
Juno Internet GmbH
Geschäftsführer
Tel.: +49 (0) 30 / 253-293-25
Mobil: + 49 (0) 173 / 63 62 62 3
[email protected]
https://www.xing.com/profile/Thomas_Frieling2
Sitz des kaufDA Office:
Kunsthaus Lempertz, Poststr. 21-22, 10178 Berlin
Sitz der Gesellschaft:
Fronhoferstr. 6, 12165 Berlin
Amtsgericht Charlottenburg - HRB 115393 B
Geschäftsführung: Cihan Aksakal, Thomas Frieling, Christian W. Gaiser, Tim M.
Marbach
*** file-pdf.c 2008-11-20 23:43:49.000000000 +0100
--- file-pdf-patched.c 2009-05-07 16:51:02.000000000 +0200
***************
*** 264,271 ****
static const GimpParamDef thumb_args[] =
{
! { GIMP_PDB_STRING, "filename", "The name of the file to load" },
! { GIMP_PDB_INT32, "thumb-size", "Preferred thumbnail size" }
};
static const GimpParamDef thumb_return_vals[] =
--- 264,277 ----
static const GimpParamDef thumb_args[] =
{
! { GIMP_PDB_STRING, "filename", "The name of the file to load" },
! /* start of patched part to define preferred height and width of thumbnail - by Cihan Aksakal, Nils Mull */
! { GIMP_PDB_INT32, "preferred-width", "Preferred thumbnail width" },
! { GIMP_PDB_INT32, "preferred-height", "Preferred thumbnail height" },
! /* end of patched part to define preferred height and width of thumbnail - by Cihan Aksakal, Nils Mull */
! /* start of patched part to define page number to extract - by Cihan Aksakal, Nils Mull */
! { GIMP_PDB_INT32, "page-number", "Page Number to extract (Note: 0-Based)" }
! /* end of patched part to define page number to extract - by Cihan Aksakal, Nils Mull */
};
static const GimpParamDef thumb_return_vals[] =
***************
*** 427,513 ****
}
else if (strcmp (name, LOAD_THUMB_PROC) == 0)
{
! if (nparams < 2)
! {
status = GIMP_PDB_CALLING_ERROR;
! }
! else
! {
gdouble width = 0;
gdouble height = 0;
gdouble scale;
gint32 image = -1;
GdkPixbuf *pixbuf = NULL;
/* Possibly retrieve last settings */
gimp_get_data (LOAD_PROC, &loadvals);
doc = open_document (param[0].data.d_string, &error);
! if (doc)
! {
! PopplerPage *page = poppler_document_get_page (doc, 0);
!
! if (page)
! {
! poppler_page_get_size (page, &width, &height);
!
! g_object_unref (page);
}
! pixbuf = get_thumbnail (doc, 0, param[1].data.d_int32);
g_object_unref (doc);
- }
-
- if (pixbuf)
- {
- image = gimp_image_new (gdk_pixbuf_get_width (pixbuf),
- gdk_pixbuf_get_height (pixbuf),
- GIMP_RGB);
-
- gimp_image_undo_disable (image);
-
- layer_from_pixbuf (image, "thumbnail", 0, pixbuf, 0.0, 1.0);
- g_object_unref (pixbuf);
! gimp_image_undo_enable (image);
! gimp_image_clean_all (image);
}
! scale = loadvals.resolution / gimp_unit_get_factor (GIMP_UNIT_POINT);
- width *= scale;
- height *= scale;
-
- if (image != -1)
- {
- *nreturn_vals = 4;
-
- values[1].type = GIMP_PDB_IMAGE;
- values[1].data.d_image = image;
- values[2].type = GIMP_PDB_INT32;
- values[2].data.d_int32 = width;
- values[3].type = GIMP_PDB_INT32;
- values[3].data.d_int32 = height;
- }
- else
- {
- status = GIMP_PDB_EXECUTION_ERROR;
- }
}
! }
! else
! {
status = GIMP_PDB_CALLING_ERROR;
}
! if (status != GIMP_PDB_SUCCESS && error)
! {
*nreturn_vals = 2;
values[1].type = GIMP_PDB_STRING;
values[1].data.d_string = error->message;
! }
values[0].data.d_status = status;
}
--- 433,568 ----
}
else if (strcmp (name, LOAD_THUMB_PROC) == 0)
{
! if (nparams < 2) {
status = GIMP_PDB_CALLING_ERROR;
! } else {
! /* start of patched part to define page number to extract - by Cihan Aksakal, Nils Mull */
gdouble width = 0;
gdouble height = 0;
gdouble scale;
gint32 image = -1;
GdkPixbuf *pixbuf = NULL;
+ /* calculate page number to extract */
+ gint32 extractionPageNumber = 0;
+ if (param[3].data.d_int32 > 0) {
+ extractionPageNumber = param[3].data.d_int32;
+ }
/* Possibly retrieve last settings */
gimp_get_data (LOAD_PROC, &loadvals);
doc = open_document (param[0].data.d_string, &error);
+
+ /* check whether the document is loaded and contains enough pages to process the extraction of the defined page */
+ if (doc && poppler_document_get_n_pages(doc) > extractionPageNumber) {
+ /* added var extractionPageNumber instead of 0 - now it extracts the defined page */
+ PopplerPage *page = poppler_document_get_page (doc, extractionPageNumber);
+ if (page) {
+ poppler_page_get_size (page, &width, &height);
+ g_object_unref (page);
+ }
! /* calculate prefered size */
! if (param[1].data.d_int32 > 0 && param[2].data.d_int32 > 0) {
! /* calculate preferred width and height */
! gint32 preferredSize = 0;
!
! gint32 bbWidth = param[1].data.d_int32;
! gint32 bbHeight = param[2].data.d_int32;
!
! gdouble pWidth = 0;
! gdouble pHeight = 0;
!
! /* calculation for image scaling & preferredSize */
! if (width > height) {
! printf("width > height\n");
! pWidth = bbWidth;
! pHeight = (gdouble) (pWidth/width) * height;
! printf("%f x %f\n", pWidth, pHeight);
! if (pHeight > bbHeight) {
! printf("pHeight (%f) > bbHeight (%d)\n", pHeight, bbHeight);
! pWidth = (gdouble) (bbHeight/pHeight) * pWidth; // check
! pHeight = bbHeight;
! printf("%f x %f\n", pWidth, pHeight);
}
+ } else {
+ printf("width =< height\n");
+ pHeight = bbHeight;
+ pWidth = (gdouble) (pHeight/height) * width;
+ printf("%f x %f\n", pWidth, pHeight);
+ if (pWidth > bbWidth) {
+ printf("pWidth (%f) > bbWidth (%d)\n", pWidth, bbWidth);
+ pHeight = (gdouble) (bbWidth/pWidth) * pHeight; // check
+ pWidth = bbWidth;
+ printf("%f x %f\n", pWidth, pHeight);
+ }
+ }
+ preferredSize = MAX(pWidth, pHeight);
! /* added var extractionPageNumber instead of 0 - now it extracts the defined page */
! pixbuf = get_thumbnail (doc, extractionPageNumber, preferredSize);
g_object_unref (doc);
! if (pixbuf) {
! image = gimp_image_new (gdk_pixbuf_get_width (pixbuf),
! gdk_pixbuf_get_height (pixbuf),
! GIMP_RGB);
!
! gimp_image_undo_disable (image);
!
! layer_from_pixbuf (image, "thumbnail", 0, pixbuf, 0.0, 1.0);
! g_object_unref (pixbuf);
!
! gimp_image_undo_enable (image);
! gimp_image_clean_all (image);
! }
!
! scale = loadvals.resolution / gimp_unit_get_factor (GIMP_UNIT_POINT);
!
! width *= scale;
! height *= scale;
!
! if (image != -1) {
! *nreturn_vals = 4;
!
! values[1].type = GIMP_PDB_IMAGE;
! values[1].data.d_image = image;
! values[2].type = GIMP_PDB_INT32;
! values[2].data.d_int32 = width;
! values[3].type = GIMP_PDB_INT32;
! values[3].data.d_int32 = height;
! } else {
! status = GIMP_PDB_EXECUTION_ERROR;
! }
! } else { /* preferred height and width parameters check failed */
! /* set error message and status that a file page number violation occured */
! g_set_error (&error, 0, 0,
! "Could not calculate preferred thumbail height and width (width: '%d', height: '%d' ). Note: Both parameters have to be greater 0. PDF-File: '%s'",
! param[1].data.d_int32, param[2].data.d_int32, gimp_filename_to_utf8 (param[0].data.d_string) );
! status = GIMP_PDB_EXECUTION_ERROR;
}
! } else if (doc) { /* check doc & page number failed */
! /* set error message and status that a file page number violation occured */
! g_set_error (&error, 0, 0,
! "Could not extract page-number %d (Note: 0-Based). Documnent has only %d pages. PDF-File: '%s'",
! extractionPageNumber, poppler_document_get_n_pages(doc), gimp_filename_to_utf8 (param[0].data.d_string) );
! status = GIMP_PDB_EXECUTION_ERROR;
! }
! /* check doc & page number */
}
+ /* end of patched part to define page number to extract - by Cihan Aksakal, Nils Mull */
! } else {
status = GIMP_PDB_CALLING_ERROR;
}
! if (status != GIMP_PDB_SUCCESS && error) {
*nreturn_vals = 2;
values[1].type = GIMP_PDB_STRING;
values[1].data.d_string = error->message;
! }
values[0].data.d_status = status;
}
_______________________________________________
Gimp-developer mailing list
[email protected]
https://lists.XCF.Berkeley.EDU/mailman/listinfo/gimp-developer