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

Annotation of /eyes/compute_cyclone_averages.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (hide annotations)
Thu Mar 8 15:49:21 2012 UTC (7 years, 11 months ago) by hib
Branch: MAIN
CVS Tags: HEAD
File MIME type: text/plain
more cleanup for girl3 (Jenni) and cyclone is still in process

1 hib 1.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    
11     /* This thing is real cool because it
12     Lets me circle an important area in a picture, and we will make this gradient
13     from 0 to 65536 which gives the gray scale gradients that allows us to "fit"
14     the original record based on its colors and size and stuff.
15     */
16    
17    
18    
19    
20     int bits[] = {0x80,0x40,0x20,0x10,0x8,0x4,0x2,0x1};
21    
22    
23    
24     int read_imagez(char *name,unsigned short **prbuf,ssize_t *pwidth,ssize_t *pheight,
25     unsigned short **prgbbuf)
26     {
27     char iname[1000];
28     char mname[1000];
29    
30    
31     MagickPixelPacket input_pixel;
32     MagickWand *input_wand = NULL;
33     PixelWand *c_wand = NULL;
34     PixelWand *c2_wand = NULL;
35     MagickBooleanType status;
36     PixelIterator *input_iterator;
37     PixelWand **input_pixels;
38     register ssize_t x;
39     ssize_t y;
40    
41    
42     /* read the input color */
43     sprintf(iname,"../i/%s",name);
44     input_wand =NewMagickWand();
45     MagickReadImage(input_wand,iname);
46     ssize_t width,height;
47     width = MagickGetImageWidth(input_wand);
48     height = MagickGetImageHeight(input_wand);
49     int number_of_points;
50     number_of_points=0;
51     unsigned short *rbuf;
52     unsigned short *rgbbuf;
53    
54     rgbbuf= (unsigned short *)malloc(width*height*sizeof(unsigned short)*3);
55     if (!rgbbuf) {fprintf(stderr,"memory\n"); exit(-1);}
56    
57     #define ThrowWandException(wand) \
58     { \
59     char \
60     *description; \
61     \
62     ExceptionType \
63     severity; \
64     \
65     description=MagickGetException(wand,&severity); \
66     (void) fprintf(stderr,"%s %s %lu %s\n",GetMagickModule(),description); \
67     description=(char *) MagickRelinquishMemory(description); \
68     exit(-1); \
69     }
70    
71    
72     input_iterator=NewPixelIterator(input_wand);
73     if ((input_iterator == (PixelIterator *) NULL) )
74     ThrowWandException(input_wand);
75    
76     for (y=0; y < (ssize_t) height; y++)
77     {
78     input_pixels=PixelGetNextIteratorRow(input_iterator,&width);
79     if ((input_pixels == (PixelWand **) NULL))
80     break;
81     for (x=0; x < (ssize_t) width; x++)
82     {
83     unsigned short *rl;
84     PixelGetMagickColor(input_pixels[x],&input_pixel);
85     rl=rgbbuf + ((y*width)+x)*3;
86     *(rl++) = input_pixel.red;
87     *(rl++) = input_pixel.green;
88     *(rl++) = input_pixel.blue;
89     }
90     }
91     if (y < (ssize_t) MagickGetImageHeight(input_wand))
92     ThrowWandException(input_wand);
93     input_iterator=DestroyPixelIterator(input_iterator);
94     input_wand=DestroyMagickWand(input_wand);
95    
96    
97    
98    
99     /* read the gery mask */
100     sprintf(mname,"../k/%s",name);
101     input_wand =NewMagickWand();
102     MagickReadImage(input_wand,mname);
103     {
104     int width2;
105     int height2;
106     width2 = MagickGetImageWidth(input_wand);
107     height2 = MagickGetImageHeight(input_wand);
108     if ((width2 != width)||(height2 != height)) {
109     fprintf(stderr,"reading mismatch for %s: i has %d,%d, k has %d,%d\n",name,
110     (int)(width),(int)(height),width2,height2);
111     exit(-1);
112     }
113     }
114    
115     rbuf= (unsigned short *)malloc(width*height*sizeof(unsigned short));
116     if (!rbuf) {fprintf(stderr,"memory\n"); exit(-1);}
117    
118    
119     input_iterator=NewPixelIterator(input_wand);
120     if ((input_iterator == (PixelIterator *) NULL) )
121     ThrowWandException(input_wand);
122    
123     for (y=0; y < (ssize_t) height; y++)
124     {
125     input_pixels=PixelGetNextIteratorRow(input_iterator,&width);
126     if ((input_pixels == (PixelWand **) NULL))
127     break;
128     for (x=0; x < (ssize_t) width; x++)
129     {
130     unsigned short *rl;
131     PixelGetMagickColor(input_pixels[x],&input_pixel);
132     rl=rbuf + ((y*width)+x);
133     int (val) = (input_pixel.red + input_pixel.green + input_pixel.blue) / 3.;
134     *(rl) = val;
135     }
136     }
137     input_iterator=DestroyPixelIterator(input_iterator);
138     input_wand=DestroyMagickWand(input_wand);
139    
140    
141    
142    
143     *prgbbuf = rgbbuf;
144     *prbuf = rbuf;
145     *pwidth = width;
146     *pheight=height;
147    
148    
149    
150     return (0);
151     }
152    
153    
154    
155     void make_stack_really_big() {
156     const rlim_t stacksize = 2000*1024*1024; // 2 gigabytes
157     struct rlimit rl;
158     int result;
159     result = getrlimit(RLIMIT_STACK, &rl);
160     if (result == 0)
161     {
162     if (rl.rlim_cur < stacksize)
163     {
164     rl.rlim_cur = stacksize;
165     result = setrlimit(RLIMIT_STACK, &rl);
166     if (result != 0)
167     {
168     fprintf(stderr, "setrlimit returned result = %d\n", result);
169     exit(-1);
170     }
171     }
172     }
173     else {
174     fprintf(stderr, "getrlimit returned result = %d\n", result);
175     exit(-1);
176     }
177     }
178    
179    
180    
181     int main(int argc,char **argv)
182     {
183     /* set the stack limit to 500 megabytes */
184     make_stack_really_big();
185    
186     /* seed1 ip_Address fname */
187     char *iname=argv[1];
188     char *oname=argv[2];
189    
190     unsigned char *otherrand;
191     int other;
192     unsigned short *rbuf;
193     unsigned short *rgbbuf;
194     int number_of_points;
195     int number_second_points;
196     struct point *points_to_look_at;
197     struct point *second_points_to_look_at;
198     struct point *end_points_to_look_at;
199     struct point *temp_points;
200     int number_end_points;
201     MagickWand *m_wand = NULL;
202     PixelWand *c_wand = NULL;
203     PixelWand *c2_wand = NULL;
204     MagickPixelPacket pixel;
205    
206     MagickWandGenesis();
207     ssize_t width,height;
208    
209     read_imagez(iname,&rbuf,&width,&height,&rgbbuf);
210    
211     int i;
212     for (i=1;i<256;i++) {
213     unsigned int from,to;
214     from = i*256;
215     to = i*256+255;
216    
217     int x,y;
218     double row_rtally;
219     double row_gtally;
220     double row_btally;
221     double column_rtally;
222     double column_gtally;
223     double column_btally;
224     double row_size;
225     double column_size;
226     row_rtally = 0.;
227     row_gtally = 0.;
228     row_btally = 0.;
229     row_size = 0.;
230    
231     for (y=0;y<height;y++) {
232     column_rtally = 0.;
233     column_gtally = 0.;
234     column_btally = 0.;
235     column_size = 0.;
236     for (x=0;x<width;x++) {
237     unsigned short *rgbp = rgbbuf+((y*width)+x)*3;
238     unsigned short r = rbuf[y*width+x];
239     if (r>=from) { /* if we are part at this level */
240     column_size += 1.;
241     column_rtally += *(rgbp++);
242     column_gtally += *(rgbp++);
243     column_btally += *(rgbp++);
244     }
245     }
246     row_size += column_size;
247     row_rtally += column_rtally;
248     row_gtally += column_gtally;
249     row_btally += column_btally;
250     }
251     double average_r;
252     double average_g;
253     double average_b;
254     if (row_size != 0.) {
255     average_r = (row_rtally / row_size);
256     average_g = (row_gtally / row_size);
257     average_b = (row_btally / row_size);
258     unsigned short r,g,b;
259     r = (short) (average_r / 256.);
260     g = (short) (average_g / 256.);
261     b = (short) (average_b / 256.);
262    
263     fprintf(stderr,"%s|%d|%d|%2.2x|%2.2x|%2.2x|%3.3d\n",argv[1],(int)(width),(int)(height),r,g,b,i);
264    
265     if (argc>2) {
266     char oname[1000];
267     sprintf(oname,"../see/%s_%3.3d.png",argv[1],i);
268     PixelIterator *iterator;
269     PixelWand **pixels;
270     m_wand = NewMagickWand();
271     c_wand = NewPixelWand();
272     PixelSetColor(c_wand,"black"); /* for now */
273     MagickNewImage(m_wand,width,height,c_wand);
274     MagickSetImageChannelDepth(m_wand, AllChannels, 16);
275     MagickSetImageDepth(m_wand, 16);
276    
277     iterator=NewPixelIterator(m_wand);
278     if ((iterator == (PixelIterator *) NULL) )
279     ThrowWandException(m_wand);
280    
281    
282     register ssize_t x;
283     ssize_t y;
284     for (y=0; y < (ssize_t) height; y++) {
285     pixels=PixelGetNextIteratorRow(iterator,&width);
286     if ((pixels == (PixelWand **) NULL))
287     break;
288     for (x=0; x < (ssize_t) width; x++) {
289     unsigned short *rgbp = rgbbuf+((y*width)+x)*3;
290     unsigned short r = rbuf[y*width+x];
291     PixelGetMagickColor(pixels[x],&pixel);
292     if (r>=from) { /* if we are part at this level */
293     pixel.red= *(rgbp++);
294     pixel.green= *(rgbp++);
295     pixel.blue= *(rgbp++);
296     }
297     else {
298     pixel.red = average_r;
299     pixel.green = average_g;
300     pixel.blue = average_b;
301     }
302     PixelSetMagickColor(pixels[x],&pixel);
303     }
304     (void) PixelSyncIterator(iterator);
305     }
306     if (y < (ssize_t) MagickGetImageHeight(m_wand))
307     ThrowWandException(m_wand);
308     iterator=DestroyPixelIterator(iterator);
309    
310    
311     /*
312     Write the image then destroy it.
313     */
314     MagickBooleanType status;
315     status=MagickWriteImages(m_wand,oname,MagickTrue);
316     if (status == MagickFalse)
317     ThrowWandException(m_wand);
318     m_wand=DestroyMagickWand(m_wand);
319     } /* if we are showing the pics */
320     } /* if there is any image at all */
321     } /* for all possible images 1 to 255 */
322    
323     MagickWandTerminus();
324     return(0);
325     }
326    

  ViewVC Help
Powered by ViewVC 1.1.26