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