OSDN Git Service

binding with libharu.
[putex/putex.git] / src / texsourc / lib / libhpdf / demo / png_image_demo.c
1 /*
2  * << Haru Free PDF Library 2.0.0 >> -- raw_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 jmp_buf env;
23
24 #ifdef HPDF_DLL
25 void  __stdcall
26 #else
27 void
28 #endif
29 error_handler  (HPDF_STATUS   error_no,
30                 HPDF_STATUS   detail_no,
31                 void         *user_data)
32 {
33     printf ("ERROR: error_no=%04X, detail_no=%u\n", (HPDF_UINT)error_no,
34                 (HPDF_UINT)detail_no);
35     longjmp(env, 1);
36 }
37
38 const HPDF_BYTE RAW_IMAGE_DATA[128] = {
39     0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xff, 0xfc,
40     0xff, 0xff, 0xff, 0xf8, 0xff, 0xff, 0xff, 0xf0,
41     0xf3, 0xf3, 0xff, 0xe0, 0xf3, 0xf3, 0xff, 0xc0,
42     0xf3, 0xf3, 0xff, 0x80, 0xf3, 0x33, 0xff, 0x00,
43     0xf3, 0x33, 0xfe, 0x00, 0xf3, 0x33, 0xfc, 0x00,
44     0xf8, 0x07, 0xf8, 0x00, 0xf8, 0x07, 0xf0, 0x00,
45     0xfc, 0xcf, 0xe0, 0x00, 0xfc, 0xcf, 0xc0, 0x00,
46     0xff, 0xff, 0x80, 0x00, 0xff, 0xff, 0x00, 0x00,
47     0xff, 0xfe, 0x00, 0x00, 0xff, 0xfc, 0x00, 0x00,
48     0xff, 0xf8, 0x0f, 0xe0, 0xff, 0xf0, 0x0f, 0xe0,
49     0xff, 0xe0, 0x0c, 0x30, 0xff, 0xc0, 0x0c, 0x30,
50     0xff, 0x80, 0x0f, 0xe0, 0xff, 0x00, 0x0f, 0xe0,
51     0xfe, 0x00, 0x0c, 0x30, 0xfc, 0x00, 0x0c, 0x30,
52     0xf8, 0x00, 0x0f, 0xe0, 0xf0, 0x00, 0x0f, 0xe0,
53     0xe0, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00,
54     0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
55 };
56
57 int main (int argc, char **argv)
58 {
59     HPDF_Doc  pdf;
60     HPDF_Font font;
61     HPDF_Page page;
62     char fname[256];
63     HPDF_Image image;
64         char *png_image_data;
65         FILE *fp;
66
67     HPDF_REAL x, y;
68
69     strcpy (fname, argv[0]);
70     strcat (fname, ".pdf");
71
72     pdf = HPDF_New (error_handler, NULL);
73     if (!pdf) {
74         printf ("error: cannot create PdfDoc object\n");
75         return 1;
76     }
77
78     /* error-handler */
79     if (setjmp(env)) {
80         HPDF_Free (pdf);
81         return 1;
82     }
83
84     HPDF_SetCompressionMode (pdf, HPDF_COMP_ALL);
85
86     /* create default-font */
87     font = HPDF_GetFont (pdf, "Helvetica", NULL);
88
89     /* add a new page object. */
90     page = HPDF_AddPage (pdf);
91
92     HPDF_Page_BeginText (page);
93     HPDF_Page_SetFontAndSize (page, font, 20);
94     HPDF_Page_MoveTextPos (page, 220, HPDF_Page_GetHeight (page) - 70);
95     HPDF_Page_ShowText (page, "RawImageDemo");
96     HPDF_Page_EndText (page);
97
98         int n;
99
100         fp = fopen("2.png", "r");
101         png_image_data = malloc(1024*1024);
102         n = fread(png_image_data, 1, 1024*1024, fp);
103         fclose(fp);
104
105     /* load GrayScale raw-image (1bit) file from memory. */
106     image = HPDF_LoadPngImageFromMem (pdf, png_image_data, n);
107
108     /* Draw image to the canvas. (normal-mode with actual size.)*/
109     HPDF_Page_DrawImage (page, image, 20, 20, 1024, 768);
110
111         free(png_image_data);
112
113     /* save the document to a file */
114     HPDF_SaveToFile (pdf, fname);
115
116     /* clean up */
117     HPDF_Free (pdf);
118
119     return 0;
120 }
121
122