forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hpdf_doc_png.c
184 lines (140 loc) · 4.36 KB
/
hpdf_doc_png.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
* << Haru Free PDF Library >> -- hpdf_doc_png.c
*
* URL: http://libharu.org
*
* Copyright (c) 1999-2006 Takeshi Kanno <takeshi_kanno@est.hi-ho.ne.jp>
* Copyright (c) 2007-2009 Antony Dovgal <tony@daylessday.org>
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation.
* It is provided "as is" without express or implied warranty.
*
*/
#include "hpdf_conf.h"
#include "hpdf_utils.h"
#include "hpdf.h"
#include "hpdf_image.h"
static HPDF_Image
LoadPngImageFromStream (HPDF_Doc pdf,
HPDF_Stream imagedata,
HPDF_BOOL delayed_loading);
HPDF_EXPORT(HPDF_Image)
HPDF_LoadPngImageFromMem (HPDF_Doc pdf,
const HPDF_BYTE *buffer,
HPDF_UINT size)
{
HPDF_Stream imagedata;
HPDF_Image image;
HPDF_PTRACE ((" HPDF_LoadPngImageFromFile\n"));
if (!HPDF_HasDoc (pdf)) {
return NULL;
}
/* create file stream */
imagedata = HPDF_MemStream_New (pdf->mmgr, size);
if (!HPDF_Stream_Validate (imagedata)) {
HPDF_RaiseError (&pdf->error, HPDF_INVALID_STREAM, 0);
return NULL;
}
if (HPDF_Stream_Write (imagedata, buffer, size) != HPDF_OK) {
HPDF_Stream_Free (imagedata);
return NULL;
}
image = LoadPngImageFromStream (pdf, imagedata, HPDF_FALSE);
/* destroy file stream */
HPDF_Stream_Free (imagedata);
if (!image) {
HPDF_CheckError (&pdf->error);
}
return image;
}
HPDF_EXPORT(HPDF_Image)
HPDF_LoadPngImageFromFile (HPDF_Doc pdf,
const char *filename)
{
HPDF_Stream imagedata;
HPDF_Image image;
HPDF_PTRACE ((" HPDF_LoadPngImageFromFile\n"));
if (!HPDF_HasDoc (pdf))
return NULL;
/* create file stream */
imagedata = HPDF_FileReader_New (pdf->mmgr, filename);
if (HPDF_Stream_Validate (imagedata))
image = LoadPngImageFromStream (pdf, imagedata, HPDF_FALSE);
else
image = NULL;
/* destroy file stream */
if (imagedata)
HPDF_Stream_Free (imagedata);
if (!image)
HPDF_CheckError (&pdf->error);
return image;
}
/* delaied loading version of HPDF_LoadPngImageFromFile */
HPDF_EXPORT(HPDF_Image)
HPDF_LoadPngImageFromFile2 (HPDF_Doc pdf,
const char *filename)
{
HPDF_Stream imagedata;
HPDF_Image image;
HPDF_String fname;
HPDF_PTRACE ((" HPDF_LoadPngImageFromFile\n"));
if (!HPDF_HasDoc (pdf))
return NULL;
/* check whether file name is valid or not. */
imagedata = HPDF_FileReader_New (pdf->mmgr, filename);
if (HPDF_Stream_Validate (imagedata))
image = LoadPngImageFromStream (pdf, imagedata, HPDF_TRUE);
else
image = NULL;
/* destroy file stream */
if (imagedata)
HPDF_Stream_Free (imagedata);
if (!image) {
HPDF_CheckError (&pdf->error);
return NULL;
}
/* add file-name to image dictionary as a hidden entry.
* it is used when the image data is needed.
*/
fname = HPDF_String_New (pdf->mmgr, filename, NULL);
if (!fname) {
HPDF_CheckError (&pdf->error);
return NULL;
}
fname->header.obj_id |= HPDF_OTYPE_HIDDEN;
if ((HPDF_Dict_Add (image, "_FILE_NAME", fname)) != HPDF_OK) {
HPDF_CheckError (&pdf->error);
return NULL;
}
return image;
}
#ifndef LIBHPDF_HAVE_NOPNGLIB
static HPDF_Image
LoadPngImageFromStream (HPDF_Doc pdf,
HPDF_Stream imagedata,
HPDF_BOOL delayed_loading)
{
HPDF_Image image;
HPDF_PTRACE ((" HPDF_LoadPngImageFromStream\n"));
image = HPDF_Image_LoadPngImage (pdf->mmgr, imagedata, pdf->xref,
delayed_loading);
if (image && (pdf->compression_mode & HPDF_COMP_IMAGE))
image->filter = HPDF_STREAM_FILTER_FLATE_DECODE;
return image;
}
#else
static HPDF_Image
LoadPngImageFromStream (HPDF_Doc pdf,
HPDF_Stream imagedata,
HPDF_BOOL delayed_loading)
{
HPDF_SetError (&pdf->error, HPDF_UNSUPPORTED_FUNC, 0);
HPDF_UNUSED (delayed_loading);
HPDF_UNUSED (imagedata);
return NULL;
}
#endif /* LIBHPDF_HAVE_NOPNGLIB */