OSDN Git Service

binding with libharu.
[putex/putex.git] / src / texsourc / lib / libhpdf / if / python / demo / make_rawimage.py
1 ###
2 ## * << Haru Free PDF Library 2.0.0 >> -- make_rawimage.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
18 import os, sys
19
20 from ctypes import *
21 up=2
22 def 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
28 setlibpath(up)
29
30 from haru import *
31 from haru.c_func import *
32 from haru.hpdf_errorcode import *
33
34
35 @HPDF_Error_Handler(None, HPDF_UINT, HPDF_UINT, c_void_p)
36 def 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 def main ():
44     global  pdf
45
46     if (len(sys.argv) < 2):
47         printf ("usage: make_rawimage <in-file-name> <out-file-name>\n")
48         return 1
49
50     fname=os.path.realpath(sys.argv[0])
51     fname=fname[:fname.rfind('.')]+'.pdf'
52
53     pdf = HPDF_New (error_handler, NULL)
54     if (not pdf):
55         printf ("error: cannot create PdfDoc object\n")
56         return 1
57
58     # load image file.
59     image = HPDF_LoadPngImageFromFile (pdf, sys.argv[1])
60
61     iw = HPDF_Image_GetWidth (image)
62     ih = HPDF_Image_GetHeight (image)
63     bits_per_comp = HPDF_Image_GetBitsPerComponent (image)
64     cs = HPDF_Image_GetColorSpace (image)
65
66     printf ("width=%u\n", iw)
67     printf ("height=%u\n", ih)
68     printf ("bits_per_comp=%u\n", bits_per_comp)
69     printf ("color_space=%s\n", cs)
70
71     # save raw-data to file
72     stream = HPDF_FileWriter_New (pdf.mmgr, sys.argv[2])
73     if (not stream):
74         printf ("cannot open %s\n", sys.argv[2])
75     else:
76         HPDF_Stream_WriteToStream(image.stream, stream, 0, NULL)
77
78     HPDF_Stream_Free (stream)
79
80     # clean up
81     HPDF_Free (pdf)
82
83     return 0
84
85 main()