OSDN Git Service

binding with libharu.
[putex/putex.git] / src / texsourc / lib / libhpdf / demo / raw_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
65     HPDF_REAL x, y;
66
67     strcpy (fname, argv[0]);
68     strcat (fname, ".pdf");
69
70     pdf = HPDF_New (error_handler, NULL);
71     if (!pdf) {
72         printf ("error: cannot create PdfDoc object\n");
73         return 1;
74     }
75
76     /* error-handler */
77     if (setjmp(env)) {
78         HPDF_Free (pdf);
79         return 1;
80     }
81
82     HPDF_SetCompressionMode (pdf, HPDF_COMP_ALL);
83
84     /* create default-font */
85     font = HPDF_GetFont (pdf, "Helvetica", NULL);
86
87     /* add a new page object. */
88     page = HPDF_AddPage (pdf);
89
90     HPDF_Page_SetWidth (page, 172);
91     HPDF_Page_SetHeight (page, 80);
92
93     HPDF_Page_BeginText (page);
94     HPDF_Page_SetFontAndSize (page, font, 20);
95     HPDF_Page_MoveTextPos (page, 220, HPDF_Page_GetHeight (page) - 70);
96     HPDF_Page_ShowText (page, "RawImageDemo");
97     HPDF_Page_EndText (page);
98
99     /* load RGB raw-image file. */
100     #ifndef __WIN32__
101     image = HPDF_LoadRawImageFromFile (pdf, "rawimage/32_32_rgb.dat",
102             32, 32, HPDF_CS_DEVICE_RGB);
103     #else
104     image = HPDF_LoadRawImageFromFile (pdf, "rawimage\\32_32_rgb.dat",
105             32, 32, HPDF_CS_DEVICE_RGB);
106     #endif
107
108     x = 20;
109     y = 20;
110
111     /* Draw image to the canvas. (normal-mode with actual size.)*/
112     HPDF_Page_DrawImage (page, image, x, y, 32, 32);
113
114     /* load GrayScale raw-image file. */
115     #ifndef __WIN32__
116     image = HPDF_LoadRawImageFromFile (pdf, "rawimage/32_32_gray.dat",
117             32, 32, HPDF_CS_DEVICE_GRAY);
118     #else
119     image = HPDF_LoadRawImageFromFile (pdf, "rawimage\\32_32_gray.dat",
120             32, 32, HPDF_CS_DEVICE_GRAY);
121     #endif
122
123     x = 70;
124     y = 20;
125
126     /* Draw image to the canvas. (normal-mode with actual size.)*/
127     HPDF_Page_DrawImage (page, image, x, y, 32, 32);
128
129     /* load GrayScale raw-image (1bit) file from memory. */
130     image = HPDF_LoadRawImageFromMem (pdf, RAW_IMAGE_DATA, 32, 32,
131                 HPDF_CS_DEVICE_GRAY, 1);
132
133     x = 120;
134     y = 20;
135
136     /* Draw image to the canvas. (normal-mode with actual size.)*/
137     HPDF_Page_DrawImage (page, image, x, y, 32, 32);
138
139     /* save the document to a file */
140     HPDF_SaveToFile (pdf, fname);
141
142     /* clean up */
143     HPDF_Free (pdf);
144
145     return 0;
146 }
147
148