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

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

~ [ 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.