summaryrefslogtreecommitdiff
path: root/src/generic/evas/ps/main.c
blob: 48e8a4b6f671fcea10e78ecc22fb899896c1d54f (plain)
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <sys/mman.h>
#include <fcntl.h>

#include <libspectre/spectre.h>

#include <Eina.h>

#include "shmfile.h"
#include "timeout.h"

#define DATA32  unsigned int

#define PS_DBG

#ifdef PS_DBG
#define D(fmt, args...) fprintf(stderr, fmt, ## args)
#else
#define D(fmt, args...)
#endif


static SpectreDocument *psdoc;
static int page_count;

static SpectrePage *page;

static int width = 0;
static int height = 0;
static void *data = NULL;
static double dpi = -1.0;

#define DEF_DPI 72.0

static Eina_Bool
_spectre_init(const char *file, int page_nbr, int size_w, int size_h)
{
   double w, h;
   int ww, hh;
   SpectreOrientation rot;
   SpectreStatus status;

   if (!file || !*file)
     return EINA_FALSE;

   if (page_nbr < 0)
     return EINA_FALSE;

   if (!eina_init())
     return EINA_FALSE;

   psdoc = spectre_document_new();
   if (!psdoc)
     goto shutdown_eina;

   spectre_document_load(psdoc, file);
   status = spectre_document_status (psdoc);
   if (status != SPECTRE_STATUS_SUCCESS)
     {
        D("[ps] %s\n", spectre_status_to_string(status));
        goto free_psdoc;
   }

   page_count = spectre_document_get_n_pages(psdoc);
   status = spectre_document_status(psdoc);
   if (status != SPECTRE_STATUS_SUCCESS)
     {
        D("[eps] %s\n", spectre_status_to_string (status));
        goto free_psdoc;
   }

   if (page_nbr >= page_count)
     goto free_psdoc;

   /* load the page */

   page = spectre_document_get_page(psdoc, page_nbr);
   status = spectre_document_status(psdoc);
   if (status != SPECTRE_STATUS_SUCCESS)
     {
        D("[eps] %s\n", spectre_status_to_string (status));
        goto free_page;
   }

   spectre_page_get_size(page, &ww, &hh);
   w = ww;
   h = hh;
   rot = spectre_page_get_orientation(page);

   if ((rot == SPECTRE_ORIENTATION_LANDSCAPE) || (rot == SPECTRE_ORIENTATION_REVERSE_LANDSCAPE))
     {
        double t;
        // swap width & height
        t = w; w = h; h = t;
     }

   if ((size_w > 0) || (size_h > 0))
     {
        double w2 = w, h2 = h;

        w2 = size_w;
        h2 = (size_w * h) / w;
        if (h2 > size_h)
          {
             h2 = size_h;
             w2 = (size_h * w) / h;
          }
        D("XXXXXXXXXXXXXXXXXXXXx %3.3fx%3.3f\n", w2, h2);
        if (w2 > h2) dpi = (w2 * DEF_DPI) / w;
        else dpi = (h2 * DEF_DPI) / h;
     }

   if (dpi > 0.0)
     {
        w = (w * dpi) / DEF_DPI;
        h = (h * dpi) / DEF_DPI;
     }
   width = w;
   height = h;

   return EINA_TRUE;

 free_page:
   spectre_page_free(page);
 free_psdoc:
   spectre_document_free(psdoc);
 shutdown_eina:
   eina_shutdown();

   return EINA_FALSE;
}

static void
_spectre_shutdown()
{
   spectre_page_free(page);
   spectre_document_free(psdoc);
   eina_shutdown();
}

static void
_pixcopy(DATA32 *dst, unsigned char *src, int size)
{
   DATA32 *d;
   unsigned char *s, *e;

   d = dst;
   s = src;
   e = s + size;
   while (s < e)
     {
        d[0] = 
           0xff000000 |
           (s[2] << 16) |
           (s[1] << 8 ) |
           (s[0]      );
        d++;
        s += 4;
     }
}

static void
_spectre_load_image(int size_w EINA_UNUSED, int size_h EINA_UNUSED)
{
   SpectreRenderContext *rc;
   unsigned char        *psdata;
   int                   stride;
   unsigned char        *src;
   DATA32               *dst;
   int                   yy;
   SpectreStatus         status;

   rc = spectre_render_context_new();
   if (!rc)
     return;

   spectre_page_render(page, rc, &psdata, &stride);
   spectre_render_context_set_page_size (rc, width, height);
   status = spectre_page_status(page);
   if (status != SPECTRE_STATUS_SUCCESS)
     {
        D("[eps] %s\n", spectre_status_to_string (status));
        return;
     }

   shm_alloc(width * height * sizeof(DATA32));
   if (!shm_addr) return;
   data = shm_addr;

   if (stride == 4 * width)
     _pixcopy(data, psdata, height * stride);
   else
     {
        src = psdata;
        dst = (DATA32 *)data;
        for (yy = 0; yy < height; src += stride, dst += width, ++yy)
          _pixcopy (dst, src, width * 4);
     }

   spectre_render_context_free(rc);
}

int
main(int argc, char **argv)
{
   char *file;
   int i;
   int size_w = 0, size_h = 0;
   int head_only = 0;
   int page_nbr = 0;

   if (argc < 2) return -1;
   // file is ALWAYS first arg, other options come after
   file = argv[1];
   for (i = 2; i < argc; i++)
     {
        if      (!strcmp(argv[i], "-head"))
           // asked to only load header, not body/data
           head_only = 1;
        else if (!strcmp(argv[i], "-key"))
          {
             i++;
             page_nbr = atoi(argv[i]);
             i++;
          }
        else if (!strcmp(argv[i], "-opt-scale-down-by"))
          { // not used by ps loader
             i++;
             // int scale_down = atoi(argv[i]);
          }
        else if (!strcmp(argv[i], "-opt-dpi"))
          {
             i++;
             dpi = ((double)atoi(argv[i])) / 1000.0; // dpi is an int multiplied by 1000 (so 72dpi is 72000)
             i++;
          }
        else if (!strcmp(argv[i], "-opt-size"))
          { // not used by ps loader
             i++;
             size_w = atoi(argv[i]);
             i++;
             size_h = atoi(argv[i]);
          }
     }

   D("_spectre_init_file\n");
   D("dpi....: %f\n", dpi);
   D("page...: %d\n", page_nbr);

   timeout_init(10);
   
   if (!_spectre_init(file, page_nbr, size_w, size_h))
     return -1;
   D("_spectre_init done\n");

   D("dpi2...: %f\n", dpi);
   if (!head_only)
     {
        _spectre_load_image(size_w, size_h);
     }

   D("size...: %ix%i\n", width, height);
   D("alpha..: 0\n");

   printf("size %i %i\n", width, height);
   printf("alpha 0\n");

   if (!head_only)
     {
        if (shm_fd >= 0) printf("shmfile %s\n", shmfile);
        else
          {
             // could also to "tmpfile %s\n" like shmfile but just
             // a mmaped tmp file on the system
             printf("data\n");
             fwrite(data, width * height * sizeof(DATA32), 1, stdout);
          }
        shm_free();
     }
   else
     printf("done\n");

   _spectre_shutdown();
   fflush(stdout);
   return 0;
}