/[opencvs]/eyes/enlightenment_montage_32.c
ViewVC logotype

Contents of /eyes/enlightenment_montage_32.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (show annotations)
Sat Nov 1 17:13:48 2014 UTC (3 years, 5 months ago) by hib
Branch: MAIN
CVS Tags: HEAD
File MIME type: text/plain
setting up for enlighenment subrev 3
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <math.h>
4 #include <string.h>
5 #include <sys/resource.h>
6 #include <wand/MagickWand.h>
7
8 /* This is a variant of enlightenbmentmontage - with jsut a 32x32 grid
9 USed for subrev 3 and above where we ahve 1024x1024 images
10 Hib 10/31/2014
11 */
12 #include "uthash.h"
13 #include "util.h"
14 #include "square_to_quad.h"
15
16 //define OUTPUT_PICTURE
17 /* ^^^ this makes it output an image . If not defined prints in outr text format */
18
19 #define ThrowWandException(wand) \
20 { \
21 char \
22 *description; \
23 \
24 ExceptionType \
25 severity; \
26 \
27 description=MagickGetException(wand,&severity); \
28 (void) fprintf(stderr,"%s %s %lu %s\n",GetMagickModule(),description); \
29 description=(char *) MagickRelinquishMemory(description); \
30 exit(-1); \
31 }
32
33
34 int size;
35
36
37
38
39 double fieldn(char *x,int fieldnum) {
40 while (fieldnum && *x) {
41 if ((*x == '|')||(*x == ' ')) fieldnum--;
42 x++;
43 }
44 return atof(x);
45 }
46
47
48 struct quad {
49 int row;
50 int column;
51 double x0,x1,x3,x2;
52 double y0,y1,y3,y2;
53 double xmin,ymin,xmax,ymax;
54 double one_over_vertys[4];
55 struct Warper w;
56 };
57
58 #define min(a,b) ((a)<(b)?(a):(b))
59 #define max(a,b) ((a)>(b)?(a):(b))
60
61
62
63 void setup_quad(struct quad *sp) {
64 sp->xmax = max(max(sp->x1,sp->x2),max(sp->x3,sp->x0));
65 sp->xmin = min(min(sp->x1,sp->x2),min(sp->x3,sp->x0));
66 sp->ymax = max(max(sp->y1,sp->y2),max(sp->y3,sp->y0));
67 sp->ymin = min(min(sp->y1,sp->y2),min(sp->y3,sp->y0));
68 double ydelta;
69 ydelta = sp->y2-sp->y0;
70 if (ydelta!= 0.0) ydelta = 1.0 / ydelta;
71 sp->one_over_vertys[0] = ydelta;
72 ydelta = sp->y0-sp->y1;
73 if (ydelta!= 0.0) ydelta = 1.0 / ydelta;
74 sp->one_over_vertys[1] = ydelta;
75 ydelta = sp->y1-sp->y3;
76 if (ydelta!= 0.0) ydelta = 1.0 / ydelta;
77 sp->one_over_vertys[2] = ydelta;
78 ydelta = sp->y3-sp->y2;
79 if (ydelta!= 0.0) ydelta = 1.0 / ydelta;
80 sp->one_over_vertys[3] = ydelta;
81 struct Warper *w;
82 w=&(sp->w);
83 setIdentity(w);
84 setSource(w,sp->x0,sp->y0,sp->x1,sp->y1,sp->x2,sp->y2,sp->x3,sp->y3);
85 /* the destination varies depending on if it is the edge corner or the main section*/
86 {
87 int x=sp->column;
88 int y=sp->row;
89 double smallsize;
90 smallsize=size/32.;
91 setDestination(w,0.,0.,smallsize,0.,0.,smallsize,smallsize,smallsize);
92 }
93
94 }
95
96
97
98
99 inline int some_inside(double x,double y,struct quad *q) {
100 int i, j, c = 0;
101 double *vertx = &(q->x0); /* ??? hack */
102 double *verty = &(q->y0); /* ??? hack */
103 if ((x<q->xmin)||(x>=q->xmax)||(y<q->ymin)||(y>q->ymax)) return 0;
104
105 for (i = 0, j = 3; i < 4; j = i++) {
106 if ( ((verty[i]>y) != (verty[j]>y)) &&
107 (x < (vertx[j]-vertx[i]) * (y-verty[i])
108 * (q->one_over_vertys[i]) + vertx[i]) )
109 c = !c;
110 }
111 return c;
112
113 }
114
115
116
117 struct quad *find_quad(struct quad *quads,int number_quads,int ix,int iy) {
118 int i= ix/(size/32) + 32*(iy/(size/32));
119 return quads+i;
120 }
121
122
123
124
125 int file_number_from_quad(struct quad *q) {
126 int x,y;
127 x=q->column;
128 y=q->row;
129 return (y*32+x);
130 }
131
132
133
134
135 int main(int argc,char **argv)
136 {
137 size=atoi(argv[1]);
138 int fromx=atoi(argv[2]);
139 int fromy=atoi(argv[3]);
140 int tox=atoi(argv[4]);
141 int toy=atoi(argv[5]);
142 #ifdef OUTPUT_PICTURE
143 char *oname=argv[6];
144 #endif
145
146 MagickWand *m_wand = NULL;
147 PixelWand *c_wand = NULL;
148 PixelWand *c2_wand = NULL;
149 MagickPixelPacket pixel;
150
151 char *filenames[2000];
152 int number_files=0;
153
154 MagickWand *input_wands[2000];
155
156
157 int number_quads=0;
158 struct quad quads[2000];
159
160 MagickWandGenesis();
161
162 // these did not work
163 /*if (!MagickSetResourceLimit(MemoryResource,2000000000)) {
164 fprintf(stderr,"couldnt set memory resource\n");
165 }
166 if (!MagickSetResourceLimit(AreaResource,2000000000)) {
167 fprintf(stderr,"couldnt set area resource\n");
168 }
169 if (!MagickSetResourceLimit(MapResource,2000000000)) {
170 fprintf(stderr,"couldnt set map resource\n");
171 }
172 */
173
174 ssize_t width,height;
175 char buf[20000];
176
177
178
179
180 // read the file names
181 while (gets(buf)) {
182 char bf[20000];
183 strcpy(bf,buf);
184 filenames[number_files++]=strdup(bf);
185 }
186 {
187 int row,column;
188 for (row=0;row<32;row++) {
189 for (column=0;column<32;column++) {
190 struct quad *q=quads + (number_quads++);
191 q->column=column;
192 q->row=row;
193 q->x0 = column*(size/32);
194 q->y0 = row*(size/32);
195 q->x1 = (column+1)*(size/32);
196 q->y1 = row*(size/32);
197 q->x2 = column*(size/32);
198 q->y2 = (row+1)*(size/32);
199 q->x3 = (column+1)*(size/32);
200 q->y3 = (row+1)*(size/32);
201 setup_quad(q);
202 }
203 }
204 }
205
206
207 /* init input wands */
208 {
209 int i;
210 for (i=0;i<number_files;i++) {
211 input_wands[i]=NULL;
212 }
213 }
214
215
216 //read_image(iname,&rbuf,&width,&height,&points_to_look_at,&number_of_points);
217 //rbackbuf = malloc(width*height*2*sizeof(unsigned short));
218 //if (rbackbuf == NULL) {fprintf(stderr,"memory\n");exit(-1);}
219
220 width=tox-fromx;
221 height=toy-fromy;
222
223
224
225 #ifdef OUTPUT_PICTURE
226 PixelIterator *iterator;
227 PixelWand **pixels;
228 m_wand = NewMagickWand();
229 c_wand = NewPixelWand();
230 PixelSetColor(c_wand,"black"); /* for now */
231 MagickNewImage(m_wand,width,height,c_wand);
232 MagickSetImageChannelDepth(m_wand, AllChannels, 8);
233 MagickSetImageDepth(m_wand, 8);
234
235 iterator=NewPixelIterator(m_wand);
236 if ((iterator == (PixelIterator *) NULL) )
237 ThrowWandException(m_wand);
238 #endif
239
240 register ssize_t x;
241 ssize_t y;
242 PixelWand *p;
243 p = NewPixelWand();
244 struct quad *min_quad = quads;
245 struct quad *new_min_quad=quads+number_quads;
246
247 for (y=0; y < (ssize_t) height; y++)
248 {
249
250 #ifdef OUTPUT_PICTURE
251 pixels=PixelGetNextIteratorRow(iterator,&width);
252 if ((pixels == (PixelWand **) NULL))
253 break;
254 #endif
255
256 short rowww[128000];
257 int now_files[2000];
258 int i;
259 for (i=0;i<2000;i++) now_files[i]=0;
260
261 for (x=0; x < (ssize_t) width; x++)
262 {
263 struct quad *q;
264 q =find_quad(min_quad,number_quads-(quads-min_quad),x+fromx,y+fromy);
265 if (!q) {
266 q = find_quad(min_quad,number_quads-(quads-min_quad),x+fromx+1,y+fromy);
267 if (!q) {
268 q=find_quad(min_quad,number_quads-(quads-min_quad),x+fromx+1,y+fromy+1);
269 }
270 if (!q) {
271 fprintf(stderr,"cannot find quad x %d y %d\n",(int)(x+fromx),(int)(y+fromy));
272 exit(-1);
273 }
274 }
275
276 int f;
277 f = file_number_from_quad(q);
278
279 now_files[f]=1;
280 rowww[x] = q-quads;
281 if (new_min_quad > q) new_min_quad=q; /* speed it up by forgetting earlier quads */
282 }
283
284
285 /* close files no longer needed */
286 {
287 int i;
288 for (i=0;i<number_files;i++) {
289 if ((now_files[i]==0)&&(input_wands[i])) {
290 input_wands[i] = DestroyMagickWand(input_wands[i]);
291 }
292 }
293 }
294
295
296 /* open files needed */
297 {
298 int i;
299 for (i=0;i<number_files;i++) {
300 if ((now_files[i])&&(!input_wands[i])) {
301 input_wands[i] = NewMagickWand();
302 if (!MagickReadImage(input_wands[i],filenames[i])) {
303 fprintf(stderr,"error reading %s\n",filenames[i]);
304 exit(-1);
305 }
306 }
307 }
308 }
309
310 /* adjust the min_quads so we don't look at quads we no longer look at */
311 if (min_quad > new_min_quad) min_quad=new_min_quad;
312
313
314 for (x=0; x < (ssize_t) width; x++)
315 {
316 struct quad *q;
317 q = quads +rowww[x];
318 int f;
319 f = file_number_from_quad(q);
320 double xo,yo;
321 warp(&(q->w),(double)(x+fromx),(double)(y+fromy),&xo,&yo);
322 ssize_t localx,localy;
323 MagickPixelPacket p_color;
324 localx = xo;
325 localy = yo;
326 if (!MagickGetImagePixelColor(input_wands[f],localx,localy,p)) {
327 fprintf(stderr,"error getting color wand quad %d,%d warped %d,%d to %lf,%lf\n",q->column,q->row,
328 (int)x,(int)y,xo,yo,
329 filenames[f]);
330 exit(-1);
331 }
332 PixelGetMagickColor(p,&p_color);
333
334 if ((y%300==0)&&(x==0))
335 fprintf(stderr,".");
336
337 #ifdef OUTPUT_PICTURE
338 PixelGetMagickColor(pixels[x],&pixel);
339 pixel.red=p_color.red;
340 pixel.green=p_color.green;
341 pixel.blue=p_color.blue;
342
343 PixelSetMagickColor(pixels[x],&pixel);
344 #else
345 printf("%d|%d|%d|%d|%d\n",(int)x+fromx,(int)y+fromy,((int)(p_color.red))>>8,((int)(p_color.green))>>8,((int)(p_color.blue)>>8));
346 #endif
347 }
348
349 #ifdef OUTPUT_PICTURE
350 (void) PixelSyncIterator(iterator);
351 #endif
352 }
353 fprintf(stderr,"\n");
354 #ifdef OUTPUT_PICTURE
355 if (y < (ssize_t) MagickGetImageHeight(m_wand))
356 ThrowWandException(m_wand);
357 iterator=DestroyPixelIterator(iterator);
358 /*
359 Write the image then destroy it.
360 */
361 MagickBooleanType status;
362 status=MagickWriteImages(m_wand,oname,MagickTrue);
363 if (status == MagickFalse)
364 ThrowWandException(m_wand);
365
366 m_wand=DestroyMagickWand(m_wand);
367
368 #endif
369
370 MagickWandTerminus();
371 return(0);
372 }
373
374

  ViewVC Help
Powered by ViewVC 1.1.5