summaryrefslogtreecommitdiff
path: root/src/generic/evas/raw/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/generic/evas/raw/main.c')
-rw-r--r--src/generic/evas/raw/main.c242
1 files changed, 242 insertions, 0 deletions
diff --git a/src/generic/evas/raw/main.c b/src/generic/evas/raw/main.c
new file mode 100644
index 0000000..ac9f810
--- /dev/null
+++ b/src/generic/evas/raw/main.c
@@ -0,0 +1,242 @@
1#ifdef HAVE_CONFIG_H
2#include <config.h>
3#endif
4#include <fcntl.h>
5#include <stdio.h>
6#include <sys/mman.h>
7#include <sys/stat.h>
8#include <sys/types.h>
9#include <unistd.h>
10#include <libraw.h>
11#include "shmfile.h"
12#include "timeout.h"
13
14#ifdef HAVE_NETINET_IN_H
15# include <netinet/in.h>
16#endif
17
18#ifdef HAVE_UNISTD_H
19# include <unistd.h>
20#endif
21
22#include <Eina.h>
23
24
25#define DATA32 unsigned int
26#define DATA8 unsigned char
27
28#define ARGB_JOIN(a,r,g,b) \
29 (((a) << 24) + ((r) << 16) + ((g) << 8) + (b))
30
31//#define RAW_DBG 1
32
33#ifdef RAW_DBG
34#define D(fmt, args...) fprintf(stderr, fmt, ## args)
35#else
36#define D(fmt, args...)
37#endif
38
39static int fd = -1;
40static size_t seg_size = 0;
41static unsigned char *seg = MAP_FAILED;
42static libraw_data_t *raw_data = NULL;
43static void *data = NULL;
44static int width = 0;
45static int height = 0;
46
47static int
48_raw_init(const char *file)
49{
50 struct stat ss;
51 fd = open(file, O_RDONLY);
52 if (fd < 0) return EINA_FALSE;
53
54 if (stat(file, &ss)) goto close_file;
55 seg_size = ss.st_size;
56 seg = mmap(NULL, seg_size, PROT_READ, MAP_SHARED, fd, 0);
57 if (seg == MAP_FAILED) goto close_file;
58
59 D("raw_init\n");
60 raw_data = libraw_init(0);
61 raw_data->params.half_size = 0;
62 raw_data->params.user_qual = 2;
63
64 D("raw_open_buffer\n");
65 if (libraw_open_buffer(raw_data, seg, seg_size) != LIBRAW_SUCCESS)
66 return EINA_FALSE;
67 return EINA_TRUE;
68
69close_file:
70 close(fd);
71 return EINA_FALSE;
72}
73
74static void
75_raw_shutdown()
76{
77 D("raw_shutdown\n");
78 if (raw_data)
79 libraw_close(raw_data);
80 if (seg != MAP_FAILED) munmap(seg, seg_size);
81 close(fd);
82}
83
84static int
85read_raw_header()
86{
87 int ret;
88
89 D("raw_open_buffer\n");
90 if ((ret = libraw_open_buffer(raw_data, seg, seg_size)) != LIBRAW_SUCCESS)
91 return 0;
92
93 D("raw_adjust_size\n");
94 if ((ret = libraw_adjust_sizes_info_only(raw_data)) != LIBRAW_SUCCESS)
95 {
96 if (LIBRAW_FATAL_ERROR(ret))
97 return 0;
98 }
99
100 if ((raw_data->sizes.width < 1) || (raw_data->sizes.height < 1))
101 return 0;
102
103 width = raw_data->sizes.iwidth;
104 height = raw_data->sizes.iheight;
105
106 return 1;
107
108}
109
110
111static int
112read_raw_data()
113{
114 int ret;
115 unsigned int count;
116 libraw_processed_image_t *image = NULL;
117 DATA8 *bufptr;
118 DATA32 *dataptr;
119
120
121 D("raw_open_unpack\n");
122 if ((ret = libraw_unpack(raw_data)) != LIBRAW_SUCCESS)
123 return 0;
124
125 D("raw_dcraw_process\n");
126 if ((ret = libraw_dcraw_process(raw_data)) != LIBRAW_SUCCESS)
127 {
128 if (LIBRAW_FATAL_ERROR(ret))
129 return 0;;
130 }
131
132 D("raw_make_mem_image\n");
133 image = libraw_dcraw_make_mem_image(raw_data, &ret);
134 if (image)
135 {
136 if ((image->width < 1) || (image->height < 1))
137 goto clean_image;
138 width = image->width;
139 height = image->height;
140 if (image->type != LIBRAW_IMAGE_BITMAP)
141 goto clean_image;
142 if (image->colors != 3)
143 goto clean_image;
144#define SWAP(a, b) { a ^= b; a = (b ^=a); }
145 if ((image->bits == 16) && (htons(0x55aa) != 0x55aa))
146 for (count = 0; count < image->data_size; count +=2)
147 SWAP(image->data[count], image->data[count + 1]);
148#undef SWAP
149 shm_alloc((unsigned int)(image->width * image->height) * (sizeof(DATA32)));
150 if (!shm_addr)
151 goto clean_image;
152 data = shm_addr;
153 memset(shm_addr, 0, (unsigned int)(image->width * image->height) * (sizeof(DATA32)));
154 dataptr = data;
155 bufptr = image->data;
156 for (count = (unsigned int)(image->width * image->height); count > 0; --count)
157 {
158 *dataptr = ARGB_JOIN(0xff, bufptr[0], bufptr[1], bufptr[2]);
159 dataptr++;
160 bufptr += 3;
161 }
162
163 free(image);
164 }
165 return 1;
166
167clean_image:
168 free(image);
169 return 0;
170}
171
172
173int main(int argc, char **argv)
174{
175 char *file;
176 int i;
177 int head_only = 0;
178
179 if (argc < 2) return -1;
180 file = argv[1];
181
182 for (i = 2; i < argc; ++i)
183 {
184 if (!strcmp(argv[i], "-head"))
185 head_only = 1;
186 else if (!strcmp(argv[i], "-key"))
187 { // not used by raw loader
188 i++;
189 // const char *key = argv[i];
190 }
191 else if (!strcmp(argv[i], "-opt-scale-down-by"))
192 { // not used by raw loader
193 i++;
194 // int scale_down = atoi(argv[i]);
195 }
196 else if (!strcmp(argv[i], "-opt-dpi"))
197 { // not used by raw loader
198 i++;
199 // double dpi = ((double)atoi(argv[i])) / 1000.0;
200 }
201 else if (!strcmp(argv[i], "-opt-size"))
202 { // not used by raw loader
203 i++;
204 // int size_w = atoi(argv[i]);
205 i++;
206 // int size_h = atoi(argv[i]);
207 }
208 }
209
210 timeout_init(4);
211
212 if (!_raw_init(file)) return -1;
213 if (head_only != 0)
214 {
215 if (read_raw_header())
216 {
217 printf("size %d %d\n", width, height);
218 printf("alpha 1\n");
219 }
220 printf("done\n");
221 }
222 else
223 {
224 if (read_raw_data())
225 {
226 printf("size %d %d\n", width, height);
227 printf("alpha 1\n");
228 if (shm_fd >= 0) printf("shmfile %s\n", shmfile);
229 else
230 {
231 printf("data\n");
232 fwrite(data, (unsigned int)(width * height) * sizeof(DATA32), 1, stdout);
233 }
234 shm_free();
235 }
236 }
237 _raw_shutdown();
238 fflush(stdout);
239 return 0;
240
241}
242