efl

Форк
0
/
main.c 
298 строк · 6.4 Кб
1
#ifdef HAVE_CONFIG_H
2
# include <config.h>
3
#endif
4

5
#include <fcntl.h>
6

7
#include <libspectre/spectre.h>
8

9
#include <Eina.h>
10

11
#include "shmfile.h"
12
#include "timeout.h"
13

14
#define DATA32  unsigned int
15

16
#define PS_DBG
17

18
#ifdef PS_DBG
19
#define D(fmt, args...) fprintf(stderr, fmt, ## args)
20
#else
21
#define D(fmt, args...)
22
#endif
23

24

25
static SpectreDocument *psdoc;
26
static int page_count;
27

28
static SpectrePage *page;
29

30
static int width = 0;
31
static int height = 0;
32
static void *data = NULL;
33
static double dpi = -1.0;
34

35
#define DEF_DPI 72.0
36

37
static Eina_Bool
38
_spectre_init(const char *file, int page_nbr, int size_w, int size_h)
39
{
40
   double w, h;
41
   int ww, hh;
42
   SpectreOrientation rot;
43
   SpectreStatus status;
44

45
   if (!file || !*file)
46
     return EINA_FALSE;
47

48
   if (page_nbr < 0)
49
     return EINA_FALSE;
50

51
   if (!eina_init())
52
     return EINA_FALSE;
53

54
   psdoc = spectre_document_new();
55
   if (!psdoc)
56
     goto shutdown_eina;
57

58
   spectre_document_load(psdoc, file);
59
   status = spectre_document_status (psdoc);
60
   if (status != SPECTRE_STATUS_SUCCESS)
61
     {
62
        D("[ps] %s\n", spectre_status_to_string(status));
63
        goto free_psdoc;
64
   }
65

66
   page_count = spectre_document_get_n_pages(psdoc);
67
   status = spectre_document_status(psdoc);
68
   if (status != SPECTRE_STATUS_SUCCESS)
69
     {
70
        D("[eps] %s\n", spectre_status_to_string (status));
71
        goto free_psdoc;
72
   }
73

74
   if (page_nbr >= page_count)
75
     goto free_psdoc;
76

77
   /* load the page */
78

79
   page = spectre_document_get_page(psdoc, page_nbr);
80
   status = spectre_document_status(psdoc);
81
   if (status != SPECTRE_STATUS_SUCCESS)
82
     {
83
        D("[eps] %s\n", spectre_status_to_string (status));
84
        goto free_page;
85
   }
86

87
   spectre_page_get_size(page, &ww, &hh);
88
   w = ww;
89
   h = hh;
90
   rot = spectre_page_get_orientation(page);
91

92
   if ((rot == SPECTRE_ORIENTATION_LANDSCAPE) || (rot == SPECTRE_ORIENTATION_REVERSE_LANDSCAPE))
93
     {
94
        double t;
95
        // swap width & height
96
        t = w; w = h; h = t;
97
     }
98

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

103
        w2 = size_w;
104
        h2 = (size_w * h) / w;
105
        if (h2 > size_h)
106
          {
107
             h2 = size_h;
108
             w2 = (size_h * w) / h;
109
          }
110
        D("XXXXXXXXXXXXXXXXXXXXx %3.3fx%3.3f\n", w2, h2);
111
        if (w2 > h2) dpi = (w2 * DEF_DPI) / w;
112
        else dpi = (h2 * DEF_DPI) / h;
113
     }
114

115
   if (dpi > 0.0)
116
     {
117
        w = (w * dpi) / DEF_DPI;
118
        h = (h * dpi) / DEF_DPI;
119
     }
120
   width = w;
121
   height = h;
122

123
   return EINA_TRUE;
124

125
 free_page:
126
   spectre_page_free(page);
127
 free_psdoc:
128
   spectre_document_free(psdoc);
129
 shutdown_eina:
130
   eina_shutdown();
131

132
   return EINA_FALSE;
133
}
134

135
static void
136
_spectre_shutdown()
137
{
138
   spectre_page_free(page);
139
   spectre_document_free(psdoc);
140
   eina_shutdown();
141
}
142

143
static void
144
_pixcopy(DATA32 *dst, unsigned char *src, int size)
145
{
146
   DATA32 *d;
147
   unsigned char *s, *e;
148

149
   d = dst;
150
   s = src;
151
   e = s + size;
152
   while (s < e)
153
     {
154
        d[0] =
155
           0xff000000 |
156
           (s[2] << 16) |
157
           (s[1] << 8 ) |
158
           (s[0]      );
159
        d++;
160
        s += 4;
161
     }
162
}
163

164
static void
165
_spectre_load_image(int size_w EINA_UNUSED, int size_h EINA_UNUSED)
166
{
167
   SpectreRenderContext *rc;
168
   unsigned char        *psdata;
169
   int                   stride;
170
   unsigned char        *src;
171
   DATA32               *dst;
172
   int                   yy;
173
   SpectreStatus         status;
174

175
   rc = spectre_render_context_new();
176
   if (!rc)
177
     return;
178

179
   spectre_page_render(page, rc, &psdata, &stride);
180
   spectre_render_context_set_page_size (rc, width, height);
181
   status = spectre_page_status(page);
182
   if (status != SPECTRE_STATUS_SUCCESS)
183
     {
184
        D("[eps] %s\n", spectre_status_to_string (status));
185
        return;
186
     }
187

188
   shm_alloc(width * height * sizeof(DATA32));
189
   if (!shm_addr) return;
190
   data = shm_addr;
191

192
   if (stride == 4 * width)
193
     _pixcopy(data, psdata, height * stride);
194
   else
195
     {
196
        src = psdata;
197
        dst = (DATA32 *)data;
198
        for (yy = 0; yy < height; src += stride, dst += width, ++yy)
199
          _pixcopy (dst, src, width * 4);
200
     }
201

202
   spectre_render_context_free(rc);
203
}
204

205
int
206
main(int argc, char **argv)
207
{
208
   char *file;
209
   int i;
210
   int size_w = 0, size_h = 0;
211
   int head_only = 0;
212
   int page_nbr = 0;
213

214
   if (argc < 2) return -1;
215
   // file is ALWAYS first arg, other options come after
216
   file = argv[1];
217
   for (i = 2; i < argc; i++)
218
     {
219
        if      (!strcmp(argv[i], "-head"))
220
           // asked to only load header, not body/data
221
           head_only = 1;
222
        else if (!strcmp(argv[i], "-key"))
223
          {
224
             i++;
225
             page_nbr = atoi(argv[i]);
226
             i++;
227
          }
228
        else if (!strcmp(argv[i], "-opt-scale-down-by"))
229
          { // not used by ps loader
230
             i++;
231
             // int scale_down = atoi(argv[i]);
232
          }
233
        else if (!strcmp(argv[i], "-opt-dpi"))
234
          {
235
             i++;
236
             dpi = ((double)atoi(argv[i])) / 1000.0; // dpi is an int multiplied by 1000 (so 72dpi is 72000)
237
             i++;
238
          }
239
        else if (!strcmp(argv[i], "-opt-size"))
240
          { // not used by ps loader
241
             i++;
242
             size_w = atoi(argv[i]);
243
             i++;
244
             size_h = atoi(argv[i]);
245
          }
246
     }
247

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

252
   timeout_init(10);
253

254
   if (!_spectre_init(file, page_nbr, size_w, size_h))
255
     return -1;
256
   D("_spectre_init done\n");
257

258
   D("dpi2...: %f\n", dpi);
259
   if (!head_only)
260
     {
261
        _spectre_load_image(size_w, size_h);
262
     }
263

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

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

270
   if (!head_only)
271
     {
272
#ifdef _WIN32
273
        if (shm_fd) printf("shmfile %s\n", shmfile);
274
#else
275
        if (shm_fd >= 0) printf("shmfile %s\n", shmfile);
276
#endif
277
        else
278
          {
279
             // could also to "tmpfile %s\n" like shmfile but just
280
             // a mmaped tmp file on the system
281
             printf("data\n");
282
             if (fwrite(data, width * height * sizeof(DATA32), 1, stdout) != 1)
283
               {
284
                  // nothing much to do, the receiver will simply ignore the
285
                  // data as it's too short
286
                  D("fwrite failed (%llu bytes): %m\n",
287
                    (unsigned long long)(width * height * sizeof(DATA32)));
288
               }
289
          }
290
        shm_free();
291
     }
292
   else
293
     printf("done\n");
294

295
   _spectre_shutdown();
296
   fflush(stdout);
297
   return 0;
298
}
299

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.