Haru Free PDF Library
image_demo.c
/*
* << Haru Free PDF Library 2.0.0 >> -- image_demo.c
*
* Copyright (c) 1999-2006 Takeshi Kanno <takeshi_kanno@est.hi-ho.ne.jp>
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation.
* It is provided "as is" without express or implied warranty.
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <setjmp.h>
#include "hpdf.h"
#include "handler.h"
#include "utils.h"
#ifdef LIBHPDF_HAVE_LIBPNG
void
show_description (HPDF_Page page,
float x,
float y,
const char *text)
{
char buf[255];
HPDF_Page_MoveTo (page, x, y - 10);
HPDF_Page_LineTo (page, x, y + 10);
HPDF_Page_MoveTo (page, x - 10, y);
HPDF_Page_LineTo (page, x + 10, y);
HPDF_Page_SetRGBFill (page, 0, 0, 0);
HPDF_snprintf(buf, 255, "(x=%d,y=%d)", (int)x, (int)y);
HPDF_Page_MoveTextPos (page, x - HPDF_Page_TextWidth (page, buf) - 5,
y - 10);
HPDF_Page_ShowText (page, buf);
HPDF_Page_MoveTextPos (page, x - 20, y - 25);
HPDF_Page_ShowText (page, text);
}
int main (int argc, char **argv)
{
HPDF_Doc pdf;
HPDF_Font font;
HPDF_Page page;
char fname[256];
HPDF_Image image;
HPDF_Image image1;
HPDF_Image image2;
HPDF_Image image3;
double x;
double y;
double angle;
double angle1;
double angle2;
double rad;
double rad1;
double rad2;
double iw;
double ih;
strcpy (fname, argv[0]);
strcat (fname, ".pdf");
pdf = HPDF_New (demo_error_handler, NULL);
if (!pdf) {
printf ("error: cannot create PdfDoc object\n");
return 1;
}
/* error-handler */
if (setjmp(env)) {
HPDF_Free (pdf);
return 1;
}
/* create default-font */
font = HPDF_GetFont (pdf, "Helvetica", NULL);
/* add a new page object. */
page = HPDF_AddPage (pdf);
HPDF_Page_SetWidth (page, 550);
HPDF_Page_SetHeight (page, 500);
HPDF_SetOpenAction(pdf, dst);
HPDF_Page_SetFontAndSize (page, font, 20);
HPDF_Page_MoveTextPos (page, 220, HPDF_Page_GetHeight (page) - 70);
HPDF_Page_ShowText (page, "ImageDemo");
/* load image file. */
#ifndef __WIN32__
image = HPDF_LoadPngImageFromFile (pdf, "pngsuite/basn3p02.png");
#else
image = HPDF_LoadPngImageFromFile (pdf, "pngsuite\\basn3p02.png");
#endif
/* image1 is masked by image2. */
#ifndef __WIN32__
image1 = HPDF_LoadPngImageFromFile (pdf, "pngsuite/basn3p02.png");
#else
image1 = HPDF_LoadPngImageFromFile (pdf, "pngsuite\\basn3p02.png");
#endif
/* image2 is a mask image. */
#ifndef __WIN32__
image2 = HPDF_LoadPngImageFromFile (pdf, "pngsuite/basn0g01.png");
#else
image2 = HPDF_LoadPngImageFromFile (pdf, "pngsuite\\basn0g01.png");
#endif
/* image3 is a RGB-color image. we use this image for color-mask
* demo.
*/
#ifndef __WIN32__
image3 = HPDF_LoadPngImageFromFile (pdf, "pngsuite/maskimage.png");
#else
image3 = HPDF_LoadPngImageFromFile (pdf, "pngsuite\\maskimage.png");
#endif
iw = HPDF_Image_GetWidth (image);
ih = HPDF_Image_GetHeight (image);
x = 100;
y = HPDF_Page_GetHeight (page) - 150;
/* Draw image to the canvas. (normal-mode with actual size.)*/
HPDF_Page_DrawImage (page, image, x, y, iw, ih);
show_description (page, x, y, "Actual Size");
x += 150;
/* Scalling image (X direction) */
HPDF_Page_DrawImage (page, image, x, y, iw * 1.5, ih);
show_description (page, x, y, "Scalling image (X direction)");
x += 150;
/* Scalling image (Y direction). */
HPDF_Page_DrawImage (page, image, x, y, iw, ih * 1.5);
show_description (page, x, y, "Scalling image (Y direction)");
x = 100;
y -= 120;
/* Skewing image. */
angle1 = 10;
angle2 = 20;
rad1 = angle1 / 180 * HPDF_PI;
rad2 = angle2 / 180 * HPDF_PI;
HPDF_Page_Concat (page, iw, tan(rad1) * iw, tan(rad2) * ih, ih, x, y);
HPDF_Page_ExecuteXObject (page, image);
show_description (page, x, y, "Skewing image");
x += 150;
/* Rotating image */
angle = 30; /* rotation of 30 degrees. */
rad = angle / 180 * HPDF_PI; /* Calculate the radian value. */
HPDF_Page_Concat (page, iw * cos(rad),
iw * sin(rad),
ih * -sin(rad),
ih * cos(rad),
x, y);
HPDF_Page_ExecuteXObject (page, image);
show_description (page, x, y, "Rotating image");
x += 150;
/* draw masked image. */
/* Set image2 to the mask image of image1 */
HPDF_Image_SetMaskImage (image1, image2);
HPDF_Page_SetRGBFill (page, 0, 0, 0);
HPDF_Page_MoveTextPos (page, x - 6, y + 14);
HPDF_Page_ShowText (page, "MASKMASK");
HPDF_Page_DrawImage (page, image1, x - 3, y - 3, iw + 6, ih + 6);
show_description (page, x, y, "masked image");
x = 100;
y -= 120;
/* color mask. */
HPDF_Page_SetRGBFill (page, 0, 0, 0);
HPDF_Page_MoveTextPos (page, x - 6, y + 14);
HPDF_Page_ShowText (page, "MASKMASK");
HPDF_Image_SetColorMask (image3, 0, 255, 0, 0, 0, 255);
HPDF_Page_DrawImage (page, image3, x, y, iw, ih);
show_description (page, x, y, "Color Mask");
/* save the document to a file */
HPDF_SaveToFile (pdf, fname);
/* clean up */
HPDF_Free (pdf);
return 0;
}
#else /* LIBHPDF_HAVE_LIBPNG */
int main()
{
printf("WARNING: image_demo was not built correctly. \n"
"Make sure libpng is installed and CMake is able to find it.\n");
return 0;
}
#endif /* LIBHPDF_HAVE_LIBPNG */
HPDF_STATUS HPDF_SaveToFile(HPDF_Doc pdf, const char *filename)
Saves the current document to file.
void HPDF_Free(HPDF_Doc pdf)
Revoke a document object and all resources.
HPDF_Doc HPDF_New(HPDF_Error_Handler user_error_fn, void *user_data)
Create an instance of a document object and initialize it.
HPDF_Font HPDF_GetFont(HPDF_Doc pdf, const char *font_name, const char *encoding_name)
Get requested font object handle.
HPDF_STATUS HPDF_Page_SetLineWidth(HPDF_Page page, HPDF_REAL line_width)
Set width of the line used to stroke paths.
HPDF_STATUS HPDF_Page_Stroke(HPDF_Page page)
Paint current path.
HPDF_STATUS HPDF_Page_MoveTo(HPDF_Page page, HPDF_REAL x, HPDF_REAL y)
Start new subpath and move current point for drawing path.
HPDF_STATUS HPDF_Page_SetRGBFill(HPDF_Page page, HPDF_REAL r, HPDF_REAL g, HPDF_REAL b)
Set filling color (RGB).
HPDF_STATUS HPDF_Page_DrawImage(HPDF_Page page, HPDF_Image image, HPDF_REAL x, HPDF_REAL y, HPDF_REAL width, HPDF_REAL height)
Show an image in one operation.
HPDF_STATUS HPDF_Page_GSave(HPDF_Page page)
Save the page's current graphics state to the stack.
HPDF_STATUS HPDF_Page_SetFontAndSize(HPDF_Page page, HPDF_Font font, HPDF_REAL size)
Set the type of font and size leading.
HPDF_STATUS HPDF_Page_Concat(HPDF_Page page, HPDF_REAL a, HPDF_REAL b, HPDF_REAL c, HPDF_REAL d, HPDF_REAL x, HPDF_REAL y)
Concatenate the page's transformation matrix and specified matrix.
HPDF_STATUS HPDF_Page_ExecuteXObject(HPDF_Page page, HPDF_XObject obj)
Draw XObject using current graphics context.
HPDF_STATUS HPDF_Page_LineTo(HPDF_Page page, HPDF_REAL x, HPDF_REAL y)
Append path from current point to specified point.
HPDF_STATUS HPDF_Page_ShowText(HPDF_Page page, const char *text)
Put text at the current text position on the page.
HPDF_STATUS HPDF_Page_GRestore(HPDF_Page page)
Restore graphics state which is saved by HPDF_Page_GSave().
HPDF_STATUS HPDF_Page_BeginText(HPDF_Page page)
Begin text object and set text position to (0, 0).
HPDF_STATUS HPDF_Page_EndText(HPDF_Page page)
Finish text object.
HPDF_STATUS HPDF_Page_MoveTextPos(HPDF_Page page, HPDF_REAL x, HPDF_REAL y)
Change current text position using the specified offset values.
HPDF_STATUS HPDF_Image_SetColorMask(HPDF_Image image, HPDF_UINT rmin, HPDF_UINT rmax, HPDF_UINT gmin, HPDF_UINT gmax, HPDF_UINT bmin, HPDF_UINT bmax)
Sets transparent color of the image by the RGB range values.
HPDF_STATUS HPDF_Image_SetMaskImage(HPDF_Image image, HPDF_Image mask_image)
Set image mask.
HPDF_UINT HPDF_Image_GetWidth(HPDF_Image image)
Get image width of an image object.
HPDF_Image HPDF_LoadPngImageFromFile(HPDF_Doc pdf, const char *filename)
Load external PNG image file.
HPDF_UINT HPDF_Image_GetHeight(HPDF_Image image)
Get image height of an image object.
HPDF_REAL HPDF_Page_TextWidth(HPDF_Page page, const char *text)
Get text width in current font size, character spacing and word spacing.
HPDF_REAL HPDF_Page_GetHeight(HPDF_Page page)
Get page height.
HPDF_STATUS HPDF_SetOpenAction(HPDF_Doc pdf, HPDF_Destination open_action)
Set the first page to appear when a document is opened.
HPDF_Page HPDF_AddPage(HPDF_Doc pdf)
Create new page and add it after the last page of document.
HPDF_Font HPDF_Page_GetCurrentFont(HPDF_Page page)
Get page current font handle.
HPDF_STATUS HPDF_Page_SetWidth(HPDF_Page page, HPDF_REAL value)
Change page width.
HPDF_STATUS HPDF_Page_SetHeight(HPDF_Page page, HPDF_REAL value)
Change page height.
HPDF_STATUS HPDF_SetCompressionMode(HPDF_Doc pdf, HPDF_UINT mode)
Set compression mode.
#define HPDF_COMP_ALL
Definition: hpdf_consts.h:82
#define HPDF_PI
Definition: hpdf_consts.h:32
Definition: hpdf_objects.h:333
Definition: hpdf_objects.h:421
Definition: hpdf_doc.h:36