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

Contents of /eyes/enlightenment_straighten.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (show annotations)
Sun Apr 29 08:50:13 2012 UTC (6 years, 4 months ago) by hib
Branch: MAIN
CVS Tags: HEAD
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 int size; /* made global so we can divide by 32 to get the number of pixels */
14
15 /* straighten straigens out the girl, instead of goofing up the pattern
16 It turns out that quads don't have the magic that a perfect square does.
17 Symmetry is important for psychadelics.
18 So I am going to undo the warpage on the girl
19 */
20
21 //define OUTPUT_PICTURE
22 /* ^^^ this makes it output an image . If not defined prints in outr text format */
23
24 #define ThrowWandException(wand) \
25 { \
26 char \
27 *description; \
28 \
29 ExceptionType \
30 severity; \
31 \
32 description=MagickGetException(wand,&severity); \
33 (void) fprintf(stderr,"%s %s %lu %s\n",GetMagickModule(),description); \
34 description=(char *) MagickRelinquishMemory(description); \
35 exit(-1); \
36 }
37
38
39
40
41
42
43
44 double fieldn(char *x,int fieldnum) {
45 while (fieldnum && *x) {
46 if ((*x == '|')||(*x == ' ')) fieldnum--;
47 x++;
48 }
49 return atof(x);
50 }
51
52
53 struct quad {
54 int row;
55 int column;
56 double x0,x1,x3,x2;
57 double y0,y1,y3,y2;
58 double xmin,ymin,xmax,ymax;
59 double one_over_vertys[4];
60 struct Warper w;
61 };
62
63 #define min(a,b) ((a)<(b)?(a):(b))
64 #define max(a,b) ((a)>(b)?(a):(b))
65
66
67
68 void setup_quad(struct quad *sp) {
69 /* before we set up quads for these x,y values. But not any more*/
70 double x1,x2,x3,x0;
71 double y1,y2,y3,y0;
72 x0 = sp->x0;
73 x1 = sp->x1;
74 x2 = sp->x2;
75 x3 = sp->x3;
76 y0 = sp->y0;
77 y1 = sp->y1;
78 y2 = sp->y2;
79 y3 = sp->y3;
80 double pixels;
81 pixels = ((double)(size))/32.; // should be 875
82 /* set the values to what they really should be */
83 sp->x0 = sp->column * pixels;
84 sp->x1 = (sp->column+1) * pixels;
85 sp->x2 = sp->column * pixels;
86 sp->x3 = (sp->column+1) * pixels;
87 sp->y0 = sp->row * pixels;
88 sp->y1 = sp->row * pixels;
89 sp->y2 = (sp->row+1) * pixels;
90 sp->y3 = (sp->row+1) * pixels;
91
92 sp->xmax = max(max(sp->x1,sp->x2),max(sp->x3,sp->x0));
93 sp->xmin = min(min(sp->x1,sp->x2),min(sp->x3,sp->x0));
94 sp->ymax = max(max(sp->y1,sp->y2),max(sp->y3,sp->y0));
95 sp->ymin = min(min(sp->y1,sp->y2),min(sp->y3,sp->y0));
96 double ydelta;
97 ydelta = sp->y2-sp->y0;
98 if (ydelta!= 0.0) ydelta = 1.0 / ydelta;
99 sp->one_over_vertys[0] = ydelta;
100 ydelta = sp->y0-sp->y1;
101 if (ydelta!= 0.0) ydelta = 1.0 / ydelta;
102 sp->one_over_vertys[1] = ydelta;
103 ydelta = sp->y1-sp->y3;
104 if (ydelta!= 0.0) ydelta = 1.0 / ydelta;
105 sp->one_over_vertys[2] = ydelta;
106 ydelta = sp->y3-sp->y2;
107 if (ydelta!= 0.0) ydelta = 1.0 / ydelta;
108 sp->one_over_vertys[3] = ydelta;
109 struct Warper *w;
110 w=&(sp->w);
111 setIdentity(w);
112 setSource(w,sp->x0,sp->y0,sp->x1,sp->y1,sp->x2,sp->y2,sp->x3,sp->y3);
113 /* The source is the square and the destination is the quad - reverse of before */
114 setDestination(w,x0,y0,x1,y1,x2,y2,x3,y3);
115 }
116
117
118
119
120 inline int some_inside(double x,double y,struct quad *q) {
121 int i, j, c = 0;
122 double *vertx = &(q->x0); /* ??? hack */
123 double *verty = &(q->y0); /* ??? hack */
124 if ((x<q->xmin)||(x>=q->xmax)||(y<q->ymin)||(y>q->ymax)) return 0;
125
126 for (i = 0, j = 3; i < 4; j = i++) {
127 if ( ((verty[i]>y) != (verty[j]>y)) &&
128 (x < (vertx[j]-vertx[i]) * (y-verty[i])
129 * (q->one_over_vertys[i]) + vertx[i]) )
130 c = !c;
131 }
132 return c;
133
134 }
135
136
137
138 struct quad *find_quad(struct quad *quads,int number_quads,int ix,int iy) {
139 double x,y;
140 x=ix;y=iy;
141 int i;
142 for (i=0;i<number_quads;i++) {
143 if (some_inside(x,y,quads+i)) {
144 return quads+i;
145 }
146 }
147 return NULL;
148 }
149
150
151
152
153 int file_number_from_quad(struct quad *q) {
154 int x,y;
155 return 0; /* always the one image */
156 }
157
158
159
160
161 int main(int argc,char **argv)
162 {
163 char *square_file=argv[1];
164 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