efl

Форк
0
253 строки · 5.7 Кб
1
#ifdef HAVE_CONFIG_H
2
#include <config.h>
3
#endif
4
#include <fcntl.h>
5
#include <stdio.h>
6
#include <sys/stat.h>
7
#include <sys/types.h>
8
#include <unistd.h>
9
#include <libraw.h>
10
#include "shmfile.h"
11
#include "timeout.h"
12

13
#ifdef HAVE_UNISTD_H
14
# include <unistd.h>
15
#endif
16

17
#ifdef _WIN32
18
# include <evil_private.h> /* mmap */
19
#else
20
# include <sys/mman.h>
21
#endif
22

23
#include <Eina.h>
24

25

26
#define DATA32 unsigned int
27
#define DATA8 unsigned char
28

29
#define ARGB_JOIN(a,r,g,b) \
30
   (((a) << 24) + ((r) << 16) + ((g) << 8) + (b))
31

32
//#define RAW_DBG 1
33

34
#ifdef RAW_DBG
35
#define D(fmt, args...) fprintf(stderr, fmt, ## args)
36
#else
37
#define D(fmt, args...)
38
#endif
39

40
static int fd = -1;
41
static size_t seg_size = 0;
42
static unsigned char *seg = MAP_FAILED;
43
static libraw_data_t *raw_data = NULL;
44
static void *data = NULL;
45
static int width = 0;
46
static int height = 0;
47

48
static int
49
_raw_init(const char *file)
50
{
51
   struct stat ss;
52
   fd = open(file, O_RDONLY);
53
   if (fd < 0) return EINA_FALSE;
54

55
   if (stat(file, &ss)) goto close_file;
56
   seg_size = ss.st_size;
57
   seg = mmap(NULL, seg_size, PROT_READ, MAP_SHARED, fd, 0);
58
   if (seg == MAP_FAILED) goto close_file;
59

60
   D("raw_init\n");
61
   raw_data = libraw_init(0);
62
   raw_data->params.half_size = 0;
63
   raw_data->params.user_qual = 2;
64

65
   D("raw_open_buffer\n");
66
   if (libraw_open_buffer(raw_data, seg, seg_size) != LIBRAW_SUCCESS)
67
     return EINA_FALSE;
68
   return EINA_TRUE;
69

70
close_file:
71
   close(fd);
72
   return EINA_FALSE;
73
}
74

75
static void
76
_raw_shutdown()
77
{
78
   D("raw_shutdown\n");
79
   if (raw_data)
80
     libraw_close(raw_data);
81
   if (seg != MAP_FAILED) munmap(seg, seg_size);
82
   close(fd);
83
}
84

85
static int
86
read_raw_header()
87
{
88
   int ret;
89

90
   D("raw_open_buffer\n");
91
   if ((ret = libraw_open_buffer(raw_data, seg, seg_size)) != LIBRAW_SUCCESS)
92
     return 0;
93

94
   D("raw_adjust_size\n");
95
   if ((ret = libraw_adjust_sizes_info_only(raw_data)) != LIBRAW_SUCCESS)
96
     {
97
        if (LIBRAW_FATAL_ERROR(ret))
98
          return 0;
99
     }
100

101
   if ((raw_data->sizes.width < 1) || (raw_data->sizes.height < 1))
102
     return 0;
103

104
   width = raw_data->sizes.iwidth;
105
   height = raw_data->sizes.iheight;
106

107
   return 1;
108

109
}
110

111

112
static int
113
read_raw_data()
114
{
115
   int ret;
116
   unsigned int count;
117
   libraw_processed_image_t *image = NULL;
118
   DATA8 *bufptr;
119
   DATA32 *dataptr;
120

121

122
   D("raw_open_unpack\n");
123
   if ((ret = libraw_unpack(raw_data)) != LIBRAW_SUCCESS)
124
     return 0;
125

126
   D("raw_dcraw_process\n");
127
   if ((ret = libraw_dcraw_process(raw_data)) != LIBRAW_SUCCESS)
128
     {
129
        if (LIBRAW_FATAL_ERROR(ret))
130
          return 0;;
131
     }
132

133
   D("raw_make_mem_image\n");
134
   image = libraw_dcraw_make_mem_image(raw_data, &ret);
135
   if (image)
136
     {
137
        if ((image->width < 1) || (image->height < 1))
138
          goto clean_image;
139
        width = image->width;
140
        height = image->height;
141
        if (image->type != LIBRAW_IMAGE_BITMAP)
142
          goto clean_image;
143
        if (image->colors != 3)
144
          goto clean_image;
145
#define SWAP(a, b) { a ^= b; a = (b ^=a); }
146
        if ((image->bits == 16) && (eina_htons(0x55aa) != 0x55aa))
147
          for (count = 0; count < image->data_size; count +=2)
148
            SWAP(image->data[count], image->data[count + 1]);
149
#undef SWAP
150
        shm_alloc((unsigned int)(image->width * image->height) * (sizeof(DATA32)));
151
        if (!shm_addr)
152
          goto clean_image;
153
        data = shm_addr;
154
        memset(shm_addr, 0, (unsigned int)(image->width * image->height) * (sizeof(DATA32)));
155
        dataptr = data;
156
        bufptr = image->data;
157
        for (count = (unsigned int)(image->width * image->height); count > 0; --count)
158
          {
159
             *dataptr = ARGB_JOIN(0xff, bufptr[0], bufptr[1], bufptr[2]);
160
             dataptr++;
161
             bufptr += 3;
162
          }
163

164
        free(image);
165
     }
166
   return 1;
167

168
clean_image:
169
   free(image);
170
   return 0;
171
}
172

173

174
int main(int argc, char **argv)
175
{
176
   char *file;
177
   int i;
178
   int head_only = 0;
179

180
   if (argc < 2) return -1;
181
   file = argv[1];
182

183
   for (i = 2; i < argc; ++i)
184
     {
185
        if (!strcmp(argv[i], "-head"))
186
          head_only = 1;
187
        else if (!strcmp(argv[i], "-key"))
188
          { // not used by raw loader
189
             i++;
190
             // const char *key = argv[i];
191
          }
192
        else if (!strcmp(argv[i], "-opt-scale-down-by"))
193
          { // not used by raw loader
194
             i++;
195
             // int scale_down = atoi(argv[i]);
196
          }
197
        else if (!strcmp(argv[i], "-opt-dpi"))
198
          { // not used by raw loader
199
             i++;
200
             // double dpi = ((double)atoi(argv[i])) / 1000.0;
201
          }
202
        else if (!strcmp(argv[i], "-opt-size"))
203
          { // not used by raw loader
204
             i++;
205
             // int size_w = atoi(argv[i]);
206
             i++;
207
             // int size_h = atoi(argv[i]);
208
          }
209
     }
210

211
   timeout_init(4);
212

213
   if (!_raw_init(file)) return -1;
214
   if (head_only != 0)
215
     {
216
        if (read_raw_header())
217
          {
218
             printf("size %d %d\n", width, height);
219
             printf("alpha 1\n");
220
          }
221
        printf("done\n");
222
     }
223
   else
224
     {
225
        if (read_raw_data())
226
          {
227
             printf("size %d %d\n", width, height);
228
             printf("alpha 1\n");
229
#ifdef _WIN32
230
             if (shm_fd) printf("shmfile %s\n", shmfile);
231
#else
232
             if (shm_fd >= 0) printf("shmfile %s\n", shmfile);
233
#endif
234
             else
235
               {
236
                  printf("data\n");
237
                  if (fwrite(data,
238
                             (unsigned int)(width * height) * sizeof(DATA32),
239
                             1, stdout) != 1)
240
                    {
241
                       shm_free();
242
                       _raw_shutdown();
243
                       return -1;
244
                    }
245
               }
246
             shm_free();
247
          }
248
     }
249
   _raw_shutdown();
250
   fflush(stdout);
251
   return 0;
252

253
}
254

255

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

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

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

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