summaryrefslogtreecommitdiff
path: root/src/generic/evas/ps/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/generic/evas/ps/main.c')
-rw-r--r--src/generic/evas/ps/main.c289
1 files changed, 289 insertions, 0 deletions
diff --git a/src/generic/evas/ps/main.c b/src/generic/evas/ps/main.c
new file mode 100644
index 0000000..48e8a4b
--- /dev/null
+++ b/src/generic/evas/ps/main.c
@@ -0,0 +1,289 @@
1#ifdef HAVE_CONFIG_H
2# include <config.h>
3#endif
4
5#include <sys/mman.h>
6#include <fcntl.h>
7
8#include <libspectre/spectre.h>
9
10#include <Eina.h>
11
12#include "shmfile.h"
13#include "timeout.h"
14
15#define DATA32 unsigned int
16
17#define PS_DBG
18
19#ifdef PS_DBG
20#define D(fmt, args...) fprintf(stderr, fmt, ## args)
21#else
22#define D(fmt, args...)
23#endif
24
25
26static SpectreDocument *psdoc;
27static int page_count;
28
29static SpectrePage *page;
30
31static int width = 0;
32static int height = 0;
33static void *data = NULL;
34static double dpi = -1.0;
35
36#define DEF_DPI 72.0
37
38static Eina_Bool
39_spectre_init(const char *file, int page_nbr, int size_w, int size_h)
40{
41 double w, h;
42 int ww, hh;
43 SpectreOrientation rot;
44 SpectreStatus status;
45
46 if (!file || !*file)
47 return EINA_FALSE;
48
49 if (page_nbr < 0)
50 return EINA_FALSE;
51
52 if (!eina_init())
53 return EINA_FALSE;
54
55 psdoc = spectre_document_new();
56 if (!psdoc)
57 goto shutdown_eina;
58
59 spectre_document_load(psdoc, file);
60 status = spectre_document_status (psdoc);
61 if (status != SPECTRE_STATUS_SUCCESS)
62 {
63 D("[ps] %s\n", spectre_status_to_string(status));
64 goto free_psdoc;
65 }
66
67 page_count = spectre_document_get_n_pages(psdoc);
68 status = spectre_document_status(psdoc);
69 if (status != SPECTRE_STATUS_SUCCESS)
70 {
71 D("[eps] %s\n", spectre_status_to_string (status));
72 goto free_psdoc;
73 }
74
75 if (page_nbr >= page_count)
76 goto free_psdoc;
77
78 /* load the page */
79
80 page = spectre_document_get_page(psdoc, page_nbr);
81 status = spectre_document_status(psdoc);
82 if (status != SPECTRE_STATUS_SUCCESS)
83 {
84 D("[eps] %s\n", spectre_status_to_string (status));
85 goto free_page;
86 }
87
88 spectre_page_get_size(page, &ww, &hh);
89 w = ww;
90 h = hh;
91 rot = spectre_page_get_orientation(page);
92
93 if ((rot == SPECTRE_ORIENTATION_LANDSCAPE) || (rot == SPECTRE_ORIENTATION_REVERSE_LANDSCAPE))
94 {
95 double t;
96 // swap width & height
97 t = w; w = h; h = t;
98 }
99
100 if ((size_w > 0) || (size_h > 0))
101 {
102 double w2 = w, h2 = h;
103
104 w2 = size_w;
105 h2 = (size_w * h) / w;
106 if (h2 > size_h)
107 {
108 h2 = size_h;
109 w2 = (size_h * w) / h;
110 }
111 D("XXXXXXXXXXXXXXXXXXXXx %3.3fx%3.3f\n", w2, h2);
112 if (w2 > h2) dpi = (w2 * DEF_DPI) / w;
113 else dpi = (h2 * DEF_DPI) / h;
114 }
115
116 if (dpi > 0.0)
117 {
118 w = (w * dpi) / DEF_DPI;
119 h = (h * dpi) / DEF_DPI;
120 }
121 width = w;
122 height = h;
123
124 return EINA_TRUE;
125
126 free_page:
127 spectre_page_free(page);
128 free_psdoc:
129 spectre_document_free(psdoc);
130 shutdown_eina:
131 eina_shutdown();
132
133 return EINA_FALSE;
134}
135
136static void
137_spectre_shutdown()
138{
139 spectre_page_free(page);
140 spectre_document_free(psdoc);
141 eina_shutdown();
142}
143
144static void
145_pixcopy(DATA32 *dst, unsigned char *src, int size)
146{
147 DATA32 *d;
148 unsigned char *s, *e;
149
150 d = dst;
151 s = src;
152 e = s + size;
153 while (s < e)
154 {
155 d[0] =
156 0xff000000 |
157 (s[2] << 16) |
158 (s[1] << 8 ) |
159 (s[0] );
160 d++;
161 s += 4;
162 }
163}
164
165static void
166_spectre_load_image(int size_w EINA_UNUSED, int size_h EINA_UNUSED)
167{
168 SpectreRenderContext *rc;
169 unsigned char *psdata;
170 int stride;
171 unsigned char *src;
172 DATA32 *dst;
173 int yy;
174 SpectreStatus status;
175
176 rc = spectre_render_context_new();
177 if (!rc)
178 return;
179
180 spectre_page_render(page, rc, &psdata, &stride);
181 spectre_render_context_set_page_size (rc, width, height);
182 status = spectre_page_status(page);
183 if (status != SPECTRE_STATUS_SUCCESS)
184 {
185 D("[eps] %s\n", spectre_status_to_string (status));
186 return;
187 }
188
189 shm_alloc(width * height * sizeof(DATA32));
190 if (!shm_addr) return;
191 data = shm_addr;
192
193 if (stride == 4 * width)
194 _pixcopy(data, psdata, height * stride);
195 else
196 {
197 src = psdata;
198 dst = (DATA32 *)data;
199 for (yy = 0; yy < height; src += stride, dst += width, ++yy)
200 _pixcopy (dst, src, width * 4);
201 }
202
203 spectre_render_context_free(rc);
204}
205
206int
207main(int argc, char **argv)
208{
209 char *file;
210 int i;
211 int size_w = 0, size_h = 0;
212 int head_only = 0;
213 int page_nbr = 0;
214
215 if (argc < 2) return -1;
216 // file is ALWAYS first arg, other options come after
217 file = argv[1];
218 for (i = 2; i < argc; i++)
219 {
220 if (!strcmp(argv[i], "-head"))
221 // asked to only load header, not body/data
222 head_only = 1;
223 else if (!strcmp(argv[i], "-key"))
224 {
225 i++;
226 page_nbr = atoi(argv[i]);
227 i++;
228 }
229 else if (!strcmp(argv[i], "-opt-scale-down-by"))
230 { // not used by ps loader
231 i++;
232 // int scale_down = atoi(argv[i]);
233 }
234 else if (!strcmp(argv[i], "-opt-dpi"))
235 {
236 i++;
237 dpi = ((double)atoi(argv[i])) / 1000.0; // dpi is an int multiplied by 1000 (so 72dpi is 72000)
238 i++;
239 }
240 else if (!strcmp(argv[i], "-opt-size"))
241 { // not used by ps loader
242 i++;
243 size_w = atoi(argv[i]);
244 i++;
245 size_h = atoi(argv[i]);
246 }
247 }
248
249 D("_spectre_init_file\n");
250 D("dpi....: %f\n", dpi);
251 D("page...: %d\n", page_nbr);
252
253 timeout_init(10);
254
255 if (!_spectre_init(file, page_nbr, size_w, size_h))
256 return -1;
257 D("_spectre_init done\n");
258
259 D("dpi2...: %f\n", dpi);
260 if (!head_only)
261 {
262 _spectre_load_image(size_w, size_h);
263 }
264
265 D("size...: %ix%i\n", width, height);
266 D("alpha..: 0\n");
267
268 printf("size %i %i\n", width, height);
269 printf("alpha 0\n");
270
271 if (!head_only)
272 {
273 if (shm_fd >= 0) printf("shmfile %s\n", shmfile);
274 else
275 {
276 // could also to "tmpfile %s\n" like shmfile but just
277 // a mmaped tmp file on the system
278 printf("data\n");
279 fwrite(data, width * height * sizeof(DATA32), 1, stdout);
280 }
281 shm_free();
282 }
283 else
284 printf("done\n");
285
286 _spectre_shutdown();
287 fflush(stdout);
288 return 0;
289}