1### 2## * << Haru Free PDF Library 2.0.0 >> -- image_demo.c 3## * 4## * Copyright (c) 1999-2006 Takeshi Kanno <takeshi_kanno@est.hi-ho.ne.jp> 5## * 6## * Permission to use, copy, modify, distribute and sell this software 7## * and its documentation for any purpose is hereby granted without fee, 8## * provided that the above copyright notice appear in all copies and 9## * that both that copyright notice and this permission notice appear 10## * in supporting documentation. 11## * It is provided "as is" without express or implied warranty. 12## * 13## 14 15## port to python by Li Jun 16## http://groups.google.com/group/pythoncia 17 18import os, sys 19 20from ctypes import * 21up=2 22def setlibpath(up): 23 import sys 24 path=os.path.normpath(os.path.split(os.path.realpath(__file__))[0]+'\..'*up) 25 if path not in sys.path: 26 sys.path.append(path) 27 28setlibpath(up) 29 30from haru import * 31from haru.c_func import * 32from haru.hpdf_errorcode import * 33 34from math import * 35 36@HPDF_Error_Handler(None, HPDF_UINT, HPDF_UINT, c_void_p) 37def error_handler (error_no, detail_no, user_data): 38 global pdf 39 printf ("ERROR: %s, detail_no=%u\n", error_detail[error_no], 40 detail_no) 41 HPDF_Free (pdf) 42 sys.exit(1) 43 44 45def show_description (page, x, y, text): 46 HPDF_Page_MoveTo (page, x, y - 10) 47 HPDF_Page_LineTo (page, x, y + 10) 48 HPDF_Page_MoveTo (page, x - 10, y) 49 HPDF_Page_LineTo (page, x + 10, y) 50 HPDF_Page_Stroke (page) 51 52 HPDF_Page_SetFontAndSize (page, HPDF_Page_GetCurrentFont (page), 8) 53 HPDF_Page_SetRGBFill (page, 0, 0, 0) 54 55 HPDF_Page_BeginText (page) 56 57 buf="(x=%d,y=%d)" % (int(x), int(y)) 58 59 HPDF_Page_MoveTextPos (page, x - HPDF_Page_TextWidth (page, buf) - 5, 60 y - 10) 61 HPDF_Page_ShowText (page, buf) 62 HPDF_Page_EndText (page) 63 64 HPDF_Page_BeginText (page) 65 HPDF_Page_MoveTextPos (page, x - 20, y - 25) 66 HPDF_Page_ShowText (page, text) 67 HPDF_Page_EndText (page) 68 69 70def main (): 71 72 fname=os.path.realpath(sys.argv[0]) 73 fname=fname[:fname.rfind('.')]+'.pdf' 74 75 pdf = HPDF_New (error_handler, NULL) 76 if (not pdf): 77 printf ("error: cannot create PdfDoc object\n") 78 return 1 79 80 HPDF_SetCompressionMode (pdf, HPDF_COMP_ALL) 81 82 # create default-font 83 font = HPDF_GetFont (pdf, "Helvetica", NULL) 84 85 # add a new page object. 86 page = HPDF_AddPage (pdf) 87 88 HPDF_Page_SetWidth (page, 550) 89 HPDF_Page_SetHeight (page, 500) 90 91 dst = HPDF_Page_CreateDestination (page) 92 HPDF_Destination_SetXYZ (dst, 0, HPDF_Page_GetHeight (page), 1) 93 HPDF_SetOpenAction(pdf, dst) 94 95 HPDF_Page_BeginText (page) 96 HPDF_Page_SetFontAndSize (page, font, 20) 97 HPDF_Page_MoveTextPos (page, 220, HPDF_Page_GetHeight (page) - 70) 98 HPDF_Page_ShowText (page, "ImageDemo") 99 HPDF_Page_EndText (page) 100 101 # load image file. 102 image = HPDF_LoadPngImageFromFile (pdf, "pngsuite/basn3p02.png") 103 104 # image1 is masked by image2. 105 image1 = HPDF_LoadPngImageFromFile (pdf, "pngsuite/basn3p02.png") 106 107 # image2 is a mask image. 108 image2 = HPDF_LoadPngImageFromFile (pdf, "pngsuite/basn0g01.png") 109 110 # image3 is a RGB-color image. we use this image for color-mask 111 # * demo. 112 image3 = HPDF_LoadPngImageFromFile (pdf, "pngsuite/maskimage.png") 113 114 iw = HPDF_Image_GetWidth (image) 115 ih = HPDF_Image_GetHeight (image) 116 117 HPDF_Page_SetLineWidth (page, 0.5) 118 119 x = 100 120 y = HPDF_Page_GetHeight (page) - 150 121 122 # Draw image to the canvas. (normal-mode with actual size.) 123 HPDF_Page_DrawImage (page, image, x, y, iw, ih) 124 125 show_description (page, x, y, "Actual Size") 126 127 x += 150 128 129 # Scalling image (X direction) 130 HPDF_Page_DrawImage (page, image, x, y, iw * 1.5, ih) 131 132 show_description (page, x, y, "Scalling image (X direction)") 133 134 x += 150 135 136 # Scalling image (Y direction). 137 HPDF_Page_DrawImage (page, image, x, y, iw, ih * 1.5) 138 show_description (page, x, y, "Scalling image (Y direction)") 139 140 x = 100 141 y -= 120 142 143 # Skewing image. 144 angle1 = 10 145 angle2 = 20 146 rad1 = angle1 / 180 * 3.141592 147 rad2 = angle2 / 180 * 3.141592 148 149 HPDF_Page_GSave (page) 150 151 HPDF_Page_Concat (page, iw, tan(rad1) * iw, tan(rad2) * ih, ih, x, y) 152 153 HPDF_Page_ExecuteXObject (page, image) 154 HPDF_Page_GRestore (page) 155 156 show_description (page, x, y, "Skewing image") 157 158 x += 150 159 160 # Rotating image 161 angle = 30; # rotation of 30 degrees. 162 rad = angle / 180 * 3.141592; # Calcurate the radian value. 163 164 HPDF_Page_GSave (page) 165 166 HPDF_Page_Concat (page, iw * cos(rad), 167 iw * sin(rad), 168 ih * -sin(rad), 169 ih * cos(rad), 170 x, y) 171 172 HPDF_Page_ExecuteXObject (page, image) 173 HPDF_Page_GRestore (page) 174 175 show_description (page, x, y, "Rotating image") 176 177 x += 150 178 179 # draw masked image. 180 181 # Set image2 to the mask image of image1 182 HPDF_Image_SetMaskImage (image1, image2) 183 184 HPDF_Page_SetRGBFill (page, 0, 0, 0) 185 HPDF_Page_BeginText (page) 186 HPDF_Page_MoveTextPos (page, x - 6, y + 14) 187 HPDF_Page_ShowText (page, "MASKMASK") 188 HPDF_Page_EndText (page) 189 190 HPDF_Page_DrawImage (page, image1, x - 3, y - 3, iw + 6, ih + 6) 191 192 show_description (page, x, y, "masked image") 193 194 x = 100 195 y -= 120 196 197 # color mask. 198 HPDF_Page_SetRGBFill (page, 0, 0, 0) 199 HPDF_Page_BeginText (page) 200 HPDF_Page_MoveTextPos (page, x - 6, y + 14) 201 HPDF_Page_ShowText (page, "MASKMASK") 202 HPDF_Page_EndText (page) 203 204 HPDF_Image_SetColorMask (image3, 0, 255, 0, 0, 0, 255) 205 HPDF_Page_DrawImage (page, image3, x, y, iw, ih) 206 207 show_description (page, x, y, "Color Mask") 208 209 # save the document to a file 210 HPDF_SaveToFile (pdf, fname) 211 212 # clean up 213 HPDF_Free (pdf) 214 215 return 0 216 217 218 219if HPDF_NOPNGLIB: 220 printf("WARNING: if you want to run this demo, \n" 221 "make libhpdf with HPDF_USE_PNGLIB option.\n") 222 sys.exit(1) 223else: 224 main()