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## 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
34
35@HPDF_Error_Handler(None, HPDF_UINT, HPDF_UINT, c_void_p)
36def error_handler (error_no, detail_no, user_data):
37    global pdf
38    printf ("ERROR: %s, detail_no=%u\n", error_detail[error_no],
39                detail_no)
40    HPDF_Free (pdf)
41    sys.exit(1)
42
43
44RAW_IMAGE_DATA=[
45    0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xff, 0xfc,
46    0xff, 0xff, 0xff, 0xf8, 0xff, 0xff, 0xff, 0xf0,
47    0xf3, 0xf3, 0xff, 0xe0, 0xf3, 0xf3, 0xff, 0xc0,
48    0xf3, 0xf3, 0xff, 0x80, 0xf3, 0x33, 0xff, 0x00,
49    0xf3, 0x33, 0xfe, 0x00, 0xf3, 0x33, 0xfc, 0x00,
50    0xf8, 0x07, 0xf8, 0x00, 0xf8, 0x07, 0xf0, 0x00,
51    0xfc, 0xcf, 0xe0, 0x00, 0xfc, 0xcf, 0xc0, 0x00,
52    0xff, 0xff, 0x80, 0x00, 0xff, 0xff, 0x00, 0x00,
53    0xff, 0xfe, 0x00, 0x00, 0xff, 0xfc, 0x00, 0x00,
54    0xff, 0xf8, 0x0f, 0xe0, 0xff, 0xf0, 0x0f, 0xe0,
55    0xff, 0xe0, 0x0c, 0x30, 0xff, 0xc0, 0x0c, 0x30,
56    0xff, 0x80, 0x0f, 0xe0, 0xff, 0x00, 0x0f, 0xe0,
57    0xfe, 0x00, 0x0c, 0x30, 0xfc, 0x00, 0x0c, 0x30,
58    0xf8, 0x00, 0x0f, 0xe0, 0xf0, 0x00, 0x0f, 0xe0,
59    0xe0, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00,
60    0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
61]
62
63def main ():
64    global  pdf
65
66    fname=os.path.realpath(sys.argv[0])
67    fname=fname[:fname.rfind('.')]+'.pdf'
68
69    pdf = HPDF_New (error_handler, NULL)
70    if (not pdf):
71        printf ("error: cannot create PdfDoc object\n")
72        return 1
73
74    HPDF_SetCompressionMode (pdf, HPDF_COMP_ALL)
75
76    # create default-font
77    font = HPDF_GetFont (pdf, "Helvetica", NULL)
78
79    # add a new page object.
80    page = HPDF_AddPage (pdf)
81
82    HPDF_Page_SetWidth (page, 172)
83    HPDF_Page_SetHeight (page, 80)
84
85    HPDF_Page_BeginText (page)
86    HPDF_Page_SetFontAndSize (page, font, 20)
87    HPDF_Page_MoveTextPos (page, 220, HPDF_Page_GetHeight (page) - 70)
88    HPDF_Page_ShowText (page, "RawImageDemo")
89    HPDF_Page_EndText (page)
90
91    # load RGB raw-image file.
92    image = HPDF_LoadRawImageFromFile (pdf, "rawimage/32_32_rgb.dat",
93            32, 32, HPDF_CS_DEVICE_RGB)
94
95    x = 20
96    y = 20
97
98    # Draw image to the canvas. (normal-mode with actual size.)
99    HPDF_Page_DrawImage (page, image, x, y, 32, 32)
100
101    # load GrayScale raw-image file.
102    image = HPDF_LoadRawImageFromFile (pdf, "rawimage/32_32_gray.dat",
103            32, 32, HPDF_CS_DEVICE_GRAY)
104
105    x = 70
106    y = 20
107
108    # Draw image to the canvas. (normal-mode with actual size.)
109    HPDF_Page_DrawImage (page, image, x, y, 32, 32)
110
111    # load GrayScale raw-image (1bit) file from memory.
112    image = HPDF_LoadRawImageFromMem (pdf, RAW_IMAGE_DATA, 32, 32,
113                HPDF_CS_DEVICE_GRAY, 1)
114
115    x = 120
116    y = 20
117
118    # Draw image to the canvas. (normal-mode with actual size.)
119    HPDF_Page_DrawImage (page, image, x, y, 32, 32)
120
121    # save the document to a file
122    HPDF_SaveToFile (pdf, fname)
123
124    # clean up
125    HPDF_Free (pdf)
126
127    return 0
128
129main()