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 #include <stdlib.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include <math.h>
19 #include <setjmp.h>
20 #include "hpdf.h"
21
22 #ifndef HPDF_NOPNGLIB
23
24 jmp_buf env;
25
26 #ifdef HPDF_DLL
27 void __stdcall
28 #else
29 void
30 #endif
error_handler(HPDF_STATUS error_no,HPDF_STATUS detail_no,void * user_data)31 error_handler (HPDF_STATUS error_no,
32 HPDF_STATUS detail_no,
33 void *user_data)
34 {
35 printf ("ERROR: error_no=%04X, detail_no=%u\n", (HPDF_UINT)error_no,
36 (HPDF_UINT)detail_no);
37 longjmp(env, 1);
38 }
39
40 void
show_description(HPDF_Page page,float x,float y,const char * text)41 show_description (HPDF_Page page,
42 float x,
43 float y,
44 const char *text)
45 {
46 char buf[255];
47
48 HPDF_Page_MoveTo (page, x, y - 10);
49 HPDF_Page_LineTo (page, x, y + 10);
50 HPDF_Page_MoveTo (page, x - 10, y);
51 HPDF_Page_LineTo (page, x + 10, y);
52 HPDF_Page_Stroke (page);
53
54 HPDF_Page_SetFontAndSize (page, HPDF_Page_GetCurrentFont (page), 8);
55 HPDF_Page_SetRGBFill (page, 0, 0, 0);
56
57 HPDF_Page_BeginText (page);
58
59 #ifdef __WIN32__
60 _snprintf(buf, 255, "(x=%d,y=%d)", (int)x, (int)y);
61 #else
62 snprintf(buf, 255, "(x=%d,y=%d)", (int)x, (int)y);
63 #endif /* __WIN32__ */
64 HPDF_Page_MoveTextPos (page, x - HPDF_Page_TextWidth (page, buf) - 5,
65 y - 10);
66 HPDF_Page_ShowText (page, buf);
67 HPDF_Page_EndText (page);
68
69 HPDF_Page_BeginText (page);
70 HPDF_Page_MoveTextPos (page, x - 20, y - 25);
71 HPDF_Page_ShowText (page, text);
72 HPDF_Page_EndText (page);
73 }
74
75
main(int argc,char ** argv)76 int main (int argc, char **argv)
77 {
78 HPDF_Doc pdf;
79 HPDF_Font font;
80 HPDF_Page page;
81 char fname[256];
82 HPDF_Destination dst;
83 HPDF_Image image;
84 HPDF_Image image1;
85 HPDF_Image image2;
86 HPDF_Image image3;
87
88 double x;
89 double y;
90 double angle;
91 double angle1;
92 double angle2;
93 double rad;
94 double rad1;
95 double rad2;
96
97 double iw;
98 double ih;
99
100 strcpy (fname, argv[0]);
101 strcat (fname, ".pdf");
102
103 pdf = HPDF_New (error_handler, NULL);
104 if (!pdf) {
105 printf ("error: cannot create PdfDoc object\n");
106 return 1;
107 }
108
109 /* error-handler */
110 if (setjmp(env)) {
111 HPDF_Free (pdf);
112 return 1;
113 }
114
115 HPDF_SetCompressionMode (pdf, HPDF_COMP_ALL);
116
117 /* create default-font */
118 font = HPDF_GetFont (pdf, "Helvetica", NULL);
119
120 /* add a new page object. */
121 page = HPDF_AddPage (pdf);
122
123 HPDF_Page_SetWidth (page, 550);
124 HPDF_Page_SetHeight (page, 500);
125
126 dst = HPDF_Page_CreateDestination (page);
127 HPDF_Destination_SetXYZ (dst, 0, HPDF_Page_GetHeight (page), 1);
128 HPDF_SetOpenAction(pdf, dst);
129
130 HPDF_Page_BeginText (page);
131 HPDF_Page_SetFontAndSize (page, font, 20);
132 HPDF_Page_MoveTextPos (page, 220, HPDF_Page_GetHeight (page) - 70);
133 HPDF_Page_ShowText (page, "ImageDemo");
134 HPDF_Page_EndText (page);
135
136 /* load image file. */
137 #ifndef __WIN32__
138 image = HPDF_LoadPngImageFromFile (pdf, "pngsuite/basn3p02.png");
139 #else
140 image = HPDF_LoadPngImageFromFile (pdf, "pngsuite\\basn3p02.png");
141 #endif
142
143 /* image1 is masked by image2. */
144 #ifndef __WIN32__
145 image1 = HPDF_LoadPngImageFromFile (pdf, "pngsuite/basn3p02.png");
146 #else
147 image1 = HPDF_LoadPngImageFromFile (pdf, "pngsuite\\basn3p02.png");
148 #endif
149
150 /* image2 is a mask image. */
151 #ifndef __WIN32__
152 image2 = HPDF_LoadPngImageFromFile (pdf, "pngsuite/basn0g01.png");
153 #else
154 image2 = HPDF_LoadPngImageFromFile (pdf, "pngsuite\\basn0g01.png");
155 #endif
156
157 /* image3 is a RGB-color image. we use this image for color-mask
158 * demo.
159 */
160 #ifndef __WIN32__
161 image3 = HPDF_LoadPngImageFromFile (pdf, "pngsuite/maskimage.png");
162 #else
163 image3 = HPDF_LoadPngImageFromFile (pdf, "pngsuite\\maskimage.png");
164 #endif
165
166 iw = HPDF_Image_GetWidth (image);
167 ih = HPDF_Image_GetHeight (image);
168
169 HPDF_Page_SetLineWidth (page, 0.5);
170
171 x = 100;
172 y = HPDF_Page_GetHeight (page) - 150;
173
174 /* Draw image to the canvas. (normal-mode with actual size.)*/
175 HPDF_Page_DrawImage (page, image, x, y, iw, ih);
176
177 show_description (page, x, y, "Actual Size");
178
179 x += 150;
180
181 /* Scalling image (X direction) */
182 HPDF_Page_DrawImage (page, image, x, y, iw * 1.5, ih);
183
184 show_description (page, x, y, "Scalling image (X direction)");
185
186 x += 150;
187
188 /* Scalling image (Y direction). */
189 HPDF_Page_DrawImage (page, image, x, y, iw, ih * 1.5);
190 show_description (page, x, y, "Scalling image (Y direction)");
191
192 x = 100;
193 y -= 120;
194
195 /* Skewing image. */
196 angle1 = 10;
197 angle2 = 20;
198 rad1 = angle1 / 180 * 3.141592;
199 rad2 = angle2 / 180 * 3.141592;
200
201 HPDF_Page_GSave (page);
202
203 HPDF_Page_Concat (page, iw, tan(rad1) * iw, tan(rad2) * ih, ih, x, y);
204
205 HPDF_Page_ExecuteXObject (page, image);
206 HPDF_Page_GRestore (page);
207
208 show_description (page, x, y, "Skewing image");
209
210 x += 150;
211
212 /* Rotating image */
213 angle = 30; /* rotation of 30 degrees. */
214 rad = angle / 180 * 3.141592; /* Calcurate the radian value. */
215
216 HPDF_Page_GSave (page);
217
218 HPDF_Page_Concat (page, iw * cos(rad),
219 iw * sin(rad),
220 ih * -sin(rad),
221 ih * cos(rad),
222 x, y);
223
224 HPDF_Page_ExecuteXObject (page, image);
225 HPDF_Page_GRestore (page);
226
227 show_description (page, x, y, "Rotating image");
228
229 x += 150;
230
231 /* draw masked image. */
232
233 /* Set image2 to the mask image of image1 */
234 HPDF_Image_SetMaskImage (image1, image2);
235
236 HPDF_Page_SetRGBFill (page, 0, 0, 0);
237 HPDF_Page_BeginText (page);
238 HPDF_Page_MoveTextPos (page, x - 6, y + 14);
239 HPDF_Page_ShowText (page, "MASKMASK");
240 HPDF_Page_EndText (page);
241
242 HPDF_Page_DrawImage (page, image1, x - 3, y - 3, iw + 6, ih + 6);
243
244 show_description (page, x, y, "masked image");
245
246 x = 100;
247 y -= 120;
248
249 /* color mask. */
250 HPDF_Page_SetRGBFill (page, 0, 0, 0);
251 HPDF_Page_BeginText (page);
252 HPDF_Page_MoveTextPos (page, x - 6, y + 14);
253 HPDF_Page_ShowText (page, "MASKMASK");
254 HPDF_Page_EndText (page);
255
256 HPDF_Image_SetColorMask (image3, 0, 255, 0, 0, 0, 255);
257 HPDF_Page_DrawImage (page, image3, x, y, iw, ih);
258
259 show_description (page, x, y, "Color Mask");
260
261 /* save the document to a file */
262 HPDF_SaveToFile (pdf, fname);
263
264 /* clean up */
265 HPDF_Free (pdf);
266
267 return 0;
268 }
269
270 #else
271
main()272 int main()
273 {
274 printf("WARNING: if you want to run this demo, \n"
275 "make libhpdf with HPDF_USE_PNGLIB option.\n");
276 return 0;
277 }
278
279 #endif /* HPDF_NOPNGLIB */
280
281