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

Contents of /eyes/enlightenment_montage.c

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5