~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Tina5/tina-tools/tinatool/tlbase/tlbaseColour_tool.c

Version: ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /**********
  2  *
  3  * Copyright (c) 2003, Division of Imaging Science and Biomedical Engineering,
  4  * University of Manchester, UK.  All rights reserved.
  5  *
  6  * Redistribution and use in source and binary forms, with or without modification,
  7  * are permitted provided that the following conditions are met:
  8  *
  9  *   . Redistributions of source code must retain the above copyright notice,
 10  *     this list of conditions and the following disclaimer.
 11  *
 12  *   . Redistributions in binary form must reproduce the above copyright notice,
 13  *     this list of conditions and the following disclaimer in the documentation
 14  *     and/or other materials provided with the distribution.
 15  *
 16  *   . Neither the name of the University of Manchester nor the names of its
 17  *     contributors may be used to endorse or promote products derived from this
 18  *     software without specific prior written permission.
 19  *
 20  *
 21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 31  * POSSIBILITY OF SUCH DAMAGE.
 32  *
 33  **********
 34  *
 35  * Program :    TINA
 36  * File    :
 37  * Date    :
 38  * Version :
 39  * CVS Id  :
 40  *
 41  * Notes : 
 42  *
 43  * Colour vision tools: input from PPM, conversion to different colour spaces, 
 44  * and colour segmentation: see Tina Memo no. 2001-015. IJK colour space is my 
 45  * own invention: see the Tina memo.
 46  *
 47  *********
 48 */
 49 
 50 #include "tlbaseColour_tool.h"
 51 
 52 #if HAVE_CONFIG_H
 53 #    include <config.h>
 54 #endif
 55 
 56 #include <math.h>
 57 
 58 #include<tina/sys/sysDef.h>
 59 #include<tina/file/filePro.h>
 60 #include<tina/image/imgPro.h>
 61 #include<tina/image/imgDef.h>
 62 
 63 #include <tinatool/draw/drawDef.h>
 64 #include <tinatool/draw/drawPro.h>
 65 #include <tinatool/gphx/gphxDef.h>
 66 #include <tinatool/gphx/gphxPro.h>
 67 #include <tinatool/wdgts/wdgtsDef.h>
 68 #include <tinatool/wdgts/wdgtsPro.h>
 69 #include <tinatool/tlbase/tlbasePro.h>
 70 #include <tinatool/tlbase/tlbaseColour_view.h>
 71 
 72 #define Bit(mask,num) (mask&(1<<num))
 73 
 74 static char directory_name[1024];
 75 static char file_name[1024];
 76 static char pathname[1024];
 77 static void *pdir=0, *pfile=0;
 78 static void ***covar_matrix=NULL;
 79 
 80 static int rgb_mask=7, hsi_mask=0, yiq_mask=0, ijk_mask=0;
 81 static double threshold=3.0;
 82 static int label_select=1;
 83 
 84 
 85 /* Functions for the Tvs */
 86 
 87 
 88 static void col_tv_draw(Tv *tv)
 89 {
 90         if(tv!=NULL)
 91         {
 92                 tv_free_background(tv);
 93                 tv_repaint(tv);
 94         }
 95 }
 96 
 97 
 98 static void col_tv_proc(void)
 99 {
100         col_tv_red_set(col_tv_red_make());
101         col_tv_green_set(col_tv_green_make());
102         col_tv_blue_set(col_tv_blue_make());
103         tv_set_next(col_tv_red_get());
104 }
105 
106 
107 static void tv_choice_proc(int val)
108 {
109         if(val==0)
110         {
111                 tv_set_next(col_tv_red_get());
112         }
113         else if(val==1)
114         {
115                 tv_set_next(col_tv_green_get());
116         }
117         else
118         {
119                 tv_set_next(col_tv_blue_get());
120         }
121 }
122 
123 
124 /* Function to input pnm images */
125 
126 
127 static void noise_measure(Imrect *im, float *noise)
128 {
129         Imregion *roi=NULL;
130         float sigma_x, sigma_y;
131 
132         roi = im->region;
133         
134         sigma_x = imf_diffx_noise(im, roi);
135         sigma_y = imf_diffy_noise(im, roi);
136         *noise = (sigma_x + sigma_y)/2;
137 }
138 
139 
140 static void covar_matrix_free(void)
141 {
142         Imrect *im_red=NULL;
143         Imregion *roi=NULL;
144         int i, j;
145 
146         /* Free the old covar matrix on any input */
147         
148         if(covar_matrix!=NULL)
149         {
150                 im_red = col_im_red_get();
151                 roi = im_red->region;
152                 for(i=roi->ly; i<roi->uy; i++)
153                 {
154                         for(j=roi->lx; j<roi->ux; j++)
155                         {
156                                 pvector_free(covar_matrix[i][j], 0);
157                         }
158                 }
159                 parray_free(covar_matrix, roi->ly, roi->lx, roi->uy, roi->ux);
160                 covar_matrix=NULL;
161         }
162 }
163 
164 
165 static void covar_matrix_fill(void)
166 {
167         Imrect *im_red=NULL, *im_green=NULL, *im_blue=NULL;
168         Imregion *roi=NULL;
169         float sigma_r, sigma_g, sigma_b, *covar_row=NULL;
170         int i, j;
171         
172         im_red = col_im_red_get();
173         im_green = col_im_green_get();
174         im_blue = col_im_blue_get();
175         
176         if(im_red!=NULL&&im_green!=NULL&&im_blue!=NULL)
177         {
178                 noise_measure(im_red, &sigma_r);
179                 noise_measure(im_green, &sigma_g);
180                 noise_measure(im_blue, &sigma_b);
181 
182                 roi = im_red->region;
183                 covar_matrix = parray_alloc(roi->ly, roi->lx, roi->uy, roi->ux);
184                 {
185                         for(i=roi->ly; i<roi->uy; i++)
186                         {
187                                 for(j=roi->lx; j<roi->ux; j++)
188                                 {
189                                         covar_row = fvector_alloc(0, 3);
190                                         covar_row[0] = sigma_r;
191                                         covar_row[1] = sigma_g;
192                                         covar_row[2] = sigma_b;
193                                         covar_matrix[i][j] = covar_row;
194                                 }
195                         }
196                 }
197         }
198 }
199 
200 
201 static void ppm_input_proc(void)
202 {
203         void **image_vector=NULL;
204 
205         (void) strip_spaces(file_name);
206         (void) strip_spaces(directory_name);
207         (void) string_append(pathname, directory_name, "/", file_name, NULL);   
208         
209         image_vector = ppm_read_image(pathname);
210         if(image_vector==NULL) 
211         {
212                 return;
213         }
214         else
215         {
216                 covar_matrix_free();
217         }
218         
219         col_im_red_set((Imrect *)image_vector[0]);
220         col_im_green_set((Imrect *)image_vector[1]);
221         col_im_blue_set((Imrect *)image_vector[2]);
222         
223         col_tv_draw(col_tv_red_get());
224         col_tv_draw(col_tv_green_get());
225         col_tv_draw(col_tv_blue_get());
226         
227         if(col_tv_red_get()!=NULL) tv_init(col_tv_red_get());
228         if(col_tv_green_get()!=NULL) tv_init(col_tv_green_get());
229         if(col_tv_blue_get()!=NULL) tv_init(col_tv_blue_get());
230         
231         covar_matrix_fill();
232 }
233 
234 
235 static void colour_clear_proc(void)
236 {
237         /* Delete the images and covar matrix */
238 
239         covar_matrix_free();
240         col_im_red_set(NULL);
241         col_im_green_set(NULL);
242         col_im_blue_set(NULL);
243         
244         if(col_tv_red_get()!=NULL) col_tv_draw(col_tv_red_get());
245         if(col_tv_green_get()!=NULL) col_tv_draw(col_tv_green_get());
246         if(col_tv_blue_get()!=NULL) col_tv_draw(col_tv_blue_get());
247 }
248 
249 
250 static void ppm_output(Imrect *im_red, Imrect *im_green, Imrect *im_blue)
251 {
252         void **image_vector=NULL;
253 
254         (void) strip_spaces(file_name);
255         (void) strip_spaces(directory_name);
256         (void) string_append(pathname, directory_name, "/", file_name, NULL);   
257         
258         image_vector = pvector_alloc(0, 3);
259         image_vector[0] = im_red;
260         image_vector[1] = im_green;
261         image_vector[2] = im_blue;
262         
263         ppm_write_image(image_vector, pathname);
264         pvector_free(image_vector, 0);
265 }
266 
267 static void ppm_output_proc(void)
268 {
269         Imrect *im_red=NULL, *im_green=NULL, *im_blue=NULL;
270         
271         im_red = col_im_red_get();
272         im_green = col_im_green_get();
273         im_blue = col_im_blue_get();
274         
275         ppm_output(im_red, im_green, im_blue);
276 }
277 
278 
279 /* Colour segmentation and manipulation functions */
280 
281 
282 static void rgb_normalise_proc(void)
283 {
284         Imrect *im_r=NULL, *im_g=NULL, *im_b=NULL;
285         void **image_vector=NULL, ***covar_matrix_out=NULL;
286 
287         im_r = col_im_red_get();
288         im_g = col_im_green_get();
289         im_b = col_im_blue_get();
290 
291         if(im_r==NULL||im_g==NULL||im_b==NULL)
292         {
293                 error("No RGB images to normalise\n", warning);
294                 return;
295         }
296 
297         image_vector = rgb_normalise(im_r, im_g, im_b, covar_matrix);
298 
299         im_r = image_vector[0];
300         im_g = image_vector[1];
301         im_b = image_vector[2];
302         covar_matrix_out = image_vector[3];
303         col_im_red_set(im_r);
304         col_im_green_set(im_g);
305         col_im_blue_set(im_b);
306 
307         covar_matrix_free();
308         covar_matrix = covar_matrix_out;
309 
310         col_tv_draw(col_tv_red_get());
311         col_tv_draw(col_tv_green_get());
312         col_tv_draw(col_tv_blue_get());
313         
314         if(col_tv_red_get()!=NULL) tv_init(col_tv_red_get());
315         if(col_tv_green_get()!=NULL) tv_init(col_tv_green_get());
316         if(col_tv_blue_get()!=NULL) tv_init(col_tv_blue_get());
317 }
318 
319 
320 static void density_list_overlayer(Imrect *im_j, Imrect *im_k, List *density_list)
321 {
322         /* function to overlayer the peaks and linking onto the density list image */
323 
324         List *plist;
325         Match *mptr, *nptr;
326         float *data1, *data2;
327         float y_offset=0, x_offset=0, x_scale=1, y_scale=1;
328         float imj_min, imj_max, imk_min, imk_max;
329         Imrect *imk_scale=NULL, *imj_scale=NULL, *imjk_cmplx=NULL, *imjk_scat=NULL; 
330         Imrect *imjk_gauss=NULL, *imjk_sqrt=NULL;
331         Tv *tv = imcalc_tv_get();
332         int i;
333         double sigma=1.0, range = 5.0, acc;
334         
335         acc = exp(-range*range/(2.0*sigma*sigma));
336         
337         imf_minmax(im_j, &imj_min, &imj_max);
338         imf_minmax(im_k, &imk_min, &imk_max);
339         x_offset = -imj_min;
340         y_offset = -imk_min;
341         x_scale = 256.0/(imj_max-imj_min);
342         y_scale = 256.0/(imk_max-imk_min);
343         
344         imj_scale = imf_scale(im_j, 0.0, 256.0);
345         imk_scale = imf_scale(im_k, 0.0, 256.0);
346         imjk_cmplx = imz_cmplx(imj_scale, imk_scale);
347         im_free(imj_scale);
348         im_free(imk_scale);
349         imjk_scat = imz_scat(imjk_cmplx, NULL, 256.0);
350         im_free(imjk_cmplx);
351         imjk_gauss = imf_gauss(imjk_scat, sigma, acc);
352         im_free(imjk_scat);
353         for(i=0; i<2; i++)
354         {
355                 imjk_sqrt = imf_sqrt(imjk_gauss);
356                 im_free(imjk_gauss);
357                 imjk_gauss = imjk_sqrt;
358         }
359         
360         stack_push(imjk_gauss, IMRECT, im_free);
361         if(imcalc_tv_get()!=NULL) tv_init(imcalc_tv_get());
362         if(imcal2_tv_get()!=NULL) tv_init(imcal2_tv_get());
363         
364         for(plist=density_list; plist!=NULL; plist=plist->next)
365         {
366                 mptr = plist->to;
367                 data1 = mptr->to1;
368                 if(mptr->to2==NULL)
369                 {
370                         tv_set_color(tv, baby_blue);
371                         tv_dotxy(tv, (int)(x_scale*(x_offset+data1[0])),
372                          (int)(256.0-y_scale*(y_offset+data1[1])));
373                 }
374                 else
375                 {
376                         tv_set_color(tv, red);
377                         nptr = mptr->to2;
378                         data2 = nptr->to1;
379                         tv_dotxy(tv, (int)(x_scale*(x_offset+data1[0])),
380                          (int)(256.0-y_scale*(y_offset+data1[1])));
381                         tv_linexy(tv, (int)(x_scale*(x_offset+data1[0])),
382                           (int)(256.0-y_scale*(y_offset+data1[1])),
383                           (int)(x_scale*(x_offset+data2[0])),
384                           (int)(256.0-y_scale*(y_offset+data2[1])));
385                 }
386         }
387         tv_reset_draw(tv);
388 }
389 
390 
391 static void colour_segment_proc(void)
392 {
393         Imrect *im_r=NULL, *im_g=NULL, *im_b=NULL, *im_i=NULL, *im_j=NULL, *im_k=NULL, *im_seg=NULL;
394         Imregion *roi=NULL;
395         void ***covar_matrix_ijk=NULL;
396         int i, j;
397         void **image_vector=NULL;
398         Match *return_match=NULL;
399         List *density_list=NULL;
400 
401         im_r = col_im_red_get();
402         im_g = col_im_green_get();
403         im_b = col_im_blue_get();
404         if(im_r==NULL||im_g==NULL||im_b==NULL)
405         {
406                 error("No RGB imges to segment\n", warning);
407                 return;
408         }
409 
410         image_vector = ijk_convert(im_r, im_g, im_b, covar_matrix);
411         im_i = image_vector[0];
412         im_j = image_vector[1];
413         im_k = image_vector[2];
414         im_free(im_i);
415         covar_matrix_ijk = image_vector[3];
416         pvector_free(image_vector, 0);
417 
418         /*im_seg = colour_segment(im_j, im_k, covar_matrix_ijk, threshold); */
419         return_match = colour_segment(im_j, im_k, covar_matrix_ijk, threshold);
420 
421         if(return_match!=NULL)
422         {
423                 im_seg = (Imrect *)return_match->to1;
424                 density_list = (List *)return_match->to2;
425                 
426                 stack_push(im_seg, IMRECT, im_free);
427                 density_list_overlayer(im_j, im_k, density_list);
428                 density_list = knn_destroy(density_list);
429                 match_free(return_match);
430         }
431 
432         im_r = col_im_red_get();
433         roi = im_r->region;
434         for(i=roi->ly; i<roi->uy; i++)
435         {
436                 for(j=roi->lx; j<roi->ux; j++)
437                 {
438                         pvector_free(covar_matrix_ijk[i][j], 0);
439                 }
440         }
441         parray_free(covar_matrix_ijk, roi->ly, roi->lx, roi->uy, roi->ux);
442         im_free(im_j);
443         im_free(im_k);
444 }
445 
446 
447 static void label_select_proc(void)
448 {
449         /* Pick out single values in images, setting everything else to zero */
450 
451         Imrect *im1, *im2;
452         Imregion *roi;
453         int type, i, j, number=0;
454         float pix, lower_threshold, upper_threshold;
455 
456         im1 = (Imrect *)stack_pop(&type);
457         if(im1==NULL) return;
458 
459         roi = im1->region;
460         im2 = im_alloc((roi->uy-roi->ly), (roi->ux-roi->lx), (Imregion *)NULL, float_v);
461 
462         lower_threshold = (float)label_select-0.5;
463         upper_threshold = (float)label_select+0.5;
464 
465         for(i=(roi->ly); i<(roi->uy); i++)
466         {
467                 for(j=(roi->lx); j<(roi->ux); j++)
468                 {
469                         pix = im_get_pixf(im1, i, j);
470                         if(pix<lower_threshold||pix>upper_threshold)
471                         {
472                                 im_put_pixf(0.0, im2, i, j);
473                         }
474                         else
475                         {
476                                 im_put_pixf(1.0, im2, i, j);
477                                 number++;
478                         }
479                 }
480         }
481         stack_push(im2, IMRECT, im_free);
482         stack_push(im1, IMRECT, im_free);
483         if(imcalc_tv_get()!=NULL) tv_init(imcalc_tv_get());
484         if(imcal2_tv_get()!=NULL) tv_init(imcal2_tv_get());
485         format("The number of pixels chosen is %d\n", number);
486 }
487 
488 
489 static void mean_col_output_proc(void)
490 {
491         Imrect *im=NULL, *seg_im=NULL, *mean_im_r=NULL, *mean_im_g=NULL;
492         Imrect *mean_im_b=NULL, *im_r=NULL, *im_g=NULL, *im_b=NULL;
493         Imregion *roi=NULL;
494         int type, imax, pix_seg, i, j;
495         float min, max, **label_vec=NULL;
496         
497         im = (Imrect *)stack_pop(&type);
498         im_r = col_im_red_get();
499         im_g = col_im_green_get();
500         im_b = col_im_blue_get();
501         if(im==NULL||im_r==NULL||im_g==NULL||im_b==NULL) return;
502         
503         seg_im = im_cast(im, uchar_v);
504         stack_push(im, IMRECT, im_free);
505         roi = seg_im->region;
506         mean_im_r = im_alloc((roi->uy-roi->ly), (roi->ux-roi->lx), roi, float_v);
507         mean_im_g = im_alloc((roi->uy-roi->ly), (roi->ux-roi->lx), roi, float_v);
508         mean_im_b = im_alloc((roi->uy-roi->ly), (roi->ux-roi->lx), roi, float_v);
509 
510         imf_minmax(seg_im, &min, &max);
511         imax = (int)max+1;
512         label_vec = farray_alloc(0, 0, imax, 4);
513         
514         for(i=0; i<imax; i++)
515         {
516                 for(j=0; j<4; j++)
517                 {
518                         label_vec[i][j]=0.0;
519                 }
520         }
521         
522         for(i=roi->ly; i<roi->uy; i++)
523         {
524                 for(j=roi->lx; j<roi->ux; j++)
525                 {
526                         pix_seg = im_get_pix(seg_im, i, j);
527                         label_vec[pix_seg][0]+=im_get_pixf(im_r, i, j);
528                         label_vec[pix_seg][1]+=im_get_pixf(im_g, i, j);
529                         label_vec[pix_seg][2]+=im_get_pixf(im_b, i, j);
530                         label_vec[pix_seg][3]++;
531                 }
532         }
533         
534         for(i=0; i<imax; i++)
535         {
536                 for(j=0; j<3; j++)
537                 {
538                         label_vec[i][j]=label_vec[i][j]/label_vec[i][3];
539                 }
540         }
541         
542         for(i=roi->ly; i<roi->uy; i++)
543         {
544                 for(j=roi->lx; j<roi->ux; j++)
545                 {
546                         pix_seg = im_get_pix(seg_im, i, j);
547                         im_put_pixf(label_vec[pix_seg][0], mean_im_r, i, j);
548                         im_put_pixf(label_vec[pix_seg][1], mean_im_g, i, j);
549                         im_put_pixf(label_vec[pix_seg][2], mean_im_b, i, j);
550                 }
551         }
552         
553         ppm_output(mean_im_r, mean_im_g, mean_im_b);
554         im_free(mean_im_r);
555         im_free(mean_im_g);
556         im_free(mean_im_b);
557         im_free(seg_im);
558         farray_free(label_vec, 0, 0, imax, 4);
559 }
560 
561 
562 /* Functions to pop RGB fields from the stack */
563 
564 
565 static void r_pop_proc(void)
566 {
567         Imrect *im_r=NULL, *im_g=NULL, *im_b=NULL;
568         Imregion *roi_r=NULL, *roi_g=NULL, *roi_b=NULL;
569         int type;
570         
571         im_r = (Imrect *)stack_pop(&type);
572         if(im_r==NULL)
573         {
574                 return;
575         }
576         roi_r = im_r->region;
577         
578         im_g = col_im_green_get();
579         im_b = col_im_blue_get();
580 
581         /* Check that the fields are the same size, if they exist */
582         
583         if(im_g!=NULL)
584         {
585                 roi_g = im_g->region;
586                 
587                 if((roi_r->ly!=roi_g->ly)||(roi_r->uy!=roi_g->uy)||(roi_r->lx!=roi_g->lx)
588                 ||(roi_r->ux!=roi_g->ux))
589                 {
590                         error("Images must be the same size\n", warning);
591                         stack_push(im_r, IMRECT, im_free);
592                         return;
593                 }
594         }
595         if(im_b!=NULL)
596         {
597                 roi_b = im_b->region;
598 
599                 if((roi_r->ly!=roi_b->ly)||(roi_r->uy!=roi_b->uy)||(roi_r->lx!=roi_b->lx)
600                 ||(roi_r->ux!=roi_b->ux))
601                 {
602                         error("Images must be the same size\n", warning);
603                         stack_push(im_r, IMRECT, im_free);
604                         return;
605                 }
606         }
607         covar_matrix_free();
608         col_im_red_set(im_cast(im_r, float_v));
609         covar_matrix_fill();
610         
611         col_tv_draw(col_tv_red_get());
612         col_tv_draw(col_tv_green_get());
613         col_tv_draw(col_tv_blue_get());
614         
615         if(col_tv_red_get()!=NULL) tv_init(col_tv_red_get());
616         if(col_tv_green_get()!=NULL) tv_init(col_tv_green_get());
617         if(col_tv_blue_get()!=NULL) tv_init(col_tv_blue_get());
618         
619         imcalc_draw(imcalc_tv_get());
620         imcalc_draw(imcal2_tv_get());
621 }
622 
623 
624 static void g_pop_proc(void)
625 {
626         Imrect *im_r=NULL, *im_g=NULL, *im_b=NULL;
627         Imregion *roi_r=NULL, *roi_g=NULL, *roi_b=NULL;
628         int type;
629         
630         im_g = (Imrect *)stack_pop(&type);
631         if(im_g==NULL)
632         {
633                 return;
634         }
635         roi_g = im_g->region;
636         
637         im_r = col_im_red_get();
638         im_b = col_im_blue_get();
639         
640         /* Check that the fields are the same size, if they exist */
641         
642         if(im_r!=NULL)
643         {
644                 roi_r = im_r->region;
645                 
646                 if((roi_r->ly!=roi_g->ly)||(roi_r->uy!=roi_g->uy)||
647                 (roi_r->lx!=roi_g->lx)||(roi_r->ux!=roi_g->ux))
648                 {
649                         error("Images must be the same size\n", warning);
650                         stack_push(im_g, IMRECT, im_free);
651                         return;
652                 }
653         }
654         if(im_b!=NULL)
655         {
656                 roi_b = im_b->region;
657 
658                 if((roi_g->ly!=roi_b->ly)||(roi_g->uy!=roi_b->uy)||
659                 (roi_g->lx!=roi_b->lx)||(roi_g->ux!=roi_b->ux))
660                 {
661                         error("Images must be the same size\n", warning);
662                         stack_push(im_g, IMRECT, im_free);
663                         return;
664                 }
665         }
666         covar_matrix_free();
667         col_im_green_set(im_cast(im_g, float_v));
668         covar_matrix_fill();
669         
670         col_tv_draw(col_tv_red_get());
671         col_tv_draw(col_tv_green_get());
672         col_tv_draw(col_tv_blue_get());
673         
674         if(col_tv_red_get()!=NULL) tv_init(col_tv_red_get());
675         if(col_tv_green_get()!=NULL) tv_init(col_tv_green_get());
676         if(col_tv_blue_get()!=NULL) tv_init(col_tv_blue_get());
677         
678         imcalc_draw(imcalc_tv_get());
679         imcalc_draw(imcal2_tv_get());
680 }
681 
682 
683 static void b_pop_proc(void)
684 {
685         Imrect *im_r=NULL, *im_g=NULL, *im_b=NULL;
686         Imregion *roi_r=NULL, *roi_g=NULL, *roi_b=NULL;
687         int type;
688         
689         im_b = (Imrect *)stack_pop(&type);
690         if(im_b==NULL)
691         {
692                 return;
693         }
694         roi_b = im_b->region;
695         
696         im_r = col_im_red_get();
697         im_g = col_im_green_get();
698         
699         /* Check that the fields are the same size, if they exist */
700         
701         if(im_g!=NULL)
702         {
703                 roi_g = im_g->region;
704                 
705                 if((roi_b->ly!=roi_g->ly)||(roi_b->uy!=roi_g->uy)||
706                 (roi_b->lx!=roi_g->lx)||(roi_b->ux!=roi_g->ux))
707                 {
708                         error("Images must be the same size\n", warning);
709                         stack_push(im_b, IMRECT, im_free);
710                         return;
711                 }
712         }
713         if(im_r!=NULL)
714         {
715                 roi_r = im_r->region;
716 
717                 if((roi_r->ly!=roi_b->ly)||(roi_r->uy!=roi_b->uy)||
718                 (roi_r->lx!=roi_b->lx)||(roi_r->ux!=roi_b->ux))
719                 {
720                         error("Images must be the same size\n", warning);
721                         stack_push(im_b, IMRECT, im_free);
722                         return;
723                 }
724         }
725         covar_matrix_free();
726         col_im_blue_set(im_cast(im_b, float_v));
727         covar_matrix_fill();
728         
729         col_tv_draw(col_tv_red_get());
730         col_tv_draw(col_tv_green_get());
731         col_tv_draw(col_tv_blue_get());
732         
733         if(col_tv_red_get()!=NULL) tv_init(col_tv_red_get());
734         if(col_tv_green_get()!=NULL) tv_init(col_tv_green_get());
735         if(col_tv_blue_get()!=NULL) tv_init(col_tv_blue_get()); 
736 
737         imcalc_draw(imcalc_tv_get());
738         imcalc_draw(imcal2_tv_get());
739 }
740 
741 
742 /* Functions to push colour fields to the stack */
743 
744 
745 static void rgb_push_proc(void)
746 {
747         Imrect *im_red=NULL, *im_green=NULL, *im_blue=NULL;
748         
749         im_red = col_im_red_get();
750         im_green = col_im_green_get();
751         im_blue = col_im_blue_get();
752         
753         if(im_red==NULL||im_green==NULL||im_blue==NULL) 
754         {
755                 error("No colour image loaded\n", warning);
756                 return;
757         }
758         
759         if(Bit(rgb_mask, 2)) stack_push(im_copy(im_blue), IMRECT, im_free);
760         if(Bit(rgb_mask, 1)) stack_push(im_copy(im_green), IMRECT, im_free);
761         if(Bit(rgb_mask, 0)) stack_push(im_copy(im_red), IMRECT, im_free);
762         
763         if(imcalc_tv_get()!=NULL) tv_init(imcalc_tv_get());
764         if(imcal2_tv_get()!=NULL) tv_init(imcal2_tv_get());
765 }
766 
767 
768 static void hsi_push_proc(void)
769 {
770         Imrect *im_red=NULL, *im_green=NULL, *im_blue=NULL;
771         Imrect *im_h=NULL, *im_s=NULL, *im_i=NULL;
772         void **image_vector=NULL;
773         
774         im_red = col_im_red_get();
775         im_green = col_im_green_get();
776         im_blue = col_im_blue_get();
777 
778         if(im_red==NULL||im_green==NULL||im_blue==NULL) 
779         {
780                 error("No colour image loaded\n", warning);
781                 return;
782         }
783 
784         image_vector = hsi_convert(im_red, im_green, im_blue, NULL);
785         im_h = image_vector[0];
786         im_s = image_vector[1];
787         im_i = image_vector[2];
788         pvector_free(image_vector, 0);
789         
790         if(Bit(hsi_mask, 2))
791         {
792                 stack_push(im_i, IMRECT, im_free);
793         }
794         else
795         {
796                 im_free(im_i);
797         }
798         if(Bit(hsi_mask, 1))
799         {
800                 stack_push(im_s, IMRECT, im_free);
801         }
802         else
803         {
804                 im_free(im_s);
805         }
806         if(Bit(hsi_mask, 0))
807         {
808                 stack_push(im_h, IMRECT, im_free);
809         }
810         else
811         {
812                 im_free(im_h);
813         }
814         
815         if(imcalc_tv_get()!=NULL) tv_init(imcalc_tv_get());
816         if(imcal2_tv_get()!=NULL) tv_init(imcal2_tv_get());
817 }
818 
819 
820 static void yiq_push_proc(void)
821 {
822         Imrect *im_red=NULL, *im_green=NULL, *im_blue=NULL;
823         Imrect *im_y=NULL, *im_i=NULL, *im_q=NULL;
824         void **image_vector=NULL;
825         
826         im_red = col_im_red_get();
827         im_green = col_im_green_get();
828         im_blue = col_im_blue_get();
829         
830         if(im_red==NULL||im_green==NULL||im_blue==NULL) 
831         {
832                 error("No colour image loaded\n", warning);
833                 return;
834         }
835         
836         image_vector = yiq_convert(im_red, im_green, im_blue, NULL);
837         im_y = image_vector[0];
838         im_i = image_vector[1];
839         im_q = image_vector[2];
840         pvector_free(image_vector, 0);
841         
842         if(Bit(yiq_mask, 2)) 
843         {
844                 stack_push(im_q, IMRECT, im_free);
845         }
846         else
847         {
848                 im_free(im_q);
849         }
850         if(Bit(yiq_mask, 1))
851         {
852                 stack_push(im_i, IMRECT, im_free);
853         }
854         else
855         {
856                 im_free(im_i);
857         }
858         if(Bit(yiq_mask, 0))
859         {
860                 stack_push(im_y, IMRECT, im_free);
861         }
862         else
863         {
864                 im_free(im_y);
865         }
866         
867         if(imcalc_tv_get()!=NULL) tv_init(imcalc_tv_get());
868         if(imcal2_tv_get()!=NULL) tv_init(imcal2_tv_get());
869 }
870 
871 
872 static void ijk_push_proc(void)
873 {
874         Imrect *im_red=NULL, *im_green=NULL, *im_blue=NULL;
875         Imrect *im_i=NULL, *im_j=NULL, *im_k=NULL;
876         void **image_vector=NULL;
877 
878         im_red = col_im_red_get();
879         im_green = col_im_green_get();
880         im_blue = col_im_blue_get();
881         
882         if(im_red==NULL||im_green==NULL||im_blue==NULL) 
883         {
884                 error("No colour image loaded\n", warning);
885                 return;
886         }
887         
888         image_vector = ijk_convert(im_red, im_green, im_blue, NULL);
889         im_i = image_vector[0];
890         im_j = image_vector[1];
891         im_k = image_vector[2];
892         pvector_free(image_vector, 0);
893         
894         if(Bit(ijk_mask, 2))
895         {
896                 stack_push(im_k, IMRECT, im_free);
897         }
898         else
899         {
900                 im_free(im_k);
901         }
902         if(Bit(ijk_mask, 1))
903         {
904                 stack_push(im_j, IMRECT, im_free);
905         }
906         else
907         {
908                 im_free(im_j);
909         }
910         if(Bit(ijk_mask, 0))
911         {
912                 stack_push(im_i, IMRECT, im_free);
913         }
914         else
915         {
916                 im_free(im_i);
917         }
918         
919         if(imcalc_tv_get()!=NULL) tv_init(imcalc_tv_get());
920         if(imcal2_tv_get()!=NULL) tv_init(imcal2_tv_get());
921 }
922 
923 
924 static void rgbfield_check_proc(int val)
925 {
926         rgb_mask = val;
927 }
928 
929 
930 static void hsifield_check_proc(int val)
931 {
932         hsi_mask = val;
933 }
934 
935 
936 static void yiqfield_check_proc(int val)
937 {
938         yiq_mask = val;
939 }
940 
941 
942 static void ijkfield_check_proc(int val)
943 {
944         ijk_mask = val;
945 }
946 
947 
948 /* Main function */
949 
950 
951 void colour_tool(int x, int y)
952 {
953         static void *tool = NULL;
954         
955         if (tool)
956         {
957                 tw_show_tool(tool);
958                 return;
959         }
960 
961         col_tv_proc();
962         
963         tool = (void *)tw_tool("Colour Tool", x, y);
964 
965         tw_choice("Tv : ", tv_choice_proc, 0, "red", "green", "blue", NULL);
966 
967                
968         tw_newrow();
969         
970         pdir = (void *) tw_sglobal("Directory:", directory_name, 32);
971         tw_newrow();
972         pfile = (void*) tw_sglobal("File:", file_name, 32);
973         tw_newrow();
974         tw_button("Input PPM", ppm_input_proc, NULL);
975         tw_button("Output PPM", ppm_output_proc, NULL);
976         tw_newrow();
977         tw_button("R Pop", r_pop_proc, NULL);
978         tw_button("G Pop", g_pop_proc, NULL);
979         tw_button("B Pop", b_pop_proc, NULL);
980         tw_button("Clear All", colour_clear_proc, NULL);
981         tw_newrow();
982         tw_label("Colour space conversions:");
983         tw_newrow();
984         tw_check("To stack:    ", rgbfield_check_proc, rgb_mask, "R", "G", "B", NULL);
985         tw_label("   ");
986         tw_button("RGB Push", rgb_push_proc, NULL);
987         tw_newrow();
988         tw_check("To stack:    ", hsifield_check_proc, hsi_mask, "H", "S", "I", NULL);
989         tw_label("   ");
990         tw_button("HSI Push", hsi_push_proc, NULL);
991         tw_newrow();
992         tw_check("To stack:    ", yiqfield_check_proc, yiq_mask, "Y", "I", "Q", NULL);
993         tw_label("   ");
994         tw_button("YIQ Push", yiq_push_proc, NULL);
995         tw_newrow();
996         tw_check("To stack:    ", ijkfield_check_proc, ijk_mask, "I", "J", "K", NULL);
997         tw_label("   ");
998         tw_button("IJK Push", ijk_push_proc, NULL);
999         tw_newrow();
1000         tw_button("Normalise RGB", rgb_normalise_proc, NULL);
1001         tw_newrow();
1002         tw_label("Colour segmentation:");
1003         tw_newrow();
1004         tw_fglobal("Resolution (s.d.'s)", &threshold, 5);
1005         tw_button("Segment JK", colour_segment_proc, NULL);
1006         tw_newrow();
1007         tw_iglobal("Label: ", &label_select, 5);
1008         tw_button("Select", label_select_proc, NULL);
1009         tw_button("Mean Col Output", mean_col_output_proc, NULL);
1010         
1011         tw_end_tool();
1012 }
1013 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.