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

Contents of /eyes/enlightenment_pattern.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (show annotations)
Sun Apr 29 08:50:13 2012 UTC (5 years, 11 months ago) by hib
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +23 -14 lines
File MIME type: text/plain
about to gut the _sh script again.
Currently it warps the psychadelics to fit the picture.  but it looked bad, childish.
So instead I straightened the square by warping backwards to make the base, and alpha clean up
The hologram is close enough.
So with this,  I am about to change the _hp.sh build
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
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 if ((y==0)||(x==0)||(y==31)||(x==31)) {
87 setDestination(w,0.,0.,8192.,0.,0.,8192.,8192.,8192.);
88 }
89 else {
90 x = (x-1)&1;
91 y = (y-1)&1;
92 double fromx,fromy;
93 if (!x) {fromx=0.;} else {fromx=4096.;}
94 if (!y) {fromy=0.;} else {fromy=4096.;}
95 setDestination(w,fromx,fromy,fromx+4096.,fromy,fromx,fromy+4096.,fromx+4096.,fromy+4096.);
96 }
97 }
98
99 }
100
101
102
103
104 inline int some_inside(double x,double y,struct quad *q) {
105 int i, j, c = 0;
106 double *vertx = &(q->x0); /* ??? hack */
107 double *verty = &(q->y0); /* ??? hack */
108 if ((x<q->xmin)||(x>=q->xmax)||(y<q->ymin)||(y>q->ymax)) return 0;
109
110 for (i = 0, j = 3; i < 4; j = i++) {
111 if ( ((verty[i]>y) != (verty[j]>y)) &&
112 (x < (vertx[j]-vertx[i]) * (y-verty[i])
113 * (q->one_over_vertys[i]) + vertx[i]) )
114 c = !c;
115 }
116 return c;
117
118 }
119
120
121
122 struct quad *find_quad(struct quad *quads,int number_quads,int ix,int iy) {
123 double x,y;
124 x=ix;y=iy;
125 int i;
126 for (i=0;i<number_quads;i++) {
127 if (some_inside(x,y,quads+i)) {
128 return quads+i;
129 }
130 }
131 return NULL;
132 }
133
134
135
136
137 int file_number_from_quad(struct quad *q) {
138 int x,y;
139 x=q->column;
140 y=q->row;
141 if (y==0) { /* top row */
142 return (x);
143 }
144 if (y==31) { /* bottom row*/
145 return (x+32);
146 }
147 if (x==0) { /* left row */
148 return ((y-1)+64);
149 }
150 if (x==31) { /* right row */
151 return ((y-1)+94);
152 }
153 x = (x-1)>>1;
154 y =(y-1)>>1;
155 return (y*15+x+124);
156 }
157
158
159
160
161 int main(int argc,char **argv)
162 {
163 char *square_file=argv[1];
164 int size=atoi(argv[2]);
165 int fromx=atoi(argv[3]);
166 int fromy=atoi(argv[4]);
167 int tox=atoi(argv[5]);
168 int toy=atoi(argv[6]);
169 #ifdef OUTPUT_PICTURE
170 char *oname=argv[7];
171 #endif
172
173 fprintf(stderr,"from %d,%d to %d,%d\n",fromx,fromy,tox,toy);
174 MagickWand *m_wand = NULL;
175 PixelWand *c_wand = NULL;
176 PixelWand *c2_wand = NULL;
177 MagickPixelPacket pixel;
178
179 char *filenames[2000];
180 int number_files=0;
181
182 MagickWand *input_wands[2000];
183
184
185 int number_quads=0;
186 struct quad quads[2000];
187
188 MagickWandGenesis();
189
190 // these did not work
191 /*if (!MagickSetResourceLimit(MemoryResource,2000000000)) {
192 fprintf(stderr,"couldnt set memory resource\n");
193 }
194 if (!MagickSetResourceLimit(AreaResource,2000000000)) {
195 fprintf(stderr,"couldnt set area resource\n");
196 }
197 if (!MagickSetResourceLimit(MapResource,2000000000)) {
198 fprintf(stderr,"couldnt set map resource\n");
199 }
200 */
201
202 ssize_t width,height;
203 char buf[20000];
204
205
206
207
208 // read the file names
209 while (gets(buf)) {
210 char bf[20000];
211 strcpy(bf,buf);
212 filenames[number_files++]=strdup(bf);
213 }
214
215 // eat the square file
216 {
217 FILE *xf;
218 xf =fopen(square_file,"r");
219 if (!xf) {
220 fprintf(stderr,"cannot open square file %s\n",square_file);
221 exit(-1);
222 }
223 while (fgets(buf,19999,xf)) {
224 struct quad *q = quads + (number_quads++);
225 q->column=fieldi(buf,0);
226 q->row=fieldi(buf,1);
227 q->x0=fieldn(buf,2);
228 q->y0=fieldn(buf,3);
229 q->x1=fieldn(buf,4);
230 q->y1=fieldn(buf,5);
231 q->x2=fieldn(buf,6);
232 q->y2=fieldn(buf,7);
233 q->x3=fieldn(buf,8);
234 q->y3=fieldn(buf,9);
235 setup_quad(q);
236 }
237 }
238
239
240 /* init input wands */
241 {
242 int i;
243 for (i=0;i<number_files;i++) {
244 input_wands[i]=NULL;
245 }
246 }
247
248
249 //read_image(iname,&rbuf,&width,&height,&points_to_look_at,&number_of_points);
250 //rbackbuf = malloc(width*height*2*sizeof(unsigned short));
251 //if (rbackbuf == NULL) {fprintf(stderr,"memory\n");exit(-1);}
252
253 width=tox-fromx;
254 height=toy-fromy;
255
256
257
258 #ifdef OUTPUT_PICTURE
259 PixelIterator *iterator;
260 PixelWand **pixels;
261 m_wand = NewMagickWand();
262 c_wand = NewPixelWand();
263 PixelSetColor(c_wand,"black"); /* for now */
264 MagickNewImage(m_wand,width,height,c_wand);
265 MagickSetImageChannelDepth(m_wand, AllChannels, 8);
266 MagickSetImageDepth(m_wand, 8);
267
268 iterator=NewPixelIterator(m_wand);
269 if ((iterator == (PixelIterator *) NULL) )
270 ThrowWandException(m_wand);
271 #endif
272
273 register ssize_t x;
274 ssize_t y;
275 PixelWand *p;
276 p = NewPixelWand();
277 struct quad *min_quad = quads;
278 struct quad *new_min_quad=quads+number_quads;
279
280 for (y=0; y < (ssize_t) height; y++)
281 {
282
283 #ifdef OUTPUT_PICTURE
284 pixels=PixelGetNextIteratorRow(iterator,&width);
285 if ((pixels == (PixelWand **) NULL))
286 break;
287 #endif
288
289 short rowww[128000];
290 int now_files[2000];
291 int i;
292 for (i=0;i<2000;i++) now_files[i]=0;
293
294 for (x=0; x < (ssize_t) width; x++)
295 {
296 struct quad *q;
297 q =find_quad(min_quad,number_quads-(quads-min_quad),x+fromx,y+fromy);
298 if (!q) {
299 q = find_quad(min_quad,number_quads-(quads-min_quad),x+fromx+1,y+fromy);
300 if (!q) {
301 q=find_quad(min_quad,number_quads-(quads-min_quad),x+fromx+1,y+fromy+1);
302 }
303 if (!q) {
304 fprintf(stderr,"cannot find quad x %d y %d\n",(int)(x+fromx),(int)(y+fromy));
305 exit(-1);
306 }
307 }
308
309 int f;
310 f = file_number_from_quad(q);
311
312 now_files[f]=1;
313 rowww[x] = q-quads;
314 if (new_min_quad > q) new_min_quad=q; /* speed it up by forgetting earlier quads */
315 }
316
317
318 /* close files no longer needed */
319 {
320 int i;
321 for (i=0;i<number_files;i++) {
322 if ((now_files[i]==0)&&(input_wands[i])) {
323 input_wands[i] = DestroyMagickWand(input_wands[i]);
324 fprintf(stderr,"closing %s\n",filenames[i]);
325 }
326 }
327 }
328
329
330 /* open files needed */
331 {
332 int i;
333 for (i=0;i<number_files;i++) {
334 if ((now_files[i])&&(!input_wands[i])) {
335 input_wands[i] = NewMagickWand();
336 fprintf(stderr,"reading %s\n",filenames[i]);
337 if (!MagickReadImage(input_wands[i],filenames[i])) {
338 fprintf(stderr,"error reading %s\n",filenames[i]);
339 exit(-1);
340 }
341 }
342 }
343 }
344
345 /* adjust the min_quads so we don't look at quads we no longer look at */
346 if (min_quad > new_min_quad) min_quad=new_min_quad;
347
348
349 for (x=0; x < (ssize_t) width; x++)
350 {
351 struct quad *q;
352 q = quads +rowww[x];
353 int f;
354 f = file_number_from_quad(q);
355 double xo,yo;
356 warp(&(q->w),(double)(x+fromx),(double)(y+fromy),&xo,&yo);
357 ssize_t localx,localy;
358 MagickPixelPacket p_color;
359 localx = xo;
360 localy = yo;
361 if (!MagickGetImagePixelColor(input_wands[f],localx,localy,p)) {
362 fprintf(stderr,"error getting color wand quad %d,%d warped %d,%d to %lf,%lf\n",q->column,q->row,
363 (int)x,(int)y,xo,yo,
364 filenames[f]);
365 exit(-1);
366 }
367 PixelGetMagickColor(p,&p_color);
368
369 if ((y%300==0)&&(x%height==y%width))
370 fprintf(stderr,"quad %d,%d warped %d,%d to %d,%d color %d,%d,%d file %s\n",
371 q->column,q->row,(int)x+fromx,(int)y+fromy,(int)localx,(int)localy,
372 (int)p_color.red,(int)p_color.green,(int)p_color.blue,filenames[f]);
373
374 #ifdef OUTPUT_PICTURE
375 PixelGetMagickColor(pixels[x],&pixel);
376 pixel.red=p_color.red;
377 pixel.green=p_color.green;
378 pixel.blue=p_color.blue;
379
380 PixelSetMagickColor(pixels[x],&pixel);
381 #else
382 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));
383 #endif
384 }
385
386 #ifdef OUTPUT_PICTURE
387 (void) PixelSyncIterator(iterator);
388 #endif
389 }
390 #ifdef OUTPUT_PICTURE
391 if (y < (ssize_t) MagickGetImageHeight(m_wand))
392 ThrowWandException(m_wand);
393 fprintf(stderr,"killing iterator...\n");
394 iterator=DestroyPixelIterator(iterator);
395 fprintf(stderr,"writing image...\n");
396 /*
397 Write the image then destroy it.
398 */
399 MagickBooleanType status;
400 status=MagickWriteImages(m_wand,oname,MagickTrue);
401 if (status == MagickFalse)
402 ThrowWandException(m_wand);
403 fprintf(stderr,"destroying wand...\n");
404 m_wand=DestroyMagickWand(m_wand);
405 fprintf(stderr,"terminating...\n");
406 #endif
407
408 MagickWandTerminus();
409 return(0);
410 }
411
412

  ViewVC Help
Powered by ViewVC 1.1.5